Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit e005e68a authored by Winson Chiu's avatar Winson Chiu Committed by Android (Google) Code Review
Browse files

Merge "Only send SUSPENSION_CHANGED for changed packages"

parents cea00539 1e7d8779
Loading
Loading
Loading
Loading
+29 −29
Original line number Diff line number Diff line
@@ -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,22 +140,18 @@ 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)) {

            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
                        changedPackagesList.add(packageName);
                        changedUids.add(UserHandle.getUid(userId, packageState.getAppId()));
                notifyPackagesList.add(packageName);
                notifyUids.add(
                        UserHandle.getUid(userId, packageState.getAppId()));
                continue;
            }
                }
            }

            // If only the callingPackage is suspending this package,
            // it will be unsuspended when this change is committed
@@ -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]);
    }