Merge "[Wi-Fi] Wrong MAC address shown in Settings."
This commit is contained in:
@@ -2370,8 +2370,10 @@
|
||||
<string name="wifi_advanced_titlebar">Advanced Wi\u2011Fi</string>
|
||||
<!-- Wi-Fi settings screen, advanced, title of the item to show the Wi-Fi device's SSID. [CHAR LIMIT=20] -->
|
||||
<string name="wifi_advanced_ssid_title">SSID</string>
|
||||
<!-- Wi-Fi settings screen, advanced, title of the item to show the Wi-Fi device's MAC address. -->
|
||||
<string name="wifi_advanced_mac_address_title">MAC address</string>
|
||||
<!-- Wi-Fi settings screen, advanced, title of the item to show the factory's Wi-Fi MAC address. [CHAR LIMIT=30] -->
|
||||
<string name="wifi_advanced_factory_mac_address_title">Factory MAC address</string>
|
||||
<!-- Wi-Fi settings screen, advanced, title of the item to show the randomized Wi-Fi MAC address. [CHAR LIMIT=30] -->
|
||||
<string name="wifi_advanced_randomized_mac_address_title">Randomized MAC address</string>
|
||||
<!-- Title of the screen to adjust IP settings -->
|
||||
<!-- Wi-Fi settings screen, advanced, title of the item to show the Wi-Fi device's current IP address. -->
|
||||
<string name="wifi_advanced_ip_address_title">IP address</string>
|
||||
@@ -3137,6 +3139,8 @@
|
||||
<string name="storage_settings_summary" product="default">Unmount SD card, view available storage</string>
|
||||
<!-- About phone screen, title for IMEI for multi-sim devices -->
|
||||
<string name="imei_multi_sim">IMEI (sim slot %1$d)</string>
|
||||
<!-- About phone screen, summary of the MAC address [CHAR LIMIT=80] -->
|
||||
<string name="view_saved_network">To view, choose saved network</string>
|
||||
<!-- Do not translate. About phone, status item title -->
|
||||
<string name="status_imei">IMEI</string>
|
||||
<!-- Do not translate. About phone, status item title -->
|
||||
@@ -3195,6 +3199,8 @@
|
||||
<string name="status_operator">Network</string>
|
||||
<!-- About phone, status item title. The MAC address of the Wi-Fi network adapter. -->
|
||||
<string name="status_wifi_mac_address">Wi\u2011Fi MAC address</string>
|
||||
<!-- About phone, status item title. The Factory MAC address of the Wi-Fi network adapter. [CHAR LIMIT=30] -->
|
||||
<string name="status_factory_wifi_mac_address">Factory Wi\u2011Fi MAC address</string>
|
||||
<!-- About phone, status item title. The bluetooth adapter's hardware address-->
|
||||
<string name="status_bt_address">Bluetooth address</string>
|
||||
<!-- About phone, status item title. The hardware serial number. [CHAR LIMIT=30]-->
|
||||
|
@@ -139,9 +139,18 @@
|
||||
|
||||
<!-- Wi-Fi MAC address -->
|
||||
<Preference
|
||||
android:key="wifi_mac_address"
|
||||
android:key="saved_accesspoints_wifi_mac_address"
|
||||
android:order="45"
|
||||
android:title="@string/status_wifi_mac_address"
|
||||
android:summary="@string/view_saved_network"
|
||||
android:fragment="com.android.settings.wifi.savedaccesspoints.SavedAccessPointsWifiSettings"
|
||||
settings:enableCopying="false"/>
|
||||
|
||||
<!-- Factory Wi-Fi MAC address -->
|
||||
<Preference
|
||||
android:key="wifi_mac_address"
|
||||
android:order="46"
|
||||
android:title="@string/status_factory_wifi_mac_address"
|
||||
android:summary="@string/summary_placeholder"
|
||||
android:selectable="false"
|
||||
settings:enableCopying="true"/>
|
||||
@@ -149,7 +158,7 @@
|
||||
<!-- Bluetooth address -->
|
||||
<Preference
|
||||
android:key="bt_address"
|
||||
android:order="46"
|
||||
android:order="47"
|
||||
android:title="@string/status_bt_address"
|
||||
android:summary="@string/summary_placeholder"
|
||||
android:selectable="false"
|
||||
@@ -158,7 +167,7 @@
|
||||
<!-- Device up time -->
|
||||
<Preference
|
||||
android:key="up_time"
|
||||
android:order="47"
|
||||
android:order="48"
|
||||
android:title="@string/status_up_time"
|
||||
android:summary="@string/summary_placeholder"
|
||||
android:selectable="false"/>
|
||||
|
@@ -69,12 +69,4 @@
|
||||
android:title="@string/wifi_menu_p2p"
|
||||
android:fragment="com.android.settings.wifi.p2p.WifiP2pSettings" />
|
||||
|
||||
<Preference
|
||||
android:key="mac_address"
|
||||
android:title="@string/wifi_advanced_mac_address_title" />
|
||||
|
||||
<Preference
|
||||
android:key="current_ip_address"
|
||||
android:title="@string/wifi_advanced_ip_address_title" />
|
||||
|
||||
</PreferenceScreen>
|
||||
|
@@ -87,7 +87,7 @@
|
||||
settings:enableCopying="true"/>
|
||||
<Preference
|
||||
android:key="mac_address"
|
||||
android:title="@string/wifi_advanced_mac_address_title"
|
||||
android:title="@string/wifi_advanced_randomized_mac_address_title"
|
||||
android:selectable="false"
|
||||
settings:enableCopying="true"/>
|
||||
<Preference
|
||||
|
@@ -20,8 +20,6 @@ import static android.content.Context.WIFI_SERVICE;
|
||||
import android.app.settings.SettingsEnums;
|
||||
import android.content.Context;
|
||||
import android.content.Intent;
|
||||
import android.net.ConnectivityManager;
|
||||
import android.net.NetworkInfo;
|
||||
import android.net.wifi.WifiManager;
|
||||
|
||||
import com.android.settings.R;
|
||||
@@ -39,7 +37,6 @@ public class ConfigureWifiSettings extends DashboardFragment {
|
||||
|
||||
private static final String TAG = "ConfigureWifiSettings";
|
||||
|
||||
public static final String KEY_IP_ADDRESS = "current_ip_address";
|
||||
public static final int WIFI_WAKEUP_REQUEST_CODE = 600;
|
||||
|
||||
private WifiWakeupPreferenceController mWifiWakeupPreferenceController;
|
||||
@@ -73,8 +70,6 @@ public class ConfigureWifiSettings extends DashboardFragment {
|
||||
protected List<AbstractPreferenceController> createPreferenceControllers(Context context) {
|
||||
final WifiManager wifiManager = (WifiManager) getSystemService(WIFI_SERVICE);
|
||||
final List<AbstractPreferenceController> controllers = new ArrayList<>();
|
||||
controllers.add(new WifiInfoPreferenceController(context, getSettingsLifecycle(),
|
||||
wifiManager));
|
||||
controllers.add(new WifiP2pPreferenceController(context, getSettingsLifecycle(),
|
||||
wifiManager));
|
||||
return controllers;
|
||||
@@ -107,24 +102,6 @@ public class ConfigureWifiSettings extends DashboardFragment {
|
||||
|
||||
public static final BaseSearchIndexProvider SEARCH_INDEX_DATA_PROVIDER =
|
||||
new BaseSearchIndexProvider(R.xml.wifi_configure_settings) {
|
||||
|
||||
@Override
|
||||
public List<String> getNonIndexableKeys(Context context) {
|
||||
List<String> keys = super.getNonIndexableKeys(context);
|
||||
|
||||
// If connected to WiFi, this IP address will be the same as the Status IP.
|
||||
// Or, if there is no connection they will say unavailable.
|
||||
ConnectivityManager cm = (ConnectivityManager)
|
||||
context.getSystemService(Context.CONNECTIVITY_SERVICE);
|
||||
NetworkInfo info = cm.getActiveNetworkInfo();
|
||||
if (info == null
|
||||
|| info.getType() == ConnectivityManager.TYPE_WIFI) {
|
||||
keys.add(KEY_IP_ADDRESS);
|
||||
}
|
||||
|
||||
return keys;
|
||||
}
|
||||
|
||||
protected boolean isPageSearchEnabled(Context context) {
|
||||
return context.getResources()
|
||||
.getBoolean(R.bool.config_show_wifi_settings);
|
||||
|
@@ -1,132 +0,0 @@
|
||||
/*
|
||||
* 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.WifiInfo;
|
||||
import android.net.wifi.WifiManager;
|
||||
import android.provider.Settings;
|
||||
import android.text.TextUtils;
|
||||
|
||||
import androidx.core.text.BidiFormatter;
|
||||
import androidx.preference.Preference;
|
||||
import androidx.preference.PreferenceScreen;
|
||||
|
||||
import com.android.settings.R;
|
||||
import com.android.settings.Utils;
|
||||
import com.android.settings.core.PreferenceControllerMixin;
|
||||
import com.android.settingslib.core.AbstractPreferenceController;
|
||||
import com.android.settingslib.core.lifecycle.Lifecycle;
|
||||
import com.android.settingslib.core.lifecycle.LifecycleObserver;
|
||||
import com.android.settingslib.core.lifecycle.events.OnPause;
|
||||
import com.android.settingslib.core.lifecycle.events.OnResume;
|
||||
|
||||
/**
|
||||
* {@link PreferenceControllerMixin} that updates MAC/IP address.
|
||||
*/
|
||||
public class WifiInfoPreferenceController extends AbstractPreferenceController
|
||||
implements PreferenceControllerMixin, LifecycleObserver, OnResume, OnPause {
|
||||
|
||||
private static final String KEY_CURRENT_IP_ADDRESS = "current_ip_address";
|
||||
private static final String KEY_MAC_ADDRESS = "mac_address";
|
||||
|
||||
private final IntentFilter mFilter;
|
||||
private final WifiManager mWifiManager;
|
||||
|
||||
private Preference mWifiMacAddressPref;
|
||||
private Preference mWifiIpAddressPref;
|
||||
|
||||
public WifiInfoPreferenceController(Context context, Lifecycle lifecycle,
|
||||
WifiManager wifiManager) {
|
||||
super(context);
|
||||
mWifiManager = wifiManager;
|
||||
mFilter = new IntentFilter();
|
||||
mFilter.addAction(WifiManager.LINK_CONFIGURATION_CHANGED_ACTION);
|
||||
mFilter.addAction(WifiManager.NETWORK_STATE_CHANGED_ACTION);
|
||||
|
||||
lifecycle.addObserver(this);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isAvailable() {
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getPreferenceKey() {
|
||||
// Returns null because this controller contains more than 1 preference.
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void displayPreference(PreferenceScreen screen) {
|
||||
super.displayPreference(screen);
|
||||
mWifiMacAddressPref = screen.findPreference(KEY_MAC_ADDRESS);
|
||||
mWifiMacAddressPref.setSelectable(false);
|
||||
mWifiIpAddressPref = screen.findPreference(KEY_CURRENT_IP_ADDRESS);
|
||||
mWifiIpAddressPref.setSelectable(false);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onResume() {
|
||||
mContext.registerReceiver(mReceiver, mFilter);
|
||||
updateWifiInfo();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onPause() {
|
||||
mContext.unregisterReceiver(mReceiver);
|
||||
}
|
||||
|
||||
public void updateWifiInfo() {
|
||||
if (mWifiMacAddressPref != null) {
|
||||
final WifiInfo wifiInfo = mWifiManager.getConnectionInfo();
|
||||
final boolean macRandomizationSupported = mContext.getResources().getBoolean(
|
||||
com.android.internal.R.bool.config_wifi_connected_mac_randomization_supported);
|
||||
final String macAddress = wifiInfo == null ? null : wifiInfo.getMacAddress();
|
||||
|
||||
if (macRandomizationSupported && WifiInfo.DEFAULT_MAC_ADDRESS.equals(macAddress)) {
|
||||
mWifiMacAddressPref.setSummary(R.string.wifi_status_mac_randomized);
|
||||
} else if (TextUtils.isEmpty(macAddress)
|
||||
|| WifiInfo.DEFAULT_MAC_ADDRESS.equals(macAddress)) {
|
||||
mWifiMacAddressPref.setSummary(R.string.status_unavailable);
|
||||
} else {
|
||||
mWifiMacAddressPref.setSummary(macAddress);
|
||||
}
|
||||
}
|
||||
if (mWifiIpAddressPref != null) {
|
||||
final String ipAddress = Utils.getWifiIpAddresses(mContext);
|
||||
mWifiIpAddressPref.setSummary(ipAddress == null
|
||||
? mContext.getString(R.string.status_unavailable)
|
||||
: BidiFormatter.getInstance().unicodeWrap(ipAddress));
|
||||
}
|
||||
}
|
||||
|
||||
private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
|
||||
@Override
|
||||
public void onReceive(Context context, Intent intent) {
|
||||
String action = intent.getAction();
|
||||
if (action.equals(WifiManager.LINK_CONFIGURATION_CHANGED_ACTION) ||
|
||||
action.equals(WifiManager.NETWORK_STATE_CHANGED_ACTION)) {
|
||||
updateWifiInfo();
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
@@ -687,6 +687,9 @@ public class WifiDetailPreferenceController extends AbstractPreferenceController
|
||||
} else {
|
||||
mMacAddressPref.setSummary(macAddress);
|
||||
}
|
||||
|
||||
// MAC Address Pref Title
|
||||
refreshMacTitle();
|
||||
}
|
||||
|
||||
private String getMacAddress() {
|
||||
@@ -1141,4 +1144,24 @@ public class WifiDetailPreferenceController extends AbstractPreferenceController
|
||||
mTimer.cancel();
|
||||
mTimer = null;
|
||||
}
|
||||
|
||||
private void refreshMacTitle() {
|
||||
if (mWifiConfig == null) {
|
||||
return;
|
||||
}
|
||||
|
||||
// For saved Passpoint network, framework doesn't have the field to keep the MAC choice
|
||||
// persistently, so Passpoint network will always use the default value so far, which is
|
||||
// randomized MAC address, so don't need to modify title.
|
||||
if (mAccessPoint.isPasspoint() || mAccessPoint.isPasspointConfig()) {
|
||||
return;
|
||||
}
|
||||
|
||||
mMacAddressPref.setTitle(
|
||||
(mWifiConfig.macRandomizationSetting
|
||||
== WifiConfiguration.RANDOMIZATION_PERSISTENT)
|
||||
? R.string.wifi_advanced_randomized_mac_address_title
|
||||
: R.string.wifi_advanced_factory_mac_address_title);
|
||||
|
||||
}
|
||||
}
|
||||
|
@@ -27,7 +27,6 @@ import androidx.preference.PreferenceGroup;
|
||||
import androidx.preference.PreferenceScreen;
|
||||
|
||||
import com.android.settings.core.BasePreferenceController;
|
||||
import com.android.settings.utils.PreferenceGroupChildrenCache;
|
||||
import com.android.settings.R;
|
||||
import com.android.settingslib.core.lifecycle.LifecycleObserver;
|
||||
import com.android.settingslib.core.lifecycle.events.OnStart;
|
||||
@@ -50,7 +49,6 @@ public class SavedAccessPointsPreferenceController extends BasePreferenceControl
|
||||
private static final String TAG = "SavedAPPrefCtrl";
|
||||
|
||||
private final WifiManager mWifiManager;
|
||||
private final PreferenceGroupChildrenCache mChildrenCache;
|
||||
|
||||
private final UserBadgeCache mUserBadgeCache;
|
||||
private PreferenceGroup mPreferenceGroup;
|
||||
@@ -61,7 +59,6 @@ public class SavedAccessPointsPreferenceController extends BasePreferenceControl
|
||||
super(context, preferenceKey);
|
||||
mUserBadgeCache = new AccessPointPreference.UserBadgeCache(context.getPackageManager());
|
||||
mWifiManager = context.getSystemService(WifiManager.class);
|
||||
mChildrenCache = new PreferenceGroupChildrenCache();
|
||||
}
|
||||
|
||||
public SavedAccessPointsPreferenceController setHost(SavedAccessPointsWifiSettings host) {
|
||||
@@ -91,8 +88,9 @@ public class SavedAccessPointsPreferenceController extends BasePreferenceControl
|
||||
|
||||
@Override
|
||||
public boolean onPreferenceClick(Preference preference) {
|
||||
final Preference preferenceInGroup = mPreferenceGroup.findPreference(preference.getKey());
|
||||
if (mHost != null) {
|
||||
mHost.showWifiPage((AccessPointPreference) preference);
|
||||
mHost.showWifiPage((AccessPointPreference) preferenceInGroup);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
@@ -118,7 +116,7 @@ public class SavedAccessPointsPreferenceController extends BasePreferenceControl
|
||||
final List<AccessPoint> accessPoints =
|
||||
WifiSavedConfigUtils.getAllConfigs(mContext, mWifiManager);
|
||||
Collections.sort(accessPoints, SavedNetworkComparator.INSTANCE);
|
||||
mChildrenCache.cacheRemoveAllPrefs(mPreferenceGroup);
|
||||
mPreferenceGroup.removeAll();
|
||||
|
||||
final int accessPointsSize = accessPoints.size();
|
||||
for (int i = 0; i < accessPointsSize; ++i) {
|
||||
@@ -130,20 +128,15 @@ public class SavedAccessPointsPreferenceController extends BasePreferenceControl
|
||||
}
|
||||
|
||||
String key = ap.getKey();
|
||||
AccessPointPreference preference =
|
||||
(AccessPointPreference) mChildrenCache.getCachedPreference(key);
|
||||
if (preference == null) {
|
||||
preference = new AccessPointPreference(ap, prefContext, mUserBadgeCache, true);
|
||||
preference.setKey(key);
|
||||
preference.setIcon(null);
|
||||
preference.setOnPreferenceClickListener(this);
|
||||
mPreferenceGroup.addPreference(preference);
|
||||
}
|
||||
AccessPointPreference preference = new AccessPointPreference(ap, prefContext,
|
||||
mUserBadgeCache, true);
|
||||
preference.setKey(key);
|
||||
preference.setIcon(null);
|
||||
preference.setOnPreferenceClickListener(this);
|
||||
mPreferenceGroup.addPreference(preference);
|
||||
preference.setOrder(i);
|
||||
}
|
||||
|
||||
mChildrenCache.removeCachedPrefs(mPreferenceGroup);
|
||||
|
||||
if (mPreferenceGroup.getPreferenceCount() < 1) {
|
||||
Log.w(TAG, "Saved networks activity loaded, but there are no saved networks!");
|
||||
mPreferenceGroup.setVisible(false);
|
||||
|
@@ -2,14 +2,9 @@ package com.android.settings.wifi;
|
||||
|
||||
import static com.google.common.truth.Truth.assertThat;
|
||||
|
||||
import static org.mockito.Mockito.doReturn;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.spy;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
import android.content.Context;
|
||||
import android.net.ConnectivityManager;
|
||||
import android.net.NetworkInfo;
|
||||
|
||||
import com.android.settings.testutils.XmlTestUtils;
|
||||
|
||||
@@ -54,41 +49,4 @@ public class ConfigureWifiSettingsTest {
|
||||
assertThat(keys).isNotNull();
|
||||
assertThat(niks).containsAllIn(keys);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testNonIndexableKeys_noConnection_blocksIP() {
|
||||
ConnectivityManager manager = mock(ConnectivityManager.class);
|
||||
when(manager.getActiveNetworkInfo()).thenReturn(null);
|
||||
doReturn(manager).when(mContext).getSystemService(Context.CONNECTIVITY_SERVICE);
|
||||
|
||||
final List<String> niks =
|
||||
ConfigureWifiSettings.SEARCH_INDEX_DATA_PROVIDER.getNonIndexableKeys(mContext);
|
||||
assertThat(niks).contains(ConfigureWifiSettings.KEY_IP_ADDRESS);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testNonIndexableKeys_wifiConnection_blocksIP() {
|
||||
ConnectivityManager manager = mock(ConnectivityManager.class);
|
||||
NetworkInfo info = mock(NetworkInfo.class);
|
||||
when(info.getType()).thenReturn(ConnectivityManager.TYPE_WIFI);
|
||||
when(manager.getActiveNetworkInfo()).thenReturn(info);
|
||||
doReturn(manager).when(mContext).getSystemService(Context.CONNECTIVITY_SERVICE);
|
||||
|
||||
final List<String> niks =
|
||||
ConfigureWifiSettings.SEARCH_INDEX_DATA_PROVIDER.getNonIndexableKeys(mContext);
|
||||
assertThat(niks).contains(ConfigureWifiSettings.KEY_IP_ADDRESS);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testNonIndexableKeys_mobileConnection_blocksIP() {
|
||||
ConnectivityManager manager = mock(ConnectivityManager.class);
|
||||
NetworkInfo info = mock(NetworkInfo.class);
|
||||
when(info.getType()).thenReturn(ConnectivityManager.TYPE_MOBILE);
|
||||
when(manager.getActiveNetworkInfo()).thenReturn(info);
|
||||
doReturn(manager).when(mContext).getSystemService(Context.CONNECTIVITY_SERVICE);
|
||||
|
||||
final List<String> niks =
|
||||
ConfigureWifiSettings.SEARCH_INDEX_DATA_PROVIDER.getNonIndexableKeys(mContext);
|
||||
assertThat(niks).doesNotContain(ConfigureWifiSettings.KEY_IP_ADDRESS);
|
||||
}
|
||||
}
|
||||
|
@@ -1,225 +0,0 @@
|
||||
/*
|
||||
* 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 static androidx.lifecycle.Lifecycle.Event.ON_PAUSE;
|
||||
import static androidx.lifecycle.Lifecycle.Event.ON_RESUME;
|
||||
|
||||
import static com.google.common.truth.Truth.assertThat;
|
||||
|
||||
import static org.mockito.ArgumentMatchers.any;
|
||||
import static org.mockito.ArgumentMatchers.anyString;
|
||||
import static org.mockito.Mockito.verify;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
import android.content.BroadcastReceiver;
|
||||
import android.content.Context;
|
||||
import android.content.IntentFilter;
|
||||
import android.content.res.Resources;
|
||||
import android.net.wifi.WifiInfo;
|
||||
import android.net.wifi.WifiManager;
|
||||
import android.provider.Settings;
|
||||
|
||||
import androidx.lifecycle.LifecycleOwner;
|
||||
import androidx.preference.Preference;
|
||||
import androidx.preference.PreferenceScreen;
|
||||
|
||||
import com.android.settings.R;
|
||||
import com.android.settingslib.core.lifecycle.Lifecycle;
|
||||
|
||||
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.RobolectricTestRunner;
|
||||
|
||||
@RunWith(RobolectricTestRunner.class)
|
||||
public class WifiInfoPreferenceControllerTest {
|
||||
|
||||
private static final String TEST_MAC_ADDRESS = "42:0a:23:43:ac:02";
|
||||
|
||||
@Mock
|
||||
private Context mContext;
|
||||
@Mock(answer = Answers.RETURNS_DEEP_STUBS)
|
||||
private WifiManager mWifiManager;
|
||||
@Mock
|
||||
private PreferenceScreen mScreen;
|
||||
@Mock
|
||||
private Preference mIpPreference;
|
||||
@Mock
|
||||
private Preference mMacPreference;
|
||||
@Mock
|
||||
private WifiInfo mWifiInfo;
|
||||
@Mock
|
||||
private Resources mResources;
|
||||
|
||||
private Lifecycle mLifecycle;
|
||||
private LifecycleOwner mLifecycleOwner;
|
||||
private WifiInfoPreferenceController mController;
|
||||
|
||||
@Before
|
||||
public void setUp() {
|
||||
MockitoAnnotations.initMocks(this);
|
||||
mLifecycleOwner = () -> mLifecycle;
|
||||
mLifecycle = new Lifecycle(mLifecycleOwner);
|
||||
when(mContext.getSystemService(WifiManager.class))
|
||||
.thenReturn(mWifiManager);
|
||||
when(mScreen.findPreference(anyString()))
|
||||
.thenReturn(mMacPreference)
|
||||
.thenReturn(mIpPreference);
|
||||
when(mWifiManager.getConnectionInfo()).thenReturn(mWifiInfo);
|
||||
when(mWifiManager.getCurrentNetwork()).thenReturn(null);
|
||||
when(mContext.getResources()).thenReturn(mResources);
|
||||
mController = new WifiInfoPreferenceController(mContext, mLifecycle, mWifiManager);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testIsAvailable_shouldAlwaysReturnTrue() {
|
||||
assertThat(mController.isAvailable()).isTrue();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void getPreferenceKey_shouldReturnNull() {
|
||||
assertThat(mController.getPreferenceKey()).isNull();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void runThroughLifecycle_shouldInstallListenerOnResume() {
|
||||
mLifecycle.handleLifecycleEvent(ON_RESUME);
|
||||
verify(mContext).registerReceiver(any(BroadcastReceiver.class), any(IntentFilter.class));
|
||||
|
||||
mLifecycle.handleLifecycleEvent(ON_PAUSE);
|
||||
verify(mContext).unregisterReceiver(any(BroadcastReceiver.class));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void onResume_shouldUpdateWifiInfo() {
|
||||
when(mWifiInfo.getMacAddress()).thenReturn(TEST_MAC_ADDRESS);
|
||||
|
||||
mController.displayPreference(mScreen);
|
||||
mController.onResume();
|
||||
|
||||
verify(mMacPreference).setSummary(TEST_MAC_ADDRESS);
|
||||
verify(mIpPreference).setSummary(any());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void updateWifiInfo_nullWifiInfoWithMacRandomizationOff_setMacUnavailable() {
|
||||
when(mResources.getBoolean(
|
||||
com.android.internal.R.bool.config_wifi_connected_mac_randomization_supported))
|
||||
.thenReturn(false);
|
||||
mController.displayPreference(mScreen);
|
||||
when(mWifiManager.getConnectionInfo()).thenReturn(null);
|
||||
|
||||
mController.updateWifiInfo();
|
||||
|
||||
verify(mMacPreference).setSummary(R.string.status_unavailable);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void updateWifiInfo_nullMacWithMacRandomizationOff_setMacUnavailable() {
|
||||
when(mResources.getBoolean(
|
||||
com.android.internal.R.bool.config_wifi_connected_mac_randomization_supported))
|
||||
.thenReturn(false);
|
||||
mController.displayPreference(mScreen);
|
||||
when(mWifiInfo.getMacAddress()).thenReturn(null);
|
||||
|
||||
mController.updateWifiInfo();
|
||||
|
||||
verify(mMacPreference).setSummary(R.string.status_unavailable);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void updateWifiInfo_defaultMacWithMacRandomizationOff_setMacUnavailable() {
|
||||
when(mResources.getBoolean(
|
||||
com.android.internal.R.bool.config_wifi_connected_mac_randomization_supported))
|
||||
.thenReturn(false);
|
||||
mController.displayPreference(mScreen);
|
||||
when(mWifiInfo.getMacAddress()).thenReturn(WifiInfo.DEFAULT_MAC_ADDRESS);
|
||||
|
||||
mController.updateWifiInfo();
|
||||
|
||||
verify(mMacPreference).setSummary(R.string.status_unavailable);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void updateWifiInfo_validMacWithMacRandomizationOff_setValidMac() {
|
||||
when(mResources.getBoolean(
|
||||
com.android.internal.R.bool.config_wifi_connected_mac_randomization_supported))
|
||||
.thenReturn(false);
|
||||
mController.displayPreference(mScreen);
|
||||
when(mWifiInfo.getMacAddress()).thenReturn(TEST_MAC_ADDRESS);
|
||||
|
||||
mController.updateWifiInfo();
|
||||
|
||||
verify(mMacPreference).setSummary(TEST_MAC_ADDRESS);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void updateWifiInfo_nullWifiInfoWithMacRandomizationOn_setMacUnavailable() {
|
||||
when(mResources.getBoolean(
|
||||
com.android.internal.R.bool.config_wifi_connected_mac_randomization_supported))
|
||||
.thenReturn(true);
|
||||
mController.displayPreference(mScreen);
|
||||
when(mWifiManager.getConnectionInfo()).thenReturn(null);
|
||||
|
||||
mController.updateWifiInfo();
|
||||
|
||||
verify(mMacPreference).setSummary(R.string.status_unavailable);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void updateWifiInfo_nullMacWithMacRandomizationOn_setMacUnavailable() {
|
||||
when(mResources.getBoolean(
|
||||
com.android.internal.R.bool.config_wifi_connected_mac_randomization_supported))
|
||||
.thenReturn(true);
|
||||
mController.displayPreference(mScreen);
|
||||
when(mWifiInfo.getMacAddress()).thenReturn(null);
|
||||
|
||||
mController.updateWifiInfo();
|
||||
|
||||
verify(mMacPreference).setSummary(R.string.status_unavailable);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void updateWifiInfo_defaultMacWithMacRandomizationOn_setMacRandomized() {
|
||||
when(mResources.getBoolean(
|
||||
com.android.internal.R.bool.config_wifi_connected_mac_randomization_supported))
|
||||
.thenReturn(true);
|
||||
mController.displayPreference(mScreen);
|
||||
when(mWifiInfo.getMacAddress()).thenReturn(WifiInfo.DEFAULT_MAC_ADDRESS);
|
||||
|
||||
mController.updateWifiInfo();
|
||||
|
||||
verify(mMacPreference).setSummary(R.string.wifi_status_mac_randomized);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void updateWifiInfo_validMacWithMacRandomizationOn_setValidMac() {
|
||||
when(mResources.getBoolean(
|
||||
com.android.internal.R.bool.config_wifi_connected_mac_randomization_supported))
|
||||
.thenReturn(true);
|
||||
mController.displayPreference(mScreen);
|
||||
when(mWifiInfo.getMacAddress()).thenReturn(TEST_MAC_ADDRESS);
|
||||
|
||||
mController.updateWifiInfo();
|
||||
|
||||
verify(mMacPreference).setSummary(TEST_MAC_ADDRESS);
|
||||
}
|
||||
}
|
@@ -1845,6 +1845,31 @@ public class WifiDetailPreferenceControllerTest {
|
||||
assertThat(icon).isNotNull();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void checkMacTitle_whenPrivacyRandomizedMac_shouldBeRandom() {
|
||||
setUpForDisconnectedNetwork();
|
||||
mockWifiConfig.macRandomizationSetting = WifiConfiguration.RANDOMIZATION_PERSISTENT;
|
||||
when(mockWifiConfig.getRandomizedMacAddress()).thenReturn(mockMacAddress);
|
||||
when(mockMacAddress.toString()).thenReturn(RANDOMIZED_MAC_ADDRESS);
|
||||
|
||||
displayAndResume();
|
||||
|
||||
verify(mockMacAddressPref).setTitle(R.string.wifi_advanced_randomized_mac_address_title);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void checkMacTitle_whenPrivacyDeviceMac_shouldBeFactory() {
|
||||
setUpForDisconnectedNetwork();
|
||||
mockWifiConfig.macRandomizationSetting = WifiConfiguration.RANDOMIZATION_NONE;
|
||||
when(mockWifiConfig.getRandomizedMacAddress()).thenReturn(mockMacAddress);
|
||||
when(mockWifiManager.getFactoryMacAddresses())
|
||||
.thenReturn(new String[]{FACTORY_MAC_ADDRESS});
|
||||
|
||||
displayAndResume();
|
||||
|
||||
verify(mockMacAddressPref).setTitle(R.string.wifi_advanced_factory_mac_address_title);
|
||||
}
|
||||
|
||||
private ActionButtonsPreference createMock() {
|
||||
final ActionButtonsPreference pref = mock(ActionButtonsPreference.class);
|
||||
when(pref.setButton1Text(anyInt())).thenReturn(pref);
|
||||
|
Reference in New Issue
Block a user