John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2014 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | package com.android.systemui.qs; |
| 18 | |
Sudheer Shanka | b6fc931 | 2016-01-27 19:59:03 +0000 | [diff] [blame] | 19 | import android.app.ActivityManager; |
Adrian Roos | 62692b2 | 2015-09-11 17:46:23 -0700 | [diff] [blame] | 20 | import android.app.PendingIntent; |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 21 | import android.content.Context; |
| 22 | import android.content.Intent; |
John Spurlock | 444eb2e | 2014-05-14 13:32:14 -0400 | [diff] [blame] | 23 | import android.graphics.drawable.Drawable; |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 24 | import android.os.Handler; |
| 25 | import android.os.Looper; |
| 26 | import android.os.Message; |
| 27 | import android.util.Log; |
John Spurlock | 2d69581 | 2014-10-30 13:25:21 -0400 | [diff] [blame] | 28 | import android.util.SparseArray; |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 29 | import android.view.View; |
| 30 | import android.view.ViewGroup; |
Sudheer Shanka | 1c7cda8 | 2015-12-31 14:46:02 +0000 | [diff] [blame] | 31 | |
| 32 | import com.android.settingslib.RestrictedLockUtils; |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 33 | import com.android.systemui.qs.QSTile.State; |
Jason Monk | 66c89c1 | 2016-01-06 08:51:26 -0500 | [diff] [blame] | 34 | import com.android.systemui.qs.external.TileServices; |
Jason Monk | c3f42c1 | 2016-02-05 12:33:13 -0500 | [diff] [blame] | 35 | import com.android.systemui.statusbar.phone.ManagedProfileController; |
Jason Monk | 66239fb | 2015-12-21 14:27:00 -0500 | [diff] [blame] | 36 | import com.android.systemui.statusbar.policy.BatteryController; |
| 37 | import com.android.systemui.statusbar.policy.BluetoothController; |
| 38 | import com.android.systemui.statusbar.policy.CastController; |
Jason Monk | 5dbd4aa | 2016-02-07 13:13:39 -0500 | [diff] [blame] | 39 | import com.android.systemui.statusbar.policy.NightModeController; |
Jason Monk | 66239fb | 2015-12-21 14:27:00 -0500 | [diff] [blame] | 40 | import com.android.systemui.statusbar.policy.FlashlightController; |
| 41 | import com.android.systemui.statusbar.policy.HotspotController; |
| 42 | import com.android.systemui.statusbar.policy.KeyguardMonitor; |
| 43 | import com.android.systemui.statusbar.policy.Listenable; |
| 44 | import com.android.systemui.statusbar.policy.LocationController; |
| 45 | import com.android.systemui.statusbar.policy.NetworkController; |
| 46 | import com.android.systemui.statusbar.policy.RotationLockController; |
| 47 | import com.android.systemui.statusbar.policy.UserInfoController; |
| 48 | import com.android.systemui.statusbar.policy.UserSwitcherController; |
| 49 | import com.android.systemui.statusbar.policy.ZenModeController; |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 50 | |
Jason Monk | ca894a0 | 2016-01-12 15:30:22 -0500 | [diff] [blame] | 51 | import java.util.ArrayList; |
John Spurlock | bceed06 | 2014-08-10 18:04:16 -0400 | [diff] [blame] | 52 | import java.util.Collection; |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 53 | import java.util.Objects; |
| 54 | |
Sudheer Shanka | 1c7cda8 | 2015-12-31 14:46:02 +0000 | [diff] [blame] | 55 | import static com.android.settingslib.RestrictedLockUtils.EnforcedAdmin; |
| 56 | |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 57 | /** |
| 58 | * Base quick-settings tile, extend this to create a new tile. |
| 59 | * |
| 60 | * State management done on a looper provided by the host. Tiles should update state in |
| 61 | * handleUpdateState. Callbacks affecting state should use refreshState to trigger another |
| 62 | * state update pass on tile looper. |
| 63 | */ |
John Spurlock | ccb6b9a | 2014-05-17 15:54:40 -0400 | [diff] [blame] | 64 | public abstract class QSTile<TState extends State> implements Listenable { |
Jason Monk | bbadff8 | 2015-11-06 15:47:26 -0500 | [diff] [blame] | 65 | protected final String TAG = "Tile." + getClass().getSimpleName(); |
| 66 | protected static final boolean DEBUG = Log.isLoggable("Tile", Log.DEBUG); |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 67 | |
| 68 | protected final Host mHost; |
| 69 | protected final Context mContext; |
| 70 | protected final H mHandler; |
John Spurlock | 76c43b9 | 2014-05-13 21:10:51 -0400 | [diff] [blame] | 71 | protected final Handler mUiHandler = new Handler(Looper.getMainLooper()); |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 72 | |
Jason Monk | ca894a0 | 2016-01-12 15:30:22 -0500 | [diff] [blame] | 73 | private final ArrayList<Callback> mCallbacks = new ArrayList<>(); |
Jason Monk | a758ba6 | 2015-07-14 12:29:28 -0400 | [diff] [blame] | 74 | protected TState mState = newTileState(); |
| 75 | private TState mTmpState = newTileState(); |
Selim Cinek | 4fda7b2 | 2014-08-18 22:07:25 +0200 | [diff] [blame] | 76 | private boolean mAnnounceNextStateChange; |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 77 | |
Jason Monk | bd6dbb0 | 2015-09-03 15:46:25 -0400 | [diff] [blame] | 78 | private String mTileSpec; |
| 79 | |
Jason Monk | 62b63a0 | 2016-02-02 15:15:31 -0500 | [diff] [blame] | 80 | public abstract TState newTileState(); |
Chris Wren | 9e7283f | 2015-05-08 17:23:47 -0400 | [diff] [blame] | 81 | abstract protected void handleClick(); |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 82 | abstract protected void handleUpdateState(TState state, Object arg); |
| 83 | |
Chris Wren | 457a21c | 2015-05-06 17:50:34 -0400 | [diff] [blame] | 84 | /** |
| 85 | * Declare the category of this tile. |
| 86 | * |
Chris Wren | f6e9228b | 2016-01-26 18:04:35 -0500 | [diff] [blame] | 87 | * Categories are defined in {@link com.android.internal.logging.MetricsProto.MetricsEvent} |
| 88 | * by editing frameworks/base/proto/src/metrics_constants.proto. |
Chris Wren | 457a21c | 2015-05-06 17:50:34 -0400 | [diff] [blame] | 89 | */ |
| 90 | abstract public int getMetricsCategory(); |
| 91 | |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 92 | protected QSTile(Host host) { |
| 93 | mHost = host; |
| 94 | mContext = host.getContext(); |
| 95 | mHandler = new H(host.getLooper()); |
| 96 | } |
Jason Monk | bd6dbb0 | 2015-09-03 15:46:25 -0400 | [diff] [blame] | 97 | |
| 98 | public String getTileSpec() { |
| 99 | return mTileSpec; |
| 100 | } |
| 101 | |
| 102 | public void setTileSpec(String tileSpec) { |
| 103 | mTileSpec = tileSpec; |
| 104 | } |
| 105 | |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 106 | public Host getHost() { |
| 107 | return mHost; |
| 108 | } |
| 109 | |
Jason Monk | dc35dcb | 2015-12-04 16:36:15 -0500 | [diff] [blame] | 110 | public QSIconView createTileView(Context context) { |
| 111 | return new QSIconView(context); |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 112 | } |
| 113 | |
John Spurlock | 7f8f22a | 2014-07-02 18:54:17 -0400 | [diff] [blame] | 114 | public DetailAdapter getDetailAdapter() { |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 115 | return null; // optional |
| 116 | } |
| 117 | |
Jason Monk | c3f42c1 | 2016-02-05 12:33:13 -0500 | [diff] [blame] | 118 | /** |
| 119 | * Is a startup check whether this device currently supports this tile. |
| 120 | * Should not be used to conditionally hide tiles. Only checked on tile |
| 121 | * creation or whether should be shown in edit screen. |
| 122 | */ |
| 123 | public boolean isAvailable() { |
| 124 | return true; |
| 125 | } |
| 126 | |
John Spurlock | 7f8f22a | 2014-07-02 18:54:17 -0400 | [diff] [blame] | 127 | public interface DetailAdapter { |
Jason Monk | c06fbb1 | 2016-01-08 14:12:18 -0500 | [diff] [blame] | 128 | CharSequence getTitle(); |
John Spurlock | 7f8f22a | 2014-07-02 18:54:17 -0400 | [diff] [blame] | 129 | Boolean getToggleState(); |
| 130 | View createDetailView(Context context, View convertView, ViewGroup parent); |
| 131 | Intent getSettingsIntent(); |
| 132 | void setToggleState(boolean state); |
Chris Wren | 457a21c | 2015-05-06 17:50:34 -0400 | [diff] [blame] | 133 | int getMetricsCategory(); |
John Spurlock | 7f8f22a | 2014-07-02 18:54:17 -0400 | [diff] [blame] | 134 | } |
| 135 | |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 136 | // safe to call from any thread |
| 137 | |
Jason Monk | ca894a0 | 2016-01-12 15:30:22 -0500 | [diff] [blame] | 138 | public void addCallback(Callback callback) { |
| 139 | mHandler.obtainMessage(H.ADD_CALLBACK, callback).sendToTarget(); |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 140 | } |
| 141 | |
Jason Monk | 9d02a43 | 2016-01-20 16:33:46 -0500 | [diff] [blame] | 142 | public void removeCallbacks() { |
| 143 | mHandler.sendEmptyMessage(H.REMOVE_CALLBACKS); |
| 144 | } |
| 145 | |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 146 | public void click() { |
| 147 | mHandler.sendEmptyMessage(H.CLICK); |
| 148 | } |
| 149 | |
| 150 | public void secondaryClick() { |
| 151 | mHandler.sendEmptyMessage(H.SECONDARY_CLICK); |
| 152 | } |
| 153 | |
John Spurlock | c247b8f | 2014-11-06 23:06:25 -0500 | [diff] [blame] | 154 | public void longClick() { |
| 155 | mHandler.sendEmptyMessage(H.LONG_CLICK); |
| 156 | } |
| 157 | |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 158 | public void showDetail(boolean show) { |
| 159 | mHandler.obtainMessage(H.SHOW_DETAIL, show ? 1 : 0, 0).sendToTarget(); |
| 160 | } |
| 161 | |
Jason Monk | 5db8a41 | 2015-10-21 15:16:23 -0700 | [diff] [blame] | 162 | public final void refreshState() { |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 163 | refreshState(null); |
| 164 | } |
| 165 | |
| 166 | protected final void refreshState(Object arg) { |
| 167 | mHandler.obtainMessage(H.REFRESH_STATE, arg).sendToTarget(); |
| 168 | } |
| 169 | |
Jason Monk | a758ba6 | 2015-07-14 12:29:28 -0400 | [diff] [blame] | 170 | public final void clearState() { |
| 171 | mHandler.sendEmptyMessage(H.CLEAR_STATE); |
| 172 | } |
| 173 | |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 174 | public void userSwitch(int newUserId) { |
Adrian Roos | 32d88e8 | 2014-09-24 17:08:22 +0200 | [diff] [blame] | 175 | mHandler.obtainMessage(H.USER_SWITCH, newUserId, 0).sendToTarget(); |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 176 | } |
| 177 | |
John Spurlock | 7f8f22a | 2014-07-02 18:54:17 -0400 | [diff] [blame] | 178 | public void fireToggleStateChanged(boolean state) { |
| 179 | mHandler.obtainMessage(H.TOGGLE_STATE_CHANGED, state ? 1 : 0, 0).sendToTarget(); |
| 180 | } |
| 181 | |
John Spurlock | 486b78e | 2014-07-07 08:37:56 -0400 | [diff] [blame] | 182 | public void fireScanStateChanged(boolean state) { |
| 183 | mHandler.obtainMessage(H.SCAN_STATE_CHANGED, state ? 1 : 0, 0).sendToTarget(); |
| 184 | } |
| 185 | |
John Spurlock | bceed06 | 2014-08-10 18:04:16 -0400 | [diff] [blame] | 186 | public void destroy() { |
| 187 | mHandler.sendEmptyMessage(H.DESTROY); |
| 188 | } |
| 189 | |
| 190 | public TState getState() { |
| 191 | return mState; |
| 192 | } |
| 193 | |
Jason Monk | 0d6a1c4 | 2015-04-20 16:38:51 -0400 | [diff] [blame] | 194 | public void setDetailListening(boolean listening) { |
| 195 | // optional |
| 196 | } |
| 197 | |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 198 | // call only on tile worker looper |
| 199 | |
Jason Monk | ca894a0 | 2016-01-12 15:30:22 -0500 | [diff] [blame] | 200 | private void handleAddCallback(Callback callback) { |
| 201 | mCallbacks.add(callback); |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 202 | handleRefreshState(null); |
| 203 | } |
| 204 | |
Jason Monk | 9d02a43 | 2016-01-20 16:33:46 -0500 | [diff] [blame] | 205 | private void handleRemoveCallbacks() { |
| 206 | mCallbacks.clear(); |
| 207 | } |
| 208 | |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 209 | protected void handleSecondaryClick() { |
Jason Monk | dc35dcb | 2015-12-04 16:36:15 -0500 | [diff] [blame] | 210 | // Default to normal click. |
| 211 | handleClick(); |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 212 | } |
| 213 | |
John Spurlock | c247b8f | 2014-11-06 23:06:25 -0500 | [diff] [blame] | 214 | protected void handleLongClick() { |
Jason Monk | 76c67aa | 2016-02-19 14:49:42 -0500 | [diff] [blame] | 215 | mHost.startActivityDismissingKeyguard(getLongClickIntent()); |
John Spurlock | c247b8f | 2014-11-06 23:06:25 -0500 | [diff] [blame] | 216 | } |
| 217 | |
Jason Monk | 76c67aa | 2016-02-19 14:49:42 -0500 | [diff] [blame] | 218 | public abstract Intent getLongClickIntent(); |
| 219 | |
Jason Monk | a758ba6 | 2015-07-14 12:29:28 -0400 | [diff] [blame] | 220 | protected void handleClearState() { |
| 221 | mTmpState = newTileState(); |
| 222 | mState = newTileState(); |
| 223 | } |
| 224 | |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 225 | protected void handleRefreshState(Object arg) { |
| 226 | handleUpdateState(mTmpState, arg); |
| 227 | final boolean changed = mTmpState.copyTo(mState); |
| 228 | if (changed) { |
| 229 | handleStateChanged(); |
| 230 | } |
| 231 | } |
| 232 | |
| 233 | private void handleStateChanged() { |
Selim Cinek | 4fda7b2 | 2014-08-18 22:07:25 +0200 | [diff] [blame] | 234 | boolean delayAnnouncement = shouldAnnouncementBeDelayed(); |
Jason Monk | ca894a0 | 2016-01-12 15:30:22 -0500 | [diff] [blame] | 235 | if (mCallbacks.size() != 0) { |
| 236 | for (int i = 0; i < mCallbacks.size(); i++) { |
| 237 | mCallbacks.get(i).onStateChanged(mState); |
| 238 | } |
Selim Cinek | 4fda7b2 | 2014-08-18 22:07:25 +0200 | [diff] [blame] | 239 | if (mAnnounceNextStateChange && !delayAnnouncement) { |
| 240 | String announcement = composeChangeAnnouncement(); |
| 241 | if (announcement != null) { |
Jason Monk | ca894a0 | 2016-01-12 15:30:22 -0500 | [diff] [blame] | 242 | mCallbacks.get(0).onAnnouncementRequested(announcement); |
Selim Cinek | 4fda7b2 | 2014-08-18 22:07:25 +0200 | [diff] [blame] | 243 | } |
| 244 | } |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 245 | } |
Selim Cinek | 4fda7b2 | 2014-08-18 22:07:25 +0200 | [diff] [blame] | 246 | mAnnounceNextStateChange = mAnnounceNextStateChange && delayAnnouncement; |
| 247 | } |
| 248 | |
| 249 | protected boolean shouldAnnouncementBeDelayed() { |
| 250 | return false; |
| 251 | } |
| 252 | |
| 253 | protected String composeChangeAnnouncement() { |
| 254 | return null; |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 255 | } |
| 256 | |
| 257 | private void handleShowDetail(boolean show) { |
Jason Monk | ca894a0 | 2016-01-12 15:30:22 -0500 | [diff] [blame] | 258 | for (int i = 0; i < mCallbacks.size(); i++) { |
| 259 | mCallbacks.get(i).onShowDetail(show); |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 260 | } |
| 261 | } |
| 262 | |
John Spurlock | 7f8f22a | 2014-07-02 18:54:17 -0400 | [diff] [blame] | 263 | private void handleToggleStateChanged(boolean state) { |
Jason Monk | ca894a0 | 2016-01-12 15:30:22 -0500 | [diff] [blame] | 264 | for (int i = 0; i < mCallbacks.size(); i++) { |
| 265 | mCallbacks.get(i).onToggleStateChanged(state); |
John Spurlock | 7f8f22a | 2014-07-02 18:54:17 -0400 | [diff] [blame] | 266 | } |
| 267 | } |
| 268 | |
John Spurlock | 486b78e | 2014-07-07 08:37:56 -0400 | [diff] [blame] | 269 | private void handleScanStateChanged(boolean state) { |
Jason Monk | ca894a0 | 2016-01-12 15:30:22 -0500 | [diff] [blame] | 270 | for (int i = 0; i < mCallbacks.size(); i++) { |
| 271 | mCallbacks.get(i).onScanStateChanged(state); |
John Spurlock | 486b78e | 2014-07-07 08:37:56 -0400 | [diff] [blame] | 272 | } |
| 273 | } |
| 274 | |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 275 | protected void handleUserSwitch(int newUserId) { |
| 276 | handleRefreshState(null); |
| 277 | } |
| 278 | |
John Spurlock | bceed06 | 2014-08-10 18:04:16 -0400 | [diff] [blame] | 279 | protected void handleDestroy() { |
| 280 | setListening(false); |
Jason Monk | ca894a0 | 2016-01-12 15:30:22 -0500 | [diff] [blame] | 281 | mCallbacks.clear(); |
John Spurlock | bceed06 | 2014-08-10 18:04:16 -0400 | [diff] [blame] | 282 | } |
| 283 | |
Sudheer Shanka | a8fbbb3 | 2016-02-11 17:17:57 +0000 | [diff] [blame] | 284 | protected void checkIfRestrictionEnforcedByAdminOnly(State state, String userRestriction) { |
Sudheer Shanka | 1c7cda8 | 2015-12-31 14:46:02 +0000 | [diff] [blame] | 285 | EnforcedAdmin admin = RestrictedLockUtils.checkIfRestrictionEnforced(mContext, |
Sudheer Shanka | b6fc931 | 2016-01-27 19:59:03 +0000 | [diff] [blame] | 286 | userRestriction, ActivityManager.getCurrentUser()); |
Sudheer Shanka | a8fbbb3 | 2016-02-11 17:17:57 +0000 | [diff] [blame] | 287 | if (admin != null && !RestrictedLockUtils.hasBaseUserRestriction(mContext, |
| 288 | userRestriction, ActivityManager.getCurrentUser())) { |
Sudheer Shanka | 1c7cda8 | 2015-12-31 14:46:02 +0000 | [diff] [blame] | 289 | state.disabledByPolicy = true; |
| 290 | state.enforcedAdmin = admin; |
| 291 | } else { |
| 292 | state.disabledByPolicy = false; |
| 293 | state.enforcedAdmin = null; |
| 294 | } |
| 295 | } |
| 296 | |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 297 | protected final class H extends Handler { |
Jason Monk | ca894a0 | 2016-01-12 15:30:22 -0500 | [diff] [blame] | 298 | private static final int ADD_CALLBACK = 1; |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 299 | private static final int CLICK = 2; |
| 300 | private static final int SECONDARY_CLICK = 3; |
John Spurlock | c247b8f | 2014-11-06 23:06:25 -0500 | [diff] [blame] | 301 | private static final int LONG_CLICK = 4; |
| 302 | private static final int REFRESH_STATE = 5; |
| 303 | private static final int SHOW_DETAIL = 6; |
| 304 | private static final int USER_SWITCH = 7; |
| 305 | private static final int TOGGLE_STATE_CHANGED = 8; |
| 306 | private static final int SCAN_STATE_CHANGED = 9; |
| 307 | private static final int DESTROY = 10; |
Jason Monk | a758ba6 | 2015-07-14 12:29:28 -0400 | [diff] [blame] | 308 | private static final int CLEAR_STATE = 11; |
Jason Monk | 9d02a43 | 2016-01-20 16:33:46 -0500 | [diff] [blame] | 309 | private static final int REMOVE_CALLBACKS = 12; |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 310 | |
| 311 | private H(Looper looper) { |
| 312 | super(looper); |
| 313 | } |
| 314 | |
| 315 | @Override |
| 316 | public void handleMessage(Message msg) { |
| 317 | String name = null; |
| 318 | try { |
Jason Monk | ca894a0 | 2016-01-12 15:30:22 -0500 | [diff] [blame] | 319 | if (msg.what == ADD_CALLBACK) { |
| 320 | name = "handleAddCallback"; |
Jason Monk | 9d02a43 | 2016-01-20 16:33:46 -0500 | [diff] [blame] | 321 | handleAddCallback((QSTile.Callback) msg.obj); |
| 322 | } else if (msg.what == REMOVE_CALLBACKS) { |
| 323 | name = "handleRemoveCallbacks"; |
| 324 | handleRemoveCallbacks(); |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 325 | } else if (msg.what == CLICK) { |
| 326 | name = "handleClick"; |
Sudheer Shanka | 1c7cda8 | 2015-12-31 14:46:02 +0000 | [diff] [blame] | 327 | if (mState.disabledByPolicy) { |
| 328 | Intent intent = RestrictedLockUtils.getShowAdminSupportDetailsIntent( |
| 329 | mContext, mState.enforcedAdmin); |
| 330 | mHost.startActivityDismissingKeyguard(intent); |
| 331 | } else { |
| 332 | mAnnounceNextStateChange = true; |
| 333 | handleClick(); |
| 334 | } |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 335 | } else if (msg.what == SECONDARY_CLICK) { |
| 336 | name = "handleSecondaryClick"; |
| 337 | handleSecondaryClick(); |
John Spurlock | c247b8f | 2014-11-06 23:06:25 -0500 | [diff] [blame] | 338 | } else if (msg.what == LONG_CLICK) { |
| 339 | name = "handleLongClick"; |
| 340 | handleLongClick(); |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 341 | } else if (msg.what == REFRESH_STATE) { |
| 342 | name = "handleRefreshState"; |
| 343 | handleRefreshState(msg.obj); |
| 344 | } else if (msg.what == SHOW_DETAIL) { |
| 345 | name = "handleShowDetail"; |
| 346 | handleShowDetail(msg.arg1 != 0); |
| 347 | } else if (msg.what == USER_SWITCH) { |
| 348 | name = "handleUserSwitch"; |
| 349 | handleUserSwitch(msg.arg1); |
John Spurlock | 7f8f22a | 2014-07-02 18:54:17 -0400 | [diff] [blame] | 350 | } else if (msg.what == TOGGLE_STATE_CHANGED) { |
| 351 | name = "handleToggleStateChanged"; |
| 352 | handleToggleStateChanged(msg.arg1 != 0); |
John Spurlock | 486b78e | 2014-07-07 08:37:56 -0400 | [diff] [blame] | 353 | } else if (msg.what == SCAN_STATE_CHANGED) { |
| 354 | name = "handleScanStateChanged"; |
| 355 | handleScanStateChanged(msg.arg1 != 0); |
John Spurlock | bceed06 | 2014-08-10 18:04:16 -0400 | [diff] [blame] | 356 | } else if (msg.what == DESTROY) { |
| 357 | name = "handleDestroy"; |
| 358 | handleDestroy(); |
Jason Monk | a758ba6 | 2015-07-14 12:29:28 -0400 | [diff] [blame] | 359 | } else if (msg.what == CLEAR_STATE) { |
| 360 | name = "handleClearState"; |
| 361 | handleClearState(); |
John Spurlock | bceed06 | 2014-08-10 18:04:16 -0400 | [diff] [blame] | 362 | } else { |
| 363 | throw new IllegalArgumentException("Unknown msg: " + msg.what); |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 364 | } |
| 365 | } catch (Throwable t) { |
| 366 | final String error = "Error in " + name; |
| 367 | Log.w(TAG, error, t); |
| 368 | mHost.warn(error, t); |
| 369 | } |
| 370 | } |
| 371 | } |
| 372 | |
| 373 | public interface Callback { |
| 374 | void onStateChanged(State state); |
| 375 | void onShowDetail(boolean show); |
John Spurlock | 7f8f22a | 2014-07-02 18:54:17 -0400 | [diff] [blame] | 376 | void onToggleStateChanged(boolean state); |
John Spurlock | 486b78e | 2014-07-07 08:37:56 -0400 | [diff] [blame] | 377 | void onScanStateChanged(boolean state); |
Selim Cinek | 4fda7b2 | 2014-08-18 22:07:25 +0200 | [diff] [blame] | 378 | void onAnnouncementRequested(CharSequence announcement); |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 379 | } |
| 380 | |
| 381 | public interface Host { |
Jason Monk | ee43cdf | 2015-06-19 14:20:46 -0400 | [diff] [blame] | 382 | void startActivityDismissingKeyguard(Intent intent); |
Adrian Roos | 62692b2 | 2015-09-11 17:46:23 -0700 | [diff] [blame] | 383 | void startActivityDismissingKeyguard(PendingIntent intent); |
Jason Monk | ba2318e | 2015-12-08 09:04:23 -0500 | [diff] [blame] | 384 | void startRunnableDismissingKeyguard(Runnable runnable); |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 385 | void warn(String message, Throwable t); |
| 386 | void collapsePanels(); |
Jason Monk | ca894a0 | 2016-01-12 15:30:22 -0500 | [diff] [blame] | 387 | void animateExpandQS(); |
Jason Monk | ba2318e | 2015-12-08 09:04:23 -0500 | [diff] [blame] | 388 | void openPanels(); |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 389 | Looper getLooper(); |
| 390 | Context getContext(); |
John Spurlock | bceed06 | 2014-08-10 18:04:16 -0400 | [diff] [blame] | 391 | Collection<QSTile<?>> getTiles(); |
Jason Monk | bbadff8 | 2015-11-06 15:47:26 -0500 | [diff] [blame] | 392 | void addCallback(Callback callback); |
Jason Monk | 46dbfb4 | 2016-02-25 14:59:20 -0500 | [diff] [blame] | 393 | void removeCallback(Callback callback); |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 394 | BluetoothController getBluetoothController(); |
| 395 | LocationController getLocationController(); |
| 396 | RotationLockController getRotationLockController(); |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 397 | NetworkController getNetworkController(); |
| 398 | ZenModeController getZenModeController(); |
Jason Monk | 51e4dc0 | 2014-07-22 12:00:47 -0400 | [diff] [blame] | 399 | HotspotController getHotspotController(); |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 400 | CastController getCastController(); |
Adrian Roos | b83777b | 2014-06-30 15:11:53 +0200 | [diff] [blame] | 401 | FlashlightController getFlashlightController(); |
John Spurlock | 657c62c | 2014-07-22 12:18:09 -0400 | [diff] [blame] | 402 | KeyguardMonitor getKeyguardMonitor(); |
Jason Monk | abe1974 | 2015-09-29 09:47:06 -0400 | [diff] [blame] | 403 | UserSwitcherController getUserSwitcherController(); |
| 404 | UserInfoController getUserInfoController(); |
| 405 | BatteryController getBatteryController(); |
Jason Monk | 66c89c1 | 2016-01-06 08:51:26 -0500 | [diff] [blame] | 406 | TileServices getTileServices(); |
Jason Monk | 5dbd4aa | 2016-02-07 13:13:39 -0500 | [diff] [blame] | 407 | NightModeController getNightModeController(); |
Jason Monk | ba2318e | 2015-12-08 09:04:23 -0500 | [diff] [blame] | 408 | void removeTile(String tileSpec); |
Jason Monk | c3f42c1 | 2016-02-05 12:33:13 -0500 | [diff] [blame] | 409 | ManagedProfileController getManagedProfileController(); |
John Spurlock | bceed06 | 2014-08-10 18:04:16 -0400 | [diff] [blame] | 410 | |
Jason Monk | 66c89c1 | 2016-01-06 08:51:26 -0500 | [diff] [blame] | 411 | |
John Spurlock | bceed06 | 2014-08-10 18:04:16 -0400 | [diff] [blame] | 412 | public interface Callback { |
| 413 | void onTilesChanged(); |
| 414 | } |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 415 | } |
| 416 | |
John Spurlock | 2d69581 | 2014-10-30 13:25:21 -0400 | [diff] [blame] | 417 | public static abstract class Icon { |
| 418 | abstract public Drawable getDrawable(Context context); |
| 419 | |
Jason Monk | 1aec93f | 2016-03-01 09:39:30 -0500 | [diff] [blame] | 420 | public Drawable getInvisibleDrawable(Context context) { |
| 421 | return getDrawable(context); |
| 422 | } |
| 423 | |
John Spurlock | 2d69581 | 2014-10-30 13:25:21 -0400 | [diff] [blame] | 424 | @Override |
| 425 | public int hashCode() { |
| 426 | return Icon.class.hashCode(); |
| 427 | } |
Jason Monk | b53b6c5 | 2016-02-24 17:25:49 -0500 | [diff] [blame] | 428 | |
| 429 | public int getPadding() { |
| 430 | return 0; |
| 431 | } |
John Spurlock | 2d69581 | 2014-10-30 13:25:21 -0400 | [diff] [blame] | 432 | } |
| 433 | |
Jason Monk | 5db8a41 | 2015-10-21 15:16:23 -0700 | [diff] [blame] | 434 | public static class DrawableIcon extends Icon { |
| 435 | protected final Drawable mDrawable; |
| 436 | |
| 437 | public DrawableIcon(Drawable drawable) { |
| 438 | mDrawable = drawable; |
| 439 | } |
| 440 | |
| 441 | @Override |
| 442 | public Drawable getDrawable(Context context) { |
| 443 | return mDrawable; |
| 444 | } |
Jason Monk | 1aec93f | 2016-03-01 09:39:30 -0500 | [diff] [blame] | 445 | |
| 446 | @Override |
| 447 | public Drawable getInvisibleDrawable(Context context) { |
| 448 | return mDrawable; |
| 449 | } |
Jason Monk | 5db8a41 | 2015-10-21 15:16:23 -0700 | [diff] [blame] | 450 | } |
| 451 | |
John Spurlock | 2d69581 | 2014-10-30 13:25:21 -0400 | [diff] [blame] | 452 | public static class ResourceIcon extends Icon { |
| 453 | private static final SparseArray<Icon> ICONS = new SparseArray<Icon>(); |
| 454 | |
Andrew Flynn | a478d70 | 2015-04-14 23:33:45 -0400 | [diff] [blame] | 455 | protected final int mResId; |
John Spurlock | 2d69581 | 2014-10-30 13:25:21 -0400 | [diff] [blame] | 456 | |
| 457 | private ResourceIcon(int resId) { |
| 458 | mResId = resId; |
| 459 | } |
| 460 | |
| 461 | public static Icon get(int resId) { |
| 462 | Icon icon = ICONS.get(resId); |
| 463 | if (icon == null) { |
| 464 | icon = new ResourceIcon(resId); |
| 465 | ICONS.put(resId, icon); |
| 466 | } |
| 467 | return icon; |
| 468 | } |
| 469 | |
| 470 | @Override |
| 471 | public Drawable getDrawable(Context context) { |
Jason Monk | 66239fb | 2015-12-21 14:27:00 -0500 | [diff] [blame] | 472 | return context.getDrawable(mResId); |
John Spurlock | 2d69581 | 2014-10-30 13:25:21 -0400 | [diff] [blame] | 473 | } |
| 474 | |
| 475 | @Override |
Jason Monk | 1aec93f | 2016-03-01 09:39:30 -0500 | [diff] [blame] | 476 | public Drawable getInvisibleDrawable(Context context) { |
| 477 | return context.getDrawable(mResId); |
| 478 | } |
| 479 | |
| 480 | @Override |
John Spurlock | 2d69581 | 2014-10-30 13:25:21 -0400 | [diff] [blame] | 481 | public boolean equals(Object o) { |
| 482 | return o instanceof ResourceIcon && ((ResourceIcon) o).mResId == mResId; |
| 483 | } |
| 484 | |
| 485 | @Override |
| 486 | public String toString() { |
| 487 | return String.format("ResourceIcon[resId=0x%08x]", mResId); |
| 488 | } |
| 489 | } |
| 490 | |
| 491 | protected class AnimationIcon extends ResourceIcon { |
Jason Monk | 1aec93f | 2016-03-01 09:39:30 -0500 | [diff] [blame] | 492 | private final int mAnimatedResId; |
| 493 | |
| 494 | public AnimationIcon(int resId, int staticResId) { |
| 495 | super(staticResId); |
| 496 | mAnimatedResId = resId; |
John Spurlock | 2d69581 | 2014-10-30 13:25:21 -0400 | [diff] [blame] | 497 | } |
| 498 | |
John Spurlock | 2d69581 | 2014-10-30 13:25:21 -0400 | [diff] [blame] | 499 | @Override |
| 500 | public Drawable getDrawable(Context context) { |
| 501 | // workaround: get a clean state for every new AVD |
Jason Monk | 1aec93f | 2016-03-01 09:39:30 -0500 | [diff] [blame] | 502 | return context.getDrawable(mAnimatedResId).getConstantState().newDrawable(); |
John Spurlock | 2d69581 | 2014-10-30 13:25:21 -0400 | [diff] [blame] | 503 | } |
| 504 | } |
| 505 | |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 506 | public static class State { |
John Spurlock | 2d69581 | 2014-10-30 13:25:21 -0400 | [diff] [blame] | 507 | public Icon icon; |
Jason Monk | bbadff8 | 2015-11-06 15:47:26 -0500 | [diff] [blame] | 508 | public CharSequence label; |
| 509 | public CharSequence contentDescription; |
| 510 | public CharSequence dualLabelContentDescription; |
Selim Cinek | 06d3bca | 2014-08-26 17:29:20 +0200 | [diff] [blame] | 511 | public boolean autoMirrorDrawable = true; |
Sudheer Shanka | 1c7cda8 | 2015-12-31 14:46:02 +0000 | [diff] [blame] | 512 | public boolean disabledByPolicy; |
| 513 | public EnforcedAdmin enforcedAdmin; |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 514 | |
| 515 | public boolean copyTo(State other) { |
| 516 | if (other == null) throw new IllegalArgumentException(); |
| 517 | if (!other.getClass().equals(getClass())) throw new IllegalArgumentException(); |
Jason Monk | ba2318e | 2015-12-08 09:04:23 -0500 | [diff] [blame] | 518 | final boolean changed = !Objects.equals(other.icon, icon) |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 519 | || !Objects.equals(other.label, label) |
Selim Cinek | e5557a9 | 2014-08-15 19:59:23 +0200 | [diff] [blame] | 520 | || !Objects.equals(other.contentDescription, contentDescription) |
Selim Cinek | 06d3bca | 2014-08-26 17:29:20 +0200 | [diff] [blame] | 521 | || !Objects.equals(other.autoMirrorDrawable, autoMirrorDrawable) |
Selim Cinek | e5557a9 | 2014-08-15 19:59:23 +0200 | [diff] [blame] | 522 | || !Objects.equals(other.dualLabelContentDescription, |
Sudheer Shanka | 1c7cda8 | 2015-12-31 14:46:02 +0000 | [diff] [blame] | 523 | dualLabelContentDescription) |
| 524 | || !Objects.equals(other.disabledByPolicy, disabledByPolicy) |
| 525 | || !Objects.equals(other.enforcedAdmin, enforcedAdmin); |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 526 | other.icon = icon; |
| 527 | other.label = label; |
| 528 | other.contentDescription = contentDescription; |
Selim Cinek | e5557a9 | 2014-08-15 19:59:23 +0200 | [diff] [blame] | 529 | other.dualLabelContentDescription = dualLabelContentDescription; |
Selim Cinek | 06d3bca | 2014-08-26 17:29:20 +0200 | [diff] [blame] | 530 | other.autoMirrorDrawable = autoMirrorDrawable; |
Sudheer Shanka | 1c7cda8 | 2015-12-31 14:46:02 +0000 | [diff] [blame] | 531 | other.disabledByPolicy = disabledByPolicy; |
Sudheer Shanka | 1c09db2 | 2016-01-18 11:18:35 +0000 | [diff] [blame] | 532 | if (enforcedAdmin == null) { |
| 533 | other.enforcedAdmin = null; |
| 534 | } else if (other.enforcedAdmin == null) { |
| 535 | other.enforcedAdmin = new EnforcedAdmin(enforcedAdmin); |
| 536 | } else { |
| 537 | enforcedAdmin.copyTo(other.enforcedAdmin); |
| 538 | } |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 539 | return changed; |
| 540 | } |
| 541 | |
| 542 | @Override |
| 543 | public String toString() { |
| 544 | return toStringBuilder().toString(); |
| 545 | } |
| 546 | |
| 547 | protected StringBuilder toStringBuilder() { |
John Spurlock | 2d69581 | 2014-10-30 13:25:21 -0400 | [diff] [blame] | 548 | final StringBuilder sb = new StringBuilder(getClass().getSimpleName()).append('['); |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 549 | sb.append(",icon=").append(icon); |
| 550 | sb.append(",label=").append(label); |
| 551 | sb.append(",contentDescription=").append(contentDescription); |
Selim Cinek | e5557a9 | 2014-08-15 19:59:23 +0200 | [diff] [blame] | 552 | sb.append(",dualLabelContentDescription=").append(dualLabelContentDescription); |
Selim Cinek | 06d3bca | 2014-08-26 17:29:20 +0200 | [diff] [blame] | 553 | sb.append(",autoMirrorDrawable=").append(autoMirrorDrawable); |
Sudheer Shanka | 1c7cda8 | 2015-12-31 14:46:02 +0000 | [diff] [blame] | 554 | sb.append(",disabledByPolicy=").append(disabledByPolicy); |
| 555 | sb.append(",enforcedAdmin=").append(enforcedAdmin); |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 556 | return sb.append(']'); |
| 557 | } |
| 558 | } |
| 559 | |
| 560 | public static class BooleanState extends State { |
| 561 | public boolean value; |
| 562 | |
| 563 | @Override |
| 564 | public boolean copyTo(State other) { |
| 565 | final BooleanState o = (BooleanState) other; |
| 566 | final boolean changed = super.copyTo(other) || o.value != value; |
| 567 | o.value = value; |
| 568 | return changed; |
| 569 | } |
| 570 | |
| 571 | @Override |
| 572 | protected StringBuilder toStringBuilder() { |
| 573 | final StringBuilder rt = super.toStringBuilder(); |
| 574 | rt.insert(rt.length() - 1, ",value=" + value); |
| 575 | return rt; |
| 576 | } |
| 577 | } |
| 578 | |
| 579 | public static final class SignalState extends State { |
| 580 | public boolean enabled; |
| 581 | public boolean connected; |
| 582 | public boolean activityIn; |
| 583 | public boolean activityOut; |
| 584 | public int overlayIconId; |
John Spurlock | 899f439 | 2014-06-11 10:59:11 -0400 | [diff] [blame] | 585 | public boolean filter; |
Jorim Jaggi | c737b9b | 2014-09-08 23:57:20 +0200 | [diff] [blame] | 586 | public boolean isOverlayIconWide; |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 587 | |
| 588 | @Override |
| 589 | public boolean copyTo(State other) { |
| 590 | final SignalState o = (SignalState) other; |
| 591 | final boolean changed = o.enabled != enabled |
| 592 | || o.connected != connected || o.activityIn != activityIn |
| 593 | || o.activityOut != activityOut |
Jorim Jaggi | c737b9b | 2014-09-08 23:57:20 +0200 | [diff] [blame] | 594 | || o.overlayIconId != overlayIconId |
| 595 | || o.isOverlayIconWide != isOverlayIconWide; |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 596 | o.enabled = enabled; |
| 597 | o.connected = connected; |
| 598 | o.activityIn = activityIn; |
| 599 | o.activityOut = activityOut; |
| 600 | o.overlayIconId = overlayIconId; |
John Spurlock | 899f439 | 2014-06-11 10:59:11 -0400 | [diff] [blame] | 601 | o.filter = filter; |
Jorim Jaggi | c737b9b | 2014-09-08 23:57:20 +0200 | [diff] [blame] | 602 | o.isOverlayIconWide = isOverlayIconWide; |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 603 | return super.copyTo(other) || changed; |
| 604 | } |
| 605 | |
| 606 | @Override |
| 607 | protected StringBuilder toStringBuilder() { |
| 608 | final StringBuilder rt = super.toStringBuilder(); |
| 609 | rt.insert(rt.length() - 1, ",enabled=" + enabled); |
| 610 | rt.insert(rt.length() - 1, ",connected=" + connected); |
| 611 | rt.insert(rt.length() - 1, ",activityIn=" + activityIn); |
| 612 | rt.insert(rt.length() - 1, ",activityOut=" + activityOut); |
| 613 | rt.insert(rt.length() - 1, ",overlayIconId=" + overlayIconId); |
John Spurlock | 899f439 | 2014-06-11 10:59:11 -0400 | [diff] [blame] | 614 | rt.insert(rt.length() - 1, ",filter=" + filter); |
Jorim Jaggi | c737b9b | 2014-09-08 23:57:20 +0200 | [diff] [blame] | 615 | rt.insert(rt.length() - 1, ",wideOverlayIcon=" + isOverlayIconWide); |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 616 | return rt; |
| 617 | } |
| 618 | } |
| 619 | } |