Jason Monk | d5a204f | 2015-12-21 08:50:01 -0500 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2015 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License |
| 15 | */ |
| 16 | package com.android.systemui.qs.external; |
| 17 | |
| 18 | import android.app.ActivityManager; |
Jason Monk | 07f55a2 | 2016-04-11 15:30:38 -0400 | [diff] [blame] | 19 | import android.content.BroadcastReceiver; |
Jason Monk | d5a204f | 2015-12-21 08:50:01 -0500 | [diff] [blame] | 20 | import android.content.ComponentName; |
Jason Monk | 07f55a2 | 2016-04-11 15:30:38 -0400 | [diff] [blame] | 21 | import android.content.Context; |
Jason Monk | d5a204f | 2015-12-21 08:50:01 -0500 | [diff] [blame] | 22 | import android.content.Intent; |
Jason Monk | 07f55a2 | 2016-04-11 15:30:38 -0400 | [diff] [blame] | 23 | import android.content.IntentFilter; |
Will Harmon | 604c2f9 | 2016-06-06 12:54:59 -0700 | [diff] [blame] | 24 | import android.content.pm.PackageManager; |
| 25 | import android.content.pm.ResolveInfo; |
Jason Monk | 07f55a2 | 2016-04-11 15:30:38 -0400 | [diff] [blame] | 26 | import android.net.Uri; |
Jason Monk | d5a204f | 2015-12-21 08:50:01 -0500 | [diff] [blame] | 27 | import android.os.Handler; |
Jason Monk | ee68fd8 | 2016-06-23 13:12:23 -0400 | [diff] [blame] | 28 | import android.os.IBinder; |
Jason Monk | d5a204f | 2015-12-21 08:50:01 -0500 | [diff] [blame] | 29 | import android.os.UserHandle; |
| 30 | import android.service.quicksettings.IQSTileService; |
Jason Monk | a3453b8b | 2016-06-17 12:42:59 -0400 | [diff] [blame] | 31 | import android.service.quicksettings.Tile; |
Will Harmon | 604c2f9 | 2016-06-06 12:54:59 -0700 | [diff] [blame] | 32 | import android.service.quicksettings.TileService; |
Jason Monk | d5a204f | 2015-12-21 08:50:01 -0500 | [diff] [blame] | 33 | import android.util.Log; |
Will Harmon | 604c2f9 | 2016-06-06 12:54:59 -0700 | [diff] [blame] | 34 | |
Gus Prevas | ab33679 | 2018-11-14 13:52:20 -0500 | [diff] [blame] | 35 | import androidx.annotation.VisibleForTesting; |
| 36 | |
Jason Monk | 624cbe2 | 2016-05-02 10:42:17 -0400 | [diff] [blame] | 37 | import com.android.systemui.qs.external.TileLifecycleManager.TileChangeListener; |
Will Harmon | 604c2f9 | 2016-06-06 12:54:59 -0700 | [diff] [blame] | 38 | |
| 39 | import java.util.List; |
Narayan Kamath | 607223f | 2018-02-19 14:09:02 +0000 | [diff] [blame] | 40 | import java.util.Objects; |
Jason Monk | d5a204f | 2015-12-21 08:50:01 -0500 | [diff] [blame] | 41 | |
| 42 | /** |
| 43 | * Manages the priority which lets {@link TileServices} make decisions about which tiles |
| 44 | * to bind. Also holds on to and manages the {@link TileLifecycleManager}, informing it |
| 45 | * of when it is allowed to bind based on decisions frome the {@link TileServices}. |
| 46 | */ |
| 47 | public class TileServiceManager { |
| 48 | |
| 49 | private static final long MIN_BIND_TIME = 5000; |
| 50 | private static final long UNBIND_DELAY = 30000; |
| 51 | |
| 52 | public static final boolean DEBUG = true; |
| 53 | |
| 54 | private static final String TAG = "TileServiceManager"; |
| 55 | |
Jason Monk | fe8f682 | 2015-12-21 15:12:01 -0500 | [diff] [blame] | 56 | @VisibleForTesting |
| 57 | static final String PREFS_FILE = "CustomTileModes"; |
| 58 | |
Jason Monk | d5a204f | 2015-12-21 08:50:01 -0500 | [diff] [blame] | 59 | private final TileServices mServices; |
| 60 | private final TileLifecycleManager mStateManager; |
| 61 | private final Handler mHandler; |
| 62 | private boolean mBindRequested; |
| 63 | private boolean mBindAllowed; |
| 64 | private boolean mBound; |
| 65 | private int mPriority; |
| 66 | private boolean mJustBound; |
| 67 | private long mLastUpdate; |
Jason Monk | 34a5cef | 2016-01-29 11:28:44 -0500 | [diff] [blame] | 68 | private boolean mShowingDialog; |
Jason Monk | 1c2fea8 | 2016-03-11 11:33:36 -0500 | [diff] [blame] | 69 | // Whether we have a pending bind going out to the service without a response yet. |
| 70 | // This defaults to true to ensure tiles start out unavailable. |
| 71 | private boolean mPendingBind = true; |
Fabian Kozynski | e6395dd | 2019-05-02 13:16:02 -0400 | [diff] [blame] | 72 | private boolean mStarted = false; |
Jason Monk | d5a204f | 2015-12-21 08:50:01 -0500 | [diff] [blame] | 73 | |
Jason Monk | a3453b8b | 2016-06-17 12:42:59 -0400 | [diff] [blame] | 74 | TileServiceManager(TileServices tileServices, Handler handler, ComponentName component, |
| 75 | Tile tile) { |
Jason Monk | d5a204f | 2015-12-21 08:50:01 -0500 | [diff] [blame] | 76 | this(tileServices, handler, new TileLifecycleManager(handler, |
Jason Monk | a3453b8b | 2016-06-17 12:42:59 -0400 | [diff] [blame] | 77 | tileServices.getContext(), tileServices, tile, new Intent().setComponent(component), |
Jason Monk | d5a204f | 2015-12-21 08:50:01 -0500 | [diff] [blame] | 78 | new UserHandle(ActivityManager.getCurrentUser()))); |
| 79 | } |
| 80 | |
| 81 | @VisibleForTesting |
| 82 | TileServiceManager(TileServices tileServices, Handler handler, |
| 83 | TileLifecycleManager tileLifecycleManager) { |
| 84 | mServices = tileServices; |
| 85 | mHandler = handler; |
| 86 | mStateManager = tileLifecycleManager; |
Jason Monk | 07f55a2 | 2016-04-11 15:30:38 -0400 | [diff] [blame] | 87 | |
| 88 | IntentFilter filter = new IntentFilter(); |
| 89 | filter.addAction(Intent.ACTION_PACKAGE_REMOVED); |
| 90 | filter.addDataScheme("package"); |
Jason Monk | baade75 | 2016-08-25 15:57:14 -0400 | [diff] [blame] | 91 | Context context = mServices.getContext(); |
| 92 | context.registerReceiverAsUser(mUninstallReceiver, |
Jason Monk | 07f55a2 | 2016-04-11 15:30:38 -0400 | [diff] [blame] | 93 | new UserHandle(ActivityManager.getCurrentUser()), filter, null, mHandler); |
Fabian Kozynski | e6395dd | 2019-05-02 13:16:02 -0400 | [diff] [blame] | 94 | } |
| 95 | |
| 96 | boolean isLifecycleStarted() { |
| 97 | return mStarted; |
| 98 | } |
| 99 | |
| 100 | /** |
| 101 | * Starts the TileLifecycleManager by adding the corresponding component as a Tile and |
| 102 | * binding to it if needed. |
| 103 | * |
| 104 | * This method should be called after constructing a TileServiceManager to guarantee that the |
| 105 | * TileLifecycleManager has added the tile and bound to it at least once. |
| 106 | */ |
| 107 | void startLifecycleManagerAndAddTile() { |
| 108 | mStarted = true; |
| 109 | ComponentName component = mStateManager.getComponent(); |
| 110 | Context context = mServices.getContext(); |
Jason Monk | baade75 | 2016-08-25 15:57:14 -0400 | [diff] [blame] | 111 | if (!TileLifecycleManager.isTileAdded(context, component)) { |
| 112 | TileLifecycleManager.setTileAdded(context, component, true); |
| 113 | mStateManager.onTileAdded(); |
Jason Monk | 34f6cbc | 2016-08-30 16:33:48 -0400 | [diff] [blame] | 114 | mStateManager.flushMessagesAndUnbind(); |
Jason Monk | baade75 | 2016-08-25 15:57:14 -0400 | [diff] [blame] | 115 | } |
Jason Monk | fe8f682 | 2015-12-21 15:12:01 -0500 | [diff] [blame] | 116 | } |
| 117 | |
Jason Monk | 624cbe2 | 2016-05-02 10:42:17 -0400 | [diff] [blame] | 118 | public void setTileChangeListener(TileChangeListener changeListener) { |
| 119 | mStateManager.setTileChangeListener(changeListener); |
| 120 | } |
| 121 | |
Jason Monk | 97d2272 | 2016-04-07 11:41:47 -0400 | [diff] [blame] | 122 | public boolean isActiveTile() { |
| 123 | return mStateManager.isActiveTile(); |
Jason Monk | d5a204f | 2015-12-21 08:50:01 -0500 | [diff] [blame] | 124 | } |
| 125 | |
Fabian Kozynski | 05843f0 | 2019-06-28 13:19:57 -0400 | [diff] [blame] | 126 | public boolean isBooleanTile() { |
| 127 | return mStateManager.isBooleanTile(); |
| 128 | } |
| 129 | |
Jason Monk | 34a5cef | 2016-01-29 11:28:44 -0500 | [diff] [blame] | 130 | public void setShowingDialog(boolean dialog) { |
| 131 | mShowingDialog = dialog; |
| 132 | } |
| 133 | |
Jason Monk | d5a204f | 2015-12-21 08:50:01 -0500 | [diff] [blame] | 134 | public IQSTileService getTileService() { |
| 135 | return mStateManager; |
| 136 | } |
| 137 | |
Jason Monk | ee68fd8 | 2016-06-23 13:12:23 -0400 | [diff] [blame] | 138 | public IBinder getToken() { |
| 139 | return mStateManager.getToken(); |
| 140 | } |
| 141 | |
Jason Monk | d5a204f | 2015-12-21 08:50:01 -0500 | [diff] [blame] | 142 | public void setBindRequested(boolean bindRequested) { |
| 143 | if (mBindRequested == bindRequested) return; |
| 144 | mBindRequested = bindRequested; |
| 145 | if (mBindAllowed && mBindRequested && !mBound) { |
Jason Monk | fe8f682 | 2015-12-21 15:12:01 -0500 | [diff] [blame] | 146 | mHandler.removeCallbacks(mUnbind); |
Jason Monk | d5a204f | 2015-12-21 08:50:01 -0500 | [diff] [blame] | 147 | bindService(); |
| 148 | } else { |
| 149 | mServices.recalculateBindAllowance(); |
| 150 | } |
| 151 | if (mBound && !mBindRequested) { |
Jason Monk | fe8f682 | 2015-12-21 15:12:01 -0500 | [diff] [blame] | 152 | mHandler.postDelayed(mUnbind, UNBIND_DELAY); |
Jason Monk | d5a204f | 2015-12-21 08:50:01 -0500 | [diff] [blame] | 153 | } |
| 154 | } |
| 155 | |
| 156 | public void setLastUpdate(long lastUpdate) { |
| 157 | mLastUpdate = lastUpdate; |
Jason Monk | 97d2272 | 2016-04-07 11:41:47 -0400 | [diff] [blame] | 158 | if (mBound && isActiveTile()) { |
Jason Monk | fe8f682 | 2015-12-21 15:12:01 -0500 | [diff] [blame] | 159 | mStateManager.onStopListening(); |
| 160 | setBindRequested(false); |
| 161 | } |
Jason Monk | d5a204f | 2015-12-21 08:50:01 -0500 | [diff] [blame] | 162 | mServices.recalculateBindAllowance(); |
| 163 | } |
| 164 | |
| 165 | public void handleDestroy() { |
Jason Monk | 8852905 | 2016-11-04 13:29:58 -0400 | [diff] [blame] | 166 | setBindAllowed(false); |
Jason Monk | 07f55a2 | 2016-04-11 15:30:38 -0400 | [diff] [blame] | 167 | mServices.getContext().unregisterReceiver(mUninstallReceiver); |
Jason Monk | d5a204f | 2015-12-21 08:50:01 -0500 | [diff] [blame] | 168 | mStateManager.handleDestroy(); |
| 169 | } |
| 170 | |
| 171 | public void setBindAllowed(boolean allowed) { |
| 172 | if (mBindAllowed == allowed) return; |
| 173 | mBindAllowed = allowed; |
| 174 | if (!mBindAllowed && mBound) { |
| 175 | unbindService(); |
| 176 | } else if (mBindAllowed && mBindRequested && !mBound) { |
| 177 | bindService(); |
| 178 | } |
| 179 | } |
| 180 | |
Jason Monk | 1c2fea8 | 2016-03-11 11:33:36 -0500 | [diff] [blame] | 181 | public boolean hasPendingBind() { |
| 182 | return mPendingBind; |
| 183 | } |
| 184 | |
| 185 | public void clearPendingBind() { |
| 186 | mPendingBind = false; |
| 187 | } |
| 188 | |
Jason Monk | d5a204f | 2015-12-21 08:50:01 -0500 | [diff] [blame] | 189 | private void bindService() { |
| 190 | if (mBound) { |
| 191 | Log.e(TAG, "Service already bound"); |
| 192 | return; |
| 193 | } |
Jason Monk | 1c2fea8 | 2016-03-11 11:33:36 -0500 | [diff] [blame] | 194 | mPendingBind = true; |
Jason Monk | d5a204f | 2015-12-21 08:50:01 -0500 | [diff] [blame] | 195 | mBound = true; |
| 196 | mJustBound = true; |
| 197 | mHandler.postDelayed(mJustBoundOver, MIN_BIND_TIME); |
| 198 | mStateManager.setBindService(true); |
| 199 | } |
| 200 | |
| 201 | private void unbindService() { |
| 202 | if (!mBound) { |
| 203 | Log.e(TAG, "Service not bound"); |
| 204 | return; |
| 205 | } |
| 206 | mBound = false; |
| 207 | mJustBound = false; |
| 208 | mStateManager.setBindService(false); |
| 209 | } |
| 210 | |
| 211 | public void calculateBindPriority(long currentTime) { |
| 212 | if (mStateManager.hasPendingClick()) { |
| 213 | // Pending click is the most important thing, need to put this service at the top of |
| 214 | // the list to be bound. |
| 215 | mPriority = Integer.MAX_VALUE; |
Jason Monk | 34a5cef | 2016-01-29 11:28:44 -0500 | [diff] [blame] | 216 | } else if (mShowingDialog) { |
| 217 | // Hang on to services that are showing dialogs so they don't die. |
| 218 | mPriority = Integer.MAX_VALUE - 1; |
Jason Monk | d5a204f | 2015-12-21 08:50:01 -0500 | [diff] [blame] | 219 | } else if (mJustBound) { |
| 220 | // If we just bound, lets not thrash on binding/unbinding too much, this is second most |
| 221 | // important. |
Jason Monk | 34a5cef | 2016-01-29 11:28:44 -0500 | [diff] [blame] | 222 | mPriority = Integer.MAX_VALUE - 2; |
Jason Monk | d5a204f | 2015-12-21 08:50:01 -0500 | [diff] [blame] | 223 | } else if (!mBindRequested) { |
| 224 | // Don't care about binding right now, put us last. |
| 225 | mPriority = Integer.MIN_VALUE; |
| 226 | } else { |
| 227 | // Order based on whether this was just updated. |
| 228 | long timeSinceUpdate = currentTime - mLastUpdate; |
| 229 | // Fit compare into integer space for simplicity. Make sure to leave MAX_VALUE and |
| 230 | // MAX_VALUE - 1 for the more important states above. |
Jason Monk | 34a5cef | 2016-01-29 11:28:44 -0500 | [diff] [blame] | 231 | if (timeSinceUpdate > Integer.MAX_VALUE - 3) { |
| 232 | mPriority = Integer.MAX_VALUE - 3; |
Jason Monk | d5a204f | 2015-12-21 08:50:01 -0500 | [diff] [blame] | 233 | } else { |
| 234 | mPriority = (int) timeSinceUpdate; |
| 235 | } |
| 236 | } |
| 237 | } |
| 238 | |
| 239 | public int getBindPriority() { |
| 240 | return mPriority; |
| 241 | } |
| 242 | |
Jason Monk | fe8f682 | 2015-12-21 15:12:01 -0500 | [diff] [blame] | 243 | private final Runnable mUnbind = new Runnable() { |
| 244 | @Override |
| 245 | public void run() { |
| 246 | if (mBound && !mBindRequested) { |
| 247 | unbindService(); |
| 248 | } |
| 249 | } |
| 250 | }; |
| 251 | |
Jason Monk | d5a204f | 2015-12-21 08:50:01 -0500 | [diff] [blame] | 252 | @VisibleForTesting |
| 253 | final Runnable mJustBoundOver = new Runnable() { |
| 254 | @Override |
| 255 | public void run() { |
| 256 | mJustBound = false; |
| 257 | mServices.recalculateBindAllowance(); |
| 258 | } |
| 259 | }; |
Jason Monk | 07f55a2 | 2016-04-11 15:30:38 -0400 | [diff] [blame] | 260 | |
| 261 | private final BroadcastReceiver mUninstallReceiver = new BroadcastReceiver() { |
| 262 | @Override |
| 263 | public void onReceive(Context context, Intent intent) { |
| 264 | if (!Intent.ACTION_PACKAGE_REMOVED.equals(intent.getAction())) { |
| 265 | return; |
| 266 | } |
Will Harmon | 604c2f9 | 2016-06-06 12:54:59 -0700 | [diff] [blame] | 267 | |
Jason Monk | 07f55a2 | 2016-04-11 15:30:38 -0400 | [diff] [blame] | 268 | Uri data = intent.getData(); |
| 269 | String pkgName = data.getEncodedSchemeSpecificPart(); |
| 270 | final ComponentName component = mStateManager.getComponent(); |
Narayan Kamath | 607223f | 2018-02-19 14:09:02 +0000 | [diff] [blame] | 271 | if (!Objects.equals(pkgName, component.getPackageName())) { |
Jason Monk | 07f55a2 | 2016-04-11 15:30:38 -0400 | [diff] [blame] | 272 | return; |
| 273 | } |
Will Harmon | 604c2f9 | 2016-06-06 12:54:59 -0700 | [diff] [blame] | 274 | |
| 275 | // If the package is being updated, verify the component still exists. |
| 276 | if (intent.getBooleanExtra(Intent.EXTRA_REPLACING, false)) { |
| 277 | Intent queryIntent = new Intent(TileService.ACTION_QS_TILE); |
| 278 | queryIntent.setPackage(pkgName); |
| 279 | PackageManager pm = context.getPackageManager(); |
| 280 | List<ResolveInfo> services = pm.queryIntentServicesAsUser( |
| 281 | queryIntent, 0, ActivityManager.getCurrentUser()); |
| 282 | for (ResolveInfo info : services) { |
Narayan Kamath | 607223f | 2018-02-19 14:09:02 +0000 | [diff] [blame] | 283 | if (Objects.equals(info.serviceInfo.packageName, component.getPackageName()) |
| 284 | && Objects.equals(info.serviceInfo.name, component.getClassName())) { |
Will Harmon | 604c2f9 | 2016-06-06 12:54:59 -0700 | [diff] [blame] | 285 | return; |
| 286 | } |
| 287 | } |
| 288 | } |
| 289 | |
Jason Monk | 07f55a2 | 2016-04-11 15:30:38 -0400 | [diff] [blame] | 290 | mServices.getHost().removeTile(component); |
| 291 | } |
| 292 | }; |
Jason Monk | d5a204f | 2015-12-21 08:50:01 -0500 | [diff] [blame] | 293 | } |