• 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.annotation.NonNull;
20 import android.car.drivingstate.CarUxRestrictions;
21 import android.content.Context;
22 import android.content.SharedPreferences;
23 import android.net.wifi.SoftApCapability;
24 import android.net.wifi.SoftApConfiguration;
25 import android.net.wifi.WifiManager;
26 
27 import androidx.preference.ListPreference;
28 
29 import com.android.car.settings.R;
30 import com.android.car.settings.common.FragmentController;
31 import com.android.car.settings.common.Logger;
32 
33 import java.util.LinkedHashMap;
34 import java.util.Map;
35 
36 /**
37  * Controls WiFi Hotspot Security Type configuration.
38  */
39 public class WifiTetherSecurityPreferenceController extends
40         WifiTetherBasePreferenceController<ListPreference> implements WifiManager.SoftApCallback {
41 
42     protected static final String KEY_SECURITY_TYPE =
43             "com.android.car.settings.wifi.KEY_SECURITY_TYPE";
44 
45     private static final Logger LOG = new Logger(WifiTetherSecurityPreferenceController.class);
46 
47     private int mSecurityType;
48 
49     private boolean mIsWpa3Supported = true;
50 
51     private final SharedPreferences mSharedPreferences = getContext().getSharedPreferences(
52             WifiTetherPasswordPreferenceController.SHARED_PREFERENCE_PATH,
53             Context.MODE_PRIVATE);
54 
55     private final Map<Integer, String> mSecurityMap = new LinkedHashMap<>();
56 
WifiTetherSecurityPreferenceController(Context context, String preferenceKey, FragmentController fragmentController, CarUxRestrictions uxRestrictions)57     public WifiTetherSecurityPreferenceController(Context context, String preferenceKey,
58             FragmentController fragmentController, CarUxRestrictions uxRestrictions) {
59         super(context, preferenceKey, fragmentController, uxRestrictions);
60         String[] securityNames = getContext().getResources().getStringArray(
61                 R.array.wifi_tether_security);
62         String[] securityValues = getContext().getResources().getStringArray(
63                 R.array.wifi_tether_security_values);
64         for (int i = 0; i < securityNames.length; i++) {
65             mSecurityMap.put(Integer.parseInt(securityValues[i]), securityNames[i]);
66         }
67     }
68 
69     @Override
getPreferenceType()70     protected Class<ListPreference> getPreferenceType() {
71         return ListPreference.class;
72     }
73 
74     @Override
onCreateInternal()75     protected void onCreateInternal() {
76         super.onCreateInternal();
77         mSecurityType = getCarSoftApConfig().getSecurityType();
78         getCarWifiManager().registerSoftApCallback(getContext().getMainExecutor(), this);
79         updatePreferenceOptions();
80     }
81 
updatePreferenceOptions()82     private void updatePreferenceOptions() {
83         if (!mIsWpa3Supported) {
84             mSecurityMap.keySet()
85                     .removeIf(key -> key > SoftApConfiguration.SECURITY_TYPE_WPA2_PSK);
86         }
87 
88         getPreference().setEntries(mSecurityMap.values().toArray(new CharSequence[0]));
89         getPreference().setEntryValues(
90                 mSecurityMap.keySet().stream().map(Object::toString).toArray(CharSequence[]::new));
91         // Need to update the security type in case current mSecurityType
92         // is incompatible with the device
93         updateSecurityType(mSecurityType);
94         getPreference().setValue(String.valueOf(mSecurityType));
95     }
96 
97     @Override
handlePreferenceChanged(ListPreference preference, Object newValue)98     protected boolean handlePreferenceChanged(ListPreference preference,
99             Object newValue) {
100         updateSecurityType(Integer.parseInt(newValue.toString()));
101         // Rather than updating the ap config here, we will only update the security type shared
102         // preference. When the user confirms their selection by going back, the config will be
103         // updated by the WifiTetherPasswordPreferenceController. By updating the config in that
104         // controller, we avoid running into a transient state where the (securityType, passphrase)
105         // pair is invalid due to not being updated simultaneously.
106         mSharedPreferences.edit().putInt(KEY_SECURITY_TYPE, mSecurityType).commit();
107         refreshUi();
108         return true;
109     }
110 
updateSecurityType(int newValue)111     private void updateSecurityType(int newValue) {
112         mSecurityType = mSecurityMap.containsKey(newValue)
113                 ? newValue : SoftApConfiguration.SECURITY_TYPE_WPA2_PSK;
114     }
115 
116     @Override
updateState(ListPreference preference)117     protected void updateState(ListPreference preference) {
118         super.updateState(preference);
119         preference.setValue(Integer.toString(mSecurityType));
120     }
121 
122     @Override
getSummary()123     protected String getSummary() {
124         return mSecurityMap.containsKey(mSecurityType) ? mSecurityMap.get(mSecurityType)
125                 : getContext().getString(R.string.wifi_hotspot_security_none);
126     }
127 
128     @Override
getDefaultSummary()129     protected String getDefaultSummary() {
130         return null;
131     }
132 
133     @Override
onCapabilityChanged(@onNull SoftApCapability softApCapability)134     public void onCapabilityChanged(@NonNull SoftApCapability softApCapability) {
135         boolean isWpa3Supported =
136                 softApCapability.areFeaturesSupported(SoftApCapability.SOFTAP_FEATURE_WPA3_SAE);
137         if (!isWpa3Supported) {
138             LOG.i("WPA3 SAE is not supported on this device");
139         }
140         if (mIsWpa3Supported != isWpa3Supported) {
141             mIsWpa3Supported = isWpa3Supported;
142             updatePreferenceOptions();
143         }
144         getCarWifiManager().unregisterSoftApCallback(this);
145     }
146 }
147