Update Network & internet->Wi-Fi to use MasterSwitchPreference.

- Add a preference controller for Network & internet->Wi-Fi to control
  the preference toggling and summary update.
- Refactor WifiSettings and WifiEnabler to share code between the new
  wifi preference controller and the wifi setting.
- Refactor BluetoothSummaryHelper to have a common base class with the
  WifiSummaryHelper.
- Rename the summary helper to summary updater.

Bug: 34280769
Test: make RunSettingsRoboTests
Change-Id: I00ebfc161bcef89331bb41ba405ed8cb8232d248
This commit is contained in:
Doris Ling
2017-01-23 16:16:06 -08:00
parent 42c61c10cc
commit c4c9f4d50e
24 changed files with 926 additions and 171 deletions

View File

@@ -17,6 +17,8 @@
package com.android.settings.widget;
import android.support.v7.preference.Preference;
import android.widget.Switch;
import com.android.settingslib.RestrictedLockUtils.EnforcedAdmin;
/*
* The switch controller that is used to update the switch widget in the MasterSwitchPreference
@@ -67,4 +69,14 @@ public class MasterSwitchController extends SwitchWidgetController implements
}
return false;
}
@Override
public void setDisabledByAdmin(EnforcedAdmin admin) {
mPreference.setDisabledByAdmin(admin);
}
@Override
public Switch getSwitch() {
return mPreference.getSwitch();
}
}

View File

@@ -24,6 +24,7 @@ import android.widget.CompoundButton;
import android.widget.Switch;
import com.android.settings.R;
import com.android.settingslib.RestrictedLockUtils.EnforcedAdmin;
/**
* A custom preference that provides inline switch toggle. It has a mandatory field for title, and
@@ -76,7 +77,7 @@ public class MasterSwitchPreference extends Preference {
}
public boolean isChecked() {
return isEnabled() && mChecked;
return mSwitch != null && mSwitch.isEnabled() && mChecked;
}
public void setChecked(boolean checked) {
@@ -87,7 +88,7 @@ public class MasterSwitchPreference extends Preference {
}
public boolean isSwitchEnabled() {
return isEnabled() && mSwitch != null && mSwitch.isEnabled();
return mSwitch != null && mSwitch.isEnabled();
}
public void setSwitchEnabled(boolean enabled) {
@@ -96,6 +97,18 @@ public class MasterSwitchPreference extends Preference {
}
}
/**
* If admin is not null, disables the switch.
* Otherwise, keep it enabled.
*/
public void setDisabledByAdmin(EnforcedAdmin admin) {
setSwitchEnabled(admin == null);
}
public Switch getSwitch() {
return mSwitch;
}
private void init() {
setWidgetLayoutResource(R.layout.preference_widget_master_switch);
}

View File

@@ -0,0 +1,86 @@
/*
* Copyright (C) 2017 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.text.TextUtils;
/**
* Helper class that listens to settings changes and notifies client when there is update in
* corresponding summary info.
*/
public abstract class SummaryUpdater {
protected final Context mContext;
private final OnSummaryChangeListener mListener;
private String mSummary;
/**
* Interface definition for a callback to be invoked when the summary has been changed.
*/
public interface OnSummaryChangeListener {
/**
* Called when summary has changed.
*
* @param summary The new summary .
*/
void onSummaryChanged(String summary);
}
/**
* Constructor
*
* @param context The Context the updater is running in, through which it can register broadcast
* receiver etc.
* @param listener The listener that would like to receive summary change notification.
*
*/
public SummaryUpdater(Context context, OnSummaryChangeListener listener) {
mContext = context;
mListener = listener;
}
/**
* Notifies the listener when there is update in summary
*/
protected void notifyChangeIfNeeded() {
String summary = getSummary();
if (!TextUtils.equals(mSummary, summary)) {
mSummary = summary;
if (mListener != null) {
mListener.onSummaryChanged(summary);
}
}
}
/**
* Starts/stops receiving updates on the summary.
*
* @param register true if we want to receive updates, false otherwise
*/
public abstract void register(boolean register);
/**
* Gets the summary. Subclass should checks latest conditions and update the summary
* accordingly.
*
* @return the latest summary text
*/
protected abstract String getSummary();
}

View File

@@ -17,6 +17,7 @@
package com.android.settings.widget;
import android.widget.Switch;
import com.android.settingslib.RestrictedLockUtils.EnforcedAdmin;
/*
* The switch controller that is used to update the switch widget in the SwitchBar layout.
@@ -25,11 +26,9 @@ public class SwitchBarController extends SwitchWidgetController implements
SwitchBar.OnSwitchChangeListener {
private final SwitchBar mSwitchBar;
private final Switch mSwitch;
public SwitchBarController(SwitchBar switchBar) {
mSwitchBar = switchBar;
mSwitch = switchBar.getSwitch();
}
@Override
@@ -59,17 +58,17 @@ public class SwitchBarController extends SwitchWidgetController implements
@Override
public void setChecked(boolean checked) {
mSwitch.setChecked(checked);
mSwitchBar.setChecked(checked);
}
@Override
public boolean isChecked() {
return mSwitch.isChecked();
return mSwitchBar.isChecked();
}
@Override
public void setEnabled(boolean enabled) {
mSwitch.setEnabled(enabled);
mSwitchBar.setEnabled(enabled);
}
@Override
@@ -78,4 +77,15 @@ public class SwitchBarController extends SwitchWidgetController implements
mListener.onSwitchToggled(isChecked);
}
}
@Override
public void setDisabledByAdmin(EnforcedAdmin admin) {
mSwitchBar.setDisabledByAdmin(admin);
}
@Override
public Switch getSwitch() {
return mSwitchBar.getSwitch();
}
}

View File

@@ -16,6 +16,9 @@
package com.android.settings.widget;
import android.widget.Switch;
import com.android.settingslib.RestrictedLockUtils.EnforcedAdmin;
/*
* A controller class for general switch widget handling. We have different containers that provide
* different forms of switch layout. Provide a centralized control for updating the switch widget.
@@ -24,35 +27,93 @@ public abstract class SwitchWidgetController {
protected OnSwitchChangeListener mListener;
/**
* Interface definition for a callback to be invoked when the switch has been toggled.
*/
public interface OnSwitchChangeListener {
/**
* Called when the checked state of the Switch has changed.
*
* @param isChecked The new checked state of switchView.
* @param isChecked The new checked state of switchView.
*
* @return true to update the state of the switch with the new value.
*/
boolean onSwitchToggled(boolean isChecked);
}
/**
* Perform any view setup.
*/
public void setupView() {
}
/**
* Perform any view teardown.
*/
public void teardownView() {
}
/**
* Set the callback to be invoked when the switch is toggled by the user (but before the
* internal state has been updated).
*
* @param listener the callback to be invoked
*/
public void setListener(OnSwitchChangeListener listener) {
mListener = listener;
}
/**
* Update the preference title associated with the switch.
*
* @param isChecked whether the switch is currently checked
*/
public abstract void updateTitle(boolean isChecked);
/**
* Start listening to switch toggling.
*/
public abstract void startListening();
/**
* Stop listening to switch toggling.
*/
public abstract void stopListening();
/**
* Set the checked state for the switch.
*
* @param checked whether the switch should be checked or not.
*/
public abstract void setChecked(boolean checked);
/**
* Get the checked state for the switch.
*
* @return true if the switch is currently checked, false otherwise.
*/
public abstract boolean isChecked();
/**
* Set the enabled state for the switch.
*
* @param enabled whether the switch should be enabled or not.
*/
public abstract void setEnabled(boolean enabled);
}
/**
* Disable the switch based on the enforce admin.
*
* @param admin Details of the admin who enforced the restriction. If it
* is {@code null}, then this preference will be enabled. Otherwise, it will be disabled.
*/
public abstract void setDisabledByAdmin(EnforcedAdmin admin);
/**
* Get the underlying switch widget.
*
* @return the switch widget.
*/
public abstract Switch getSwitch();
}