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

Commit be8e34e0 authored by Sebastian Pickl's avatar Sebastian Pickl Committed by Android (Google) Code Review
Browse files

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
parent 9b43c4bd
Loading
Loading
Loading
Loading
+19 −11
Original line number Diff line number Diff line
@@ -24,8 +24,8 @@ import android.util.AttributeSet;
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 @@ import java.util.List;
 * 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 @@ public class MainSwitchBar extends LinearLayout implements OnCheckedChangeListen
    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 @@ public class MainSwitchBar extends LinearLayout implements OnCheckedChangeListen
        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) {
@@ -135,6 +135,13 @@ public class MainSwitchBar extends LinearLayout implements OnCheckedChangeListen
        return mSwitch.isChecked();
    }

    /**
     * Return the Switch
     */
    public final Switch getSwitch() {
        return mSwitch;
    }

    /**
     * Set the title text
     */
@@ -185,7 +192,7 @@ public class MainSwitchBar extends LinearLayout implements OnCheckedChangeListen
    /**
     * 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 @@ public class MainSwitchBar extends LinearLayout implements OnCheckedChangeListen
    /**
     * Remove a listener for switch changes
     */
    public void removeOnSwitchChangeListener(OnCheckedChangeListener listener) {
    public void removeOnSwitchChangeListener(OnMainSwitchChangeListener listener) {
        mSwitchChangeListeners.remove(listener);
    }

@@ -216,8 +223,9 @@ public class MainSwitchBar extends LinearLayout implements OnCheckedChangeListen
    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);
        }
    }

+9 −10
Original line number Diff line number Diff line
@@ -19,25 +19,24 @@ package com.android.settingslib.widget;
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 @@ public class MainSwitchPreference extends TwoStatePreference implements OnChecke
    }

    @Override
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
    public void onSwitchChanged(Switch switchView, boolean isChecked) {
        super.setChecked(isChecked);
    }

@@ -139,7 +138,7 @@ public class MainSwitchPreference extends TwoStatePreference implements OnChecke
    /**
     * 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 @@ public class MainSwitchPreference extends TwoStatePreference implements OnChecke
    /**
     * 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 @@ public class MainSwitchPreference extends TwoStatePreference implements OnChecke
    }

    private void registerListenerToSwitchBar() {
        for (OnCheckedChangeListener listener : mSwitchChangeListeners) {
        for (OnMainSwitchChangeListener listener : mSwitchChangeListeners) {
            mMainSwitchBar.addOnSwitchChangeListener(listener);
        }
    }
+32 −0
Original line number Diff line number Diff line
/*
 * 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);
}
+21 −11
Original line number Diff line number Diff line
@@ -21,25 +21,30 @@ import static android.graphics.text.LineBreakConfig.LINE_BREAK_WORD_STYLE_PHRASE
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 @@ public class MainSwitchBarTest {
        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 @@ public class MainSwitchBarTest {

        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