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

@@ -24,6 +24,7 @@ import android.content.IntentFilter;
import android.os.Handler;
import android.os.Message;
import android.provider.Settings;
import android.widget.Switch;
import android.widget.Toast;
import com.android.internal.logging.nano.MetricsProto.MetricsEvent;
@@ -41,6 +42,7 @@ import com.android.settingslib.bluetooth.LocalBluetoothManager;
* preference reflects the current state.
*/
public final class BluetoothEnabler implements SwitchWidgetController.OnSwitchChangeListener {
private final Switch mSwitch;
private final SwitchWidgetController mSwitchWidget;
private final MetricsFeatureProvider mMetricsFeatureProvider;
private Context mContext;
@@ -79,6 +81,7 @@ public final class BluetoothEnabler implements SwitchWidgetController.OnSwitchCh
mContext = context;
mMetricsFeatureProvider = metricsFeatureProvider;
mSwitchWidget = switchWidget;
mSwitch = mSwitchWidget.getSwitch();
mSwitchWidget.setListener(this);
mValidListener = false;
@@ -92,11 +95,11 @@ public final class BluetoothEnabler implements SwitchWidgetController.OnSwitchCh
mIntentFilter = new IntentFilter(BluetoothAdapter.ACTION_STATE_CHANGED);
}
public void setupSwitchBar() {
public void setupSwitchController() {
mSwitchWidget.setupView();
}
public void teardownSwitchBar() {
public void teardownSwitchController() {
mSwitchWidget.teardownView();
}
@@ -184,7 +187,7 @@ public final class BluetoothEnabler implements SwitchWidgetController.OnSwitchCh
!WirelessUtils.isRadioAllowed(mContext, Settings.Global.RADIO_BLUETOOTH)) {
Toast.makeText(mContext, R.string.wifi_in_airplane_mode, Toast.LENGTH_SHORT).show();
// Reset switch to off
mSwitchWidget.setChecked(false);
mSwitch.setChecked(false);
return false;
}
@@ -196,8 +199,8 @@ public final class BluetoothEnabler implements SwitchWidgetController.OnSwitchCh
// a) The switch should be OFF but it should still be togglable (enabled = True)
// b) The switch bar should have OFF text.
if (isChecked && !status) {
mSwitchWidget.setChecked(false);
mSwitchWidget.setEnabled(true);
mSwitch.setChecked(false);
mSwitch.setEnabled(true);
mSwitchWidget.updateTitle(false);
return false;
}

View File

@@ -25,12 +25,13 @@ import com.android.settings.core.lifecycle.events.OnResume;
import com.android.settings.core.lifecycle.events.OnStart;
import com.android.settings.core.lifecycle.events.OnStop;
import com.android.settings.overlay.FeatureFactory;
import com.android.settings.widget.SummaryUpdater.OnSummaryChangeListener;
import com.android.settings.widget.MasterSwitchPreference;
import com.android.settings.widget.MasterSwitchController;
import com.android.settingslib.bluetooth.LocalBluetoothManager;
public class BluetoothMasterSwitchPreferenceController extends PreferenceController
implements BluetoothSummaryHelper.OnSummaryChangeListener,
implements OnSummaryChangeListener,
LifecycleObserver, OnResume, OnPause, OnStart, OnStop {
private static final String KEY_TOGGLE_BLUETOOTH = "toggle_bluetooth";
@@ -38,14 +39,13 @@ public class BluetoothMasterSwitchPreferenceController extends PreferenceControl
private LocalBluetoothManager mBluetoothManager;
private MasterSwitchPreference mBtPreference;
private BluetoothEnabler mBluetoothEnabler;
private BluetoothSummaryHelper mSummaryHelper;
private BluetoothSummaryUpdater mSummaryUpdater;
public BluetoothMasterSwitchPreferenceController(Context context,
LocalBluetoothManager bluetoothManager) {
super(context);
mBluetoothManager = bluetoothManager;
mSummaryHelper = new BluetoothSummaryHelper(mContext, mBluetoothManager);
mSummaryHelper.setOnSummaryChangeListener(this);
mSummaryUpdater = new BluetoothSummaryUpdater(mContext, this, mBluetoothManager);
}
@Override
@@ -68,12 +68,12 @@ public class BluetoothMasterSwitchPreferenceController extends PreferenceControl
}
public void onResume() {
mSummaryHelper.setListening(true);
mSummaryUpdater.register(true);
}
@Override
public void onPause() {
mSummaryHelper.setListening(false);
mSummaryUpdater.register(false);
}
@Override

View File

@@ -45,7 +45,7 @@ import com.android.internal.logging.nano.MetricsProto.MetricsEvent;
import com.android.settings.LinkifyUtils;
import com.android.settings.R;
import com.android.settings.SettingsActivity;
import com.android.settings.bluetooth.BluetoothSummaryHelper.OnSummaryChangeListener;
import com.android.settings.widget.SummaryUpdater.OnSummaryChangeListener;
import com.android.settings.dashboard.SummaryLoader;
import com.android.settings.location.ScanningSettings;
import com.android.settings.search.BaseSearchIndexProvider;
@@ -150,14 +150,14 @@ public final class BluetoothSettings extends DeviceListPreferenceFragment implem
mBluetoothEnabler = new BluetoothEnabler(activity, new SwitchBarController(mSwitchBar),
mMetricsFeatureProvider, Utils.getLocalBtManager(activity));
mBluetoothEnabler.setupSwitchBar();
mBluetoothEnabler.setupSwitchController();
}
@Override
public void onDestroyView() {
super.onDestroyView();
mBluetoothEnabler.teardownSwitchBar();
mBluetoothEnabler.teardownSwitchController();
}
@Override
@@ -516,20 +516,19 @@ public final class BluetoothSettings extends DeviceListPreferenceFragment implem
private final SummaryLoader mSummaryLoader;
@VisibleForTesting
BluetoothSummaryHelper mSummaryHelper;
BluetoothSummaryUpdater mSummaryUpdater;
public SummaryProvider(Context context, SummaryLoader summaryLoader,
LocalBluetoothManager bluetoothManager) {
mBluetoothManager = bluetoothManager;
mContext = context;
mSummaryLoader = summaryLoader;
mSummaryHelper = new BluetoothSummaryHelper(mContext, mBluetoothManager);
mSummaryHelper.setOnSummaryChangeListener(this);
mSummaryUpdater = new BluetoothSummaryUpdater(mContext, this, mBluetoothManager);
}
@Override
public void setListening(boolean listening) {
mSummaryHelper.setListening(listening);
mSummaryUpdater.register(listening);
}
@Override

View File

@@ -18,8 +18,8 @@ package com.android.settings.bluetooth;
import android.bluetooth.BluetoothAdapter;
import android.content.Context;
import android.text.TextUtils;
import com.android.settings.R;
import com.android.settings.widget.SummaryUpdater;
import com.android.settingslib.bluetooth.BluetoothCallback;
import com.android.settingslib.bluetooth.CachedBluetoothDevice;
import com.android.settingslib.bluetooth.LocalBluetoothAdapter;
@@ -30,29 +30,17 @@ import java.util.Collection;
* Helper class that listeners to bluetooth callback and notify client when there is update in
* bluetooth summary info.
*/
public final class BluetoothSummaryHelper implements BluetoothCallback {
private OnSummaryChangeListener mListener;
public final class BluetoothSummaryUpdater extends SummaryUpdater implements BluetoothCallback {
private final LocalBluetoothManager mBluetoothManager;
private final LocalBluetoothAdapter mBluetoothAdapter;
private final Context mContext;
private boolean mEnabled;
private int mConnectionState;
private String mSummary;
public interface OnSummaryChangeListener {
/**
* Called when bluetooth summary has changed.
*
* @param summary The new bluetooth summary .
*/
void onSummaryChanged(String summary);
}
public BluetoothSummaryHelper(Context context, LocalBluetoothManager bluetoothManager) {
mContext = context;
public BluetoothSummaryUpdater(Context context, OnSummaryChangeListener listener,
LocalBluetoothManager bluetoothManager) {
super(context, listener);
mBluetoothManager = bluetoothManager;
mBluetoothAdapter = mBluetoothManager != null
? mBluetoothManager.getBluetoothAdapter() : null;
@@ -88,11 +76,8 @@ public final class BluetoothSummaryHelper implements BluetoothCallback {
public void onDeviceBondStateChanged(CachedBluetoothDevice cachedDevice, int bondState) {
}
public void setOnSummaryChangeListener(OnSummaryChangeListener listener) {
mListener = listener;
}
public void setListening(boolean listening) {
@Override
public void register(boolean listening) {
if (mBluetoothAdapter == null) {
return;
}
@@ -106,17 +91,8 @@ public final class BluetoothSummaryHelper implements BluetoothCallback {
}
}
private void notifyChangeIfNeeded() {
String summary = getSummary();
if (!TextUtils.equals(mSummary, summary)) {
mSummary = summary;
if (mListener != null) {
mListener.onSummaryChanged(summary);
}
}
}
private String getSummary() {
@Override
public String getSummary() {
if (!mEnabled) {
return mContext.getString(R.string.bluetooth_disabled);
}