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 | |
Jason Monk | 724214a | 2016-02-19 16:43:00 -0500 | [diff] [blame] | 18 | import android.app.ActivityManager; |
Jason Monk | 5db8a41 | 2015-10-21 15:16:23 -0700 | [diff] [blame] | 19 | import android.content.ComponentName; |
Jason Monk | 76c67aa | 2016-02-19 14:49:42 -0500 | [diff] [blame] | 20 | import android.content.Intent; |
Jason Monk | 5db8a41 | 2015-10-21 15:16:23 -0700 | [diff] [blame] | 21 | import android.content.pm.PackageManager; |
Jason Monk | 724214a | 2016-02-19 16:43:00 -0500 | [diff] [blame] | 22 | import android.content.pm.ResolveInfo; |
Jason Monk | 5db8a41 | 2015-10-21 15:16:23 -0700 | [diff] [blame] | 23 | import android.content.pm.ServiceInfo; |
Jason Monk | 068cb8b | 2015-12-02 11:30:36 -0500 | [diff] [blame] | 24 | import android.graphics.drawable.Drawable; |
Jason Monk | 76c67aa | 2016-02-19 14:49:42 -0500 | [diff] [blame] | 25 | import android.net.Uri; |
Jason Monk | 8f7f318 | 2015-11-18 16:35:14 -0500 | [diff] [blame] | 26 | import android.os.Binder; |
Jason Monk | bbadff8 | 2015-11-06 15:47:26 -0500 | [diff] [blame] | 27 | import android.os.IBinder; |
Jason Monk | 8f7f318 | 2015-11-18 16:35:14 -0500 | [diff] [blame] | 28 | import android.os.RemoteException; |
Jason Monk | 76c67aa | 2016-02-19 14:49:42 -0500 | [diff] [blame] | 29 | import android.provider.Settings; |
Jason Monk | bbadff8 | 2015-11-06 15:47:26 -0500 | [diff] [blame] | 30 | import android.service.quicksettings.IQSTileService; |
| 31 | import android.service.quicksettings.Tile; |
Jason Monk | fe8f682 | 2015-12-21 15:12:01 -0500 | [diff] [blame] | 32 | import android.service.quicksettings.TileService; |
Jason Monk | 9429513 | 2016-01-12 11:27:02 -0500 | [diff] [blame] | 33 | import android.text.SpannableStringBuilder; |
| 34 | import android.text.style.ForegroundColorSpan; |
Jason Monk | bbadff8 | 2015-11-06 15:47:26 -0500 | [diff] [blame] | 35 | import android.util.Log; |
Jason Monk | 8f7f318 | 2015-11-18 16:35:14 -0500 | [diff] [blame] | 36 | import android.view.IWindowManager; |
| 37 | import android.view.WindowManager; |
| 38 | import android.view.WindowManagerGlobal; |
Jason Monk | 5db8a41 | 2015-10-21 15:16:23 -0700 | [diff] [blame] | 39 | import com.android.internal.logging.MetricsLogger; |
Tamas Berghammer | cbd3f0c | 2016-06-22 15:21:38 +0100 | [diff] [blame^] | 40 | import com.android.internal.logging.nano.MetricsProto.MetricsEvent; |
Jason Monk | 9429513 | 2016-01-12 11:27:02 -0500 | [diff] [blame] | 41 | import com.android.systemui.R; |
Jason Monk | 5db8a41 | 2015-10-21 15:16:23 -0700 | [diff] [blame] | 42 | import com.android.systemui.qs.QSTile; |
Jason Monk | 624cbe2 | 2016-05-02 10:42:17 -0400 | [diff] [blame] | 43 | import com.android.systemui.qs.external.TileLifecycleManager.TileChangeListener; |
Jason Monk | bbadff8 | 2015-11-06 15:47:26 -0500 | [diff] [blame] | 44 | import com.android.systemui.statusbar.phone.QSTileHost; |
Jason Monk | 624cbe2 | 2016-05-02 10:42:17 -0400 | [diff] [blame] | 45 | import libcore.util.Objects; |
Jason Monk | 5db8a41 | 2015-10-21 15:16:23 -0700 | [diff] [blame] | 46 | |
Jason Monk | 624cbe2 | 2016-05-02 10:42:17 -0400 | [diff] [blame] | 47 | public class CustomTile extends QSTile<QSTile.State> implements TileChangeListener { |
Jason Monk | 5db8a41 | 2015-10-21 15:16:23 -0700 | [diff] [blame] | 48 | public static final String PREFIX = "custom("; |
| 49 | |
Jason Monk | 8f7f318 | 2015-11-18 16:35:14 -0500 | [diff] [blame] | 50 | private static final boolean DEBUG = false; |
| 51 | |
Jason Monk | bbadff8 | 2015-11-06 15:47:26 -0500 | [diff] [blame] | 52 | // We don't want to thrash binding and unbinding if the user opens and closes the panel a lot. |
| 53 | // So instead we have a period of waiting. |
| 54 | private static final long UNBIND_DELAY = 30000; |
Jason Monk | 5db8a41 | 2015-10-21 15:16:23 -0700 | [diff] [blame] | 55 | |
Jason Monk | bbadff8 | 2015-11-06 15:47:26 -0500 | [diff] [blame] | 56 | private final ComponentName mComponent; |
| 57 | private final Tile mTile; |
Jason Monk | 8f7f318 | 2015-11-18 16:35:14 -0500 | [diff] [blame] | 58 | private final IWindowManager mWindowManager; |
| 59 | private final IBinder mToken = new Binder(); |
Jason Monk | d5a204f | 2015-12-21 08:50:01 -0500 | [diff] [blame] | 60 | private final IQSTileService mService; |
| 61 | private final TileServiceManager mServiceManager; |
Jason Monk | 1ffa11b | 2016-03-08 14:44:23 -0500 | [diff] [blame] | 62 | private final int mUser; |
Jason Monk | 624cbe2 | 2016-05-02 10:42:17 -0400 | [diff] [blame] | 63 | private android.graphics.drawable.Icon mDefaultIcon; |
Jason Monk | bbadff8 | 2015-11-06 15:47:26 -0500 | [diff] [blame] | 64 | |
Jason Monk | bbadff8 | 2015-11-06 15:47:26 -0500 | [diff] [blame] | 65 | private boolean mListening; |
| 66 | private boolean mBound; |
Jason Monk | 8f7f318 | 2015-11-18 16:35:14 -0500 | [diff] [blame] | 67 | private boolean mIsTokenGranted; |
| 68 | private boolean mIsShowingDialog; |
Jason Monk | bbadff8 | 2015-11-06 15:47:26 -0500 | [diff] [blame] | 69 | |
| 70 | private CustomTile(QSTileHost host, String action) { |
Jason Monk | 5db8a41 | 2015-10-21 15:16:23 -0700 | [diff] [blame] | 71 | super(host); |
Jason Monk | 8f7f318 | 2015-11-18 16:35:14 -0500 | [diff] [blame] | 72 | mWindowManager = WindowManagerGlobal.getWindowManagerService(); |
Jason Monk | 5db8a41 | 2015-10-21 15:16:23 -0700 | [diff] [blame] | 73 | mComponent = ComponentName.unflattenFromString(action); |
Jason Monk | ee68fd8 | 2016-06-23 13:12:23 -0400 | [diff] [blame] | 74 | mTile = new Tile(); |
Jason Monk | a3453b8b | 2016-06-17 12:42:59 -0400 | [diff] [blame] | 75 | setTileIcon(); |
Jason Monk | d5a204f | 2015-12-21 08:50:01 -0500 | [diff] [blame] | 76 | mServiceManager = host.getTileServices().getTileWrapper(this); |
| 77 | mService = mServiceManager.getTileService(); |
Jason Monk | 624cbe2 | 2016-05-02 10:42:17 -0400 | [diff] [blame] | 78 | mServiceManager.setTileChangeListener(this); |
Jason Monk | 1ffa11b | 2016-03-08 14:44:23 -0500 | [diff] [blame] | 79 | mUser = ActivityManager.getCurrentUser(); |
Jason Monk | 5db8a41 | 2015-10-21 15:16:23 -0700 | [diff] [blame] | 80 | } |
| 81 | |
Jason Monk | 624cbe2 | 2016-05-02 10:42:17 -0400 | [diff] [blame] | 82 | private void setTileIcon() { |
| 83 | try { |
| 84 | PackageManager pm = mContext.getPackageManager(); |
Will Harmon | 294af23 | 2016-06-24 17:02:34 -0700 | [diff] [blame] | 85 | int flags = PackageManager.MATCH_ENCRYPTION_AWARE_AND_UNAWARE; |
| 86 | if (isSystemApp(pm)) { |
| 87 | flags |= PackageManager.MATCH_DISABLED_COMPONENTS; |
| 88 | } |
| 89 | ServiceInfo info = pm.getServiceInfo(mComponent, flags); |
Jason Monk | a5f6ed3 | 2016-06-22 09:58:46 -0400 | [diff] [blame] | 90 | int icon = info.icon != 0 ? info.icon |
| 91 | : info.applicationInfo.icon; |
Jason Monk | 624cbe2 | 2016-05-02 10:42:17 -0400 | [diff] [blame] | 92 | // Update the icon if its not set or is the default icon. |
| 93 | boolean updateIcon = mTile.getIcon() == null |
| 94 | || iconEquals(mTile.getIcon(), mDefaultIcon); |
Jason Monk | a5f6ed3 | 2016-06-22 09:58:46 -0400 | [diff] [blame] | 95 | mDefaultIcon = icon != 0 ? android.graphics.drawable.Icon |
| 96 | .createWithResource(mComponent.getPackageName(), icon) : null; |
Jason Monk | 624cbe2 | 2016-05-02 10:42:17 -0400 | [diff] [blame] | 97 | if (updateIcon) { |
| 98 | mTile.setIcon(mDefaultIcon); |
| 99 | } |
| 100 | // Update the label if there is no label. |
| 101 | if (mTile.getLabel() == null) { |
| 102 | mTile.setLabel(info.loadLabel(pm)); |
| 103 | } |
| 104 | } catch (Exception e) { |
| 105 | mDefaultIcon = null; |
| 106 | } |
| 107 | } |
| 108 | |
Will Harmon | 294af23 | 2016-06-24 17:02:34 -0700 | [diff] [blame] | 109 | private boolean isSystemApp(PackageManager pm) throws PackageManager.NameNotFoundException { |
| 110 | return pm.getApplicationInfo(mComponent.getPackageName(), 0).isSystemApp(); |
| 111 | } |
| 112 | |
Jason Monk | 624cbe2 | 2016-05-02 10:42:17 -0400 | [diff] [blame] | 113 | /** |
| 114 | * Compare two icons, only works for resources. |
| 115 | */ |
| 116 | private boolean iconEquals(android.graphics.drawable.Icon icon1, |
| 117 | android.graphics.drawable.Icon icon2) { |
| 118 | if (icon1 == icon2) { |
| 119 | return true; |
| 120 | } |
| 121 | if (icon1 == null || icon2 == null) { |
| 122 | return false; |
| 123 | } |
| 124 | if (icon1.getType() != android.graphics.drawable.Icon.TYPE_RESOURCE |
| 125 | || icon2.getType() != android.graphics.drawable.Icon.TYPE_RESOURCE) { |
| 126 | return false; |
| 127 | } |
| 128 | if (icon1.getResId() != icon2.getResId()) { |
| 129 | return false; |
| 130 | } |
| 131 | if (!Objects.equal(icon1.getResPackage(), icon2.getResPackage())) { |
| 132 | return false; |
| 133 | } |
| 134 | return true; |
| 135 | } |
| 136 | |
| 137 | @Override |
| 138 | public void onTileChanged(ComponentName tile) { |
| 139 | setTileIcon(); |
| 140 | } |
| 141 | |
Jason Monk | 1c2fea8 | 2016-03-11 11:33:36 -0500 | [diff] [blame] | 142 | @Override |
| 143 | public boolean isAvailable() { |
Jason Monk | 4a906f9 | 2016-04-20 10:54:55 -0400 | [diff] [blame] | 144 | return mDefaultIcon != null; |
Jason Monk | 1c2fea8 | 2016-03-11 11:33:36 -0500 | [diff] [blame] | 145 | } |
| 146 | |
Jason Monk | 1ffa11b | 2016-03-08 14:44:23 -0500 | [diff] [blame] | 147 | public int getUser() { |
| 148 | return mUser; |
| 149 | } |
| 150 | |
Jason Monk | bbadff8 | 2015-11-06 15:47:26 -0500 | [diff] [blame] | 151 | public ComponentName getComponent() { |
| 152 | return mComponent; |
| 153 | } |
| 154 | |
| 155 | public Tile getQsTile() { |
| 156 | return mTile; |
| 157 | } |
| 158 | |
| 159 | public void updateState(Tile tile) { |
Jason Monk | bbadff8 | 2015-11-06 15:47:26 -0500 | [diff] [blame] | 160 | mTile.setIcon(tile.getIcon()); |
| 161 | mTile.setLabel(tile.getLabel()); |
| 162 | mTile.setContentDescription(tile.getContentDescription()); |
Jason Monk | 9429513 | 2016-01-12 11:27:02 -0500 | [diff] [blame] | 163 | mTile.setState(tile.getState()); |
Jason Monk | 5db8a41 | 2015-10-21 15:16:23 -0700 | [diff] [blame] | 164 | } |
| 165 | |
Jason Monk | 8f7f318 | 2015-11-18 16:35:14 -0500 | [diff] [blame] | 166 | public void onDialogShown() { |
| 167 | mIsShowingDialog = true; |
| 168 | } |
| 169 | |
Jason Monk | 34a5cef | 2016-01-29 11:28:44 -0500 | [diff] [blame] | 170 | public void onDialogHidden() { |
| 171 | mIsShowingDialog = false; |
| 172 | try { |
| 173 | if (DEBUG) Log.d(TAG, "Removing token"); |
| 174 | mWindowManager.removeWindowToken(mToken); |
| 175 | } catch (RemoteException e) { |
| 176 | } |
| 177 | } |
| 178 | |
Jason Monk | 5db8a41 | 2015-10-21 15:16:23 -0700 | [diff] [blame] | 179 | @Override |
| 180 | public void setListening(boolean listening) { |
Jason Monk | bbadff8 | 2015-11-06 15:47:26 -0500 | [diff] [blame] | 181 | if (mListening == listening) return; |
| 182 | mListening = listening; |
Jason Monk | d5a204f | 2015-12-21 08:50:01 -0500 | [diff] [blame] | 183 | try { |
| 184 | if (listening) { |
Jason Monk | 624cbe2 | 2016-05-02 10:42:17 -0400 | [diff] [blame] | 185 | setTileIcon(); |
| 186 | refreshState(); |
Jason Monk | 97d2272 | 2016-04-07 11:41:47 -0400 | [diff] [blame] | 187 | if (!mServiceManager.isActiveTile()) { |
Jason Monk | fe8f682 | 2015-12-21 15:12:01 -0500 | [diff] [blame] | 188 | mServiceManager.setBindRequested(true); |
| 189 | mService.onStartListening(); |
| 190 | } |
Jason Monk | dc35dcb | 2015-12-04 16:36:15 -0500 | [diff] [blame] | 191 | } else { |
Jason Monk | bbadff8 | 2015-11-06 15:47:26 -0500 | [diff] [blame] | 192 | mService.onStopListening(); |
Jason Monk | d5a204f | 2015-12-21 08:50:01 -0500 | [diff] [blame] | 193 | if (mIsTokenGranted && !mIsShowingDialog) { |
| 194 | try { |
| 195 | if (DEBUG) Log.d(TAG, "Removing token"); |
| 196 | mWindowManager.removeWindowToken(mToken); |
| 197 | } catch (RemoteException e) { |
| 198 | } |
| 199 | mIsTokenGranted = false; |
Jason Monk | 8f7f318 | 2015-11-18 16:35:14 -0500 | [diff] [blame] | 200 | } |
Jason Monk | d5a204f | 2015-12-21 08:50:01 -0500 | [diff] [blame] | 201 | mIsShowingDialog = false; |
| 202 | mServiceManager.setBindRequested(false); |
Jason Monk | 8f7f318 | 2015-11-18 16:35:14 -0500 | [diff] [blame] | 203 | } |
Jason Monk | d5a204f | 2015-12-21 08:50:01 -0500 | [diff] [blame] | 204 | } catch (RemoteException e) { |
| 205 | // Called through wrapper, won't happen here. |
Jason Monk | bbadff8 | 2015-11-06 15:47:26 -0500 | [diff] [blame] | 206 | } |
| 207 | } |
Jason Monk | 8f7f318 | 2015-11-18 16:35:14 -0500 | [diff] [blame] | 208 | |
Jason Monk | bbadff8 | 2015-11-06 15:47:26 -0500 | [diff] [blame] | 209 | @Override |
| 210 | protected void handleDestroy() { |
| 211 | super.handleDestroy(); |
Jason Monk | 8f7f318 | 2015-11-18 16:35:14 -0500 | [diff] [blame] | 212 | if (mIsTokenGranted) { |
| 213 | try { |
| 214 | if (DEBUG) Log.d(TAG, "Removing token"); |
| 215 | mWindowManager.removeWindowToken(mToken); |
| 216 | } catch (RemoteException e) { |
| 217 | } |
| 218 | } |
Jason Monk | 66c89c1 | 2016-01-06 08:51:26 -0500 | [diff] [blame] | 219 | mHost.getTileServices().freeService(this, mServiceManager); |
Jason Monk | 5db8a41 | 2015-10-21 15:16:23 -0700 | [diff] [blame] | 220 | } |
| 221 | |
| 222 | @Override |
Jason Monk | 62b63a0 | 2016-02-02 15:15:31 -0500 | [diff] [blame] | 223 | public State newTileState() { |
Yoshinori Hirano | eb09362 | 2016-08-17 14:09:58 +0900 | [diff] [blame] | 224 | State state = new State(); |
| 225 | state.autoMirrorDrawable = false; |
| 226 | return state; |
Jason Monk | 5db8a41 | 2015-10-21 15:16:23 -0700 | [diff] [blame] | 227 | } |
| 228 | |
| 229 | @Override |
Jason Monk | 76c67aa | 2016-02-19 14:49:42 -0500 | [diff] [blame] | 230 | public Intent getLongClickIntent() { |
Jason Monk | 724214a | 2016-02-19 16:43:00 -0500 | [diff] [blame] | 231 | Intent i = new Intent(TileService.ACTION_QS_TILE_PREFERENCES); |
| 232 | i.setPackage(mComponent.getPackageName()); |
| 233 | i = resolveIntent(i); |
| 234 | if (i != null) { |
| 235 | return i; |
| 236 | } |
Jason Monk | 76c67aa | 2016-02-19 14:49:42 -0500 | [diff] [blame] | 237 | return new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS).setData( |
| 238 | Uri.fromParts("package", mComponent.getPackageName(), null)); |
Jason Monk | 5db8a41 | 2015-10-21 15:16:23 -0700 | [diff] [blame] | 239 | } |
| 240 | |
Jason Monk | 724214a | 2016-02-19 16:43:00 -0500 | [diff] [blame] | 241 | private Intent resolveIntent(Intent i) { |
| 242 | ResolveInfo result = mContext.getPackageManager().resolveActivityAsUser(i, 0, |
| 243 | ActivityManager.getCurrentUser()); |
| 244 | return result != null ? new Intent(TileService.ACTION_QS_TILE_PREFERENCES) |
| 245 | .setClassName(result.activityInfo.packageName, result.activityInfo.name) : null; |
| 246 | } |
| 247 | |
Jason Monk | 5db8a41 | 2015-10-21 15:16:23 -0700 | [diff] [blame] | 248 | @Override |
| 249 | protected void handleClick() { |
Jason Monk | 9429513 | 2016-01-12 11:27:02 -0500 | [diff] [blame] | 250 | if (mTile.getState() == Tile.STATE_UNAVAILABLE) { |
| 251 | return; |
| 252 | } |
Jason Monk | fe8f682 | 2015-12-21 15:12:01 -0500 | [diff] [blame] | 253 | try { |
| 254 | if (DEBUG) Log.d(TAG, "Adding token"); |
| 255 | mWindowManager.addWindowToken(mToken, WindowManager.LayoutParams.TYPE_QS_DIALOG); |
| 256 | mIsTokenGranted = true; |
| 257 | } catch (RemoteException e) { |
| 258 | } |
| 259 | try { |
Jason Monk | 97d2272 | 2016-04-07 11:41:47 -0400 | [diff] [blame] | 260 | if (mServiceManager.isActiveTile()) { |
Jason Monk | fe8f682 | 2015-12-21 15:12:01 -0500 | [diff] [blame] | 261 | mServiceManager.setBindRequested(true); |
| 262 | mService.onStartListening(); |
Jason Monk | 8f7f318 | 2015-11-18 16:35:14 -0500 | [diff] [blame] | 263 | } |
Jason Monk | fe8f682 | 2015-12-21 15:12:01 -0500 | [diff] [blame] | 264 | mService.onClick(mToken); |
| 265 | } catch (RemoteException e) { |
| 266 | // Called through wrapper, won't happen here. |
Jason Monk | bbadff8 | 2015-11-06 15:47:26 -0500 | [diff] [blame] | 267 | } |
Jason Monk | 5db8a41 | 2015-10-21 15:16:23 -0700 | [diff] [blame] | 268 | MetricsLogger.action(mContext, getMetricsCategory(), mComponent.getPackageName()); |
| 269 | } |
| 270 | |
| 271 | @Override |
Jason Monk | 39c98e6 | 2016-03-16 09:18:35 -0400 | [diff] [blame] | 272 | public CharSequence getTileLabel() { |
| 273 | return getState().label; |
| 274 | } |
| 275 | |
| 276 | @Override |
Jason Monk | 5db8a41 | 2015-10-21 15:16:23 -0700 | [diff] [blame] | 277 | protected void handleUpdateState(State state, Object arg) { |
Jason Monk | 1c2fea8 | 2016-03-11 11:33:36 -0500 | [diff] [blame] | 278 | int tileState = mTile.getState(); |
| 279 | if (mServiceManager.hasPendingBind()) { |
| 280 | tileState = Tile.STATE_UNAVAILABLE; |
| 281 | } |
Jason Monk | 4a906f9 | 2016-04-20 10:54:55 -0400 | [diff] [blame] | 282 | Drawable drawable; |
| 283 | try { |
| 284 | drawable = mTile.getIcon().loadDrawable(mContext); |
| 285 | } catch (Exception e) { |
| 286 | Log.w(TAG, "Invalid icon, forcing into unavailable state"); |
| 287 | tileState = Tile.STATE_UNAVAILABLE; |
| 288 | drawable = mDefaultIcon.loadDrawable(mContext); |
| 289 | } |
Jason Monk | 1c2fea8 | 2016-03-11 11:33:36 -0500 | [diff] [blame] | 290 | int color = mContext.getColor(getColor(tileState)); |
Jason Monk | 9429513 | 2016-01-12 11:27:02 -0500 | [diff] [blame] | 291 | drawable.setTint(color); |
Jason Monk | 068cb8b | 2015-12-02 11:30:36 -0500 | [diff] [blame] | 292 | state.icon = new DrawableIcon(drawable); |
Jason Monk | bbadff8 | 2015-11-06 15:47:26 -0500 | [diff] [blame] | 293 | state.label = mTile.getLabel(); |
Jason Monk | 1c2fea8 | 2016-03-11 11:33:36 -0500 | [diff] [blame] | 294 | if (tileState == Tile.STATE_UNAVAILABLE) { |
Jason Monk | 9429513 | 2016-01-12 11:27:02 -0500 | [diff] [blame] | 295 | state.label = new SpannableStringBuilder().append(state.label, |
| 296 | new ForegroundColorSpan(color), |
| 297 | SpannableStringBuilder.SPAN_INCLUSIVE_INCLUSIVE); |
| 298 | } |
Jason Monk | bbadff8 | 2015-11-06 15:47:26 -0500 | [diff] [blame] | 299 | if (mTile.getContentDescription() != null) { |
| 300 | state.contentDescription = mTile.getContentDescription(); |
| 301 | } else { |
Jason Monk | 5db8a41 | 2015-10-21 15:16:23 -0700 | [diff] [blame] | 302 | state.contentDescription = state.label; |
Jason Monk | 5db8a41 | 2015-10-21 15:16:23 -0700 | [diff] [blame] | 303 | } |
| 304 | } |
| 305 | |
| 306 | @Override |
| 307 | public int getMetricsCategory() { |
Chris Wren | f6e9228b | 2016-01-26 18:04:35 -0500 | [diff] [blame] | 308 | return MetricsEvent.QS_CUSTOM; |
Jason Monk | 5db8a41 | 2015-10-21 15:16:23 -0700 | [diff] [blame] | 309 | } |
Jason Monk | bbadff8 | 2015-11-06 15:47:26 -0500 | [diff] [blame] | 310 | |
Jason Monk | 9429513 | 2016-01-12 11:27:02 -0500 | [diff] [blame] | 311 | public void startUnlockAndRun() { |
| 312 | mHost.startRunnableDismissingKeyguard(new Runnable() { |
| 313 | @Override |
| 314 | public void run() { |
| 315 | try { |
| 316 | mService.onUnlockComplete(); |
| 317 | } catch (RemoteException e) { |
| 318 | } |
| 319 | } |
| 320 | }); |
| 321 | } |
| 322 | |
| 323 | private static int getColor(int state) { |
| 324 | switch (state) { |
| 325 | case Tile.STATE_UNAVAILABLE: |
| 326 | return R.color.qs_tile_tint_unavailable; |
| 327 | case Tile.STATE_INACTIVE: |
| 328 | return R.color.qs_tile_tint_inactive; |
| 329 | case Tile.STATE_ACTIVE: |
| 330 | return R.color.qs_tile_tint_active; |
| 331 | } |
| 332 | return 0; |
| 333 | } |
| 334 | |
Jason Monk | 7e53f20 | 2016-01-28 10:40:20 -0500 | [diff] [blame] | 335 | public static String toSpec(ComponentName name) { |
| 336 | return PREFIX + name.flattenToShortString() + ")"; |
| 337 | } |
| 338 | |
Jason Monk | bbadff8 | 2015-11-06 15:47:26 -0500 | [diff] [blame] | 339 | public static ComponentName getComponentFromSpec(String spec) { |
| 340 | final String action = spec.substring(PREFIX.length(), spec.length() - 1); |
| 341 | if (action.isEmpty()) { |
| 342 | throw new IllegalArgumentException("Empty custom tile spec action"); |
| 343 | } |
| 344 | return ComponentName.unflattenFromString(action); |
| 345 | } |
| 346 | |
| 347 | public static QSTile<?> create(QSTileHost host, String spec) { |
| 348 | if (spec == null || !spec.startsWith(PREFIX) || !spec.endsWith(")")) { |
| 349 | throw new IllegalArgumentException("Bad custom tile spec: " + spec); |
| 350 | } |
| 351 | final String action = spec.substring(PREFIX.length(), spec.length() - 1); |
| 352 | if (action.isEmpty()) { |
| 353 | throw new IllegalArgumentException("Empty custom tile spec action"); |
| 354 | } |
| 355 | return new CustomTile(host, action); |
| 356 | } |
Jason Monk | 5db8a41 | 2015-10-21 15:16:23 -0700 | [diff] [blame] | 357 | } |