• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2017 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package com.android.settings.wifi.tether;
18 
19 import android.content.Context;
20 import android.net.wifi.WifiConfiguration;
21 import android.support.v7.preference.EditTextPreference;
22 import android.support.v7.preference.Preference;
23 import android.text.TextUtils;
24 
25 import com.android.settings.R;
26 import com.android.settings.widget.ValidatedEditTextPreference;
27 import com.android.settings.wifi.WifiUtils;
28 
29 import java.util.UUID;
30 
31 public class WifiTetherPasswordPreferenceController extends WifiTetherBasePreferenceController
32         implements ValidatedEditTextPreference.Validator {
33 
34     private static final String PREF_KEY = "wifi_tether_network_password";
35 
36     private String mPassword;
37 
WifiTetherPasswordPreferenceController(Context context, OnTetherConfigUpdateListener listener)38     public WifiTetherPasswordPreferenceController(Context context,
39             OnTetherConfigUpdateListener listener) {
40         super(context, listener);
41     }
42 
43     @Override
getPreferenceKey()44     public String getPreferenceKey() {
45         return PREF_KEY;
46     }
47 
48     @Override
updateDisplay()49     public void updateDisplay() {
50         final WifiConfiguration config = mWifiManager.getWifiApConfiguration();
51         if (config == null || (config.getAuthType() == WifiConfiguration.KeyMgmt.WPA2_PSK
52                 && TextUtils.isEmpty(config.preSharedKey))) {
53             mPassword = generateRandomPassword();
54         } else {
55             mPassword = config.preSharedKey;
56         }
57         ((ValidatedEditTextPreference) mPreference).setValidator(this);
58         ((ValidatedEditTextPreference) mPreference).setIsPassword(true);
59         ((ValidatedEditTextPreference) mPreference).setIsSummaryPassword(true);
60         updatePasswordDisplay((EditTextPreference) mPreference);
61     }
62 
63     @Override
onPreferenceChange(Preference preference, Object newValue)64     public boolean onPreferenceChange(Preference preference, Object newValue) {
65         mPassword = (String) newValue;
66         updatePasswordDisplay((EditTextPreference) mPreference);
67         mListener.onTetherConfigUpdated();
68         return true;
69     }
70 
71     /**
72      * This method returns the current password if it is valid for the indicated security type. If
73      * the password currently set is invalid it will forcefully set a random password that is valid.
74      *
75      * @param securityType The security type for the password.
76      * @return The current password if it is valid for the indicated security type. A new randomly
77      * generated password if it is not.
78      */
getPasswordValidated(int securityType)79     public String getPasswordValidated(int securityType) {
80         // don't actually overwrite unless we get a new config in case it was accidentally toggled.
81         if (securityType == WifiConfiguration.KeyMgmt.NONE) {
82             return "";
83         } else if (!isTextValid(mPassword)) {
84             mPassword = generateRandomPassword();
85             updatePasswordDisplay((EditTextPreference) mPreference);
86         }
87         return mPassword;
88     }
89 
updateVisibility(int securityType)90     public void updateVisibility(int securityType) {
91         mPreference.setVisible(securityType != WifiConfiguration.KeyMgmt.NONE);
92     }
93 
94     @Override
isTextValid(String value)95     public boolean isTextValid(String value) {
96         return WifiUtils.isHotspotPasswordValid(value);
97     }
98 
generateRandomPassword()99     private static String generateRandomPassword() {
100         String randomUUID = UUID.randomUUID().toString();
101         //first 12 chars from xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx
102         return randomUUID.substring(0, 8) + randomUUID.substring(9, 13);
103     }
104 
updatePasswordDisplay(EditTextPreference preference)105     private void updatePasswordDisplay(EditTextPreference preference) {
106         ValidatedEditTextPreference pref = (ValidatedEditTextPreference) preference;
107         pref.setText(mPassword);
108         if (!TextUtils.isEmpty(mPassword)) {
109             pref.setIsSummaryPassword(true);
110             pref.setSummary(mPassword);
111             pref.setVisible(true);
112         } else {
113             pref.setIsSummaryPassword(false);
114             pref.setSummary(R.string.wifi_hotspot_no_password_subtext);
115             pref.setVisible(false);
116         }
117     }
118 }
119