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