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

Commit 68263d1d authored by Julia Reynolds's avatar Julia Reynolds
Browse files

Disable non-system managed services on low ram devices

Bug: 35219985
Test: runtest systemui-notification
Change-Id: If25a387a77f2755326f9c59eee41bb43f7019e7b
parent be34f2d0
Loading
Loading
Loading
Loading
+48 −33
Original line number Original line Diff line number Diff line
@@ -265,6 +265,7 @@ public class NotificationManagerService extends SystemService {
    private static final String EXTRA_KEY = "key";
    private static final String EXTRA_KEY = "key";


    private IActivityManager mAm;
    private IActivityManager mAm;
    private ActivityManager mActivityManager;
    private IPackageManager mPackageManager;
    private IPackageManager mPackageManager;
    private PackageManager mPackageManagerClient;
    private PackageManager mPackageManagerClient;
    AudioManager mAudioManager;
    AudioManager mAudioManager;
@@ -450,10 +451,13 @@ public class NotificationManagerService extends SystemService {
        while (parser.next() != END_DOCUMENT) {
        while (parser.next() != END_DOCUMENT) {
            mZenModeHelper.readXml(parser, forRestore);
            mZenModeHelper.readXml(parser, forRestore);
            mRankingHelper.readXml(parser, forRestore);
            mRankingHelper.readXml(parser, forRestore);
            // No non-system managed services are allowed on low ram devices
            if (!ActivityManager.isLowRamDeviceStatic()) {
                saveXml |= mListeners.readXml(parser);
                saveXml |= mListeners.readXml(parser);
                saveXml |= mNotificationAssistants.readXml(parser);
                saveXml |= mNotificationAssistants.readXml(parser);
                saveXml |= mConditionProviders.readXml(parser);
                saveXml |= mConditionProviders.readXml(parser);
            }
            }
        }


        if (saveXml) {
        if (saveXml) {
            savePolicyFile();
            savePolicyFile();
@@ -1131,7 +1135,8 @@ public class NotificationManagerService extends SystemService {
            LightsManager lightsManager, NotificationListeners notificationListeners,
            LightsManager lightsManager, NotificationListeners notificationListeners,
            NotificationAssistants notificationAssistants, ConditionProviders conditionProviders,
            NotificationAssistants notificationAssistants, ConditionProviders conditionProviders,
            ICompanionDeviceManager companionManager, SnoozeHelper snoozeHelper,
            ICompanionDeviceManager companionManager, SnoozeHelper snoozeHelper,
            NotificationUsageStats usageStats, AtomicFile policyFile) {
            NotificationUsageStats usageStats, AtomicFile policyFile,
            ActivityManager activityManager) {
        Resources resources = getContext().getResources();
        Resources resources = getContext().getResources();
        mMaxPackageEnqueueRate = Settings.Global.getFloat(getContext().getContentResolver(),
        mMaxPackageEnqueueRate = Settings.Global.getFloat(getContext().getContentResolver(),
                Settings.Global.MAX_NOTIFICATION_ENQUEUE_RATE,
                Settings.Global.MAX_NOTIFICATION_ENQUEUE_RATE,
@@ -1145,6 +1150,7 @@ public class NotificationManagerService extends SystemService {
        mAppUsageStats = LocalServices.getService(UsageStatsManagerInternal.class);
        mAppUsageStats = LocalServices.getService(UsageStatsManagerInternal.class);
        mAlarmManager = (AlarmManager) getContext().getSystemService(Context.ALARM_SERVICE);
        mAlarmManager = (AlarmManager) getContext().getSystemService(Context.ALARM_SERVICE);
        mCompanionManager = companionManager;
        mCompanionManager = companionManager;
        mActivityManager = activityManager;


        mHandler = new WorkerHandler(looper);
        mHandler = new WorkerHandler(looper);
        mRankingThread.start();
        mRankingThread.start();
@@ -1330,7 +1336,8 @@ public class NotificationManagerService extends SystemService {
                new NotificationAssistants(AppGlobals.getPackageManager()),
                new NotificationAssistants(AppGlobals.getPackageManager()),
                new ConditionProviders(getContext(), mUserProfiles, AppGlobals.getPackageManager()),
                new ConditionProviders(getContext(), mUserProfiles, AppGlobals.getPackageManager()),
                null, snoozeHelper, new NotificationUsageStats(getContext()),
                null, snoozeHelper, new NotificationUsageStats(getContext()),
                new AtomicFile(new File(systemDir, "notification_policy.xml")));
                new AtomicFile(new File(systemDir, "notification_policy.xml")),
                (ActivityManager) getContext().getSystemService(Context.ACTIVITY_SERVICE));
        publishBinderService(Context.NOTIFICATION_SERVICE, mService);
        publishBinderService(Context.NOTIFICATION_SERVICE, mService);
        publishLocalService(NotificationManagerInternal.class, mInternalService);
        publishLocalService(NotificationManagerInternal.class, mInternalService);
    }
    }
@@ -2689,17 +2696,19 @@ public class NotificationManagerService extends SystemService {
        public void setNotificationPolicyAccessGranted(String pkg, boolean granted)
        public void setNotificationPolicyAccessGranted(String pkg, boolean granted)
                throws RemoteException {
                throws RemoteException {
            checkCallerIsSystemOrShell();
            checkCallerIsSystemOrShell();
            if (!mActivityManager.isLowRamDevice()) {
                mConditionProviders.setPackageOrComponentEnabled(
                mConditionProviders.setPackageOrComponentEnabled(
                        pkg, getCallingUserHandle().getIdentifier(), true, granted);
                        pkg, getCallingUserHandle().getIdentifier(), true, granted);


            getContext().sendBroadcastAsUser(new Intent(NotificationManager
                getContext().sendBroadcastAsUser(new Intent(
                    .ACTION_NOTIFICATION_POLICY_ACCESS_GRANTED_CHANGED)
                        NotificationManager.ACTION_NOTIFICATION_POLICY_ACCESS_GRANTED_CHANGED)
                                .setPackage(pkg)
                                .setPackage(pkg)
                                .addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY),
                                .addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY),
                        getCallingUserHandle(), null);
                        getCallingUserHandle(), null);


                savePolicyFile();
                savePolicyFile();
            }
            }
        }


        @Override
        @Override
        public Policy getNotificationPolicy(String pkg) {
        public Policy getNotificationPolicy(String pkg) {
@@ -2779,36 +2788,42 @@ public class NotificationManagerService extends SystemService {
                boolean granted) throws RemoteException {
                boolean granted) throws RemoteException {
            Preconditions.checkNotNull(listener);
            Preconditions.checkNotNull(listener);
            enforceSystemOrSystemUI("grant notification listener access");
            enforceSystemOrSystemUI("grant notification listener access");
            if (!mActivityManager.isLowRamDevice()) {
                mConditionProviders.setPackageOrComponentEnabled(listener.flattenToString(),
                mConditionProviders.setPackageOrComponentEnabled(listener.flattenToString(),
                        userId, false, granted);
                        userId, false, granted);
                mListeners.setPackageOrComponentEnabled(listener.flattenToString(),
                mListeners.setPackageOrComponentEnabled(listener.flattenToString(),
                        userId, true, granted);
                        userId, true, granted);


            getContext().sendBroadcastAsUser(new Intent(NotificationManager
                getContext().sendBroadcastAsUser(new Intent(
                    .ACTION_NOTIFICATION_POLICY_ACCESS_GRANTED_CHANGED)
                        NotificationManager.ACTION_NOTIFICATION_POLICY_ACCESS_GRANTED_CHANGED)
                                .setPackage(listener.getPackageName())
                                .setPackage(listener.getPackageName())
                    .addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY), getCallingUserHandle(), null);
                                .addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY),
                        getCallingUserHandle(), null);


                savePolicyFile();
                savePolicyFile();
            }
            }
        }


        @Override
        @Override
        public void setNotificationAssistantAccessGrantedForUser(ComponentName assistant,
        public void setNotificationAssistantAccessGrantedForUser(ComponentName assistant,
                int userId, boolean granted) throws RemoteException {
                int userId, boolean granted) throws RemoteException {
            Preconditions.checkNotNull(assistant);
            Preconditions.checkNotNull(assistant);
            enforceSystemOrSystemUI("grant notification assistant access");
            enforceSystemOrSystemUI("grant notification assistant access");
            if (!mActivityManager.isLowRamDevice()) {
                mConditionProviders.setPackageOrComponentEnabled(assistant.flattenToString(),
                mConditionProviders.setPackageOrComponentEnabled(assistant.flattenToString(),
                        userId, false, granted);
                        userId, false, granted);
                mNotificationAssistants.setPackageOrComponentEnabled(assistant.flattenToString(),
                mNotificationAssistants.setPackageOrComponentEnabled(assistant.flattenToString(),
                        userId, true, granted);
                        userId, true, granted);


            getContext().sendBroadcastAsUser(new Intent(NotificationManager
                getContext().sendBroadcastAsUser(new Intent(
                    .ACTION_NOTIFICATION_POLICY_ACCESS_GRANTED_CHANGED)
                        NotificationManager.ACTION_NOTIFICATION_POLICY_ACCESS_GRANTED_CHANGED)
                                .setPackage(assistant.getPackageName())
                                .setPackage(assistant.getPackageName())
                    .addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY), getCallingUserHandle(), null);
                                .addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY),
                        getCallingUserHandle(), null);


                savePolicyFile();
                savePolicyFile();
            }
            }
        }


        @Override
        @Override
        public void applyEnqueuedAdjustmentFromAssistant(INotificationListener token,
        public void applyEnqueuedAdjustmentFromAssistant(INotificationListener token,
+47 −1
Original line number Original line Diff line number Diff line
@@ -37,6 +37,7 @@ import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import static org.mockito.Mockito.when;


import android.app.ActivityManager;
import android.app.INotificationManager;
import android.app.INotificationManager;
import android.app.Notification;
import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationChannel;
@@ -106,6 +107,8 @@ public class NotificationManagerServiceTest extends NotificationTestCase {
    private NotificationUsageStats mUsageStats;
    private NotificationUsageStats mUsageStats;
    @Mock
    @Mock
    private AudioManager mAudioManager;
    private AudioManager mAudioManager;
    @Mock
    ActivityManager mActivityManager;
    private NotificationChannel mTestNotificationChannel = new NotificationChannel(
    private NotificationChannel mTestNotificationChannel = new NotificationChannel(
            TEST_CHANNEL_ID, TEST_CHANNEL_ID, NotificationManager.IMPORTANCE_DEFAULT);
            TEST_CHANNEL_ID, TEST_CHANNEL_ID, NotificationManager.IMPORTANCE_DEFAULT);
    @Mock
    @Mock
@@ -172,7 +175,7 @@ public class NotificationManagerServiceTest extends NotificationTestCase {
            mNotificationManagerService.init(mTestableLooper.getLooper(), mPackageManager,
            mNotificationManagerService.init(mTestableLooper.getLooper(), mPackageManager,
                    mPackageManagerClient, mockLightsManager, mNotificationListeners,
                    mPackageManagerClient, mockLightsManager, mNotificationListeners,
                    mNotificationAssistants, mConditionProviders, mCompanionMgr,
                    mNotificationAssistants, mConditionProviders, mCompanionMgr,
                    mSnoozeHelper, mUsageStats, mPolicyFile);
                    mSnoozeHelper, mUsageStats, mPolicyFile, mActivityManager);
        } catch (SecurityException e) {
        } catch (SecurityException e) {
            if (!e.getMessage().contains("Permission Denial: not allowed to send broadcast")) {
            if (!e.getMessage().contains("Permission Denial: not allowed to send broadcast")) {
                throw e;
                throw e;
@@ -1040,4 +1043,47 @@ public class NotificationManagerServiceTest extends NotificationTestCase {
        verify(mNotificationListeners, never()).setPackageOrComponentEnabled(
        verify(mNotificationListeners, never()).setPackageOrComponentEnabled(
                any(), anyInt(), anyBoolean(), anyBoolean());
                any(), anyInt(), anyBoolean(), anyBoolean());
    }
    }

    @Test
    public void testSetListenerAccess_doesNothingOnLowRam() throws Exception {
        when(mActivityManager.isLowRamDevice()).thenReturn(true);
        ComponentName c = ComponentName.unflattenFromString("package/Component");
        mBinderService.setNotificationListenerAccessGranted(c, true);

        verify(mNotificationListeners, never()).setPackageOrComponentEnabled(
                c.flattenToString(), 0, true, true);
        verify(mConditionProviders, never()).setPackageOrComponentEnabled(
                c.flattenToString(), 0, false, true);
        verify(mNotificationAssistants, never()).setPackageOrComponentEnabled(
                any(), anyInt(), anyBoolean(), anyBoolean());
    }

    @Test
    public void testSetAssistantAccess_doesNothingOnLowRam() throws Exception {
        when(mActivityManager.isLowRamDevice()).thenReturn(true);
        ComponentName c = ComponentName.unflattenFromString("package/Component");
        mBinderService.setNotificationAssistantAccessGranted(c, true);


        verify(mNotificationListeners, never()).setPackageOrComponentEnabled(
                c.flattenToString(), 0, true, true);
        verify(mConditionProviders, never()).setPackageOrComponentEnabled(
                c.flattenToString(), 0, false, true);
        verify(mNotificationAssistants, never()).setPackageOrComponentEnabled(
                any(), anyInt(), anyBoolean(), anyBoolean());
    }

    @Test
    public void testSetDndAccess_doesNothingOnLowRam() throws Exception {
        when(mActivityManager.isLowRamDevice()).thenReturn(true);
        ComponentName c = ComponentName.unflattenFromString("package/Component");
        mBinderService.setNotificationPolicyAccessGranted(c.getPackageName(), true);

        verify(mNotificationListeners, never()).setPackageOrComponentEnabled(
                c.flattenToString(), 0, true, true);
        verify(mConditionProviders, never()).setPackageOrComponentEnabled(
                c.flattenToString(), 0, false, true);
        verify(mNotificationAssistants, never()).setPackageOrComponentEnabled(
                any(), anyInt(), anyBoolean(), anyBoolean());
    }
}
}