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

Commit e2411cf0 authored by Evan Severson's avatar Evan Severson Committed by Automerger Merge Worker
Browse files

Merge changes Ib9672d16,Id602b45e,I18b7cf48 into udc-dev am: b21934dc

parents d9f2a002 b21934dc
Loading
Loading
Loading
Loading
+0 −3
Original line number Diff line number Diff line
@@ -14687,9 +14687,6 @@ public class ActivityManagerService extends IActivityManager.Stub
                                    sendPackageBroadcastLocked(cmd,
                                            new String[] {ssp}, userId);
                                    if (fullUninstall) {
                                        mAppOpsService.packageRemoved(
                                                intent.getIntExtra(Intent.EXTRA_UID, -1), ssp);
                                        // Remove all permissions granted from/to this package
                                        mUgmInternal.removeUriPermissionsForPackage(ssp, userId,
                                                true, false);
+94 −114
Original line number Diff line number Diff line
@@ -60,8 +60,6 @@ import static android.app.AppOpsManager.opAllowSystemBypassRestriction;
import static android.app.AppOpsManager.opRestrictsRead;
import static android.app.AppOpsManager.opToName;
import static android.app.AppOpsManager.opToPublicName;
import static android.content.Intent.ACTION_PACKAGE_REMOVED;
import static android.content.Intent.EXTRA_REPLACING;
import static android.content.pm.PermissionInfo.PROTECTION_DANGEROUS;
import static android.content.pm.PermissionInfo.PROTECTION_FLAG_APPOP;

@@ -493,7 +491,8 @@ public class AppOpsService extends IAppOpsService.Stub {
    final class UidState {
        public final int uid;

        public ArrayMap<String, Ops> pkgOps;
        @NonNull
        public final ArrayMap<String, Ops> pkgOps = new ArrayMap<>();

        // true indicates there is an interested observer, false there isn't but it has such an op
        //TODO: Move foregroundOps and hasForegroundWatchers into the AppOpsServiceInterface.
@@ -506,13 +505,11 @@ public class AppOpsService extends IAppOpsService.Stub {

        public void clear() {
            mAppOpsCheckingService.removeUid(uid);
            if (pkgOps != null) {
                for (String packageName : pkgOps.keySet()) {
            for (int i = 0; i < pkgOps.size(); i++) {
                String packageName = pkgOps.keyAt(i);
                mAppOpsCheckingService.removePackage(packageName, UserHandle.getUserId(uid));
            }
        }
            pkgOps = null;
        }

        // Functions for uid mode access and manipulation.
        public SparseIntArray getNonDefaultUidModes() {
@@ -535,13 +532,11 @@ public class AppOpsService extends IAppOpsService.Stub {
        public void evalForegroundOps() {
            foregroundOps = null;
            foregroundOps = mAppOpsCheckingService.evalForegroundUidOps(uid, foregroundOps);
            if (pkgOps != null) {
            for (int i = pkgOps.size() - 1; i >= 0; i--) {
                foregroundOps = mAppOpsCheckingService
                        .evalForegroundPackageOps(pkgOps.valueAt(i).packageName, foregroundOps,
                                UserHandle.getUserId(uid));
            }
            }
            hasForegroundWatchers = false;
            if (foregroundOps != null) {
                for (int i = 0;  i < foregroundOps.size(); i++) {
@@ -960,7 +955,7 @@ public class AppOpsService extends IAppOpsService.Stub {
        LocalManagerRegistry.addManager(AppOpsManagerLocal.class, new AppOpsManagerLocalImpl());
    }

    /** Handler for work when packages are removed or updated */
    /** Handler for work when packages are updated */
    private BroadcastReceiver mOnPackageUpdatedReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
@@ -968,19 +963,7 @@ public class AppOpsService extends IAppOpsService.Stub {
            String pkgName = intent.getData().getEncodedSchemeSpecificPart();
            int uid = intent.getIntExtra(Intent.EXTRA_UID, Process.INVALID_UID);

            if (action.equals(ACTION_PACKAGE_REMOVED) && !intent.hasExtra(EXTRA_REPLACING)) {
                synchronized (AppOpsService.this) {
                    UidState uidState = mUidStates.get(uid);
                    if (uidState == null || uidState.pkgOps == null) {
                        return;
                    }
                    mAppOpsCheckingService.removePackage(pkgName, UserHandle.getUserId(uid));
                    Ops removedOps = uidState.pkgOps.remove(pkgName);
                    if (removedOps != null) {
                        scheduleFastWriteLocked();
                    }
                }
            } else if (action.equals(Intent.ACTION_PACKAGE_REPLACED)) {
            if (action.equals(Intent.ACTION_PACKAGE_REPLACED)) {
                AndroidPackage pkg = getPackageManagerInternal().getPackage(pkgName);
                if (pkg == null) {
                    return;
@@ -1007,7 +990,7 @@ public class AppOpsService extends IAppOpsService.Stub {

                synchronized (AppOpsService.this) {
                    UidState uidState = mUidStates.get(uid);
                    if (uidState == null || uidState.pkgOps == null) {
                    if (uidState == null) {
                        return;
                    }

@@ -1055,21 +1038,10 @@ public class AppOpsService extends IAppOpsService.Stub {
        mAppOpsCheckingService.systemReady();
        initializeUidStates();

        getUserManagerInternal().addUserLifecycleListener(
                new UserManagerInternal.UserLifecycleListener() {
                    @Override
                    public void onUserCreated(UserInfo user, Object token) {
                        initializeUserUidStates(user.id);
                    }

                    // onUserRemoved handled by #removeUser
                });

        mConstants.startMonitoring(mContext.getContentResolver());
        mHistoricalRegistry.systemReady(mContext.getContentResolver());

        IntentFilter packageUpdateFilter = new IntentFilter();
        packageUpdateFilter.addAction(Intent.ACTION_PACKAGE_REMOVED);
        packageUpdateFilter.addAction(Intent.ACTION_PACKAGE_REPLACED);
        packageUpdateFilter.addDataScheme("package");

@@ -1090,9 +1062,6 @@ public class AppOpsService extends IAppOpsService.Stub {
                }

                ArrayMap<String, Ops> pkgs = uidState.pkgOps;
                if (pkgs == null) {
                    continue;
                }

                int numPkgs = pkgs.size();
                for (int pkgNum = 0; pkgNum < numPkgs; pkgNum++) {
@@ -1114,6 +1083,52 @@ public class AppOpsService extends IAppOpsService.Stub {
            }
        }

        getUserManagerInternal().addUserLifecycleListener(
                new UserManagerInternal.UserLifecycleListener() {
                    @Override
                    public void onUserCreated(UserInfo user, Object token) {
                        initializeUserUidStates(user.id);
                    }

                    // onUserRemoved handled by #removeUser
                });

        getPackageManagerInternal().getPackageList(
                new PackageManagerInternal.PackageListObserver() {
                    @Override
                    public void onPackageAdded(String packageName, int appId) {
                        PackageInfo pi = getPackageManagerInternal().getPackageInfo(packageName,
                                PackageManager.GET_PERMISSIONS, Process.myUid(),
                                mContext.getUserId());
                        boolean isSamplingTarget = isSamplingTarget(pi);
                        int[] userIds = getUserManagerInternal().getUserIds();
                        synchronized (AppOpsService.this) {
                            if (isSamplingTarget) {
                                mRarelyUsedPackages.add(packageName);
                            }
                            for (int i = 0; i < userIds.length; i++) {
                                int uid = UserHandle.getUid(userIds[i], appId);
                                UidState uidState = getUidStateLocked(uid, true);
                                if (!uidState.pkgOps.containsKey(packageName)) {
                                    uidState.pkgOps.put(packageName,
                                            new Ops(packageName, uidState));
                                }
                            }
                        }
                    }

                    @Override
                    public void onPackageRemoved(String packageName, int appId) {
                        int[] userIds = getUserManagerInternal().getUserIds();
                        synchronized (AppOpsService.this) {
                            for (int i = 0; i < userIds.length; i++) {
                                int uid = UserHandle.getUid(userIds[i], appId);
                                packageRemovedLocked(uid, packageName);
                            }
                        }
                    }
                });

        final IntentFilter packageSuspendFilter = new IntentFilter();
        packageSuspendFilter.addAction(Intent.ACTION_PACKAGES_UNSUSPENDED);
        packageSuspendFilter.addAction(Intent.ACTION_PACKAGES_SUSPENDED);
@@ -1143,25 +1158,6 @@ public class AppOpsService extends IAppOpsService.Stub {
            }
        }, UserHandle.ALL, packageSuspendFilter, null, null);

        final IntentFilter packageAddedFilter = new IntentFilter();
        packageAddedFilter.addAction(Intent.ACTION_PACKAGE_ADDED);
        packageAddedFilter.addDataScheme("package");
        mContext.registerReceiver(new BroadcastReceiver() {
            @Override
            public void onReceive(Context context, Intent intent) {
                final Uri data = intent.getData();

                final String packageName = data.getSchemeSpecificPart();
                PackageInfo pi = getPackageManagerInternal().getPackageInfo(packageName,
                        PackageManager.GET_PERMISSIONS, Process.myUid(), mContext.getUserId());
                if (isSamplingTarget(pi)) {
                    synchronized (AppOpsService.this) {
                        mRarelyUsedPackages.add(packageName);
                    }
                }
            }
        }, packageAddedFilter);

        mHandler.postDelayed(new Runnable() {
            @Override
            public void run() {
@@ -1217,9 +1213,6 @@ public class AppOpsService extends IAppOpsService.Stub {
            PackageStateInternal packageState = packageStates.valueAt(j);
            int uid = UserHandle.getUid(userId, packageState.getAppId());
            UidState uidState = getUidStateLocked(uid, true);
            if (uidState.pkgOps == null) {
                uidState.pkgOps = new ArrayMap<>();
            }
            String packageName = packageStates.keyAt(j);
            Ops ops = new Ops(packageName, uidState);
            uidState.pkgOps.put(packageName, ops);
@@ -1246,8 +1239,15 @@ public class AppOpsService extends IAppOpsService.Stub {
        mCheckOpsDelegateDispatcher = new CheckOpsDelegateDispatcher(policy, delegate);
    }

    public void packageRemoved(int uid, String packageName) {
    @VisibleForTesting
    void packageRemoved(int uid, String packageName) {
        synchronized (this) {
            packageRemovedLocked(uid, packageName);
        }
    }

    @GuardedBy("this")
    private void packageRemovedLocked(int uid, String packageName) {
        UidState uidState = mUidStates.get(uid);
        if (uidState == null) {
            return;
@@ -1256,17 +1256,8 @@ public class AppOpsService extends IAppOpsService.Stub {
        Ops removedOps = null;

        // Remove any package state if such.
            if (uidState.pkgOps != null) {
        removedOps = uidState.pkgOps.remove(packageName);
        mAppOpsCheckingService.removePackage(packageName, UserHandle.getUserId(uid));
            }

            // If we just nuked the last package state check if the UID is valid.
            if (removedOps != null && uidState.pkgOps.isEmpty()
                    && getPackagesForUid(uid).length <= 0) {
                uidState.clear();
                mUidStates.remove(uid);
            }

        if (removedOps != null) {
            scheduleFastWriteLocked();
@@ -1289,7 +1280,6 @@ public class AppOpsService extends IAppOpsService.Stub {
                }
            }
        }
        }

        mHandler.post(PooledLambda.obtainRunnable(HistoricalRegistry::clearHistory,
                    mHistoricalRegistry, uid, packageName));
@@ -1322,7 +1312,7 @@ public class AppOpsService extends IAppOpsService.Stub {
                        mHandler.sendMessage(PooledLambda.obtainMessage(
                                AppOpsService::notifyOpChangedForAllPkgsInUid,
                                this, code, uidState.uid, true, null));
                    } else if (uidState.pkgOps != null) {
                    } else if (!uidState.pkgOps.isEmpty()) {
                        final ArraySet<OnOpModeChangedListener> listenerSet =
                                mAppOpsCheckingService.getOpModeChangedListeners(code);
                        if (listenerSet != null) {
@@ -1351,7 +1341,7 @@ public class AppOpsService extends IAppOpsService.Stub {
                }
            }

            if (uidState != null && uidState.pkgOps != null) {
            if (uidState != null) {
                int numPkgs = uidState.pkgOps.size();
                for (int pkgNum = 0; pkgNum < numPkgs; pkgNum++) {
                    Ops ops = uidState.pkgOps.valueAt(pkgNum);
@@ -1481,7 +1471,7 @@ public class AppOpsService extends IAppOpsService.Stub {
            final int uidStateCount = mUidStates.size();
            for (int i = 0; i < uidStateCount; i++) {
                UidState uidState = mUidStates.valueAt(i);
                if (uidState.pkgOps == null || uidState.pkgOps.isEmpty()) {
                if (uidState.pkgOps.isEmpty()) {
                    continue;
                }
                ArrayMap<String, Ops> packages = uidState.pkgOps;
@@ -2122,7 +2112,7 @@ public class AppOpsService extends IAppOpsService.Stub {
                    }
                }

                if (uidState.pkgOps == null) {
                if (uidState.pkgOps.isEmpty()) {
                    continue;
                }

@@ -3706,7 +3696,7 @@ public class AppOpsService extends IAppOpsService.Stub {
        // Do not check if uid/packageName/attributionTag is already known.
        synchronized (this) {
            UidState uidState = mUidStates.get(uid);
            if (uidState != null && uidState.pkgOps != null) {
            if (uidState != null && !uidState.pkgOps.isEmpty()) {
                Ops ops = uidState.pkgOps.get(packageName);

                if (ops != null && (attributionTag == null || ops.knownAttributionTags.contains(
@@ -3842,13 +3832,6 @@ public class AppOpsService extends IAppOpsService.Stub {
            return null;
        }

        if (uidState.pkgOps == null) {
            if (!edit) {
                return null;
            }
            uidState.pkgOps = new ArrayMap<>();
        }

        Ops ops = uidState.pkgOps.get(packageName);
        if (ops == null) {
            if (!edit) {
@@ -4159,9 +4142,6 @@ public class AppOpsService extends IAppOpsService.Stub {
            }
        }

        if (uidState.pkgOps == null) {
            uidState.pkgOps = new ArrayMap<>();
        }
        Ops ops = uidState.pkgOps.get(pkgName);
        if (ops == null) {
            ops = new Ops(pkgName, uidState);
@@ -5476,7 +5456,7 @@ public class AppOpsService extends IAppOpsService.Stub {

    private void updateStartedOpModeForUidLocked(int code, boolean restricted, int uid) {
        UidState uidState = mUidStates.get(uid);
        if (uidState == null || uidState.pkgOps == null) {
        if (uidState == null) {
            return;
        }

@@ -5600,7 +5580,7 @@ public class AppOpsService extends IAppOpsService.Stub {
                return;
            }
            UidState uidState = mUidStates.get(uid);
            if (uidState == null || uidState.pkgOps == null) {
            if (uidState == null) {
                return;
            }
            Ops removedOps = uidState.pkgOps.remove(packageName);