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
17package com.android.settings.wifi.tether;
18
19import android.content.Context;
20import android.net.wifi.WifiConfiguration;
21import android.support.v7.preference.EditTextPreference;
22import android.support.v7.preference.Preference;
23import android.support.v7.preference.PreferenceScreen;
24
25import com.android.settings.widget.ValidatedEditTextPreference;
26import com.android.settings.wifi.WifiUtils;
27
28public class WifiTetherPasswordPreferenceController extends WifiTetherBasePreferenceController
29        implements ValidatedEditTextPreference.Validator {
30
31    private static final String PREF_KEY = "wifi_tether_network_password";
32
33    private String mPassword;
34
35    public WifiTetherPasswordPreferenceController(Context context,
36            OnTetherConfigUpdateListener listener) {
37        super(context, listener);
38        final WifiConfiguration config = mWifiManager.getWifiApConfiguration();
39        if (config != null) {
40            mPassword = config.preSharedKey;
41        }
42    }
43
44    @Override
45    public String getPreferenceKey() {
46        return PREF_KEY;
47    }
48
49    @Override
50    public void displayPreference(PreferenceScreen screen) {
51        super.displayPreference(screen);
52        ((ValidatedEditTextPreference) mPreference).setValidator(this);
53        updatePasswordDisplay((EditTextPreference) mPreference);
54    }
55
56    @Override
57    public boolean onPreferenceChange(Preference preference, Object newValue) {
58        mPassword = (String) newValue;
59        updatePasswordDisplay((EditTextPreference) mPreference);
60        mListener.onTetherConfigUpdated();
61        return true;
62    }
63
64    public String getPassword() {
65        return mPassword;
66    }
67
68    @Override
69    public boolean isTextValid(String value) {
70        return WifiUtils.isPasswordValid(value);
71    }
72
73    private void updatePasswordDisplay(EditTextPreference preference) {
74        preference.setText(mPassword);
75        preference.setSummary(mPassword);
76    }
77}
78