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 | |
Jason Monk | 39c98e6 | 2016-03-16 09:18:35 -0400 | [diff] [blame] | 297 | public abstract CharSequence getTileLabel(); |
| 298 | |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 299 | protected final class H extends Handler { |
Jason Monk | ca894a0 | 2016-01-12 15:30:22 -0500 | [diff] [blame] | 300 | private static final int ADD_CALLBACK = 1; |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 301 | private static final int CLICK = 2; |
| 302 | private static final int SECONDARY_CLICK = 3; |
John Spurlock | c247b8f | 2014-11-06 23:06:25 -0500 | [diff] [blame] | 303 | private static final int LONG_CLICK = 4; |
| 304 | private static final int REFRESH_STATE = 5; |
| 305 | private static final int SHOW_DETAIL = 6; |
| 306 | private static final int USER_SWITCH = 7; |
| 307 | private static final int TOGGLE_STATE_CHANGED = 8; |
| 308 | private static final int SCAN_STATE_CHANGED = 9; |
| 309 | private static final int DESTROY = 10; |
Jason Monk | a758ba6 | 2015-07-14 12:29:28 -0400 | [diff] [blame] | 310 | private static final int CLEAR_STATE = 11; |
Jason Monk | 9d02a43 | 2016-01-20 16:33:46 -0500 | [diff] [blame] | 311 | private static final int REMOVE_CALLBACKS = 12; |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 312 | |
| 313 | private H(Looper looper) { |
| 314 | super(looper); |
| 315 | } |
| 316 | |
| 317 | @Override |
| 318 | public void handleMessage(Message msg) { |
| 319 | String name = null; |
| 320 | try { |
Jason Monk | ca894a0 | 2016-01-12 15:30:22 -0500 | [diff] [blame] | 321 | if (msg.what == ADD_CALLBACK) { |
| 322 | name = "handleAddCallback"; |
Jason Monk | 9d02a43 | 2016-01-20 16:33:46 -0500 | [diff] [blame] | 323 | handleAddCallback((QSTile.Callback) msg.obj); |
| 324 | } else if (msg.what == REMOVE_CALLBACKS) { |
| 325 | name = "handleRemoveCallbacks"; |
| 326 | handleRemoveCallbacks(); |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 327 | } else if (msg.what == CLICK) { |
| 328 | name = "handleClick"; |
Sudheer Shanka | 1c7cda8 | 2015-12-31 14:46:02 +0000 | [diff] [blame] | 329 | if (mState.disabledByPolicy) { |
| 330 | Intent intent = RestrictedLockUtils.getShowAdminSupportDetailsIntent( |
| 331 | mContext, mState.enforcedAdmin); |
| 332 | mHost.startActivityDismissingKeyguard(intent); |
| 333 | } else { |
| 334 | mAnnounceNextStateChange = true; |
| 335 | handleClick(); |
| 336 | } |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 337 | } else if (msg.what == SECONDARY_CLICK) { |
| 338 | name = "handleSecondaryClick"; |
| 339 | handleSecondaryClick(); |
John Spurlock | c247b8f | 2014-11-06 23:06:25 -0500 | [diff] [blame] | 340 | } else if (msg.what == LONG_CLICK) { |
| 341 | name = "handleLongClick"; |
| 342 | handleLongClick(); |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 343 | } else if (msg.what == REFRESH_STATE) { |
| 344 | name = "handleRefreshState"; |
| 345 | handleRefreshState(msg.obj); |
| 346 | } else if (msg.what == SHOW_DETAIL) { |
| 347 | name = "handleShowDetail"; |
| 348 | handleShowDetail(msg.arg1 != 0); |
| 349 | } else if (msg.what == USER_SWITCH) { |
| 350 | name = "handleUserSwitch"; |
| 351 | handleUserSwitch(msg.arg1); |
John Spurlock | 7f8f22a | 2014-07-02 18:54:17 -0400 | [diff] [blame] | 352 | } else if (msg.what == TOGGLE_STATE_CHANGED) { |
| 353 | name = "handleToggleStateChanged"; |
| 354 | handleToggleStateChanged(msg.arg1 != 0); |
John Spurlock | 486b78e | 2014-07-07 08:37:56 -0400 | [diff] [blame] | 355 | } else if (msg.what == SCAN_STATE_CHANGED) { |
| 356 | name = "handleScanStateChanged"; |
| 357 | handleScanStateChanged(msg.arg1 != 0); |
John Spurlock | bceed06 | 2014-08-10 18:04:16 -0400 | [diff] [blame] | 358 | } else if (msg.what == DESTROY) { |
| 359 | name = "handleDestroy"; |
| 360 | handleDestroy(); |
Jason Monk | a758ba6 | 2015-07-14 12:29:28 -0400 | [diff] [blame] | 361 | } else if (msg.what == CLEAR_STATE) { |
| 362 | name = "handleClearState"; |
| 363 | handleClearState(); |
John Spurlock | bceed06 | 2014-08-10 18:04:16 -0400 | [diff] [blame] | 364 | } else { |
| 365 | throw new IllegalArgumentException("Unknown msg: " + msg.what); |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 366 | } |
| 367 | } catch (Throwable t) { |
| 368 | final String error = "Error in " + name; |
| 369 | Log.w(TAG, error, t); |
| 370 | mHost.warn(error, t); |
| 371 | } |
| 372 | } |
| 373 | } |
| 374 | |
| 375 | public interface Callback { |
| 376 | void onStateChanged(State state); |
| 377 | void onShowDetail(boolean show); |
John Spurlock | 7f8f22a | 2014-07-02 18:54:17 -0400 | [diff] [blame] | 378 | void onToggleStateChanged(boolean state); |
John Spurlock | 486b78e | 2014-07-07 08:37:56 -0400 | [diff] [blame] | 379 | void onScanStateChanged(boolean state); |
Selim Cinek | 4fda7b2 | 2014-08-18 22:07:25 +0200 | [diff] [blame] | 380 | void onAnnouncementRequested(CharSequence announcement); |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 381 | } |
| 382 | |
| 383 | public interface Host { |
Jason Monk | ee43cdf | 2015-06-19 14:20:46 -0400 | [diff] [blame] | 384 | void startActivityDismissingKeyguard(Intent intent); |
Adrian Roos | 62692b2 | 2015-09-11 17:46:23 -0700 | [diff] [blame] | 385 | void startActivityDismissingKeyguard(PendingIntent intent); |
Jason Monk | ba2318e | 2015-12-08 09:04:23 -0500 | [diff] [blame] | 386 | void startRunnableDismissingKeyguard(Runnable runnable); |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 387 | void warn(String message, Throwable t); |
| 388 | void collapsePanels(); |
Jason Monk | ca894a0 | 2016-01-12 15:30:22 -0500 | [diff] [blame] | 389 | void animateExpandQS(); |
Jason Monk | ba2318e | 2015-12-08 09:04:23 -0500 | [diff] [blame] | 390 | void openPanels(); |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 391 | Looper getLooper(); |
| 392 | Context getContext(); |
John Spurlock | bceed06 | 2014-08-10 18:04:16 -0400 | [diff] [blame] | 393 | Collection<QSTile<?>> getTiles(); |
Jason Monk | bbadff8 | 2015-11-06 15:47:26 -0500 | [diff] [blame] | 394 | void addCallback(Callback callback); |
Jason Monk | 46dbfb4 | 2016-02-25 14:59:20 -0500 | [diff] [blame] | 395 | void removeCallback(Callback callback); |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 396 | BluetoothController getBluetoothController(); |
| 397 | LocationController getLocationController(); |
| 398 | RotationLockController getRotationLockController(); |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 399 | NetworkController getNetworkController(); |
| 400 | ZenModeController getZenModeController(); |
Jason Monk | 51e4dc0 | 2014-07-22 12:00:47 -0400 | [diff] [blame] | 401 | HotspotController getHotspotController(); |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 402 | CastController getCastController(); |
Adrian Roos | b83777b | 2014-06-30 15:11:53 +0200 | [diff] [blame] | 403 | FlashlightController getFlashlightController(); |
John Spurlock | 657c62c | 2014-07-22 12:18:09 -0400 | [diff] [blame] | 404 | KeyguardMonitor getKeyguardMonitor(); |
Jason Monk | abe1974 | 2015-09-29 09:47:06 -0400 | [diff] [blame] | 405 | UserSwitcherController getUserSwitcherController(); |
| 406 | UserInfoController getUserInfoController(); |
| 407 | BatteryController getBatteryController(); |
Jason Monk | 66c89c1 | 2016-01-06 08:51:26 -0500 | [diff] [blame] | 408 | TileServices getTileServices(); |
Jason Monk | 5dbd4aa | 2016-02-07 13:13:39 -0500 | [diff] [blame] | 409 | NightModeController getNightModeController(); |
Jason Monk | ba2318e | 2015-12-08 09:04:23 -0500 | [diff] [blame] | 410 | void removeTile(String tileSpec); |
Jason Monk | c3f42c1 | 2016-02-05 12:33:13 -0500 | [diff] [blame] | 411 | ManagedProfileController getManagedProfileController(); |
John Spurlock | bceed06 | 2014-08-10 18:04:16 -0400 | [diff] [blame] | 412 | |
Jason Monk | 66c89c1 | 2016-01-06 08:51:26 -0500 | [diff] [blame] | 413 | |
John Spurlock | bceed06 | 2014-08-10 18:04:16 -0400 | [diff] [blame] | 414 | public interface Callback { |
| 415 | void onTilesChanged(); |
| 416 | } |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 417 | } |
| 418 | |
John Spurlock | 2d69581 | 2014-10-30 13:25:21 -0400 | [diff] [blame] | 419 | public static abstract class Icon { |
| 420 | abstract public Drawable getDrawable(Context context); |
| 421 | |
Jason Monk | 1aec93f | 2016-03-01 09:39:30 -0500 | [diff] [blame] | 422 | public Drawable getInvisibleDrawable(Context context) { |
| 423 | return getDrawable(context); |
| 424 | } |
| 425 | |
John Spurlock | 2d69581 | 2014-10-30 13:25:21 -0400 | [diff] [blame] | 426 | @Override |
| 427 | public int hashCode() { |
| 428 | return Icon.class.hashCode(); |
| 429 | } |
Jason Monk | b53b6c5 | 2016-02-24 17:25:49 -0500 | [diff] [blame] | 430 | |
| 431 | public int getPadding() { |
| 432 | return 0; |
| 433 | } |
John Spurlock | 2d69581 | 2014-10-30 13:25:21 -0400 | [diff] [blame] | 434 | } |
| 435 | |
Jason Monk | 5db8a41 | 2015-10-21 15:16:23 -0700 | [diff] [blame] | 436 | public static class DrawableIcon extends Icon { |
| 437 | protected final Drawable mDrawable; |
| 438 | |
| 439 | public DrawableIcon(Drawable drawable) { |
| 440 | mDrawable = drawable; |
| 441 | } |
| 442 | |
| 443 | @Override |
| 444 | public Drawable getDrawable(Context context) { |
| 445 | return mDrawable; |
| 446 | } |
Jason Monk | 1aec93f | 2016-03-01 09:39:30 -0500 | [diff] [blame] | 447 | |
| 448 | @Override |
| 449 | public Drawable getInvisibleDrawable(Context context) { |
| 450 | return mDrawable; |
| 451 | } |
Jason Monk | 5db8a41 | 2015-10-21 15:16:23 -0700 | [diff] [blame] | 452 | } |
| 453 | |
John Spurlock | 2d69581 | 2014-10-30 13:25:21 -0400 | [diff] [blame] | 454 | public static class ResourceIcon extends Icon { |
| 455 | private static final SparseArray<Icon> ICONS = new SparseArray<Icon>(); |
| 456 | |
Andrew Flynn | a478d70 | 2015-04-14 23:33:45 -0400 | [diff] [blame] | 457 | protected final int mResId; |
John Spurlock | 2d69581 | 2014-10-30 13:25:21 -0400 | [diff] [blame] | 458 | |
| 459 | private ResourceIcon(int resId) { |
| 460 | mResId = resId; |
| 461 | } |
| 462 | |
| 463 | public static Icon get(int resId) { |
| 464 | Icon icon = ICONS.get(resId); |
| 465 | if (icon == null) { |
| 466 | icon = new ResourceIcon(resId); |
| 467 | ICONS.put(resId, icon); |
| 468 | } |
| 469 | return icon; |
| 470 | } |
| 471 | |
| 472 | @Override |
| 473 | public Drawable getDrawable(Context context) { |
Jason Monk | 66239fb | 2015-12-21 14:27:00 -0500 | [diff] [blame] | 474 | return context.getDrawable(mResId); |
John Spurlock | 2d69581 | 2014-10-30 13:25:21 -0400 | [diff] [blame] | 475 | } |
| 476 | |
| 477 | @Override |
Jason Monk | 1aec93f | 2016-03-01 09:39:30 -0500 | [diff] [blame] | 478 | public Drawable getInvisibleDrawable(Context context) { |
| 479 | return context.getDrawable(mResId); |
| 480 | } |
| 481 | |
| 482 | @Override |
John Spurlock | 2d69581 | 2014-10-30 13:25:21 -0400 | [diff] [blame] | 483 | public boolean equals(Object o) { |
| 484 | return o instanceof ResourceIcon && ((ResourceIcon) o).mResId == mResId; |
| 485 | } |
| 486 | |
| 487 | @Override |
| 488 | public String toString() { |
| 489 | return String.format("ResourceIcon[resId=0x%08x]", mResId); |
| 490 | } |
| 491 | } |
| 492 | |
| 493 | protected class AnimationIcon extends ResourceIcon { |
Jason Monk | 1aec93f | 2016-03-01 09:39:30 -0500 | [diff] [blame] | 494 | private final int mAnimatedResId; |
| 495 | |
| 496 | public AnimationIcon(int resId, int staticResId) { |
| 497 | super(staticResId); |
| 498 | mAnimatedResId = resId; |
John Spurlock | 2d69581 | 2014-10-30 13:25:21 -0400 | [diff] [blame] | 499 | } |
| 500 | |
John Spurlock | 2d69581 | 2014-10-30 13:25:21 -0400 | [diff] [blame] | 501 | @Override |
| 502 | public Drawable getDrawable(Context context) { |
| 503 | // workaround: get a clean state for every new AVD |
Jason Monk | 1aec93f | 2016-03-01 09:39:30 -0500 | [diff] [blame] | 504 | return context.getDrawable(mAnimatedResId).getConstantState().newDrawable(); |
John Spurlock | 2d69581 | 2014-10-30 13:25:21 -0400 | [diff] [blame] | 505 | } |
| 506 | } |
| 507 | |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 508 | public static class State { |
John Spurlock | 2d69581 | 2014-10-30 13:25:21 -0400 | [diff] [blame] | 509 | public Icon icon; |
Jason Monk | bbadff8 | 2015-11-06 15:47:26 -0500 | [diff] [blame] | 510 | public CharSequence label; |
| 511 | public CharSequence contentDescription; |
| 512 | public CharSequence dualLabelContentDescription; |
Selim Cinek | 06d3bca | 2014-08-26 17:29:20 +0200 | [diff] [blame] | 513 | public boolean autoMirrorDrawable = true; |
Sudheer Shanka | 1c7cda8 | 2015-12-31 14:46:02 +0000 | [diff] [blame] | 514 | public boolean disabledByPolicy; |
| 515 | public EnforcedAdmin enforcedAdmin; |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 516 | |
| 517 | public boolean copyTo(State other) { |
| 518 | if (other == null) throw new IllegalArgumentException(); |
| 519 | if (!other.getClass().equals(getClass())) throw new IllegalArgumentException(); |
Jason Monk | ba2318e | 2015-12-08 09:04:23 -0500 | [diff] [blame] | 520 | final boolean changed = !Objects.equals(other.icon, icon) |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 521 | || !Objects.equals(other.label, label) |
Selim Cinek | e5557a9 | 2014-08-15 19:59:23 +0200 | [diff] [blame] | 522 | || !Objects.equals(other.contentDescription, contentDescription) |
Selim Cinek | 06d3bca | 2014-08-26 17:29:20 +0200 | [diff] [blame] | 523 | || !Objects.equals(other.autoMirrorDrawable, autoMirrorDrawable) |
Selim Cinek | e5557a9 | 2014-08-15 19:59:23 +0200 | [diff] [blame] | 524 | || !Objects.equals(other.dualLabelContentDescription, |
Sudheer Shanka | 1c7cda8 | 2015-12-31 14:46:02 +0000 | [diff] [blame] | 525 | dualLabelContentDescription) |
| 526 | || !Objects.equals(other.disabledByPolicy, disabledByPolicy) |
| 527 | || !Objects.equals(other.enforcedAdmin, enforcedAdmin); |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 528 | other.icon = icon; |
| 529 | other.label = label; |
| 530 | other.contentDescription = contentDescription; |
Selim Cinek | e5557a9 | 2014-08-15 19:59:23 +0200 | [diff] [blame] | 531 | other.dualLabelContentDescription = dualLabelContentDescription; |
Selim Cinek | 06d3bca | 2014-08-26 17:29:20 +0200 | [diff] [blame] | 532 | other.autoMirrorDrawable = autoMirrorDrawable; |
Sudheer Shanka | 1c7cda8 | 2015-12-31 14:46:02 +0000 | [diff] [blame] | 533 | other.disabledByPolicy = disabledByPolicy; |
Sudheer Shanka | 1c09db2 | 2016-01-18 11:18:35 +0000 | [diff] [blame] | 534 | if (enforcedAdmin == null) { |
| 535 | other.enforcedAdmin = null; |
| 536 | } else if (other.enforcedAdmin == null) { |
| 537 | other.enforcedAdmin = new EnforcedAdmin(enforcedAdmin); |
| 538 | } else { |
| 539 | enforcedAdmin.copyTo(other.enforcedAdmin); |
| 540 | } |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 541 | return changed; |
| 542 | } |
| 543 | |
| 544 | @Override |
| 545 | public String toString() { |
| 546 | return toStringBuilder().toString(); |
| 547 | } |
| 548 | |
| 549 | protected StringBuilder toStringBuilder() { |
John Spurlock | 2d69581 | 2014-10-30 13:25:21 -0400 | [diff] [blame] | 550 | final StringBuilder sb = new StringBuilder(getClass().getSimpleName()).append('['); |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 551 | sb.append(",icon=").append(icon); |
| 552 | sb.append(",label=").append(label); |
| 553 | sb.append(",contentDescription=").append(contentDescription); |
Selim Cinek | e5557a9 | 2014-08-15 19:59:23 +0200 | [diff] [blame] | 554 | sb.append(",dualLabelContentDescription=").append(dualLabelContentDescription); |
Selim Cinek | 06d3bca | 2014-08-26 17:29:20 +0200 | [diff] [blame] | 555 | sb.append(",autoMirrorDrawable=").append(autoMirrorDrawable); |
Sudheer Shanka | 1c7cda8 | 2015-12-31 14:46:02 +0000 | [diff] [blame] | 556 | sb.append(",disabledByPolicy=").append(disabledByPolicy); |
| 557 | sb.append(",enforcedAdmin=").append(enforcedAdmin); |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 558 | return sb.append(']'); |
| 559 | } |
| 560 | } |
| 561 | |
| 562 | public static class BooleanState extends State { |
| 563 | public boolean value; |
| 564 | |
| 565 | @Override |
| 566 | public boolean copyTo(State other) { |
| 567 | final BooleanState o = (BooleanState) other; |
| 568 | final boolean changed = super.copyTo(other) || o.value != value; |
| 569 | o.value = value; |
| 570 | return changed; |
| 571 | } |
| 572 | |
| 573 | @Override |
| 574 | protected StringBuilder toStringBuilder() { |
| 575 | final StringBuilder rt = super.toStringBuilder(); |
| 576 | rt.insert(rt.length() - 1, ",value=" + value); |
| 577 | return rt; |
| 578 | } |
| 579 | } |
| 580 | |
| 581 | public static final class SignalState extends State { |
| 582 | public boolean enabled; |
| 583 | public boolean connected; |
| 584 | public boolean activityIn; |
| 585 | public boolean activityOut; |
| 586 | public int overlayIconId; |
John Spurlock | 899f439 | 2014-06-11 10:59:11 -0400 | [diff] [blame] | 587 | public boolean filter; |
Jorim Jaggi | c737b9b | 2014-09-08 23:57:20 +0200 | [diff] [blame] | 588 | public boolean isOverlayIconWide; |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 589 | |
| 590 | @Override |
| 591 | public boolean copyTo(State other) { |
| 592 | final SignalState o = (SignalState) other; |
| 593 | final boolean changed = o.enabled != enabled |
| 594 | || o.connected != connected || o.activityIn != activityIn |
| 595 | || o.activityOut != activityOut |
Jorim Jaggi | c737b9b | 2014-09-08 23:57:20 +0200 | [diff] [blame] | 596 | || o.overlayIconId != overlayIconId |
| 597 | || o.isOverlayIconWide != isOverlayIconWide; |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 598 | o.enabled = enabled; |
| 599 | o.connected = connected; |
| 600 | o.activityIn = activityIn; |
| 601 | o.activityOut = activityOut; |
| 602 | o.overlayIconId = overlayIconId; |
John Spurlock | 899f439 | 2014-06-11 10:59:11 -0400 | [diff] [blame] | 603 | o.filter = filter; |
Jorim Jaggi | c737b9b | 2014-09-08 23:57:20 +0200 | [diff] [blame] | 604 | o.isOverlayIconWide = isOverlayIconWide; |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 605 | return super.copyTo(other) || changed; |
| 606 | } |
| 607 | |
| 608 | @Override |
| 609 | protected StringBuilder toStringBuilder() { |
| 610 | final StringBuilder rt = super.toStringBuilder(); |
| 611 | rt.insert(rt.length() - 1, ",enabled=" + enabled); |
| 612 | rt.insert(rt.length() - 1, ",connected=" + connected); |
| 613 | rt.insert(rt.length() - 1, ",activityIn=" + activityIn); |
| 614 | rt.insert(rt.length() - 1, ",activityOut=" + activityOut); |
| 615 | rt.insert(rt.length() - 1, ",overlayIconId=" + overlayIconId); |
John Spurlock | 899f439 | 2014-06-11 10:59:11 -0400 | [diff] [blame] | 616 | rt.insert(rt.length() - 1, ",filter=" + filter); |
Jorim Jaggi | c737b9b | 2014-09-08 23:57:20 +0200 | [diff] [blame] | 617 | rt.insert(rt.length() - 1, ",wideOverlayIcon=" + isOverlayIconWide); |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 618 | return rt; |
| 619 | } |
| 620 | } |
| 621 | } |