From 91a5994859cc6e6a6f8d7ff8ed7d6f4ae25f5501 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E2=80=9CAnkita?= Date: Thu, 8 Dec 2022 08:21:14 +0000 Subject: [PATCH] Register receiver for package added in clone profile. Existing PackageIntentReceiver listens to package changes in user-0 only. We also need to listen to if any package gets added in clone profile in order to refresh app list in Cloned Apps page. Bug: 259022623 Test: make RunSettingsLibRoboTests -j40 ROBOTEST_FILTER=ApplicationsStateRoboTest Change-Id: I82249a7c25967ea7b46b3cb85203656055e3472b --- .../settingslib/applications/AppUtils.java | 14 +++++++++++++ .../applications/ApplicationsState.java | 20 +++++++++++++++++++ 2 files changed, 34 insertions(+) diff --git a/packages/SettingsLib/src/com/android/settingslib/applications/AppUtils.java b/packages/SettingsLib/src/com/android/settingslib/applications/AppUtils.java index 7f65837d57167..cac3103a43280 100644 --- a/packages/SettingsLib/src/com/android/settingslib/applications/AppUtils.java +++ b/packages/SettingsLib/src/com/android/settingslib/applications/AppUtils.java @@ -32,6 +32,7 @@ import android.os.Environment; import android.os.RemoteException; import android.os.SystemProperties; import android.os.UserHandle; +import android.os.UserManager; import android.text.TextUtils; import android.util.Log; @@ -306,4 +307,17 @@ public class AppUtils { } } } + + /** + * Returns clone user profile id if present. Returns -1 if not present. + */ + public static int getCloneUserId(Context context) { + UserManager userManager = context.getSystemService(UserManager.class); + for (UserHandle userHandle : userManager.getUserProfiles()) { + if (userManager.getUserInfo(userHandle.getIdentifier()).isCloneProfile()) { + return userHandle.getIdentifier(); + } + } + return -1; + } } diff --git a/packages/SettingsLib/src/com/android/settingslib/applications/ApplicationsState.java b/packages/SettingsLib/src/com/android/settingslib/applications/ApplicationsState.java index ca5f57d3d6973..21e7d816d7273 100644 --- a/packages/SettingsLib/src/com/android/settingslib/applications/ApplicationsState.java +++ b/packages/SettingsLib/src/com/android/settingslib/applications/ApplicationsState.java @@ -123,6 +123,7 @@ public class ApplicationsState { final int mAdminRetrieveFlags; final int mRetrieveFlags; PackageIntentReceiver mPackageIntentReceiver; + PackageIntentReceiver mClonePackageIntentReceiver; boolean mResumed; boolean mHaveDisabledApps; @@ -265,6 +266,15 @@ public class ApplicationsState { mPackageIntentReceiver.registerReceiver(); } + // Listen to any package additions in clone user to refresh the app list. + if (mClonePackageIntentReceiver == null) { + int cloneUserId = AppUtils.getCloneUserId(mContext); + if (cloneUserId != -1) { + mClonePackageIntentReceiver = new PackageIntentReceiver(); + mClonePackageIntentReceiver.registerReceiverForClone(cloneUserId); + } + } + final List prevApplications = mApplications; mApplications = new ArrayList<>(); for (UserInfo user : mUm.getProfiles(UserHandle.myUserId())) { @@ -456,6 +466,10 @@ public class ApplicationsState { mPackageIntentReceiver.unregisterReceiver(); mPackageIntentReceiver = null; } + if (mClonePackageIntentReceiver != null) { + mClonePackageIntentReceiver.unregisterReceiver(); + mClonePackageIntentReceiver = null; + } } public AppEntry getEntry(String packageName, int userId) { @@ -1526,6 +1540,12 @@ public class ApplicationsState { removeUser(intent.getIntExtra(Intent.EXTRA_USER_HANDLE, UserHandle.USER_NULL)); } } + + public void registerReceiverForClone(int cloneId) { + IntentFilter filter = new IntentFilter(Intent.ACTION_PACKAGE_ADDED); + filter.addDataScheme("package"); + mContext.registerReceiverAsUser(this, UserHandle.of(cloneId), filter, null, null); + } } /**