Merge "Throw IllegalArgumentException when calling setNightModeCustomType with an unsupported type"

This commit is contained in:
TreeHugger Robot
2022-02-15 14:00:26 +00:00
committed by Android (Google) Code Review
4 changed files with 50 additions and 6 deletions

View File

@@ -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);
/**

View File

@@ -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();

View File

@@ -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<Integer> 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) {

View File

@@ -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();