Revert "Migrate to CompoundButton.OnCheckedChangeListener"

Revert submission 25147565-onSwitchChanged-CompoundButton

Reason for revert: breaking builds 

Bug:309601476

Reverted changes: /q/submissionid:25147565-onSwitchChanged-CompoundButton

Change-Id: I8e9f724bac503983eb5bf9240de2bf27763b0ecf
diff --git a/packages/SettingsLib/MainSwitchPreference/src/com/android/settingslib/widget/MainSwitchBar.java b/packages/SettingsLib/MainSwitchPreference/src/com/android/settingslib/widget/MainSwitchBar.java
index 2b5fcd8..6001155 100644
--- a/packages/SettingsLib/MainSwitchPreference/src/com/android/settingslib/widget/MainSwitchBar.java
+++ b/packages/SettingsLib/MainSwitchPreference/src/com/android/settingslib/widget/MainSwitchBar.java
@@ -24,8 +24,8 @@
 import android.view.LayoutInflater;
 import android.view.View;
 import android.widget.CompoundButton;
-import android.widget.CompoundButton.OnCheckedChangeListener;
 import android.widget.LinearLayout;
+import android.widget.Switch;
 import android.widget.TextView;
 
 import androidx.annotation.ColorInt;
@@ -41,9 +41,9 @@
  * This component is used as the main switch of the page
  * to enable or disable the prefereces on the page.
  */
-public class MainSwitchBar extends LinearLayout implements OnCheckedChangeListener {
+public class MainSwitchBar extends LinearLayout implements CompoundButton.OnCheckedChangeListener {
 
-    private final List<OnCheckedChangeListener> mSwitchChangeListeners = new ArrayList<>();
+    private final List<OnMainSwitchChangeListener> mSwitchChangeListeners = new ArrayList<>();
 
     @ColorInt
     private int mBackgroundColor;
@@ -51,8 +51,8 @@
     private int mBackgroundActivatedColor;
 
     protected TextView mTextView;
-    protected CompoundButton mSwitch;
-    private final View mFrameView;
+    protected Switch mSwitch;
+    private View mFrameView;
 
     public MainSwitchBar(Context context) {
         this(context, null);
@@ -84,8 +84,8 @@
         setClickable(true);
 
         mFrameView = findViewById(R.id.frame);
-        mTextView = findViewById(R.id.switch_text);
-        mSwitch = findViewById(android.R.id.switch_widget);
+        mTextView = (TextView) findViewById(R.id.switch_text);
+        mSwitch = (Switch) findViewById(android.R.id.switch_widget);
         addOnSwitchChangeListener((switchView, isChecked) -> setChecked(isChecked));
 
         if (mSwitch.getVisibility() == VISIBLE) {
@@ -136,6 +136,13 @@
     }
 
     /**
+     * Return the Switch
+     */
+    public final Switch getSwitch() {
+        return mSwitch;
+    }
+
+    /**
      * Set the title text
      */
     public void setTitle(CharSequence text) {
@@ -185,7 +192,7 @@
     /**
      * Adds a listener for switch changes
      */
-    public void addOnSwitchChangeListener(OnCheckedChangeListener listener) {
+    public void addOnSwitchChangeListener(OnMainSwitchChangeListener listener) {
         if (!mSwitchChangeListeners.contains(listener)) {
             mSwitchChangeListeners.add(listener);
         }
@@ -194,7 +201,7 @@
     /**
      * Remove a listener for switch changes
      */
-    public void removeOnSwitchChangeListener(OnCheckedChangeListener listener) {
+    public void removeOnSwitchChangeListener(OnMainSwitchChangeListener listener) {
         mSwitchChangeListeners.remove(listener);
     }
 
@@ -216,8 +223,9 @@
     private void propagateChecked(boolean isChecked) {
         setBackground(isChecked);
 
-        for (OnCheckedChangeListener changeListener : mSwitchChangeListeners) {
-            changeListener.onCheckedChanged(mSwitch, isChecked);
+        final int count = mSwitchChangeListeners.size();
+        for (int n = 0; n < count; n++) {
+            mSwitchChangeListeners.get(n).onSwitchChanged(mSwitch, isChecked);
         }
     }
 
diff --git a/packages/SettingsLib/MainSwitchPreference/src/com/android/settingslib/widget/MainSwitchPreference.java b/packages/SettingsLib/MainSwitchPreference/src/com/android/settingslib/widget/MainSwitchPreference.java
index b294d4e..11a6804 100644
--- a/packages/SettingsLib/MainSwitchPreference/src/com/android/settingslib/widget/MainSwitchPreference.java
+++ b/packages/SettingsLib/MainSwitchPreference/src/com/android/settingslib/widget/MainSwitchPreference.java
@@ -19,25 +19,24 @@
 import android.content.Context;
 import android.content.res.TypedArray;
 import android.util.AttributeSet;
-import android.widget.CompoundButton;
-import android.widget.CompoundButton.OnCheckedChangeListener;
+import android.widget.Switch;
 
 import androidx.preference.PreferenceViewHolder;
 import androidx.preference.TwoStatePreference;
 
-import com.android.settingslib.widget.mainswitch.R;
-
 import java.util.ArrayList;
 import java.util.List;
 
+import com.android.settingslib.widget.mainswitch.R;
+
 /**
  * MainSwitchPreference is a Preference with a customized Switch.
  * This component is used as the main switch of the page
  * to enable or disable the prefereces on the page.
  */
-public class MainSwitchPreference extends TwoStatePreference implements OnCheckedChangeListener {
+public class MainSwitchPreference extends TwoStatePreference implements OnMainSwitchChangeListener {
 
-    private final List<OnCheckedChangeListener> mSwitchChangeListeners = new ArrayList<>();
+    private final List<OnMainSwitchChangeListener> mSwitchChangeListeners = new ArrayList<>();
 
     private MainSwitchBar mMainSwitchBar;
 
@@ -121,7 +120,7 @@
     }
 
     @Override
-    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
+    public void onSwitchChanged(Switch switchView, boolean isChecked) {
         super.setChecked(isChecked);
     }
 
@@ -139,7 +138,7 @@
     /**
      * Adds a listener for switch changes
      */
-    public void addOnSwitchChangeListener(OnCheckedChangeListener listener) {
+    public void addOnSwitchChangeListener(OnMainSwitchChangeListener listener) {
         if (!mSwitchChangeListeners.contains(listener)) {
             mSwitchChangeListeners.add(listener);
         }
@@ -152,7 +151,7 @@
     /**
      * Remove a listener for switch changes
      */
-    public void removeOnSwitchChangeListener(OnCheckedChangeListener listener) {
+    public void removeOnSwitchChangeListener(OnMainSwitchChangeListener listener) {
         mSwitchChangeListeners.remove(listener);
         if (mMainSwitchBar != null) {
             mMainSwitchBar.removeOnSwitchChangeListener(listener);
@@ -160,7 +159,7 @@
     }
 
     private void registerListenerToSwitchBar() {
-        for (OnCheckedChangeListener listener : mSwitchChangeListeners) {
+        for (OnMainSwitchChangeListener listener : mSwitchChangeListeners) {
             mMainSwitchBar.addOnSwitchChangeListener(listener);
         }
     }
diff --git a/packages/SettingsLib/MainSwitchPreference/src/com/android/settingslib/widget/OnMainSwitchChangeListener.java b/packages/SettingsLib/MainSwitchPreference/src/com/android/settingslib/widget/OnMainSwitchChangeListener.java
new file mode 100644
index 0000000..03868f9
--- /dev/null
+++ b/packages/SettingsLib/MainSwitchPreference/src/com/android/settingslib/widget/OnMainSwitchChangeListener.java
@@ -0,0 +1,32 @@
+/*
+ * Copyright (C) 2020 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.android.settingslib.widget;
+
+import android.widget.Switch;
+
+import com.android.settingslib.widget.mainswitch.R;
+
+/**
+ * Called when the checked state of the Switch has changed.
+ */
+public interface OnMainSwitchChangeListener {
+    /**
+     * @param switchView The Switch view whose state has changed.
+     * @param isChecked  The new checked state of switchView.
+     */
+    void onSwitchChanged(Switch switchView, boolean isChecked);
+}
diff --git a/packages/SettingsLib/tests/robotests/src/com/android/settingslib/widget/MainSwitchBarTest.java b/packages/SettingsLib/tests/robotests/src/com/android/settingslib/widget/MainSwitchBarTest.java
index 74a282f..942e915 100644
--- a/packages/SettingsLib/tests/robotests/src/com/android/settingslib/widget/MainSwitchBarTest.java
+++ b/packages/SettingsLib/tests/robotests/src/com/android/settingslib/widget/MainSwitchBarTest.java
@@ -21,25 +21,30 @@
 import static com.google.common.truth.Truth.assertThat;
 
 import android.content.Context;
+import android.text.TextUtils;
 import android.view.View;
-import android.widget.CompoundButton;
+import android.widget.Switch;
 import android.widget.TextView;
 
-import androidx.test.core.app.ApplicationProvider;
-
 import com.android.settingslib.widget.mainswitch.R;
 
+import org.junit.Before;
 import org.junit.Test;
 import org.junit.runner.RunWith;
 import org.robolectric.RobolectricTestRunner;
+import org.robolectric.RuntimeEnvironment;
 
 @RunWith(RobolectricTestRunner.class)
 public class MainSwitchBarTest {
 
-    private final Context mContext = ApplicationProvider.getApplicationContext();
-    private final MainSwitchBar mBar = new MainSwitchBar(mContext);
+    private Context mContext;
+    private MainSwitchBar mBar;
 
-    private final CompoundButton mSwitch = mBar.findViewById(android.R.id.switch_widget);
+    @Before
+    public void setUp() {
+        mContext = RuntimeEnvironment.application;
+        mBar = new MainSwitchBar(mContext);
+    }
 
     @Test
     public void setChecked_true_shouldChecked() {
@@ -55,7 +60,7 @@
         mBar.setTitle(title);
         final TextView textView = ((TextView) mBar.findViewById(R.id.switch_text));
 
-        assertThat(textView.getText().toString()).isEqualTo(title);
+        assertThat(textView.getText()).isEqualTo(title);
     }
 
     @Test
@@ -64,18 +69,23 @@
 
         mBar.setTitle(title);
 
-        assertThat(mSwitch.getContentDescription()).isNull();
+        final Switch switchObj = mBar.getSwitch();
+        assertThat(TextUtils.isEmpty(switchObj.getContentDescription())).isTrue();
     }
 
     @Test
     public void getSwitch_shouldNotNull() {
-        assertThat(mSwitch).isNotNull();
+        final Switch switchObj = mBar.getSwitch();
+
+        assertThat(switchObj).isNotNull();
     }
 
     @Test
     public void getSwitch_shouldNotFocusableAndClickable() {
-        assertThat(mSwitch.isFocusable()).isFalse();
-        assertThat(mSwitch.isClickable()).isFalse();
+        final Switch switchObj = mBar.getSwitch();
+
+        assertThat(switchObj.isFocusable()).isFalse();
+        assertThat(switchObj.isClickable()).isFalse();
     }
 
     @Test