Merge "Back porting Dark theme bug fixes from R" into qt-qpr1-dev

This commit is contained in:
Jay Aliomer
2020-03-02 23:18:48 +00:00
committed by Android (Google) Code Review
2 changed files with 85 additions and 48 deletions

View File

@@ -16,6 +16,7 @@
package com.android.server; package com.android.server;
import android.annotation.IntRange;
import android.annotation.Nullable; import android.annotation.Nullable;
import android.app.Activity; import android.app.Activity;
import android.app.ActivityManager; import android.app.ActivityManager;
@@ -41,6 +42,7 @@ import android.os.Handler;
import android.os.PowerManager; import android.os.PowerManager;
import android.os.PowerManager.ServiceType; import android.os.PowerManager.ServiceType;
import android.os.PowerManagerInternal; import android.os.PowerManagerInternal;
import android.os.Process;
import android.os.RemoteException; import android.os.RemoteException;
import android.os.ResultReceiver; import android.os.ResultReceiver;
import android.os.ServiceManager; import android.os.ServiceManager;
@@ -67,6 +69,10 @@ import com.android.server.wm.WindowManagerInternal;
import java.io.FileDescriptor; import java.io.FileDescriptor;
import java.io.PrintWriter; 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 { final class UiModeManagerService extends SystemService {
private static final String TAG = UiModeManager.class.getSimpleName(); private static final String TAG = UiModeManager.class.getSimpleName();
@@ -124,6 +130,7 @@ final class UiModeManagerService extends SystemService {
private NotificationManager mNotificationManager; private NotificationManager mNotificationManager;
private StatusBarManager mStatusBarManager; private StatusBarManager mStatusBarManager;
private WindowManagerInternal mWindowManager; private WindowManagerInternal mWindowManager;
private PowerManager mPowerManager;
private PowerManager.WakeLock mWakeLock; private PowerManager.WakeLock mWakeLock;
@@ -136,11 +143,12 @@ final class UiModeManagerService extends SystemService {
@VisibleForTesting @VisibleForTesting
protected UiModeManagerService(Context context, WindowManagerInternal wm, protected UiModeManagerService(Context context, WindowManagerInternal wm,
PowerManager.WakeLock wl, TwilightManager tm, PowerManager.WakeLock wl, TwilightManager tm,
boolean setupWizardComplete) { PowerManager pm, boolean setupWizardComplete) {
super(context); super(context);
mWindowManager = wm; mWindowManager = wm;
mWakeLock = wl; mWakeLock = wl;
mTwilightManager = tm; mTwilightManager = tm;
mPowerManager = pm;
mSetupWizardComplete = setupWizardComplete; mSetupWizardComplete = setupWizardComplete;
} }
@@ -261,14 +269,19 @@ final class UiModeManagerService extends SystemService {
private final ContentObserver mDarkThemeObserver = new ContentObserver(mHandler) { private final ContentObserver mDarkThemeObserver = new ContentObserver(mHandler) {
@Override @Override
public void onChange(boolean selfChange, Uri uri) { public void onChange(boolean selfChange, Uri uri) {
int mode = Secure.getIntForUser(getContext().getContentResolver(), Secure.UI_NIGHT_MODE, updateSystemProperties();
mNightMode, 0);
mode = mode == UiModeManager.MODE_NIGHT_AUTO
? UiModeManager.MODE_NIGHT_YES : mode;
SystemProperties.set(SYSTEM_PROPERTY_DEVICE_THEME, Integer.toString(mode));
} }
}; };
private void updateSystemProperties() {
int mode = Secure.getIntForUser(getContext().getContentResolver(), Secure.UI_NIGHT_MODE,
mNightMode, 0);
if (mode == MODE_NIGHT_AUTO) {
mode = MODE_NIGHT_YES;
}
SystemProperties.set(SYSTEM_PROPERTY_DEVICE_THEME, Integer.toString(mode));
}
@Override @Override
public void onSwitchUser(int userHandle) { public void onSwitchUser(int userHandle) {
super.onSwitchUser(userHandle); super.onSwitchUser(userHandle);
@@ -280,9 +293,9 @@ final class UiModeManagerService extends SystemService {
public void onStart() { public void onStart() {
final Context context = getContext(); final Context context = getContext();
final PowerManager powerManager = mPowerManager =
(PowerManager) context.getSystemService(Context.POWER_SERVICE); (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); mWindowManager = LocalServices.getService(WindowManagerInternal.class);
// 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
@@ -349,6 +362,7 @@ final class UiModeManagerService extends SystemService {
context.getContentResolver().registerContentObserver(Secure.getUriFor(Secure.UI_NIGHT_MODE), context.getContentResolver().registerContentObserver(Secure.getUriFor(Secure.UI_NIGHT_MODE),
false, mDarkThemeObserver, 0); false, mDarkThemeObserver, 0);
mHandler.post(() -> updateSystemProperties());
} }
@VisibleForTesting @VisibleForTesting
@@ -406,6 +420,7 @@ final class UiModeManagerService extends SystemService {
} }
private void registerScreenOffEvent() { private void registerScreenOffEvent() {
if (mPowerSave) return;
mWaitForScreenOff = true; mWaitForScreenOff = true;
final IntentFilter intentFilter = final IntentFilter intentFilter =
new IntentFilter(Intent.ACTION_SCREEN_OFF); new IntentFilter(Intent.ACTION_SCREEN_OFF);
@@ -510,7 +525,9 @@ final class UiModeManagerService extends SystemService {
persistNightMode(user); persistNightMode(user);
} }
// on screen off will update configuration instead // on screen off will update configuration instead
if (mNightMode != UiModeManager.MODE_NIGHT_AUTO || mCar) { if ((mNightMode != MODE_NIGHT_AUTO)
|| shouldApplyAutomaticChangesImmediately()) {
unregisterScreenOffEvent();
updateLocked(0, 0); updateLocked(0, 0);
} else { } else {
registerScreenOffEvent(); registerScreenOffEvent();
@@ -600,6 +617,7 @@ final class UiModeManagerService extends SystemService {
pw.print(" mSetUiMode=0x"); pw.println(Integer.toHexString(mSetUiMode)); pw.print(" mSetUiMode=0x"); pw.println(Integer.toHexString(mSetUiMode));
pw.print(" mHoldingConfiguration="); pw.print(mHoldingConfiguration); pw.print(" mHoldingConfiguration="); pw.print(mHoldingConfiguration);
pw.print(" mSystemReady="); pw.println(mSystemReady); pw.print(" mSystemReady="); pw.println(mSystemReady);
if (mTwilightManager != null) { if (mTwilightManager != null) {
// We may not have a TwilightManager. // We may not have a TwilightManager.
pw.print(" mTwilightService.getLastTwilightState()="); pw.print(" mTwilightService.getLastTwilightState()=");
@@ -615,7 +633,6 @@ final class UiModeManagerService extends SystemService {
mTwilightManager = getLocalService(TwilightManager.class); mTwilightManager = getLocalService(TwilightManager.class);
mSystemReady = true; mSystemReady = true;
mCarModeEnabled = mDockState == Intent.EXTRA_DOCK_STATE_CAR; mCarModeEnabled = mDockState == Intent.EXTRA_DOCK_STATE_CAR;
updateComputedNightModeLocked();
registerVrStateListener(); registerVrStateListener();
updateLocked(0, 0); updateLocked(0, 0);
} }
@@ -683,24 +700,31 @@ final class UiModeManagerService extends SystemService {
uiMode = Configuration.UI_MODE_TYPE_VR_HEADSET; 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) { if (mTwilightManager != null) {
mTwilightManager.registerListener(mTwilightListener, mHandler); mTwilightManager.registerListener(mTwilightListener, mHandler);
final TwilightState lastState = mTwilightManager.getLastTwilightState();
activateNightMode = lastState == null ? mComputedNightMode : lastState.isNight();
} }
updateComputedNightModeLocked();
uiMode |= mComputedNightMode ? Configuration.UI_MODE_NIGHT_YES updateComputedNightModeLocked(activateNightMode);
: Configuration.UI_MODE_NIGHT_NO;
} else { } else {
if (mTwilightManager != null) { if (mTwilightManager != null) {
mTwilightManager.unregisterListener(mTwilightListener); mTwilightManager.unregisterListener(mTwilightListener);
} }
uiMode |= mNightMode << 4;
} }
// Override night mode in power save mode if not in car mode // Override night mode in power save mode if not in car mode
if (mPowerSave && !mCarModeEnabled) { if (mPowerSave && !mCarModeEnabled) {
uiMode &= ~Configuration.UI_MODE_NIGHT_NO; uiMode &= ~Configuration.UI_MODE_NIGHT_NO;
uiMode |= Configuration.UI_MODE_NIGHT_YES; uiMode |= Configuration.UI_MODE_NIGHT_YES;
} else {
uiMode = getComputedUiModeConfiguration(uiMode);
} }
if (LOG) { if (LOG) {
@@ -712,11 +736,20 @@ final class UiModeManagerService extends SystemService {
} }
mCurUiMode = uiMode; mCurUiMode = uiMode;
if (!mHoldingConfiguration || !mWaitForScreenOff) { if (!mHoldingConfiguration && (!mWaitForScreenOff || mPowerSave)) {
mConfiguration.uiMode = uiMode; 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() { private void applyConfigurationExternallyLocked() {
if (mSetUiMode != mConfiguration.uiMode) { if (mSetUiMode != mConfiguration.uiMode) {
mSetUiMode = mConfiguration.uiMode; mSetUiMode = mConfiguration.uiMode;
@@ -724,10 +757,16 @@ final class UiModeManagerService extends SystemService {
ActivityTaskManager.getService().updateConfiguration(mConfiguration); ActivityTaskManager.getService().updateConfiguration(mConfiguration);
} catch (RemoteException e) { } catch (RemoteException e) {
Slog.w(TAG, "Failure communicating with activity manager", 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) { void updateLocked(int enableFlags, int disableFlags) {
String action = null; String action = null;
String oldAction = null; String oldAction = null;
@@ -958,12 +997,8 @@ final class UiModeManagerService extends SystemService {
} }
} }
private void updateComputedNightModeLocked() { private void updateComputedNightModeLocked(boolean activate) {
if (mTwilightManager != null) { mComputedNightMode = activate;
TwilightState state = mTwilightManager.getLastTwilightState();
if (state != null) {
mComputedNightMode = state.isNight();
}
if (mNightModeOverride == UiModeManager.MODE_NIGHT_YES && !mComputedNightMode) { if (mNightModeOverride == UiModeManager.MODE_NIGHT_YES && !mComputedNightMode) {
mComputedNightMode = true; mComputedNightMode = true;
return; return;
@@ -978,7 +1013,6 @@ final class UiModeManagerService extends SystemService {
Secure.putIntForUser(getContext().getContentResolver(), Secure.putIntForUser(getContext().getContentResolver(),
OVERRIDE_NIGHT_MODE, mNightModeOverride, user); OVERRIDE_NIGHT_MODE, mNightModeOverride, user);
} }
}
private void registerVrStateListener() { private void registerVrStateListener() {
IVrManager vrManager = IVrManager.Stub.asInterface(ServiceManager.getService( IVrManager vrManager = IVrManager.Stub.asInterface(ServiceManager.getService(

View File

@@ -24,6 +24,7 @@ import android.content.pm.PackageManager;
import android.content.res.Configuration; import android.content.res.Configuration;
import android.content.res.Resources; import android.content.res.Resources;
import android.os.PowerManager; import android.os.PowerManager;
import android.os.PowerManagerInternal;
import android.os.RemoteException; import android.os.RemoteException;
import android.testing.AndroidTestingRunner; import android.testing.AndroidTestingRunner;
import android.testing.TestableLooper; import android.testing.TestableLooper;
@@ -66,12 +67,14 @@ public class UiModeManagerServiceTest extends UiServiceTestCase {
TwilightManager mTwilightManager; TwilightManager mTwilightManager;
@Mock @Mock
PowerManager.WakeLock mWakeLock; PowerManager.WakeLock mWakeLock;
@Mock
PowerManager mPowerManager;
private Set<BroadcastReceiver> mScreenOffRecievers; private Set<BroadcastReceiver> mScreenOffRecievers;
@Before @Before
public void setUp() { public void setUp() {
mUiManagerService = new UiModeManagerService(mContext, mWindowManager, mWakeLock, mUiManagerService = new UiModeManagerService(mContext, mWindowManager, mWakeLock,
mTwilightManager, true); mTwilightManager, mPowerManager, true);
mScreenOffRecievers = new HashSet<>(); mScreenOffRecievers = new HashSet<>();
mService = mUiManagerService.getService(); mService = mUiManagerService.getService();
when(mContext.checkCallingOrSelfPermission(anyString())) when(mContext.checkCallingOrSelfPermission(anyString()))