Loading packages/SystemUI/src/com/android/systemui/appops/AppOpsControllerImpl.java +10 −2 Original line number Diff line number Diff line Loading @@ -62,6 +62,7 @@ public class AppOpsControllerImpl implements AppOpsController, private H mBGHandler; private final List<AppOpsController.Callback> mCallbacks = new ArrayList<>(); private final ArrayMap<Integer, Set<Callback>> mCallbacksByCode = new ArrayMap<>(); private final PermissionFlagsCache mFlagsCache; private boolean mListening; @GuardedBy("mActiveItems") Loading @@ -79,8 +80,14 @@ public class AppOpsControllerImpl implements AppOpsController, @Inject public AppOpsControllerImpl(Context context, @Named(BG_LOOPER_NAME) Looper bgLooper) { this(context, bgLooper, new PermissionFlagsCache(context)); } @VisibleForTesting protected AppOpsControllerImpl(Context context, Looper bgLooper, PermissionFlagsCache cache) { mContext = context; mAppOps = (AppOpsManager) context.getSystemService(Context.APP_OPS_SERVICE); mFlagsCache = cache; mBGHandler = new H(bgLooper); final int numOps = OPS.length; for (int i = 0; i < numOps; i++) { Loading Loading @@ -239,7 +246,7 @@ public class AppOpsControllerImpl implements AppOpsController, if (permission == null) { return false; } int permFlags = mContext.getPackageManager().getPermissionFlags(permission, int permFlags = mFlagsCache.getPermissionFlags(permission, packageName, UserHandle.getUserHandleForUid(uid)); return (permFlags & PackageManager.FLAG_PERMISSION_USER_SENSITIVE_WHEN_GRANTED) != 0; } Loading Loading @@ -359,7 +366,8 @@ public class AppOpsControllerImpl implements AppOpsController, } private void notifySuscribers(int code, int uid, String packageName, boolean active) { if (mCallbacksByCode.containsKey(code)) { if (mCallbacksByCode.containsKey(code) && isUserVisible(code, uid, packageName)) { if (DEBUG) Log.d(TAG, "Notifying of change in package " + packageName); for (Callback cb: mCallbacksByCode.get(code)) { cb.onActiveStateChanged(code, uid, packageName, active); Loading packages/SystemUI/src/com/android/systemui/appops/PermissionFlagsCache.kt 0 → 100644 +70 −0 Original line number Diff line number Diff line /* * Copyright (C) 2019 The Android Open Source Project * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License */ package com.android.systemui.appops import android.content.Context import android.content.pm.PackageManager import android.os.UserHandle import android.util.ArrayMap import com.android.internal.annotations.VisibleForTesting private data class PermissionFlag(val flag: Int, val timestamp: Long) private data class PermissionFlagKey( val permission: String, val packageName: String, val user: UserHandle ) internal const val CACHE_EXPIRATION = 10000L /** * Cache for PackageManager's PermissionFlags. * * Flags older than [CACHE_EXPIRATION] will be retrieved again. */ internal open class PermissionFlagsCache(context: Context) { private val packageManager = context.packageManager private val permissionFlagsCache = ArrayMap<PermissionFlagKey, PermissionFlag>() /** * Retrieve permission flags from cache or PackageManager. There parameters will be passed * directly to [PackageManager]. * * Calls to this method should be done from a background thread. */ fun getPermissionFlags(permission: String, packageName: String, user: UserHandle): Int { val key = PermissionFlagKey(permission, packageName, user) val now = getCurrentTime() val value = permissionFlagsCache.getOrPut(key) { PermissionFlag(getFlags(key), now) } if (now - value.timestamp > CACHE_EXPIRATION) { val newValue = PermissionFlag(getFlags(key), now) permissionFlagsCache.put(key, newValue) return newValue.flag } else { return value.flag } } private fun getFlags(key: PermissionFlagKey) = packageManager.getPermissionFlags(key.permission, key.packageName, key.user) @VisibleForTesting protected open fun getCurrentTime() = System.currentTimeMillis() } No newline at end of file packages/SystemUI/tests/src/com/android/systemui/appops/AppOpsControllerTest.java +28 −2 Original line number Diff line number Diff line Loading @@ -16,17 +16,20 @@ package com.android.systemui.appops; import static junit.framework.TestCase.assertFalse; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertTrue; import static org.mockito.ArgumentMatchers.any; import static org.mockito.ArgumentMatchers.anyBoolean; import static org.mockito.ArgumentMatchers.anyInt; import static org.mockito.ArgumentMatchers.anyLong; import static org.mockito.ArgumentMatchers.anyString; import static org.mockito.ArgumentMatchers.eq; import static org.mockito.Mockito.never; import static org.mockito.Mockito.times; import static org.mockito.Mockito.verify; import static org.mockito.Mockito.when; import static java.lang.Thread.sleep; Loading Loading @@ -55,6 +58,7 @@ public class AppOpsControllerTest extends SysuiTestCase { private static final String TEST_PACKAGE_NAME = "test"; private static final int TEST_UID = UserHandle.getUid(0, 0); private static final int TEST_UID_OTHER = UserHandle.getUid(1, 0); private static final int TEST_UID_NON_USER_SENSITIVE = UserHandle.getUid(2, 0); @Mock private AppOpsManager mAppOpsManager; Loading @@ -64,6 +68,8 @@ public class AppOpsControllerTest extends SysuiTestCase { private AppOpsController.Callback mCallback; @Mock private AppOpsControllerImpl.H mMockHandler; @Mock private PermissionFlagsCache mFlagsCache; private AppOpsControllerImpl mController; private TestableLooper mTestableLooper; Loading @@ -75,7 +81,19 @@ public class AppOpsControllerTest extends SysuiTestCase { getContext().addMockSystemService(AppOpsManager.class, mAppOpsManager); mController = new AppOpsControllerImpl(mContext, mTestableLooper.getLooper()); // All permissions of TEST_UID and TEST_UID_OTHER are user sensitive. None of // TEST_UID_NON_USER_SENSITIVE are user sensitive. getContext().setMockPackageManager(mPackageManager); when(mFlagsCache.getPermissionFlags(anyString(), anyString(), eq(UserHandle.getUserHandleForUid(TEST_UID)))).thenReturn( PackageManager.FLAG_PERMISSION_USER_SENSITIVE_WHEN_GRANTED); when(mFlagsCache.getPermissionFlags(anyString(), anyString(), eq(UserHandle.getUserHandleForUid(TEST_UID_OTHER)))).thenReturn( PackageManager.FLAG_PERMISSION_USER_SENSITIVE_WHEN_GRANTED); when(mFlagsCache.getPermissionFlags(anyString(), anyString(), eq(UserHandle.getUserHandleForUid(TEST_UID_NON_USER_SENSITIVE)))).thenReturn(0); mController = new AppOpsControllerImpl(mContext, mTestableLooper.getLooper(), mFlagsCache); } @Test Loading Loading @@ -170,6 +188,14 @@ public class AppOpsControllerTest extends SysuiTestCase { mController.getActiveAppOpsForUser(UserHandle.getUserId(TEST_UID_OTHER)).size()); } @Test public void nonUserSensitiveOpsAreIgnored() { mController.onOpActiveChanged(AppOpsManager.OP_RECORD_AUDIO, TEST_UID_NON_USER_SENSITIVE, TEST_PACKAGE_NAME, true); assertEquals(0, mController.getActiveAppOpsForUser( UserHandle.getUserId(TEST_UID_NON_USER_SENSITIVE)).size()); } @Test public void opNotedScheduledForRemoval() { mController.setBGHandler(mMockHandler); Loading packages/SystemUI/tests/src/com/android/systemui/appops/PermissionFlagsCacheTest.kt 0 → 100644 +88 −0 Original line number Diff line number Diff line /* * Copyright (C) 2019 The Android Open Source Project * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License */ package com.android.systemui.appops import android.content.Context import android.content.pm.PackageManager import android.os.UserHandle import android.testing.AndroidTestingRunner import androidx.test.filters.SmallTest import com.android.systemui.SysuiTestCase import org.junit.Before import org.junit.Test import org.junit.runner.RunWith import org.mockito.ArgumentMatchers.any import org.mockito.ArgumentMatchers.anyString import org.mockito.Mock import org.mockito.Mockito.times import org.mockito.Mockito.verify import org.mockito.MockitoAnnotations @SmallTest @RunWith(AndroidTestingRunner::class) class PermissionFlagsCacheTest : SysuiTestCase() { companion object { const val TEST_PERMISSION = "test_permission" const val TEST_PACKAGE = "test_package" } @Mock private lateinit var mPackageManager: PackageManager @Mock private lateinit var mUserHandle: UserHandle private lateinit var flagsCache: TestPermissionFlagsCache @Before fun setUp() { MockitoAnnotations.initMocks(this) mContext.setMockPackageManager(mPackageManager) flagsCache = TestPermissionFlagsCache(mContext) } @Test fun testCallsPackageManager_exactlyOnce() { flagsCache.getPermissionFlags(TEST_PERMISSION, TEST_PACKAGE, mUserHandle) flagsCache.time = CACHE_EXPIRATION - 1 verify(mPackageManager).getPermissionFlags(TEST_PERMISSION, TEST_PACKAGE, mUserHandle) } @Test fun testCallsPackageManager_cacheExpired() { flagsCache.getPermissionFlags(TEST_PERMISSION, TEST_PACKAGE, mUserHandle) flagsCache.time = CACHE_EXPIRATION + 1 flagsCache.getPermissionFlags(TEST_PERMISSION, TEST_PACKAGE, mUserHandle) verify(mPackageManager, times(2)) .getPermissionFlags(TEST_PERMISSION, TEST_PACKAGE, mUserHandle) } @Test fun testCallsPackageMaanger_multipleKeys() { flagsCache.getPermissionFlags(TEST_PERMISSION, TEST_PACKAGE, mUserHandle) flagsCache.getPermissionFlags(TEST_PERMISSION, "", mUserHandle) verify(mPackageManager, times(2)) .getPermissionFlags(anyString(), anyString(), any()) } private class TestPermissionFlagsCache(context: Context) : PermissionFlagsCache(context) { var time = 0L override fun getCurrentTime(): Long { return time } } } No newline at end of file Loading
packages/SystemUI/src/com/android/systemui/appops/AppOpsControllerImpl.java +10 −2 Original line number Diff line number Diff line Loading @@ -62,6 +62,7 @@ public class AppOpsControllerImpl implements AppOpsController, private H mBGHandler; private final List<AppOpsController.Callback> mCallbacks = new ArrayList<>(); private final ArrayMap<Integer, Set<Callback>> mCallbacksByCode = new ArrayMap<>(); private final PermissionFlagsCache mFlagsCache; private boolean mListening; @GuardedBy("mActiveItems") Loading @@ -79,8 +80,14 @@ public class AppOpsControllerImpl implements AppOpsController, @Inject public AppOpsControllerImpl(Context context, @Named(BG_LOOPER_NAME) Looper bgLooper) { this(context, bgLooper, new PermissionFlagsCache(context)); } @VisibleForTesting protected AppOpsControllerImpl(Context context, Looper bgLooper, PermissionFlagsCache cache) { mContext = context; mAppOps = (AppOpsManager) context.getSystemService(Context.APP_OPS_SERVICE); mFlagsCache = cache; mBGHandler = new H(bgLooper); final int numOps = OPS.length; for (int i = 0; i < numOps; i++) { Loading Loading @@ -239,7 +246,7 @@ public class AppOpsControllerImpl implements AppOpsController, if (permission == null) { return false; } int permFlags = mContext.getPackageManager().getPermissionFlags(permission, int permFlags = mFlagsCache.getPermissionFlags(permission, packageName, UserHandle.getUserHandleForUid(uid)); return (permFlags & PackageManager.FLAG_PERMISSION_USER_SENSITIVE_WHEN_GRANTED) != 0; } Loading Loading @@ -359,7 +366,8 @@ public class AppOpsControllerImpl implements AppOpsController, } private void notifySuscribers(int code, int uid, String packageName, boolean active) { if (mCallbacksByCode.containsKey(code)) { if (mCallbacksByCode.containsKey(code) && isUserVisible(code, uid, packageName)) { if (DEBUG) Log.d(TAG, "Notifying of change in package " + packageName); for (Callback cb: mCallbacksByCode.get(code)) { cb.onActiveStateChanged(code, uid, packageName, active); Loading
packages/SystemUI/src/com/android/systemui/appops/PermissionFlagsCache.kt 0 → 100644 +70 −0 Original line number Diff line number Diff line /* * Copyright (C) 2019 The Android Open Source Project * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License */ package com.android.systemui.appops import android.content.Context import android.content.pm.PackageManager import android.os.UserHandle import android.util.ArrayMap import com.android.internal.annotations.VisibleForTesting private data class PermissionFlag(val flag: Int, val timestamp: Long) private data class PermissionFlagKey( val permission: String, val packageName: String, val user: UserHandle ) internal const val CACHE_EXPIRATION = 10000L /** * Cache for PackageManager's PermissionFlags. * * Flags older than [CACHE_EXPIRATION] will be retrieved again. */ internal open class PermissionFlagsCache(context: Context) { private val packageManager = context.packageManager private val permissionFlagsCache = ArrayMap<PermissionFlagKey, PermissionFlag>() /** * Retrieve permission flags from cache or PackageManager. There parameters will be passed * directly to [PackageManager]. * * Calls to this method should be done from a background thread. */ fun getPermissionFlags(permission: String, packageName: String, user: UserHandle): Int { val key = PermissionFlagKey(permission, packageName, user) val now = getCurrentTime() val value = permissionFlagsCache.getOrPut(key) { PermissionFlag(getFlags(key), now) } if (now - value.timestamp > CACHE_EXPIRATION) { val newValue = PermissionFlag(getFlags(key), now) permissionFlagsCache.put(key, newValue) return newValue.flag } else { return value.flag } } private fun getFlags(key: PermissionFlagKey) = packageManager.getPermissionFlags(key.permission, key.packageName, key.user) @VisibleForTesting protected open fun getCurrentTime() = System.currentTimeMillis() } No newline at end of file
packages/SystemUI/tests/src/com/android/systemui/appops/AppOpsControllerTest.java +28 −2 Original line number Diff line number Diff line Loading @@ -16,17 +16,20 @@ package com.android.systemui.appops; import static junit.framework.TestCase.assertFalse; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertTrue; import static org.mockito.ArgumentMatchers.any; import static org.mockito.ArgumentMatchers.anyBoolean; import static org.mockito.ArgumentMatchers.anyInt; import static org.mockito.ArgumentMatchers.anyLong; import static org.mockito.ArgumentMatchers.anyString; import static org.mockito.ArgumentMatchers.eq; import static org.mockito.Mockito.never; import static org.mockito.Mockito.times; import static org.mockito.Mockito.verify; import static org.mockito.Mockito.when; import static java.lang.Thread.sleep; Loading Loading @@ -55,6 +58,7 @@ public class AppOpsControllerTest extends SysuiTestCase { private static final String TEST_PACKAGE_NAME = "test"; private static final int TEST_UID = UserHandle.getUid(0, 0); private static final int TEST_UID_OTHER = UserHandle.getUid(1, 0); private static final int TEST_UID_NON_USER_SENSITIVE = UserHandle.getUid(2, 0); @Mock private AppOpsManager mAppOpsManager; Loading @@ -64,6 +68,8 @@ public class AppOpsControllerTest extends SysuiTestCase { private AppOpsController.Callback mCallback; @Mock private AppOpsControllerImpl.H mMockHandler; @Mock private PermissionFlagsCache mFlagsCache; private AppOpsControllerImpl mController; private TestableLooper mTestableLooper; Loading @@ -75,7 +81,19 @@ public class AppOpsControllerTest extends SysuiTestCase { getContext().addMockSystemService(AppOpsManager.class, mAppOpsManager); mController = new AppOpsControllerImpl(mContext, mTestableLooper.getLooper()); // All permissions of TEST_UID and TEST_UID_OTHER are user sensitive. None of // TEST_UID_NON_USER_SENSITIVE are user sensitive. getContext().setMockPackageManager(mPackageManager); when(mFlagsCache.getPermissionFlags(anyString(), anyString(), eq(UserHandle.getUserHandleForUid(TEST_UID)))).thenReturn( PackageManager.FLAG_PERMISSION_USER_SENSITIVE_WHEN_GRANTED); when(mFlagsCache.getPermissionFlags(anyString(), anyString(), eq(UserHandle.getUserHandleForUid(TEST_UID_OTHER)))).thenReturn( PackageManager.FLAG_PERMISSION_USER_SENSITIVE_WHEN_GRANTED); when(mFlagsCache.getPermissionFlags(anyString(), anyString(), eq(UserHandle.getUserHandleForUid(TEST_UID_NON_USER_SENSITIVE)))).thenReturn(0); mController = new AppOpsControllerImpl(mContext, mTestableLooper.getLooper(), mFlagsCache); } @Test Loading Loading @@ -170,6 +188,14 @@ public class AppOpsControllerTest extends SysuiTestCase { mController.getActiveAppOpsForUser(UserHandle.getUserId(TEST_UID_OTHER)).size()); } @Test public void nonUserSensitiveOpsAreIgnored() { mController.onOpActiveChanged(AppOpsManager.OP_RECORD_AUDIO, TEST_UID_NON_USER_SENSITIVE, TEST_PACKAGE_NAME, true); assertEquals(0, mController.getActiveAppOpsForUser( UserHandle.getUserId(TEST_UID_NON_USER_SENSITIVE)).size()); } @Test public void opNotedScheduledForRemoval() { mController.setBGHandler(mMockHandler); Loading
packages/SystemUI/tests/src/com/android/systemui/appops/PermissionFlagsCacheTest.kt 0 → 100644 +88 −0 Original line number Diff line number Diff line /* * Copyright (C) 2019 The Android Open Source Project * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License */ package com.android.systemui.appops import android.content.Context import android.content.pm.PackageManager import android.os.UserHandle import android.testing.AndroidTestingRunner import androidx.test.filters.SmallTest import com.android.systemui.SysuiTestCase import org.junit.Before import org.junit.Test import org.junit.runner.RunWith import org.mockito.ArgumentMatchers.any import org.mockito.ArgumentMatchers.anyString import org.mockito.Mock import org.mockito.Mockito.times import org.mockito.Mockito.verify import org.mockito.MockitoAnnotations @SmallTest @RunWith(AndroidTestingRunner::class) class PermissionFlagsCacheTest : SysuiTestCase() { companion object { const val TEST_PERMISSION = "test_permission" const val TEST_PACKAGE = "test_package" } @Mock private lateinit var mPackageManager: PackageManager @Mock private lateinit var mUserHandle: UserHandle private lateinit var flagsCache: TestPermissionFlagsCache @Before fun setUp() { MockitoAnnotations.initMocks(this) mContext.setMockPackageManager(mPackageManager) flagsCache = TestPermissionFlagsCache(mContext) } @Test fun testCallsPackageManager_exactlyOnce() { flagsCache.getPermissionFlags(TEST_PERMISSION, TEST_PACKAGE, mUserHandle) flagsCache.time = CACHE_EXPIRATION - 1 verify(mPackageManager).getPermissionFlags(TEST_PERMISSION, TEST_PACKAGE, mUserHandle) } @Test fun testCallsPackageManager_cacheExpired() { flagsCache.getPermissionFlags(TEST_PERMISSION, TEST_PACKAGE, mUserHandle) flagsCache.time = CACHE_EXPIRATION + 1 flagsCache.getPermissionFlags(TEST_PERMISSION, TEST_PACKAGE, mUserHandle) verify(mPackageManager, times(2)) .getPermissionFlags(TEST_PERMISSION, TEST_PACKAGE, mUserHandle) } @Test fun testCallsPackageMaanger_multipleKeys() { flagsCache.getPermissionFlags(TEST_PERMISSION, TEST_PACKAGE, mUserHandle) flagsCache.getPermissionFlags(TEST_PERMISSION, "", mUserHandle) verify(mPackageManager, times(2)) .getPermissionFlags(anyString(), anyString(), any()) } private class TestPermissionFlagsCache(context: Context) : PermissionFlagsCache(context) { var time = 0L override fun getCurrentTime(): Long { return time } } } No newline at end of file