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

Commit daea3b48 authored by Jacky Wang's avatar Jacky Wang
Browse files

[Catalyst] Add MainSwitchBar.PreChangeListener

Bug: 335132588
Flag: EXEMPT library
Test: manual
Change-Id: Ib46675948770524813a5d9844a4ab5bab9bd3f23
parent 79ea2bf9
Loading
Loading
Loading
Loading
+27 −2
Original line number Diff line number Diff line
@@ -32,6 +32,7 @@ import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.ColorInt;
import androidx.annotation.Nullable;
import androidx.annotation.RequiresApi;

import com.android.settingslib.widget.mainswitch.R;
@@ -42,7 +43,7 @@ import java.util.List;
/**
 * MainSwitchBar is a View with a customized Switch.
 * This component is used as the main switch of the page
 * to enable or disable the prefereces on the page.
 * to enable or disable the preferences on the page.
 */
public class MainSwitchBar extends LinearLayout implements OnCheckedChangeListener {

@@ -58,6 +59,8 @@ public class MainSwitchBar extends LinearLayout implements OnCheckedChangeListen
    protected CompoundButton mSwitch;
    private final View mFrameView;

    private @Nullable PreChangeListener mPreChangeListener;

    public MainSwitchBar(Context context) {
        this(context, null);
    }
@@ -138,10 +141,20 @@ public class MainSwitchBar extends LinearLayout implements OnCheckedChangeListen

    @Override
    public boolean performClick() {
        if (callPreChangeListener()) {
            mSwitch.performClick();
        }
        return super.performClick();
    }

    protected boolean callPreChangeListener() {
        return mPreChangeListener == null || mPreChangeListener.preChange(!mSwitch.isChecked());
    }

    public void setPreChangeListener(@Nullable PreChangeListener preChangeListener) {
        mPreChangeListener = preChangeListener;
    }

    /**
     * Update the switch status
     */
@@ -341,4 +354,16 @@ public class MainSwitchBar extends LinearLayout implements OnCheckedChangeListen

        requestLayout();
    }

    /**
     * Listener callback before switch is toggled.
     */
    public interface PreChangeListener {
        /**
         * Returns if the new value can be set.
         *
         * When false is return, the switch toggle is not triggered at all.
         */
        boolean preChange(boolean isCheck);
    }
}