summaryrefslogtreecommitdiff
path: root/core/java/android/widget/CompoundButton.java
diff options
context:
space:
mode:
authoryingleiw <yingleiw@google.com>2019-10-11 15:10:55 -0700
committeryingleiw <yingleiw@google.com>2019-11-19 12:13:56 -0800
commit473fc125fa2d2ebebac797d01eeee6c15cc05a56 (patch)
tree88ac953cef574205e4a287e9e0b7f6a470b9df1a /core/java/android/widget/CompoundButton.java
parent0d9dd2b0f330538a7a48e1b66991483f78fca6a7 (diff)
change CompoundButton button to use the accessibility state API
Test: tested with updated talkback (see CL/274237446). It works as before. Change-Id: I928350c8bc9112252e8b8a703c465489f03fde11
Diffstat (limited to 'core/java/android/widget/CompoundButton.java')
-rw-r--r--core/java/android/widget/CompoundButton.java44
1 files changed, 42 insertions, 2 deletions
diff --git a/core/java/android/widget/CompoundButton.java b/core/java/android/widget/CompoundButton.java
index 2674ca4d159a..547aad64fc3e 100644
--- a/core/java/android/widget/CompoundButton.java
+++ b/core/java/android/widget/CompoundButton.java
@@ -80,6 +80,8 @@ public abstract class CompoundButton extends Button implements Checkable {
// to sanitize autofill requests.
private boolean mCheckedFromResource = false;
+ private CharSequence mCustomStateDescription = null;
+
private static final int[] CHECKED_STATE_SET = {
R.attr.state_checked
};
@@ -156,6 +158,44 @@ public abstract class CompoundButton extends Button implements Checkable {
return mChecked;
}
+ /** @hide */
+ @NonNull
+ protected CharSequence getButtonStateDescription() {
+ if (isChecked()) {
+ return getResources().getString(R.string.checked);
+ } else {
+ return getResources().getString(R.string.not_checked);
+ }
+ }
+
+ /**
+ * This function is called when an instance or subclass sets the state description. Once this
+ * is called and the argument is not null, the app developer will be responsible for updating
+ * state description when checked state changes and we will not set state description
+ * in {@link #setChecked}. App developers can restore the default behavior by setting the
+ * argument to null. If {@link #setChecked} is called first and then setStateDescription is
+ * called, two state change events will be merged by event throttling and we can still get
+ * the correct state description.
+ *
+ * @param stateDescription The state description.
+ */
+ @Override
+ public void setStateDescription(@Nullable CharSequence stateDescription) {
+ mCustomStateDescription = stateDescription;
+ if (stateDescription == null) {
+ setDefaultStateDescritption();
+ } else {
+ super.setStateDescription(stateDescription);
+ }
+ }
+
+ /** @hide **/
+ protected void setDefaultStateDescritption() {
+ if (mCustomStateDescription == null) {
+ super.setStateDescription(getButtonStateDescription());
+ }
+ }
+
/**
* <p>Changes the checked state of this button.</p>
*
@@ -167,8 +207,6 @@ public abstract class CompoundButton extends Button implements Checkable {
mCheckedFromResource = false;
mChecked = checked;
refreshDrawableState();
- notifyViewAccessibilityStateChangedIfNeeded(
- AccessibilityEvent.CONTENT_CHANGE_TYPE_UNDEFINED);
// Avoid infinite recursions if setChecked() is called from a listener
if (mBroadcasting) {
@@ -189,6 +227,8 @@ public abstract class CompoundButton extends Button implements Checkable {
mBroadcasting = false;
}
+ // setStateDescription will not send out event if the description is unchanged.
+ setDefaultStateDescritption();
}
/**