From f9da7872aed0d4319b42b66f999f6c72aad4cf25 Mon Sep 17 00:00:00 2001 From: Steven Ng Date: Thu, 10 Feb 2022 17:22:33 +0000 Subject: [PATCH] Throw IllegalArgumentException when calling setNightModeCustomType with an unsupported type Also 1. update documentation about the IllegalArgumentException thrown in setNightModeCustomType 2. modify NightModeCustomType to not include MODE_NIGHT_CUSTOM_TYPE_UNKNOWN 3. add @IntDef, NightModeCustomReturnType, for the getNightModeCustomType 4. Fix lint warning Test: atest FrameworksUiServicesTests:UiModeManagerServiceTest Bug: 218860231 Change-Id: Id8b1f1d7a3ee60f62a0ff6cdee0b5e416a130d63 --- core/java/android/app/IUiModeManager.aidl | 3 +++ core/java/android/app/UiModeManager.java | 19 ++++++++++++++--- .../android/server/UiModeManagerService.java | 13 ++++++++++-- .../server/UiModeManagerServiceTest.java | 21 ++++++++++++++++++- 4 files changed, 50 insertions(+), 6 deletions(-) diff --git a/core/java/android/app/IUiModeManager.aidl b/core/java/android/app/IUiModeManager.aidl index 55afed20c826f..2242224b27695 100644 --- a/core/java/android/app/IUiModeManager.aidl +++ b/core/java/android/app/IUiModeManager.aidl @@ -73,6 +73,7 @@ interface IUiModeManager { * @param nightModeCustomType * @hide */ + @JavaPassthrough(annotation="@android.annotation.RequiresPermission(android.Manifest.permission.MODIFY_DAY_NIGHT_MODE)") void setNightModeCustomType(int nightModeCustomType); /** @@ -82,6 +83,7 @@ interface IUiModeManager { * {@link #MODE_NIGHT_CUSTOM_TYPE_UNKNOWN}. * @hide */ + @JavaPassthrough(annotation="@android.annotation.RequiresPermission(android.Manifest.permission.MODIFY_DAY_NIGHT_MODE)") int getNightModeCustomType(); /** @@ -113,6 +115,7 @@ interface IUiModeManager { * {@code nightModeCustomType}. * @hide */ + @JavaPassthrough(annotation="@android.annotation.RequiresPermission(android.Manifest.permission.MODIFY_DAY_NIGHT_MODE)") boolean setNightModeActivatedForCustomMode(int nightModeCustom, boolean active); /** diff --git a/core/java/android/app/UiModeManager.java b/core/java/android/app/UiModeManager.java index 73a9e5a221c71..92dc8d87fe4b8 100644 --- a/core/java/android/app/UiModeManager.java +++ b/core/java/android/app/UiModeManager.java @@ -244,7 +244,18 @@ public class UiModeManager { public static final int MODE_NIGHT_YES = 2; /** - * Granular types for {@link MODE_NIGHT_CUSTOM_TYPE_BEDTIME} + * Granular types for {@link #setNightModeCustomType(int)} + * @hide + */ + @IntDef(prefix = { "MODE_NIGHT_CUSTOM_TYPE_" }, value = { + MODE_NIGHT_CUSTOM_TYPE_SCHEDULE, + MODE_NIGHT_CUSTOM_TYPE_BEDTIME, + }) + @Retention(RetentionPolicy.SOURCE) + public @interface NightModeCustomType {} + + /** + * Granular types for {@link #getNightModeCustomType()} * @hide */ @IntDef(prefix = { "MODE_NIGHT_CUSTOM_TYPE_" }, value = { @@ -253,7 +264,7 @@ public class UiModeManager { MODE_NIGHT_CUSTOM_TYPE_BEDTIME, }) @Retention(RetentionPolicy.SOURCE) - public @interface NightModeCustomType {} + public @interface NightModeCustomReturnType {} /** * A granular type for {@link #MODE_NIGHT_CUSTOM} which is unknown. @@ -539,6 +550,8 @@ public class UiModeManager { * {@code nightModeCustomType}. * * @param nightModeCustomType + * @throws IllegalArgumentException if passed an unsupported type to + * {@code nightModeCustomType}. * @hide */ @SystemApi @@ -562,7 +575,7 @@ public class UiModeManager { */ @SystemApi @RequiresPermission(android.Manifest.permission.MODIFY_DAY_NIGHT_MODE) - public int getNightModeCustomType() { + public @NightModeCustomReturnType int getNightModeCustomType() { if (mService != null) { try { return mService.getNightModeCustomType(); diff --git a/services/core/java/com/android/server/UiModeManagerService.java b/services/core/java/com/android/server/UiModeManagerService.java index c236a7f800004..eaa8c16ceea87 100644 --- a/services/core/java/com/android/server/UiModeManagerService.java +++ b/services/core/java/com/android/server/UiModeManagerService.java @@ -43,6 +43,7 @@ import android.app.NotificationManager; import android.app.PendingIntent; import android.app.StatusBarManager; import android.app.UiModeManager; +import android.app.UiModeManager.NightModeCustomReturnType; import android.app.UiModeManager.NightModeCustomType; import android.content.BroadcastReceiver; import android.content.Context; @@ -111,6 +112,9 @@ final class UiModeManagerService extends SystemService { // Enable launching of applications when entering the dock. private static final boolean ENABLE_LAUNCH_DESK_DOCK_APP = true; private static final String SYSTEM_PROPERTY_DEVICE_THEME = "persist.sys.theme"; + @VisibleForTesting + public static final Set SUPPORTED_NIGHT_MODE_CUSTOM_TYPES = new ArraySet( + new Integer[]{MODE_NIGHT_CUSTOM_TYPE_SCHEDULE, MODE_NIGHT_CUSTOM_TYPE_BEDTIME}); private final Injector mInjector; private final Object mLock = new Object(); @@ -728,8 +732,13 @@ final class UiModeManagerService extends SystemService { case UiModeManager.MODE_NIGHT_NO: case UiModeManager.MODE_NIGHT_YES: case MODE_NIGHT_AUTO: - case MODE_NIGHT_CUSTOM: break; + case MODE_NIGHT_CUSTOM: + if (SUPPORTED_NIGHT_MODE_CUSTOM_TYPES.contains(customModeType)) { + break; + } + throw new IllegalArgumentException( + "Can't set the custom type to " + customModeType); default: throw new IllegalArgumentException("Unknown mode: " + mode); } @@ -783,7 +792,7 @@ final class UiModeManagerService extends SystemService { } @Override - public int getNightModeCustomType() { + public @NightModeCustomReturnType int getNightModeCustomType() { if (getContext().checkCallingOrSelfPermission( android.Manifest.permission.MODIFY_DAY_NIGHT_MODE) != PackageManager.PERMISSION_GRANTED) { diff --git a/services/tests/uiservicestests/src/com/android/server/UiModeManagerServiceTest.java b/services/tests/uiservicestests/src/com/android/server/UiModeManagerServiceTest.java index a12bc3b4c59ff..d386b5ae6d1ca 100644 --- a/services/tests/uiservicestests/src/com/android/server/UiModeManagerServiceTest.java +++ b/services/tests/uiservicestests/src/com/android/server/UiModeManagerServiceTest.java @@ -28,6 +28,8 @@ import static android.app.UiModeManager.PROJECTION_TYPE_ALL; import static android.app.UiModeManager.PROJECTION_TYPE_AUTOMOTIVE; import static android.app.UiModeManager.PROJECTION_TYPE_NONE; +import static com.android.server.UiModeManagerService.SUPPORTED_NIGHT_MODE_CUSTOM_TYPES; + import static com.google.common.truth.Truth.assertThat; import static junit.framework.TestCase.assertFalse; @@ -294,6 +296,24 @@ public class UiModeManagerServiceTest extends UiServiceTestCase { () -> mService.setNightModeCustomType(MODE_NIGHT_CUSTOM_TYPE_BEDTIME)); } + @Test + public void setNightModeCustomType_customTypeUnknown_shouldThrow() throws RemoteException { + assertThrows(IllegalArgumentException.class, + () -> mService.setNightModeCustomType(MODE_NIGHT_CUSTOM_TYPE_UNKNOWN)); + } + + @Test + public void setNightModeCustomType_customTypeUnsupported_shouldThrow() throws RemoteException { + assertThrows(IllegalArgumentException.class, + () -> { + int maxSupportedCustomType = 0; + for (Integer supportedType : SUPPORTED_NIGHT_MODE_CUSTOM_TYPES) { + maxSupportedCustomType = Math.max(maxSupportedCustomType, supportedType); + } + mService.setNightModeCustomType(maxSupportedCustomType + 1); + }); + } + @Test public void setNightModeCustomType_bedtime_shouldHaveNoScreenOffRegistered() throws RemoteException { @@ -777,7 +797,6 @@ public class UiModeManagerServiceTest extends UiServiceTestCase { } - @Test public void customTime_darkThemeOn_beforeStartEnd() throws RemoteException { LocalTime now = LocalTime.now();