Skip to content

Commit b6c171b

Browse files
Amith YamasaniAndroid (Google) Code Review
authored andcommitted
Merge "Do cleanup when Stopping users" into jb-mr1-dev
2 parents ec5acee + 756901d commit b6c171b

File tree

4 files changed

+51
-20
lines changed

4 files changed

+51
-20
lines changed

services/java/com/android/server/AppWidgetService.java

Lines changed: 20 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -98,21 +98,19 @@ public void systemReady(boolean safeMode) {
9898

9999
IntentFilter userFilter = new IntentFilter();
100100
userFilter.addAction(Intent.ACTION_USER_REMOVED);
101+
userFilter.addAction(Intent.ACTION_USER_STOPPING);
101102
mContext.registerReceiver(new BroadcastReceiver() {
102103
@Override
103104
public void onReceive(Context context, Intent intent) {
104-
onUserRemoved(intent.getIntExtra(Intent.EXTRA_USER_HANDLE, -1));
105+
if (Intent.ACTION_USER_REMOVED.equals(intent.getAction())) {
106+
onUserRemoved(intent.getIntExtra(Intent.EXTRA_USER_HANDLE,
107+
UserHandle.USER_NULL));
108+
} else if (Intent.ACTION_USER_STOPPING.equals(intent.getAction())) {
109+
onUserStopping(intent.getIntExtra(Intent.EXTRA_USER_HANDLE,
110+
UserHandle.USER_NULL));
111+
}
105112
}
106113
}, userFilter);
107-
108-
IntentFilter userStopFilter = new IntentFilter();
109-
userStopFilter.addAction(Intent.ACTION_USER_STOPPED);
110-
mContext.registerReceiverAsUser(new BroadcastReceiver() {
111-
@Override
112-
public void onReceive(Context context, Intent intent) {
113-
onUserStopped(getSendingUserId());
114-
}
115-
}, UserHandle.ALL, userFilter, null, null);
116114
}
117115

118116
/**
@@ -203,7 +201,7 @@ public void onUserRemoved(int userId) {
203201
synchronized (mAppWidgetServices) {
204202
AppWidgetServiceImpl impl = mAppWidgetServices.get(userId);
205203
mAppWidgetServices.remove(userId);
206-
204+
207205
if (impl == null) {
208206
AppWidgetServiceImpl.getSettingsFile(userId).delete();
209207
} else {
@@ -212,7 +210,15 @@ public void onUserRemoved(int userId) {
212210
}
213211
}
214212

215-
public void onUserStopped(int userId) {
213+
public void onUserStopping(int userId) {
214+
if (userId < 1) return;
215+
synchronized (mAppWidgetServices) {
216+
AppWidgetServiceImpl impl = mAppWidgetServices.get(userId);
217+
if (impl != null) {
218+
mAppWidgetServices.remove(userId);
219+
impl.onUserStopping();
220+
}
221+
}
216222
}
217223

218224
private AppWidgetServiceImpl getImplForUser(int userId) {
@@ -324,11 +330,11 @@ public void onReceive(Context context, Intent intent) {
324330
String action = intent.getAction();
325331
// Slog.d(TAG, "received " + action);
326332
if (Intent.ACTION_BOOT_COMPLETED.equals(action)) {
327-
int userId = intent.getIntExtra(Intent.EXTRA_USER_HANDLE, -1);
333+
int userId = intent.getIntExtra(Intent.EXTRA_USER_HANDLE, UserHandle.USER_NULL);
328334
if (userId >= 0) {
329335
getImplForUser(userId).sendInitialBroadcasts();
330336
} else {
331-
Slog.w(TAG, "Not user handle supplied in " + intent);
337+
Slog.w(TAG, "Incorrect user handle supplied in " + intent);
332338
}
333339
} else if (Intent.ACTION_CONFIGURATION_CHANGED.equals(action)) {
334340
for (int i = 0; i < mAppWidgetServices.size(); i++) {

services/java/com/android/server/AppWidgetServiceImpl.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1778,13 +1778,16 @@ AtomicFile savedStateFile() {
17781778
return new AtomicFile(settingsFile);
17791779
}
17801780

1781-
void onUserRemoved() {
1781+
void onUserStopping() {
17821782
// prune the ones we don't want to keep
17831783
int N = mInstalledProviders.size();
17841784
for (int i = N - 1; i >= 0; i--) {
17851785
Provider p = mInstalledProviders.get(i);
17861786
cancelBroadcasts(p);
17871787
}
1788+
}
1789+
1790+
void onUserRemoved() {
17881791
getSettingsFile(mUserId).delete();
17891792
}
17901793

services/java/com/android/server/WallpaperManagerService.java

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -458,15 +458,21 @@ public void systemReady() {
458458

459459
IntentFilter userFilter = new IntentFilter();
460460
userFilter.addAction(Intent.ACTION_USER_REMOVED);
461+
userFilter.addAction(Intent.ACTION_USER_STOPPING);
461462
mContext.registerReceiver(new BroadcastReceiver() {
462463
@Override
463464
public void onReceive(Context context, Intent intent) {
464465
String action = intent.getAction();
465466
if (Intent.ACTION_USER_REMOVED.equals(action)) {
466-
removeUser(intent.getIntExtra(Intent.EXTRA_USER_HANDLE, 0));
467+
onRemoveUser(intent.getIntExtra(Intent.EXTRA_USER_HANDLE,
468+
UserHandle.USER_NULL));
469+
} else if (Intent.ACTION_USER_STOPPING.equals(action)) {
470+
onStoppingUser(intent.getIntExtra(Intent.EXTRA_USER_HANDLE,
471+
UserHandle.USER_NULL));
467472
}
468473
}
469474
}, userFilter);
475+
470476
try {
471477
ActivityManagerNative.getDefault().registerUserSwitchObserver(
472478
new IUserSwitchObserver.Stub() {
@@ -491,13 +497,24 @@ String getName() {
491497
}
492498
}
493499

494-
void removeUser(int userId) {
500+
void onStoppingUser(int userId) {
501+
if (userId < 1) return;
495502
synchronized (mLock) {
496503
WallpaperData wallpaper = mWallpaperMap.get(userId);
497504
if (wallpaper != null) {
498-
wallpaper.wallpaperObserver.stopWatching();
505+
if (wallpaper.wallpaperObserver != null) {
506+
wallpaper.wallpaperObserver.stopWatching();
507+
wallpaper.wallpaperObserver = null;
508+
}
499509
mWallpaperMap.remove(userId);
500510
}
511+
}
512+
}
513+
514+
void onRemoveUser(int userId) {
515+
if (userId < 1) return;
516+
synchronized (mLock) {
517+
onStoppingUser(userId);
501518
File wallpaperFile = new File(getWallpaperDir(userId), WALLPAPER);
502519
wallpaperFile.delete();
503520
File wallpaperInfoFile = new File(getWallpaperDir(userId), WALLPAPER_INFO);

services/java/com/android/server/pm/UserManagerService.java

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,7 @@ private UserManagerService(Context context, PackageManagerService pm,
149149
-1, -1);
150150
mUserListFile = new File(mUsersDir, USER_LIST_FILENAME);
151151
readUserListLocked();
152-
// Prune out any partially created users.
152+
// Prune out any partially created/partially removed users.
153153
ArrayList<UserInfo> partials = new ArrayList<UserInfo>();
154154
for (int i = 0; i < mUsers.size(); i++) {
155155
UserInfo ui = mUsers.valueAt(i);
@@ -459,7 +459,7 @@ private void readUserListLocked() {
459459
private void fallbackToSingleUserLocked() {
460460
// Create the primary user
461461
UserInfo primary = new UserInfo(0, "Primary", null,
462-
UserInfo.FLAG_ADMIN | UserInfo.FLAG_PRIMARY);
462+
UserInfo.FLAG_ADMIN | UserInfo.FLAG_PRIMARY | UserInfo.FLAG_INITIALIZED);
463463
mUsers.put(0, primary);
464464
mNextSerialNumber = MIN_USER_ID;
465465
updateUserIdsLocked();
@@ -703,6 +703,11 @@ public boolean removeUser(int userHandle) {
703703
return false;
704704
}
705705
mRemovingUserIds.add(userHandle);
706+
// Set this to a partially created user, so that the user will be purged
707+
// on next startup, in case the runtime stops now before stopping and
708+
// removing the user completely.
709+
user.partial = true;
710+
writeUserLocked(user);
706711
}
707712

708713
int res;

0 commit comments

Comments
 (0)