From 47a83c7ed72e62f8e5a62f59b2c88118c1d8eb78 Mon Sep 17 00:00:00 2001 From: Quang Anh Luong Date: Thu, 18 Jul 2024 14:25:15 +0900 Subject: [PATCH] Fix mac randomization value mismatch WifiConfigController2 passes in WifiConfiguration.MacRandomizationSetting to methods that expect WifiEntry.Privacy enums, resulting in the wrong values being saved and displayed for the edit dialog. Fix this by making sure the correct WifiEntry values are used. Note that the Network Details Page uses WifiPrivacyPreferenceController2, which uses the correct values. Flag: EXEMPT bugfix Bug: 284230986 Test: atest WifiPrivacyPreferenceController2Test, atest WifiConfigController2Test, manually verify adding a new network saves the config with RANDOMIZATION_AUTO by default and changing the value works correctly. Change-Id: I84dc7cc50d04360aca611ca80ee400a97b693722 --- .../settings/wifi/WifiConfigController2.java | 4 +-- .../WifiPrivacyPreferenceController2.java | 19 +++++++------- .../wifi/WifiConfigController2Test.java | 25 +++++++++---------- .../WifiPrivacyPreferenceController2Test.kt | 4 +-- 4 files changed, 26 insertions(+), 26 deletions(-) diff --git a/src/com/android/settings/wifi/WifiConfigController2.java b/src/com/android/settings/wifi/WifiConfigController2.java index 70e08eb9033..1ea0103c28e 100644 --- a/src/com/android/settings/wifi/WifiConfigController2.java +++ b/src/com/android/settings/wifi/WifiConfigController2.java @@ -344,7 +344,7 @@ public class WifiConfigController2 implements TextWatcher, if (mPrivacySettingsSpinner != null) { final int prefMacValue = WifiPrivacyPreferenceController2 - .translateMacRandomizedValueToPrefValue(config.macRandomizationSetting); + .translateWifiEntryPrivacyToPrefValue(mWifiEntry.getPrivacy()); mPrivacySettingsSpinner.setSelection(prefMacValue); } @@ -863,7 +863,7 @@ public class WifiConfigController2 implements TextWatcher, if (mPrivacySettingsSpinner != null) { config.macRandomizationSetting = WifiPrivacyPreferenceController2 - .translatePrefValueToMacRandomizedValue(mPrivacySettingsSpinner + .translatePrefValueToWifiConfigSetting(mPrivacySettingsSpinner .getSelectedItemPosition()); } diff --git a/src/com/android/settings/wifi/details2/WifiPrivacyPreferenceController2.java b/src/com/android/settings/wifi/details2/WifiPrivacyPreferenceController2.java index 5d393e54a21..0c67c04622e 100644 --- a/src/com/android/settings/wifi/details2/WifiPrivacyPreferenceController2.java +++ b/src/com/android/settings/wifi/details2/WifiPrivacyPreferenceController2.java @@ -17,6 +17,7 @@ package com.android.settings.wifi.details2; import android.content.Context; +import android.net.wifi.WifiConfiguration; import android.net.wifi.WifiManager; import androidx.annotation.NonNull; @@ -98,30 +99,30 @@ public class WifiPrivacyPreferenceController2 extends BasePreferenceController i private static final int PREF_RANDOMIZATION_NONE = 1; /** - * Returns preference index value. + * Translates a WifiEntry.Privacy value to the matching preference index value. * - * @param macRandomized is mac randomized value + * @param privacy WifiEntry.Privacy value * @return index value of preference */ - public static int translateMacRandomizedValueToPrefValue(int macRandomized) { - return (macRandomized == WifiEntry.PRIVACY_RANDOMIZED_MAC) + public static int translateWifiEntryPrivacyToPrefValue(@WifiEntry.Privacy int privacy) { + return (privacy == WifiEntry.PRIVACY_RANDOMIZED_MAC) ? PREF_RANDOMIZATION_PERSISTENT : PREF_RANDOMIZATION_NONE; } /** - * Returns mac randomized value. + * Translates the pref value to WifiConfiguration.MacRandomizationSetting value * * @param prefMacRandomized is preference index value - * @return mac randomized value + * @return WifiConfiguration.MacRandomizationSetting value */ - public static int translatePrefValueToMacRandomizedValue(int prefMacRandomized) { + public static int translatePrefValueToWifiConfigSetting(int prefMacRandomized) { return (prefMacRandomized == PREF_RANDOMIZATION_PERSISTENT) - ? WifiEntry.PRIVACY_RANDOMIZED_MAC : WifiEntry.PRIVACY_DEVICE_MAC; + ? WifiConfiguration.RANDOMIZATION_AUTO : WifiConfiguration.RANDOMIZATION_NONE; } private void updateSummary(ListPreference preference, int macRandomized) { // Translates value here to set RANDOMIZATION_PERSISTENT as first item in UI for better UX. - final int prefMacRandomized = translateMacRandomizedValueToPrefValue(macRandomized); + final int prefMacRandomized = translateWifiEntryPrivacyToPrefValue(macRandomized); preference.setSummary(preference.getEntries()[prefMacRandomized]); } } diff --git a/tests/robotests/src/com/android/settings/wifi/WifiConfigController2Test.java b/tests/robotests/src/com/android/settings/wifi/WifiConfigController2Test.java index 7d96496282e..d985ee5b9d3 100644 --- a/tests/robotests/src/com/android/settings/wifi/WifiConfigController2Test.java +++ b/tests/robotests/src/com/android/settings/wifi/WifiConfigController2Test.java @@ -459,46 +459,45 @@ public class WifiConfigController2Test { public void loadMacRandomizedValue_shouldPersistentAsDefault() { final Spinner privacySetting = mView.findViewById(R.id.privacy_settings); final int prefPersist = - WifiPrivacyPreferenceController2.translateMacRandomizedValueToPrefValue( - WifiConfiguration.RANDOMIZATION_PERSISTENT); + WifiPrivacyPreferenceController2.translateWifiEntryPrivacyToPrefValue( + WifiEntry.PRIVACY_RANDOMIZED_MAC); assertThat(privacySetting.getVisibility()).isEqualTo(View.VISIBLE); assertThat(privacySetting.getSelectedItemPosition()).isEqualTo(prefPersist); } @Test - public void loadSavedMacRandomizedPersistentValue_shouldCorrectMacValue() { - checkSavedMacRandomizedValue(WifiConfiguration.RANDOMIZATION_PERSISTENT); + public void loadSavedPrivacyRandomizedMacValue_shouldCorrectMacValue() { + checkSavedMacRandomizedValue(WifiEntry.PRIVACY_RANDOMIZED_MAC); } @Test - public void loadSavedMacRandomizedNoneValue_shouldCorrectMacValue() { - checkSavedMacRandomizedValue(WifiConfiguration.RANDOMIZATION_NONE); + public void loadSavedPrivacyDeviceMacValue_shouldCorrectMacValue() { + checkSavedMacRandomizedValue(WifiEntry.PRIVACY_DEVICE_MAC); } - private void checkSavedMacRandomizedValue(int macRandomizedValue) { + private void checkSavedMacRandomizedValue(@WifiEntry.Privacy int privacy) { when(mWifiEntry.isSaved()).thenReturn(true); final WifiConfiguration mockWifiConfig = spy(new WifiConfiguration()); when(mockWifiConfig.getIpConfiguration()).thenReturn(mock(IpConfiguration.class)); when(mWifiEntry.getWifiConfiguration()).thenReturn(mockWifiConfig); - mockWifiConfig.macRandomizationSetting = macRandomizedValue; + when(mWifiEntry.getPrivacy()).thenReturn(privacy); createController(mWifiEntry, WifiConfigUiBase2.MODE_CONNECT, false); final Spinner privacySetting = mView.findViewById(R.id.privacy_settings); final int expectedPrefValue = - WifiPrivacyPreferenceController2.translateMacRandomizedValueToPrefValue( - macRandomizedValue); + WifiPrivacyPreferenceController2.translateWifiEntryPrivacyToPrefValue(privacy); assertThat(privacySetting.getVisibility()).isEqualTo(View.VISIBLE); assertThat(privacySetting.getSelectedItemPosition()).isEqualTo(expectedPrefValue); } @Test - public void saveMacRandomizedValue_noChanged_shouldPersistentAsDefault() { + public void saveMacRandomizedValue_noChanged_shouldAutoAsDefault() { createController(mWifiEntry, WifiConfigUiBase2.MODE_CONNECT, false); WifiConfiguration config = mController.getConfig(); assertThat(config.macRandomizationSetting).isEqualTo( - WifiConfiguration.RANDOMIZATION_PERSISTENT); + WifiConfiguration.RANDOMIZATION_AUTO); } @Test @@ -506,7 +505,7 @@ public class WifiConfigController2Test { createController(mWifiEntry, WifiConfigUiBase2.MODE_CONNECT, false); final Spinner privacySetting = mView.findViewById(R.id.privacy_settings); final int prefMacNone = - WifiPrivacyPreferenceController2.translateMacRandomizedValueToPrefValue( + WifiPrivacyPreferenceController2.translateWifiEntryPrivacyToPrefValue( WifiConfiguration.RANDOMIZATION_NONE); privacySetting.setSelection(prefMacNone); diff --git a/tests/spa_unit/src/com/android/settings/wifi/details2/WifiPrivacyPreferenceController2Test.kt b/tests/spa_unit/src/com/android/settings/wifi/details2/WifiPrivacyPreferenceController2Test.kt index cb1f997f4e5..9260409af37 100644 --- a/tests/spa_unit/src/com/android/settings/wifi/details2/WifiPrivacyPreferenceController2Test.kt +++ b/tests/spa_unit/src/com/android/settings/wifi/details2/WifiPrivacyPreferenceController2Test.kt @@ -59,7 +59,7 @@ class WifiPrivacyPreferenceController2Test { controller.updateState(preference) - val prefValue = WifiPrivacyPreferenceController2.translateMacRandomizedValueToPrefValue( + val prefValue = WifiPrivacyPreferenceController2.translateWifiEntryPrivacyToPrefValue( WifiEntry.PRIVACY_DEVICE_MAC ) assertThat(preference.entry).isEqualTo(preferenceStrings[prefValue]) @@ -73,7 +73,7 @@ class WifiPrivacyPreferenceController2Test { controller.updateState(preference) - val prefValue = WifiPrivacyPreferenceController2.translateMacRandomizedValueToPrefValue( + val prefValue = WifiPrivacyPreferenceController2.translateWifiEntryPrivacyToPrefValue( WifiEntry.PRIVACY_RANDOMIZED_MAC ) assertThat(preference.entry).isEqualTo(preferenceStrings[prefValue])