Search in sources :

Example 51 with ParcelFileDescriptor

use of android.os.ParcelFileDescriptor in project platform_frameworks_base by android.

the class Backup method doFullRestore.

private void doFullRestore(int socketFd) {
    // No arguments to restore
    ParcelFileDescriptor fd = null;
    try {
        fd = ParcelFileDescriptor.adoptFd(socketFd);
        mBackupManager.fullRestore(fd);
    } catch (RemoteException e) {
        Log.e(TAG, "Unable to invoke backup manager for restore");
    } finally {
        if (fd != null) {
            try {
                fd.close();
            } catch (IOException e) {
            }
        }
    }
}
Also used : ParcelFileDescriptor(android.os.ParcelFileDescriptor) IOException(java.io.IOException) RemoteException(android.os.RemoteException)

Example 52 with ParcelFileDescriptor

use of android.os.ParcelFileDescriptor in project platform_frameworks_base by android.

the class Am method openForSystemServer.

/**
     * Open the given file for sending into the system process. This verifies
     * with SELinux that the system will have access to the file.
     */
private static ParcelFileDescriptor openForSystemServer(File file, int mode) throws FileNotFoundException {
    final ParcelFileDescriptor fd = ParcelFileDescriptor.open(file, mode);
    final String tcon = SELinux.getFileContext(file.getAbsolutePath());
    if (!SELinux.checkSELinuxAccess("u:r:system_server:s0", tcon, "file", "read")) {
        throw new FileNotFoundException("System server has no access to file context " + tcon);
    }
    return fd;
}
Also used : ParcelFileDescriptor(android.os.ParcelFileDescriptor) FileNotFoundException(java.io.FileNotFoundException)

Example 53 with ParcelFileDescriptor

use of android.os.ParcelFileDescriptor in project platform_frameworks_base by android.

the class Am method runStart.

private void runStart() throws Exception {
    Intent intent = makeIntent(UserHandle.USER_CURRENT);
    if (mUserId == UserHandle.USER_ALL) {
        System.err.println("Error: Can't start service with user 'all'");
        return;
    }
    String mimeType = intent.getType();
    if (mimeType == null && intent.getData() != null && "content".equals(intent.getData().getScheme())) {
        mimeType = mAm.getProviderMimeType(intent.getData(), mUserId);
    }
    do {
        if (mStopOption) {
            String packageName;
            if (intent.getComponent() != null) {
                packageName = intent.getComponent().getPackageName();
            } else {
                List<ResolveInfo> activities = mPm.queryIntentActivities(intent, mimeType, 0, mUserId).getList();
                if (activities == null || activities.size() <= 0) {
                    System.err.println("Error: Intent does not match any activities: " + intent);
                    return;
                } else if (activities.size() > 1) {
                    System.err.println("Error: Intent matches multiple activities; can't stop: " + intent);
                    return;
                }
                packageName = activities.get(0).activityInfo.packageName;
            }
            System.out.println("Stopping: " + packageName);
            mAm.forceStopPackage(packageName, mUserId);
            Thread.sleep(250);
        }
        System.out.println("Starting: " + intent);
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        ParcelFileDescriptor fd = null;
        ProfilerInfo profilerInfo = null;
        if (mProfileFile != null) {
            try {
                fd = openForSystemServer(new File(mProfileFile), ParcelFileDescriptor.MODE_CREATE | ParcelFileDescriptor.MODE_TRUNCATE | ParcelFileDescriptor.MODE_WRITE_ONLY);
            } catch (FileNotFoundException e) {
                System.err.println("Error: Unable to open file: " + mProfileFile);
                System.err.println("Consider using a file under /data/local/tmp/");
                return;
            }
            profilerInfo = new ProfilerInfo(mProfileFile, fd, mSamplingInterval, mAutoStop, mStreaming);
        }
        IActivityManager.WaitResult result = null;
        int res;
        final long startTime = SystemClock.uptimeMillis();
        ActivityOptions options = null;
        if (mStackId != INVALID_STACK_ID) {
            options = ActivityOptions.makeBasic();
            options.setLaunchStackId(mStackId);
        }
        if (mWaitOption) {
            result = mAm.startActivityAndWait(null, null, intent, mimeType, null, null, 0, mStartFlags, profilerInfo, options != null ? options.toBundle() : null, mUserId);
            res = result.result;
        } else {
            res = mAm.startActivityAsUser(null, null, intent, mimeType, null, null, 0, mStartFlags, profilerInfo, options != null ? options.toBundle() : null, mUserId);
        }
        final long endTime = SystemClock.uptimeMillis();
        PrintStream out = mWaitOption ? System.out : System.err;
        boolean launched = false;
        switch(res) {
            case ActivityManager.START_SUCCESS:
                launched = true;
                break;
            case ActivityManager.START_SWITCHES_CANCELED:
                launched = true;
                out.println("Warning: Activity not started because the " + " current activity is being kept for the user.");
                break;
            case ActivityManager.START_DELIVERED_TO_TOP:
                launched = true;
                out.println("Warning: Activity not started, intent has " + "been delivered to currently running " + "top-most instance.");
                break;
            case ActivityManager.START_RETURN_INTENT_TO_CALLER:
                launched = true;
                out.println("Warning: Activity not started because intent " + "should be handled by the caller");
                break;
            case ActivityManager.START_TASK_TO_FRONT:
                launched = true;
                out.println("Warning: Activity not started, its current " + "task has been brought to the front");
                break;
            case ActivityManager.START_INTENT_NOT_RESOLVED:
                out.println("Error: Activity not started, unable to " + "resolve " + intent.toString());
                break;
            case ActivityManager.START_CLASS_NOT_FOUND:
                out.println(NO_CLASS_ERROR_CODE);
                out.println("Error: Activity class " + intent.getComponent().toShortString() + " does not exist.");
                break;
            case ActivityManager.START_FORWARD_AND_REQUEST_CONFLICT:
                out.println("Error: Activity not started, you requested to " + "both forward and receive its result");
                break;
            case ActivityManager.START_PERMISSION_DENIED:
                out.println("Error: Activity not started, you do not " + "have permission to access it.");
                break;
            case ActivityManager.START_NOT_VOICE_COMPATIBLE:
                out.println("Error: Activity not started, voice control not allowed for: " + intent);
                break;
            case ActivityManager.START_NOT_CURRENT_USER_ACTIVITY:
                out.println("Error: Not allowed to start background user activity" + " that shouldn't be displayed for all users.");
                break;
            default:
                out.println("Error: Activity not started, unknown error code " + res);
                break;
        }
        if (mWaitOption && launched) {
            if (result == null) {
                result = new IActivityManager.WaitResult();
                result.who = intent.getComponent();
            }
            System.out.println("Status: " + (result.timeout ? "timeout" : "ok"));
            if (result.who != null) {
                System.out.println("Activity: " + result.who.flattenToShortString());
            }
            if (result.thisTime >= 0) {
                System.out.println("ThisTime: " + result.thisTime);
            }
            if (result.totalTime >= 0) {
                System.out.println("TotalTime: " + result.totalTime);
            }
            System.out.println("WaitTime: " + (endTime - startTime));
            System.out.println("Complete");
        }
        mRepeat--;
        if (mRepeat > 0) {
            mAm.unhandledBack();
        }
    } while (mRepeat > 0);
}
Also used : ProfilerInfo(android.app.ProfilerInfo) PrintStream(java.io.PrintStream) FileNotFoundException(java.io.FileNotFoundException) Intent(android.content.Intent) ResolveInfo(android.content.pm.ResolveInfo) ParcelFileDescriptor(android.os.ParcelFileDescriptor) File(java.io.File) IActivityManager(android.app.IActivityManager) ActivityOptions(android.app.ActivityOptions)

Example 54 with ParcelFileDescriptor

use of android.os.ParcelFileDescriptor in project platform_frameworks_base by android.

the class Am method runProfile.

private void runProfile() throws Exception {
    String profileFile = null;
    boolean start = false;
    boolean wall = false;
    int userId = UserHandle.USER_CURRENT;
    int profileType = 0;
    mSamplingInterval = 0;
    mStreaming = false;
    String process = null;
    String cmd = nextArgRequired();
    if ("start".equals(cmd)) {
        start = true;
        String opt;
        while ((opt = nextOption()) != null) {
            if (opt.equals("--user")) {
                userId = parseUserArg(nextArgRequired());
            } else if (opt.equals("--wall")) {
                wall = true;
            } else if (opt.equals("--streaming")) {
                mStreaming = true;
            } else if (opt.equals("--sampling")) {
                mSamplingInterval = Integer.parseInt(nextArgRequired());
            } else {
                System.err.println("Error: Unknown option: " + opt);
                return;
            }
        }
        process = nextArgRequired();
    } else if ("stop".equals(cmd)) {
        String opt;
        while ((opt = nextOption()) != null) {
            if (opt.equals("--user")) {
                userId = parseUserArg(nextArgRequired());
            } else {
                System.err.println("Error: Unknown option: " + opt);
                return;
            }
        }
        process = nextArg();
    } else {
        // Compatibility with old syntax: process is specified first.
        process = cmd;
        cmd = nextArgRequired();
        if ("start".equals(cmd)) {
            start = true;
        } else if (!"stop".equals(cmd)) {
            throw new IllegalArgumentException("Profile command " + process + " not valid");
        }
    }
    if (userId == UserHandle.USER_ALL) {
        System.err.println("Error: Can't profile with user 'all'");
        return;
    }
    ParcelFileDescriptor fd = null;
    ProfilerInfo profilerInfo = null;
    if (start) {
        profileFile = nextArgRequired();
        try {
            fd = openForSystemServer(new File(profileFile), ParcelFileDescriptor.MODE_CREATE | ParcelFileDescriptor.MODE_TRUNCATE | ParcelFileDescriptor.MODE_WRITE_ONLY);
        } catch (FileNotFoundException e) {
            System.err.println("Error: Unable to open file: " + profileFile);
            System.err.println("Consider using a file under /data/local/tmp/");
            return;
        }
        profilerInfo = new ProfilerInfo(profileFile, fd, mSamplingInterval, false, mStreaming);
    }
    try {
        if (wall) {
            // XXX doesn't work -- this needs to be set before booting.
            String props = SystemProperties.get("dalvik.vm.extra-opts");
            if (props == null || !props.contains("-Xprofile:wallclock")) {
                props = props + " -Xprofile:wallclock";
            //SystemProperties.set("dalvik.vm.extra-opts", props);
            }
        } else if (start) {
        //removeWallOption();
        }
        if (!mAm.profileControl(process, userId, start, profilerInfo, profileType)) {
            wall = false;
            throw new AndroidException("PROFILE FAILED on process " + process);
        }
    } finally {
        if (!wall) {
        //removeWallOption();
        }
    }
}
Also used : ProfilerInfo(android.app.ProfilerInfo) AndroidException(android.util.AndroidException) ParcelFileDescriptor(android.os.ParcelFileDescriptor) FileNotFoundException(java.io.FileNotFoundException) File(java.io.File)

Example 55 with ParcelFileDescriptor

use of android.os.ParcelFileDescriptor in project platform_frameworks_base by android.

the class FileBackupHelperBase method performBackup_checked.

/**
     * Check the parameters so the native code doesn't have to throw all the exceptions
     * since it's easier to do that from Java.
     */
static void performBackup_checked(ParcelFileDescriptor oldState, BackupDataOutput data, ParcelFileDescriptor newState, String[] files, String[] keys) {
    if (files.length == 0) {
        return;
    }
    // files must be all absolute paths
    for (String f : files) {
        if (f.charAt(0) != '/') {
            throw new RuntimeException("files must have all absolute paths: " + f);
        }
    }
    // the length of files and keys must be the same
    if (files.length != keys.length) {
        throw new RuntimeException("files.length=" + files.length + " keys.length=" + keys.length);
    }
    // oldStateFd can be null
    FileDescriptor oldStateFd = oldState != null ? oldState.getFileDescriptor() : null;
    FileDescriptor newStateFd = newState.getFileDescriptor();
    if (newStateFd == null) {
        throw new NullPointerException();
    }
    int err = performBackup_native(oldStateFd, data.mBackupWriter, newStateFd, files, keys);
    if (err != 0) {
        // TODO: more here
        throw new RuntimeException("Backup failed 0x" + Integer.toHexString(err));
    }
}
Also used : ParcelFileDescriptor(android.os.ParcelFileDescriptor) FileDescriptor(java.io.FileDescriptor)

Aggregations

ParcelFileDescriptor (android.os.ParcelFileDescriptor)526 IOException (java.io.IOException)199 FileNotFoundException (java.io.FileNotFoundException)136 RemoteException (android.os.RemoteException)127 File (java.io.File)127 FileDescriptor (java.io.FileDescriptor)58 FileOutputStream (java.io.FileOutputStream)58 AssetFileDescriptor (android.content.res.AssetFileDescriptor)44 FileInputStream (java.io.FileInputStream)36 Parcel (android.os.Parcel)35 Uri (android.net.Uri)33 Intent (android.content.Intent)30 Bundle (android.os.Bundle)29 Cursor (android.database.Cursor)27 StorageManager (android.os.storage.StorageManager)25 Request (android.app.DownloadManager.Request)24 Bitmap (android.graphics.Bitmap)22 InputStream (java.io.InputStream)18 ProfilerInfo (android.app.ProfilerInfo)17 Binder (android.os.Binder)17