From e718cfcd949e4fadd5208b53e0f3f421adb84620 Mon Sep 17 00:00:00 2001 From: Kevin Chang Date: Mon, 29 Apr 2019 11:21:02 +0800 Subject: [PATCH] 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 --- .../android/settings/widget/SwitchBar.java | 25 ++++++++----------- 1 file changed, 10 insertions(+), 15 deletions(-) diff --git a/src/com/android/settings/widget/SwitchBar.java b/src/com/android/settings/widget/SwitchBar.java index 6cde6440f22..dfcc131e524 100644 --- a/src/com/android/settings/widget/SwitchBar.java +++ b/src/com/android/settings/widget/SwitchBar.java @@ -30,7 +30,6 @@ import android.text.TextUtils; import android.text.style.TextAppearanceSpan; import android.util.AttributeSet; import android.view.LayoutInflater; -import android.view.TouchDelegate; import android.view.View; import android.view.ViewGroup; import android.widget.CompoundButton; @@ -110,6 +109,11 @@ public class SwitchBar extends LinearLayout implements CompoundButton.OnCheckedC super(context, attrs, defStyleAttr, defStyleRes); 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 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 // on our own 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.setMarginEnd(switchBarMarginEnd); @@ -239,14 +246,14 @@ public class SwitchBar extends LinearLayout implements CompoundButton.OnCheckedC mSwitch.setEnabled(false); mSwitch.setVisibility(View.GONE); mRestrictedIcon.setVisibility(View.VISIBLE); + mRestrictedIcon.setFocusable(false); + mRestrictedIcon.setClickable(false); } else { mDisabledByAdmin = false; mSwitch.setVisibility(View.VISIBLE); mRestrictedIcon.setVisibility(View.GONE); setEnabled(true); } - setTouchDelegate(new TouchDelegate(new Rect(0, 0, getWidth(), getHeight()), - getDelegatingView())); } public final ToggleSwitch getSwitch() { @@ -257,10 +264,6 @@ public class SwitchBar extends LinearLayout implements CompoundButton.OnCheckedC if (!isShowing()) { setVisibility(View.VISIBLE); 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() { return (getVisibility() == View.VISIBLE); }