2 * Copyright (C) 2017 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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
17 package com.android.systemui.colorextraction;
19 import android.app.WallpaperColors;
20 import android.app.WallpaperManager;
21 import android.content.Context;
22 import android.graphics.Color;
23 import android.os.UserHandle;
25 import com.android.internal.annotations.VisibleForTesting;
26 import com.android.internal.colorextraction.ColorExtractor;
27 import com.android.internal.colorextraction.types.ExtractionType;
28 import com.android.internal.colorextraction.types.Tonal;
29 import com.android.keyguard.KeyguardUpdateMonitor;
30 import com.android.systemui.Dumpable;
31 import com.android.systemui.statusbar.policy.ConfigurationController;
33 import java.io.FileDescriptor;
34 import java.io.PrintWriter;
35 import java.util.Arrays;
37 import javax.inject.Inject;
38 import javax.inject.Singleton;
41 * ColorExtractor aware of wallpaper visibility
44 public class SysuiColorExtractor extends ColorExtractor implements Dumpable,
45 ConfigurationController.ConfigurationListener {
46 private static final String TAG = "SysuiColorExtractor";
47 private final Tonal mTonal;
48 private boolean mHasMediaArtwork;
49 private final GradientColors mNeutralColorsLock;
50 private final GradientColors mBackdropColors;
53 public SysuiColorExtractor(Context context, ConfigurationController configurationController) {
54 this(context, new Tonal(context), configurationController,
55 context.getSystemService(WallpaperManager.class), false /* immediately */);
59 public SysuiColorExtractor(Context context, ExtractionType type,
60 ConfigurationController configurationController,
61 WallpaperManager wallpaperManager, boolean immediately) {
62 super(context, type, immediately, wallpaperManager);
63 mTonal = type instanceof Tonal ? (Tonal) type : new Tonal(context);
64 mNeutralColorsLock = new GradientColors();
65 configurationController.addCallback(this);
67 mBackdropColors = new GradientColors();
68 mBackdropColors.setMainColor(Color.BLACK);
70 // Listen to all users instead of only the current one.
71 wallpaperManager.removeOnColorsChangedListener(this);
72 wallpaperManager.addOnColorsChangedListener(this, null /* handler */,
77 protected void extractWallpaperColors() {
78 super.extractWallpaperColors();
79 // mTonal is final but this method will be invoked by the base class during its ctor.
80 if (mTonal == null || mNeutralColorsLock == null) {
83 mTonal.applyFallback(mLockColors == null ? mSystemColors : mLockColors, mNeutralColorsLock);
87 public void onColorsChanged(WallpaperColors colors, int which, int userId) {
88 if (userId != KeyguardUpdateMonitor.getCurrentUser()) {
89 // Colors do not belong to current user, ignoring.
92 if ((which & WallpaperManager.FLAG_LOCK) != 0) {
93 mTonal.applyFallback(colors, mNeutralColorsLock);
95 super.onColorsChanged(colors, which);
99 public void onUiModeChanged() {
100 extractWallpaperColors();
101 triggerColorsChanged(WallpaperManager.FLAG_SYSTEM | WallpaperManager.FLAG_LOCK);
105 public GradientColors getColors(int which, int type) {
106 if (mHasMediaArtwork && (which & WallpaperManager.FLAG_LOCK) != 0) {
107 return mBackdropColors;
109 return super.getColors(which, type);
113 * Colors that should be using for scrims.
116 * - A light gray if the wallpaper is light
117 * - A dark gray if the wallpaper is very dark or we're in night mode.
120 public GradientColors getNeutralColors() {
121 return mHasMediaArtwork ? mBackdropColors : mNeutralColorsLock;
124 public void setHasMediaArtwork(boolean hasBackdrop) {
125 if (mHasMediaArtwork != hasBackdrop) {
126 mHasMediaArtwork = hasBackdrop;
127 triggerColorsChanged(WallpaperManager.FLAG_LOCK);
132 public void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
133 pw.println("SysuiColorExtractor:");
135 pw.println(" Current wallpaper colors:");
136 pw.println(" system: " + mSystemColors);
137 pw.println(" lock: " + mLockColors);
139 GradientColors[] system = mGradientColors.get(WallpaperManager.FLAG_SYSTEM);
140 GradientColors[] lock = mGradientColors.get(WallpaperManager.FLAG_LOCK);
141 pw.println(" Gradients:");
142 pw.println(" system: " + Arrays.toString(system));
143 pw.println(" lock: " + Arrays.toString(lock));
144 pw.println(" Neutral colors: " + mNeutralColorsLock);
145 pw.println(" Has media backdrop: " + mHasMediaArtwork);