• 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 static com.android.settings.AllInOneTetherSettings.DEDUP_POSTFIX;
20 
21 import android.app.settings.SettingsEnums;
22 import android.content.Context;
23 import android.net.wifi.SoftApConfiguration;
24 import android.text.TextUtils;
25 import android.util.FeatureFlagUtils;
26 
27 import androidx.annotation.VisibleForTesting;
28 import androidx.preference.EditTextPreference;
29 import androidx.preference.Preference;
30 
31 import com.android.settings.R;
32 import com.android.settings.core.FeatureFlags;
33 import com.android.settings.overlay.FeatureFactory;
34 import com.android.settings.widget.ValidatedEditTextPreference;
35 import com.android.settings.wifi.WifiUtils;
36 import com.android.settingslib.core.instrumentation.MetricsFeatureProvider;
37 
38 import java.util.UUID;
39 
40 public class WifiTetherPasswordPreferenceController extends WifiTetherBasePreferenceController
41         implements ValidatedEditTextPreference.Validator {
42 
43     private static final String PREF_KEY = "wifi_tether_network_password";
44 
45     private String mPassword;
46 
47     private final MetricsFeatureProvider mMetricsFeatureProvider;
48 
49     @VisibleForTesting
WifiTetherPasswordPreferenceController(Context context, OnTetherConfigUpdateListener listener, MetricsFeatureProvider provider)50     WifiTetherPasswordPreferenceController(Context context, OnTetherConfigUpdateListener listener,
51             MetricsFeatureProvider provider) {
52         super(context, listener);
53         mMetricsFeatureProvider = provider;
54     }
55 
WifiTetherPasswordPreferenceController(Context context, OnTetherConfigUpdateListener listener)56     public WifiTetherPasswordPreferenceController(Context context,
57             OnTetherConfigUpdateListener listener) {
58         super(context, listener);
59         mMetricsFeatureProvider = FeatureFactory.getFactory(context).getMetricsFeatureProvider();
60     }
61 
62     @Override
getPreferenceKey()63     public String getPreferenceKey() {
64         return FeatureFlagUtils.isEnabled(mContext, FeatureFlags.TETHER_ALL_IN_ONE)
65                 ? PREF_KEY + DEDUP_POSTFIX : PREF_KEY;
66     }
67 
68     @Override
updateDisplay()69     public void updateDisplay() {
70         final SoftApConfiguration config = mWifiManager.getSoftApConfiguration();
71         if (config == null
72                 || (config.getSecurityType() == SoftApConfiguration.SECURITY_TYPE_WPA2_PSK
73                 && TextUtils.isEmpty(config.getPassphrase()))) {
74             mPassword = generateRandomPassword();
75         } else {
76             mPassword = config.getPassphrase();
77         }
78         ((ValidatedEditTextPreference) mPreference).setValidator(this);
79         ((ValidatedEditTextPreference) mPreference).setIsPassword(true);
80         ((ValidatedEditTextPreference) mPreference).setIsSummaryPassword(true);
81         updatePasswordDisplay((EditTextPreference) mPreference);
82     }
83 
84     @Override
onPreferenceChange(Preference preference, Object newValue)85     public boolean onPreferenceChange(Preference preference, Object newValue) {
86         if (!TextUtils.equals(mPassword, (String) newValue)) {
87             mMetricsFeatureProvider.action(mContext,
88                     SettingsEnums.ACTION_SETTINGS_CHANGE_WIFI_HOTSPOT_PASSWORD);
89         }
90         mPassword = (String) newValue;
91         updatePasswordDisplay((EditTextPreference) mPreference);
92         mListener.onTetherConfigUpdated(this);
93         return true;
94     }
95 
96     /**
97      * This method returns the current password if it is valid for the indicated security type. If
98      * the password currently set is invalid it will forcefully set a random password that is valid.
99      *
100      * @param securityType The security type for the password.
101      * @return The current password if it is valid for the indicated security type. A new randomly
102      * generated password if it is not.
103      */
getPasswordValidated(int securityType)104     public String getPasswordValidated(int securityType) {
105         // don't actually overwrite unless we get a new config in case it was accidentally toggled.
106         if (securityType == SoftApConfiguration.SECURITY_TYPE_OPEN) {
107             return "";
108         } else if (!isTextValid(mPassword)) {
109             mPassword = generateRandomPassword();
110             updatePasswordDisplay((EditTextPreference) mPreference);
111         }
112         return mPassword;
113     }
114 
updateVisibility(int securityType)115     public void updateVisibility(int securityType) {
116         mPreference.setVisible(securityType != SoftApConfiguration.SECURITY_TYPE_OPEN);
117     }
118 
119     @Override
isTextValid(String value)120     public boolean isTextValid(String value) {
121         return WifiUtils.isHotspotWpa2PasswordValid(value);
122     }
123 
generateRandomPassword()124     private static String generateRandomPassword() {
125         String randomUUID = UUID.randomUUID().toString();
126         //first 12 chars from xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx
127         return randomUUID.substring(0, 8) + randomUUID.substring(9, 13);
128     }
129 
updatePasswordDisplay(EditTextPreference preference)130     private void updatePasswordDisplay(EditTextPreference preference) {
131         ValidatedEditTextPreference pref = (ValidatedEditTextPreference) preference;
132         pref.setText(mPassword);
133         if (!TextUtils.isEmpty(mPassword)) {
134             pref.setIsSummaryPassword(true);
135             pref.setSummary(mPassword);
136             pref.setVisible(true);
137         } else {
138             pref.setIsSummaryPassword(false);
139             pref.setSummary(R.string.wifi_hotspot_no_password_subtext);
140             pref.setVisible(false);
141         }
142     }
143 }
144