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

Commit 26454070 authored by Jan Tomljanovic's avatar Jan Tomljanovic Committed by Android (Google) Code Review
Browse files

Merge "Prevent process state uplift when toast is rendered by the systemUI....

Merge "Prevent process state uplift when toast is rendered by the systemUI. Test: NotificationManagerServiceTest"
parents 2acc3f4a 874ef0d1
Loading
Loading
Loading
Loading
+2 −2
Original line number Original line Diff line number Diff line
@@ -7288,12 +7288,12 @@ public class NotificationManagerService extends SystemService {


    @GuardedBy("mToastQueue")
    @GuardedBy("mToastQueue")
    private void keepProcessAliveForToastIfNeededLocked(int pid) {
    private void keepProcessAliveForToastIfNeededLocked(int pid) {
        int toastCount = 0; // toasts from this pid
        int toastCount = 0; // toasts from this pid, rendered by the app
        ArrayList<ToastRecord> list = mToastQueue;
        ArrayList<ToastRecord> list = mToastQueue;
        int n = list.size();
        int n = list.size();
        for (int i = 0; i < n; i++) {
        for (int i = 0; i < n; i++) {
            ToastRecord r = list.get(i);
            ToastRecord r = list.get(i);
            if (r.pid == pid) {
            if (r.pid == pid && r.keepProcessAlive()) {
                toastCount++;
                toastCount++;
            }
            }
        }
        }
+7 −0
Original line number Original line Diff line number Diff line
@@ -70,6 +70,13 @@ public class CustomToastRecord extends ToastRecord {
        }
        }
    }
    }


    @Override
    public boolean keepProcessAlive() {
        // As custom toasts are rendered by the app, we need to keep the app alive for it to show
        // the toast.
        return true;
    }

    @Override
    @Override
    public String toString() {
    public String toString() {
        return "CustomToastRecord{"
        return "CustomToastRecord{"
+10 −0
Original line number Original line Diff line number Diff line
@@ -85,4 +85,14 @@ public abstract class ToastRecord {
        }
        }
        pw.println(prefix + this);
        pw.println(prefix + this);
    }
    }

    /**
     * Returns whether it's necessary to bump the process state to keep it alive in order to show
     * the toast.
     */
    public boolean keepProcessAlive() {
        // By default we assume the toast is rendered by the systemUI. Any toast rendered by the app
        // should override this method.
        return false;
    }
}
}
+64 −0
Original line number Original line Diff line number Diff line
@@ -4855,6 +4855,70 @@ public class NotificationManagerServiceTest extends UiServiceTestCase {
        assertEquals(1, mService.mToastQueue.size());
        assertEquals(1, mService.mToastQueue.size());
    }
    }


    @Test
    public void backgroundSystemCustomToast_callsSetProcessImportantAsForegroundForToast() throws
            Exception {
        final String testPackage = "testPackageName";
        assertEquals(0, mService.mToastQueue.size());
        mService.isSystemUid = true;

        // package is not suspended
        when(mPackageManager.isPackageSuspendedForUser(testPackage, UserHandle.getUserId(mUid)))
                .thenReturn(false);

        // notifications from this package are blocked by the user
        mService.setPreferencesHelper(mPreferencesHelper);
        when(mPreferencesHelper.getImportance(testPackage, mUid)).thenReturn(IMPORTANCE_NONE);

        setAppInForegroundForToasts(mUid, false);

        // enqueue toast -> toast should still enqueue
        ((INotificationManager) mService.mService).enqueueToast(testPackage, new Binder(),
                new TestableToastCallback(), 2000, 0);
        assertEquals(1, mService.mToastQueue.size());
        verify(mAm).setProcessImportant(any(), anyInt(), eq(true), any());
    }

    @Test
    public void foregroundTextToast_callsSetProcessImportantAsNotForegroundForToast() throws
            Exception {
        final String testPackage = "testPackageName";
        assertEquals(0, mService.mToastQueue.size());
        mService.isSystemUid = false;

        // package is not suspended
        when(mPackageManager.isPackageSuspendedForUser(testPackage, UserHandle.getUserId(mUid)))
                .thenReturn(false);

        setAppInForegroundForToasts(mUid, true);

        // enqueue toast -> toast should still enqueue
        ((INotificationManager) mService.mService).enqueueTextToast(testPackage, new Binder(),
                "Text", 2000, 0, null);
        assertEquals(1, mService.mToastQueue.size());
        verify(mAm).setProcessImportant(any(), anyInt(), eq(false), any());
    }

    @Test
    public void backgroundTextToast_callsSetProcessImportantAsNotForegroundForToast() throws
            Exception {
        final String testPackage = "testPackageName";
        assertEquals(0, mService.mToastQueue.size());
        mService.isSystemUid = false;

        // package is not suspended
        when(mPackageManager.isPackageSuspendedForUser(testPackage, UserHandle.getUserId(mUid)))
                .thenReturn(false);

        setAppInForegroundForToasts(mUid, false);

        // enqueue toast -> toast should still enqueue
        ((INotificationManager) mService.mService).enqueueTextToast(testPackage, new Binder(),
                "Text", 2000, 0, null);
        assertEquals(1, mService.mToastQueue.size());
        verify(mAm).setProcessImportant(any(), anyInt(), eq(false), any());
    }

    @Test
    @Test
    public void testTextToastsCallStatusBar() throws Exception {
    public void testTextToastsCallStatusBar() throws Exception {
        final String testPackage = "testPackageName";
        final String testPackage = "testPackageName";