| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
package android.widget; |
| 18 |
|
| 19 |
import android.annotation.DrawableRes; |
| 20 |
import android.annotation.NonNull; |
| 21 |
import android.annotation.Nullable; |
| 22 |
import android.compat.annotation.UnsupportedAppUsage; |
| 23 |
import android.content.Context; |
| 24 |
import android.content.res.ColorStateList; |
| 25 |
import android.content.res.TypedArray; |
| 26 |
import android.graphics.BlendMode; |
| 27 |
import android.graphics.Canvas; |
| 28 |
import android.graphics.PorterDuff; |
| 29 |
import android.graphics.drawable.Drawable; |
| 30 |
import android.graphics.drawable.Icon; |
| 31 |
import android.os.Parcel; |
| 32 |
import android.os.Parcelable; |
| 33 |
import android.util.AttributeSet; |
| 34 |
import android.util.Log; |
| 35 |
import android.view.Gravity; |
| 36 |
import android.view.RemotableViewMethod; |
| 37 |
import android.view.SoundEffectConstants; |
| 38 |
import android.view.ViewDebug; |
| 39 |
import android.view.ViewHierarchyEncoder; |
| 40 |
import android.view.ViewStructure; |
| 41 |
import android.view.accessibility.AccessibilityEvent; |
| 42 |
import android.view.accessibility.AccessibilityNodeInfo; |
| 43 |
import android.view.autofill.AutofillManager; |
| 44 |
import android.view.autofill.AutofillValue; |
| 45 |
import android.view.inspector.InspectableProperty; |
| 46 |
|
| 47 |
import com.android.internal.R; |
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
public abstract class CompoundButton extends Button implements Checkable { |
| 64 |
private static final String LOG_TAG = CompoundButton.class.getSimpleName(); |
| 65 |
|
| 66 |
private boolean mChecked; |
| 67 |
@UnsupportedAppUsage |
| 68 |
private boolean mBroadcasting; |
| 69 |
|
| 70 |
@UnsupportedAppUsage |
| 71 |
private Drawable mButtonDrawable; |
| 72 |
private ColorStateList mButtonTintList = null; |
| 73 |
private BlendMode mButtonBlendMode = null; |
| 74 |
private boolean mHasButtonTint = false; |
| 75 |
private boolean mHasButtonBlendMode = false; |
| 76 |
|
| 77 |
@UnsupportedAppUsage |
| 78 |
private OnCheckedChangeListener mOnCheckedChangeListener; |
| 79 |
private OnCheckedChangeListener mOnCheckedChangeWidgetListener; |
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
private boolean mCheckedFromResource = false; |
| 84 |
|
| 85 |
private CharSequence mCustomStateDescription = null; |
| 86 |
|
| 87 |
private static final int[] CHECKED_STATE_SET = { |
| 88 |
R.attr.state_checked |
| 89 |
}; |
| 90 |
|
| 91 |
public CompoundButton(Context context) { |
| 92 |
this(context, null); |
| 93 |
} |
| 94 |
|
| 95 |
public CompoundButton(Context context, AttributeSet attrs) { |
| 96 |
this(context, attrs, 0); |
| 97 |
} |
| 98 |
|
| 99 |
public CompoundButton(Context context, AttributeSet attrs, int defStyleAttr) { |
| 100 |
this(context, attrs, defStyleAttr, 0); |
| 101 |
} |
| 102 |
|
| 103 |
public CompoundButton(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) { |
| 104 |
super(context, attrs, defStyleAttr, defStyleRes); |
| 105 |
|
| 106 |
final TypedArray a = context.obtainStyledAttributes( |
| 107 |
attrs, com.android.internal.R.styleable.CompoundButton, defStyleAttr, defStyleRes); |
| 108 |
saveAttributeDataForStyleable(context, com.android.internal.R.styleable.CompoundButton, |
| 109 |
attrs, a, defStyleAttr, defStyleRes); |
| 110 |
|
| 111 |
final Drawable d = a.getDrawable(com.android.internal.R.styleable.CompoundButton_button); |
| 112 |
if (d != null) { |
| 113 |
setButtonDrawable(d); |
| 114 |
} |
| 115 |
|
| 116 |
if (a.hasValue(R.styleable.CompoundButton_buttonTintMode)) { |
| 117 |
mButtonBlendMode = Drawable.parseBlendMode(a.getInt( |
| 118 |
R.styleable.CompoundButton_buttonTintMode, -1), mButtonBlendMode); |
| 119 |
mHasButtonBlendMode = true; |
| 120 |
} |
| 121 |
|
| 122 |
if (a.hasValue(R.styleable.CompoundButton_buttonTint)) { |
| 123 |
mButtonTintList = a.getColorStateList(R.styleable.CompoundButton_buttonTint); |
| 124 |
mHasButtonTint = true; |
| 125 |
} |
| 126 |
|
| 127 |
final boolean checked = a.getBoolean( |
| 128 |
com.android.internal.R.styleable.CompoundButton_checked, false); |
| 129 |
setChecked(checked); |
| 130 |
mCheckedFromResource = true; |
| 131 |
|
| 132 |
a.recycle(); |
| 133 |
|
| 134 |
applyButtonTint(); |
| 135 |
} |
| 136 |
|
| 137 |
@Override |
| 138 |
public void toggle() { |
| 139 |
setChecked(!mChecked); |
| 140 |
} |
| 141 |
|
| 142 |
@Override |
| 143 |
public boolean performClick() { |
| 144 |
toggle(); |
| 145 |
|
| 146 |
final boolean handled = super.performClick(); |
| 147 |
if (!handled) { |
| 148 |
|
| 149 |
|
| 150 |
playSoundEffect(SoundEffectConstants.CLICK); |
| 151 |
} |
| 152 |
|
| 153 |
return handled; |
| 154 |
} |
| 155 |
|
| 156 |
@InspectableProperty |
| 157 |
@ViewDebug.ExportedProperty |
| 158 |
@Override |
| 159 |
public boolean isChecked() { |
| 160 |
return mChecked; |
| 161 |
} |
| 162 |
|
| 163 |
|
| 164 |
@NonNull |
| 165 |
protected CharSequence getButtonStateDescription() { |
| 166 |
if (isChecked()) { |
| 167 |
return getResources().getString(R.string.checked); |
| 168 |
} else { |
| 169 |
return getResources().getString(R.string.not_checked); |
| 170 |
} |
| 171 |
} |
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
@Override |
| 185 |
public void setStateDescription(@Nullable CharSequence stateDescription) { |
| 186 |
mCustomStateDescription = stateDescription; |
| 187 |
if (stateDescription == null) { |
| 188 |
setDefaultStateDescription(); |
| 189 |
} else { |
| 190 |
super.setStateDescription(stateDescription); |
| 191 |
} |
| 192 |
} |
| 193 |
|
| 194 |
|
| 195 |
protected void setDefaultStateDescription() { |
| 196 |
if (mCustomStateDescription == null) { |
| 197 |
super.setStateDescription(getButtonStateDescription()); |
| 198 |
} |
| 199 |
} |
| 200 |
|
| 201 |
|
| 202 |
|
| 203 |
|
| 204 |
|
| 205 |
|
| 206 |
@Override |
| 207 |
public void setChecked(boolean checked) { |
| 208 |
if (mChecked != checked) { |
| 209 |
mCheckedFromResource = false; |
| 210 |
mChecked = checked; |
| 211 |
refreshDrawableState(); |
| 212 |
|
| 213 |
|
| 214 |
if (mBroadcasting) { |
| 215 |
|
| 216 |
setDefaultStateDescription(); |
| 217 |
return; |
| 218 |
} |
| 219 |
|
| 220 |
mBroadcasting = true; |
| 221 |
if (mOnCheckedChangeListener != null) { |
| 222 |
mOnCheckedChangeListener.onCheckedChanged(this, mChecked); |
| 223 |
} |
| 224 |
if (mOnCheckedChangeWidgetListener != null) { |
| 225 |
mOnCheckedChangeWidgetListener.onCheckedChanged(this, mChecked); |
| 226 |
} |
| 227 |
final AutofillManager afm = mContext.getSystemService(AutofillManager.class); |
| 228 |
if (afm != null) { |
| 229 |
afm.notifyValueChanged(this); |
| 230 |
} |
| 231 |
|
| 232 |
mBroadcasting = false; |
| 233 |
} |
| 234 |
|
| 235 |
setDefaultStateDescription(); |
| 236 |
} |
| 237 |
|
| 238 |
|
| 239 |
|
| 240 |
|
| 241 |
|
| 242 |
|
| 243 |
|
| 244 |
public void setOnCheckedChangeListener(@Nullable OnCheckedChangeListener listener) { |
| 245 |
mOnCheckedChangeListener = listener; |
| 246 |
} |
| 247 |
|
| 248 |
|
| 249 |
|
| 250 |
|
| 251 |
|
| 252 |
|
| 253 |
|
| 254 |
|
| 255 |
void setOnCheckedChangeWidgetListener(OnCheckedChangeListener listener) { |
| 256 |
mOnCheckedChangeWidgetListener = listener; |
| 257 |
} |
| 258 |
|
| 259 |
|
| 260 |
|
| 261 |
|
| 262 |
|
| 263 |
public static interface OnCheckedChangeListener { |
| 264 |
|
| 265 |
|
| 266 |
|
| 267 |
|
| 268 |
|
| 269 |
|
| 270 |
void onCheckedChanged(CompoundButton buttonView, boolean isChecked); |
| 271 |
} |
| 272 |
|
| 273 |
|
| 274 |
|
| 275 |
|
| 276 |
|
| 277 |
|
| 278 |
|
| 279 |
|
| 280 |
@RemotableViewMethod(asyncImpl = "setButtonDrawableAsync") |
| 281 |
public void setButtonDrawable(@DrawableRes int resId) { |
| 282 |
final Drawable d; |
| 283 |
if (resId != 0) { |
| 284 |
d = getContext().getDrawable(resId); |
| 285 |
} else { |
| 286 |
d = null; |
| 287 |
} |
| 288 |
setButtonDrawable(d); |
| 289 |
} |
| 290 |
|
| 291 |
|
| 292 |
public Runnable setButtonDrawableAsync(@DrawableRes int resId) { |
| 293 |
Drawable drawable = resId == 0 ? null : getContext().getDrawable(resId); |
| 294 |
return () -> setButtonDrawable(drawable); |
| 295 |
} |
| 296 |
|
| 297 |
|
| 298 |
|
| 299 |
|
| 300 |
|
| 301 |
|
| 302 |
|
| 303 |
public void setButtonDrawable(@Nullable Drawable drawable) { |
| 304 |
if (mButtonDrawable != drawable) { |
| 305 |
if (mButtonDrawable != null) { |
| 306 |
mButtonDrawable.setCallback(null); |
| 307 |
unscheduleDrawable(mButtonDrawable); |
| 308 |
} |
| 309 |
|
| 310 |
mButtonDrawable = drawable; |
| 311 |
|
| 312 |
if (drawable != null) { |
| 313 |
drawable.setCallback(this); |
| 314 |
drawable.setLayoutDirection(getLayoutDirection()); |
| 315 |
if (drawable.isStateful()) { |
| 316 |
drawable.setState(getDrawableState()); |
| 317 |
} |
| 318 |
drawable.setVisible(getVisibility() == VISIBLE, false); |
| 319 |
setMinHeight(drawable.getIntrinsicHeight()); |
| 320 |
applyButtonTint(); |
| 321 |
} |
| 322 |
} |
| 323 |
} |
| 324 |
|
| 325 |
|
| 326 |
|
| 327 |
|
| 328 |
@Override |
| 329 |
public void onResolveDrawables(@ResolvedLayoutDir int layoutDirection) { |
| 330 |
super.onResolveDrawables(layoutDirection); |
| 331 |
if (mButtonDrawable != null) { |
| 332 |
mButtonDrawable.setLayoutDirection(layoutDirection); |
| 333 |
} |
| 334 |
} |
| 335 |
|
| 336 |
|
| 337 |
|
| 338 |
|
| 339 |
|
| 340 |
|
| 341 |
@InspectableProperty(name = "button") |
| 342 |
@Nullable |
| 343 |
public Drawable getButtonDrawable() { |
| 344 |
return mButtonDrawable; |
| 345 |
} |
| 346 |
|
| 347 |
|
| 348 |
|
| 349 |
|
| 350 |
|
| 351 |
|
| 352 |
|
| 353 |
@RemotableViewMethod(asyncImpl = "setButtonIconAsync") |
| 354 |
public void setButtonIcon(@Nullable Icon icon) { |
| 355 |
setButtonDrawable(icon == null ? null : icon.loadDrawable(getContext())); |
| 356 |
} |
| 357 |
|
| 358 |
|
| 359 |
public Runnable setButtonIconAsync(@Nullable Icon icon) { |
| 360 |
Drawable button = icon == null ? null : icon.loadDrawable(getContext()); |
| 361 |
return () -> setButtonDrawable(button); |
| 362 |
} |
| 363 |
|
| 364 |
|
| 365 |
|
| 366 |
|
| 367 |
|
| 368 |
|
| 369 |
|
| 370 |
|
| 371 |
|
| 372 |
|
| 373 |
|
| 374 |
|
| 375 |
|
| 376 |
|
| 377 |
|
| 378 |
|
| 379 |
@RemotableViewMethod |
| 380 |
public void setButtonTintList(@Nullable ColorStateList tint) { |
| 381 |
mButtonTintList = tint; |
| 382 |
mHasButtonTint = true; |
| 383 |
|
| 384 |
applyButtonTint(); |
| 385 |
} |
| 386 |
|
| 387 |
|
| 388 |
|
| 389 |
|
| 390 |
|
| 391 |
|
| 392 |
@InspectableProperty(name = "buttonTint") |
| 393 |
@Nullable |
| 394 |
public ColorStateList getButtonTintList() { |
| 395 |
return mButtonTintList; |
| 396 |
} |
| 397 |
|
| 398 |
|
| 399 |
|
| 400 |
|
| 401 |
|
| 402 |
|
| 403 |
|
| 404 |
|
| 405 |
|
| 406 |
|
| 407 |
|
| 408 |
|
| 409 |
public void setButtonTintMode(@Nullable PorterDuff.Mode tintMode) { |
| 410 |
setButtonTintBlendMode(tintMode != null ? BlendMode.fromValue(tintMode.nativeInt) : null); |
| 411 |
} |
| 412 |
|
| 413 |
|
| 414 |
|
| 415 |
|
| 416 |
|
| 417 |
|
| 418 |
|
| 419 |
|
| 420 |
|
| 421 |
|
| 422 |
|
| 423 |
|
| 424 |
@RemotableViewMethod |
| 425 |
public void setButtonTintBlendMode(@Nullable BlendMode tintMode) { |
| 426 |
mButtonBlendMode = tintMode; |
| 427 |
mHasButtonBlendMode = true; |
| 428 |
|
| 429 |
applyButtonTint(); |
| 430 |
} |
| 431 |
|
| 432 |
|
| 433 |
|
| 434 |
|
| 435 |
|
| 436 |
|
| 437 |
@InspectableProperty(name = "buttonTintMode") |
| 438 |
@Nullable |
| 439 |
public PorterDuff.Mode getButtonTintMode() { |
| 440 |
return mButtonBlendMode != null ? BlendMode.blendModeToPorterDuffMode(mButtonBlendMode) : |
| 441 |
null; |
| 442 |
} |
| 443 |
|
| 444 |
|
| 445 |
|
| 446 |
|
| 447 |
|
| 448 |
|
| 449 |
@InspectableProperty(name = "buttonBlendMode", |
| 450 |
attributeId = R.styleable.CompoundButton_buttonTintMode) |
| 451 |
@Nullable |
| 452 |
public BlendMode getButtonTintBlendMode() { |
| 453 |
return mButtonBlendMode; |
| 454 |
} |
| 455 |
|
| 456 |
private void applyButtonTint() { |
| 457 |
if (mButtonDrawable != null && (mHasButtonTint || mHasButtonBlendMode)) { |
| 458 |
mButtonDrawable = mButtonDrawable.mutate(); |
| 459 |
|
| 460 |
if (mHasButtonTint) { |
| 461 |
mButtonDrawable.setTintList(mButtonTintList); |
| 462 |
} |
| 463 |
|
| 464 |
if (mHasButtonBlendMode) { |
| 465 |
mButtonDrawable.setTintBlendMode(mButtonBlendMode); |
| 466 |
} |
| 467 |
|
| 468 |
|
| 469 |
|
| 470 |
if (mButtonDrawable.isStateful()) { |
| 471 |
mButtonDrawable.setState(getDrawableState()); |
| 472 |
} |
| 473 |
} |
| 474 |
} |
| 475 |
|
| 476 |
@Override |
| 477 |
public CharSequence getAccessibilityClassName() { |
| 478 |
return CompoundButton.class.getName(); |
| 479 |
} |
| 480 |
|
| 481 |
|
| 482 |
@Override |
| 483 |
public void onInitializeAccessibilityEventInternal(AccessibilityEvent event) { |
| 484 |
super.onInitializeAccessibilityEventInternal(event); |
| 485 |
event.setChecked(mChecked); |
| 486 |
} |
| 487 |
|
| 488 |
|
| 489 |
@Override |
| 490 |
public void onInitializeAccessibilityNodeInfoInternal(AccessibilityNodeInfo info) { |
| 491 |
super.onInitializeAccessibilityNodeInfoInternal(info); |
| 492 |
info.setCheckable(true); |
| 493 |
info.setChecked(mChecked); |
| 494 |
} |
| 495 |
|
| 496 |
@Override |
| 497 |
public int getCompoundPaddingLeft() { |
| 498 |
int padding = super.getCompoundPaddingLeft(); |
| 499 |
if (!isLayoutRtl()) { |
| 500 |
final Drawable buttonDrawable = mButtonDrawable; |
| 501 |
if (buttonDrawable != null) { |
| 502 |
padding += buttonDrawable.getIntrinsicWidth(); |
| 503 |
} |
| 504 |
} |
| 505 |
return padding; |
| 506 |
} |
| 507 |
|
| 508 |
@Override |
| 509 |
public int getCompoundPaddingRight() { |
| 510 |
int padding = super.getCompoundPaddingRight(); |
| 511 |
if (isLayoutRtl()) { |
| 512 |
final Drawable buttonDrawable = mButtonDrawable; |
| 513 |
if (buttonDrawable != null) { |
| 514 |
padding += buttonDrawable.getIntrinsicWidth(); |
| 515 |
} |
| 516 |
} |
| 517 |
return padding; |
| 518 |
} |
| 519 |
|
| 520 |
|
| 521 |
|
| 522 |
|
| 523 |
@Override |
| 524 |
public int getHorizontalOffsetForDrawables() { |
| 525 |
final Drawable buttonDrawable = mButtonDrawable; |
| 526 |
return (buttonDrawable != null) ? buttonDrawable.getIntrinsicWidth() : 0; |
| 527 |
} |
| 528 |
|
| 529 |
@Override |
| 530 |
protected void onDraw(Canvas canvas) { |
| 531 |
final Drawable buttonDrawable = mButtonDrawable; |
| 532 |
if (buttonDrawable != null) { |
| 533 |
final int verticalGravity = getGravity() & Gravity.VERTICAL_GRAVITY_MASK; |
| 534 |
final int drawableHeight = buttonDrawable.getIntrinsicHeight(); |
| 535 |
final int drawableWidth = buttonDrawable.getIntrinsicWidth(); |
| 536 |
|
| 537 |
final int top; |
| 538 |
switch (verticalGravity) { |
| 539 |
case Gravity.BOTTOM: |
| 540 |
top = getHeight() - drawableHeight; |
| 541 |
break; |
| 542 |
case Gravity.CENTER_VERTICAL: |
| 543 |
top = (getHeight() - drawableHeight) / 2; |
| 544 |
break; |
| 545 |
default: |
| 546 |
top = 0; |
| 547 |
} |
| 548 |
final int bottom = top + drawableHeight; |
| 549 |
final int left = isLayoutRtl() ? getWidth() - drawableWidth : 0; |
| 550 |
final int right = isLayoutRtl() ? getWidth() : drawableWidth; |
| 551 |
|
| 552 |
buttonDrawable.setBounds(left, top, right, bottom); |
| 553 |
|
| 554 |
final Drawable background = getBackground(); |
| 555 |
if (background != null) { |
| 556 |
background.setHotspotBounds(left, top, right, bottom); |
| 557 |
} |
| 558 |
} |
| 559 |
|
| 560 |
super.onDraw(canvas); |
| 561 |
|
| 562 |
if (buttonDrawable != null) { |
| 563 |
final int scrollX = mScrollX; |
| 564 |
final int scrollY = mScrollY; |
| 565 |
if (scrollX == 0 && scrollY == 0) { |
| 566 |
buttonDrawable.draw(canvas); |
| 567 |
} else { |
| 568 |
canvas.translate(scrollX, scrollY); |
| 569 |
buttonDrawable.draw(canvas); |
| 570 |
canvas.translate(-scrollX, -scrollY); |
| 571 |
} |
| 572 |
} |
| 573 |
} |
| 574 |
|
| 575 |
@Override |
| 576 |
protected int[] onCreateDrawableState(int extraSpace) { |
| 577 |
final int[] drawableState = super.onCreateDrawableState(extraSpace + 1); |
| 578 |
if (isChecked()) { |
| 579 |
mergeDrawableStates(drawableState, CHECKED_STATE_SET); |
| 580 |
} |
| 581 |
return drawableState; |
| 582 |
} |
| 583 |
|
| 584 |
@Override |
| 585 |
protected void drawableStateChanged() { |
| 586 |
super.drawableStateChanged(); |
| 587 |
|
| 588 |
final Drawable buttonDrawable = mButtonDrawable; |
| 589 |
if (buttonDrawable != null && buttonDrawable.isStateful() |
| 590 |
&& buttonDrawable.setState(getDrawableState())) { |
| 591 |
invalidateDrawable(buttonDrawable); |
| 592 |
} |
| 593 |
} |
| 594 |
|
| 595 |
@Override |
| 596 |
public void drawableHotspotChanged(float x, float y) { |
| 597 |
super.drawableHotspotChanged(x, y); |
| 598 |
|
| 599 |
if (mButtonDrawable != null) { |
| 600 |
mButtonDrawable.setHotspot(x, y); |
| 601 |
} |
| 602 |
} |
| 603 |
|
| 604 |
@Override |
| 605 |
protected boolean verifyDrawable(@NonNull Drawable who) { |
| 606 |
return super.verifyDrawable(who) || who == mButtonDrawable; |
| 607 |
} |
| 608 |
|
| 609 |
@Override |
| 610 |
public void jumpDrawablesToCurrentState() { |
| 611 |
super.jumpDrawablesToCurrentState(); |
| 612 |
if (mButtonDrawable != null) mButtonDrawable.jumpToCurrentState(); |
| 613 |
} |
| 614 |
|
| 615 |
static class SavedState extends BaseSavedState { |
| 616 |
boolean checked; |
| 617 |
|
| 618 |
|
| 619 |
|
| 620 |
|
| 621 |
SavedState(Parcelable superState) { |
| 622 |
super(superState); |
| 623 |
} |
| 624 |
|
| 625 |
|
| 626 |
|
| 627 |
|
| 628 |
private SavedState(Parcel in) { |
| 629 |
super(in); |
| 630 |
checked = (Boolean)in.readValue(null); |
| 631 |
} |
| 632 |
|
| 633 |
@Override |
| 634 |
public void writeToParcel(Parcel out, int flags) { |
| 635 |
super.writeToParcel(out, flags); |
| 636 |
out.writeValue(checked); |
| 637 |
} |
| 638 |
|
| 639 |
@Override |
| 640 |
public String toString() { |
| 641 |
return "CompoundButton.SavedState{" |
| 642 |
+ Integer.toHexString(System.identityHashCode(this)) |
| 643 |
+ " checked=" + checked + "}"; |
| 644 |
} |
| 645 |
|
| 646 |
@SuppressWarnings("hiding") |
| 647 |
public static final @android.annotation.NonNull Parcelable.Creator<SavedState> CREATOR = |
| 648 |
new Parcelable.Creator<SavedState>() { |
| 649 |
@Override |
| 650 |
public SavedState createFromParcel(Parcel in) { |
| 651 |
return new SavedState(in); |
| 652 |
} |
| 653 |
|
| 654 |
@Override |
| 655 |
public SavedState[] newArray(int size) { |
| 656 |
return new SavedState[size]; |
| 657 |
} |
| 658 |
}; |
| 659 |
} |
| 660 |
|
| 661 |
@Override |
| 662 |
public Parcelable onSaveInstanceState() { |
| 663 |
Parcelable superState = super.onSaveInstanceState(); |
| 664 |
|
| 665 |
SavedState ss = new SavedState(superState); |
| 666 |
|
| 667 |
ss.checked = isChecked(); |
| 668 |
return ss; |
| 669 |
} |
| 670 |
|
| 671 |
@Override |
| 672 |
public void onRestoreInstanceState(Parcelable state) { |
| 673 |
SavedState ss = (SavedState) state; |
| 674 |
|
| 675 |
super.onRestoreInstanceState(ss.getSuperState()); |
| 676 |
setChecked(ss.checked); |
| 677 |
requestLayout(); |
| 678 |
} |
| 679 |
|
| 680 |
|
| 681 |
@Override |
| 682 |
protected void encodeProperties(@NonNull ViewHierarchyEncoder stream) { |
| 683 |
super.encodeProperties(stream); |
| 684 |
stream.addProperty("checked", isChecked()); |
| 685 |
} |
| 686 |
|
| 687 |
|
| 688 |
|
| 689 |
@Override |
| 690 |
protected void onProvideStructure(@NonNull ViewStructure structure, |
| 691 |
@ViewStructureType int viewFor, int flags) { |
| 692 |
super.onProvideStructure(structure, viewFor, flags); |
| 693 |
|
| 694 |
if (viewFor == VIEW_STRUCTURE_FOR_AUTOFILL) { |
| 695 |
structure.setDataIsSensitive(!mCheckedFromResource); |
| 696 |
} |
| 697 |
} |
| 698 |
|
| 699 |
@Override |
| 700 |
public void autofill(AutofillValue value) { |
| 701 |
if (!isEnabled()) return; |
| 702 |
|
| 703 |
if (!value.isToggle()) { |
| 704 |
Log.w(LOG_TAG, value + " could not be autofilled into " + this); |
| 705 |
return; |
| 706 |
} |
| 707 |
|
| 708 |
setChecked(value.getToggleValue()); |
| 709 |
} |
| 710 |
|
| 711 |
@Override |
| 712 |
public @AutofillType int getAutofillType() { |
| 713 |
return isEnabled() ? AUTOFILL_TYPE_TOGGLE : AUTOFILL_TYPE_NONE; |
| 714 |
} |
| 715 |
|
| 716 |
@Override |
| 717 |
public AutofillValue getAutofillValue() { |
| 718 |
return isEnabled() ? AutofillValue.forToggle(isChecked()) : null; |
| 719 |
} |
| 720 |
} |