Merge "Dark theme not working bug" into rvc-dev am: 55318a3474

Change-Id: If12a18b8cf04da153d4f7064ccefa44bf57b3b87
This commit is contained in:
Automerger Merge Worker
2020-03-04 16:15:26 +00:00
3 changed files with 71 additions and 36 deletions

View File

@@ -7836,12 +7836,20 @@ public final class Settings {
public static final String UI_NIGHT_MODE = "ui_night_mode"; public static final String UI_NIGHT_MODE = "ui_night_mode";
/** /**
* The current night mode that has been overrided by the system. Owned * The current night mode that has been overridden to turn on by the system. Owned
* and controlled by UiModeManagerService. Constants are as per * and controlled by UiModeManagerService. Constants are as per
* UiModeManager. * UiModeManager.
* @hide * @hide
*/ */
public static final String UI_NIGHT_MODE_OVERRIDE = "ui_night_mode_override"; public static final String UI_NIGHT_MODE_OVERRIDE_ON = "ui_night_mode_override_on";
/**
* The current night mode that has been overridden to turn off by the system. Owned
* and controlled by UiModeManagerService. Constants are as per
* UiModeManager.
* @hide
*/
public static final String UI_NIGHT_MODE_OVERRIDE_OFF = "ui_night_mode_override_off";
/** /**
* Whether screensavers are enabled. * Whether screensavers are enabled.

View File

@@ -97,17 +97,11 @@ final class UiModeManagerService extends SystemService {
private int mLastBroadcastState = Intent.EXTRA_DOCK_STATE_UNDOCKED; private int mLastBroadcastState = Intent.EXTRA_DOCK_STATE_UNDOCKED;
private int mNightMode = UiModeManager.MODE_NIGHT_NO; private int mNightMode = UiModeManager.MODE_NIGHT_NO;
// we use the override auto mode
// for example: force night mode off in the night time
// while in auto mode
private int mNightModeOverride = mNightMode;
private final LocalTime DEFAULT_CUSTOM_NIGHT_START_TIME = LocalTime.of(22, 0); private final LocalTime DEFAULT_CUSTOM_NIGHT_START_TIME = LocalTime.of(22, 0);
private final LocalTime DEFAULT_CUSTOM_NIGHT_END_TIME = LocalTime.of(6, 0); private final LocalTime DEFAULT_CUSTOM_NIGHT_END_TIME = LocalTime.of(6, 0);
private LocalTime mCustomAutoNightModeStartMilliseconds = DEFAULT_CUSTOM_NIGHT_START_TIME; private LocalTime mCustomAutoNightModeStartMilliseconds = DEFAULT_CUSTOM_NIGHT_START_TIME;
private LocalTime mCustomAutoNightModeEndMilliseconds = DEFAULT_CUSTOM_NIGHT_END_TIME; private LocalTime mCustomAutoNightModeEndMilliseconds = DEFAULT_CUSTOM_NIGHT_END_TIME;
protected static final String OVERRIDE_NIGHT_MODE = Secure.UI_NIGHT_MODE_OVERRIDE;
private Map<Integer, String> mCarModePackagePriority = new HashMap<>(); private Map<Integer, String> mCarModePackagePriority = new HashMap<>();
private boolean mCarModeEnabled = false; private boolean mCarModeEnabled = false;
private boolean mCharging = false; private boolean mCharging = false;
@@ -150,6 +144,12 @@ final class UiModeManagerService extends SystemService {
private AlarmManager mAlarmManager; private AlarmManager mAlarmManager;
private PowerManager mPowerManager; private PowerManager mPowerManager;
// In automatic scheduling, the user is able
// to override the computed night mode until the two match
// Example: Activate dark mode in the day time until sunrise the next day
private boolean mOverrideNightModeOn;
private boolean mOverrideNightModeOff;
private PowerManager.WakeLock mWakeLock; private PowerManager.WakeLock mWakeLock;
private final LocalService mLocalService = new LocalService(); private final LocalService mLocalService = new LocalService();
@@ -291,15 +291,18 @@ final class UiModeManagerService extends SystemService {
private final ContentObserver mSetupWizardObserver = new ContentObserver(mHandler) { private final ContentObserver mSetupWizardObserver = new ContentObserver(mHandler) {
@Override @Override
public void onChange(boolean selfChange, Uri uri) { public void onChange(boolean selfChange, Uri uri) {
// setup wizard is done now so we can unblock synchronized (mLock) {
if (setupWizardCompleteForCurrentUser()) { // setup wizard is done now so we can unblock
mSetupWizardComplete = true; if (setupWizardCompleteForCurrentUser()) {
getContext().getContentResolver().unregisterContentObserver(mSetupWizardObserver); mSetupWizardComplete = true;
// update night mode getContext().getContentResolver()
Context context = getContext(); .unregisterContentObserver(mSetupWizardObserver);
updateNightModeFromSettings(context, context.getResources(), // update night mode
UserHandle.getCallingUserId()); Context context = getContext();
updateLocked(0, 0); updateNightModeFromSettingsLocked(context, context.getResources(),
UserHandle.getCallingUserId());
updateLocked(0, 0);
}
} }
} }
}; };
@@ -371,11 +374,10 @@ final class UiModeManagerService extends SystemService {
mCar = pm.hasSystemFeature(PackageManager.FEATURE_AUTOMOTIVE); mCar = pm.hasSystemFeature(PackageManager.FEATURE_AUTOMOTIVE);
mWatch = pm.hasSystemFeature(PackageManager.FEATURE_WATCH); mWatch = pm.hasSystemFeature(PackageManager.FEATURE_WATCH);
updateNightModeFromSettings(context, res, UserHandle.getCallingUserId());
// Update the initial, static configurations. // Update the initial, static configurations.
SystemServerInitThreadPool.submit(() -> { SystemServerInitThreadPool.submit(() -> {
synchronized (mLock) { synchronized (mLock) {
updateNightModeFromSettingsLocked(context, res, UserHandle.getCallingUserId());
updateConfigurationLocked(); updateConfigurationLocked();
applyConfigurationExternallyLocked(); applyConfigurationExternallyLocked();
} }
@@ -457,15 +459,17 @@ final class UiModeManagerService extends SystemService {
* @param userId The user to update the setting for * @param userId The user to update the setting for
* @return True if the new value is different from the old value. False otherwise. * @return True if the new value is different from the old value. False otherwise.
*/ */
private boolean updateNightModeFromSettings(Context context, Resources res, int userId) { private boolean updateNightModeFromSettingsLocked(Context context, Resources res, int userId) {
final int defaultNightMode = res.getInteger( final int defaultNightMode = res.getInteger(
com.android.internal.R.integer.config_defaultNightMode); com.android.internal.R.integer.config_defaultNightMode);
int oldNightMode = mNightMode; int oldNightMode = mNightMode;
if (mSetupWizardComplete) { if (mSetupWizardComplete) {
mNightMode = Secure.getIntForUser(context.getContentResolver(), mNightMode = Secure.getIntForUser(context.getContentResolver(),
Secure.UI_NIGHT_MODE, defaultNightMode, userId); Secure.UI_NIGHT_MODE, defaultNightMode, userId);
mNightModeOverride = Secure.getIntForUser(context.getContentResolver(), mOverrideNightModeOn = Secure.getIntForUser(context.getContentResolver(),
OVERRIDE_NIGHT_MODE, defaultNightMode, userId); Secure.UI_NIGHT_MODE_OVERRIDE_ON, defaultNightMode, userId) != 0;
mOverrideNightModeOff = Secure.getIntForUser(context.getContentResolver(),
Secure.UI_NIGHT_MODE_OVERRIDE_OFF, defaultNightMode, userId) != 0;
mCustomAutoNightModeStartMilliseconds = LocalTime.ofNanoOfDay( mCustomAutoNightModeStartMilliseconds = LocalTime.ofNanoOfDay(
Secure.getLongForUser(context.getContentResolver(), Secure.getLongForUser(context.getContentResolver(),
Secure.DARK_THEME_CUSTOM_START_TIME, Secure.DARK_THEME_CUSTOM_START_TIME,
@@ -476,9 +480,10 @@ final class UiModeManagerService extends SystemService {
DEFAULT_CUSTOM_NIGHT_END_TIME.toNanoOfDay() / 1000L, userId) * 1000); DEFAULT_CUSTOM_NIGHT_END_TIME.toNanoOfDay() / 1000L, userId) * 1000);
} else { } else {
mNightMode = defaultNightMode; mNightMode = defaultNightMode;
mNightModeOverride = defaultNightMode;
mCustomAutoNightModeEndMilliseconds = DEFAULT_CUSTOM_NIGHT_END_TIME; mCustomAutoNightModeEndMilliseconds = DEFAULT_CUSTOM_NIGHT_END_TIME;
mCustomAutoNightModeStartMilliseconds = DEFAULT_CUSTOM_NIGHT_START_TIME; mCustomAutoNightModeStartMilliseconds = DEFAULT_CUSTOM_NIGHT_START_TIME;
mOverrideNightModeOn = false;
mOverrideNightModeOff = false;
} }
return oldNightMode != mNightMode; return oldNightMode != mNightMode;
@@ -647,7 +652,7 @@ final class UiModeManagerService extends SystemService {
} }
mNightMode = mode; mNightMode = mode;
mNightModeOverride = mode; resetNightModeOverrideLocked();
// Only persist setting if not in car mode // Only persist setting if not in car mode
if (!mCarModeEnabled) { if (!mCarModeEnabled) {
persistNightMode(user); persistNightMode(user);
@@ -708,8 +713,8 @@ final class UiModeManagerService extends SystemService {
try { try {
if (mNightMode == MODE_NIGHT_AUTO || mNightMode == MODE_NIGHT_CUSTOM) { if (mNightMode == MODE_NIGHT_AUTO || mNightMode == MODE_NIGHT_CUSTOM) {
unregisterScreenOffEventLocked(); unregisterScreenOffEventLocked();
mNightModeOverride = active mOverrideNightModeOff = !active;
? UiModeManager.MODE_NIGHT_YES : UiModeManager.MODE_NIGHT_NO; mOverrideNightModeOn = active;
} else if (mNightMode == UiModeManager.MODE_NIGHT_NO } else if (mNightMode == UiModeManager.MODE_NIGHT_NO
&& active) { && active) {
mNightMode = UiModeManager.MODE_NIGHT_YES; mNightMode = UiModeManager.MODE_NIGHT_YES;
@@ -800,9 +805,11 @@ final class UiModeManagerService extends SystemService {
pw.print(" mDockState="); pw.print(mDockState); pw.print(" mDockState="); pw.print(mDockState);
pw.print(" mLastBroadcastState="); pw.println(mLastBroadcastState); pw.print(" mLastBroadcastState="); pw.println(mLastBroadcastState);
pw.print(" mNightModeOverride="); pw.print(mNightModeOverride);
pw.print(" mNightMode="); pw.print(mNightMode); pw.print(" ("); pw.print(" mNightMode="); pw.print(mNightMode); pw.print(" (");
pw.print(Shell.nightModeToStr(mNightMode)); pw.print(") "); pw.print(Shell.nightModeToStr(mNightMode)); pw.print(") ");
pw.print(" mOverrideOn/Off="); pw.print(mOverrideNightModeOn);
pw.print("/"); pw.print(mOverrideNightModeOff);
pw.print(" mNightModeLocked="); pw.println(mNightModeLocked); pw.print(" mNightModeLocked="); pw.println(mNightModeLocked);
pw.print(" mCarModeEnabled="); pw.print(mCarModeEnabled); pw.print(" mCarModeEnabled="); pw.print(mCarModeEnabled);
@@ -874,7 +881,7 @@ final class UiModeManagerService extends SystemService {
// When exiting car mode, restore night mode from settings // When exiting car mode, restore night mode from settings
if (!isCarModeNowEnabled) { if (!isCarModeNowEnabled) {
Context context = getContext(); Context context = getContext();
updateNightModeFromSettings(context, updateNightModeFromSettingsLocked(context,
context.getResources(), context.getResources(),
UserHandle.getCallingUserId()); UserHandle.getCallingUserId());
} }
@@ -1000,7 +1007,9 @@ final class UiModeManagerService extends SystemService {
Secure.putIntForUser(getContext().getContentResolver(), Secure.putIntForUser(getContext().getContentResolver(),
Secure.UI_NIGHT_MODE, mNightMode, user); Secure.UI_NIGHT_MODE, mNightMode, user);
Secure.putIntForUser(getContext().getContentResolver(), Secure.putIntForUser(getContext().getContentResolver(),
OVERRIDE_NIGHT_MODE, mNightModeOverride, user); Secure.UI_NIGHT_MODE_OVERRIDE_ON, mOverrideNightModeOn ? 1 : 0, user);
Secure.putIntForUser(getContext().getContentResolver(),
Secure.UI_NIGHT_MODE_OVERRIDE_OFF, mOverrideNightModeOff ? 1 : 0, user);
Secure.putLongForUser(getContext().getContentResolver(), Secure.putLongForUser(getContext().getContentResolver(),
Secure.DARK_THEME_CUSTOM_START_TIME, Secure.DARK_THEME_CUSTOM_START_TIME,
mCustomAutoNightModeStartMilliseconds.toNanoOfDay() / 1000, user); mCustomAutoNightModeStartMilliseconds.toNanoOfDay() / 1000, user);
@@ -1130,6 +1139,7 @@ final class UiModeManagerService extends SystemService {
void updateLocked(int enableFlags, int disableFlags) { void updateLocked(int enableFlags, int disableFlags) {
String action = null; String action = null;
String oldAction = null; String oldAction = null;
boolean originalComputedNightMode = mComputedNightMode;
if (mLastBroadcastState == Intent.EXTRA_DOCK_STATE_CAR) { if (mLastBroadcastState == Intent.EXTRA_DOCK_STATE_CAR) {
adjustStatusBarCarModeLocked(); adjustStatusBarCarModeLocked();
oldAction = UiModeManager.ACTION_EXIT_CAR_MODE; oldAction = UiModeManager.ACTION_EXIT_CAR_MODE;
@@ -1210,6 +1220,11 @@ final class UiModeManagerService extends SystemService {
sendConfigurationAndStartDreamOrDockAppLocked(category); sendConfigurationAndStartDreamOrDockAppLocked(category);
} }
// reset overrides if mComputedNightMode changes
if (originalComputedNightMode != mComputedNightMode) {
resetNightModeOverrideLocked();
}
// keep screen on when charging and in car mode // keep screen on when charging and in car mode
boolean keepScreenOn = mCharging && boolean keepScreenOn = mCharging &&
((mCarModeEnabled && mCarModeKeepsScreenOn && ((mCarModeEnabled && mCarModeKeepsScreenOn &&
@@ -1360,19 +1375,22 @@ final class UiModeManagerService extends SystemService {
private void updateComputedNightModeLocked(boolean activate) { private void updateComputedNightModeLocked(boolean activate) {
mComputedNightMode = activate; mComputedNightMode = activate;
if (mNightModeOverride == UiModeManager.MODE_NIGHT_YES && !mComputedNightMode) { if (mOverrideNightModeOn && !mComputedNightMode) {
mComputedNightMode = true; mComputedNightMode = true;
return; return;
} }
if (mNightModeOverride == UiModeManager.MODE_NIGHT_NO && mComputedNightMode) { if (mOverrideNightModeOff && mComputedNightMode) {
mComputedNightMode = false; mComputedNightMode = false;
return; return;
} }
}
mNightModeOverride = mNightMode; private void resetNightModeOverrideLocked() {
final int user = UserHandle.getCallingUserId(); if (mOverrideNightModeOff || mOverrideNightModeOn) {
Secure.putIntForUser(getContext().getContentResolver(), mOverrideNightModeOff = false;
OVERRIDE_NIGHT_MODE, mNightModeOverride, user); mOverrideNightModeOn = false;
persistNightMode(UserHandle.getCallingUserId());
}
} }
private void registerVrStateListener() { private void registerVrStateListener() {
@@ -1515,7 +1533,7 @@ final class UiModeManagerService extends SystemService {
final int currentId = intent.getIntExtra( final int currentId = intent.getIntExtra(
Intent.EXTRA_USER_HANDLE, UserHandle.USER_SYSTEM); Intent.EXTRA_USER_HANDLE, UserHandle.USER_SYSTEM);
// only update if the value is actually changed // only update if the value is actually changed
if (updateNightModeFromSettings(context, context.getResources(), currentId)) { if (updateNightModeFromSettingsLocked(context, context.getResources(), currentId)) {
updateLocked(0, 0); updateLocked(0, 0);
} }
} }

View File

@@ -166,6 +166,15 @@ public class UiModeManagerServiceTest extends UiServiceTestCase {
verify(mContext, atLeastOnce()).unregisterReceiver(any(BroadcastReceiver.class)); verify(mContext, atLeastOnce()).unregisterReceiver(any(BroadcastReceiver.class));
} }
@Test
public void setNightModeActivated_fromNoToYesAndBAck() throws RemoteException {
mService.setNightMode(MODE_NIGHT_NO);
mService.setNightModeActivated(true);
assertTrue(isNightModeActivated());
mService.setNightModeActivated(false);
assertFalse(isNightModeActivated());
}
@Test @Test
public void autoNightModeSwitch_batterySaverOn() throws RemoteException { public void autoNightModeSwitch_batterySaverOn() throws RemoteException {
mService.setNightMode(MODE_NIGHT_NO); mService.setNightMode(MODE_NIGHT_NO);