Merge "Applying API changes on WallpaperColors." into ub-launcher3-dorval-polish
This commit is contained in:
committed by
Android (Google) Code Review
commit
092e856498
@@ -15,29 +15,40 @@
|
|||||||
*/
|
*/
|
||||||
package com.android.launcher3.compat;
|
package com.android.launcher3.compat;
|
||||||
|
|
||||||
import android.util.SparseIntArray;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* A compatibility layer around platform implementation of WallpaperColors
|
* A compatibility layer around platform implementation of WallpaperColors
|
||||||
*/
|
*/
|
||||||
public class WallpaperColorsCompat {
|
public class WallpaperColorsCompat {
|
||||||
|
|
||||||
private final SparseIntArray mColors;
|
public static final int HINT_SUPPORTS_DARK_TEXT = 0x1;
|
||||||
private final boolean mSupportsDarkText;
|
|
||||||
|
|
||||||
public WallpaperColorsCompat(SparseIntArray colors, boolean supportsDarkText) {
|
private final int mPrimaryColor;
|
||||||
mColors = colors;
|
private final int mSecondaryColor;
|
||||||
mSupportsDarkText = supportsDarkText;
|
private final int mTertiaryColor;
|
||||||
|
private final int mColorHints;
|
||||||
|
|
||||||
|
public WallpaperColorsCompat(int primaryColor, int secondaryColor, int tertiaryColor,
|
||||||
|
int colorHints) {
|
||||||
|
mPrimaryColor = primaryColor;
|
||||||
|
mSecondaryColor = secondaryColor;
|
||||||
|
mTertiaryColor = tertiaryColor;
|
||||||
|
mColorHints = colorHints;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
public int getPrimaryColor() {
|
||||||
* A map of color code to their occurrences. The bigger the int, the more relevant the color.
|
return mPrimaryColor;
|
||||||
*/
|
|
||||||
public SparseIntArray getColors() {
|
|
||||||
return mColors;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean supportsDarkText() {
|
public int getSecondaryColor() {
|
||||||
return mSupportsDarkText;
|
return mSecondaryColor;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public int getTertiaryColor() {
|
||||||
|
return mTertiaryColor;
|
||||||
|
}
|
||||||
|
|
||||||
|
public int getColorHints() {
|
||||||
|
return mColorHints;
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -15,10 +15,6 @@
|
|||||||
*/
|
*/
|
||||||
package com.android.launcher3.compat;
|
package com.android.launcher3.compat;
|
||||||
|
|
||||||
import static android.app.WallpaperManager.FLAG_SYSTEM;
|
|
||||||
|
|
||||||
import static com.android.launcher3.Utilities.getDevicePrefs;
|
|
||||||
|
|
||||||
import android.app.WallpaperInfo;
|
import android.app.WallpaperInfo;
|
||||||
import android.app.WallpaperManager;
|
import android.app.WallpaperManager;
|
||||||
import android.app.job.JobInfo;
|
import android.app.job.JobInfo;
|
||||||
@@ -45,12 +41,17 @@ import android.support.annotation.Nullable;
|
|||||||
import android.support.v7.graphics.Palette;
|
import android.support.v7.graphics.Palette;
|
||||||
import android.util.Log;
|
import android.util.Log;
|
||||||
import android.util.Pair;
|
import android.util.Pair;
|
||||||
import android.util.SparseIntArray;
|
|
||||||
|
|
||||||
import com.android.launcher3.Utilities;
|
import com.android.launcher3.Utilities;
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
|
import java.util.Collections;
|
||||||
|
import java.util.Comparator;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
import static android.app.WallpaperManager.FLAG_SYSTEM;
|
||||||
|
import static com.android.launcher3.Utilities.getDevicePrefs;
|
||||||
|
|
||||||
public class WallpaperManagerCompatVL extends WallpaperManagerCompat {
|
public class WallpaperManagerCompatVL extends WallpaperManagerCompat {
|
||||||
|
|
||||||
@@ -154,11 +155,12 @@ public class WallpaperManagerCompatVL extends WallpaperManagerCompat {
|
|||||||
return Pair.create(wallpaperId, null);
|
return Pair.create(wallpaperId, null);
|
||||||
}
|
}
|
||||||
|
|
||||||
SparseIntArray colorsToOccurrences = new SparseIntArray((parts.length - 2) / 2);
|
int primary = parts.length > 2 ? Integer.parseInt(parts[2]) : 0;
|
||||||
for (int i = 2; i < parts.length; i += 2) {
|
int secondary = parts.length > 3 ? Integer.parseInt(parts[3]) : 0;
|
||||||
colorsToOccurrences.put(Integer.parseInt(parts[i]), Integer.parseInt(parts[i + 1]));
|
int tertiary = parts.length > 4 ? Integer.parseInt(parts[4]) : 0;
|
||||||
}
|
|
||||||
return Pair.create(wallpaperId, new WallpaperColorsCompat(colorsToOccurrences, false));
|
return Pair.create(wallpaperId, new WallpaperColorsCompat(primary, secondary, tertiary,
|
||||||
|
0 /* hints */));
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -262,12 +264,22 @@ public class WallpaperManagerCompatVL extends WallpaperManagerCompat {
|
|||||||
bitmap.recycle();
|
bitmap.recycle();
|
||||||
|
|
||||||
StringBuilder builder = new StringBuilder(value);
|
StringBuilder builder = new StringBuilder(value);
|
||||||
|
List<Pair<Integer,Integer>> colorsToOccurrences = new ArrayList<>();
|
||||||
for (Palette.Swatch swatch : palette.getSwatches()) {
|
for (Palette.Swatch swatch : palette.getSwatches()) {
|
||||||
builder.append(',')
|
colorsToOccurrences.add(new Pair(swatch.getRgb(), swatch.getPopulation()));
|
||||||
.append(swatch.getRgb())
|
|
||||||
.append(',')
|
|
||||||
.append(swatch.getPopulation());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Collections.sort(colorsToOccurrences, new Comparator<Pair<Integer, Integer>>() {
|
||||||
|
@Override
|
||||||
|
public int compare(Pair<Integer, Integer> a, Pair<Integer, Integer> b) {
|
||||||
|
return b.second - a.second;
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
for (int i=0; i < Math.min(3, colorsToOccurrences.size()); i++) {
|
||||||
|
builder.append(',').append(colorsToOccurrences.get(i).first);
|
||||||
|
}
|
||||||
|
|
||||||
value = builder.toString();
|
value = builder.toString();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -22,13 +22,10 @@ import android.graphics.Color;
|
|||||||
import android.os.Build;
|
import android.os.Build;
|
||||||
import android.support.annotation.Nullable;
|
import android.support.annotation.Nullable;
|
||||||
import android.util.Log;
|
import android.util.Log;
|
||||||
import android.util.Pair;
|
|
||||||
import android.util.SparseIntArray;
|
|
||||||
|
|
||||||
import java.lang.reflect.InvocationHandler;
|
import java.lang.reflect.InvocationHandler;
|
||||||
import java.lang.reflect.Method;
|
import java.lang.reflect.Method;
|
||||||
import java.lang.reflect.Proxy;
|
import java.lang.reflect.Proxy;
|
||||||
import java.util.List;
|
|
||||||
|
|
||||||
@TargetApi(Build.VERSION_CODES.O)
|
@TargetApi(Build.VERSION_CODES.O)
|
||||||
public class WallpaperManagerCompatVOMR1 extends WallpaperManagerCompat {
|
public class WallpaperManagerCompatVOMR1 extends WallpaperManagerCompat {
|
||||||
@@ -41,8 +38,10 @@ public class WallpaperManagerCompatVOMR1 extends WallpaperManagerCompat {
|
|||||||
private final Method mAddOCLMethod;
|
private final Method mAddOCLMethod;
|
||||||
|
|
||||||
private final Method mWCGetMethod;
|
private final Method mWCGetMethod;
|
||||||
private final Method mWCGetColorsMethod;
|
private final Method mWCGetPrimaryColorMethod;
|
||||||
private final Method mWCSupportsDarkTextMethod;
|
private final Method mWCGetSecondaryColorMethod;
|
||||||
|
private final Method mWCGetTertiaryColorMethod;
|
||||||
|
private final Method mWCColorHintsMethod;
|
||||||
|
|
||||||
WallpaperManagerCompatVOMR1(Context context) throws Exception {
|
WallpaperManagerCompatVOMR1(Context context) throws Exception {
|
||||||
mWm = context.getSystemService(WallpaperManager.class);
|
mWm = context.getSystemService(WallpaperManager.class);
|
||||||
@@ -53,8 +52,10 @@ public class WallpaperManagerCompatVOMR1 extends WallpaperManagerCompat {
|
|||||||
|
|
||||||
mWCGetMethod = WallpaperManager.class.getDeclaredMethod("getWallpaperColors", int.class);
|
mWCGetMethod = WallpaperManager.class.getDeclaredMethod("getWallpaperColors", int.class);
|
||||||
Class wallpaperColorsClass = mWCGetMethod.getReturnType();
|
Class wallpaperColorsClass = mWCGetMethod.getReturnType();
|
||||||
mWCGetColorsMethod = wallpaperColorsClass.getDeclaredMethod("getColors");
|
mWCGetPrimaryColorMethod = wallpaperColorsClass.getDeclaredMethod("getPrimaryColor");
|
||||||
mWCSupportsDarkTextMethod = wallpaperColorsClass.getDeclaredMethod("supportsDarkText");
|
mWCGetSecondaryColorMethod = wallpaperColorsClass.getDeclaredMethod("getSecondaryColor");
|
||||||
|
mWCGetTertiaryColorMethod = wallpaperColorsClass.getDeclaredMethod("getTertiaryColor");
|
||||||
|
mWCColorHintsMethod = wallpaperColorsClass.getDeclaredMethod("getColorHints");
|
||||||
}
|
}
|
||||||
|
|
||||||
@Nullable
|
@Nullable
|
||||||
@@ -98,12 +99,13 @@ public class WallpaperManagerCompatVOMR1 extends WallpaperManagerCompat {
|
|||||||
if (colors == null) {
|
if (colors == null) {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
List<Pair<Color, Integer>> list = (List) mWCGetColorsMethod.invoke(colors);
|
Color primary = (Color) mWCGetPrimaryColorMethod.invoke(colors);
|
||||||
boolean supportsDarkText = (Boolean) mWCSupportsDarkTextMethod.invoke(colors);
|
Color secondary = (Color) mWCGetSecondaryColorMethod.invoke(colors);
|
||||||
SparseIntArray colorMap = new SparseIntArray(list.size());
|
Color tertiary = (Color) mWCGetTertiaryColorMethod.invoke(colors);
|
||||||
for (Pair<Color, Integer> color : list) {
|
int primaryVal = primary != null ? primary.toArgb() : 0;
|
||||||
colorMap.put(color.first.toArgb(), color.second);
|
int secondaryVal = secondary != null ? secondary.toArgb() : 0;
|
||||||
}
|
int tertiaryVal = tertiary != null ? tertiary.toArgb() : 0;
|
||||||
return new WallpaperColorsCompat(colorMap, supportsDarkText);
|
int colorHints = (Integer) mWCColorHintsMethod.invoke(colors);
|
||||||
|
return new WallpaperColorsCompat(primaryVal, secondaryVal, tertiaryVal, colorHints);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -24,15 +24,12 @@ import android.support.v4.graphics.ColorUtils;
|
|||||||
import android.util.Log;
|
import android.util.Log;
|
||||||
import android.util.Pair;
|
import android.util.Pair;
|
||||||
import android.util.Range;
|
import android.util.Range;
|
||||||
import android.util.SparseIntArray;
|
|
||||||
|
|
||||||
import com.android.launcher3.R;
|
import com.android.launcher3.R;
|
||||||
import com.android.launcher3.Utilities;
|
import com.android.launcher3.Utilities;
|
||||||
import com.android.launcher3.compat.WallpaperColorsCompat;
|
import com.android.launcher3.compat.WallpaperColorsCompat;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.LinkedList;
|
||||||
import java.util.Collections;
|
|
||||||
import java.util.Comparator;
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -52,20 +49,18 @@ public class ColorExtractionAlgorithm {
|
|||||||
private static final float FIT_WEIGHT_S = 1.0f;
|
private static final float FIT_WEIGHT_S = 1.0f;
|
||||||
private static final float FIT_WEIGHT_L = 10.0f;
|
private static final float FIT_WEIGHT_L = 10.0f;
|
||||||
|
|
||||||
// When extracting the main color, only consider colors
|
|
||||||
// present in at least MIN_COLOR_OCCURRENCE of the image
|
|
||||||
private static final float MIN_COLOR_OCCURRENCE = 0.1f;
|
|
||||||
|
|
||||||
// Temporary variable to avoid allocations
|
// Temporary variable to avoid allocations
|
||||||
private final float[] mTmpHSL = new float[3];
|
private float[] mTmpHSL = new float[3];
|
||||||
|
|
||||||
public @Nullable Pair<Integer, Integer> extractInto(WallpaperColorsCompat wallpaperColors) {
|
public @Nullable Pair<Integer, Integer> extractInto(WallpaperColorsCompat inWallpaperColors) {
|
||||||
if (wallpaperColors == null) {
|
if (inWallpaperColors == null) {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
SparseIntArray colorsArray = wallpaperColors.getColors();
|
final List<Integer> mainColors = getMainColors(inWallpaperColors);
|
||||||
if (colorsArray.size() == 0) {
|
final int mainColorsSize = mainColors.size();
|
||||||
|
|
||||||
|
if (mainColorsSize == 0) {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
// Tonal is not really a sort, it takes a color from the extracted
|
// Tonal is not really a sort, it takes a color from the extracted
|
||||||
@@ -73,35 +68,17 @@ public class ColorExtractionAlgorithm {
|
|||||||
// palettes. The best fit is tweaked to be closer to the source color
|
// palettes. The best fit is tweaked to be closer to the source color
|
||||||
// and replaces the original palette
|
// and replaces the original palette
|
||||||
|
|
||||||
List<Pair<Integer, Integer>> colors = new ArrayList<>(colorsArray.size());
|
// Get the most preeminent, non-blacklisted color.
|
||||||
for (int i = colorsArray.size() - 1; i >= 0; i--) {
|
Integer bestColor = 0;
|
||||||
colors.add(Pair.create(colorsArray.keyAt(i), colorsArray.valueAt(i)));
|
final float[] hsl = new float[3];
|
||||||
}
|
for (int i = 0; i < mainColorsSize; i++) {
|
||||||
|
final int colorValue = mainColors.get(i);
|
||||||
// First find the most representative color in the image
|
|
||||||
populationSort(colors);
|
|
||||||
// Calculate total
|
|
||||||
int total = 0;
|
|
||||||
for (Pair<Integer, Integer> weightedColor : colors) {
|
|
||||||
total += weightedColor.second;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Get bright colors that occur often enough in this image
|
|
||||||
Pair<Integer, Integer> bestColor = null;
|
|
||||||
float[] hsl = new float[3];
|
|
||||||
for (Pair<Integer, Integer> weightedColor : colors) {
|
|
||||||
float colorOccurrence = weightedColor.second / (float) total;
|
|
||||||
if (colorOccurrence < MIN_COLOR_OCCURRENCE) {
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
|
|
||||||
int colorValue = weightedColor.first;
|
|
||||||
ColorUtils.RGBToHSL(Color.red(colorValue), Color.green(colorValue),
|
ColorUtils.RGBToHSL(Color.red(colorValue), Color.green(colorValue),
|
||||||
Color.blue(colorValue), hsl);
|
Color.blue(colorValue), hsl);
|
||||||
|
|
||||||
// Stop when we find a color that meets our criteria
|
// Stop when we find a color that meets our criteria
|
||||||
if (!isBlacklisted(hsl)) {
|
if (!isBlacklisted(hsl)) {
|
||||||
bestColor = weightedColor;
|
bestColor = colorValue;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -111,7 +88,7 @@ public class ColorExtractionAlgorithm {
|
|||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
int colorValue = bestColor.first;
|
int colorValue = bestColor;
|
||||||
ColorUtils.RGBToHSL(Color.red(colorValue), Color.green(colorValue), Color.blue(colorValue),
|
ColorUtils.RGBToHSL(Color.red(colorValue), Color.green(colorValue), Color.blue(colorValue),
|
||||||
hsl);
|
hsl);
|
||||||
|
|
||||||
@@ -121,7 +98,6 @@ public class ColorExtractionAlgorithm {
|
|||||||
|
|
||||||
// Find the palette that contains the closest color
|
// Find the palette that contains the closest color
|
||||||
TonalPalette palette = findTonalPalette(hsl[0]);
|
TonalPalette palette = findTonalPalette(hsl[0]);
|
||||||
|
|
||||||
if (palette == null) {
|
if (palette == null) {
|
||||||
Log.w(TAG, "Could not find a tonal palette!");
|
Log.w(TAG, "Could not find a tonal palette!");
|
||||||
return null;
|
return null;
|
||||||
@@ -140,8 +116,7 @@ public class ColorExtractionAlgorithm {
|
|||||||
float[] s = fit(palette.s, hsl[1], fitIndex, 0.0f, 1.0f);
|
float[] s = fit(palette.s, hsl[1], fitIndex, 0.0f, 1.0f);
|
||||||
float[] l = fit(palette.l, hsl[2], fitIndex, 0.0f, 1.0f);
|
float[] l = fit(palette.l, hsl[2], fitIndex, 0.0f, 1.0f);
|
||||||
|
|
||||||
final int textInversionIndex = h.length - 3;
|
// Normal colors:
|
||||||
|
|
||||||
// best fit + a 2 colors offset
|
// best fit + a 2 colors offset
|
||||||
int primaryIndex = fitIndex;
|
int primaryIndex = fitIndex;
|
||||||
int secondaryIndex = primaryIndex + (primaryIndex >= 2 ? -2 : 2);
|
int secondaryIndex = primaryIndex + (primaryIndex >= 2 ? -2 : 2);
|
||||||
@@ -172,15 +147,6 @@ public class ColorExtractionAlgorithm {
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
private static void populationSort(@NonNull List<Pair<Integer, Integer>> wallpaperColors) {
|
|
||||||
Collections.sort(wallpaperColors, new Comparator<Pair<Integer, Integer>>() {
|
|
||||||
@Override
|
|
||||||
public int compare(Pair<Integer, Integer> a, Pair<Integer, Integer> b) {
|
|
||||||
return b.second - a.second;
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Offsets all colors by a delta, clamping values that go beyond what's
|
* Offsets all colors by a delta, clamping values that go beyond what's
|
||||||
* supported on the color space.
|
* supported on the color space.
|
||||||
@@ -234,7 +200,9 @@ public class ColorExtractionAlgorithm {
|
|||||||
TonalPalette best = null;
|
TonalPalette best = null;
|
||||||
float error = Float.POSITIVE_INFINITY;
|
float error = Float.POSITIVE_INFINITY;
|
||||||
|
|
||||||
for (TonalPalette candidate : TONAL_PALETTES) {
|
for (int i = 0; i < TONAL_PALETTES.length; i++) {
|
||||||
|
final TonalPalette candidate = TONAL_PALETTES[i];
|
||||||
|
|
||||||
if (h >= candidate.minHue && h <= candidate.maxHue) {
|
if (h >= candidate.minHue && h <= candidate.maxHue) {
|
||||||
best = candidate;
|
best = candidate;
|
||||||
break;
|
break;
|
||||||
@@ -757,4 +725,18 @@ public class ColorExtractionAlgorithm {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private static List<Integer> getMainColors(WallpaperColorsCompat wallpaperColors) {
|
||||||
|
LinkedList<Integer> colors = new LinkedList<>();
|
||||||
|
if (wallpaperColors.getPrimaryColor() != 0) {
|
||||||
|
colors.add(wallpaperColors.getPrimaryColor());
|
||||||
|
}
|
||||||
|
if (wallpaperColors.getSecondaryColor() != 0) {
|
||||||
|
colors.add(wallpaperColors.getSecondaryColor());
|
||||||
|
}
|
||||||
|
if (wallpaperColors.getTertiaryColor() != 0) {
|
||||||
|
colors.add(wallpaperColors.getTertiaryColor());
|
||||||
|
}
|
||||||
|
return colors;
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -78,7 +78,9 @@ public class WallpaperColorInfo implements WallpaperManagerCompat.OnColorsChange
|
|||||||
mMainColor = FALLBACK_COLOR;
|
mMainColor = FALLBACK_COLOR;
|
||||||
mSecondaryColor = FALLBACK_COLOR;
|
mSecondaryColor = FALLBACK_COLOR;
|
||||||
}
|
}
|
||||||
mSupportsDarkText = wallpaperColors != null ? wallpaperColors.supportsDarkText() : false;
|
mSupportsDarkText = wallpaperColors != null
|
||||||
|
? (wallpaperColors.getColorHints()
|
||||||
|
& WallpaperColorsCompat.HINT_SUPPORTS_DARK_TEXT) > 0 : false;
|
||||||
float[] hsl = new float[3];
|
float[] hsl = new float[3];
|
||||||
ColorUtils.colorToHSL(mMainColor, hsl);
|
ColorUtils.colorToHSL(mMainColor, hsl);
|
||||||
mIsDark = hsl[2] < 0.2f;
|
mIsDark = hsl[2] < 0.2f;
|
||||||
|
|||||||
Reference in New Issue
Block a user