diff --git a/AndroidManifest.xml b/AndroidManifest.xml
index a68b2a77c0f..05e95f0e5e7 100644
--- a/AndroidManifest.xml
+++ b/AndroidManifest.xml
@@ -3044,20 +3044,6 @@
android:value="com.android.settings.network.NetworkDashboardFragment"/>
-
-
-
-
-
-
-
-
-
diff --git a/res/xml/network_and_internet.xml b/res/xml/network_and_internet.xml
index dc2efd8f890..7c414981334 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">
+
+
+
+
+ android:order="5"/>
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 7e6de9608e5..2d914148ddb 100644
--- a/src/com/android/settings/network/NetworkDashboardFragment.java
+++ b/src/com/android/settings/network/NetworkDashboardFragment.java
@@ -24,9 +24,11 @@ import android.util.Log;
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 @@ public class NetworkDashboardFragment extends DashboardFragment implements
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 controllers = new ArrayList<>();
controllers.add(airplaneModePreferenceController);
@@ -73,6 +79,7 @@ public class NetworkDashboardFragment extends DashboardFragment implements
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 53e5fe7817c..f7253fd5e7a 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 @@ public class MasterSwitchController extends SwitchWidgetController implements
}
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 8130ca5ad64..4650730fba6 100644
--- a/src/com/android/settings/widget/MasterSwitchPreference.java
+++ b/src/com/android/settings/widget/MasterSwitchPreference.java
@@ -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);
}
diff --git a/src/com/android/settings/widget/SummaryUpdater.java b/src/com/android/settings/widget/SummaryUpdater.java
new file mode 100644
index 00000000000..89752b9087f
--- /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 70fd7ba75b6..624db2aa164 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 @@ 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();
+ }
+
}
diff --git a/src/com/android/settings/widget/SwitchWidgetController.java b/src/com/android/settings/widget/SwitchWidgetController.java
index c5a8c87c09f..325a093767f 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 @@ 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();
+
+}
\ 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 a05e4988e1f..1ac94ea25d9 100644
--- a/src/com/android/settings/wifi/WifiEnabler.java
+++ b/src/com/android/settings/wifi/WifiEnabler.java
@@ -29,23 +29,22 @@ import android.os.Message;
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 class WifiEnabler implements SwitchBar.OnSwitchChangeListener {
}
};
- 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 @@ public class WifiEnabler implements SwitchBar.OnSwitchChangeListener {
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 @@ public class WifiEnabler implements SwitchBar.OnSwitchChangeListener {
// 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 class WifiEnabler implements SwitchBar.OnSwitchChangeListener {
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 @@ public class WifiEnabler implements SwitchBar.OnSwitchChangeListener {
private void setSwitchBarChecked(boolean checked) {
mStateMachineEvent = true;
- mSwitchBar.setChecked(checked);
+ mSwitchWidget.setChecked(checked);
mStateMachineEvent = false;
}
@@ -214,17 +214,17 @@ public class WifiEnabler implements SwitchBar.OnSwitchChangeListener {
}
@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 @@ public class WifiEnabler implements SwitchBar.OnSwitchChangeListener {
}
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 00000000000..d72bb3e6f48
--- /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 4fd93b53129..fd317612f8a 100644
--- a/src/com/android/settings/wifi/WifiSettings.java
+++ b/src/com/android/settings/wifi/WifiSettings.java
@@ -19,12 +19,10 @@ package com.android.settings.wifi;
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.Bundle;
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.location.ScanningSettings;
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 @@ public class WifiSettings extends RestrictedSettingsFragment
super.onDestroyView();
if (mWifiEnabler != null) {
- mWifiEnabler.teardownSwitchBar();
+ mWifiEnabler.teardownSwitchController();
}
}
@@ -320,7 +320,8 @@ public class WifiSettings extends RestrictedSettingsFragment
*/
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 @@ public class WifiSettings extends RestrictedSettingsFragment
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 00000000000..533ee0750df
--- /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 2822b1efee6..8272dbf5f5f 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 @@ public class BluetoothSettingsSummaryProviderTest {
mSummaryProvider.setListening(true);
verify(mBluetoothManager.getEventManager()).registerCallback(
- mSummaryProvider.mSummaryHelper);
+ mSummaryProvider.mSummaryUpdater);
}
@Test
@@ -71,7 +71,7 @@ public class BluetoothSettingsSummaryProviderTest {
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 f25e6d98f08..a2770a0ecd8 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 android.content.Context;
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 @@ import static org.mockito.Mockito.when;
@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 @@ public class BluetoothSummaryHelperTest {
@Mock
private LocalBluetoothAdapter mBtAdapter;
- private BluetoothSummaryHelper mHelper;
+ private BluetoothSummaryUpdater mSummaryUpdater;
@Mock
private SummaryListener mListener;
@@ -64,43 +65,42 @@ public class BluetoothSummaryHelperTest {
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 @@ public class BluetoothSummaryHelperTest {
@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 @@ public class BluetoothSummaryHelperTest {
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 1dce599d311..8cf33899ec2 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 android.widget.Switch;
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 @@ public class MasterSwitchPreferenceTest {
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 00000000000..e57d25a91e8
--- /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 00000000000..694fe0eae31
--- /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 00000000000..df290b30c51
--- /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;
+ }
+ }
+
+}