Loading core/java/com/android/internal/colorextraction/ColorExtractor.java +34 −11 Original line number Diff line number Diff line Loading @@ -21,8 +21,7 @@ import android.annotation.Nullable; import android.app.WallpaperColors; import android.app.WallpaperManager; import android.content.Context; import android.os.Trace; import android.os.UserHandle; import android.os.AsyncTask; import android.util.Log; import android.util.SparseArray; Loading @@ -32,7 +31,6 @@ import com.android.internal.colorextraction.types.Tonal; import java.lang.ref.WeakReference; import java.util.ArrayList; import java.util.Iterator; /** * Class to process wallpaper colors and generate a tonal palette based on them. Loading @@ -55,11 +53,11 @@ public class ColorExtractor implements WallpaperManager.OnColorsChangedListener protected WallpaperColors mLockColors; public ColorExtractor(Context context) { this(context, new Tonal(context)); this(context, new Tonal(context), true /* immediately */); } @VisibleForTesting public ColorExtractor(Context context, ExtractionType extractionType) { public ColorExtractor(Context context, ExtractionType extractionType, boolean immediately) { mContext = context; mExtractionType = extractionType; Loading @@ -73,23 +71,48 @@ public class ColorExtractor implements WallpaperManager.OnColorsChangedListener } mOnColorsChangedListeners = new ArrayList<>(); GradientColors[] systemColors = mGradientColors.get(WallpaperManager.FLAG_SYSTEM); GradientColors[] lockColors = mGradientColors.get(WallpaperManager.FLAG_LOCK); WallpaperManager wallpaperManager = mContext.getSystemService(WallpaperManager.class); if (wallpaperManager == null) { Log.w(TAG, "Can't listen to color changes!"); } else { wallpaperManager.addOnColorsChangedListener(this, null /* handler */); initExtractColors(wallpaperManager, immediately); } } // Initialize all gradients with the current colors Trace.beginSection("ColorExtractor#getWallpaperColors"); private void initExtractColors(WallpaperManager wallpaperManager, boolean immediately) { if (immediately) { mSystemColors = wallpaperManager.getWallpaperColors(WallpaperManager.FLAG_SYSTEM); mLockColors = wallpaperManager.getWallpaperColors(WallpaperManager.FLAG_LOCK); Trace.endSection(); extractWallpaperColors(); } else { new LoadWallpaperColors().executeOnExecutor( AsyncTask.THREAD_POOL_EXECUTOR, wallpaperManager); } } private class LoadWallpaperColors extends AsyncTask<WallpaperManager, Void, Void> { private WallpaperColors mSystemColors; private WallpaperColors mLockColors; @Override protected Void doInBackground(WallpaperManager... params) { mSystemColors = params[0].getWallpaperColors(WallpaperManager.FLAG_SYSTEM); mLockColors = params[0].getWallpaperColors(WallpaperManager.FLAG_LOCK); return null; } @Override protected void onPostExecute(Void b) { ColorExtractor.this.mSystemColors = mSystemColors; ColorExtractor.this.mLockColors = mLockColors; extractWallpaperColors(); triggerColorsChanged(WallpaperManager.FLAG_SYSTEM | WallpaperManager.FLAG_LOCK); } } // Initialize all gradients with the current colors private void extractWallpaperColors() { GradientColors[] systemColors = mGradientColors.get(WallpaperManager.FLAG_SYSTEM); GradientColors[] lockColors = mGradientColors.get(WallpaperManager.FLAG_LOCK); extractInto(mSystemColors, systemColors[TYPE_NORMAL], systemColors[TYPE_DARK], Loading packages/SystemUI/src/com/android/systemui/colorextraction/SysuiColorExtractor.java +1 −1 Original line number Diff line number Diff line Loading @@ -55,7 +55,7 @@ public class SysuiColorExtractor extends ColorExtractor implements Dumpable { @VisibleForTesting public SysuiColorExtractor(Context context, ExtractionType type, boolean registerVisibility) { super(context, type); super(context, type, false /* immediately */); mWpHiddenColors = new GradientColors(); WallpaperColors systemColors = getWallpaperColors(WallpaperManager.FLAG_SYSTEM); Loading tests/Internal/src/com/android/internal/colorextraction/ColorExtractorTest.java +3 −3 Original line number Diff line number Diff line Loading @@ -56,7 +56,7 @@ public class ColorExtractorTest { @Test public void ColorExtractor_extractWhenInitialized() { ExtractionType type = mock(Tonal.class); new ColorExtractor(mContext, type); new ColorExtractor(mContext, type, true); // 1 for lock and 1 for system verify(type, times(2)) .extractInto(any(), any(), any(), any()); Loading @@ -83,7 +83,7 @@ public class ColorExtractorTest { outGradientColorsDark.set(colorsExpectedDark); outGradientColorsExtraDark.set(colorsExpectedExtraDark); }; ColorExtractor extractor = new ColorExtractor(mContext, type); ColorExtractor extractor = new ColorExtractor(mContext, type, true); GradientColors colors = extractor.getColors(WallpaperManager.FLAG_SYSTEM, ColorExtractor.TYPE_NORMAL); Loading @@ -98,7 +98,7 @@ public class ColorExtractorTest { public void addOnColorsChangedListener_invokesListener() { ColorExtractor.OnColorsChangedListener mockedListeners = mock(ColorExtractor.OnColorsChangedListener.class); ColorExtractor extractor = new ColorExtractor(mContext, new Tonal(mContext)); ColorExtractor extractor = new ColorExtractor(mContext, new Tonal(mContext), true); extractor.addOnColorsChangedListener(mockedListeners); extractor.onColorsChanged(new WallpaperColors(Color.valueOf(Color.RED), null, null), Loading Loading
core/java/com/android/internal/colorextraction/ColorExtractor.java +34 −11 Original line number Diff line number Diff line Loading @@ -21,8 +21,7 @@ import android.annotation.Nullable; import android.app.WallpaperColors; import android.app.WallpaperManager; import android.content.Context; import android.os.Trace; import android.os.UserHandle; import android.os.AsyncTask; import android.util.Log; import android.util.SparseArray; Loading @@ -32,7 +31,6 @@ import com.android.internal.colorextraction.types.Tonal; import java.lang.ref.WeakReference; import java.util.ArrayList; import java.util.Iterator; /** * Class to process wallpaper colors and generate a tonal palette based on them. Loading @@ -55,11 +53,11 @@ public class ColorExtractor implements WallpaperManager.OnColorsChangedListener protected WallpaperColors mLockColors; public ColorExtractor(Context context) { this(context, new Tonal(context)); this(context, new Tonal(context), true /* immediately */); } @VisibleForTesting public ColorExtractor(Context context, ExtractionType extractionType) { public ColorExtractor(Context context, ExtractionType extractionType, boolean immediately) { mContext = context; mExtractionType = extractionType; Loading @@ -73,23 +71,48 @@ public class ColorExtractor implements WallpaperManager.OnColorsChangedListener } mOnColorsChangedListeners = new ArrayList<>(); GradientColors[] systemColors = mGradientColors.get(WallpaperManager.FLAG_SYSTEM); GradientColors[] lockColors = mGradientColors.get(WallpaperManager.FLAG_LOCK); WallpaperManager wallpaperManager = mContext.getSystemService(WallpaperManager.class); if (wallpaperManager == null) { Log.w(TAG, "Can't listen to color changes!"); } else { wallpaperManager.addOnColorsChangedListener(this, null /* handler */); initExtractColors(wallpaperManager, immediately); } } // Initialize all gradients with the current colors Trace.beginSection("ColorExtractor#getWallpaperColors"); private void initExtractColors(WallpaperManager wallpaperManager, boolean immediately) { if (immediately) { mSystemColors = wallpaperManager.getWallpaperColors(WallpaperManager.FLAG_SYSTEM); mLockColors = wallpaperManager.getWallpaperColors(WallpaperManager.FLAG_LOCK); Trace.endSection(); extractWallpaperColors(); } else { new LoadWallpaperColors().executeOnExecutor( AsyncTask.THREAD_POOL_EXECUTOR, wallpaperManager); } } private class LoadWallpaperColors extends AsyncTask<WallpaperManager, Void, Void> { private WallpaperColors mSystemColors; private WallpaperColors mLockColors; @Override protected Void doInBackground(WallpaperManager... params) { mSystemColors = params[0].getWallpaperColors(WallpaperManager.FLAG_SYSTEM); mLockColors = params[0].getWallpaperColors(WallpaperManager.FLAG_LOCK); return null; } @Override protected void onPostExecute(Void b) { ColorExtractor.this.mSystemColors = mSystemColors; ColorExtractor.this.mLockColors = mLockColors; extractWallpaperColors(); triggerColorsChanged(WallpaperManager.FLAG_SYSTEM | WallpaperManager.FLAG_LOCK); } } // Initialize all gradients with the current colors private void extractWallpaperColors() { GradientColors[] systemColors = mGradientColors.get(WallpaperManager.FLAG_SYSTEM); GradientColors[] lockColors = mGradientColors.get(WallpaperManager.FLAG_LOCK); extractInto(mSystemColors, systemColors[TYPE_NORMAL], systemColors[TYPE_DARK], Loading
packages/SystemUI/src/com/android/systemui/colorextraction/SysuiColorExtractor.java +1 −1 Original line number Diff line number Diff line Loading @@ -55,7 +55,7 @@ public class SysuiColorExtractor extends ColorExtractor implements Dumpable { @VisibleForTesting public SysuiColorExtractor(Context context, ExtractionType type, boolean registerVisibility) { super(context, type); super(context, type, false /* immediately */); mWpHiddenColors = new GradientColors(); WallpaperColors systemColors = getWallpaperColors(WallpaperManager.FLAG_SYSTEM); Loading
tests/Internal/src/com/android/internal/colorextraction/ColorExtractorTest.java +3 −3 Original line number Diff line number Diff line Loading @@ -56,7 +56,7 @@ public class ColorExtractorTest { @Test public void ColorExtractor_extractWhenInitialized() { ExtractionType type = mock(Tonal.class); new ColorExtractor(mContext, type); new ColorExtractor(mContext, type, true); // 1 for lock and 1 for system verify(type, times(2)) .extractInto(any(), any(), any(), any()); Loading @@ -83,7 +83,7 @@ public class ColorExtractorTest { outGradientColorsDark.set(colorsExpectedDark); outGradientColorsExtraDark.set(colorsExpectedExtraDark); }; ColorExtractor extractor = new ColorExtractor(mContext, type); ColorExtractor extractor = new ColorExtractor(mContext, type, true); GradientColors colors = extractor.getColors(WallpaperManager.FLAG_SYSTEM, ColorExtractor.TYPE_NORMAL); Loading @@ -98,7 +98,7 @@ public class ColorExtractorTest { public void addOnColorsChangedListener_invokesListener() { ColorExtractor.OnColorsChangedListener mockedListeners = mock(ColorExtractor.OnColorsChangedListener.class); ColorExtractor extractor = new ColorExtractor(mContext, new Tonal(mContext)); ColorExtractor extractor = new ColorExtractor(mContext, new Tonal(mContext), true); extractor.addOnColorsChangedListener(mockedListeners); extractor.onColorsChanged(new WallpaperColors(Color.valueOf(Color.RED), null, null), Loading