1 /*
2  * Copyright (C) 2019 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.car.settings.wifi;
18 
19 import android.car.drivingstate.CarUxRestrictions;
20 import android.content.Context;
21 import android.net.wifi.WifiConfiguration;
22 
23 import com.android.car.settings.common.FragmentController;
24 import com.android.car.settings.common.ValidatedEditTextPreference;
25 
26 /**
27  * Controls WiFi Hotspot name configuration. When Hotspot is enabled, this name that will be
28  * displayed as the Access Point to the Hotspot.
29  */
30 public class WifiTetherNamePreferenceController extends
31         WifiTetherBasePreferenceController<ValidatedEditTextPreference> {
32 
33     private static final int HOTSPOT_NAME_MIN_LENGTH = 1;
34     private static final int HOTSPOT_NAME_MAX_LENGTH = 32;
35     private static final ValidatedEditTextPreference.Validator NAME_VALIDATOR =
36             value -> value.length() >= HOTSPOT_NAME_MIN_LENGTH
37                     && value.length() <= HOTSPOT_NAME_MAX_LENGTH;
38 
39     private String mName;
40 
WifiTetherNamePreferenceController(Context context, String preferenceKey, FragmentController fragmentController, CarUxRestrictions uxRestrictions)41     public WifiTetherNamePreferenceController(Context context, String preferenceKey,
42             FragmentController fragmentController, CarUxRestrictions uxRestrictions) {
43         super(context, preferenceKey, fragmentController, uxRestrictions);
44     }
45 
46     @Override
getPreferenceType()47     protected Class<ValidatedEditTextPreference> getPreferenceType() {
48         return ValidatedEditTextPreference.class;
49     }
50 
51     @Override
onCreateInternal()52     protected void onCreateInternal() {
53         super.onCreateInternal();
54         getPreference().setValidator(NAME_VALIDATOR);
55         mName = getCarWifiApConfig().SSID;
56     }
57 
58     @Override
handlePreferenceChanged(ValidatedEditTextPreference preference, Object newValue)59     protected boolean handlePreferenceChanged(ValidatedEditTextPreference preference,
60             Object newValue) {
61         mName = newValue.toString();
62         updateSSID(mName);
63         refreshUi();
64         return true;
65     }
66 
67     @Override
updateState(ValidatedEditTextPreference preference)68     protected void updateState(ValidatedEditTextPreference preference) {
69         super.updateState(preference);
70         preference.setText(mName);
71     }
72 
updateSSID(String ssid)73     private void updateSSID(String ssid) {
74         WifiConfiguration config = getCarWifiApConfig();
75         config.SSID = ssid;
76         setCarWifiApConfig(config);
77     }
78 
79     @Override
getSummary()80     protected String getSummary() {
81         return mName;
82     }
83 
84     @Override
getDefaultSummary()85     protected String getDefaultSummary() {
86         return null;
87     }
88 }
89