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
diff --git a/src/com/android/settings/widget/SwitchBar.java b/src/com/android/settings/widget/SwitchBar.java
index 6cde644..dfcc131 100644
--- a/src/com/android/settings/widget/SwitchBar.java
+++ b/src/com/android/settings/widget/SwitchBar.java
@@ -30,7 +30,6 @@
 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 @@
         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 @@
         // 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 @@
             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 @@
         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 @@
         }
     }
 
-    @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);
     }