Skip to content

Commit 8b5bce8

Browse files
Amith YamasaniAndroid (Google) Code Review
authored andcommitted
Merge "User management and switching"
2 parents 79763f3 + 1359360 commit 8b5bce8

File tree

22 files changed

+682
-274
lines changed

22 files changed

+682
-274
lines changed

core/java/android/accounts/AccountManagerService.java

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -231,6 +231,14 @@ public void onReceive(Context context1, Intent intent) {
231231
}
232232
}, intentFilter);
233233

234+
IntentFilter userFilter = new IntentFilter();
235+
userFilter.addAction(Intent.ACTION_USER_REMOVED);
236+
mContext.registerReceiver(new BroadcastReceiver() {
237+
@Override
238+
public void onReceive(Context context, Intent intent) {
239+
onUserRemoved(intent);
240+
}
241+
}, userFilter);
234242
}
235243

236244
private UserAccounts initUser(int userId) {
@@ -347,6 +355,28 @@ protected UserAccounts getUserAccounts(int userId) {
347355
}
348356
}
349357

358+
private void onUserRemoved(Intent intent) {
359+
int userId = intent.getIntExtra(Intent.EXTRA_USERID, -1);
360+
if (userId < 1) return;
361+
362+
UserAccounts accounts;
363+
synchronized (mUsers) {
364+
accounts = mUsers.get(userId);
365+
mUsers.remove(userId);
366+
}
367+
if (accounts == null) {
368+
File dbFile = new File(getDatabaseName(userId));
369+
dbFile.delete();
370+
return;
371+
}
372+
373+
synchronized (accounts.cacheLock) {
374+
accounts.openHelper.close();
375+
File dbFile = new File(getDatabaseName(userId));
376+
dbFile.delete();
377+
}
378+
}
379+
350380
private List<UserInfo> getAllUsers() {
351381
try {
352382
return AppGlobals.getPackageManager().getUsers();

core/java/android/app/ApplicationPackageManager.java

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1211,6 +1211,18 @@ public List<UserInfo> getUsers() {
12111211
}
12121212
}
12131213

1214+
/**
1215+
* @hide
1216+
*/
1217+
@Override
1218+
public UserInfo getUser(int userId) {
1219+
try {
1220+
return mPM.getUser(userId);
1221+
} catch (RemoteException re) {
1222+
return null;
1223+
}
1224+
}
1225+
12141226
/**
12151227
* @hide
12161228
*/
@@ -1228,7 +1240,10 @@ public boolean removeUser(int id) {
12281240
*/
12291241
@Override
12301242
public void updateUserName(int id, String name) {
1231-
// TODO:
1243+
try {
1244+
mPM.updateUserName(id, name);
1245+
} catch (RemoteException re) {
1246+
}
12321247
}
12331248

12341249
/**

core/java/android/content/Intent.java

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2141,6 +2141,30 @@ public static Intent createChooser(Intent target, CharSequence title) {
21412141
public static final String ACTION_PRE_BOOT_COMPLETED =
21422142
"android.intent.action.PRE_BOOT_COMPLETED";
21432143

2144+
/**
2145+
* Broadcast sent to the system when a user is added. Carries an extra EXTRA_USERID that has the
2146+
* userid of the new user.
2147+
* @hide
2148+
*/
2149+
public static final String ACTION_USER_ADDED =
2150+
"android.intent.action.USER_ADDED";
2151+
2152+
/**
2153+
* Broadcast sent to the system when a user is removed. Carries an extra EXTRA_USERID that has
2154+
* the userid of the user.
2155+
* @hide
2156+
*/
2157+
public static final String ACTION_USER_REMOVED =
2158+
"android.intent.action.USER_REMOVED";
2159+
2160+
/**
2161+
* Broadcast sent to the system when the user switches. Carries an extra EXTRA_USERID that has
2162+
* the userid of the user to become the current one.
2163+
* @hide
2164+
*/
2165+
public static final String ACTION_USER_SWITCHED =
2166+
"android.intent.action.USER_SWITCHED";
2167+
21442168
// ---------------------------------------------------------------------
21452169
// ---------------------------------------------------------------------
21462170
// Standard intent categories (see addCategory()).
@@ -2682,6 +2706,13 @@ public static Intent createChooser(Intent target, CharSequence title) {
26822706
public static final String EXTRA_LOCAL_ONLY =
26832707
"android.intent.extra.LOCAL_ONLY";
26842708

2709+
/**
2710+
* The userid carried with broadcast intents related to addition, removal and switching of users
2711+
* - {@link #ACTION_USER_ADDED}, {@link #ACTION_USER_REMOVED} and {@link #ACTION_USER_SWITCHED}.
2712+
* @hide
2713+
*/
2714+
public static final String EXTRA_USERID =
2715+
"android.intent.extra.user_id";
26852716
// ---------------------------------------------------------------------
26862717
// ---------------------------------------------------------------------
26872718
// Intent flags (see mFlags variable).

core/java/android/content/SyncManager.java

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -326,6 +326,13 @@ public void onReceive(Context context, Intent intent) {
326326
}
327327
};
328328

329+
private BroadcastReceiver mUserIntentReceiver = new BroadcastReceiver() {
330+
@Override
331+
public void onReceive(Context context, Intent intent) {
332+
onUserRemoved(intent);
333+
}
334+
};
335+
329336
private static final String ACTION_SYNC_ALARM = "android.content.syncmanager.SYNC_ALARM";
330337
private final SyncHandler mSyncHandler;
331338

@@ -420,6 +427,10 @@ public void onServiceChanged(SyncAdapterType type, boolean removed) {
420427
intentFilter.setPriority(100);
421428
context.registerReceiver(mShutdownIntentReceiver, intentFilter);
422429

430+
intentFilter = new IntentFilter();
431+
intentFilter.addAction(Intent.ACTION_USER_REMOVED);
432+
mContext.registerReceiver(mUserIntentReceiver, intentFilter);
433+
423434
if (!factoryTest) {
424435
mNotificationMgr = (NotificationManager)
425436
context.getSystemService(Context.NOTIFICATION_SERVICE);
@@ -905,6 +916,18 @@ void maybeRescheduleSync(SyncResult syncResult, SyncOperation operation) {
905916
}
906917
}
907918

919+
private void onUserRemoved(Intent intent) {
920+
int userId = intent.getIntExtra(Intent.EXTRA_USERID, -1);
921+
if (userId == -1) return;
922+
923+
// Clean up the storage engine database
924+
mSyncStorageEngine.doDatabaseCleanup(new Account[0], userId);
925+
onAccountsUpdated(null);
926+
synchronized (mSyncQueue) {
927+
mSyncQueue.removeUser(userId);
928+
}
929+
}
930+
908931
/**
909932
* @hide
910933
*/

core/java/android/content/SyncQueue.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,19 @@ private boolean add(SyncOperation operation,
117117
return true;
118118
}
119119

120+
public void removeUser(int userId) {
121+
ArrayList<SyncOperation> opsToRemove = new ArrayList<SyncOperation>();
122+
for (SyncOperation op : mOperationsMap.values()) {
123+
if (op.userId == userId) {
124+
opsToRemove.add(op);
125+
}
126+
}
127+
128+
for (SyncOperation op : opsToRemove) {
129+
remove(op);
130+
}
131+
}
132+
120133
/**
121134
* Remove the specified operation if it is in the queue.
122135
* @param operation the operation to remove

core/java/android/content/pm/IPackageManager.aidl

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -358,6 +358,7 @@ interface IPackageManager {
358358

359359
UserInfo createUser(in String name, int flags);
360360
boolean removeUser(int userId);
361+
void updateUserName(int userId, String name);
361362

362363
void installPackageWithVerification(in Uri packageURI, in IPackageInstallObserver observer,
363364
int flags, in String installerPackageName, in Uri verificationURI,
@@ -370,6 +371,7 @@ interface IPackageManager {
370371
boolean isFirstBoot();
371372

372373
List<UserInfo> getUsers();
374+
UserInfo getUser(int userId);
373375

374376
void setPermissionEnforcement(String permission, int enforcement);
375377
int getPermissionEnforcement(String permission);

core/java/android/content/pm/PackageManager.java

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2153,7 +2153,8 @@ public PackageInfo getPackageArchiveInfo(String archiveFilePath, int flags) {
21532153
if ((flags & GET_SIGNATURES) != 0) {
21542154
packageParser.collectCertificates(pkg, 0);
21552155
}
2156-
return PackageParser.generatePackageInfo(pkg, null, flags, 0, 0, null);
2156+
return PackageParser.generatePackageInfo(pkg, null, flags, 0, 0, null, false,
2157+
COMPONENT_ENABLED_STATE_DEFAULT);
21572158
}
21582159

21592160
/**
@@ -2637,10 +2638,17 @@ public abstract void movePackage(
26372638
public abstract void updateUserFlags(int id, int flags);
26382639

26392640
/**
2640-
* Returns the device identity that verifiers can use to associate their
2641-
* scheme to a particular device. This should not be used by anything other
2642-
* than a package verifier.
2643-
*
2641+
* Returns the details for the user specified by userId.
2642+
* @param userId the user id of the user
2643+
* @return UserInfo for the specified user, or null if no such user exists.
2644+
* @hide
2645+
*/
2646+
public abstract UserInfo getUser(int userId);
2647+
2648+
/**
2649+
* Returns the device identity that verifiers can use to associate their scheme to a particular
2650+
* device. This should not be used by anything other than a package verifier.
2651+
*
26442652
* @return identity that uniquely identifies current device
26452653
* @hide
26462654
*/

0 commit comments

Comments
 (0)