Deprecated UserManager.getUsers(excludeDying) / added getAliveUsers()
The existing method is confusing (the argument used to be called includeDying) and it puts the burden on the caller (which need to understand what the parameter means). Furthermore: - The majority of calls are for getUsers(excludeDying=true). - The calls for getUsers(excludeDying=false) are equivalent to calls to getUsers() Test: m Test: a VpnTest ConnectivityServiceTest PermissionMonitorTest Bug: 157921703 Change-Id: Ife767a40b7b7790ba28b5377046de822ddbf275c
This commit is contained in:
@@ -43,7 +43,6 @@ import android.content.IntentFilter;
|
||||
import android.content.IntentSender;
|
||||
import android.content.pm.UserInfo;
|
||||
import android.content.pm.UserInfo.UserInfoFlag;
|
||||
import android.content.res.Configuration;
|
||||
import android.content.res.Resources;
|
||||
import android.graphics.Bitmap;
|
||||
import android.graphics.BitmapFactory;
|
||||
@@ -3174,28 +3173,55 @@ public class UserManager {
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns information for all users on this device, including ones marked for deletion.
|
||||
* To retrieve only users that are alive, use {@link #getUsers(boolean)}.
|
||||
* Returns information for all fully-created users on this device, including ones marked for
|
||||
* deletion.
|
||||
*
|
||||
* <p>To retrieve only users that are not marked for deletion, use {@link #getAliveUsers()}.
|
||||
*
|
||||
* <p>To retrieve *all* users (including partial and pre-created users), use
|
||||
* {@link #getUsers(boolean, boolean, boolean)) getUsers(false, false, false)}.
|
||||
*
|
||||
* <p>To retrieve a more specific list of users, use
|
||||
* {@link #getUsers(boolean, boolean, boolean)}.
|
||||
*
|
||||
* @return the list of users that were created.
|
||||
*
|
||||
* @return the list of users that exist on the device.
|
||||
* @hide
|
||||
*/
|
||||
@UnsupportedAppUsage
|
||||
@RequiresPermission(android.Manifest.permission.MANAGE_USERS)
|
||||
public List<UserInfo> getUsers() {
|
||||
return getUsers(/* excludeDying= */ false);
|
||||
return getUsers(/*excludePartial= */ true, /* excludeDying= */ false,
|
||||
/* excludePreCreated= */ true);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns information for all users on this device. Requires
|
||||
* {@link android.Manifest.permission#MANAGE_USERS} permission.
|
||||
* Returns information for all "usable" users on this device (i.e, it excludes users that are
|
||||
* marked for deletion, pre-created users, etc...).
|
||||
*
|
||||
* <p>To retrieve all fully-created users, use {@link #getUsers()}.
|
||||
*
|
||||
* <p>To retrieve a more specific list of users, use
|
||||
* {@link #getUsers(boolean, boolean, boolean)}.
|
||||
*
|
||||
* @param excludeDying specify if the list should exclude users being
|
||||
* removed.
|
||||
* @return the list of users that were created.
|
||||
* @hide
|
||||
*/
|
||||
@RequiresPermission(android.Manifest.permission.MANAGE_USERS)
|
||||
public @NonNull List<UserInfo> getAliveUsers() {
|
||||
return getUsers(/*excludePartial= */ true, /* excludeDying= */ true,
|
||||
/* excludePreCreated= */ true);
|
||||
}
|
||||
|
||||
/**
|
||||
* @deprecated use {@link #getAliveUsers()} for {@code getUsers(true)}, or
|
||||
* {@link #getUsers()} for @code getUsers(false)}.
|
||||
*
|
||||
* @hide
|
||||
*/
|
||||
@Deprecated
|
||||
@UnsupportedAppUsage
|
||||
@RequiresPermission(android.Manifest.permission.MANAGE_USERS)
|
||||
public @NonNull List<UserInfo> getUsers(boolean excludeDying) {
|
||||
return getUsers(/*excludePartial= */ true, excludeDying,
|
||||
/* excludePreCreated= */ true);
|
||||
|
||||
@@ -226,7 +226,7 @@ public class PermissionMonitor implements PackageManagerInternal.PackageListObse
|
||||
netdPermsUids.put(uid, permInfo);
|
||||
}
|
||||
|
||||
List<UserInfo> users = mUserManager.getUsers(true); // exclude dying users
|
||||
List<UserInfo> users = mUserManager.getAliveUsers();
|
||||
if (users != null) {
|
||||
for (UserInfo user : users) {
|
||||
mUsers.add(user.id);
|
||||
|
||||
@@ -1463,7 +1463,7 @@ public class Vpn {
|
||||
final long token = Binder.clearCallingIdentity();
|
||||
List<UserInfo> users;
|
||||
try {
|
||||
users = UserManager.get(mContext).getUsers(true);
|
||||
users = UserManager.get(mContext).getAliveUsers();
|
||||
} finally {
|
||||
Binder.restoreCallingIdentity(token);
|
||||
}
|
||||
|
||||
@@ -1199,7 +1199,7 @@ public class ConnectivityServiceTest {
|
||||
MockitoAnnotations.initMocks(this);
|
||||
when(mMetricsService.defaultNetworkMetrics()).thenReturn(mDefaultNetworkMetrics);
|
||||
|
||||
when(mUserManager.getUsers(eq(true))).thenReturn(
|
||||
when(mUserManager.getAliveUsers()).thenReturn(
|
||||
Arrays.asList(new UserInfo[] {
|
||||
new UserInfo(VPN_USER, "", 0),
|
||||
}));
|
||||
|
||||
@@ -127,7 +127,7 @@ public class PermissionMonitorTest {
|
||||
MockitoAnnotations.initMocks(this);
|
||||
when(mContext.getPackageManager()).thenReturn(mPackageManager);
|
||||
when(mContext.getSystemService(eq(Context.USER_SERVICE))).thenReturn(mUserManager);
|
||||
when(mUserManager.getUsers(eq(true))).thenReturn(
|
||||
when(mUserManager.getAliveUsers()).thenReturn(
|
||||
Arrays.asList(new UserInfo[] {
|
||||
new UserInfo(MOCK_USER1, "", 0),
|
||||
new UserInfo(MOCK_USER2, "", 0),
|
||||
|
||||
@@ -1238,15 +1238,14 @@ public class VpnTest {
|
||||
* @see UserManagerService#getUsers(boolean)
|
||||
*/
|
||||
doAnswer(invocation -> {
|
||||
final boolean excludeDying = (boolean) invocation.getArguments()[0];
|
||||
final ArrayList<UserInfo> result = new ArrayList<>(users.length);
|
||||
for (UserInfo ui : users) {
|
||||
if (!excludeDying || (ui.isEnabled() && !ui.partial)) {
|
||||
if (ui.isEnabled() && !ui.partial) {
|
||||
result.add(ui);
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}).when(mUserManager).getUsers(anyBoolean());
|
||||
}).when(mUserManager).getAliveUsers();
|
||||
|
||||
doAnswer(invocation -> {
|
||||
final int id = (int) invocation.getArguments()[0];
|
||||
|
||||
Reference in New Issue
Block a user