Java Code Examples for android.os.UserManager#getUsers()

The following examples show how to use android.os.UserManager#getUsers() . 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: OverlayManagerService.java    From android_9.0.0_r45 with Apache License 2.0 6 votes vote down vote up
private void initIfNeeded() {
    final UserManager um = getContext().getSystemService(UserManager.class);
    final List<UserInfo> users = um.getUsers(true /*excludeDying*/);
    synchronized (mLock) {
        final int userCount = users.size();
        for (int i = 0; i < userCount; i++) {
            final UserInfo userInfo = users.get(i);
            if (!userInfo.supportsSwitchTo() && userInfo.id != UserHandle.USER_SYSTEM) {
                // Initialize any users that can't be switched to, as there state would
                // never be setup in onSwitchUser(). We will switch to the system user right
                // after this, and its state will be setup there.
                final List<String> targets = mImpl.updateOverlaysForUser(users.get(i).id);
                updateOverlayPaths(users.get(i).id, targets);
            }
        }
    }
}
 
Example 2
Source File: BaseSettingsProviderTest.java    From Study_Android_Demo with Apache License 2.0 6 votes vote down vote up
protected int getSecondaryUserId() {
    if (mSecondaryUserId == Integer.MIN_VALUE) {
        UserManager userManager = (UserManager) getContext()
                .getSystemService(Context.USER_SERVICE);
        List<UserInfo> users = userManager.getUsers();
        final int userCount = users.size();
        for (int i = 0; i < userCount; i++) {
            UserInfo user = users.get(i);
            if (!user.isPrimary() && !user.isManagedProfile()) {
                mSecondaryUserId = user.id;
                return mSecondaryUserId;
            }
        }
    }
    if (mSecondaryUserId == Integer.MIN_VALUE) {
        mSecondaryUserId =  UserHandle.USER_SYSTEM;
    }
    return mSecondaryUserId;
}
 
Example 3
Source File: SystemImpl.java    From android_9.0.0_r45 with Apache License 2.0 5 votes vote down vote up
@Override
public void enablePackageForAllUsers(Context context, String packageName, boolean enable) {
    UserManager userManager = (UserManager)context.getSystemService(Context.USER_SERVICE);
    for(UserInfo userInfo : userManager.getUsers()) {
        enablePackageForUser(packageName, enable, userInfo.id);
    }
}
 
Example 4
Source File: LegacyGlobalActions.java    From android_9.0.0_r45 with Apache License 2.0 5 votes vote down vote up
private void addUsersToMenu(ArrayList<Action> items) {
    UserManager um = (UserManager) mContext.getSystemService(Context.USER_SERVICE);
    if (um.isUserSwitcherEnabled()) {
        List<UserInfo> users = um.getUsers();
        UserInfo currentUser = getCurrentUser();
        for (final UserInfo user : users) {
            if (user.supportsSwitchToByUser()) {
                boolean isCurrentUser = currentUser == null
                        ? user.id == 0 : (currentUser.id == user.id);
                Drawable icon = user.iconPath != null ? Drawable.createFromPath(user.iconPath)
                        : null;
                SinglePressAction switchToUser = new SinglePressAction(
                        com.android.internal.R.drawable.ic_menu_cc, icon,
                        (user.name != null ? user.name : "Primary")
                        + (isCurrentUser ? " \u2714" : "")) {
                    @Override
                    public void onPress() {
                        try {
                            ActivityManager.getService().switchUser(user.id);
                        } catch (RemoteException re) {
                            Log.e(TAG, "Couldn't switch user " + re);
                        }
                    }

                    @Override
                    public boolean showDuringKeyguard() {
                        return true;
                    }

                    @Override
                    public boolean showBeforeProvisioning() {
                        return false;
                    }
                };
                items.add(switchToUser);
            }
        }
    }
}
 
Example 5
Source File: LockSettingsStorage.java    From android_9.0.0_r45 with Apache License 2.0 5 votes vote down vote up
public Map<Integer, List<Long>> listSyntheticPasswordHandlesForAllUsers(String stateName) {
    Map<Integer, List<Long>> result = new ArrayMap<>();
    final UserManager um = UserManager.get(mContext);
    for (UserInfo user : um.getUsers(false)) {
        result.put(user.id, listSyntheticPasswordHandlesForUser(stateName, user.id));
    }
    return result;
}
 
Example 6
Source File: StatsCompanionService.java    From android_9.0.0_r45 with Apache License 2.0 5 votes vote down vote up
@GuardedBy("sStatsdLock")
private final void informAllUidsLocked(Context context) throws RemoteException {
    UserManager um = (UserManager) context.getSystemService(Context.USER_SERVICE);
    PackageManager pm = context.getPackageManager();
    final List<UserInfo> users = um.getUsers(true);
    if (DEBUG) {
        Slog.d(TAG, "Iterating over " + users.size() + " profiles.");
    }

    List<Integer> uids = new ArrayList<>();
    List<Long> versions = new ArrayList<>();
    List<String> apps = new ArrayList<>();

    // Add in all the apps for every user/profile.
    for (UserInfo profile : users) {
        List<PackageInfo> pi =
            pm.getInstalledPackagesAsUser(PackageManager.MATCH_KNOWN_PACKAGES, profile.id);
        for (int j = 0; j < pi.size(); j++) {
            if (pi.get(j).applicationInfo != null) {
                uids.add(pi.get(j).applicationInfo.uid);
                versions.add(pi.get(j).getLongVersionCode());
                apps.add(pi.get(j).packageName);
            }
        }
    }
    sStatsd.informAllUidData(toIntArray(uids), toLongArray(versions), apps.toArray(new
            String[apps.size()]));
    if (DEBUG) {
        Slog.d(TAG, "Sent data for " + uids.size() + " apps");
    }
}
 
Example 7
Source File: MediaSessionService.java    From android_9.0.0_r45 with Apache License 2.0 5 votes vote down vote up
private void updateUser() {
    synchronized (mLock) {
        UserManager manager = (UserManager) getContext().getSystemService(Context.USER_SERVICE);
        mFullUserIds.clear();
        List<UserInfo> allUsers = manager.getUsers();
        if (allUsers != null) {
            for (UserInfo userInfo : allUsers) {
                if (userInfo.isManagedProfile()) {
                    mFullUserIds.put(userInfo.id, userInfo.profileGroupId);
                } else {
                    mFullUserIds.put(userInfo.id, userInfo.id);
                    if (mUserRecords.get(userInfo.id) == null) {
                        mUserRecords.put(userInfo.id, new FullUserRecord(userInfo.id));
                    }
                }
            }
        }
        // Ensure that the current full user exists.
        int currentFullUserId = ActivityManager.getCurrentUser();
        mCurrentFullUserRecord = mUserRecords.get(currentFullUserId);
        if (mCurrentFullUserRecord == null) {
            Log.w(TAG, "Cannot find FullUserInfo for the current user " + currentFullUserId);
            mCurrentFullUserRecord = new FullUserRecord(currentFullUserId);
            mUserRecords.put(currentFullUserId, mCurrentFullUserRecord);
        }
        mFullUserIds.put(currentFullUserId, currentFullUserId);
    }
}
 
Example 8
Source File: CacheQuotaStrategy.java    From android_9.0.0_r45 with Apache License 2.0 4 votes vote down vote up
/**
 * Returns a list of CacheQuotaHints which do not have their quotas filled out for apps
 * which have been used in the last year.
 */
private List<CacheQuotaHint> getUnfulfilledRequests() {
    long timeNow = System.currentTimeMillis();
    long oneYearAgo = timeNow - DateUtils.YEAR_IN_MILLIS;

    List<CacheQuotaHint> requests = new ArrayList<>();
    UserManager um = mContext.getSystemService(UserManager.class);
    final List<UserInfo> users = um.getUsers();
    final int userCount = users.size();
    final PackageManager packageManager = mContext.getPackageManager();
    for (int i = 0; i < userCount; i++) {
        UserInfo info = users.get(i);
        List<UsageStats> stats =
                mUsageStats.queryUsageStatsForUser(info.id, UsageStatsManager.INTERVAL_BEST,
                        oneYearAgo, timeNow, /*obfuscateInstantApps=*/ false);
        if (stats == null) {
            continue;
        }

        for (UsageStats stat : stats) {
            String packageName = stat.getPackageName();
            try {
                // We need the app info to determine the uid and the uuid of the volume
                // where the app is installed.
                ApplicationInfo appInfo = packageManager.getApplicationInfoAsUser(
                        packageName, 0, info.id);
                requests.add(
                        new CacheQuotaHint.Builder()
                                .setVolumeUuid(appInfo.volumeUuid)
                                .setUid(appInfo.uid)
                                .setUsageStats(stat)
                                .setQuota(CacheQuotaHint.QUOTA_NOT_SET)
                                .build());
            } catch (PackageManager.NameNotFoundException e) {
                // This may happen if an app has a recorded usage, but has been uninstalled.
                continue;
            }
        }
    }
    return requests;
}
 
Example 9
Source File: UserPackage.java    From android_9.0.0_r45 with Apache License 2.0 4 votes vote down vote up
private static List<UserInfo> getAllUsers(Context context) {
    UserManager userManager = (UserManager) context.getSystemService(Context.USER_SERVICE);
    return userManager.getUsers(false);
}