Wifi Setting: Use SoftApConfiguration for Tether Setting

Bug: 145578449
Test: Manual
Test: make RunSettingsRoboTests ROBOTEST_FILTER=CodeInspectionTest
Change-Id: I02b4dcbb7b6e29b67ecc7356200f3fc3a1007562
This commit is contained in:
lesl
2019-12-03 17:44:00 +08:00
parent 60b7c23583
commit ff012f03a8
18 changed files with 191 additions and 177 deletions

View File

@@ -229,7 +229,7 @@
<!-- Values for security type for wireless tether --> <!-- Values for security type for wireless tether -->
<string-array name="wifi_tether_security_values" translatable="false"> <string-array name="wifi_tether_security_values" translatable="false">
<!-- Do not translate. --> <!-- Do not translate. -->
<item>4</item> <item>1</item>
<!-- Do not translate. --> <!-- Do not translate. -->
<item>0</item> <item>0</item>
</string-array> </string-array>

View File

@@ -18,7 +18,7 @@ package com.android.settings.deviceinfo;
import android.bluetooth.BluetoothAdapter; import android.bluetooth.BluetoothAdapter;
import android.content.Context; import android.content.Context;
import android.net.wifi.WifiConfiguration; import android.net.wifi.SoftApConfiguration;
import android.net.wifi.WifiManager; import android.net.wifi.WifiManager;
import android.os.Build; import android.os.Build;
import android.os.Bundle; import android.os.Bundle;
@@ -160,10 +160,10 @@ public class DeviceNamePreferenceController extends BasePreferenceController
} }
private void setTetherSsidName(String deviceName) { private void setTetherSsidName(String deviceName) {
final WifiConfiguration config = mWifiManager.getWifiApConfiguration(); final SoftApConfiguration config = mWifiManager.getSoftApConfiguration();
config.SSID = deviceName;
// TODO: If tether is running, turn off the AP and restart it after setting config. // TODO: If tether is running, turn off the AP and restart it after setting config.
mWifiManager.setWifiApConfiguration(config); mWifiManager.setSoftApConfiguration(
new SoftApConfiguration.Builder(config).setSsid(deviceName).build());
} }
@Override @Override

View File

@@ -22,7 +22,7 @@ import android.content.Context;
import android.content.Intent; import android.content.Intent;
import android.content.IntentFilter; import android.content.IntentFilter;
import android.net.ConnectivityManager; import android.net.ConnectivityManager;
import android.net.wifi.WifiConfiguration; import android.net.wifi.SoftApConfiguration;
import android.net.wifi.WifiManager; import android.net.wifi.WifiManager;
import android.os.UserHandle; import android.os.UserHandle;
import android.os.UserManager; import android.os.UserManager;
@@ -114,13 +114,12 @@ public class HotspotConditionController implements ConditionalCardController {
} }
private CharSequence getSsid() { private CharSequence getSsid() {
WifiConfiguration wifiConfig = mWifiManager.getWifiApConfiguration(); final SoftApConfiguration softApConfig = mWifiManager.getSoftApConfiguration();
if (wifiConfig == null) { if (softApConfig == null) {
// Should never happen. // Should never happen.
return ""; return "";
} else {
return wifiConfig.SSID;
} }
return softApConfig.getSsid();
} }
public class Receiver extends BroadcastReceiver { public class Receiver extends BroadcastReceiver {

View File

@@ -20,8 +20,9 @@ import android.app.KeyguardManager;
import android.content.Context; import android.content.Context;
import android.content.Intent; import android.content.Intent;
import android.hardware.biometrics.BiometricPrompt; import android.hardware.biometrics.BiometricPrompt;
import android.net.wifi.WifiConfiguration.KeyMgmt; import android.net.wifi.SoftApConfiguration;
import android.net.wifi.WifiConfiguration; import android.net.wifi.WifiConfiguration;
import android.net.wifi.WifiConfiguration.KeyMgmt;
import android.net.wifi.WifiManager; import android.net.wifi.WifiManager;
import android.os.CancellationSignal; import android.os.CancellationSignal;
import android.os.Handler; import android.os.Handler;
@@ -31,12 +32,10 @@ import android.os.Vibrator;
import android.text.TextUtils; import android.text.TextUtils;
import com.android.settings.R; import com.android.settings.R;
import com.android.settingslib.wifi.AccessPoint; import com.android.settingslib.wifi.AccessPoint;
import java.util.List;
import java.time.Duration; import java.time.Duration;
import java.util.List;
/** /**
* Here are the items shared by both WifiDppConfiguratorActivity & WifiDppEnrolleeActivity * Here are the items shared by both WifiDppConfiguratorActivity & WifiDppEnrolleeActivity
@@ -248,19 +247,42 @@ public class WifiDppUtils {
* *
* @param context The context to use for the content resolver * @param context The context to use for the content resolver
* @param wifiManager An instance of {@link WifiManager} * @param wifiManager An instance of {@link WifiManager}
* @param wifiConfiguration {@link WifiConfiguration} of the Wi-Fi hotspot * @param softApConfiguration {@link SoftApConfiguration} of the Wi-Fi hotspot
* @return Intent for launching QR code generator * @return Intent for launching QR code generator
*/ */
public static Intent getHotspotConfiguratorIntentOrNull(Context context, public static Intent getHotspotConfiguratorIntentOrNull(Context context,
WifiManager wifiManager, WifiConfiguration wifiConfiguration) { WifiManager wifiManager, SoftApConfiguration softApConfiguration) {
final Intent intent = new Intent(context, WifiDppConfiguratorActivity.class); final Intent intent = new Intent(context, WifiDppConfiguratorActivity.class);
if (isSupportHotspotConfiguratorQrCodeGenerator(wifiConfiguration)) { if (isSupportHotspotConfiguratorQrCodeGenerator(softApConfiguration)) {
intent.setAction(WifiDppConfiguratorActivity.ACTION_CONFIGURATOR_QR_CODE_GENERATOR); intent.setAction(WifiDppConfiguratorActivity.ACTION_CONFIGURATOR_QR_CODE_GENERATOR);
} else { } else {
return null; return null;
} }
setConfiguratorIntentExtra(intent, wifiManager, wifiConfiguration); final String ssid = removeFirstAndLastDoubleQuotes(softApConfiguration.getSsid());
String security;
if (softApConfiguration.getSecurityType() == SoftApConfiguration.SECURITY_TYPE_WPA2_PSK) {
security = WifiQrCode.SECURITY_WPA_PSK;
} else {
security = WifiQrCode.SECURITY_NO_PASSWORD;
}
// When the value of this key is read, the actual key is not returned, just a "*".
// Call privileged system API to obtain actual key.
final String preSharedKey = removeFirstAndLastDoubleQuotes(
softApConfiguration.getWpa2Passphrase());
if (!TextUtils.isEmpty(ssid)) {
intent.putExtra(EXTRA_WIFI_SSID, ssid);
}
if (!TextUtils.isEmpty(security)) {
intent.putExtra(EXTRA_WIFI_SECURITY, security);
}
if (!TextUtils.isEmpty(preSharedKey)) {
intent.putExtra(EXTRA_WIFI_PRE_SHARED_KEY, preSharedKey);
}
intent.putExtra(EXTRA_WIFI_HIDDEN_SSID, softApConfiguration.isHiddenSsid());
intent.putExtra(EXTRA_WIFI_NETWORK_ID, WifiConfiguration.INVALID_NETWORK_ID); intent.putExtra(EXTRA_WIFI_NETWORK_ID, WifiConfiguration.INVALID_NETWORK_ID);
intent.putExtra(EXTRA_IS_HOTSPOT, true); intent.putExtra(EXTRA_IS_HOTSPOT, true);
@@ -383,12 +405,12 @@ public class WifiDppUtils {
} }
private static boolean isSupportHotspotConfiguratorQrCodeGenerator( private static boolean isSupportHotspotConfiguratorQrCodeGenerator(
WifiConfiguration wifiConfiguration) { SoftApConfiguration softApConfiguration) {
// QR code generator produces QR code with ZXing's Wi-Fi network config format, // QR code generator produces QR code with ZXing's Wi-Fi network config format,
// it supports PSK and WEP and non security // it supports PSK and WEP and non security
// KeyMgmt.NONE is for WEP or non security // KeyMgmt.NONE is for WEP or non security
return wifiConfiguration.allowedKeyManagement.get(KeyMgmt.WPA2_PSK) || return softApConfiguration.getSecurityType() == SoftApConfiguration.SECURITY_TYPE_WPA2_PSK
wifiConfiguration.allowedKeyManagement.get(KeyMgmt.NONE); || softApConfiguration.getSecurityType() == SoftApConfiguration.SECURITY_TYPE_OPEN;
} }
private static boolean isSupportWifiDpp(Context context, int accesspointSecurity) { private static boolean isSupportWifiDpp(Context context, int accesspointSecurity) {

View File

@@ -18,7 +18,7 @@ package com.android.settings.wifi.tether;
import android.content.Context; import android.content.Context;
import android.content.res.Resources; import android.content.res.Resources;
import android.net.wifi.WifiConfiguration; import android.net.wifi.SoftApConfiguration;
import android.util.Log; import android.util.Log;
import androidx.annotation.VisibleForTesting; import androidx.annotation.VisibleForTesting;
@@ -46,17 +46,17 @@ public class WifiTetherApBandPreferenceController extends WifiTetherBasePreferen
@Override @Override
public void updateDisplay() { public void updateDisplay() {
final WifiConfiguration config = mWifiManager.getWifiApConfiguration(); final SoftApConfiguration config = mWifiManager.getSoftApConfiguration();
if (config == null) { if (config == null) {
mBandIndex = 0; mBandIndex = 0;
Log.d(TAG, "Updating band index to 0 because no config"); Log.d(TAG, "Updating band index to 0 because no config");
} else if (is5GhzBandSupported()) { } else if (is5GhzBandSupported()) {
mBandIndex = validateSelection(config.apBand); mBandIndex = validateSelection(config.getBand());
Log.d(TAG, "Updating band index to " + mBandIndex); Log.d(TAG, "Updating band index to " + mBandIndex);
} else { } else {
config.apBand = 0; mWifiManager.setSoftApConfiguration(
mWifiManager.setWifiApConfiguration(config); new SoftApConfiguration.Builder(config).setBand(0).build());
mBandIndex = config.apBand; mBandIndex = config.getBand();
Log.d(TAG, "5Ghz not supported, updating band index to " + mBandIndex); Log.d(TAG, "5Ghz not supported, updating band index to " + mBandIndex);
} }
ListPreference preference = ListPreference preference =
@@ -68,13 +68,13 @@ public class WifiTetherApBandPreferenceController extends WifiTetherBasePreferen
preference.setEnabled(false); preference.setEnabled(false);
preference.setSummary(R.string.wifi_ap_choose_2G); preference.setSummary(R.string.wifi_ap_choose_2G);
} else { } else {
preference.setValue(Integer.toString(config.apBand)); preference.setValue(Integer.toString(config.getBand()));
preference.setSummary(getConfigSummary()); preference.setSummary(getConfigSummary());
} }
} }
String getConfigSummary() { String getConfigSummary() {
if (mBandIndex == WifiConfiguration.AP_BAND_ANY) { if (mBandIndex == SoftApConfiguration.BAND_ANY) {
return mContext.getString(R.string.wifi_ap_prefer_5G); return mContext.getString(R.string.wifi_ap_prefer_5G);
} }
return mBandSummaries[mBandIndex]; return mBandSummaries[mBandIndex];
@@ -102,12 +102,12 @@ public class WifiTetherApBandPreferenceController extends WifiTetherBasePreferen
// 1: no dual mode means we can't have AP_BAND_ANY - default to 5GHZ // 1: no dual mode means we can't have AP_BAND_ANY - default to 5GHZ
// 2: no 5 GHZ support means we can't have AP_BAND_5GHZ - default to 2GHZ // 2: no 5 GHZ support means we can't have AP_BAND_5GHZ - default to 2GHZ
// 3: With Dual mode support we can't have AP_BAND_5GHZ - default to ANY // 3: With Dual mode support we can't have AP_BAND_5GHZ - default to ANY
if (!isDualMode && WifiConfiguration.AP_BAND_ANY == band) { if (!isDualMode && SoftApConfiguration.BAND_ANY == band) {
return WifiConfiguration.AP_BAND_5GHZ; return SoftApConfiguration.BAND_5GHZ;
} else if (!is5GhzBandSupported() && WifiConfiguration.AP_BAND_5GHZ == band) { } else if (!is5GhzBandSupported() && SoftApConfiguration.BAND_5GHZ == band) {
return WifiConfiguration.AP_BAND_2GHZ; return SoftApConfiguration.BAND_2GHZ;
} else if (isDualMode && WifiConfiguration.AP_BAND_5GHZ == band) { } else if (isDualMode && SoftApConfiguration.BAND_5GHZ == band) {
return WifiConfiguration.AP_BAND_ANY; return SoftApConfiguration.BAND_ANY;
} }
return band; return band;

View File

@@ -17,7 +17,7 @@
package com.android.settings.wifi.tether; package com.android.settings.wifi.tether;
import android.content.Context; import android.content.Context;
import android.net.wifi.WifiConfiguration; import android.net.wifi.SoftApConfiguration;
import android.text.TextUtils; import android.text.TextUtils;
import androidx.preference.EditTextPreference; import androidx.preference.EditTextPreference;
@@ -48,12 +48,13 @@ public class WifiTetherPasswordPreferenceController extends WifiTetherBasePrefer
@Override @Override
public void updateDisplay() { public void updateDisplay() {
final WifiConfiguration config = mWifiManager.getWifiApConfiguration(); final SoftApConfiguration config = mWifiManager.getSoftApConfiguration();
if (config == null || (config.getAuthType() == WifiConfiguration.KeyMgmt.WPA2_PSK if (config == null
&& TextUtils.isEmpty(config.preSharedKey))) { || (config.getSecurityType() == SoftApConfiguration.SECURITY_TYPE_WPA2_PSK
&& TextUtils.isEmpty(config.getWpa2Passphrase()))) {
mPassword = generateRandomPassword(); mPassword = generateRandomPassword();
} else { } else {
mPassword = config.preSharedKey; mPassword = config.getWpa2Passphrase();
} }
((ValidatedEditTextPreference) mPreference).setValidator(this); ((ValidatedEditTextPreference) mPreference).setValidator(this);
((ValidatedEditTextPreference) mPreference).setIsPassword(true); ((ValidatedEditTextPreference) mPreference).setIsPassword(true);
@@ -79,7 +80,7 @@ public class WifiTetherPasswordPreferenceController extends WifiTetherBasePrefer
*/ */
public String getPasswordValidated(int securityType) { public String getPasswordValidated(int securityType) {
// don't actually overwrite unless we get a new config in case it was accidentally toggled. // don't actually overwrite unless we get a new config in case it was accidentally toggled.
if (securityType == WifiConfiguration.KeyMgmt.NONE) { if (securityType == SoftApConfiguration.SECURITY_TYPE_OPEN) {
return ""; return "";
} else if (!isTextValid(mPassword)) { } else if (!isTextValid(mPassword)) {
mPassword = generateRandomPassword(); mPassword = generateRandomPassword();
@@ -89,7 +90,7 @@ public class WifiTetherPasswordPreferenceController extends WifiTetherBasePrefer
} }
public void updateVisibility(int securityType) { public void updateVisibility(int securityType) {
mPreference.setVisible(securityType != WifiConfiguration.KeyMgmt.NONE); mPreference.setVisible(securityType != SoftApConfiguration.SECURITY_TYPE_OPEN);
} }
@Override @Override

View File

@@ -19,8 +19,8 @@ package com.android.settings.wifi.tether;
import android.annotation.NonNull; import android.annotation.NonNull;
import android.content.Context; import android.content.Context;
import android.net.ConnectivityManager; import android.net.ConnectivityManager;
import android.net.wifi.SoftApConfiguration;
import android.net.wifi.WifiClient; import android.net.wifi.WifiClient;
import android.net.wifi.WifiConfiguration;
import android.net.wifi.WifiManager; import android.net.wifi.WifiManager;
import android.text.BidiFormatter; import android.text.BidiFormatter;
@@ -147,8 +147,8 @@ public class WifiTetherPreferenceController extends AbstractPreferenceController
mPreference.setSummary(R.string.wifi_tether_starting); mPreference.setSummary(R.string.wifi_tether_starting);
break; break;
case WifiManager.WIFI_AP_STATE_ENABLED: case WifiManager.WIFI_AP_STATE_ENABLED:
WifiConfiguration wifiConfig = mWifiManager.getWifiApConfiguration(); final SoftApConfiguration softApConfig = mWifiManager.getSoftApConfiguration();
updateConfigSummary(wifiConfig); updateConfigSummary(softApConfig);
break; break;
case WifiManager.WIFI_AP_STATE_DISABLING: case WifiManager.WIFI_AP_STATE_DISABLING:
mPreference.setSummary(R.string.wifi_tether_stopping); mPreference.setSummary(R.string.wifi_tether_stopping);
@@ -165,12 +165,12 @@ public class WifiTetherPreferenceController extends AbstractPreferenceController
} }
} }
private void updateConfigSummary(@NonNull WifiConfiguration wifiConfig) { private void updateConfigSummary(@NonNull SoftApConfiguration softApConfig) {
if (wifiConfig == null) { if (softApConfig == null) {
// Should never happen. // Should never happen.
return; return;
} }
mPreference.setSummary(mContext.getString(R.string.wifi_tether_enabled_subtext, mPreference.setSummary(mContext.getString(R.string.wifi_tether_enabled_subtext,
BidiFormatter.getInstance().unicodeWrap(wifiConfig.SSID))); BidiFormatter.getInstance().unicodeWrap(softApConfig.getSsid())));
} }
} }

View File

@@ -19,19 +19,16 @@ package com.android.settings.wifi.tether;
import android.app.settings.SettingsEnums; import android.app.settings.SettingsEnums;
import android.content.Context; import android.content.Context;
import android.content.Intent; import android.content.Intent;
import android.net.wifi.WifiConfiguration; import android.net.wifi.SoftApConfiguration;
import android.util.Log; import android.util.Log;
import android.view.View;
import androidx.annotation.VisibleForTesting; import androidx.annotation.VisibleForTesting;
import androidx.preference.EditTextPreference; import androidx.preference.EditTextPreference;
import androidx.preference.Preference; import androidx.preference.Preference;
import com.android.settings.R;
import com.android.settings.overlay.FeatureFactory; import com.android.settings.overlay.FeatureFactory;
import com.android.settings.widget.ValidatedEditTextPreference; import com.android.settings.widget.ValidatedEditTextPreference;
import com.android.settings.wifi.dpp.WifiDppUtils; import com.android.settings.wifi.dpp.WifiDppUtils;
import com.android.settingslib.core.instrumentation.MetricsFeatureProvider; import com.android.settingslib.core.instrumentation.MetricsFeatureProvider;
public class WifiTetherSSIDPreferenceController extends WifiTetherBasePreferenceController public class WifiTetherSSIDPreferenceController extends WifiTetherBasePreferenceController
@@ -62,9 +59,9 @@ public class WifiTetherSSIDPreferenceController extends WifiTetherBasePreference
@Override @Override
public void updateDisplay() { public void updateDisplay() {
final WifiConfiguration config = mWifiManager.getWifiApConfiguration(); final SoftApConfiguration config = mWifiManager.getSoftApConfiguration();
if (config != null) { if (config != null) {
mSSID = config.SSID; mSSID = config.getSsid();
} else { } else {
mSSID = DEFAULT_SSID; mSSID = DEFAULT_SSID;
} }

View File

@@ -1,7 +1,7 @@
package com.android.settings.wifi.tether; package com.android.settings.wifi.tether;
import android.content.Context; import android.content.Context;
import android.net.wifi.WifiConfiguration; import android.net.wifi.SoftApConfiguration;
import androidx.preference.ListPreference; import androidx.preference.ListPreference;
import androidx.preference.Preference; import androidx.preference.Preference;
@@ -28,12 +28,11 @@ public class WifiTetherSecurityPreferenceController extends WifiTetherBasePrefer
@Override @Override
public void updateDisplay() { public void updateDisplay() {
final WifiConfiguration config = mWifiManager.getWifiApConfiguration(); final SoftApConfiguration config = mWifiManager.getSoftApConfiguration();
if (config != null && config.getAuthType() == WifiConfiguration.KeyMgmt.NONE) { if (config != null && config.getSecurityType() == SoftApConfiguration.SECURITY_TYPE_OPEN) {
mSecurityValue = WifiConfiguration.KeyMgmt.NONE; mSecurityValue = SoftApConfiguration.SECURITY_TYPE_OPEN;
} else { } else {
mSecurityValue = WifiConfiguration.KeyMgmt.WPA2_PSK; mSecurityValue = SoftApConfiguration.SECURITY_TYPE_WPA2_PSK;
} }
final ListPreference preference = (ListPreference) mPreference; final ListPreference preference = (ListPreference) mPreference;
@@ -54,7 +53,7 @@ public class WifiTetherSecurityPreferenceController extends WifiTetherBasePrefer
} }
private String getSummaryForSecurityType(int securityType) { private String getSummaryForSecurityType(int securityType) {
if (securityType == WifiConfiguration.KeyMgmt.NONE) { if (securityType == SoftApConfiguration.SECURITY_TYPE_OPEN) {
return mSecurityEntries[1]; return mSecurityEntries[1];
} }
// WPA2 PSK // WPA2 PSK

View File

@@ -24,14 +24,15 @@ import android.content.BroadcastReceiver;
import android.content.Context; import android.content.Context;
import android.content.Intent; import android.content.Intent;
import android.content.IntentFilter; import android.content.IntentFilter;
import android.net.wifi.WifiConfiguration; import android.net.wifi.SoftApConfiguration;
import android.net.wifi.WifiManager; import android.net.wifi.WifiManager;
import android.os.Bundle; import android.os.Bundle;
import android.os.UserManager; import android.os.UserManager;
import android.provider.SearchIndexableResource;
import android.util.Log; import android.util.Log;
import androidx.annotation.VisibleForTesting; import androidx.annotation.VisibleForTesting;
import androidx.preference.PreferenceGroup; import androidx.preference.PreferenceGroup;
import com.android.settings.R; import com.android.settings.R;
import com.android.settings.SettingsActivity; import com.android.settings.SettingsActivity;
import com.android.settings.dashboard.RestrictedDashboardFragment; import com.android.settings.dashboard.RestrictedDashboardFragment;
@@ -41,8 +42,8 @@ import com.android.settings.widget.SwitchBarController;
import com.android.settingslib.TetherUtil; import com.android.settingslib.TetherUtil;
import com.android.settingslib.core.AbstractPreferenceController; import com.android.settingslib.core.AbstractPreferenceController;
import com.android.settingslib.search.SearchIndexable; import com.android.settingslib.search.SearchIndexable;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays;
import java.util.List; import java.util.List;
@SearchIndexable @SearchIndexable
@@ -187,8 +188,8 @@ public class WifiTetherSettings extends RestrictedDashboardFragment
@Override @Override
public void onTetherConfigUpdated(AbstractPreferenceController context) { public void onTetherConfigUpdated(AbstractPreferenceController context) {
final WifiConfiguration config = buildNewConfig(); final SoftApConfiguration config = buildNewConfig();
mPasswordPreferenceController.updateVisibility(config.getAuthType()); mPasswordPreferenceController.updateVisibility(config.getSecurityType());
/** /**
* if soft AP is stopped, bring up * if soft AP is stopped, bring up
@@ -201,23 +202,23 @@ public class WifiTetherSettings extends RestrictedDashboardFragment
mRestartWifiApAfterConfigChange = true; mRestartWifiApAfterConfigChange = true;
mSwitchBarController.stopTether(); mSwitchBarController.stopTether();
} }
mWifiManager.setWifiApConfiguration(config); mWifiManager.setSoftApConfiguration(config);
if (context instanceof WifiTetherSecurityPreferenceController) { if (context instanceof WifiTetherSecurityPreferenceController) {
reConfigInitialExpandedChildCount(); reConfigInitialExpandedChildCount();
} }
} }
private WifiConfiguration buildNewConfig() { private SoftApConfiguration buildNewConfig() {
final WifiConfiguration config = new WifiConfiguration(); final SoftApConfiguration.Builder configBuilder = new SoftApConfiguration.Builder();
final int securityType = mSecurityPreferenceController.getSecurityType(); final int securityType = mSecurityPreferenceController.getSecurityType();
configBuilder.setSsid(mSSIDPreferenceController.getSSID());
config.SSID = mSSIDPreferenceController.getSSID(); if (securityType == SoftApConfiguration.SECURITY_TYPE_WPA2_PSK) {
config.allowedKeyManagement.set(securityType); configBuilder.setWpa2Passphrase(
config.preSharedKey = mPasswordPreferenceController.getPasswordValidated(securityType); mPasswordPreferenceController.getPasswordValidated(securityType));
config.allowedAuthAlgorithms.set(WifiConfiguration.AuthAlgorithm.OPEN); }
config.apBand = mApBandPreferenceController.getBandIndex(); configBuilder.setBand(mApBandPreferenceController.getBandIndex());
return config; return configBuilder.build();
} }
private void startTether() { private void startTether() {
@@ -286,7 +287,8 @@ public class WifiTetherSettings extends RestrictedDashboardFragment
private void reConfigInitialExpandedChildCount() { private void reConfigInitialExpandedChildCount() {
final PreferenceGroup screen = getPreferenceScreen(); final PreferenceGroup screen = getPreferenceScreen();
if (mSecurityPreferenceController.getSecurityType() == WifiConfiguration.KeyMgmt.NONE) { if (mSecurityPreferenceController.getSecurityType()
== SoftApConfiguration.SECURITY_TYPE_OPEN) {
screen.setInitialExpandedChildrenCount(EXPANDED_CHILD_COUNT_WITH_SECURITY_NON); screen.setInitialExpandedChildrenCount(EXPANDED_CHILD_COUNT_WITH_SECURITY_NON);
return; return;
} }
@@ -299,7 +301,8 @@ public class WifiTetherSettings extends RestrictedDashboardFragment
return EXPANDED_CHILD_COUNT_DEFAULT; return EXPANDED_CHILD_COUNT_DEFAULT;
} }
return (mSecurityPreferenceController.getSecurityType() == WifiConfiguration.KeyMgmt.NONE) ? return (mSecurityPreferenceController.getSecurityType()
EXPANDED_CHILD_COUNT_WITH_SECURITY_NON : EXPANDED_CHILD_COUNT_DEFAULT; == SoftApConfiguration.SECURITY_TYPE_OPEN)
? EXPANDED_CHILD_COUNT_WITH_SECURITY_NON : EXPANDED_CHILD_COUNT_DEFAULT;
} }
} }

View File

@@ -27,7 +27,7 @@ import static org.mockito.Mockito.when;
import android.bluetooth.BluetoothAdapter; import android.bluetooth.BluetoothAdapter;
import android.content.Context; import android.content.Context;
import android.net.wifi.WifiConfiguration; import android.net.wifi.SoftApConfiguration;
import android.net.wifi.WifiManager; import android.net.wifi.WifiManager;
import android.os.Build; import android.os.Build;
import android.provider.Settings; import android.provider.Settings;
@@ -71,9 +71,9 @@ public class DeviceNamePreferenceControllerTest {
mContext = RuntimeEnvironment.application; mContext = RuntimeEnvironment.application;
mPreference = new ValidatedEditTextPreference(mContext); mPreference = new ValidatedEditTextPreference(mContext);
when(mScreen.findPreference(anyString())).thenReturn(mPreference); when(mScreen.findPreference(anyString())).thenReturn(mPreference);
final WifiConfiguration configuration = new WifiConfiguration(); final SoftApConfiguration configuration =
configuration.SSID = "test-ap"; new SoftApConfiguration.Builder().setSsid("test-ap").build();
when(mWifiManager.getWifiApConfiguration()).thenReturn(configuration); when(mWifiManager.getSoftApConfiguration()).thenReturn(configuration);
mController = new DeviceNamePreferenceController(mContext, "test_key"); mController = new DeviceNamePreferenceController(mContext, "test_key");
mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter(); mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
@@ -137,9 +137,10 @@ public class DeviceNamePreferenceControllerTest {
mController.displayPreference(mScreen); mController.displayPreference(mScreen);
mController.onPreferenceChange(mPreference, TESTING_STRING); mController.onPreferenceChange(mPreference, TESTING_STRING);
ArgumentCaptor<WifiConfiguration> captor = ArgumentCaptor.forClass(WifiConfiguration.class); ArgumentCaptor<SoftApConfiguration> captor =
verify(mWifiManager).setWifiApConfiguration(captor.capture()); ArgumentCaptor.forClass(SoftApConfiguration.class);
assertThat(captor.getValue().SSID).isEqualTo(TESTING_STRING); verify(mWifiManager).setSoftApConfiguration(captor.capture());
assertThat(captor.getValue().getSsid()).isEqualTo(TESTING_STRING);
} }
@Test @Test

View File

@@ -19,7 +19,7 @@ package com.android.settings.homepage.contextualcards.conditional;
import static com.google.common.truth.Truth.assertThat; import static com.google.common.truth.Truth.assertThat;
import android.content.Context; import android.content.Context;
import android.net.wifi.WifiConfiguration; import android.net.wifi.SoftApConfiguration;
import android.net.wifi.WifiManager; import android.net.wifi.WifiManager;
import com.android.settings.homepage.contextualcards.ContextualCard; import com.android.settings.homepage.contextualcards.ContextualCard;
@@ -53,7 +53,7 @@ public class HotspotConditionControllerTest {
@Test @Test
public void buildContextualCard_hasWifiAp_shouldHaveWifiApSsid() { public void buildContextualCard_hasWifiAp_shouldHaveWifiApSsid() {
setupWifiApConfiguration(); setupSoftApConfiguration();
final ContextualCard card = mController.buildContextualCard(); final ContextualCard card = mController.buildContextualCard();
@@ -67,9 +67,9 @@ public class HotspotConditionControllerTest {
assertThat(card.getSummaryText()).isEqualTo(""); assertThat(card.getSummaryText()).isEqualTo("");
} }
private void setupWifiApConfiguration() { private void setupSoftApConfiguration() {
final WifiConfiguration wifiApConfig = new WifiConfiguration(); final SoftApConfiguration wifiApConfig = new SoftApConfiguration.Builder()
wifiApConfig.SSID = WIFI_AP_SSID; .setSsid(WIFI_AP_SSID).build();
mContext.getSystemService(WifiManager.class).setWifiApConfiguration(wifiApConfig); mContext.getSystemService(WifiManager.class).setSoftApConfiguration(wifiApConfig);
} }
} }

View File

@@ -18,6 +18,7 @@ package com.android.settings.testutils.shadow;
import static org.robolectric.RuntimeEnvironment.application; import static org.robolectric.RuntimeEnvironment.application;
import android.net.wifi.SoftApConfiguration;
import android.net.wifi.WifiConfiguration; import android.net.wifi.WifiConfiguration;
import android.net.wifi.WifiManager; import android.net.wifi.WifiManager;
import android.net.wifi.hotspot2.PasspointConfiguration; import android.net.wifi.hotspot2.PasspointConfiguration;
@@ -37,16 +38,16 @@ public class ShadowWifiManager extends org.robolectric.shadows.ShadowWifiManager
private List<PasspointConfiguration> mPasspointConfiguration; private List<PasspointConfiguration> mPasspointConfiguration;
public WifiConfiguration savedWifiConfig; public WifiConfiguration savedWifiConfig;
private WifiConfiguration mSavedApConfig; private SoftApConfiguration mSavedApConfig;
@Implementation @Implementation
protected WifiConfiguration getWifiApConfiguration() { protected SoftApConfiguration getSoftApConfiguration() {
return mSavedApConfig; return mSavedApConfig;
} }
@Implementation @Implementation
protected boolean setWifiApConfiguration(WifiConfiguration wifiConfig) { protected boolean setSoftApConfiguration(SoftApConfiguration softApConfig) {
mSavedApConfig = wifiConfig; mSavedApConfig = softApConfig;
return true; return true;
} }

View File

@@ -26,7 +26,7 @@ import static org.mockito.Mockito.when;
import android.content.Context; import android.content.Context;
import android.net.ConnectivityManager; import android.net.ConnectivityManager;
import android.net.wifi.WifiConfiguration; import android.net.wifi.SoftApConfiguration;
import android.net.wifi.WifiManager; import android.net.wifi.WifiManager;
import androidx.preference.ListPreference; import androidx.preference.ListPreference;
@@ -72,9 +72,8 @@ public class WifiTetherApBandPreferenceControllerTest {
when(mConnectivityManager.getTetherableWifiRegexs()).thenReturn(new String[]{"1", "2"}); when(mConnectivityManager.getTetherableWifiRegexs()).thenReturn(new String[]{"1", "2"});
when(mContext.getResources()).thenReturn(RuntimeEnvironment.application.getResources()); when(mContext.getResources()).thenReturn(RuntimeEnvironment.application.getResources());
when(mScreen.findPreference(anyString())).thenReturn(mPreference); when(mScreen.findPreference(anyString())).thenReturn(mPreference);
WifiConfiguration config = new WifiConfiguration(); when(mWifiManager.getSoftApConfiguration()).thenReturn(
config.apBand = WifiConfiguration.AP_BAND_ANY; new SoftApConfiguration.Builder().build());
when(mWifiManager.getWifiApConfiguration()).thenReturn(new WifiConfiguration());
when(mWifiManager.isDualModeSupported()).thenReturn(false); when(mWifiManager.isDualModeSupported()).thenReturn(false);
mController = new WifiTetherApBandPreferenceController(mContext, mListener); mController = new WifiTetherApBandPreferenceController(mContext, mListener);
@@ -123,7 +122,7 @@ public class WifiTetherApBandPreferenceControllerTest {
mController.displayPreference(mScreen); mController.displayPreference(mScreen);
// -1 is WifiConfiguration.AP_BAND_ANY, for 'Auto' option. This should be prevented from // -1 is SoftApConfiguration.BAND_ANY, for 'Auto' option. This should be prevented from
// being set since it is invalid for this configuration // being set since it is invalid for this configuration
mController.onPreferenceChange(mPreference, "-1"); mController.onPreferenceChange(mPreference, "-1");
assertThat(mController.getBandIndex()).isEqualTo(1); assertThat(mController.getBandIndex()).isEqualTo(1);
@@ -151,7 +150,7 @@ public class WifiTetherApBandPreferenceControllerTest {
mController.displayPreference(mScreen); mController.displayPreference(mScreen);
// -1 is WifiConfiguration.AP_BAND_ANY, for 'Auto' option. // -1 is SoftApConfiguration.BAND_ANY, for 'Auto' option.
mController.onPreferenceChange(mPreference, "-1"); mController.onPreferenceChange(mPreference, "-1");
assertThat(mController.getBandIndex()).isEqualTo(-1); assertThat(mController.getBandIndex()).isEqualTo(-1);
assertThat(mPreference.getSummary()).isEqualTo(ALL_BANDS); assertThat(mPreference.getSummary()).isEqualTo(ALL_BANDS);

View File

@@ -25,7 +25,7 @@ import static org.mockito.Mockito.when;
import android.content.Context; import android.content.Context;
import android.net.ConnectivityManager; import android.net.ConnectivityManager;
import android.net.wifi.WifiConfiguration; import android.net.wifi.SoftApConfiguration;
import android.net.wifi.WifiManager; import android.net.wifi.WifiManager;
import androidx.preference.PreferenceScreen; import androidx.preference.PreferenceScreen;
@@ -59,18 +59,17 @@ public class WifiTetherPasswordPreferenceControllerTest {
private WifiTetherPasswordPreferenceController mController; private WifiTetherPasswordPreferenceController mController;
private ValidatedEditTextPreference mPreference; private ValidatedEditTextPreference mPreference;
private WifiConfiguration mConfig; private SoftApConfiguration mConfig;
@Before @Before
public void setUp() { public void setUp() {
MockitoAnnotations.initMocks(this); MockitoAnnotations.initMocks(this);
mPreference = new ValidatedEditTextPreference(RuntimeEnvironment.application); mPreference = new ValidatedEditTextPreference(RuntimeEnvironment.application);
mConfig = new WifiConfiguration(); mConfig = new SoftApConfiguration.Builder().setSsid("test_1234")
mConfig.SSID = "test_1234"; .setWpa2Passphrase("test_password").build();
mConfig.preSharedKey = "test_password";
when(mContext.getSystemService(Context.WIFI_SERVICE)).thenReturn(mWifiManager); when(mContext.getSystemService(Context.WIFI_SERVICE)).thenReturn(mWifiManager);
when(mWifiManager.getWifiApConfiguration()).thenReturn(mConfig); when(mWifiManager.getSoftApConfiguration()).thenReturn(mConfig);
when(mContext.getSystemService(Context.CONNECTIVITY_SERVICE)) when(mContext.getSystemService(Context.CONNECTIVITY_SERVICE))
.thenReturn(mConnectivityManager); .thenReturn(mConnectivityManager);
when(mConnectivityManager.getTetherableWifiRegexs()).thenReturn(new String[]{"1", "2"}); when(mConnectivityManager.getTetherableWifiRegexs()).thenReturn(new String[]{"1", "2"});
@@ -84,19 +83,19 @@ public class WifiTetherPasswordPreferenceControllerTest {
public void displayPreference_shouldStylePreference() { public void displayPreference_shouldStylePreference() {
mController.displayPreference(mScreen); mController.displayPreference(mScreen);
assertThat(mPreference.getText()).isEqualTo(mConfig.preSharedKey); assertThat(mPreference.getText()).isEqualTo(mConfig.getWpa2Passphrase());
assertThat(mPreference.getSummary()).isEqualTo(mConfig.preSharedKey); assertThat(mPreference.getSummary()).isEqualTo(mConfig.getWpa2Passphrase());
} }
@Test @Test
public void changePreference_shouldUpdateValue() { public void changePreference_shouldUpdateValue() {
mController.displayPreference(mScreen); mController.displayPreference(mScreen);
mController.onPreferenceChange(mPreference, VALID_PASS); mController.onPreferenceChange(mPreference, VALID_PASS);
assertThat(mController.getPasswordValidated(WifiConfiguration.KeyMgmt.WPA2_PSK)) assertThat(mController.getPasswordValidated(SoftApConfiguration.SECURITY_TYPE_WPA2_PSK))
.isEqualTo(VALID_PASS); .isEqualTo(VALID_PASS);
mController.onPreferenceChange(mPreference, VALID_PASS2); mController.onPreferenceChange(mPreference, VALID_PASS2);
assertThat(mController.getPasswordValidated(WifiConfiguration.KeyMgmt.WPA2_PSK)) assertThat(mController.getPasswordValidated(SoftApConfiguration.SECURITY_TYPE_WPA2_PSK))
.isEqualTo(VALID_PASS2); .isEqualTo(VALID_PASS2);
verify(mListener, times(2)).onTetherConfigUpdated(mController); verify(mListener, times(2)).onTetherConfigUpdated(mController);
@@ -107,19 +106,19 @@ public class WifiTetherPasswordPreferenceControllerTest {
// Set controller password to anything and verify is set. // Set controller password to anything and verify is set.
mController.displayPreference(mScreen); mController.displayPreference(mScreen);
mController.onPreferenceChange(mPreference, VALID_PASS); mController.onPreferenceChange(mPreference, VALID_PASS);
assertThat(mController.getPasswordValidated(WifiConfiguration.KeyMgmt.WPA2_PSK)) assertThat(mController.getPasswordValidated(SoftApConfiguration.SECURITY_TYPE_WPA2_PSK))
.isEqualTo(VALID_PASS); .isEqualTo(VALID_PASS);
// Create a new config using different password // Create a new config using different password
final WifiConfiguration config = new WifiConfiguration(); final SoftApConfiguration config = new SoftApConfiguration.Builder()
config.preSharedKey = VALID_PASS2; .setWpa2Passphrase(VALID_PASS2).build();
when(mWifiManager.getWifiApConfiguration()).thenReturn(config); when(mWifiManager.getSoftApConfiguration()).thenReturn(config);
// Call updateDisplay and verify it's changed. // Call updateDisplay and verify it's changed.
mController.updateDisplay(); mController.updateDisplay();
assertThat(mController.getPasswordValidated(WifiConfiguration.KeyMgmt.WPA2_PSK)) assertThat(mController.getPasswordValidated(SoftApConfiguration.SECURITY_TYPE_WPA2_PSK))
.isEqualTo(config.preSharedKey); .isEqualTo(config.getWpa2Passphrase());
assertThat(mPreference.getSummary()).isEqualTo(config.preSharedKey); assertThat(mPreference.getSummary()).isEqualTo(config.getWpa2Passphrase());
} }
@Test @Test
@@ -127,13 +126,13 @@ public class WifiTetherPasswordPreferenceControllerTest {
// Set controller password to anything and verify is set. // Set controller password to anything and verify is set.
mController.displayPreference(mScreen); mController.displayPreference(mScreen);
mController.onPreferenceChange(mPreference, VALID_PASS); mController.onPreferenceChange(mPreference, VALID_PASS);
assertThat(mController.getPasswordValidated(WifiConfiguration.KeyMgmt.WPA2_PSK)) assertThat(mController.getPasswordValidated(SoftApConfiguration.SECURITY_TYPE_WPA2_PSK))
.isEqualTo(VALID_PASS); .isEqualTo(VALID_PASS);
// Create a new config using different password // Create a new config using different password
final WifiConfiguration config = new WifiConfiguration(); final SoftApConfiguration config = new SoftApConfiguration.Builder()
config.preSharedKey = VALID_PASS2; .setWpa2Passphrase(VALID_PASS2).build();
when(mWifiManager.getWifiApConfiguration()).thenReturn(config); when(mWifiManager.getSoftApConfiguration()).thenReturn(config);
// Call updateDisplay and verify it's changed. // Call updateDisplay and verify it's changed.
mController.updateDisplay(); mController.updateDisplay();

View File

@@ -19,18 +19,13 @@ package com.android.settings.wifi.tether;
import static com.google.common.truth.Truth.assertThat; import static com.google.common.truth.Truth.assertThat;
import static org.mockito.ArgumentMatchers.anyString; import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.spy; import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.when; import static org.mockito.Mockito.when;
import android.content.BroadcastReceiver;
import android.content.ContentResolver;
import android.content.Context; import android.content.Context;
import android.content.Intent;
import android.net.ConnectivityManager; import android.net.ConnectivityManager;
import android.net.wifi.WifiConfiguration; import android.net.wifi.SoftApConfiguration;
import android.net.wifi.WifiManager; import android.net.wifi.WifiManager;
import android.provider.Settings;
import androidx.lifecycle.LifecycleOwner; import androidx.lifecycle.LifecycleOwner;
import androidx.preference.PreferenceScreen; import androidx.preference.PreferenceScreen;
@@ -49,7 +44,6 @@ import org.robolectric.RuntimeEnvironment;
import org.robolectric.annotation.Config; import org.robolectric.annotation.Config;
import org.robolectric.annotation.Implementation; import org.robolectric.annotation.Implementation;
import org.robolectric.annotation.Implements; import org.robolectric.annotation.Implements;
import org.robolectric.util.ReflectionHelpers;
@RunWith(RobolectricTestRunner.class) @RunWith(RobolectricTestRunner.class)
@Config(shadows = { @Config(shadows = {
@@ -67,8 +61,7 @@ public class WifiTetherPreferenceControllerTest {
private WifiManager mWifiManager; private WifiManager mWifiManager;
@Mock @Mock
private PreferenceScreen mScreen; private PreferenceScreen mScreen;
@Mock private SoftApConfiguration mSoftApConfiguration;
private WifiConfiguration mWifiConfiguration;
private WifiTetherPreferenceController mController; private WifiTetherPreferenceController mController;
private Lifecycle mLifecycle; private Lifecycle mLifecycle;
@@ -88,8 +81,8 @@ public class WifiTetherPreferenceControllerTest {
.thenReturn(mConnectivityManager); .thenReturn(mConnectivityManager);
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(mWifiManager.getWifiApConfiguration()).thenReturn(mWifiConfiguration); mSoftApConfiguration = new SoftApConfiguration.Builder().setSsid(SSID).build();
mWifiConfiguration.SSID = SSID; when(mWifiManager.getSoftApConfiguration()).thenReturn(mSoftApConfiguration);
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,

View File

@@ -25,14 +25,11 @@ import static org.mockito.Mockito.when;
import android.content.Context; import android.content.Context;
import android.net.ConnectivityManager; import android.net.ConnectivityManager;
import android.net.wifi.WifiConfiguration; import android.net.wifi.SoftApConfiguration;
import android.net.wifi.WifiConfiguration.KeyMgmt;
import android.net.wifi.WifiManager; import android.net.wifi.WifiManager;
import androidx.preference.PreferenceScreen; import androidx.preference.PreferenceScreen;
import com.android.settings.widget.ValidatedEditTextPreference;
import org.junit.Before; import org.junit.Before;
import org.junit.Test; import org.junit.Test;
import org.junit.runner.RunWith; import org.junit.runner.RunWith;
@@ -76,7 +73,7 @@ public class WifiTetherSSIDPreferenceControllerTest {
@Test @Test
public void displayPreference_noWifiConfig_shouldDisplayDefaultSSID() { public void displayPreference_noWifiConfig_shouldDisplayDefaultSSID() {
when(mWifiManager.getWifiApConfiguration()).thenReturn(null); when(mWifiManager.getSoftApConfiguration()).thenReturn(null);
mController.displayPreference(mScreen); mController.displayPreference(mScreen);
assertThat(mController.getSSID()) assertThat(mController.getSSID())
@@ -85,12 +82,12 @@ public class WifiTetherSSIDPreferenceControllerTest {
@Test @Test
public void displayPreference_hasCustomWifiConfig_shouldDisplayCustomSSID() { public void displayPreference_hasCustomWifiConfig_shouldDisplayCustomSSID() {
final WifiConfiguration config = new WifiConfiguration(); final SoftApConfiguration config = new SoftApConfiguration.Builder()
config.SSID = "test_1234"; .setSsid("test_1234").build();
when(mWifiManager.getWifiApConfiguration()).thenReturn(config); when(mWifiManager.getSoftApConfiguration()).thenReturn(config);
mController.displayPreference(mScreen); mController.displayPreference(mScreen);
assertThat(mController.getSSID()).isEqualTo(config.SSID); assertThat(mController.getSSID()).isEqualTo(config.getSsid());
} }
@Test @Test
@@ -113,23 +110,22 @@ public class WifiTetherSSIDPreferenceControllerTest {
assertThat(mController.getSSID()).isEqualTo("1"); assertThat(mController.getSSID()).isEqualTo("1");
// Create a new config using different SSID // Create a new config using different SSID
final WifiConfiguration config = new WifiConfiguration(); final SoftApConfiguration config = new SoftApConfiguration.Builder()
config.SSID = "test_1234"; .setSsid("test_1234").build();
when(mWifiManager.getWifiApConfiguration()).thenReturn(config); when(mWifiManager.getSoftApConfiguration()).thenReturn(config);
// Call updateDisplay and verify it's changed. // Call updateDisplay and verify it's changed.
mController.updateDisplay(); mController.updateDisplay();
assertThat(mController.getSSID()).isEqualTo(config.SSID); assertThat(mController.getSSID()).isEqualTo(config.getSsid());
assertThat(mPreference.getSummary()).isEqualTo(config.SSID); assertThat(mPreference.getSummary()).isEqualTo(config.getSsid());
} }
@Test @Test
public void displayPreference_wifiApDisabled_shouldHideQrCodeIcon() { public void displayPreference_wifiApDisabled_shouldHideQrCodeIcon() {
when(mWifiManager.isWifiApEnabled()).thenReturn(false); when(mWifiManager.isWifiApEnabled()).thenReturn(false);
final WifiConfiguration config = new WifiConfiguration(); final SoftApConfiguration config = new SoftApConfiguration.Builder()
config.SSID = "test_1234"; .setSsid("test_1234").setWpa2Passphrase("test_password").build();
config.allowedKeyManagement.set(KeyMgmt.WPA2_PSK); when(mWifiManager.getSoftApConfiguration()).thenReturn(config);
when(mWifiManager.getWifiApConfiguration()).thenReturn(config);
mController.displayPreference(mScreen); mController.displayPreference(mScreen);
assertThat(mController.isQrCodeButtonAvailable()).isEqualTo(false); assertThat(mController.isQrCodeButtonAvailable()).isEqualTo(false);
@@ -138,10 +134,9 @@ public class WifiTetherSSIDPreferenceControllerTest {
@Test @Test
public void displayPreference_wifiApEnabled_shouldShowQrCodeIcon() { public void displayPreference_wifiApEnabled_shouldShowQrCodeIcon() {
when(mWifiManager.isWifiApEnabled()).thenReturn(true); when(mWifiManager.isWifiApEnabled()).thenReturn(true);
final WifiConfiguration config = new WifiConfiguration(); final SoftApConfiguration config = new SoftApConfiguration.Builder()
config.SSID = "test_1234"; .setSsid("test_1234").setWpa2Passphrase("test_password").build();
config.allowedKeyManagement.set(KeyMgmt.WPA2_PSK); when(mWifiManager.getSoftApConfiguration()).thenReturn(config);
when(mWifiManager.getWifiApConfiguration()).thenReturn(config);
mController.displayPreference(mScreen); mController.displayPreference(mScreen);
assertThat(mController.isQrCodeButtonAvailable()).isEqualTo(true); assertThat(mController.isQrCodeButtonAvailable()).isEqualTo(true);

View File

@@ -8,7 +8,7 @@ import static org.mockito.Mockito.when;
import android.content.Context; import android.content.Context;
import android.net.ConnectivityManager; import android.net.ConnectivityManager;
import android.net.wifi.WifiConfiguration; import android.net.wifi.SoftApConfiguration;
import android.net.wifi.WifiManager; import android.net.wifi.WifiManager;
import androidx.preference.ListPreference; import androidx.preference.ListPreference;
@@ -25,8 +25,9 @@ import org.robolectric.RuntimeEnvironment;
@RunWith(RobolectricTestRunner.class) @RunWith(RobolectricTestRunner.class)
public class WifiTetherSecurityPreferenceControllerTest { public class WifiTetherSecurityPreferenceControllerTest {
private static final String WPA2_PSK = String.valueOf(WifiConfiguration.KeyMgmt.WPA2_PSK); private static final String WPA2_PSK =
private static final String NONE = String.valueOf(WifiConfiguration.KeyMgmt.NONE); String.valueOf(SoftApConfiguration.SECURITY_TYPE_WPA2_PSK);
private static final String NONE = String.valueOf(SoftApConfiguration.SECURITY_TYPE_OPEN);
@Mock @Mock
private WifiTetherBasePreferenceController.OnTetherConfigUpdateListener mListener; private WifiTetherBasePreferenceController.OnTetherConfigUpdateListener mListener;
private Context mContext; private Context mContext;
@@ -38,19 +39,17 @@ public class WifiTetherSecurityPreferenceControllerTest {
private PreferenceScreen mScreen; private PreferenceScreen mScreen;
private WifiTetherSecurityPreferenceController mController; private WifiTetherSecurityPreferenceController mController;
private ListPreference mPreference; private ListPreference mPreference;
private WifiConfiguration mConfig; private SoftApConfiguration mConfig;
@Before @Before
public void setUp() { public void setUp() {
MockitoAnnotations.initMocks(this); MockitoAnnotations.initMocks(this);
mConfig = new WifiConfiguration(); mConfig = new SoftApConfiguration.Builder().setSsid("test_1234")
mConfig.SSID = "test_1234"; .setWpa2Passphrase("test_password").build();
mConfig.preSharedKey = "test_password";
mConfig.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.WPA2_PSK);
mContext = spy(RuntimeEnvironment.application); mContext = spy(RuntimeEnvironment.application);
when(mContext.getSystemService(Context.WIFI_SERVICE)).thenReturn(mWifiManager); when(mContext.getSystemService(Context.WIFI_SERVICE)).thenReturn(mWifiManager);
when(mWifiManager.getWifiApConfiguration()).thenReturn(mConfig); when(mWifiManager.getSoftApConfiguration()).thenReturn(mConfig);
when(mContext.getSystemService(Context.CONNECTIVITY_SERVICE)) when(mContext.getSystemService(Context.CONNECTIVITY_SERVICE))
.thenReturn(mConnectivityManager); .thenReturn(mConnectivityManager);
when(mConnectivityManager.getTetherableWifiRegexs()).thenReturn(new String[]{"1", "2"}); when(mConnectivityManager.getTetherableWifiRegexs()).thenReturn(new String[]{"1", "2"});
@@ -64,35 +63,41 @@ public class WifiTetherSecurityPreferenceControllerTest {
@Test @Test
public void onPreferenceChange_securityValueUpdated() { public void onPreferenceChange_securityValueUpdated() {
mController.onPreferenceChange(mPreference, WPA2_PSK); mController.onPreferenceChange(mPreference, WPA2_PSK);
assertThat(mController.getSecurityType()).isEqualTo(WifiConfiguration.KeyMgmt.WPA2_PSK); assertThat(mController.getSecurityType()).isEqualTo(
SoftApConfiguration.SECURITY_TYPE_WPA2_PSK);
assertThat(mPreference.getSummary().toString()).isEqualTo("WPA2-Personal"); assertThat(mPreference.getSummary().toString()).isEqualTo("WPA2-Personal");
mController.onPreferenceChange(mPreference, NONE); mController.onPreferenceChange(mPreference, NONE);
assertThat(mController.getSecurityType()).isEqualTo(WifiConfiguration.KeyMgmt.NONE); assertThat(mController.getSecurityType()).isEqualTo(
SoftApConfiguration.SECURITY_TYPE_OPEN);
assertThat(mPreference.getSummary().toString()).isEqualTo("None"); assertThat(mPreference.getSummary().toString()).isEqualTo("None");
} }
@Test @Test
public void updateDisplay_preferenceUpdated() { public void updateDisplay_preferenceUpdated() {
// test defaulting to WPA2-Personal on new config // test defaulting to WPA2-Personal on new config
when(mWifiManager.getWifiApConfiguration()).thenReturn(null); when(mWifiManager.getSoftApConfiguration()).thenReturn(null);
mController.updateDisplay(); mController.updateDisplay();
assertThat(mController.getSecurityType()).isEqualTo(WifiConfiguration.KeyMgmt.WPA2_PSK); assertThat(mController.getSecurityType()).isEqualTo(
SoftApConfiguration.SECURITY_TYPE_WPA2_PSK);
assertThat(mPreference.getSummary().toString()).isEqualTo("WPA2-Personal"); assertThat(mPreference.getSummary().toString()).isEqualTo("WPA2-Personal");
// test open tether network // test open tether network
when(mWifiManager.getWifiApConfiguration()).thenReturn(mConfig); SoftApConfiguration config = new SoftApConfiguration.Builder(mConfig)
mConfig.allowedKeyManagement.clear(); .setWpa2Passphrase(null).build();
mConfig.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE); when(mWifiManager.getSoftApConfiguration()).thenReturn(config);
mController.updateDisplay(); mController.updateDisplay();
assertThat(mController.getSecurityType()).isEqualTo(WifiConfiguration.KeyMgmt.NONE); assertThat(mController.getSecurityType()).isEqualTo(
SoftApConfiguration.SECURITY_TYPE_OPEN);
assertThat(mPreference.getSummary().toString()).isEqualTo("None"); assertThat(mPreference.getSummary().toString()).isEqualTo("None");
// test WPA2-Personal tether network // test WPA2-Personal tether network
mConfig.allowedKeyManagement.clear(); SoftApConfiguration config2 = new SoftApConfiguration.Builder(mConfig)
mConfig.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.WPA2_PSK); .setWpa2Passphrase("test_password").build();
when(mWifiManager.getSoftApConfiguration()).thenReturn(config2);
mController.updateDisplay(); mController.updateDisplay();
assertThat(mController.getSecurityType()).isEqualTo(WifiConfiguration.KeyMgmt.WPA2_PSK); assertThat(mController.getSecurityType()).isEqualTo(
SoftApConfiguration.SECURITY_TYPE_WPA2_PSK);
assertThat(mPreference.getSummary().toString()).isEqualTo("WPA2-Personal"); assertThat(mPreference.getSummary().toString()).isEqualTo("WPA2-Personal");
} }
} }