Change the attributes of SwitchBar and ToggleSwitch

Change the focusable and clickable of SwitchBar and ToggleSwitch
to avoid multiple focus with TalkBack

Bug: 124334500
Bug: 128475156
Test: Visual
Change-Id: I13003f4e17d4c76644f87095c140f005b38889af
This commit is contained in:
Kevin Chang
2019-04-29 11:21:02 +08:00
parent 54cddc70e5
commit e718cfcd94

View File

@@ -30,7 +30,6 @@ import android.text.TextUtils;
import android.text.style.TextAppearanceSpan; import android.text.style.TextAppearanceSpan;
import android.util.AttributeSet; import android.util.AttributeSet;
import android.view.LayoutInflater; import android.view.LayoutInflater;
import android.view.TouchDelegate;
import android.view.View; import android.view.View;
import android.view.ViewGroup; import android.view.ViewGroup;
import android.widget.CompoundButton; import android.widget.CompoundButton;
@@ -110,6 +109,11 @@ public class SwitchBar extends LinearLayout implements CompoundButton.OnCheckedC
super(context, attrs, defStyleAttr, defStyleRes); super(context, attrs, defStyleAttr, defStyleRes);
LayoutInflater.from(context).inflate(R.layout.switch_bar, this); LayoutInflater.from(context).inflate(R.layout.switch_bar, this);
// Set the whole SwitchBar focusable and clickable.
setFocusable(true);
setClickable(true);
// Set a onClickListener to handle the functionality of ToggleSwitch.
setOnClickListener((View v) -> getDelegatingView().performClick());
final TypedArray a = context.obtainStyledAttributes(attrs, XML_ATTRIBUTES); final TypedArray a = context.obtainStyledAttributes(attrs, XML_ATTRIBUTES);
final int switchBarMarginStart = (int) a.getDimension(0, 0); final int switchBarMarginStart = (int) a.getDimension(0, 0);
@@ -128,6 +132,9 @@ public class SwitchBar extends LinearLayout implements CompoundButton.OnCheckedC
// Prevent onSaveInstanceState() to be called as we are managing the state of the Switch // Prevent onSaveInstanceState() to be called as we are managing the state of the Switch
// on our own // on our own
mSwitch.setSaveEnabled(false); mSwitch.setSaveEnabled(false);
// Set the ToggleSwitch non-focusable and non-clickable to avoid multiple focus.
mSwitch.setFocusable(false);
mSwitch.setClickable(false);
lp = (MarginLayoutParams) mSwitch.getLayoutParams(); lp = (MarginLayoutParams) mSwitch.getLayoutParams();
lp.setMarginEnd(switchBarMarginEnd); lp.setMarginEnd(switchBarMarginEnd);
@@ -239,14 +246,14 @@ public class SwitchBar extends LinearLayout implements CompoundButton.OnCheckedC
mSwitch.setEnabled(false); mSwitch.setEnabled(false);
mSwitch.setVisibility(View.GONE); mSwitch.setVisibility(View.GONE);
mRestrictedIcon.setVisibility(View.VISIBLE); mRestrictedIcon.setVisibility(View.VISIBLE);
mRestrictedIcon.setFocusable(false);
mRestrictedIcon.setClickable(false);
} else { } else {
mDisabledByAdmin = false; mDisabledByAdmin = false;
mSwitch.setVisibility(View.VISIBLE); mSwitch.setVisibility(View.VISIBLE);
mRestrictedIcon.setVisibility(View.GONE); mRestrictedIcon.setVisibility(View.GONE);
setEnabled(true); setEnabled(true);
} }
setTouchDelegate(new TouchDelegate(new Rect(0, 0, getWidth(), getHeight()),
getDelegatingView()));
} }
public final ToggleSwitch getSwitch() { public final ToggleSwitch getSwitch() {
@@ -257,10 +264,6 @@ public class SwitchBar extends LinearLayout implements CompoundButton.OnCheckedC
if (!isShowing()) { if (!isShowing()) {
setVisibility(View.VISIBLE); setVisibility(View.VISIBLE);
mSwitch.setOnCheckedChangeListener(this); mSwitch.setOnCheckedChangeListener(this);
// Make the entire bar work as a switch
post(() -> setTouchDelegate(
new TouchDelegate(new Rect(0, 0, getWidth(), getHeight()),
getDelegatingView())));
} }
} }
@@ -271,14 +274,6 @@ public class SwitchBar extends LinearLayout implements CompoundButton.OnCheckedC
} }
} }
@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
if ((w > 0) && (h > 0)) {
setTouchDelegate(new TouchDelegate(new Rect(0, 0, w, h),
getDelegatingView()));
}
}
public boolean isShowing() { public boolean isShowing() {
return (getVisibility() == View.VISIBLE); return (getVisibility() == View.VISIBLE);
} }