• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2016 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file
5  * except in compliance with the License. You may obtain a copy of the License at
6  *
7  *      http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software distributed under the
10  * License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
11  * KIND, either express or implied. See the License for the specific language governing
12  * permissions and limitations under the License.
13  */
14 
15 package com.android.systemui.statusbar.phone;
16 
17 import android.content.Context;
18 import android.os.Handler;
19 import android.os.Looper;
20 import android.provider.Settings.Secure;
21 
22 import com.android.internal.annotations.VisibleForTesting;
23 import com.android.internal.app.NightDisplayController;
24 import com.android.systemui.Dependency;
25 import com.android.systemui.Prefs;
26 import com.android.systemui.Prefs.Key;
27 import com.android.systemui.qs.AutoAddTracker;
28 import com.android.systemui.qs.QSTileHost;
29 import com.android.systemui.qs.SecureSetting;
30 import com.android.systemui.statusbar.policy.DataSaverController;
31 import com.android.systemui.statusbar.policy.DataSaverController.Listener;
32 import com.android.systemui.statusbar.policy.HotspotController;
33 import com.android.systemui.statusbar.policy.HotspotController.Callback;
34 
35 /**
36  * Manages which tiles should be automatically added to QS.
37  */
38 public class AutoTileManager {
39 
40     public static final String HOTSPOT = "hotspot";
41     public static final String SAVER = "saver";
42     public static final String INVERSION = "inversion";
43     public static final String WORK = "work";
44     public static final String NIGHT = "night";
45     private final Context mContext;
46     private final QSTileHost mHost;
47     private final Handler mHandler;
48     private final AutoAddTracker mAutoTracker;
49 
AutoTileManager(Context context, QSTileHost host)50     public AutoTileManager(Context context, QSTileHost host) {
51         mAutoTracker = new AutoAddTracker(context);
52         mContext = context;
53         mHost = host;
54         mHandler = new Handler((Looper) Dependency.get(Dependency.BG_LOOPER));
55         if (!mAutoTracker.isAdded(HOTSPOT)) {
56             Dependency.get(HotspotController.class).addCallback(mHotspotCallback);
57         }
58         if (!mAutoTracker.isAdded(SAVER)) {
59             Dependency.get(DataSaverController.class).addCallback(mDataSaverListener);
60         }
61         if (!mAutoTracker.isAdded(INVERSION)) {
62             mColorsSetting = new SecureSetting(mContext, mHandler,
63                     Secure.ACCESSIBILITY_DISPLAY_INVERSION_ENABLED) {
64                 @Override
65                 protected void handleValueChanged(int value, boolean observedChange) {
66                     if (mAutoTracker.isAdded(INVERSION)) return;
67                     if (value != 0) {
68                         mHost.addTile(INVERSION);
69                         mAutoTracker.setTileAdded(INVERSION);
70                         mHandler.post(() -> mColorsSetting.setListening(false));
71                     }
72                 }
73             };
74             mColorsSetting.setListening(true);
75         }
76         if (!mAutoTracker.isAdded(WORK)) {
77             Dependency.get(ManagedProfileController.class).addCallback(mProfileCallback);
78         }
79 
80         if (!mAutoTracker.isAdded(NIGHT)
81                 && NightDisplayController.isAvailable(mContext)) {
82             Dependency.get(NightDisplayController.class).setListener(mNightDisplayCallback);
83         }
84     }
85 
destroy()86     public void destroy() {
87         mColorsSetting.setListening(false);
88         mAutoTracker.destroy();
89         Dependency.get(HotspotController.class).removeCallback(mHotspotCallback);
90         Dependency.get(DataSaverController.class).removeCallback(mDataSaverListener);
91         Dependency.get(ManagedProfileController.class).removeCallback(mProfileCallback);
92         Dependency.get(NightDisplayController.class).setListener(null);
93     }
94 
95     private final ManagedProfileController.Callback mProfileCallback =
96             new ManagedProfileController.Callback() {
97                 @Override
98                 public void onManagedProfileChanged() {
99                     if (mAutoTracker.isAdded(WORK)) return;
100                     if (Dependency.get(ManagedProfileController.class).hasActiveProfile()) {
101                         mHost.addTile(WORK);
102                         mAutoTracker.setTileAdded(WORK);
103                         mHandler.post(() -> Dependency.get(ManagedProfileController.class)
104                                 .removeCallback(mProfileCallback));
105                     }
106                 }
107 
108                 @Override
109                 public void onManagedProfileRemoved() {
110                 }
111             };
112 
113     private SecureSetting mColorsSetting;
114 
115     private final DataSaverController.Listener mDataSaverListener = new Listener() {
116         @Override
117         public void onDataSaverChanged(boolean isDataSaving) {
118             if (mAutoTracker.isAdded(SAVER)) return;
119             if (isDataSaving) {
120                 mHost.addTile(SAVER);
121                 mAutoTracker.setTileAdded(SAVER);
122                 mHandler.post(() -> Dependency.get(DataSaverController.class).removeCallback(
123                         mDataSaverListener));
124             }
125         }
126     };
127 
128     private final HotspotController.Callback mHotspotCallback = new Callback() {
129         @Override
130         public void onHotspotChanged(boolean enabled) {
131             if (mAutoTracker.isAdded(HOTSPOT)) return;
132             if (enabled) {
133                 mHost.addTile(HOTSPOT);
134                 mAutoTracker.setTileAdded(HOTSPOT);
135                 mHandler.post(() -> Dependency.get(HotspotController.class)
136                         .removeCallback(mHotspotCallback));
137             }
138         }
139     };
140 
141     @VisibleForTesting
142     final NightDisplayController.Callback mNightDisplayCallback =
143             new NightDisplayController.Callback() {
144         @Override
145         public void onActivated(boolean activated) {
146             if (activated) {
147                 addNightTile();
148             }
149         }
150 
151         @Override
152         public void onAutoModeChanged(int autoMode) {
153             if (autoMode == NightDisplayController.AUTO_MODE_CUSTOM
154                     || autoMode == NightDisplayController.AUTO_MODE_TWILIGHT) {
155                 addNightTile();
156             }
157         }
158 
159         private void addNightTile() {
160             if (mAutoTracker.isAdded(NIGHT)) return;
161             mHost.addTile(NIGHT);
162             mAutoTracker.setTileAdded(NIGHT);
163             mHandler.post(() -> Dependency.get(NightDisplayController.class)
164                     .setListener(null));
165         }
166     };
167 }
168