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

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

Merge "Make user explicitly set security type for tether network" into pi-dev

parents 018548b3 d9bae5a1
Loading
Loading
Loading
Loading
+16 −0
Original line number Diff line number Diff line
@@ -235,6 +235,22 @@
        <item>@string/wifi_security_psk_generic</item>
    </string-array>

    <!-- Security types for wireless tether -->
    <string-array name="wifi_tether_security">
        <!-- Do not translate. -->
        <item>@string/wifi_security_wpa2</item>
        <!-- Do not translate. -->
        <item>@string/wifi_security_none</item>
    </string-array>

    <!-- Values for security type for wireless tether -->
    <string-array name="wifi_tether_security_values">
        <!-- Do not translate. -->
        <item>4</item>
        <!-- Do not translate. -->
        <item>0</item>
    </string-array>

    <!-- Match this with the constants in WifiDialog. --> <skip />
    <!-- Wi-Fi settings.  The type of EAP method a Wi-Fi network has. -->
    <string-array name="wifi_eap_method">
+10 −3
Original line number Diff line number Diff line
@@ -19,13 +19,20 @@
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:settings="http://schemas.android.com/apk/res-auto"
    android:title="@string/wifi_hotspot_checkbox_text"
    settings:initialExpandedChildrenCount="2">
    settings:initialExpandedChildrenCount="3">

    <com.android.settings.widget.ValidatedEditTextPreference
        android:key="wifi_tether_network_name"
        android:title="@string/wifi_hotspot_name_title"
        android:summary="@string/summary_placeholder" />

    <ListPreference
        android:key="wifi_tether_security"
        android:title="@string/wifi_security"
        android:summary="@string/summary_placeholder"
        android:entries="@array/wifi_tether_security"
        android:entryValues="@array/wifi_tether_security_values" />

    <com.android.settings.widget.ValidatedEditTextPreference
        android:key="wifi_tether_network_password"
        android:persistent="false"
+1 −1
Original line number Diff line number Diff line
@@ -52,7 +52,7 @@ public class WifiUtils {

    public static boolean isHotspotPasswordValid(String password) {
        if (TextUtils.isEmpty(password)) {
            return true;
            return false;
        }

        final int length = password.length();
+24 −12
Original line number Diff line number Diff line
@@ -31,7 +31,6 @@ import java.util.UUID;
public class WifiTetherPasswordPreferenceController extends WifiTetherBasePreferenceController
        implements ValidatedEditTextPreference.Validator {

    private static final String TAG = "WifiTetherPswdPref";
    private static final String PREF_KEY = "wifi_tether_network_password";

    private String mPassword;
@@ -49,10 +48,11 @@ public class WifiTetherPasswordPreferenceController extends WifiTetherBasePrefer
    @Override
    public void updateDisplay() {
        final WifiConfiguration config = mWifiManager.getWifiApConfiguration();
        if (config != null) {
            mPassword = config.preSharedKey;
        } else {
        if (config == null || (config.getAuthType() == WifiConfiguration.KeyMgmt.WPA2_PSK
                && TextUtils.isEmpty(config.preSharedKey))) {
            mPassword = generateRandomPassword();
        } else {
            mPassword = config.preSharedKey;
        }
        ((ValidatedEditTextPreference) mPreference).setValidator(this);
        ((ValidatedEditTextPreference) mPreference).setIsPassword(true);
@@ -68,17 +68,27 @@ public class WifiTetherPasswordPreferenceController extends WifiTetherBasePrefer
        return true;
    }

    public String getPassword() {
    /**
     * This method returns the current password if it is valid for the indicated security type. If
     * the password currently set is invalid it will forcefully set a random password that is valid.
     *
     * @param securityType The security type for the password.
     * @return The current password if it is valid for the indicated security type. A new randomly
     * generated password if it is not.
     */
    public String getPasswordValidated(int securityType) {
        // don't actually overwrite unless we get a new config in case it was accidentally toggled.
        if (securityType == WifiConfiguration.KeyMgmt.NONE) {
            return "";
        } else if (!isTextValid(mPassword)) {
            mPassword = generateRandomPassword();
            updatePasswordDisplay((EditTextPreference) mPreference);
        }
        return mPassword;
    }

    public int getSecuritySettingForPassword() {
        // We should return NONE when no password is set
        if (TextUtils.isEmpty(mPassword)) {
            return WifiConfiguration.KeyMgmt.NONE;
        }
        // Only other currently supported type is WPA2 so we'll try that
        return WifiConfiguration.KeyMgmt.WPA2_PSK;
    public void updateVisibility(int securityType) {
        mPreference.setVisible(securityType != WifiConfiguration.KeyMgmt.NONE);
    }

    @Override
@@ -98,9 +108,11 @@ public class WifiTetherPasswordPreferenceController extends WifiTetherBasePrefer
        if (!TextUtils.isEmpty(mPassword)) {
            pref.setIsSummaryPassword(true);
            pref.setSummary(mPassword);
            pref.setVisible(true);
        } else {
            pref.setIsSummaryPassword(false);
            pref.setSummary(R.string.wifi_hotspot_no_password_subtext);
            pref.setVisible(false);
        }
    }
}
+63 −0
Original line number Diff line number Diff line
package com.android.settings.wifi.tether;

import android.content.Context;
import android.content.res.Resources;
import android.net.wifi.WifiConfiguration;
import android.support.v7.preference.ListPreference;
import android.support.v7.preference.Preference;

import com.android.settings.R;

public class WifiTetherSecurityPreferenceController extends WifiTetherBasePreferenceController {

    private static final String PREF_KEY = "wifi_tether_security";

    private final String[] mSecurityEntries;
    private int mSecurityValue;

    public WifiTetherSecurityPreferenceController(Context context,
            OnTetherConfigUpdateListener listener) {
        super(context, listener);
        mSecurityEntries = mContext.getResources().getStringArray(R.array.wifi_tether_security);
    }

    @Override
    public String getPreferenceKey() {
        return PREF_KEY;
    }

    @Override
    public void updateDisplay() {
        final WifiConfiguration config = mWifiManager.getWifiApConfiguration();
        if (config != null && config.getAuthType() == WifiConfiguration.KeyMgmt.NONE) {
            mSecurityValue = WifiConfiguration.KeyMgmt.NONE;

        } else {
            mSecurityValue = WifiConfiguration.KeyMgmt.WPA2_PSK;
        }

        final ListPreference preference = (ListPreference) mPreference;
        preference.setSummary(getSummaryForSecurityType(mSecurityValue));
        preference.setValue(String.valueOf(mSecurityValue));
    }

    @Override
    public boolean onPreferenceChange(Preference preference, Object newValue) {
        mSecurityValue = Integer.parseInt((String) newValue);
        preference.setSummary(getSummaryForSecurityType(mSecurityValue));
        mListener.onTetherConfigUpdated();
        return true;
    }

    public int getSecurityType() {
        return mSecurityValue;
    }

    private String getSummaryForSecurityType(int securityType) {
        if (securityType == WifiConfiguration.KeyMgmt.NONE) {
            return mSecurityEntries[1];
        }
        // WPA2 PSK
        return mSecurityEntries[0];
    }
}
Loading