Loading src/com/android/settings/notification/NotificationBackend.java +7 −35 Original line number Diff line number Diff line Loading @@ -106,29 +106,20 @@ public class NotificationBackend { return row; } public AppRow loadAppRow(Context context, PackageManager pm, RoleManager roleManager, PackageInfo app) { public AppRow loadAppRow(Context context, PackageManager pm, PackageInfo app) { final AppRow row = loadAppRow(context, pm, app.applicationInfo); recordCanBeBlocked(context, pm, roleManager, app, row); recordCanBeBlocked(app, row); return row; } void recordCanBeBlocked(Context context, PackageManager pm, RoleManager rm, PackageInfo app, AppRow row) { void recordCanBeBlocked(PackageInfo app, AppRow row) { try { row.systemApp = row.lockedImportance = sINM.isPermissionFixed(app.packageName, row.userId); sINM.isImportanceLocked(app.packageName, app.applicationInfo.uid); } catch (RemoteException e) { Log.w(TAG, "Error calling NMS", e); } // The permission system cannot make role permissions 'fixed', so check for these // roles explicitly List<String> roles = rm.getHeldRolesFromController(app.packageName); if (roles.contains(RoleManager.ROLE_DIALER) || roles.contains(RoleManager.ROLE_EMERGENCY)) { row.systemApp = row.lockedImportance = true; } // if the app targets T but has not requested the permission, we cannot change the // permission state if (app.applicationInfo.targetSdkVersion > Build.VERSION_CODES.S_V2) { Loading @@ -139,24 +130,6 @@ public class NotificationBackend { } } @VisibleForTesting static void markAppRowWithBlockables(String[] nonBlockablePkgs, AppRow row, String packageName) { if (nonBlockablePkgs != null) { int N = nonBlockablePkgs.length; for (int i = 0; i < N; i++) { String pkg = nonBlockablePkgs[i]; if (pkg == null) { continue; } else if (pkg.contains(":")) { // handled by NotificationChannel.isImportanceLockedByOEM() continue; } else if (packageName.equals(nonBlockablePkgs[i])) { row.systemApp = row.lockedImportance = true; } } } } static public CharSequence getDeviceList(ICompanionDeviceManager cdm, LocalBluetoothManager lbm, String pkg, int userId) { boolean multiple = false; Loading Loading @@ -191,10 +164,9 @@ public class NotificationBackend { public boolean enableSwitch(Context context, ApplicationInfo app) { try { PackageInfo info = context.getPackageManager().getPackageInfo( app.packageName, PackageManager.GET_SIGNATURES); RoleManager rm = context.getSystemService(RoleManager.class); app.packageName, PackageManager.GET_PERMISSIONS); final AppRow row = new AppRow(); recordCanBeBlocked(context, context.getPackageManager(), rm, info, row); recordCanBeBlocked(info, row); boolean systemBlockable = !row.systemApp || (row.systemApp && row.banned); return systemBlockable && !row.lockedImportance; } catch (PackageManager.NameNotFoundException e) { Loading src/com/android/settings/notification/app/NotificationPreferenceController.java +3 −1 Original line number Diff line number Diff line Loading @@ -24,6 +24,7 @@ import android.app.NotificationChannel; import android.app.NotificationChannelGroup; import android.app.NotificationManager; import android.content.Context; import android.content.pm.ApplicationInfo; import android.content.pm.PackageManager; import android.content.pm.ShortcutInfo; import android.graphics.drawable.Drawable; Loading Loading @@ -171,7 +172,8 @@ public abstract class NotificationPreferenceController extends AbstractPreferenc return overrideCanConfigureValue; } if (mAppRow != null) { return !mAppRow.systemApp && !mAppRow.lockedImportance; boolean systemBlockable = !mAppRow.systemApp || (mAppRow.systemApp && mAppRow.banned); return systemBlockable && !mAppRow.lockedImportance; } return true; } Loading src/com/android/settings/notification/app/NotificationSettings.java +1 −4 Original line number Diff line number Diff line Loading @@ -25,7 +25,6 @@ import android.app.Notification; import android.app.NotificationChannel; import android.app.NotificationChannelGroup; import android.app.NotificationManager; import android.app.role.RoleManager; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; Loading Loading @@ -69,7 +68,6 @@ abstract public class NotificationSettings extends DashboardFragment { protected PackageManager mPm; protected NotificationBackend mBackend = new NotificationBackend(); protected NotificationManager mNm; protected RoleManager mRm; protected Context mContext; protected int mUid; Loading Loading @@ -116,7 +114,6 @@ abstract public class NotificationSettings extends DashboardFragment { mPm = getPackageManager(); mNm = NotificationManager.from(mContext); mRm = mContext.getSystemService(RoleManager.class); mPkg = mArgs != null && mArgs.containsKey(AppInfoBase.ARG_PACKAGE_NAME) ? mArgs.getString(AppInfoBase.ARG_PACKAGE_NAME) Loading Loading @@ -290,7 +287,7 @@ abstract public class NotificationSettings extends DashboardFragment { } private void loadAppRow() { mAppRow = mBackend.loadAppRow(mContext, mPm, mRm, mPkgInfo); mAppRow = mBackend.loadAppRow(mContext, mPm, mPkgInfo); } private void loadChannelGroup() { Loading tests/robotests/src/com/android/settings/notification/NotificationBackendTest.java +8 −76 Original line number Diff line number Diff line Loading @@ -20,6 +20,7 @@ import static com.google.common.truth.Truth.assertThat; import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertTrue; import static org.mockito.ArgumentMatchers.anyInt; import static org.mockito.ArgumentMatchers.anyString; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.when; Loading Loading @@ -80,69 +81,17 @@ public class NotificationBackendTest { } @Test public void testMarkAppRow_unblockablePackage() { AppRow appRow = new AppRow(); String packageName = "foo.bar.unblockable"; appRow.pkg = packageName; String[] nonBlockablePkgs = new String[2]; nonBlockablePkgs[0] = packageName; nonBlockablePkgs[1] = "some.other.package"; NotificationBackend.markAppRowWithBlockables(nonBlockablePkgs, appRow, packageName); // This package has a package lock but no locked channels assertTrue(appRow.lockedImportance); } @Test public void testMarkAppRow_defaultPackage() { PackageInfo pi = new PackageInfo(); pi.packageName = "test"; pi.applicationInfo = new ApplicationInfo(); pi.applicationInfo.packageName = "test"; List<String> roles = new ArrayList<>(); roles.add(RoleManager.ROLE_DIALER); RoleManager rm = mock(RoleManager.class); when(rm.getHeldRolesFromController(anyString())).thenReturn(roles); AppRow appRow = new NotificationBackend().loadAppRow(RuntimeEnvironment.application, mock(PackageManager.class), rm, pi); assertTrue(appRow.systemApp); } @Test public void testMarkAppRow_fixedPermission_withRole() throws Exception { public void testMarkAppRow_fixedImportance() throws Exception { PackageInfo pi = new PackageInfo(); pi.packageName = "test"; pi.applicationInfo = new ApplicationInfo(); pi.applicationInfo.packageName = "test"; pi.applicationInfo.uid = 123; List<String> roles = new ArrayList<>(); roles.add(RoleManager.ROLE_DIALER); RoleManager rm = mock(RoleManager.class); when(rm.getHeldRolesFromController(anyString())).thenReturn(roles); when(mInm.isPermissionFixed(pi.packageName, 0)).thenReturn(false); when(mInm.isImportanceLocked(pi.packageName, 123)).thenReturn(true); AppRow appRow = new NotificationBackend().loadAppRow(RuntimeEnvironment.application, mock(PackageManager.class), rm, pi); assertTrue(appRow.systemApp); assertTrue(appRow.lockedImportance); } @Test public void testMarkAppRow_fixedPermission() throws Exception { PackageInfo pi = new PackageInfo(); pi.packageName = "test"; pi.applicationInfo = new ApplicationInfo(); pi.applicationInfo.packageName = "test"; pi.applicationInfo.uid = 123; when(mInm.isPermissionFixed(pi.packageName, 0)).thenReturn(true); AppRow appRow = new NotificationBackend().loadAppRow(RuntimeEnvironment.application, mock(PackageManager.class), mock(RoleManager.class), pi); mock(PackageManager.class), pi); assertTrue(appRow.systemApp); assertTrue(appRow.lockedImportance); Loading @@ -156,10 +105,10 @@ public class NotificationBackendTest { pi.applicationInfo.packageName = "test"; pi.applicationInfo.uid = 123; when(mInm.isPermissionFixed(pi.packageName, 0)).thenReturn(false); when(mInm.isImportanceLocked(anyString(), anyInt())).thenReturn(false); AppRow appRow = new NotificationBackend().loadAppRow(RuntimeEnvironment.application, mock(PackageManager.class), mock(RoleManager.class), pi); mock(PackageManager.class), pi); assertFalse(appRow.systemApp); assertFalse(appRow.lockedImportance); Loading @@ -178,7 +127,7 @@ public class NotificationBackendTest { when(mInm.isPermissionFixed(pi.packageName, 0)).thenReturn(false); AppRow appRow = new NotificationBackend().loadAppRow(RuntimeEnvironment.application, mock(PackageManager.class), mock(RoleManager.class), pi); mock(PackageManager.class), pi); assertFalse(appRow.systemApp); assertTrue(appRow.lockedImportance); Loading @@ -198,29 +147,12 @@ public class NotificationBackendTest { when(mInm.isPermissionFixed(pi.packageName, 0)).thenReturn(false); AppRow appRow = new NotificationBackend().loadAppRow(RuntimeEnvironment.application, mock(PackageManager.class), mock(RoleManager.class), pi); mock(PackageManager.class), pi); assertFalse(appRow.systemApp); assertFalse(appRow.lockedImportance); } @Test public void testMarkAppRow_notDefaultPackage() { PackageInfo pi = new PackageInfo(); pi.packageName = "test"; pi.applicationInfo = new ApplicationInfo(); pi.applicationInfo.packageName = "test"; List<String> roles = new ArrayList<>(); roles.add(RoleManager.ROLE_HOME); RoleManager rm = mock(RoleManager.class); when(rm.getHeldRolesFromController(anyString())).thenReturn(roles); AppRow appRow = new NotificationBackend().loadAppRow(RuntimeEnvironment.application, mock(PackageManager.class), rm, pi); assertFalse(appRow.systemApp); } @Test public void testGetAggregatedUsageEvents_multipleEventsAgg() { List<UsageEvents.Event> events = new ArrayList<>(); Loading tests/robotests/src/com/android/settings/notification/app/NotificationPreferenceControllerTest.java +16 −4 Original line number Diff line number Diff line Loading @@ -284,23 +284,35 @@ public class NotificationPreferenceControllerTest { } @Test public void testIsAppBlockable_postMigration_locked() { public void testIsAppBlockable_fixedPermission() { mController = new TestPreferenceController(mContext, mBackend); NotificationBackend.AppRow appRow = new NotificationBackend.AppRow(); appRow.lockedImportance = true; appRow.systemApp = true; appRow.banned = false; mController.onResume(appRow, null, null, null, null, null, null); assertFalse(mController.isAppBlockable()); } @Test public void testIsAppBlockable_postMigration_locked_butAppOff() { public void testIsAppBlockable_fixedPermission_butAppOff() { mController = new TestPreferenceController(mContext, mBackend); NotificationBackend.AppRow appRow = new NotificationBackend.AppRow(); appRow.lockedImportance = true; appRow.systemApp = true; appRow.banned = true; mController.onResume(appRow, null, null, null, null, null, null); assertTrue(mController.isAppBlockable()); } @Test public void testIsAppBlockable_notFixedButAppInBadState() { mController = new TestPreferenceController(mContext, mBackend); NotificationBackend.AppRow appRow = new NotificationBackend.AppRow(); appRow.systemApp = false; appRow.banned = true; appRow.lockedImportance = true; mController.onResume(appRow, null, null, null, null, null, null); assertFalse(mController.isAppBlockable()); } Loading Loading
src/com/android/settings/notification/NotificationBackend.java +7 −35 Original line number Diff line number Diff line Loading @@ -106,29 +106,20 @@ public class NotificationBackend { return row; } public AppRow loadAppRow(Context context, PackageManager pm, RoleManager roleManager, PackageInfo app) { public AppRow loadAppRow(Context context, PackageManager pm, PackageInfo app) { final AppRow row = loadAppRow(context, pm, app.applicationInfo); recordCanBeBlocked(context, pm, roleManager, app, row); recordCanBeBlocked(app, row); return row; } void recordCanBeBlocked(Context context, PackageManager pm, RoleManager rm, PackageInfo app, AppRow row) { void recordCanBeBlocked(PackageInfo app, AppRow row) { try { row.systemApp = row.lockedImportance = sINM.isPermissionFixed(app.packageName, row.userId); sINM.isImportanceLocked(app.packageName, app.applicationInfo.uid); } catch (RemoteException e) { Log.w(TAG, "Error calling NMS", e); } // The permission system cannot make role permissions 'fixed', so check for these // roles explicitly List<String> roles = rm.getHeldRolesFromController(app.packageName); if (roles.contains(RoleManager.ROLE_DIALER) || roles.contains(RoleManager.ROLE_EMERGENCY)) { row.systemApp = row.lockedImportance = true; } // if the app targets T but has not requested the permission, we cannot change the // permission state if (app.applicationInfo.targetSdkVersion > Build.VERSION_CODES.S_V2) { Loading @@ -139,24 +130,6 @@ public class NotificationBackend { } } @VisibleForTesting static void markAppRowWithBlockables(String[] nonBlockablePkgs, AppRow row, String packageName) { if (nonBlockablePkgs != null) { int N = nonBlockablePkgs.length; for (int i = 0; i < N; i++) { String pkg = nonBlockablePkgs[i]; if (pkg == null) { continue; } else if (pkg.contains(":")) { // handled by NotificationChannel.isImportanceLockedByOEM() continue; } else if (packageName.equals(nonBlockablePkgs[i])) { row.systemApp = row.lockedImportance = true; } } } } static public CharSequence getDeviceList(ICompanionDeviceManager cdm, LocalBluetoothManager lbm, String pkg, int userId) { boolean multiple = false; Loading Loading @@ -191,10 +164,9 @@ public class NotificationBackend { public boolean enableSwitch(Context context, ApplicationInfo app) { try { PackageInfo info = context.getPackageManager().getPackageInfo( app.packageName, PackageManager.GET_SIGNATURES); RoleManager rm = context.getSystemService(RoleManager.class); app.packageName, PackageManager.GET_PERMISSIONS); final AppRow row = new AppRow(); recordCanBeBlocked(context, context.getPackageManager(), rm, info, row); recordCanBeBlocked(info, row); boolean systemBlockable = !row.systemApp || (row.systemApp && row.banned); return systemBlockable && !row.lockedImportance; } catch (PackageManager.NameNotFoundException e) { Loading
src/com/android/settings/notification/app/NotificationPreferenceController.java +3 −1 Original line number Diff line number Diff line Loading @@ -24,6 +24,7 @@ import android.app.NotificationChannel; import android.app.NotificationChannelGroup; import android.app.NotificationManager; import android.content.Context; import android.content.pm.ApplicationInfo; import android.content.pm.PackageManager; import android.content.pm.ShortcutInfo; import android.graphics.drawable.Drawable; Loading Loading @@ -171,7 +172,8 @@ public abstract class NotificationPreferenceController extends AbstractPreferenc return overrideCanConfigureValue; } if (mAppRow != null) { return !mAppRow.systemApp && !mAppRow.lockedImportance; boolean systemBlockable = !mAppRow.systemApp || (mAppRow.systemApp && mAppRow.banned); return systemBlockable && !mAppRow.lockedImportance; } return true; } Loading
src/com/android/settings/notification/app/NotificationSettings.java +1 −4 Original line number Diff line number Diff line Loading @@ -25,7 +25,6 @@ import android.app.Notification; import android.app.NotificationChannel; import android.app.NotificationChannelGroup; import android.app.NotificationManager; import android.app.role.RoleManager; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; Loading Loading @@ -69,7 +68,6 @@ abstract public class NotificationSettings extends DashboardFragment { protected PackageManager mPm; protected NotificationBackend mBackend = new NotificationBackend(); protected NotificationManager mNm; protected RoleManager mRm; protected Context mContext; protected int mUid; Loading Loading @@ -116,7 +114,6 @@ abstract public class NotificationSettings extends DashboardFragment { mPm = getPackageManager(); mNm = NotificationManager.from(mContext); mRm = mContext.getSystemService(RoleManager.class); mPkg = mArgs != null && mArgs.containsKey(AppInfoBase.ARG_PACKAGE_NAME) ? mArgs.getString(AppInfoBase.ARG_PACKAGE_NAME) Loading Loading @@ -290,7 +287,7 @@ abstract public class NotificationSettings extends DashboardFragment { } private void loadAppRow() { mAppRow = mBackend.loadAppRow(mContext, mPm, mRm, mPkgInfo); mAppRow = mBackend.loadAppRow(mContext, mPm, mPkgInfo); } private void loadChannelGroup() { Loading
tests/robotests/src/com/android/settings/notification/NotificationBackendTest.java +8 −76 Original line number Diff line number Diff line Loading @@ -20,6 +20,7 @@ import static com.google.common.truth.Truth.assertThat; import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertTrue; import static org.mockito.ArgumentMatchers.anyInt; import static org.mockito.ArgumentMatchers.anyString; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.when; Loading Loading @@ -80,69 +81,17 @@ public class NotificationBackendTest { } @Test public void testMarkAppRow_unblockablePackage() { AppRow appRow = new AppRow(); String packageName = "foo.bar.unblockable"; appRow.pkg = packageName; String[] nonBlockablePkgs = new String[2]; nonBlockablePkgs[0] = packageName; nonBlockablePkgs[1] = "some.other.package"; NotificationBackend.markAppRowWithBlockables(nonBlockablePkgs, appRow, packageName); // This package has a package lock but no locked channels assertTrue(appRow.lockedImportance); } @Test public void testMarkAppRow_defaultPackage() { PackageInfo pi = new PackageInfo(); pi.packageName = "test"; pi.applicationInfo = new ApplicationInfo(); pi.applicationInfo.packageName = "test"; List<String> roles = new ArrayList<>(); roles.add(RoleManager.ROLE_DIALER); RoleManager rm = mock(RoleManager.class); when(rm.getHeldRolesFromController(anyString())).thenReturn(roles); AppRow appRow = new NotificationBackend().loadAppRow(RuntimeEnvironment.application, mock(PackageManager.class), rm, pi); assertTrue(appRow.systemApp); } @Test public void testMarkAppRow_fixedPermission_withRole() throws Exception { public void testMarkAppRow_fixedImportance() throws Exception { PackageInfo pi = new PackageInfo(); pi.packageName = "test"; pi.applicationInfo = new ApplicationInfo(); pi.applicationInfo.packageName = "test"; pi.applicationInfo.uid = 123; List<String> roles = new ArrayList<>(); roles.add(RoleManager.ROLE_DIALER); RoleManager rm = mock(RoleManager.class); when(rm.getHeldRolesFromController(anyString())).thenReturn(roles); when(mInm.isPermissionFixed(pi.packageName, 0)).thenReturn(false); when(mInm.isImportanceLocked(pi.packageName, 123)).thenReturn(true); AppRow appRow = new NotificationBackend().loadAppRow(RuntimeEnvironment.application, mock(PackageManager.class), rm, pi); assertTrue(appRow.systemApp); assertTrue(appRow.lockedImportance); } @Test public void testMarkAppRow_fixedPermission() throws Exception { PackageInfo pi = new PackageInfo(); pi.packageName = "test"; pi.applicationInfo = new ApplicationInfo(); pi.applicationInfo.packageName = "test"; pi.applicationInfo.uid = 123; when(mInm.isPermissionFixed(pi.packageName, 0)).thenReturn(true); AppRow appRow = new NotificationBackend().loadAppRow(RuntimeEnvironment.application, mock(PackageManager.class), mock(RoleManager.class), pi); mock(PackageManager.class), pi); assertTrue(appRow.systemApp); assertTrue(appRow.lockedImportance); Loading @@ -156,10 +105,10 @@ public class NotificationBackendTest { pi.applicationInfo.packageName = "test"; pi.applicationInfo.uid = 123; when(mInm.isPermissionFixed(pi.packageName, 0)).thenReturn(false); when(mInm.isImportanceLocked(anyString(), anyInt())).thenReturn(false); AppRow appRow = new NotificationBackend().loadAppRow(RuntimeEnvironment.application, mock(PackageManager.class), mock(RoleManager.class), pi); mock(PackageManager.class), pi); assertFalse(appRow.systemApp); assertFalse(appRow.lockedImportance); Loading @@ -178,7 +127,7 @@ public class NotificationBackendTest { when(mInm.isPermissionFixed(pi.packageName, 0)).thenReturn(false); AppRow appRow = new NotificationBackend().loadAppRow(RuntimeEnvironment.application, mock(PackageManager.class), mock(RoleManager.class), pi); mock(PackageManager.class), pi); assertFalse(appRow.systemApp); assertTrue(appRow.lockedImportance); Loading @@ -198,29 +147,12 @@ public class NotificationBackendTest { when(mInm.isPermissionFixed(pi.packageName, 0)).thenReturn(false); AppRow appRow = new NotificationBackend().loadAppRow(RuntimeEnvironment.application, mock(PackageManager.class), mock(RoleManager.class), pi); mock(PackageManager.class), pi); assertFalse(appRow.systemApp); assertFalse(appRow.lockedImportance); } @Test public void testMarkAppRow_notDefaultPackage() { PackageInfo pi = new PackageInfo(); pi.packageName = "test"; pi.applicationInfo = new ApplicationInfo(); pi.applicationInfo.packageName = "test"; List<String> roles = new ArrayList<>(); roles.add(RoleManager.ROLE_HOME); RoleManager rm = mock(RoleManager.class); when(rm.getHeldRolesFromController(anyString())).thenReturn(roles); AppRow appRow = new NotificationBackend().loadAppRow(RuntimeEnvironment.application, mock(PackageManager.class), rm, pi); assertFalse(appRow.systemApp); } @Test public void testGetAggregatedUsageEvents_multipleEventsAgg() { List<UsageEvents.Event> events = new ArrayList<>(); Loading
tests/robotests/src/com/android/settings/notification/app/NotificationPreferenceControllerTest.java +16 −4 Original line number Diff line number Diff line Loading @@ -284,23 +284,35 @@ public class NotificationPreferenceControllerTest { } @Test public void testIsAppBlockable_postMigration_locked() { public void testIsAppBlockable_fixedPermission() { mController = new TestPreferenceController(mContext, mBackend); NotificationBackend.AppRow appRow = new NotificationBackend.AppRow(); appRow.lockedImportance = true; appRow.systemApp = true; appRow.banned = false; mController.onResume(appRow, null, null, null, null, null, null); assertFalse(mController.isAppBlockable()); } @Test public void testIsAppBlockable_postMigration_locked_butAppOff() { public void testIsAppBlockable_fixedPermission_butAppOff() { mController = new TestPreferenceController(mContext, mBackend); NotificationBackend.AppRow appRow = new NotificationBackend.AppRow(); appRow.lockedImportance = true; appRow.systemApp = true; appRow.banned = true; mController.onResume(appRow, null, null, null, null, null, null); assertTrue(mController.isAppBlockable()); } @Test public void testIsAppBlockable_notFixedButAppInBadState() { mController = new TestPreferenceController(mContext, mBackend); NotificationBackend.AppRow appRow = new NotificationBackend.AppRow(); appRow.systemApp = false; appRow.banned = true; appRow.lockedImportance = true; mController.onResume(appRow, null, null, null, null, null, null); assertFalse(mController.isAppBlockable()); } Loading