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

Commit d61aae29 authored by Julia Reynolds's avatar Julia Reynolds
Browse files

Restrict alarm broadcast

To android only

Test: NotificationManagerTest, NotificationManagerServiceTest
Bug: 175614289
Change-Id: I4f8e56729d90f8f5288d08881129b1c45d5790e7
Merged-In: I4f8e56729d90f8f5288d08881129b1c45d5790e7
(cherry picked from commit abf15b73)
(cherry picked from commit 4056976f)
(cherry picked from commit f1e84b42)
parent d749dc35
Loading
Loading
Loading
Loading
+2 −1
Original line number Diff line number Diff line
@@ -207,6 +207,7 @@ import com.android.server.lights.LightsManager;
import com.android.server.notification.ManagedServices.ManagedServiceInfo;
import com.android.server.notification.ManagedServices.UserProfiles;
import com.android.server.policy.PhoneWindowManager;
import com.android.server.pm.PackageManagerService;
import com.android.server.statusbar.StatusBarManagerInternal;
import com.android.server.wm.WindowManagerInternal;

@@ -4811,7 +4812,7 @@ public class NotificationManagerService extends SystemService {
            final PendingIntent pi = PendingIntent.getBroadcast(getContext(),
                    REQUEST_CODE_TIMEOUT,
                    new Intent(ACTION_NOTIFICATION_TIMEOUT)
                            .setPackage("android")
                            .setPackage(PackageManagerService.PLATFORM_PACKAGE_NAME)
                            .setData(new Uri.Builder().scheme(SCHEME_TIMEOUT)
                                    .appendPath(record.getKey()).build())
                            .addFlags(Intent.FLAG_RECEIVER_FOREGROUND)
+27 −0
Original line number Diff line number Diff line
@@ -64,6 +64,7 @@ import static org.mockito.Mockito.when;

import android.app.ActivityManager;
import android.app.ActivityManagerInternal;
import android.app.AlarmManager;
import android.app.IActivityManager;
import android.app.INotificationManager;
import android.app.Notification;
@@ -71,6 +72,7 @@ import android.app.Notification.MessagingStyle.Message;
import android.app.NotificationChannel;
import android.app.NotificationChannelGroup;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.admin.DeviceAdminInfo;
import android.app.admin.DevicePolicyManagerInternal;
import android.app.usage.UsageStatsManagerInternal;
@@ -120,6 +122,7 @@ import com.android.server.lights.Light;
import com.android.server.lights.LightsManager;
import com.android.server.notification.NotificationManagerService.NotificationAssistants;
import com.android.server.notification.NotificationManagerService.NotificationListeners;
import com.android.server.pm.PackageManagerService;

import org.junit.After;
import org.junit.Before;
@@ -190,6 +193,8 @@ public class NotificationManagerServiceTest extends UiServiceTestCase {
    IBinder mPermOwner;
    @Mock
    IActivityManager mAm;
    @Mock
    AlarmManager mAlarmManager;

    // Use a Testable subclass so we can simulate calls from the system without failing.
    private static class TestableNotificationManagerService extends NotificationManagerService {
@@ -236,6 +241,8 @@ public class NotificationManagerServiceTest extends UiServiceTestCase {
                Secure.NOTIFICATION_BADGING, 1,
                UserHandle.getUserHandleForUid(mUid).getIdentifier());

        mContext.addMockSystemService(Context.ALARM_SERVICE, mAlarmManager);

        mService = new TestableNotificationManagerService(mContext);

        // Use this testable looper.
@@ -391,6 +398,26 @@ public class NotificationManagerServiceTest extends UiServiceTestCase {
        return answers;
    }

    @Test
    public void testLimitTimeOutBroadcast() {
        NotificationChannel channel = new NotificationChannel("id", "name",
                NotificationManager.IMPORTANCE_HIGH);
        Notification.Builder nb = new Notification.Builder(mContext, channel.getId())
                .setContentTitle("foo")
                .setSmallIcon(android.R.drawable.sym_def_app_icon)
                .setTimeoutAfter(1);

        StatusBarNotification sbn = new StatusBarNotification(PKG, PKG, 8, "tag", mUid, 0,
                nb.build(), UserHandle.getUserHandleForUid(mUid), null, 0);
        NotificationRecord r = new NotificationRecord(mContext, sbn, channel);

        mService.scheduleTimeoutLocked(r);
        ArgumentCaptor<PendingIntent> captor = ArgumentCaptor.forClass(PendingIntent.class);
        verify(mAlarmManager).setExactAndAllowWhileIdle(anyInt(), anyLong(), captor.capture());
        assertEquals(PackageManagerService.PLATFORM_PACKAGE_NAME,
                captor.getValue().getIntent().getPackage());
    }

    @Test
    public void testCreateNotificationChannels_SingleChannel() throws Exception {
        final NotificationChannel channel =