• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.SoftApConfiguration;
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         SoftApConfiguration config = getCarSoftApConfig();
56         if (config != null) {
57             mName = config.getSsid();
58         }
59     }
60 
61     @Override
handlePreferenceChanged(ValidatedEditTextPreference preference, Object newValue)62     protected boolean handlePreferenceChanged(ValidatedEditTextPreference preference,
63             Object newValue) {
64         mName = newValue.toString();
65         updateSSID(mName);
66         refreshUi();
67         return true;
68     }
69 
70     @Override
updateState(ValidatedEditTextPreference preference)71     protected void updateState(ValidatedEditTextPreference preference) {
72         super.updateState(preference);
73         preference.setText(mName);
74     }
75 
updateSSID(String ssid)76     private void updateSSID(String ssid) {
77         SoftApConfiguration config = getCarSoftApConfig();
78         if (config != null) {
79             config = new SoftApConfiguration.Builder(config)
80                     .setSsid(ssid)
81                     .build();
82             setCarSoftApConfig(config);
83         }
84     }
85 
86     @Override
getSummary()87     protected String getSummary() {
88         return mName;
89     }
90 
91     @Override
getDefaultSummary()92     protected String getDefaultSummary() {
93         return null;
94     }
95 }
96