Lucas Dupin | 65f5658 | 2017-04-27 10:21:41 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2017 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License |
| 15 | */ |
| 16 | |
Lucas Dupin | e2292a9 | 2017-07-06 14:35:30 -0700 | [diff] [blame] | 17 | package com.android.internal.colorextraction; |
Lucas Dupin | 65f5658 | 2017-04-27 10:21:41 -0700 | [diff] [blame] | 18 | |
Lucas Dupin | 75ec379 | 2017-06-29 14:07:18 -0700 | [diff] [blame] | 19 | import android.annotation.NonNull; |
| 20 | import android.annotation.Nullable; |
Lucas Dupin | 65f5658 | 2017-04-27 10:21:41 -0700 | [diff] [blame] | 21 | import android.app.WallpaperColors; |
| 22 | import android.app.WallpaperManager; |
| 23 | import android.content.Context; |
Lucas Dupin | 75ec379 | 2017-06-29 14:07:18 -0700 | [diff] [blame] | 24 | import android.os.Trace; |
Lucas Dupin | 26fb43c | 2017-07-14 11:55:05 -0700 | [diff] [blame] | 25 | import android.os.UserHandle; |
Lucas Dupin | 65f5658 | 2017-04-27 10:21:41 -0700 | [diff] [blame] | 26 | import android.util.Log; |
Lucas Dupin | 7aaa353 | 2017-05-28 08:51:07 -0700 | [diff] [blame] | 27 | import android.util.SparseArray; |
Lucas Dupin | 65f5658 | 2017-04-27 10:21:41 -0700 | [diff] [blame] | 28 | |
Lucas Dupin | e2292a9 | 2017-07-06 14:35:30 -0700 | [diff] [blame] | 29 | import com.android.internal.annotations.VisibleForTesting; |
| 30 | import com.android.internal.colorextraction.types.ExtractionType; |
| 31 | import com.android.internal.colorextraction.types.Tonal; |
Lucas Dupin | 65f5658 | 2017-04-27 10:21:41 -0700 | [diff] [blame] | 32 | |
Lucas Dupin | e17ce52 | 2017-07-17 15:45:06 -0700 | [diff] [blame] | 33 | import java.lang.ref.WeakReference; |
Lucas Dupin | 314d41f | 2017-05-08 15:52:58 -0700 | [diff] [blame] | 34 | import java.util.ArrayList; |
Lucas Dupin | e17ce52 | 2017-07-17 15:45:06 -0700 | [diff] [blame] | 35 | import java.util.Iterator; |
Lucas Dupin | 314d41f | 2017-05-08 15:52:58 -0700 | [diff] [blame] | 36 | |
Lucas Dupin | 65f5658 | 2017-04-27 10:21:41 -0700 | [diff] [blame] | 37 | /** |
| 38 | * Class to process wallpaper colors and generate a tonal palette based on them. |
| 39 | */ |
| 40 | public class ColorExtractor implements WallpaperManager.OnColorsChangedListener { |
Lucas Dupin | 7aaa353 | 2017-05-28 08:51:07 -0700 | [diff] [blame] | 41 | |
| 42 | public static final int TYPE_NORMAL = 0; |
| 43 | public static final int TYPE_DARK = 1; |
| 44 | public static final int TYPE_EXTRA_DARK = 2; |
| 45 | private static final int[] sGradientTypes = new int[]{TYPE_NORMAL, TYPE_DARK, TYPE_EXTRA_DARK}; |
| 46 | |
Lucas Dupin | 65f5658 | 2017-04-27 10:21:41 -0700 | [diff] [blame] | 47 | private static final String TAG = "ColorExtractor"; |
Lucas Dupin | 26fb43c | 2017-07-14 11:55:05 -0700 | [diff] [blame] | 48 | private static final boolean DEBUG = false; |
Lucas Dupin | 7aaa353 | 2017-05-28 08:51:07 -0700 | [diff] [blame] | 49 | |
Lucas Dupin | 8a4522d | 2017-07-26 21:48:06 -0700 | [diff] [blame] | 50 | protected final SparseArray<GradientColors[]> mGradientColors; |
Lucas Dupin | e17ce52 | 2017-07-17 15:45:06 -0700 | [diff] [blame] | 51 | private final ArrayList<WeakReference<OnColorsChangedListener>> mOnColorsChangedListeners; |
Lucas Dupin | 65f5658 | 2017-04-27 10:21:41 -0700 | [diff] [blame] | 52 | private final Context mContext; |
| 53 | private final ExtractionType mExtractionType; |
Lucas Dupin | 8a4522d | 2017-07-26 21:48:06 -0700 | [diff] [blame] | 54 | protected WallpaperColors mSystemColors; |
| 55 | protected WallpaperColors mLockColors; |
Lucas Dupin | 65f5658 | 2017-04-27 10:21:41 -0700 | [diff] [blame] | 56 | |
| 57 | public ColorExtractor(Context context) { |
Lucas Dupin | 6e69c85 | 2017-07-06 16:09:24 -0700 | [diff] [blame] | 58 | this(context, new Tonal(context)); |
Lucas Dupin | 7aaa353 | 2017-05-28 08:51:07 -0700 | [diff] [blame] | 59 | } |
| 60 | |
| 61 | @VisibleForTesting |
| 62 | public ColorExtractor(Context context, ExtractionType extractionType) { |
Lucas Dupin | 65f5658 | 2017-04-27 10:21:41 -0700 | [diff] [blame] | 63 | mContext = context; |
Lucas Dupin | 7aaa353 | 2017-05-28 08:51:07 -0700 | [diff] [blame] | 64 | mExtractionType = extractionType; |
| 65 | |
| 66 | mGradientColors = new SparseArray<>(); |
| 67 | for (int which : new int[] { WallpaperManager.FLAG_LOCK, WallpaperManager.FLAG_SYSTEM}) { |
| 68 | GradientColors[] colors = new GradientColors[sGradientTypes.length]; |
| 69 | mGradientColors.append(which, colors); |
| 70 | for (int type : sGradientTypes) { |
| 71 | colors[type] = new GradientColors(); |
| 72 | } |
| 73 | } |
| 74 | |
Lucas Dupin | 314d41f | 2017-05-08 15:52:58 -0700 | [diff] [blame] | 75 | mOnColorsChangedListeners = new ArrayList<>(); |
Lucas Dupin | c77b71d | 2017-07-05 17:34:41 -0700 | [diff] [blame] | 76 | GradientColors[] systemColors = mGradientColors.get(WallpaperManager.FLAG_SYSTEM); |
| 77 | GradientColors[] lockColors = mGradientColors.get(WallpaperManager.FLAG_LOCK); |
| 78 | |
Lucas Dupin | 65f5658 | 2017-04-27 10:21:41 -0700 | [diff] [blame] | 79 | WallpaperManager wallpaperManager = mContext.getSystemService(WallpaperManager.class); |
Lucas Dupin | 65f5658 | 2017-04-27 10:21:41 -0700 | [diff] [blame] | 80 | if (wallpaperManager == null) { |
| 81 | Log.w(TAG, "Can't listen to color changes!"); |
| 82 | } else { |
| 83 | wallpaperManager.addOnColorsChangedListener(this); |
Lucas Dupin | 7aaa353 | 2017-05-28 08:51:07 -0700 | [diff] [blame] | 84 | |
| 85 | // Initialize all gradients with the current colors |
Lucas Dupin | 75ec379 | 2017-06-29 14:07:18 -0700 | [diff] [blame] | 86 | Trace.beginSection("ColorExtractor#getWallpaperColors"); |
| 87 | mSystemColors = wallpaperManager.getWallpaperColors(WallpaperManager.FLAG_SYSTEM); |
| 88 | mLockColors = wallpaperManager.getWallpaperColors(WallpaperManager.FLAG_LOCK); |
Lucas Dupin | 75ec379 | 2017-06-29 14:07:18 -0700 | [diff] [blame] | 89 | Trace.endSection(); |
Lucas Dupin | 65f5658 | 2017-04-27 10:21:41 -0700 | [diff] [blame] | 90 | } |
Lucas Dupin | c77b71d | 2017-07-05 17:34:41 -0700 | [diff] [blame] | 91 | |
| 92 | // Initialize all gradients with the current colors |
| 93 | extractInto(mSystemColors, |
| 94 | systemColors[TYPE_NORMAL], |
| 95 | systemColors[TYPE_DARK], |
| 96 | systemColors[TYPE_EXTRA_DARK]); |
| 97 | extractInto(mLockColors, |
| 98 | lockColors[TYPE_NORMAL], |
| 99 | lockColors[TYPE_DARK], |
| 100 | lockColors[TYPE_EXTRA_DARK]); |
Lucas Dupin | 65f5658 | 2017-04-27 10:21:41 -0700 | [diff] [blame] | 101 | } |
| 102 | |
Lucas Dupin | 7aaa353 | 2017-05-28 08:51:07 -0700 | [diff] [blame] | 103 | /** |
Lucas Dupin | 78d4aff | 2017-07-17 10:15:30 -0700 | [diff] [blame] | 104 | * Retrieve gradient colors for a specific wallpaper. |
Lucas Dupin | 7aaa353 | 2017-05-28 08:51:07 -0700 | [diff] [blame] | 105 | * |
| 106 | * @param which FLAG_LOCK or FLAG_SYSTEM |
| 107 | * @return colors |
| 108 | */ |
| 109 | @NonNull |
Lucas Dupin | 65f5658 | 2017-04-27 10:21:41 -0700 | [diff] [blame] | 110 | public GradientColors getColors(int which) { |
Lucas Dupin | 78d4aff | 2017-07-17 10:15:30 -0700 | [diff] [blame] | 111 | return getColors(which, TYPE_DARK); |
Lucas Dupin | 7aaa353 | 2017-05-28 08:51:07 -0700 | [diff] [blame] | 112 | } |
| 113 | |
| 114 | /** |
| 115 | * Get current gradient colors for one of the possible gradient types |
| 116 | * |
| 117 | * @param which FLAG_LOCK or FLAG_SYSTEM |
| 118 | * @param type TYPE_NORMAL, TYPE_DARK or TYPE_EXTRA_DARK |
| 119 | * @return colors |
| 120 | */ |
Lucas Dupin | 75ec379 | 2017-06-29 14:07:18 -0700 | [diff] [blame] | 121 | @NonNull |
Lucas Dupin | 7aaa353 | 2017-05-28 08:51:07 -0700 | [diff] [blame] | 122 | public GradientColors getColors(int which, int type) { |
| 123 | if (type != TYPE_NORMAL && type != TYPE_DARK && type != TYPE_EXTRA_DARK) { |
| 124 | throw new IllegalArgumentException( |
| 125 | "type should be TYPE_NORMAL, TYPE_DARK or TYPE_EXTRA_DARK"); |
Lucas Dupin | 65f5658 | 2017-04-27 10:21:41 -0700 | [diff] [blame] | 126 | } |
Lucas Dupin | 7aaa353 | 2017-05-28 08:51:07 -0700 | [diff] [blame] | 127 | if (which != WallpaperManager.FLAG_LOCK && which != WallpaperManager.FLAG_SYSTEM) { |
| 128 | throw new IllegalArgumentException("which should be FLAG_SYSTEM or FLAG_NORMAL"); |
| 129 | } |
Lucas Dupin | 7aaa353 | 2017-05-28 08:51:07 -0700 | [diff] [blame] | 130 | return mGradientColors.get(which)[type]; |
Lucas Dupin | 65f5658 | 2017-04-27 10:21:41 -0700 | [diff] [blame] | 131 | } |
| 132 | |
Lucas Dupin | 75ec379 | 2017-06-29 14:07:18 -0700 | [diff] [blame] | 133 | /** |
| 134 | * Get the last available WallpaperColors without forcing new extraction. |
| 135 | * |
| 136 | * @param which FLAG_LOCK or FLAG_SYSTEM |
| 137 | * @return Last cached colors |
| 138 | */ |
| 139 | @Nullable |
| 140 | public WallpaperColors getWallpaperColors(int which) { |
| 141 | if (which == WallpaperManager.FLAG_LOCK) { |
| 142 | return mLockColors; |
| 143 | } else if (which == WallpaperManager.FLAG_SYSTEM) { |
| 144 | return mSystemColors; |
| 145 | } else { |
| 146 | throw new IllegalArgumentException("Invalid value for which: " + which); |
| 147 | } |
| 148 | } |
| 149 | |
Lucas Dupin | 65f5658 | 2017-04-27 10:21:41 -0700 | [diff] [blame] | 150 | @Override |
| 151 | public void onColorsChanged(WallpaperColors colors, int which) { |
Lucas Dupin | 26fb43c | 2017-07-14 11:55:05 -0700 | [diff] [blame] | 152 | if (DEBUG) { |
| 153 | Log.d(TAG, "New wallpaper colors for " + which + ": " + colors); |
| 154 | } |
Lucas Dupin | 7aaa353 | 2017-05-28 08:51:07 -0700 | [diff] [blame] | 155 | boolean changed = false; |
Lucas Dupin | 65f5658 | 2017-04-27 10:21:41 -0700 | [diff] [blame] | 156 | if ((which & WallpaperManager.FLAG_LOCK) != 0) { |
Lucas Dupin | 75ec379 | 2017-06-29 14:07:18 -0700 | [diff] [blame] | 157 | mLockColors = colors; |
Lucas Dupin | 7aaa353 | 2017-05-28 08:51:07 -0700 | [diff] [blame] | 158 | GradientColors[] lockColors = mGradientColors.get(WallpaperManager.FLAG_LOCK); |
| 159 | extractInto(colors, lockColors[TYPE_NORMAL], lockColors[TYPE_DARK], |
| 160 | lockColors[TYPE_EXTRA_DARK]); |
Lucas Dupin | 7aaa353 | 2017-05-28 08:51:07 -0700 | [diff] [blame] | 161 | changed = true; |
Lucas Dupin | 65f5658 | 2017-04-27 10:21:41 -0700 | [diff] [blame] | 162 | } |
| 163 | if ((which & WallpaperManager.FLAG_SYSTEM) != 0) { |
Lucas Dupin | 75ec379 | 2017-06-29 14:07:18 -0700 | [diff] [blame] | 164 | mSystemColors = colors; |
Lucas Dupin | 7aaa353 | 2017-05-28 08:51:07 -0700 | [diff] [blame] | 165 | GradientColors[] systemColors = mGradientColors.get(WallpaperManager.FLAG_SYSTEM); |
| 166 | extractInto(colors, systemColors[TYPE_NORMAL], systemColors[TYPE_DARK], |
| 167 | systemColors[TYPE_EXTRA_DARK]); |
| 168 | changed = true; |
| 169 | } |
| 170 | |
| 171 | if (changed) { |
| 172 | triggerColorsChanged(which); |
Lucas Dupin | 65f5658 | 2017-04-27 10:21:41 -0700 | [diff] [blame] | 173 | } |
| 174 | } |
| 175 | |
Lucas Dupin | 1ead7fc | 2017-05-24 14:14:44 -0700 | [diff] [blame] | 176 | protected void triggerColorsChanged(int which) { |
Lucas Dupin | e17ce52 | 2017-07-17 15:45:06 -0700 | [diff] [blame] | 177 | ArrayList<WeakReference<OnColorsChangedListener>> references = |
| 178 | new ArrayList<>(mOnColorsChangedListeners); |
| 179 | final int size = references.size(); |
| 180 | for (int i = 0; i < size; i++) { |
| 181 | final WeakReference<OnColorsChangedListener> weakReference = references.get(i); |
| 182 | final OnColorsChangedListener listener = weakReference.get(); |
| 183 | if (listener == null) { |
| 184 | mOnColorsChangedListeners.remove(weakReference); |
| 185 | } else { |
| 186 | listener.onColorsChanged(this, which); |
| 187 | } |
Lucas Dupin | 7aaa353 | 2017-05-28 08:51:07 -0700 | [diff] [blame] | 188 | } |
| 189 | } |
| 190 | |
| 191 | private void extractInto(WallpaperColors inWallpaperColors, |
| 192 | GradientColors outGradientColorsNormal, GradientColors outGradientColorsDark, |
| 193 | GradientColors outGradientColorsExtraDark) { |
Lucas Dupin | c77b71d | 2017-07-05 17:34:41 -0700 | [diff] [blame] | 194 | mExtractionType.extractInto(inWallpaperColors, outGradientColorsNormal, |
Lucas Dupin | 7aaa353 | 2017-05-28 08:51:07 -0700 | [diff] [blame] | 195 | outGradientColorsDark, outGradientColorsExtraDark); |
Lucas Dupin | 65f5658 | 2017-04-27 10:21:41 -0700 | [diff] [blame] | 196 | } |
| 197 | |
| 198 | public void destroy() { |
| 199 | WallpaperManager wallpaperManager = mContext.getSystemService(WallpaperManager.class); |
| 200 | if (wallpaperManager != null) { |
| 201 | wallpaperManager.removeOnColorsChangedListener(this); |
| 202 | } |
| 203 | } |
| 204 | |
Lucas Dupin | 7aaa353 | 2017-05-28 08:51:07 -0700 | [diff] [blame] | 205 | public void addOnColorsChangedListener(@NonNull OnColorsChangedListener listener) { |
Lucas Dupin | e17ce52 | 2017-07-17 15:45:06 -0700 | [diff] [blame] | 206 | mOnColorsChangedListeners.add(new WeakReference<>(listener)); |
Lucas Dupin | 314d41f | 2017-05-08 15:52:58 -0700 | [diff] [blame] | 207 | } |
| 208 | |
Lucas Dupin | 7aaa353 | 2017-05-28 08:51:07 -0700 | [diff] [blame] | 209 | public void removeOnColorsChangedListener(@NonNull OnColorsChangedListener listener) { |
Lucas Dupin | e17ce52 | 2017-07-17 15:45:06 -0700 | [diff] [blame] | 210 | ArrayList<WeakReference<OnColorsChangedListener>> references = |
| 211 | new ArrayList<>(mOnColorsChangedListeners); |
| 212 | final int size = references.size(); |
| 213 | for (int i = 0; i < size; i++) { |
| 214 | final WeakReference<OnColorsChangedListener> weakReference = references.get(i); |
| 215 | if (weakReference.get() == listener) { |
| 216 | mOnColorsChangedListeners.remove(weakReference); |
| 217 | break; |
| 218 | } |
| 219 | } |
Lucas Dupin | 314d41f | 2017-05-08 15:52:58 -0700 | [diff] [blame] | 220 | } |
| 221 | |
Lucas Dupin | 65f5658 | 2017-04-27 10:21:41 -0700 | [diff] [blame] | 222 | public static class GradientColors { |
Lucas Dupin | c77b71d | 2017-07-05 17:34:41 -0700 | [diff] [blame] | 223 | private int mMainColor; |
| 224 | private int mSecondaryColor; |
Lucas Dupin | 65f5658 | 2017-04-27 10:21:41 -0700 | [diff] [blame] | 225 | private boolean mSupportsDarkText; |
| 226 | |
| 227 | public void setMainColor(int mainColor) { |
| 228 | mMainColor = mainColor; |
| 229 | } |
| 230 | |
| 231 | public void setSecondaryColor(int secondaryColor) { |
| 232 | mSecondaryColor = secondaryColor; |
| 233 | } |
| 234 | |
| 235 | public void setSupportsDarkText(boolean supportsDarkText) { |
| 236 | mSupportsDarkText = supportsDarkText; |
| 237 | } |
| 238 | |
| 239 | public void set(GradientColors other) { |
| 240 | mMainColor = other.mMainColor; |
| 241 | mSecondaryColor = other.mSecondaryColor; |
| 242 | mSupportsDarkText = other.mSupportsDarkText; |
| 243 | } |
| 244 | |
| 245 | public int getMainColor() { |
| 246 | return mMainColor; |
| 247 | } |
| 248 | |
| 249 | public int getSecondaryColor() { |
| 250 | return mSecondaryColor; |
| 251 | } |
| 252 | |
| 253 | public boolean supportsDarkText() { |
| 254 | return mSupportsDarkText; |
| 255 | } |
| 256 | |
| 257 | @Override |
| 258 | public boolean equals(Object o) { |
| 259 | if (o == null || o.getClass() != getClass()) { |
| 260 | return false; |
| 261 | } |
| 262 | GradientColors other = (GradientColors) o; |
| 263 | return other.mMainColor == mMainColor && |
| 264 | other.mSecondaryColor == mSecondaryColor && |
| 265 | other.mSupportsDarkText == mSupportsDarkText; |
| 266 | } |
| 267 | |
| 268 | @Override |
| 269 | public int hashCode() { |
| 270 | int code = mMainColor; |
| 271 | code = 31 * code + mSecondaryColor; |
| 272 | code = 31 * code + (mSupportsDarkText ? 0 : 1); |
| 273 | return code; |
| 274 | } |
Lucas Dupin | 7aaa353 | 2017-05-28 08:51:07 -0700 | [diff] [blame] | 275 | |
| 276 | @Override |
| 277 | public String toString() { |
| 278 | return "GradientColors(" + Integer.toHexString(mMainColor) + ", " |
| 279 | + Integer.toHexString(mSecondaryColor) + ")"; |
| 280 | } |
Lucas Dupin | 65f5658 | 2017-04-27 10:21:41 -0700 | [diff] [blame] | 281 | } |
| 282 | |
| 283 | public interface OnColorsChangedListener { |
Lucas Dupin | 7aaa353 | 2017-05-28 08:51:07 -0700 | [diff] [blame] | 284 | void onColorsChanged(ColorExtractor colorExtractor, int which); |
Lucas Dupin | 65f5658 | 2017-04-27 10:21:41 -0700 | [diff] [blame] | 285 | } |
Lucas Dupin | 1ead7fc | 2017-05-24 14:14:44 -0700 | [diff] [blame] | 286 | } |