summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
author Jacky Wang <jiannan@google.com> 2024-12-09 17:02:29 +0800
committer Jacky Wang <jiannan@google.com> 2024-12-09 17:02:29 +0800
commitdaea3b48123b5b9d9746f34823b91c5f13d8dd13 (patch)
tree073928e48b8c67595a98481de893248f6cfb15dd
parent79ea2bf9be1a83b7ee35c117c986d5f10ab9008e (diff)
[Catalyst] Add MainSwitchBar.PreChangeListener
Bug: 335132588 Flag: EXEMPT library Test: manual Change-Id: Ib46675948770524813a5d9844a4ab5bab9bd3f23
-rw-r--r--packages/SettingsLib/MainSwitchPreference/src/com/android/settingslib/widget/MainSwitchBar.java29
1 files changed, 27 insertions, 2 deletions
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 106802e9d1d1..c3594b8320c5 100644
--- a/packages/SettingsLib/MainSwitchPreference/src/com/android/settingslib/widget/MainSwitchBar.java
+++ b/packages/SettingsLib/MainSwitchPreference/src/com/android/settingslib/widget/MainSwitchBar.java
@@ -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() {
- mSwitch.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);
+ }
}