Merge "Update Network & internet->Wi-Fi to use MasterSwitchPreference."
diff --git a/AndroidManifest.xml b/AndroidManifest.xml
index 2ffc5db..0de7a4e 100644
--- a/AndroidManifest.xml
+++ b/AndroidManifest.xml
@@ -3060,20 +3060,6 @@
android:value="com.android.settings.network.NetworkDashboardFragment"/>
</activity>
- <activity-alias android:name="WifiDashboardAlias"
- android:targetActivity="Settings$WifiSettingsActivity"
- android:configChanges="orientation|keyboardHidden|screenSize">
- <intent-filter android:priority="20">
- <action android:name="com.android.settings.action.SETTINGS" />
- </intent-filter>
- <meta-data android:name="com.android.settings.category"
- android:value="com.android.settings.category.ia.wireless" />
- <meta-data android:name="com.android.settings.FRAGMENT_CLASS"
- android:value="com.android.settings.wifi.WifiSettings" />
- <meta-data android:name="com.android.settings.PRIMARY_PROFILE_CONTROLLED"
- android:value="true" />
- </activity-alias>
-
<activity-alias android:name="DataUsageDashboardAlias"
android:targetActivity="Settings$DataUsageSummaryActivity">
<intent-filter android:priority="10">
diff --git a/res/xml/network_and_internet.xml b/res/xml/network_and_internet.xml
index dc2efd8..7c41498 100644
--- a/res/xml/network_and_internet.xml
+++ b/res/xml/network_and_internet.xml
@@ -19,12 +19,23 @@
xmlns:settings="http://schemas.android.com/apk/res/com.android.settings"
android:title="@string/network_dashboard_title">
+ <com.android.settings.widget.MasterSwitchPreference
+ android:fragment="com.android.settings.wifi.WifiSettings"
+ android:key="toggle_wifi"
+ android:title="@string/wifi_settings"
+ android:icon="@drawable/ic_settings_wireless"
+ android:order="-30">
+ <intent
+ android:action="android.settings.WIFI_SETTINGS"
+ android:targetClass="Settings$WifiSettingsActivity" />
+ </com.android.settings.widget.MasterSwitchPreference>
+
<SwitchPreference
android:key="toggle_airplane"
android:title="@string/airplane_mode"
android:icon="@drawable/ic_airplanemode_active"
android:disableDependentsState="true"
- android:order="-30"/>
+ android:order="5"/>
<com.android.settingslib.RestrictedPreference
android:key="mobile_network_settings"
diff --git a/src/com/android/settings/bluetooth/BluetoothEnabler.java b/src/com/android/settings/bluetooth/BluetoothEnabler.java
index aaf9f3c..83c0560 100644
--- a/src/com/android/settings/bluetooth/BluetoothEnabler.java
+++ b/src/com/android/settings/bluetooth/BluetoothEnabler.java
@@ -24,6 +24,7 @@
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 @@
* 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 @@
mContext = context;
mMetricsFeatureProvider = metricsFeatureProvider;
mSwitchWidget = switchWidget;
+ mSwitch = mSwitchWidget.getSwitch();
mSwitchWidget.setListener(this);
mValidListener = false;
@@ -92,11 +95,11 @@
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 @@
!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 @@
// 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;
}
diff --git a/src/com/android/settings/bluetooth/BluetoothMasterSwitchPreferenceController.java b/src/com/android/settings/bluetooth/BluetoothMasterSwitchPreferenceController.java
index 955454f..d9b1bea 100644
--- a/src/com/android/settings/bluetooth/BluetoothMasterSwitchPreferenceController.java
+++ b/src/com/android/settings/bluetooth/BluetoothMasterSwitchPreferenceController.java
@@ -25,12 +25,13 @@
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 @@
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 void onResume() {
- mSummaryHelper.setListening(true);
+ mSummaryUpdater.register(true);
}
@Override
public void onPause() {
- mSummaryHelper.setListening(false);
+ mSummaryUpdater.register(false);
}
@Override
diff --git a/src/com/android/settings/bluetooth/BluetoothSettings.java b/src/com/android/settings/bluetooth/BluetoothSettings.java
index ba53ea3..6e7ae47 100644
--- a/src/com/android/settings/bluetooth/BluetoothSettings.java
+++ b/src/com/android/settings/bluetooth/BluetoothSettings.java
@@ -45,7 +45,7 @@
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 @@
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 @@
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
diff --git a/src/com/android/settings/bluetooth/BluetoothSummaryHelper.java b/src/com/android/settings/bluetooth/BluetoothSummaryUpdater.java
similarity index 81%
rename from src/com/android/settings/bluetooth/BluetoothSummaryHelper.java
rename to src/com/android/settings/bluetooth/BluetoothSummaryUpdater.java
index 2bd6f70..dc37efc 100644
--- a/src/com/android/settings/bluetooth/BluetoothSummaryHelper.java
+++ b/src/com/android/settings/bluetooth/BluetoothSummaryUpdater.java
@@ -18,8 +18,8 @@
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 @@
* 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 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 @@
}
}
- 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);
}
diff --git a/src/com/android/settings/connecteddevice/ConnectedDeviceDashboardFragment.java b/src/com/android/settings/connecteddevice/ConnectedDeviceDashboardFragment.java
index 481a0eb..786b9d3 100644
--- a/src/com/android/settings/connecteddevice/ConnectedDeviceDashboardFragment.java
+++ b/src/com/android/settings/connecteddevice/ConnectedDeviceDashboardFragment.java
@@ -39,7 +39,6 @@
private static final String TAG = "ConnectedDeviceFrag";
private UsbModePreferenceController mUsbPrefController;
- private BluetoothMasterSwitchPreferenceController mBluetoothPreferenceController;
@Override
public int getMetricsCategory() {
@@ -67,10 +66,11 @@
mUsbPrefController = new UsbModePreferenceController(context, new UsbBackend(context));
lifecycle.addObserver(mUsbPrefController);
controllers.add(mUsbPrefController);
- mBluetoothPreferenceController = new BluetoothMasterSwitchPreferenceController(
+ final BluetoothMasterSwitchPreferenceController bluetoothPreferenceController =
+ new BluetoothMasterSwitchPreferenceController(
context, Utils.getLocalBtManager(context));
- lifecycle.addObserver(mBluetoothPreferenceController);
- controllers.add(mBluetoothPreferenceController);
+ lifecycle.addObserver(bluetoothPreferenceController);
+ controllers.add(bluetoothPreferenceController);
return controllers;
}
diff --git a/src/com/android/settings/core/gateway/SettingsGateway.java b/src/com/android/settings/core/gateway/SettingsGateway.java
index 51a990e..1dd83cb 100644
--- a/src/com/android/settings/core/gateway/SettingsGateway.java
+++ b/src/com/android/settings/core/gateway/SettingsGateway.java
@@ -296,7 +296,6 @@
"com.android.settings.ManageApplicationsDashboardAlias",
"com.android.settings.PaymentSettingsDashboardAlias",
// Home page > Network & Internet
- "com.android.settings.WifiDashboardAlias",
"com.android.settings.DataUsageDashboardAlias",
// Home page > Security
"com.android.settings.LocationDashboardAlias",
diff --git a/src/com/android/settings/network/NetworkDashboardFragment.java b/src/com/android/settings/network/NetworkDashboardFragment.java
index 7e6de96..2d91414 100644
--- a/src/com/android/settings/network/NetworkDashboardFragment.java
+++ b/src/com/android/settings/network/NetworkDashboardFragment.java
@@ -24,9 +24,11 @@
import com.android.internal.logging.nano.MetricsProto;
import com.android.settings.R;
import com.android.settings.core.PreferenceController;
+import com.android.settings.core.lifecycle.Lifecycle;
import com.android.settings.dashboard.DashboardFragment;
import com.android.settings.overlay.FeatureFactory;
import com.android.settings.search.BaseSearchIndexProvider;
+import com.android.settings.wifi.WifiMasterSwitchPreferenceController;
import java.util.ArrayList;
import java.util.Arrays;
@@ -61,8 +63,12 @@
new AirplaneModePreferenceController(context, this /* fragment */);
final MobilePlanPreferenceController mobilePlanPreferenceController =
new MobilePlanPreferenceController(context, this);
- getLifecycle().addObserver(airplaneModePreferenceController);
- getLifecycle().addObserver(mobilePlanPreferenceController);
+ final WifiMasterSwitchPreferenceController wifiPreferenceController =
+ new WifiMasterSwitchPreferenceController(context, mMetricsFeatureProvider);
+ final Lifecycle lifecycle = getLifecycle();
+ lifecycle.addObserver(airplaneModePreferenceController);
+ lifecycle.addObserver(mobilePlanPreferenceController);
+ lifecycle.addObserver(wifiPreferenceController);
final List<PreferenceController> controllers = new ArrayList<>();
controllers.add(airplaneModePreferenceController);
@@ -73,6 +79,7 @@
controllers.add(new NetworkResetPreferenceController(context));
controllers.add(new ProxyPreferenceController(context));
controllers.add(mobilePlanPreferenceController);
+ controllers.add(wifiPreferenceController);
return controllers;
}
diff --git a/src/com/android/settings/widget/MasterSwitchController.java b/src/com/android/settings/widget/MasterSwitchController.java
index 53e5fe7..f7253fd 100644
--- a/src/com/android/settings/widget/MasterSwitchController.java
+++ b/src/com/android/settings/widget/MasterSwitchController.java
@@ -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 @@
}
return false;
}
+
+ @Override
+ public void setDisabledByAdmin(EnforcedAdmin admin) {
+ mPreference.setDisabledByAdmin(admin);
+ }
+
+ @Override
+ public Switch getSwitch() {
+ return mPreference.getSwitch();
+ }
}
diff --git a/src/com/android/settings/widget/MasterSwitchPreference.java b/src/com/android/settings/widget/MasterSwitchPreference.java
index 8130ca5..4650730 100644
--- a/src/com/android/settings/widget/MasterSwitchPreference.java
+++ b/src/com/android/settings/widget/MasterSwitchPreference.java
@@ -24,6 +24,7 @@
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 boolean isChecked() {
- return isEnabled() && mChecked;
+ return mSwitch != null && mSwitch.isEnabled() && mChecked;
}
public void setChecked(boolean checked) {
@@ -87,7 +88,7 @@
}
public boolean isSwitchEnabled() {
- return isEnabled() && mSwitch != null && mSwitch.isEnabled();
+ return mSwitch != null && mSwitch.isEnabled();
}
public void setSwitchEnabled(boolean enabled) {
@@ -96,6 +97,18 @@
}
}
+ /**
+ * 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);
}
diff --git a/src/com/android/settings/widget/SummaryUpdater.java b/src/com/android/settings/widget/SummaryUpdater.java
new file mode 100644
index 0000000..89752b9
--- /dev/null
+++ b/src/com/android/settings/widget/SummaryUpdater.java
@@ -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();
+
+}
diff --git a/src/com/android/settings/widget/SwitchBarController.java b/src/com/android/settings/widget/SwitchBarController.java
index 70fd7ba..624db2a 100644
--- a/src/com/android/settings/widget/SwitchBarController.java
+++ b/src/com/android/settings/widget/SwitchBarController.java
@@ -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 @@
SwitchBar.OnSwitchChangeListener {
private final SwitchBar mSwitchBar;
- private final Switch mSwitch;
public SwitchBarController(SwitchBar switchBar) {
mSwitchBar = switchBar;
- mSwitch = switchBar.getSwitch();
}
@Override
@@ -59,17 +58,17 @@
@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 @@
mListener.onSwitchToggled(isChecked);
}
}
+
+ @Override
+ public void setDisabledByAdmin(EnforcedAdmin admin) {
+ mSwitchBar.setDisabledByAdmin(admin);
+ }
+
+ @Override
+ public Switch getSwitch() {
+ return mSwitchBar.getSwitch();
+ }
+
}
diff --git a/src/com/android/settings/widget/SwitchWidgetController.java b/src/com/android/settings/widget/SwitchWidgetController.java
index c5a8c87..325a093 100644
--- a/src/com/android/settings/widget/SwitchWidgetController.java
+++ b/src/com/android/settings/widget/SwitchWidgetController.java
@@ -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 @@
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();
+
+}
\ No newline at end of file
diff --git a/src/com/android/settings/wifi/WifiEnabler.java b/src/com/android/settings/wifi/WifiEnabler.java
index a05e498..1ac94ea 100644
--- a/src/com/android/settings/wifi/WifiEnabler.java
+++ b/src/com/android/settings/wifi/WifiEnabler.java
@@ -29,23 +29,22 @@
import android.os.UserHandle;
import android.os.UserManager;
import android.provider.Settings;
-import android.widget.Switch;
import android.widget.Toast;
import com.android.internal.logging.nano.MetricsProto.MetricsEvent;
import com.android.settings.R;
import com.android.settings.core.instrumentation.MetricsFeatureProvider;
import com.android.settings.search.Index;
-import com.android.settings.widget.SwitchBar;
+import com.android.settings.widget.SwitchWidgetController;
import com.android.settingslib.RestrictedLockUtils;
import com.android.settingslib.RestrictedLockUtils.EnforcedAdmin;
import com.android.settingslib.WirelessUtils;
import java.util.concurrent.atomic.AtomicBoolean;
-public class WifiEnabler implements SwitchBar.OnSwitchChangeListener {
+public class WifiEnabler implements SwitchWidgetController.OnSwitchChangeListener {
- private final SwitchBar mSwitchBar;
+ private final SwitchWidgetController mSwitchWidget;
private final WifiManager mWifiManager;
private final MetricsFeatureProvider mMetricsFeatureProvider;
@@ -93,10 +92,11 @@
}
};
- public WifiEnabler(Context context, SwitchBar switchBar,
+ public WifiEnabler(Context context, SwitchWidgetController switchWidget,
MetricsFeatureProvider metricsFeatureProvider) {
mContext = context;
- mSwitchBar = switchBar;
+ mSwitchWidget = switchWidget;
+ mSwitchWidget.setListener(this);
mMetricsFeatureProvider = metricsFeatureProvider;
mWifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
@@ -105,25 +105,25 @@
mIntentFilter.addAction(WifiManager.SUPPLICANT_STATE_CHANGED_ACTION);
mIntentFilter.addAction(WifiManager.NETWORK_STATE_CHANGED_ACTION);
- setupSwitchBar();
+ setupSwitchController();
}
- public void setupSwitchBar() {
+ public void setupSwitchController() {
final int state = mWifiManager.getWifiState();
handleWifiStateChanged(state);
if (!mListeningToOnSwitchChange) {
- mSwitchBar.addOnSwitchChangeListener(this);
+ mSwitchWidget.startListening();
mListeningToOnSwitchChange = true;
}
- mSwitchBar.show();
+ mSwitchWidget.setupView();
}
- public void teardownSwitchBar() {
+ public void teardownSwitchController() {
if (mListeningToOnSwitchChange) {
- mSwitchBar.removeOnSwitchChangeListener(this);
+ mSwitchWidget.stopListening();
mListeningToOnSwitchChange = false;
}
- mSwitchBar.hide();
+ mSwitchWidget.teardownView();
}
public void resume(Context context) {
@@ -131,7 +131,7 @@
// Wi-Fi state is sticky, so just let the receiver update UI
mContext.registerReceiver(mReceiver, mIntentFilter);
if (!mListeningToOnSwitchChange) {
- mSwitchBar.addOnSwitchChangeListener(this);
+ mSwitchWidget.startListening();
mListeningToOnSwitchChange = true;
}
}
@@ -139,45 +139,45 @@
public void pause() {
mContext.unregisterReceiver(mReceiver);
if (mListeningToOnSwitchChange) {
- mSwitchBar.removeOnSwitchChangeListener(this);
+ mSwitchWidget.stopListening();
mListeningToOnSwitchChange = false;
}
}
private void handleWifiStateChanged(int state) {
// Clear any previous state
- mSwitchBar.setDisabledByAdmin(null);
+ mSwitchWidget.setDisabledByAdmin(null);
switch (state) {
case WifiManager.WIFI_STATE_ENABLING:
- mSwitchBar.setEnabled(false);
+ mSwitchWidget.setEnabled(false);
break;
case WifiManager.WIFI_STATE_ENABLED:
setSwitchBarChecked(true);
- mSwitchBar.setEnabled(true);
+ mSwitchWidget.setEnabled(true);
updateSearchIndex(true);
break;
case WifiManager.WIFI_STATE_DISABLING:
- mSwitchBar.setEnabled(false);
+ mSwitchWidget.setEnabled(false);
break;
case WifiManager.WIFI_STATE_DISABLED:
setSwitchBarChecked(false);
- mSwitchBar.setEnabled(true);
+ mSwitchWidget.setEnabled(true);
updateSearchIndex(false);
break;
default:
setSwitchBarChecked(false);
- mSwitchBar.setEnabled(true);
+ mSwitchWidget.setEnabled(true);
updateSearchIndex(false);
}
- if (mayDisableTethering(!mSwitchBar.isChecked())) {
+ if (mayDisableTethering(!mSwitchWidget.isChecked())) {
if (RestrictedLockUtils.hasBaseUserRestriction(mContext,
UserManager.DISALLOW_CONFIG_TETHERING, UserHandle.myUserId())) {
- mSwitchBar.setEnabled(false);
+ mSwitchWidget.setEnabled(false);
} else {
final EnforcedAdmin admin = RestrictedLockUtils.checkIfRestrictionEnforced(mContext,
UserManager.DISALLOW_CONFIG_TETHERING, UserHandle.myUserId());
- mSwitchBar.setDisabledByAdmin(admin);
+ mSwitchWidget.setDisabledByAdmin(admin);
}
}
}
@@ -193,7 +193,7 @@
private void setSwitchBarChecked(boolean checked) {
mStateMachineEvent = true;
- mSwitchBar.setChecked(checked);
+ mSwitchWidget.setChecked(checked);
mStateMachineEvent = false;
}
@@ -214,17 +214,17 @@
}
@Override
- public void onSwitchChanged(Switch switchView, boolean isChecked) {
+ public boolean onSwitchToggled(boolean isChecked) {
//Do nothing if called as a result of a state machine event
if (mStateMachineEvent) {
- return;
+ return true;
}
// Show toast message if Wi-Fi is not allowed in airplane mode
if (isChecked && !WirelessUtils.isRadioAllowed(mContext, Settings.Global.RADIO_WIFI)) {
Toast.makeText(mContext, R.string.wifi_in_airplane_mode, Toast.LENGTH_SHORT).show();
// Reset switch to off. No infinite check/listenenr loop.
- mSwitchBar.setChecked(false);
- return;
+ mSwitchWidget.setChecked(false);
+ return false;
}
// Disable tethering if enabling Wifi
@@ -240,9 +240,10 @@
}
if (!mWifiManager.setWifiEnabled(isChecked)) {
// Error
- mSwitchBar.setEnabled(true);
+ mSwitchWidget.setEnabled(true);
Toast.makeText(mContext, R.string.wifi_error, Toast.LENGTH_SHORT).show();
}
+ return true;
}
private boolean mayDisableTethering(boolean isChecked) {
diff --git a/src/com/android/settings/wifi/WifiMasterSwitchPreferenceController.java b/src/com/android/settings/wifi/WifiMasterSwitchPreferenceController.java
new file mode 100644
index 0000000..d72bb3e
--- /dev/null
+++ b/src/com/android/settings/wifi/WifiMasterSwitchPreferenceController.java
@@ -0,0 +1,102 @@
+/*
+ * 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.wifi;
+
+import android.content.Context;
+import android.support.v7.preference.PreferenceScreen;
+
+import com.android.settings.core.PreferenceController;
+import com.android.settings.core.instrumentation.MetricsFeatureProvider;
+import com.android.settings.core.lifecycle.LifecycleObserver;
+import com.android.settings.core.lifecycle.events.OnPause;
+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.widget.SummaryUpdater;
+import com.android.settings.widget.MasterSwitchPreference;
+import com.android.settings.widget.MasterSwitchController;
+
+public class WifiMasterSwitchPreferenceController extends PreferenceController
+ implements SummaryUpdater.OnSummaryChangeListener,
+ LifecycleObserver, OnResume, OnPause, OnStart, OnStop {
+
+ private static final String KEY_TOGGLE_WIFI = "toggle_wifi";
+
+ private MasterSwitchPreference mWifiPreference;
+ private WifiEnabler mWifiEnabler;
+ private final WifiSummaryUpdater mSummaryHelper;
+ private final MetricsFeatureProvider mMetricsFeatureProvider;
+
+ public WifiMasterSwitchPreferenceController(Context context,
+ MetricsFeatureProvider metricsFeatureProvider) {
+ super(context);
+ mMetricsFeatureProvider = metricsFeatureProvider;
+ mSummaryHelper = new WifiSummaryUpdater(mContext, this);
+ }
+
+ @Override
+ public void displayPreference(PreferenceScreen screen) {
+ super.displayPreference(screen);
+ mWifiPreference = (MasterSwitchPreference) screen.findPreference(KEY_TOGGLE_WIFI);
+ }
+
+ @Override
+ public boolean isAvailable() {
+ return true;
+ }
+
+ @Override
+ public String getPreferenceKey() {
+ return KEY_TOGGLE_WIFI;
+ }
+
+ @Override
+ public void onResume() {
+ mSummaryHelper.register(true);
+ if (mWifiEnabler != null) {
+ mWifiEnabler.resume(mContext);
+ }
+ }
+
+ @Override
+ public void onPause() {
+ if (mWifiEnabler != null) {
+ mWifiEnabler.pause();
+ }
+ mSummaryHelper.register(false);
+ }
+
+ @Override
+ public void onStart() {
+ mWifiEnabler = new WifiEnabler(mContext, new MasterSwitchController(mWifiPreference),
+ mMetricsFeatureProvider);
+ }
+
+ @Override
+ public void onStop() {
+ if (mWifiEnabler != null) {
+ mWifiEnabler.teardownSwitchController();
+ }
+ }
+
+ @Override
+ public void onSummaryChanged(String summary) {
+ if (mWifiPreference != null) {
+ mWifiPreference.setSummary(summary);
+ }
+ }
+
+}
diff --git a/src/com/android/settings/wifi/WifiSettings.java b/src/com/android/settings/wifi/WifiSettings.java
index 4fd93b5..fd31761 100644
--- a/src/com/android/settings/wifi/WifiSettings.java
+++ b/src/com/android/settings/wifi/WifiSettings.java
@@ -19,12 +19,10 @@
import android.app.Activity;
import android.app.Dialog;
import android.app.admin.DevicePolicyManager;
-import android.content.BroadcastReceiver;
import android.content.ComponentName;
import android.content.ContentResolver;
import android.content.Context;
import android.content.Intent;
-import android.content.IntentFilter;
import android.content.pm.PackageManager;
import android.content.pm.PackageManager.NameNotFoundException;
import android.content.res.Resources;
@@ -39,6 +37,7 @@
import android.os.HandlerThread;
import android.os.Process;
import android.provider.Settings;
+import android.support.annotation.VisibleForTesting;
import android.support.v7.preference.Preference;
import android.support.v7.preference.PreferenceCategory;
import android.support.v7.preference.PreferenceManager;
@@ -64,11 +63,12 @@
import com.android.settings.search.BaseSearchIndexProvider;
import com.android.settings.search.Indexable;
import com.android.settings.search.SearchIndexableRaw;
+import com.android.settings.widget.SummaryUpdater.OnSummaryChangeListener;
+import com.android.settings.widget.SwitchBarController;
import com.android.settingslib.RestrictedLockUtils;
import com.android.settingslib.wifi.AccessPoint;
import com.android.settingslib.wifi.AccessPoint.AccessPointListener;
import com.android.settingslib.wifi.AccessPointPreference;
-import com.android.settingslib.wifi.WifiStatusTracker;
import com.android.settingslib.wifi.WifiTracker;
import java.util.ArrayList;
@@ -303,7 +303,7 @@
super.onDestroyView();
if (mWifiEnabler != null) {
- mWifiEnabler.teardownSwitchBar();
+ mWifiEnabler.teardownSwitchController();
}
}
@@ -320,7 +320,8 @@
*/
private WifiEnabler createWifiEnabler() {
final SettingsActivity activity = (SettingsActivity) getActivity();
- return new WifiEnabler(activity, activity.getSwitchBar(), mMetricsFeatureProvider);
+ return new WifiEnabler(activity, new SwitchBarController(activity.getSwitchBar()),
+ mMetricsFeatureProvider);
}
@Override
@@ -1002,46 +1003,30 @@
return !isLockdownFeatureEnabled;
}
- private static class SummaryProvider extends BroadcastReceiver
- implements SummaryLoader.SummaryProvider {
+ private static class SummaryProvider
+ implements SummaryLoader.SummaryProvider, OnSummaryChangeListener {
private final Context mContext;
- private final WifiManager mWifiManager;
- private final WifiStatusTracker mWifiTracker;
private final SummaryLoader mSummaryLoader;
+ @VisibleForTesting
+ WifiSummaryUpdater mSummaryHelper;
+
public SummaryProvider(Context context, SummaryLoader summaryLoader) {
mContext = context;
mSummaryLoader = summaryLoader;
- mWifiManager = context.getSystemService(WifiManager.class);
- mWifiTracker = new WifiStatusTracker(mWifiManager);
+ mSummaryHelper = new WifiSummaryUpdater(mContext, this);
}
- private CharSequence getSummary() {
- if (!mWifiTracker.enabled) {
- return mContext.getString(R.string.wifi_disabled_generic);
- }
- if (!mWifiTracker.connected) {
- return mContext.getString(R.string.disconnected);
- }
- return mWifiTracker.ssid;
- }
@Override
public void setListening(boolean listening) {
- if (listening) {
- IntentFilter filter = new IntentFilter();
- filter.addAction(WifiManager.WIFI_STATE_CHANGED_ACTION);
- filter.addAction(WifiManager.NETWORK_STATE_CHANGED_ACTION);
- filter.addAction(WifiManager.RSSI_CHANGED_ACTION);
- mSummaryLoader.registerReceiver(this, filter);
- }
+ mSummaryHelper.register(listening);
}
@Override
- public void onReceive(Context context, Intent intent) {
- mWifiTracker.handleBroadcast(intent);
- mSummaryLoader.setSummary(this, getSummary());
+ public void onSummaryChanged(String summary) {
+ mSummaryLoader.setSummary(this, summary);
}
}
diff --git a/src/com/android/settings/wifi/WifiSummaryUpdater.java b/src/com/android/settings/wifi/WifiSummaryUpdater.java
new file mode 100644
index 0000000..533ee07
--- /dev/null
+++ b/src/com/android/settings/wifi/WifiSummaryUpdater.java
@@ -0,0 +1,84 @@
+/*
+ * 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.wifi;
+
+import android.content.BroadcastReceiver;
+import android.content.Context;
+import android.content.Intent;
+import android.content.IntentFilter;
+import android.net.wifi.WifiManager;
+import android.support.annotation.VisibleForTesting;
+import com.android.settings.R;
+import com.android.settings.widget.SummaryUpdater;
+import com.android.settingslib.wifi.WifiStatusTracker;
+
+/**
+ * Helper class that listeners to wifi callback and notify client when there is update in
+ * wifi summary info.
+ */
+public final class WifiSummaryUpdater extends SummaryUpdater {
+
+ private final WifiStatusTracker mWifiTracker;
+ private final BroadcastReceiver mReceiver;
+
+ private static final IntentFilter INTENT_FILTER;
+ static {
+ INTENT_FILTER = new IntentFilter();
+ INTENT_FILTER.addAction(WifiManager.WIFI_STATE_CHANGED_ACTION);
+ INTENT_FILTER.addAction(WifiManager.NETWORK_STATE_CHANGED_ACTION);
+ INTENT_FILTER.addAction(WifiManager.RSSI_CHANGED_ACTION);
+ }
+
+ public WifiSummaryUpdater(Context context, OnSummaryChangeListener listener) {
+ this(context, listener, new WifiStatusTracker(context.getSystemService(WifiManager.class)));
+ }
+
+ @VisibleForTesting
+ public WifiSummaryUpdater(Context context, OnSummaryChangeListener listener,
+ WifiStatusTracker wifiTracker) {
+ super(context, listener);
+ mWifiTracker = wifiTracker;
+ mReceiver = new BroadcastReceiver() {
+ @Override
+ public void onReceive(Context context, Intent intent) {
+ mWifiTracker.handleBroadcast(intent);
+ notifyChangeIfNeeded();
+ }
+ };
+ }
+
+ @Override
+ public void register(boolean register) {
+ if (register) {
+ mContext.registerReceiver(mReceiver, INTENT_FILTER);
+ } else {
+ mContext.unregisterReceiver(mReceiver);
+ }
+ }
+
+ @Override
+ public String getSummary() {
+ if (!mWifiTracker.enabled) {
+ return mContext.getString(R.string.wifi_disabled_generic);
+ }
+ if (!mWifiTracker.connected) {
+ return mContext.getString(R.string.disconnected);
+ }
+ return mWifiTracker.ssid;
+ }
+
+}
diff --git a/tests/robotests/src/com/android/settings/bluetooth/BluetoothSettingsSummaryProviderTest.java b/tests/robotests/src/com/android/settings/bluetooth/BluetoothSettingsSummaryProviderTest.java
index 2822b1e..8272dbf 100644
--- a/tests/robotests/src/com/android/settings/bluetooth/BluetoothSettingsSummaryProviderTest.java
+++ b/tests/robotests/src/com/android/settings/bluetooth/BluetoothSettingsSummaryProviderTest.java
@@ -63,7 +63,7 @@
mSummaryProvider.setListening(true);
verify(mBluetoothManager.getEventManager()).registerCallback(
- mSummaryProvider.mSummaryHelper);
+ mSummaryProvider.mSummaryUpdater);
}
@Test
@@ -71,7 +71,7 @@
mSummaryProvider.setListening(false);
verify(mBluetoothManager.getEventManager()).unregisterCallback(
- mSummaryProvider.mSummaryHelper);
+ mSummaryProvider.mSummaryUpdater);
}
@Test
diff --git a/tests/robotests/src/com/android/settings/bluetooth/BluetoothSummaryHelperTest.java b/tests/robotests/src/com/android/settings/bluetooth/BluetoothSummaryUpdaterTest.java
similarity index 74%
rename from tests/robotests/src/com/android/settings/bluetooth/BluetoothSummaryHelperTest.java
rename to tests/robotests/src/com/android/settings/bluetooth/BluetoothSummaryUpdaterTest.java
index f25e6d9..a2770a0 100644
--- a/tests/robotests/src/com/android/settings/bluetooth/BluetoothSummaryHelperTest.java
+++ b/tests/robotests/src/com/android/settings/bluetooth/BluetoothSummaryUpdaterTest.java
@@ -22,6 +22,7 @@
import com.android.settings.R;
import com.android.settings.SettingsRobolectricTestRunner;
import com.android.settings.TestConfig;
+import com.android.settings.widget.SummaryUpdater.OnSummaryChangeListener;
import com.android.settingslib.bluetooth.CachedBluetoothDevice;
import com.android.settingslib.bluetooth.LocalBluetoothManager;
import com.android.settingslib.bluetooth.LocalBluetoothAdapter;
@@ -45,7 +46,7 @@
@RunWith(SettingsRobolectricTestRunner.class)
@Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION)
-public class BluetoothSummaryHelperTest {
+public class BluetoothSummaryUpdaterTest {
private Context mContext;
@Mock(answer = Answers.RETURNS_DEEP_STUBS)
@@ -53,7 +54,7 @@
@Mock
private LocalBluetoothAdapter mBtAdapter;
- private BluetoothSummaryHelper mHelper;
+ private BluetoothSummaryUpdater mSummaryUpdater;
@Mock
private SummaryListener mListener;
@@ -64,43 +65,42 @@
when(mBtAdapter.isEnabled()).thenReturn(true);
when(mBtAdapter.getConnectionState()).thenReturn(BluetoothAdapter.STATE_CONNECTED);
mContext = RuntimeEnvironment.application.getApplicationContext();
- mHelper = new BluetoothSummaryHelper(mContext, mBluetoothManager);
- mHelper.setOnSummaryChangeListener(mListener);
+ mSummaryUpdater = new BluetoothSummaryUpdater(mContext, mListener, mBluetoothManager);
}
@Test
- public void setListening_shouldRegisterListener() {
- mHelper.setListening(true);
+ public void register_true_shouldRegisterListener() {
+ mSummaryUpdater.register(true);
- verify(mBluetoothManager.getEventManager()).registerCallback(mHelper);
+ verify(mBluetoothManager.getEventManager()).registerCallback(mSummaryUpdater);
}
@Test
- public void setNotListening_shouldUnregisterListener() {
- mHelper.setListening(false);
+ public void register_false_shouldUnregisterListener() {
+ mSummaryUpdater.register(false);
- verify(mBluetoothManager.getEventManager()).unregisterCallback(mHelper);
+ verify(mBluetoothManager.getEventManager()).unregisterCallback(mSummaryUpdater);
}
@Test
- public void setListening_shouldSendSummaryChange() {
- mHelper.setListening(true);
+ public void register_true_shouldSendSummaryChange() {
+ mSummaryUpdater.register(true);
verify(mListener).onSummaryChanged(mContext.getString(R.string.bluetooth_connected));
}
@Test
public void onBluetoothStateChanged_btDisabled_shouldSendDisabledSummary() {
- mHelper.setListening(true);
- mHelper.onBluetoothStateChanged(BluetoothAdapter.STATE_OFF);
+ mSummaryUpdater.register(true);
+ mSummaryUpdater.onBluetoothStateChanged(BluetoothAdapter.STATE_OFF);
verify(mListener).onSummaryChanged(mContext.getString(R.string.bluetooth_disabled));
}
@Test
public void onBluetoothStateChanged_btEnabled_connected_shouldSendConnectedSummary() {
- mHelper.setListening(true);
- mHelper.onBluetoothStateChanged(BluetoothAdapter.STATE_ON);
+ mSummaryUpdater.register(true);
+ mSummaryUpdater.onBluetoothStateChanged(BluetoothAdapter.STATE_ON);
verify(mListener).onSummaryChanged(mContext.getString(R.string.bluetooth_connected));
}
@@ -108,8 +108,8 @@
@Test
public void onBluetoothStateChanged_btEnabled_notConnected_shouldSendDisconnectedMessage() {
when(mBtAdapter.getConnectionState()).thenReturn(BluetoothAdapter.STATE_DISCONNECTED);
- mHelper.setListening(true);
- mHelper.onBluetoothStateChanged(BluetoothAdapter.STATE_TURNING_ON);
+ mSummaryUpdater.register(true);
+ mSummaryUpdater.onBluetoothStateChanged(BluetoothAdapter.STATE_TURNING_ON);
verify(mListener).onSummaryChanged(mContext.getString(R.string.bluetooth_disconnected));
}
@@ -122,41 +122,45 @@
when(mBluetoothManager.getCachedDeviceManager().getCachedDevicesCopy())
.thenReturn(devices);
when(mBtAdapter.getConnectionState()).thenReturn(BluetoothAdapter.STATE_DISCONNECTED);
- mHelper.setListening(true);
+ mSummaryUpdater.register(true);
when(mBtAdapter.getConnectionState()).thenReturn(BluetoothAdapter.STATE_CONNECTED);
- mHelper.onConnectionStateChanged(null /* device */, BluetoothAdapter.STATE_CONNECTED);
+ mSummaryUpdater.onConnectionStateChanged(null /* device */,
+ BluetoothAdapter.STATE_CONNECTED);
verify(mListener).onSummaryChanged(mContext.getString(R.string.bluetooth_connected));
}
@Test
public void onConnectionStateChanged_inconsistentState_shouldSendDisconnectedMessage() {
- mHelper.setListening(true);
- mHelper.onConnectionStateChanged(null /* device */, BluetoothAdapter.STATE_CONNECTED);
+ mSummaryUpdater.register(true);
+ mSummaryUpdater.onConnectionStateChanged(null /* device */,
+ BluetoothAdapter.STATE_CONNECTED);
verify(mListener).onSummaryChanged(mContext.getString(R.string.bluetooth_disconnected));
}
@Test
public void onConnectionStateChanged_connecting_shouldSendConnectingMessage() {
- mHelper.setListening(true);
+ mSummaryUpdater.register(true);
when(mBtAdapter.getConnectionState()).thenReturn(BluetoothAdapter.STATE_CONNECTING);
- mHelper.onConnectionStateChanged(null /* device */, BluetoothAdapter.STATE_CONNECTING);
+ mSummaryUpdater.onConnectionStateChanged(null /* device */,
+ BluetoothAdapter.STATE_CONNECTING);
verify(mListener).onSummaryChanged(mContext.getString(R.string.bluetooth_connecting));
}
@Test
public void onConnectionStateChanged_disconnecting_shouldSendDisconnectingMessage() {
- mHelper.setListening(true);
+ mSummaryUpdater.register(true);
when(mBtAdapter.getConnectionState()).thenReturn(BluetoothAdapter.STATE_DISCONNECTING);
- mHelper.onConnectionStateChanged(null /* device */, BluetoothAdapter.STATE_DISCONNECTING);
+ mSummaryUpdater.onConnectionStateChanged(null /* device */,
+ BluetoothAdapter.STATE_DISCONNECTING);
verify(mListener).onSummaryChanged(mContext.getString(R.string.bluetooth_disconnecting));
}
- private class SummaryListener implements BluetoothSummaryHelper.OnSummaryChangeListener {
+ private class SummaryListener implements OnSummaryChangeListener {
String summary;
@Override
diff --git a/tests/robotests/src/com/android/settings/widget/MasterSwitchPreferenceTest.java b/tests/robotests/src/com/android/settings/widget/MasterSwitchPreferenceTest.java
index 1dce599..8cf3389 100644
--- a/tests/robotests/src/com/android/settings/widget/MasterSwitchPreferenceTest.java
+++ b/tests/robotests/src/com/android/settings/widget/MasterSwitchPreferenceTest.java
@@ -27,6 +27,7 @@
import com.android.settings.R;
import com.android.settings.SettingsRobolectricTestRunner;
import com.android.settings.TestConfig;
+import com.android.settingslib.RestrictedLockUtils.EnforcedAdmin;
import org.junit.Before;
import org.junit.Test;
@@ -118,4 +119,30 @@
toggle.setChecked(false);
verify(listener).onPreferenceChange(preference, false);
}
+
+ @Test
+ public void setDisabledByAdmin_hasEnforcedAdmin_shouldDisableButton() {
+ final MasterSwitchPreference preference = new MasterSwitchPreference(mContext);
+ final PreferenceViewHolder holder = new PreferenceViewHolder(
+ LayoutInflater.from(mContext).inflate(R.layout.preference_widget_master_switch, null));
+ final Switch toggle = (Switch) holder.itemView.findViewById(R.id.switchWidget);
+ toggle.setEnabled(true);
+ preference.onBindViewHolder(holder);
+
+ preference.setDisabledByAdmin(mock(EnforcedAdmin.class));
+ assertThat(toggle.isEnabled()).isFalse();
+ }
+
+ @Test
+ public void setDisabledByAdmin_noEnforcedAdmin_shouldEnaableButton() {
+ final MasterSwitchPreference preference = new MasterSwitchPreference(mContext);
+ final PreferenceViewHolder holder = new PreferenceViewHolder(
+ LayoutInflater.from(mContext).inflate(R.layout.preference_widget_master_switch, null));
+ final Switch toggle = (Switch) holder.itemView.findViewById(R.id.switchWidget);
+ toggle.setEnabled(false);
+ preference.onBindViewHolder(holder);
+
+ preference.setDisabledByAdmin(null);
+ assertThat(toggle.isEnabled()).isTrue();
+ }
}
diff --git a/tests/robotests/src/com/android/settings/widget/SummaryUpdaterTest.java b/tests/robotests/src/com/android/settings/widget/SummaryUpdaterTest.java
new file mode 100644
index 0000000..e57d25a
--- /dev/null
+++ b/tests/robotests/src/com/android/settings/widget/SummaryUpdaterTest.java
@@ -0,0 +1,126 @@
+/*
+ * 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 com.android.settings.R;
+import com.android.settings.SettingsRobolectricTestRunner;
+import com.android.settings.TestConfig;
+
+import org.junit.Before;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.mockito.Answers;
+import org.mockito.Mock;
+import org.mockito.MockitoAnnotations;
+import org.robolectric.RuntimeEnvironment;
+import org.robolectric.annotation.Config;
+
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.times;
+import static org.mockito.Mockito.verify;
+import static org.mockito.Mockito.when;
+
+@RunWith(SettingsRobolectricTestRunner.class)
+@Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION)
+public class SummaryUpdaterTest {
+
+ private Context mContext;
+
+ private SummaryUpdaterTestable mSummaryUpdater;
+ @Mock
+ private SummaryListener mListener;
+
+ @Before
+ public void setUp() {
+ MockitoAnnotations.initMocks(this);
+ mContext = RuntimeEnvironment.application.getApplicationContext();
+ mSummaryUpdater = new SummaryUpdaterTestable(mContext, mListener);
+ }
+
+ @Test
+ public void notifyChangeIfNeeded_fistTimeInit_shouldNotifyChange() {
+ final String summary = "initialized";
+ mSummaryUpdater.setTestSummary(summary);
+ mSummaryUpdater.notifyChangeIfNeeded();
+
+ verify(mListener).onSummaryChanged(summary);
+ }
+
+ @Test
+ public void notifyChangeIfNeeded_summaryUpdated_shouldNotifyChange() {
+ final String summaryInit = "initialized";
+ mSummaryUpdater.setTestSummary(summaryInit);
+ mSummaryUpdater.notifyChangeIfNeeded();
+ final String summaryUpdate = "updated";
+
+ mSummaryUpdater.setTestSummary(summaryUpdate);
+ mSummaryUpdater.notifyChangeIfNeeded();
+
+ verify(mListener).onSummaryChanged(summaryUpdate);
+ }
+
+ @Test
+ public void notifyChangeIfNeeded_summaryNotUpdated_shouldOnlyNotifyChangeOnce() {
+ final String summaryInit = "initialized";
+ mSummaryUpdater.setTestSummary(summaryInit);
+ mSummaryUpdater.notifyChangeIfNeeded();
+ final String summaryUpdate = "updated";
+ mSummaryUpdater.setTestSummary(summaryUpdate);
+
+ mSummaryUpdater.notifyChangeIfNeeded();
+ mSummaryUpdater.notifyChangeIfNeeded();
+
+ verify(mListener, times(1)).onSummaryChanged(summaryUpdate);
+ }
+
+ private class SummaryListener implements SummaryUpdater.OnSummaryChangeListener {
+ String summary;
+
+ @Override
+ public void onSummaryChanged(String summary) {
+ this.summary = summary;
+ }
+ }
+
+ public final class SummaryUpdaterTestable extends SummaryUpdater {
+ private String mTestSummary;
+
+ SummaryUpdaterTestable(Context context, SummaryUpdater.OnSummaryChangeListener listener) {
+ super(context, listener);
+ }
+
+ @Override
+ public void register(boolean register) {
+ }
+
+ @Override
+ public void notifyChangeIfNeeded() {
+ super.notifyChangeIfNeeded();
+ }
+
+ @Override
+ public String getSummary() {
+ return mTestSummary;
+ }
+
+ public void setTestSummary(String summary) {
+ mTestSummary = summary;
+ }
+ }
+}
diff --git a/tests/robotests/src/com/android/settings/wifi/WifiMasterSwitchPreferenceControllerTest.java b/tests/robotests/src/com/android/settings/wifi/WifiMasterSwitchPreferenceControllerTest.java
new file mode 100644
index 0000000..694fe0e
--- /dev/null
+++ b/tests/robotests/src/com/android/settings/wifi/WifiMasterSwitchPreferenceControllerTest.java
@@ -0,0 +1,127 @@
+/*
+ * 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.wifi;
+
+import android.content.BroadcastReceiver;
+import android.content.Context;
+import android.content.IntentFilter;
+import android.net.wifi.WifiManager;
+import android.support.v7.preference.Preference;
+import android.support.v7.preference.Preference.OnPreferenceChangeListener;
+import android.support.v7.preference.PreferenceScreen;
+
+import com.android.settings.R;
+import com.android.settings.SettingsRobolectricTestRunner;
+import com.android.settings.TestConfig;
+import com.android.settings.core.instrumentation.MetricsFeatureProvider;
+import com.android.settings.testutils.FakeFeatureFactory;
+import com.android.settings.widget.MasterSwitchPreference;
+
+import org.junit.Before;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.mockito.Answers;
+import org.mockito.Mock;
+import org.mockito.MockitoAnnotations;
+import org.robolectric.RuntimeEnvironment;
+import org.robolectric.annotation.Config;
+
+import static com.google.common.truth.Truth.assertThat;
+import static org.mockito.Matchers.any;
+import static org.mockito.Mockito.spy;
+import static org.mockito.Mockito.times;
+import static org.mockito.Mockito.verify;
+import static org.mockito.Mockito.when;
+
+@RunWith(SettingsRobolectricTestRunner.class)
+@Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION)
+public class WifiMasterSwitchPreferenceControllerTest {
+
+ @Mock(answer = Answers.RETURNS_DEEP_STUBS)
+ Context mMockContext;
+ @Mock
+ private WifiManager mWifiManager;
+ @Mock
+ private PreferenceScreen mScreen;
+ @Mock
+ private MasterSwitchPreference mPreference;
+
+ private Context mContext;
+ private WifiMasterSwitchPreferenceController mController;
+ private FakeFeatureFactory mFakeFeatureFactory;
+ private MetricsFeatureProvider mMetricsFeatureProvider;
+
+ @Before
+ public void setUp() {
+ MockitoAnnotations.initMocks(this);
+ FakeFeatureFactory.setupForTest(mMockContext);
+ mFakeFeatureFactory = (FakeFeatureFactory) FakeFeatureFactory.getFactory(mMockContext);
+ mMetricsFeatureProvider = mFakeFeatureFactory.getMetricsFeatureProvider();
+ mContext = spy(RuntimeEnvironment.application.getApplicationContext());
+ mController = new WifiMasterSwitchPreferenceController(mContext, mMetricsFeatureProvider);
+ when(mScreen.findPreference(mController.getPreferenceKey())).thenReturn(mPreference);
+ when(mContext.getSystemService(Context.WIFI_SERVICE)).thenReturn(mWifiManager);
+ when(mWifiManager.getWifiState()).thenReturn(WifiManager.WIFI_STATE_DISABLED);
+ }
+
+ @Test
+ public void isAvailable_shouldAlwaysReturnTrue() {
+ assertThat(mController.isAvailable()).isTrue();
+ }
+
+ @Test
+ public void onResume_shouldRegisterCallback() {
+ mController.onResume();
+
+ verify(mContext).registerReceiver(any(BroadcastReceiver.class), any(IntentFilter.class));
+ }
+
+ @Test
+ public void onPause_shouldUnregisterCallback() {
+ mController.onResume();
+ mController.onPause();
+
+ verify(mContext).unregisterReceiver(any(BroadcastReceiver.class));
+ }
+
+ @Test
+ public void onStart_shouldRegisterPreferenceChangeListener() {
+ mController.displayPreference(mScreen);
+ mController.onStart();
+
+ verify(mPreference).setOnPreferenceChangeListener(any(OnPreferenceChangeListener.class));
+ }
+
+ @Test
+ public void onStop_shouldRegisterPreferenceChangeListener() {
+ mController.displayPreference(mScreen);
+ mController.onStart();
+
+ mController.onStop();
+
+ verify(mPreference).setOnPreferenceChangeListener(null);
+ }
+
+ @Test
+ public void onSummaryChanged_shouldUpdatePreferenceSummary() {
+ mController.displayPreference(mScreen);
+
+ mController.onSummaryChanged("test summary");
+
+ verify(mPreference).setSummary("test summary");
+ }
+}
diff --git a/tests/robotests/src/com/android/settings/wifi/WifiSummaryUpdaterTest.java b/tests/robotests/src/com/android/settings/wifi/WifiSummaryUpdaterTest.java
new file mode 100644
index 0000000..df290b3
--- /dev/null
+++ b/tests/robotests/src/com/android/settings/wifi/WifiSummaryUpdaterTest.java
@@ -0,0 +1,136 @@
+/*
+ * 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.wifi;
+
+import android.content.BroadcastReceiver;
+import android.content.Context;
+import android.content.Intent;
+import android.content.IntentFilter;
+import android.net.wifi.WifiManager;
+
+import com.android.settings.R;
+import com.android.settings.SettingsRobolectricTestRunner;
+import com.android.settings.TestConfig;
+import com.android.settings.widget.SummaryUpdater.OnSummaryChangeListener;
+import com.android.settingslib.wifi.WifiStatusTracker;
+
+import org.junit.Before;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.mockito.Answers;
+import org.mockito.Mock;
+import org.mockito.MockitoAnnotations;
+import org.robolectric.RuntimeEnvironment;
+import org.robolectric.annotation.Config;
+
+import static com.google.common.truth.Truth.assertThat;
+import static org.mockito.Matchers.any;
+import static org.mockito.Matchers.anyString;
+import static org.mockito.Mockito.spy;
+import static org.mockito.Mockito.verify;
+
+@RunWith(SettingsRobolectricTestRunner.class)
+@Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION)
+public class WifiSummaryUpdaterTest {
+
+ @Mock(answer = Answers.RETURNS_DEEP_STUBS)
+ private WifiManager mWifiManager;
+ @Mock
+ private SummaryListener mListener;
+
+ private Context mContext;
+ private WifiSummaryUpdater mSummaryUpdater;
+ private WifiStatusTracker mWifiTracker;
+
+ @Before
+ public void setUp() {
+ MockitoAnnotations.initMocks(this);
+ mWifiTracker = new WifiStatusTracker(mWifiManager);
+
+ mContext = spy(RuntimeEnvironment.application.getApplicationContext());
+ mSummaryUpdater = new WifiSummaryUpdater(mContext, mListener, mWifiTracker);
+ }
+
+ @Test
+ public void register_true_shouldRegisterListener() {
+ mSummaryUpdater.register(true);
+
+ verify(mContext).registerReceiver(any(BroadcastReceiver.class), any(IntentFilter.class));
+ }
+
+ @Test
+ public void register_false_shouldUnregisterListener() {
+ mSummaryUpdater.register(true);
+ mSummaryUpdater.register(false);
+
+ verify(mContext).unregisterReceiver(any(BroadcastReceiver.class));
+ }
+
+ @Test
+ public void onReceive_networkStateChanged_shouldSendSummaryChange() {
+ mSummaryUpdater.register(true);
+ mContext.sendBroadcast(new Intent(WifiManager.NETWORK_STATE_CHANGED_ACTION));
+
+
+ verify(mListener).onSummaryChanged(anyString());
+ }
+
+ @Test
+ public void onReceive_rssiChanged_shouldSendSummaryChange() {
+ mSummaryUpdater.register(true);
+ mContext.sendBroadcast(new Intent(WifiManager.RSSI_CHANGED_ACTION));
+
+
+ verify(mListener).onSummaryChanged(anyString());
+ }
+
+ @Test
+ public void getSummary_wifiDisabled_shouldReturnDisabled() {
+ mWifiTracker.enabled = false;
+
+ assertThat(mSummaryUpdater.getSummary()).isEqualTo(
+ mContext.getString(R.string.wifi_disabled_generic));
+ }
+
+ @Test
+ public void getSummary_wifiDisconnected_shouldReturnDisconnected() {
+ mWifiTracker.enabled = true;
+ mWifiTracker.connected = false;
+
+ assertThat(mSummaryUpdater.getSummary()).isEqualTo(
+ mContext.getString(R.string.disconnected));
+ }
+
+ @Test
+ public void getSummary_wifiConnected_shouldReturnSsid() {
+ mWifiTracker.enabled = true;
+ mWifiTracker.connected = true;
+ mWifiTracker.ssid = "Test Ssid";
+
+ assertThat(mSummaryUpdater.getSummary()).isEqualTo("Test Ssid");
+ }
+
+ private class SummaryListener implements OnSummaryChangeListener {
+ String summary;
+
+ @Override
+ public void onSummaryChanged(String summary) {
+ this.summary = summary;
+ }
+ }
+
+}