diff --git a/res/values/strings.xml b/res/values/strings.xml index 7507de2dee8..96c5be33f79 100644 --- a/res/values/strings.xml +++ b/res/values/strings.xml @@ -2162,6 +2162,8 @@ Sharing this phone\u2019s internet connection via hotspot App is sharing content. To share internet connection, turn hotspot off, then on + + No password set diff --git a/src/com/android/settings/widget/ValidatedEditTextPreference.java b/src/com/android/settings/widget/ValidatedEditTextPreference.java index 580eb58f6a4..a5bab1cd980 100644 --- a/src/com/android/settings/widget/ValidatedEditTextPreference.java +++ b/src/com/android/settings/widget/ValidatedEditTextPreference.java @@ -86,9 +86,14 @@ public class ValidatedEditTextPreference extends CustomEditTextPreference { super.onBindViewHolder(holder); final TextView textView = (TextView) holder.findViewById(android.R.id.summary); - if (textView != null && mIsSummaryPassword) { + if (textView == null) { + return; + } + if (mIsSummaryPassword) { textView.setInputType( InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD); + } else { + textView.setInputType(InputType.TYPE_CLASS_TEXT); } } diff --git a/src/com/android/settings/wifi/WifiUtils.java b/src/com/android/settings/wifi/WifiUtils.java index 0477280c8e0..3e797d77b47 100644 --- a/src/com/android/settings/wifi/WifiUtils.java +++ b/src/com/android/settings/wifi/WifiUtils.java @@ -50,10 +50,11 @@ public class WifiUtils { return ssid.length() < SSID_ASCII_MIN_LENGTH; } - public static boolean isPasswordValid(String password) { + public static boolean isHotspotPasswordValid(String password) { if (TextUtils.isEmpty(password)) { - return false; + return true; } + final int length = password.length(); return length >= PASSWORD_MIN_LENGTH && length <= PASSWORD_MAX_LENGTH; } diff --git a/src/com/android/settings/wifi/tether/WifiTetherPasswordPreferenceController.java b/src/com/android/settings/wifi/tether/WifiTetherPasswordPreferenceController.java index 0e973ae8142..8eab9f41fcc 100644 --- a/src/com/android/settings/wifi/tether/WifiTetherPasswordPreferenceController.java +++ b/src/com/android/settings/wifi/tether/WifiTetherPasswordPreferenceController.java @@ -20,11 +20,15 @@ import android.content.Context; import android.net.wifi.WifiConfiguration; import android.support.v7.preference.EditTextPreference; import android.support.v7.preference.Preference; +import android.text.TextUtils; import android.util.Log; +import com.android.settings.R; import com.android.settings.widget.ValidatedEditTextPreference; import com.android.settings.wifi.WifiUtils; +import java.util.UUID; + public class WifiTetherPasswordPreferenceController extends WifiTetherBasePreferenceController implements ValidatedEditTextPreference.Validator { @@ -49,6 +53,8 @@ public class WifiTetherPasswordPreferenceController extends WifiTetherBasePrefer if (config != null) { mPassword = config.preSharedKey; Log.d(TAG, "Updating password in Preference, " + mPassword); + } else { + mPassword = generateRandomPassword(); } ((ValidatedEditTextPreference) mPreference).setValidator(this); ((ValidatedEditTextPreference) mPreference).setIsSummaryPassword(true); @@ -67,13 +73,35 @@ public class WifiTetherPasswordPreferenceController extends WifiTetherBasePrefer 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; + } + @Override public boolean isTextValid(String value) { - return WifiUtils.isPasswordValid(value); + return WifiUtils.isHotspotPasswordValid(value); + } + + private static String generateRandomPassword() { + String randomUUID = UUID.randomUUID().toString(); + //first 12 chars from xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx + return randomUUID.substring(0, 8) + randomUUID.substring(9, 13); } private void updatePasswordDisplay(EditTextPreference preference) { - preference.setText(mPassword); - preference.setSummary(mPassword); + ValidatedEditTextPreference pref = (ValidatedEditTextPreference) preference; + pref.setText(mPassword); + if (!TextUtils.isEmpty(mPassword)) { + pref.setIsSummaryPassword(true); + pref.setSummary(mPassword); + } else { + pref.setIsSummaryPassword(false); + pref.setSummary(R.string.wifi_hotspot_no_password_subtext); + } } } diff --git a/src/com/android/settings/wifi/tether/WifiTetherSettings.java b/src/com/android/settings/wifi/tether/WifiTetherSettings.java index 996c73ccc68..de6243c2f31 100644 --- a/src/com/android/settings/wifi/tether/WifiTetherSettings.java +++ b/src/com/android/settings/wifi/tether/WifiTetherSettings.java @@ -161,8 +161,8 @@ public class WifiTetherSettings extends RestrictedDashboardFragment config.SSID = mSSIDPreferenceController.getSSID(); config.preSharedKey = mPasswordPreferenceController.getPassword(); - ensureWifiConfigHasPassword(config); - config.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.WPA2_PSK); + config.allowedKeyManagement.set( + mPasswordPreferenceController.getSecuritySettingForPassword()); config.allowedAuthAlgorithms.set(WifiConfiguration.AuthAlgorithm.OPEN); config.apBand = mApBandPreferenceController.getBandIndex(); return config; @@ -182,15 +182,6 @@ public class WifiTetherSettings extends RestrictedDashboardFragment .updateDisplay(); } - @VisibleForTesting - static void ensureWifiConfigHasPassword(WifiConfiguration config) { - if (TextUtils.isEmpty(config.preSharedKey)) { - String randomUUID = UUID.randomUUID().toString(); - //first 12 chars from xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx - config.preSharedKey = randomUUID.substring(0, 8) + randomUUID.substring(9, 13); - } - } - @VisibleForTesting class TetherChangeReceiver extends BroadcastReceiver { @Override diff --git a/tests/robotests/src/com/android/settings/wifi/WifiUtilsTest.java b/tests/robotests/src/com/android/settings/wifi/WifiUtilsTest.java index 197fd4055bd..198517a297f 100644 --- a/tests/robotests/src/com/android/settings/wifi/WifiUtilsTest.java +++ b/tests/robotests/src/com/android/settings/wifi/WifiUtilsTest.java @@ -39,9 +39,10 @@ public class WifiUtilsTest { public void testPassword() { final String longPassword = "123456789012345678901234567890" + "1234567890123456789012345678901234567890"; - assertThat(WifiUtils.isPasswordValid("123")).isFalse(); - assertThat(WifiUtils.isPasswordValid("12345678")).isTrue(); - assertThat(WifiUtils.isPasswordValid("1234567890")).isTrue(); - assertThat(WifiUtils.isPasswordValid(longPassword)).isFalse(); + assertThat(WifiUtils.isHotspotPasswordValid("123")).isFalse(); + assertThat(WifiUtils.isHotspotPasswordValid("12345678")).isTrue(); + assertThat(WifiUtils.isHotspotPasswordValid("1234567890")).isTrue(); + assertThat(WifiUtils.isHotspotPasswordValid(longPassword)).isFalse(); + assertThat(WifiUtils.isHotspotPasswordValid("")).isTrue(); } } diff --git a/tests/robotests/src/com/android/settings/wifi/tether/WifiTetherPasswordPreferenceControllerTest.java b/tests/robotests/src/com/android/settings/wifi/tether/WifiTetherPasswordPreferenceControllerTest.java index 58f31b1310a..60faa2e0702 100644 --- a/tests/robotests/src/com/android/settings/wifi/tether/WifiTetherPasswordPreferenceControllerTest.java +++ b/tests/robotests/src/com/android/settings/wifi/tether/WifiTetherPasswordPreferenceControllerTest.java @@ -113,4 +113,36 @@ public class WifiTetherPasswordPreferenceControllerTest { assertThat(mController.getPassword()).isEqualTo(config.preSharedKey); assertThat(mPreference.getSummary()).isEqualTo(config.preSharedKey); } + + @Test + public void getSecuritySettingForPassword_returnCorrectType() { + // valid wpa2 password + mController.displayPreference(mScreen); + assertThat(mController.getSecuritySettingForPassword()) + .isEqualTo(WifiConfiguration.KeyMgmt.WPA2_PSK); + + // password which is empty returns NONE + mConfig = new WifiConfiguration(); + mConfig.SSID = "test_1234"; + mConfig.preSharedKey = ""; + when(mContext.getSystemService(Context.WIFI_SERVICE)).thenReturn(mWifiManager); + when(mWifiManager.getWifiApConfiguration()).thenReturn(mConfig); + mController = new WifiTetherPasswordPreferenceController(mContext, mListener); + + mController.displayPreference(mScreen); + assertThat(mController.getSecuritySettingForPassword()) + .isEqualTo(WifiConfiguration.KeyMgmt.NONE); + + // default for unsupported types is wpa2 + mConfig = new WifiConfiguration(); + mConfig.SSID = "test_1234"; + mConfig.preSharedKey = "short"; + when(mContext.getSystemService(Context.WIFI_SERVICE)).thenReturn(mWifiManager); + when(mWifiManager.getWifiApConfiguration()).thenReturn(mConfig); + mController = new WifiTetherPasswordPreferenceController(mContext, mListener); + + mController.displayPreference(mScreen); + assertThat(mController.getSecuritySettingForPassword()) + .isEqualTo(WifiConfiguration.KeyMgmt.WPA2_PSK); + } } diff --git a/tests/robotests/src/com/android/settings/wifi/tether/WifiTetherSettingsTest.java b/tests/robotests/src/com/android/settings/wifi/tether/WifiTetherSettingsTest.java deleted file mode 100644 index 8c845758918..00000000000 --- a/tests/robotests/src/com/android/settings/wifi/tether/WifiTetherSettingsTest.java +++ /dev/null @@ -1,38 +0,0 @@ -/* - * Copyright (C) 2017 The Android Open Source Project - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package com.android.settings.wifi.tether; - -import static com.google.common.truth.Truth.assertThat; - -import android.net.wifi.WifiConfiguration; - -import com.android.settings.testutils.SettingsRobolectricTestRunner; - -import org.junit.Test; -import org.junit.runner.RunWith; - -@RunWith(SettingsRobolectricTestRunner.class) -public class WifiTetherSettingsTest { - - @Test - public void ensureWifiConfigHasPassword_shouldGeneratePassword() { - WifiConfiguration config = new WifiConfiguration(); - WifiTetherSettings.ensureWifiConfigHasPassword(config); - - assertThat(config.preSharedKey).isNotEmpty(); - } -}