summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorQuang Luong <qal@google.com>2020-07-30 14:43:58 -0700
committerQuang Luong <qal@google.com>2020-08-07 23:22:17 +0000
commit65779e054a210b106029807204f12ff3ceda9d04 (patch)
tree12de1b9ccef19287c5d92e5052b512d5b8e54d4c
parent058fc1de39ed8c11473bf9d60aac92dbf21106c0 (diff)
[WifiTrackerLib] Use WifiInfo MAC address if available
Always use the mac address from WifiInfo if available so that we are always displaying the actual mac address of the connection instead of the address stored in the wificonfig, in case enhanced mac randomization is enabled. Bug: 160028657 Test: atest WifiTrackerLibTests Change-Id: I403957b2ba5a845ed5ddc51fe8b39a087f29456a (cherry picked from commit 6fd689db23cb2974fa1078d476fe94aa127cec24)
-rw-r--r--libs/WifiTrackerLib/src/com/android/wifitrackerlib/PasspointWifiEntry.java14
-rw-r--r--libs/WifiTrackerLib/src/com/android/wifitrackerlib/StandardWifiEntry.java14
-rw-r--r--libs/WifiTrackerLib/tests/src/com/android/wifitrackerlib/PasspointWifiEntryTest.java25
-rw-r--r--libs/WifiTrackerLib/tests/src/com/android/wifitrackerlib/StandardWifiEntryTest.java26
4 files changed, 71 insertions, 8 deletions
diff --git a/libs/WifiTrackerLib/src/com/android/wifitrackerlib/PasspointWifiEntry.java b/libs/WifiTrackerLib/src/com/android/wifitrackerlib/PasspointWifiEntry.java
index d873c25e5..71420b9ad 100644
--- a/libs/WifiTrackerLib/src/com/android/wifitrackerlib/PasspointWifiEntry.java
+++ b/libs/WifiTrackerLib/src/com/android/wifitrackerlib/PasspointWifiEntry.java
@@ -16,6 +16,7 @@
package com.android.wifitrackerlib;
+import static android.net.wifi.WifiInfo.DEFAULT_MAC_ADDRESS;
import static android.net.wifi.WifiInfo.sanitizeSsid;
import static androidx.core.util.Preconditions.checkNotNull;
@@ -277,16 +278,21 @@ public class PasspointWifiEntry extends WifiEntry implements WifiEntry.WifiEntry
@Override
public String getMacAddress() {
+ if (mWifiInfo != null) {
+ final String wifiInfoMac = mWifiInfo.getMacAddress();
+ if (!TextUtils.isEmpty(wifiInfoMac)
+ && !TextUtils.equals(wifiInfoMac, DEFAULT_MAC_ADDRESS)) {
+ return wifiInfoMac;
+ }
+ }
if (mWifiConfig == null || getPrivacy() != PRIVACY_RANDOMIZED_MAC) {
final String[] factoryMacs = mWifiManager.getFactoryMacAddresses();
if (factoryMacs.length > 0) {
return factoryMacs[0];
- } else {
- return null;
}
- } else {
- return mWifiConfig.getRandomizedMacAddress().toString();
+ return null;
}
+ return mWifiConfig.getRandomizedMacAddress().toString();
}
@Override
diff --git a/libs/WifiTrackerLib/src/com/android/wifitrackerlib/StandardWifiEntry.java b/libs/WifiTrackerLib/src/com/android/wifitrackerlib/StandardWifiEntry.java
index 6fef5e961..56cd16503 100644
--- a/libs/WifiTrackerLib/src/com/android/wifitrackerlib/StandardWifiEntry.java
+++ b/libs/WifiTrackerLib/src/com/android/wifitrackerlib/StandardWifiEntry.java
@@ -20,6 +20,7 @@ import static android.net.wifi.WifiConfiguration.NetworkSelectionStatus.DISABLED
import static android.net.wifi.WifiConfiguration.NetworkSelectionStatus.DISABLED_AUTHENTICATION_NO_CREDENTIALS;
import static android.net.wifi.WifiConfiguration.NetworkSelectionStatus.DISABLED_BY_WRONG_PASSWORD;
import static android.net.wifi.WifiConfiguration.NetworkSelectionStatus.NETWORK_SELECTION_ENABLED;
+import static android.net.wifi.WifiInfo.DEFAULT_MAC_ADDRESS;
import static android.net.wifi.WifiInfo.sanitizeSsid;
import static androidx.core.util.Preconditions.checkNotNull;
@@ -312,16 +313,21 @@ public class StandardWifiEntry extends WifiEntry {
@Override
public String getMacAddress() {
+ if (mWifiInfo != null) {
+ final String wifiInfoMac = mWifiInfo.getMacAddress();
+ if (!TextUtils.isEmpty(wifiInfoMac)
+ && !TextUtils.equals(wifiInfoMac, DEFAULT_MAC_ADDRESS)) {
+ return wifiInfoMac;
+ }
+ }
if (mWifiConfig == null || getPrivacy() != PRIVACY_RANDOMIZED_MAC) {
final String[] factoryMacs = mWifiManager.getFactoryMacAddresses();
if (factoryMacs.length > 0) {
return factoryMacs[0];
- } else {
- return null;
}
- } else {
- return mWifiConfig.getRandomizedMacAddress().toString();
+ return null;
}
+ return mWifiConfig.getRandomizedMacAddress().toString();
}
@Override
diff --git a/libs/WifiTrackerLib/tests/src/com/android/wifitrackerlib/PasspointWifiEntryTest.java b/libs/WifiTrackerLib/tests/src/com/android/wifitrackerlib/PasspointWifiEntryTest.java
index 80a31e53f..fa7812fe6 100644
--- a/libs/WifiTrackerLib/tests/src/com/android/wifitrackerlib/PasspointWifiEntryTest.java
+++ b/libs/WifiTrackerLib/tests/src/com/android/wifitrackerlib/PasspointWifiEntryTest.java
@@ -221,4 +221,29 @@ public class PasspointWifiEntryTest {
assertThat(entry.getSpeed()).isEqualTo(SPEED_SLOW);
}
+
+ @Test
+ public void testGetMacAddress_wifiInfoAvailable_usesWifiInfoMacAddress() {
+ final String factoryMac = "01:23:45:67:89:ab";
+ final String wifiInfoMac = "11:23:45:67:89:ab";
+ final WifiConfiguration config = new WifiConfiguration();
+ config.SSID = "\"ssid\"";
+ config.macRandomizationSetting = WifiConfiguration.RANDOMIZATION_NONE;
+ config.FQDN = FQDN;
+ when(mMockWifiManager.getFactoryMacAddresses()).thenReturn(new String[]{factoryMac});
+ WifiInfo wifiInfo = mock(WifiInfo.class);
+ when(wifiInfo.isPasspointAp()).thenReturn(true);
+ when(wifiInfo.getPasspointFqdn()).thenReturn(FQDN);
+ when(wifiInfo.getMacAddress()).thenReturn(wifiInfoMac);
+ NetworkInfo networkInfo =
+ new NetworkInfo(ConnectivityManager.TYPE_WIFI, 0 /* subtype */, "WIFI", "");
+ networkInfo.setDetailedState(NetworkInfo.DetailedState.CONNECTED, "", "");
+ PasspointWifiEntry entry = new PasspointWifiEntry(mMockContext, mTestHandler,
+ getPasspointConfiguration(), mMockWifiManager, mMockScoreCache,
+ false /* forSavedNetworksPage */);
+
+ entry.updateConnectionInfo(wifiInfo, networkInfo);
+
+ assertThat(entry.getMacAddress()).isEqualTo(wifiInfoMac);
+ }
}
diff --git a/libs/WifiTrackerLib/tests/src/com/android/wifitrackerlib/StandardWifiEntryTest.java b/libs/WifiTrackerLib/tests/src/com/android/wifitrackerlib/StandardWifiEntryTest.java
index ba78b3fa5..81b20348d 100644
--- a/libs/WifiTrackerLib/tests/src/com/android/wifitrackerlib/StandardWifiEntryTest.java
+++ b/libs/WifiTrackerLib/tests/src/com/android/wifitrackerlib/StandardWifiEntryTest.java
@@ -479,6 +479,32 @@ public class StandardWifiEntryTest {
}
@Test
+ public void testGetMacAddress_wifiInfoAvailable_usesWifiInfoMacAddress() {
+ final int networkId = 1;
+ final String factoryMac = "01:23:45:67:89:ab";
+ final String wifiInfoMac = "11:23:45:67:89:ab";
+
+ final NetworkInfo networkInfo =
+ new NetworkInfo(ConnectivityManager.TYPE_WIFI, 0 /* subtype */, "WIFI", "");
+ networkInfo.setDetailedState(NetworkInfo.DetailedState.CONNECTED, "", "");
+
+ when(mMockWifiInfo.getNetworkId()).thenReturn(networkId);
+ when(mMockWifiInfo.getMacAddress()).thenReturn(wifiInfoMac);
+ final WifiConfiguration config = new WifiConfiguration();
+ config.SSID = "\"ssid\"";
+ config.networkId = networkId;
+ config.macRandomizationSetting = WifiConfiguration.RANDOMIZATION_NONE;
+ when(mMockWifiManager.getFactoryMacAddresses()).thenReturn(new String[]{factoryMac});
+ final StandardWifiEntry entry = new StandardWifiEntry(mMockContext, mTestHandler,
+ ssidAndSecurityToStandardWifiEntryKey("ssid", SECURITY_NONE), config,
+ mMockWifiManager, mMockScoreCache, false /* forSavedNetworksPage */);
+
+ entry.updateConnectionInfo(mMockWifiInfo, networkInfo);
+
+ assertThat(entry.getMacAddress()).isEqualTo(wifiInfoMac);
+ }
+
+ @Test
public void testCanShare_securityCanShare_shouldReturnTrue() {
final StandardWifiEntry pskWifiEntry =
getSavedStandardWifiEntry(WifiConfiguration.SECURITY_TYPE_PSK);