Lucas Dupin | c40608c | 2017-04-14 18:33:08 -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 | |
| 17 | package android.app; |
| 18 | |
| 19 | import android.graphics.Color; |
| 20 | import android.os.Parcel; |
| 21 | import android.os.Parcelable; |
| 22 | |
| 23 | import android.util.Pair; |
| 24 | |
Lucas Dupin | ea1fb1e | 2017-04-05 17:39:44 -0700 | [diff] [blame^] | 25 | import java.util.ArrayList; |
Lucas Dupin | c40608c | 2017-04-14 18:33:08 -0700 | [diff] [blame] | 26 | import java.util.List; |
| 27 | |
| 28 | /** |
| 29 | * A class containing information about the colors of a wallpaper. |
| 30 | */ |
| 31 | public final class WallpaperColors implements Parcelable { |
| 32 | |
Lucas Dupin | ea1fb1e | 2017-04-05 17:39:44 -0700 | [diff] [blame^] | 33 | private static final float BRIGHT_LUMINANCE = 0.9f; |
| 34 | private final List<Pair<Color, Integer>> mColors; |
| 35 | private final boolean mSupportsDarkText; |
| 36 | |
Lucas Dupin | c40608c | 2017-04-14 18:33:08 -0700 | [diff] [blame] | 37 | public WallpaperColors(Parcel parcel) { |
Lucas Dupin | ea1fb1e | 2017-04-05 17:39:44 -0700 | [diff] [blame^] | 38 | mColors = new ArrayList<>(); |
| 39 | int count = parcel.readInt(); |
| 40 | for (int i=0; i < count; i++) { |
| 41 | Color color = Color.valueOf(parcel.readInt()); |
| 42 | int weight = parcel.readInt(); |
| 43 | mColors.add(new Pair<>(color, weight)); |
| 44 | } |
| 45 | mSupportsDarkText = parcel.readBoolean(); |
Lucas Dupin | c40608c | 2017-04-14 18:33:08 -0700 | [diff] [blame] | 46 | } |
| 47 | |
| 48 | /** |
| 49 | * Wallpaper color details containing a list of colors and their weights, |
| 50 | * as if it were an histogram. |
| 51 | * This list can be extracted from a bitmap by the Palette API. |
| 52 | * |
| 53 | * Dark text support will be calculated internally based on the histogram. |
| 54 | * |
| 55 | * @param colors list of pairs where each pair contains a color |
| 56 | * and number of occurrences/influence. |
| 57 | */ |
| 58 | public WallpaperColors(List<Pair<Color, Integer>> colors) { |
Lucas Dupin | ea1fb1e | 2017-04-05 17:39:44 -0700 | [diff] [blame^] | 59 | this(colors, calculateDarkTextSupport(colors)); |
Lucas Dupin | c40608c | 2017-04-14 18:33:08 -0700 | [diff] [blame] | 60 | } |
| 61 | |
| 62 | /** |
| 63 | * Wallpaper color details containing a list of colors and their weights, |
| 64 | * as if it were an histogram. |
| 65 | * Explicit dark text support. |
| 66 | * |
| 67 | * @param colors list of pairs where each pair contains a color |
| 68 | * and number of occurrences/influence. |
| 69 | * @param supportsDarkText can have dark text on top or not |
| 70 | */ |
| 71 | public WallpaperColors(List<Pair<Color, Integer>> colors, boolean supportsDarkText) { |
Lucas Dupin | ea1fb1e | 2017-04-05 17:39:44 -0700 | [diff] [blame^] | 72 | if (colors == null) |
| 73 | colors = new ArrayList<>(); |
| 74 | mColors = colors; |
| 75 | mSupportsDarkText = supportsDarkText; |
Lucas Dupin | c40608c | 2017-04-14 18:33:08 -0700 | [diff] [blame] | 76 | } |
| 77 | |
| 78 | public static final Creator<WallpaperColors> CREATOR = new Creator<WallpaperColors>() { |
| 79 | @Override |
| 80 | public WallpaperColors createFromParcel(Parcel in) { |
| 81 | return new WallpaperColors(in); |
| 82 | } |
| 83 | |
| 84 | @Override |
| 85 | public WallpaperColors[] newArray(int size) { |
| 86 | return new WallpaperColors[size]; |
| 87 | } |
| 88 | }; |
| 89 | |
| 90 | @Override |
| 91 | public int describeContents() { |
| 92 | return 0; |
| 93 | } |
| 94 | |
| 95 | @Override |
| 96 | public void writeToParcel(Parcel dest, int flags) { |
Lucas Dupin | ea1fb1e | 2017-04-05 17:39:44 -0700 | [diff] [blame^] | 97 | int count = mColors.size(); |
| 98 | dest.writeInt(count); |
| 99 | for (Pair<Color, Integer> color : mColors) { |
| 100 | dest.writeInt(color.first.toArgb()); |
| 101 | dest.writeInt(color.second); |
| 102 | } |
| 103 | dest.writeBoolean(mSupportsDarkText); |
Lucas Dupin | c40608c | 2017-04-14 18:33:08 -0700 | [diff] [blame] | 104 | } |
| 105 | |
| 106 | /** |
| 107 | * List of colors with their occurrences. The bigger the int, the more relevant the color. |
| 108 | * @return list of colors paired with their weights. |
| 109 | */ |
| 110 | public List<Pair<Color, Integer>> getColors() { |
Lucas Dupin | ea1fb1e | 2017-04-05 17:39:44 -0700 | [diff] [blame^] | 111 | return mColors; |
| 112 | } |
| 113 | |
| 114 | @Override |
| 115 | public boolean equals(Object o) { |
| 116 | if (o == null || getClass() != o.getClass()) { |
| 117 | return false; |
| 118 | } |
| 119 | |
| 120 | WallpaperColors other = (WallpaperColors) o; |
| 121 | return mColors.equals(other.mColors) && mSupportsDarkText == other.mSupportsDarkText; |
| 122 | } |
| 123 | |
| 124 | @Override |
| 125 | public int hashCode() { |
| 126 | return 31 * mColors.hashCode() + (mSupportsDarkText ? 1 : 0); |
Lucas Dupin | c40608c | 2017-04-14 18:33:08 -0700 | [diff] [blame] | 127 | } |
| 128 | |
| 129 | /** |
| 130 | * Whether or not dark text is legible on top of this wallpaper. |
| 131 | * |
| 132 | * @return true if dark text is supported |
| 133 | */ |
| 134 | public boolean supportsDarkText() { |
Lucas Dupin | ea1fb1e | 2017-04-05 17:39:44 -0700 | [diff] [blame^] | 135 | return mSupportsDarkText; |
| 136 | } |
| 137 | |
| 138 | private static boolean calculateDarkTextSupport(List<Pair<Color, Integer>> colors) { |
| 139 | if (colors == null) { |
| 140 | return false; |
| 141 | } |
| 142 | |
| 143 | Pair<Color, Integer> mainColor = null; |
| 144 | |
| 145 | for (Pair<Color, Integer> color : colors) { |
| 146 | if (mainColor == null) { |
| 147 | mainColor = color; |
| 148 | } else if (color.second > mainColor.second) { |
| 149 | mainColor = color; |
| 150 | } |
| 151 | } |
| 152 | return mainColor != null && |
| 153 | mainColor.first.luminance() > BRIGHT_LUMINANCE; |
Lucas Dupin | c40608c | 2017-04-14 18:33:08 -0700 | [diff] [blame] | 154 | } |
| 155 | } |