import android.app.WallpaperColors;
import android.app.WallpaperManager;
import android.content.Context;
-import android.os.Trace;
+import android.os.AsyncTask;
import android.util.Log;
import android.util.SparseArray;
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;
}
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],
@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());
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);
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),