John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 1 | /* |
Jason Monk | e5b770e | 2017-03-03 21:49:29 -0500 | [diff] [blame] | 2 | * Copyright (C) 2017 The Android Open Source Project |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 3 | * |
Jason Monk | e5b770e | 2017-03-03 21:49:29 -0500 | [diff] [blame] | 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 |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 6 | * |
| 7 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | * |
Jason Monk | e5b770e | 2017-03-03 21:49:29 -0500 | [diff] [blame] | 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. |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 13 | */ |
| 14 | |
Jason Monk | e5b770e | 2017-03-03 21:49:29 -0500 | [diff] [blame] | 15 | package com.android.systemui.qs; |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 16 | |
Jason Monk | ba2318e | 2015-12-08 09:04:23 -0500 | [diff] [blame] | 17 | import android.app.ActivityManager; |
Jason Monk | 7e53f20 | 2016-01-28 10:40:20 -0500 | [diff] [blame] | 18 | import android.content.ComponentName; |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 19 | import android.content.Context; |
| 20 | import android.content.Intent; |
John Spurlock | bceed06 | 2014-08-10 18:04:16 -0400 | [diff] [blame] | 21 | import android.content.res.Resources; |
Jason Monk | 7e53f20 | 2016-01-28 10:40:20 -0500 | [diff] [blame] | 22 | import android.os.Handler; |
Jason Monk | 30b64fa | 2018-12-27 13:19:34 -0500 | [diff] [blame^] | 23 | import android.os.Looper; |
Jason Monk | 7e53f20 | 2016-01-28 10:40:20 -0500 | [diff] [blame] | 24 | import android.os.UserHandle; |
Sudheer Shanka | ad79049 | 2016-06-03 10:48:27 -0700 | [diff] [blame] | 25 | import android.os.UserManager; |
Jason Monk | ba2318e | 2015-12-08 09:04:23 -0500 | [diff] [blame] | 26 | import android.provider.Settings; |
Jason Monk | 7e53f20 | 2016-01-28 10:40:20 -0500 | [diff] [blame] | 27 | import android.provider.Settings.Secure; |
Jason Monk | a3453b8b | 2016-06-17 12:42:59 -0400 | [diff] [blame] | 28 | import android.service.quicksettings.Tile; |
Jason Monk | ba2318e | 2015-12-08 09:04:23 -0500 | [diff] [blame] | 29 | import android.text.TextUtils; |
John Spurlock | bceed06 | 2014-08-10 18:04:16 -0400 | [diff] [blame] | 30 | import android.util.Log; |
Winson | c0d7058 | 2016-01-29 10:24:39 -0800 | [diff] [blame] | 31 | |
Jason Monk | 9c7844c | 2017-01-18 15:21:53 -0500 | [diff] [blame] | 32 | import com.android.systemui.Dependency; |
John Spurlock | bceed06 | 2014-08-10 18:04:16 -0400 | [diff] [blame] | 33 | import com.android.systemui.R; |
Jason Monk | 5d57720 | 2018-12-26 15:43:06 -0500 | [diff] [blame] | 34 | import com.android.systemui.SysUiServiceProvider; |
Jason Monk | 702e2eb | 2017-03-03 16:53:44 -0500 | [diff] [blame] | 35 | import com.android.systemui.plugins.PluginListener; |
Jason Monk | 702e2eb | 2017-03-03 16:53:44 -0500 | [diff] [blame] | 36 | import com.android.systemui.plugins.qs.QSFactory; |
Jason Monk | 702e2eb | 2017-03-03 16:53:44 -0500 | [diff] [blame] | 37 | import com.android.systemui.plugins.qs.QSTile; |
Gus Prevas | ab33679 | 2018-11-14 13:52:20 -0500 | [diff] [blame] | 38 | import com.android.systemui.plugins.qs.QSTileView; |
Jason Monk | d5a204f | 2015-12-21 08:50:01 -0500 | [diff] [blame] | 39 | import com.android.systemui.qs.external.CustomTile; |
Jason Monk | 7e53f20 | 2016-01-28 10:40:20 -0500 | [diff] [blame] | 40 | import com.android.systemui.qs.external.TileLifecycleManager; |
Jason Monk | d5a204f | 2015-12-21 08:50:01 -0500 | [diff] [blame] | 41 | import com.android.systemui.qs.external.TileServices; |
Jason Monk | 702e2eb | 2017-03-03 16:53:44 -0500 | [diff] [blame] | 42 | import com.android.systemui.qs.tileimpl.QSFactoryImpl; |
Gus Prevas | ab33679 | 2018-11-14 13:52:20 -0500 | [diff] [blame] | 43 | import com.android.systemui.shared.plugins.PluginManager; |
Jason Monk | e5b770e | 2017-03-03 21:49:29 -0500 | [diff] [blame] | 44 | import com.android.systemui.statusbar.phone.AutoTileManager; |
| 45 | import com.android.systemui.statusbar.phone.StatusBar; |
| 46 | import com.android.systemui.statusbar.phone.StatusBarIconController; |
Jason Monk | 5e74517 | 2015-06-02 19:14:44 -0400 | [diff] [blame] | 47 | import com.android.systemui.tuner.TunerService; |
| 48 | import com.android.systemui.tuner.TunerService.Tunable; |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 49 | |
| 50 | import java.util.ArrayList; |
John Spurlock | bceed06 | 2014-08-10 18:04:16 -0400 | [diff] [blame] | 51 | import java.util.Arrays; |
| 52 | import java.util.Collection; |
| 53 | import java.util.LinkedHashMap; |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 54 | import java.util.List; |
Amin Shaikh | a062beb | 2018-07-25 11:21:54 -0400 | [diff] [blame] | 55 | import java.util.function.Predicate; |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 56 | |
Jason Monk | 5d57720 | 2018-12-26 15:43:06 -0500 | [diff] [blame] | 57 | import javax.inject.Inject; |
| 58 | import javax.inject.Named; |
| 59 | import javax.inject.Singleton; |
| 60 | |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 61 | /** Platform implementation of the quick settings tile host **/ |
Jason Monk | 5d57720 | 2018-12-26 15:43:06 -0500 | [diff] [blame] | 62 | @Singleton |
Jason Monk | 702e2eb | 2017-03-03 16:53:44 -0500 | [diff] [blame] | 63 | public class QSTileHost implements QSHost, Tunable, PluginListener<QSFactory> { |
John Spurlock | bceed06 | 2014-08-10 18:04:16 -0400 | [diff] [blame] | 64 | private static final String TAG = "QSTileHost"; |
| 65 | private static final boolean DEBUG = Log.isLoggable(TAG, Log.DEBUG); |
| 66 | |
Jason Monk | baade75 | 2016-08-25 15:57:14 -0400 | [diff] [blame] | 67 | public static final String TILES_SETTING = Secure.QS_TILES; |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 68 | |
| 69 | private final Context mContext; |
Jason Monk | 702e2eb | 2017-03-03 16:53:44 -0500 | [diff] [blame] | 70 | private final LinkedHashMap<String, QSTile> mTiles = new LinkedHashMap<>(); |
Jason Monk | 5e74517 | 2015-06-02 19:14:44 -0400 | [diff] [blame] | 71 | protected final ArrayList<String> mTileSpecs = new ArrayList<>(); |
Jason Monk | d5a204f | 2015-12-21 08:50:01 -0500 | [diff] [blame] | 72 | private final TileServices mServices; |
Jason Monk | 30b64fa | 2018-12-27 13:19:34 -0500 | [diff] [blame^] | 73 | private final TunerService mTunerService; |
| 74 | private final PluginManager mPluginManager; |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 75 | |
Jason Monk | bbadff8 | 2015-11-06 15:47:26 -0500 | [diff] [blame] | 76 | private final List<Callback> mCallbacks = new ArrayList<>(); |
Jason Monk | c3f42c1 | 2016-02-05 12:33:13 -0500 | [diff] [blame] | 77 | private final AutoTileManager mAutoTiles; |
Jason Monk | 9c7844c | 2017-01-18 15:21:53 -0500 | [diff] [blame] | 78 | private final StatusBarIconController mIconController; |
Jason Monk | 702e2eb | 2017-03-03 16:53:44 -0500 | [diff] [blame] | 79 | private final ArrayList<QSFactory> mQsFactories = new ArrayList<>(); |
Jason Monk | 1ffa11b | 2016-03-08 14:44:23 -0500 | [diff] [blame] | 80 | private int mCurrentUser; |
Jason Monk | 5d57720 | 2018-12-26 15:43:06 -0500 | [diff] [blame] | 81 | private StatusBar mStatusBar; |
John Spurlock | bceed06 | 2014-08-10 18:04:16 -0400 | [diff] [blame] | 82 | |
Jason Monk | 5d57720 | 2018-12-26 15:43:06 -0500 | [diff] [blame] | 83 | @Inject |
| 84 | public QSTileHost(Context context, |
| 85 | StatusBarIconController iconController, |
| 86 | QSFactoryImpl defaultFactory, |
Jason Monk | 30b64fa | 2018-12-27 13:19:34 -0500 | [diff] [blame^] | 87 | @Named(Dependency.MAIN_HANDLER_NAME) Handler mainHandler, |
| 88 | @Named(Dependency.BG_LOOPER_NAME) Looper bgLooper, |
| 89 | PluginManager pluginManager, |
| 90 | TunerService tunerService) { |
Jason Monk | 9c7844c | 2017-01-18 15:21:53 -0500 | [diff] [blame] | 91 | mIconController = iconController; |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 92 | mContext = context; |
Jason Monk | 30b64fa | 2018-12-27 13:19:34 -0500 | [diff] [blame^] | 93 | mTunerService = tunerService; |
| 94 | mPluginManager = pluginManager; |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 95 | |
Jason Monk | 30b64fa | 2018-12-27 13:19:34 -0500 | [diff] [blame^] | 96 | mServices = new TileServices(this, bgLooper); |
Jason Monk | d5a204f | 2015-12-21 08:50:01 -0500 | [diff] [blame] | 97 | |
Jason Monk | 5d57720 | 2018-12-26 15:43:06 -0500 | [diff] [blame] | 98 | defaultFactory.setHost(this); |
| 99 | mQsFactories.add(defaultFactory); |
Jason Monk | 30b64fa | 2018-12-27 13:19:34 -0500 | [diff] [blame^] | 100 | pluginManager.addPluginListener(this, QSFactory.class, true); |
Jason Monk | 702e2eb | 2017-03-03 16:53:44 -0500 | [diff] [blame] | 101 | |
Jason Monk | 5d57720 | 2018-12-26 15:43:06 -0500 | [diff] [blame] | 102 | 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 Monk | 30b64fa | 2018-12-27 13:19:34 -0500 | [diff] [blame^] | 106 | tunerService.addTunable(this, TILES_SETTING); |
Jason Monk | 5d57720 | 2018-12-26 15:43:06 -0500 | [diff] [blame] | 107 | }); |
Rubin Xu | cedcd28 | 2016-02-15 14:44:44 +0000 | [diff] [blame] | 108 | // AutoTileManager can modify mTiles so make sure mTiles has already been initialized. |
| 109 | mAutoTiles = new AutoTileManager(context, this); |
Jason Monk | 5e74517 | 2015-06-02 19:14:44 -0400 | [diff] [blame] | 110 | } |
John Spurlock | bceed06 | 2014-08-10 18:04:16 -0400 | [diff] [blame] | 111 | |
Jason Monk | 9c7844c | 2017-01-18 15:21:53 -0500 | [diff] [blame] | 112 | public StatusBarIconController getIconController() { |
| 113 | return mIconController; |
Jason Monk | ca894a0 | 2016-01-12 15:30:22 -0500 | [diff] [blame] | 114 | } |
| 115 | |
Jason Monk | 5e74517 | 2015-06-02 19:14:44 -0400 | [diff] [blame] | 116 | public void destroy() { |
Jason Monk | 8852905 | 2016-11-04 13:29:58 -0400 | [diff] [blame] | 117 | mTiles.values().forEach(tile -> tile.destroy()); |
Jason Monk | c3f42c1 | 2016-02-05 12:33:13 -0500 | [diff] [blame] | 118 | mAutoTiles.destroy(); |
Jason Monk | 30b64fa | 2018-12-27 13:19:34 -0500 | [diff] [blame^] | 119 | mTunerService.removeTunable(this); |
Jason Monk | 8852905 | 2016-11-04 13:29:58 -0400 | [diff] [blame] | 120 | mServices.destroy(); |
Jason Monk | 30b64fa | 2018-12-27 13:19:34 -0500 | [diff] [blame^] | 121 | mPluginManager.removePluginListener(this); |
Jason Monk | 702e2eb | 2017-03-03 16:53:44 -0500 | [diff] [blame] | 122 | } |
| 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 Monk | 30b64fa | 2018-12-27 13:19:34 -0500 | [diff] [blame^] | 128 | String value = mTunerService.getValue(TILES_SETTING); |
Jason Monk | 702e2eb | 2017-03-03 16:53:44 -0500 | [diff] [blame] | 129 | // 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 Monk | 30b64fa | 2018-12-27 13:19:34 -0500 | [diff] [blame^] | 138 | String value = mTunerService.getValue(TILES_SETTING); |
Jason Monk | 702e2eb | 2017-03-03 16:53:44 -0500 | [diff] [blame] | 139 | onTuningChanged(TILES_SETTING, ""); |
| 140 | onTuningChanged(TILES_SETTING, value); |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 141 | } |
| 142 | |
| 143 | @Override |
Jason Monk | bbadff8 | 2015-11-06 15:47:26 -0500 | [diff] [blame] | 144 | public void addCallback(Callback callback) { |
| 145 | mCallbacks.add(callback); |
John Spurlock | bceed06 | 2014-08-10 18:04:16 -0400 | [diff] [blame] | 146 | } |
| 147 | |
| 148 | @Override |
Jason Monk | 46dbfb4 | 2016-02-25 14:59:20 -0500 | [diff] [blame] | 149 | public void removeCallback(Callback callback) { |
| 150 | mCallbacks.remove(callback); |
| 151 | } |
| 152 | |
| 153 | @Override |
Jason Monk | 702e2eb | 2017-03-03 16:53:44 -0500 | [diff] [blame] | 154 | public Collection<QSTile> getTiles() { |
John Spurlock | bceed06 | 2014-08-10 18:04:16 -0400 | [diff] [blame] | 155 | return mTiles.values(); |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 156 | } |
| 157 | |
| 158 | @Override |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 159 | public void warn(String message, Throwable t) { |
| 160 | // already logged |
| 161 | } |
| 162 | |
| 163 | @Override |
| 164 | public void collapsePanels() { |
Jason Monk | 5d57720 | 2018-12-26 15:43:06 -0500 | [diff] [blame] | 165 | if (mStatusBar == null) { |
| 166 | mStatusBar = SysUiServiceProvider.getComponent(mContext, StatusBar.class); |
| 167 | } |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 168 | mStatusBar.postAnimateCollapsePanels(); |
| 169 | } |
| 170 | |
| 171 | @Override |
Yoshinori Hirano | c673fbc | 2017-04-14 11:29:38 +0900 | [diff] [blame] | 172 | public void forceCollapsePanels() { |
Jason Monk | 5d57720 | 2018-12-26 15:43:06 -0500 | [diff] [blame] | 173 | if (mStatusBar == null) { |
| 174 | mStatusBar = SysUiServiceProvider.getComponent(mContext, StatusBar.class); |
| 175 | } |
Yoshinori Hirano | c673fbc | 2017-04-14 11:29:38 +0900 | [diff] [blame] | 176 | mStatusBar.postAnimateForceCollapsePanels(); |
| 177 | } |
| 178 | |
| 179 | @Override |
Jason Monk | ba2318e | 2015-12-08 09:04:23 -0500 | [diff] [blame] | 180 | public void openPanels() { |
Jason Monk | 5d57720 | 2018-12-26 15:43:06 -0500 | [diff] [blame] | 181 | if (mStatusBar == null) { |
| 182 | mStatusBar = SysUiServiceProvider.getComponent(mContext, StatusBar.class); |
| 183 | } |
Jason Monk | ba2318e | 2015-12-08 09:04:23 -0500 | [diff] [blame] | 184 | mStatusBar.postAnimateOpenPanels(); |
| 185 | } |
| 186 | |
| 187 | @Override |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 188 | public Context getContext() { |
| 189 | return mContext; |
| 190 | } |
| 191 | |
Jason Monk | bbadff8 | 2015-11-06 15:47:26 -0500 | [diff] [blame] | 192 | |
Jason Monk | d5a204f | 2015-12-21 08:50:01 -0500 | [diff] [blame] | 193 | public TileServices getTileServices() { |
| 194 | return mServices; |
| 195 | } |
| 196 | |
Jason Monk | 8c09ac7 | 2017-03-16 11:53:40 -0400 | [diff] [blame] | 197 | public int indexOf(String spec) { |
| 198 | return mTileSpecs.indexOf(spec); |
| 199 | } |
| 200 | |
Jason Monk | 5e74517 | 2015-06-02 19:14:44 -0400 | [diff] [blame] | 201 | @Override |
| 202 | public void onTuningChanged(String key, String newValue) { |
| 203 | if (!TILES_SETTING.equals(key)) { |
| 204 | return; |
| 205 | } |
John Spurlock | bceed06 | 2014-08-10 18:04:16 -0400 | [diff] [blame] | 206 | if (DEBUG) Log.d(TAG, "Recreating tiles"); |
Sudheer Shanka | ad79049 | 2016-06-03 10:48:27 -0700 | [diff] [blame] | 207 | if (newValue == null && UserManager.isDeviceInDemoMode(mContext)) { |
| 208 | newValue = mContext.getResources().getString(R.string.quick_settings_tiles_retail_mode); |
| 209 | } |
Jason Monk | bbadff8 | 2015-11-06 15:47:26 -0500 | [diff] [blame] | 210 | final List<String> tileSpecs = loadTileSpecs(mContext, newValue); |
Jason Monk | 1ffa11b | 2016-03-08 14:44:23 -0500 | [diff] [blame] | 211 | int currentUser = ActivityManager.getCurrentUser(); |
| 212 | if (tileSpecs.equals(mTileSpecs) && currentUser == mCurrentUser) return; |
Jason Monk | 8852905 | 2016-11-04 13:29:58 -0400 | [diff] [blame] | 213 | 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 Monk | 702e2eb | 2017-03-03 16:53:44 -0500 | [diff] [blame] | 218 | final LinkedHashMap<String, QSTile> newTiles = new LinkedHashMap<>(); |
John Spurlock | bceed06 | 2014-08-10 18:04:16 -0400 | [diff] [blame] | 219 | for (String tileSpec : tileSpecs) { |
Jason Monk | 702e2eb | 2017-03-03 16:53:44 -0500 | [diff] [blame] | 220 | QSTile tile = mTiles.get(tileSpec); |
Jason Monk | 1ffa11b | 2016-03-08 14:44:23 -0500 | [diff] [blame] | 221 | if (tile != null && (!(tile instanceof CustomTile) |
| 222 | || ((CustomTile) tile).getUser() == currentUser)) { |
Akitaka Kimura | efd1c54 | 2016-09-27 20:06:00 +0900 | [diff] [blame] | 223 | if (tile.isAvailable()) { |
| 224 | if (DEBUG) Log.d(TAG, "Adding " + tile); |
| 225 | tile.removeCallbacks(); |
Jason Monk | f120cf3 | 2016-12-06 11:34:12 -0500 | [diff] [blame] | 226 | if (!(tile instanceof CustomTile) && mCurrentUser != currentUser) { |
| 227 | tile.userSwitch(currentUser); |
| 228 | } |
Akitaka Kimura | efd1c54 | 2016-09-27 20:06:00 +0900 | [diff] [blame] | 229 | newTiles.put(tileSpec, tile); |
| 230 | } else { |
| 231 | tile.destroy(); |
Justin Klaassen | dd32d90 | 2016-07-31 10:25:36 -0700 | [diff] [blame] | 232 | } |
John Spurlock | bceed06 | 2014-08-10 18:04:16 -0400 | [diff] [blame] | 233 | } else { |
| 234 | if (DEBUG) Log.d(TAG, "Creating tile: " + tileSpec); |
| 235 | try { |
Jason Monk | 1ffa11b | 2016-03-08 14:44:23 -0500 | [diff] [blame] | 236 | tile = createTile(tileSpec); |
Jason Monk | 8852905 | 2016-11-04 13:29:58 -0400 | [diff] [blame] | 237 | if (tile != null) { |
| 238 | if (tile.isAvailable()) { |
| 239 | tile.setTileSpec(tileSpec); |
| 240 | newTiles.put(tileSpec, tile); |
| 241 | } else { |
| 242 | tile.destroy(); |
| 243 | } |
Jason Monk | ba2318e | 2015-12-08 09:04:23 -0500 | [diff] [blame] | 244 | } |
John Spurlock | bceed06 | 2014-08-10 18:04:16 -0400 | [diff] [blame] | 245 | } catch (Throwable t) { |
| 246 | Log.w(TAG, "Error creating tile for spec: " + tileSpec, t); |
| 247 | } |
| 248 | } |
| 249 | } |
Jason Monk | 1ffa11b | 2016-03-08 14:44:23 -0500 | [diff] [blame] | 250 | mCurrentUser = currentUser; |
Jason Monk | 11a7744 | 2015-05-12 19:29:02 -0400 | [diff] [blame] | 251 | mTileSpecs.clear(); |
| 252 | mTileSpecs.addAll(tileSpecs); |
John Spurlock | bceed06 | 2014-08-10 18:04:16 -0400 | [diff] [blame] | 253 | mTiles.clear(); |
| 254 | mTiles.putAll(newTiles); |
Jason Monk | bbadff8 | 2015-11-06 15:47:26 -0500 | [diff] [blame] | 255 | for (int i = 0; i < mCallbacks.size(); i++) { |
| 256 | mCallbacks.get(i).onTilesChanged(); |
John Spurlock | bceed06 | 2014-08-10 18:04:16 -0400 | [diff] [blame] | 257 | } |
| 258 | } |
| 259 | |
Jason Monk | bbadff8 | 2015-11-06 15:47:26 -0500 | [diff] [blame] | 260 | @Override |
Amin Shaikh | a062beb | 2018-07-25 11:21:54 -0400 | [diff] [blame] | 261 | public void removeTile(String spec) { |
| 262 | changeTileSpecs(tileSpecs-> tileSpecs.remove(spec)); |
Jason Monk | ba2318e | 2015-12-08 09:04:23 -0500 | [diff] [blame] | 263 | } |
| 264 | |
arangelov | 5fe9598 | 2018-08-17 18:43:31 +0100 | [diff] [blame] | 265 | @Override |
| 266 | public void unmarkTileAsAutoAdded(String spec) { |
| 267 | mAutoTiles.unmarkTileAsAutoAdded(spec); |
| 268 | } |
| 269 | |
Jason Monk | c3f42c1 | 2016-02-05 12:33:13 -0500 | [diff] [blame] | 270 | public void addTile(String spec) { |
Amin Shaikh | a062beb | 2018-07-25 11:21:54 -0400 | [diff] [blame] | 271 | changeTileSpecs(tileSpecs-> tileSpecs.add(spec)); |
| 272 | } |
| 273 | |
| 274 | private void changeTileSpecs(Predicate<List<String>> changeFunction) { |
Ricky Wai | b96c1eac | 2016-06-02 11:42:16 +0100 | [diff] [blame] | 275 | final String setting = Settings.Secure.getStringForUser(mContext.getContentResolver(), |
Amin Shaikh | a062beb | 2018-07-25 11:21:54 -0400 | [diff] [blame] | 276 | TILES_SETTING, ActivityManager.getCurrentUser()); |
Ricky Wai | b96c1eac | 2016-06-02 11:42:16 +0100 | [diff] [blame] | 277 | final List<String> tileSpecs = loadTileSpecs(mContext, setting); |
Amin Shaikh | a062beb | 2018-07-25 11:21:54 -0400 | [diff] [blame] | 278 | if (changeFunction.test(tileSpecs)) { |
| 279 | Settings.Secure.putStringForUser(mContext.getContentResolver(), TILES_SETTING, |
Ricky Wai | b96c1eac | 2016-06-02 11:42:16 +0100 | [diff] [blame] | 280 | TextUtils.join(",", tileSpecs), ActivityManager.getCurrentUser()); |
Amin Shaikh | a062beb | 2018-07-25 11:21:54 -0400 | [diff] [blame] | 281 | } |
Jason Monk | c3f42c1 | 2016-02-05 12:33:13 -0500 | [diff] [blame] | 282 | } |
| 283 | |
Jason Monk | 7e53f20 | 2016-01-28 10:40:20 -0500 | [diff] [blame] | 284 | 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 Monk | a3453b8b | 2016-06-17 12:42:59 -0400 | [diff] [blame] | 303 | ComponentName component = CustomTile.getComponentFromSpec(tileSpec); |
| 304 | Intent intent = new Intent().setComponent(component); |
Jason Monk | 7e53f20 | 2016-01-28 10:40:20 -0500 | [diff] [blame] | 305 | TileLifecycleManager lifecycleManager = new TileLifecycleManager(new Handler(), |
Jason Monk | ee68fd8 | 2016-06-23 13:12:23 -0400 | [diff] [blame] | 306 | mContext, mServices, new Tile(), intent, |
Jason Monk | a3453b8b | 2016-06-17 12:42:59 -0400 | [diff] [blame] | 307 | new UserHandle(ActivityManager.getCurrentUser())); |
Jason Monk | 7e53f20 | 2016-01-28 10:40:20 -0500 | [diff] [blame] | 308 | lifecycleManager.onStopListening(); |
| 309 | lifecycleManager.onTileRemoved(); |
Jason Monk | baade75 | 2016-08-25 15:57:14 -0400 | [diff] [blame] | 310 | TileLifecycleManager.setTileAdded(mContext, component, false); |
Jason Monk | 7e53f20 | 2016-01-28 10:40:20 -0500 | [diff] [blame] | 311 | 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 Monk | 702e2eb | 2017-03-03 16:53:44 -0500 | [diff] [blame] | 319 | 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 Monk | 2b37190f | 2016-01-06 10:19:04 -0500 | [diff] [blame] | 325 | } |
Jason Monk | 702e2eb | 2017-03-03 16:53:44 -0500 | [diff] [blame] | 326 | 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 Spurlock | bceed06 | 2014-08-10 18:04:16 -0400 | [diff] [blame] | 337 | } |
| 338 | |
Amin Shaikh | a062beb | 2018-07-25 11:21:54 -0400 | [diff] [blame] | 339 | protected static List<String> loadTileSpecs(Context context, String tileList) { |
Jason Monk | bbadff8 | 2015-11-06 15:47:26 -0500 | [diff] [blame] | 340 | final Resources res = context.getResources(); |
John Spurlock | bceed06 | 2014-08-10 18:04:16 -0400 | [diff] [blame] | 341 | final String defaultTileList = res.getString(R.string.quick_settings_tiles_default); |
Fabian Kozynski | 7f370dd | 2018-12-05 10:41:07 -0500 | [diff] [blame] | 342 | if (TextUtils.isEmpty(tileList)) { |
John Spurlock | bceed06 | 2014-08-10 18:04:16 -0400 | [diff] [blame] | 343 | 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 Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 364 | } |