Jason Monk | 5db8a41 | 2015-10-21 15:16:23 -0700 | [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 |
Jason Monk | d5a204f | 2015-12-21 08:50:01 -0500 | [diff] [blame] | 14 | * limitations under the License |
Jason Monk | 5db8a41 | 2015-10-21 15:16:23 -0700 | [diff] [blame] | 15 | */ |
Jason Monk | d5a204f | 2015-12-21 08:50:01 -0500 | [diff] [blame] | 16 | package com.android.systemui.qs.external; |
Jason Monk | 5db8a41 | 2015-10-21 15:16:23 -0700 | [diff] [blame] | 17 | |
Gus Prevas | ab33679 | 2018-11-14 13:52:20 -0500 | [diff] [blame] | 18 | import static android.view.Display.DEFAULT_DISPLAY; |
| 19 | import static android.view.WindowManager.LayoutParams.TYPE_QS_DIALOG; |
| 20 | |
Jason Monk | 724214a | 2016-02-19 16:43:00 -0500 | [diff] [blame] | 21 | import android.app.ActivityManager; |
Jason Monk | 5db8a41 | 2015-10-21 15:16:23 -0700 | [diff] [blame] | 22 | import android.content.ComponentName; |
Fabian Kozynski | 4ba3f9a | 2020-02-21 11:02:42 -0500 | [diff] [blame] | 23 | import android.content.Context; |
Jason Monk | 76c67aa | 2016-02-19 14:49:42 -0500 | [diff] [blame] | 24 | import android.content.Intent; |
Jason Monk | 5db8a41 | 2015-10-21 15:16:23 -0700 | [diff] [blame] | 25 | import android.content.pm.PackageManager; |
Jason Monk | 724214a | 2016-02-19 16:43:00 -0500 | [diff] [blame] | 26 | import android.content.pm.ResolveInfo; |
Jason Monk | 5db8a41 | 2015-10-21 15:16:23 -0700 | [diff] [blame] | 27 | import android.content.pm.ServiceInfo; |
Jason Monk | 068cb8b | 2015-12-02 11:30:36 -0500 | [diff] [blame] | 28 | import android.graphics.drawable.Drawable; |
Jason Monk | 8c09ac7 | 2017-03-16 11:53:40 -0400 | [diff] [blame] | 29 | import android.metrics.LogMaker; |
Jason Monk | 76c67aa | 2016-02-19 14:49:42 -0500 | [diff] [blame] | 30 | import android.net.Uri; |
Jason Monk | 8f7f318 | 2015-11-18 16:35:14 -0500 | [diff] [blame] | 31 | import android.os.Binder; |
Jason Monk | bbadff8 | 2015-11-06 15:47:26 -0500 | [diff] [blame] | 32 | import android.os.IBinder; |
Jason Monk | 8f7f318 | 2015-11-18 16:35:14 -0500 | [diff] [blame] | 33 | import android.os.RemoteException; |
Jason Monk | 76c67aa | 2016-02-19 14:49:42 -0500 | [diff] [blame] | 34 | import android.provider.Settings; |
Jason Monk | bbadff8 | 2015-11-06 15:47:26 -0500 | [diff] [blame] | 35 | import android.service.quicksettings.IQSTileService; |
| 36 | import android.service.quicksettings.Tile; |
Jason Monk | fe8f682 | 2015-12-21 15:12:01 -0500 | [diff] [blame] | 37 | import android.service.quicksettings.TileService; |
Amin Shaikh | abcca63 | 2018-08-29 15:15:08 -0400 | [diff] [blame] | 38 | import android.text.TextUtils; |
Jason Monk | 1c6116c | 2017-09-06 17:33:01 -0400 | [diff] [blame] | 39 | import android.text.format.DateUtils; |
Jason Monk | bbadff8 | 2015-11-06 15:47:26 -0500 | [diff] [blame] | 40 | import android.util.Log; |
Jason Monk | 8f7f318 | 2015-11-18 16:35:14 -0500 | [diff] [blame] | 41 | import android.view.IWindowManager; |
Jason Monk | 8f7f318 | 2015-11-18 16:35:14 -0500 | [diff] [blame] | 42 | import android.view.WindowManagerGlobal; |
Fabian Kozynski | 05843f0 | 2019-06-28 13:19:57 -0400 | [diff] [blame] | 43 | import android.widget.Switch; |
Amin Shaikh | abcca63 | 2018-08-29 15:15:08 -0400 | [diff] [blame] | 44 | |
Tamas Berghammer | 383db5eb | 2016-06-22 15:21:38 +0100 | [diff] [blame] | 45 | import com.android.internal.logging.nano.MetricsProto.MetricsEvent; |
Jason Monk | 9c7844c | 2017-01-18 15:21:53 -0500 | [diff] [blame] | 46 | import com.android.systemui.Dependency; |
Jason Monk | ec34da8 | 2017-02-24 15:57:05 -0500 | [diff] [blame] | 47 | import com.android.systemui.plugins.ActivityStarter; |
Jason Monk | 702e2eb | 2017-03-03 16:53:44 -0500 | [diff] [blame] | 48 | import com.android.systemui.plugins.qs.QSTile.State; |
John Li | 4e3dddc | 2020-03-04 21:35:43 +0800 | [diff] [blame] | 49 | import com.android.systemui.qs.QSHost; |
Gus Prevas | ab33679 | 2018-11-14 13:52:20 -0500 | [diff] [blame] | 50 | import com.android.systemui.qs.external.TileLifecycleManager.TileChangeListener; |
| 51 | import com.android.systemui.qs.tileimpl.QSTileImpl; |
Jason Monk | 5db8a41 | 2015-10-21 15:16:23 -0700 | [diff] [blame] | 52 | |
Gus Prevas | ab33679 | 2018-11-14 13:52:20 -0500 | [diff] [blame] | 53 | import java.util.Objects; |
Jason Monk | 3250885 | 2017-01-18 09:17:13 -0500 | [diff] [blame] | 54 | |
Jason Monk | 702e2eb | 2017-03-03 16:53:44 -0500 | [diff] [blame] | 55 | public class CustomTile extends QSTileImpl<State> implements TileChangeListener { |
Jason Monk | 5db8a41 | 2015-10-21 15:16:23 -0700 | [diff] [blame] | 56 | public static final String PREFIX = "custom("; |
| 57 | |
Jason Monk | 1c6116c | 2017-09-06 17:33:01 -0400 | [diff] [blame] | 58 | private static final long CUSTOM_STALE_TIMEOUT = DateUtils.HOUR_IN_MILLIS; |
| 59 | |
Jason Monk | 8f7f318 | 2015-11-18 16:35:14 -0500 | [diff] [blame] | 60 | private static final boolean DEBUG = false; |
| 61 | |
Jason Monk | bbadff8 | 2015-11-06 15:47:26 -0500 | [diff] [blame] | 62 | // We don't want to thrash binding and unbinding if the user opens and closes the panel a lot. |
| 63 | // So instead we have a period of waiting. |
| 64 | private static final long UNBIND_DELAY = 30000; |
Jason Monk | 5db8a41 | 2015-10-21 15:16:23 -0700 | [diff] [blame] | 65 | |
Jason Monk | bbadff8 | 2015-11-06 15:47:26 -0500 | [diff] [blame] | 66 | private final ComponentName mComponent; |
| 67 | private final Tile mTile; |
Jason Monk | 8f7f318 | 2015-11-18 16:35:14 -0500 | [diff] [blame] | 68 | private final IWindowManager mWindowManager; |
| 69 | private final IBinder mToken = new Binder(); |
Jason Monk | d5a204f | 2015-12-21 08:50:01 -0500 | [diff] [blame] | 70 | private final IQSTileService mService; |
| 71 | private final TileServiceManager mServiceManager; |
Jason Monk | 1ffa11b | 2016-03-08 14:44:23 -0500 | [diff] [blame] | 72 | private final int mUser; |
Jason Monk | 624cbe2 | 2016-05-02 10:42:17 -0400 | [diff] [blame] | 73 | private android.graphics.drawable.Icon mDefaultIcon; |
Amin Shaikh | abcca63 | 2018-08-29 15:15:08 -0400 | [diff] [blame] | 74 | private CharSequence mDefaultLabel; |
Jason Monk | bbadff8 | 2015-11-06 15:47:26 -0500 | [diff] [blame] | 75 | |
Fabian Kozynski | 4ba3f9a | 2020-02-21 11:02:42 -0500 | [diff] [blame] | 76 | private final Context mUserContext; |
| 77 | |
Jason Monk | bbadff8 | 2015-11-06 15:47:26 -0500 | [diff] [blame] | 78 | private boolean mListening; |
Jason Monk | 8f7f318 | 2015-11-18 16:35:14 -0500 | [diff] [blame] | 79 | private boolean mIsTokenGranted; |
| 80 | private boolean mIsShowingDialog; |
quakentsai(58374) | 3217129 | 2021-05-27 11:48:54 +0800 | [diff] [blame^] | 81 | private boolean mEnable = false; |
Jason Monk | bbadff8 | 2015-11-06 15:47:26 -0500 | [diff] [blame] | 82 | |
John Li | 4e3dddc | 2020-03-04 21:35:43 +0800 | [diff] [blame] | 83 | private CustomTile(QSHost host, String action, Context userContext) { |
Jason Monk | 5db8a41 | 2015-10-21 15:16:23 -0700 | [diff] [blame] | 84 | super(host); |
Jason Monk | 8f7f318 | 2015-11-18 16:35:14 -0500 | [diff] [blame] | 85 | mWindowManager = WindowManagerGlobal.getWindowManagerService(); |
Jason Monk | 5db8a41 | 2015-10-21 15:16:23 -0700 | [diff] [blame] | 86 | mComponent = ComponentName.unflattenFromString(action); |
Jason Monk | ee68fd8 | 2016-06-23 13:12:23 -0400 | [diff] [blame] | 87 | mTile = new Tile(); |
Fabian Kozynski | 4ba3f9a | 2020-02-21 11:02:42 -0500 | [diff] [blame] | 88 | mUserContext = userContext; |
| 89 | mUser = mUserContext.getUserId(); |
Amin Shaikh | abcca63 | 2018-08-29 15:15:08 -0400 | [diff] [blame] | 90 | updateDefaultTileAndIcon(); |
Jason Monk | d5a204f | 2015-12-21 08:50:01 -0500 | [diff] [blame] | 91 | mServiceManager = host.getTileServices().getTileWrapper(this); |
Fabian Kozynski | 489ad0d | 2020-02-07 10:30:45 -0500 | [diff] [blame] | 92 | if (mServiceManager.isToggleableTile()) { |
Fabian Kozynski | 05843f0 | 2019-06-28 13:19:57 -0400 | [diff] [blame] | 93 | // Replace states with BooleanState |
| 94 | resetStates(); |
| 95 | } |
| 96 | |
Jason Monk | d5a204f | 2015-12-21 08:50:01 -0500 | [diff] [blame] | 97 | mService = mServiceManager.getTileService(); |
Jason Monk | 624cbe2 | 2016-05-02 10:42:17 -0400 | [diff] [blame] | 98 | mServiceManager.setTileChangeListener(this); |
Jason Monk | 5db8a41 | 2015-10-21 15:16:23 -0700 | [diff] [blame] | 99 | } |
| 100 | |
Jason Monk | 1c6116c | 2017-09-06 17:33:01 -0400 | [diff] [blame] | 101 | @Override |
| 102 | protected long getStaleTimeout() { |
| 103 | return CUSTOM_STALE_TIMEOUT + DateUtils.MINUTE_IN_MILLIS * mHost.indexOf(getTileSpec()); |
| 104 | } |
| 105 | |
Amin Shaikh | abcca63 | 2018-08-29 15:15:08 -0400 | [diff] [blame] | 106 | private void updateDefaultTileAndIcon() { |
Jason Monk | 624cbe2 | 2016-05-02 10:42:17 -0400 | [diff] [blame] | 107 | try { |
Fabian Kozynski | 4ba3f9a | 2020-02-21 11:02:42 -0500 | [diff] [blame] | 108 | PackageManager pm = mUserContext.getPackageManager(); |
Jeff Sharkey | dd9bda8 | 2017-02-23 17:38:31 -0700 | [diff] [blame] | 109 | int flags = PackageManager.MATCH_DIRECT_BOOT_UNAWARE | PackageManager.MATCH_DIRECT_BOOT_AWARE; |
quakentsai(58374) | 3217129 | 2021-05-27 11:48:54 +0800 | [diff] [blame^] | 110 | |
| 111 | mEnable = pm.getServiceInfo(mComponent, flags) != null; |
| 112 | |
Will Harmon | 294af23 | 2016-06-24 17:02:34 -0700 | [diff] [blame] | 113 | if (isSystemApp(pm)) { |
| 114 | flags |= PackageManager.MATCH_DISABLED_COMPONENTS; |
| 115 | } |
Amin Shaikh | abcca63 | 2018-08-29 15:15:08 -0400 | [diff] [blame] | 116 | |
Will Harmon | 294af23 | 2016-06-24 17:02:34 -0700 | [diff] [blame] | 117 | ServiceInfo info = pm.getServiceInfo(mComponent, flags); |
Jason Monk | a5f6ed3 | 2016-06-22 09:58:46 -0400 | [diff] [blame] | 118 | int icon = info.icon != 0 ? info.icon |
| 119 | : info.applicationInfo.icon; |
Jason Monk | 624cbe2 | 2016-05-02 10:42:17 -0400 | [diff] [blame] | 120 | // Update the icon if its not set or is the default icon. |
| 121 | boolean updateIcon = mTile.getIcon() == null |
| 122 | || iconEquals(mTile.getIcon(), mDefaultIcon); |
Jason Monk | a5f6ed3 | 2016-06-22 09:58:46 -0400 | [diff] [blame] | 123 | mDefaultIcon = icon != 0 ? android.graphics.drawable.Icon |
| 124 | .createWithResource(mComponent.getPackageName(), icon) : null; |
Jason Monk | 624cbe2 | 2016-05-02 10:42:17 -0400 | [diff] [blame] | 125 | if (updateIcon) { |
| 126 | mTile.setIcon(mDefaultIcon); |
| 127 | } |
Amin Shaikh | abcca63 | 2018-08-29 15:15:08 -0400 | [diff] [blame] | 128 | // Update the label if there is no label or it is the default label. |
| 129 | boolean updateLabel = mTile.getLabel() == null |
| 130 | || TextUtils.equals(mTile.getLabel(), mDefaultLabel); |
| 131 | mDefaultLabel = info.loadLabel(pm); |
| 132 | if (updateLabel) { |
| 133 | mTile.setLabel(mDefaultLabel); |
Jason Monk | 624cbe2 | 2016-05-02 10:42:17 -0400 | [diff] [blame] | 134 | } |
Amin Shaikh | abcca63 | 2018-08-29 15:15:08 -0400 | [diff] [blame] | 135 | } catch (PackageManager.NameNotFoundException e) { |
Jason Monk | 624cbe2 | 2016-05-02 10:42:17 -0400 | [diff] [blame] | 136 | mDefaultIcon = null; |
Amin Shaikh | abcca63 | 2018-08-29 15:15:08 -0400 | [diff] [blame] | 137 | mDefaultLabel = null; |
Jason Monk | 624cbe2 | 2016-05-02 10:42:17 -0400 | [diff] [blame] | 138 | } |
| 139 | } |
| 140 | |
Will Harmon | 294af23 | 2016-06-24 17:02:34 -0700 | [diff] [blame] | 141 | private boolean isSystemApp(PackageManager pm) throws PackageManager.NameNotFoundException { |
| 142 | return pm.getApplicationInfo(mComponent.getPackageName(), 0).isSystemApp(); |
| 143 | } |
| 144 | |
Jason Monk | 624cbe2 | 2016-05-02 10:42:17 -0400 | [diff] [blame] | 145 | /** |
| 146 | * Compare two icons, only works for resources. |
| 147 | */ |
| 148 | private boolean iconEquals(android.graphics.drawable.Icon icon1, |
| 149 | android.graphics.drawable.Icon icon2) { |
| 150 | if (icon1 == icon2) { |
| 151 | return true; |
| 152 | } |
| 153 | if (icon1 == null || icon2 == null) { |
| 154 | return false; |
| 155 | } |
| 156 | if (icon1.getType() != android.graphics.drawable.Icon.TYPE_RESOURCE |
| 157 | || icon2.getType() != android.graphics.drawable.Icon.TYPE_RESOURCE) { |
| 158 | return false; |
| 159 | } |
| 160 | if (icon1.getResId() != icon2.getResId()) { |
| 161 | return false; |
| 162 | } |
Narayan Kamath | 607223f | 2018-02-19 14:09:02 +0000 | [diff] [blame] | 163 | if (!Objects.equals(icon1.getResPackage(), icon2.getResPackage())) { |
Jason Monk | 624cbe2 | 2016-05-02 10:42:17 -0400 | [diff] [blame] | 164 | return false; |
| 165 | } |
| 166 | return true; |
| 167 | } |
| 168 | |
| 169 | @Override |
| 170 | public void onTileChanged(ComponentName tile) { |
Amin Shaikh | abcca63 | 2018-08-29 15:15:08 -0400 | [diff] [blame] | 171 | updateDefaultTileAndIcon(); |
Jason Monk | 624cbe2 | 2016-05-02 10:42:17 -0400 | [diff] [blame] | 172 | } |
| 173 | |
Jason Monk | 1c2fea8 | 2016-03-11 11:33:36 -0500 | [diff] [blame] | 174 | @Override |
| 175 | public boolean isAvailable() { |
quakentsai(58374) | 3217129 | 2021-05-27 11:48:54 +0800 | [diff] [blame^] | 176 | return mEnable && mDefaultIcon != null; |
Jason Monk | 1c2fea8 | 2016-03-11 11:33:36 -0500 | [diff] [blame] | 177 | } |
| 178 | |
Jason Monk | 1ffa11b | 2016-03-08 14:44:23 -0500 | [diff] [blame] | 179 | public int getUser() { |
| 180 | return mUser; |
| 181 | } |
| 182 | |
Jason Monk | bbadff8 | 2015-11-06 15:47:26 -0500 | [diff] [blame] | 183 | public ComponentName getComponent() { |
| 184 | return mComponent; |
| 185 | } |
| 186 | |
Jason Monk | 8c09ac7 | 2017-03-16 11:53:40 -0400 | [diff] [blame] | 187 | @Override |
Jason Monk | cb4b31d | 2017-05-03 10:37:34 -0400 | [diff] [blame] | 188 | public LogMaker populate(LogMaker logMaker) { |
Jason Monk | 8c09ac7 | 2017-03-16 11:53:40 -0400 | [diff] [blame] | 189 | return super.populate(logMaker).setComponentName(mComponent); |
| 190 | } |
| 191 | |
Jason Monk | bbadff8 | 2015-11-06 15:47:26 -0500 | [diff] [blame] | 192 | public Tile getQsTile() { |
Amin Shaikh | abcca63 | 2018-08-29 15:15:08 -0400 | [diff] [blame] | 193 | updateDefaultTileAndIcon(); |
Jason Monk | bbadff8 | 2015-11-06 15:47:26 -0500 | [diff] [blame] | 194 | return mTile; |
| 195 | } |
| 196 | |
| 197 | public void updateState(Tile tile) { |
Jason Monk | bbadff8 | 2015-11-06 15:47:26 -0500 | [diff] [blame] | 198 | mTile.setIcon(tile.getIcon()); |
| 199 | mTile.setLabel(tile.getLabel()); |
Evan Laird | 66a8e73 | 2018-12-12 13:52:28 -0500 | [diff] [blame] | 200 | mTile.setSubtitle(tile.getSubtitle()); |
Jason Monk | bbadff8 | 2015-11-06 15:47:26 -0500 | [diff] [blame] | 201 | mTile.setContentDescription(tile.getContentDescription()); |
yingleiw | 5e0f9f4 | 2020-02-06 11:12:56 -0800 | [diff] [blame] | 202 | mTile.setStateDescription(tile.getStateDescription()); |
Jason Monk | 9429513 | 2016-01-12 11:27:02 -0500 | [diff] [blame] | 203 | mTile.setState(tile.getState()); |
Jason Monk | 5db8a41 | 2015-10-21 15:16:23 -0700 | [diff] [blame] | 204 | } |
| 205 | |
Jason Monk | 8f7f318 | 2015-11-18 16:35:14 -0500 | [diff] [blame] | 206 | public void onDialogShown() { |
| 207 | mIsShowingDialog = true; |
| 208 | } |
| 209 | |
Jason Monk | 34a5cef | 2016-01-29 11:28:44 -0500 | [diff] [blame] | 210 | public void onDialogHidden() { |
| 211 | mIsShowingDialog = false; |
| 212 | try { |
| 213 | if (DEBUG) Log.d(TAG, "Removing token"); |
Wale Ogunwale | ac2561e | 2016-11-01 15:43:46 -0700 | [diff] [blame] | 214 | mWindowManager.removeWindowToken(mToken, DEFAULT_DISPLAY); |
Jason Monk | 34a5cef | 2016-01-29 11:28:44 -0500 | [diff] [blame] | 215 | } catch (RemoteException e) { |
| 216 | } |
| 217 | } |
| 218 | |
Jason Monk | 5db8a41 | 2015-10-21 15:16:23 -0700 | [diff] [blame] | 219 | @Override |
Jason Monk | 1c6116c | 2017-09-06 17:33:01 -0400 | [diff] [blame] | 220 | public void handleSetListening(boolean listening) { |
Fabian Kozynski | ca30f57 | 2019-10-24 10:01:04 -0400 | [diff] [blame] | 221 | super.handleSetListening(listening); |
Jason Monk | bbadff8 | 2015-11-06 15:47:26 -0500 | [diff] [blame] | 222 | if (mListening == listening) return; |
| 223 | mListening = listening; |
Fabian Kozynski | ca30f57 | 2019-10-24 10:01:04 -0400 | [diff] [blame] | 224 | |
Jason Monk | d5a204f | 2015-12-21 08:50:01 -0500 | [diff] [blame] | 225 | try { |
| 226 | if (listening) { |
Amin Shaikh | abcca63 | 2018-08-29 15:15:08 -0400 | [diff] [blame] | 227 | updateDefaultTileAndIcon(); |
Jason Monk | 624cbe2 | 2016-05-02 10:42:17 -0400 | [diff] [blame] | 228 | refreshState(); |
Jason Monk | 97d2272 | 2016-04-07 11:41:47 -0400 | [diff] [blame] | 229 | if (!mServiceManager.isActiveTile()) { |
Jason Monk | fe8f682 | 2015-12-21 15:12:01 -0500 | [diff] [blame] | 230 | mServiceManager.setBindRequested(true); |
| 231 | mService.onStartListening(); |
| 232 | } |
Jason Monk | dc35dcb | 2015-12-04 16:36:15 -0500 | [diff] [blame] | 233 | } else { |
Jason Monk | bbadff8 | 2015-11-06 15:47:26 -0500 | [diff] [blame] | 234 | mService.onStopListening(); |
Jason Monk | d5a204f | 2015-12-21 08:50:01 -0500 | [diff] [blame] | 235 | if (mIsTokenGranted && !mIsShowingDialog) { |
| 236 | try { |
| 237 | if (DEBUG) Log.d(TAG, "Removing token"); |
Wale Ogunwale | ac2561e | 2016-11-01 15:43:46 -0700 | [diff] [blame] | 238 | mWindowManager.removeWindowToken(mToken, DEFAULT_DISPLAY); |
Jason Monk | d5a204f | 2015-12-21 08:50:01 -0500 | [diff] [blame] | 239 | } catch (RemoteException e) { |
| 240 | } |
| 241 | mIsTokenGranted = false; |
Jason Monk | 8f7f318 | 2015-11-18 16:35:14 -0500 | [diff] [blame] | 242 | } |
Jason Monk | d5a204f | 2015-12-21 08:50:01 -0500 | [diff] [blame] | 243 | mIsShowingDialog = false; |
| 244 | mServiceManager.setBindRequested(false); |
Jason Monk | 8f7f318 | 2015-11-18 16:35:14 -0500 | [diff] [blame] | 245 | } |
Jason Monk | d5a204f | 2015-12-21 08:50:01 -0500 | [diff] [blame] | 246 | } catch (RemoteException e) { |
| 247 | // Called through wrapper, won't happen here. |
Jason Monk | bbadff8 | 2015-11-06 15:47:26 -0500 | [diff] [blame] | 248 | } |
| 249 | } |
Jason Monk | 8f7f318 | 2015-11-18 16:35:14 -0500 | [diff] [blame] | 250 | |
Jason Monk | bbadff8 | 2015-11-06 15:47:26 -0500 | [diff] [blame] | 251 | @Override |
| 252 | protected void handleDestroy() { |
| 253 | super.handleDestroy(); |
Jason Monk | 8f7f318 | 2015-11-18 16:35:14 -0500 | [diff] [blame] | 254 | if (mIsTokenGranted) { |
| 255 | try { |
| 256 | if (DEBUG) Log.d(TAG, "Removing token"); |
Wale Ogunwale | ac2561e | 2016-11-01 15:43:46 -0700 | [diff] [blame] | 257 | mWindowManager.removeWindowToken(mToken, DEFAULT_DISPLAY); |
Jason Monk | 8f7f318 | 2015-11-18 16:35:14 -0500 | [diff] [blame] | 258 | } catch (RemoteException e) { |
| 259 | } |
| 260 | } |
Jason Monk | 66c89c1 | 2016-01-06 08:51:26 -0500 | [diff] [blame] | 261 | mHost.getTileServices().freeService(this, mServiceManager); |
Jason Monk | 5db8a41 | 2015-10-21 15:16:23 -0700 | [diff] [blame] | 262 | } |
| 263 | |
| 264 | @Override |
Jason Monk | 62b63a0 | 2016-02-02 15:15:31 -0500 | [diff] [blame] | 265 | public State newTileState() { |
Fabian Kozynski | 489ad0d | 2020-02-07 10:30:45 -0500 | [diff] [blame] | 266 | if (mServiceManager != null && mServiceManager.isToggleableTile()) { |
Fabian Kozynski | 05843f0 | 2019-06-28 13:19:57 -0400 | [diff] [blame] | 267 | return new BooleanState(); |
| 268 | } |
| 269 | return new State(); |
Jason Monk | 5db8a41 | 2015-10-21 15:16:23 -0700 | [diff] [blame] | 270 | } |
| 271 | |
| 272 | @Override |
Jason Monk | 76c67aa | 2016-02-19 14:49:42 -0500 | [diff] [blame] | 273 | public Intent getLongClickIntent() { |
Jason Monk | 724214a | 2016-02-19 16:43:00 -0500 | [diff] [blame] | 274 | Intent i = new Intent(TileService.ACTION_QS_TILE_PREFERENCES); |
| 275 | i.setPackage(mComponent.getPackageName()); |
| 276 | i = resolveIntent(i); |
| 277 | if (i != null) { |
Jeff Sharkey | 1a74942 | 2017-04-28 14:19:50 -0600 | [diff] [blame] | 278 | i.putExtra(Intent.EXTRA_COMPONENT_NAME, mComponent); |
Akira Oshimi | 8d2e9a9 | 2017-01-24 16:50:53 +0900 | [diff] [blame] | 279 | i.putExtra(TileService.EXTRA_STATE, mTile.getState()); |
Jason Monk | 724214a | 2016-02-19 16:43:00 -0500 | [diff] [blame] | 280 | return i; |
| 281 | } |
Jason Monk | 76c67aa | 2016-02-19 14:49:42 -0500 | [diff] [blame] | 282 | return new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS).setData( |
| 283 | Uri.fromParts("package", mComponent.getPackageName(), null)); |
Jason Monk | 5db8a41 | 2015-10-21 15:16:23 -0700 | [diff] [blame] | 284 | } |
| 285 | |
Jason Monk | 724214a | 2016-02-19 16:43:00 -0500 | [diff] [blame] | 286 | private Intent resolveIntent(Intent i) { |
| 287 | ResolveInfo result = mContext.getPackageManager().resolveActivityAsUser(i, 0, |
| 288 | ActivityManager.getCurrentUser()); |
| 289 | return result != null ? new Intent(TileService.ACTION_QS_TILE_PREFERENCES) |
| 290 | .setClassName(result.activityInfo.packageName, result.activityInfo.name) : null; |
| 291 | } |
| 292 | |
Jason Monk | 5db8a41 | 2015-10-21 15:16:23 -0700 | [diff] [blame] | 293 | @Override |
| 294 | protected void handleClick() { |
Jason Monk | 9429513 | 2016-01-12 11:27:02 -0500 | [diff] [blame] | 295 | if (mTile.getState() == Tile.STATE_UNAVAILABLE) { |
| 296 | return; |
| 297 | } |
Jason Monk | fe8f682 | 2015-12-21 15:12:01 -0500 | [diff] [blame] | 298 | try { |
| 299 | if (DEBUG) Log.d(TAG, "Adding token"); |
Wale Ogunwale | ac2561e | 2016-11-01 15:43:46 -0700 | [diff] [blame] | 300 | mWindowManager.addWindowToken(mToken, TYPE_QS_DIALOG, DEFAULT_DISPLAY); |
Jason Monk | fe8f682 | 2015-12-21 15:12:01 -0500 | [diff] [blame] | 301 | mIsTokenGranted = true; |
| 302 | } catch (RemoteException e) { |
| 303 | } |
| 304 | try { |
Jason Monk | 97d2272 | 2016-04-07 11:41:47 -0400 | [diff] [blame] | 305 | if (mServiceManager.isActiveTile()) { |
Jason Monk | fe8f682 | 2015-12-21 15:12:01 -0500 | [diff] [blame] | 306 | mServiceManager.setBindRequested(true); |
| 307 | mService.onStartListening(); |
Jason Monk | 8f7f318 | 2015-11-18 16:35:14 -0500 | [diff] [blame] | 308 | } |
Jason Monk | fe8f682 | 2015-12-21 15:12:01 -0500 | [diff] [blame] | 309 | mService.onClick(mToken); |
| 310 | } catch (RemoteException e) { |
| 311 | // Called through wrapper, won't happen here. |
Jason Monk | bbadff8 | 2015-11-06 15:47:26 -0500 | [diff] [blame] | 312 | } |
Jason Monk | 5db8a41 | 2015-10-21 15:16:23 -0700 | [diff] [blame] | 313 | } |
| 314 | |
| 315 | @Override |
Jason Monk | 39c98e6 | 2016-03-16 09:18:35 -0400 | [diff] [blame] | 316 | public CharSequence getTileLabel() { |
| 317 | return getState().label; |
| 318 | } |
| 319 | |
| 320 | @Override |
Jason Monk | 5db8a41 | 2015-10-21 15:16:23 -0700 | [diff] [blame] | 321 | protected void handleUpdateState(State state, Object arg) { |
Jason Monk | 1c2fea8 | 2016-03-11 11:33:36 -0500 | [diff] [blame] | 322 | int tileState = mTile.getState(); |
| 323 | if (mServiceManager.hasPendingBind()) { |
| 324 | tileState = Tile.STATE_UNAVAILABLE; |
| 325 | } |
Jason Monk | 3250885 | 2017-01-18 09:17:13 -0500 | [diff] [blame] | 326 | state.state = tileState; |
Jason Monk | 4a906f9 | 2016-04-20 10:54:55 -0400 | [diff] [blame] | 327 | Drawable drawable; |
| 328 | try { |
Fabian Kozynski | 4ba3f9a | 2020-02-21 11:02:42 -0500 | [diff] [blame] | 329 | drawable = mTile.getIcon().loadDrawable(mUserContext); |
Jason Monk | 4a906f9 | 2016-04-20 10:54:55 -0400 | [diff] [blame] | 330 | } catch (Exception e) { |
| 331 | Log.w(TAG, "Invalid icon, forcing into unavailable state"); |
Jason Monk | 6f352aa | 2017-03-03 09:10:50 -0500 | [diff] [blame] | 332 | state.state = Tile.STATE_UNAVAILABLE; |
Fabian Kozynski | 4ba3f9a | 2020-02-21 11:02:42 -0500 | [diff] [blame] | 333 | drawable = mDefaultIcon.loadDrawable(mUserContext); |
Jason Monk | 4a906f9 | 2016-04-20 10:54:55 -0400 | [diff] [blame] | 334 | } |
Evan Laird | b3daf2b | 2017-10-17 16:00:29 -0400 | [diff] [blame] | 335 | |
| 336 | final Drawable drawableF = drawable; |
| 337 | state.iconSupplier = () -> { |
Fabian Kozynski | 2648a2a | 2020-02-21 13:20:07 -0500 | [diff] [blame] | 338 | if (drawableF == null) return null; |
Evan Laird | b3daf2b | 2017-10-17 16:00:29 -0400 | [diff] [blame] | 339 | Drawable.ConstantState cs = drawableF.getConstantState(); |
| 340 | if (cs != null) { |
| 341 | return new DrawableIcon(cs.newDrawable()); |
| 342 | } |
| 343 | return null; |
| 344 | }; |
Jason Monk | bbadff8 | 2015-11-06 15:47:26 -0500 | [diff] [blame] | 345 | state.label = mTile.getLabel(); |
Evan Laird | 66a8e73 | 2018-12-12 13:52:28 -0500 | [diff] [blame] | 346 | |
| 347 | CharSequence subtitle = mTile.getSubtitle(); |
| 348 | if (subtitle != null && subtitle.length() > 0) { |
| 349 | state.secondaryLabel = subtitle; |
| 350 | } else { |
| 351 | state.secondaryLabel = null; |
| 352 | } |
| 353 | |
Jason Monk | bbadff8 | 2015-11-06 15:47:26 -0500 | [diff] [blame] | 354 | if (mTile.getContentDescription() != null) { |
| 355 | state.contentDescription = mTile.getContentDescription(); |
| 356 | } else { |
Jason Monk | 5db8a41 | 2015-10-21 15:16:23 -0700 | [diff] [blame] | 357 | state.contentDescription = state.label; |
Jason Monk | 5db8a41 | 2015-10-21 15:16:23 -0700 | [diff] [blame] | 358 | } |
Fabian Kozynski | 05843f0 | 2019-06-28 13:19:57 -0400 | [diff] [blame] | 359 | |
yingleiw | 5e0f9f4 | 2020-02-06 11:12:56 -0800 | [diff] [blame] | 360 | if (mTile.getStateDescription() != null) { |
| 361 | state.stateDescription = mTile.getStateDescription(); |
| 362 | } else { |
| 363 | state.stateDescription = null; |
| 364 | } |
| 365 | |
Fabian Kozynski | 05843f0 | 2019-06-28 13:19:57 -0400 | [diff] [blame] | 366 | if (state instanceof BooleanState) { |
| 367 | state.expandedAccessibilityClassName = Switch.class.getName(); |
| 368 | ((BooleanState) state).value = (state.state == Tile.STATE_ACTIVE); |
| 369 | } |
| 370 | |
Jason Monk | 5db8a41 | 2015-10-21 15:16:23 -0700 | [diff] [blame] | 371 | } |
| 372 | |
| 373 | @Override |
| 374 | public int getMetricsCategory() { |
Chris Wren | f6e9228b | 2016-01-26 18:04:35 -0500 | [diff] [blame] | 375 | return MetricsEvent.QS_CUSTOM; |
Jason Monk | 5db8a41 | 2015-10-21 15:16:23 -0700 | [diff] [blame] | 376 | } |
Jason Monk | bbadff8 | 2015-11-06 15:47:26 -0500 | [diff] [blame] | 377 | |
Fabian Kozynski | 2ff6df9 | 2020-04-24 12:00:49 -0400 | [diff] [blame] | 378 | @Override |
| 379 | public final String getMetricsSpec() { |
| 380 | return mComponent.getPackageName(); |
| 381 | } |
| 382 | |
Jason Monk | 9429513 | 2016-01-12 11:27:02 -0500 | [diff] [blame] | 383 | public void startUnlockAndRun() { |
Jason Monk | 9c7844c | 2017-01-18 15:21:53 -0500 | [diff] [blame] | 384 | Dependency.get(ActivityStarter.class).postQSRunnableDismissingKeyguard(() -> { |
| 385 | try { |
| 386 | mService.onUnlockComplete(); |
| 387 | } catch (RemoteException e) { |
Jason Monk | 9429513 | 2016-01-12 11:27:02 -0500 | [diff] [blame] | 388 | } |
| 389 | }); |
| 390 | } |
| 391 | |
Jason Monk | 7e53f20 | 2016-01-28 10:40:20 -0500 | [diff] [blame] | 392 | public static String toSpec(ComponentName name) { |
| 393 | return PREFIX + name.flattenToShortString() + ")"; |
| 394 | } |
| 395 | |
Jason Monk | bbadff8 | 2015-11-06 15:47:26 -0500 | [diff] [blame] | 396 | public static ComponentName getComponentFromSpec(String spec) { |
| 397 | final String action = spec.substring(PREFIX.length(), spec.length() - 1); |
| 398 | if (action.isEmpty()) { |
| 399 | throw new IllegalArgumentException("Empty custom tile spec action"); |
| 400 | } |
| 401 | return ComponentName.unflattenFromString(action); |
| 402 | } |
| 403 | |
John Li | 4e3dddc | 2020-03-04 21:35:43 +0800 | [diff] [blame] | 404 | public static CustomTile create(QSHost host, String spec, Context userContext) { |
Jason Monk | bbadff8 | 2015-11-06 15:47:26 -0500 | [diff] [blame] | 405 | if (spec == null || !spec.startsWith(PREFIX) || !spec.endsWith(")")) { |
| 406 | throw new IllegalArgumentException("Bad custom tile spec: " + spec); |
| 407 | } |
| 408 | final String action = spec.substring(PREFIX.length(), spec.length() - 1); |
| 409 | if (action.isEmpty()) { |
| 410 | throw new IllegalArgumentException("Empty custom tile spec action"); |
| 411 | } |
Fabian Kozynski | 4ba3f9a | 2020-02-21 11:02:42 -0500 | [diff] [blame] | 412 | return new CustomTile(host, action, userContext); |
Jason Monk | bbadff8 | 2015-11-06 15:47:26 -0500 | [diff] [blame] | 413 | } |
Jason Monk | 5db8a41 | 2015-10-21 15:16:23 -0700 | [diff] [blame] | 414 | } |