package com.android.systemui.colorextraction;
-import android.annotation.ColorInt;
import android.app.WallpaperColors;
import android.app.WallpaperManager;
import android.content.Context;
-import android.os.Handler;
-import android.os.RemoteException;
+import android.graphics.Color;
import android.os.UserHandle;
-import android.util.Log;
-import android.view.Display;
-import android.view.IWallpaperVisibilityListener;
-import android.view.IWindowManager;
-import android.view.WindowManagerGlobal;
import com.android.internal.annotations.VisibleForTesting;
import com.android.internal.colorextraction.ColorExtractor;
ConfigurationController.ConfigurationListener {
private static final String TAG = "SysuiColorExtractor";
private final Tonal mTonal;
- private boolean mWallpaperVisible;
- private boolean mHasBackdrop;
- // Colors to return when the wallpaper isn't visible
- private final GradientColors mWpHiddenColors;
+ private boolean mHasMediaArtwork;
+ private final GradientColors mNeutralColorsLock;
+ private final GradientColors mBackdropColors;
@Inject
public SysuiColorExtractor(Context context, ConfigurationController configurationController) {
- this(context, new Tonal(context), configurationController, true,
- context.getSystemService(WallpaperManager.class));
+ this(context, new Tonal(context), configurationController,
+ context.getSystemService(WallpaperManager.class), false /* immediately */);
}
@VisibleForTesting
public SysuiColorExtractor(Context context, ExtractionType type,
- ConfigurationController configurationController, boolean registerVisibility,
- WallpaperManager wallpaperManager) {
- super(context, type, false /* immediately */, wallpaperManager);
+ ConfigurationController configurationController,
+ WallpaperManager wallpaperManager, boolean immediately) {
+ super(context, type, immediately, wallpaperManager);
mTonal = type instanceof Tonal ? (Tonal) type : new Tonal(context);
- mWpHiddenColors = new GradientColors();
+ mNeutralColorsLock = new GradientColors();
configurationController.addCallback(this);
- WallpaperColors systemColors = getWallpaperColors(WallpaperManager.FLAG_SYSTEM);
- updateDefaultGradients(systemColors);
-
- if (registerVisibility) {
- try {
- IWindowManager windowManagerService = WindowManagerGlobal.getWindowManagerService();
- Handler handler = Handler.getMain();
- boolean visible = windowManagerService.registerWallpaperVisibilityListener(
- new IWallpaperVisibilityListener.Stub() {
- @Override
- public void onWallpaperVisibilityChanged(boolean newVisibility,
- int displayId) throws RemoteException {
- handler.post(() -> setWallpaperVisible(newVisibility));
- }
- }, Display.DEFAULT_DISPLAY);
- setWallpaperVisible(visible);
- } catch (RemoteException e) {
- Log.w(TAG, "Can't listen to wallpaper visibility changes", e);
- }
- }
+ mBackdropColors = new GradientColors();
+ mBackdropColors.setMainColor(Color.BLACK);
// Listen to all users instead of only the current one.
wallpaperManager.removeOnColorsChangedListener(this);
UserHandle.USER_ALL);
}
- private void updateDefaultGradients(WallpaperColors colors) {
- mTonal.applyFallback(colors, mWpHiddenColors);
+ @Override
+ protected void extractWallpaperColors() {
+ super.extractWallpaperColors();
+ // mTonal is final but this method will be invoked by the base class during its ctor.
+ if (mTonal == null) {
+ return;
+ }
+ mTonal.applyFallback(mLockColors == null ? mSystemColors : mLockColors, mNeutralColorsLock);
}
@Override
// Colors do not belong to current user, ignoring.
return;
}
-
- super.onColorsChanged(colors, which);
-
- if ((which & WallpaperManager.FLAG_SYSTEM) != 0) {
- @ColorInt int oldColor = mWpHiddenColors.getMainColor();
- updateDefaultGradients(colors);
- if (oldColor != mWpHiddenColors.getMainColor()) {
- triggerColorsChanged(WallpaperManager.FLAG_SYSTEM);
- }
+ if ((which & WallpaperManager.FLAG_LOCK) != 0) {
+ mTonal.applyFallback(colors, mNeutralColorsLock);
}
+ super.onColorsChanged(colors, which);
}
@Override
public void onUiModeChanged() {
- WallpaperColors systemColors = getWallpaperColors(WallpaperManager.FLAG_SYSTEM);
- updateDefaultGradients(systemColors);
- triggerColorsChanged(WallpaperManager.FLAG_SYSTEM);
+ extractWallpaperColors();
+ triggerColorsChanged(WallpaperManager.FLAG_SYSTEM | WallpaperManager.FLAG_LOCK);
+ }
+
+ @Override
+ public GradientColors getColors(int which, int type) {
+ if (mHasMediaArtwork && (which & WallpaperManager.FLAG_LOCK) != 0) {
+ return mBackdropColors;
+ }
+ return super.getColors(which, type);
}
/**
- * Colors the should be using for scrims.
+ * Colors that should be using for scrims.
*
* They will be:
* - A light gray if the wallpaper is light
* - Black otherwise
*/
public GradientColors getNeutralColors() {
- return mWpHiddenColors;
- }
-
- /**
- * Get TYPE_NORMAL colors when wallpaper is visible, or fallback otherwise.
- *
- * @param which FLAG_LOCK or FLAG_SYSTEM
- * @return colors
- */
- @Override
- public GradientColors getColors(int which) {
- return getColors(which, TYPE_DARK);
- }
-
- /**
- * Wallpaper colors when the wallpaper is visible, fallback otherwise.
- *
- * @param which FLAG_LOCK or FLAG_SYSTEM
- * @param type TYPE_NORMAL, TYPE_DARK or TYPE_EXTRA_DARK
- * @return colors
- */
- @Override
- public GradientColors getColors(int which, int type) {
- return getColors(which, type, false /* ignoreVisibility */);
- }
-
- /**
- * Get TYPE_NORMAL colors, possibly ignoring wallpaper visibility.
- *
- * @param which FLAG_LOCK or FLAG_SYSTEM
- * @param ignoreWallpaperVisibility whether you want fallback colors or not if the wallpaper
- * isn't visible
- * @return
- */
- public GradientColors getColors(int which, boolean ignoreWallpaperVisibility) {
- return getColors(which, TYPE_NORMAL, ignoreWallpaperVisibility);
- }
-
- /**
- *
- * @param which FLAG_LOCK or FLAG_SYSTEM
- * @param type TYPE_NORMAL, TYPE_DARK or TYPE_EXTRA_DARK
- * @param ignoreWallpaperVisibility true if true wallpaper colors should be returning
- * if it's visible or not
- * @return colors
- */
- public GradientColors getColors(int which, int type, boolean ignoreWallpaperVisibility) {
- // mWallpaperVisible only handles the "system wallpaper" and will be always set to false
- // if we have different lock and system wallpapers.
- if (which == WallpaperManager.FLAG_SYSTEM) {
- if (mWallpaperVisible || ignoreWallpaperVisibility) {
- return super.getColors(which, type);
- } else {
- return mWpHiddenColors;
- }
- } else {
- if (mHasBackdrop) {
- return mWpHiddenColors;
- } else {
- return super.getColors(which, type);
- }
- }
- }
-
- @VisibleForTesting
- void setWallpaperVisible(boolean visible) {
- if (mWallpaperVisible != visible) {
- mWallpaperVisible = visible;
- triggerColorsChanged(WallpaperManager.FLAG_SYSTEM);
- }
+ return mHasMediaArtwork ? mBackdropColors : mNeutralColorsLock;
}
- public void setHasBackdrop(boolean hasBackdrop) {
- if (mHasBackdrop != hasBackdrop) {
- mHasBackdrop = hasBackdrop;
+ public void setHasMediaArtwork(boolean hasBackdrop) {
+ if (mHasMediaArtwork != hasBackdrop) {
+ mHasMediaArtwork = hasBackdrop;
triggerColorsChanged(WallpaperManager.FLAG_LOCK);
}
}
pw.println(" Gradients:");
pw.println(" system: " + Arrays.toString(system));
pw.println(" lock: " + Arrays.toString(lock));
- pw.println(" Default scrim: " + mWpHiddenColors);
+ pw.println(" Neutral colors: " + mNeutralColorsLock);
+ pw.println(" Has media backdrop: " + mHasMediaArtwork);
}
}
outGradientColorsNormal.set(mColors);
outGradientColorsDark.set(mColors);
outGradientColorsExtraDark.set(mColors);
- }, mock(ConfigurationController.class), false, mWallpaperManager);
+ }, mock(ConfigurationController.class), mWallpaperManager, true /* immediately */);
}
@Test
- public void getColors_usesGreyIfWallpaperNotVisible() {
- simulateEvent(mColorExtractor);
- mColorExtractor.setWallpaperVisible(false);
-
- ColorExtractor.GradientColors fallbackColors = mColorExtractor.getNeutralColors();
-
- for (int type : sTypes) {
- assertEquals("Not using fallback!",
- mColorExtractor.getColors(WallpaperManager.FLAG_SYSTEM, type), fallbackColors);
- assertNotEquals("Wallpaper visibility event should not affect lock wallpaper.",
- mColorExtractor.getColors(WallpaperManager.FLAG_LOCK, type), fallbackColors);
- }
- }
-
- @Test
- public void getColors_doesntUseFallbackIfVisible() {
+ public void getColors() {
mColors.setMainColor(Color.RED);
mColors.setSecondaryColor(Color.RED);
simulateEvent(mColorExtractor);
- mColorExtractor.setWallpaperVisible(true);
-
for (int which : sWhich) {
for (int type : sTypes) {
assertEquals("Not using extracted colors!",
@Test
public void getColors_fallbackWhenMediaIsVisible() {
simulateEvent(mColorExtractor);
- mColorExtractor.setWallpaperVisible(true);
- mColorExtractor.setHasBackdrop(true);
+ mColorExtractor.setHasMediaArtwork(true);
ColorExtractor.GradientColors fallbackColors = mColorExtractor.getNeutralColors();
Tonal tonal = mock(Tonal.class);
ConfigurationController configurationController = mock(ConfigurationController.class);
SysuiColorExtractor sysuiColorExtractor = new SysuiColorExtractor(getContext(),
- tonal, configurationController, false /* registerVisibility */, mWallpaperManager);
+ tonal, configurationController, mWallpaperManager, true /* immediately */);
verify(configurationController).addCallback(eq(sysuiColorExtractor));
reset(tonal);