Loading services/core/java/com/android/server/UiModeManagerService.java +81 −47 Original line number Diff line number Diff line Loading @@ -16,6 +16,7 @@ package com.android.server; import android.annotation.IntRange; import android.annotation.Nullable; import android.app.Activity; import android.app.ActivityManager; Loading @@ -41,6 +42,7 @@ import android.os.Handler; import android.os.PowerManager; import android.os.PowerManager.ServiceType; import android.os.PowerManagerInternal; import android.os.Process; import android.os.RemoteException; import android.os.ResultReceiver; import android.os.ServiceManager; Loading @@ -67,6 +69,10 @@ import com.android.server.wm.WindowManagerInternal; import java.io.FileDescriptor; import java.io.PrintWriter; import java.util.Map; import static android.app.UiModeManager.MODE_NIGHT_AUTO; import static android.app.UiModeManager.MODE_NIGHT_YES; final class UiModeManagerService extends SystemService { private static final String TAG = UiModeManager.class.getSimpleName(); Loading Loading @@ -124,6 +130,7 @@ final class UiModeManagerService extends SystemService { private NotificationManager mNotificationManager; private StatusBarManager mStatusBarManager; private WindowManagerInternal mWindowManager; private PowerManager mPowerManager; private PowerManager.WakeLock mWakeLock; Loading @@ -136,11 +143,12 @@ final class UiModeManagerService extends SystemService { @VisibleForTesting protected UiModeManagerService(Context context, WindowManagerInternal wm, PowerManager.WakeLock wl, TwilightManager tm, boolean setupWizardComplete) { PowerManager pm, boolean setupWizardComplete) { super(context); mWindowManager = wm; mWakeLock = wl; mTwilightManager = tm; mPowerManager = pm; mSetupWizardComplete = setupWizardComplete; } Loading Loading @@ -261,13 +269,18 @@ final class UiModeManagerService extends SystemService { private final ContentObserver mDarkThemeObserver = new ContentObserver(mHandler) { @Override public void onChange(boolean selfChange, Uri uri) { updateSystemProperties(); } }; private void updateSystemProperties() { int mode = Secure.getIntForUser(getContext().getContentResolver(), Secure.UI_NIGHT_MODE, mNightMode, 0); mode = mode == UiModeManager.MODE_NIGHT_AUTO ? UiModeManager.MODE_NIGHT_YES : mode; if (mode == MODE_NIGHT_AUTO) { mode = MODE_NIGHT_YES; } SystemProperties.set(SYSTEM_PROPERTY_DEVICE_THEME, Integer.toString(mode)); } }; @Override public void onSwitchUser(int userHandle) { Loading @@ -280,9 +293,9 @@ final class UiModeManagerService extends SystemService { public void onStart() { final Context context = getContext(); final PowerManager powerManager = mPowerManager = (PowerManager) context.getSystemService(Context.POWER_SERVICE); mWakeLock = powerManager.newWakeLock(PowerManager.FULL_WAKE_LOCK, TAG); mWakeLock = mPowerManager.newWakeLock(PowerManager.FULL_WAKE_LOCK, TAG); mWindowManager = LocalServices.getService(WindowManagerInternal.class); // If setup isn't complete for this user listen for completion so we can unblock Loading Loading @@ -349,6 +362,7 @@ final class UiModeManagerService extends SystemService { context.getContentResolver().registerContentObserver(Secure.getUriFor(Secure.UI_NIGHT_MODE), false, mDarkThemeObserver, 0); mHandler.post(() -> updateSystemProperties()); } @VisibleForTesting Loading Loading @@ -406,6 +420,7 @@ final class UiModeManagerService extends SystemService { } private void registerScreenOffEvent() { if (mPowerSave) return; mWaitForScreenOff = true; final IntentFilter intentFilter = new IntentFilter(Intent.ACTION_SCREEN_OFF); Loading Loading @@ -510,7 +525,9 @@ final class UiModeManagerService extends SystemService { persistNightMode(user); } // on screen off will update configuration instead if (mNightMode != UiModeManager.MODE_NIGHT_AUTO || mCar) { if ((mNightMode != MODE_NIGHT_AUTO) || shouldApplyAutomaticChangesImmediately()) { unregisterScreenOffEvent(); updateLocked(0, 0); } else { registerScreenOffEvent(); Loading Loading @@ -600,6 +617,7 @@ final class UiModeManagerService extends SystemService { pw.print(" mSetUiMode=0x"); pw.println(Integer.toHexString(mSetUiMode)); pw.print(" mHoldingConfiguration="); pw.print(mHoldingConfiguration); pw.print(" mSystemReady="); pw.println(mSystemReady); if (mTwilightManager != null) { // We may not have a TwilightManager. pw.print(" mTwilightService.getLastTwilightState()="); Loading @@ -615,7 +633,6 @@ final class UiModeManagerService extends SystemService { mTwilightManager = getLocalService(TwilightManager.class); mSystemReady = true; mCarModeEnabled = mDockState == Intent.EXTRA_DOCK_STATE_CAR; updateComputedNightModeLocked(); registerVrStateListener(); updateLocked(0, 0); } Loading Loading @@ -683,24 +700,31 @@ final class UiModeManagerService extends SystemService { uiMode = Configuration.UI_MODE_TYPE_VR_HEADSET; } if (mNightMode == UiModeManager.MODE_NIGHT_AUTO) { if (mNightMode == MODE_NIGHT_YES || mNightMode == UiModeManager.MODE_NIGHT_NO) { mComputedNightMode = mNightMode == MODE_NIGHT_YES; } if (mNightMode == MODE_NIGHT_AUTO) { boolean activateNightMode = mComputedNightMode; if (mTwilightManager != null) { mTwilightManager.registerListener(mTwilightListener, mHandler); final TwilightState lastState = mTwilightManager.getLastTwilightState(); activateNightMode = lastState == null ? mComputedNightMode : lastState.isNight(); } updateComputedNightModeLocked(); uiMode |= mComputedNightMode ? Configuration.UI_MODE_NIGHT_YES : Configuration.UI_MODE_NIGHT_NO; updateComputedNightModeLocked(activateNightMode); } else { if (mTwilightManager != null) { mTwilightManager.unregisterListener(mTwilightListener); } uiMode |= mNightMode << 4; } // Override night mode in power save mode if not in car mode if (mPowerSave && !mCarModeEnabled) { uiMode &= ~Configuration.UI_MODE_NIGHT_NO; uiMode |= Configuration.UI_MODE_NIGHT_YES; } else { uiMode = getComputedUiModeConfiguration(uiMode); } if (LOG) { Loading @@ -712,11 +736,20 @@ final class UiModeManagerService extends SystemService { } mCurUiMode = uiMode; if (!mHoldingConfiguration || !mWaitForScreenOff) { if (!mHoldingConfiguration && (!mWaitForScreenOff || mPowerSave)) { mConfiguration.uiMode = uiMode; } } @UiModeManager.NightMode private int getComputedUiModeConfiguration(@UiModeManager.NightMode int uiMode) { uiMode |= mComputedNightMode ? Configuration.UI_MODE_NIGHT_YES : Configuration.UI_MODE_NIGHT_NO; uiMode &= mComputedNightMode ? ~Configuration.UI_MODE_NIGHT_NO : ~Configuration.UI_MODE_NIGHT_YES; return uiMode; } private void applyConfigurationExternallyLocked() { if (mSetUiMode != mConfiguration.uiMode) { mSetUiMode = mConfiguration.uiMode; Loading @@ -724,8 +757,14 @@ final class UiModeManagerService extends SystemService { ActivityTaskManager.getService().updateConfiguration(mConfiguration); } catch (RemoteException e) { Slog.w(TAG, "Failure communicating with activity manager", e); } catch (SecurityException e) { Slog.e(TAG, "Activity does not have the ", e); } } } private boolean shouldApplyAutomaticChangesImmediately() { return mCar || !mPowerManager.isInteractive(); } void updateLocked(int enableFlags, int disableFlags) { Loading Loading @@ -958,12 +997,8 @@ final class UiModeManagerService extends SystemService { } } private void updateComputedNightModeLocked() { if (mTwilightManager != null) { TwilightState state = mTwilightManager.getLastTwilightState(); if (state != null) { mComputedNightMode = state.isNight(); } private void updateComputedNightModeLocked(boolean activate) { mComputedNightMode = activate; if (mNightModeOverride == UiModeManager.MODE_NIGHT_YES && !mComputedNightMode) { mComputedNightMode = true; return; Loading @@ -978,7 +1013,6 @@ final class UiModeManagerService extends SystemService { Secure.putIntForUser(getContext().getContentResolver(), OVERRIDE_NIGHT_MODE, mNightModeOverride, user); } } private void registerVrStateListener() { IVrManager vrManager = IVrManager.Stub.asInterface(ServiceManager.getService( Loading services/tests/uiservicestests/src/com/android/server/UiModeManagerServiceTest.java +4 −1 Original line number Diff line number Diff line Loading @@ -24,6 +24,7 @@ import android.content.pm.PackageManager; import android.content.res.Configuration; import android.content.res.Resources; import android.os.PowerManager; import android.os.PowerManagerInternal; import android.os.RemoteException; import android.testing.AndroidTestingRunner; import android.testing.TestableLooper; Loading Loading @@ -66,12 +67,14 @@ public class UiModeManagerServiceTest extends UiServiceTestCase { TwilightManager mTwilightManager; @Mock PowerManager.WakeLock mWakeLock; @Mock PowerManager mPowerManager; private Set<BroadcastReceiver> mScreenOffRecievers; @Before public void setUp() { mUiManagerService = new UiModeManagerService(mContext, mWindowManager, mWakeLock, mTwilightManager, true); mTwilightManager, mPowerManager, true); mScreenOffRecievers = new HashSet<>(); mService = mUiManagerService.getService(); when(mContext.checkCallingOrSelfPermission(anyString())) Loading Loading
services/core/java/com/android/server/UiModeManagerService.java +81 −47 Original line number Diff line number Diff line Loading @@ -16,6 +16,7 @@ package com.android.server; import android.annotation.IntRange; import android.annotation.Nullable; import android.app.Activity; import android.app.ActivityManager; Loading @@ -41,6 +42,7 @@ import android.os.Handler; import android.os.PowerManager; import android.os.PowerManager.ServiceType; import android.os.PowerManagerInternal; import android.os.Process; import android.os.RemoteException; import android.os.ResultReceiver; import android.os.ServiceManager; Loading @@ -67,6 +69,10 @@ import com.android.server.wm.WindowManagerInternal; import java.io.FileDescriptor; import java.io.PrintWriter; import java.util.Map; import static android.app.UiModeManager.MODE_NIGHT_AUTO; import static android.app.UiModeManager.MODE_NIGHT_YES; final class UiModeManagerService extends SystemService { private static final String TAG = UiModeManager.class.getSimpleName(); Loading Loading @@ -124,6 +130,7 @@ final class UiModeManagerService extends SystemService { private NotificationManager mNotificationManager; private StatusBarManager mStatusBarManager; private WindowManagerInternal mWindowManager; private PowerManager mPowerManager; private PowerManager.WakeLock mWakeLock; Loading @@ -136,11 +143,12 @@ final class UiModeManagerService extends SystemService { @VisibleForTesting protected UiModeManagerService(Context context, WindowManagerInternal wm, PowerManager.WakeLock wl, TwilightManager tm, boolean setupWizardComplete) { PowerManager pm, boolean setupWizardComplete) { super(context); mWindowManager = wm; mWakeLock = wl; mTwilightManager = tm; mPowerManager = pm; mSetupWizardComplete = setupWizardComplete; } Loading Loading @@ -261,13 +269,18 @@ final class UiModeManagerService extends SystemService { private final ContentObserver mDarkThemeObserver = new ContentObserver(mHandler) { @Override public void onChange(boolean selfChange, Uri uri) { updateSystemProperties(); } }; private void updateSystemProperties() { int mode = Secure.getIntForUser(getContext().getContentResolver(), Secure.UI_NIGHT_MODE, mNightMode, 0); mode = mode == UiModeManager.MODE_NIGHT_AUTO ? UiModeManager.MODE_NIGHT_YES : mode; if (mode == MODE_NIGHT_AUTO) { mode = MODE_NIGHT_YES; } SystemProperties.set(SYSTEM_PROPERTY_DEVICE_THEME, Integer.toString(mode)); } }; @Override public void onSwitchUser(int userHandle) { Loading @@ -280,9 +293,9 @@ final class UiModeManagerService extends SystemService { public void onStart() { final Context context = getContext(); final PowerManager powerManager = mPowerManager = (PowerManager) context.getSystemService(Context.POWER_SERVICE); mWakeLock = powerManager.newWakeLock(PowerManager.FULL_WAKE_LOCK, TAG); mWakeLock = mPowerManager.newWakeLock(PowerManager.FULL_WAKE_LOCK, TAG); mWindowManager = LocalServices.getService(WindowManagerInternal.class); // If setup isn't complete for this user listen for completion so we can unblock Loading Loading @@ -349,6 +362,7 @@ final class UiModeManagerService extends SystemService { context.getContentResolver().registerContentObserver(Secure.getUriFor(Secure.UI_NIGHT_MODE), false, mDarkThemeObserver, 0); mHandler.post(() -> updateSystemProperties()); } @VisibleForTesting Loading Loading @@ -406,6 +420,7 @@ final class UiModeManagerService extends SystemService { } private void registerScreenOffEvent() { if (mPowerSave) return; mWaitForScreenOff = true; final IntentFilter intentFilter = new IntentFilter(Intent.ACTION_SCREEN_OFF); Loading Loading @@ -510,7 +525,9 @@ final class UiModeManagerService extends SystemService { persistNightMode(user); } // on screen off will update configuration instead if (mNightMode != UiModeManager.MODE_NIGHT_AUTO || mCar) { if ((mNightMode != MODE_NIGHT_AUTO) || shouldApplyAutomaticChangesImmediately()) { unregisterScreenOffEvent(); updateLocked(0, 0); } else { registerScreenOffEvent(); Loading Loading @@ -600,6 +617,7 @@ final class UiModeManagerService extends SystemService { pw.print(" mSetUiMode=0x"); pw.println(Integer.toHexString(mSetUiMode)); pw.print(" mHoldingConfiguration="); pw.print(mHoldingConfiguration); pw.print(" mSystemReady="); pw.println(mSystemReady); if (mTwilightManager != null) { // We may not have a TwilightManager. pw.print(" mTwilightService.getLastTwilightState()="); Loading @@ -615,7 +633,6 @@ final class UiModeManagerService extends SystemService { mTwilightManager = getLocalService(TwilightManager.class); mSystemReady = true; mCarModeEnabled = mDockState == Intent.EXTRA_DOCK_STATE_CAR; updateComputedNightModeLocked(); registerVrStateListener(); updateLocked(0, 0); } Loading Loading @@ -683,24 +700,31 @@ final class UiModeManagerService extends SystemService { uiMode = Configuration.UI_MODE_TYPE_VR_HEADSET; } if (mNightMode == UiModeManager.MODE_NIGHT_AUTO) { if (mNightMode == MODE_NIGHT_YES || mNightMode == UiModeManager.MODE_NIGHT_NO) { mComputedNightMode = mNightMode == MODE_NIGHT_YES; } if (mNightMode == MODE_NIGHT_AUTO) { boolean activateNightMode = mComputedNightMode; if (mTwilightManager != null) { mTwilightManager.registerListener(mTwilightListener, mHandler); final TwilightState lastState = mTwilightManager.getLastTwilightState(); activateNightMode = lastState == null ? mComputedNightMode : lastState.isNight(); } updateComputedNightModeLocked(); uiMode |= mComputedNightMode ? Configuration.UI_MODE_NIGHT_YES : Configuration.UI_MODE_NIGHT_NO; updateComputedNightModeLocked(activateNightMode); } else { if (mTwilightManager != null) { mTwilightManager.unregisterListener(mTwilightListener); } uiMode |= mNightMode << 4; } // Override night mode in power save mode if not in car mode if (mPowerSave && !mCarModeEnabled) { uiMode &= ~Configuration.UI_MODE_NIGHT_NO; uiMode |= Configuration.UI_MODE_NIGHT_YES; } else { uiMode = getComputedUiModeConfiguration(uiMode); } if (LOG) { Loading @@ -712,11 +736,20 @@ final class UiModeManagerService extends SystemService { } mCurUiMode = uiMode; if (!mHoldingConfiguration || !mWaitForScreenOff) { if (!mHoldingConfiguration && (!mWaitForScreenOff || mPowerSave)) { mConfiguration.uiMode = uiMode; } } @UiModeManager.NightMode private int getComputedUiModeConfiguration(@UiModeManager.NightMode int uiMode) { uiMode |= mComputedNightMode ? Configuration.UI_MODE_NIGHT_YES : Configuration.UI_MODE_NIGHT_NO; uiMode &= mComputedNightMode ? ~Configuration.UI_MODE_NIGHT_NO : ~Configuration.UI_MODE_NIGHT_YES; return uiMode; } private void applyConfigurationExternallyLocked() { if (mSetUiMode != mConfiguration.uiMode) { mSetUiMode = mConfiguration.uiMode; Loading @@ -724,8 +757,14 @@ final class UiModeManagerService extends SystemService { ActivityTaskManager.getService().updateConfiguration(mConfiguration); } catch (RemoteException e) { Slog.w(TAG, "Failure communicating with activity manager", e); } catch (SecurityException e) { Slog.e(TAG, "Activity does not have the ", e); } } } private boolean shouldApplyAutomaticChangesImmediately() { return mCar || !mPowerManager.isInteractive(); } void updateLocked(int enableFlags, int disableFlags) { Loading Loading @@ -958,12 +997,8 @@ final class UiModeManagerService extends SystemService { } } private void updateComputedNightModeLocked() { if (mTwilightManager != null) { TwilightState state = mTwilightManager.getLastTwilightState(); if (state != null) { mComputedNightMode = state.isNight(); } private void updateComputedNightModeLocked(boolean activate) { mComputedNightMode = activate; if (mNightModeOverride == UiModeManager.MODE_NIGHT_YES && !mComputedNightMode) { mComputedNightMode = true; return; Loading @@ -978,7 +1013,6 @@ final class UiModeManagerService extends SystemService { Secure.putIntForUser(getContext().getContentResolver(), OVERRIDE_NIGHT_MODE, mNightModeOverride, user); } } private void registerVrStateListener() { IVrManager vrManager = IVrManager.Stub.asInterface(ServiceManager.getService( Loading
services/tests/uiservicestests/src/com/android/server/UiModeManagerServiceTest.java +4 −1 Original line number Diff line number Diff line Loading @@ -24,6 +24,7 @@ import android.content.pm.PackageManager; import android.content.res.Configuration; import android.content.res.Resources; import android.os.PowerManager; import android.os.PowerManagerInternal; import android.os.RemoteException; import android.testing.AndroidTestingRunner; import android.testing.TestableLooper; Loading Loading @@ -66,12 +67,14 @@ public class UiModeManagerServiceTest extends UiServiceTestCase { TwilightManager mTwilightManager; @Mock PowerManager.WakeLock mWakeLock; @Mock PowerManager mPowerManager; private Set<BroadcastReceiver> mScreenOffRecievers; @Before public void setUp() { mUiManagerService = new UiModeManagerService(mContext, mWindowManager, mWakeLock, mTwilightManager, true); mTwilightManager, mPowerManager, true); mScreenOffRecievers = new HashSet<>(); mService = mUiManagerService.getService(); when(mContext.checkCallingOrSelfPermission(anyString())) Loading