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; |
Jason Monk | a26641f | 2018-12-27 14:20:53 -0500 | [diff] [blame] | 59 | import javax.inject.Provider; |
Jason Monk | 5d57720 | 2018-12-26 15:43:06 -0500 | [diff] [blame] | 60 | import javax.inject.Singleton; |
| 61 | |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 62 | /** Platform implementation of the quick settings tile host **/ |
Jason Monk | 5d57720 | 2018-12-26 15:43:06 -0500 | [diff] [blame] | 63 | @Singleton |
Jason Monk | 702e2eb | 2017-03-03 16:53:44 -0500 | [diff] [blame] | 64 | public class QSTileHost implements QSHost, Tunable, PluginListener<QSFactory> { |
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 | baade75 | 2016-08-25 15:57:14 -0400 | [diff] [blame] | 68 | public static final String TILES_SETTING = Secure.QS_TILES; |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 69 | |
| 70 | private final Context mContext; |
Jason Monk | 702e2eb | 2017-03-03 16:53:44 -0500 | [diff] [blame] | 71 | private final LinkedHashMap<String, QSTile> mTiles = new LinkedHashMap<>(); |
Jason Monk | 5e74517 | 2015-06-02 19:14:44 -0400 | [diff] [blame] | 72 | protected final ArrayList<String> mTileSpecs = new ArrayList<>(); |
Jason Monk | d5a204f | 2015-12-21 08:50:01 -0500 | [diff] [blame] | 73 | private final TileServices mServices; |
Jason Monk | 30b64fa | 2018-12-27 13:19:34 -0500 | [diff] [blame] | 74 | private final TunerService mTunerService; |
| 75 | private final PluginManager mPluginManager; |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 76 | |
Jason Monk | bbadff8 | 2015-11-06 15:47:26 -0500 | [diff] [blame] | 77 | private final List<Callback> mCallbacks = new ArrayList<>(); |
Jason Monk | a26641f | 2018-12-27 14:20:53 -0500 | [diff] [blame] | 78 | private AutoTileManager mAutoTiles; |
Jason Monk | 9c7844c | 2017-01-18 15:21:53 -0500 | [diff] [blame] | 79 | private final StatusBarIconController mIconController; |
Jason Monk | 702e2eb | 2017-03-03 16:53:44 -0500 | [diff] [blame] | 80 | private final ArrayList<QSFactory> mQsFactories = new ArrayList<>(); |
Jason Monk | 1ffa11b | 2016-03-08 14:44:23 -0500 | [diff] [blame] | 81 | private int mCurrentUser; |
Jason Monk | 5d57720 | 2018-12-26 15:43:06 -0500 | [diff] [blame] | 82 | private StatusBar mStatusBar; |
John Spurlock | bceed06 | 2014-08-10 18:04:16 -0400 | [diff] [blame] | 83 | |
Jason Monk | 5d57720 | 2018-12-26 15:43:06 -0500 | [diff] [blame] | 84 | @Inject |
| 85 | public QSTileHost(Context context, |
| 86 | StatusBarIconController iconController, |
| 87 | QSFactoryImpl defaultFactory, |
Jason Monk | 30b64fa | 2018-12-27 13:19:34 -0500 | [diff] [blame] | 88 | @Named(Dependency.MAIN_HANDLER_NAME) Handler mainHandler, |
| 89 | @Named(Dependency.BG_LOOPER_NAME) Looper bgLooper, |
| 90 | PluginManager pluginManager, |
Jason Monk | a26641f | 2018-12-27 14:20:53 -0500 | [diff] [blame] | 91 | TunerService tunerService, |
| 92 | Provider<AutoTileManager> autoTiles) { |
Jason Monk | 9c7844c | 2017-01-18 15:21:53 -0500 | [diff] [blame] | 93 | mIconController = iconController; |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 94 | mContext = context; |
Jason Monk | 30b64fa | 2018-12-27 13:19:34 -0500 | [diff] [blame] | 95 | mTunerService = tunerService; |
| 96 | mPluginManager = pluginManager; |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 97 | |
Jason Monk | 30b64fa | 2018-12-27 13:19:34 -0500 | [diff] [blame] | 98 | mServices = new TileServices(this, bgLooper); |
Jason Monk | d5a204f | 2015-12-21 08:50:01 -0500 | [diff] [blame] | 99 | |
Jason Monk | 5d57720 | 2018-12-26 15:43:06 -0500 | [diff] [blame] | 100 | defaultFactory.setHost(this); |
| 101 | mQsFactories.add(defaultFactory); |
Jason Monk | 30b64fa | 2018-12-27 13:19:34 -0500 | [diff] [blame] | 102 | pluginManager.addPluginListener(this, QSFactory.class, true); |
Jason Monk | 702e2eb | 2017-03-03 16:53:44 -0500 | [diff] [blame] | 103 | |
Jason Monk | 5d57720 | 2018-12-26 15:43:06 -0500 | [diff] [blame] | 104 | mainHandler.post(() -> { |
| 105 | // This is technically a hack to avoid circular dependency of |
| 106 | // QSTileHost -> XXXTile -> QSTileHost. Posting ensures creation |
| 107 | // finishes before creating any tiles. |
Jason Monk | 30b64fa | 2018-12-27 13:19:34 -0500 | [diff] [blame] | 108 | tunerService.addTunable(this, TILES_SETTING); |
Jason Monk | a26641f | 2018-12-27 14:20:53 -0500 | [diff] [blame] | 109 | // AutoTileManager can modify mTiles so make sure mTiles has already been initialized. |
| 110 | mAutoTiles = autoTiles.get(); |
Jason Monk | 5d57720 | 2018-12-26 15:43:06 -0500 | [diff] [blame] | 111 | }); |
Jason Monk | 5e74517 | 2015-06-02 19:14:44 -0400 | [diff] [blame] | 112 | } |
John Spurlock | bceed06 | 2014-08-10 18:04:16 -0400 | [diff] [blame] | 113 | |
Jason Monk | 9c7844c | 2017-01-18 15:21:53 -0500 | [diff] [blame] | 114 | public StatusBarIconController getIconController() { |
| 115 | return mIconController; |
Jason Monk | ca894a0 | 2016-01-12 15:30:22 -0500 | [diff] [blame] | 116 | } |
| 117 | |
Jason Monk | 5e74517 | 2015-06-02 19:14:44 -0400 | [diff] [blame] | 118 | public void destroy() { |
Jason Monk | 8852905 | 2016-11-04 13:29:58 -0400 | [diff] [blame] | 119 | mTiles.values().forEach(tile -> tile.destroy()); |
Jason Monk | c3f42c1 | 2016-02-05 12:33:13 -0500 | [diff] [blame] | 120 | mAutoTiles.destroy(); |
Jason Monk | 30b64fa | 2018-12-27 13:19:34 -0500 | [diff] [blame] | 121 | mTunerService.removeTunable(this); |
Jason Monk | 8852905 | 2016-11-04 13:29:58 -0400 | [diff] [blame] | 122 | mServices.destroy(); |
Jason Monk | 30b64fa | 2018-12-27 13:19:34 -0500 | [diff] [blame] | 123 | mPluginManager.removePluginListener(this); |
Jason Monk | 702e2eb | 2017-03-03 16:53:44 -0500 | [diff] [blame] | 124 | } |
| 125 | |
| 126 | @Override |
| 127 | public void onPluginConnected(QSFactory plugin, Context pluginContext) { |
| 128 | // Give plugins priority over creation so they can override if they wish. |
| 129 | mQsFactories.add(0, plugin); |
Jason Monk | 30b64fa | 2018-12-27 13:19:34 -0500 | [diff] [blame] | 130 | String value = mTunerService.getValue(TILES_SETTING); |
Jason Monk | 702e2eb | 2017-03-03 16:53:44 -0500 | [diff] [blame] | 131 | // Force remove and recreate of all tiles. |
| 132 | onTuningChanged(TILES_SETTING, ""); |
| 133 | onTuningChanged(TILES_SETTING, value); |
| 134 | } |
| 135 | |
| 136 | @Override |
| 137 | public void onPluginDisconnected(QSFactory plugin) { |
| 138 | mQsFactories.remove(plugin); |
| 139 | // Force remove and recreate of all tiles. |
Jason Monk | 30b64fa | 2018-12-27 13:19:34 -0500 | [diff] [blame] | 140 | String value = mTunerService.getValue(TILES_SETTING); |
Jason Monk | 702e2eb | 2017-03-03 16:53:44 -0500 | [diff] [blame] | 141 | onTuningChanged(TILES_SETTING, ""); |
| 142 | onTuningChanged(TILES_SETTING, value); |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 143 | } |
| 144 | |
| 145 | @Override |
Jason Monk | bbadff8 | 2015-11-06 15:47:26 -0500 | [diff] [blame] | 146 | public void addCallback(Callback callback) { |
| 147 | mCallbacks.add(callback); |
John Spurlock | bceed06 | 2014-08-10 18:04:16 -0400 | [diff] [blame] | 148 | } |
| 149 | |
| 150 | @Override |
Jason Monk | 46dbfb4 | 2016-02-25 14:59:20 -0500 | [diff] [blame] | 151 | public void removeCallback(Callback callback) { |
| 152 | mCallbacks.remove(callback); |
| 153 | } |
| 154 | |
| 155 | @Override |
Jason Monk | 702e2eb | 2017-03-03 16:53:44 -0500 | [diff] [blame] | 156 | public Collection<QSTile> getTiles() { |
John Spurlock | bceed06 | 2014-08-10 18:04:16 -0400 | [diff] [blame] | 157 | return mTiles.values(); |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 158 | } |
| 159 | |
| 160 | @Override |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 161 | public void warn(String message, Throwable t) { |
| 162 | // already logged |
| 163 | } |
| 164 | |
| 165 | @Override |
| 166 | public void collapsePanels() { |
Jason Monk | 5d57720 | 2018-12-26 15:43:06 -0500 | [diff] [blame] | 167 | if (mStatusBar == null) { |
| 168 | mStatusBar = SysUiServiceProvider.getComponent(mContext, StatusBar.class); |
| 169 | } |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 170 | mStatusBar.postAnimateCollapsePanels(); |
| 171 | } |
| 172 | |
| 173 | @Override |
Yoshinori Hirano | c673fbc | 2017-04-14 11:29:38 +0900 | [diff] [blame] | 174 | public void forceCollapsePanels() { |
Jason Monk | 5d57720 | 2018-12-26 15:43:06 -0500 | [diff] [blame] | 175 | if (mStatusBar == null) { |
| 176 | mStatusBar = SysUiServiceProvider.getComponent(mContext, StatusBar.class); |
| 177 | } |
Yoshinori Hirano | c673fbc | 2017-04-14 11:29:38 +0900 | [diff] [blame] | 178 | mStatusBar.postAnimateForceCollapsePanels(); |
| 179 | } |
| 180 | |
| 181 | @Override |
Jason Monk | ba2318e | 2015-12-08 09:04:23 -0500 | [diff] [blame] | 182 | public void openPanels() { |
Jason Monk | 5d57720 | 2018-12-26 15:43:06 -0500 | [diff] [blame] | 183 | if (mStatusBar == null) { |
| 184 | mStatusBar = SysUiServiceProvider.getComponent(mContext, StatusBar.class); |
| 185 | } |
Jason Monk | ba2318e | 2015-12-08 09:04:23 -0500 | [diff] [blame] | 186 | mStatusBar.postAnimateOpenPanels(); |
| 187 | } |
| 188 | |
| 189 | @Override |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 190 | public Context getContext() { |
| 191 | return mContext; |
| 192 | } |
| 193 | |
Jason Monk | bbadff8 | 2015-11-06 15:47:26 -0500 | [diff] [blame] | 194 | |
Jason Monk | d5a204f | 2015-12-21 08:50:01 -0500 | [diff] [blame] | 195 | public TileServices getTileServices() { |
| 196 | return mServices; |
| 197 | } |
| 198 | |
Jason Monk | 8c09ac7 | 2017-03-16 11:53:40 -0400 | [diff] [blame] | 199 | public int indexOf(String spec) { |
| 200 | return mTileSpecs.indexOf(spec); |
| 201 | } |
| 202 | |
Jason Monk | 5e74517 | 2015-06-02 19:14:44 -0400 | [diff] [blame] | 203 | @Override |
| 204 | public void onTuningChanged(String key, String newValue) { |
| 205 | if (!TILES_SETTING.equals(key)) { |
| 206 | return; |
| 207 | } |
John Spurlock | bceed06 | 2014-08-10 18:04:16 -0400 | [diff] [blame] | 208 | if (DEBUG) Log.d(TAG, "Recreating tiles"); |
Sudheer Shanka | ad79049 | 2016-06-03 10:48:27 -0700 | [diff] [blame] | 209 | if (newValue == null && UserManager.isDeviceInDemoMode(mContext)) { |
| 210 | newValue = mContext.getResources().getString(R.string.quick_settings_tiles_retail_mode); |
| 211 | } |
Jason Monk | bbadff8 | 2015-11-06 15:47:26 -0500 | [diff] [blame] | 212 | final List<String> tileSpecs = loadTileSpecs(mContext, newValue); |
Jason Monk | 1ffa11b | 2016-03-08 14:44:23 -0500 | [diff] [blame] | 213 | int currentUser = ActivityManager.getCurrentUser(); |
| 214 | if (tileSpecs.equals(mTileSpecs) && currentUser == mCurrentUser) return; |
Jason Monk | 8852905 | 2016-11-04 13:29:58 -0400 | [diff] [blame] | 215 | mTiles.entrySet().stream().filter(tile -> !tileSpecs.contains(tile.getKey())).forEach( |
| 216 | tile -> { |
| 217 | if (DEBUG) Log.d(TAG, "Destroying tile: " + tile.getKey()); |
| 218 | tile.getValue().destroy(); |
| 219 | }); |
Jason Monk | 702e2eb | 2017-03-03 16:53:44 -0500 | [diff] [blame] | 220 | final LinkedHashMap<String, QSTile> newTiles = new LinkedHashMap<>(); |
John Spurlock | bceed06 | 2014-08-10 18:04:16 -0400 | [diff] [blame] | 221 | for (String tileSpec : tileSpecs) { |
Jason Monk | 702e2eb | 2017-03-03 16:53:44 -0500 | [diff] [blame] | 222 | QSTile tile = mTiles.get(tileSpec); |
Jason Monk | 1ffa11b | 2016-03-08 14:44:23 -0500 | [diff] [blame] | 223 | if (tile != null && (!(tile instanceof CustomTile) |
| 224 | || ((CustomTile) tile).getUser() == currentUser)) { |
Akitaka Kimura | efd1c54 | 2016-09-27 20:06:00 +0900 | [diff] [blame] | 225 | if (tile.isAvailable()) { |
| 226 | if (DEBUG) Log.d(TAG, "Adding " + tile); |
| 227 | tile.removeCallbacks(); |
Jason Monk | f120cf3 | 2016-12-06 11:34:12 -0500 | [diff] [blame] | 228 | if (!(tile instanceof CustomTile) && mCurrentUser != currentUser) { |
| 229 | tile.userSwitch(currentUser); |
| 230 | } |
Akitaka Kimura | efd1c54 | 2016-09-27 20:06:00 +0900 | [diff] [blame] | 231 | newTiles.put(tileSpec, tile); |
| 232 | } else { |
| 233 | tile.destroy(); |
Justin Klaassen | dd32d90 | 2016-07-31 10:25:36 -0700 | [diff] [blame] | 234 | } |
John Spurlock | bceed06 | 2014-08-10 18:04:16 -0400 | [diff] [blame] | 235 | } else { |
| 236 | if (DEBUG) Log.d(TAG, "Creating tile: " + tileSpec); |
| 237 | try { |
Jason Monk | 1ffa11b | 2016-03-08 14:44:23 -0500 | [diff] [blame] | 238 | tile = createTile(tileSpec); |
Jason Monk | 8852905 | 2016-11-04 13:29:58 -0400 | [diff] [blame] | 239 | if (tile != null) { |
| 240 | if (tile.isAvailable()) { |
| 241 | tile.setTileSpec(tileSpec); |
| 242 | newTiles.put(tileSpec, tile); |
| 243 | } else { |
| 244 | tile.destroy(); |
| 245 | } |
Jason Monk | ba2318e | 2015-12-08 09:04:23 -0500 | [diff] [blame] | 246 | } |
John Spurlock | bceed06 | 2014-08-10 18:04:16 -0400 | [diff] [blame] | 247 | } catch (Throwable t) { |
| 248 | Log.w(TAG, "Error creating tile for spec: " + tileSpec, t); |
| 249 | } |
| 250 | } |
| 251 | } |
Jason Monk | 1ffa11b | 2016-03-08 14:44:23 -0500 | [diff] [blame] | 252 | mCurrentUser = currentUser; |
Jason Monk | 11a7744 | 2015-05-12 19:29:02 -0400 | [diff] [blame] | 253 | mTileSpecs.clear(); |
| 254 | mTileSpecs.addAll(tileSpecs); |
John Spurlock | bceed06 | 2014-08-10 18:04:16 -0400 | [diff] [blame] | 255 | mTiles.clear(); |
| 256 | mTiles.putAll(newTiles); |
Jason Monk | bbadff8 | 2015-11-06 15:47:26 -0500 | [diff] [blame] | 257 | for (int i = 0; i < mCallbacks.size(); i++) { |
| 258 | mCallbacks.get(i).onTilesChanged(); |
John Spurlock | bceed06 | 2014-08-10 18:04:16 -0400 | [diff] [blame] | 259 | } |
| 260 | } |
| 261 | |
Jason Monk | bbadff8 | 2015-11-06 15:47:26 -0500 | [diff] [blame] | 262 | @Override |
Amin Shaikh | a062beb | 2018-07-25 11:21:54 -0400 | [diff] [blame] | 263 | public void removeTile(String spec) { |
| 264 | changeTileSpecs(tileSpecs-> tileSpecs.remove(spec)); |
Jason Monk | ba2318e | 2015-12-08 09:04:23 -0500 | [diff] [blame] | 265 | } |
| 266 | |
arangelov | 5fe9598 | 2018-08-17 18:43:31 +0100 | [diff] [blame] | 267 | @Override |
| 268 | public void unmarkTileAsAutoAdded(String spec) { |
Jason Monk | a26641f | 2018-12-27 14:20:53 -0500 | [diff] [blame] | 269 | if (mAutoTiles != null) mAutoTiles.unmarkTileAsAutoAdded(spec); |
arangelov | 5fe9598 | 2018-08-17 18:43:31 +0100 | [diff] [blame] | 270 | } |
| 271 | |
Jason Monk | c3f42c1 | 2016-02-05 12:33:13 -0500 | [diff] [blame] | 272 | public void addTile(String spec) { |
Amin Shaikh | a062beb | 2018-07-25 11:21:54 -0400 | [diff] [blame] | 273 | changeTileSpecs(tileSpecs-> tileSpecs.add(spec)); |
| 274 | } |
| 275 | |
| 276 | private void changeTileSpecs(Predicate<List<String>> changeFunction) { |
Ricky Wai | b96c1eac | 2016-06-02 11:42:16 +0100 | [diff] [blame] | 277 | final String setting = Settings.Secure.getStringForUser(mContext.getContentResolver(), |
Amin Shaikh | a062beb | 2018-07-25 11:21:54 -0400 | [diff] [blame] | 278 | TILES_SETTING, ActivityManager.getCurrentUser()); |
Ricky Wai | b96c1eac | 2016-06-02 11:42:16 +0100 | [diff] [blame] | 279 | final List<String> tileSpecs = loadTileSpecs(mContext, setting); |
Amin Shaikh | a062beb | 2018-07-25 11:21:54 -0400 | [diff] [blame] | 280 | if (changeFunction.test(tileSpecs)) { |
| 281 | Settings.Secure.putStringForUser(mContext.getContentResolver(), TILES_SETTING, |
Ricky Wai | b96c1eac | 2016-06-02 11:42:16 +0100 | [diff] [blame] | 282 | TextUtils.join(",", tileSpecs), ActivityManager.getCurrentUser()); |
Amin Shaikh | a062beb | 2018-07-25 11:21:54 -0400 | [diff] [blame] | 283 | } |
Jason Monk | c3f42c1 | 2016-02-05 12:33:13 -0500 | [diff] [blame] | 284 | } |
| 285 | |
Jason Monk | 7e53f20 | 2016-01-28 10:40:20 -0500 | [diff] [blame] | 286 | public void addTile(ComponentName tile) { |
| 287 | List<String> newSpecs = new ArrayList<>(mTileSpecs); |
| 288 | newSpecs.add(0, CustomTile.toSpec(tile)); |
| 289 | changeTiles(mTileSpecs, newSpecs); |
| 290 | } |
| 291 | |
| 292 | public void removeTile(ComponentName tile) { |
| 293 | List<String> newSpecs = new ArrayList<>(mTileSpecs); |
| 294 | newSpecs.remove(CustomTile.toSpec(tile)); |
| 295 | changeTiles(mTileSpecs, newSpecs); |
| 296 | } |
| 297 | |
| 298 | public void changeTiles(List<String> previousTiles, List<String> newTiles) { |
| 299 | final int NP = previousTiles.size(); |
| 300 | final int NA = newTiles.size(); |
| 301 | for (int i = 0; i < NP; i++) { |
| 302 | String tileSpec = previousTiles.get(i); |
| 303 | if (!tileSpec.startsWith(CustomTile.PREFIX)) continue; |
| 304 | if (!newTiles.contains(tileSpec)) { |
Jason Monk | a3453b8b | 2016-06-17 12:42:59 -0400 | [diff] [blame] | 305 | ComponentName component = CustomTile.getComponentFromSpec(tileSpec); |
| 306 | Intent intent = new Intent().setComponent(component); |
Jason Monk | 7e53f20 | 2016-01-28 10:40:20 -0500 | [diff] [blame] | 307 | TileLifecycleManager lifecycleManager = new TileLifecycleManager(new Handler(), |
Jason Monk | ee68fd8 | 2016-06-23 13:12:23 -0400 | [diff] [blame] | 308 | mContext, mServices, new Tile(), intent, |
Jason Monk | a3453b8b | 2016-06-17 12:42:59 -0400 | [diff] [blame] | 309 | new UserHandle(ActivityManager.getCurrentUser())); |
Jason Monk | 7e53f20 | 2016-01-28 10:40:20 -0500 | [diff] [blame] | 310 | lifecycleManager.onStopListening(); |
| 311 | lifecycleManager.onTileRemoved(); |
Jason Monk | baade75 | 2016-08-25 15:57:14 -0400 | [diff] [blame] | 312 | TileLifecycleManager.setTileAdded(mContext, component, false); |
Jason Monk | 7e53f20 | 2016-01-28 10:40:20 -0500 | [diff] [blame] | 313 | lifecycleManager.flushMessagesAndUnbind(); |
| 314 | } |
| 315 | } |
| 316 | if (DEBUG) Log.d(TAG, "saveCurrentTiles " + newTiles); |
| 317 | Secure.putStringForUser(getContext().getContentResolver(), QSTileHost.TILES_SETTING, |
| 318 | TextUtils.join(",", newTiles), ActivityManager.getCurrentUser()); |
| 319 | } |
| 320 | |
Jason Monk | 702e2eb | 2017-03-03 16:53:44 -0500 | [diff] [blame] | 321 | public QSTile createTile(String tileSpec) { |
| 322 | for (int i = 0; i < mQsFactories.size(); i++) { |
| 323 | QSTile t = mQsFactories.get(i).createTile(tileSpec); |
| 324 | if (t != null) { |
| 325 | return t; |
| 326 | } |
Jason Monk | 2b37190f | 2016-01-06 10:19:04 -0500 | [diff] [blame] | 327 | } |
Jason Monk | 702e2eb | 2017-03-03 16:53:44 -0500 | [diff] [blame] | 328 | return null; |
| 329 | } |
| 330 | |
| 331 | public QSTileView createTileView(QSTile tile, boolean collapsedView) { |
| 332 | for (int i = 0; i < mQsFactories.size(); i++) { |
| 333 | QSTileView view = mQsFactories.get(i).createTileView(tile, collapsedView); |
| 334 | if (view != null) { |
| 335 | return view; |
| 336 | } |
| 337 | } |
| 338 | throw new RuntimeException("Default factory didn't create view for " + tile.getTileSpec()); |
John Spurlock | bceed06 | 2014-08-10 18:04:16 -0400 | [diff] [blame] | 339 | } |
| 340 | |
Amin Shaikh | a062beb | 2018-07-25 11:21:54 -0400 | [diff] [blame] | 341 | protected static List<String> loadTileSpecs(Context context, String tileList) { |
Jason Monk | bbadff8 | 2015-11-06 15:47:26 -0500 | [diff] [blame] | 342 | final Resources res = context.getResources(); |
John Spurlock | bceed06 | 2014-08-10 18:04:16 -0400 | [diff] [blame] | 343 | final String defaultTileList = res.getString(R.string.quick_settings_tiles_default); |
Fabian Kozynski | 7f370dd | 2018-12-05 10:41:07 -0500 | [diff] [blame] | 344 | if (TextUtils.isEmpty(tileList)) { |
John Spurlock | bceed06 | 2014-08-10 18:04:16 -0400 | [diff] [blame] | 345 | tileList = res.getString(R.string.quick_settings_tiles); |
| 346 | if (DEBUG) Log.d(TAG, "Loaded tile specs from config: " + tileList); |
| 347 | } else { |
| 348 | if (DEBUG) Log.d(TAG, "Loaded tile specs from setting: " + tileList); |
| 349 | } |
| 350 | final ArrayList<String> tiles = new ArrayList<String>(); |
| 351 | boolean addedDefault = false; |
| 352 | for (String tile : tileList.split(",")) { |
| 353 | tile = tile.trim(); |
| 354 | if (tile.isEmpty()) continue; |
| 355 | if (tile.equals("default")) { |
| 356 | if (!addedDefault) { |
| 357 | tiles.addAll(Arrays.asList(defaultTileList.split(","))); |
| 358 | addedDefault = true; |
| 359 | } |
| 360 | } else { |
| 361 | tiles.add(tile); |
| 362 | } |
| 363 | } |
| 364 | return tiles; |
| 365 | } |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 366 | } |