Merge "Use SettingsLib's MainSwitchBar to replace SwitchBar in Settings."
This commit is contained in:
@@ -49,7 +49,7 @@ public class GenericSwitchController extends SwitchWidgetController implements
|
||||
}
|
||||
|
||||
@Override
|
||||
public void updateTitle(boolean isChecked) {
|
||||
public void setTitle(String title) {
|
||||
}
|
||||
|
||||
@Override
|
||||
|
87
src/com/android/settings/widget/MainSwitchBarController.java
Normal file
87
src/com/android/settings/widget/MainSwitchBarController.java
Normal file
@@ -0,0 +1,87 @@
|
||||
/*
|
||||
* 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.settings.widget;
|
||||
|
||||
import android.widget.Switch;
|
||||
|
||||
import com.android.settingslib.RestrictedLockUtils;
|
||||
import com.android.settingslib.widget.OnMainSwitchChangeListener;
|
||||
|
||||
/**
|
||||
* The switch controller that is used to update the switch widget in the SettingsMainSwitchBar.
|
||||
*/
|
||||
public class MainSwitchBarController extends SwitchWidgetController implements
|
||||
OnMainSwitchChangeListener {
|
||||
|
||||
private final SettingsMainSwitchBar mMainSwitch;
|
||||
|
||||
public MainSwitchBarController(SettingsMainSwitchBar mainSwitch) {
|
||||
mMainSwitch = mainSwitch;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setupView() {
|
||||
mMainSwitch.show();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void teardownView() {
|
||||
mMainSwitch.hide();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setTitle(String title) {
|
||||
mMainSwitch.setTitle(title);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void startListening() {
|
||||
mMainSwitch.addOnSwitchChangeListener(this);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void stopListening() {
|
||||
mMainSwitch.removeOnSwitchChangeListener(this);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setChecked(boolean checked) {
|
||||
mMainSwitch.setChecked(checked);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isChecked() {
|
||||
return mMainSwitch.isChecked();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setEnabled(boolean enabled) {
|
||||
mMainSwitch.setEnabled(enabled);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onSwitchChanged(Switch switchView, boolean isChecked) {
|
||||
if (mListener != null) {
|
||||
mListener.onSwitchToggled(isChecked);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setDisabledByAdmin(RestrictedLockUtils.EnforcedAdmin admin) {
|
||||
mMainSwitch.setDisabledByAdmin(admin);
|
||||
}
|
||||
}
|
129
src/com/android/settings/widget/SettingsMainSwitchBar.java
Normal file
129
src/com/android/settings/widget/SettingsMainSwitchBar.java
Normal file
@@ -0,0 +1,129 @@
|
||||
/*
|
||||
* 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.settings.widget;
|
||||
|
||||
import android.app.settings.SettingsEnums;
|
||||
import android.content.Context;
|
||||
import android.util.AttributeSet;
|
||||
import android.view.View;
|
||||
import android.widget.Switch;
|
||||
|
||||
import com.android.settings.overlay.FeatureFactory;
|
||||
import com.android.settingslib.core.instrumentation.MetricsFeatureProvider;
|
||||
import com.android.settingslib.widget.MainSwitchBar;
|
||||
|
||||
/**
|
||||
* A {@link MainSwitchBar} with a customized Switch and provides the metrics feature.
|
||||
*/
|
||||
public class SettingsMainSwitchBar extends MainSwitchBar {
|
||||
|
||||
/**
|
||||
* Called before the checked state of the Switch has changed.
|
||||
*/
|
||||
public interface OnBeforeCheckedChangeListener {
|
||||
|
||||
/**
|
||||
* @param switchView The Switch view whose state has changed.
|
||||
* @param isChecked The new checked state of switchView.
|
||||
*/
|
||||
boolean onBeforeCheckedChanged(Switch switchView, boolean isChecked);
|
||||
}
|
||||
|
||||
private final MetricsFeatureProvider mMetricsFeatureProvider;
|
||||
private OnBeforeCheckedChangeListener mOnBeforeListener;
|
||||
|
||||
private Switch mSwitch;
|
||||
private String mMetricsTag;
|
||||
|
||||
public SettingsMainSwitchBar(Context context) {
|
||||
this(context, null);
|
||||
}
|
||||
|
||||
public SettingsMainSwitchBar(Context context, AttributeSet attrs) {
|
||||
this(context, attrs, 0);
|
||||
}
|
||||
|
||||
public SettingsMainSwitchBar(Context context, AttributeSet attrs, int defStyleAttr) {
|
||||
this(context, attrs, defStyleAttr, 0);
|
||||
}
|
||||
|
||||
public SettingsMainSwitchBar(Context context, AttributeSet attrs, int defStyleAttr,
|
||||
int defStyleRes) {
|
||||
super(context, attrs, defStyleAttr, defStyleRes);
|
||||
mMetricsFeatureProvider = FeatureFactory.getFactory(context).getMetricsFeatureProvider();
|
||||
|
||||
mSwitch = (Switch) findViewById(android.R.id.switch_widget);
|
||||
|
||||
addOnSwitchChangeListener((switchView, isChecked) -> logMetrics(isChecked));
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onRestrictedIconClick() {
|
||||
mMetricsFeatureProvider.action(
|
||||
SettingsEnums.PAGE_UNKNOWN,
|
||||
SettingsEnums.ACTION_SETTINGS_PREFERENCE_CHANGE,
|
||||
SettingsEnums.PAGE_UNKNOWN,
|
||||
mMetricsTag + "/switch_bar|restricted",
|
||||
1);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setChecked(boolean checked) {
|
||||
if (mOnBeforeListener != null
|
||||
&& mOnBeforeListener.onBeforeCheckedChanged(mSwitch, checked)) {
|
||||
return;
|
||||
}
|
||||
super.setChecked(checked);
|
||||
}
|
||||
|
||||
/**
|
||||
* Update the status of switch but doesn't notify the mOnBeforeListener.
|
||||
*/
|
||||
public void setCheckedInternal(boolean checked) {
|
||||
super.setChecked(checked);
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the OnBeforeCheckedChangeListener.
|
||||
*/
|
||||
public void setOnBeforeCheckedChangeListener(OnBeforeCheckedChangeListener listener) {
|
||||
mOnBeforeListener = listener;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns if this view is visible.
|
||||
*/
|
||||
public boolean isShowing() {
|
||||
return (getVisibility() == View.VISIBLE);
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the metrics tag.
|
||||
*/
|
||||
public void setMetricsTag(String tag) {
|
||||
mMetricsTag = tag;
|
||||
}
|
||||
|
||||
private void logMetrics(boolean isChecked) {
|
||||
mMetricsFeatureProvider.action(
|
||||
SettingsEnums.PAGE_UNKNOWN,
|
||||
SettingsEnums.ACTION_SETTINGS_PREFERENCE_CHANGE,
|
||||
SettingsEnums.PAGE_UNKNOWN,
|
||||
mMetricsTag + "/switch_bar",
|
||||
isChecked ? 1 : 0);
|
||||
}
|
||||
}
|
@@ -0,0 +1,217 @@
|
||||
/*
|
||||
* Copyright (C) 2021 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.settings.widget;
|
||||
|
||||
import android.content.Context;
|
||||
import android.util.AttributeSet;
|
||||
|
||||
import androidx.preference.PreferenceViewHolder;
|
||||
import androidx.preference.TwoStatePreference;
|
||||
|
||||
import com.android.settings.R;
|
||||
import com.android.settings.widget.SettingsMainSwitchBar.OnBeforeCheckedChangeListener;
|
||||
import com.android.settingslib.RestrictedLockUtils;
|
||||
import com.android.settingslib.widget.OnMainSwitchChangeListener;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* SettingsMainSwitchPreference 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 SettingsMainSwitchPreference extends TwoStatePreference {
|
||||
private final List<OnBeforeCheckedChangeListener> mBeforeCheckedChangeListeners =
|
||||
new ArrayList<>();
|
||||
private final List<OnMainSwitchChangeListener> mSwitchChangeListeners = new ArrayList<>();
|
||||
|
||||
private SettingsMainSwitchBar mMainSwitchBar;
|
||||
private String mTitle;
|
||||
|
||||
private RestrictedLockUtils.EnforcedAdmin mEnforcedAdmin;
|
||||
|
||||
public SettingsMainSwitchPreference(Context context) {
|
||||
super(context);
|
||||
init();
|
||||
}
|
||||
|
||||
public SettingsMainSwitchPreference(Context context, AttributeSet attrs) {
|
||||
super(context, attrs);
|
||||
init();
|
||||
}
|
||||
|
||||
public SettingsMainSwitchPreference(Context context, AttributeSet attrs, int defStyleAttr) {
|
||||
super(context, attrs, defStyleAttr);
|
||||
init();
|
||||
}
|
||||
|
||||
public SettingsMainSwitchPreference(Context context, AttributeSet attrs, int defStyleAttr,
|
||||
int defStyleRes) {
|
||||
super(context, attrs, defStyleAttr, defStyleRes);
|
||||
init();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onBindViewHolder(PreferenceViewHolder holder) {
|
||||
super.onBindViewHolder(holder);
|
||||
|
||||
holder.setDividerAllowedAbove(true);
|
||||
holder.setDividerAllowedBelow(false);
|
||||
|
||||
mMainSwitchBar = (SettingsMainSwitchBar) holder.findViewById(R.id.main_switch_bar);
|
||||
mMainSwitchBar.show();
|
||||
updateStatus(isChecked());
|
||||
registerListenerToSwitchBar();
|
||||
}
|
||||
|
||||
private void init() {
|
||||
setLayoutResource(R.layout.preference_widget_main_switch);
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the SettingsMainSwitchBar
|
||||
*/
|
||||
public final SettingsMainSwitchBar getSwitchBar() {
|
||||
return mMainSwitchBar;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the preference title text
|
||||
*/
|
||||
public void setTitle(String text) {
|
||||
mTitle = text;
|
||||
if (mMainSwitchBar != null) {
|
||||
mMainSwitchBar.setTitle(mTitle);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Update the switch status of preference
|
||||
*/
|
||||
public void updateStatus(boolean checked) {
|
||||
setChecked(checked);
|
||||
if (mMainSwitchBar != null) {
|
||||
mMainSwitchBar.setChecked(checked);
|
||||
mMainSwitchBar.setTitle(mTitle);
|
||||
mMainSwitchBar.setDisabledByAdmin(mEnforcedAdmin);
|
||||
mMainSwitchBar.show();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Show the MainSwitchBar
|
||||
*/
|
||||
public void show() {
|
||||
if (mMainSwitchBar != null) {
|
||||
mMainSwitchBar.show();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Hide the MainSwitchBar
|
||||
*/
|
||||
public void hide() {
|
||||
if (mMainSwitchBar != null) {
|
||||
mMainSwitchBar.hide();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns if the MainSwitchBar is visible.
|
||||
*/
|
||||
public boolean isShowing() {
|
||||
if (mMainSwitchBar != null) {
|
||||
return mMainSwitchBar.isShowing();
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Update the status of switch but doesn't notify the mOnBeforeListener.
|
||||
*/
|
||||
public void setCheckedInternal(boolean checked) {
|
||||
if (mMainSwitchBar != null) {
|
||||
mMainSwitchBar.setCheckedInternal(checked);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Enable or disable the text and switch.
|
||||
*/
|
||||
public void setEnabled(boolean enabled) {
|
||||
if (mMainSwitchBar != null) {
|
||||
mMainSwitchBar.setEnabled(enabled);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the OnBeforeCheckedChangeListener.
|
||||
*/
|
||||
public void setOnBeforeCheckedChangeListener(OnBeforeCheckedChangeListener listener) {
|
||||
if (mMainSwitchBar == null) {
|
||||
mBeforeCheckedChangeListeners.add(listener);
|
||||
} else {
|
||||
mMainSwitchBar.setOnBeforeCheckedChangeListener(listener);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds a listener for switch changes
|
||||
*/
|
||||
public void addOnSwitchChangeListener(OnMainSwitchChangeListener listener) {
|
||||
if (mMainSwitchBar == null) {
|
||||
mSwitchChangeListeners.add(listener);
|
||||
} else {
|
||||
mMainSwitchBar.addOnSwitchChangeListener(listener);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Remove a listener for switch changes
|
||||
*/
|
||||
public void removeOnSwitchChangeListener(OnMainSwitchChangeListener listener) {
|
||||
if (mMainSwitchBar == null) {
|
||||
mSwitchChangeListeners.remove(listener);
|
||||
} else {
|
||||
mMainSwitchBar.removeOnSwitchChangeListener(listener);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* If admin is not null, disables the text and switch but keeps the view clickable.
|
||||
* Otherwise, calls setEnabled which will enables the entire view including
|
||||
* the text and switch.
|
||||
*/
|
||||
public void setDisabledByAdmin(RestrictedLockUtils.EnforcedAdmin admin) {
|
||||
mEnforcedAdmin = admin;
|
||||
if (mMainSwitchBar != null) {
|
||||
mMainSwitchBar.setDisabledByAdmin(mEnforcedAdmin);
|
||||
}
|
||||
}
|
||||
|
||||
private void registerListenerToSwitchBar() {
|
||||
for (OnBeforeCheckedChangeListener listener : mBeforeCheckedChangeListeners) {
|
||||
mMainSwitchBar.setOnBeforeCheckedChangeListener(listener);
|
||||
}
|
||||
for (OnMainSwitchChangeListener listener : mSwitchChangeListeners) {
|
||||
mMainSwitchBar.addOnSwitchChangeListener(listener);
|
||||
}
|
||||
mBeforeCheckedChangeListeners.clear();
|
||||
mSwitchChangeListeners.clear();
|
||||
}
|
||||
}
|
@@ -43,8 +43,7 @@ public class SwitchBarController extends SwitchWidgetController implements
|
||||
}
|
||||
|
||||
@Override
|
||||
public void updateTitle(boolean isChecked) {
|
||||
mSwitchBar.setTextViewLabelAndBackground(isChecked);
|
||||
public void setTitle(String title) {
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@@ -65,9 +65,9 @@ public abstract class SwitchWidgetController {
|
||||
/**
|
||||
* Update the preference title associated with the switch.
|
||||
*
|
||||
* @param isChecked whether the switch is currently checked
|
||||
* @param title the preference title
|
||||
*/
|
||||
public abstract void updateTitle(boolean isChecked);
|
||||
public abstract void setTitle(String title);
|
||||
|
||||
/**
|
||||
* Start listening to switch toggling.
|
||||
@@ -107,4 +107,4 @@ public abstract class SwitchWidgetController {
|
||||
* is {@code null}, then this preference will be enabled. Otherwise, it will be disabled.
|
||||
*/
|
||||
public abstract void setDisabledByAdmin(EnforcedAdmin admin);
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user