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

Commit 59482597 authored by TreeHugger Robot's avatar TreeHugger Robot Committed by Android (Google) Code Review
Browse files

Merge "[WPA3] Filter unsupported networks from Add network spinner"

parents abc37ee8 16381683
Loading
Loading
Loading
Loading
+3 −2
Original line number Diff line number Diff line
@@ -83,12 +83,13 @@
                        style="@style/wifi_item_label"
                        android:text="@string/wifi_security" />

                <!-- Entries are added dynamically to this spinner -->
                <!-- See WifiConfigController.configureSecuritySpinner -->
                <Spinner android:id="@+id/security"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        style="@style/wifi_item_spinner"
                        android:prompt="@string/wifi_security"
                        android:entries="@array/wifi_security" />
                        android:prompt="@string/wifi_security" />
            </LinearLayout>
        </LinearLayout>

+0 −23
Original line number Diff line number Diff line
@@ -210,29 +210,6 @@
        <item>Temporarily avoiding poor connection</item>
    </string-array>

    <!-- Match this with the constants in AccessPoint. --> <skip />
    <!-- Wi-Fi security choices used when manually added a Wi-Fi network -->
    <string-array name="wifi_security">
        <!-- The Wi-Fi network does not have any security. -->
        <item>@string/wifi_security_none</item>
        <item translatable="false">@string/wifi_security_wep</item>
        <item translatable="false">@string/wifi_security_psk_generic</item>
        <item translatable="false">@string/wifi_security_eap</item>
        <item translatable="false">@string/wifi_security_owe</item>
        <item translatable="false">@string/wifi_security_sae</item>
        <item translatable="false">@string/wifi_security_eap_suiteb</item>
    </string-array>

    <!-- Match this with the constants in AccessPoint. --> <skip />
    <!-- Wi-Fi security types for New User Dialog. EAP is not configurable. -->
    <string-array name="wifi_security_no_eap">
        <!-- The Wi-Fi network does not have any security. -->
        <item>@string/wifi_security_none</item>
        <item translatable="false">@string/wifi_security_wep</item>
        <item translatable="false">@string/wifi_security_psk_generic</item>
        <item translatable="false">@string/wifi_security_sae</item>
    </string-array>

    <!-- Security types for wireless tether -->
    <string-array name="wifi_tether_security">
        <!-- Do not translate. -->
+80 −20
Original line number Diff line number Diff line
@@ -34,6 +34,7 @@ import android.net.wifi.WifiEnterpriseConfig;
import android.net.wifi.WifiEnterpriseConfig.Eap;
import android.net.wifi.WifiEnterpriseConfig.Phase2;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;
import android.os.UserManager;
import android.security.Credentials;
import android.security.KeyStore;
@@ -119,9 +120,9 @@ public class WifiConfigController implements TextWatcher,


    /* Phase2 methods supported by PEAP are limited */
    private final ArrayAdapter<String> mPhase2PeapAdapter;
    private ArrayAdapter<String> mPhase2PeapAdapter;
    /* Full list of phase2 methods */
    private final ArrayAdapter<String> mPhase2FullAdapter;
    private ArrayAdapter<String> mPhase2FullAdapter;

    // e.g. AccessPoint.SECURITY_NONE
    @VisibleForTesting
@@ -174,6 +175,9 @@ public class WifiConfigController implements TextWatcher,
    private TextView mSsidView;

    private Context mContext;
    private Integer mSecurityInPosition[];

    private final WifiManager mWifiManager;

    public WifiConfigController(WifiConfigUiBase parent, View view, AccessPoint accessPoint,
            int mode) {
@@ -181,11 +185,31 @@ public class WifiConfigController implements TextWatcher,

        mView = view;
        mAccessPoint = accessPoint;
        mContext = mConfigUi.getContext();

        // Init Wi-Fi manager
        mWifiManager = (WifiManager) mContext.getSystemService(Context.WIFI_SERVICE);
        initWifiConfigController(accessPoint, mode);
    }

    @VisibleForTesting
    public WifiConfigController(WifiConfigUiBase parent, View view, AccessPoint accessPoint,
            int mode, WifiManager wifiManager) {
        mConfigUi = parent;

        mView = view;
        mAccessPoint = accessPoint;
        mContext = mConfigUi.getContext();
        mWifiManager = wifiManager;
        initWifiConfigController(accessPoint, mode);
    }

    private void initWifiConfigController(AccessPoint accessPoint, int mode) {

        mAccessPointSecurity = (accessPoint == null) ? AccessPoint.SECURITY_NONE :
                accessPoint.getSecurity();
        mMode = mode;

        mContext = mConfigUi.getContext();
        final Resources res = mContext.getResources();

        mLevels = res.getStringArray(R.array.wifi_signal);
@@ -234,24 +258,10 @@ public class WifiConfigController implements TextWatcher,
                mHiddenSettingsSpinner.getSelectedItemPosition() == NOT_HIDDEN_NETWORK
                        ? View.GONE
                        : View.VISIBLE);
        mSecurityInPosition = new Integer[AccessPoint.SECURITY_MAX_VAL];

        if (mAccessPoint == null) { // new network
            mConfigUi.setTitle(R.string.wifi_add_network);

            mSsidView = (TextView) mView.findViewById(R.id.ssid);
            mSsidView.addTextChangedListener(this);
            mSecuritySpinner = ((Spinner) mView.findViewById(R.id.security));
            mSecuritySpinner.setOnItemSelectedListener(this);
            mView.findViewById(R.id.type).setVisibility(View.VISIBLE);

            showIpConfigFields();
            showProxyFields();
            mView.findViewById(R.id.wifi_advanced_toggle).setVisibility(View.VISIBLE);
            // Hidden option can be changed only when the user adds a network manually.
            mView.findViewById(R.id.hidden_settings_field).setVisibility(View.VISIBLE);
            ((CheckBox) mView.findViewById(R.id.wifi_advanced_togglebox))
                    .setOnCheckedChangeListener(this);

            configureSecuritySpinner();
            mConfigUi.setSubmitButton(res.getString(R.string.wifi_save));
        } else {
            if (!mAccessPoint.isPasspointConfig()) {
@@ -1414,7 +1424,8 @@ public class WifiConfigController implements TextWatcher,
    @Override
    public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
        if (parent == mSecuritySpinner) {
            mAccessPointSecurity = position;
            // Convert menu position to actual Wi-Fi security type
            mAccessPointSecurity = mSecurityInPosition[position];
            showSecurityFields();
        } else if (parent == mEapMethodSpinner || parent == mEapCaCertSpinner) {
            showSecurityFields();
@@ -1459,4 +1470,53 @@ public class WifiConfigController implements TextWatcher,
    public AccessPoint getAccessPoint() {
        return mAccessPoint;
    }

    private void configureSecuritySpinner() {
        mConfigUi.setTitle(R.string.wifi_add_network);

        mSsidView = (TextView) mView.findViewById(R.id.ssid);
        mSsidView.addTextChangedListener(this);
        mSecuritySpinner = ((Spinner) mView.findViewById(R.id.security));
        mSecuritySpinner.setOnItemSelectedListener(this);

        ArrayAdapter<String> spinnerAdapter = new ArrayAdapter<String>(mContext,
                android.R.layout.simple_spinner_item, android.R.id.text1);
        spinnerAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        mSecuritySpinner.setAdapter(spinnerAdapter);
        int idx = 0;

        // Populate the Wi-Fi security spinner with the various supported key management types
        spinnerAdapter.add(mContext.getString(R.string.wifi_security_none));
        mSecurityInPosition[idx++] = AccessPoint.SECURITY_NONE;
        if (mWifiManager.isOweSupported()) {
            spinnerAdapter.add(mContext.getString(R.string.wifi_security_owe));
            mSecurityInPosition[idx++] = AccessPoint.SECURITY_OWE;
        }
        spinnerAdapter.add(mContext.getString(R.string.wifi_security_wep));
        mSecurityInPosition[idx++] = AccessPoint.SECURITY_WEP;
        spinnerAdapter.add(mContext.getString(R.string.wifi_security_wpa_wpa2));
        mSecurityInPosition[idx++] = AccessPoint.SECURITY_PSK;
        if (mWifiManager.isWpa3SaeSupported()) {
            spinnerAdapter.add(mContext.getString(R.string.wifi_security_sae));
            mSecurityInPosition[idx++] = AccessPoint.SECURITY_SAE;
        }
        spinnerAdapter.add(mContext.getString(R.string.wifi_security_eap));
        mSecurityInPosition[idx++] = AccessPoint.SECURITY_EAP;
        if (mWifiManager.isWpa3SuiteBSupported()) {
            spinnerAdapter.add(mContext.getString(R.string.wifi_security_eap_suiteb));
            mSecurityInPosition[idx++] = AccessPoint.SECURITY_EAP_SUITE_B;
        }

        spinnerAdapter.notifyDataSetChanged();

        mView.findViewById(R.id.type).setVisibility(View.VISIBLE);

        showIpConfigFields();
        showProxyFields();
        mView.findViewById(R.id.wifi_advanced_toggle).setVisibility(View.VISIBLE);
        // Hidden option can be changed only when the user adds a network manually.
        mView.findViewById(R.id.hidden_settings_field).setVisibility(View.VISIBLE);
        ((CheckBox) mView.findViewById(R.id.wifi_advanced_togglebox))
                .setOnCheckedChangeListener(this);
    }
}
+77 −0
Original line number Diff line number Diff line
@@ -22,16 +22,19 @@ import static org.mockito.Mockito.any;
import static org.mockito.Mockito.anyBoolean;
import static org.mockito.Mockito.anyInt;
import static org.mockito.Mockito.anyString;
import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

import android.content.Context;
import android.net.wifi.WifiManager;
import android.os.ServiceSpecificException;
import android.security.KeyStore;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
import android.widget.TextView;

@@ -298,6 +301,74 @@ public class WifiConfigControllerTest {
        assertThat(hiddenField.getVisibility()).isEqualTo(View.VISIBLE);
    }

    @Test
    public void securitySpinner_saeSuitebAndOweNotVisible() {
        securitySpinnerTestHelper(false, false, false);
    }

    @Test
    public void securitySpinner_saeSuitebAndOweVisible() {
        securitySpinnerTestHelper(true, true, true);
    }

    @Test
    public void securitySpinner_saeVisible_suitebAndOweNotVisible() {
        securitySpinnerTestHelper(true, false, false);
    }

    @Test
    public void securitySpinner_oweVisible_suitebAndSaeNotVisible() {
        securitySpinnerTestHelper(false, false, true);
    }

    private void securitySpinnerTestHelper(boolean saeVisible, boolean suitebVisible,
            boolean oweVisible) {
        WifiManager wifiManager = mock(WifiManager.class);
        when(wifiManager.isWpa3SaeSupported()).thenReturn(saeVisible ? true : false);
        when(wifiManager.isWpa3SuiteBSupported()).thenReturn(suitebVisible ? true : false);
        when(wifiManager.isOweSupported()).thenReturn(oweVisible ? true : false);

        mController = new TestWifiConfigController(mConfigUiBase, mView, null /* accessPoint */,
                WifiConfigUiBase.MODE_MODIFY, wifiManager);

        final Spinner securitySpinner = ((Spinner) mView.findViewById(R.id.security));
        final ArrayAdapter<String> adapter = (ArrayAdapter) securitySpinner.getAdapter();
        boolean saeFound = false;
        boolean suitebFound = false;
        boolean oweFound = false;
        for (int i = 0; i < adapter.getCount(); i++) {
            String val = adapter.getItem(i);

            if (val.compareTo(mContext.getString(R.string.wifi_security_sae)) == 0) {
                saeFound = true;
            }

            if (val.compareTo(mContext.getString(R.string.wifi_security_eap_suiteb)) == 0) {
                suitebFound = true;
            }

            if (val.compareTo(mContext.getString(R.string.wifi_security_owe)) == 0) {
                oweFound = true;
            }
        }

        if (saeVisible) {
            assertThat(saeFound).isTrue();
        } else {
            assertThat(saeFound).isFalse();
        }
        if (suitebVisible) {
            assertThat(suitebFound).isTrue();
        } else {
            assertThat(suitebFound).isFalse();
        }
        if (oweVisible) {
            assertThat(oweFound).isTrue();
        } else {
            assertThat(oweFound).isFalse();
        }
    }

    public class TestWifiConfigController extends WifiConfigController {

        private TestWifiConfigController(
@@ -305,6 +376,12 @@ public class WifiConfigControllerTest {
            super(parent, view, accessPoint, mode);
        }

        private TestWifiConfigController(
                WifiConfigUiBase parent, View view, AccessPoint accessPoint, int mode,
                    WifiManager wifiManager) {
            super(parent, view, accessPoint, mode, wifiManager);
        }

        @Override
        boolean isSplitSystemUser() {
            return false;
+5 −5
Original line number Diff line number Diff line
@@ -66,11 +66,11 @@ public class WifiTetherSecurityPreferenceControllerTest {
    public void onPreferenceChange_securityValueUpdated() {
        mController.onPreferenceChange(mPreference, WPA2_PSK);
        assertThat(mController.getSecurityType()).isEqualTo(WifiConfiguration.KeyMgmt.WPA2_PSK);
        assertThat(mPreference.getSummary()).isEqualTo("WPA2-Personal");
        assertThat(mPreference.getSummary().toString()).isEqualTo("WPA2-Personal");

        mController.onPreferenceChange(mPreference, NONE);
        assertThat(mController.getSecurityType()).isEqualTo(WifiConfiguration.KeyMgmt.NONE);
        assertThat(mPreference.getSummary()).isEqualTo("None");
        assertThat(mPreference.getSummary().toString()).isEqualTo("None");
    }

    @Test
@@ -79,7 +79,7 @@ public class WifiTetherSecurityPreferenceControllerTest {
        when(mWifiManager.getWifiApConfiguration()).thenReturn(null);
        mController.updateDisplay();
        assertThat(mController.getSecurityType()).isEqualTo(WifiConfiguration.KeyMgmt.WPA2_PSK);
        assertThat(mPreference.getSummary()).isEqualTo("WPA2-Personal");
        assertThat(mPreference.getSummary().toString()).isEqualTo("WPA2-Personal");

        // test open tether network
        when(mWifiManager.getWifiApConfiguration()).thenReturn(mConfig);
@@ -87,13 +87,13 @@ public class WifiTetherSecurityPreferenceControllerTest {
        mConfig.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE);
        mController.updateDisplay();
        assertThat(mController.getSecurityType()).isEqualTo(WifiConfiguration.KeyMgmt.NONE);
        assertThat(mPreference.getSummary()).isEqualTo("None");
        assertThat(mPreference.getSummary().toString()).isEqualTo("None");

        // test WPA2-Personal tether network
        mConfig.allowedKeyManagement.clear();
        mConfig.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.WPA2_PSK);
        mController.updateDisplay();
        assertThat(mController.getSecurityType()).isEqualTo(WifiConfiguration.KeyMgmt.WPA2_PSK);
        assertThat(mPreference.getSummary()).isEqualTo("WPA2-Personal");
        assertThat(mPreference.getSummary().toString()).isEqualTo("WPA2-Personal");
    }
}