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

Commit c25d45b0 authored by Patrick Baumann's avatar Patrick Baumann Committed by Android (Google) Code Review
Browse files

Merge changes from topic "192281479-scdev" into sc-dev

* changes:
  Revert "Only update cache for new user on user create"
  Revert "Fixes incorrect array type comparison"
parents 89bc0823 89daef04
Loading
Loading
Loading
Loading
+28 −73
Original line number Diff line number Diff line
@@ -17,8 +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.os.UserHandle.USER_NULL;
import static android.provider.DeviceConfig.NAMESPACE_PACKAGE_MANAGER_SERVICE;

import static com.android.internal.annotations.VisibleForTesting.Visibility.PRIVATE;
@@ -687,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);
@@ -700,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
@@ -833,57 +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 = USER_NULL;
            for (int u = 0; u < users.length; u++) {
                if (subjectUserId == users[u].id) {
                    userId = subjectUserId;
                    break;
                }
            }
            if (userId == USER_NULL) {
                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.put(uid1, uid2, mShouldFilterCache.get(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;
    }
@@ -904,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) -> {
@@ -935,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();
            }
        }
@@ -949,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*/);
                });
            }
@@ -958,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) {
@@ -968,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));
                }
            }
        }
    }

@@ -1190,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());
                    }
                }

@@ -1207,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
@@ -26504,7 +26504,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 =