Loading packages/SystemUI/src/com/android/systemui/theme/ThemeOverlayController.java +79 −43 Original line number Diff line number Diff line Loading @@ -48,6 +48,8 @@ import android.provider.Settings; import android.text.TextUtils; import android.util.ArrayMap; import android.util.Log; import android.util.SparseArray; import android.util.SparseIntArray; import android.util.TypedValue; import androidx.annotation.NonNull; Loading Loading @@ -104,14 +106,15 @@ public class ThemeOverlayController extends SystemUI implements Dumpable { private final UserManager mUserManager; private final BroadcastDispatcher mBroadcastDispatcher; private final Executor mBgExecutor; private SecureSettings mSecureSettings; private final SecureSettings mSecureSettings; private final Executor mMainExecutor; private final Handler mBgHandler; private final boolean mIsMonetEnabled; private UserTracker mUserTracker; private DeviceProvisionedController mDeviceProvisionedController; private WallpaperColors mCurrentColors; private WallpaperManager mWallpaperManager; private final UserTracker mUserTracker; private final DeviceProvisionedController mDeviceProvisionedController; // Current wallpaper colors associated to a user. private final SparseArray<WallpaperColors> mCurrentColors = new SparseArray<>(); private final WallpaperManager mWallpaperManager; private ColorScheme mColorScheme; // If fabricated overlays were already created for the current theme. private boolean mNeedsOverlayCreation; Loading @@ -125,11 +128,11 @@ public class ThemeOverlayController extends SystemUI implements Dumpable { private FabricatedOverlay mNeutralOverlay; // If wallpaper color event will be accepted and change the UI colors. private boolean mAcceptColorEvents = true; // If non-null, colors that were sent to the framework, and processing was deferred until // the next time the screen is off. private WallpaperColors mDeferredWallpaperColors; private int mDeferredWallpaperColorsFlags; private WakefulnessLifecycle mWakefulnessLifecycle; // If non-null (per user), colors that were sent to the framework, and processing was deferred // until the next time the screen is off. private final SparseArray<WallpaperColors> mDeferredWallpaperColors = new SparseArray<>(); private final SparseIntArray mDeferredWallpaperColorsFlags = new SparseIntArray(); private final WakefulnessLifecycle mWakefulnessLifecycle; // Defers changing themes until Setup Wizard is done. private boolean mDeferredThemeEvaluation; Loading @@ -152,27 +155,53 @@ public class ThemeOverlayController extends SystemUI implements Dumpable { } }; private final OnColorsChangedListener mOnColorsChangedListener = (wallpaperColors, which) -> { if (!mAcceptColorEvents && mWakefulnessLifecycle.getWakefulness() != WAKEFULNESS_ASLEEP) { mDeferredWallpaperColors = wallpaperColors; mDeferredWallpaperColorsFlags = which; Log.i(TAG, "colors received; processing deferred until screen off: " + wallpaperColors); private final OnColorsChangedListener mOnColorsChangedListener = new OnColorsChangedListener() { @Override public void onColorsChanged(WallpaperColors wallpaperColors, int which) { throw new IllegalStateException("This should never be invoked, all messages should " + "arrive on the overload that has a user id"); } @Override public void onColorsChanged(WallpaperColors wallpaperColors, int which, int userId) { boolean currentUser = userId == mUserTracker.getUserId(); if (currentUser && !mAcceptColorEvents && mWakefulnessLifecycle.getWakefulness() != WAKEFULNESS_ASLEEP) { mDeferredWallpaperColors.put(userId, wallpaperColors); mDeferredWallpaperColorsFlags.put(userId, which); Log.i(TAG, "colors received; processing deferred until screen off: " + wallpaperColors + " user: " + userId); return; } if (wallpaperColors != null) { if (currentUser && wallpaperColors != null) { mAcceptColorEvents = false; // Any cache of colors deferred for process is now stale. mDeferredWallpaperColors = null; mDeferredWallpaperColorsFlags = 0; mDeferredWallpaperColors.put(userId, null); mDeferredWallpaperColorsFlags.put(userId, 0); } handleWallpaperColors(wallpaperColors, which); handleWallpaperColors(wallpaperColors, which, userId); } }; private int getLatestWallpaperType() { return mWallpaperManager.getWallpaperId(WallpaperManager.FLAG_LOCK) > mWallpaperManager.getWallpaperId(WallpaperManager.FLAG_SYSTEM) private final UserTracker.Callback mUserTrackerCallback = new UserTracker.Callback() { @Override public void onUserChanged(int newUser, @NonNull Context userContext) { boolean isManagedProfile = mUserManager.isManagedProfile(newUser); if (!mDeviceProvisionedController.isCurrentUserSetup() && isManagedProfile) { Log.i(TAG, "User setup not finished when new user event was received. " + "Deferring... Managed profile? " + isManagedProfile); return; } if (DEBUG) Log.d(TAG, "Updating overlays for user switch / profile added."); reevaluateSystemTheme(true /* forceReload */); } }; private int getLatestWallpaperType(int userId) { return mWallpaperManager.getWallpaperIdForUser(WallpaperManager.FLAG_LOCK, userId) > mWallpaperManager.getWallpaperIdForUser(WallpaperManager.FLAG_SYSTEM, userId) ? WallpaperManager.FLAG_LOCK : WallpaperManager.FLAG_SYSTEM; } Loading Loading @@ -204,14 +233,21 @@ public class ThemeOverlayController extends SystemUI implements Dumpable { return false; } private void handleWallpaperColors(WallpaperColors wallpaperColors, int flags) { final boolean hadWallpaperColors = mCurrentColors != null; int latestWallpaperType = getLatestWallpaperType(); private void handleWallpaperColors(WallpaperColors wallpaperColors, int flags, int userId) { final int currentUser = mUserTracker.getUserId(); final boolean hadWallpaperColors = mCurrentColors.get(userId) != null; int latestWallpaperType = getLatestWallpaperType(userId); if ((flags & latestWallpaperType) != 0) { mCurrentColors = wallpaperColors; mCurrentColors.put(userId, wallpaperColors); if (DEBUG) Log.d(TAG, "got new colors: " + wallpaperColors + " where: " + flags); } if (userId != currentUser) { Log.d(TAG, "Colors " + wallpaperColors + " for user " + userId + ". " + "Not for current user: " + currentUser); return; } if (mDeviceProvisionedController != null && !mDeviceProvisionedController.isCurrentUserSetup()) { if (hadWallpaperColors) { Loading @@ -226,13 +262,12 @@ public class ThemeOverlayController extends SystemUI implements Dumpable { } else { if (DEBUG) { Log.i(TAG, "During user setup, but allowing first color event: had? " + hadWallpaperColors + " has? " + (mCurrentColors != null)); + hadWallpaperColors + " has? " + (mCurrentColors.get(userId) != null)); } } } // Check if we need to reset to default colors (if a color override was set that is sourced // from the wallpaper) int currentUser = mUserTracker.getUserId(); String overlayPackageJson = mSecureSettings.getStringForUser( Settings.Secure.THEME_CUSTOMIZATION_OVERLAY_PACKAGES, currentUser); Loading Loading @@ -278,10 +313,9 @@ public class ThemeOverlayController extends SystemUI implements Dumpable { @Override public void onReceive(Context context, Intent intent) { boolean newWorkProfile = Intent.ACTION_MANAGED_PROFILE_ADDED.equals(intent.getAction()); boolean userStarted = Intent.ACTION_USER_SWITCHED.equals(intent.getAction()); boolean isManagedProfile = mUserManager.isManagedProfile( intent.getIntExtra(Intent.EXTRA_USER_HANDLE, 0)); if (userStarted || newWorkProfile) { if (newWorkProfile) { if (!mDeviceProvisionedController.isCurrentUserSetup() && isManagedProfile) { Log.i(TAG, "User setup not finished when " + intent.getAction() + " was received. Deferring... Managed profile? " + isManagedProfile); Loading Loading @@ -330,7 +364,6 @@ public class ThemeOverlayController extends SystemUI implements Dumpable { public void start() { if (DEBUG) Log.d(TAG, "Start"); final IntentFilter filter = new IntentFilter(); filter.addAction(Intent.ACTION_USER_SWITCHED); filter.addAction(Intent.ACTION_MANAGED_PROFILE_ADDED); filter.addAction(Intent.ACTION_WALLPAPER_CHANGED); mBroadcastDispatcher.registerReceiver(mBroadcastReceiver, filter, mMainExecutor, Loading Loading @@ -365,15 +398,17 @@ public class ThemeOverlayController extends SystemUI implements Dumpable { return; } mUserTracker.addCallback(mUserTrackerCallback, mMainExecutor); mDeviceProvisionedController.addCallback(mDeviceProvisionedListener); // Upon boot, make sure we have the most up to date colors Runnable updateColors = () -> { WallpaperColors systemColor = mWallpaperManager.getWallpaperColors( getLatestWallpaperType()); getLatestWallpaperType(mUserTracker.getUserId())); Runnable applyColors = () -> { if (DEBUG) Log.d(TAG, "Boot colors: " + systemColor); mCurrentColors = systemColor; mCurrentColors.put(mUserTracker.getUserId(), systemColor); reevaluateSystemTheme(false /* forceReload */); }; if (mDeviceProvisionedController.isCurrentUserSetup()) { Loading @@ -395,21 +430,22 @@ public class ThemeOverlayController extends SystemUI implements Dumpable { mWakefulnessLifecycle.addObserver(new WakefulnessLifecycle.Observer() { @Override public void onFinishedGoingToSleep() { if (mDeferredWallpaperColors != null) { WallpaperColors colors = mDeferredWallpaperColors; int flags = mDeferredWallpaperColorsFlags; final int userId = mUserTracker.getUserId(); final WallpaperColors colors = mDeferredWallpaperColors.get(userId); if (colors != null) { int flags = mDeferredWallpaperColorsFlags.get(userId); mDeferredWallpaperColors = null; mDeferredWallpaperColorsFlags = 0; mDeferredWallpaperColors.put(userId, null); mDeferredWallpaperColorsFlags.put(userId, 0); handleWallpaperColors(colors, flags); handleWallpaperColors(colors, flags, userId); } } }); } private void reevaluateSystemTheme(boolean forceReload) { final WallpaperColors currentColors = mCurrentColors; final WallpaperColors currentColors = mCurrentColors.get(mUserTracker.getUserId()); final int mainColor; final int accentCandidate; if (currentColors == null) { Loading packages/SystemUI/tests/src/com/android/systemui/theme/ThemeOverlayControllerTest.java +99 −35 File changed.Preview size limit exceeded, changes collapsed. Show changes Loading
packages/SystemUI/src/com/android/systemui/theme/ThemeOverlayController.java +79 −43 Original line number Diff line number Diff line Loading @@ -48,6 +48,8 @@ import android.provider.Settings; import android.text.TextUtils; import android.util.ArrayMap; import android.util.Log; import android.util.SparseArray; import android.util.SparseIntArray; import android.util.TypedValue; import androidx.annotation.NonNull; Loading Loading @@ -104,14 +106,15 @@ public class ThemeOverlayController extends SystemUI implements Dumpable { private final UserManager mUserManager; private final BroadcastDispatcher mBroadcastDispatcher; private final Executor mBgExecutor; private SecureSettings mSecureSettings; private final SecureSettings mSecureSettings; private final Executor mMainExecutor; private final Handler mBgHandler; private final boolean mIsMonetEnabled; private UserTracker mUserTracker; private DeviceProvisionedController mDeviceProvisionedController; private WallpaperColors mCurrentColors; private WallpaperManager mWallpaperManager; private final UserTracker mUserTracker; private final DeviceProvisionedController mDeviceProvisionedController; // Current wallpaper colors associated to a user. private final SparseArray<WallpaperColors> mCurrentColors = new SparseArray<>(); private final WallpaperManager mWallpaperManager; private ColorScheme mColorScheme; // If fabricated overlays were already created for the current theme. private boolean mNeedsOverlayCreation; Loading @@ -125,11 +128,11 @@ public class ThemeOverlayController extends SystemUI implements Dumpable { private FabricatedOverlay mNeutralOverlay; // If wallpaper color event will be accepted and change the UI colors. private boolean mAcceptColorEvents = true; // If non-null, colors that were sent to the framework, and processing was deferred until // the next time the screen is off. private WallpaperColors mDeferredWallpaperColors; private int mDeferredWallpaperColorsFlags; private WakefulnessLifecycle mWakefulnessLifecycle; // If non-null (per user), colors that were sent to the framework, and processing was deferred // until the next time the screen is off. private final SparseArray<WallpaperColors> mDeferredWallpaperColors = new SparseArray<>(); private final SparseIntArray mDeferredWallpaperColorsFlags = new SparseIntArray(); private final WakefulnessLifecycle mWakefulnessLifecycle; // Defers changing themes until Setup Wizard is done. private boolean mDeferredThemeEvaluation; Loading @@ -152,27 +155,53 @@ public class ThemeOverlayController extends SystemUI implements Dumpable { } }; private final OnColorsChangedListener mOnColorsChangedListener = (wallpaperColors, which) -> { if (!mAcceptColorEvents && mWakefulnessLifecycle.getWakefulness() != WAKEFULNESS_ASLEEP) { mDeferredWallpaperColors = wallpaperColors; mDeferredWallpaperColorsFlags = which; Log.i(TAG, "colors received; processing deferred until screen off: " + wallpaperColors); private final OnColorsChangedListener mOnColorsChangedListener = new OnColorsChangedListener() { @Override public void onColorsChanged(WallpaperColors wallpaperColors, int which) { throw new IllegalStateException("This should never be invoked, all messages should " + "arrive on the overload that has a user id"); } @Override public void onColorsChanged(WallpaperColors wallpaperColors, int which, int userId) { boolean currentUser = userId == mUserTracker.getUserId(); if (currentUser && !mAcceptColorEvents && mWakefulnessLifecycle.getWakefulness() != WAKEFULNESS_ASLEEP) { mDeferredWallpaperColors.put(userId, wallpaperColors); mDeferredWallpaperColorsFlags.put(userId, which); Log.i(TAG, "colors received; processing deferred until screen off: " + wallpaperColors + " user: " + userId); return; } if (wallpaperColors != null) { if (currentUser && wallpaperColors != null) { mAcceptColorEvents = false; // Any cache of colors deferred for process is now stale. mDeferredWallpaperColors = null; mDeferredWallpaperColorsFlags = 0; mDeferredWallpaperColors.put(userId, null); mDeferredWallpaperColorsFlags.put(userId, 0); } handleWallpaperColors(wallpaperColors, which); handleWallpaperColors(wallpaperColors, which, userId); } }; private int getLatestWallpaperType() { return mWallpaperManager.getWallpaperId(WallpaperManager.FLAG_LOCK) > mWallpaperManager.getWallpaperId(WallpaperManager.FLAG_SYSTEM) private final UserTracker.Callback mUserTrackerCallback = new UserTracker.Callback() { @Override public void onUserChanged(int newUser, @NonNull Context userContext) { boolean isManagedProfile = mUserManager.isManagedProfile(newUser); if (!mDeviceProvisionedController.isCurrentUserSetup() && isManagedProfile) { Log.i(TAG, "User setup not finished when new user event was received. " + "Deferring... Managed profile? " + isManagedProfile); return; } if (DEBUG) Log.d(TAG, "Updating overlays for user switch / profile added."); reevaluateSystemTheme(true /* forceReload */); } }; private int getLatestWallpaperType(int userId) { return mWallpaperManager.getWallpaperIdForUser(WallpaperManager.FLAG_LOCK, userId) > mWallpaperManager.getWallpaperIdForUser(WallpaperManager.FLAG_SYSTEM, userId) ? WallpaperManager.FLAG_LOCK : WallpaperManager.FLAG_SYSTEM; } Loading Loading @@ -204,14 +233,21 @@ public class ThemeOverlayController extends SystemUI implements Dumpable { return false; } private void handleWallpaperColors(WallpaperColors wallpaperColors, int flags) { final boolean hadWallpaperColors = mCurrentColors != null; int latestWallpaperType = getLatestWallpaperType(); private void handleWallpaperColors(WallpaperColors wallpaperColors, int flags, int userId) { final int currentUser = mUserTracker.getUserId(); final boolean hadWallpaperColors = mCurrentColors.get(userId) != null; int latestWallpaperType = getLatestWallpaperType(userId); if ((flags & latestWallpaperType) != 0) { mCurrentColors = wallpaperColors; mCurrentColors.put(userId, wallpaperColors); if (DEBUG) Log.d(TAG, "got new colors: " + wallpaperColors + " where: " + flags); } if (userId != currentUser) { Log.d(TAG, "Colors " + wallpaperColors + " for user " + userId + ". " + "Not for current user: " + currentUser); return; } if (mDeviceProvisionedController != null && !mDeviceProvisionedController.isCurrentUserSetup()) { if (hadWallpaperColors) { Loading @@ -226,13 +262,12 @@ public class ThemeOverlayController extends SystemUI implements Dumpable { } else { if (DEBUG) { Log.i(TAG, "During user setup, but allowing first color event: had? " + hadWallpaperColors + " has? " + (mCurrentColors != null)); + hadWallpaperColors + " has? " + (mCurrentColors.get(userId) != null)); } } } // Check if we need to reset to default colors (if a color override was set that is sourced // from the wallpaper) int currentUser = mUserTracker.getUserId(); String overlayPackageJson = mSecureSettings.getStringForUser( Settings.Secure.THEME_CUSTOMIZATION_OVERLAY_PACKAGES, currentUser); Loading Loading @@ -278,10 +313,9 @@ public class ThemeOverlayController extends SystemUI implements Dumpable { @Override public void onReceive(Context context, Intent intent) { boolean newWorkProfile = Intent.ACTION_MANAGED_PROFILE_ADDED.equals(intent.getAction()); boolean userStarted = Intent.ACTION_USER_SWITCHED.equals(intent.getAction()); boolean isManagedProfile = mUserManager.isManagedProfile( intent.getIntExtra(Intent.EXTRA_USER_HANDLE, 0)); if (userStarted || newWorkProfile) { if (newWorkProfile) { if (!mDeviceProvisionedController.isCurrentUserSetup() && isManagedProfile) { Log.i(TAG, "User setup not finished when " + intent.getAction() + " was received. Deferring... Managed profile? " + isManagedProfile); Loading Loading @@ -330,7 +364,6 @@ public class ThemeOverlayController extends SystemUI implements Dumpable { public void start() { if (DEBUG) Log.d(TAG, "Start"); final IntentFilter filter = new IntentFilter(); filter.addAction(Intent.ACTION_USER_SWITCHED); filter.addAction(Intent.ACTION_MANAGED_PROFILE_ADDED); filter.addAction(Intent.ACTION_WALLPAPER_CHANGED); mBroadcastDispatcher.registerReceiver(mBroadcastReceiver, filter, mMainExecutor, Loading Loading @@ -365,15 +398,17 @@ public class ThemeOverlayController extends SystemUI implements Dumpable { return; } mUserTracker.addCallback(mUserTrackerCallback, mMainExecutor); mDeviceProvisionedController.addCallback(mDeviceProvisionedListener); // Upon boot, make sure we have the most up to date colors Runnable updateColors = () -> { WallpaperColors systemColor = mWallpaperManager.getWallpaperColors( getLatestWallpaperType()); getLatestWallpaperType(mUserTracker.getUserId())); Runnable applyColors = () -> { if (DEBUG) Log.d(TAG, "Boot colors: " + systemColor); mCurrentColors = systemColor; mCurrentColors.put(mUserTracker.getUserId(), systemColor); reevaluateSystemTheme(false /* forceReload */); }; if (mDeviceProvisionedController.isCurrentUserSetup()) { Loading @@ -395,21 +430,22 @@ public class ThemeOverlayController extends SystemUI implements Dumpable { mWakefulnessLifecycle.addObserver(new WakefulnessLifecycle.Observer() { @Override public void onFinishedGoingToSleep() { if (mDeferredWallpaperColors != null) { WallpaperColors colors = mDeferredWallpaperColors; int flags = mDeferredWallpaperColorsFlags; final int userId = mUserTracker.getUserId(); final WallpaperColors colors = mDeferredWallpaperColors.get(userId); if (colors != null) { int flags = mDeferredWallpaperColorsFlags.get(userId); mDeferredWallpaperColors = null; mDeferredWallpaperColorsFlags = 0; mDeferredWallpaperColors.put(userId, null); mDeferredWallpaperColorsFlags.put(userId, 0); handleWallpaperColors(colors, flags); handleWallpaperColors(colors, flags, userId); } } }); } private void reevaluateSystemTheme(boolean forceReload) { final WallpaperColors currentColors = mCurrentColors; final WallpaperColors currentColors = mCurrentColors.get(mUserTracker.getUserId()); final int mainColor; final int accentCandidate; if (currentColors == null) { Loading
packages/SystemUI/tests/src/com/android/systemui/theme/ThemeOverlayControllerTest.java +99 −35 File changed.Preview size limit exceeded, changes collapsed. Show changes