Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit c88f75a9 authored by Chaohui Wang's avatar Chaohui Wang
Browse files

Migrate PrimarySwitchPreference to CompoundButton

Switch and SwitchCompat are both CompoundButton.

Using CompoundButton in Java will helps migration in the future.

Bug: 306658427
Test: manual - check Settings pages
Change-Id: I753bd254f13bf06b8ee4c688e94821143779a15c
parent 180ab777
Loading
Loading
Loading
Loading
+4 −4
Original line number Diff line number Diff line
@@ -19,7 +19,7 @@ package com.android.settingslib;
import android.content.Context;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.widget.Switch;
import android.widget.CompoundButton;

import androidx.annotation.Keep;
import androidx.annotation.Nullable;
@@ -35,7 +35,7 @@ import com.android.settingslib.core.instrumentation.SettingsJankMonitor;
 */
public class PrimarySwitchPreference extends RestrictedPreference {

    private Switch mSwitch;
    private CompoundButton mSwitch;
    private boolean mChecked;
    private boolean mCheckedSet;
    private boolean mEnableSwitch = true;
@@ -65,7 +65,7 @@ public class PrimarySwitchPreference extends RestrictedPreference {
    @Override
    public void onBindViewHolder(PreferenceViewHolder holder) {
        super.onBindViewHolder(holder);
        mSwitch = (Switch) holder.findViewById(R.id.switchWidget);
        mSwitch = (CompoundButton) holder.findViewById(R.id.switchWidget);
        if (mSwitch != null) {
            mSwitch.setOnClickListener(v -> {
                if (mSwitch != null && !mSwitch.isEnabled()) {
@@ -153,7 +153,7 @@ public class PrimarySwitchPreference extends RestrictedPreference {
        setSwitchEnabled(admin == null);
    }

    public Switch getSwitch() {
    public CompoundButton getSwitch() {
        return mSwitch;
    }

+2 −2
Original line number Diff line number Diff line
@@ -31,8 +31,8 @@ import android.view.ViewGroup;
import android.view.inputmethod.InputMethodInfo;
import android.view.inputmethod.InputMethodManager;
import android.view.inputmethod.InputMethodSubtype;
import android.widget.CompoundButton;
import android.widget.ImageView;
import android.widget.Switch;
import android.widget.Toast;

import androidx.preference.Preference;
@@ -138,7 +138,7 @@ public class InputMethodPreference extends PrimarySwitchPreference
    @Override
    public void onBindViewHolder(PreferenceViewHolder holder) {
        super.onBindViewHolder(holder);
        final Switch switchWidget = getSwitch();
        final CompoundButton switchWidget = getSwitch();
        if (switchWidget != null) {
            // Avoid default behavior in {@link PrimarySwitchPreference#onBindViewHolder}.
            switchWidget.setOnClickListener(v -> {
+10 −10
Original line number Diff line number Diff line
@@ -23,8 +23,8 @@ import static org.mockito.Mockito.verify;

import android.content.Context;
import android.view.LayoutInflater;
import android.widget.CompoundButton;
import android.widget.LinearLayout;
import android.widget.Switch;

import androidx.preference.Preference.OnPreferenceChangeListener;
import androidx.preference.PreferenceViewHolder;
@@ -62,7 +62,7 @@ public class PrimarySwitchPreferenceTest {

    @Test
    public void setChecked_shouldUpdateButtonCheckedState() {
        final Switch toggle = (Switch) mHolder.findViewById(R.id.switchWidget);
        final CompoundButton toggle = (CompoundButton) mHolder.findViewById(R.id.switchWidget);
        mPreference.onBindViewHolder(mHolder);

        mPreference.setChecked(true);
@@ -74,7 +74,7 @@ public class PrimarySwitchPreferenceTest {

    @Test
    public void setSwitchEnabled_shouldUpdateButtonEnabledState() {
        final Switch toggle = (Switch) mHolder.findViewById(R.id.switchWidget);
        final CompoundButton toggle = (CompoundButton) mHolder.findViewById(R.id.switchWidget);
        mPreference.onBindViewHolder(mHolder);

        mPreference.setSwitchEnabled(true);
@@ -86,7 +86,7 @@ public class PrimarySwitchPreferenceTest {

    @Test
    public void setSwitchEnabled_shouldUpdateButtonEnabledState_beforeViewBound() {
        final Switch toggle = (Switch) mHolder.findViewById(R.id.switchWidget);
        final CompoundButton toggle = (CompoundButton) mHolder.findViewById(R.id.switchWidget);

        mPreference.setSwitchEnabled(false);
        mPreference.onBindViewHolder(mHolder);
@@ -97,7 +97,7 @@ public class PrimarySwitchPreferenceTest {
    public void clickWidgetView_shouldToggleButton() {
        assertThat(mWidgetView).isNotNull();

        final Switch toggle = (Switch) mHolder.findViewById(R.id.switchWidget);
        final CompoundButton toggle = (CompoundButton) mHolder.findViewById(R.id.switchWidget);
        mPreference.onBindViewHolder(mHolder);

        toggle.performClick();
@@ -111,7 +111,7 @@ public class PrimarySwitchPreferenceTest {
    public void clickWidgetView_shouldNotToggleButtonIfDisabled() {
        assertThat(mWidgetView).isNotNull();

        final Switch toggle = (Switch) mHolder.findViewById(R.id.switchWidget);
        final CompoundButton toggle = (CompoundButton) mHolder.findViewById(R.id.switchWidget);
        mPreference.onBindViewHolder(mHolder);
        toggle.setEnabled(false);

@@ -122,7 +122,7 @@ public class PrimarySwitchPreferenceTest {
    @Test
    public void clickWidgetView_shouldNotifyPreferenceChanged() {

        final Switch toggle = (Switch) mHolder.findViewById(R.id.switchWidget);
        final CompoundButton toggle = (CompoundButton) mHolder.findViewById(R.id.switchWidget);

        final OnPreferenceChangeListener listener = mock(OnPreferenceChangeListener.class);
        mPreference.setOnPreferenceChangeListener(listener);
@@ -139,7 +139,7 @@ public class PrimarySwitchPreferenceTest {

    @Test
    public void setDisabledByAdmin_hasEnforcedAdmin_shouldDisableButton() {
        final Switch toggle = (Switch) mHolder.findViewById(R.id.switchWidget);
        final CompoundButton toggle = (CompoundButton) mHolder.findViewById(R.id.switchWidget);
        toggle.setEnabled(true);
        mPreference.onBindViewHolder(mHolder);

@@ -149,7 +149,7 @@ public class PrimarySwitchPreferenceTest {

    @Test
    public void setDisabledByAdmin_noEnforcedAdmin_shouldEnableButton() {
        final Switch toggle = (Switch) mHolder.findViewById(R.id.switchWidget);
        final CompoundButton toggle = (CompoundButton) mHolder.findViewById(R.id.switchWidget);
        toggle.setEnabled(false);
        mPreference.onBindViewHolder(mHolder);

@@ -159,7 +159,7 @@ public class PrimarySwitchPreferenceTest {

    @Test
    public void onBindViewHolder_toggleButtonShouldHaveContentDescription() {
        final Switch toggle = (Switch) mHolder.findViewById(R.id.switchWidget);
        final CompoundButton toggle = (CompoundButton) mHolder.findViewById(R.id.switchWidget);
        final String label = "TestButton";
        mPreference.setTitle(label);