From d9f60d9a1a39bdc3481d6505fbbc247bc8fef796 Mon Sep 17 00:00:00 2001 From: Himanshu Gupta Date: Tue, 16 May 2023 14:25:55 +0100 Subject: [PATCH] Clearing obsolete cloneUserId from CloneBackend. When cloned user is removed from device, the previous clonedUserId present with the CloneBackend instance becomes obsolete, and does not update to reflect the new clonedUserId, when the cloned user is re-created. To fix this we set cloneUserId to -1 when cloned user is removed, causing CloneBackend#installCloneApp to update clonedUserId when the cloned profile is re-created. Bug: 280431074, 275062025 Test: Manual by flashing the local build. Change-Id: If0d7960998865b4081502e84d0d4125b3b05c69c --- .../settings/applications/AppStateClonedAppsBridge.java | 5 +++++ .../applications/manageapplications/CloneBackend.java | 9 +++++++++ .../manageapplications/ManageApplications.java | 9 +++++++-- 3 files changed, 21 insertions(+), 2 deletions(-) diff --git a/src/com/android/settings/applications/AppStateClonedAppsBridge.java b/src/com/android/settings/applications/AppStateClonedAppsBridge.java index 3348079b294..719023d10e2 100644 --- a/src/com/android/settings/applications/AppStateClonedAppsBridge.java +++ b/src/com/android/settings/applications/AppStateClonedAppsBridge.java @@ -58,6 +58,11 @@ public class AppStateClonedAppsBridge extends AppStateBaseBridge{ mCloneProfileApps = mContext.getPackageManager() .getInstalledPackagesAsUser(GET_ACTIVITIES, mCloneUserId).stream().map(x -> x.packageName).toList(); + } else if (!mCloneProfileApps.isEmpty()) { + // In case we remove clone profile (mCloneUserId becomes -1), the bridge state should + // reflect the same by setting cloneProfileApps as empty, without building the entire + // page. + mCloneProfileApps = new ArrayList<>(); } final List allApps = mAppSession.getAllApps(); diff --git a/src/com/android/settings/applications/manageapplications/CloneBackend.java b/src/com/android/settings/applications/manageapplications/CloneBackend.java index 568631ec460..406c9304386 100644 --- a/src/com/android/settings/applications/manageapplications/CloneBackend.java +++ b/src/com/android/settings/applications/manageapplications/CloneBackend.java @@ -165,4 +165,13 @@ public class CloneBackend { public int getCloneUserId() { return mCloneUserId; } + + /** + * Resets {@link #mCloneUserId} to -1. + * Typically called after the cloneUser is removed, so that the obsolete clonedUserId present + * with the CloneBackend instance can be cleared. + */ + public void resetCloneUserId() { + mCloneUserId = -1; + } } diff --git a/src/com/android/settings/applications/manageapplications/ManageApplications.java b/src/com/android/settings/applications/manageapplications/ManageApplications.java index e0f1b5f702d..548ca553b40 100644 --- a/src/com/android/settings/applications/manageapplications/ManageApplications.java +++ b/src/com/android/settings/applications/manageapplications/ManageApplications.java @@ -923,10 +923,15 @@ public class ManageApplications extends InstrumentedFragment } IUserManager um = IUserManager.Stub.asInterface( ServiceManager.getService(Context.USER_SERVICE)); + CloneBackend cloneBackend = CloneBackend.getInstance(getContext()); try { // Warning: This removes all the data, media & images present in cloned user. - um.removeUser(clonedUserId); - mApplications.rebuild(); + if (um.removeUser(clonedUserId)) { + cloneBackend.resetCloneUserId(); + mApplications.rebuild(); + } else if (ManageApplications.DEBUG) { + Log.e(TAG, "Failed to remove cloned user"); + } } catch (RemoteException e) { Log.e(TAG, "Failed to remove cloned apps", e); Toast.makeText(getContext(),