John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2014 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | package com.android.systemui.qs; |
| 18 | |
| 19 | import android.content.Context; |
| 20 | import android.content.Intent; |
Andrew Flynn | a478d70 | 2015-04-14 23:33:45 -0400 | [diff] [blame] | 21 | import android.graphics.drawable.Animatable; |
John Spurlock | 2d69581 | 2014-10-30 13:25:21 -0400 | [diff] [blame] | 22 | import android.graphics.drawable.AnimatedVectorDrawable; |
John Spurlock | 444eb2e | 2014-05-14 13:32:14 -0400 | [diff] [blame] | 23 | import android.graphics.drawable.Drawable; |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 24 | import android.os.Handler; |
| 25 | import android.os.Looper; |
| 26 | import android.os.Message; |
| 27 | import android.util.Log; |
John Spurlock | 2d69581 | 2014-10-30 13:25:21 -0400 | [diff] [blame] | 28 | import android.util.SparseArray; |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 29 | import android.view.View; |
| 30 | import android.view.ViewGroup; |
| 31 | |
| 32 | import com.android.systemui.qs.QSTile.State; |
| 33 | import com.android.systemui.statusbar.policy.BluetoothController; |
| 34 | import com.android.systemui.statusbar.policy.CastController; |
Adrian Roos | b83777b | 2014-06-30 15:11:53 +0200 | [diff] [blame] | 35 | import com.android.systemui.statusbar.policy.FlashlightController; |
John Spurlock | 657c62c | 2014-07-22 12:18:09 -0400 | [diff] [blame] | 36 | import com.android.systemui.statusbar.policy.KeyguardMonitor; |
John Spurlock | ccb6b9a | 2014-05-17 15:54:40 -0400 | [diff] [blame] | 37 | import com.android.systemui.statusbar.policy.Listenable; |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 38 | import com.android.systemui.statusbar.policy.LocationController; |
| 39 | import com.android.systemui.statusbar.policy.NetworkController; |
| 40 | import com.android.systemui.statusbar.policy.RotationLockController; |
Jason Monk | 51e4dc0 | 2014-07-22 12:00:47 -0400 | [diff] [blame] | 41 | import com.android.systemui.statusbar.policy.HotspotController; |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 42 | import com.android.systemui.statusbar.policy.ZenModeController; |
| 43 | |
John Spurlock | bceed06 | 2014-08-10 18:04:16 -0400 | [diff] [blame] | 44 | import java.util.Collection; |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 45 | import java.util.Objects; |
| 46 | |
| 47 | /** |
| 48 | * Base quick-settings tile, extend this to create a new tile. |
| 49 | * |
| 50 | * State management done on a looper provided by the host. Tiles should update state in |
| 51 | * handleUpdateState. Callbacks affecting state should use refreshState to trigger another |
| 52 | * state update pass on tile looper. |
| 53 | */ |
John Spurlock | ccb6b9a | 2014-05-17 15:54:40 -0400 | [diff] [blame] | 54 | public abstract class QSTile<TState extends State> implements Listenable { |
| 55 | protected final String TAG = "QSTile." + getClass().getSimpleName(); |
John Spurlock | 486b78e | 2014-07-07 08:37:56 -0400 | [diff] [blame] | 56 | protected static final boolean DEBUG = Log.isLoggable("QSTile", Log.DEBUG); |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 57 | |
| 58 | protected final Host mHost; |
| 59 | protected final Context mContext; |
| 60 | protected final H mHandler; |
John Spurlock | 76c43b9 | 2014-05-13 21:10:51 -0400 | [diff] [blame] | 61 | protected final Handler mUiHandler = new Handler(Looper.getMainLooper()); |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 62 | |
| 63 | private Callback mCallback; |
| 64 | protected final TState mState = newTileState(); |
| 65 | private final TState mTmpState = newTileState(); |
Selim Cinek | 4fda7b2 | 2014-08-18 22:07:25 +0200 | [diff] [blame] | 66 | private boolean mAnnounceNextStateChange; |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 67 | |
| 68 | abstract protected TState newTileState(); |
| 69 | abstract protected void handleClick(); |
| 70 | abstract protected void handleUpdateState(TState state, Object arg); |
| 71 | |
| 72 | protected QSTile(Host host) { |
| 73 | mHost = host; |
| 74 | mContext = host.getContext(); |
| 75 | mHandler = new H(host.getLooper()); |
| 76 | } |
| 77 | |
John Spurlock | ccb6b9a | 2014-05-17 15:54:40 -0400 | [diff] [blame] | 78 | public boolean supportsDualTargets() { |
| 79 | return false; |
| 80 | } |
| 81 | |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 82 | public Host getHost() { |
| 83 | return mHost; |
| 84 | } |
| 85 | |
| 86 | public QSTileView createTileView(Context context) { |
| 87 | return new QSTileView(context); |
| 88 | } |
| 89 | |
John Spurlock | 7f8f22a | 2014-07-02 18:54:17 -0400 | [diff] [blame] | 90 | public DetailAdapter getDetailAdapter() { |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 91 | return null; // optional |
| 92 | } |
| 93 | |
John Spurlock | 7f8f22a | 2014-07-02 18:54:17 -0400 | [diff] [blame] | 94 | public interface DetailAdapter { |
| 95 | int getTitle(); |
| 96 | Boolean getToggleState(); |
| 97 | View createDetailView(Context context, View convertView, ViewGroup parent); |
| 98 | Intent getSettingsIntent(); |
| 99 | void setToggleState(boolean state); |
| 100 | } |
| 101 | |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 102 | // safe to call from any thread |
| 103 | |
| 104 | public void setCallback(Callback callback) { |
| 105 | mHandler.obtainMessage(H.SET_CALLBACK, callback).sendToTarget(); |
| 106 | } |
| 107 | |
| 108 | public void click() { |
| 109 | mHandler.sendEmptyMessage(H.CLICK); |
| 110 | } |
| 111 | |
| 112 | public void secondaryClick() { |
| 113 | mHandler.sendEmptyMessage(H.SECONDARY_CLICK); |
| 114 | } |
| 115 | |
John Spurlock | c247b8f | 2014-11-06 23:06:25 -0500 | [diff] [blame] | 116 | public void longClick() { |
| 117 | mHandler.sendEmptyMessage(H.LONG_CLICK); |
| 118 | } |
| 119 | |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 120 | public void showDetail(boolean show) { |
| 121 | mHandler.obtainMessage(H.SHOW_DETAIL, show ? 1 : 0, 0).sendToTarget(); |
| 122 | } |
| 123 | |
| 124 | protected final void refreshState() { |
| 125 | refreshState(null); |
| 126 | } |
| 127 | |
| 128 | protected final void refreshState(Object arg) { |
| 129 | mHandler.obtainMessage(H.REFRESH_STATE, arg).sendToTarget(); |
| 130 | } |
| 131 | |
| 132 | public void userSwitch(int newUserId) { |
Adrian Roos | 32d88e8 | 2014-09-24 17:08:22 +0200 | [diff] [blame] | 133 | mHandler.obtainMessage(H.USER_SWITCH, newUserId, 0).sendToTarget(); |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 134 | } |
| 135 | |
John Spurlock | 7f8f22a | 2014-07-02 18:54:17 -0400 | [diff] [blame] | 136 | public void fireToggleStateChanged(boolean state) { |
| 137 | mHandler.obtainMessage(H.TOGGLE_STATE_CHANGED, state ? 1 : 0, 0).sendToTarget(); |
| 138 | } |
| 139 | |
John Spurlock | 486b78e | 2014-07-07 08:37:56 -0400 | [diff] [blame] | 140 | public void fireScanStateChanged(boolean state) { |
| 141 | mHandler.obtainMessage(H.SCAN_STATE_CHANGED, state ? 1 : 0, 0).sendToTarget(); |
| 142 | } |
| 143 | |
John Spurlock | bceed06 | 2014-08-10 18:04:16 -0400 | [diff] [blame] | 144 | public void destroy() { |
| 145 | mHandler.sendEmptyMessage(H.DESTROY); |
| 146 | } |
| 147 | |
| 148 | public TState getState() { |
| 149 | return mState; |
| 150 | } |
| 151 | |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 152 | // call only on tile worker looper |
| 153 | |
| 154 | private void handleSetCallback(Callback callback) { |
| 155 | mCallback = callback; |
| 156 | handleRefreshState(null); |
| 157 | } |
| 158 | |
| 159 | protected void handleSecondaryClick() { |
| 160 | // optional |
| 161 | } |
| 162 | |
John Spurlock | c247b8f | 2014-11-06 23:06:25 -0500 | [diff] [blame] | 163 | protected void handleLongClick() { |
| 164 | // optional |
| 165 | } |
| 166 | |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 167 | protected void handleRefreshState(Object arg) { |
| 168 | handleUpdateState(mTmpState, arg); |
| 169 | final boolean changed = mTmpState.copyTo(mState); |
| 170 | if (changed) { |
| 171 | handleStateChanged(); |
| 172 | } |
| 173 | } |
| 174 | |
| 175 | private void handleStateChanged() { |
Selim Cinek | 4fda7b2 | 2014-08-18 22:07:25 +0200 | [diff] [blame] | 176 | boolean delayAnnouncement = shouldAnnouncementBeDelayed(); |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 177 | if (mCallback != null) { |
| 178 | mCallback.onStateChanged(mState); |
Selim Cinek | 4fda7b2 | 2014-08-18 22:07:25 +0200 | [diff] [blame] | 179 | if (mAnnounceNextStateChange && !delayAnnouncement) { |
| 180 | String announcement = composeChangeAnnouncement(); |
| 181 | if (announcement != null) { |
| 182 | mCallback.onAnnouncementRequested(announcement); |
| 183 | } |
| 184 | } |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 185 | } |
Selim Cinek | 4fda7b2 | 2014-08-18 22:07:25 +0200 | [diff] [blame] | 186 | mAnnounceNextStateChange = mAnnounceNextStateChange && delayAnnouncement; |
| 187 | } |
| 188 | |
| 189 | protected boolean shouldAnnouncementBeDelayed() { |
| 190 | return false; |
| 191 | } |
| 192 | |
| 193 | protected String composeChangeAnnouncement() { |
| 194 | return null; |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 195 | } |
| 196 | |
| 197 | private void handleShowDetail(boolean show) { |
| 198 | if (mCallback != null) { |
| 199 | mCallback.onShowDetail(show); |
| 200 | } |
| 201 | } |
| 202 | |
John Spurlock | 7f8f22a | 2014-07-02 18:54:17 -0400 | [diff] [blame] | 203 | private void handleToggleStateChanged(boolean state) { |
| 204 | if (mCallback != null) { |
| 205 | mCallback.onToggleStateChanged(state); |
| 206 | } |
| 207 | } |
| 208 | |
John Spurlock | 486b78e | 2014-07-07 08:37:56 -0400 | [diff] [blame] | 209 | private void handleScanStateChanged(boolean state) { |
| 210 | if (mCallback != null) { |
| 211 | mCallback.onScanStateChanged(state); |
| 212 | } |
| 213 | } |
| 214 | |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 215 | protected void handleUserSwitch(int newUserId) { |
| 216 | handleRefreshState(null); |
| 217 | } |
| 218 | |
John Spurlock | bceed06 | 2014-08-10 18:04:16 -0400 | [diff] [blame] | 219 | protected void handleDestroy() { |
| 220 | setListening(false); |
| 221 | mCallback = null; |
| 222 | } |
| 223 | |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 224 | protected final class H extends Handler { |
| 225 | private static final int SET_CALLBACK = 1; |
| 226 | private static final int CLICK = 2; |
| 227 | private static final int SECONDARY_CLICK = 3; |
John Spurlock | c247b8f | 2014-11-06 23:06:25 -0500 | [diff] [blame] | 228 | private static final int LONG_CLICK = 4; |
| 229 | private static final int REFRESH_STATE = 5; |
| 230 | private static final int SHOW_DETAIL = 6; |
| 231 | private static final int USER_SWITCH = 7; |
| 232 | private static final int TOGGLE_STATE_CHANGED = 8; |
| 233 | private static final int SCAN_STATE_CHANGED = 9; |
| 234 | private static final int DESTROY = 10; |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 235 | |
| 236 | private H(Looper looper) { |
| 237 | super(looper); |
| 238 | } |
| 239 | |
| 240 | @Override |
| 241 | public void handleMessage(Message msg) { |
| 242 | String name = null; |
| 243 | try { |
| 244 | if (msg.what == SET_CALLBACK) { |
| 245 | name = "handleSetCallback"; |
| 246 | handleSetCallback((QSTile.Callback)msg.obj); |
| 247 | } else if (msg.what == CLICK) { |
| 248 | name = "handleClick"; |
Selim Cinek | 4fda7b2 | 2014-08-18 22:07:25 +0200 | [diff] [blame] | 249 | mAnnounceNextStateChange = true; |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 250 | handleClick(); |
| 251 | } else if (msg.what == SECONDARY_CLICK) { |
| 252 | name = "handleSecondaryClick"; |
| 253 | handleSecondaryClick(); |
John Spurlock | c247b8f | 2014-11-06 23:06:25 -0500 | [diff] [blame] | 254 | } else if (msg.what == LONG_CLICK) { |
| 255 | name = "handleLongClick"; |
| 256 | handleLongClick(); |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 257 | } else if (msg.what == REFRESH_STATE) { |
| 258 | name = "handleRefreshState"; |
| 259 | handleRefreshState(msg.obj); |
| 260 | } else if (msg.what == SHOW_DETAIL) { |
| 261 | name = "handleShowDetail"; |
| 262 | handleShowDetail(msg.arg1 != 0); |
| 263 | } else if (msg.what == USER_SWITCH) { |
| 264 | name = "handleUserSwitch"; |
| 265 | handleUserSwitch(msg.arg1); |
John Spurlock | 7f8f22a | 2014-07-02 18:54:17 -0400 | [diff] [blame] | 266 | } else if (msg.what == TOGGLE_STATE_CHANGED) { |
| 267 | name = "handleToggleStateChanged"; |
| 268 | handleToggleStateChanged(msg.arg1 != 0); |
John Spurlock | 486b78e | 2014-07-07 08:37:56 -0400 | [diff] [blame] | 269 | } else if (msg.what == SCAN_STATE_CHANGED) { |
| 270 | name = "handleScanStateChanged"; |
| 271 | handleScanStateChanged(msg.arg1 != 0); |
John Spurlock | bceed06 | 2014-08-10 18:04:16 -0400 | [diff] [blame] | 272 | } else if (msg.what == DESTROY) { |
| 273 | name = "handleDestroy"; |
| 274 | handleDestroy(); |
| 275 | } else { |
| 276 | throw new IllegalArgumentException("Unknown msg: " + msg.what); |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 277 | } |
| 278 | } catch (Throwable t) { |
| 279 | final String error = "Error in " + name; |
| 280 | Log.w(TAG, error, t); |
| 281 | mHost.warn(error, t); |
| 282 | } |
| 283 | } |
| 284 | } |
| 285 | |
| 286 | public interface Callback { |
| 287 | void onStateChanged(State state); |
| 288 | void onShowDetail(boolean show); |
John Spurlock | 7f8f22a | 2014-07-02 18:54:17 -0400 | [diff] [blame] | 289 | void onToggleStateChanged(boolean state); |
John Spurlock | 486b78e | 2014-07-07 08:37:56 -0400 | [diff] [blame] | 290 | void onScanStateChanged(boolean state); |
Selim Cinek | 4fda7b2 | 2014-08-18 22:07:25 +0200 | [diff] [blame] | 291 | void onAnnouncementRequested(CharSequence announcement); |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 292 | } |
| 293 | |
| 294 | public interface Host { |
| 295 | void startSettingsActivity(Intent intent); |
| 296 | void warn(String message, Throwable t); |
| 297 | void collapsePanels(); |
| 298 | Looper getLooper(); |
| 299 | Context getContext(); |
John Spurlock | bceed06 | 2014-08-10 18:04:16 -0400 | [diff] [blame] | 300 | Collection<QSTile<?>> getTiles(); |
| 301 | void setCallback(Callback callback); |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 302 | BluetoothController getBluetoothController(); |
| 303 | LocationController getLocationController(); |
| 304 | RotationLockController getRotationLockController(); |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 305 | NetworkController getNetworkController(); |
| 306 | ZenModeController getZenModeController(); |
Jason Monk | 51e4dc0 | 2014-07-22 12:00:47 -0400 | [diff] [blame] | 307 | HotspotController getHotspotController(); |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 308 | CastController getCastController(); |
Adrian Roos | b83777b | 2014-06-30 15:11:53 +0200 | [diff] [blame] | 309 | FlashlightController getFlashlightController(); |
John Spurlock | 657c62c | 2014-07-22 12:18:09 -0400 | [diff] [blame] | 310 | KeyguardMonitor getKeyguardMonitor(); |
John Spurlock | bceed06 | 2014-08-10 18:04:16 -0400 | [diff] [blame] | 311 | |
| 312 | public interface Callback { |
| 313 | void onTilesChanged(); |
| 314 | } |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 315 | } |
| 316 | |
John Spurlock | 2d69581 | 2014-10-30 13:25:21 -0400 | [diff] [blame] | 317 | public static abstract class Icon { |
| 318 | abstract public Drawable getDrawable(Context context); |
| 319 | |
| 320 | @Override |
| 321 | public int hashCode() { |
| 322 | return Icon.class.hashCode(); |
| 323 | } |
| 324 | } |
| 325 | |
| 326 | public static class ResourceIcon extends Icon { |
| 327 | private static final SparseArray<Icon> ICONS = new SparseArray<Icon>(); |
| 328 | |
Andrew Flynn | a478d70 | 2015-04-14 23:33:45 -0400 | [diff] [blame] | 329 | protected final int mResId; |
John Spurlock | 2d69581 | 2014-10-30 13:25:21 -0400 | [diff] [blame] | 330 | |
| 331 | private ResourceIcon(int resId) { |
| 332 | mResId = resId; |
| 333 | } |
| 334 | |
| 335 | public static Icon get(int resId) { |
| 336 | Icon icon = ICONS.get(resId); |
| 337 | if (icon == null) { |
| 338 | icon = new ResourceIcon(resId); |
| 339 | ICONS.put(resId, icon); |
| 340 | } |
| 341 | return icon; |
| 342 | } |
| 343 | |
| 344 | @Override |
| 345 | public Drawable getDrawable(Context context) { |
Andrew Flynn | a478d70 | 2015-04-14 23:33:45 -0400 | [diff] [blame] | 346 | Drawable d = context.getDrawable(mResId); |
| 347 | if (d instanceof Animatable) { |
| 348 | ((Animatable) d).start(); |
| 349 | } |
| 350 | return d; |
John Spurlock | 2d69581 | 2014-10-30 13:25:21 -0400 | [diff] [blame] | 351 | } |
| 352 | |
| 353 | @Override |
| 354 | public boolean equals(Object o) { |
| 355 | return o instanceof ResourceIcon && ((ResourceIcon) o).mResId == mResId; |
| 356 | } |
| 357 | |
| 358 | @Override |
| 359 | public String toString() { |
| 360 | return String.format("ResourceIcon[resId=0x%08x]", mResId); |
| 361 | } |
| 362 | } |
| 363 | |
| 364 | protected class AnimationIcon extends ResourceIcon { |
| 365 | private boolean mAllowAnimation; |
| 366 | |
| 367 | public AnimationIcon(int resId) { |
| 368 | super(resId); |
| 369 | } |
| 370 | |
| 371 | public void setAllowAnimation(boolean allowAnimation) { |
| 372 | mAllowAnimation = allowAnimation; |
| 373 | } |
| 374 | |
| 375 | @Override |
| 376 | public Drawable getDrawable(Context context) { |
| 377 | // workaround: get a clean state for every new AVD |
Andrew Flynn | a478d70 | 2015-04-14 23:33:45 -0400 | [diff] [blame] | 378 | final AnimatedVectorDrawable d = (AnimatedVectorDrawable) context.getDrawable(mResId) |
John Spurlock | 2d69581 | 2014-10-30 13:25:21 -0400 | [diff] [blame] | 379 | .getConstantState().newDrawable(); |
| 380 | d.start(); |
| 381 | if (mAllowAnimation) { |
| 382 | mAllowAnimation = false; |
| 383 | } else { |
| 384 | d.stop(); // skip directly to end state |
| 385 | } |
| 386 | return d; |
| 387 | } |
| 388 | } |
| 389 | |
| 390 | protected enum UserBoolean { |
| 391 | USER_TRUE(true, true), |
| 392 | USER_FALSE(true, false), |
| 393 | BACKGROUND_TRUE(false, true), |
| 394 | BACKGROUND_FALSE(false, false); |
| 395 | public final boolean value; |
| 396 | public final boolean userInitiated; |
| 397 | private UserBoolean(boolean userInitiated, boolean value) { |
| 398 | this.value = value; |
| 399 | this.userInitiated = userInitiated; |
| 400 | } |
| 401 | } |
| 402 | |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 403 | public static class State { |
| 404 | public boolean visible; |
John Spurlock | 2d69581 | 2014-10-30 13:25:21 -0400 | [diff] [blame] | 405 | public Icon icon; |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 406 | public String label; |
| 407 | public String contentDescription; |
Selim Cinek | e5557a9 | 2014-08-15 19:59:23 +0200 | [diff] [blame] | 408 | public String dualLabelContentDescription; |
Selim Cinek | 06d3bca | 2014-08-26 17:29:20 +0200 | [diff] [blame] | 409 | public boolean autoMirrorDrawable = true; |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 410 | |
| 411 | public boolean copyTo(State other) { |
| 412 | if (other == null) throw new IllegalArgumentException(); |
| 413 | if (!other.getClass().equals(getClass())) throw new IllegalArgumentException(); |
| 414 | final boolean changed = other.visible != visible |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 415 | || !Objects.equals(other.icon, icon) |
| 416 | || !Objects.equals(other.label, label) |
Selim Cinek | e5557a9 | 2014-08-15 19:59:23 +0200 | [diff] [blame] | 417 | || !Objects.equals(other.contentDescription, contentDescription) |
Selim Cinek | 06d3bca | 2014-08-26 17:29:20 +0200 | [diff] [blame] | 418 | || !Objects.equals(other.autoMirrorDrawable, autoMirrorDrawable) |
Selim Cinek | e5557a9 | 2014-08-15 19:59:23 +0200 | [diff] [blame] | 419 | || !Objects.equals(other.dualLabelContentDescription, |
| 420 | dualLabelContentDescription); |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 421 | other.visible = visible; |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 422 | other.icon = icon; |
| 423 | other.label = label; |
| 424 | other.contentDescription = contentDescription; |
Selim Cinek | e5557a9 | 2014-08-15 19:59:23 +0200 | [diff] [blame] | 425 | other.dualLabelContentDescription = dualLabelContentDescription; |
Selim Cinek | 06d3bca | 2014-08-26 17:29:20 +0200 | [diff] [blame] | 426 | other.autoMirrorDrawable = autoMirrorDrawable; |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 427 | return changed; |
| 428 | } |
| 429 | |
| 430 | @Override |
| 431 | public String toString() { |
| 432 | return toStringBuilder().toString(); |
| 433 | } |
| 434 | |
| 435 | protected StringBuilder toStringBuilder() { |
John Spurlock | 2d69581 | 2014-10-30 13:25:21 -0400 | [diff] [blame] | 436 | final StringBuilder sb = new StringBuilder(getClass().getSimpleName()).append('['); |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 437 | sb.append("visible=").append(visible); |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 438 | sb.append(",icon=").append(icon); |
| 439 | sb.append(",label=").append(label); |
| 440 | sb.append(",contentDescription=").append(contentDescription); |
Selim Cinek | e5557a9 | 2014-08-15 19:59:23 +0200 | [diff] [blame] | 441 | sb.append(",dualLabelContentDescription=").append(dualLabelContentDescription); |
Selim Cinek | 06d3bca | 2014-08-26 17:29:20 +0200 | [diff] [blame] | 442 | sb.append(",autoMirrorDrawable=").append(autoMirrorDrawable); |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 443 | return sb.append(']'); |
| 444 | } |
| 445 | } |
| 446 | |
| 447 | public static class BooleanState extends State { |
| 448 | public boolean value; |
| 449 | |
| 450 | @Override |
| 451 | public boolean copyTo(State other) { |
| 452 | final BooleanState o = (BooleanState) other; |
| 453 | final boolean changed = super.copyTo(other) || o.value != value; |
| 454 | o.value = value; |
| 455 | return changed; |
| 456 | } |
| 457 | |
| 458 | @Override |
| 459 | protected StringBuilder toStringBuilder() { |
| 460 | final StringBuilder rt = super.toStringBuilder(); |
| 461 | rt.insert(rt.length() - 1, ",value=" + value); |
| 462 | return rt; |
| 463 | } |
| 464 | } |
| 465 | |
| 466 | public static final class SignalState extends State { |
| 467 | public boolean enabled; |
| 468 | public boolean connected; |
| 469 | public boolean activityIn; |
| 470 | public boolean activityOut; |
| 471 | public int overlayIconId; |
John Spurlock | 899f439 | 2014-06-11 10:59:11 -0400 | [diff] [blame] | 472 | public boolean filter; |
Jorim Jaggi | c737b9b | 2014-09-08 23:57:20 +0200 | [diff] [blame] | 473 | public boolean isOverlayIconWide; |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 474 | |
| 475 | @Override |
| 476 | public boolean copyTo(State other) { |
| 477 | final SignalState o = (SignalState) other; |
| 478 | final boolean changed = o.enabled != enabled |
| 479 | || o.connected != connected || o.activityIn != activityIn |
| 480 | || o.activityOut != activityOut |
Jorim Jaggi | c737b9b | 2014-09-08 23:57:20 +0200 | [diff] [blame] | 481 | || o.overlayIconId != overlayIconId |
| 482 | || o.isOverlayIconWide != isOverlayIconWide; |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 483 | o.enabled = enabled; |
| 484 | o.connected = connected; |
| 485 | o.activityIn = activityIn; |
| 486 | o.activityOut = activityOut; |
| 487 | o.overlayIconId = overlayIconId; |
John Spurlock | 899f439 | 2014-06-11 10:59:11 -0400 | [diff] [blame] | 488 | o.filter = filter; |
Jorim Jaggi | c737b9b | 2014-09-08 23:57:20 +0200 | [diff] [blame] | 489 | o.isOverlayIconWide = isOverlayIconWide; |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 490 | return super.copyTo(other) || changed; |
| 491 | } |
| 492 | |
| 493 | @Override |
| 494 | protected StringBuilder toStringBuilder() { |
| 495 | final StringBuilder rt = super.toStringBuilder(); |
| 496 | rt.insert(rt.length() - 1, ",enabled=" + enabled); |
| 497 | rt.insert(rt.length() - 1, ",connected=" + connected); |
| 498 | rt.insert(rt.length() - 1, ",activityIn=" + activityIn); |
| 499 | rt.insert(rt.length() - 1, ",activityOut=" + activityOut); |
| 500 | rt.insert(rt.length() - 1, ",overlayIconId=" + overlayIconId); |
John Spurlock | 899f439 | 2014-06-11 10:59:11 -0400 | [diff] [blame] | 501 | rt.insert(rt.length() - 1, ",filter=" + filter); |
Jorim Jaggi | c737b9b | 2014-09-08 23:57:20 +0200 | [diff] [blame] | 502 | rt.insert(rt.length() - 1, ",wideOverlayIcon=" + isOverlayIconWide); |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 503 | return rt; |
| 504 | } |
| 505 | } |
| 506 | } |