Only send SUSPENSION_CHANGED for changed packages

Additional fix follow up to Ie06df12ff3da6cf209fb3149e6aa80b0e63ce56e.

With the write commit refactor, this was sending unnecessary changed broadcasts.
This fixes it using a real old vs. new equality check, taking into account both
presence and deep object equality. This restricts the broadcasts to only being
sent if the package SuspendParams metadata has actually changed.

Bug: 245426112

Test: atest SuspendPackagesTest
Test: atest android.suspendapps.cts

Change-Id: Ic4287d2a7b1b8ae0779b234e5ccd8a7a0787f265
This commit is contained in:
Winson Chiu
2022-09-19 23:03:01 +00:00
committed by Winson
parent e775b14da7
commit 1e7d8779c2

View File

@@ -108,12 +108,12 @@ public final class SuspendPackageHelper {
final SuspendParams newSuspendParams =
new SuspendParams(dialogInfo, appExtras, launcherExtras);
final List<String> changedPackagesList = new ArrayList<>(packageNames.length);
final IntArray changedUids = new IntArray(packageNames.length);
final IntArray modifiedUids = new IntArray(packageNames.length);
final List<String> unmodifiablePackages = new ArrayList<>(packageNames.length);
ArraySet<String> modifiedPackages = new ArraySet<>();
final List<String> notifyPackagesList = new ArrayList<>(packageNames.length);
final IntArray notifyUids = new IntArray(packageNames.length);
final ArraySet<String> changedPackagesList = new ArraySet<>(packageNames.length);
final IntArray changedUids = new IntArray(packageNames.length);
final boolean[] canSuspend = suspended
? canSuspendPackageForUser(snapshot, packageNames, userId, callingUid) : null;
@@ -140,21 +140,17 @@ public final class SuspendPackageHelper {
final WatchedArrayMap<String, SuspendParams> suspendParamsMap =
packageState.getUserStateOrDefault(userId).getSuspendParams();
if (suspended) {
if (suspendParamsMap != null && suspendParamsMap.containsKey(packageName)) {
final SuspendParams suspendParams = suspendParamsMap.get(packageName);
// Skip if there's no changes
if (suspendParams != null
&& Objects.equals(suspendParams.getDialogInfo(), dialogInfo)
&& Objects.equals(suspendParams.getAppExtras(), appExtras)
&& Objects.equals(suspendParams.getLauncherExtras(),
launcherExtras)) {
// Carried over API behavior, must notify change even if no change
changedPackagesList.add(packageName);
changedUids.add(UserHandle.getUid(userId, packageState.getAppId()));
continue;
}
}
SuspendParams oldSuspendParams = suspendParamsMap == null
? null : suspendParamsMap.get(packageName);
boolean changed = !Objects.equals(oldSuspendParams, newSuspendParams);
if (suspended && !changed) {
// Carried over API behavior, must notify change even if no change
notifyPackagesList.add(packageName);
notifyUids.add(
UserHandle.getUid(userId, packageState.getAppId()));
continue;
}
// If only the callingPackage is suspending this package,
@@ -163,18 +159,21 @@ public final class SuspendPackageHelper {
&& CollectionUtils.size(suspendParamsMap) == 1
&& suspendParamsMap.containsKey(callingPackage);
if (suspended || packageUnsuspended) {
// Always notify of a suspend call + notify when fully unsuspended
notifyPackagesList.add(packageName);
notifyUids.add(UserHandle.getUid(userId, packageState.getAppId()));
}
if (changed) {
changedPackagesList.add(packageName);
changedUids.add(UserHandle.getUid(userId, packageState.getAppId()));
}
modifiedPackages.add(packageName);
modifiedUids.add(UserHandle.getUid(userId, packageState.getAppId()));
}
mPm.commitPackageStateMutation(null, mutator -> {
final int size = modifiedPackages.size();
final int size = changedPackagesList.size();
for (int index = 0; index < size; index++) {
final String packageName = modifiedPackages.valueAt(index);
final String packageName = changedPackagesList.valueAt(index);
final PackageUserStateWrite userState = mutator.forPackage(packageName)
.userState(userId);
if (suspended) {
@@ -185,19 +184,20 @@ public final class SuspendPackageHelper {
}
});
if (!changedPackagesList.isEmpty()) {
final String[] changedPackages = changedPackagesList.toArray(new String[0]);
if (!notifyPackagesList.isEmpty()) {
final String[] changedPackages =
notifyPackagesList.toArray(new String[0]);
sendPackagesSuspendedForUser(
suspended ? Intent.ACTION_PACKAGES_SUSPENDED
: Intent.ACTION_PACKAGES_UNSUSPENDED,
changedPackages, changedUids.toArray(), userId);
changedPackages, notifyUids.toArray(), userId);
sendMyPackageSuspendedOrUnsuspended(changedPackages, suspended, userId);
mPm.scheduleWritePackageRestrictions(userId);
}
// Send the suspension changed broadcast to ensure suspension state is not stale.
if (!modifiedPackages.isEmpty()) {
if (!changedPackagesList.isEmpty()) {
sendPackagesSuspendedForUser(Intent.ACTION_PACKAGES_SUSPENSION_CHANGED,
modifiedPackages.toArray(new String[0]), modifiedUids.toArray(), userId);
changedPackagesList.toArray(new String[0]), changedUids.toArray(), userId);
}
return unmodifiablePackages.toArray(new String[0]);
}