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 | fa452ef | 2018-12-26 17:26:10 -0500 | [diff] [blame] | 17 | import static androidx.lifecycle.Lifecycle.State.RESUMED; |
Fabian Kozynski | f455951 | 2020-06-23 14:02:58 -0400 | [diff] [blame] | 18 | import static androidx.lifecycle.Lifecycle.State.STARTED; |
Jason Monk | fa452ef | 2018-12-26 17:26:10 -0500 | [diff] [blame] | 19 | |
Jason Monk | 8c09ac7 | 2017-03-16 11:53:40 -0400 | [diff] [blame] | 20 | import static com.android.internal.logging.nano.MetricsProto.MetricsEvent.ACTION_QS_CLICK; |
| 21 | import static com.android.internal.logging.nano.MetricsProto.MetricsEvent.ACTION_QS_LONG_PRESS; |
| 22 | import static com.android.internal.logging.nano.MetricsProto.MetricsEvent.ACTION_QS_SECONDARY_CLICK; |
Susi Kharraz-Post | 9b03367 | 2018-11-28 08:14:07 -0500 | [diff] [blame] | 23 | import static com.android.internal.logging.nano.MetricsProto.MetricsEvent.FIELD_IS_FULL_QS; |
Jason Monk | 8c09ac7 | 2017-03-16 11:53:40 -0400 | [diff] [blame] | 24 | import static com.android.internal.logging.nano.MetricsProto.MetricsEvent.FIELD_QS_POSITION; |
| 25 | import static com.android.internal.logging.nano.MetricsProto.MetricsEvent.FIELD_QS_VALUE; |
Susi Kharraz-Post | 9b03367 | 2018-11-28 08:14:07 -0500 | [diff] [blame] | 26 | import static com.android.internal.logging.nano.MetricsProto.MetricsEvent.FIELD_STATUS_BAR_STATE; |
Jason Monk | 8c09ac7 | 2017-03-16 11:53:40 -0400 | [diff] [blame] | 27 | import static com.android.internal.logging.nano.MetricsProto.MetricsEvent.TYPE_ACTION; |
Jason Monk | 702e2eb | 2017-03-03 16:53:44 -0500 | [diff] [blame] | 28 | import static com.android.settingslib.RestrictedLockUtils.EnforcedAdmin; |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 29 | |
Fabian Kozynski | ca30f57 | 2019-10-24 10:01:04 -0400 | [diff] [blame] | 30 | import android.annotation.CallSuper; |
| 31 | import android.annotation.NonNull; |
Sudheer Shanka | b6fc931 | 2016-01-27 19:59:03 +0000 | [diff] [blame] | 32 | import android.app.ActivityManager; |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 33 | import android.content.Context; |
| 34 | import android.content.Intent; |
John Spurlock | 444eb2e | 2014-05-14 13:32:14 -0400 | [diff] [blame] | 35 | import android.graphics.drawable.Drawable; |
Jason Monk | 8c09ac7 | 2017-03-16 11:53:40 -0400 | [diff] [blame] | 36 | import android.metrics.LogMaker; |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 37 | import android.os.Handler; |
| 38 | import android.os.Looper; |
| 39 | import android.os.Message; |
Jason Monk | 3250885 | 2017-01-18 09:17:13 -0500 | [diff] [blame] | 40 | import android.service.quicksettings.Tile; |
Jason Monk | 1c6116c | 2017-09-06 17:33:01 -0400 | [diff] [blame] | 41 | import android.text.format.DateUtils; |
Jason Monk | 1bec6af | 2016-05-31 15:40:58 -0400 | [diff] [blame] | 42 | import android.util.ArraySet; |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 43 | import android.util.Log; |
John Spurlock | 2d69581 | 2014-10-30 13:25:21 -0400 | [diff] [blame] | 44 | import android.util.SparseArray; |
Sudheer Shanka | 1c7cda8 | 2015-12-31 14:46:02 +0000 | [diff] [blame] | 45 | |
Jason Monk | fa452ef | 2018-12-26 17:26:10 -0500 | [diff] [blame] | 46 | import androidx.lifecycle.Lifecycle; |
| 47 | import androidx.lifecycle.LifecycleOwner; |
| 48 | import androidx.lifecycle.LifecycleRegistry; |
| 49 | |
Jason Monk | 1c6116c | 2017-09-06 17:33:01 -0400 | [diff] [blame] | 50 | import com.android.internal.annotations.VisibleForTesting; |
Fabian Kozynski | 2ff6df9 | 2020-04-24 12:00:49 -0400 | [diff] [blame] | 51 | import com.android.internal.logging.InstanceId; |
Jason Monk | 96defbe | 2016-03-29 16:51:03 -0400 | [diff] [blame] | 52 | import com.android.internal.logging.MetricsLogger; |
Fabian Kozynski | 2ff6df9 | 2020-04-24 12:00:49 -0400 | [diff] [blame] | 53 | import com.android.internal.logging.UiEventLogger; |
Sudheer Shanka | 1c7cda8 | 2015-12-31 14:46:02 +0000 | [diff] [blame] | 54 | import com.android.settingslib.RestrictedLockUtils; |
Philip P. Moltmann | 4e615e6 | 2018-08-28 14:57:49 -0700 | [diff] [blame] | 55 | import com.android.settingslib.RestrictedLockUtilsInternal; |
Jason Monk | 702e2eb | 2017-03-03 16:53:44 -0500 | [diff] [blame] | 56 | import com.android.settingslib.Utils; |
Jason Monk | 9c7844c | 2017-01-18 15:21:53 -0500 | [diff] [blame] | 57 | import com.android.systemui.Dependency; |
Fabian Kozynski | 00d494d | 2019-04-04 09:53:50 -0400 | [diff] [blame] | 58 | import com.android.systemui.Dumpable; |
Rohan Shah | db2cfa3 | 2018-02-20 11:27:22 -0800 | [diff] [blame] | 59 | import com.android.systemui.Prefs; |
Jason Monk | ec34da8 | 2017-02-24 15:57:05 -0500 | [diff] [blame] | 60 | import com.android.systemui.plugins.ActivityStarter; |
Jason Monk | e5b770e | 2017-03-03 21:49:29 -0500 | [diff] [blame] | 61 | import com.android.systemui.plugins.qs.DetailAdapter; |
Jason Monk | 702e2eb | 2017-03-03 16:53:44 -0500 | [diff] [blame] | 62 | import com.android.systemui.plugins.qs.QSIconView; |
| 63 | import com.android.systemui.plugins.qs.QSTile; |
| 64 | import com.android.systemui.plugins.qs.QSTile.State; |
Beverly | 8fdb533 | 2019-02-04 14:29:49 -0500 | [diff] [blame] | 65 | import com.android.systemui.plugins.statusbar.StatusBarStateController; |
Jason Monk | f8c2f7b | 2017-09-06 09:22:29 -0400 | [diff] [blame] | 66 | import com.android.systemui.qs.PagedTileLayout.TilePage; |
Fabian Kozynski | 2ff6df9 | 2020-04-24 12:00:49 -0400 | [diff] [blame] | 67 | import com.android.systemui.qs.QSEvent; |
Jason Monk | 702e2eb | 2017-03-03 16:53:44 -0500 | [diff] [blame] | 68 | import com.android.systemui.qs.QSHost; |
Rohan Shah | d3cf756 | 2018-02-23 11:12:28 -0800 | [diff] [blame] | 69 | import com.android.systemui.qs.QuickStatusBarHeader; |
Fabian Kozynski | ca30f57 | 2019-10-24 10:01:04 -0400 | [diff] [blame] | 70 | import com.android.systemui.qs.logging.QSLogger; |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 71 | |
Fabian Kozynski | 00d494d | 2019-04-04 09:53:50 -0400 | [diff] [blame] | 72 | import java.io.FileDescriptor; |
| 73 | import java.io.PrintWriter; |
Jason Monk | ca894a0 | 2016-01-12 15:30:22 -0500 | [diff] [blame] | 74 | import java.util.ArrayList; |
Sudheer Shanka | 1c7cda8 | 2015-12-31 14:46:02 +0000 | [diff] [blame] | 75 | |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 76 | /** |
| 77 | * Base quick-settings tile, extend this to create a new tile. |
| 78 | * |
| 79 | * State management done on a looper provided by the host. Tiles should update state in |
| 80 | * handleUpdateState. Callbacks affecting state should use refreshState to trigger another |
| 81 | * state update pass on tile looper. |
Susi Kharraz-Post | 9b03367 | 2018-11-28 08:14:07 -0500 | [diff] [blame] | 82 | * |
| 83 | * @param <TState> see above |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 84 | */ |
Fabian Kozynski | 00d494d | 2019-04-04 09:53:50 -0400 | [diff] [blame] | 85 | public abstract class QSTileImpl<TState extends State> implements QSTile, LifecycleOwner, Dumpable { |
Jason Monk | bbadff8 | 2015-11-06 15:47:26 -0500 | [diff] [blame] | 86 | protected final String TAG = "Tile." + getClass().getSimpleName(); |
| 87 | protected static final boolean DEBUG = Log.isLoggable("Tile", Log.DEBUG); |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 88 | |
Jason Monk | 1c6116c | 2017-09-06 17:33:01 -0400 | [diff] [blame] | 89 | private static final long DEFAULT_STALE_TIMEOUT = 10 * DateUtils.MINUTE_IN_MILLIS; |
Amin Shaikh | d03a743 | 2018-03-01 15:46:55 -0500 | [diff] [blame] | 90 | protected static final Object ARG_SHOW_TRANSIENT_ENABLING = new Object(); |
Jason Monk | 1c6116c | 2017-09-06 17:33:01 -0400 | [diff] [blame] | 91 | |
Jason Monk | 702e2eb | 2017-03-03 16:53:44 -0500 | [diff] [blame] | 92 | protected final QSHost mHost; |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 93 | protected final Context mContext; |
Jason Monk | 1c6116c | 2017-09-06 17:33:01 -0400 | [diff] [blame] | 94 | // @NonFinalForTesting |
| 95 | protected H mHandler = new H(Dependency.get(Dependency.BG_LOOPER)); |
John Spurlock | 76c43b9 | 2014-05-13 21:10:51 -0400 | [diff] [blame] | 96 | protected final Handler mUiHandler = new Handler(Looper.getMainLooper()); |
Jason Monk | 1bec6af | 2016-05-31 15:40:58 -0400 | [diff] [blame] | 97 | private final ArraySet<Object> mListeners = new ArraySet<>(); |
Jason Monk | 8c09ac7 | 2017-03-16 11:53:40 -0400 | [diff] [blame] | 98 | private final MetricsLogger mMetricsLogger = Dependency.get(MetricsLogger.class); |
Susi Kharraz-Post | 9b03367 | 2018-11-28 08:14:07 -0500 | [diff] [blame] | 99 | private final StatusBarStateController |
| 100 | mStatusBarStateController = Dependency.get(StatusBarStateController.class); |
Fabian Kozynski | 2ff6df9 | 2020-04-24 12:00:49 -0400 | [diff] [blame] | 101 | private final UiEventLogger mUiEventLogger; |
Fabian Kozynski | ca30f57 | 2019-10-24 10:01:04 -0400 | [diff] [blame] | 102 | private final QSLogger mQSLogger; |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 103 | |
Jason Monk | ca894a0 | 2016-01-12 15:30:22 -0500 | [diff] [blame] | 104 | private final ArrayList<Callback> mCallbacks = new ArrayList<>(); |
Jason Monk | 1c6116c | 2017-09-06 17:33:01 -0400 | [diff] [blame] | 105 | private final Object mStaleListener = new Object(); |
Fabian Kozynski | 67ffcf8 | 2019-09-05 16:32:12 -0400 | [diff] [blame] | 106 | protected TState mState; |
| 107 | private TState mTmpState; |
Fabian Kozynski | 2ff6df9 | 2020-04-24 12:00:49 -0400 | [diff] [blame] | 108 | private final InstanceId mInstanceId; |
Selim Cinek | 4fda7b2 | 2014-08-18 22:07:25 +0200 | [diff] [blame] | 109 | private boolean mAnnounceNextStateChange; |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 110 | |
Jason Monk | bd6dbb0 | 2015-09-03 15:46:25 -0400 | [diff] [blame] | 111 | private String mTileSpec; |
Jason Monk | 702e2eb | 2017-03-03 16:53:44 -0500 | [diff] [blame] | 112 | private EnforcedAdmin mEnforcedAdmin; |
Jason Monk | be3235a | 2017-04-05 09:29:53 -0400 | [diff] [blame] | 113 | private boolean mShowingDetail; |
Amin Shaikh | 572230b | 2018-03-20 17:24:57 -0400 | [diff] [blame] | 114 | private int mIsFullQs; |
Jason Monk | bd6dbb0 | 2015-09-03 15:46:25 -0400 | [diff] [blame] | 115 | |
Jason Monk | fa452ef | 2018-12-26 17:26:10 -0500 | [diff] [blame] | 116 | private final LifecycleRegistry mLifecycle = new LifecycleRegistry(this); |
| 117 | |
Fabian Kozynski | 01f0768 | 2019-06-06 14:07:17 -0400 | [diff] [blame] | 118 | /** |
| 119 | * Provides a new {@link TState} of the appropriate type to use between this tile and the |
| 120 | * corresponding view. |
| 121 | * |
| 122 | * @return new state to use by the tile. |
| 123 | */ |
Jason Monk | 62b63a0 | 2016-02-02 15:15:31 -0500 | [diff] [blame] | 124 | public abstract TState newTileState(); |
Jason Monk | 3250885 | 2017-01-18 09:17:13 -0500 | [diff] [blame] | 125 | |
Fabian Kozynski | 01f0768 | 2019-06-06 14:07:17 -0400 | [diff] [blame] | 126 | /** |
| 127 | * Handles clicks by the user. |
| 128 | * |
| 129 | * Calls to the controller should be made here to set the new state of the device. |
| 130 | */ |
Chris Wren | 9e7283f | 2015-05-08 17:23:47 -0400 | [diff] [blame] | 131 | abstract protected void handleClick(); |
Jason Monk | 3250885 | 2017-01-18 09:17:13 -0500 | [diff] [blame] | 132 | |
Fabian Kozynski | 01f0768 | 2019-06-06 14:07:17 -0400 | [diff] [blame] | 133 | /** |
| 134 | * Update state of the tile based on device state |
| 135 | * |
| 136 | * Called whenever the state of the tile needs to be updated, either after user |
| 137 | * interaction or from callbacks from the controller. It populates {@code state} with the |
| 138 | * information to display to the user. |
| 139 | * |
| 140 | * @param state {@link TState} to populate with information to display |
| 141 | * @param arg additional arguments needed to populate {@code state} |
| 142 | */ |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 143 | abstract protected void handleUpdateState(TState state, Object arg); |
| 144 | |
Chris Wren | 457a21c | 2015-05-06 17:50:34 -0400 | [diff] [blame] | 145 | /** |
| 146 | * Declare the category of this tile. |
| 147 | * |
Jason Monk | 8c09ac7 | 2017-03-16 11:53:40 -0400 | [diff] [blame] | 148 | * Categories are defined in {@link com.android.internal.logging.nano.MetricsProto.MetricsEvent} |
Chris Wren | f6e9228b | 2016-01-26 18:04:35 -0500 | [diff] [blame] | 149 | * by editing frameworks/base/proto/src/metrics_constants.proto. |
Chris Wren | 457a21c | 2015-05-06 17:50:34 -0400 | [diff] [blame] | 150 | */ |
| 151 | abstract public int getMetricsCategory(); |
| 152 | |
Jason Monk | 702e2eb | 2017-03-03 16:53:44 -0500 | [diff] [blame] | 153 | protected QSTileImpl(QSHost host) { |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 154 | mHost = host; |
| 155 | mContext = host.getContext(); |
Fabian Kozynski | 2ff6df9 | 2020-04-24 12:00:49 -0400 | [diff] [blame] | 156 | mInstanceId = host.getNewInstanceId(); |
Fabian Kozynski | 67ffcf8 | 2019-09-05 16:32:12 -0400 | [diff] [blame] | 157 | mState = newTileState(); |
| 158 | mTmpState = newTileState(); |
Fabian Kozynski | ca30f57 | 2019-10-24 10:01:04 -0400 | [diff] [blame] | 159 | mQSLogger = host.getQSLogger(); |
Fabian Kozynski | 2ff6df9 | 2020-04-24 12:00:49 -0400 | [diff] [blame] | 160 | mUiEventLogger = host.getUiEventLogger(); |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 161 | } |
Jason Monk | bd6dbb0 | 2015-09-03 15:46:25 -0400 | [diff] [blame] | 162 | |
Fabian Kozynski | 05843f0 | 2019-06-28 13:19:57 -0400 | [diff] [blame] | 163 | protected final void resetStates() { |
| 164 | mState = newTileState(); |
| 165 | mTmpState = newTileState(); |
| 166 | } |
| 167 | |
Jason Monk | fa452ef | 2018-12-26 17:26:10 -0500 | [diff] [blame] | 168 | @NonNull |
| 169 | @Override |
| 170 | public Lifecycle getLifecycle() { |
| 171 | return mLifecycle; |
| 172 | } |
| 173 | |
Fabian Kozynski | 2ff6df9 | 2020-04-24 12:00:49 -0400 | [diff] [blame] | 174 | @Override |
| 175 | public InstanceId getInstanceId() { |
| 176 | return mInstanceId; |
| 177 | } |
| 178 | |
Jason Monk | 1bec6af | 2016-05-31 15:40:58 -0400 | [diff] [blame] | 179 | /** |
| 180 | * Adds or removes a listening client for the tile. If the tile has one or more |
| 181 | * listening client it will go into the listening state. |
| 182 | */ |
| 183 | public void setListening(Object listener, boolean listening) { |
Amin Shaikh | 572230b | 2018-03-20 17:24:57 -0400 | [diff] [blame] | 184 | mHandler.obtainMessage(H.SET_LISTENING, listening ? 1 : 0, 0, listener).sendToTarget(); |
Jason Monk | 1bec6af | 2016-05-31 15:40:58 -0400 | [diff] [blame] | 185 | } |
| 186 | |
Jason Monk | 1c6116c | 2017-09-06 17:33:01 -0400 | [diff] [blame] | 187 | protected long getStaleTimeout() { |
| 188 | return DEFAULT_STALE_TIMEOUT; |
| 189 | } |
| 190 | |
| 191 | @VisibleForTesting |
| 192 | protected void handleStale() { |
| 193 | setListening(mStaleListener, true); |
| 194 | } |
| 195 | |
Jason Monk | bd6dbb0 | 2015-09-03 15:46:25 -0400 | [diff] [blame] | 196 | public String getTileSpec() { |
| 197 | return mTileSpec; |
| 198 | } |
| 199 | |
| 200 | public void setTileSpec(String tileSpec) { |
| 201 | mTileSpec = tileSpec; |
| 202 | } |
| 203 | |
Jason Monk | 702e2eb | 2017-03-03 16:53:44 -0500 | [diff] [blame] | 204 | public QSHost getHost() { |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 205 | return mHost; |
| 206 | } |
| 207 | |
Fabian Kozynski | 01f0768 | 2019-06-06 14:07:17 -0400 | [diff] [blame] | 208 | /** |
| 209 | * Return the {@link QSIconView} to be used by this tile's view. |
| 210 | * |
| 211 | * @param context view context for the view |
| 212 | * @return icon view for this tile |
| 213 | */ |
Jason Monk | dc35dcb | 2015-12-04 16:36:15 -0500 | [diff] [blame] | 214 | public QSIconView createTileView(Context context) { |
Jason Monk | 702e2eb | 2017-03-03 16:53:44 -0500 | [diff] [blame] | 215 | return new QSIconViewImpl(context); |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 216 | } |
| 217 | |
John Spurlock | 7f8f22a | 2014-07-02 18:54:17 -0400 | [diff] [blame] | 218 | public DetailAdapter getDetailAdapter() { |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 219 | return null; // optional |
| 220 | } |
| 221 | |
Jason Monk | 3250885 | 2017-01-18 09:17:13 -0500 | [diff] [blame] | 222 | protected DetailAdapter createDetailAdapter() { |
| 223 | throw new UnsupportedOperationException(); |
| 224 | } |
Muyuan Li | 0e9f538 | 2016-04-27 15:51:15 -0700 | [diff] [blame] | 225 | |
Jason Monk | c3f42c1 | 2016-02-05 12:33:13 -0500 | [diff] [blame] | 226 | /** |
| 227 | * Is a startup check whether this device currently supports this tile. |
| 228 | * Should not be used to conditionally hide tiles. Only checked on tile |
| 229 | * creation or whether should be shown in edit screen. |
| 230 | */ |
| 231 | public boolean isAvailable() { |
| 232 | return true; |
| 233 | } |
| 234 | |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 235 | // safe to call from any thread |
| 236 | |
Jason Monk | ca894a0 | 2016-01-12 15:30:22 -0500 | [diff] [blame] | 237 | public void addCallback(Callback callback) { |
| 238 | mHandler.obtainMessage(H.ADD_CALLBACK, callback).sendToTarget(); |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 239 | } |
| 240 | |
Xiaohui Chen | 08e266c | 2016-04-18 12:53:28 -0700 | [diff] [blame] | 241 | public void removeCallback(Callback callback) { |
| 242 | mHandler.obtainMessage(H.REMOVE_CALLBACK, callback).sendToTarget(); |
| 243 | } |
| 244 | |
Jason Monk | 9d02a43 | 2016-01-20 16:33:46 -0500 | [diff] [blame] | 245 | public void removeCallbacks() { |
| 246 | mHandler.sendEmptyMessage(H.REMOVE_CALLBACKS); |
| 247 | } |
| 248 | |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 249 | public void click() { |
Susi Kharraz-Post | 9b03367 | 2018-11-28 08:14:07 -0500 | [diff] [blame] | 250 | mMetricsLogger.write(populate(new LogMaker(ACTION_QS_CLICK).setType(TYPE_ACTION) |
| 251 | .addTaggedData(FIELD_STATUS_BAR_STATE, |
| 252 | mStatusBarStateController.getState()))); |
Fabian Kozynski | 2ff6df9 | 2020-04-24 12:00:49 -0400 | [diff] [blame] | 253 | mUiEventLogger.logWithInstanceId(QSEvent.QS_ACTION_CLICK, 0, getMetricsSpec(), |
| 254 | getInstanceId()); |
Fabian Kozynski | ca30f57 | 2019-10-24 10:01:04 -0400 | [diff] [blame] | 255 | mQSLogger.logTileClick(mTileSpec, mStatusBarStateController.getState(), mState.state); |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 256 | mHandler.sendEmptyMessage(H.CLICK); |
| 257 | } |
| 258 | |
| 259 | public void secondaryClick() { |
Susi Kharraz-Post | 9b03367 | 2018-11-28 08:14:07 -0500 | [diff] [blame] | 260 | mMetricsLogger.write(populate(new LogMaker(ACTION_QS_SECONDARY_CLICK).setType(TYPE_ACTION) |
| 261 | .addTaggedData(FIELD_STATUS_BAR_STATE, |
| 262 | mStatusBarStateController.getState()))); |
Fabian Kozynski | 2ff6df9 | 2020-04-24 12:00:49 -0400 | [diff] [blame] | 263 | mUiEventLogger.logWithInstanceId(QSEvent.QS_ACTION_SECONDARY_CLICK, 0, getMetricsSpec(), |
| 264 | getInstanceId()); |
Fabian Kozynski | ca30f57 | 2019-10-24 10:01:04 -0400 | [diff] [blame] | 265 | mQSLogger.logTileSecondaryClick(mTileSpec, mStatusBarStateController.getState(), |
| 266 | mState.state); |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 267 | mHandler.sendEmptyMessage(H.SECONDARY_CLICK); |
| 268 | } |
| 269 | |
John Spurlock | c247b8f | 2014-11-06 23:06:25 -0500 | [diff] [blame] | 270 | public void longClick() { |
Susi Kharraz-Post | 9b03367 | 2018-11-28 08:14:07 -0500 | [diff] [blame] | 271 | mMetricsLogger.write(populate(new LogMaker(ACTION_QS_LONG_PRESS).setType(TYPE_ACTION) |
| 272 | .addTaggedData(FIELD_STATUS_BAR_STATE, |
| 273 | mStatusBarStateController.getState()))); |
Fabian Kozynski | 2ff6df9 | 2020-04-24 12:00:49 -0400 | [diff] [blame] | 274 | mUiEventLogger.logWithInstanceId(QSEvent.QS_ACTION_LONG_PRESS, 0, getMetricsSpec(), |
| 275 | getInstanceId()); |
Fabian Kozynski | ca30f57 | 2019-10-24 10:01:04 -0400 | [diff] [blame] | 276 | mQSLogger.logTileLongClick(mTileSpec, mStatusBarStateController.getState(), mState.state); |
John Spurlock | c247b8f | 2014-11-06 23:06:25 -0500 | [diff] [blame] | 277 | mHandler.sendEmptyMessage(H.LONG_CLICK); |
Rohan Shah | db2cfa3 | 2018-02-20 11:27:22 -0800 | [diff] [blame] | 278 | |
| 279 | Prefs.putInt( |
| 280 | mContext, |
| 281 | Prefs.Key.QS_LONG_PRESS_TOOLTIP_SHOWN_COUNT, |
Rohan Shah | d3cf756 | 2018-02-23 11:12:28 -0800 | [diff] [blame] | 282 | QuickStatusBarHeader.MAX_TOOLTIP_SHOWN_COUNT); |
John Spurlock | c247b8f | 2014-11-06 23:06:25 -0500 | [diff] [blame] | 283 | } |
| 284 | |
Jason Monk | cb4b31d | 2017-05-03 10:37:34 -0400 | [diff] [blame] | 285 | public LogMaker populate(LogMaker logMaker) { |
Jason Monk | 8c09ac7 | 2017-03-16 11:53:40 -0400 | [diff] [blame] | 286 | if (mState instanceof BooleanState) { |
| 287 | logMaker.addTaggedData(FIELD_QS_VALUE, ((BooleanState) mState).value ? 1 : 0); |
| 288 | } |
| 289 | return logMaker.setSubtype(getMetricsCategory()) |
Susi Kharraz-Post | 9b03367 | 2018-11-28 08:14:07 -0500 | [diff] [blame] | 290 | .addTaggedData(FIELD_IS_FULL_QS, mIsFullQs) |
Jason Monk | 8c09ac7 | 2017-03-16 11:53:40 -0400 | [diff] [blame] | 291 | .addTaggedData(FIELD_QS_POSITION, mHost.indexOf(mTileSpec)); |
| 292 | } |
| 293 | |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 294 | public void showDetail(boolean show) { |
| 295 | mHandler.obtainMessage(H.SHOW_DETAIL, show ? 1 : 0, 0).sendToTarget(); |
| 296 | } |
| 297 | |
Evan Laird | 6b28473 | 2017-02-28 17:27:04 -0500 | [diff] [blame] | 298 | public void refreshState() { |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 299 | refreshState(null); |
| 300 | } |
| 301 | |
| 302 | protected final void refreshState(Object arg) { |
| 303 | mHandler.obtainMessage(H.REFRESH_STATE, arg).sendToTarget(); |
| 304 | } |
| 305 | |
| 306 | public void userSwitch(int newUserId) { |
Adrian Roos | 32d88e8 | 2014-09-24 17:08:22 +0200 | [diff] [blame] | 307 | mHandler.obtainMessage(H.USER_SWITCH, newUserId, 0).sendToTarget(); |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 308 | } |
| 309 | |
John Spurlock | 7f8f22a | 2014-07-02 18:54:17 -0400 | [diff] [blame] | 310 | public void fireToggleStateChanged(boolean state) { |
| 311 | mHandler.obtainMessage(H.TOGGLE_STATE_CHANGED, state ? 1 : 0, 0).sendToTarget(); |
| 312 | } |
| 313 | |
John Spurlock | 486b78e | 2014-07-07 08:37:56 -0400 | [diff] [blame] | 314 | public void fireScanStateChanged(boolean state) { |
| 315 | mHandler.obtainMessage(H.SCAN_STATE_CHANGED, state ? 1 : 0, 0).sendToTarget(); |
| 316 | } |
| 317 | |
John Spurlock | bceed06 | 2014-08-10 18:04:16 -0400 | [diff] [blame] | 318 | public void destroy() { |
| 319 | mHandler.sendEmptyMessage(H.DESTROY); |
| 320 | } |
| 321 | |
| 322 | public TState getState() { |
| 323 | return mState; |
| 324 | } |
| 325 | |
Jason Monk | 0d6a1c4 | 2015-04-20 16:38:51 -0400 | [diff] [blame] | 326 | public void setDetailListening(boolean listening) { |
| 327 | // optional |
| 328 | } |
| 329 | |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 330 | // call only on tile worker looper |
| 331 | |
Jason Monk | ca894a0 | 2016-01-12 15:30:22 -0500 | [diff] [blame] | 332 | private void handleAddCallback(Callback callback) { |
| 333 | mCallbacks.add(callback); |
Jason Monk | 968d269 | 2016-06-17 13:48:44 -0400 | [diff] [blame] | 334 | callback.onStateChanged(mState); |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 335 | } |
| 336 | |
Xiaohui Chen | 08e266c | 2016-04-18 12:53:28 -0700 | [diff] [blame] | 337 | private void handleRemoveCallback(Callback callback) { |
| 338 | mCallbacks.remove(callback); |
| 339 | } |
| 340 | |
Jason Monk | 9d02a43 | 2016-01-20 16:33:46 -0500 | [diff] [blame] | 341 | private void handleRemoveCallbacks() { |
| 342 | mCallbacks.clear(); |
| 343 | } |
| 344 | |
Fabian Kozynski | 01f0768 | 2019-06-06 14:07:17 -0400 | [diff] [blame] | 345 | /** |
| 346 | * Handles secondary click on the tile. |
| 347 | * |
| 348 | * Defaults to {@link QSTileImpl#handleClick} |
| 349 | */ |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 350 | protected void handleSecondaryClick() { |
Jason Monk | dc35dcb | 2015-12-04 16:36:15 -0500 | [diff] [blame] | 351 | // Default to normal click. |
| 352 | handleClick(); |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 353 | } |
| 354 | |
Fabian Kozynski | 01f0768 | 2019-06-06 14:07:17 -0400 | [diff] [blame] | 355 | /** |
| 356 | * Handles long click on the tile by launching the {@link Intent} defined in |
| 357 | * {@link QSTileImpl#getLongClickIntent} |
| 358 | */ |
John Spurlock | c247b8f | 2014-11-06 23:06:25 -0500 | [diff] [blame] | 359 | protected void handleLongClick() { |
Jason Monk | 9c7844c | 2017-01-18 15:21:53 -0500 | [diff] [blame] | 360 | Dependency.get(ActivityStarter.class).postStartActivityDismissingKeyguard( |
| 361 | getLongClickIntent(), 0); |
John Spurlock | c247b8f | 2014-11-06 23:06:25 -0500 | [diff] [blame] | 362 | } |
| 363 | |
Fabian Kozynski | 01f0768 | 2019-06-06 14:07:17 -0400 | [diff] [blame] | 364 | /** |
| 365 | * Returns an intent to be launched when the tile is long pressed. |
| 366 | * |
| 367 | * @return the intent to launch |
| 368 | */ |
Jason Monk | 76c67aa | 2016-02-19 14:49:42 -0500 | [diff] [blame] | 369 | public abstract Intent getLongClickIntent(); |
| 370 | |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 371 | protected void handleRefreshState(Object arg) { |
| 372 | handleUpdateState(mTmpState, arg); |
| 373 | final boolean changed = mTmpState.copyTo(mState); |
| 374 | if (changed) { |
Fabian Kozynski | ca30f57 | 2019-10-24 10:01:04 -0400 | [diff] [blame] | 375 | mQSLogger.logTileUpdated(mTileSpec, mState); |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 376 | handleStateChanged(); |
| 377 | } |
Jason Monk | 1c6116c | 2017-09-06 17:33:01 -0400 | [diff] [blame] | 378 | mHandler.removeMessages(H.STALE); |
| 379 | mHandler.sendEmptyMessageDelayed(H.STALE, getStaleTimeout()); |
| 380 | setListening(mStaleListener, false); |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 381 | } |
| 382 | |
| 383 | private void handleStateChanged() { |
Selim Cinek | 4fda7b2 | 2014-08-18 22:07:25 +0200 | [diff] [blame] | 384 | boolean delayAnnouncement = shouldAnnouncementBeDelayed(); |
Jason Monk | ca894a0 | 2016-01-12 15:30:22 -0500 | [diff] [blame] | 385 | if (mCallbacks.size() != 0) { |
| 386 | for (int i = 0; i < mCallbacks.size(); i++) { |
| 387 | mCallbacks.get(i).onStateChanged(mState); |
| 388 | } |
Selim Cinek | 4fda7b2 | 2014-08-18 22:07:25 +0200 | [diff] [blame] | 389 | if (mAnnounceNextStateChange && !delayAnnouncement) { |
| 390 | String announcement = composeChangeAnnouncement(); |
| 391 | if (announcement != null) { |
Jason Monk | ca894a0 | 2016-01-12 15:30:22 -0500 | [diff] [blame] | 392 | mCallbacks.get(0).onAnnouncementRequested(announcement); |
Selim Cinek | 4fda7b2 | 2014-08-18 22:07:25 +0200 | [diff] [blame] | 393 | } |
| 394 | } |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 395 | } |
Selim Cinek | 4fda7b2 | 2014-08-18 22:07:25 +0200 | [diff] [blame] | 396 | mAnnounceNextStateChange = mAnnounceNextStateChange && delayAnnouncement; |
| 397 | } |
| 398 | |
| 399 | protected boolean shouldAnnouncementBeDelayed() { |
| 400 | return false; |
| 401 | } |
| 402 | |
| 403 | protected String composeChangeAnnouncement() { |
| 404 | return null; |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 405 | } |
| 406 | |
| 407 | private void handleShowDetail(boolean show) { |
Jason Monk | be3235a | 2017-04-05 09:29:53 -0400 | [diff] [blame] | 408 | mShowingDetail = show; |
Jason Monk | ca894a0 | 2016-01-12 15:30:22 -0500 | [diff] [blame] | 409 | for (int i = 0; i < mCallbacks.size(); i++) { |
| 410 | mCallbacks.get(i).onShowDetail(show); |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 411 | } |
| 412 | } |
| 413 | |
Jason Monk | be3235a | 2017-04-05 09:29:53 -0400 | [diff] [blame] | 414 | protected boolean isShowingDetail() { |
| 415 | return mShowingDetail; |
| 416 | } |
| 417 | |
John Spurlock | 7f8f22a | 2014-07-02 18:54:17 -0400 | [diff] [blame] | 418 | private void handleToggleStateChanged(boolean state) { |
Jason Monk | ca894a0 | 2016-01-12 15:30:22 -0500 | [diff] [blame] | 419 | for (int i = 0; i < mCallbacks.size(); i++) { |
| 420 | mCallbacks.get(i).onToggleStateChanged(state); |
John Spurlock | 7f8f22a | 2014-07-02 18:54:17 -0400 | [diff] [blame] | 421 | } |
| 422 | } |
| 423 | |
John Spurlock | 486b78e | 2014-07-07 08:37:56 -0400 | [diff] [blame] | 424 | private void handleScanStateChanged(boolean state) { |
Jason Monk | ca894a0 | 2016-01-12 15:30:22 -0500 | [diff] [blame] | 425 | for (int i = 0; i < mCallbacks.size(); i++) { |
| 426 | mCallbacks.get(i).onScanStateChanged(state); |
John Spurlock | 486b78e | 2014-07-07 08:37:56 -0400 | [diff] [blame] | 427 | } |
| 428 | } |
| 429 | |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 430 | protected void handleUserSwitch(int newUserId) { |
| 431 | handleRefreshState(null); |
| 432 | } |
| 433 | |
Amin Shaikh | 572230b | 2018-03-20 17:24:57 -0400 | [diff] [blame] | 434 | private void handleSetListeningInternal(Object listener, boolean listening) { |
Fabian Kozynski | f455951 | 2020-06-23 14:02:58 -0400 | [diff] [blame] | 435 | // This should be used to go from resumed to paused. Listening for ON_RESUME and ON_PAUSE |
| 436 | // in this lifecycle will determine the listening window. |
Amin Shaikh | 572230b | 2018-03-20 17:24:57 -0400 | [diff] [blame] | 437 | if (listening) { |
| 438 | if (mListeners.add(listener) && mListeners.size() == 1) { |
| 439 | if (DEBUG) Log.d(TAG, "handleSetListening true"); |
Fabian Kozynski | f455951 | 2020-06-23 14:02:58 -0400 | [diff] [blame] | 440 | mLifecycle.setCurrentState(RESUMED); |
Amin Shaikh | 572230b | 2018-03-20 17:24:57 -0400 | [diff] [blame] | 441 | handleSetListening(listening); |
| 442 | refreshState(); // Ensure we get at least one refresh after listening. |
| 443 | } |
| 444 | } else { |
| 445 | if (mListeners.remove(listener) && mListeners.size() == 0) { |
| 446 | if (DEBUG) Log.d(TAG, "handleSetListening false"); |
Fabian Kozynski | f455951 | 2020-06-23 14:02:58 -0400 | [diff] [blame] | 447 | mLifecycle.setCurrentState(STARTED); |
Amin Shaikh | 572230b | 2018-03-20 17:24:57 -0400 | [diff] [blame] | 448 | handleSetListening(listening); |
| 449 | } |
| 450 | } |
| 451 | updateIsFullQs(); |
| 452 | } |
| 453 | |
| 454 | private void updateIsFullQs() { |
| 455 | for (Object listener : mListeners) { |
| 456 | if (TilePage.class.equals(listener.getClass())) { |
| 457 | mIsFullQs = 1; |
| 458 | return; |
| 459 | } |
| 460 | } |
| 461 | mIsFullQs = 0; |
| 462 | } |
| 463 | |
Fabian Kozynski | ca30f57 | 2019-10-24 10:01:04 -0400 | [diff] [blame] | 464 | @CallSuper |
| 465 | protected void handleSetListening(boolean listening) { |
| 466 | if (mTileSpec != null) { |
| 467 | mQSLogger.logTileChangeListening(mTileSpec, listening); |
| 468 | } |
| 469 | } |
Jason Monk | 1bec6af | 2016-05-31 15:40:58 -0400 | [diff] [blame] | 470 | |
John Spurlock | bceed06 | 2014-08-10 18:04:16 -0400 | [diff] [blame] | 471 | protected void handleDestroy() { |
Fabian Kozynski | ca30f57 | 2019-10-24 10:01:04 -0400 | [diff] [blame] | 472 | mQSLogger.logTileDestroyed(mTileSpec, "Handle destroy"); |
Jason Monk | 794bcd2 | 2017-06-06 16:38:00 -0400 | [diff] [blame] | 473 | if (mListeners.size() != 0) { |
Jason Monk | 1c6116c | 2017-09-06 17:33:01 -0400 | [diff] [blame] | 474 | handleSetListening(false); |
Jason Monk | 794bcd2 | 2017-06-06 16:38:00 -0400 | [diff] [blame] | 475 | } |
Jason Monk | ca894a0 | 2016-01-12 15:30:22 -0500 | [diff] [blame] | 476 | mCallbacks.clear(); |
Fabian Kozynski | 30aa3ad | 2019-12-12 15:12:37 -0500 | [diff] [blame] | 477 | mHandler.removeCallbacksAndMessages(null); |
John Spurlock | bceed06 | 2014-08-10 18:04:16 -0400 | [diff] [blame] | 478 | } |
| 479 | |
Sudheer Shanka | a8fbbb3 | 2016-02-11 17:17:57 +0000 | [diff] [blame] | 480 | protected void checkIfRestrictionEnforcedByAdminOnly(State state, String userRestriction) { |
Philip P. Moltmann | 4e615e6 | 2018-08-28 14:57:49 -0700 | [diff] [blame] | 481 | EnforcedAdmin admin = RestrictedLockUtilsInternal.checkIfRestrictionEnforced(mContext, |
Sudheer Shanka | ad143c0 | 2016-03-31 00:24:05 +0000 | [diff] [blame] | 482 | userRestriction, ActivityManager.getCurrentUser()); |
Philip P. Moltmann | 4e615e6 | 2018-08-28 14:57:49 -0700 | [diff] [blame] | 483 | if (admin != null && !RestrictedLockUtilsInternal.hasBaseUserRestriction(mContext, |
Sudheer Shanka | ad143c0 | 2016-03-31 00:24:05 +0000 | [diff] [blame] | 484 | userRestriction, ActivityManager.getCurrentUser())) { |
Sudheer Shanka | 1c7cda8 | 2015-12-31 14:46:02 +0000 | [diff] [blame] | 485 | state.disabledByPolicy = true; |
Jason Monk | 702e2eb | 2017-03-03 16:53:44 -0500 | [diff] [blame] | 486 | mEnforcedAdmin = admin; |
Sudheer Shanka | 1c7cda8 | 2015-12-31 14:46:02 +0000 | [diff] [blame] | 487 | } else { |
| 488 | state.disabledByPolicy = false; |
Jason Monk | 702e2eb | 2017-03-03 16:53:44 -0500 | [diff] [blame] | 489 | mEnforcedAdmin = null; |
Sudheer Shanka | 1c7cda8 | 2015-12-31 14:46:02 +0000 | [diff] [blame] | 490 | } |
| 491 | } |
| 492 | |
Fabian Kozynski | 2ff6df9 | 2020-04-24 12:00:49 -0400 | [diff] [blame] | 493 | @Override |
| 494 | public String getMetricsSpec() { |
| 495 | return mTileSpec; |
| 496 | } |
| 497 | |
Fabian Kozynski | 01f0768 | 2019-06-06 14:07:17 -0400 | [diff] [blame] | 498 | /** |
| 499 | * Provides a default label for the tile. |
| 500 | * @return default label for the tile. |
| 501 | */ |
Jason Monk | 39c98e6 | 2016-03-16 09:18:35 -0400 | [diff] [blame] | 502 | public abstract CharSequence getTileLabel(); |
| 503 | |
Jason Monk | 3250885 | 2017-01-18 09:17:13 -0500 | [diff] [blame] | 504 | public static int getColorForState(Context context, int state) { |
| 505 | switch (state) { |
| 506 | case Tile.STATE_UNAVAILABLE: |
| 507 | return Utils.getDisabled(context, |
Jason Chang | b4e879d | 2018-04-11 11:17:58 +0800 | [diff] [blame] | 508 | Utils.getColorAttrDefaultColor(context, android.R.attr.textColorSecondary)); |
Jason Monk | b4cc7b1 | 2017-05-09 13:50:47 -0400 | [diff] [blame] | 509 | case Tile.STATE_INACTIVE: |
Jason Chang | b4e879d | 2018-04-11 11:17:58 +0800 | [diff] [blame] | 510 | return Utils.getColorAttrDefaultColor(context, android.R.attr.textColorSecondary); |
Jason Monk | 3250885 | 2017-01-18 09:17:13 -0500 | [diff] [blame] | 511 | case Tile.STATE_ACTIVE: |
Fabian Kozynski | e87ef4d | 2020-02-04 10:27:22 -0500 | [diff] [blame] | 512 | return Utils.getColorAttrDefaultColor(context, android.R.attr.colorPrimary); |
Jason Monk | 3250885 | 2017-01-18 09:17:13 -0500 | [diff] [blame] | 513 | default: |
| 514 | Log.e("QSTile", "Invalid state " + state); |
| 515 | return 0; |
| 516 | } |
| 517 | } |
| 518 | |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 519 | protected final class H extends Handler { |
Jason Monk | ca894a0 | 2016-01-12 15:30:22 -0500 | [diff] [blame] | 520 | private static final int ADD_CALLBACK = 1; |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 521 | private static final int CLICK = 2; |
| 522 | private static final int SECONDARY_CLICK = 3; |
John Spurlock | c247b8f | 2014-11-06 23:06:25 -0500 | [diff] [blame] | 523 | private static final int LONG_CLICK = 4; |
| 524 | private static final int REFRESH_STATE = 5; |
| 525 | private static final int SHOW_DETAIL = 6; |
| 526 | private static final int USER_SWITCH = 7; |
| 527 | private static final int TOGGLE_STATE_CHANGED = 8; |
| 528 | private static final int SCAN_STATE_CHANGED = 9; |
| 529 | private static final int DESTROY = 10; |
Amin Shaikh | 299c45c | 2018-08-16 10:49:48 -0400 | [diff] [blame] | 530 | private static final int REMOVE_CALLBACKS = 11; |
| 531 | private static final int REMOVE_CALLBACK = 12; |
| 532 | private static final int SET_LISTENING = 13; |
| 533 | private static final int STALE = 14; |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 534 | |
Jason Monk | 1c6116c | 2017-09-06 17:33:01 -0400 | [diff] [blame] | 535 | @VisibleForTesting |
| 536 | protected H(Looper looper) { |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 537 | super(looper); |
| 538 | } |
| 539 | |
| 540 | @Override |
| 541 | public void handleMessage(Message msg) { |
| 542 | String name = null; |
| 543 | try { |
Jason Monk | ca894a0 | 2016-01-12 15:30:22 -0500 | [diff] [blame] | 544 | if (msg.what == ADD_CALLBACK) { |
| 545 | name = "handleAddCallback"; |
Jason Monk | 9d02a43 | 2016-01-20 16:33:46 -0500 | [diff] [blame] | 546 | handleAddCallback((QSTile.Callback) msg.obj); |
| 547 | } else if (msg.what == REMOVE_CALLBACKS) { |
| 548 | name = "handleRemoveCallbacks"; |
| 549 | handleRemoveCallbacks(); |
Xiaohui Chen | 08e266c | 2016-04-18 12:53:28 -0700 | [diff] [blame] | 550 | } else if (msg.what == REMOVE_CALLBACK) { |
| 551 | name = "handleRemoveCallback"; |
| 552 | handleRemoveCallback((QSTile.Callback) msg.obj); |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 553 | } else if (msg.what == CLICK) { |
| 554 | name = "handleClick"; |
Sudheer Shanka | 1c7cda8 | 2015-12-31 14:46:02 +0000 | [diff] [blame] | 555 | if (mState.disabledByPolicy) { |
| 556 | Intent intent = RestrictedLockUtils.getShowAdminSupportDetailsIntent( |
Jason Monk | 702e2eb | 2017-03-03 16:53:44 -0500 | [diff] [blame] | 557 | mContext, mEnforcedAdmin); |
Jason Monk | 9c7844c | 2017-01-18 15:21:53 -0500 | [diff] [blame] | 558 | Dependency.get(ActivityStarter.class).postStartActivityDismissingKeyguard( |
| 559 | intent, 0); |
Sudheer Shanka | 1c7cda8 | 2015-12-31 14:46:02 +0000 | [diff] [blame] | 560 | } else { |
Sudheer Shanka | 1c7cda8 | 2015-12-31 14:46:02 +0000 | [diff] [blame] | 561 | handleClick(); |
| 562 | } |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 563 | } else if (msg.what == SECONDARY_CLICK) { |
| 564 | name = "handleSecondaryClick"; |
| 565 | handleSecondaryClick(); |
John Spurlock | c247b8f | 2014-11-06 23:06:25 -0500 | [diff] [blame] | 566 | } else if (msg.what == LONG_CLICK) { |
| 567 | name = "handleLongClick"; |
| 568 | handleLongClick(); |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 569 | } else if (msg.what == REFRESH_STATE) { |
| 570 | name = "handleRefreshState"; |
| 571 | handleRefreshState(msg.obj); |
| 572 | } else if (msg.what == SHOW_DETAIL) { |
| 573 | name = "handleShowDetail"; |
| 574 | handleShowDetail(msg.arg1 != 0); |
| 575 | } else if (msg.what == USER_SWITCH) { |
| 576 | name = "handleUserSwitch"; |
| 577 | handleUserSwitch(msg.arg1); |
John Spurlock | 7f8f22a | 2014-07-02 18:54:17 -0400 | [diff] [blame] | 578 | } else if (msg.what == TOGGLE_STATE_CHANGED) { |
| 579 | name = "handleToggleStateChanged"; |
| 580 | handleToggleStateChanged(msg.arg1 != 0); |
John Spurlock | 486b78e | 2014-07-07 08:37:56 -0400 | [diff] [blame] | 581 | } else if (msg.what == SCAN_STATE_CHANGED) { |
| 582 | name = "handleScanStateChanged"; |
| 583 | handleScanStateChanged(msg.arg1 != 0); |
John Spurlock | bceed06 | 2014-08-10 18:04:16 -0400 | [diff] [blame] | 584 | } else if (msg.what == DESTROY) { |
| 585 | name = "handleDestroy"; |
| 586 | handleDestroy(); |
Jason Monk | 1bec6af | 2016-05-31 15:40:58 -0400 | [diff] [blame] | 587 | } else if (msg.what == SET_LISTENING) { |
Amin Shaikh | 572230b | 2018-03-20 17:24:57 -0400 | [diff] [blame] | 588 | name = "handleSetListeningInternal"; |
| 589 | handleSetListeningInternal(msg.obj, msg.arg1 != 0); |
Jason Monk | 1c6116c | 2017-09-06 17:33:01 -0400 | [diff] [blame] | 590 | } else if (msg.what == STALE) { |
| 591 | name = "handleStale"; |
| 592 | handleStale(); |
John Spurlock | bceed06 | 2014-08-10 18:04:16 -0400 | [diff] [blame] | 593 | } else { |
| 594 | throw new IllegalArgumentException("Unknown msg: " + msg.what); |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 595 | } |
| 596 | } catch (Throwable t) { |
| 597 | final String error = "Error in " + name; |
| 598 | Log.w(TAG, error, t); |
| 599 | mHost.warn(error, t); |
| 600 | } |
| 601 | } |
| 602 | } |
| 603 | |
Jason Monk | 5db8a41 | 2015-10-21 15:16:23 -0700 | [diff] [blame] | 604 | public static class DrawableIcon extends Icon { |
| 605 | protected final Drawable mDrawable; |
Kensuke Matsui | 5a58cf9 | 2017-07-05 14:53:05 +0900 | [diff] [blame] | 606 | protected final Drawable mInvisibleDrawable; |
Jason Monk | 5db8a41 | 2015-10-21 15:16:23 -0700 | [diff] [blame] | 607 | |
| 608 | public DrawableIcon(Drawable drawable) { |
| 609 | mDrawable = drawable; |
Kensuke Matsui | 5a58cf9 | 2017-07-05 14:53:05 +0900 | [diff] [blame] | 610 | mInvisibleDrawable = drawable.getConstantState().newDrawable(); |
Jason Monk | 5db8a41 | 2015-10-21 15:16:23 -0700 | [diff] [blame] | 611 | } |
| 612 | |
| 613 | @Override |
| 614 | public Drawable getDrawable(Context context) { |
| 615 | return mDrawable; |
| 616 | } |
Kensuke Matsui | 5a58cf9 | 2017-07-05 14:53:05 +0900 | [diff] [blame] | 617 | |
| 618 | @Override |
| 619 | public Drawable getInvisibleDrawable(Context context) { |
| 620 | return mInvisibleDrawable; |
| 621 | } |
Fabian Kozynski | ca30f57 | 2019-10-24 10:01:04 -0400 | [diff] [blame] | 622 | |
| 623 | @Override |
| 624 | @NonNull |
| 625 | public String toString() { |
| 626 | return "DrawableIcon"; |
| 627 | } |
Jason Monk | d2274f8 | 2016-12-12 12:02:16 -0500 | [diff] [blame] | 628 | } |
| 629 | |
| 630 | public static class DrawableIconWithRes extends DrawableIcon { |
| 631 | private final int mId; |
| 632 | |
| 633 | public DrawableIconWithRes(Drawable drawable, int id) { |
| 634 | super(drawable); |
| 635 | mId = id; |
| 636 | } |
Jason Monk | 1aec93f | 2016-03-01 09:39:30 -0500 | [diff] [blame] | 637 | |
| 638 | @Override |
Jason Monk | d2274f8 | 2016-12-12 12:02:16 -0500 | [diff] [blame] | 639 | public boolean equals(Object o) { |
| 640 | return o instanceof DrawableIconWithRes && ((DrawableIconWithRes) o).mId == mId; |
Jason Monk | 1aec93f | 2016-03-01 09:39:30 -0500 | [diff] [blame] | 641 | } |
Fabian Kozynski | ca30f57 | 2019-10-24 10:01:04 -0400 | [diff] [blame] | 642 | |
| 643 | @Override |
| 644 | @NonNull |
| 645 | public String toString() { |
| 646 | return String.format("DrawableIconWithRes[resId=0x%08x]", mId); |
| 647 | } |
Jason Monk | 5db8a41 | 2015-10-21 15:16:23 -0700 | [diff] [blame] | 648 | } |
| 649 | |
John Spurlock | 2d69581 | 2014-10-30 13:25:21 -0400 | [diff] [blame] | 650 | public static class ResourceIcon extends Icon { |
| 651 | private static final SparseArray<Icon> ICONS = new SparseArray<Icon>(); |
| 652 | |
Andrew Flynn | a478d70 | 2015-04-14 23:33:45 -0400 | [diff] [blame] | 653 | protected final int mResId; |
John Spurlock | 2d69581 | 2014-10-30 13:25:21 -0400 | [diff] [blame] | 654 | |
| 655 | private ResourceIcon(int resId) { |
| 656 | mResId = resId; |
| 657 | } |
| 658 | |
Fabian Kozynski | fa1a1e2 | 2019-05-13 15:24:10 -0400 | [diff] [blame] | 659 | public static synchronized Icon get(int resId) { |
John Spurlock | 2d69581 | 2014-10-30 13:25:21 -0400 | [diff] [blame] | 660 | Icon icon = ICONS.get(resId); |
| 661 | if (icon == null) { |
| 662 | icon = new ResourceIcon(resId); |
| 663 | ICONS.put(resId, icon); |
| 664 | } |
| 665 | return icon; |
| 666 | } |
| 667 | |
| 668 | @Override |
| 669 | public Drawable getDrawable(Context context) { |
Jason Monk | 66239fb | 2015-12-21 14:27:00 -0500 | [diff] [blame] | 670 | return context.getDrawable(mResId); |
John Spurlock | 2d69581 | 2014-10-30 13:25:21 -0400 | [diff] [blame] | 671 | } |
| 672 | |
| 673 | @Override |
Jason Monk | 1aec93f | 2016-03-01 09:39:30 -0500 | [diff] [blame] | 674 | public Drawable getInvisibleDrawable(Context context) { |
| 675 | return context.getDrawable(mResId); |
| 676 | } |
| 677 | |
| 678 | @Override |
John Spurlock | 2d69581 | 2014-10-30 13:25:21 -0400 | [diff] [blame] | 679 | public boolean equals(Object o) { |
| 680 | return o instanceof ResourceIcon && ((ResourceIcon) o).mResId == mResId; |
| 681 | } |
| 682 | |
| 683 | @Override |
Fabian Kozynski | ca30f57 | 2019-10-24 10:01:04 -0400 | [diff] [blame] | 684 | @NonNull |
John Spurlock | 2d69581 | 2014-10-30 13:25:21 -0400 | [diff] [blame] | 685 | public String toString() { |
| 686 | return String.format("ResourceIcon[resId=0x%08x]", mResId); |
| 687 | } |
| 688 | } |
| 689 | |
Jason Monk | 1c6116c | 2017-09-06 17:33:01 -0400 | [diff] [blame] | 690 | protected static class AnimationIcon extends ResourceIcon { |
Jason Monk | 1aec93f | 2016-03-01 09:39:30 -0500 | [diff] [blame] | 691 | private final int mAnimatedResId; |
| 692 | |
| 693 | public AnimationIcon(int resId, int staticResId) { |
| 694 | super(staticResId); |
| 695 | mAnimatedResId = resId; |
John Spurlock | 2d69581 | 2014-10-30 13:25:21 -0400 | [diff] [blame] | 696 | } |
| 697 | |
John Spurlock | 2d69581 | 2014-10-30 13:25:21 -0400 | [diff] [blame] | 698 | @Override |
| 699 | public Drawable getDrawable(Context context) { |
| 700 | // workaround: get a clean state for every new AVD |
Jason Monk | 1aec93f | 2016-03-01 09:39:30 -0500 | [diff] [blame] | 701 | return context.getDrawable(mAnimatedResId).getConstantState().newDrawable(); |
John Spurlock | 2d69581 | 2014-10-30 13:25:21 -0400 | [diff] [blame] | 702 | } |
Fabian Kozynski | ca30f57 | 2019-10-24 10:01:04 -0400 | [diff] [blame] | 703 | |
| 704 | @Override |
| 705 | @NonNull |
| 706 | public String toString() { |
| 707 | return String.format("AnimationIcon[resId=0x%08x]", mResId); |
| 708 | } |
John Spurlock | 2d69581 | 2014-10-30 13:25:21 -0400 | [diff] [blame] | 709 | } |
Fabian Kozynski | 00d494d | 2019-04-04 09:53:50 -0400 | [diff] [blame] | 710 | |
Fabian Kozynski | 05843f0 | 2019-06-28 13:19:57 -0400 | [diff] [blame] | 711 | /** |
| 712 | * Dumps the state of this tile along with its name. |
| 713 | * |
| 714 | * This may be used for CTS testing of tiles. |
| 715 | */ |
Fabian Kozynski | 00d494d | 2019-04-04 09:53:50 -0400 | [diff] [blame] | 716 | @Override |
| 717 | public void dump(FileDescriptor fd, PrintWriter pw, String[] args) { |
| 718 | pw.println(this.getClass().getSimpleName() + ":"); |
| 719 | pw.print(" "); pw.println(getState().toString()); |
| 720 | } |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 721 | } |