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