Java Code Examples for android.os.UserHandle#parseUserArg()

The following examples show how to use android.os.UserHandle#parseUserArg() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: PackageManagerShellCommand.java    From android_9.0.0_r45 with Apache License 2.0 6 votes vote down vote up
private int runSetHarmfulAppWarning() throws RemoteException {
    int userId = UserHandle.USER_CURRENT;

    String opt;
    while ((opt = getNextOption()) != null) {
        if (opt.equals("--user")) {
            userId = UserHandle.parseUserArg(getNextArgRequired());
        } else {
            getErrPrintWriter().println("Error: Unknown option: " + opt);
            return -1;
        }
    }

    userId = translateUserId(userId, false /*allowAll*/, "runSetHarmfulAppWarning");

    final String packageName = getNextArgRequired();
    final String warning = getNextArg();

    mInterface.setHarmfulAppWarning(packageName, warning, userId);

    return 0;
}
 
Example 2
Source File: ActivityManagerShellCommand.java    From android_9.0.0_r45 with Apache License 2.0 6 votes vote down vote up
int runSetInactive(PrintWriter pw) throws RemoteException {
    int userId = UserHandle.USER_CURRENT;

    String opt;
    while ((opt=getNextOption()) != null) {
        if (opt.equals("--user")) {
            userId = UserHandle.parseUserArg(getNextArgRequired());
        } else {
            getErrPrintWriter().println("Error: Unknown option: " + opt);
            return -1;
        }
    }
    String packageName = getNextArgRequired();
    String value = getNextArgRequired();

    IUsageStatsManager usm = IUsageStatsManager.Stub.asInterface(ServiceManager.getService(
            Context.USAGE_STATS_SERVICE));
    usm.setAppInactive(packageName, Boolean.parseBoolean(value), userId);
    return 0;
}
 
Example 3
Source File: OverlayManagerShellCommand.java    From android_9.0.0_r45 with Apache License 2.0 6 votes vote down vote up
private int runEnableDisable(final boolean enable) throws RemoteException {
    final PrintWriter err = getErrPrintWriter();

    int userId = UserHandle.USER_SYSTEM;
    String opt;
    while ((opt = getNextOption()) != null) {
        switch (opt) {
            case "--user":
                userId = UserHandle.parseUserArg(getNextArgRequired());
                break;
            default:
                err.println("Error: Unknown option: " + opt);
                return 1;
        }
    }

    final String packageName = getNextArgRequired();
    return mInterface.setEnabled(packageName, enable, userId) ? 0 : 1;
}
 
Example 4
Source File: OverlayManagerShellCommand.java    From android_9.0.0_r45 with Apache License 2.0 6 votes vote down vote up
private int runEnableExclusive() throws RemoteException {
    final PrintWriter err = getErrPrintWriter();

    int userId = UserHandle.USER_SYSTEM;
    boolean inCategory = false;
    String opt;
    while ((opt = getNextOption()) != null) {
        switch (opt) {
            case "--user":
                userId = UserHandle.parseUserArg(getNextArgRequired());
                break;
            case "--category":
                inCategory = true;
                break;
            default:
                err.println("Error: Unknown option: " + opt);
                return 1;
        }
    }
    final String overlay = getNextArgRequired();
    if (inCategory) {
        return mInterface.setEnabledExclusiveInCategory(overlay, userId) ? 0 : 1;
    } else {
        return mInterface.setEnabledExclusive(overlay, true, userId) ? 0 : 1;
    }
}
 
Example 5
Source File: JobSchedulerShellCommand.java    From android_9.0.0_r45 with Apache License 2.0 5 votes vote down vote up
private int timeout(PrintWriter pw) throws Exception {
    checkPermission("force timeout jobs");

    int userId = UserHandle.USER_ALL;

    String opt;
    while ((opt = getNextOption()) != null) {
        switch (opt) {
            case "-u":
            case "--user":
                userId = UserHandle.parseUserArg(getNextArgRequired());
                break;

            default:
                pw.println("Error: unknown option '" + opt + "'");
                return -1;
        }
    }

    if (userId == UserHandle.USER_CURRENT) {
        userId = ActivityManager.getCurrentUser();
    }

    final String pkgName = getNextArg();
    final String jobIdStr = getNextArg();
    final int jobId = jobIdStr != null ? Integer.parseInt(jobIdStr) : -1;

    final long ident = Binder.clearCallingIdentity();
    try {
        return mInternal.executeTimeoutCommand(pw, pkgName, userId, jobIdStr != null, jobId);
    } finally {
        Binder.restoreCallingIdentity(ident);
    }
}
 
Example 6
Source File: AppOpsService.java    From android_9.0.0_r45 with Apache License 2.0 5 votes vote down vote up
int parseUserOpMode(int defMode, PrintWriter err) throws RemoteException {
    userId = UserHandle.USER_CURRENT;
    opStr = null;
    modeStr = null;
    for (String argument; (argument = getNextArg()) != null;) {
        if ("--user".equals(argument)) {
            userId = UserHandle.parseUserArg(getNextArgRequired());
        } else {
            if (opStr == null) {
                opStr = argument;
            } else if (modeStr == null) {
                modeStr = argument;
                break;
            }
        }
    }
    if (opStr == null) {
        err.println("Error: Operation not specified.");
        return -1;
    }
    op = strOpToOp(opStr, err);
    if (op < 0) {
        return -1;
    }
    if (modeStr != null) {
        if ((mode=strModeToMode(modeStr, err)) < 0) {
            return -1;
        }
    } else {
        mode = defMode;
    }
    return 0;
}
 
Example 7
Source File: ActivityManagerShellCommand.java    From android_9.0.0_r45 with Apache License 2.0 5 votes vote down vote up
int runKill(PrintWriter pw) throws RemoteException {
    int userId = UserHandle.USER_ALL;

    String opt;
    while ((opt=getNextOption()) != null) {
        if (opt.equals("--user")) {
            userId = UserHandle.parseUserArg(getNextArgRequired());
        } else {
            getErrPrintWriter().println("Error: Unknown option: " + opt);
            return -1;
        }
    }
    mInterface.killBackgroundProcesses(getNextArgRequired(), userId);
    return 0;
}
 
Example 8
Source File: ActivityManagerShellCommand.java    From android_9.0.0_r45 with Apache License 2.0 5 votes vote down vote up
int runGetStandbyBucket(PrintWriter pw) throws RemoteException {
    int userId = UserHandle.USER_CURRENT;

    String opt;
    while ((opt=getNextOption()) != null) {
        if (opt.equals("--user")) {
            userId = UserHandle.parseUserArg(getNextArgRequired());
        } else {
            getErrPrintWriter().println("Error: Unknown option: " + opt);
            return -1;
        }
    }
    String packageName = getNextArg();

    IUsageStatsManager usm = IUsageStatsManager.Stub.asInterface(ServiceManager.getService(
            Context.USAGE_STATS_SERVICE));
    if (packageName != null) {
        int bucket = usm.getAppStandbyBucket(packageName, null, userId);
        pw.println(bucket);
    } else {
        ParceledListSlice<AppStandbyInfo> buckets = usm.getAppStandbyBuckets(
                SHELL_PACKAGE_NAME, userId);
        for (AppStandbyInfo bucketInfo : buckets.getList()) {
            pw.print(bucketInfo.mPackageName); pw.print(": ");
            pw.println(bucketInfo.mStandbyBucket);
        }
    }
    return 0;
}
 
Example 9
Source File: ActivityManagerShellCommand.java    From android_9.0.0_r45 with Apache License 2.0 5 votes vote down vote up
int runUpdateApplicationInfo(PrintWriter pw) throws RemoteException {
    int userid = UserHandle.parseUserArg(getNextArgRequired());
    ArrayList<String> packages = new ArrayList<>();
    packages.add(getNextArgRequired());
    String packageName;
    while ((packageName = getNextArg()) != null) {
        packages.add(packageName);
    }
    mInternal.scheduleApplicationInfoChanged(packages, userid);
    pw.println("Packages updated with most recent ApplicationInfos.");
    return 0;
}
 
Example 10
Source File: PackageManagerShellCommand.java    From android_9.0.0_r45 with Apache License 2.0 5 votes vote down vote up
private int runSetEnabledSetting(int state) throws RemoteException {
    int userId = UserHandle.USER_SYSTEM;
    String option = getNextOption();
    if (option != null && option.equals("--user")) {
        userId = UserHandle.parseUserArg(getNextArgRequired());
    }

    String pkg = getNextArg();
    if (pkg == null) {
        getErrPrintWriter().println("Error: no package or component specified");
        return 1;
    }
    ComponentName cn = ComponentName.unflattenFromString(pkg);
    if (cn == null) {
        mInterface.setApplicationEnabledSetting(pkg, state, 0, userId,
                "shell:" + android.os.Process.myUid());
        getOutPrintWriter().println("Package " + pkg + " new state: "
                + enabledSettingToString(
                mInterface.getApplicationEnabledSetting(pkg, userId)));
        return 0;
    } else {
        mInterface.setComponentEnabledSetting(cn, state, 0, userId);
        getOutPrintWriter().println("Component " + cn.toShortString() + " new state: "
                + enabledSettingToString(
                mInterface.getComponentEnabledSetting(cn, userId)));
        return 0;
    }
}
 
Example 11
Source File: PackageManagerShellCommand.java    From android_9.0.0_r45 with Apache License 2.0 5 votes vote down vote up
private int runClear() throws RemoteException {
    int userId = UserHandle.USER_SYSTEM;
    String option = getNextOption();
    if (option != null && option.equals("--user")) {
        userId = UserHandle.parseUserArg(getNextArgRequired());
    }

    String pkg = getNextArg();
    if (pkg == null) {
        getErrPrintWriter().println("Error: no package specified");
        return 1;
    }

    ClearDataObserver obs = new ClearDataObserver();
    ActivityManager.getService().clearApplicationUserData(pkg, false, obs, userId);
    synchronized (obs) {
        while (!obs.finished) {
            try {
                obs.wait();
            } catch (InterruptedException e) {
            }
        }
    }

    if (obs.result) {
        getOutPrintWriter().println("Success");
        return 0;
    } else {
        getErrPrintWriter().println("Failed");
        return 1;
    }
}
 
Example 12
Source File: PackageManagerShellCommand.java    From android_9.0.0_r45 with Apache License 2.0 5 votes vote down vote up
private int runPath() throws RemoteException {
    int userId = UserHandle.USER_SYSTEM;
    String option = getNextOption();
    if (option != null && option.equals("--user")) {
        userId = UserHandle.parseUserArg(getNextArgRequired());
    }

    String pkg = getNextArgRequired();
    if (pkg == null) {
        getErrPrintWriter().println("Error: no package specified");
        return 1;
    }
    return displayPackageFilePath(pkg, userId);
}
 
Example 13
Source File: ActivityManagerShellCommand.java    From android_9.0.0_r45 with Apache License 2.0 5 votes vote down vote up
int runSetStandbyBucket(PrintWriter pw) throws RemoteException {
    int userId = UserHandle.USER_CURRENT;

    String opt;
    while ((opt=getNextOption()) != null) {
        if (opt.equals("--user")) {
            userId = UserHandle.parseUserArg(getNextArgRequired());
        } else {
            getErrPrintWriter().println("Error: Unknown option: " + opt);
            return -1;
        }
    }
    String packageName = getNextArgRequired();
    String value = getNextArgRequired();
    int bucket = bucketNameToBucketValue(value);
    if (bucket < 0) return -1;
    boolean multiple = peekNextArg() != null;


    IUsageStatsManager usm = IUsageStatsManager.Stub.asInterface(ServiceManager.getService(
            Context.USAGE_STATS_SERVICE));
    if (!multiple) {
        usm.setAppStandbyBucket(packageName, bucketNameToBucketValue(value), userId);
    } else {
        ArrayList<AppStandbyInfo> bucketInfoList = new ArrayList<>();
        bucketInfoList.add(new AppStandbyInfo(packageName, bucket));
        while ((packageName = getNextArg()) != null) {
            value = getNextArgRequired();
            bucket = bucketNameToBucketValue(value);
            if (bucket < 0) continue;
            bucketInfoList.add(new AppStandbyInfo(packageName, bucket));
        }
        ParceledListSlice<AppStandbyInfo> slice = new ParceledListSlice<>(bucketInfoList);
        usm.setAppStandbyBuckets(slice, userId);
    }
    return 0;
}
 
Example 14
Source File: AccountManagerServiceShellCommand.java    From android_9.0.0_r45 with Apache License 2.0 5 votes vote down vote up
private Integer parseUserId() {
    final String option = getNextOption();
    if (option != null) {
        if (option.equals("--user")) {
            return UserHandle.parseUserArg(getNextArgRequired());
        } else {
            getErrPrintWriter().println("Unknown option: " + option);
            return null;
        }
    }
    return UserHandle.USER_SYSTEM;
}
 
Example 15
Source File: PackageManagerShellCommand.java    From android_9.0.0_r45 with Apache License 2.0 4 votes vote down vote up
private int runSuspend(boolean suspendedState) {
    final PrintWriter pw = getOutPrintWriter();
    int userId = UserHandle.USER_SYSTEM;
    String dialogMessage = null;
    final PersistableBundle appExtras = new PersistableBundle();
    final PersistableBundle launcherExtras = new PersistableBundle();
    String opt;
    while ((opt = getNextOption()) != null) {
        switch (opt) {
            case "--user":
                userId = UserHandle.parseUserArg(getNextArgRequired());
                break;
            case "--dialogMessage":
                dialogMessage = getNextArgRequired();
                break;
            case "--ael":
            case "--aes":
            case "--aed":
            case "--lel":
            case "--les":
            case "--led":
                final String key = getNextArgRequired();
                final String val = getNextArgRequired();
                if (!suspendedState) {
                    break;
                }
                final PersistableBundle bundleToInsert =
                        opt.startsWith("--a") ? appExtras : launcherExtras;
                switch (opt.charAt(4)) {
                    case 'l':
                        bundleToInsert.putLong(key, Long.valueOf(val));
                        break;
                    case 'd':
                        bundleToInsert.putDouble(key, Double.valueOf(val));
                        break;
                    case 's':
                        bundleToInsert.putString(key, val);
                        break;
                }
                break;
            default:
                pw.println("Error: Unknown option: " + opt);
                return 1;
        }
    }

    final String packageName = getNextArg();
    if (packageName == null) {
        pw.println("Error: package name not specified");
        return 1;
    }
    final String callingPackage =
            (Binder.getCallingUid() == Process.ROOT_UID) ? "root" : "com.android.shell";
    try {
        mInterface.setPackagesSuspendedAsUser(new String[]{packageName}, suspendedState,
                appExtras, launcherExtras, dialogMessage, callingPackage, userId);
        pw.println("Package " + packageName + " new suspended state: "
                + mInterface.isPackageSuspendedForUser(packageName, userId));
        return 0;
    } catch (RemoteException | IllegalArgumentException e) {
        pw.println(e.toString());
        return 1;
    }
}
 
Example 16
Source File: PackageManagerShellCommand.java    From android_9.0.0_r45 with Apache License 2.0 4 votes vote down vote up
private int runSetAppLink() throws RemoteException {
    int userId = UserHandle.USER_SYSTEM;

    String opt;
    while ((opt = getNextOption()) != null) {
        if (opt.equals("--user")) {
            userId = UserHandle.parseUserArg(getNextArgRequired());
        } else {
            getErrPrintWriter().println("Error: unknown option: " + opt);
            return 1;
        }
    }

    // Package name to act on; required
    final String pkg = getNextArg();
    if (pkg == null) {
        getErrPrintWriter().println("Error: no package specified.");
        return 1;
    }

    // State to apply; {always|ask|never|undefined}, required
    final String modeString = getNextArg();
    if (modeString == null) {
        getErrPrintWriter().println("Error: no app link state specified.");
        return 1;
    }

    final int newMode;
    switch (modeString.toLowerCase()) {
        case "undefined":
            newMode = INTENT_FILTER_DOMAIN_VERIFICATION_STATUS_UNDEFINED;
            break;

        case "always":
            newMode = INTENT_FILTER_DOMAIN_VERIFICATION_STATUS_ALWAYS;
            break;

        case "ask":
            newMode = INTENT_FILTER_DOMAIN_VERIFICATION_STATUS_ASK;
            break;

        case "always-ask":
            newMode = INTENT_FILTER_DOMAIN_VERIFICATION_STATUS_ALWAYS_ASK;
            break;

        case "never":
            newMode = INTENT_FILTER_DOMAIN_VERIFICATION_STATUS_NEVER;
            break;

        default:
            getErrPrintWriter().println("Error: unknown app link state '" + modeString + "'");
            return 1;
    }

    final PackageInfo info = mInterface.getPackageInfo(pkg, 0, userId);
    if (info == null) {
        getErrPrintWriter().println("Error: package " + pkg + " not found.");
        return 1;
    }

    if ((info.applicationInfo.privateFlags & ApplicationInfo.PRIVATE_FLAG_HAS_DOMAIN_URLS) == 0) {
        getErrPrintWriter().println("Error: package " + pkg + " does not handle web links.");
        return 1;
    }

    if (!mInterface.updateIntentVerificationStatus(pkg, newMode, userId)) {
        getErrPrintWriter().println("Error: unable to update app link status for " + pkg);
        return 1;
    }

    return 0;
}
 
Example 17
Source File: PackageManagerShellCommand.java    From android_9.0.0_r45 with Apache License 2.0 4 votes vote down vote up
private int runUninstall() throws RemoteException {
    final PrintWriter pw = getOutPrintWriter();
    int flags = 0;
    int userId = UserHandle.USER_ALL;
    long versionCode = PackageManager.VERSION_CODE_HIGHEST;

    String opt;
    while ((opt = getNextOption()) != null) {
        switch (opt) {
            case "-k":
                flags |= PackageManager.DELETE_KEEP_DATA;
                break;
            case "--user":
                userId = UserHandle.parseUserArg(getNextArgRequired());
                break;
            case "--versionCode":
                versionCode = Long.parseLong(getNextArgRequired());
                break;
            default:
                pw.println("Error: Unknown option: " + opt);
                return 1;
        }
    }

    final String packageName = getNextArg();
    if (packageName == null) {
        pw.println("Error: package name not specified");
        return 1;
    }

    // if a split is specified, just remove it and not the whole package
    final String splitName = getNextArg();
    if (splitName != null) {
        return runRemoveSplit(packageName, splitName);
    }

    userId = translateUserId(userId, true /*allowAll*/, "runUninstall");
    if (userId == UserHandle.USER_ALL) {
        userId = UserHandle.USER_SYSTEM;
        flags |= PackageManager.DELETE_ALL_USERS;
    } else {
        final PackageInfo info = mInterface.getPackageInfo(packageName,
                PackageManager.MATCH_STATIC_SHARED_LIBRARIES, userId);
        if (info == null) {
            pw.println("Failure [not installed for " + userId + "]");
            return 1;
        }
        final boolean isSystem =
                (info.applicationInfo.flags & ApplicationInfo.FLAG_SYSTEM) != 0;
        // If we are being asked to delete a system app for just one
        // user set flag so it disables rather than reverting to system
        // version of the app.
        if (isSystem) {
            flags |= PackageManager.DELETE_SYSTEM_APP;
        }
    }

    final LocalIntentReceiver receiver = new LocalIntentReceiver();
    mInterface.getPackageInstaller().uninstall(new VersionedPackage(packageName,
            versionCode), null /*callerPackageName*/, flags,
            receiver.getIntentSender(), userId);

    final Intent result = receiver.getResult();
    final int status = result.getIntExtra(PackageInstaller.EXTRA_STATUS,
            PackageInstaller.STATUS_FAILURE);
    if (status == PackageInstaller.STATUS_SUCCESS) {
        pw.println("Success");
        return 0;
    } else {
        pw.println("Failure ["
                + result.getStringExtra(PackageInstaller.EXTRA_STATUS_MESSAGE) + "]");
        return 1;
    }
}
 
Example 18
Source File: OverlayManagerShellCommand.java    From android_9.0.0_r45 with Apache License 2.0 4 votes vote down vote up
private int runList() throws RemoteException {
    final PrintWriter out = getOutPrintWriter();
    final PrintWriter err = getErrPrintWriter();

    int userId = UserHandle.USER_SYSTEM;
    String opt;
    while ((opt = getNextOption()) != null) {
        switch (opt) {
            case "--user":
                userId = UserHandle.parseUserArg(getNextArgRequired());
                break;
            default:
                err.println("Error: Unknown option: " + opt);
                return 1;
        }
    }

    final Map<String, List<OverlayInfo>> allOverlays = mInterface.getAllOverlays(userId);
    for (final String targetPackageName : allOverlays.keySet()) {
        out.println(targetPackageName);
        List<OverlayInfo> overlaysForTarget = allOverlays.get(targetPackageName);
        final int N = overlaysForTarget.size();
        for (int i = 0; i < N; i++) {
            final OverlayInfo oi = overlaysForTarget.get(i);
            String status;
            switch (oi.state) {
                case OverlayInfo.STATE_ENABLED_STATIC:
                case OverlayInfo.STATE_ENABLED:
                    status = "[x]";
                    break;
                case OverlayInfo.STATE_DISABLED:
                    status = "[ ]";
                    break;
                default:
                    status = "---";
                    break;
            }
            out.println(String.format("%s %s", status, oi.packageName));
        }
        out.println();
    }
    return 0;
}
 
Example 19
Source File: ActivityManagerShellCommand.java    From android_9.0.0_r45 with Apache License 2.0 4 votes vote down vote up
int runIsUserStopped(PrintWriter pw) {
    int userId = UserHandle.parseUserArg(getNextArgRequired());
    boolean stopped = mInternal.isUserStopped(userId);
    pw.println(stopped);
    return 0;
}
 
Example 20
Source File: ActivityManagerShellCommand.java    From android_9.0.0_r45 with Apache License 2.0 4 votes vote down vote up
int runDumpHeap(PrintWriter pw) throws RemoteException {
    final PrintWriter err = getErrPrintWriter();
    boolean managed = true;
    boolean mallocInfo = false;
    int userId = UserHandle.USER_CURRENT;
    boolean runGc = false;

    String opt;
    while ((opt=getNextOption()) != null) {
        if (opt.equals("--user")) {
            userId = UserHandle.parseUserArg(getNextArgRequired());
            if (userId == UserHandle.USER_ALL) {
                err.println("Error: Can't dump heap with user 'all'");
                return -1;
            }
        } else if (opt.equals("-n")) {
            managed = false;
        } else if (opt.equals("-g")) {
            runGc = true;
        } else if (opt.equals("-m")) {
            managed = false;
            mallocInfo = true;
        } else {
            err.println("Error: Unknown option: " + opt);
            return -1;
        }
    }
    String process = getNextArgRequired();
    String heapFile = getNextArgRequired();

    File file = new File(heapFile);
    file.delete();
    ParcelFileDescriptor fd = openFileForSystem(heapFile, "w");
    if (fd == null) {
        return -1;
    }

    if (!mInterface.dumpHeap(process, userId, managed, mallocInfo, runGc, heapFile, fd)) {
        err.println("HEAP DUMP FAILED on process " + process);
        return -1;
    }
    return 0;
}