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 | |
Adrian Roos | f89e800 | 2015-09-11 17:46:23 -0700 | [diff] [blame] | 19 | import android.app.PendingIntent; |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 20 | import android.content.Context; |
| 21 | import android.content.Intent; |
John Spurlock | bceed06 | 2014-08-10 18:04:16 -0400 | [diff] [blame] | 22 | import android.content.res.Resources; |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 23 | import android.os.HandlerThread; |
| 24 | import android.os.Looper; |
Jason Monk | 5e74517 | 2015-06-02 19:14:44 -0400 | [diff] [blame] | 25 | import android.os.Process; |
John Spurlock | bceed06 | 2014-08-10 18:04:16 -0400 | [diff] [blame] | 26 | import android.util.Log; |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 27 | |
John Spurlock | bceed06 | 2014-08-10 18:04:16 -0400 | [diff] [blame] | 28 | import com.android.systemui.R; |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 29 | import com.android.systemui.qs.QSTile; |
| 30 | import com.android.systemui.qs.tiles.AirplaneModeTile; |
| 31 | import com.android.systemui.qs.tiles.BluetoothTile; |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 32 | import com.android.systemui.qs.tiles.CastTile; |
| 33 | import com.android.systemui.qs.tiles.CellularTile; |
| 34 | import com.android.systemui.qs.tiles.ColorInversionTile; |
Jason Monk | 5e74517 | 2015-06-02 19:14:44 -0400 | [diff] [blame] | 35 | import com.android.systemui.qs.tiles.DndTile; |
Adrian Roos | b83777b | 2014-06-30 15:11:53 +0200 | [diff] [blame] | 36 | import com.android.systemui.qs.tiles.FlashlightTile; |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 37 | import com.android.systemui.qs.tiles.HotspotTile; |
John Spurlock | bceed06 | 2014-08-10 18:04:16 -0400 | [diff] [blame] | 38 | import com.android.systemui.qs.tiles.IntentTile; |
John Spurlock | ae641c9 | 2014-06-30 18:11:40 -0400 | [diff] [blame] | 39 | import com.android.systemui.qs.tiles.LocationTile; |
| 40 | import com.android.systemui.qs.tiles.RotationLockTile; |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 41 | import com.android.systemui.qs.tiles.WifiTile; |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 42 | import com.android.systemui.statusbar.policy.BluetoothController; |
| 43 | import com.android.systemui.statusbar.policy.CastController; |
Adrian Roos | b83777b | 2014-06-30 15:11:53 +0200 | [diff] [blame] | 44 | import com.android.systemui.statusbar.policy.FlashlightController; |
Jason Monk | 5e74517 | 2015-06-02 19:14:44 -0400 | [diff] [blame] | 45 | import com.android.systemui.statusbar.policy.HotspotController; |
John Spurlock | 657c62c | 2014-07-22 12:18:09 -0400 | [diff] [blame] | 46 | import com.android.systemui.statusbar.policy.KeyguardMonitor; |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 47 | import com.android.systemui.statusbar.policy.LocationController; |
| 48 | import com.android.systemui.statusbar.policy.NetworkController; |
| 49 | import com.android.systemui.statusbar.policy.RotationLockController; |
Jason Monk | 3d5f551 | 2014-07-25 11:17:28 -0400 | [diff] [blame] | 50 | import com.android.systemui.statusbar.policy.SecurityController; |
Adrian Roos | 00a0b1f | 2014-07-16 16:44:49 +0200 | [diff] [blame] | 51 | import com.android.systemui.statusbar.policy.UserSwitcherController; |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 52 | import com.android.systemui.statusbar.policy.ZenModeController; |
Jason Monk | 5e74517 | 2015-06-02 19:14:44 -0400 | [diff] [blame] | 53 | import com.android.systemui.tuner.TunerService; |
| 54 | import com.android.systemui.tuner.TunerService.Tunable; |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 55 | |
| 56 | import java.util.ArrayList; |
John Spurlock | bceed06 | 2014-08-10 18:04:16 -0400 | [diff] [blame] | 57 | import java.util.Arrays; |
| 58 | import java.util.Collection; |
| 59 | import java.util.LinkedHashMap; |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 60 | import java.util.List; |
John Spurlock | bceed06 | 2014-08-10 18:04:16 -0400 | [diff] [blame] | 61 | import java.util.Map; |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 62 | |
| 63 | /** Platform implementation of the quick settings tile host **/ |
Jason Monk | 5e74517 | 2015-06-02 19:14:44 -0400 | [diff] [blame] | 64 | public class QSTileHost implements QSTile.Host, Tunable { |
John Spurlock | bceed06 | 2014-08-10 18:04:16 -0400 | [diff] [blame] | 65 | private static final String TAG = "QSTileHost"; |
| 66 | private static final boolean DEBUG = Log.isLoggable(TAG, Log.DEBUG); |
| 67 | |
Jason Monk | 11a7744 | 2015-05-12 19:29:02 -0400 | [diff] [blame] | 68 | protected static final String TILES_SETTING = "sysui_qs_tiles"; |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 69 | |
| 70 | private final Context mContext; |
| 71 | private final PhoneStatusBar mStatusBar; |
John Spurlock | bceed06 | 2014-08-10 18:04:16 -0400 | [diff] [blame] | 72 | private final LinkedHashMap<String, QSTile<?>> mTiles = new LinkedHashMap<>(); |
Jason Monk | 5e74517 | 2015-06-02 19:14:44 -0400 | [diff] [blame] | 73 | protected final ArrayList<String> mTileSpecs = new ArrayList<>(); |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 74 | private final BluetoothController mBluetooth; |
| 75 | private final LocationController mLocation; |
| 76 | private final RotationLockController mRotation; |
| 77 | private final NetworkController mNetwork; |
| 78 | private final ZenModeController mZen; |
Jason Monk | 51e4dc0 | 2014-07-22 12:00:47 -0400 | [diff] [blame] | 79 | private final HotspotController mHotspot; |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 80 | private final CastController mCast; |
| 81 | private final Looper mLooper; |
Adrian Roos | b83777b | 2014-06-30 15:11:53 +0200 | [diff] [blame] | 82 | private final FlashlightController mFlashlight; |
Adrian Roos | 00a0b1f | 2014-07-16 16:44:49 +0200 | [diff] [blame] | 83 | private final UserSwitcherController mUserSwitcherController; |
John Spurlock | 657c62c | 2014-07-22 12:18:09 -0400 | [diff] [blame] | 84 | private final KeyguardMonitor mKeyguard; |
Jason Monk | 3d5f551 | 2014-07-25 11:17:28 -0400 | [diff] [blame] | 85 | private final SecurityController mSecurity; |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 86 | |
John Spurlock | bceed06 | 2014-08-10 18:04:16 -0400 | [diff] [blame] | 87 | private Callback mCallback; |
| 88 | |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 89 | public QSTileHost(Context context, PhoneStatusBar statusBar, |
| 90 | BluetoothController bluetooth, LocationController location, |
| 91 | RotationLockController rotation, NetworkController network, |
Jason Monk | 51e4dc0 | 2014-07-22 12:00:47 -0400 | [diff] [blame] | 92 | ZenModeController zen, HotspotController hotspot, |
John Spurlock | 657c62c | 2014-07-22 12:18:09 -0400 | [diff] [blame] | 93 | CastController cast, FlashlightController flashlight, |
Jason Monk | 3d5f551 | 2014-07-25 11:17:28 -0400 | [diff] [blame] | 94 | UserSwitcherController userSwitcher, KeyguardMonitor keyguard, |
| 95 | SecurityController security) { |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 96 | mContext = context; |
| 97 | mStatusBar = statusBar; |
| 98 | mBluetooth = bluetooth; |
| 99 | mLocation = location; |
| 100 | mRotation = rotation; |
| 101 | mNetwork = network; |
| 102 | mZen = zen; |
Jason Monk | 51e4dc0 | 2014-07-22 12:00:47 -0400 | [diff] [blame] | 103 | mHotspot = hotspot; |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 104 | mCast = cast; |
Adrian Roos | b83777b | 2014-06-30 15:11:53 +0200 | [diff] [blame] | 105 | mFlashlight = flashlight; |
Adrian Roos | 00a0b1f | 2014-07-16 16:44:49 +0200 | [diff] [blame] | 106 | mUserSwitcherController = userSwitcher; |
John Spurlock | 657c62c | 2014-07-22 12:18:09 -0400 | [diff] [blame] | 107 | mKeyguard = keyguard; |
Jason Monk | 3d5f551 | 2014-07-25 11:17:28 -0400 | [diff] [blame] | 108 | mSecurity = security; |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 109 | |
Jorim Jaggi | 1ecd7cd | 2014-11-03 16:18:03 +0100 | [diff] [blame] | 110 | final HandlerThread ht = new HandlerThread(QSTileHost.class.getSimpleName(), |
| 111 | Process.THREAD_PRIORITY_BACKGROUND); |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 112 | ht.start(); |
| 113 | mLooper = ht.getLooper(); |
| 114 | |
Jason Monk | 5e74517 | 2015-06-02 19:14:44 -0400 | [diff] [blame] | 115 | TunerService.get(mContext).addTunable(this, TILES_SETTING); |
| 116 | } |
John Spurlock | bceed06 | 2014-08-10 18:04:16 -0400 | [diff] [blame] | 117 | |
Jason Monk | 5e74517 | 2015-06-02 19:14:44 -0400 | [diff] [blame] | 118 | public void destroy() { |
| 119 | TunerService.get(mContext).removeTunable(this); |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 120 | } |
| 121 | |
| 122 | @Override |
John Spurlock | bceed06 | 2014-08-10 18:04:16 -0400 | [diff] [blame] | 123 | public void setCallback(Callback callback) { |
| 124 | mCallback = callback; |
| 125 | } |
| 126 | |
| 127 | @Override |
| 128 | public Collection<QSTile<?>> getTiles() { |
| 129 | return mTiles.values(); |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 130 | } |
| 131 | |
| 132 | @Override |
Jason Monk | ee43cdf | 2015-06-19 14:20:46 -0400 | [diff] [blame] | 133 | public void startActivityDismissingKeyguard(final Intent intent) { |
| 134 | mStatusBar.postStartActivityDismissingKeyguard(intent, 0); |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 135 | } |
| 136 | |
| 137 | @Override |
Adrian Roos | f89e800 | 2015-09-11 17:46:23 -0700 | [diff] [blame] | 138 | public void startActivityDismissingKeyguard(PendingIntent intent) { |
| 139 | mStatusBar.postStartActivityDismissingKeyguard(intent); |
| 140 | } |
| 141 | |
| 142 | @Override |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 143 | public void warn(String message, Throwable t) { |
| 144 | // already logged |
| 145 | } |
| 146 | |
| 147 | @Override |
| 148 | public void collapsePanels() { |
| 149 | mStatusBar.postAnimateCollapsePanels(); |
| 150 | } |
| 151 | |
| 152 | @Override |
| 153 | public Looper getLooper() { |
| 154 | return mLooper; |
| 155 | } |
| 156 | |
| 157 | @Override |
| 158 | public Context getContext() { |
| 159 | return mContext; |
| 160 | } |
| 161 | |
| 162 | @Override |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 163 | public BluetoothController getBluetoothController() { |
| 164 | return mBluetooth; |
| 165 | } |
| 166 | |
| 167 | @Override |
| 168 | public LocationController getLocationController() { |
| 169 | return mLocation; |
| 170 | } |
| 171 | |
| 172 | @Override |
| 173 | public RotationLockController getRotationLockController() { |
| 174 | return mRotation; |
| 175 | } |
| 176 | |
| 177 | @Override |
| 178 | public NetworkController getNetworkController() { |
| 179 | return mNetwork; |
| 180 | } |
| 181 | |
| 182 | @Override |
| 183 | public ZenModeController getZenModeController() { |
| 184 | return mZen; |
| 185 | } |
| 186 | |
| 187 | @Override |
Jason Monk | 51e4dc0 | 2014-07-22 12:00:47 -0400 | [diff] [blame] | 188 | public HotspotController getHotspotController() { |
| 189 | return mHotspot; |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 190 | } |
| 191 | |
| 192 | @Override |
| 193 | public CastController getCastController() { |
| 194 | return mCast; |
| 195 | } |
John Spurlock | 8600534 | 2014-05-23 11:58:00 -0400 | [diff] [blame] | 196 | |
| 197 | @Override |
John Spurlock | 657c62c | 2014-07-22 12:18:09 -0400 | [diff] [blame] | 198 | public FlashlightController getFlashlightController() { |
| 199 | return mFlashlight; |
John Spurlock | 8600534 | 2014-05-23 11:58:00 -0400 | [diff] [blame] | 200 | } |
Adrian Roos | b83777b | 2014-06-30 15:11:53 +0200 | [diff] [blame] | 201 | |
| 202 | @Override |
John Spurlock | 657c62c | 2014-07-22 12:18:09 -0400 | [diff] [blame] | 203 | public KeyguardMonitor getKeyguardMonitor() { |
| 204 | return mKeyguard; |
Adrian Roos | b83777b | 2014-06-30 15:11:53 +0200 | [diff] [blame] | 205 | } |
Adrian Roos | 00a0b1f | 2014-07-16 16:44:49 +0200 | [diff] [blame] | 206 | |
| 207 | public UserSwitcherController getUserSwitcherController() { |
| 208 | return mUserSwitcherController; |
| 209 | } |
Jason Monk | 3d5f551 | 2014-07-25 11:17:28 -0400 | [diff] [blame] | 210 | |
| 211 | public SecurityController getSecurityController() { |
| 212 | return mSecurity; |
| 213 | } |
Jason Monk | 5e74517 | 2015-06-02 19:14:44 -0400 | [diff] [blame] | 214 | |
| 215 | @Override |
| 216 | public void onTuningChanged(String key, String newValue) { |
| 217 | if (!TILES_SETTING.equals(key)) { |
| 218 | return; |
| 219 | } |
John Spurlock | bceed06 | 2014-08-10 18:04:16 -0400 | [diff] [blame] | 220 | if (DEBUG) Log.d(TAG, "Recreating tiles"); |
Jason Monk | 5e74517 | 2015-06-02 19:14:44 -0400 | [diff] [blame] | 221 | final List<String> tileSpecs = loadTileSpecs(newValue); |
Jason Monk | 11a7744 | 2015-05-12 19:29:02 -0400 | [diff] [blame] | 222 | if (tileSpecs.equals(mTileSpecs)) return; |
John Spurlock | bceed06 | 2014-08-10 18:04:16 -0400 | [diff] [blame] | 223 | for (Map.Entry<String, QSTile<?>> tile : mTiles.entrySet()) { |
| 224 | if (!tileSpecs.contains(tile.getKey())) { |
| 225 | if (DEBUG) Log.d(TAG, "Destroying tile: " + tile.getKey()); |
| 226 | tile.getValue().destroy(); |
| 227 | } |
| 228 | } |
| 229 | final LinkedHashMap<String, QSTile<?>> newTiles = new LinkedHashMap<>(); |
| 230 | for (String tileSpec : tileSpecs) { |
| 231 | if (mTiles.containsKey(tileSpec)) { |
| 232 | newTiles.put(tileSpec, mTiles.get(tileSpec)); |
| 233 | } else { |
| 234 | if (DEBUG) Log.d(TAG, "Creating tile: " + tileSpec); |
| 235 | try { |
| 236 | newTiles.put(tileSpec, createTile(tileSpec)); |
| 237 | } catch (Throwable t) { |
| 238 | Log.w(TAG, "Error creating tile for spec: " + tileSpec, t); |
| 239 | } |
| 240 | } |
| 241 | } |
Jason Monk | 11a7744 | 2015-05-12 19:29:02 -0400 | [diff] [blame] | 242 | mTileSpecs.clear(); |
| 243 | mTileSpecs.addAll(tileSpecs); |
John Spurlock | bceed06 | 2014-08-10 18:04:16 -0400 | [diff] [blame] | 244 | mTiles.clear(); |
| 245 | mTiles.putAll(newTiles); |
| 246 | if (mCallback != null) { |
| 247 | mCallback.onTilesChanged(); |
| 248 | } |
| 249 | } |
| 250 | |
Jason Monk | 11a7744 | 2015-05-12 19:29:02 -0400 | [diff] [blame] | 251 | protected QSTile<?> createTile(String tileSpec) { |
John Spurlock | bceed06 | 2014-08-10 18:04:16 -0400 | [diff] [blame] | 252 | if (tileSpec.equals("wifi")) return new WifiTile(this); |
| 253 | else if (tileSpec.equals("bt")) return new BluetoothTile(this); |
| 254 | else if (tileSpec.equals("inversion")) return new ColorInversionTile(this); |
| 255 | else if (tileSpec.equals("cell")) return new CellularTile(this); |
| 256 | else if (tileSpec.equals("airplane")) return new AirplaneModeTile(this); |
John Spurlock | 3c4076a | 2015-02-24 12:12:25 -0500 | [diff] [blame] | 257 | else if (tileSpec.equals("dnd")) return new DndTile(this); |
John Spurlock | bceed06 | 2014-08-10 18:04:16 -0400 | [diff] [blame] | 258 | else if (tileSpec.equals("rotation")) return new RotationLockTile(this); |
| 259 | else if (tileSpec.equals("flashlight")) return new FlashlightTile(this); |
| 260 | else if (tileSpec.equals("location")) return new LocationTile(this); |
| 261 | else if (tileSpec.equals("cast")) return new CastTile(this); |
| 262 | else if (tileSpec.equals("hotspot")) return new HotspotTile(this); |
| 263 | else if (tileSpec.startsWith(IntentTile.PREFIX)) return IntentTile.create(this,tileSpec); |
| 264 | else throw new IllegalArgumentException("Bad tile spec: " + tileSpec); |
| 265 | } |
| 266 | |
Jason Monk | 5e74517 | 2015-06-02 19:14:44 -0400 | [diff] [blame] | 267 | protected List<String> loadTileSpecs(String tileList) { |
John Spurlock | bceed06 | 2014-08-10 18:04:16 -0400 | [diff] [blame] | 268 | final Resources res = mContext.getResources(); |
| 269 | final String defaultTileList = res.getString(R.string.quick_settings_tiles_default); |
John Spurlock | bceed06 | 2014-08-10 18:04:16 -0400 | [diff] [blame] | 270 | if (tileList == null) { |
| 271 | tileList = res.getString(R.string.quick_settings_tiles); |
| 272 | if (DEBUG) Log.d(TAG, "Loaded tile specs from config: " + tileList); |
| 273 | } else { |
| 274 | if (DEBUG) Log.d(TAG, "Loaded tile specs from setting: " + tileList); |
| 275 | } |
| 276 | final ArrayList<String> tiles = new ArrayList<String>(); |
| 277 | boolean addedDefault = false; |
| 278 | for (String tile : tileList.split(",")) { |
| 279 | tile = tile.trim(); |
| 280 | if (tile.isEmpty()) continue; |
| 281 | if (tile.equals("default")) { |
| 282 | if (!addedDefault) { |
| 283 | tiles.addAll(Arrays.asList(defaultTileList.split(","))); |
| 284 | addedDefault = true; |
| 285 | } |
| 286 | } else { |
| 287 | tiles.add(tile); |
| 288 | } |
| 289 | } |
| 290 | return tiles; |
| 291 | } |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 292 | } |