Merge "Only send SUSPENSION_CHANGED for changed packages" into tm-qpr-dev

This commit is contained in:
TreeHugger Robot
2022-09-23 17:25:28 +00:00
committed by Android (Google) Code Review

View File

@@ -110,12 +110,12 @@ public final class SuspendPackageHelper {
final SuspendParams newSuspendParams = final SuspendParams newSuspendParams =
new SuspendParams(dialogInfo, appExtras, launcherExtras); 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); 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 final boolean[] canSuspend = suspended
? canSuspendPackageForUser(snapshot, packageNames, userId, callingUid) : null; ? canSuspendPackageForUser(snapshot, packageNames, userId, callingUid) : null;
@@ -143,21 +143,16 @@ public final class SuspendPackageHelper {
final WatchedArrayMap<String, SuspendParams> suspendParamsMap = final WatchedArrayMap<String, SuspendParams> suspendParamsMap =
packageState.getUserStateOrDefault(userId).getSuspendParams(); packageState.getUserStateOrDefault(userId).getSuspendParams();
if (suspended) {
if (suspendParamsMap != null && suspendParamsMap.containsKey(packageName)) { SuspendParams oldSuspendParams = suspendParamsMap == null
final SuspendParams suspendParams = suspendParamsMap.get(packageName); ? null : suspendParamsMap.get(packageName);
// Skip if there's no changes boolean changed = !Objects.equals(oldSuspendParams, newSuspendParams);
if (suspendParams != null
&& Objects.equals(suspendParams.getDialogInfo(), dialogInfo) if (suspended && !changed) {
&& Objects.equals(suspendParams.getAppExtras(), appExtras) // Carried over API behavior, must notify change even if no change
&& Objects.equals(suspendParams.getLauncherExtras(), notifyPackagesList.add(packageName);
launcherExtras)) { notifyUids.add(UserHandle.getUid(userId, packageState.getAppId()));
// Carried over API behavior, must notify change even if no change continue;
changedPackagesList.add(packageName);
changedUids.add(UserHandle.getUid(userId, packageState.getAppId()));
continue;
}
}
} }
// If only the callingPackage is suspending this package, // If only the callingPackage is suspending this package,
@@ -166,18 +161,21 @@ public final class SuspendPackageHelper {
&& CollectionUtils.size(suspendParamsMap) == 1 && CollectionUtils.size(suspendParamsMap) == 1
&& suspendParamsMap.containsKey(callingPackage); && suspendParamsMap.containsKey(callingPackage);
if (suspended || packageUnsuspended) { 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); changedPackagesList.add(packageName);
changedUids.add(UserHandle.getUid(userId, packageState.getAppId())); changedUids.add(UserHandle.getUid(userId, packageState.getAppId()));
} }
modifiedPackages.add(packageName);
modifiedUids.add(UserHandle.getUid(userId, packageState.getAppId()));
} }
mPm.commitPackageStateMutation(null, mutator -> { mPm.commitPackageStateMutation(null, mutator -> {
final int size = modifiedPackages.size(); final int size = changedPackagesList.size();
for (int index = 0; index < size; index++) { 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) final PackageUserStateWrite userState = mutator.forPackage(packageName)
.userState(userId); .userState(userId);
if (suspended) { if (suspended) {
@@ -190,19 +188,19 @@ public final class SuspendPackageHelper {
final Computer newSnapshot = mPm.snapshotComputer(); final Computer newSnapshot = mPm.snapshotComputer();
if (!changedPackagesList.isEmpty()) { if (!notifyPackagesList.isEmpty()) {
final String[] changedPackages = changedPackagesList.toArray(new String[0]); final String[] notifyPackages = notifyPackagesList.toArray(new String[0]);
sendPackagesSuspendedForUser(newSnapshot, sendPackagesSuspendedForUser(newSnapshot,
suspended ? Intent.ACTION_PACKAGES_SUSPENDED suspended ? Intent.ACTION_PACKAGES_SUSPENDED
: Intent.ACTION_PACKAGES_UNSUSPENDED, : Intent.ACTION_PACKAGES_UNSUSPENDED,
changedPackages, changedUids.toArray(), userId); notifyPackages, notifyUids.toArray(), userId);
sendMyPackageSuspendedOrUnsuspended(changedPackages, suspended, userId); sendMyPackageSuspendedOrUnsuspended(notifyPackages, suspended, userId);
mPm.scheduleWritePackageRestrictions(userId); mPm.scheduleWritePackageRestrictions(userId);
} }
// Send the suspension changed broadcast to ensure suspension state is not stale. // Send the suspension changed broadcast to ensure suspension state is not stale.
if (!modifiedPackages.isEmpty()) { if (!changedPackagesList.isEmpty()) {
sendPackagesSuspendedForUser(newSnapshot, Intent.ACTION_PACKAGES_SUSPENSION_CHANGED, sendPackagesSuspendedForUser(newSnapshot, 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]); return unmodifiablePackages.toArray(new String[0]);
} }