Merge "Use SoftAp API to get number of connected device"

This commit is contained in:
TreeHugger Robot
2018-01-22 23:49:34 +00:00
committed by Android (Google) Code Review
3 changed files with 116 additions and 5 deletions

View File

@@ -24,6 +24,7 @@ import android.net.ConnectivityManager;
import android.net.wifi.WifiConfiguration; import android.net.wifi.WifiConfiguration;
import android.net.wifi.WifiManager; import android.net.wifi.WifiManager;
import android.provider.Settings; import android.provider.Settings;
import android.support.annotation.VisibleForTesting;
import android.support.v7.preference.PreferenceScreen; import android.support.v7.preference.PreferenceScreen;
import android.text.BidiFormatter; import android.text.BidiFormatter;
@@ -51,7 +52,11 @@ public class WifiTetherPreferenceController extends AbstractPreferenceController
private final WifiManager mWifiManager; private final WifiManager mWifiManager;
private final Lifecycle mLifecycle; private final Lifecycle mLifecycle;
private WifiTetherSwitchBarController mSwitchController; private WifiTetherSwitchBarController mSwitchController;
private MasterSwitchPreference mPreference; private int mSoftApState;
@VisibleForTesting
MasterSwitchPreference mPreference;
@VisibleForTesting
WifiTetherSoftApManager mWifiTetherSoftApManager;
static { static {
WIFI_TETHER_INTENT_FILTER = new IntentFilter(WifiManager.WIFI_AP_STATE_CHANGED_ACTION); WIFI_TETHER_INTENT_FILTER = new IntentFilter(WifiManager.WIFI_AP_STATE_CHANGED_ACTION);
@@ -60,6 +65,12 @@ public class WifiTetherPreferenceController extends AbstractPreferenceController
} }
public WifiTetherPreferenceController(Context context, Lifecycle lifecycle) { public WifiTetherPreferenceController(Context context, Lifecycle lifecycle) {
this(context, lifecycle, true /* initSoftApManager */);
}
@VisibleForTesting
WifiTetherPreferenceController(Context context, Lifecycle lifecycle,
boolean initSoftApManager) {
super(context); super(context);
mConnectivityManager = mConnectivityManager =
(ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE); (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
@@ -69,6 +80,9 @@ public class WifiTetherPreferenceController extends AbstractPreferenceController
if (lifecycle != null) { if (lifecycle != null) {
lifecycle.addObserver(this); lifecycle.addObserver(this);
} }
if (initSoftApManager) {
initWifiTetherSoftApManager();
}
} }
@Override @Override
@@ -101,6 +115,9 @@ public class WifiTetherPreferenceController extends AbstractPreferenceController
if (mPreference != null) { if (mPreference != null) {
mContext.registerReceiver(mReceiver, WIFI_TETHER_INTENT_FILTER); mContext.registerReceiver(mReceiver, WIFI_TETHER_INTENT_FILTER);
clearSummaryForAirplaneMode(); clearSummaryForAirplaneMode();
if (mWifiTetherSoftApManager != null) {
mWifiTetherSoftApManager.registerSoftApCallback();
}
} }
} }
@@ -108,9 +125,36 @@ public class WifiTetherPreferenceController extends AbstractPreferenceController
public void onStop() { public void onStop() {
if (mPreference != null) { if (mPreference != null) {
mContext.unregisterReceiver(mReceiver); mContext.unregisterReceiver(mReceiver);
if (mWifiTetherSoftApManager != null) {
mWifiTetherSoftApManager.unRegisterSoftApCallback();
}
} }
} }
@VisibleForTesting
void initWifiTetherSoftApManager() {
// This manager only handles the number of connected devices, other parts are handled by
// normal BroadcastReceiver in this controller
mWifiTetherSoftApManager = new WifiTetherSoftApManager(mWifiManager,
new WifiTetherSoftApManager.WifiTetherSoftApCallback() {
@Override
public void onStateChanged(int state, int failureReason) {
mSoftApState = state;
}
@Override
public void onNumClientsChanged(int numClients) {
if (mPreference != null
&& mSoftApState == WifiManager.WIFI_AP_STATE_ENABLED) {
// Only show the number of clients when state is on
mPreference.setSummary(mContext.getResources().getQuantityString(
R.plurals.wifi_tether_connected_summary, numClients,
numClients));
}
}
});
}
// //
// Everything below is copied from WifiApEnabler // Everything below is copied from WifiApEnabler
// //

View File

@@ -0,0 +1,47 @@
package com.android.settings.wifi.tether;
import android.net.wifi.WifiManager;
import android.os.Handler;
/**
* Wrapper for {@link android.net.wifi.WifiManager.SoftApCallback} to pass the robo test
*/
public class WifiTetherSoftApManager {
private WifiManager mWifiManager;
private WifiTetherSoftApCallback mWifiTetherSoftApCallback;
private WifiManager.SoftApCallback mSoftApCallback = new WifiManager.SoftApCallback() {
@Override
public void onStateChanged(int state, int failureReason) {
mWifiTetherSoftApCallback.onStateChanged(state, failureReason);
}
@Override
public void onNumClientsChanged(int numClients) {
mWifiTetherSoftApCallback.onNumClientsChanged(numClients);
}
};
private Handler mHandler;
WifiTetherSoftApManager(WifiManager wifiManager,
WifiTetherSoftApCallback wifiTetherSoftApCallback) {
mWifiManager = wifiManager;
mWifiTetherSoftApCallback = wifiTetherSoftApCallback;
mHandler = new Handler();
}
public void registerSoftApCallback() {
mWifiManager.registerSoftApCallback(mSoftApCallback, mHandler);
}
public void unRegisterSoftApCallback() {
mWifiManager.unregisterSoftApCallback(mSoftApCallback);
}
public interface WifiTetherSoftApCallback {
void onStateChanged(int state, int failureReason);
void onNumClientsChanged(int numClients);
}
}

View File

@@ -18,11 +18,15 @@ package com.android.settings.wifi.tether;
import static android.arch.lifecycle.Lifecycle.Event.ON_START; import static android.arch.lifecycle.Lifecycle.Event.ON_START;
import static android.arch.lifecycle.Lifecycle.Event.ON_STOP; import static android.arch.lifecycle.Lifecycle.Event.ON_STOP;
import static com.google.common.truth.Truth.assertThat; import static com.google.common.truth.Truth.assertThat;
import static org.mockito.Matchers.any; import static org.mockito.Matchers.any;
import static org.mockito.Matchers.anyString; import static org.mockito.Matchers.anyString;
import static org.mockito.Matchers.eq; import static org.mockito.Matchers.eq;
import static org.mockito.Mockito.doNothing;
import static org.mockito.Mockito.mock; import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.verify; import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when; import static org.mockito.Mockito.when;
@@ -65,6 +69,7 @@ import java.util.ArrayList;
shadows = { shadows = {
WifiTetherPreferenceControllerTest.ShadowWifiTetherSettings.class, WifiTetherPreferenceControllerTest.ShadowWifiTetherSettings.class,
WifiTetherPreferenceControllerTest.ShadowWifiTetherSwitchBarController.class, WifiTetherPreferenceControllerTest.ShadowWifiTetherSwitchBarController.class,
WifiTetherPreferenceControllerTest.ShadowWifiTetherSoftApManager.class
}) })
public class WifiTetherPreferenceControllerTest { public class WifiTetherPreferenceControllerTest {
@@ -94,8 +99,9 @@ public class WifiTetherPreferenceControllerTest {
when(mContext.getSystemService(Context.WIFI_SERVICE)).thenReturn(mWifiManager); when(mContext.getSystemService(Context.WIFI_SERVICE)).thenReturn(mWifiManager);
when(mScreen.findPreference(anyString())).thenReturn(mPreference); when(mScreen.findPreference(anyString())).thenReturn(mPreference);
when(mConnectivityManager.getTetherableWifiRegexs()).thenReturn(new String[] {"1", "2"}); when(mConnectivityManager.getTetherableWifiRegexs()).thenReturn(new String[]{"1", "2"});
mController = new WifiTetherPreferenceController(mContext, mLifecycle); mController = new WifiTetherPreferenceController(mContext, mLifecycle,
false /* initSoftApManager */);
} }
@After @After
@@ -105,8 +111,9 @@ public class WifiTetherPreferenceControllerTest {
@Test @Test
public void isAvailable_noTetherRegex_shouldReturnFalse() { public void isAvailable_noTetherRegex_shouldReturnFalse() {
when(mConnectivityManager.getTetherableWifiRegexs()).thenReturn(new String[] {}); when(mConnectivityManager.getTetherableWifiRegexs()).thenReturn(new String[]{});
mController = new WifiTetherPreferenceController(mContext, mLifecycle); mController = new WifiTetherPreferenceController(mContext, mLifecycle,
false /* initSoftApManager */);
assertThat(mController.isAvailable()).isFalse(); assertThat(mController.isAvailable()).isFalse();
} }
@@ -244,6 +251,19 @@ public class WifiTetherPreferenceControllerTest {
} }
} }
@Implements(WifiTetherSoftApManager.class)
public static final class ShadowWifiTetherSoftApManager {
@Implementation
public void registerSoftApCallback() {
// do nothing
}
@Implementation
public void unRegisterSoftApCallback() {
// do nothing
}
}
@Implements(WifiTetherSwitchBarController.class) @Implements(WifiTetherSwitchBarController.class)
public static final class ShadowWifiTetherSwitchBarController { public static final class ShadowWifiTetherSwitchBarController {