Use NMS calculation of fixed importance

Rather than recalculating it again. Also align logic on
listing and details pages.

Test: NotificationBackendTest, NotificationPreferenceControllerTest
Bug: 231662091
Fixes: 231815850
Change-Id: If9572766666620008afb839ecb0828ace8d6073d
This commit is contained in:
Julia Reynolds
2022-05-10 16:42:37 -04:00
parent 23a85a1f96
commit 2011588b16
5 changed files with 35 additions and 120 deletions

View File

@@ -106,29 +106,20 @@ public class NotificationBackend {
return row; return row;
} }
public AppRow loadAppRow(Context context, PackageManager pm, public AppRow loadAppRow(Context context, PackageManager pm, PackageInfo app) {
RoleManager roleManager, PackageInfo app) {
final AppRow row = loadAppRow(context, pm, app.applicationInfo); final AppRow row = loadAppRow(context, pm, app.applicationInfo);
recordCanBeBlocked(context, pm, roleManager, app, row); recordCanBeBlocked(app, row);
return row; return row;
} }
void recordCanBeBlocked(Context context, PackageManager pm, RoleManager rm, PackageInfo app, void recordCanBeBlocked(PackageInfo app, AppRow row) {
AppRow row) {
try { try {
row.systemApp = row.lockedImportance = row.systemApp = row.lockedImportance =
sINM.isPermissionFixed(app.packageName, row.userId); sINM.isImportanceLocked(app.packageName, app.applicationInfo.uid);
} catch (RemoteException e) { } catch (RemoteException e) {
Log.w(TAG, "Error calling NMS", 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 // if the app targets T but has not requested the permission, we cannot change the
// permission state // permission state
if (app.applicationInfo.targetSdkVersion > Build.VERSION_CODES.S_V2) { if (app.applicationInfo.targetSdkVersion > Build.VERSION_CODES.S_V2) {
@@ -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, static public CharSequence getDeviceList(ICompanionDeviceManager cdm, LocalBluetoothManager lbm,
String pkg, int userId) { String pkg, int userId) {
boolean multiple = false; boolean multiple = false;
@@ -191,10 +164,9 @@ public class NotificationBackend {
public boolean enableSwitch(Context context, ApplicationInfo app) { public boolean enableSwitch(Context context, ApplicationInfo app) {
try { try {
PackageInfo info = context.getPackageManager().getPackageInfo( PackageInfo info = context.getPackageManager().getPackageInfo(
app.packageName, PackageManager.GET_SIGNATURES); app.packageName, PackageManager.GET_PERMISSIONS);
RoleManager rm = context.getSystemService(RoleManager.class);
final AppRow row = new AppRow(); final AppRow row = new AppRow();
recordCanBeBlocked(context, context.getPackageManager(), rm, info, row); recordCanBeBlocked(info, row);
boolean systemBlockable = !row.systemApp || (row.systemApp && row.banned); boolean systemBlockable = !row.systemApp || (row.systemApp && row.banned);
return systemBlockable && !row.lockedImportance; return systemBlockable && !row.lockedImportance;
} catch (PackageManager.NameNotFoundException e) { } catch (PackageManager.NameNotFoundException e) {

View File

@@ -24,6 +24,7 @@ import android.app.NotificationChannel;
import android.app.NotificationChannelGroup; import android.app.NotificationChannelGroup;
import android.app.NotificationManager; import android.app.NotificationManager;
import android.content.Context; import android.content.Context;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager; import android.content.pm.PackageManager;
import android.content.pm.ShortcutInfo; import android.content.pm.ShortcutInfo;
import android.graphics.drawable.Drawable; import android.graphics.drawable.Drawable;
@@ -171,7 +172,8 @@ public abstract class NotificationPreferenceController extends AbstractPreferenc
return overrideCanConfigureValue; return overrideCanConfigureValue;
} }
if (mAppRow != null) { if (mAppRow != null) {
return !mAppRow.systemApp && !mAppRow.lockedImportance; boolean systemBlockable = !mAppRow.systemApp || (mAppRow.systemApp && mAppRow.banned);
return systemBlockable && !mAppRow.lockedImportance;
} }
return true; return true;
} }

View File

@@ -25,7 +25,6 @@ import android.app.Notification;
import android.app.NotificationChannel; import android.app.NotificationChannel;
import android.app.NotificationChannelGroup; import android.app.NotificationChannelGroup;
import android.app.NotificationManager; import android.app.NotificationManager;
import android.app.role.RoleManager;
import android.content.BroadcastReceiver; import android.content.BroadcastReceiver;
import android.content.Context; import android.content.Context;
import android.content.Intent; import android.content.Intent;
@@ -69,7 +68,6 @@ abstract public class NotificationSettings extends DashboardFragment {
protected PackageManager mPm; protected PackageManager mPm;
protected NotificationBackend mBackend = new NotificationBackend(); protected NotificationBackend mBackend = new NotificationBackend();
protected NotificationManager mNm; protected NotificationManager mNm;
protected RoleManager mRm;
protected Context mContext; protected Context mContext;
protected int mUid; protected int mUid;
@@ -116,7 +114,6 @@ abstract public class NotificationSettings extends DashboardFragment {
mPm = getPackageManager(); mPm = getPackageManager();
mNm = NotificationManager.from(mContext); mNm = NotificationManager.from(mContext);
mRm = mContext.getSystemService(RoleManager.class);
mPkg = mArgs != null && mArgs.containsKey(AppInfoBase.ARG_PACKAGE_NAME) mPkg = mArgs != null && mArgs.containsKey(AppInfoBase.ARG_PACKAGE_NAME)
? mArgs.getString(AppInfoBase.ARG_PACKAGE_NAME) ? mArgs.getString(AppInfoBase.ARG_PACKAGE_NAME)
@@ -290,7 +287,7 @@ abstract public class NotificationSettings extends DashboardFragment {
} }
private void loadAppRow() { private void loadAppRow() {
mAppRow = mBackend.loadAppRow(mContext, mPm, mRm, mPkgInfo); mAppRow = mBackend.loadAppRow(mContext, mPm, mPkgInfo);
} }
private void loadChannelGroup() { private void loadChannelGroup() {

View File

@@ -20,6 +20,7 @@ import static com.google.common.truth.Truth.assertThat;
import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue; import static org.junit.Assert.assertTrue;
import static org.mockito.ArgumentMatchers.anyInt;
import static org.mockito.ArgumentMatchers.anyString; import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.Mockito.mock; import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when; import static org.mockito.Mockito.when;
@@ -80,69 +81,17 @@ public class NotificationBackendTest {
} }
@Test @Test
public void testMarkAppRow_unblockablePackage() { public void testMarkAppRow_fixedImportance() throws Exception {
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 {
PackageInfo pi = new PackageInfo(); PackageInfo pi = new PackageInfo();
pi.packageName = "test"; pi.packageName = "test";
pi.applicationInfo = new ApplicationInfo(); pi.applicationInfo = new ApplicationInfo();
pi.applicationInfo.packageName = "test"; pi.applicationInfo.packageName = "test";
pi.applicationInfo.uid = 123; pi.applicationInfo.uid = 123;
List<String> roles = new ArrayList<>(); when(mInm.isImportanceLocked(pi.packageName, 123)).thenReturn(true);
roles.add(RoleManager.ROLE_DIALER);
RoleManager rm = mock(RoleManager.class);
when(rm.getHeldRolesFromController(anyString())).thenReturn(roles);
when(mInm.isPermissionFixed(pi.packageName, 0)).thenReturn(false);
AppRow appRow = new NotificationBackend().loadAppRow(RuntimeEnvironment.application, AppRow appRow = new NotificationBackend().loadAppRow(RuntimeEnvironment.application,
mock(PackageManager.class), rm, pi); mock(PackageManager.class), 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);
assertTrue(appRow.systemApp); assertTrue(appRow.systemApp);
assertTrue(appRow.lockedImportance); assertTrue(appRow.lockedImportance);
@@ -156,10 +105,10 @@ public class NotificationBackendTest {
pi.applicationInfo.packageName = "test"; pi.applicationInfo.packageName = "test";
pi.applicationInfo.uid = 123; 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, AppRow appRow = new NotificationBackend().loadAppRow(RuntimeEnvironment.application,
mock(PackageManager.class), mock(RoleManager.class), pi); mock(PackageManager.class), pi);
assertFalse(appRow.systemApp); assertFalse(appRow.systemApp);
assertFalse(appRow.lockedImportance); assertFalse(appRow.lockedImportance);
@@ -178,7 +127,7 @@ public class NotificationBackendTest {
when(mInm.isPermissionFixed(pi.packageName, 0)).thenReturn(false); when(mInm.isPermissionFixed(pi.packageName, 0)).thenReturn(false);
AppRow appRow = new NotificationBackend().loadAppRow(RuntimeEnvironment.application, AppRow appRow = new NotificationBackend().loadAppRow(RuntimeEnvironment.application,
mock(PackageManager.class), mock(RoleManager.class), pi); mock(PackageManager.class), pi);
assertFalse(appRow.systemApp); assertFalse(appRow.systemApp);
assertTrue(appRow.lockedImportance); assertTrue(appRow.lockedImportance);
@@ -198,29 +147,12 @@ public class NotificationBackendTest {
when(mInm.isPermissionFixed(pi.packageName, 0)).thenReturn(false); when(mInm.isPermissionFixed(pi.packageName, 0)).thenReturn(false);
AppRow appRow = new NotificationBackend().loadAppRow(RuntimeEnvironment.application, AppRow appRow = new NotificationBackend().loadAppRow(RuntimeEnvironment.application,
mock(PackageManager.class), mock(RoleManager.class), pi); mock(PackageManager.class), pi);
assertFalse(appRow.systemApp); assertFalse(appRow.systemApp);
assertFalse(appRow.lockedImportance); 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 @Test
public void testGetAggregatedUsageEvents_multipleEventsAgg() { public void testGetAggregatedUsageEvents_multipleEventsAgg() {
List<UsageEvents.Event> events = new ArrayList<>(); List<UsageEvents.Event> events = new ArrayList<>();

View File

@@ -284,24 +284,36 @@ public class NotificationPreferenceControllerTest {
} }
@Test @Test
public void testIsAppBlockable_postMigration_locked() { public void testIsAppBlockable_fixedPermission() {
mController = new TestPreferenceController(mContext, mBackend); mController = new TestPreferenceController(mContext, mBackend);
NotificationBackend.AppRow appRow = new NotificationBackend.AppRow(); NotificationBackend.AppRow appRow = new NotificationBackend.AppRow();
appRow.lockedImportance = true; appRow.systemApp = true;
appRow.banned = false; appRow.banned = false;
mController.onResume(appRow, null, null, null, null, null, null); mController.onResume(appRow, null, null, null, null, null, null);
assertFalse(mController.isAppBlockable()); assertFalse(mController.isAppBlockable());
} }
@Test @Test
public void testIsAppBlockable_postMigration_locked_butAppOff() { public void testIsAppBlockable_fixedPermission_butAppOff() {
mController = new TestPreferenceController(mContext, mBackend); mController = new TestPreferenceController(mContext, mBackend);
NotificationBackend.AppRow appRow = new NotificationBackend.AppRow(); NotificationBackend.AppRow appRow = new NotificationBackend.AppRow();
appRow.lockedImportance = true; appRow.systemApp = true;
appRow.banned = true; appRow.banned = true;
mController.onResume(appRow, null, null, null, null, null, null); 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()); assertFalse(mController.isAppBlockable());
} }