John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 1 | /* |
Jason Monk | 702e2eb | 2017-03-03 16:53:44 -0500 | [diff] [blame] | 2 | * Copyright (C) 2017 The Android Open Source Project |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 3 | * |
Jason Monk | 702e2eb | 2017-03-03 16:53:44 -0500 | [diff] [blame] | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file |
| 5 | * except in compliance with the License. You may obtain a copy of the License at |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 6 | * |
| 7 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | * |
Jason Monk | 702e2eb | 2017-03-03 16:53:44 -0500 | [diff] [blame] | 9 | * Unless required by applicable law or agreed to in writing, software distributed under the |
| 10 | * License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 11 | * KIND, either express or implied. See the License for the specific language governing |
| 12 | * permissions and limitations under the License. |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 13 | */ |
| 14 | |
Jason Monk | 702e2eb | 2017-03-03 16:53:44 -0500 | [diff] [blame] | 15 | package com.android.systemui.qs.tileimpl; |
| 16 | |
Jason Monk | 8c09ac7 | 2017-03-16 11:53:40 -0400 | [diff] [blame] | 17 | import static com.android.internal.logging.nano.MetricsProto.MetricsEvent.ACTION_QS_CLICK; |
| 18 | import static com.android.internal.logging.nano.MetricsProto.MetricsEvent.ACTION_QS_LONG_PRESS; |
| 19 | import static com.android.internal.logging.nano.MetricsProto.MetricsEvent.ACTION_QS_SECONDARY_CLICK; |
| 20 | import static com.android.internal.logging.nano.MetricsProto.MetricsEvent.FIELD_QS_POSITION; |
| 21 | import static com.android.internal.logging.nano.MetricsProto.MetricsEvent.FIELD_QS_VALUE; |
| 22 | import static com.android.internal.logging.nano.MetricsProto.MetricsEvent.TYPE_ACTION; |
Jason Monk | 702e2eb | 2017-03-03 16:53:44 -0500 | [diff] [blame] | 23 | import static com.android.settingslib.RestrictedLockUtils.EnforcedAdmin; |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 24 | |
Jason Monk | 824ffff | 2017-04-11 15:49:06 -0400 | [diff] [blame] | 25 | import android.R.attr; |
Sudheer Shanka | b6fc931 | 2016-01-27 19:59:03 +0000 | [diff] [blame] | 26 | import android.app.ActivityManager; |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 27 | import android.content.Context; |
| 28 | import android.content.Intent; |
John Spurlock | 444eb2e | 2014-05-14 13:32:14 -0400 | [diff] [blame] | 29 | import android.graphics.drawable.Drawable; |
Jason Monk | 8c09ac7 | 2017-03-16 11:53:40 -0400 | [diff] [blame] | 30 | import android.metrics.LogMaker; |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 31 | import android.os.Handler; |
| 32 | import android.os.Looper; |
| 33 | import android.os.Message; |
Jason Monk | 3250885 | 2017-01-18 09:17:13 -0500 | [diff] [blame] | 34 | import android.service.quicksettings.Tile; |
Jason Monk | 1bec6af | 2016-05-31 15:40:58 -0400 | [diff] [blame] | 35 | import android.util.ArraySet; |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 36 | import android.util.Log; |
John Spurlock | 2d69581 | 2014-10-30 13:25:21 -0400 | [diff] [blame] | 37 | import android.util.SparseArray; |
Sudheer Shanka | 1c7cda8 | 2015-12-31 14:46:02 +0000 | [diff] [blame] | 38 | |
Jason Monk | 96defbe | 2016-03-29 16:51:03 -0400 | [diff] [blame] | 39 | import com.android.internal.logging.MetricsLogger; |
Sudheer Shanka | 1c7cda8 | 2015-12-31 14:46:02 +0000 | [diff] [blame] | 40 | import com.android.settingslib.RestrictedLockUtils; |
Jason Monk | 702e2eb | 2017-03-03 16:53:44 -0500 | [diff] [blame] | 41 | import com.android.settingslib.Utils; |
Jason Monk | 9c7844c | 2017-01-18 15:21:53 -0500 | [diff] [blame] | 42 | import com.android.systemui.Dependency; |
Jason Monk | ec34da8 | 2017-02-24 15:57:05 -0500 | [diff] [blame] | 43 | import com.android.systemui.plugins.ActivityStarter; |
Jason Monk | e5b770e | 2017-03-03 21:49:29 -0500 | [diff] [blame] | 44 | import com.android.systemui.plugins.qs.DetailAdapter; |
Jason Monk | 702e2eb | 2017-03-03 16:53:44 -0500 | [diff] [blame] | 45 | import com.android.systemui.plugins.qs.QSIconView; |
| 46 | import com.android.systemui.plugins.qs.QSTile; |
| 47 | import com.android.systemui.plugins.qs.QSTile.State; |
| 48 | import com.android.systemui.qs.QSHost; |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 49 | |
Jason Monk | ca894a0 | 2016-01-12 15:30:22 -0500 | [diff] [blame] | 50 | import java.util.ArrayList; |
Sudheer Shanka | 1c7cda8 | 2015-12-31 14:46:02 +0000 | [diff] [blame] | 51 | |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 52 | /** |
| 53 | * Base quick-settings tile, extend this to create a new tile. |
| 54 | * |
| 55 | * State management done on a looper provided by the host. Tiles should update state in |
| 56 | * handleUpdateState. Callbacks affecting state should use refreshState to trigger another |
| 57 | * state update pass on tile looper. |
| 58 | */ |
Jason Monk | 702e2eb | 2017-03-03 16:53:44 -0500 | [diff] [blame] | 59 | public abstract class QSTileImpl<TState extends State> implements QSTile { |
Jason Monk | bbadff8 | 2015-11-06 15:47:26 -0500 | [diff] [blame] | 60 | protected final String TAG = "Tile." + getClass().getSimpleName(); |
| 61 | protected static final boolean DEBUG = Log.isLoggable("Tile", Log.DEBUG); |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 62 | |
Jason Monk | 702e2eb | 2017-03-03 16:53:44 -0500 | [diff] [blame] | 63 | protected final QSHost mHost; |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 64 | protected final Context mContext; |
Evan Laird | 6b28473 | 2017-02-28 17:27:04 -0500 | [diff] [blame] | 65 | protected final H mHandler = new H(Dependency.get(Dependency.BG_LOOPER)); |
John Spurlock | 76c43b9 | 2014-05-13 21:10:51 -0400 | [diff] [blame] | 66 | protected final Handler mUiHandler = new Handler(Looper.getMainLooper()); |
Jason Monk | 1bec6af | 2016-05-31 15:40:58 -0400 | [diff] [blame] | 67 | private final ArraySet<Object> mListeners = new ArraySet<>(); |
Jason Monk | 8c09ac7 | 2017-03-16 11:53:40 -0400 | [diff] [blame] | 68 | private final MetricsLogger mMetricsLogger = Dependency.get(MetricsLogger.class); |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 69 | |
Jason Monk | ca894a0 | 2016-01-12 15:30:22 -0500 | [diff] [blame] | 70 | private final ArrayList<Callback> mCallbacks = new ArrayList<>(); |
Jason Monk | a758ba6 | 2015-07-14 12:29:28 -0400 | [diff] [blame] | 71 | protected TState mState = newTileState(); |
| 72 | private TState mTmpState = newTileState(); |
Selim Cinek | 4fda7b2 | 2014-08-18 22:07:25 +0200 | [diff] [blame] | 73 | private boolean mAnnounceNextStateChange; |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 74 | |
Jason Monk | bd6dbb0 | 2015-09-03 15:46:25 -0400 | [diff] [blame] | 75 | private String mTileSpec; |
Jason Monk | 702e2eb | 2017-03-03 16:53:44 -0500 | [diff] [blame] | 76 | private EnforcedAdmin mEnforcedAdmin; |
Jason Monk | be3235a | 2017-04-05 09:29:53 -0400 | [diff] [blame] | 77 | private boolean mShowingDetail; |
Jason Monk | bd6dbb0 | 2015-09-03 15:46:25 -0400 | [diff] [blame] | 78 | |
Jason Monk | 62b63a0 | 2016-02-02 15:15:31 -0500 | [diff] [blame] | 79 | public abstract TState newTileState(); |
Jason Monk | 3250885 | 2017-01-18 09:17:13 -0500 | [diff] [blame] | 80 | |
Chris Wren | 9e7283f | 2015-05-08 17:23:47 -0400 | [diff] [blame] | 81 | abstract protected void handleClick(); |
Jason Monk | 3250885 | 2017-01-18 09:17:13 -0500 | [diff] [blame] | 82 | |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 83 | abstract protected void handleUpdateState(TState state, Object arg); |
| 84 | |
Chris Wren | 457a21c | 2015-05-06 17:50:34 -0400 | [diff] [blame] | 85 | /** |
| 86 | * Declare the category of this tile. |
| 87 | * |
Jason Monk | 8c09ac7 | 2017-03-16 11:53:40 -0400 | [diff] [blame] | 88 | * Categories are defined in {@link com.android.internal.logging.nano.MetricsProto.MetricsEvent} |
Chris Wren | f6e9228b | 2016-01-26 18:04:35 -0500 | [diff] [blame] | 89 | * by editing frameworks/base/proto/src/metrics_constants.proto. |
Chris Wren | 457a21c | 2015-05-06 17:50:34 -0400 | [diff] [blame] | 90 | */ |
| 91 | abstract public int getMetricsCategory(); |
| 92 | |
Jason Monk | 702e2eb | 2017-03-03 16:53:44 -0500 | [diff] [blame] | 93 | protected QSTileImpl(QSHost host) { |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 94 | mHost = host; |
| 95 | mContext = host.getContext(); |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 96 | } |
Jason Monk | bd6dbb0 | 2015-09-03 15:46:25 -0400 | [diff] [blame] | 97 | |
Jason Monk | 1bec6af | 2016-05-31 15:40:58 -0400 | [diff] [blame] | 98 | /** |
| 99 | * Adds or removes a listening client for the tile. If the tile has one or more |
| 100 | * listening client it will go into the listening state. |
| 101 | */ |
| 102 | public void setListening(Object listener, boolean listening) { |
| 103 | if (listening) { |
| 104 | if (mListeners.add(listener) && mListeners.size() == 1) { |
| 105 | if (DEBUG) Log.d(TAG, "setListening " + true); |
| 106 | mHandler.obtainMessage(H.SET_LISTENING, 1, 0).sendToTarget(); |
| 107 | } |
| 108 | } else { |
| 109 | if (mListeners.remove(listener) && mListeners.size() == 0) { |
| 110 | if (DEBUG) Log.d(TAG, "setListening " + false); |
| 111 | mHandler.obtainMessage(H.SET_LISTENING, 0, 0).sendToTarget(); |
| 112 | } |
| 113 | } |
| 114 | } |
| 115 | |
Jason Monk | bd6dbb0 | 2015-09-03 15:46:25 -0400 | [diff] [blame] | 116 | public String getTileSpec() { |
| 117 | return mTileSpec; |
| 118 | } |
| 119 | |
| 120 | public void setTileSpec(String tileSpec) { |
| 121 | mTileSpec = tileSpec; |
| 122 | } |
| 123 | |
Jason Monk | 702e2eb | 2017-03-03 16:53:44 -0500 | [diff] [blame] | 124 | public QSHost getHost() { |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 125 | return mHost; |
| 126 | } |
| 127 | |
Jason Monk | dc35dcb | 2015-12-04 16:36:15 -0500 | [diff] [blame] | 128 | public QSIconView createTileView(Context context) { |
Jason Monk | 702e2eb | 2017-03-03 16:53:44 -0500 | [diff] [blame] | 129 | return new QSIconViewImpl(context); |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 130 | } |
| 131 | |
John Spurlock | 7f8f22a | 2014-07-02 18:54:17 -0400 | [diff] [blame] | 132 | public DetailAdapter getDetailAdapter() { |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 133 | return null; // optional |
| 134 | } |
| 135 | |
Jason Monk | 3250885 | 2017-01-18 09:17:13 -0500 | [diff] [blame] | 136 | protected DetailAdapter createDetailAdapter() { |
| 137 | throw new UnsupportedOperationException(); |
| 138 | } |
Muyuan Li | 0e9f538 | 2016-04-27 15:51:15 -0700 | [diff] [blame] | 139 | |
Jason Monk | c3f42c1 | 2016-02-05 12:33:13 -0500 | [diff] [blame] | 140 | /** |
| 141 | * Is a startup check whether this device currently supports this tile. |
| 142 | * Should not be used to conditionally hide tiles. Only checked on tile |
| 143 | * creation or whether should be shown in edit screen. |
| 144 | */ |
| 145 | public boolean isAvailable() { |
| 146 | return true; |
| 147 | } |
| 148 | |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 149 | // safe to call from any thread |
| 150 | |
Jason Monk | ca894a0 | 2016-01-12 15:30:22 -0500 | [diff] [blame] | 151 | public void addCallback(Callback callback) { |
| 152 | mHandler.obtainMessage(H.ADD_CALLBACK, callback).sendToTarget(); |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 153 | } |
| 154 | |
Xiaohui Chen | 08e266c | 2016-04-18 12:53:28 -0700 | [diff] [blame] | 155 | public void removeCallback(Callback callback) { |
| 156 | mHandler.obtainMessage(H.REMOVE_CALLBACK, callback).sendToTarget(); |
| 157 | } |
| 158 | |
Jason Monk | 9d02a43 | 2016-01-20 16:33:46 -0500 | [diff] [blame] | 159 | public void removeCallbacks() { |
| 160 | mHandler.sendEmptyMessage(H.REMOVE_CALLBACKS); |
| 161 | } |
| 162 | |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 163 | public void click() { |
Jason Monk | 8c09ac7 | 2017-03-16 11:53:40 -0400 | [diff] [blame] | 164 | mMetricsLogger.write(populate(new LogMaker(ACTION_QS_CLICK).setType(TYPE_ACTION))); |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 165 | mHandler.sendEmptyMessage(H.CLICK); |
| 166 | } |
| 167 | |
| 168 | public void secondaryClick() { |
Jason Monk | 8c09ac7 | 2017-03-16 11:53:40 -0400 | [diff] [blame] | 169 | mMetricsLogger.write(populate(new LogMaker(ACTION_QS_SECONDARY_CLICK).setType(TYPE_ACTION))); |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 170 | mHandler.sendEmptyMessage(H.SECONDARY_CLICK); |
| 171 | } |
| 172 | |
John Spurlock | c247b8f | 2014-11-06 23:06:25 -0500 | [diff] [blame] | 173 | public void longClick() { |
Jason Monk | 8c09ac7 | 2017-03-16 11:53:40 -0400 | [diff] [blame] | 174 | mMetricsLogger.write(populate(new LogMaker(ACTION_QS_LONG_PRESS).setType(TYPE_ACTION))); |
John Spurlock | c247b8f | 2014-11-06 23:06:25 -0500 | [diff] [blame] | 175 | mHandler.sendEmptyMessage(H.LONG_CLICK); |
| 176 | } |
| 177 | |
Jason Monk | cb4b31d | 2017-05-03 10:37:34 -0400 | [diff] [blame] | 178 | public LogMaker populate(LogMaker logMaker) { |
Jason Monk | 8c09ac7 | 2017-03-16 11:53:40 -0400 | [diff] [blame] | 179 | if (mState instanceof BooleanState) { |
| 180 | logMaker.addTaggedData(FIELD_QS_VALUE, ((BooleanState) mState).value ? 1 : 0); |
| 181 | } |
| 182 | return logMaker.setSubtype(getMetricsCategory()) |
| 183 | .addTaggedData(FIELD_QS_POSITION, mHost.indexOf(mTileSpec)); |
| 184 | } |
| 185 | |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 186 | public void showDetail(boolean show) { |
| 187 | mHandler.obtainMessage(H.SHOW_DETAIL, show ? 1 : 0, 0).sendToTarget(); |
| 188 | } |
| 189 | |
Evan Laird | 6b28473 | 2017-02-28 17:27:04 -0500 | [diff] [blame] | 190 | public void refreshState() { |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 191 | refreshState(null); |
| 192 | } |
| 193 | |
| 194 | protected final void refreshState(Object arg) { |
| 195 | mHandler.obtainMessage(H.REFRESH_STATE, arg).sendToTarget(); |
| 196 | } |
| 197 | |
Evan Laird | 6b28473 | 2017-02-28 17:27:04 -0500 | [diff] [blame] | 198 | public void clearState() { |
Jason Monk | a758ba6 | 2015-07-14 12:29:28 -0400 | [diff] [blame] | 199 | mHandler.sendEmptyMessage(H.CLEAR_STATE); |
| 200 | } |
| 201 | |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 202 | public void userSwitch(int newUserId) { |
Adrian Roos | 32d88e8 | 2014-09-24 17:08:22 +0200 | [diff] [blame] | 203 | mHandler.obtainMessage(H.USER_SWITCH, newUserId, 0).sendToTarget(); |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 204 | } |
| 205 | |
John Spurlock | 7f8f22a | 2014-07-02 18:54:17 -0400 | [diff] [blame] | 206 | public void fireToggleStateChanged(boolean state) { |
| 207 | mHandler.obtainMessage(H.TOGGLE_STATE_CHANGED, state ? 1 : 0, 0).sendToTarget(); |
| 208 | } |
| 209 | |
John Spurlock | 486b78e | 2014-07-07 08:37:56 -0400 | [diff] [blame] | 210 | public void fireScanStateChanged(boolean state) { |
| 211 | mHandler.obtainMessage(H.SCAN_STATE_CHANGED, state ? 1 : 0, 0).sendToTarget(); |
| 212 | } |
| 213 | |
John Spurlock | bceed06 | 2014-08-10 18:04:16 -0400 | [diff] [blame] | 214 | public void destroy() { |
| 215 | mHandler.sendEmptyMessage(H.DESTROY); |
| 216 | } |
| 217 | |
| 218 | public TState getState() { |
| 219 | return mState; |
| 220 | } |
| 221 | |
Jason Monk | 0d6a1c4 | 2015-04-20 16:38:51 -0400 | [diff] [blame] | 222 | public void setDetailListening(boolean listening) { |
| 223 | // optional |
| 224 | } |
| 225 | |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 226 | // call only on tile worker looper |
| 227 | |
Jason Monk | ca894a0 | 2016-01-12 15:30:22 -0500 | [diff] [blame] | 228 | private void handleAddCallback(Callback callback) { |
| 229 | mCallbacks.add(callback); |
Jason Monk | 968d269 | 2016-06-17 13:48:44 -0400 | [diff] [blame] | 230 | callback.onStateChanged(mState); |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 231 | } |
| 232 | |
Xiaohui Chen | 08e266c | 2016-04-18 12:53:28 -0700 | [diff] [blame] | 233 | private void handleRemoveCallback(Callback callback) { |
| 234 | mCallbacks.remove(callback); |
| 235 | } |
| 236 | |
Jason Monk | 9d02a43 | 2016-01-20 16:33:46 -0500 | [diff] [blame] | 237 | private void handleRemoveCallbacks() { |
| 238 | mCallbacks.clear(); |
| 239 | } |
| 240 | |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 241 | protected void handleSecondaryClick() { |
Jason Monk | dc35dcb | 2015-12-04 16:36:15 -0500 | [diff] [blame] | 242 | // Default to normal click. |
| 243 | handleClick(); |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 244 | } |
| 245 | |
John Spurlock | c247b8f | 2014-11-06 23:06:25 -0500 | [diff] [blame] | 246 | protected void handleLongClick() { |
Jason Monk | 9c7844c | 2017-01-18 15:21:53 -0500 | [diff] [blame] | 247 | Dependency.get(ActivityStarter.class).postStartActivityDismissingKeyguard( |
| 248 | getLongClickIntent(), 0); |
John Spurlock | c247b8f | 2014-11-06 23:06:25 -0500 | [diff] [blame] | 249 | } |
| 250 | |
Jason Monk | 76c67aa | 2016-02-19 14:49:42 -0500 | [diff] [blame] | 251 | public abstract Intent getLongClickIntent(); |
| 252 | |
Jason Monk | a758ba6 | 2015-07-14 12:29:28 -0400 | [diff] [blame] | 253 | protected void handleClearState() { |
| 254 | mTmpState = newTileState(); |
| 255 | mState = newTileState(); |
| 256 | } |
| 257 | |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 258 | protected void handleRefreshState(Object arg) { |
| 259 | handleUpdateState(mTmpState, arg); |
| 260 | final boolean changed = mTmpState.copyTo(mState); |
| 261 | if (changed) { |
| 262 | handleStateChanged(); |
| 263 | } |
| 264 | } |
| 265 | |
| 266 | private void handleStateChanged() { |
Selim Cinek | 4fda7b2 | 2014-08-18 22:07:25 +0200 | [diff] [blame] | 267 | boolean delayAnnouncement = shouldAnnouncementBeDelayed(); |
Jason Monk | ca894a0 | 2016-01-12 15:30:22 -0500 | [diff] [blame] | 268 | if (mCallbacks.size() != 0) { |
| 269 | for (int i = 0; i < mCallbacks.size(); i++) { |
| 270 | mCallbacks.get(i).onStateChanged(mState); |
| 271 | } |
Selim Cinek | 4fda7b2 | 2014-08-18 22:07:25 +0200 | [diff] [blame] | 272 | if (mAnnounceNextStateChange && !delayAnnouncement) { |
| 273 | String announcement = composeChangeAnnouncement(); |
| 274 | if (announcement != null) { |
Jason Monk | ca894a0 | 2016-01-12 15:30:22 -0500 | [diff] [blame] | 275 | mCallbacks.get(0).onAnnouncementRequested(announcement); |
Selim Cinek | 4fda7b2 | 2014-08-18 22:07:25 +0200 | [diff] [blame] | 276 | } |
| 277 | } |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 278 | } |
Selim Cinek | 4fda7b2 | 2014-08-18 22:07:25 +0200 | [diff] [blame] | 279 | mAnnounceNextStateChange = mAnnounceNextStateChange && delayAnnouncement; |
| 280 | } |
| 281 | |
| 282 | protected boolean shouldAnnouncementBeDelayed() { |
| 283 | return false; |
| 284 | } |
| 285 | |
| 286 | protected String composeChangeAnnouncement() { |
| 287 | return null; |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 288 | } |
| 289 | |
| 290 | private void handleShowDetail(boolean show) { |
Jason Monk | be3235a | 2017-04-05 09:29:53 -0400 | [diff] [blame] | 291 | mShowingDetail = show; |
Jason Monk | ca894a0 | 2016-01-12 15:30:22 -0500 | [diff] [blame] | 292 | for (int i = 0; i < mCallbacks.size(); i++) { |
| 293 | mCallbacks.get(i).onShowDetail(show); |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 294 | } |
| 295 | } |
| 296 | |
Jason Monk | be3235a | 2017-04-05 09:29:53 -0400 | [diff] [blame] | 297 | protected boolean isShowingDetail() { |
| 298 | return mShowingDetail; |
| 299 | } |
| 300 | |
John Spurlock | 7f8f22a | 2014-07-02 18:54:17 -0400 | [diff] [blame] | 301 | private void handleToggleStateChanged(boolean state) { |
Jason Monk | ca894a0 | 2016-01-12 15:30:22 -0500 | [diff] [blame] | 302 | for (int i = 0; i < mCallbacks.size(); i++) { |
| 303 | mCallbacks.get(i).onToggleStateChanged(state); |
John Spurlock | 7f8f22a | 2014-07-02 18:54:17 -0400 | [diff] [blame] | 304 | } |
| 305 | } |
| 306 | |
John Spurlock | 486b78e | 2014-07-07 08:37:56 -0400 | [diff] [blame] | 307 | private void handleScanStateChanged(boolean state) { |
Jason Monk | ca894a0 | 2016-01-12 15:30:22 -0500 | [diff] [blame] | 308 | for (int i = 0; i < mCallbacks.size(); i++) { |
| 309 | mCallbacks.get(i).onScanStateChanged(state); |
John Spurlock | 486b78e | 2014-07-07 08:37:56 -0400 | [diff] [blame] | 310 | } |
| 311 | } |
| 312 | |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 313 | protected void handleUserSwitch(int newUserId) { |
| 314 | handleRefreshState(null); |
| 315 | } |
| 316 | |
Jason Monk | 1bec6af | 2016-05-31 15:40:58 -0400 | [diff] [blame] | 317 | protected abstract void setListening(boolean listening); |
| 318 | |
John Spurlock | bceed06 | 2014-08-10 18:04:16 -0400 | [diff] [blame] | 319 | protected void handleDestroy() { |
Jason Monk | 794bcd2 | 2017-06-06 16:38:00 -0400 | [diff] [blame] | 320 | if (mListeners.size() != 0) { |
| 321 | setListening(false); |
| 322 | } |
Jason Monk | ca894a0 | 2016-01-12 15:30:22 -0500 | [diff] [blame] | 323 | mCallbacks.clear(); |
John Spurlock | bceed06 | 2014-08-10 18:04:16 -0400 | [diff] [blame] | 324 | } |
| 325 | |
Sudheer Shanka | a8fbbb3 | 2016-02-11 17:17:57 +0000 | [diff] [blame] | 326 | protected void checkIfRestrictionEnforcedByAdminOnly(State state, String userRestriction) { |
Sudheer Shanka | ad143c0 | 2016-03-31 00:24:05 +0000 | [diff] [blame] | 327 | EnforcedAdmin admin = RestrictedLockUtils.checkIfRestrictionEnforced(mContext, |
| 328 | userRestriction, ActivityManager.getCurrentUser()); |
| 329 | if (admin != null && !RestrictedLockUtils.hasBaseUserRestriction(mContext, |
| 330 | userRestriction, ActivityManager.getCurrentUser())) { |
Sudheer Shanka | 1c7cda8 | 2015-12-31 14:46:02 +0000 | [diff] [blame] | 331 | state.disabledByPolicy = true; |
Jason Monk | 702e2eb | 2017-03-03 16:53:44 -0500 | [diff] [blame] | 332 | mEnforcedAdmin = admin; |
Sudheer Shanka | 1c7cda8 | 2015-12-31 14:46:02 +0000 | [diff] [blame] | 333 | } else { |
| 334 | state.disabledByPolicy = false; |
Jason Monk | 702e2eb | 2017-03-03 16:53:44 -0500 | [diff] [blame] | 335 | mEnforcedAdmin = null; |
Sudheer Shanka | 1c7cda8 | 2015-12-31 14:46:02 +0000 | [diff] [blame] | 336 | } |
| 337 | } |
| 338 | |
Jason Monk | 39c98e6 | 2016-03-16 09:18:35 -0400 | [diff] [blame] | 339 | public abstract CharSequence getTileLabel(); |
| 340 | |
Jason Monk | 3250885 | 2017-01-18 09:17:13 -0500 | [diff] [blame] | 341 | public static int getColorForState(Context context, int state) { |
| 342 | switch (state) { |
| 343 | case Tile.STATE_UNAVAILABLE: |
| 344 | return Utils.getDisabled(context, |
Jason Monk | 3536b23 | 2017-04-19 13:11:11 -0400 | [diff] [blame] | 345 | Utils.getColorAttr(context, android.R.attr.colorForeground)); |
Jason Monk | b4cc7b1 | 2017-05-09 13:50:47 -0400 | [diff] [blame] | 346 | case Tile.STATE_INACTIVE: |
| 347 | return Utils.getColorAttr(context, android.R.attr.textColorHint); |
Jason Monk | 3250885 | 2017-01-18 09:17:13 -0500 | [diff] [blame] | 348 | case Tile.STATE_ACTIVE: |
Jason Monk | 3536b23 | 2017-04-19 13:11:11 -0400 | [diff] [blame] | 349 | return Utils.getColorAttr(context, android.R.attr.textColorPrimary); |
Jason Monk | 3250885 | 2017-01-18 09:17:13 -0500 | [diff] [blame] | 350 | default: |
| 351 | Log.e("QSTile", "Invalid state " + state); |
| 352 | return 0; |
| 353 | } |
| 354 | } |
| 355 | |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 356 | protected final class H extends Handler { |
Jason Monk | ca894a0 | 2016-01-12 15:30:22 -0500 | [diff] [blame] | 357 | private static final int ADD_CALLBACK = 1; |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 358 | private static final int CLICK = 2; |
| 359 | private static final int SECONDARY_CLICK = 3; |
John Spurlock | c247b8f | 2014-11-06 23:06:25 -0500 | [diff] [blame] | 360 | private static final int LONG_CLICK = 4; |
| 361 | private static final int REFRESH_STATE = 5; |
| 362 | private static final int SHOW_DETAIL = 6; |
| 363 | private static final int USER_SWITCH = 7; |
| 364 | private static final int TOGGLE_STATE_CHANGED = 8; |
| 365 | private static final int SCAN_STATE_CHANGED = 9; |
| 366 | private static final int DESTROY = 10; |
Jason Monk | a758ba6 | 2015-07-14 12:29:28 -0400 | [diff] [blame] | 367 | private static final int CLEAR_STATE = 11; |
Jason Monk | 9d02a43 | 2016-01-20 16:33:46 -0500 | [diff] [blame] | 368 | private static final int REMOVE_CALLBACKS = 12; |
Xiaohui Chen | 08e266c | 2016-04-18 12:53:28 -0700 | [diff] [blame] | 369 | private static final int REMOVE_CALLBACK = 13; |
Jason Monk | 1bec6af | 2016-05-31 15:40:58 -0400 | [diff] [blame] | 370 | private static final int SET_LISTENING = 14; |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 371 | |
| 372 | private H(Looper looper) { |
| 373 | super(looper); |
| 374 | } |
| 375 | |
| 376 | @Override |
| 377 | public void handleMessage(Message msg) { |
| 378 | String name = null; |
| 379 | try { |
Jason Monk | ca894a0 | 2016-01-12 15:30:22 -0500 | [diff] [blame] | 380 | if (msg.what == ADD_CALLBACK) { |
| 381 | name = "handleAddCallback"; |
Jason Monk | 9d02a43 | 2016-01-20 16:33:46 -0500 | [diff] [blame] | 382 | handleAddCallback((QSTile.Callback) msg.obj); |
| 383 | } else if (msg.what == REMOVE_CALLBACKS) { |
| 384 | name = "handleRemoveCallbacks"; |
| 385 | handleRemoveCallbacks(); |
Xiaohui Chen | 08e266c | 2016-04-18 12:53:28 -0700 | [diff] [blame] | 386 | } else if (msg.what == REMOVE_CALLBACK) { |
| 387 | name = "handleRemoveCallback"; |
| 388 | handleRemoveCallback((QSTile.Callback) msg.obj); |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 389 | } else if (msg.what == CLICK) { |
| 390 | name = "handleClick"; |
Sudheer Shanka | 1c7cda8 | 2015-12-31 14:46:02 +0000 | [diff] [blame] | 391 | if (mState.disabledByPolicy) { |
| 392 | Intent intent = RestrictedLockUtils.getShowAdminSupportDetailsIntent( |
Jason Monk | 702e2eb | 2017-03-03 16:53:44 -0500 | [diff] [blame] | 393 | mContext, mEnforcedAdmin); |
Jason Monk | 9c7844c | 2017-01-18 15:21:53 -0500 | [diff] [blame] | 394 | Dependency.get(ActivityStarter.class).postStartActivityDismissingKeyguard( |
| 395 | intent, 0); |
Sudheer Shanka | 1c7cda8 | 2015-12-31 14:46:02 +0000 | [diff] [blame] | 396 | } else { |
Sudheer Shanka | 1c7cda8 | 2015-12-31 14:46:02 +0000 | [diff] [blame] | 397 | handleClick(); |
| 398 | } |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 399 | } else if (msg.what == SECONDARY_CLICK) { |
| 400 | name = "handleSecondaryClick"; |
| 401 | handleSecondaryClick(); |
John Spurlock | c247b8f | 2014-11-06 23:06:25 -0500 | [diff] [blame] | 402 | } else if (msg.what == LONG_CLICK) { |
| 403 | name = "handleLongClick"; |
| 404 | handleLongClick(); |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 405 | } else if (msg.what == REFRESH_STATE) { |
| 406 | name = "handleRefreshState"; |
| 407 | handleRefreshState(msg.obj); |
| 408 | } else if (msg.what == SHOW_DETAIL) { |
| 409 | name = "handleShowDetail"; |
| 410 | handleShowDetail(msg.arg1 != 0); |
| 411 | } else if (msg.what == USER_SWITCH) { |
| 412 | name = "handleUserSwitch"; |
| 413 | handleUserSwitch(msg.arg1); |
John Spurlock | 7f8f22a | 2014-07-02 18:54:17 -0400 | [diff] [blame] | 414 | } else if (msg.what == TOGGLE_STATE_CHANGED) { |
| 415 | name = "handleToggleStateChanged"; |
| 416 | handleToggleStateChanged(msg.arg1 != 0); |
John Spurlock | 486b78e | 2014-07-07 08:37:56 -0400 | [diff] [blame] | 417 | } else if (msg.what == SCAN_STATE_CHANGED) { |
| 418 | name = "handleScanStateChanged"; |
| 419 | handleScanStateChanged(msg.arg1 != 0); |
John Spurlock | bceed06 | 2014-08-10 18:04:16 -0400 | [diff] [blame] | 420 | } else if (msg.what == DESTROY) { |
| 421 | name = "handleDestroy"; |
| 422 | handleDestroy(); |
Jason Monk | a758ba6 | 2015-07-14 12:29:28 -0400 | [diff] [blame] | 423 | } else if (msg.what == CLEAR_STATE) { |
| 424 | name = "handleClearState"; |
| 425 | handleClearState(); |
Jason Monk | 1bec6af | 2016-05-31 15:40:58 -0400 | [diff] [blame] | 426 | } else if (msg.what == SET_LISTENING) { |
| 427 | name = "setListening"; |
| 428 | setListening(msg.arg1 != 0); |
John Spurlock | bceed06 | 2014-08-10 18:04:16 -0400 | [diff] [blame] | 429 | } else { |
| 430 | throw new IllegalArgumentException("Unknown msg: " + msg.what); |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 431 | } |
| 432 | } catch (Throwable t) { |
| 433 | final String error = "Error in " + name; |
| 434 | Log.w(TAG, error, t); |
| 435 | mHost.warn(error, t); |
| 436 | } |
| 437 | } |
| 438 | } |
| 439 | |
Jason Monk | 5db8a41 | 2015-10-21 15:16:23 -0700 | [diff] [blame] | 440 | public static class DrawableIcon extends Icon { |
| 441 | protected final Drawable mDrawable; |
| 442 | |
| 443 | public DrawableIcon(Drawable drawable) { |
| 444 | mDrawable = drawable; |
| 445 | } |
| 446 | |
| 447 | @Override |
| 448 | public Drawable getDrawable(Context context) { |
| 449 | return mDrawable; |
| 450 | } |
Jason Monk | d2274f8 | 2016-12-12 12:02:16 -0500 | [diff] [blame] | 451 | } |
| 452 | |
| 453 | public static class DrawableIconWithRes extends DrawableIcon { |
| 454 | private final int mId; |
| 455 | |
| 456 | public DrawableIconWithRes(Drawable drawable, int id) { |
| 457 | super(drawable); |
| 458 | mId = id; |
| 459 | } |
Jason Monk | 1aec93f | 2016-03-01 09:39:30 -0500 | [diff] [blame] | 460 | |
| 461 | @Override |
Jason Monk | d2274f8 | 2016-12-12 12:02:16 -0500 | [diff] [blame] | 462 | public boolean equals(Object o) { |
| 463 | return o instanceof DrawableIconWithRes && ((DrawableIconWithRes) o).mId == mId; |
Jason Monk | 1aec93f | 2016-03-01 09:39:30 -0500 | [diff] [blame] | 464 | } |
Jason Monk | 5db8a41 | 2015-10-21 15:16:23 -0700 | [diff] [blame] | 465 | } |
| 466 | |
John Spurlock | 2d69581 | 2014-10-30 13:25:21 -0400 | [diff] [blame] | 467 | public static class ResourceIcon extends Icon { |
| 468 | private static final SparseArray<Icon> ICONS = new SparseArray<Icon>(); |
| 469 | |
Andrew Flynn | a478d70 | 2015-04-14 23:33:45 -0400 | [diff] [blame] | 470 | protected final int mResId; |
John Spurlock | 2d69581 | 2014-10-30 13:25:21 -0400 | [diff] [blame] | 471 | |
| 472 | private ResourceIcon(int resId) { |
| 473 | mResId = resId; |
| 474 | } |
| 475 | |
| 476 | public static Icon get(int resId) { |
| 477 | Icon icon = ICONS.get(resId); |
| 478 | if (icon == null) { |
| 479 | icon = new ResourceIcon(resId); |
| 480 | ICONS.put(resId, icon); |
| 481 | } |
| 482 | return icon; |
| 483 | } |
| 484 | |
| 485 | @Override |
| 486 | public Drawable getDrawable(Context context) { |
Jason Monk | 66239fb | 2015-12-21 14:27:00 -0500 | [diff] [blame] | 487 | return context.getDrawable(mResId); |
John Spurlock | 2d69581 | 2014-10-30 13:25:21 -0400 | [diff] [blame] | 488 | } |
| 489 | |
| 490 | @Override |
Jason Monk | 1aec93f | 2016-03-01 09:39:30 -0500 | [diff] [blame] | 491 | public Drawable getInvisibleDrawable(Context context) { |
| 492 | return context.getDrawable(mResId); |
| 493 | } |
| 494 | |
| 495 | @Override |
John Spurlock | 2d69581 | 2014-10-30 13:25:21 -0400 | [diff] [blame] | 496 | public boolean equals(Object o) { |
| 497 | return o instanceof ResourceIcon && ((ResourceIcon) o).mResId == mResId; |
| 498 | } |
| 499 | |
| 500 | @Override |
| 501 | public String toString() { |
| 502 | return String.format("ResourceIcon[resId=0x%08x]", mResId); |
| 503 | } |
| 504 | } |
| 505 | |
| 506 | protected class AnimationIcon extends ResourceIcon { |
Jason Monk | 1aec93f | 2016-03-01 09:39:30 -0500 | [diff] [blame] | 507 | private final int mAnimatedResId; |
| 508 | |
| 509 | public AnimationIcon(int resId, int staticResId) { |
| 510 | super(staticResId); |
| 511 | mAnimatedResId = resId; |
John Spurlock | 2d69581 | 2014-10-30 13:25:21 -0400 | [diff] [blame] | 512 | } |
| 513 | |
John Spurlock | 2d69581 | 2014-10-30 13:25:21 -0400 | [diff] [blame] | 514 | @Override |
| 515 | public Drawable getDrawable(Context context) { |
| 516 | // workaround: get a clean state for every new AVD |
Jason Monk | 1aec93f | 2016-03-01 09:39:30 -0500 | [diff] [blame] | 517 | return context.getDrawable(mAnimatedResId).getConstantState().newDrawable(); |
John Spurlock | 2d69581 | 2014-10-30 13:25:21 -0400 | [diff] [blame] | 518 | } |
| 519 | } |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 520 | } |