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; |
Chris Wren | f6e9228b | 2016-01-26 18:04:35 -0500 | [diff] [blame] | 39 | |
Jason Monk | 5db8a41 | 2015-10-21 15:16:23 -0700 | [diff] [blame] | 40 | import com.android.internal.logging.MetricsLogger; |
Chris Wren | f6e9228b | 2016-01-26 18:04:35 -0500 | [diff] [blame] | 41 | import com.android.internal.logging.MetricsProto.MetricsEvent; |
Jason Monk | 9429513 | 2016-01-12 11:27:02 -0500 | [diff] [blame] | 42 | import com.android.systemui.R; |
Jason Monk | 5db8a41 | 2015-10-21 15:16:23 -0700 | [diff] [blame] | 43 | import com.android.systemui.qs.QSTile; |
Jason Monk | bbadff8 | 2015-11-06 15:47:26 -0500 | [diff] [blame] | 44 | import com.android.systemui.statusbar.phone.QSTileHost; |
Jason Monk | 5db8a41 | 2015-10-21 15:16:23 -0700 | [diff] [blame] | 45 | |
| 46 | public class CustomTile extends QSTile<QSTile.State> { |
| 47 | public static final String PREFIX = "custom("; |
| 48 | |
Jason Monk | 8f7f318 | 2015-11-18 16:35:14 -0500 | [diff] [blame] | 49 | private static final boolean DEBUG = false; |
| 50 | |
Jason Monk | bbadff8 | 2015-11-06 15:47:26 -0500 | [diff] [blame] | 51 | // We don't want to thrash binding and unbinding if the user opens and closes the panel a lot. |
| 52 | // So instead we have a period of waiting. |
| 53 | private static final long UNBIND_DELAY = 30000; |
Jason Monk | 5db8a41 | 2015-10-21 15:16:23 -0700 | [diff] [blame] | 54 | |
Jason Monk | bbadff8 | 2015-11-06 15:47:26 -0500 | [diff] [blame] | 55 | private final ComponentName mComponent; |
| 56 | private final Tile mTile; |
Jason Monk | 8f7f318 | 2015-11-18 16:35:14 -0500 | [diff] [blame] | 57 | private final IWindowManager mWindowManager; |
| 58 | private final IBinder mToken = new Binder(); |
Jason Monk | d5a204f | 2015-12-21 08:50:01 -0500 | [diff] [blame] | 59 | private final IQSTileService mService; |
| 60 | private final TileServiceManager mServiceManager; |
Jason Monk | bbadff8 | 2015-11-06 15:47:26 -0500 | [diff] [blame] | 61 | |
Jason Monk | bbadff8 | 2015-11-06 15:47:26 -0500 | [diff] [blame] | 62 | private boolean mListening; |
| 63 | private boolean mBound; |
Jason Monk | 8f7f318 | 2015-11-18 16:35:14 -0500 | [diff] [blame] | 64 | private boolean mIsTokenGranted; |
| 65 | private boolean mIsShowingDialog; |
Jason Monk | bbadff8 | 2015-11-06 15:47:26 -0500 | [diff] [blame] | 66 | |
| 67 | private CustomTile(QSTileHost host, String action) { |
Jason Monk | 5db8a41 | 2015-10-21 15:16:23 -0700 | [diff] [blame] | 68 | super(host); |
Jason Monk | 8f7f318 | 2015-11-18 16:35:14 -0500 | [diff] [blame] | 69 | mWindowManager = WindowManagerGlobal.getWindowManagerService(); |
Jason Monk | 5db8a41 | 2015-10-21 15:16:23 -0700 | [diff] [blame] | 70 | mComponent = ComponentName.unflattenFromString(action); |
Jason Monk | d5a204f | 2015-12-21 08:50:01 -0500 | [diff] [blame] | 71 | mServiceManager = host.getTileServices().getTileWrapper(this); |
| 72 | mService = mServiceManager.getTileService(); |
Jason Monk | fe8f682 | 2015-12-21 15:12:01 -0500 | [diff] [blame] | 73 | mTile = new Tile(mComponent); |
Jason Monk | bbadff8 | 2015-11-06 15:47:26 -0500 | [diff] [blame] | 74 | try { |
| 75 | PackageManager pm = mContext.getPackageManager(); |
| 76 | ServiceInfo info = pm.getServiceInfo(mComponent, 0); |
| 77 | mTile.setIcon(android.graphics.drawable.Icon |
| 78 | .createWithResource(mComponent.getPackageName(), info.icon)); |
| 79 | mTile.setLabel(info.loadLabel(pm)); |
| 80 | } catch (Exception e) { |
| 81 | } |
Jason Monk | fe8f682 | 2015-12-21 15:12:01 -0500 | [diff] [blame] | 82 | try { |
| 83 | mService.setQSTile(mTile); |
| 84 | } catch (RemoteException e) { |
| 85 | // Called through wrapper, won't happen here. |
| 86 | } |
Jason Monk | 5db8a41 | 2015-10-21 15:16:23 -0700 | [diff] [blame] | 87 | } |
| 88 | |
Jason Monk | bbadff8 | 2015-11-06 15:47:26 -0500 | [diff] [blame] | 89 | public ComponentName getComponent() { |
| 90 | return mComponent; |
| 91 | } |
| 92 | |
| 93 | public Tile getQsTile() { |
| 94 | return mTile; |
| 95 | } |
| 96 | |
| 97 | public void updateState(Tile tile) { |
Jason Monk | bbadff8 | 2015-11-06 15:47:26 -0500 | [diff] [blame] | 98 | mTile.setIcon(tile.getIcon()); |
| 99 | mTile.setLabel(tile.getLabel()); |
| 100 | mTile.setContentDescription(tile.getContentDescription()); |
Jason Monk | 9429513 | 2016-01-12 11:27:02 -0500 | [diff] [blame] | 101 | mTile.setState(tile.getState()); |
Jason Monk | 5db8a41 | 2015-10-21 15:16:23 -0700 | [diff] [blame] | 102 | } |
| 103 | |
Jason Monk | 8f7f318 | 2015-11-18 16:35:14 -0500 | [diff] [blame] | 104 | public void onDialogShown() { |
| 105 | mIsShowingDialog = true; |
| 106 | } |
| 107 | |
Jason Monk | 34a5cef | 2016-01-29 11:28:44 -0500 | [diff] [blame] | 108 | public void onDialogHidden() { |
| 109 | mIsShowingDialog = false; |
| 110 | try { |
| 111 | if (DEBUG) Log.d(TAG, "Removing token"); |
| 112 | mWindowManager.removeWindowToken(mToken); |
| 113 | } catch (RemoteException e) { |
| 114 | } |
| 115 | } |
| 116 | |
Jason Monk | 5db8a41 | 2015-10-21 15:16:23 -0700 | [diff] [blame] | 117 | @Override |
| 118 | public void setListening(boolean listening) { |
Jason Monk | bbadff8 | 2015-11-06 15:47:26 -0500 | [diff] [blame] | 119 | if (mListening == listening) return; |
| 120 | mListening = listening; |
Jason Monk | d5a204f | 2015-12-21 08:50:01 -0500 | [diff] [blame] | 121 | try { |
| 122 | if (listening) { |
Jason Monk | fe8f682 | 2015-12-21 15:12:01 -0500 | [diff] [blame] | 123 | if (mServiceManager.getType() == TileService.TILE_MODE_PASSIVE) { |
| 124 | mServiceManager.setBindRequested(true); |
| 125 | mService.onStartListening(); |
| 126 | } |
Jason Monk | dc35dcb | 2015-12-04 16:36:15 -0500 | [diff] [blame] | 127 | } else { |
Jason Monk | bbadff8 | 2015-11-06 15:47:26 -0500 | [diff] [blame] | 128 | mService.onStopListening(); |
Jason Monk | d5a204f | 2015-12-21 08:50:01 -0500 | [diff] [blame] | 129 | if (mIsTokenGranted && !mIsShowingDialog) { |
| 130 | try { |
| 131 | if (DEBUG) Log.d(TAG, "Removing token"); |
| 132 | mWindowManager.removeWindowToken(mToken); |
| 133 | } catch (RemoteException e) { |
| 134 | } |
| 135 | mIsTokenGranted = false; |
Jason Monk | 8f7f318 | 2015-11-18 16:35:14 -0500 | [diff] [blame] | 136 | } |
Jason Monk | d5a204f | 2015-12-21 08:50:01 -0500 | [diff] [blame] | 137 | mIsShowingDialog = false; |
| 138 | mServiceManager.setBindRequested(false); |
Jason Monk | 8f7f318 | 2015-11-18 16:35:14 -0500 | [diff] [blame] | 139 | } |
Jason Monk | d5a204f | 2015-12-21 08:50:01 -0500 | [diff] [blame] | 140 | } catch (RemoteException e) { |
| 141 | // Called through wrapper, won't happen here. |
Jason Monk | bbadff8 | 2015-11-06 15:47:26 -0500 | [diff] [blame] | 142 | } |
| 143 | } |
Jason Monk | 8f7f318 | 2015-11-18 16:35:14 -0500 | [diff] [blame] | 144 | |
Jason Monk | bbadff8 | 2015-11-06 15:47:26 -0500 | [diff] [blame] | 145 | @Override |
| 146 | protected void handleDestroy() { |
| 147 | super.handleDestroy(); |
Jason Monk | 8f7f318 | 2015-11-18 16:35:14 -0500 | [diff] [blame] | 148 | if (mIsTokenGranted) { |
| 149 | try { |
| 150 | if (DEBUG) Log.d(TAG, "Removing token"); |
| 151 | mWindowManager.removeWindowToken(mToken); |
| 152 | } catch (RemoteException e) { |
| 153 | } |
| 154 | } |
Jason Monk | 66c89c1 | 2016-01-06 08:51:26 -0500 | [diff] [blame] | 155 | mHost.getTileServices().freeService(this, mServiceManager); |
Jason Monk | 5db8a41 | 2015-10-21 15:16:23 -0700 | [diff] [blame] | 156 | } |
| 157 | |
| 158 | @Override |
Jason Monk | 62b63a0 | 2016-02-02 15:15:31 -0500 | [diff] [blame] | 159 | public State newTileState() { |
Jason Monk | 5db8a41 | 2015-10-21 15:16:23 -0700 | [diff] [blame] | 160 | return new State(); |
| 161 | } |
| 162 | |
| 163 | @Override |
Jason Monk | 76c67aa | 2016-02-19 14:49:42 -0500 | [diff] [blame] | 164 | public Intent getLongClickIntent() { |
Jason Monk | 724214a | 2016-02-19 16:43:00 -0500 | [diff] [blame] | 165 | Intent i = new Intent(TileService.ACTION_QS_TILE_PREFERENCES); |
| 166 | i.setPackage(mComponent.getPackageName()); |
| 167 | i = resolveIntent(i); |
| 168 | if (i != null) { |
| 169 | return i; |
| 170 | } |
Jason Monk | 76c67aa | 2016-02-19 14:49:42 -0500 | [diff] [blame] | 171 | return new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS).setData( |
| 172 | Uri.fromParts("package", mComponent.getPackageName(), null)); |
Jason Monk | 5db8a41 | 2015-10-21 15:16:23 -0700 | [diff] [blame] | 173 | } |
| 174 | |
Jason Monk | 724214a | 2016-02-19 16:43:00 -0500 | [diff] [blame] | 175 | private Intent resolveIntent(Intent i) { |
| 176 | ResolveInfo result = mContext.getPackageManager().resolveActivityAsUser(i, 0, |
| 177 | ActivityManager.getCurrentUser()); |
| 178 | return result != null ? new Intent(TileService.ACTION_QS_TILE_PREFERENCES) |
| 179 | .setClassName(result.activityInfo.packageName, result.activityInfo.name) : null; |
| 180 | } |
| 181 | |
Jason Monk | 5db8a41 | 2015-10-21 15:16:23 -0700 | [diff] [blame] | 182 | @Override |
| 183 | protected void handleClick() { |
Jason Monk | 9429513 | 2016-01-12 11:27:02 -0500 | [diff] [blame] | 184 | if (mTile.getState() == Tile.STATE_UNAVAILABLE) { |
| 185 | return; |
| 186 | } |
Jason Monk | fe8f682 | 2015-12-21 15:12:01 -0500 | [diff] [blame] | 187 | try { |
| 188 | if (DEBUG) Log.d(TAG, "Adding token"); |
| 189 | mWindowManager.addWindowToken(mToken, WindowManager.LayoutParams.TYPE_QS_DIALOG); |
| 190 | mIsTokenGranted = true; |
| 191 | } catch (RemoteException e) { |
| 192 | } |
| 193 | try { |
| 194 | if (mServiceManager.getType() == TileService.TILE_MODE_ACTIVE) { |
| 195 | mServiceManager.setBindRequested(true); |
| 196 | mService.onStartListening(); |
Jason Monk | 8f7f318 | 2015-11-18 16:35:14 -0500 | [diff] [blame] | 197 | } |
Jason Monk | fe8f682 | 2015-12-21 15:12:01 -0500 | [diff] [blame] | 198 | mService.onClick(mToken); |
| 199 | } catch (RemoteException e) { |
| 200 | // Called through wrapper, won't happen here. |
Jason Monk | bbadff8 | 2015-11-06 15:47:26 -0500 | [diff] [blame] | 201 | } |
Jason Monk | 5db8a41 | 2015-10-21 15:16:23 -0700 | [diff] [blame] | 202 | MetricsLogger.action(mContext, getMetricsCategory(), mComponent.getPackageName()); |
| 203 | } |
| 204 | |
| 205 | @Override |
Jason Monk | 5db8a41 | 2015-10-21 15:16:23 -0700 | [diff] [blame] | 206 | protected void handleUpdateState(State state, Object arg) { |
Jason Monk | 068cb8b | 2015-12-02 11:30:36 -0500 | [diff] [blame] | 207 | Drawable drawable = mTile.getIcon().loadDrawable(mContext); |
Jason Monk | 9429513 | 2016-01-12 11:27:02 -0500 | [diff] [blame] | 208 | int color = mContext.getColor(getColor(mTile.getState())); |
| 209 | drawable.setTint(color); |
Jason Monk | 068cb8b | 2015-12-02 11:30:36 -0500 | [diff] [blame] | 210 | state.icon = new DrawableIcon(drawable); |
Jason Monk | bbadff8 | 2015-11-06 15:47:26 -0500 | [diff] [blame] | 211 | state.label = mTile.getLabel(); |
Jason Monk | 9429513 | 2016-01-12 11:27:02 -0500 | [diff] [blame] | 212 | if (mTile.getState() == Tile.STATE_UNAVAILABLE) { |
| 213 | state.label = new SpannableStringBuilder().append(state.label, |
| 214 | new ForegroundColorSpan(color), |
| 215 | SpannableStringBuilder.SPAN_INCLUSIVE_INCLUSIVE); |
| 216 | } |
Jason Monk | bbadff8 | 2015-11-06 15:47:26 -0500 | [diff] [blame] | 217 | if (mTile.getContentDescription() != null) { |
| 218 | state.contentDescription = mTile.getContentDescription(); |
| 219 | } else { |
Jason Monk | 5db8a41 | 2015-10-21 15:16:23 -0700 | [diff] [blame] | 220 | state.contentDescription = state.label; |
Jason Monk | 5db8a41 | 2015-10-21 15:16:23 -0700 | [diff] [blame] | 221 | } |
| 222 | } |
| 223 | |
| 224 | @Override |
| 225 | public int getMetricsCategory() { |
Chris Wren | f6e9228b | 2016-01-26 18:04:35 -0500 | [diff] [blame] | 226 | return MetricsEvent.QS_CUSTOM; |
Jason Monk | 5db8a41 | 2015-10-21 15:16:23 -0700 | [diff] [blame] | 227 | } |
Jason Monk | bbadff8 | 2015-11-06 15:47:26 -0500 | [diff] [blame] | 228 | |
Jason Monk | 9429513 | 2016-01-12 11:27:02 -0500 | [diff] [blame] | 229 | public void startUnlockAndRun() { |
| 230 | mHost.startRunnableDismissingKeyguard(new Runnable() { |
| 231 | @Override |
| 232 | public void run() { |
| 233 | try { |
| 234 | mService.onUnlockComplete(); |
| 235 | } catch (RemoteException e) { |
| 236 | } |
| 237 | } |
| 238 | }); |
| 239 | } |
| 240 | |
| 241 | private static int getColor(int state) { |
| 242 | switch (state) { |
| 243 | case Tile.STATE_UNAVAILABLE: |
| 244 | return R.color.qs_tile_tint_unavailable; |
| 245 | case Tile.STATE_INACTIVE: |
| 246 | return R.color.qs_tile_tint_inactive; |
| 247 | case Tile.STATE_ACTIVE: |
| 248 | return R.color.qs_tile_tint_active; |
| 249 | } |
| 250 | return 0; |
| 251 | } |
| 252 | |
Jason Monk | 7e53f20 | 2016-01-28 10:40:20 -0500 | [diff] [blame] | 253 | public static String toSpec(ComponentName name) { |
| 254 | return PREFIX + name.flattenToShortString() + ")"; |
| 255 | } |
| 256 | |
Jason Monk | bbadff8 | 2015-11-06 15:47:26 -0500 | [diff] [blame] | 257 | public static ComponentName getComponentFromSpec(String spec) { |
| 258 | final String action = spec.substring(PREFIX.length(), spec.length() - 1); |
| 259 | if (action.isEmpty()) { |
| 260 | throw new IllegalArgumentException("Empty custom tile spec action"); |
| 261 | } |
| 262 | return ComponentName.unflattenFromString(action); |
| 263 | } |
| 264 | |
| 265 | public static QSTile<?> create(QSTileHost host, String spec) { |
| 266 | if (spec == null || !spec.startsWith(PREFIX) || !spec.endsWith(")")) { |
| 267 | throw new IllegalArgumentException("Bad custom tile spec: " + spec); |
| 268 | } |
| 269 | final String action = spec.substring(PREFIX.length(), spec.length() - 1); |
| 270 | if (action.isEmpty()) { |
| 271 | throw new IllegalArgumentException("Empty custom tile spec action"); |
| 272 | } |
| 273 | return new CustomTile(host, action); |
| 274 | } |
Jason Monk | 5db8a41 | 2015-10-21 15:16:23 -0700 | [diff] [blame] | 275 | } |