blob: 3a6b785eb9b5efb91fab574f8fd80fd3d2e86fa5 [file] [log] [blame]
John Spurlockaf8d6c42014-05-07 17:49:08 -04001/*
Jason Monke5b770e2017-03-03 21:49:29 -05002 * Copyright (C) 2017 The Android Open Source Project
John Spurlockaf8d6c42014-05-07 17:49:08 -04003 *
Jason Monke5b770e2017-03-03 21:49:29 -05004 * 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
John Spurlockaf8d6c42014-05-07 17:49:08 -04006 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
Jason Monke5b770e2017-03-03 21:49:29 -05009 * 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.
John Spurlockaf8d6c42014-05-07 17:49:08 -040013 */
14
Jason Monke5b770e2017-03-03 21:49:29 -050015package com.android.systemui.qs;
John Spurlockaf8d6c42014-05-07 17:49:08 -040016
Jason Monkba2318e2015-12-08 09:04:23 -050017import android.app.ActivityManager;
Jason Monk7e53f202016-01-28 10:40:20 -050018import android.content.ComponentName;
John Spurlockaf8d6c42014-05-07 17:49:08 -040019import android.content.Context;
20import android.content.Intent;
John Spurlockbceed062014-08-10 18:04:16 -040021import android.content.res.Resources;
Jason Monk7e53f202016-01-28 10:40:20 -050022import android.os.Handler;
Jason Monk30b64fa2018-12-27 13:19:34 -050023import android.os.Looper;
Jason Monk7e53f202016-01-28 10:40:20 -050024import android.os.UserHandle;
Sudheer Shankaad790492016-06-03 10:48:27 -070025import android.os.UserManager;
Jason Monkba2318e2015-12-08 09:04:23 -050026import android.provider.Settings;
Jason Monk7e53f202016-01-28 10:40:20 -050027import android.provider.Settings.Secure;
Jason Monka3453b8b2016-06-17 12:42:59 -040028import android.service.quicksettings.Tile;
Jason Monkba2318e2015-12-08 09:04:23 -050029import android.text.TextUtils;
John Spurlockbceed062014-08-10 18:04:16 -040030import android.util.Log;
Winsonc0d70582016-01-29 10:24:39 -080031
Jason Monk9c7844c2017-01-18 15:21:53 -050032import com.android.systemui.Dependency;
John Spurlockbceed062014-08-10 18:04:16 -040033import com.android.systemui.R;
Jason Monk5d577202018-12-26 15:43:06 -050034import com.android.systemui.SysUiServiceProvider;
Jason Monk702e2eb2017-03-03 16:53:44 -050035import com.android.systemui.plugins.PluginListener;
Jason Monk702e2eb2017-03-03 16:53:44 -050036import com.android.systemui.plugins.qs.QSFactory;
Jason Monk702e2eb2017-03-03 16:53:44 -050037import com.android.systemui.plugins.qs.QSTile;
Gus Prevasab336792018-11-14 13:52:20 -050038import com.android.systemui.plugins.qs.QSTileView;
Jason Monkd5a204f2015-12-21 08:50:01 -050039import com.android.systemui.qs.external.CustomTile;
Jason Monk7e53f202016-01-28 10:40:20 -050040import com.android.systemui.qs.external.TileLifecycleManager;
Jason Monkd5a204f2015-12-21 08:50:01 -050041import com.android.systemui.qs.external.TileServices;
Jason Monk702e2eb2017-03-03 16:53:44 -050042import com.android.systemui.qs.tileimpl.QSFactoryImpl;
Gus Prevasab336792018-11-14 13:52:20 -050043import com.android.systemui.shared.plugins.PluginManager;
Jason Monke5b770e2017-03-03 21:49:29 -050044import com.android.systemui.statusbar.phone.AutoTileManager;
45import com.android.systemui.statusbar.phone.StatusBar;
46import com.android.systemui.statusbar.phone.StatusBarIconController;
Jason Monk5e745172015-06-02 19:14:44 -040047import com.android.systemui.tuner.TunerService;
48import com.android.systemui.tuner.TunerService.Tunable;
John Spurlockaf8d6c42014-05-07 17:49:08 -040049
50import java.util.ArrayList;
John Spurlockbceed062014-08-10 18:04:16 -040051import java.util.Arrays;
52import java.util.Collection;
53import java.util.LinkedHashMap;
John Spurlockaf8d6c42014-05-07 17:49:08 -040054import java.util.List;
Amin Shaikha062beb2018-07-25 11:21:54 -040055import java.util.function.Predicate;
John Spurlockaf8d6c42014-05-07 17:49:08 -040056
Jason Monk5d577202018-12-26 15:43:06 -050057import javax.inject.Inject;
58import javax.inject.Named;
59import javax.inject.Singleton;
60
John Spurlockaf8d6c42014-05-07 17:49:08 -040061/** Platform implementation of the quick settings tile host **/
Jason Monk5d577202018-12-26 15:43:06 -050062@Singleton
Jason Monk702e2eb2017-03-03 16:53:44 -050063public class QSTileHost implements QSHost, Tunable, PluginListener<QSFactory> {
John Spurlockbceed062014-08-10 18:04:16 -040064 private static final String TAG = "QSTileHost";
65 private static final boolean DEBUG = Log.isLoggable(TAG, Log.DEBUG);
66
Jason Monkbaade752016-08-25 15:57:14 -040067 public static final String TILES_SETTING = Secure.QS_TILES;
John Spurlockaf8d6c42014-05-07 17:49:08 -040068
69 private final Context mContext;
Jason Monk702e2eb2017-03-03 16:53:44 -050070 private final LinkedHashMap<String, QSTile> mTiles = new LinkedHashMap<>();
Jason Monk5e745172015-06-02 19:14:44 -040071 protected final ArrayList<String> mTileSpecs = new ArrayList<>();
Jason Monkd5a204f2015-12-21 08:50:01 -050072 private final TileServices mServices;
Jason Monk30b64fa2018-12-27 13:19:34 -050073 private final TunerService mTunerService;
74 private final PluginManager mPluginManager;
John Spurlockaf8d6c42014-05-07 17:49:08 -040075
Jason Monkbbadff82015-11-06 15:47:26 -050076 private final List<Callback> mCallbacks = new ArrayList<>();
Jason Monkc3f42c12016-02-05 12:33:13 -050077 private final AutoTileManager mAutoTiles;
Jason Monk9c7844c2017-01-18 15:21:53 -050078 private final StatusBarIconController mIconController;
Jason Monk702e2eb2017-03-03 16:53:44 -050079 private final ArrayList<QSFactory> mQsFactories = new ArrayList<>();
Jason Monk1ffa11b2016-03-08 14:44:23 -050080 private int mCurrentUser;
Jason Monk5d577202018-12-26 15:43:06 -050081 private StatusBar mStatusBar;
John Spurlockbceed062014-08-10 18:04:16 -040082
Jason Monk5d577202018-12-26 15:43:06 -050083 @Inject
84 public QSTileHost(Context context,
85 StatusBarIconController iconController,
86 QSFactoryImpl defaultFactory,
Jason Monk30b64fa2018-12-27 13:19:34 -050087 @Named(Dependency.MAIN_HANDLER_NAME) Handler mainHandler,
88 @Named(Dependency.BG_LOOPER_NAME) Looper bgLooper,
89 PluginManager pluginManager,
90 TunerService tunerService) {
Jason Monk9c7844c2017-01-18 15:21:53 -050091 mIconController = iconController;
John Spurlockaf8d6c42014-05-07 17:49:08 -040092 mContext = context;
Jason Monk30b64fa2018-12-27 13:19:34 -050093 mTunerService = tunerService;
94 mPluginManager = pluginManager;
John Spurlockaf8d6c42014-05-07 17:49:08 -040095
Jason Monk30b64fa2018-12-27 13:19:34 -050096 mServices = new TileServices(this, bgLooper);
Jason Monkd5a204f2015-12-21 08:50:01 -050097
Jason Monk5d577202018-12-26 15:43:06 -050098 defaultFactory.setHost(this);
99 mQsFactories.add(defaultFactory);
Jason Monk30b64fa2018-12-27 13:19:34 -0500100 pluginManager.addPluginListener(this, QSFactory.class, true);
Jason Monk702e2eb2017-03-03 16:53:44 -0500101
Jason Monk5d577202018-12-26 15:43:06 -0500102 mainHandler.post(() -> {
103 // This is technically a hack to avoid circular dependency of
104 // QSTileHost -> XXXTile -> QSTileHost. Posting ensures creation
105 // finishes before creating any tiles.
Jason Monk30b64fa2018-12-27 13:19:34 -0500106 tunerService.addTunable(this, TILES_SETTING);
Jason Monk5d577202018-12-26 15:43:06 -0500107 });
Rubin Xucedcd282016-02-15 14:44:44 +0000108 // AutoTileManager can modify mTiles so make sure mTiles has already been initialized.
109 mAutoTiles = new AutoTileManager(context, this);
Jason Monk5e745172015-06-02 19:14:44 -0400110 }
John Spurlockbceed062014-08-10 18:04:16 -0400111
Jason Monk9c7844c2017-01-18 15:21:53 -0500112 public StatusBarIconController getIconController() {
113 return mIconController;
Jason Monkca894a02016-01-12 15:30:22 -0500114 }
115
Jason Monk5e745172015-06-02 19:14:44 -0400116 public void destroy() {
Jason Monk88529052016-11-04 13:29:58 -0400117 mTiles.values().forEach(tile -> tile.destroy());
Jason Monkc3f42c12016-02-05 12:33:13 -0500118 mAutoTiles.destroy();
Jason Monk30b64fa2018-12-27 13:19:34 -0500119 mTunerService.removeTunable(this);
Jason Monk88529052016-11-04 13:29:58 -0400120 mServices.destroy();
Jason Monk30b64fa2018-12-27 13:19:34 -0500121 mPluginManager.removePluginListener(this);
Jason Monk702e2eb2017-03-03 16:53:44 -0500122 }
123
124 @Override
125 public void onPluginConnected(QSFactory plugin, Context pluginContext) {
126 // Give plugins priority over creation so they can override if they wish.
127 mQsFactories.add(0, plugin);
Jason Monk30b64fa2018-12-27 13:19:34 -0500128 String value = mTunerService.getValue(TILES_SETTING);
Jason Monk702e2eb2017-03-03 16:53:44 -0500129 // Force remove and recreate of all tiles.
130 onTuningChanged(TILES_SETTING, "");
131 onTuningChanged(TILES_SETTING, value);
132 }
133
134 @Override
135 public void onPluginDisconnected(QSFactory plugin) {
136 mQsFactories.remove(plugin);
137 // Force remove and recreate of all tiles.
Jason Monk30b64fa2018-12-27 13:19:34 -0500138 String value = mTunerService.getValue(TILES_SETTING);
Jason Monk702e2eb2017-03-03 16:53:44 -0500139 onTuningChanged(TILES_SETTING, "");
140 onTuningChanged(TILES_SETTING, value);
John Spurlockaf8d6c42014-05-07 17:49:08 -0400141 }
142
143 @Override
Jason Monkbbadff82015-11-06 15:47:26 -0500144 public void addCallback(Callback callback) {
145 mCallbacks.add(callback);
John Spurlockbceed062014-08-10 18:04:16 -0400146 }
147
148 @Override
Jason Monk46dbfb42016-02-25 14:59:20 -0500149 public void removeCallback(Callback callback) {
150 mCallbacks.remove(callback);
151 }
152
153 @Override
Jason Monk702e2eb2017-03-03 16:53:44 -0500154 public Collection<QSTile> getTiles() {
John Spurlockbceed062014-08-10 18:04:16 -0400155 return mTiles.values();
John Spurlockaf8d6c42014-05-07 17:49:08 -0400156 }
157
158 @Override
John Spurlockaf8d6c42014-05-07 17:49:08 -0400159 public void warn(String message, Throwable t) {
160 // already logged
161 }
162
163 @Override
164 public void collapsePanels() {
Jason Monk5d577202018-12-26 15:43:06 -0500165 if (mStatusBar == null) {
166 mStatusBar = SysUiServiceProvider.getComponent(mContext, StatusBar.class);
167 }
John Spurlockaf8d6c42014-05-07 17:49:08 -0400168 mStatusBar.postAnimateCollapsePanels();
169 }
170
171 @Override
Yoshinori Hiranoc673fbc2017-04-14 11:29:38 +0900172 public void forceCollapsePanels() {
Jason Monk5d577202018-12-26 15:43:06 -0500173 if (mStatusBar == null) {
174 mStatusBar = SysUiServiceProvider.getComponent(mContext, StatusBar.class);
175 }
Yoshinori Hiranoc673fbc2017-04-14 11:29:38 +0900176 mStatusBar.postAnimateForceCollapsePanels();
177 }
178
179 @Override
Jason Monkba2318e2015-12-08 09:04:23 -0500180 public void openPanels() {
Jason Monk5d577202018-12-26 15:43:06 -0500181 if (mStatusBar == null) {
182 mStatusBar = SysUiServiceProvider.getComponent(mContext, StatusBar.class);
183 }
Jason Monkba2318e2015-12-08 09:04:23 -0500184 mStatusBar.postAnimateOpenPanels();
185 }
186
187 @Override
John Spurlockaf8d6c42014-05-07 17:49:08 -0400188 public Context getContext() {
189 return mContext;
190 }
191
Jason Monkbbadff82015-11-06 15:47:26 -0500192
Jason Monkd5a204f2015-12-21 08:50:01 -0500193 public TileServices getTileServices() {
194 return mServices;
195 }
196
Jason Monk8c09ac72017-03-16 11:53:40 -0400197 public int indexOf(String spec) {
198 return mTileSpecs.indexOf(spec);
199 }
200
Jason Monk5e745172015-06-02 19:14:44 -0400201 @Override
202 public void onTuningChanged(String key, String newValue) {
203 if (!TILES_SETTING.equals(key)) {
204 return;
205 }
John Spurlockbceed062014-08-10 18:04:16 -0400206 if (DEBUG) Log.d(TAG, "Recreating tiles");
Sudheer Shankaad790492016-06-03 10:48:27 -0700207 if (newValue == null && UserManager.isDeviceInDemoMode(mContext)) {
208 newValue = mContext.getResources().getString(R.string.quick_settings_tiles_retail_mode);
209 }
Jason Monkbbadff82015-11-06 15:47:26 -0500210 final List<String> tileSpecs = loadTileSpecs(mContext, newValue);
Jason Monk1ffa11b2016-03-08 14:44:23 -0500211 int currentUser = ActivityManager.getCurrentUser();
212 if (tileSpecs.equals(mTileSpecs) && currentUser == mCurrentUser) return;
Jason Monk88529052016-11-04 13:29:58 -0400213 mTiles.entrySet().stream().filter(tile -> !tileSpecs.contains(tile.getKey())).forEach(
214 tile -> {
215 if (DEBUG) Log.d(TAG, "Destroying tile: " + tile.getKey());
216 tile.getValue().destroy();
217 });
Jason Monk702e2eb2017-03-03 16:53:44 -0500218 final LinkedHashMap<String, QSTile> newTiles = new LinkedHashMap<>();
John Spurlockbceed062014-08-10 18:04:16 -0400219 for (String tileSpec : tileSpecs) {
Jason Monk702e2eb2017-03-03 16:53:44 -0500220 QSTile tile = mTiles.get(tileSpec);
Jason Monk1ffa11b2016-03-08 14:44:23 -0500221 if (tile != null && (!(tile instanceof CustomTile)
222 || ((CustomTile) tile).getUser() == currentUser)) {
Akitaka Kimuraefd1c542016-09-27 20:06:00 +0900223 if (tile.isAvailable()) {
224 if (DEBUG) Log.d(TAG, "Adding " + tile);
225 tile.removeCallbacks();
Jason Monkf120cf32016-12-06 11:34:12 -0500226 if (!(tile instanceof CustomTile) && mCurrentUser != currentUser) {
227 tile.userSwitch(currentUser);
228 }
Akitaka Kimuraefd1c542016-09-27 20:06:00 +0900229 newTiles.put(tileSpec, tile);
230 } else {
231 tile.destroy();
Justin Klaassendd32d902016-07-31 10:25:36 -0700232 }
John Spurlockbceed062014-08-10 18:04:16 -0400233 } else {
234 if (DEBUG) Log.d(TAG, "Creating tile: " + tileSpec);
235 try {
Jason Monk1ffa11b2016-03-08 14:44:23 -0500236 tile = createTile(tileSpec);
Jason Monk88529052016-11-04 13:29:58 -0400237 if (tile != null) {
238 if (tile.isAvailable()) {
239 tile.setTileSpec(tileSpec);
240 newTiles.put(tileSpec, tile);
241 } else {
242 tile.destroy();
243 }
Jason Monkba2318e2015-12-08 09:04:23 -0500244 }
John Spurlockbceed062014-08-10 18:04:16 -0400245 } catch (Throwable t) {
246 Log.w(TAG, "Error creating tile for spec: " + tileSpec, t);
247 }
248 }
249 }
Jason Monk1ffa11b2016-03-08 14:44:23 -0500250 mCurrentUser = currentUser;
Jason Monk11a77442015-05-12 19:29:02 -0400251 mTileSpecs.clear();
252 mTileSpecs.addAll(tileSpecs);
John Spurlockbceed062014-08-10 18:04:16 -0400253 mTiles.clear();
254 mTiles.putAll(newTiles);
Jason Monkbbadff82015-11-06 15:47:26 -0500255 for (int i = 0; i < mCallbacks.size(); i++) {
256 mCallbacks.get(i).onTilesChanged();
John Spurlockbceed062014-08-10 18:04:16 -0400257 }
258 }
259
Jason Monkbbadff82015-11-06 15:47:26 -0500260 @Override
Amin Shaikha062beb2018-07-25 11:21:54 -0400261 public void removeTile(String spec) {
262 changeTileSpecs(tileSpecs-> tileSpecs.remove(spec));
Jason Monkba2318e2015-12-08 09:04:23 -0500263 }
264
arangelov5fe95982018-08-17 18:43:31 +0100265 @Override
266 public void unmarkTileAsAutoAdded(String spec) {
267 mAutoTiles.unmarkTileAsAutoAdded(spec);
268 }
269
Jason Monkc3f42c12016-02-05 12:33:13 -0500270 public void addTile(String spec) {
Amin Shaikha062beb2018-07-25 11:21:54 -0400271 changeTileSpecs(tileSpecs-> tileSpecs.add(spec));
272 }
273
274 private void changeTileSpecs(Predicate<List<String>> changeFunction) {
Ricky Waib96c1eac2016-06-02 11:42:16 +0100275 final String setting = Settings.Secure.getStringForUser(mContext.getContentResolver(),
Amin Shaikha062beb2018-07-25 11:21:54 -0400276 TILES_SETTING, ActivityManager.getCurrentUser());
Ricky Waib96c1eac2016-06-02 11:42:16 +0100277 final List<String> tileSpecs = loadTileSpecs(mContext, setting);
Amin Shaikha062beb2018-07-25 11:21:54 -0400278 if (changeFunction.test(tileSpecs)) {
279 Settings.Secure.putStringForUser(mContext.getContentResolver(), TILES_SETTING,
Ricky Waib96c1eac2016-06-02 11:42:16 +0100280 TextUtils.join(",", tileSpecs), ActivityManager.getCurrentUser());
Amin Shaikha062beb2018-07-25 11:21:54 -0400281 }
Jason Monkc3f42c12016-02-05 12:33:13 -0500282 }
283
Jason Monk7e53f202016-01-28 10:40:20 -0500284 public void addTile(ComponentName tile) {
285 List<String> newSpecs = new ArrayList<>(mTileSpecs);
286 newSpecs.add(0, CustomTile.toSpec(tile));
287 changeTiles(mTileSpecs, newSpecs);
288 }
289
290 public void removeTile(ComponentName tile) {
291 List<String> newSpecs = new ArrayList<>(mTileSpecs);
292 newSpecs.remove(CustomTile.toSpec(tile));
293 changeTiles(mTileSpecs, newSpecs);
294 }
295
296 public void changeTiles(List<String> previousTiles, List<String> newTiles) {
297 final int NP = previousTiles.size();
298 final int NA = newTiles.size();
299 for (int i = 0; i < NP; i++) {
300 String tileSpec = previousTiles.get(i);
301 if (!tileSpec.startsWith(CustomTile.PREFIX)) continue;
302 if (!newTiles.contains(tileSpec)) {
Jason Monka3453b8b2016-06-17 12:42:59 -0400303 ComponentName component = CustomTile.getComponentFromSpec(tileSpec);
304 Intent intent = new Intent().setComponent(component);
Jason Monk7e53f202016-01-28 10:40:20 -0500305 TileLifecycleManager lifecycleManager = new TileLifecycleManager(new Handler(),
Jason Monkee68fd82016-06-23 13:12:23 -0400306 mContext, mServices, new Tile(), intent,
Jason Monka3453b8b2016-06-17 12:42:59 -0400307 new UserHandle(ActivityManager.getCurrentUser()));
Jason Monk7e53f202016-01-28 10:40:20 -0500308 lifecycleManager.onStopListening();
309 lifecycleManager.onTileRemoved();
Jason Monkbaade752016-08-25 15:57:14 -0400310 TileLifecycleManager.setTileAdded(mContext, component, false);
Jason Monk7e53f202016-01-28 10:40:20 -0500311 lifecycleManager.flushMessagesAndUnbind();
312 }
313 }
314 if (DEBUG) Log.d(TAG, "saveCurrentTiles " + newTiles);
315 Secure.putStringForUser(getContext().getContentResolver(), QSTileHost.TILES_SETTING,
316 TextUtils.join(",", newTiles), ActivityManager.getCurrentUser());
317 }
318
Jason Monk702e2eb2017-03-03 16:53:44 -0500319 public QSTile createTile(String tileSpec) {
320 for (int i = 0; i < mQsFactories.size(); i++) {
321 QSTile t = mQsFactories.get(i).createTile(tileSpec);
322 if (t != null) {
323 return t;
324 }
Jason Monk2b37190f2016-01-06 10:19:04 -0500325 }
Jason Monk702e2eb2017-03-03 16:53:44 -0500326 return null;
327 }
328
329 public QSTileView createTileView(QSTile tile, boolean collapsedView) {
330 for (int i = 0; i < mQsFactories.size(); i++) {
331 QSTileView view = mQsFactories.get(i).createTileView(tile, collapsedView);
332 if (view != null) {
333 return view;
334 }
335 }
336 throw new RuntimeException("Default factory didn't create view for " + tile.getTileSpec());
John Spurlockbceed062014-08-10 18:04:16 -0400337 }
338
Amin Shaikha062beb2018-07-25 11:21:54 -0400339 protected static List<String> loadTileSpecs(Context context, String tileList) {
Jason Monkbbadff82015-11-06 15:47:26 -0500340 final Resources res = context.getResources();
John Spurlockbceed062014-08-10 18:04:16 -0400341 final String defaultTileList = res.getString(R.string.quick_settings_tiles_default);
Fabian Kozynski7f370dd2018-12-05 10:41:07 -0500342 if (TextUtils.isEmpty(tileList)) {
John Spurlockbceed062014-08-10 18:04:16 -0400343 tileList = res.getString(R.string.quick_settings_tiles);
344 if (DEBUG) Log.d(TAG, "Loaded tile specs from config: " + tileList);
345 } else {
346 if (DEBUG) Log.d(TAG, "Loaded tile specs from setting: " + tileList);
347 }
348 final ArrayList<String> tiles = new ArrayList<String>();
349 boolean addedDefault = false;
350 for (String tile : tileList.split(",")) {
351 tile = tile.trim();
352 if (tile.isEmpty()) continue;
353 if (tile.equals("default")) {
354 if (!addedDefault) {
355 tiles.addAll(Arrays.asList(defaultTileList.split(",")));
356 addedDefault = true;
357 }
358 } else {
359 tiles.add(tile);
360 }
361 }
362 return tiles;
363 }
John Spurlockaf8d6c42014-05-07 17:49:08 -0400364}