blob: 36f9f6b794173373c27edab5e706419d0777de63 [file] [log] [blame]
Jason Monkc3f42c12016-02-05 12:33:13 -05001/*
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
15package com.android.systemui.statusbar.phone;
16
17import android.content.Context;
18import android.os.Handler;
Jason Monk9c7844c2017-01-18 15:21:53 -050019import android.os.Looper;
Jason Monkc3f42c12016-02-05 12:33:13 -050020import android.provider.Settings.Secure;
Jason Monk9c7844c2017-01-18 15:21:53 -050021
Christine Franks69c2d1d2017-01-23 14:45:29 -080022import com.android.internal.annotations.VisibleForTesting;
Christine Franks5397f032017-11-01 18:35:16 -070023import com.android.internal.app.ColorDisplayController;
Jason Monk9c7844c2017-01-18 15:21:53 -050024import com.android.systemui.Dependency;
Jason Monkc3f42c12016-02-05 12:33:13 -050025import com.android.systemui.Prefs;
26import com.android.systemui.Prefs.Key;
Jason Monkcb5296a2017-06-30 10:28:18 -040027import com.android.systemui.qs.AutoAddTracker;
Jason Monke5b770e2017-03-03 21:49:29 -050028import com.android.systemui.qs.QSTileHost;
Jason Monkc3f42c12016-02-05 12:33:13 -050029import com.android.systemui.qs.SecureSetting;
30import com.android.systemui.statusbar.policy.DataSaverController;
31import com.android.systemui.statusbar.policy.DataSaverController.Listener;
32import com.android.systemui.statusbar.policy.HotspotController;
33import com.android.systemui.statusbar.policy.HotspotController.Callback;
34
35/**
36 * Manages which tiles should be automatically added to QS.
37 */
38public class AutoTileManager {
39
Jason Monkcb5296a2017-06-30 10:28:18 -040040 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";
Jason Monkc3f42c12016-02-05 12:33:13 -050045 private final Context mContext;
46 private final QSTileHost mHost;
47 private final Handler mHandler;
Jason Monkcb5296a2017-06-30 10:28:18 -040048 private final AutoAddTracker mAutoTracker;
Jason Monkc3f42c12016-02-05 12:33:13 -050049
50 public AutoTileManager(Context context, QSTileHost host) {
Jason Monkcb5296a2017-06-30 10:28:18 -040051 mAutoTracker = new AutoAddTracker(context);
Jason Monkc3f42c12016-02-05 12:33:13 -050052 mContext = context;
53 mHost = host;
Jason Monk9c7844c2017-01-18 15:21:53 -050054 mHandler = new Handler((Looper) Dependency.get(Dependency.BG_LOOPER));
Jason Monkcb5296a2017-06-30 10:28:18 -040055 if (!mAutoTracker.isAdded(HOTSPOT)) {
Jason Monk9c7844c2017-01-18 15:21:53 -050056 Dependency.get(HotspotController.class).addCallback(mHotspotCallback);
Jason Monkc3f42c12016-02-05 12:33:13 -050057 }
Jason Monkcb5296a2017-06-30 10:28:18 -040058 if (!mAutoTracker.isAdded(SAVER)) {
Jason Monk9c7844c2017-01-18 15:21:53 -050059 Dependency.get(DataSaverController.class).addCallback(mDataSaverListener);
Jason Monkc3f42c12016-02-05 12:33:13 -050060 }
Jason Monkcb5296a2017-06-30 10:28:18 -040061 if (!mAutoTracker.isAdded(INVERSION)) {
Jason Monkc3f42c12016-02-05 12:33:13 -050062 mColorsSetting = new SecureSetting(mContext, mHandler,
63 Secure.ACCESSIBILITY_DISPLAY_INVERSION_ENABLED) {
64 @Override
65 protected void handleValueChanged(int value, boolean observedChange) {
Jason Monkcb5296a2017-06-30 10:28:18 -040066 if (mAutoTracker.isAdded(INVERSION)) return;
Jason Monkc3f42c12016-02-05 12:33:13 -050067 if (value != 0) {
Jason Monkcb5296a2017-06-30 10:28:18 -040068 mHost.addTile(INVERSION);
69 mAutoTracker.setTileAdded(INVERSION);
Jason Monk9c7844c2017-01-18 15:21:53 -050070 mHandler.post(() -> mColorsSetting.setListening(false));
Jason Monkc3f42c12016-02-05 12:33:13 -050071 }
72 }
73 };
74 mColorsSetting.setListening(true);
75 }
Jason Monkcb5296a2017-06-30 10:28:18 -040076 if (!mAutoTracker.isAdded(WORK)) {
Jason Monk9c7844c2017-01-18 15:21:53 -050077 Dependency.get(ManagedProfileController.class).addCallback(mProfileCallback);
Jason Monkc3f42c12016-02-05 12:33:13 -050078 }
Christine Franks69c2d1d2017-01-23 14:45:29 -080079
Jason Monkcb5296a2017-06-30 10:28:18 -040080 if (!mAutoTracker.isAdded(NIGHT)
Christine Franks5397f032017-11-01 18:35:16 -070081 && ColorDisplayController.isAvailable(mContext)) {
82 Dependency.get(ColorDisplayController.class).setListener(mColorDisplayCallback);
Christine Franks69c2d1d2017-01-23 14:45:29 -080083 }
Jason Monkc3f42c12016-02-05 12:33:13 -050084 }
85
86 public void destroy() {
Jason Monk88529052016-11-04 13:29:58 -040087 mColorsSetting.setListening(false);
Jason Monkcb5296a2017-06-30 10:28:18 -040088 mAutoTracker.destroy();
Jason Monk9c7844c2017-01-18 15:21:53 -050089 Dependency.get(HotspotController.class).removeCallback(mHotspotCallback);
90 Dependency.get(DataSaverController.class).removeCallback(mDataSaverListener);
91 Dependency.get(ManagedProfileController.class).removeCallback(mProfileCallback);
Christine Franks5397f032017-11-01 18:35:16 -070092 Dependency.get(ColorDisplayController.class).setListener(null);
Jason Monkc3f42c12016-02-05 12:33:13 -050093 }
94
Jason Monkc3f42c12016-02-05 12:33:13 -050095 private final ManagedProfileController.Callback mProfileCallback =
96 new ManagedProfileController.Callback() {
97 @Override
98 public void onManagedProfileChanged() {
Jason Monkcb5296a2017-06-30 10:28:18 -040099 if (mAutoTracker.isAdded(WORK)) return;
Jason Monk9c7844c2017-01-18 15:21:53 -0500100 if (Dependency.get(ManagedProfileController.class).hasActiveProfile()) {
Jason Monkcb5296a2017-06-30 10:28:18 -0400101 mHost.addTile(WORK);
102 mAutoTracker.setTileAdded(WORK);
Jason Monk9c7844c2017-01-18 15:21:53 -0500103 mHandler.post(() -> Dependency.get(ManagedProfileController.class)
104 .removeCallback(mProfileCallback));
Jason Monkc3f42c12016-02-05 12:33:13 -0500105 }
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) {
Jason Monkcb5296a2017-06-30 10:28:18 -0400118 if (mAutoTracker.isAdded(SAVER)) return;
Jason Monkc3f42c12016-02-05 12:33:13 -0500119 if (isDataSaving) {
Jason Monkcb5296a2017-06-30 10:28:18 -0400120 mHost.addTile(SAVER);
121 mAutoTracker.setTileAdded(SAVER);
Jason Monk9c7844c2017-01-18 15:21:53 -0500122 mHandler.post(() -> Dependency.get(DataSaverController.class).removeCallback(
123 mDataSaverListener));
Jason Monkc3f42c12016-02-05 12:33:13 -0500124 }
125 }
126 };
127
128 private final HotspotController.Callback mHotspotCallback = new Callback() {
129 @Override
Rohan Shahe4071122018-01-22 15:16:09 -0800130 public void onHotspotChanged(boolean enabled, int numDevices) {
Jason Monkcb5296a2017-06-30 10:28:18 -0400131 if (mAutoTracker.isAdded(HOTSPOT)) return;
Jason Monkc3f42c12016-02-05 12:33:13 -0500132 if (enabled) {
Jason Monkcb5296a2017-06-30 10:28:18 -0400133 mHost.addTile(HOTSPOT);
134 mAutoTracker.setTileAdded(HOTSPOT);
Jason Monk9c7844c2017-01-18 15:21:53 -0500135 mHandler.post(() -> Dependency.get(HotspotController.class)
136 .removeCallback(mHotspotCallback));
Jason Monkc3f42c12016-02-05 12:33:13 -0500137 }
138 }
139 };
Christine Franks69c2d1d2017-01-23 14:45:29 -0800140
141 @VisibleForTesting
Christine Franks5397f032017-11-01 18:35:16 -0700142 final ColorDisplayController.Callback mColorDisplayCallback =
143 new ColorDisplayController.Callback() {
Christine Franks69c2d1d2017-01-23 14:45:29 -0800144 @Override
145 public void onActivated(boolean activated) {
146 if (activated) {
147 addNightTile();
148 }
149 }
150
151 @Override
152 public void onAutoModeChanged(int autoMode) {
Christine Franks5397f032017-11-01 18:35:16 -0700153 if (autoMode == ColorDisplayController.AUTO_MODE_CUSTOM
154 || autoMode == ColorDisplayController.AUTO_MODE_TWILIGHT) {
Christine Franks69c2d1d2017-01-23 14:45:29 -0800155 addNightTile();
156 }
157 }
158
159 private void addNightTile() {
Jason Monkcb5296a2017-06-30 10:28:18 -0400160 if (mAutoTracker.isAdded(NIGHT)) return;
161 mHost.addTile(NIGHT);
162 mAutoTracker.setTileAdded(NIGHT);
Christine Franks5397f032017-11-01 18:35:16 -0700163 mHandler.post(() -> Dependency.get(ColorDisplayController.class)
Christine Franks69c2d1d2017-01-23 14:45:29 -0800164 .setListener(null));
165 }
166 };
Jason Monkc3f42c12016-02-05 12:33:13 -0500167}