OSDN Git Service

Merge "docs: Add documentation for equals() method" into qt-dev am: 732a127636
[android-x86/frameworks-base.git] / packages / SystemUI / src / com / android / systemui / colorextraction / SysuiColorExtractor.java
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 com.android.systemui.colorextraction;
18
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;
24
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;
32
33 import java.io.FileDescriptor;
34 import java.io.PrintWriter;
35 import java.util.Arrays;
36
37 import javax.inject.Inject;
38 import javax.inject.Singleton;
39
40 /**
41  * ColorExtractor aware of wallpaper visibility
42  */
43 @Singleton
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;
51
52     @Inject
53     public SysuiColorExtractor(Context context, ConfigurationController configurationController) {
54         this(context, new Tonal(context), configurationController,
55                 context.getSystemService(WallpaperManager.class), false /* immediately */);
56     }
57
58     @VisibleForTesting
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);
66
67         mBackdropColors = new GradientColors();
68         mBackdropColors.setMainColor(Color.BLACK);
69
70         // Listen to all users instead of only the current one.
71         wallpaperManager.removeOnColorsChangedListener(this);
72         wallpaperManager.addOnColorsChangedListener(this, null /* handler */,
73                 UserHandle.USER_ALL);
74     }
75
76     @Override
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) {
81             return;
82         }
83         mTonal.applyFallback(mLockColors == null ? mSystemColors : mLockColors, mNeutralColorsLock);
84     }
85
86     @Override
87     public void onColorsChanged(WallpaperColors colors, int which, int userId) {
88         if (userId != KeyguardUpdateMonitor.getCurrentUser()) {
89             // Colors do not belong to current user, ignoring.
90             return;
91         }
92         if ((which & WallpaperManager.FLAG_LOCK) != 0) {
93             mTonal.applyFallback(colors, mNeutralColorsLock);
94         }
95         super.onColorsChanged(colors, which);
96     }
97
98     @Override
99     public void onUiModeChanged() {
100         extractWallpaperColors();
101         triggerColorsChanged(WallpaperManager.FLAG_SYSTEM | WallpaperManager.FLAG_LOCK);
102     }
103
104     @Override
105     public GradientColors getColors(int which, int type) {
106         if (mHasMediaArtwork && (which & WallpaperManager.FLAG_LOCK) != 0) {
107             return mBackdropColors;
108         }
109         return super.getColors(which, type);
110     }
111
112     /**
113      * Colors that should be using for scrims.
114      *
115      * They will be:
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.
118      * - Black otherwise
119      */
120     public GradientColors getNeutralColors() {
121         return mHasMediaArtwork ? mBackdropColors : mNeutralColorsLock;
122     }
123
124     public void setHasMediaArtwork(boolean hasBackdrop) {
125         if (mHasMediaArtwork != hasBackdrop) {
126             mHasMediaArtwork = hasBackdrop;
127             triggerColorsChanged(WallpaperManager.FLAG_LOCK);
128         }
129     }
130
131     @Override
132     public void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
133         pw.println("SysuiColorExtractor:");
134
135         pw.println("  Current wallpaper colors:");
136         pw.println("    system: " + mSystemColors);
137         pw.println("    lock: " + mLockColors);
138
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);
146
147     }
148 }