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

Commit d325d1db authored by Nate Myren's avatar Nate Myren
Browse files

Revert "Reduce ways UidState objects are created"

Revert submission 21960657

Reason for revert: Testing if this change is a possible cause for b/275108108

Reverted changes: /q/submissionid:21960657

Change-Id: I9346ea2968488b4a3cf8b2b4d532cdf2339b726a
parent 397569f6
Loading
Loading
Loading
Loading
+55 −72
Original line number Diff line number Diff line
@@ -230,14 +230,6 @@ public class AppOpsService extends IAppOpsService.Stub {
    private static final int MAX_UNUSED_POOLED_OBJECTS = 3;
    private static final int RARELY_USED_PACKAGES_INITIALIZATION_DELAY_MILLIS = 300000;

    /* Temporary solution before Uidstate class is removed. These uids get their modes set. */
    private static final int[] NON_PACKAGE_UIDS = new int[]{
            Process.PHONE_UID,
            Process.BLUETOOTH_UID,
            Process.NFC_UID,
            Process.NETWORK_STACK_UID,
            Process.SHELL_UID};

    final Context mContext;
    final AtomicFile mStorageFile;
    final AtomicFile mRecentAccessesFile;
@@ -1065,7 +1057,7 @@ public class AppOpsService extends IAppOpsService.Stub {
                UidState uidState = mUidStates.valueAt(uidNum);

                String[] pkgsInUid = getPackagesForUid(uidState.uid);
                if (ArrayUtils.isEmpty(pkgsInUid) && uid >= Process.FIRST_APPLICATION_UID) {
                if (ArrayUtils.isEmpty(pkgsInUid)) {
                    uidState.clear();
                    mUidStates.removeAt(uidNum);
                    scheduleFastWriteLocked();
@@ -1094,7 +1086,51 @@ public class AppOpsService extends IAppOpsService.Stub {
            }
        }

        prepareInternalCallbacks();
        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);
@@ -1153,55 +1189,6 @@ public class AppOpsService extends IAppOpsService.Stub {
        mActivityManagerInternal = LocalServices.getService(ActivityManagerInternal.class);
    }

    @VisibleForTesting
    void prepareInternalCallbacks() {
        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);
                            }
                        }
                    }
                });
    }

    /**
     * Initialize uid state objects for state contained in the checking service.
     */
@@ -1218,10 +1205,6 @@ public class AppOpsService extends IAppOpsService.Stub {
                    initializeUserUidStatesLocked(userId, packageStates);
                }
            }

            for (int uid : NON_PACKAGE_UIDS) {
                mUidStates.put(uid, new UidState(uid));
            }
        }
    }

@@ -1340,7 +1323,7 @@ public class AppOpsService extends IAppOpsService.Stub {
    // The callback method from AppOpsUidStateTracker
    private void onUidStateChanged(int uid, int state, boolean foregroundModeMayChange) {
        synchronized (this) {
            UidState uidState = getUidStateLocked(uid, false);
            UidState uidState = getUidStateLocked(uid, true);

            if (uidState != null && foregroundModeMayChange && uidState.hasForegroundWatchers) {
                for (int fgi = uidState.foregroundOps.size() - 1; fgi >= 0; fgi--) {
@@ -1413,6 +1396,12 @@ public class AppOpsService extends IAppOpsService.Stub {
            @ActivityManager.ProcessCapability int capability) {
        synchronized (this) {
            getUidStateTracker().updateUidProcState(uid, procState, capability);
            if (!mUidStates.contains(uid)) {
                UidState uidState = new UidState(uid);
                mUidStates.put(uid, uidState);
                onUidStateChanged(uid,
                        AppOpsUidStateTracker.processStateToUidState(procState), false);
            }
        }
    }

@@ -1547,7 +1536,7 @@ public class AppOpsService extends IAppOpsService.Stub {
                return null;
            }
            ArrayList<AppOpsManager.OpEntry> resOps = collectOps(pkgOps, ops);
            if (resOps == null || resOps.size() == 0) {
            if (resOps == null) {
                return null;
            }
            ArrayList<AppOpsManager.PackageOps> res = new ArrayList<AppOpsManager.PackageOps>();
@@ -1803,12 +1792,6 @@ public class AppOpsService extends IAppOpsService.Stub {
                if (mode == defaultMode) {
                    return;
                }
                if (uid >= Process.FIRST_APPLICATION_UID) {
                    // TODO change to a throw; no crashing for now.
                    Slog.e(TAG, "Trying to set mode for unknown uid " + uid + ".");
                }
                // I suppose we'll support setting these uids. Shouldn't matter later when UidState
                // is removed.
                uidState = new UidState(uid);
                mUidStates.put(uid, uidState);
            }
@@ -3925,7 +3908,7 @@ public class AppOpsService extends IAppOpsService.Stub {
     */
    private Ops getOpsLocked(int uid, String packageName, @Nullable String attributionTag,
            boolean isAttributionTagValid, @Nullable RestrictionBypass bypass, boolean edit) {
        UidState uidState = getUidStateLocked(uid, false);
        UidState uidState = getUidStateLocked(uid, edit);
        if (uidState == null) {
            return null;
        }
+0 −12
Original line number Diff line number Diff line
@@ -23,7 +23,6 @@ import static android.app.AppOpsManager.OP_FLAG_SELF;
import static android.app.AppOpsManager.OP_READ_SMS;
import static android.app.AppOpsManager.OP_WIFI_SCAN;
import static android.app.AppOpsManager.OP_WRITE_SMS;
import static android.os.UserHandle.getAppId;
import static android.os.UserHandle.getUserId;

import static com.android.dx.mockito.inline.extended.ExtendedMockito.doNothing;
@@ -109,7 +108,6 @@ public class AppOpsServiceTest {
        mAppOpsService = new AppOpsService(mRecentAccessesFile, mStorageFile, mHandler,
                spy(sContext));
        mAppOpsService.mHistoricalRegistry.systemReady(sContext.getContentResolver());
        mAppOpsService.prepareInternalCallbacks();

        // Always approve all permission checks
        doNothing().when(mAppOpsService.mContext).enforcePermission(anyString(), anyInt(),
@@ -186,16 +184,6 @@ public class AppOpsServiceTest {
        // Mock behavior to use specific Settings.Global.APPOP_HISTORY_PARAMETERS
        doReturn(null).when(() -> Settings.Global.getString(any(ContentResolver.class),
                eq(Settings.Global.APPOP_HISTORY_PARAMETERS)));

        prepareInstallInvocation(mockPackageManagerInternal);
    }

    private void prepareInstallInvocation(PackageManagerInternal mockPackageManagerInternal) {
        when(mockPackageManagerInternal.getPackageList(any())).thenAnswer(invocation -> {
            PackageManagerInternal.PackageListObserver observer = invocation.getArgument(0);
            observer.onPackageAdded(sMyPackageName, getAppId(mMyUid));
            return null;
        });
    }

    @Test