blob: 1d7e899afc1048b869321b7ebbfd81c504c86fae [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;
Christine Franksea7d71b2018-07-03 14:46:07 -070018import android.hardware.display.ColorDisplayManager;
Jason Monkc3f42c12016-02-05 12:33:13 -050019import android.os.Handler;
20import android.provider.Settings.Secure;
Christine Franksea7d71b2018-07-03 14:46:07 -070021
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 Monkcb5296a2017-06-30 10:28:18 -040025import com.android.systemui.qs.AutoAddTracker;
Jason Monke5b770e2017-03-03 21:49:29 -050026import com.android.systemui.qs.QSTileHost;
Jason Monkc3f42c12016-02-05 12:33:13 -050027import com.android.systemui.qs.SecureSetting;
28import com.android.systemui.statusbar.policy.DataSaverController;
29import com.android.systemui.statusbar.policy.DataSaverController.Listener;
30import com.android.systemui.statusbar.policy.HotspotController;
31import com.android.systemui.statusbar.policy.HotspotController.Callback;
32
33/**
34 * Manages which tiles should be automatically added to QS.
35 */
36public class AutoTileManager {
Jason Monkcb5296a2017-06-30 10:28:18 -040037 public static final String HOTSPOT = "hotspot";
38 public static final String SAVER = "saver";
39 public static final String INVERSION = "inversion";
40 public static final String WORK = "work";
41 public static final String NIGHT = "night";
Amin Shaikh4bd8e052018-01-29 09:52:15 -050042
Jason Monkc3f42c12016-02-05 12:33:13 -050043 private final Context mContext;
44 private final QSTileHost mHost;
45 private final Handler mHandler;
Jason Monkcb5296a2017-06-30 10:28:18 -040046 private final AutoAddTracker mAutoTracker;
Jason Monkc3f42c12016-02-05 12:33:13 -050047
48 public AutoTileManager(Context context, QSTileHost host) {
Amin Shaikh4bd8e052018-01-29 09:52:15 -050049 this(context, new AutoAddTracker(context), host,
50 new Handler(Dependency.get(Dependency.BG_LOOPER)));
51 }
52
53 @VisibleForTesting
54 AutoTileManager(Context context, AutoAddTracker autoAddTracker, QSTileHost host,
55 Handler handler) {
56 mAutoTracker = autoAddTracker;
Jason Monkc3f42c12016-02-05 12:33:13 -050057 mContext = context;
58 mHost = host;
Amin Shaikh4bd8e052018-01-29 09:52:15 -050059 mHandler = handler;
Jason Monkcb5296a2017-06-30 10:28:18 -040060 if (!mAutoTracker.isAdded(HOTSPOT)) {
Jason Monk9c7844c2017-01-18 15:21:53 -050061 Dependency.get(HotspotController.class).addCallback(mHotspotCallback);
Jason Monkc3f42c12016-02-05 12:33:13 -050062 }
Jason Monkcb5296a2017-06-30 10:28:18 -040063 if (!mAutoTracker.isAdded(SAVER)) {
Jason Monk9c7844c2017-01-18 15:21:53 -050064 Dependency.get(DataSaverController.class).addCallback(mDataSaverListener);
Jason Monkc3f42c12016-02-05 12:33:13 -050065 }
Jason Monkcb5296a2017-06-30 10:28:18 -040066 if (!mAutoTracker.isAdded(INVERSION)) {
Jason Monkc3f42c12016-02-05 12:33:13 -050067 mColorsSetting = new SecureSetting(mContext, mHandler,
68 Secure.ACCESSIBILITY_DISPLAY_INVERSION_ENABLED) {
69 @Override
70 protected void handleValueChanged(int value, boolean observedChange) {
Jason Monkcb5296a2017-06-30 10:28:18 -040071 if (mAutoTracker.isAdded(INVERSION)) return;
Jason Monkc3f42c12016-02-05 12:33:13 -050072 if (value != 0) {
Jason Monkcb5296a2017-06-30 10:28:18 -040073 mHost.addTile(INVERSION);
74 mAutoTracker.setTileAdded(INVERSION);
Jason Monk9c7844c2017-01-18 15:21:53 -050075 mHandler.post(() -> mColorsSetting.setListening(false));
Jason Monkc3f42c12016-02-05 12:33:13 -050076 }
77 }
78 };
79 mColorsSetting.setListening(true);
80 }
Jason Monkcb5296a2017-06-30 10:28:18 -040081 if (!mAutoTracker.isAdded(WORK)) {
Jason Monk9c7844c2017-01-18 15:21:53 -050082 Dependency.get(ManagedProfileController.class).addCallback(mProfileCallback);
Jason Monkc3f42c12016-02-05 12:33:13 -050083 }
Jason Monkcb5296a2017-06-30 10:28:18 -040084 if (!mAutoTracker.isAdded(NIGHT)
Christine Franksea7d71b2018-07-03 14:46:07 -070085 && ColorDisplayManager.isNightDisplayAvailable(mContext)) {
Christine Franks5397f032017-11-01 18:35:16 -070086 Dependency.get(ColorDisplayController.class).setListener(mColorDisplayCallback);
Christine Franks69c2d1d2017-01-23 14:45:29 -080087 }
Jason Monkc3f42c12016-02-05 12:33:13 -050088 }
89
90 public void destroy() {
Amin Shaikh4bd8e052018-01-29 09:52:15 -050091 if (mColorsSetting != null) {
92 mColorsSetting.setListening(false);
93 }
Jason Monkcb5296a2017-06-30 10:28:18 -040094 mAutoTracker.destroy();
Jason Monk9c7844c2017-01-18 15:21:53 -050095 Dependency.get(HotspotController.class).removeCallback(mHotspotCallback);
96 Dependency.get(DataSaverController.class).removeCallback(mDataSaverListener);
97 Dependency.get(ManagedProfileController.class).removeCallback(mProfileCallback);
Christine Franksea7d71b2018-07-03 14:46:07 -070098 if (ColorDisplayManager.isNightDisplayAvailable(mContext)) {
99 Dependency.get(ColorDisplayController.class).setListener(null);
100 }
Jason Monkc3f42c12016-02-05 12:33:13 -0500101 }
102
arangelov5fe95982018-08-17 18:43:31 +0100103 public void unmarkTileAsAutoAdded(String tabSpec) {
104 mAutoTracker.setTileRemoved(tabSpec);
105 }
106
Jason Monkc3f42c12016-02-05 12:33:13 -0500107 private final ManagedProfileController.Callback mProfileCallback =
108 new ManagedProfileController.Callback() {
109 @Override
110 public void onManagedProfileChanged() {
Jason Monkcb5296a2017-06-30 10:28:18 -0400111 if (mAutoTracker.isAdded(WORK)) return;
Jason Monk9c7844c2017-01-18 15:21:53 -0500112 if (Dependency.get(ManagedProfileController.class).hasActiveProfile()) {
Jason Monkcb5296a2017-06-30 10:28:18 -0400113 mHost.addTile(WORK);
114 mAutoTracker.setTileAdded(WORK);
Jason Monkc3f42c12016-02-05 12:33:13 -0500115 }
116 }
117
118 @Override
119 public void onManagedProfileRemoved() {
120 }
121 };
122
123 private SecureSetting mColorsSetting;
124
125 private final DataSaverController.Listener mDataSaverListener = new Listener() {
126 @Override
127 public void onDataSaverChanged(boolean isDataSaving) {
Jason Monkcb5296a2017-06-30 10:28:18 -0400128 if (mAutoTracker.isAdded(SAVER)) return;
Jason Monkc3f42c12016-02-05 12:33:13 -0500129 if (isDataSaving) {
Jason Monkcb5296a2017-06-30 10:28:18 -0400130 mHost.addTile(SAVER);
131 mAutoTracker.setTileAdded(SAVER);
Jason Monk9c7844c2017-01-18 15:21:53 -0500132 mHandler.post(() -> Dependency.get(DataSaverController.class).removeCallback(
133 mDataSaverListener));
Jason Monkc3f42c12016-02-05 12:33:13 -0500134 }
135 }
136 };
137
138 private final HotspotController.Callback mHotspotCallback = new Callback() {
139 @Override
Rohan Shahe4071122018-01-22 15:16:09 -0800140 public void onHotspotChanged(boolean enabled, int numDevices) {
Jason Monkcb5296a2017-06-30 10:28:18 -0400141 if (mAutoTracker.isAdded(HOTSPOT)) return;
Jason Monkc3f42c12016-02-05 12:33:13 -0500142 if (enabled) {
Jason Monkcb5296a2017-06-30 10:28:18 -0400143 mHost.addTile(HOTSPOT);
144 mAutoTracker.setTileAdded(HOTSPOT);
Jason Monk9c7844c2017-01-18 15:21:53 -0500145 mHandler.post(() -> Dependency.get(HotspotController.class)
146 .removeCallback(mHotspotCallback));
Jason Monkc3f42c12016-02-05 12:33:13 -0500147 }
148 }
149 };
Christine Franks69c2d1d2017-01-23 14:45:29 -0800150
151 @VisibleForTesting
Christine Franks5397f032017-11-01 18:35:16 -0700152 final ColorDisplayController.Callback mColorDisplayCallback =
153 new ColorDisplayController.Callback() {
Christine Franks69c2d1d2017-01-23 14:45:29 -0800154 @Override
155 public void onActivated(boolean activated) {
156 if (activated) {
157 addNightTile();
158 }
159 }
160
161 @Override
162 public void onAutoModeChanged(int autoMode) {
Christine Franks5397f032017-11-01 18:35:16 -0700163 if (autoMode == ColorDisplayController.AUTO_MODE_CUSTOM
164 || autoMode == ColorDisplayController.AUTO_MODE_TWILIGHT) {
Christine Franks69c2d1d2017-01-23 14:45:29 -0800165 addNightTile();
166 }
167 }
168
169 private void addNightTile() {
Jason Monkcb5296a2017-06-30 10:28:18 -0400170 if (mAutoTracker.isAdded(NIGHT)) return;
171 mHost.addTile(NIGHT);
172 mAutoTracker.setTileAdded(NIGHT);
Christine Franks5397f032017-11-01 18:35:16 -0700173 mHandler.post(() -> Dependency.get(ColorDisplayController.class)
Christine Franks69c2d1d2017-01-23 14:45:29 -0800174 .setListener(null));
175 }
176 };
Jason Monkc3f42c12016-02-05 12:33:13 -0500177}