Merge "UiMode TwilightManager null" into rvc-dev am: c78a7e4971

Change-Id: I4479a7503413f141f7caaeba0fd75ea1d1bef853
This commit is contained in:
TreeHugger Robot
2020-03-22 02:51:07 +00:00
committed by Automerger Merge Worker
2 changed files with 77 additions and 80 deletions

View File

@@ -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) {
@@ -333,33 +324,44 @@ final class UiModeManagerService extends SystemService {
} }
@Override @Override
public void onStart() { public void onBootPhase(int phase) {
if (phase == SystemService.PHASE_SYSTEM_SERVICES_READY) {
synchronized (mLock) {
final Context context = getContext(); final Context context = getContext();
mSystemReady = true;
mPowerManager = (PowerManager) context.getSystemService(Context.POWER_SERVICE); mPowerManager = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
mWakeLock = mPowerManager.newWakeLock(PowerManager.FULL_WAKE_LOCK, TAG); mWakeLock = mPowerManager.newWakeLock(PowerManager.FULL_WAKE_LOCK, TAG);
mWindowManager = LocalServices.getService(WindowManagerInternal.class); mWindowManager = LocalServices.getService(WindowManagerInternal.class);
mAlarmManager = (AlarmManager) getContext().getSystemService(Context.ALARM_SERVICE); mAlarmManager = (AlarmManager) getContext().getSystemService(Context.ALARM_SERVICE);
mLocalPowerManager =
// If setup isn't complete for this user listen for completion so we can unblock LocalServices.getService(PowerManagerInternal.class);
// being able to send a night mode configuration change event mTwilightManager = getLocalService(TwilightManager.class);
verifySetupWizardCompleted(); 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, context.registerReceiver(mDockModeReceiver,
new IntentFilter(Intent.ACTION_DOCK_EVENT)); new IntentFilter(Intent.ACTION_DOCK_EVENT));
IntentFilter batteryFilter = new IntentFilter(Intent.ACTION_BATTERY_CHANGED); IntentFilter batteryFilter = new IntentFilter(Intent.ACTION_BATTERY_CHANGED);
context.registerReceiver(mBatteryReceiver, batteryFilter); context.registerReceiver(mBatteryReceiver, batteryFilter);
IntentFilter filter = new IntentFilter();
filter.addAction(Intent.ACTION_USER_SWITCHED);
context.registerReceiver(new UserSwitchedReceiver(), filter, null, mHandler);
updateConfigurationLocked();
applyConfigurationExternallyLocked();
}
}
}
context.registerReceiver(mSettingsRestored, @Override
new IntentFilter(Intent.ACTION_SETTING_RESTORED), null, mHandler); public void onStart() {
final Context context = getContext();
mLocalPowerManager = // If setup isn't complete for this user listen for completion so we can unblock
LocalServices.getService(PowerManagerInternal.class); // being able to send a night mode configuration change event
initPowerSave(); verifySetupWizardCompleted();
mTwilightManager = getLocalService(TwilightManager.class);
mConfiguration.setToDefaults();
final Resources res = context.getResources(); final Resources res = context.getResources();
mDefaultUiModeType = res.getInteger( mDefaultUiModeType = res.getInteger(
@@ -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,6 +468,7 @@ 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
@@ -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) {
@@ -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);

View File

@@ -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)))
.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, mUiManagerService = new UiModeManagerService(mContext, true);
mWindowManager, mAlarmManager, mPowerManager,
mWakeLock, mTwilightManager, mLocalPowerManager, 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