John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2014 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | package com.android.systemui.statusbar.phone; |
| 18 | |
Jason Monk | ba2318e | 2015-12-08 09:04:23 -0500 | [diff] [blame] | 19 | import android.app.ActivityManager; |
Adrian Roos | 62692b2 | 2015-09-11 17:46:23 -0700 | [diff] [blame] | 20 | import android.app.PendingIntent; |
Jason Monk | 7e53f20 | 2016-01-28 10:40:20 -0500 | [diff] [blame] | 21 | import android.content.ComponentName; |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 22 | import android.content.Context; |
| 23 | import android.content.Intent; |
John Spurlock | bceed06 | 2014-08-10 18:04:16 -0400 | [diff] [blame] | 24 | import android.content.res.Resources; |
Jason Monk | 7e53f20 | 2016-01-28 10:40:20 -0500 | [diff] [blame] | 25 | import android.os.Handler; |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 26 | import android.os.HandlerThread; |
| 27 | import android.os.Looper; |
Jason Monk | 5e74517 | 2015-06-02 19:14:44 -0400 | [diff] [blame] | 28 | import android.os.Process; |
Jason Monk | 7e53f20 | 2016-01-28 10:40:20 -0500 | [diff] [blame] | 29 | import android.os.UserHandle; |
Jason Monk | ba2318e | 2015-12-08 09:04:23 -0500 | [diff] [blame] | 30 | import android.provider.Settings; |
Jason Monk | 7e53f20 | 2016-01-28 10:40:20 -0500 | [diff] [blame] | 31 | import android.provider.Settings.Secure; |
Jason Monk | ba2318e | 2015-12-08 09:04:23 -0500 | [diff] [blame] | 32 | import android.text.TextUtils; |
John Spurlock | bceed06 | 2014-08-10 18:04:16 -0400 | [diff] [blame] | 33 | import android.util.Log; |
Jason Monk | ca894a0 | 2016-01-12 15:30:22 -0500 | [diff] [blame] | 34 | import android.view.View; |
Winson | c0d7058 | 2016-01-29 10:24:39 -0800 | [diff] [blame] | 35 | |
John Spurlock | bceed06 | 2014-08-10 18:04:16 -0400 | [diff] [blame] | 36 | import com.android.systemui.R; |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 37 | import com.android.systemui.qs.QSTile; |
Jason Monk | d5a204f | 2015-12-21 08:50:01 -0500 | [diff] [blame] | 38 | import com.android.systemui.qs.external.CustomTile; |
Jason Monk | 7e53f20 | 2016-01-28 10:40:20 -0500 | [diff] [blame] | 39 | import com.android.systemui.qs.external.TileLifecycleManager; |
Jason Monk | d5a204f | 2015-12-21 08:50:01 -0500 | [diff] [blame] | 40 | import com.android.systemui.qs.external.TileServices; |
Jason Monk | bbadff8 | 2015-11-06 15:47:26 -0500 | [diff] [blame] | 41 | import com.android.systemui.qs.tiles.AirplaneModeTile; |
| 42 | import com.android.systemui.qs.tiles.BatteryTile; |
| 43 | import com.android.systemui.qs.tiles.BluetoothTile; |
| 44 | import com.android.systemui.qs.tiles.CastTile; |
| 45 | import com.android.systemui.qs.tiles.CellularTile; |
| 46 | import com.android.systemui.qs.tiles.ColorInversionTile; |
Jason Monk | 9a4ce13 | 2016-01-21 15:27:17 -0500 | [diff] [blame] | 47 | import com.android.systemui.qs.tiles.DataSaverTile; |
Jason Monk | bbadff8 | 2015-11-06 15:47:26 -0500 | [diff] [blame] | 48 | import com.android.systemui.qs.tiles.DndTile; |
| 49 | import com.android.systemui.qs.tiles.FlashlightTile; |
| 50 | import com.android.systemui.qs.tiles.HotspotTile; |
| 51 | import com.android.systemui.qs.tiles.IntentTile; |
| 52 | import com.android.systemui.qs.tiles.LocationTile; |
Jason Monk | bbadff8 | 2015-11-06 15:47:26 -0500 | [diff] [blame] | 53 | import com.android.systemui.qs.tiles.RotationLockTile; |
| 54 | import com.android.systemui.qs.tiles.UserTile; |
| 55 | import com.android.systemui.qs.tiles.WifiTile; |
Rubin Xu | 1205fb1 | 2015-11-04 17:45:03 +0000 | [diff] [blame] | 56 | import com.android.systemui.qs.tiles.WorkModeTile; |
Jason Monk | bbadff8 | 2015-11-06 15:47:26 -0500 | [diff] [blame] | 57 | import com.android.systemui.statusbar.policy.BatteryController; |
| 58 | import com.android.systemui.statusbar.policy.BluetoothController; |
| 59 | import com.android.systemui.statusbar.policy.CastController; |
Jason Monk | 5dbd4aa | 2016-02-07 13:13:39 -0500 | [diff] [blame] | 60 | import com.android.systemui.statusbar.policy.NightModeController; |
Jason Monk | bbadff8 | 2015-11-06 15:47:26 -0500 | [diff] [blame] | 61 | import com.android.systemui.statusbar.policy.FlashlightController; |
| 62 | import com.android.systemui.statusbar.policy.HotspotController; |
| 63 | import com.android.systemui.statusbar.policy.KeyguardMonitor; |
| 64 | import com.android.systemui.statusbar.policy.LocationController; |
| 65 | import com.android.systemui.statusbar.policy.NetworkController; |
| 66 | import com.android.systemui.statusbar.policy.RotationLockController; |
| 67 | import com.android.systemui.statusbar.policy.SecurityController; |
| 68 | import com.android.systemui.statusbar.policy.UserInfoController; |
| 69 | import com.android.systemui.statusbar.policy.UserSwitcherController; |
| 70 | import com.android.systemui.statusbar.policy.ZenModeController; |
Jason Monk | 5dbd4aa | 2016-02-07 13:13:39 -0500 | [diff] [blame] | 71 | import com.android.systemui.tuner.NightModeTile; |
Jason Monk | 5e74517 | 2015-06-02 19:14:44 -0400 | [diff] [blame] | 72 | import com.android.systemui.tuner.TunerService; |
| 73 | import com.android.systemui.tuner.TunerService.Tunable; |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 74 | |
| 75 | import java.util.ArrayList; |
John Spurlock | bceed06 | 2014-08-10 18:04:16 -0400 | [diff] [blame] | 76 | import java.util.Arrays; |
| 77 | import java.util.Collection; |
| 78 | import java.util.LinkedHashMap; |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 79 | import java.util.List; |
John Spurlock | bceed06 | 2014-08-10 18:04:16 -0400 | [diff] [blame] | 80 | import java.util.Map; |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 81 | |
| 82 | /** Platform implementation of the quick settings tile host **/ |
Jason Monk | d5a204f | 2015-12-21 08:50:01 -0500 | [diff] [blame] | 83 | public final class QSTileHost implements QSTile.Host, Tunable { |
John Spurlock | bceed06 | 2014-08-10 18:04:16 -0400 | [diff] [blame] | 84 | private static final String TAG = "QSTileHost"; |
| 85 | private static final boolean DEBUG = Log.isLoggable(TAG, Log.DEBUG); |
| 86 | |
Jason Monk | caf3762 | 2015-08-18 12:33:50 -0400 | [diff] [blame] | 87 | public static final String TILES_SETTING = "sysui_qs_tiles"; |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 88 | |
| 89 | private final Context mContext; |
| 90 | private final PhoneStatusBar mStatusBar; |
John Spurlock | bceed06 | 2014-08-10 18:04:16 -0400 | [diff] [blame] | 91 | private final LinkedHashMap<String, QSTile<?>> mTiles = new LinkedHashMap<>(); |
Jason Monk | 5e74517 | 2015-06-02 19:14:44 -0400 | [diff] [blame] | 92 | protected final ArrayList<String> mTileSpecs = new ArrayList<>(); |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 93 | private final BluetoothController mBluetooth; |
| 94 | private final LocationController mLocation; |
| 95 | private final RotationLockController mRotation; |
| 96 | private final NetworkController mNetwork; |
| 97 | private final ZenModeController mZen; |
Jason Monk | 51e4dc0 | 2014-07-22 12:00:47 -0400 | [diff] [blame] | 98 | private final HotspotController mHotspot; |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 99 | private final CastController mCast; |
| 100 | private final Looper mLooper; |
Adrian Roos | b83777b | 2014-06-30 15:11:53 +0200 | [diff] [blame] | 101 | private final FlashlightController mFlashlight; |
Adrian Roos | 00a0b1f | 2014-07-16 16:44:49 +0200 | [diff] [blame] | 102 | private final UserSwitcherController mUserSwitcherController; |
Jason Monk | abe1974 | 2015-09-29 09:47:06 -0400 | [diff] [blame] | 103 | private final UserInfoController mUserInfoController; |
John Spurlock | 657c62c | 2014-07-22 12:18:09 -0400 | [diff] [blame] | 104 | private final KeyguardMonitor mKeyguard; |
Jason Monk | 3d5f551 | 2014-07-25 11:17:28 -0400 | [diff] [blame] | 105 | private final SecurityController mSecurity; |
Jason Monk | abe1974 | 2015-09-29 09:47:06 -0400 | [diff] [blame] | 106 | private final BatteryController mBattery; |
Jason Monk | 66c89c1 | 2016-01-06 08:51:26 -0500 | [diff] [blame] | 107 | private final StatusBarIconController mIconController; |
Jason Monk | d5a204f | 2015-12-21 08:50:01 -0500 | [diff] [blame] | 108 | private final TileServices mServices; |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 109 | |
Jason Monk | bbadff8 | 2015-11-06 15:47:26 -0500 | [diff] [blame] | 110 | private final List<Callback> mCallbacks = new ArrayList<>(); |
Jason Monk | 5dbd4aa | 2016-02-07 13:13:39 -0500 | [diff] [blame] | 111 | private final NightModeController mNightModeController; |
Jason Monk | c3f42c1 | 2016-02-05 12:33:13 -0500 | [diff] [blame] | 112 | private final AutoTileManager mAutoTiles; |
| 113 | private final ManagedProfileController mProfileController; |
Jason Monk | ca894a0 | 2016-01-12 15:30:22 -0500 | [diff] [blame] | 114 | private View mHeader; |
John Spurlock | bceed06 | 2014-08-10 18:04:16 -0400 | [diff] [blame] | 115 | |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 116 | public QSTileHost(Context context, PhoneStatusBar statusBar, |
Jason Monk | 66c89c1 | 2016-01-06 08:51:26 -0500 | [diff] [blame] | 117 | BluetoothController bluetooth, LocationController location, |
| 118 | RotationLockController rotation, NetworkController network, |
| 119 | ZenModeController zen, HotspotController hotspot, |
| 120 | CastController cast, FlashlightController flashlight, |
| 121 | UserSwitcherController userSwitcher, UserInfoController userInfo, |
| 122 | KeyguardMonitor keyguard, SecurityController security, |
| 123 | BatteryController battery, StatusBarIconController iconController) { |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 124 | mContext = context; |
| 125 | mStatusBar = statusBar; |
| 126 | mBluetooth = bluetooth; |
| 127 | mLocation = location; |
| 128 | mRotation = rotation; |
| 129 | mNetwork = network; |
| 130 | mZen = zen; |
Jason Monk | 51e4dc0 | 2014-07-22 12:00:47 -0400 | [diff] [blame] | 131 | mHotspot = hotspot; |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 132 | mCast = cast; |
Adrian Roos | b83777b | 2014-06-30 15:11:53 +0200 | [diff] [blame] | 133 | mFlashlight = flashlight; |
Adrian Roos | 00a0b1f | 2014-07-16 16:44:49 +0200 | [diff] [blame] | 134 | mUserSwitcherController = userSwitcher; |
Jason Monk | abe1974 | 2015-09-29 09:47:06 -0400 | [diff] [blame] | 135 | mUserInfoController = userInfo; |
John Spurlock | 657c62c | 2014-07-22 12:18:09 -0400 | [diff] [blame] | 136 | mKeyguard = keyguard; |
Jason Monk | 3d5f551 | 2014-07-25 11:17:28 -0400 | [diff] [blame] | 137 | mSecurity = security; |
Jason Monk | abe1974 | 2015-09-29 09:47:06 -0400 | [diff] [blame] | 138 | mBattery = battery; |
Jason Monk | 66c89c1 | 2016-01-06 08:51:26 -0500 | [diff] [blame] | 139 | mIconController = iconController; |
Jason Monk | 5dbd4aa | 2016-02-07 13:13:39 -0500 | [diff] [blame] | 140 | mNightModeController = new NightModeController(mContext, true); |
Jason Monk | c3f42c1 | 2016-02-05 12:33:13 -0500 | [diff] [blame] | 141 | mProfileController = new ManagedProfileController(this); |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 142 | |
Jorim Jaggi | 1ecd7cd | 2014-11-03 16:18:03 +0100 | [diff] [blame] | 143 | final HandlerThread ht = new HandlerThread(QSTileHost.class.getSimpleName(), |
| 144 | Process.THREAD_PRIORITY_BACKGROUND); |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 145 | ht.start(); |
| 146 | mLooper = ht.getLooper(); |
| 147 | |
Jason Monk | d5a204f | 2015-12-21 08:50:01 -0500 | [diff] [blame] | 148 | mServices = new TileServices(this, mLooper); |
| 149 | |
Jason Monk | 5e74517 | 2015-06-02 19:14:44 -0400 | [diff] [blame] | 150 | TunerService.get(mContext).addTunable(this, TILES_SETTING); |
Rubin Xu | cedcd28 | 2016-02-15 14:44:44 +0000 | [diff] [blame] | 151 | // AutoTileManager can modify mTiles so make sure mTiles has already been initialized. |
| 152 | mAutoTiles = new AutoTileManager(context, this); |
Jason Monk | 5e74517 | 2015-06-02 19:14:44 -0400 | [diff] [blame] | 153 | } |
John Spurlock | bceed06 | 2014-08-10 18:04:16 -0400 | [diff] [blame] | 154 | |
Jason Monk | ca894a0 | 2016-01-12 15:30:22 -0500 | [diff] [blame] | 155 | public void setHeaderView(View view) { |
| 156 | mHeader = view; |
| 157 | } |
| 158 | |
Jason Monk | deba7a4 | 2015-12-08 16:14:10 -0500 | [diff] [blame] | 159 | public PhoneStatusBar getPhoneStatusBar() { |
Jason Monk | c34befb | 2015-10-07 16:40:02 -0400 | [diff] [blame] | 160 | return mStatusBar; |
| 161 | } |
| 162 | |
Jason Monk | 5e74517 | 2015-06-02 19:14:44 -0400 | [diff] [blame] | 163 | public void destroy() { |
Jason Monk | c3f42c1 | 2016-02-05 12:33:13 -0500 | [diff] [blame] | 164 | mAutoTiles.destroy(); |
Jason Monk | 5e74517 | 2015-06-02 19:14:44 -0400 | [diff] [blame] | 165 | TunerService.get(mContext).removeTunable(this); |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 166 | } |
| 167 | |
| 168 | @Override |
Jason Monk | bbadff8 | 2015-11-06 15:47:26 -0500 | [diff] [blame] | 169 | public void addCallback(Callback callback) { |
| 170 | mCallbacks.add(callback); |
John Spurlock | bceed06 | 2014-08-10 18:04:16 -0400 | [diff] [blame] | 171 | } |
| 172 | |
| 173 | @Override |
| 174 | public Collection<QSTile<?>> getTiles() { |
| 175 | return mTiles.values(); |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 176 | } |
| 177 | |
| 178 | @Override |
Jason Monk | ee43cdf | 2015-06-19 14:20:46 -0400 | [diff] [blame] | 179 | public void startActivityDismissingKeyguard(final Intent intent) { |
| 180 | mStatusBar.postStartActivityDismissingKeyguard(intent, 0); |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 181 | } |
| 182 | |
| 183 | @Override |
Adrian Roos | 62692b2 | 2015-09-11 17:46:23 -0700 | [diff] [blame] | 184 | public void startActivityDismissingKeyguard(PendingIntent intent) { |
| 185 | mStatusBar.postStartActivityDismissingKeyguard(intent); |
| 186 | } |
| 187 | |
| 188 | @Override |
Jason Monk | ba2318e | 2015-12-08 09:04:23 -0500 | [diff] [blame] | 189 | public void startRunnableDismissingKeyguard(Runnable runnable) { |
| 190 | mStatusBar.postQSRunnableDismissingKeyguard(runnable); |
| 191 | } |
| 192 | |
| 193 | @Override |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 194 | public void warn(String message, Throwable t) { |
| 195 | // already logged |
| 196 | } |
| 197 | |
Jason Monk | ca894a0 | 2016-01-12 15:30:22 -0500 | [diff] [blame] | 198 | public void animateExpandQS() { |
| 199 | // TODO: Better path to animated panel expansion. |
| 200 | mHeader.performClick(); |
| 201 | } |
| 202 | |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 203 | @Override |
| 204 | public void collapsePanels() { |
| 205 | mStatusBar.postAnimateCollapsePanels(); |
| 206 | } |
| 207 | |
| 208 | @Override |
Jason Monk | ba2318e | 2015-12-08 09:04:23 -0500 | [diff] [blame] | 209 | public void openPanels() { |
| 210 | mStatusBar.postAnimateOpenPanels(); |
| 211 | } |
| 212 | |
| 213 | @Override |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 214 | public Looper getLooper() { |
| 215 | return mLooper; |
| 216 | } |
| 217 | |
| 218 | @Override |
| 219 | public Context getContext() { |
| 220 | return mContext; |
| 221 | } |
| 222 | |
| 223 | @Override |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 224 | public BluetoothController getBluetoothController() { |
| 225 | return mBluetooth; |
| 226 | } |
| 227 | |
| 228 | @Override |
| 229 | public LocationController getLocationController() { |
| 230 | return mLocation; |
| 231 | } |
| 232 | |
| 233 | @Override |
| 234 | public RotationLockController getRotationLockController() { |
| 235 | return mRotation; |
| 236 | } |
| 237 | |
| 238 | @Override |
| 239 | public NetworkController getNetworkController() { |
| 240 | return mNetwork; |
| 241 | } |
| 242 | |
| 243 | @Override |
| 244 | public ZenModeController getZenModeController() { |
| 245 | return mZen; |
| 246 | } |
| 247 | |
| 248 | @Override |
Jason Monk | 51e4dc0 | 2014-07-22 12:00:47 -0400 | [diff] [blame] | 249 | public HotspotController getHotspotController() { |
| 250 | return mHotspot; |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 251 | } |
| 252 | |
| 253 | @Override |
| 254 | public CastController getCastController() { |
| 255 | return mCast; |
| 256 | } |
John Spurlock | 8600534 | 2014-05-23 11:58:00 -0400 | [diff] [blame] | 257 | |
| 258 | @Override |
John Spurlock | 657c62c | 2014-07-22 12:18:09 -0400 | [diff] [blame] | 259 | public FlashlightController getFlashlightController() { |
| 260 | return mFlashlight; |
John Spurlock | 8600534 | 2014-05-23 11:58:00 -0400 | [diff] [blame] | 261 | } |
Adrian Roos | b83777b | 2014-06-30 15:11:53 +0200 | [diff] [blame] | 262 | |
| 263 | @Override |
John Spurlock | 657c62c | 2014-07-22 12:18:09 -0400 | [diff] [blame] | 264 | public KeyguardMonitor getKeyguardMonitor() { |
| 265 | return mKeyguard; |
Adrian Roos | b83777b | 2014-06-30 15:11:53 +0200 | [diff] [blame] | 266 | } |
Adrian Roos | 00a0b1f | 2014-07-16 16:44:49 +0200 | [diff] [blame] | 267 | |
Jason Monk | abe1974 | 2015-09-29 09:47:06 -0400 | [diff] [blame] | 268 | @Override |
Adrian Roos | 00a0b1f | 2014-07-16 16:44:49 +0200 | [diff] [blame] | 269 | public UserSwitcherController getUserSwitcherController() { |
| 270 | return mUserSwitcherController; |
| 271 | } |
Jason Monk | 3d5f551 | 2014-07-25 11:17:28 -0400 | [diff] [blame] | 272 | |
Jason Monk | abe1974 | 2015-09-29 09:47:06 -0400 | [diff] [blame] | 273 | @Override |
| 274 | public UserInfoController getUserInfoController() { |
| 275 | return mUserInfoController; |
| 276 | } |
| 277 | |
| 278 | @Override |
| 279 | public BatteryController getBatteryController() { |
| 280 | return mBattery; |
| 281 | } |
| 282 | |
Jason Monk | 3d5f551 | 2014-07-25 11:17:28 -0400 | [diff] [blame] | 283 | public SecurityController getSecurityController() { |
| 284 | return mSecurity; |
| 285 | } |
Jason Monk | bbadff8 | 2015-11-06 15:47:26 -0500 | [diff] [blame] | 286 | |
Jason Monk | d5a204f | 2015-12-21 08:50:01 -0500 | [diff] [blame] | 287 | public TileServices getTileServices() { |
| 288 | return mServices; |
| 289 | } |
| 290 | |
Jason Monk | 66c89c1 | 2016-01-06 08:51:26 -0500 | [diff] [blame] | 291 | public StatusBarIconController getIconController() { |
| 292 | return mIconController; |
| 293 | } |
| 294 | |
Jason Monk | 5dbd4aa | 2016-02-07 13:13:39 -0500 | [diff] [blame] | 295 | public NightModeController getNightModeController() { |
| 296 | return mNightModeController; |
Jason Monk | f0c6f64 | 2016-01-20 22:24:50 -0500 | [diff] [blame] | 297 | } |
| 298 | |
Jason Monk | c3f42c1 | 2016-02-05 12:33:13 -0500 | [diff] [blame] | 299 | public ManagedProfileController getManagedProfileController() { |
| 300 | return mProfileController; |
| 301 | } |
| 302 | |
Jason Monk | 5e74517 | 2015-06-02 19:14:44 -0400 | [diff] [blame] | 303 | @Override |
| 304 | public void onTuningChanged(String key, String newValue) { |
| 305 | if (!TILES_SETTING.equals(key)) { |
| 306 | return; |
| 307 | } |
John Spurlock | bceed06 | 2014-08-10 18:04:16 -0400 | [diff] [blame] | 308 | if (DEBUG) Log.d(TAG, "Recreating tiles"); |
Jason Monk | bbadff8 | 2015-11-06 15:47:26 -0500 | [diff] [blame] | 309 | final List<String> tileSpecs = loadTileSpecs(mContext, newValue); |
Jason Monk | 11a7744 | 2015-05-12 19:29:02 -0400 | [diff] [blame] | 310 | if (tileSpecs.equals(mTileSpecs)) return; |
John Spurlock | bceed06 | 2014-08-10 18:04:16 -0400 | [diff] [blame] | 311 | for (Map.Entry<String, QSTile<?>> tile : mTiles.entrySet()) { |
| 312 | if (!tileSpecs.contains(tile.getKey())) { |
| 313 | if (DEBUG) Log.d(TAG, "Destroying tile: " + tile.getKey()); |
| 314 | tile.getValue().destroy(); |
| 315 | } |
| 316 | } |
| 317 | final LinkedHashMap<String, QSTile<?>> newTiles = new LinkedHashMap<>(); |
| 318 | for (String tileSpec : tileSpecs) { |
| 319 | if (mTiles.containsKey(tileSpec)) { |
Jason Monk | bbadff8 | 2015-11-06 15:47:26 -0500 | [diff] [blame] | 320 | QSTile<?> tile = mTiles.get(tileSpec); |
| 321 | if (DEBUG) Log.d(TAG, "Adding " + tile); |
Jason Monk | 9d02a43 | 2016-01-20 16:33:46 -0500 | [diff] [blame] | 322 | tile.removeCallbacks(); |
Jason Monk | bbadff8 | 2015-11-06 15:47:26 -0500 | [diff] [blame] | 323 | newTiles.put(tileSpec, tile); |
John Spurlock | bceed06 | 2014-08-10 18:04:16 -0400 | [diff] [blame] | 324 | } else { |
| 325 | if (DEBUG) Log.d(TAG, "Creating tile: " + tileSpec); |
| 326 | try { |
Jason Monk | bbadff8 | 2015-11-06 15:47:26 -0500 | [diff] [blame] | 327 | QSTile<?> tile = createTile(tileSpec); |
Jason Monk | c3f42c1 | 2016-02-05 12:33:13 -0500 | [diff] [blame] | 328 | if (tile != null && tile.isAvailable()) { |
Jason Monk | ba2318e | 2015-12-08 09:04:23 -0500 | [diff] [blame] | 329 | tile.setTileSpec(tileSpec); |
| 330 | newTiles.put(tileSpec, tile); |
| 331 | } |
John Spurlock | bceed06 | 2014-08-10 18:04:16 -0400 | [diff] [blame] | 332 | } catch (Throwable t) { |
| 333 | Log.w(TAG, "Error creating tile for spec: " + tileSpec, t); |
| 334 | } |
| 335 | } |
| 336 | } |
Jason Monk | 11a7744 | 2015-05-12 19:29:02 -0400 | [diff] [blame] | 337 | mTileSpecs.clear(); |
| 338 | mTileSpecs.addAll(tileSpecs); |
John Spurlock | bceed06 | 2014-08-10 18:04:16 -0400 | [diff] [blame] | 339 | mTiles.clear(); |
| 340 | mTiles.putAll(newTiles); |
Jason Monk | bbadff8 | 2015-11-06 15:47:26 -0500 | [diff] [blame] | 341 | for (int i = 0; i < mCallbacks.size(); i++) { |
| 342 | mCallbacks.get(i).onTilesChanged(); |
John Spurlock | bceed06 | 2014-08-10 18:04:16 -0400 | [diff] [blame] | 343 | } |
| 344 | } |
| 345 | |
Jason Monk | bbadff8 | 2015-11-06 15:47:26 -0500 | [diff] [blame] | 346 | @Override |
Jason Monk | ba2318e | 2015-12-08 09:04:23 -0500 | [diff] [blame] | 347 | public void removeTile(String tileSpec) { |
| 348 | ArrayList<String> specs = new ArrayList<>(mTileSpecs); |
| 349 | specs.remove(tileSpec); |
| 350 | Settings.Secure.putStringForUser(mContext.getContentResolver(), TILES_SETTING, |
| 351 | TextUtils.join(",", specs), ActivityManager.getCurrentUser()); |
| 352 | } |
| 353 | |
Jason Monk | c3f42c1 | 2016-02-05 12:33:13 -0500 | [diff] [blame] | 354 | public void addTile(String spec) { |
| 355 | if (mTileSpecs.contains(spec)) { |
| 356 | return; |
| 357 | } |
| 358 | ArrayList<String> specs = new ArrayList<>(mTileSpecs); |
| 359 | specs.add(spec); |
| 360 | Settings.Secure.putStringForUser(mContext.getContentResolver(), TILES_SETTING, |
| 361 | TextUtils.join(",", specs), ActivityManager.getCurrentUser()); |
| 362 | } |
| 363 | |
Jason Monk | 7e53f20 | 2016-01-28 10:40:20 -0500 | [diff] [blame] | 364 | public void addTile(ComponentName tile) { |
| 365 | List<String> newSpecs = new ArrayList<>(mTileSpecs); |
| 366 | newSpecs.add(0, CustomTile.toSpec(tile)); |
| 367 | changeTiles(mTileSpecs, newSpecs); |
| 368 | } |
| 369 | |
| 370 | public void removeTile(ComponentName tile) { |
| 371 | List<String> newSpecs = new ArrayList<>(mTileSpecs); |
| 372 | newSpecs.remove(CustomTile.toSpec(tile)); |
| 373 | changeTiles(mTileSpecs, newSpecs); |
| 374 | } |
| 375 | |
| 376 | public void changeTiles(List<String> previousTiles, List<String> newTiles) { |
| 377 | final int NP = previousTiles.size(); |
| 378 | final int NA = newTiles.size(); |
| 379 | for (int i = 0; i < NP; i++) { |
| 380 | String tileSpec = previousTiles.get(i); |
| 381 | if (!tileSpec.startsWith(CustomTile.PREFIX)) continue; |
| 382 | if (!newTiles.contains(tileSpec)) { |
| 383 | Intent intent = new Intent().setComponent( |
| 384 | CustomTile.getComponentFromSpec(tileSpec)); |
| 385 | TileLifecycleManager lifecycleManager = new TileLifecycleManager(new Handler(), |
| 386 | mContext, intent, new UserHandle(ActivityManager.getCurrentUser())); |
| 387 | lifecycleManager.onStopListening(); |
| 388 | lifecycleManager.onTileRemoved(); |
| 389 | lifecycleManager.flushMessagesAndUnbind(); |
| 390 | } |
| 391 | } |
| 392 | for (int i = 0; i < NA; i++) { |
| 393 | String tileSpec = newTiles.get(i); |
| 394 | if (!tileSpec.startsWith(CustomTile.PREFIX)) continue; |
| 395 | if (!previousTiles.contains(tileSpec)) { |
| 396 | Intent intent = new Intent().setComponent( |
| 397 | CustomTile.getComponentFromSpec(tileSpec)); |
| 398 | TileLifecycleManager lifecycleManager = new TileLifecycleManager(new Handler(), |
| 399 | mContext, intent, new UserHandle(ActivityManager.getCurrentUser())); |
| 400 | lifecycleManager.onTileAdded(); |
| 401 | lifecycleManager.flushMessagesAndUnbind(); |
| 402 | } |
| 403 | } |
| 404 | if (DEBUG) Log.d(TAG, "saveCurrentTiles " + newTiles); |
| 405 | Secure.putStringForUser(getContext().getContentResolver(), QSTileHost.TILES_SETTING, |
| 406 | TextUtils.join(",", newTiles), ActivityManager.getCurrentUser()); |
| 407 | } |
| 408 | |
Jason Monk | 5db8a41 | 2015-10-21 15:16:23 -0700 | [diff] [blame] | 409 | public QSTile<?> createTile(String tileSpec) { |
Jason Monk | c3f42c1 | 2016-02-05 12:33:13 -0500 | [diff] [blame] | 410 | if (tileSpec.equals("wifi")) return new WifiTile(this); |
| 411 | else if (tileSpec.equals("bt")) return new BluetoothTile(this); |
| 412 | else if (tileSpec.equals("cell")) return new CellularTile(this); |
| 413 | else if (tileSpec.equals("dnd")) return new DndTile(this); |
John Spurlock | bceed06 | 2014-08-10 18:04:16 -0400 | [diff] [blame] | 414 | else if (tileSpec.equals("inversion")) return new ColorInversionTile(this); |
John Spurlock | bceed06 | 2014-08-10 18:04:16 -0400 | [diff] [blame] | 415 | else if (tileSpec.equals("airplane")) return new AirplaneModeTile(this); |
Rubin Xu | 1205fb1 | 2015-11-04 17:45:03 +0000 | [diff] [blame] | 416 | else if (tileSpec.equals("work")) return new WorkModeTile(this); |
John Spurlock | bceed06 | 2014-08-10 18:04:16 -0400 | [diff] [blame] | 417 | else if (tileSpec.equals("rotation")) return new RotationLockTile(this); |
| 418 | else if (tileSpec.equals("flashlight")) return new FlashlightTile(this); |
| 419 | else if (tileSpec.equals("location")) return new LocationTile(this); |
| 420 | else if (tileSpec.equals("cast")) return new CastTile(this); |
| 421 | else if (tileSpec.equals("hotspot")) return new HotspotTile(this); |
Jason Monk | abe1974 | 2015-09-29 09:47:06 -0400 | [diff] [blame] | 422 | else if (tileSpec.equals("user")) return new UserTile(this); |
| 423 | else if (tileSpec.equals("battery")) return new BatteryTile(this); |
Jason Monk | 9a4ce13 | 2016-01-21 15:27:17 -0500 | [diff] [blame] | 424 | else if (tileSpec.equals("saver")) return new DataSaverTile(this); |
Jason Monk | 5dbd4aa | 2016-02-07 13:13:39 -0500 | [diff] [blame] | 425 | else if (tileSpec.equals(NightModeTile.NIGHT_MODE_SPEC)) |
| 426 | return new NightModeTile(this); |
Jason Monk | caf3762 | 2015-08-18 12:33:50 -0400 | [diff] [blame] | 427 | // Intent tiles. |
John Spurlock | bceed06 | 2014-08-10 18:04:16 -0400 | [diff] [blame] | 428 | else if (tileSpec.startsWith(IntentTile.PREFIX)) return IntentTile.create(this,tileSpec); |
Jason Monk | 5db8a41 | 2015-10-21 15:16:23 -0700 | [diff] [blame] | 429 | else if (tileSpec.startsWith(CustomTile.PREFIX)) return CustomTile.create(this,tileSpec); |
Jason Monk | 2b37190f | 2016-01-06 10:19:04 -0500 | [diff] [blame] | 430 | else { |
| 431 | Log.w(TAG, "Bad tile spec: " + tileSpec); |
| 432 | return null; |
| 433 | } |
John Spurlock | bceed06 | 2014-08-10 18:04:16 -0400 | [diff] [blame] | 434 | } |
| 435 | |
Jason Monk | bbadff8 | 2015-11-06 15:47:26 -0500 | [diff] [blame] | 436 | public static List<String> loadTileSpecs(Context context, String tileList) { |
| 437 | final Resources res = context.getResources(); |
John Spurlock | bceed06 | 2014-08-10 18:04:16 -0400 | [diff] [blame] | 438 | final String defaultTileList = res.getString(R.string.quick_settings_tiles_default); |
John Spurlock | bceed06 | 2014-08-10 18:04:16 -0400 | [diff] [blame] | 439 | if (tileList == null) { |
| 440 | tileList = res.getString(R.string.quick_settings_tiles); |
| 441 | if (DEBUG) Log.d(TAG, "Loaded tile specs from config: " + tileList); |
| 442 | } else { |
| 443 | if (DEBUG) Log.d(TAG, "Loaded tile specs from setting: " + tileList); |
| 444 | } |
| 445 | final ArrayList<String> tiles = new ArrayList<String>(); |
| 446 | boolean addedDefault = false; |
| 447 | for (String tile : tileList.split(",")) { |
| 448 | tile = tile.trim(); |
| 449 | if (tile.isEmpty()) continue; |
| 450 | if (tile.equals("default")) { |
| 451 | if (!addedDefault) { |
| 452 | tiles.addAll(Arrays.asList(defaultTileList.split(","))); |
| 453 | addedDefault = true; |
| 454 | } |
| 455 | } else { |
| 456 | tiles.add(tile); |
| 457 | } |
| 458 | } |
| 459 | return tiles; |
| 460 | } |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 461 | } |