| 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.os.Parcel; |
| 31 |
import android.os.Parcelable; |
| 32 |
import android.util.AttributeSet; |
| 33 |
import android.view.Gravity; |
| 34 |
import android.view.RemotableViewMethod; |
| 35 |
import android.view.ViewDebug; |
| 36 |
import android.view.ViewHierarchyEncoder; |
| 37 |
import android.view.accessibility.AccessibilityEvent; |
| 38 |
import android.view.accessibility.AccessibilityNodeInfo; |
| 39 |
import android.view.inspector.InspectableProperty; |
| 40 |
|
| 41 |
import com.android.internal.R; |
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
public class CheckedTextView extends TextView implements Checkable { |
| 56 |
private boolean mChecked; |
| 57 |
|
| 58 |
private int mCheckMarkResource; |
| 59 |
@UnsupportedAppUsage |
| 60 |
private Drawable mCheckMarkDrawable; |
| 61 |
private ColorStateList mCheckMarkTintList = null; |
| 62 |
private BlendMode mCheckMarkBlendMode = null; |
| 63 |
private boolean mHasCheckMarkTint = false; |
| 64 |
private boolean mHasCheckMarkTintMode = false; |
| 65 |
|
| 66 |
private int mBasePadding; |
| 67 |
private int mCheckMarkWidth; |
| 68 |
@UnsupportedAppUsage |
| 69 |
private int mCheckMarkGravity = Gravity.END; |
| 70 |
|
| 71 |
private boolean mNeedRequestlayout; |
| 72 |
|
| 73 |
private static final int[] CHECKED_STATE_SET = { |
| 74 |
R.attr.state_checked |
| 75 |
}; |
| 76 |
|
| 77 |
public CheckedTextView(Context context) { |
| 78 |
this(context, null); |
| 79 |
} |
| 80 |
|
| 81 |
public CheckedTextView(Context context, AttributeSet attrs) { |
| 82 |
this(context, attrs, R.attr.checkedTextViewStyle); |
| 83 |
} |
| 84 |
|
| 85 |
public CheckedTextView(Context context, AttributeSet attrs, int defStyleAttr) { |
| 86 |
this(context, attrs, defStyleAttr, 0); |
| 87 |
} |
| 88 |
|
| 89 |
public CheckedTextView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) { |
| 90 |
super(context, attrs, defStyleAttr, defStyleRes); |
| 91 |
|
| 92 |
final TypedArray a = context.obtainStyledAttributes( |
| 93 |
attrs, R.styleable.CheckedTextView, defStyleAttr, defStyleRes); |
| 94 |
saveAttributeDataForStyleable(context, R.styleable.CheckedTextView, |
| 95 |
attrs, a, defStyleAttr, defStyleRes); |
| 96 |
|
| 97 |
final Drawable d = a.getDrawable(R.styleable.CheckedTextView_checkMark); |
| 98 |
if (d != null) { |
| 99 |
setCheckMarkDrawable(d); |
| 100 |
} |
| 101 |
|
| 102 |
if (a.hasValue(R.styleable.CheckedTextView_checkMarkTintMode)) { |
| 103 |
mCheckMarkBlendMode = Drawable.parseBlendMode(a.getInt( |
| 104 |
R.styleable.CheckedTextView_checkMarkTintMode, -1), |
| 105 |
mCheckMarkBlendMode); |
| 106 |
mHasCheckMarkTintMode = true; |
| 107 |
} |
| 108 |
|
| 109 |
if (a.hasValue(R.styleable.CheckedTextView_checkMarkTint)) { |
| 110 |
mCheckMarkTintList = a.getColorStateList(R.styleable.CheckedTextView_checkMarkTint); |
| 111 |
mHasCheckMarkTint = true; |
| 112 |
} |
| 113 |
|
| 114 |
mCheckMarkGravity = a.getInt(R.styleable.CheckedTextView_checkMarkGravity, Gravity.END); |
| 115 |
|
| 116 |
final boolean checked = a.getBoolean(R.styleable.CheckedTextView_checked, false); |
| 117 |
setChecked(checked); |
| 118 |
|
| 119 |
a.recycle(); |
| 120 |
|
| 121 |
applyCheckMarkTint(); |
| 122 |
} |
| 123 |
|
| 124 |
public void toggle() { |
| 125 |
setChecked(!mChecked); |
| 126 |
} |
| 127 |
|
| 128 |
@ViewDebug.ExportedProperty |
| 129 |
@InspectableProperty |
| 130 |
public boolean isChecked() { |
| 131 |
return mChecked; |
| 132 |
} |
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
public void setChecked(boolean checked) { |
| 141 |
if (mChecked != checked) { |
| 142 |
mChecked = checked; |
| 143 |
refreshDrawableState(); |
| 144 |
notifyViewAccessibilityStateChangedIfNeeded( |
| 145 |
AccessibilityEvent.CONTENT_CHANGE_TYPE_UNDEFINED); |
| 146 |
} |
| 147 |
} |
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
public void setCheckMarkDrawable(@DrawableRes int resId) { |
| 162 |
if (resId != 0 && resId == mCheckMarkResource) { |
| 163 |
return; |
| 164 |
} |
| 165 |
|
| 166 |
final Drawable d = resId != 0 ? getContext().getDrawable(resId) : null; |
| 167 |
setCheckMarkDrawableInternal(d, resId); |
| 168 |
} |
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
|
| 181 |
public void setCheckMarkDrawable(@Nullable Drawable d) { |
| 182 |
setCheckMarkDrawableInternal(d, 0); |
| 183 |
} |
| 184 |
|
| 185 |
private void setCheckMarkDrawableInternal(@Nullable Drawable d, @DrawableRes int resId) { |
| 186 |
if (mCheckMarkDrawable != null) { |
| 187 |
mCheckMarkDrawable.setCallback(null); |
| 188 |
unscheduleDrawable(mCheckMarkDrawable); |
| 189 |
} |
| 190 |
|
| 191 |
mNeedRequestlayout = (d != mCheckMarkDrawable); |
| 192 |
|
| 193 |
if (d != null) { |
| 194 |
d.setCallback(this); |
| 195 |
d.setVisible(getVisibility() == VISIBLE, false); |
| 196 |
d.setState(CHECKED_STATE_SET); |
| 197 |
|
| 198 |
|
| 199 |
setMinHeight(d.getIntrinsicHeight()); |
| 200 |
mCheckMarkWidth = d.getIntrinsicWidth(); |
| 201 |
|
| 202 |
d.setState(getDrawableState()); |
| 203 |
} else { |
| 204 |
mCheckMarkWidth = 0; |
| 205 |
} |
| 206 |
|
| 207 |
mCheckMarkDrawable = d; |
| 208 |
mCheckMarkResource = resId; |
| 209 |
|
| 210 |
applyCheckMarkTint(); |
| 211 |
|
| 212 |
|
| 213 |
|
| 214 |
resolvePadding(); |
| 215 |
} |
| 216 |
|
| 217 |
|
| 218 |
|
| 219 |
|
| 220 |
|
| 221 |
|
| 222 |
|
| 223 |
|
| 224 |
|
| 225 |
|
| 226 |
|
| 227 |
|
| 228 |
|
| 229 |
|
| 230 |
|
| 231 |
|
| 232 |
public void setCheckMarkTintList(@Nullable ColorStateList tint) { |
| 233 |
mCheckMarkTintList = tint; |
| 234 |
mHasCheckMarkTint = true; |
| 235 |
|
| 236 |
applyCheckMarkTint(); |
| 237 |
} |
| 238 |
|
| 239 |
|
| 240 |
|
| 241 |
|
| 242 |
|
| 243 |
|
| 244 |
|
| 245 |
|
| 246 |
@InspectableProperty(name = "checkMarkTint") |
| 247 |
@Nullable |
| 248 |
public ColorStateList getCheckMarkTintList() { |
| 249 |
return mCheckMarkTintList; |
| 250 |
} |
| 251 |
|
| 252 |
|
| 253 |
|
| 254 |
|
| 255 |
|
| 256 |
|
| 257 |
|
| 258 |
|
| 259 |
|
| 260 |
|
| 261 |
|
| 262 |
|
| 263 |
public void setCheckMarkTintMode(@Nullable PorterDuff.Mode tintMode) { |
| 264 |
setCheckMarkTintBlendMode(tintMode != null |
| 265 |
? BlendMode.fromValue(tintMode.nativeInt) : null); |
| 266 |
} |
| 267 |
|
| 268 |
|
| 269 |
|
| 270 |
|
| 271 |
|
| 272 |
|
| 273 |
|
| 274 |
|
| 275 |
|
| 276 |
|
| 277 |
|
| 278 |
|
| 279 |
public void setCheckMarkTintBlendMode(@Nullable BlendMode tintMode) { |
| 280 |
mCheckMarkBlendMode = tintMode; |
| 281 |
mHasCheckMarkTintMode = true; |
| 282 |
|
| 283 |
applyCheckMarkTint(); |
| 284 |
} |
| 285 |
|
| 286 |
|
| 287 |
|
| 288 |
|
| 289 |
|
| 290 |
|
| 291 |
|
| 292 |
|
| 293 |
|
| 294 |
|
| 295 |
@InspectableProperty |
| 296 |
@Nullable |
| 297 |
public PorterDuff.Mode getCheckMarkTintMode() { |
| 298 |
return mCheckMarkBlendMode != null |
| 299 |
? BlendMode.blendModeToPorterDuffMode(mCheckMarkBlendMode) : null; |
| 300 |
} |
| 301 |
|
| 302 |
|
| 303 |
|
| 304 |
|
| 305 |
|
| 306 |
|
| 307 |
|
| 308 |
|
| 309 |
|
| 310 |
|
| 311 |
@InspectableProperty(attributeId = android.R.styleable.CheckedTextView_checkMarkTintMode) |
| 312 |
@Nullable |
| 313 |
public BlendMode getCheckMarkTintBlendMode() { |
| 314 |
return mCheckMarkBlendMode; |
| 315 |
} |
| 316 |
|
| 317 |
private void applyCheckMarkTint() { |
| 318 |
if (mCheckMarkDrawable != null && (mHasCheckMarkTint || mHasCheckMarkTintMode)) { |
| 319 |
mCheckMarkDrawable = mCheckMarkDrawable.mutate(); |
| 320 |
|
| 321 |
if (mHasCheckMarkTint) { |
| 322 |
mCheckMarkDrawable.setTintList(mCheckMarkTintList); |
| 323 |
} |
| 324 |
|
| 325 |
if (mHasCheckMarkTintMode) { |
| 326 |
mCheckMarkDrawable.setTintBlendMode(mCheckMarkBlendMode); |
| 327 |
} |
| 328 |
|
| 329 |
|
| 330 |
|
| 331 |
if (mCheckMarkDrawable.isStateful()) { |
| 332 |
mCheckMarkDrawable.setState(getDrawableState()); |
| 333 |
} |
| 334 |
} |
| 335 |
} |
| 336 |
|
| 337 |
@RemotableViewMethod |
| 338 |
@Override |
| 339 |
public void setVisibility(int visibility) { |
| 340 |
super.setVisibility(visibility); |
| 341 |
|
| 342 |
if (mCheckMarkDrawable != null) { |
| 343 |
mCheckMarkDrawable.setVisible(visibility == VISIBLE, false); |
| 344 |
} |
| 345 |
} |
| 346 |
|
| 347 |
@Override |
| 348 |
public void jumpDrawablesToCurrentState() { |
| 349 |
super.jumpDrawablesToCurrentState(); |
| 350 |
|
| 351 |
if (mCheckMarkDrawable != null) { |
| 352 |
mCheckMarkDrawable.jumpToCurrentState(); |
| 353 |
} |
| 354 |
} |
| 355 |
|
| 356 |
@Override |
| 357 |
protected boolean verifyDrawable(@NonNull Drawable who) { |
| 358 |
return who == mCheckMarkDrawable || super.verifyDrawable(who); |
| 359 |
} |
| 360 |
|
| 361 |
|
| 362 |
|
| 363 |
|
| 364 |
|
| 365 |
|
| 366 |
|
| 367 |
|
| 368 |
|
| 369 |
|
| 370 |
|
| 371 |
@InspectableProperty(name = "checkMark") |
| 372 |
public Drawable getCheckMarkDrawable() { |
| 373 |
return mCheckMarkDrawable; |
| 374 |
} |
| 375 |
|
| 376 |
|
| 377 |
|
| 378 |
|
| 379 |
@Override |
| 380 |
protected void internalSetPadding(int left, int top, int right, int bottom) { |
| 381 |
super.internalSetPadding(left, top, right, bottom); |
| 382 |
setBasePadding(isCheckMarkAtStart()); |
| 383 |
} |
| 384 |
|
| 385 |
@Override |
| 386 |
public void onRtlPropertiesChanged(int layoutDirection) { |
| 387 |
super.onRtlPropertiesChanged(layoutDirection); |
| 388 |
updatePadding(); |
| 389 |
} |
| 390 |
|
| 391 |
private void updatePadding() { |
| 392 |
resetPaddingToInitialValues(); |
| 393 |
int newPadding = (mCheckMarkDrawable != null) ? |
| 394 |
mCheckMarkWidth + mBasePadding : mBasePadding; |
| 395 |
if (isCheckMarkAtStart()) { |
| 396 |
mNeedRequestlayout |= (mPaddingLeft != newPadding); |
| 397 |
mPaddingLeft = newPadding; |
| 398 |
} else { |
| 399 |
mNeedRequestlayout |= (mPaddingRight != newPadding); |
| 400 |
mPaddingRight = newPadding; |
| 401 |
} |
| 402 |
if (mNeedRequestlayout) { |
| 403 |
requestLayout(); |
| 404 |
mNeedRequestlayout = false; |
| 405 |
} |
| 406 |
} |
| 407 |
|
| 408 |
private void setBasePadding(boolean checkmarkAtStart) { |
| 409 |
if (checkmarkAtStart) { |
| 410 |
mBasePadding = mPaddingLeft; |
| 411 |
} else { |
| 412 |
mBasePadding = mPaddingRight; |
| 413 |
} |
| 414 |
} |
| 415 |
|
| 416 |
private boolean isCheckMarkAtStart() { |
| 417 |
final int gravity = Gravity.getAbsoluteGravity(mCheckMarkGravity, getLayoutDirection()); |
| 418 |
final int hgrav = gravity & Gravity.HORIZONTAL_GRAVITY_MASK; |
| 419 |
return hgrav == Gravity.LEFT; |
| 420 |
} |
| 421 |
|
| 422 |
@Override |
| 423 |
protected void onDraw(Canvas canvas) { |
| 424 |
super.onDraw(canvas); |
| 425 |
|
| 426 |
final Drawable checkMarkDrawable = mCheckMarkDrawable; |
| 427 |
if (checkMarkDrawable != null) { |
| 428 |
final int verticalGravity = getGravity() & Gravity.VERTICAL_GRAVITY_MASK; |
| 429 |
final int height = checkMarkDrawable.getIntrinsicHeight(); |
| 430 |
|
| 431 |
int y = 0; |
| 432 |
|
| 433 |
switch (verticalGravity) { |
| 434 |
case Gravity.BOTTOM: |
| 435 |
y = getHeight() - height; |
| 436 |
break; |
| 437 |
case Gravity.CENTER_VERTICAL: |
| 438 |
y = (getHeight() - height) / 2; |
| 439 |
break; |
| 440 |
} |
| 441 |
|
| 442 |
final boolean checkMarkAtStart = isCheckMarkAtStart(); |
| 443 |
final int width = getWidth(); |
| 444 |
final int top = y; |
| 445 |
final int bottom = top + height; |
| 446 |
final int left; |
| 447 |
final int right; |
| 448 |
if (checkMarkAtStart) { |
| 449 |
left = mBasePadding; |
| 450 |
right = left + mCheckMarkWidth; |
| 451 |
} else { |
| 452 |
right = width - mBasePadding; |
| 453 |
left = right - mCheckMarkWidth; |
| 454 |
} |
| 455 |
checkMarkDrawable.setBounds(mScrollX + left, top, mScrollX + right, bottom); |
| 456 |
checkMarkDrawable.draw(canvas); |
| 457 |
|
| 458 |
final Drawable background = getBackground(); |
| 459 |
if (background != null) { |
| 460 |
background.setHotspotBounds(mScrollX + left, top, mScrollX + right, bottom); |
| 461 |
} |
| 462 |
} |
| 463 |
} |
| 464 |
|
| 465 |
@Override |
| 466 |
protected int[] onCreateDrawableState(int extraSpace) { |
| 467 |
final int[] drawableState = super.onCreateDrawableState(extraSpace + 1); |
| 468 |
if (isChecked()) { |
| 469 |
mergeDrawableStates(drawableState, CHECKED_STATE_SET); |
| 470 |
} |
| 471 |
return drawableState; |
| 472 |
} |
| 473 |
|
| 474 |
@Override |
| 475 |
protected void drawableStateChanged() { |
| 476 |
super.drawableStateChanged(); |
| 477 |
|
| 478 |
final Drawable checkMarkDrawable = mCheckMarkDrawable; |
| 479 |
if (checkMarkDrawable != null && checkMarkDrawable.isStateful() |
| 480 |
&& checkMarkDrawable.setState(getDrawableState())) { |
| 481 |
invalidateDrawable(checkMarkDrawable); |
| 482 |
} |
| 483 |
} |
| 484 |
|
| 485 |
@Override |
| 486 |
public void drawableHotspotChanged(float x, float y) { |
| 487 |
super.drawableHotspotChanged(x, y); |
| 488 |
|
| 489 |
if (mCheckMarkDrawable != null) { |
| 490 |
mCheckMarkDrawable.setHotspot(x, y); |
| 491 |
} |
| 492 |
} |
| 493 |
|
| 494 |
@Override |
| 495 |
public CharSequence getAccessibilityClassName() { |
| 496 |
return CheckedTextView.class.getName(); |
| 497 |
} |
| 498 |
|
| 499 |
static class SavedState extends BaseSavedState { |
| 500 |
boolean checked; |
| 501 |
|
| 502 |
|
| 503 |
|
| 504 |
|
| 505 |
SavedState(Parcelable superState) { |
| 506 |
super(superState); |
| 507 |
} |
| 508 |
|
| 509 |
|
| 510 |
|
| 511 |
|
| 512 |
private SavedState(Parcel in) { |
| 513 |
super(in); |
| 514 |
checked = (Boolean)in.readValue(null); |
| 515 |
} |
| 516 |
|
| 517 |
@Override |
| 518 |
public void writeToParcel(Parcel out, int flags) { |
| 519 |
super.writeToParcel(out, flags); |
| 520 |
out.writeValue(checked); |
| 521 |
} |
| 522 |
|
| 523 |
@Override |
| 524 |
public String toString() { |
| 525 |
return "CheckedTextView.SavedState{" |
| 526 |
+ Integer.toHexString(System.identityHashCode(this)) |
| 527 |
+ " checked=" + checked + "}"; |
| 528 |
} |
| 529 |
|
| 530 |
public static final @android.annotation.NonNull Parcelable.Creator<SavedState> CREATOR |
| 531 |
= new Parcelable.Creator<SavedState>() { |
| 532 |
public SavedState createFromParcel(Parcel in) { |
| 533 |
return new SavedState(in); |
| 534 |
} |
| 535 |
|
| 536 |
public SavedState[] newArray(int size) { |
| 537 |
return new SavedState[size]; |
| 538 |
} |
| 539 |
}; |
| 540 |
} |
| 541 |
|
| 542 |
@Override |
| 543 |
public Parcelable onSaveInstanceState() { |
| 544 |
Parcelable superState = super.onSaveInstanceState(); |
| 545 |
|
| 546 |
SavedState ss = new SavedState(superState); |
| 547 |
|
| 548 |
ss.checked = isChecked(); |
| 549 |
return ss; |
| 550 |
} |
| 551 |
|
| 552 |
@Override |
| 553 |
public void onRestoreInstanceState(Parcelable state) { |
| 554 |
SavedState ss = (SavedState) state; |
| 555 |
|
| 556 |
super.onRestoreInstanceState(ss.getSuperState()); |
| 557 |
setChecked(ss.checked); |
| 558 |
requestLayout(); |
| 559 |
} |
| 560 |
|
| 561 |
|
| 562 |
@Override |
| 563 |
public void onInitializeAccessibilityEventInternal(AccessibilityEvent event) { |
| 564 |
super.onInitializeAccessibilityEventInternal(event); |
| 565 |
event.setChecked(mChecked); |
| 566 |
} |
| 567 |
|
| 568 |
|
| 569 |
@Override |
| 570 |
public void onInitializeAccessibilityNodeInfoInternal(AccessibilityNodeInfo info) { |
| 571 |
super.onInitializeAccessibilityNodeInfoInternal(info); |
| 572 |
info.setCheckable(true); |
| 573 |
info.setChecked(mChecked); |
| 574 |
} |
| 575 |
|
| 576 |
|
| 577 |
@Override |
| 578 |
protected void encodeProperties(@NonNull ViewHierarchyEncoder stream) { |
| 579 |
super.encodeProperties(stream); |
| 580 |
stream.addProperty("text:checked", isChecked()); |
| 581 |
} |
| 582 |
} |