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; |
Dan Sandler | 1e5d012 | 2019-06-06 23:35:22 -0400 | [diff] [blame] | 22 | import android.os.Build; |
Jason Monk | 7e53f20 | 2016-01-28 10:40:20 -0500 | [diff] [blame] | 23 | import android.os.Handler; |
Jason Monk | 30b64fa | 2018-12-27 13:19:34 -0500 | [diff] [blame] | 24 | import android.os.Looper; |
Jason Monk | 7e53f20 | 2016-01-28 10:40:20 -0500 | [diff] [blame] | 25 | import android.os.UserHandle; |
Sudheer Shanka | ad79049 | 2016-06-03 10:48:27 -0700 | [diff] [blame] | 26 | import android.os.UserManager; |
Jason Monk | ba2318e | 2015-12-08 09:04:23 -0500 | [diff] [blame] | 27 | import android.provider.Settings; |
Jason Monk | 7e53f20 | 2016-01-28 10:40:20 -0500 | [diff] [blame] | 28 | import android.provider.Settings.Secure; |
Jason Monk | a3453b8b | 2016-06-17 12:42:59 -0400 | [diff] [blame] | 29 | import android.service.quicksettings.Tile; |
Jason Monk | ba2318e | 2015-12-08 09:04:23 -0500 | [diff] [blame] | 30 | import android.text.TextUtils; |
Fabian Kozynski | 4ba3f9a | 2020-02-21 11:02:42 -0500 | [diff] [blame] | 31 | import android.util.ArraySet; |
John Spurlock | bceed06 | 2014-08-10 18:04:16 -0400 | [diff] [blame] | 32 | import android.util.Log; |
Winson | c0d7058 | 2016-01-29 10:24:39 -0800 | [diff] [blame] | 33 | |
Fabian Kozynski | 2ff6df9 | 2020-04-24 12:00:49 -0400 | [diff] [blame] | 34 | import com.android.internal.logging.InstanceId; |
| 35 | import com.android.internal.logging.InstanceIdSequence; |
| 36 | import com.android.internal.logging.UiEventLogger; |
Fabian Kozynski | 00d494d | 2019-04-04 09:53:50 -0400 | [diff] [blame] | 37 | import com.android.systemui.Dumpable; |
John Spurlock | bceed06 | 2014-08-10 18:04:16 -0400 | [diff] [blame] | 38 | import com.android.systemui.R; |
Fabian Kozynski | 5ca7a51 | 2019-10-16 19:56:11 +0000 | [diff] [blame] | 39 | import com.android.systemui.broadcast.BroadcastDispatcher; |
Dave Mankoff | 00e8a2f | 2019-12-18 16:59:49 -0500 | [diff] [blame] | 40 | import com.android.systemui.dagger.qualifiers.Background; |
| 41 | import com.android.systemui.dagger.qualifiers.Main; |
Ned Burns | aaeb44b | 2020-02-12 23:48:26 -0500 | [diff] [blame] | 42 | import com.android.systemui.dump.DumpManager; |
Jason Monk | 702e2eb | 2017-03-03 16:53:44 -0500 | [diff] [blame] | 43 | import com.android.systemui.plugins.PluginListener; |
Jason Monk | 702e2eb | 2017-03-03 16:53:44 -0500 | [diff] [blame] | 44 | import com.android.systemui.plugins.qs.QSFactory; |
Jason Monk | 702e2eb | 2017-03-03 16:53:44 -0500 | [diff] [blame] | 45 | import com.android.systemui.plugins.qs.QSTile; |
Gus Prevas | ab33679 | 2018-11-14 13:52:20 -0500 | [diff] [blame] | 46 | import com.android.systemui.plugins.qs.QSTileView; |
Jason Monk | d5a204f | 2015-12-21 08:50:01 -0500 | [diff] [blame] | 47 | import com.android.systemui.qs.external.CustomTile; |
Jason Monk | 7e53f20 | 2016-01-28 10:40:20 -0500 | [diff] [blame] | 48 | import com.android.systemui.qs.external.TileLifecycleManager; |
Jason Monk | d5a204f | 2015-12-21 08:50:01 -0500 | [diff] [blame] | 49 | import com.android.systemui.qs.external.TileServices; |
Fabian Kozynski | ca30f57 | 2019-10-24 10:01:04 -0400 | [diff] [blame] | 50 | import com.android.systemui.qs.logging.QSLogger; |
Gus Prevas | ab33679 | 2018-11-14 13:52:20 -0500 | [diff] [blame] | 51 | import com.android.systemui.shared.plugins.PluginManager; |
Jason Monk | e5b770e | 2017-03-03 21:49:29 -0500 | [diff] [blame] | 52 | import com.android.systemui.statusbar.phone.AutoTileManager; |
| 53 | import com.android.systemui.statusbar.phone.StatusBar; |
| 54 | import com.android.systemui.statusbar.phone.StatusBarIconController; |
Jason Monk | 5e74517 | 2015-06-02 19:14:44 -0400 | [diff] [blame] | 55 | import com.android.systemui.tuner.TunerService; |
| 56 | import com.android.systemui.tuner.TunerService.Tunable; |
Dan Sandler | 1e5d012 | 2019-06-06 23:35:22 -0400 | [diff] [blame] | 57 | import com.android.systemui.util.leak.GarbageMonitor; |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 58 | |
Fabian Kozynski | 00d494d | 2019-04-04 09:53:50 -0400 | [diff] [blame] | 59 | import java.io.FileDescriptor; |
| 60 | import java.io.PrintWriter; |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 61 | import java.util.ArrayList; |
John Spurlock | bceed06 | 2014-08-10 18:04:16 -0400 | [diff] [blame] | 62 | import java.util.Arrays; |
| 63 | import java.util.Collection; |
| 64 | import java.util.LinkedHashMap; |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 65 | import java.util.List; |
Dave Mankoff | 4dd47fa | 2019-11-07 10:44:48 -0500 | [diff] [blame] | 66 | import java.util.Optional; |
Fabian Kozynski | 4ba3f9a | 2020-02-21 11:02:42 -0500 | [diff] [blame] | 67 | import java.util.Set; |
Amin Shaikh | a062beb | 2018-07-25 11:21:54 -0400 | [diff] [blame] | 68 | import java.util.function.Predicate; |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 69 | |
Jason Monk | 5d57720 | 2018-12-26 15:43:06 -0500 | [diff] [blame] | 70 | import javax.inject.Inject; |
Jason Monk | a26641f | 2018-12-27 14:20:53 -0500 | [diff] [blame] | 71 | import javax.inject.Provider; |
Jason Monk | 5d57720 | 2018-12-26 15:43:06 -0500 | [diff] [blame] | 72 | import javax.inject.Singleton; |
| 73 | |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 74 | /** Platform implementation of the quick settings tile host **/ |
Jason Monk | 5d57720 | 2018-12-26 15:43:06 -0500 | [diff] [blame] | 75 | @Singleton |
Fabian Kozynski | 00d494d | 2019-04-04 09:53:50 -0400 | [diff] [blame] | 76 | public class QSTileHost implements QSHost, Tunable, PluginListener<QSFactory>, Dumpable { |
John Spurlock | bceed06 | 2014-08-10 18:04:16 -0400 | [diff] [blame] | 77 | private static final String TAG = "QSTileHost"; |
| 78 | private static final boolean DEBUG = Log.isLoggable(TAG, Log.DEBUG); |
Fabian Kozynski | 2ff6df9 | 2020-04-24 12:00:49 -0400 | [diff] [blame] | 79 | private static final int MAX_QS_INSTANCE_ID = 1 << 20; |
John Spurlock | bceed06 | 2014-08-10 18:04:16 -0400 | [diff] [blame] | 80 | |
Jason Monk | baade75 | 2016-08-25 15:57:14 -0400 | [diff] [blame] | 81 | public static final String TILES_SETTING = Secure.QS_TILES; |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 82 | |
| 83 | private final Context mContext; |
Jason Monk | 702e2eb | 2017-03-03 16:53:44 -0500 | [diff] [blame] | 84 | private final LinkedHashMap<String, QSTile> mTiles = new LinkedHashMap<>(); |
Jason Monk | 5e74517 | 2015-06-02 19:14:44 -0400 | [diff] [blame] | 85 | protected final ArrayList<String> mTileSpecs = new ArrayList<>(); |
Jason Monk | d5a204f | 2015-12-21 08:50:01 -0500 | [diff] [blame] | 86 | private final TileServices mServices; |
Jason Monk | 30b64fa | 2018-12-27 13:19:34 -0500 | [diff] [blame] | 87 | private final TunerService mTunerService; |
| 88 | private final PluginManager mPluginManager; |
Ned Burns | aaeb44b | 2020-02-12 23:48:26 -0500 | [diff] [blame] | 89 | private final DumpManager mDumpManager; |
Fabian Kozynski | 5ca7a51 | 2019-10-16 19:56:11 +0000 | [diff] [blame] | 90 | private final BroadcastDispatcher mBroadcastDispatcher; |
Fabian Kozynski | ca30f57 | 2019-10-24 10:01:04 -0400 | [diff] [blame] | 91 | private final QSLogger mQSLogger; |
Fabian Kozynski | 2ff6df9 | 2020-04-24 12:00:49 -0400 | [diff] [blame] | 92 | private final UiEventLogger mUiEventLogger; |
| 93 | private final InstanceIdSequence mInstanceIdSequence; |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 94 | |
Jason Monk | bbadff8 | 2015-11-06 15:47:26 -0500 | [diff] [blame] | 95 | private final List<Callback> mCallbacks = new ArrayList<>(); |
Jason Monk | a26641f | 2018-12-27 14:20:53 -0500 | [diff] [blame] | 96 | private AutoTileManager mAutoTiles; |
Jason Monk | 9c7844c | 2017-01-18 15:21:53 -0500 | [diff] [blame] | 97 | private final StatusBarIconController mIconController; |
Jason Monk | 702e2eb | 2017-03-03 16:53:44 -0500 | [diff] [blame] | 98 | private final ArrayList<QSFactory> mQsFactories = new ArrayList<>(); |
Jason Monk | 1ffa11b | 2016-03-08 14:44:23 -0500 | [diff] [blame] | 99 | private int mCurrentUser; |
Dave Mankoff | 4dd47fa | 2019-11-07 10:44:48 -0500 | [diff] [blame] | 100 | private final Optional<StatusBar> mStatusBarOptional; |
Fabian Kozynski | 4ba3f9a | 2020-02-21 11:02:42 -0500 | [diff] [blame] | 101 | private Context mUserContext; |
John Spurlock | bceed06 | 2014-08-10 18:04:16 -0400 | [diff] [blame] | 102 | |
Jason Monk | 5d57720 | 2018-12-26 15:43:06 -0500 | [diff] [blame] | 103 | @Inject |
| 104 | public QSTileHost(Context context, |
| 105 | StatusBarIconController iconController, |
John Li | 4e3dddc | 2020-03-04 21:35:43 +0800 | [diff] [blame] | 106 | QSFactory defaultFactory, |
Dave Mankoff | 00e8a2f | 2019-12-18 16:59:49 -0500 | [diff] [blame] | 107 | @Main Handler mainHandler, |
| 108 | @Background Looper bgLooper, |
Jason Monk | 30b64fa | 2018-12-27 13:19:34 -0500 | [diff] [blame] | 109 | PluginManager pluginManager, |
Jason Monk | a26641f | 2018-12-27 14:20:53 -0500 | [diff] [blame] | 110 | TunerService tunerService, |
Fabian Kozynski | 00d494d | 2019-04-04 09:53:50 -0400 | [diff] [blame] | 111 | Provider<AutoTileManager> autoTiles, |
Ned Burns | aaeb44b | 2020-02-12 23:48:26 -0500 | [diff] [blame] | 112 | DumpManager dumpManager, |
Dave Mankoff | 4dd47fa | 2019-11-07 10:44:48 -0500 | [diff] [blame] | 113 | BroadcastDispatcher broadcastDispatcher, |
Fabian Kozynski | ca30f57 | 2019-10-24 10:01:04 -0400 | [diff] [blame] | 114 | Optional<StatusBar> statusBarOptional, |
Fabian Kozynski | 2ff6df9 | 2020-04-24 12:00:49 -0400 | [diff] [blame] | 115 | QSLogger qsLogger, |
| 116 | UiEventLogger uiEventLogger) { |
Jason Monk | 9c7844c | 2017-01-18 15:21:53 -0500 | [diff] [blame] | 117 | mIconController = iconController; |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 118 | mContext = context; |
Fabian Kozynski | 4ba3f9a | 2020-02-21 11:02:42 -0500 | [diff] [blame] | 119 | mUserContext = context; |
Jason Monk | 30b64fa | 2018-12-27 13:19:34 -0500 | [diff] [blame] | 120 | mTunerService = tunerService; |
| 121 | mPluginManager = pluginManager; |
Ned Burns | aaeb44b | 2020-02-12 23:48:26 -0500 | [diff] [blame] | 122 | mDumpManager = dumpManager; |
Fabian Kozynski | ca30f57 | 2019-10-24 10:01:04 -0400 | [diff] [blame] | 123 | mQSLogger = qsLogger; |
Fabian Kozynski | 2ff6df9 | 2020-04-24 12:00:49 -0400 | [diff] [blame] | 124 | mUiEventLogger = uiEventLogger; |
Fabian Kozynski | 5ca7a51 | 2019-10-16 19:56:11 +0000 | [diff] [blame] | 125 | mBroadcastDispatcher = broadcastDispatcher; |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 126 | |
Fabian Kozynski | 2ff6df9 | 2020-04-24 12:00:49 -0400 | [diff] [blame] | 127 | mInstanceIdSequence = new InstanceIdSequence(MAX_QS_INSTANCE_ID); |
Fabian Kozynski | 5ca7a51 | 2019-10-16 19:56:11 +0000 | [diff] [blame] | 128 | mServices = new TileServices(this, bgLooper, mBroadcastDispatcher); |
Dave Mankoff | 4dd47fa | 2019-11-07 10:44:48 -0500 | [diff] [blame] | 129 | mStatusBarOptional = statusBarOptional; |
Jason Monk | d5a204f | 2015-12-21 08:50:01 -0500 | [diff] [blame] | 130 | |
Jason Monk | 5d57720 | 2018-12-26 15:43:06 -0500 | [diff] [blame] | 131 | mQsFactories.add(defaultFactory); |
Jason Monk | 30b64fa | 2018-12-27 13:19:34 -0500 | [diff] [blame] | 132 | pluginManager.addPluginListener(this, QSFactory.class, true); |
Ned Burns | aaeb44b | 2020-02-12 23:48:26 -0500 | [diff] [blame] | 133 | mDumpManager.registerDumpable(TAG, this); |
Jason Monk | 702e2eb | 2017-03-03 16:53:44 -0500 | [diff] [blame] | 134 | |
Jason Monk | 5d57720 | 2018-12-26 15:43:06 -0500 | [diff] [blame] | 135 | mainHandler.post(() -> { |
| 136 | // This is technically a hack to avoid circular dependency of |
| 137 | // QSTileHost -> XXXTile -> QSTileHost. Posting ensures creation |
| 138 | // finishes before creating any tiles. |
Jason Monk | 30b64fa | 2018-12-27 13:19:34 -0500 | [diff] [blame] | 139 | tunerService.addTunable(this, TILES_SETTING); |
Jason Monk | a26641f | 2018-12-27 14:20:53 -0500 | [diff] [blame] | 140 | // AutoTileManager can modify mTiles so make sure mTiles has already been initialized. |
| 141 | mAutoTiles = autoTiles.get(); |
Jason Monk | 5d57720 | 2018-12-26 15:43:06 -0500 | [diff] [blame] | 142 | }); |
Jason Monk | 5e74517 | 2015-06-02 19:14:44 -0400 | [diff] [blame] | 143 | } |
John Spurlock | bceed06 | 2014-08-10 18:04:16 -0400 | [diff] [blame] | 144 | |
Jason Monk | 9c7844c | 2017-01-18 15:21:53 -0500 | [diff] [blame] | 145 | public StatusBarIconController getIconController() { |
| 146 | return mIconController; |
Jason Monk | ca894a0 | 2016-01-12 15:30:22 -0500 | [diff] [blame] | 147 | } |
| 148 | |
Fabian Kozynski | 2ff6df9 | 2020-04-24 12:00:49 -0400 | [diff] [blame] | 149 | @Override |
| 150 | public InstanceId getNewInstanceId() { |
| 151 | return mInstanceIdSequence.newInstanceId(); |
| 152 | } |
| 153 | |
Jason Monk | 5e74517 | 2015-06-02 19:14:44 -0400 | [diff] [blame] | 154 | public void destroy() { |
Jason Monk | 8852905 | 2016-11-04 13:29:58 -0400 | [diff] [blame] | 155 | mTiles.values().forEach(tile -> tile.destroy()); |
Jason Monk | c3f42c1 | 2016-02-05 12:33:13 -0500 | [diff] [blame] | 156 | mAutoTiles.destroy(); |
Jason Monk | 30b64fa | 2018-12-27 13:19:34 -0500 | [diff] [blame] | 157 | mTunerService.removeTunable(this); |
Jason Monk | 8852905 | 2016-11-04 13:29:58 -0400 | [diff] [blame] | 158 | mServices.destroy(); |
Jason Monk | 30b64fa | 2018-12-27 13:19:34 -0500 | [diff] [blame] | 159 | mPluginManager.removePluginListener(this); |
Ned Burns | aaeb44b | 2020-02-12 23:48:26 -0500 | [diff] [blame] | 160 | mDumpManager.unregisterDumpable(TAG); |
Jason Monk | 702e2eb | 2017-03-03 16:53:44 -0500 | [diff] [blame] | 161 | } |
| 162 | |
| 163 | @Override |
| 164 | public void onPluginConnected(QSFactory plugin, Context pluginContext) { |
| 165 | // Give plugins priority over creation so they can override if they wish. |
| 166 | mQsFactories.add(0, plugin); |
Jason Monk | 30b64fa | 2018-12-27 13:19:34 -0500 | [diff] [blame] | 167 | String value = mTunerService.getValue(TILES_SETTING); |
Jason Monk | 702e2eb | 2017-03-03 16:53:44 -0500 | [diff] [blame] | 168 | // Force remove and recreate of all tiles. |
| 169 | onTuningChanged(TILES_SETTING, ""); |
| 170 | onTuningChanged(TILES_SETTING, value); |
| 171 | } |
| 172 | |
| 173 | @Override |
| 174 | public void onPluginDisconnected(QSFactory plugin) { |
| 175 | mQsFactories.remove(plugin); |
| 176 | // Force remove and recreate of all tiles. |
Jason Monk | 30b64fa | 2018-12-27 13:19:34 -0500 | [diff] [blame] | 177 | String value = mTunerService.getValue(TILES_SETTING); |
Jason Monk | 702e2eb | 2017-03-03 16:53:44 -0500 | [diff] [blame] | 178 | onTuningChanged(TILES_SETTING, ""); |
| 179 | onTuningChanged(TILES_SETTING, value); |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 180 | } |
| 181 | |
Fabian Kozynski | ca30f57 | 2019-10-24 10:01:04 -0400 | [diff] [blame] | 182 | public QSLogger getQSLogger() { |
| 183 | return mQSLogger; |
| 184 | } |
| 185 | |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 186 | @Override |
Fabian Kozynski | 2ff6df9 | 2020-04-24 12:00:49 -0400 | [diff] [blame] | 187 | public UiEventLogger getUiEventLogger() { |
| 188 | return mUiEventLogger; |
| 189 | } |
| 190 | |
| 191 | @Override |
Jason Monk | bbadff8 | 2015-11-06 15:47:26 -0500 | [diff] [blame] | 192 | public void addCallback(Callback callback) { |
| 193 | mCallbacks.add(callback); |
John Spurlock | bceed06 | 2014-08-10 18:04:16 -0400 | [diff] [blame] | 194 | } |
| 195 | |
| 196 | @Override |
Jason Monk | 46dbfb4 | 2016-02-25 14:59:20 -0500 | [diff] [blame] | 197 | public void removeCallback(Callback callback) { |
| 198 | mCallbacks.remove(callback); |
| 199 | } |
| 200 | |
| 201 | @Override |
Jason Monk | 702e2eb | 2017-03-03 16:53:44 -0500 | [diff] [blame] | 202 | public Collection<QSTile> getTiles() { |
John Spurlock | bceed06 | 2014-08-10 18:04:16 -0400 | [diff] [blame] | 203 | return mTiles.values(); |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 204 | } |
| 205 | |
| 206 | @Override |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 207 | public void warn(String message, Throwable t) { |
| 208 | // already logged |
| 209 | } |
| 210 | |
| 211 | @Override |
| 212 | public void collapsePanels() { |
Dave Mankoff | 4dd47fa | 2019-11-07 10:44:48 -0500 | [diff] [blame] | 213 | mStatusBarOptional.ifPresent(StatusBar::postAnimateCollapsePanels); |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 214 | } |
| 215 | |
| 216 | @Override |
Yoshinori Hirano | c673fbc | 2017-04-14 11:29:38 +0900 | [diff] [blame] | 217 | public void forceCollapsePanels() { |
Dave Mankoff | 4dd47fa | 2019-11-07 10:44:48 -0500 | [diff] [blame] | 218 | mStatusBarOptional.ifPresent(StatusBar::postAnimateForceCollapsePanels); |
Yoshinori Hirano | c673fbc | 2017-04-14 11:29:38 +0900 | [diff] [blame] | 219 | } |
| 220 | |
| 221 | @Override |
Jason Monk | ba2318e | 2015-12-08 09:04:23 -0500 | [diff] [blame] | 222 | public void openPanels() { |
Dave Mankoff | 4dd47fa | 2019-11-07 10:44:48 -0500 | [diff] [blame] | 223 | mStatusBarOptional.ifPresent(StatusBar::postAnimateOpenPanels); |
Jason Monk | ba2318e | 2015-12-08 09:04:23 -0500 | [diff] [blame] | 224 | } |
| 225 | |
| 226 | @Override |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 227 | public Context getContext() { |
| 228 | return mContext; |
| 229 | } |
| 230 | |
John Li | 4e3dddc | 2020-03-04 21:35:43 +0800 | [diff] [blame] | 231 | @Override |
Fabian Kozynski | 4ba3f9a | 2020-02-21 11:02:42 -0500 | [diff] [blame] | 232 | public Context getUserContext() { |
| 233 | return mUserContext; |
| 234 | } |
Jason Monk | bbadff8 | 2015-11-06 15:47:26 -0500 | [diff] [blame] | 235 | |
John Li | 4e3dddc | 2020-03-04 21:35:43 +0800 | [diff] [blame] | 236 | @Override |
Jason Monk | d5a204f | 2015-12-21 08:50:01 -0500 | [diff] [blame] | 237 | public TileServices getTileServices() { |
| 238 | return mServices; |
| 239 | } |
| 240 | |
Jason Monk | 8c09ac7 | 2017-03-16 11:53:40 -0400 | [diff] [blame] | 241 | public int indexOf(String spec) { |
| 242 | return mTileSpecs.indexOf(spec); |
| 243 | } |
| 244 | |
Jason Monk | 5e74517 | 2015-06-02 19:14:44 -0400 | [diff] [blame] | 245 | @Override |
| 246 | public void onTuningChanged(String key, String newValue) { |
| 247 | if (!TILES_SETTING.equals(key)) { |
| 248 | return; |
| 249 | } |
Fabian Kozynski | 23e000f | 2019-09-06 15:22:24 -0400 | [diff] [blame] | 250 | Log.d(TAG, "Recreating tiles"); |
Sudheer Shanka | ad79049 | 2016-06-03 10:48:27 -0700 | [diff] [blame] | 251 | if (newValue == null && UserManager.isDeviceInDemoMode(mContext)) { |
| 252 | newValue = mContext.getResources().getString(R.string.quick_settings_tiles_retail_mode); |
| 253 | } |
Jason Monk | bbadff8 | 2015-11-06 15:47:26 -0500 | [diff] [blame] | 254 | final List<String> tileSpecs = loadTileSpecs(mContext, newValue); |
Jason Monk | 1ffa11b | 2016-03-08 14:44:23 -0500 | [diff] [blame] | 255 | int currentUser = ActivityManager.getCurrentUser(); |
Fabian Kozynski | 4ba3f9a | 2020-02-21 11:02:42 -0500 | [diff] [blame] | 256 | if (currentUser != mCurrentUser) { |
| 257 | mUserContext = mContext.createContextAsUser(UserHandle.of(currentUser), 0); |
| 258 | } |
Jason Monk | 1ffa11b | 2016-03-08 14:44:23 -0500 | [diff] [blame] | 259 | if (tileSpecs.equals(mTileSpecs) && currentUser == mCurrentUser) return; |
Jason Monk | 8852905 | 2016-11-04 13:29:58 -0400 | [diff] [blame] | 260 | mTiles.entrySet().stream().filter(tile -> !tileSpecs.contains(tile.getKey())).forEach( |
| 261 | tile -> { |
Fabian Kozynski | 23e000f | 2019-09-06 15:22:24 -0400 | [diff] [blame] | 262 | Log.d(TAG, "Destroying tile: " + tile.getKey()); |
Fabian Kozynski | ca30f57 | 2019-10-24 10:01:04 -0400 | [diff] [blame] | 263 | mQSLogger.logTileDestroyed(tile.getKey(), "Tile removed"); |
Jason Monk | 8852905 | 2016-11-04 13:29:58 -0400 | [diff] [blame] | 264 | tile.getValue().destroy(); |
| 265 | }); |
Jason Monk | 702e2eb | 2017-03-03 16:53:44 -0500 | [diff] [blame] | 266 | final LinkedHashMap<String, QSTile> newTiles = new LinkedHashMap<>(); |
John Spurlock | bceed06 | 2014-08-10 18:04:16 -0400 | [diff] [blame] | 267 | for (String tileSpec : tileSpecs) { |
Jason Monk | 702e2eb | 2017-03-03 16:53:44 -0500 | [diff] [blame] | 268 | QSTile tile = mTiles.get(tileSpec); |
Jason Monk | 1ffa11b | 2016-03-08 14:44:23 -0500 | [diff] [blame] | 269 | if (tile != null && (!(tile instanceof CustomTile) |
| 270 | || ((CustomTile) tile).getUser() == currentUser)) { |
Akitaka Kimura | efd1c54 | 2016-09-27 20:06:00 +0900 | [diff] [blame] | 271 | if (tile.isAvailable()) { |
| 272 | if (DEBUG) Log.d(TAG, "Adding " + tile); |
| 273 | tile.removeCallbacks(); |
Jason Monk | f120cf3 | 2016-12-06 11:34:12 -0500 | [diff] [blame] | 274 | if (!(tile instanceof CustomTile) && mCurrentUser != currentUser) { |
| 275 | tile.userSwitch(currentUser); |
| 276 | } |
Akitaka Kimura | efd1c54 | 2016-09-27 20:06:00 +0900 | [diff] [blame] | 277 | newTiles.put(tileSpec, tile); |
Fabian Kozynski | ca30f57 | 2019-10-24 10:01:04 -0400 | [diff] [blame] | 278 | mQSLogger.logTileAdded(tileSpec); |
Akitaka Kimura | efd1c54 | 2016-09-27 20:06:00 +0900 | [diff] [blame] | 279 | } else { |
| 280 | tile.destroy(); |
Fabian Kozynski | 23e000f | 2019-09-06 15:22:24 -0400 | [diff] [blame] | 281 | Log.d(TAG, "Destroying not available tile: " + tileSpec); |
Fabian Kozynski | ca30f57 | 2019-10-24 10:01:04 -0400 | [diff] [blame] | 282 | mQSLogger.logTileDestroyed(tileSpec, "Tile not available"); |
Justin Klaassen | dd32d90 | 2016-07-31 10:25:36 -0700 | [diff] [blame] | 283 | } |
John Spurlock | bceed06 | 2014-08-10 18:04:16 -0400 | [diff] [blame] | 284 | } else { |
Fabian Kozynski | 4ba3f9a | 2020-02-21 11:02:42 -0500 | [diff] [blame] | 285 | // This means that the tile is a CustomTile AND the user is different, so let's |
| 286 | // destroy it |
| 287 | if (tile != null) { |
| 288 | tile.destroy(); |
| 289 | Log.d(TAG, "Destroying tile for wrong user: " + tileSpec); |
| 290 | mQSLogger.logTileDestroyed(tileSpec, "Tile for wrong user"); |
| 291 | } |
Fabian Kozynski | 23e000f | 2019-09-06 15:22:24 -0400 | [diff] [blame] | 292 | Log.d(TAG, "Creating tile: " + tileSpec); |
John Spurlock | bceed06 | 2014-08-10 18:04:16 -0400 | [diff] [blame] | 293 | try { |
Jason Monk | 1ffa11b | 2016-03-08 14:44:23 -0500 | [diff] [blame] | 294 | tile = createTile(tileSpec); |
Jason Monk | 8852905 | 2016-11-04 13:29:58 -0400 | [diff] [blame] | 295 | if (tile != null) { |
Fabian Kozynski | 687da67 | 2020-03-06 11:17:41 -0500 | [diff] [blame] | 296 | tile.setTileSpec(tileSpec); |
Jason Monk | 8852905 | 2016-11-04 13:29:58 -0400 | [diff] [blame] | 297 | if (tile.isAvailable()) { |
Jason Monk | 8852905 | 2016-11-04 13:29:58 -0400 | [diff] [blame] | 298 | newTiles.put(tileSpec, tile); |
Fabian Kozynski | ca30f57 | 2019-10-24 10:01:04 -0400 | [diff] [blame] | 299 | mQSLogger.logTileAdded(tileSpec); |
Jason Monk | 8852905 | 2016-11-04 13:29:58 -0400 | [diff] [blame] | 300 | } else { |
| 301 | tile.destroy(); |
Fabian Kozynski | 23e000f | 2019-09-06 15:22:24 -0400 | [diff] [blame] | 302 | Log.d(TAG, "Destroying not available tile: " + tileSpec); |
Fabian Kozynski | ca30f57 | 2019-10-24 10:01:04 -0400 | [diff] [blame] | 303 | mQSLogger.logTileDestroyed(tileSpec, "Tile not available"); |
Jason Monk | 8852905 | 2016-11-04 13:29:58 -0400 | [diff] [blame] | 304 | } |
Jason Monk | ba2318e | 2015-12-08 09:04:23 -0500 | [diff] [blame] | 305 | } |
John Spurlock | bceed06 | 2014-08-10 18:04:16 -0400 | [diff] [blame] | 306 | } catch (Throwable t) { |
| 307 | Log.w(TAG, "Error creating tile for spec: " + tileSpec, t); |
| 308 | } |
| 309 | } |
| 310 | } |
Jason Monk | 1ffa11b | 2016-03-08 14:44:23 -0500 | [diff] [blame] | 311 | mCurrentUser = currentUser; |
Fabian Kozynski | 4ba3f9a | 2020-02-21 11:02:42 -0500 | [diff] [blame] | 312 | List<String> currentSpecs = new ArrayList<>(mTileSpecs); |
Jason Monk | 11a7744 | 2015-05-12 19:29:02 -0400 | [diff] [blame] | 313 | mTileSpecs.clear(); |
| 314 | mTileSpecs.addAll(tileSpecs); |
John Spurlock | bceed06 | 2014-08-10 18:04:16 -0400 | [diff] [blame] | 315 | mTiles.clear(); |
| 316 | mTiles.putAll(newTiles); |
Fabian Kozynski | 6ff5c18 | 2019-06-20 14:57:47 -0400 | [diff] [blame] | 317 | if (newTiles.isEmpty() && !tileSpecs.isEmpty()) { |
| 318 | // If we didn't manage to create any tiles, set it to empty (default) |
Fabian Kozynski | 23e000f | 2019-09-06 15:22:24 -0400 | [diff] [blame] | 319 | Log.d(TAG, "No valid tiles on tuning changed. Setting to default."); |
Fabian Kozynski | 6ff5c18 | 2019-06-20 14:57:47 -0400 | [diff] [blame] | 320 | changeTiles(currentSpecs, loadTileSpecs(mContext, "")); |
| 321 | } else { |
| 322 | for (int i = 0; i < mCallbacks.size(); i++) { |
| 323 | mCallbacks.get(i).onTilesChanged(); |
| 324 | } |
John Spurlock | bceed06 | 2014-08-10 18:04:16 -0400 | [diff] [blame] | 325 | } |
| 326 | } |
| 327 | |
Jason Monk | bbadff8 | 2015-11-06 15:47:26 -0500 | [diff] [blame] | 328 | @Override |
Amin Shaikh | a062beb | 2018-07-25 11:21:54 -0400 | [diff] [blame] | 329 | public void removeTile(String spec) { |
| 330 | changeTileSpecs(tileSpecs-> tileSpecs.remove(spec)); |
Jason Monk | ba2318e | 2015-12-08 09:04:23 -0500 | [diff] [blame] | 331 | } |
| 332 | |
arangelov | 5fe9598 | 2018-08-17 18:43:31 +0100 | [diff] [blame] | 333 | @Override |
| 334 | public void unmarkTileAsAutoAdded(String spec) { |
Jason Monk | a26641f | 2018-12-27 14:20:53 -0500 | [diff] [blame] | 335 | if (mAutoTiles != null) mAutoTiles.unmarkTileAsAutoAdded(spec); |
arangelov | 5fe9598 | 2018-08-17 18:43:31 +0100 | [diff] [blame] | 336 | } |
| 337 | |
Jason Monk | c3f42c1 | 2016-02-05 12:33:13 -0500 | [diff] [blame] | 338 | public void addTile(String spec) { |
Fabian Kozynski | 4ba3f9a | 2020-02-21 11:02:42 -0500 | [diff] [blame] | 339 | changeTileSpecs(tileSpecs-> !tileSpecs.contains(spec) && tileSpecs.add(spec)); |
Amin Shaikh | a062beb | 2018-07-25 11:21:54 -0400 | [diff] [blame] | 340 | } |
| 341 | |
Fabian Kozynski | 1fcc8c4 | 2020-05-04 13:31:47 -0400 | [diff] [blame] | 342 | private void saveTilesToSettings(List<String> tileSpecs) { |
| 343 | Settings.Secure.putStringForUser(mContext.getContentResolver(), TILES_SETTING, |
| 344 | TextUtils.join(",", tileSpecs), null /* tag */, |
| 345 | false /* default */, mCurrentUser, true /* overrideable by restore */); |
| 346 | } |
| 347 | |
Amin Shaikh | a062beb | 2018-07-25 11:21:54 -0400 | [diff] [blame] | 348 | private void changeTileSpecs(Predicate<List<String>> changeFunction) { |
Ricky Wai | b96c1eac | 2016-06-02 11:42:16 +0100 | [diff] [blame] | 349 | final String setting = Settings.Secure.getStringForUser(mContext.getContentResolver(), |
Fabian Kozynski | 1fcc8c4 | 2020-05-04 13:31:47 -0400 | [diff] [blame] | 350 | TILES_SETTING, mCurrentUser); |
Ricky Wai | b96c1eac | 2016-06-02 11:42:16 +0100 | [diff] [blame] | 351 | final List<String> tileSpecs = loadTileSpecs(mContext, setting); |
Amin Shaikh | a062beb | 2018-07-25 11:21:54 -0400 | [diff] [blame] | 352 | if (changeFunction.test(tileSpecs)) { |
Fabian Kozynski | 1fcc8c4 | 2020-05-04 13:31:47 -0400 | [diff] [blame] | 353 | saveTilesToSettings(tileSpecs); |
Amin Shaikh | a062beb | 2018-07-25 11:21:54 -0400 | [diff] [blame] | 354 | } |
Jason Monk | c3f42c1 | 2016-02-05 12:33:13 -0500 | [diff] [blame] | 355 | } |
| 356 | |
Jason Monk | 7e53f20 | 2016-01-28 10:40:20 -0500 | [diff] [blame] | 357 | public void addTile(ComponentName tile) { |
Fabian Kozynski | 4ba3f9a | 2020-02-21 11:02:42 -0500 | [diff] [blame] | 358 | String spec = CustomTile.toSpec(tile); |
| 359 | if (!mTileSpecs.contains(spec)) { |
| 360 | List<String> newSpecs = new ArrayList<>(mTileSpecs); |
| 361 | newSpecs.add(0, spec); |
| 362 | changeTiles(mTileSpecs, newSpecs); |
| 363 | } |
Jason Monk | 7e53f20 | 2016-01-28 10:40:20 -0500 | [diff] [blame] | 364 | } |
| 365 | |
| 366 | public void removeTile(ComponentName tile) { |
| 367 | List<String> newSpecs = new ArrayList<>(mTileSpecs); |
| 368 | newSpecs.remove(CustomTile.toSpec(tile)); |
| 369 | changeTiles(mTileSpecs, newSpecs); |
| 370 | } |
| 371 | |
| 372 | public void changeTiles(List<String> previousTiles, List<String> newTiles) { |
Fabian Kozynski | 9e40da9 | 2020-02-25 13:00:04 -0500 | [diff] [blame] | 373 | final List<String> copy = new ArrayList<>(previousTiles); |
| 374 | final int NP = copy.size(); |
Jason Monk | 7e53f20 | 2016-01-28 10:40:20 -0500 | [diff] [blame] | 375 | for (int i = 0; i < NP; i++) { |
Fabian Kozynski | 9e40da9 | 2020-02-25 13:00:04 -0500 | [diff] [blame] | 376 | String tileSpec = copy.get(i); |
Jason Monk | 7e53f20 | 2016-01-28 10:40:20 -0500 | [diff] [blame] | 377 | if (!tileSpec.startsWith(CustomTile.PREFIX)) continue; |
| 378 | if (!newTiles.contains(tileSpec)) { |
Jason Monk | a3453b8b | 2016-06-17 12:42:59 -0400 | [diff] [blame] | 379 | ComponentName component = CustomTile.getComponentFromSpec(tileSpec); |
| 380 | Intent intent = new Intent().setComponent(component); |
Jason Monk | 7e53f20 | 2016-01-28 10:40:20 -0500 | [diff] [blame] | 381 | TileLifecycleManager lifecycleManager = new TileLifecycleManager(new Handler(), |
Jason Monk | ee68fd8 | 2016-06-23 13:12:23 -0400 | [diff] [blame] | 382 | mContext, mServices, new Tile(), intent, |
Fabian Kozynski | 1fcc8c4 | 2020-05-04 13:31:47 -0400 | [diff] [blame] | 383 | new UserHandle(mCurrentUser), |
Fabian Kozynski | 5ca7a51 | 2019-10-16 19:56:11 +0000 | [diff] [blame] | 384 | mBroadcastDispatcher); |
Jason Monk | 7e53f20 | 2016-01-28 10:40:20 -0500 | [diff] [blame] | 385 | lifecycleManager.onStopListening(); |
| 386 | lifecycleManager.onTileRemoved(); |
Jason Monk | baade75 | 2016-08-25 15:57:14 -0400 | [diff] [blame] | 387 | TileLifecycleManager.setTileAdded(mContext, component, false); |
Jason Monk | 7e53f20 | 2016-01-28 10:40:20 -0500 | [diff] [blame] | 388 | lifecycleManager.flushMessagesAndUnbind(); |
| 389 | } |
| 390 | } |
| 391 | if (DEBUG) Log.d(TAG, "saveCurrentTiles " + newTiles); |
Fabian Kozynski | 1fcc8c4 | 2020-05-04 13:31:47 -0400 | [diff] [blame] | 392 | saveTilesToSettings(newTiles); |
Jason Monk | 7e53f20 | 2016-01-28 10:40:20 -0500 | [diff] [blame] | 393 | } |
| 394 | |
Jason Monk | 702e2eb | 2017-03-03 16:53:44 -0500 | [diff] [blame] | 395 | public QSTile createTile(String tileSpec) { |
| 396 | for (int i = 0; i < mQsFactories.size(); i++) { |
| 397 | QSTile t = mQsFactories.get(i).createTile(tileSpec); |
| 398 | if (t != null) { |
| 399 | return t; |
| 400 | } |
Jason Monk | 2b37190f | 2016-01-06 10:19:04 -0500 | [diff] [blame] | 401 | } |
Jason Monk | 702e2eb | 2017-03-03 16:53:44 -0500 | [diff] [blame] | 402 | return null; |
| 403 | } |
| 404 | |
| 405 | public QSTileView createTileView(QSTile tile, boolean collapsedView) { |
| 406 | for (int i = 0; i < mQsFactories.size(); i++) { |
| 407 | QSTileView view = mQsFactories.get(i).createTileView(tile, collapsedView); |
| 408 | if (view != null) { |
| 409 | return view; |
| 410 | } |
| 411 | } |
| 412 | throw new RuntimeException("Default factory didn't create view for " + tile.getTileSpec()); |
John Spurlock | bceed06 | 2014-08-10 18:04:16 -0400 | [diff] [blame] | 413 | } |
| 414 | |
Amin Shaikh | a062beb | 2018-07-25 11:21:54 -0400 | [diff] [blame] | 415 | protected static List<String> loadTileSpecs(Context context, String tileList) { |
Jason Monk | bbadff8 | 2015-11-06 15:47:26 -0500 | [diff] [blame] | 416 | final Resources res = context.getResources(); |
Fabian Kozynski | 1c0e33b | 2020-01-10 17:02:30 -0500 | [diff] [blame] | 417 | |
Fabian Kozynski | 7f370dd | 2018-12-05 10:41:07 -0500 | [diff] [blame] | 418 | if (TextUtils.isEmpty(tileList)) { |
John Spurlock | bceed06 | 2014-08-10 18:04:16 -0400 | [diff] [blame] | 419 | tileList = res.getString(R.string.quick_settings_tiles); |
| 420 | if (DEBUG) Log.d(TAG, "Loaded tile specs from config: " + tileList); |
| 421 | } else { |
| 422 | if (DEBUG) Log.d(TAG, "Loaded tile specs from setting: " + tileList); |
| 423 | } |
| 424 | final ArrayList<String> tiles = new ArrayList<String>(); |
| 425 | boolean addedDefault = false; |
Fabian Kozynski | 4ba3f9a | 2020-02-21 11:02:42 -0500 | [diff] [blame] | 426 | Set<String> addedSpecs = new ArraySet<>(); |
John Spurlock | bceed06 | 2014-08-10 18:04:16 -0400 | [diff] [blame] | 427 | for (String tile : tileList.split(",")) { |
| 428 | tile = tile.trim(); |
| 429 | if (tile.isEmpty()) continue; |
| 430 | if (tile.equals("default")) { |
| 431 | if (!addedDefault) { |
Fabian Kozynski | 4ba3f9a | 2020-02-21 11:02:42 -0500 | [diff] [blame] | 432 | List<String> defaultSpecs = getDefaultSpecs(context); |
| 433 | for (String spec : defaultSpecs) { |
| 434 | if (!addedSpecs.contains(spec)) { |
| 435 | tiles.add(spec); |
| 436 | addedSpecs.add(spec); |
| 437 | } |
| 438 | } |
John Spurlock | bceed06 | 2014-08-10 18:04:16 -0400 | [diff] [blame] | 439 | addedDefault = true; |
| 440 | } |
| 441 | } else { |
Fabian Kozynski | 4ba3f9a | 2020-02-21 11:02:42 -0500 | [diff] [blame] | 442 | if (!addedSpecs.contains(tile)) { |
| 443 | tiles.add(tile); |
| 444 | addedSpecs.add(tile); |
| 445 | } |
John Spurlock | bceed06 | 2014-08-10 18:04:16 -0400 | [diff] [blame] | 446 | } |
| 447 | } |
| 448 | return tiles; |
| 449 | } |
Fabian Kozynski | 00d494d | 2019-04-04 09:53:50 -0400 | [diff] [blame] | 450 | |
Fabian Kozynski | 1c0e33b | 2020-01-10 17:02:30 -0500 | [diff] [blame] | 451 | /** |
| 452 | * Returns the default QS tiles for the context. |
| 453 | * @param context the context to obtain the resources from |
| 454 | * @return a list of specs of the default tiles |
| 455 | */ |
| 456 | public static List<String> getDefaultSpecs(Context context) { |
| 457 | final ArrayList<String> tiles = new ArrayList<String>(); |
| 458 | |
| 459 | final Resources res = context.getResources(); |
| 460 | final String defaultTileList = res.getString(R.string.quick_settings_tiles_default); |
Fabian Kozynski | 1c0e33b | 2020-01-10 17:02:30 -0500 | [diff] [blame] | 461 | |
| 462 | tiles.addAll(Arrays.asList(defaultTileList.split(","))); |
Fabian Kozynski | 1c0e33b | 2020-01-10 17:02:30 -0500 | [diff] [blame] | 463 | if (Build.IS_DEBUGGABLE |
| 464 | && GarbageMonitor.MemoryTile.ADD_TO_DEFAULT_ON_DEBUGGABLE_BUILDS) { |
| 465 | tiles.add(GarbageMonitor.MemoryTile.TILE_SPEC); |
| 466 | } |
| 467 | return tiles; |
| 468 | } |
| 469 | |
Fabian Kozynski | 00d494d | 2019-04-04 09:53:50 -0400 | [diff] [blame] | 470 | @Override |
| 471 | public void dump(FileDescriptor fd, PrintWriter pw, String[] args) { |
| 472 | pw.println("QSTileHost:"); |
| 473 | mTiles.values().stream().filter(obj -> obj instanceof Dumpable) |
| 474 | .forEach(o -> ((Dumpable) o).dump(fd, pw, args)); |
| 475 | } |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 476 | } |