Jason Monk | c3f42c1 | 2016-02-05 12:33:13 -0500 | [diff] [blame] | 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; |
Christine Franks | ea7d71b | 2018-07-03 14:46:07 -0700 | [diff] [blame] | 18 | import android.hardware.display.ColorDisplayManager; |
Christine Franks | 71e003e | 2019-01-24 14:40:20 -0800 | [diff] [blame] | 19 | import android.hardware.display.NightDisplayListener; |
Jason Monk | c3f42c1 | 2016-02-05 12:33:13 -0500 | [diff] [blame] | 20 | import android.os.Handler; |
| 21 | import android.provider.Settings.Secure; |
Christine Franks | ea7d71b | 2018-07-03 14:46:07 -0700 | [diff] [blame] | 22 | |
Christine Franks | 69c2d1d | 2017-01-23 14:45:29 -0800 | [diff] [blame] | 23 | import com.android.internal.annotations.VisibleForTesting; |
Dave Mankoff | 00e8a2f | 2019-12-18 16:59:49 -0500 | [diff] [blame] | 24 | import com.android.systemui.dagger.qualifiers.Background; |
Jason Monk | cb5296a | 2017-06-30 10:28:18 -0400 | [diff] [blame] | 25 | import com.android.systemui.qs.AutoAddTracker; |
Jason Monk | e5b770e | 2017-03-03 21:49:29 -0500 | [diff] [blame] | 26 | import com.android.systemui.qs.QSTileHost; |
Jason Monk | c3f42c1 | 2016-02-05 12:33:13 -0500 | [diff] [blame] | 27 | import com.android.systemui.qs.SecureSetting; |
Narayan Kamath | fa0ec0c | 2019-02-22 17:49:38 +0000 | [diff] [blame] | 28 | import com.android.systemui.statusbar.policy.CastController; |
| 29 | import com.android.systemui.statusbar.policy.CastController.CastDevice; |
Jason Monk | c3f42c1 | 2016-02-05 12:33:13 -0500 | [diff] [blame] | 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 | |
Jason Monk | a26641f | 2018-12-27 14:20:53 -0500 | [diff] [blame] | 35 | import javax.inject.Inject; |
Jason Monk | a26641f | 2018-12-27 14:20:53 -0500 | [diff] [blame] | 36 | |
Jason Monk | c3f42c1 | 2016-02-05 12:33:13 -0500 | [diff] [blame] | 37 | /** |
| 38 | * Manages which tiles should be automatically added to QS. |
| 39 | */ |
| 40 | public class AutoTileManager { |
Jason Monk | cb5296a | 2017-06-30 10:28:18 -0400 | [diff] [blame] | 41 | public static final String HOTSPOT = "hotspot"; |
| 42 | public static final String SAVER = "saver"; |
| 43 | public static final String INVERSION = "inversion"; |
| 44 | public static final String WORK = "work"; |
| 45 | public static final String NIGHT = "night"; |
Narayan Kamath | fa0ec0c | 2019-02-22 17:49:38 +0000 | [diff] [blame] | 46 | public static final String CAST = "cast"; |
Amin Shaikh | 4bd8e05 | 2018-01-29 09:52:15 -0500 | [diff] [blame] | 47 | |
Jason Monk | c3f42c1 | 2016-02-05 12:33:13 -0500 | [diff] [blame] | 48 | private final Context mContext; |
| 49 | private final QSTileHost mHost; |
| 50 | private final Handler mHandler; |
Jason Monk | cb5296a | 2017-06-30 10:28:18 -0400 | [diff] [blame] | 51 | private final AutoAddTracker mAutoTracker; |
Jason Monk | a26641f | 2018-12-27 14:20:53 -0500 | [diff] [blame] | 52 | private final HotspotController mHotspotController; |
| 53 | private final DataSaverController mDataSaverController; |
| 54 | private final ManagedProfileController mManagedProfileController; |
Christine Franks | 71e003e | 2019-01-24 14:40:20 -0800 | [diff] [blame] | 55 | private final NightDisplayListener mNightDisplayListener; |
Narayan Kamath | fa0ec0c | 2019-02-22 17:49:38 +0000 | [diff] [blame] | 56 | private final CastController mCastController; |
Jason Monk | c3f42c1 | 2016-02-05 12:33:13 -0500 | [diff] [blame] | 57 | |
Jason Monk | a26641f | 2018-12-27 14:20:53 -0500 | [diff] [blame] | 58 | @Inject |
| 59 | public AutoTileManager(Context context, AutoAddTracker autoAddTracker, QSTileHost host, |
Dave Mankoff | 00e8a2f | 2019-12-18 16:59:49 -0500 | [diff] [blame] | 60 | @Background Handler handler, |
Jason Monk | a26641f | 2018-12-27 14:20:53 -0500 | [diff] [blame] | 61 | HotspotController hotspotController, |
| 62 | DataSaverController dataSaverController, |
| 63 | ManagedProfileController managedProfileController, |
Narayan Kamath | fa0ec0c | 2019-02-22 17:49:38 +0000 | [diff] [blame] | 64 | NightDisplayListener nightDisplayListener, |
| 65 | CastController castController) { |
Amin Shaikh | 4bd8e05 | 2018-01-29 09:52:15 -0500 | [diff] [blame] | 66 | mAutoTracker = autoAddTracker; |
Jason Monk | c3f42c1 | 2016-02-05 12:33:13 -0500 | [diff] [blame] | 67 | mContext = context; |
| 68 | mHost = host; |
Amin Shaikh | 4bd8e05 | 2018-01-29 09:52:15 -0500 | [diff] [blame] | 69 | mHandler = handler; |
Jason Monk | a26641f | 2018-12-27 14:20:53 -0500 | [diff] [blame] | 70 | mHotspotController = hotspotController; |
| 71 | mDataSaverController = dataSaverController; |
| 72 | mManagedProfileController = managedProfileController; |
Christine Franks | 71e003e | 2019-01-24 14:40:20 -0800 | [diff] [blame] | 73 | mNightDisplayListener = nightDisplayListener; |
Narayan Kamath | fa0ec0c | 2019-02-22 17:49:38 +0000 | [diff] [blame] | 74 | mCastController = castController; |
Jason Monk | cb5296a | 2017-06-30 10:28:18 -0400 | [diff] [blame] | 75 | if (!mAutoTracker.isAdded(HOTSPOT)) { |
Jason Monk | a26641f | 2018-12-27 14:20:53 -0500 | [diff] [blame] | 76 | hotspotController.addCallback(mHotspotCallback); |
Jason Monk | c3f42c1 | 2016-02-05 12:33:13 -0500 | [diff] [blame] | 77 | } |
Jason Monk | cb5296a | 2017-06-30 10:28:18 -0400 | [diff] [blame] | 78 | if (!mAutoTracker.isAdded(SAVER)) { |
Jason Monk | a26641f | 2018-12-27 14:20:53 -0500 | [diff] [blame] | 79 | dataSaverController.addCallback(mDataSaverListener); |
Jason Monk | c3f42c1 | 2016-02-05 12:33:13 -0500 | [diff] [blame] | 80 | } |
Jason Monk | cb5296a | 2017-06-30 10:28:18 -0400 | [diff] [blame] | 81 | if (!mAutoTracker.isAdded(INVERSION)) { |
Jason Monk | c3f42c1 | 2016-02-05 12:33:13 -0500 | [diff] [blame] | 82 | mColorsSetting = new SecureSetting(mContext, mHandler, |
| 83 | Secure.ACCESSIBILITY_DISPLAY_INVERSION_ENABLED) { |
| 84 | @Override |
| 85 | protected void handleValueChanged(int value, boolean observedChange) { |
Jason Monk | cb5296a | 2017-06-30 10:28:18 -0400 | [diff] [blame] | 86 | if (mAutoTracker.isAdded(INVERSION)) return; |
Jason Monk | c3f42c1 | 2016-02-05 12:33:13 -0500 | [diff] [blame] | 87 | if (value != 0) { |
Jason Monk | cb5296a | 2017-06-30 10:28:18 -0400 | [diff] [blame] | 88 | mHost.addTile(INVERSION); |
| 89 | mAutoTracker.setTileAdded(INVERSION); |
Jason Monk | 9c7844c | 2017-01-18 15:21:53 -0500 | [diff] [blame] | 90 | mHandler.post(() -> mColorsSetting.setListening(false)); |
Jason Monk | c3f42c1 | 2016-02-05 12:33:13 -0500 | [diff] [blame] | 91 | } |
| 92 | } |
| 93 | }; |
| 94 | mColorsSetting.setListening(true); |
| 95 | } |
Jason Monk | cb5296a | 2017-06-30 10:28:18 -0400 | [diff] [blame] | 96 | if (!mAutoTracker.isAdded(WORK)) { |
Jason Monk | a26641f | 2018-12-27 14:20:53 -0500 | [diff] [blame] | 97 | managedProfileController.addCallback(mProfileCallback); |
Jason Monk | c3f42c1 | 2016-02-05 12:33:13 -0500 | [diff] [blame] | 98 | } |
Jason Monk | cb5296a | 2017-06-30 10:28:18 -0400 | [diff] [blame] | 99 | if (!mAutoTracker.isAdded(NIGHT) |
Christine Franks | ea7d71b | 2018-07-03 14:46:07 -0700 | [diff] [blame] | 100 | && ColorDisplayManager.isNightDisplayAvailable(mContext)) { |
Christine Franks | 71e003e | 2019-01-24 14:40:20 -0800 | [diff] [blame] | 101 | nightDisplayListener.setCallback(mNightDisplayCallback); |
Christine Franks | 69c2d1d | 2017-01-23 14:45:29 -0800 | [diff] [blame] | 102 | } |
Narayan Kamath | fa0ec0c | 2019-02-22 17:49:38 +0000 | [diff] [blame] | 103 | if (!mAutoTracker.isAdded(CAST)) { |
| 104 | castController.addCallback(mCastCallback); |
| 105 | } |
Jason Monk | c3f42c1 | 2016-02-05 12:33:13 -0500 | [diff] [blame] | 106 | } |
| 107 | |
| 108 | public void destroy() { |
Amin Shaikh | 4bd8e05 | 2018-01-29 09:52:15 -0500 | [diff] [blame] | 109 | if (mColorsSetting != null) { |
| 110 | mColorsSetting.setListening(false); |
| 111 | } |
Jason Monk | cb5296a | 2017-06-30 10:28:18 -0400 | [diff] [blame] | 112 | mAutoTracker.destroy(); |
Jason Monk | a26641f | 2018-12-27 14:20:53 -0500 | [diff] [blame] | 113 | mHotspotController.removeCallback(mHotspotCallback); |
| 114 | mDataSaverController.removeCallback(mDataSaverListener); |
| 115 | mManagedProfileController.removeCallback(mProfileCallback); |
Christine Franks | ea7d71b | 2018-07-03 14:46:07 -0700 | [diff] [blame] | 116 | if (ColorDisplayManager.isNightDisplayAvailable(mContext)) { |
Christine Franks | 71e003e | 2019-01-24 14:40:20 -0800 | [diff] [blame] | 117 | mNightDisplayListener.setCallback(null); |
Christine Franks | ea7d71b | 2018-07-03 14:46:07 -0700 | [diff] [blame] | 118 | } |
Narayan Kamath | fa0ec0c | 2019-02-22 17:49:38 +0000 | [diff] [blame] | 119 | mCastController.removeCallback(mCastCallback); |
Jason Monk | c3f42c1 | 2016-02-05 12:33:13 -0500 | [diff] [blame] | 120 | } |
| 121 | |
arangelov | 5fe9598 | 2018-08-17 18:43:31 +0100 | [diff] [blame] | 122 | public void unmarkTileAsAutoAdded(String tabSpec) { |
| 123 | mAutoTracker.setTileRemoved(tabSpec); |
| 124 | } |
| 125 | |
Jason Monk | c3f42c1 | 2016-02-05 12:33:13 -0500 | [diff] [blame] | 126 | private final ManagedProfileController.Callback mProfileCallback = |
| 127 | new ManagedProfileController.Callback() { |
| 128 | @Override |
| 129 | public void onManagedProfileChanged() { |
Jason Monk | cb5296a | 2017-06-30 10:28:18 -0400 | [diff] [blame] | 130 | if (mAutoTracker.isAdded(WORK)) return; |
Jason Monk | a26641f | 2018-12-27 14:20:53 -0500 | [diff] [blame] | 131 | if (mManagedProfileController.hasActiveProfile()) { |
Jason Monk | cb5296a | 2017-06-30 10:28:18 -0400 | [diff] [blame] | 132 | mHost.addTile(WORK); |
| 133 | mAutoTracker.setTileAdded(WORK); |
Jason Monk | c3f42c1 | 2016-02-05 12:33:13 -0500 | [diff] [blame] | 134 | } |
| 135 | } |
| 136 | |
| 137 | @Override |
| 138 | public void onManagedProfileRemoved() { |
| 139 | } |
| 140 | }; |
| 141 | |
| 142 | private SecureSetting mColorsSetting; |
| 143 | |
| 144 | private final DataSaverController.Listener mDataSaverListener = new Listener() { |
| 145 | @Override |
| 146 | public void onDataSaverChanged(boolean isDataSaving) { |
Jason Monk | cb5296a | 2017-06-30 10:28:18 -0400 | [diff] [blame] | 147 | if (mAutoTracker.isAdded(SAVER)) return; |
Jason Monk | c3f42c1 | 2016-02-05 12:33:13 -0500 | [diff] [blame] | 148 | if (isDataSaving) { |
Jason Monk | cb5296a | 2017-06-30 10:28:18 -0400 | [diff] [blame] | 149 | mHost.addTile(SAVER); |
| 150 | mAutoTracker.setTileAdded(SAVER); |
Jason Monk | a26641f | 2018-12-27 14:20:53 -0500 | [diff] [blame] | 151 | mHandler.post(() -> mDataSaverController.removeCallback(mDataSaverListener)); |
Jason Monk | c3f42c1 | 2016-02-05 12:33:13 -0500 | [diff] [blame] | 152 | } |
| 153 | } |
| 154 | }; |
| 155 | |
| 156 | private final HotspotController.Callback mHotspotCallback = new Callback() { |
| 157 | @Override |
Rohan Shah | e407112 | 2018-01-22 15:16:09 -0800 | [diff] [blame] | 158 | public void onHotspotChanged(boolean enabled, int numDevices) { |
Jason Monk | cb5296a | 2017-06-30 10:28:18 -0400 | [diff] [blame] | 159 | if (mAutoTracker.isAdded(HOTSPOT)) return; |
Jason Monk | c3f42c1 | 2016-02-05 12:33:13 -0500 | [diff] [blame] | 160 | if (enabled) { |
Jason Monk | cb5296a | 2017-06-30 10:28:18 -0400 | [diff] [blame] | 161 | mHost.addTile(HOTSPOT); |
| 162 | mAutoTracker.setTileAdded(HOTSPOT); |
Jason Monk | a26641f | 2018-12-27 14:20:53 -0500 | [diff] [blame] | 163 | mHandler.post(() -> mHotspotController.removeCallback(mHotspotCallback)); |
Jason Monk | c3f42c1 | 2016-02-05 12:33:13 -0500 | [diff] [blame] | 164 | } |
| 165 | } |
| 166 | }; |
Christine Franks | 69c2d1d | 2017-01-23 14:45:29 -0800 | [diff] [blame] | 167 | |
| 168 | @VisibleForTesting |
Christine Franks | 71e003e | 2019-01-24 14:40:20 -0800 | [diff] [blame] | 169 | final NightDisplayListener.Callback mNightDisplayCallback = |
| 170 | new NightDisplayListener.Callback() { |
Christine Franks | 69c2d1d | 2017-01-23 14:45:29 -0800 | [diff] [blame] | 171 | @Override |
| 172 | public void onActivated(boolean activated) { |
| 173 | if (activated) { |
| 174 | addNightTile(); |
| 175 | } |
| 176 | } |
| 177 | |
| 178 | @Override |
| 179 | public void onAutoModeChanged(int autoMode) { |
Christine Franks | 83cc541 | 2018-07-03 14:46:07 -0700 | [diff] [blame] | 180 | if (autoMode == ColorDisplayManager.AUTO_MODE_CUSTOM_TIME |
| 181 | || autoMode == ColorDisplayManager.AUTO_MODE_TWILIGHT) { |
Christine Franks | 69c2d1d | 2017-01-23 14:45:29 -0800 | [diff] [blame] | 182 | addNightTile(); |
| 183 | } |
| 184 | } |
| 185 | |
| 186 | private void addNightTile() { |
Jason Monk | cb5296a | 2017-06-30 10:28:18 -0400 | [diff] [blame] | 187 | if (mAutoTracker.isAdded(NIGHT)) return; |
| 188 | mHost.addTile(NIGHT); |
| 189 | mAutoTracker.setTileAdded(NIGHT); |
Christine Franks | 71e003e | 2019-01-24 14:40:20 -0800 | [diff] [blame] | 190 | mHandler.post(() -> mNightDisplayListener.setCallback(null)); |
Christine Franks | 69c2d1d | 2017-01-23 14:45:29 -0800 | [diff] [blame] | 191 | } |
| 192 | }; |
Narayan Kamath | fa0ec0c | 2019-02-22 17:49:38 +0000 | [diff] [blame] | 193 | |
| 194 | @VisibleForTesting |
| 195 | final CastController.Callback mCastCallback = new CastController.Callback() { |
| 196 | @Override |
| 197 | public void onCastDevicesChanged() { |
| 198 | if (mAutoTracker.isAdded(CAST)) return; |
| 199 | |
| 200 | boolean isCasting = false; |
| 201 | for (CastDevice device : mCastController.getCastDevices()) { |
| 202 | if (device.state == CastDevice.STATE_CONNECTED |
| 203 | || device.state == CastDevice.STATE_CONNECTING) { |
| 204 | isCasting = true; |
| 205 | break; |
| 206 | } |
| 207 | } |
| 208 | |
| 209 | if (isCasting) { |
| 210 | mHost.addTile(CAST); |
| 211 | mAutoTracker.setTileAdded(CAST); |
| 212 | mHandler.post(() -> mCastController.removeCallback(mCastCallback)); |
| 213 | } |
| 214 | } |
| 215 | }; |
Jason Monk | c3f42c1 | 2016-02-05 12:33:13 -0500 | [diff] [blame] | 216 | } |