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
This commit is contained in:
“Ankita
2022-12-08 08:21:14 +00:00
parent 2e00513c55
commit 91a5994859
2 changed files with 34 additions and 0 deletions

View File

@@ -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;
}
}

View File

@@ -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<ApplicationInfo> 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);
}
}
/**