summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
author TreeHugger Robot <treehugger-gerrit@google.com> 2020-03-22 02:31:42 +0000
committer Android (Google) Code Review <android-gerrit@google.com> 2020-03-22 02:31:42 +0000
commitc78a7e4971c36f3163d07655835fd0fe3a3ee1e8 (patch)
tree18ffbd345b9c71da890827f5b03c55a2d3da5140
parente0ee0e4d4be6055775af383c48f5dc0b7ecbfbd7 (diff)
parent4430bb4049013c8c143275810cc9ac17ac54b78b (diff)
Merge "UiMode TwilightManager null" into rvc-dev
-rw-r--r--services/core/java/com/android/server/UiModeManagerService.java134
-rw-r--r--services/tests/uiservicestests/src/com/android/server/UiModeManagerServiceTest.java23
2 files changed, 77 insertions, 80 deletions
diff --git a/services/core/java/com/android/server/UiModeManagerService.java b/services/core/java/com/android/server/UiModeManagerService.java
index 12a1a9516329..564f9f3dc11a 100644
--- a/services/core/java/com/android/server/UiModeManagerService.java
+++ b/services/core/java/com/android/server/UiModeManagerService.java
@@ -159,22 +159,13 @@ final class UiModeManagerService extends SystemService {
public UiModeManagerService(Context context) {
super(context);
+ mConfiguration.setToDefaults();
}
@VisibleForTesting
- protected UiModeManagerService(Context context, WindowManagerInternal wm, AlarmManager am,
- PowerManager pm, PowerManager.WakeLock wl, TwilightManager tm,
- PowerManagerInternal localPowerManager,
- boolean setupWizardComplete) {
- super(context);
- mWindowManager = wm;
- mWakeLock = wl;
- mTwilightManager = tm;
+ protected UiModeManagerService(Context context, boolean setupWizardComplete) {
+ this(context);
mSetupWizardComplete = setupWizardComplete;
- mAlarmManager = am;
- mPowerManager = pm;
- mLocalPowerManager = localPowerManager;
- initPowerSave();
}
private static Intent buildHomeIntent(String category) {
@@ -249,8 +240,8 @@ final class UiModeManagerService extends SystemService {
};
/**
- * DO NOT USE DIRECTLY
- * see register registerScreenOffEvent and unregisterScreenOffEvent
+ * DO NOT USE DIRECTLY
+ * see register registerScreenOffEvent and unregisterScreenOffEvent
*/
private final BroadcastReceiver mOnScreenOffHandler = new BroadcastReceiver() {
@Override
@@ -333,34 +324,45 @@ final class UiModeManagerService extends SystemService {
}
@Override
+ public void onBootPhase(int phase) {
+ if (phase == SystemService.PHASE_SYSTEM_SERVICES_READY) {
+ synchronized (mLock) {
+ final Context context = getContext();
+ mSystemReady = true;
+ mPowerManager = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
+ mWakeLock = mPowerManager.newWakeLock(PowerManager.FULL_WAKE_LOCK, TAG);
+ mWindowManager = LocalServices.getService(WindowManagerInternal.class);
+ mAlarmManager = (AlarmManager) getContext().getSystemService(Context.ALARM_SERVICE);
+ mLocalPowerManager =
+ LocalServices.getService(PowerManagerInternal.class);
+ mTwilightManager = getLocalService(TwilightManager.class);
+ initPowerSave();
+ mCarModeEnabled = mDockState == Intent.EXTRA_DOCK_STATE_CAR;
+ registerVrStateListener();
+ // register listeners
+ context.getContentResolver()
+ .registerContentObserver(Secure.getUriFor(Secure.UI_NIGHT_MODE),
+ false, mDarkThemeObserver, 0);
+ context.registerReceiver(mDockModeReceiver,
+ new IntentFilter(Intent.ACTION_DOCK_EVENT));
+ IntentFilter batteryFilter = new IntentFilter(Intent.ACTION_BATTERY_CHANGED);
+ context.registerReceiver(mBatteryReceiver, batteryFilter);
+ IntentFilter filter = new IntentFilter();
+ filter.addAction(Intent.ACTION_USER_SWITCHED);
+ context.registerReceiver(new UserSwitchedReceiver(), filter, null, mHandler);
+ updateConfigurationLocked();
+ applyConfigurationExternallyLocked();
+ }
+ }
+ }
+
+ @Override
public void onStart() {
final Context context = getContext();
-
- mPowerManager = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
- mWakeLock = mPowerManager.newWakeLock(PowerManager.FULL_WAKE_LOCK, TAG);
- mWindowManager = LocalServices.getService(WindowManagerInternal.class);
- mAlarmManager = (AlarmManager) getContext().getSystemService(Context.ALARM_SERVICE);
-
// If setup isn't complete for this user listen for completion so we can unblock
// being able to send a night mode configuration change event
verifySetupWizardCompleted();
- context.registerReceiver(mDockModeReceiver,
- new IntentFilter(Intent.ACTION_DOCK_EVENT));
- IntentFilter batteryFilter = new IntentFilter(Intent.ACTION_BATTERY_CHANGED);
- context.registerReceiver(mBatteryReceiver, batteryFilter);
-
- context.registerReceiver(mSettingsRestored,
- new IntentFilter(Intent.ACTION_SETTING_RESTORED), null, mHandler);
-
- mLocalPowerManager =
- LocalServices.getService(PowerManagerInternal.class);
- initPowerSave();
-
- mTwilightManager = getLocalService(TwilightManager.class);
-
- mConfiguration.setToDefaults();
-
final Resources res = context.getResources();
mDefaultUiModeType = res.getInteger(
com.android.internal.R.integer.config_defaultUiModeType);
@@ -383,21 +385,12 @@ final class UiModeManagerService extends SystemService {
SystemServerInitThreadPool.submit(() -> {
synchronized (mLock) {
updateNightModeFromSettingsLocked(context, res, UserHandle.getCallingUserId());
- updateConfigurationLocked();
- applyConfigurationExternallyLocked();
+ updateSystemProperties();
}
}, TAG + ".onStart");
publishBinderService(Context.UI_MODE_SERVICE, mService);
publishLocalService(UiModeManagerInternal.class, mLocalService);
-
- IntentFilter filter = new IntentFilter();
- filter.addAction(Intent.ACTION_USER_SWITCHED);
- context.registerReceiver(new UserSwitchedReceiver(), filter, null, mHandler);
-
- context.getContentResolver().registerContentObserver(Secure.getUriFor(Secure.UI_NIGHT_MODE),
- false, mDarkThemeObserver, 0);
- mHandler.post(() -> updateSystemProperties());
}
private final BroadcastReceiver mSettingsRestored = new BroadcastReceiver() {
@@ -475,9 +468,10 @@ final class UiModeManagerService extends SystemService {
/**
* Updates the night mode setting in Settings.Global and returns if the value was successfully
* changed.
+ *
* @param context A valid context
- * @param res A valid resource object
- * @param userId The user to update the setting for
+ * @param res A valid resource object
+ * @param userId The user to update the setting for
* @return True if the new value is different from the old value. False otherwise.
*/
private boolean updateNightModeFromSettingsLocked(Context context, Resources res, int userId) {
@@ -493,12 +487,12 @@ final class UiModeManagerService extends SystemService {
Secure.UI_NIGHT_MODE_OVERRIDE_OFF, 0, userId) != 0;
mCustomAutoNightModeStartMilliseconds = LocalTime.ofNanoOfDay(
Secure.getLongForUser(context.getContentResolver(),
- Secure.DARK_THEME_CUSTOM_START_TIME,
- DEFAULT_CUSTOM_NIGHT_START_TIME.toNanoOfDay() / 1000L, userId) * 1000);
+ Secure.DARK_THEME_CUSTOM_START_TIME,
+ DEFAULT_CUSTOM_NIGHT_START_TIME.toNanoOfDay() / 1000L, userId) * 1000);
mCustomAutoNightModeEndMilliseconds = LocalTime.ofNanoOfDay(
Secure.getLongForUser(context.getContentResolver(),
- Secure.DARK_THEME_CUSTOM_END_TIME,
- DEFAULT_CUSTOM_NIGHT_END_TIME.toNanoOfDay() / 1000L, userId) * 1000);
+ Secure.DARK_THEME_CUSTOM_END_TIME,
+ DEFAULT_CUSTOM_NIGHT_END_TIME.toNanoOfDay() / 1000L, userId) * 1000);
} else {
mNightMode = defaultNightMode;
mCustomAutoNightModeEndMilliseconds = DEFAULT_CUSTOM_NIGHT_END_TIME;
@@ -863,18 +857,6 @@ final class UiModeManagerService extends SystemService {
}
}
- @Override
- public void onBootPhase(int phase) {
- if (phase == SystemService.PHASE_SYSTEM_SERVICES_READY) {
- synchronized (mLock) {
- mSystemReady = true;
- mCarModeEnabled = mDockState == Intent.EXTRA_DOCK_STATE_CAR;
- registerVrStateListener();
- updateLocked(0, 0);
- }
- }
- }
-
/**
* Updates the global car mode state.
* The device is considered to be in car mode if there exists an app at any priority level which
@@ -931,7 +913,8 @@ final class UiModeManagerService extends SystemService {
// Anyone can disable the default priority.
isDefaultPriority
// If priority was enabled, only enabling package can disable it.
- || isPriorityTracked && mCarModePackagePriority.get(priority).equals(packageName)
+ || isPriorityTracked && mCarModePackagePriority.get(priority).equals(
+ packageName)
// Disable all priorities flag can disable all regardless.
|| isDisableAll;
if (isChangeAllowed) {
@@ -1092,10 +1075,10 @@ final class UiModeManagerService extends SystemService {
if (LOG) {
Slog.d(TAG,
- "updateConfigurationLocked: mDockState=" + mDockState
- + "; mCarMode=" + mCarModeEnabled
- + "; mNightMode=" + mNightMode
- + "; uiMode=" + uiMode);
+ "updateConfigurationLocked: mDockState=" + mDockState
+ + "; mCarMode=" + mCarModeEnabled
+ + "; mNightMode=" + mNightMode
+ + "; uiMode=" + uiMode);
}
mCurUiMode = uiMode;
@@ -1291,9 +1274,9 @@ final class UiModeManagerService extends SystemService {
if (LOG) {
Slog.v(TAG, String.format(
- "Handling broadcast result for action %s: enable=0x%08x, disable=0x%08x, "
- + "category=%s",
- action, enableFlags, disableFlags, category));
+ "Handling broadcast result for action %s: enable=0x%08x, disable=0x%08x, "
+ + "category=%s",
+ action, enableFlags, disableFlags, category));
}
sendConfigurationAndStartDreamOrDockAppLocked(category);
@@ -1356,8 +1339,8 @@ final class UiModeManagerService extends SystemService {
// have no effect until the device is unlocked.
if (mStatusBarManager != null) {
mStatusBarManager.disable(mCarModeEnabled
- ? StatusBarManager.DISABLE_NOTIFICATION_TICKER
- : StatusBarManager.DISABLE_NONE);
+ ? StatusBarManager.DISABLE_NOTIFICATION_TICKER
+ : StatusBarManager.DISABLE_NONE);
}
if (mNotificationManager == null) {
@@ -1382,7 +1365,8 @@ final class UiModeManagerService extends SystemService {
.setContentText(
context.getString(R.string.car_mode_disable_notification_message))
.setContentIntent(
- PendingIntent.getActivityAsUser(context, 0, carModeOffIntent, 0,
+ PendingIntent.getActivityAsUser(context, 0,
+ carModeOffIntent, 0,
null, UserHandle.CURRENT));
mNotificationManager.notifyAsUser(null,
SystemMessage.NOTE_CAR_MODE_DISABLE, n.build(), UserHandle.ALL);
diff --git a/services/tests/uiservicestests/src/com/android/server/UiModeManagerServiceTest.java b/services/tests/uiservicestests/src/com/android/server/UiModeManagerServiceTest.java
index 93ca34ad1a45..69ef499749a9 100644
--- a/services/tests/uiservicestests/src/com/android/server/UiModeManagerServiceTest.java
+++ b/services/tests/uiservicestests/src/com/android/server/UiModeManagerServiceTest.java
@@ -31,6 +31,7 @@ import android.os.PowerManager;
import android.os.PowerManagerInternal;
import android.os.PowerSaveState;
import android.os.RemoteException;
+import android.provider.Settings;
import android.testing.AndroidTestingRunner;
import android.testing.TestableLooper;
import com.android.server.twilight.TwilightManager;
@@ -54,6 +55,7 @@ import static junit.framework.TestCase.assertFalse;
import static junit.framework.TestCase.assertTrue;
import static org.junit.Assert.assertEquals;
import static org.mockito.ArgumentMatchers.any;
+import static org.mockito.ArgumentMatchers.anyBoolean;
import static org.mockito.ArgumentMatchers.anyInt;
import static org.mockito.ArgumentMatchers.anyLong;
import static org.mockito.ArgumentMatchers.anyString;
@@ -113,6 +115,7 @@ public class UiModeManagerServiceTest extends UiServiceTestCase {
when(mContext.getResources()).thenReturn(mResources);
when(mContext.getContentResolver()).thenReturn(mContentResolver);
when(mPowerManager.isInteractive()).thenReturn(true);
+ when(mPowerManager.newWakeLock(anyInt(), anyString())).thenReturn(mWakeLock);
when(mTwilightManager.getLastTwilightState()).thenReturn(mTwilightState);
when(mTwilightState.isNight()).thenReturn(true);
when(mContext.registerReceiver(notNull(), notNull())).then(inv -> {
@@ -135,23 +138,33 @@ public class UiModeManagerServiceTest extends UiServiceTestCase {
mCustomListener = () -> {};
return null;
}).when(mAlarmManager).cancel(eq(mCustomListener));
-
- mUiManagerService = new UiModeManagerService(mContext,
- mWindowManager, mAlarmManager, mPowerManager,
- mWakeLock, mTwilightManager, mLocalPowerManager, true);
+ when(mContext.getSystemService(eq(Context.POWER_SERVICE)))
+ .thenReturn(mPowerManager);
+ when(mContext.getSystemService(eq(Context.ALARM_SERVICE)))
+ .thenReturn(mAlarmManager);
+ addLocalService(WindowManagerInternal.class, mWindowManager);
+ addLocalService(PowerManagerInternal.class, mLocalPowerManager);
+ addLocalService(TwilightManager.class, mTwilightManager);
+
+ mUiManagerService = new UiModeManagerService(mContext, true);
try {
mUiManagerService.onBootPhase(SystemService.PHASE_SYSTEM_SERVICES_READY);
} catch (SecurityException e) {/* ignore for permission denial */}
mService = mUiManagerService.getService();
}
+ private <T> void addLocalService(Class<T> clazz, T service) {
+ LocalServices.removeServiceForTest(clazz);
+ LocalServices.addService(clazz, service);
+ }
+
@Test
public void setAutoMode_screenOffRegistered() throws RemoteException {
try {
mService.setNightMode(MODE_NIGHT_NO);
} catch (SecurityException e) { /* we should ignore this update config exception*/ }
mService.setNightMode(MODE_NIGHT_AUTO);
- verify(mContext).registerReceiver(any(BroadcastReceiver.class), any());
+ verify(mContext, atLeastOnce()).registerReceiver(any(BroadcastReceiver.class), any());
}
@Test