diff options
author | 2024-11-12 22:20:01 +0000 | |
---|---|---|
committer | 2024-11-12 22:20:01 +0000 | |
commit | 7f0778993eb5a124d85243f20076818c39c5f63c (patch) | |
tree | ef078eb250ceb7b3ab92c9a631b668af92753ca4 | |
parent | 511699c8f1908b5a2d76f4dda57e950f0e889188 (diff) | |
parent | 11624e75781d057838aaab1ee71dd65944d65a69 (diff) |
Merge changes from topic "device-effects-applier-integration" into main
* changes:
Fix setting device effects applier
Annotate NotificationManagerInternal with @Keep
5 files changed, 80 insertions, 21 deletions
diff --git a/services/core/java/com/android/server/notification/NotificationManagerInternal.java b/services/core/java/com/android/server/notification/NotificationManagerInternal.java index d5d4070ee4c3..52ddb800fa40 100644 --- a/services/core/java/com/android/server/notification/NotificationManagerInternal.java +++ b/services/core/java/com/android/server/notification/NotificationManagerInternal.java @@ -22,8 +22,11 @@ import android.app.NotificationChannelGroup; import android.app.backup.BackupRestoreEventLogger; import android.service.notification.DeviceEffectsApplier; +import com.android.internal.annotations.Keep; + import java.util.Set; +@Keep public interface NotificationManagerInternal { NotificationChannel getNotificationChannel(String pkg, int uid, String channelId); NotificationChannelGroup getNotificationChannelGroup(String pkg, int uid, String channelId); diff --git a/services/core/java/com/android/server/notification/NotificationManagerService.java b/services/core/java/com/android/server/notification/NotificationManagerService.java index 5d088362d79f..207764b4e555 100644 --- a/services/core/java/com/android/server/notification/NotificationManagerService.java +++ b/services/core/java/com/android/server/notification/NotificationManagerService.java @@ -3089,7 +3089,7 @@ public class NotificationManagerService extends SystemService { migrateDefaultNAS(); maybeShowInitialReviewPermissionsNotification(); - if (android.app.Flags.modesApi()) { + if (android.app.Flags.modesApi() && !mZenModeHelper.hasDeviceEffectsApplier()) { // Cannot be done earlier, as some services aren't ready until this point. mZenModeHelper.setDeviceEffectsApplier( new DefaultDeviceEffectsApplier(getContext())); diff --git a/services/core/java/com/android/server/notification/ZenModeHelper.java b/services/core/java/com/android/server/notification/ZenModeHelper.java index cfeacdf2bb0d..ca4f83fd46f6 100644 --- a/services/core/java/com/android/server/notification/ZenModeHelper.java +++ b/services/core/java/com/android/server/notification/ZenModeHelper.java @@ -24,6 +24,8 @@ import static android.app.NotificationManager.AUTOMATIC_RULE_STATUS_ENABLED; import static android.app.NotificationManager.AUTOMATIC_RULE_STATUS_REMOVED; import static android.app.NotificationManager.AUTOMATIC_RULE_STATUS_UNKNOWN; import static android.app.NotificationManager.Policy.PRIORITY_SENDERS_ANY; +import static android.app.backup.NotificationLoggingConstants.DATA_TYPE_ZEN_CONFIG; +import static android.app.backup.NotificationLoggingConstants.ERROR_XML_PARSING; import static android.service.notification.Condition.SOURCE_UNKNOWN; import static android.service.notification.Condition.SOURCE_USER_ACTION; import static android.service.notification.Condition.STATE_FALSE; @@ -44,8 +46,6 @@ import static android.service.notification.ZenModeConfig.isImplicitRuleId; import static com.android.internal.util.FrameworkStatsLog.DND_MODE_RULE; import static com.android.internal.util.Preconditions.checkArgument; -import static android.app.backup.NotificationLoggingConstants.DATA_TYPE_ZEN_CONFIG; -import static android.app.backup.NotificationLoggingConstants.ERROR_XML_PARSING; import static java.util.Objects.requireNonNull; @@ -303,6 +303,15 @@ public class ZenModeHelper { } /** + * @return whether a {@link DeviceEffectsApplier} has already been set or not + */ + boolean hasDeviceEffectsApplier() { + synchronized (mConfigLock) { + return mDeviceEffectsApplier != null; + } + } + + /** * Set the {@link DeviceEffectsApplier} used to apply the consolidated effects. * * <p>Previously calculated effects (as loaded from the user's {@link ZenModeConfig}) will be diff --git a/services/tests/uiservicestests/src/com/android/server/notification/NotificationManagerServiceTest.java b/services/tests/uiservicestests/src/com/android/server/notification/NotificationManagerServiceTest.java index 0d9129f746cd..704c1b858b8d 100644 --- a/services/tests/uiservicestests/src/com/android/server/notification/NotificationManagerServiceTest.java +++ b/services/tests/uiservicestests/src/com/android/server/notification/NotificationManagerServiceTest.java @@ -361,6 +361,9 @@ import org.mockito.MockitoAnnotations; import org.mockito.invocation.InvocationOnMock; import org.mockito.stubbing.Answer; +import platform.test.runner.parameterized.ParameterizedAndroidJunit4; +import platform.test.runner.parameterized.Parameters; + import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import java.io.ByteArrayInputStream; @@ -375,9 +378,6 @@ import java.util.Map; import java.util.concurrent.CountDownLatch; import java.util.function.Consumer; -import platform.test.runner.parameterized.ParameterizedAndroidJunit4; -import platform.test.runner.parameterized.Parameters; - @SmallTest @RunWith(ParameterizedAndroidJunit4.class) @RunWithLooper @@ -490,7 +490,8 @@ public class NotificationManagerServiceTest extends UiServiceTestCase { private final NotificationChannel mParentChannel = new NotificationChannel(PARENT_CHANNEL_ID, "parentName", IMPORTANCE_DEFAULT); private final NotificationChannel mConversationChannel = - new NotificationChannel(CONVERSATION_CHANNEL_ID, "conversationName", IMPORTANCE_DEFAULT); + new NotificationChannel( + CONVERSATION_CHANNEL_ID, "conversationName", IMPORTANCE_DEFAULT); private static final String PARENT_CHANNEL_ID = "parentChannelId"; private static final String CONVERSATION_CHANNEL_ID = "conversationChannelId"; @@ -4297,8 +4298,13 @@ public class NotificationManagerServiceTest extends UiServiceTestCase { new NotificationChannel("foo", "foo", IMPORTANCE_HIGH)); Notification.TvExtender tv = new Notification.TvExtender().setChannelId("foo"); - mBinderService.enqueueNotificationWithTag(mPkg, mPkg, "testTvExtenderChannelOverride_onTv", 0, - generateNotificationRecord(null, tv).getNotification(), mUserId); + mBinderService.enqueueNotificationWithTag( + mPkg, + mPkg, + "testTvExtenderChannelOverride_onTv", + 0, + generateNotificationRecord(null, tv).getNotification(), + mUserId); verify(mPreferencesHelper, times(1)).getConversationNotificationChannel( anyString(), anyInt(), eq("foo"), eq(null), anyBoolean(), anyBoolean()); } @@ -4312,8 +4318,13 @@ public class NotificationManagerServiceTest extends UiServiceTestCase { mTestNotificationChannel); Notification.TvExtender tv = new Notification.TvExtender().setChannelId("foo"); - mBinderService.enqueueNotificationWithTag(mPkg, mPkg, "testTvExtenderChannelOverride_notOnTv", - 0, generateNotificationRecord(null, tv).getNotification(), mUserId); + mBinderService.enqueueNotificationWithTag( + mPkg, + mPkg, + "testTvExtenderChannelOverride_notOnTv", + 0, + generateNotificationRecord(null, tv).getNotification(), + mUserId); verify(mPreferencesHelper, times(1)).getConversationNotificationChannel( anyString(), anyInt(), eq(mTestNotificationChannel.getId()), eq(null), anyBoolean(), anyBoolean()); @@ -7746,9 +7757,21 @@ public class NotificationManagerServiceTest extends UiServiceTestCase { .setContentTitle("foo") .setSmallIcon(android.R.drawable.sym_def_app_icon) .setStyle(new Notification.MessagingStyle("").addMessage(message2)); - NotificationRecord recordB = new NotificationRecord(mContext, new StatusBarNotification(mPkg, - mPkg, 0, "tag", mUid, 0, nbB.build(), UserHandle.getUserHandleForUid(mUid), null, 0), - c); + NotificationRecord recordB = + new NotificationRecord( + mContext, + new StatusBarNotification( + mPkg, + mPkg, + 0, + "tag", + mUid, + 0, + nbB.build(), + UserHandle.getUserHandleForUid(mUid), + null, + 0), + c); // Update means we drop access to first reset(mUgmInternal); @@ -15400,8 +15423,13 @@ public class NotificationManagerServiceTest extends UiServiceTestCase { for (int i = 0; i < NotificationManagerService.MAX_PACKAGE_NOTIFICATIONS; i++) { StatusBarNotification sbn = generateNotificationRecord(mTestNotificationChannel, i, null, false).getSbn(); - mBinderService.enqueueNotificationWithTag(mPkg, mPkg, "testCannotPostNonUijWhenOverLimit", - sbn.getId(), sbn.getNotification(), sbn.getUserId()); + mBinderService.enqueueNotificationWithTag( + mPkg, + mPkg, + "testCannotPostNonUijWhenOverLimit", + sbn.getId(), + sbn.getNotification(), + sbn.getUserId()); waitForIdle(); } @@ -16131,6 +16159,8 @@ public class NotificationManagerServiceTest extends UiServiceTestCase { initNMS(SystemService.PHASE_SYSTEM_SERVICES_READY); mInternalService.setDeviceEffectsApplier(mock(DeviceEffectsApplier.class)); + + mService.onBootPhase(SystemService.PHASE_THIRD_PARTY_APPS_CAN_START, mMainLooper); // No exception! } diff --git a/services/tests/uiservicestests/src/com/android/server/notification/ZenModeHelperTest.java b/services/tests/uiservicestests/src/com/android/server/notification/ZenModeHelperTest.java index 4b94e103b9f4..020670dc0f0a 100644 --- a/services/tests/uiservicestests/src/com/android/server/notification/ZenModeHelperTest.java +++ b/services/tests/uiservicestests/src/com/android/server/notification/ZenModeHelperTest.java @@ -49,6 +49,8 @@ import static android.app.NotificationManager.Policy.PRIORITY_SENDERS_CONTACTS; import static android.app.NotificationManager.Policy.PRIORITY_SENDERS_STARRED; import static android.app.NotificationManager.Policy.STATE_PRIORITY_CHANNELS_BLOCKED; import static android.app.NotificationManager.Policy.SUPPRESSED_EFFECT_BADGE; +import static android.app.backup.NotificationLoggingConstants.DATA_TYPE_ZEN_CONFIG; +import static android.app.backup.NotificationLoggingConstants.DATA_TYPE_ZEN_RULES; import static android.content.pm.PackageManager.PERMISSION_GRANTED; import static android.os.Process.SYSTEM_UID; import static android.provider.Settings.Global.ZEN_MODE_ALARMS; @@ -84,8 +86,6 @@ import static com.android.os.dnd.DNDProtoEnums.PEOPLE_STARRED; import static com.android.os.dnd.DNDProtoEnums.ROOT_CONFIG; import static com.android.os.dnd.DNDProtoEnums.STATE_ALLOW; import static com.android.os.dnd.DNDProtoEnums.STATE_DISALLOW; -import static android.app.backup.NotificationLoggingConstants.DATA_TYPE_ZEN_CONFIG; -import static android.app.backup.NotificationLoggingConstants.DATA_TYPE_ZEN_RULES; import static com.android.server.notification.ZenModeEventLogger.ACTIVE_RULE_TYPE_MANUAL; import static com.android.server.notification.ZenModeHelper.RULE_LIMIT_PER_PACKAGE; @@ -102,6 +102,7 @@ import static junit.framework.TestCase.fail; import static org.junit.Assert.assertNotEquals; import static org.junit.Assert.assertNull; +import static org.junit.Assert.assertThrows; import static org.mockito.ArgumentMatchers.any; import static org.mockito.ArgumentMatchers.anyInt; import static org.mockito.ArgumentMatchers.anyString; @@ -200,6 +201,9 @@ import org.mockito.Mock; import org.mockito.MockitoAnnotations; import org.xmlpull.v1.XmlPullParserException; +import platform.test.runner.parameterized.ParameterizedAndroidJunit4; +import platform.test.runner.parameterized.Parameters; + import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import java.io.ByteArrayInputStream; @@ -219,9 +223,6 @@ import java.util.concurrent.CountDownLatch; import java.util.concurrent.TimeUnit; import java.util.stream.Collectors; -import platform.test.runner.parameterized.ParameterizedAndroidJunit4; -import platform.test.runner.parameterized.Parameters; - @SmallTest @SuppressLint("GuardedBy") // It's ok for this test to access guarded methods from the service. @RunWith(ParameterizedAndroidJunit4.class) @@ -5348,6 +5349,22 @@ public class ZenModeHelperTest extends UiServiceTestCase { mTestableLooper.processAllMessages(); verify(mDeviceEffectsApplier).apply(eq(effects), eq(ORIGIN_APP)); + assertTrue(mZenModeHelper.hasDeviceEffectsApplier()); + } + + @Test + public void testHasDeviceEffectsApplier_returnsFalseIfNotSet() { + assertFalse(mZenModeHelper.hasDeviceEffectsApplier()); + } + + @Test + @EnableFlags(FLAG_MODES_API) + public void testSettingDeviceEffects_throwsExceptionIfAlreadySet() { + mZenModeHelper.setDeviceEffectsApplier(mDeviceEffectsApplier); + + assertThrows( + IllegalStateException.class, + () -> mZenModeHelper.setDeviceEffectsApplier(mDeviceEffectsApplier)); } @Test |