Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 2f257a19 authored by Quang Luong's avatar Quang Luong Committed by Android (Google) Code Review
Browse files

Merge "Fix mac randomization value mismatch" into main

parents e980a145 47a83c7e
Loading
Loading
Loading
Loading
+2 −2
Original line number Diff line number Diff line
@@ -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());
        }

+10 −9
Original line number Diff line number Diff line
@@ -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]);
    }
}
+12 −13
Original line number Diff line number Diff line
@@ -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);

+2 −2
Original line number Diff line number Diff line
@@ -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])