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

Commit 9372f3b4 authored by Julia Reynolds's avatar Julia Reynolds Committed by Android (Google) Code Review
Browse files

Merge "Use NMS calculation of fixed importance" into tm-dev

parents 56c7f91c 2011588b
Loading
Loading
Loading
Loading
+7 −35
Original line number Diff line number Diff line
@@ -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) {
@@ -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;
@@ -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) {
+3 −1
Original line number Diff line number Diff line
@@ -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;
@@ -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;
    }
+1 −4
Original line number Diff line number Diff line
@@ -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;
@@ -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;
@@ -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)
@@ -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() {
+8 −76
Original line number Diff line number Diff line
@@ -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;
@@ -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);
@@ -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);
@@ -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);
@@ -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<>();
+16 −4
Original line number Diff line number Diff line
@@ -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());
    }