Merge "UiMode TwilightManager null" into rvc-dev
This commit is contained in:
committed by
Android (Google) Code Review
commit
c78a7e4971
@@ -159,22 +159,13 @@ final class UiModeManagerService extends SystemService {
|
|||||||
|
|
||||||
public UiModeManagerService(Context context) {
|
public UiModeManagerService(Context context) {
|
||||||
super(context);
|
super(context);
|
||||||
|
mConfiguration.setToDefaults();
|
||||||
}
|
}
|
||||||
|
|
||||||
@VisibleForTesting
|
@VisibleForTesting
|
||||||
protected UiModeManagerService(Context context, WindowManagerInternal wm, AlarmManager am,
|
protected UiModeManagerService(Context context, boolean setupWizardComplete) {
|
||||||
PowerManager pm, PowerManager.WakeLock wl, TwilightManager tm,
|
this(context);
|
||||||
PowerManagerInternal localPowerManager,
|
|
||||||
boolean setupWizardComplete) {
|
|
||||||
super(context);
|
|
||||||
mWindowManager = wm;
|
|
||||||
mWakeLock = wl;
|
|
||||||
mTwilightManager = tm;
|
|
||||||
mSetupWizardComplete = setupWizardComplete;
|
mSetupWizardComplete = setupWizardComplete;
|
||||||
mAlarmManager = am;
|
|
||||||
mPowerManager = pm;
|
|
||||||
mLocalPowerManager = localPowerManager;
|
|
||||||
initPowerSave();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private static Intent buildHomeIntent(String category) {
|
private static Intent buildHomeIntent(String category) {
|
||||||
@@ -249,8 +240,8 @@ final class UiModeManagerService extends SystemService {
|
|||||||
};
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* DO NOT USE DIRECTLY
|
* DO NOT USE DIRECTLY
|
||||||
* see register registerScreenOffEvent and unregisterScreenOffEvent
|
* see register registerScreenOffEvent and unregisterScreenOffEvent
|
||||||
*/
|
*/
|
||||||
private final BroadcastReceiver mOnScreenOffHandler = new BroadcastReceiver() {
|
private final BroadcastReceiver mOnScreenOffHandler = new BroadcastReceiver() {
|
||||||
@Override
|
@Override
|
||||||
@@ -332,35 +323,46 @@ final class UiModeManagerService extends SystemService {
|
|||||||
verifySetupWizardCompleted();
|
verifySetupWizardCompleted();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@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
|
@Override
|
||||||
public void onStart() {
|
public void onStart() {
|
||||||
final Context context = getContext();
|
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
|
// 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
|
// being able to send a night mode configuration change event
|
||||||
verifySetupWizardCompleted();
|
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();
|
final Resources res = context.getResources();
|
||||||
mDefaultUiModeType = res.getInteger(
|
mDefaultUiModeType = res.getInteger(
|
||||||
com.android.internal.R.integer.config_defaultUiModeType);
|
com.android.internal.R.integer.config_defaultUiModeType);
|
||||||
@@ -383,21 +385,12 @@ final class UiModeManagerService extends SystemService {
|
|||||||
SystemServerInitThreadPool.submit(() -> {
|
SystemServerInitThreadPool.submit(() -> {
|
||||||
synchronized (mLock) {
|
synchronized (mLock) {
|
||||||
updateNightModeFromSettingsLocked(context, res, UserHandle.getCallingUserId());
|
updateNightModeFromSettingsLocked(context, res, UserHandle.getCallingUserId());
|
||||||
updateConfigurationLocked();
|
updateSystemProperties();
|
||||||
applyConfigurationExternallyLocked();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}, TAG + ".onStart");
|
}, TAG + ".onStart");
|
||||||
publishBinderService(Context.UI_MODE_SERVICE, mService);
|
publishBinderService(Context.UI_MODE_SERVICE, mService);
|
||||||
publishLocalService(UiModeManagerInternal.class, mLocalService);
|
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() {
|
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
|
* Updates the night mode setting in Settings.Global and returns if the value was successfully
|
||||||
* changed.
|
* changed.
|
||||||
|
*
|
||||||
* @param context A valid context
|
* @param context A valid context
|
||||||
* @param res A valid resource object
|
* @param res A valid resource object
|
||||||
* @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 updateNightModeFromSettingsLocked(Context context, Resources res, int userId) {
|
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;
|
Secure.UI_NIGHT_MODE_OVERRIDE_OFF, 0, 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,
|
||||||
DEFAULT_CUSTOM_NIGHT_START_TIME.toNanoOfDay() / 1000L, userId) * 1000);
|
DEFAULT_CUSTOM_NIGHT_START_TIME.toNanoOfDay() / 1000L, userId) * 1000);
|
||||||
mCustomAutoNightModeEndMilliseconds = LocalTime.ofNanoOfDay(
|
mCustomAutoNightModeEndMilliseconds = LocalTime.ofNanoOfDay(
|
||||||
Secure.getLongForUser(context.getContentResolver(),
|
Secure.getLongForUser(context.getContentResolver(),
|
||||||
Secure.DARK_THEME_CUSTOM_END_TIME,
|
Secure.DARK_THEME_CUSTOM_END_TIME,
|
||||||
DEFAULT_CUSTOM_NIGHT_END_TIME.toNanoOfDay() / 1000L, userId) * 1000);
|
DEFAULT_CUSTOM_NIGHT_END_TIME.toNanoOfDay() / 1000L, userId) * 1000);
|
||||||
} else {
|
} else {
|
||||||
mNightMode = defaultNightMode;
|
mNightMode = defaultNightMode;
|
||||||
mCustomAutoNightModeEndMilliseconds = DEFAULT_CUSTOM_NIGHT_END_TIME;
|
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.
|
* 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
|
* 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.
|
// Anyone can disable the default priority.
|
||||||
isDefaultPriority
|
isDefaultPriority
|
||||||
// If priority was enabled, only enabling package can disable it.
|
// 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.
|
// Disable all priorities flag can disable all regardless.
|
||||||
|| isDisableAll;
|
|| isDisableAll;
|
||||||
if (isChangeAllowed) {
|
if (isChangeAllowed) {
|
||||||
@@ -1092,10 +1075,10 @@ final class UiModeManagerService extends SystemService {
|
|||||||
|
|
||||||
if (LOG) {
|
if (LOG) {
|
||||||
Slog.d(TAG,
|
Slog.d(TAG,
|
||||||
"updateConfigurationLocked: mDockState=" + mDockState
|
"updateConfigurationLocked: mDockState=" + mDockState
|
||||||
+ "; mCarMode=" + mCarModeEnabled
|
+ "; mCarMode=" + mCarModeEnabled
|
||||||
+ "; mNightMode=" + mNightMode
|
+ "; mNightMode=" + mNightMode
|
||||||
+ "; uiMode=" + uiMode);
|
+ "; uiMode=" + uiMode);
|
||||||
}
|
}
|
||||||
|
|
||||||
mCurUiMode = uiMode;
|
mCurUiMode = uiMode;
|
||||||
@@ -1291,9 +1274,9 @@ final class UiModeManagerService extends SystemService {
|
|||||||
|
|
||||||
if (LOG) {
|
if (LOG) {
|
||||||
Slog.v(TAG, String.format(
|
Slog.v(TAG, String.format(
|
||||||
"Handling broadcast result for action %s: enable=0x%08x, disable=0x%08x, "
|
"Handling broadcast result for action %s: enable=0x%08x, disable=0x%08x, "
|
||||||
+ "category=%s",
|
+ "category=%s",
|
||||||
action, enableFlags, disableFlags, category));
|
action, enableFlags, disableFlags, category));
|
||||||
}
|
}
|
||||||
|
|
||||||
sendConfigurationAndStartDreamOrDockAppLocked(category);
|
sendConfigurationAndStartDreamOrDockAppLocked(category);
|
||||||
@@ -1356,8 +1339,8 @@ final class UiModeManagerService extends SystemService {
|
|||||||
// have no effect until the device is unlocked.
|
// have no effect until the device is unlocked.
|
||||||
if (mStatusBarManager != null) {
|
if (mStatusBarManager != null) {
|
||||||
mStatusBarManager.disable(mCarModeEnabled
|
mStatusBarManager.disable(mCarModeEnabled
|
||||||
? StatusBarManager.DISABLE_NOTIFICATION_TICKER
|
? StatusBarManager.DISABLE_NOTIFICATION_TICKER
|
||||||
: StatusBarManager.DISABLE_NONE);
|
: StatusBarManager.DISABLE_NONE);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (mNotificationManager == null) {
|
if (mNotificationManager == null) {
|
||||||
@@ -1382,7 +1365,8 @@ final class UiModeManagerService extends SystemService {
|
|||||||
.setContentText(
|
.setContentText(
|
||||||
context.getString(R.string.car_mode_disable_notification_message))
|
context.getString(R.string.car_mode_disable_notification_message))
|
||||||
.setContentIntent(
|
.setContentIntent(
|
||||||
PendingIntent.getActivityAsUser(context, 0, carModeOffIntent, 0,
|
PendingIntent.getActivityAsUser(context, 0,
|
||||||
|
carModeOffIntent, 0,
|
||||||
null, UserHandle.CURRENT));
|
null, UserHandle.CURRENT));
|
||||||
mNotificationManager.notifyAsUser(null,
|
mNotificationManager.notifyAsUser(null,
|
||||||
SystemMessage.NOTE_CAR_MODE_DISABLE, n.build(), UserHandle.ALL);
|
SystemMessage.NOTE_CAR_MODE_DISABLE, n.build(), UserHandle.ALL);
|
||||||
|
|||||||
@@ -31,6 +31,7 @@ import android.os.PowerManager;
|
|||||||
import android.os.PowerManagerInternal;
|
import android.os.PowerManagerInternal;
|
||||||
import android.os.PowerSaveState;
|
import android.os.PowerSaveState;
|
||||||
import android.os.RemoteException;
|
import android.os.RemoteException;
|
||||||
|
import android.provider.Settings;
|
||||||
import android.testing.AndroidTestingRunner;
|
import android.testing.AndroidTestingRunner;
|
||||||
import android.testing.TestableLooper;
|
import android.testing.TestableLooper;
|
||||||
import com.android.server.twilight.TwilightManager;
|
import com.android.server.twilight.TwilightManager;
|
||||||
@@ -54,6 +55,7 @@ import static junit.framework.TestCase.assertFalse;
|
|||||||
import static junit.framework.TestCase.assertTrue;
|
import static junit.framework.TestCase.assertTrue;
|
||||||
import static org.junit.Assert.assertEquals;
|
import static org.junit.Assert.assertEquals;
|
||||||
import static org.mockito.ArgumentMatchers.any;
|
import static org.mockito.ArgumentMatchers.any;
|
||||||
|
import static org.mockito.ArgumentMatchers.anyBoolean;
|
||||||
import static org.mockito.ArgumentMatchers.anyInt;
|
import static org.mockito.ArgumentMatchers.anyInt;
|
||||||
import static org.mockito.ArgumentMatchers.anyLong;
|
import static org.mockito.ArgumentMatchers.anyLong;
|
||||||
import static org.mockito.ArgumentMatchers.anyString;
|
import static org.mockito.ArgumentMatchers.anyString;
|
||||||
@@ -113,6 +115,7 @@ public class UiModeManagerServiceTest extends UiServiceTestCase {
|
|||||||
when(mContext.getResources()).thenReturn(mResources);
|
when(mContext.getResources()).thenReturn(mResources);
|
||||||
when(mContext.getContentResolver()).thenReturn(mContentResolver);
|
when(mContext.getContentResolver()).thenReturn(mContentResolver);
|
||||||
when(mPowerManager.isInteractive()).thenReturn(true);
|
when(mPowerManager.isInteractive()).thenReturn(true);
|
||||||
|
when(mPowerManager.newWakeLock(anyInt(), anyString())).thenReturn(mWakeLock);
|
||||||
when(mTwilightManager.getLastTwilightState()).thenReturn(mTwilightState);
|
when(mTwilightManager.getLastTwilightState()).thenReturn(mTwilightState);
|
||||||
when(mTwilightState.isNight()).thenReturn(true);
|
when(mTwilightState.isNight()).thenReturn(true);
|
||||||
when(mContext.registerReceiver(notNull(), notNull())).then(inv -> {
|
when(mContext.registerReceiver(notNull(), notNull())).then(inv -> {
|
||||||
@@ -135,23 +138,33 @@ public class UiModeManagerServiceTest extends UiServiceTestCase {
|
|||||||
mCustomListener = () -> {};
|
mCustomListener = () -> {};
|
||||||
return null;
|
return null;
|
||||||
}).when(mAlarmManager).cancel(eq(mCustomListener));
|
}).when(mAlarmManager).cancel(eq(mCustomListener));
|
||||||
|
when(mContext.getSystemService(eq(Context.POWER_SERVICE)))
|
||||||
mUiManagerService = new UiModeManagerService(mContext,
|
.thenReturn(mPowerManager);
|
||||||
mWindowManager, mAlarmManager, mPowerManager,
|
when(mContext.getSystemService(eq(Context.ALARM_SERVICE)))
|
||||||
mWakeLock, mTwilightManager, mLocalPowerManager, true);
|
.thenReturn(mAlarmManager);
|
||||||
|
addLocalService(WindowManagerInternal.class, mWindowManager);
|
||||||
|
addLocalService(PowerManagerInternal.class, mLocalPowerManager);
|
||||||
|
addLocalService(TwilightManager.class, mTwilightManager);
|
||||||
|
|
||||||
|
mUiManagerService = new UiModeManagerService(mContext, true);
|
||||||
try {
|
try {
|
||||||
mUiManagerService.onBootPhase(SystemService.PHASE_SYSTEM_SERVICES_READY);
|
mUiManagerService.onBootPhase(SystemService.PHASE_SYSTEM_SERVICES_READY);
|
||||||
} catch (SecurityException e) {/* ignore for permission denial */}
|
} catch (SecurityException e) {/* ignore for permission denial */}
|
||||||
mService = mUiManagerService.getService();
|
mService = mUiManagerService.getService();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private <T> void addLocalService(Class<T> clazz, T service) {
|
||||||
|
LocalServices.removeServiceForTest(clazz);
|
||||||
|
LocalServices.addService(clazz, service);
|
||||||
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void setAutoMode_screenOffRegistered() throws RemoteException {
|
public void setAutoMode_screenOffRegistered() throws RemoteException {
|
||||||
try {
|
try {
|
||||||
mService.setNightMode(MODE_NIGHT_NO);
|
mService.setNightMode(MODE_NIGHT_NO);
|
||||||
} catch (SecurityException e) { /* we should ignore this update config exception*/ }
|
} catch (SecurityException e) { /* we should ignore this update config exception*/ }
|
||||||
mService.setNightMode(MODE_NIGHT_AUTO);
|
mService.setNightMode(MODE_NIGHT_AUTO);
|
||||||
verify(mContext).registerReceiver(any(BroadcastReceiver.class), any());
|
verify(mContext, atLeastOnce()).registerReceiver(any(BroadcastReceiver.class), any());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
|||||||
Reference in New Issue
Block a user