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.annotation.VisibleForTesting;
22import android.support.v7.preference.EditTextPreference;
23import android.support.v7.preference.Preference;
24import android.support.v7.preference.PreferenceScreen;
25
26import com.android.settings.widget.ValidatedEditTextPreference;
27import com.android.settings.wifi.WifiUtils;
28
29public class WifiTetherSSIDPreferenceController extends WifiTetherBasePreferenceController
30        implements ValidatedEditTextPreference.Validator {
31
32    private static final String PREF_KEY = "wifi_tether_network_name";
33    @VisibleForTesting
34    static final String DEFAULT_SSID = "AndroidAP";
35
36    private String mSSID;
37
38    public WifiTetherSSIDPreferenceController(Context context,
39            OnTetherConfigUpdateListener listener) {
40        super(context, listener);
41    }
42
43    @Override
44    public String getPreferenceKey() {
45        return PREF_KEY;
46    }
47
48    @Override
49    public void displayPreference(PreferenceScreen screen) {
50        super.displayPreference(screen);
51        final WifiConfiguration config = mWifiManager.getWifiApConfiguration();
52        if (config != null) {
53            mSSID = config.SSID;
54        } else {
55            mSSID = DEFAULT_SSID;
56        }
57        ((ValidatedEditTextPreference) mPreference).setValidator(this);
58        updateSsidDisplay((EditTextPreference) mPreference);
59    }
60
61    @Override
62    public boolean onPreferenceChange(Preference preference, Object newValue) {
63        mSSID = (String) newValue;
64        updateSsidDisplay((EditTextPreference) preference);
65        mListener.onTetherConfigUpdated();
66        return true;
67    }
68
69    @Override
70    public boolean isTextValid(String value) {
71        return !WifiUtils.isSSIDTooLong(value) && !WifiUtils.isSSIDTooShort(value);
72    }
73
74    public String getSSID() {
75        return mSSID;
76    }
77
78    private void updateSsidDisplay(EditTextPreference preference) {
79        preference.setText(mSSID);
80        preference.setSummary(mSSID);
81    }
82}
83