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

Commit 771ff857 authored by Joanne Chung's avatar Joanne Chung Committed by Android (Google) Code Review
Browse files

Merge "migrate PACKAGE_DATA_CLEARED and PACKAGE_RESTARTED off broadcasts" into main

parents 417bfba8 4f932d0c
Loading
Loading
Loading
Loading
+0 −3
Original line number Original line Diff line number Diff line
@@ -70,9 +70,6 @@ public abstract class PackageMonitor extends android.content.BroadcastReceiver {
        mPackageFilt = new IntentFilter();
        mPackageFilt = new IntentFilter();
        // Settings app sends the broadcast
        // Settings app sends the broadcast
        mPackageFilt.addAction(Intent.ACTION_QUERY_PACKAGE_RESTART);
        mPackageFilt.addAction(Intent.ACTION_QUERY_PACKAGE_RESTART);
        // AMS sends the broadcast
        mPackageFilt.addAction(Intent.ACTION_PACKAGE_RESTARTED);
        mPackageFilt.addAction(Intent.ACTION_PACKAGE_DATA_CLEARED);
        mPackageFilt.addDataScheme("package");
        mPackageFilt.addDataScheme("package");
        if (isCore) {
        if (isCore) {
            mPackageFilt.setPriority(IntentFilter.SYSTEM_HIGH_PRIORITY);
            mPackageFilt.setPriority(IntentFilter.SYSTEM_HIGH_PRIORITY);
+7 −1
Original line number Original line Diff line number Diff line
@@ -1421,7 +1421,7 @@ public abstract class PackageManagerInternal {
            @UserIdInt int userId);
            @UserIdInt int userId);


    /**
    /**
     * Sends the PACKAGE_RESTARTED broadcast on the package manager handler thread.
     * Sends the PACKAGE_RESTARTED broadcast.
     */
     */
    public abstract void sendPackageRestartedBroadcast(@NonNull String packageName,
    public abstract void sendPackageRestartedBroadcast(@NonNull String packageName,
            int uid, @Intent.Flags int flags);
            int uid, @Intent.Flags int flags);
@@ -1431,4 +1431,10 @@ public abstract class PackageManagerInternal {
     */
     */
    public abstract ParceledListSlice<PackageInstaller.SessionInfo> getHistoricalSessions(
    public abstract ParceledListSlice<PackageInstaller.SessionInfo> getHistoricalSessions(
            int userId);
            int userId);

    /**
     * Sends the ACTION_PACKAGE_DATA_CLEARED broadcast.
     */
    public abstract void sendPackageDataClearedBroadcast(@NonNull String packageName,
            int uid, int userId, boolean isRestore, boolean isInstantApp);
}
}
+5 −47
Original line number Original line Diff line number Diff line
@@ -3634,30 +3634,10 @@ public class ActivityManagerService extends IActivityManager.Stub
                    }
                    }
                    if (succeeded) {
                    if (succeeded) {
                        final Intent intent = new Intent(Intent.ACTION_PACKAGE_DATA_CLEARED,
                                Uri.fromParts("package", packageName, null /* fragment */));
                        mPackageManagerInt.sendPackageDataClearedBroadcast(packageName,
                        intent.addFlags(Intent.FLAG_RECEIVER_INCLUDE_BACKGROUND
                                ((appInfo != null) ? appInfo.uid : INVALID_UID), resolvedUserId,
                                | Intent.FLAG_RECEIVER_REGISTERED_ONLY_BEFORE_BOOT);
                                isRestore, isInstantApp);
                        intent.putExtra(Intent.EXTRA_UID,
                                (appInfo != null) ? appInfo.uid : INVALID_UID);
                        intent.putExtra(Intent.EXTRA_USER_HANDLE, resolvedUserId);
                        if (isRestore) {
                            intent.putExtra(Intent.EXTRA_IS_RESTORE, true);
                        }
                        if (isInstantApp) {
                            intent.putExtra(Intent.EXTRA_PACKAGE_NAME, packageName);
                        }
                        final int[] visibilityAllowList = mPackageManagerInt.getVisibilityAllowList(
                                packageName, resolvedUserId);
                        broadcastIntentInPackage("android", null /* featureId */,
                                SYSTEM_UID, uid, pid, intent, null /* resolvedType */,
                                null /* resultToApp */, null /* resultTo */, 0 /* resultCode */,
                                null /* resultData */, null /* resultExtras */,
                                isInstantApp ? permission.ACCESS_INSTANT_APPS : null,
                                null /* bOptions */, false /* serialized */, false /* sticky */,
                                resolvedUserId, BackgroundStartPrivileges.NONE,
                                visibilityAllowList);
                    }
                    }
                    if (observer != null) {
                    if (observer != null) {
@@ -4178,29 +4158,7 @@ public class ActivityManagerService extends IActivityManager.Stub
            flags = Intent.FLAG_RECEIVER_REGISTERED_ONLY
            flags = Intent.FLAG_RECEIVER_REGISTERED_ONLY
                    | Intent.FLAG_RECEIVER_FOREGROUND;
                    | Intent.FLAG_RECEIVER_FOREGROUND;
        }
        }
        if (android.content.pm.Flags.stayStopped()) {
        mPackageManagerInt.sendPackageRestartedBroadcast(packageName, uid, flags);
            // Sent async using the PM handler, to maintain ordering with PACKAGE_UNSTOPPED
            mPackageManagerInt.sendPackageRestartedBroadcast(packageName,
                    uid, flags);
        } else {
            Intent intent = new Intent(Intent.ACTION_PACKAGE_RESTARTED,
                    Uri.fromParts("package", packageName, null));
            intent.addFlags(flags);
            final int userId = UserHandle.getUserId(uid);
            final int[] broadcastAllowList =
                    getPackageManagerInternal().getVisibilityAllowList(packageName, userId);
            intent.putExtra(Intent.EXTRA_UID, uid);
            intent.putExtra(Intent.EXTRA_USER_HANDLE, userId);
            broadcastIntentLocked(null /* callerApp */, null /* callerPackage */,
                    null /* callerFeatureId */, intent, null /* resolvedType */,
                    null /* resultToApp */, null /* resultTo */,
                    0 /* resultCode */, null /* resultData */, null /* resultExtras */,
                    null /* requiredPermissions */, null /* excludedPermissions */,
                    null /* excludedPackages */, OP_NONE, null /* bOptions */, false /* ordered */,
                    false /* sticky */, MY_PID, SYSTEM_UID, Binder.getCallingUid(),
                    Binder.getCallingPid(), userId, BackgroundStartPrivileges.NONE,
                    broadcastAllowList, null /* filterExtrasForReceiver */);
        }
    }
    }
    private void cleanupDisabledPackageComponentsLocked(
    private void cleanupDisabledPackageComponentsLocked(
+47 −22
Original line number Original line Diff line number Diff line
@@ -100,6 +100,22 @@ public final class BroadcastHelper {
        mAppsFilter = injector.getAppsFilter();
        mAppsFilter = injector.getAppsFilter();
    }
    }


    /**
     * Sends a broadcast to registered clients on userId for the given Intent.
     */
    void sendPackageBroadcastWithIntent(Intent intent, int userId, boolean isInstantApp,
            @Intent.Flags int flags,
            int[] visibilityAllowList,
            final IIntentReceiver finishedReceiver,
            @Nullable BiFunction<Integer, Bundle, Bundle> filterExtrasForReceiver,
            @Nullable Bundle bOptions) {
        intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY_BEFORE_BOOT | flags);
        SparseArray<int[]> broadcastAllowList = new SparseArray<>();
        broadcastAllowList.put(userId, visibilityAllowList);
        broadcastIntent(intent, finishedReceiver, isInstantApp, userId, broadcastAllowList,
                filterExtrasForReceiver, bOptions);
    }

    void sendPackageBroadcast(final String action, final String pkg, final Bundle extras,
    void sendPackageBroadcast(final String action, final String pkg, final Bundle extras,
            final int flags, final String targetPkg, final IIntentReceiver finishedReceiver,
            final int flags, final String targetPkg, final IIntentReceiver finishedReceiver,
            final int[] userIds, int[] instantUserIds,
            final int[] userIds, int[] instantUserIds,
@@ -152,8 +168,6 @@ public final class BroadcastHelper {
        for (int userId : userIds) {
        for (int userId : userIds) {
            final Intent intent = new Intent(action,
            final Intent intent = new Intent(action,
                    pkg != null ? Uri.fromParts(PACKAGE_SCHEME, pkg, null) : null);
                    pkg != null ? Uri.fromParts(PACKAGE_SCHEME, pkg, null) : null);
            final String[] requiredPermissions =
                    isInstantApp ? INSTANT_APP_BROADCAST_PERMISSION : null;
            if (extras != null) {
            if (extras != null) {
                intent.putExtras(extras);
                intent.putExtras(extras);
            }
            }
@@ -172,6 +186,18 @@ public final class BroadcastHelper {
            }
            }
            intent.putExtra(Intent.EXTRA_USER_HANDLE, userId);
            intent.putExtra(Intent.EXTRA_USER_HANDLE, userId);
            intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY_BEFORE_BOOT | flags);
            intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY_BEFORE_BOOT | flags);
            broadcastIntent(intent, finishedReceiver, isInstantApp, userId, broadcastAllowList,
                    filterExtrasForReceiver, bOptions);
        }
    }


    private void broadcastIntent(Intent intent, IIntentReceiver finishedReceiver,
            boolean isInstantApp, int userId, @Nullable SparseArray<int[]> broadcastAllowList,
            @Nullable BiFunction<Integer, Bundle, Bundle> filterExtrasForReceiver,
            @Nullable Bundle bOptions) {
        final String[] requiredPermissions =
                isInstantApp ? INSTANT_APP_BROADCAST_PERMISSION : null;
        if (DEBUG_BROADCASTS) {
        if (DEBUG_BROADCASTS) {
            RuntimeException here = new RuntimeException("here");
            RuntimeException here = new RuntimeException("here");
            here.fillInStackTrace();
            here.fillInStackTrace();
@@ -194,7 +220,6 @@ public final class BroadcastHelper {
                broadcastAllowList == null ? null : broadcastAllowList.get(userId),
                broadcastAllowList == null ? null : broadcastAllowList.get(userId),
                filterExtrasForReceiver, bOptions);
                filterExtrasForReceiver, bOptions);
    }
    }
    }


    void sendResourcesChangedBroadcast(@NonNull Computer snapshot,
    void sendResourcesChangedBroadcast(@NonNull Computer snapshot,
                                       boolean mediaStatus,
                                       boolean mediaStatus,
+39 −3
Original line number Original line Diff line number Diff line
@@ -6972,7 +6972,9 @@ public class PackageManagerService implements PackageSender, TestUtilityService
            final Bundle extras = new Bundle();
            final Bundle extras = new Bundle();
            extras.putInt(Intent.EXTRA_UID, uid);
            extras.putInt(Intent.EXTRA_UID, uid);
            extras.putInt(Intent.EXTRA_USER_HANDLE, userId);
            extras.putInt(Intent.EXTRA_USER_HANDLE, userId);
            if (android.content.pm.Flags.stayStopped()) {
                extras.putLong(Intent.EXTRA_TIME, SystemClock.elapsedRealtime());
                extras.putLong(Intent.EXTRA_TIME, SystemClock.elapsedRealtime());
                // Sent async using the PM handler, to maintain ordering with PACKAGE_UNSTOPPED
                mHandler.post(() -> {
                mHandler.post(() -> {
                    mBroadcastHelper.sendPackageBroadcast(Intent.ACTION_PACKAGE_RESTARTED,
                    mBroadcastHelper.sendPackageBroadcast(Intent.ACTION_PACKAGE_RESTARTED,
                            packageName, extras,
                            packageName, extras,
@@ -6980,6 +6982,40 @@ public class PackageManagerService implements PackageSender, TestUtilityService
                            userIds, null, broadcastAllowList, null,
                            userIds, null, broadcastAllowList, null,
                            null);
                            null);
                });
                });
            } else {
                mBroadcastHelper.sendPackageBroadcast(Intent.ACTION_PACKAGE_RESTARTED,
                        packageName, extras,
                        flags, null, null,
                        userIds, null, broadcastAllowList, null,
                        null);
            }
            mPackageMonitorCallbackHelper.notifyPackageMonitor(Intent.ACTION_PACKAGE_RESTARTED,
                    packageName, extras, userIds, null /* instantUserIds */,
                    broadcastAllowList, mHandler);
        }

        @Override
        public void sendPackageDataClearedBroadcast(@NonNull String packageName,
                int uid, int userId, boolean isRestore, boolean isInstantApp) {
            int[] visibilityAllowList =
                    snapshotComputer().getVisibilityAllowList(packageName, userId);
            final Intent intent = new Intent(Intent.ACTION_PACKAGE_DATA_CLEARED,
                    Uri.fromParts("package", packageName, null /* fragment */));
            intent.addFlags(Intent.FLAG_RECEIVER_INCLUDE_BACKGROUND
                    | Intent.FLAG_RECEIVER_REGISTERED_ONLY_BEFORE_BOOT);
            intent.putExtra(Intent.EXTRA_UID, uid);
            intent.putExtra(Intent.EXTRA_USER_HANDLE, userId);
            if (isRestore) {
                intent.putExtra(Intent.EXTRA_IS_RESTORE, true);
            }
            if (isInstantApp) {
                intent.putExtra(Intent.EXTRA_PACKAGE_NAME, packageName);
            }
            mBroadcastHelper.sendPackageBroadcastWithIntent(intent, userId, isInstantApp,
                    0 /* flags */, visibilityAllowList, null /* finishedReceiver */,
                    null /* filterExtrasForReceiver */, null /* bOptions */);
            mPackageMonitorCallbackHelper.notifyPackageMonitorWithIntent(intent, userId,
                    visibilityAllowList, mHandler);
        }
        }
    }
    }


Loading