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

Commit 89daef04 authored by Patrick Baumann's avatar Patrick Baumann
Browse files

Revert "Only update cache for new user on user create"

This reverts commit d978d634.

Reason for revert: b/192281479
Bug: 192281479

Change-Id: I6bfd2c880b3f4df1238c2d746eec49e36a931fa6
Merged-In: I6bfd2c880b3f4df1238c2d746eec49e36a931fa6
parent 2c593f65
Loading
Loading
Loading
Loading
+28 −66
Original line number Diff line number Diff line
@@ -17,7 +17,6 @@
package com.android.server.pm;

import static android.os.Trace.TRACE_TAG_PACKAGE_MANAGER;
import static android.os.UserHandle.USER_ALL;
import static android.provider.DeviceConfig.NAMESPACE_PACKAGE_MANAGER_SERVICE;

import static com.android.internal.annotations.VisibleForTesting.Visibility.PRIVATE;
@@ -686,7 +685,7 @@ public class AppsFilter implements Watchable, Snappable {
                synchronized (mCacheLock) {
                    if (mShouldFilterCache != null) {
                        updateShouldFilterCacheForPackage(mShouldFilterCache, null, newPkgSetting,
                                settings, users, USER_ALL, settings.size());
                                settings, users, settings.size());
                        if (additionalChangedPackages != null) {
                            for (int index = 0; index < additionalChangedPackages.size(); index++) {
                                String changedPackage = additionalChangedPackages.valueAt(index);
@@ -699,8 +698,7 @@ public class AppsFilter implements Watchable, Snappable {
                                }

                                updateShouldFilterCacheForPackage(mShouldFilterCache, null,
                                        changedPkgSetting, settings, users, USER_ALL,
                                        settings.size());
                                        changedPkgSetting, settings, users, settings.size());
                            }
                        }
                    } // else, rebuild entire cache when system is ready
@@ -832,51 +830,24 @@ public class AppsFilter implements Watchable, Snappable {
            }
        }
    }
    private void updateEntireShouldFilterCache() {
        updateEntireShouldFilterCache(USER_ALL);
    }

    private void updateEntireShouldFilterCache(int subjectUserId) {
    private void updateEntireShouldFilterCache() {
        mStateProvider.runWithState((settings, users) -> {
            int userId = subjectUserId;
            if (!ArrayUtils.contains(users, subjectUserId)) {
                Slog.e(TAG, "We encountered a new user that isn't a member of known users, "
                        + "updating the whole cache");
                userId = USER_ALL;
            }
            WatchedSparseBooleanMatrix cache =
                    updateEntireShouldFilterCacheInner(settings, users, userId);
                    updateEntireShouldFilterCacheInner(settings, users);
            synchronized (mCacheLock) {
                if (userId != USER_ALL) {
                    // if we're only updating a single user id, we need to copy over the prior
                    // cached values for the other users.
                    int[] uids = mShouldFilterCache.keys();
                    for (int i = 0; i < uids.length; i++) {
                        int uid1 = uids[i];
                        if (UserHandle.getUserId(uid1) == userId) {
                            continue;
                        }
                        for (int j = 0; j < uids.length; j++) {
                            int uid2 = uids[j];
                            if (UserHandle.getUserId(uid2) == userId) {
                                continue;
                            }
                            cache.setValueAt(uid1, uid2, mShouldFilterCache.valueAt(uid1, uid2));
                        }
                    }
                }
                mShouldFilterCache = cache;
            }
        });
    }

    private WatchedSparseBooleanMatrix updateEntireShouldFilterCacheInner(
            ArrayMap<String, PackageSetting> settings, UserInfo[] users, int subjectUserId) {
            ArrayMap<String, PackageSetting> settings, UserInfo[] users) {
        WatchedSparseBooleanMatrix cache =
                new WatchedSparseBooleanMatrix(users.length * settings.size());
        for (int i = settings.size() - 1; i >= 0; i--) {
            updateShouldFilterCacheForPackage(cache,
                    null /*skipPackage*/, settings.valueAt(i), settings, users, subjectUserId, i);
                    null /*skipPackage*/, settings.valueAt(i), settings, users, i);
        }
        return cache;
    }
@@ -897,8 +868,8 @@ public class AppsFilter implements Watchable, Snappable {
                    packagesCache.put(settings.keyAt(i), pkg);
                }
            });
            WatchedSparseBooleanMatrix cache = updateEntireShouldFilterCacheInner(
                    settingsCopy, usersRef[0], USER_ALL);
            WatchedSparseBooleanMatrix cache =
                    updateEntireShouldFilterCacheInner(settingsCopy, usersRef[0]);
            boolean[] changed = new boolean[1];
            // We have a cache, let's make sure the world hasn't changed out from under us.
            mStateProvider.runWithState((settings, users) -> {
@@ -928,10 +899,10 @@ public class AppsFilter implements Watchable, Snappable {
        });
    }

    public void onUserCreated(int newUserId) {
    public void onUsersChanged() {
        synchronized (mCacheLock) {
            if (mShouldFilterCache != null) {
                updateEntireShouldFilterCache(newUserId);
                updateEntireShouldFilterCache();
                onChanged();
            }
        }
@@ -942,7 +913,7 @@ public class AppsFilter implements Watchable, Snappable {
            if (mShouldFilterCache != null) {
                mStateProvider.runWithState((settings, users) -> {
                    updateShouldFilterCacheForPackage(mShouldFilterCache, null /* skipPackage */,
                            settings.get(packageName), settings, users, USER_ALL,
                            settings.get(packageName), settings, users,
                            settings.size() /*maxIndex*/);
                });
            }
@@ -951,7 +922,7 @@ public class AppsFilter implements Watchable, Snappable {

    private void updateShouldFilterCacheForPackage(WatchedSparseBooleanMatrix cache,
            @Nullable String skipPackageName, PackageSetting subjectSetting, ArrayMap<String,
            PackageSetting> allSettings, UserInfo[] allUsers, int subjectUserId, int maxIndex) {
            PackageSetting> allSettings, UserInfo[] allUsers, int maxIndex) {
        for (int i = Math.min(maxIndex, allSettings.size() - 1); i >= 0; i--) {
            PackageSetting otherSetting = allSettings.valueAt(i);
            if (subjectSetting.appId == otherSetting.appId) {
@@ -961,31 +932,22 @@ public class AppsFilter implements Watchable, Snappable {
            if (subjectSetting.name == skipPackageName || otherSetting.name == skipPackageName) {
                continue;
            }
            if (subjectUserId == USER_ALL) {
                for (int su = 0; su < allUsers.length; su++) {
                    updateShouldFilterCacheForUser(cache, subjectSetting, allUsers, otherSetting,
                            allUsers[su].id);
                }
            } else {
                updateShouldFilterCacheForUser(cache, subjectSetting, allUsers, otherSetting,
                        subjectUserId);
            }
        }
    }

    private void updateShouldFilterCacheForUser(WatchedSparseBooleanMatrix cache,
            PackageSetting subjectSetting, UserInfo[] allUsers, PackageSetting otherSetting,
            int subjectUserId) {
        for (int ou = 0; ou < allUsers.length; ou++) {
            final int userCount = allUsers.length;
            final int appxUidCount = userCount * allSettings.size();
            for (int su = 0; su < userCount; su++) {
                int subjectUser = allUsers[su].id;
                for (int ou = 0; ou < userCount; ou++) {
                    int otherUser = allUsers[ou].id;
            int subjectUid = UserHandle.getUid(subjectUserId, subjectSetting.appId);
                    int subjectUid = UserHandle.getUid(subjectUser, subjectSetting.appId);
                    int otherUid = UserHandle.getUid(otherUser, otherSetting.appId);
                    cache.put(subjectUid, otherUid,
                            shouldFilterApplicationInternal(
                                    subjectUid, subjectSetting, otherSetting, otherUser));
                    cache.put(otherUid, subjectUid,
                            shouldFilterApplicationInternal(
                            otherUid, otherSetting, subjectSetting, subjectUserId));
                                    otherUid, otherSetting, subjectSetting, subjectUser));
                }
            }
        }
    }

@@ -1183,7 +1145,7 @@ public class AppsFilter implements Watchable, Snappable {
                            continue;
                        }
                        updateShouldFilterCacheForPackage(mShouldFilterCache, setting.name,
                                siblingSetting, settings, users, USER_ALL, settings.size());
                                siblingSetting, settings, users, settings.size());
                    }
                }

@@ -1200,7 +1162,7 @@ public class AppsFilter implements Watchable, Snappable {
                            }

                            updateShouldFilterCacheForPackage(mShouldFilterCache, null,
                                    changedPkgSetting, settings, users, USER_ALL, settings.size());
                                    changedPkgSetting, settings, users, settings.size());
                        }
                    }
                }
+1 −1
Original line number Diff line number Diff line
@@ -26500,7 +26500,7 @@ public class PackageManagerService extends IPackageManager.Stub
        synchronized (mLock) {
            scheduleWritePackageRestrictionsLocked(userId);
            scheduleWritePackageListLocked(userId);
            mAppsFilter.onUserCreated(userId);
            mAppsFilter.onUsersChanged();
        }
    }
+2 −51
Original line number Diff line number Diff line
@@ -83,17 +83,9 @@ public class AppsFilterTest {
    private static final int DUMMY_OVERLAY_APPID = 10756;
    private static final int SYSTEM_USER = 0;
    private static final int SECONDARY_USER = 10;
    private static final int ADDED_USER = 11;
    private static final int[] USER_ARRAY = {SYSTEM_USER, SECONDARY_USER};
    private static final int[] USER_ARRAY_WITH_ADDED = {SYSTEM_USER, SECONDARY_USER, ADDED_USER};
    private static final UserInfo[] USER_INFO_LIST = toUserInfos(USER_ARRAY);
    private static final UserInfo[] USER_INFO_LIST_WITH_ADDED = toUserInfos(USER_ARRAY_WITH_ADDED);

    private static UserInfo[] toUserInfos(int[] userIds) {
        return Arrays.stream(userIds)
                .mapToObj(id -> new UserInfo(id, Integer.toString(id), 0))
                .toArray(UserInfo[]::new);
    }
    private static final UserInfo[] USER_INFO_LIST = Arrays.stream(USER_ARRAY).mapToObj(
            id -> new UserInfo(id, Integer.toString(id), 0)).toArray(UserInfo[]::new);

    @Mock
    AppsFilter.FeatureConfig mFeatureConfigMock;
@@ -326,47 +318,6 @@ public class AppsFilterTest {
        watcher.verifyNoChangeReported("shouldFilterApplication");
    }

    @Test
    public void testOnUserCreated_FilterMatches() throws Exception {
        final AppsFilter appsFilter =
                new AppsFilter(mStateProvider, mFeatureConfigMock, new String[]{}, false, null,
                        mMockExecutor);
        simulateAddBasicAndroid(appsFilter);

        appsFilter.onSystemReady();

        PackageSetting target = simulateAddPackage(appsFilter,
                pkgWithProvider("com.some.package", "com.some.authority"), DUMMY_TARGET_APPID);
        PackageSetting calling = simulateAddPackage(appsFilter,
                pkgQueriesProvider("com.some.other.package", "com.some.authority"),
                DUMMY_CALLING_APPID);

        for (int subjectUserId : USER_ARRAY) {
            for (int otherUserId : USER_ARRAY) {
                assertFalse(appsFilter.shouldFilterApplication(
                        UserHandle.getUid(DUMMY_CALLING_APPID, subjectUserId), calling, target,
                        otherUserId));
            }
        }

        // adds new user
        doAnswer(invocation -> {
            ((AppsFilter.StateProvider.CurrentStateCallback) invocation.getArgument(0))
                    .currentState(mExisting, USER_INFO_LIST_WITH_ADDED);
            return new Object();
        }).when(mStateProvider)
                .runWithState(any(AppsFilter.StateProvider.CurrentStateCallback.class));
        appsFilter.onUserCreated(ADDED_USER);

        for (int subjectUserId : USER_ARRAY_WITH_ADDED) {
            for (int otherUserId : USER_ARRAY_WITH_ADDED) {
                assertFalse(appsFilter.shouldFilterApplication(
                        UserHandle.getUid(DUMMY_CALLING_APPID, subjectUserId), calling, target,
                        otherUserId));
            }
        }
    }

    @Test
    public void testQueriesDifferentProvider_Filters() throws Exception {
        final AppsFilter appsFilter =