diff --git a/services/core/java/com/android/server/notification/CountdownConditionProvider.java b/services/core/java/com/android/server/notification/CountdownConditionProvider.java index d04aac2f57173..471c9b97200f7 100644 --- a/services/core/java/com/android/server/notification/CountdownConditionProvider.java +++ b/services/core/java/com/android/server/notification/CountdownConditionProvider.java @@ -32,6 +32,7 @@ import android.util.Log; import android.util.Slog; import com.android.server.notification.NotificationManagerService.DumpFilter; +import com.android.server.pm.PackageManagerService; import java.io.PrintWriter; @@ -114,11 +115,7 @@ public class CountdownConditionProvider extends SystemConditionProviderService { mIsAlarm = ZenModeConfig.isValidCountdownToAlarmConditionId(conditionId); final AlarmManager alarms = (AlarmManager) mContext.getSystemService(Context.ALARM_SERVICE); - final Intent intent = new Intent(ACTION) - .putExtra(EXTRA_CONDITION_ID, conditionId) - .setFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY); - final PendingIntent pendingIntent = PendingIntent.getBroadcast(mContext, REQUEST_CODE, - intent, PendingIntent.FLAG_UPDATE_CURRENT | PendingIntent.FLAG_MUTABLE_UNAUDITED); + final PendingIntent pendingIntent = getPendingIntent(conditionId); alarms.cancel(pendingIntent); if (mTime > 0) { final long now = System.currentTimeMillis(); @@ -138,6 +135,16 @@ public class CountdownConditionProvider extends SystemConditionProviderService { } } + PendingIntent getPendingIntent(Uri conditionId) { + final Intent intent = new Intent(ACTION) + .setPackage(PackageManagerService.PLATFORM_PACKAGE_NAME) + .putExtra(EXTRA_CONDITION_ID, conditionId) + .setFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY); + final PendingIntent pendingIntent = PendingIntent.getBroadcast(mContext, REQUEST_CODE, + intent, PendingIntent.FLAG_UPDATE_CURRENT | PendingIntent.FLAG_IMMUTABLE); + return pendingIntent; + } + @Override public void onUnsubscribe(Uri conditionId) { // noop diff --git a/services/core/java/com/android/server/notification/EventConditionProvider.java b/services/core/java/com/android/server/notification/EventConditionProvider.java index 25ad9280fa99b..4be4f0a1e7f02 100644 --- a/services/core/java/com/android/server/notification/EventConditionProvider.java +++ b/services/core/java/com/android/server/notification/EventConditionProvider.java @@ -41,6 +41,7 @@ import android.util.SparseArray; import com.android.server.notification.CalendarTracker.CheckEventResult; import com.android.server.notification.NotificationManagerService.DumpFilter; +import com.android.server.pm.PackageManagerService; import java.io.PrintWriter; import java.util.ArrayList; @@ -266,12 +267,7 @@ public class EventConditionProvider extends SystemConditionProviderService { private void rescheduleAlarm(long now, long time) { mNextAlarmTime = time; final AlarmManager alarms = (AlarmManager) mContext.getSystemService(Context.ALARM_SERVICE); - final PendingIntent pendingIntent = PendingIntent.getBroadcast(mContext, - REQUEST_CODE_EVALUATE, - new Intent(ACTION_EVALUATE) - .addFlags(Intent.FLAG_RECEIVER_FOREGROUND) - .putExtra(EXTRA_TIME, time), - PendingIntent.FLAG_UPDATE_CURRENT | PendingIntent.FLAG_IMMUTABLE); + final PendingIntent pendingIntent = getPendingIntent(time); alarms.cancel(pendingIntent); if (time == 0 || time < now) { if (DEBUG) Slog.d(TAG, "Not scheduling evaluate: " + (time == 0 ? "no time specified" @@ -283,6 +279,17 @@ public class EventConditionProvider extends SystemConditionProviderService { alarms.setExact(AlarmManager.RTC_WAKEUP, time, pendingIntent); } + PendingIntent getPendingIntent(long time) { + final PendingIntent pendingIntent = PendingIntent.getBroadcast(mContext, + REQUEST_CODE_EVALUATE, + new Intent(ACTION_EVALUATE) + .addFlags(Intent.FLAG_RECEIVER_FOREGROUND) + .setPackage(PackageManagerService.PLATFORM_PACKAGE_NAME) + .putExtra(EXTRA_TIME, time), + PendingIntent.FLAG_UPDATE_CURRENT | PendingIntent.FLAG_IMMUTABLE); + return pendingIntent; + } + private Condition createCondition(Uri id, int state) { final String summary = NOT_SHOWN; final String line1 = NOT_SHOWN; diff --git a/services/tests/uiservicestests/src/com/android/server/notification/CountdownConditionProviderTest.java b/services/tests/uiservicestests/src/com/android/server/notification/CountdownConditionProviderTest.java new file mode 100644 index 0000000000000..ddb9b43be76fb --- /dev/null +++ b/services/tests/uiservicestests/src/com/android/server/notification/CountdownConditionProviderTest.java @@ -0,0 +1,73 @@ +/* + * Copyright (C) 2021 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.android.server.notification; + +import static org.junit.Assert.assertEquals; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.spy; + +import android.app.Application; +import android.app.PendingIntent; +import android.content.Intent; +import android.net.Uri; +import android.testing.AndroidTestingRunner; +import android.testing.TestableLooper.RunWithLooper; + +import androidx.test.filters.SmallTest; + +import com.android.server.UiServiceTestCase; +import com.android.server.pm.PackageManagerService; + +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.MockitoAnnotations; + +@RunWith(AndroidTestingRunner.class) +@SmallTest +@RunWithLooper +public class CountdownConditionProviderTest extends UiServiceTestCase { + + CountdownConditionProvider mService; + + @Before + public void setUp() throws Exception { + MockitoAnnotations.initMocks(this); + + Intent startIntent = + new Intent("com.android.server.notification.CountdownConditionProvider"); + startIntent.setPackage("android"); + CountdownConditionProvider service = new CountdownConditionProvider(); + service.attach( + getContext(), + null, // ActivityThread not actually used in Service + CountdownConditionProvider.class.getName(), + null, // token not needed when not talking with the activity manager + mock(Application.class), + null // mocked services don't talk with the activity manager + ); + service.onCreate(); + service.onBind(startIntent); + mService = spy(service); + } + + @Test + public void testGetPendingIntent() { + PendingIntent pi = mService.getPendingIntent(Uri.EMPTY); + assertEquals(PackageManagerService.PLATFORM_PACKAGE_NAME, pi.getIntent().getPackage()); + } +} diff --git a/services/tests/uiservicestests/src/com/android/server/notification/EventConditionProviderTest.java b/services/tests/uiservicestests/src/com/android/server/notification/EventConditionProviderTest.java new file mode 100644 index 0000000000000..4c440cabf9c4a --- /dev/null +++ b/services/tests/uiservicestests/src/com/android/server/notification/EventConditionProviderTest.java @@ -0,0 +1,73 @@ +/* + * Copyright (C) 2021 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.android.server.notification; + +import static org.junit.Assert.assertEquals; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.spy; + +import android.app.Application; +import android.app.PendingIntent; +import android.content.Intent; +import android.net.Uri; +import android.testing.AndroidTestingRunner; +import android.testing.TestableLooper.RunWithLooper; + +import androidx.test.filters.SmallTest; + +import com.android.server.UiServiceTestCase; +import com.android.server.pm.PackageManagerService; + +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.MockitoAnnotations; + +@RunWith(AndroidTestingRunner.class) +@SmallTest +@RunWithLooper +public class EventConditionProviderTest extends UiServiceTestCase { + + EventConditionProvider mService; + + @Before + public void setUp() throws Exception { + MockitoAnnotations.initMocks(this); + + Intent startIntent = + new Intent("com.android.server.notification.EventConditionProvider"); + startIntent.setPackage("android"); + EventConditionProvider service = new EventConditionProvider(); + service.attach( + getContext(), + null, // ActivityThread not actually used in Service + CountdownConditionProvider.class.getName(), + null, // token not needed when not talking with the activity manager + mock(Application.class), + null // mocked services don't talk with the activity manager + ); + service.onCreate(); + service.onBind(startIntent); + mService = spy(service); + } + + @Test + public void testGetPendingIntent() { + PendingIntent pi = mService.getPendingIntent(1000); + assertEquals(PackageManagerService.PLATFORM_PACKAGE_NAME, pi.getIntent().getPackage()); + } +}