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; |
| 18 | import android.os.Handler; |
Jason Monk | 9c7844c | 2017-01-18 15:21:53 -0500 | [diff] [blame] | 19 | import android.os.Looper; |
Jason Monk | c3f42c1 | 2016-02-05 12:33:13 -0500 | [diff] [blame] | 20 | import android.provider.Settings.Secure; |
Jason Monk | 9c7844c | 2017-01-18 15:21:53 -0500 | [diff] [blame] | 21 | |
Christine Franks | 69c2d1d | 2017-01-23 14:45:29 -0800 | [diff] [blame] | 22 | import com.android.internal.annotations.VisibleForTesting; |
Christine Franks | 5397f03 | 2017-11-01 18:35:16 -0700 | [diff] [blame] | 23 | import com.android.internal.app.ColorDisplayController; |
Jason Monk | 9c7844c | 2017-01-18 15:21:53 -0500 | [diff] [blame] | 24 | import com.android.systemui.Dependency; |
Jason Monk | c3f42c1 | 2016-02-05 12:33:13 -0500 | [diff] [blame] | 25 | import com.android.systemui.Prefs; |
| 26 | import com.android.systemui.Prefs.Key; |
Jason Monk | cb5296a | 2017-06-30 10:28:18 -0400 | [diff] [blame] | 27 | import com.android.systemui.qs.AutoAddTracker; |
Jason Monk | e5b770e | 2017-03-03 21:49:29 -0500 | [diff] [blame] | 28 | import com.android.systemui.qs.QSTileHost; |
Jason Monk | c3f42c1 | 2016-02-05 12:33:13 -0500 | [diff] [blame] | 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 | |
Jason Monk | cb5296a | 2017-06-30 10:28:18 -0400 | [diff] [blame] | 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"; |
Jason Monk | c3f42c1 | 2016-02-05 12:33:13 -0500 | [diff] [blame] | 45 | private final Context mContext; |
| 46 | private final QSTileHost mHost; |
| 47 | private final Handler mHandler; |
Jason Monk | cb5296a | 2017-06-30 10:28:18 -0400 | [diff] [blame] | 48 | private final AutoAddTracker mAutoTracker; |
Jason Monk | c3f42c1 | 2016-02-05 12:33:13 -0500 | [diff] [blame] | 49 | |
| 50 | public AutoTileManager(Context context, QSTileHost host) { |
Jason Monk | cb5296a | 2017-06-30 10:28:18 -0400 | [diff] [blame] | 51 | mAutoTracker = new AutoAddTracker(context); |
Jason Monk | c3f42c1 | 2016-02-05 12:33:13 -0500 | [diff] [blame] | 52 | mContext = context; |
| 53 | mHost = host; |
Jason Monk | 9c7844c | 2017-01-18 15:21:53 -0500 | [diff] [blame] | 54 | mHandler = new Handler((Looper) Dependency.get(Dependency.BG_LOOPER)); |
Jason Monk | cb5296a | 2017-06-30 10:28:18 -0400 | [diff] [blame] | 55 | if (!mAutoTracker.isAdded(HOTSPOT)) { |
Jason Monk | 9c7844c | 2017-01-18 15:21:53 -0500 | [diff] [blame] | 56 | Dependency.get(HotspotController.class).addCallback(mHotspotCallback); |
Jason Monk | c3f42c1 | 2016-02-05 12:33:13 -0500 | [diff] [blame] | 57 | } |
Jason Monk | cb5296a | 2017-06-30 10:28:18 -0400 | [diff] [blame] | 58 | if (!mAutoTracker.isAdded(SAVER)) { |
Jason Monk | 9c7844c | 2017-01-18 15:21:53 -0500 | [diff] [blame] | 59 | Dependency.get(DataSaverController.class).addCallback(mDataSaverListener); |
Jason Monk | c3f42c1 | 2016-02-05 12:33:13 -0500 | [diff] [blame] | 60 | } |
Jason Monk | cb5296a | 2017-06-30 10:28:18 -0400 | [diff] [blame] | 61 | if (!mAutoTracker.isAdded(INVERSION)) { |
Jason Monk | c3f42c1 | 2016-02-05 12:33:13 -0500 | [diff] [blame] | 62 | mColorsSetting = new SecureSetting(mContext, mHandler, |
| 63 | Secure.ACCESSIBILITY_DISPLAY_INVERSION_ENABLED) { |
| 64 | @Override |
| 65 | protected void handleValueChanged(int value, boolean observedChange) { |
Jason Monk | cb5296a | 2017-06-30 10:28:18 -0400 | [diff] [blame] | 66 | if (mAutoTracker.isAdded(INVERSION)) return; |
Jason Monk | c3f42c1 | 2016-02-05 12:33:13 -0500 | [diff] [blame] | 67 | if (value != 0) { |
Jason Monk | cb5296a | 2017-06-30 10:28:18 -0400 | [diff] [blame] | 68 | mHost.addTile(INVERSION); |
| 69 | mAutoTracker.setTileAdded(INVERSION); |
Jason Monk | 9c7844c | 2017-01-18 15:21:53 -0500 | [diff] [blame] | 70 | mHandler.post(() -> mColorsSetting.setListening(false)); |
Jason Monk | c3f42c1 | 2016-02-05 12:33:13 -0500 | [diff] [blame] | 71 | } |
| 72 | } |
| 73 | }; |
| 74 | mColorsSetting.setListening(true); |
| 75 | } |
Jason Monk | cb5296a | 2017-06-30 10:28:18 -0400 | [diff] [blame] | 76 | if (!mAutoTracker.isAdded(WORK)) { |
Jason Monk | 9c7844c | 2017-01-18 15:21:53 -0500 | [diff] [blame] | 77 | Dependency.get(ManagedProfileController.class).addCallback(mProfileCallback); |
Jason Monk | c3f42c1 | 2016-02-05 12:33:13 -0500 | [diff] [blame] | 78 | } |
Christine Franks | 69c2d1d | 2017-01-23 14:45:29 -0800 | [diff] [blame] | 79 | |
Jason Monk | cb5296a | 2017-06-30 10:28:18 -0400 | [diff] [blame] | 80 | if (!mAutoTracker.isAdded(NIGHT) |
Christine Franks | 5397f03 | 2017-11-01 18:35:16 -0700 | [diff] [blame] | 81 | && ColorDisplayController.isAvailable(mContext)) { |
| 82 | Dependency.get(ColorDisplayController.class).setListener(mColorDisplayCallback); |
Christine Franks | 69c2d1d | 2017-01-23 14:45:29 -0800 | [diff] [blame] | 83 | } |
Jason Monk | c3f42c1 | 2016-02-05 12:33:13 -0500 | [diff] [blame] | 84 | } |
| 85 | |
| 86 | public void destroy() { |
Jason Monk | 8852905 | 2016-11-04 13:29:58 -0400 | [diff] [blame] | 87 | mColorsSetting.setListening(false); |
Jason Monk | cb5296a | 2017-06-30 10:28:18 -0400 | [diff] [blame] | 88 | mAutoTracker.destroy(); |
Jason Monk | 9c7844c | 2017-01-18 15:21:53 -0500 | [diff] [blame] | 89 | Dependency.get(HotspotController.class).removeCallback(mHotspotCallback); |
| 90 | Dependency.get(DataSaverController.class).removeCallback(mDataSaverListener); |
| 91 | Dependency.get(ManagedProfileController.class).removeCallback(mProfileCallback); |
Christine Franks | 5397f03 | 2017-11-01 18:35:16 -0700 | [diff] [blame] | 92 | Dependency.get(ColorDisplayController.class).setListener(null); |
Jason Monk | c3f42c1 | 2016-02-05 12:33:13 -0500 | [diff] [blame] | 93 | } |
| 94 | |
Jason Monk | c3f42c1 | 2016-02-05 12:33:13 -0500 | [diff] [blame] | 95 | private final ManagedProfileController.Callback mProfileCallback = |
| 96 | new ManagedProfileController.Callback() { |
| 97 | @Override |
| 98 | public void onManagedProfileChanged() { |
Jason Monk | cb5296a | 2017-06-30 10:28:18 -0400 | [diff] [blame] | 99 | if (mAutoTracker.isAdded(WORK)) return; |
Jason Monk | 9c7844c | 2017-01-18 15:21:53 -0500 | [diff] [blame] | 100 | if (Dependency.get(ManagedProfileController.class).hasActiveProfile()) { |
Jason Monk | cb5296a | 2017-06-30 10:28:18 -0400 | [diff] [blame] | 101 | mHost.addTile(WORK); |
| 102 | mAutoTracker.setTileAdded(WORK); |
Jason Monk | 9c7844c | 2017-01-18 15:21:53 -0500 | [diff] [blame] | 103 | mHandler.post(() -> Dependency.get(ManagedProfileController.class) |
| 104 | .removeCallback(mProfileCallback)); |
Jason Monk | c3f42c1 | 2016-02-05 12:33:13 -0500 | [diff] [blame] | 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) { |
Jason Monk | cb5296a | 2017-06-30 10:28:18 -0400 | [diff] [blame] | 118 | if (mAutoTracker.isAdded(SAVER)) return; |
Jason Monk | c3f42c1 | 2016-02-05 12:33:13 -0500 | [diff] [blame] | 119 | if (isDataSaving) { |
Jason Monk | cb5296a | 2017-06-30 10:28:18 -0400 | [diff] [blame] | 120 | mHost.addTile(SAVER); |
| 121 | mAutoTracker.setTileAdded(SAVER); |
Jason Monk | 9c7844c | 2017-01-18 15:21:53 -0500 | [diff] [blame] | 122 | mHandler.post(() -> Dependency.get(DataSaverController.class).removeCallback( |
| 123 | mDataSaverListener)); |
Jason Monk | c3f42c1 | 2016-02-05 12:33:13 -0500 | [diff] [blame] | 124 | } |
| 125 | } |
| 126 | }; |
| 127 | |
| 128 | private final HotspotController.Callback mHotspotCallback = new Callback() { |
| 129 | @Override |
Rohan Shah | e407112 | 2018-01-22 15:16:09 -0800 | [diff] [blame] | 130 | public void onHotspotChanged(boolean enabled, int numDevices) { |
Jason Monk | cb5296a | 2017-06-30 10:28:18 -0400 | [diff] [blame] | 131 | if (mAutoTracker.isAdded(HOTSPOT)) return; |
Jason Monk | c3f42c1 | 2016-02-05 12:33:13 -0500 | [diff] [blame] | 132 | if (enabled) { |
Jason Monk | cb5296a | 2017-06-30 10:28:18 -0400 | [diff] [blame] | 133 | mHost.addTile(HOTSPOT); |
| 134 | mAutoTracker.setTileAdded(HOTSPOT); |
Jason Monk | 9c7844c | 2017-01-18 15:21:53 -0500 | [diff] [blame] | 135 | mHandler.post(() -> Dependency.get(HotspotController.class) |
| 136 | .removeCallback(mHotspotCallback)); |
Jason Monk | c3f42c1 | 2016-02-05 12:33:13 -0500 | [diff] [blame] | 137 | } |
| 138 | } |
| 139 | }; |
Christine Franks | 69c2d1d | 2017-01-23 14:45:29 -0800 | [diff] [blame] | 140 | |
| 141 | @VisibleForTesting |
Christine Franks | 5397f03 | 2017-11-01 18:35:16 -0700 | [diff] [blame] | 142 | final ColorDisplayController.Callback mColorDisplayCallback = |
| 143 | new ColorDisplayController.Callback() { |
Christine Franks | 69c2d1d | 2017-01-23 14:45:29 -0800 | [diff] [blame] | 144 | @Override |
| 145 | public void onActivated(boolean activated) { |
| 146 | if (activated) { |
| 147 | addNightTile(); |
| 148 | } |
| 149 | } |
| 150 | |
| 151 | @Override |
| 152 | public void onAutoModeChanged(int autoMode) { |
Christine Franks | 5397f03 | 2017-11-01 18:35:16 -0700 | [diff] [blame] | 153 | if (autoMode == ColorDisplayController.AUTO_MODE_CUSTOM |
| 154 | || autoMode == ColorDisplayController.AUTO_MODE_TWILIGHT) { |
Christine Franks | 69c2d1d | 2017-01-23 14:45:29 -0800 | [diff] [blame] | 155 | addNightTile(); |
| 156 | } |
| 157 | } |
| 158 | |
| 159 | private void addNightTile() { |
Jason Monk | cb5296a | 2017-06-30 10:28:18 -0400 | [diff] [blame] | 160 | if (mAutoTracker.isAdded(NIGHT)) return; |
| 161 | mHost.addTile(NIGHT); |
| 162 | mAutoTracker.setTileAdded(NIGHT); |
Christine Franks | 5397f03 | 2017-11-01 18:35:16 -0700 | [diff] [blame] | 163 | mHandler.post(() -> Dependency.get(ColorDisplayController.class) |
Christine Franks | 69c2d1d | 2017-01-23 14:45:29 -0800 | [diff] [blame] | 164 | .setListener(null)); |
| 165 | } |
| 166 | }; |
Jason Monk | c3f42c1 | 2016-02-05 12:33:13 -0500 | [diff] [blame] | 167 | } |