• 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.content.Intent;
22 import android.net.wifi.WifiManager;
23 
24 import androidx.localbroadcastmanager.content.LocalBroadcastManager;
25 import androidx.preference.ListPreference;
26 
27 import com.android.car.settings.R;
28 import com.android.car.settings.common.FragmentController;
29 import com.android.car.settings.common.PreferenceController;
30 import com.android.wifitrackerlib.WifiEntry;
31 
32 import java.util.ArrayList;
33 import java.util.Arrays;
34 import java.util.HashMap;
35 import java.util.List;
36 import java.util.Map;
37 
38 /** Business logic to select the security type when adding a hidden network. */
39 public class NetworkSecurityPreferenceController extends PreferenceController<ListPreference> {
40 
41     /** Action used in the {@link Intent} sent by the {@link LocalBroadcastManager}. */
42     public static final String ACTION_SECURITY_CHANGE =
43             "com.android.car.settings.wifi.SecurityChangeAction";
44     /** Key used to store the selected security type. */
45     public static final String KEY_SECURITY_TYPE = "security_type";
46 
47     private static final Map<Integer, Integer> SECURITY_TYPE_TO_DESC_RES =
48             createSecurityTypeDescMap();
49 
50     private static final List<Integer> SECURITY_TYPES = Arrays.asList(
51             WifiEntry.SECURITY_NONE,
52             WifiEntry.SECURITY_WEP,
53             WifiEntry.SECURITY_PSK,
54             WifiEntry.SECURITY_EAP,
55             WifiEntry.SECURITY_SAE,
56             WifiEntry.SECURITY_OWE,
57             WifiEntry.SECURITY_EAP_SUITE_B);
58 
59     private CharSequence[] mSecurityTypeNames;
60     private CharSequence[] mSecurityTypeIds;
61     private int mSelectedSecurityType;
62 
NetworkSecurityPreferenceController(Context context, String preferenceKey, FragmentController fragmentController, CarUxRestrictions uxRestrictions)63     public NetworkSecurityPreferenceController(Context context, String preferenceKey,
64             FragmentController fragmentController, CarUxRestrictions uxRestrictions) {
65         super(context, preferenceKey, fragmentController, uxRestrictions);
66     }
67 
68     @Override
getPreferenceType()69     protected Class<ListPreference> getPreferenceType() {
70         return ListPreference.class;
71     }
72 
73     @Override
onCreateInternal()74     protected void onCreateInternal() {
75         // Security type setup.
76         List<String> securityTypeNamesList = new ArrayList<String>();
77         List<String> securityTypeIdsList = new ArrayList<String>();
78         WifiManager wifiManager = getContext().getSystemService(WifiManager.class);
79 
80         securityTypeNamesList.add(getContext().getString(
81                 SECURITY_TYPE_TO_DESC_RES.get(WifiEntry.SECURITY_NONE)));
82         securityTypeIdsList.add(Integer.toString(WifiEntry.SECURITY_NONE));
83 
84         if (wifiManager.isEnhancedOpenSupported()) {
85             securityTypeNamesList.add(getContext().getString(
86                     SECURITY_TYPE_TO_DESC_RES.get(WifiEntry.SECURITY_OWE)));
87             securityTypeIdsList.add(Integer.toString(WifiEntry.SECURITY_OWE));
88         }
89 
90         securityTypeNamesList.add(getContext().getString(
91                 SECURITY_TYPE_TO_DESC_RES.get(WifiEntry.SECURITY_WEP)));
92         securityTypeIdsList.add(Integer.toString(WifiEntry.SECURITY_WEP));
93 
94         securityTypeNamesList.add(getContext().getString(
95                 SECURITY_TYPE_TO_DESC_RES.get(WifiEntry.SECURITY_PSK)));
96         securityTypeIdsList.add(Integer.toString(WifiEntry.SECURITY_PSK));
97 
98         if (wifiManager.isWpa3SaeSupported()) {
99             securityTypeNamesList.add(getContext().getString(
100                     SECURITY_TYPE_TO_DESC_RES.get(WifiEntry.SECURITY_SAE)));
101             securityTypeIdsList.add(Integer.toString(WifiEntry.SECURITY_SAE));
102         }
103 
104         securityTypeNamesList.add(getContext().getString(
105                 SECURITY_TYPE_TO_DESC_RES.get(WifiEntry.SECURITY_EAP)));
106         securityTypeIdsList.add(Integer.toString(WifiEntry.SECURITY_EAP));
107 
108         if (wifiManager.isWpa3SuiteBSupported()) {
109             securityTypeNamesList.add(getContext().getString(
110                     SECURITY_TYPE_TO_DESC_RES.get(WifiEntry.SECURITY_EAP_SUITE_B)));
111             securityTypeIdsList.add(Integer.toString(WifiEntry.SECURITY_EAP_SUITE_B));
112         }
113 
114         mSelectedSecurityType = WifiEntry.SECURITY_NONE;
115         mSecurityTypeNames = new CharSequence[securityTypeNamesList.size()];
116         mSecurityTypeNames = securityTypeNamesList.toArray(mSecurityTypeNames);
117         mSecurityTypeIds = new CharSequence[securityTypeIdsList.size()];
118         mSecurityTypeIds = securityTypeIdsList.toArray(mSecurityTypeIds);
119 
120         getPreference().setEntries(mSecurityTypeNames);
121         getPreference().setEntryValues(mSecurityTypeIds);
122         getPreference().setDefaultValue(Integer.toString(WifiEntry.SECURITY_NONE));
123     }
124 
125     @Override
updateState(ListPreference preference)126     protected void updateState(ListPreference preference) {
127         preference.setSummary(SECURITY_TYPE_TO_DESC_RES.get(mSelectedSecurityType));
128     }
129 
130     @Override
handlePreferenceChanged(ListPreference preference, Object newValue)131     protected boolean handlePreferenceChanged(ListPreference preference, Object newValue) {
132         mSelectedSecurityType = Integer.parseInt(newValue.toString());
133         notifySecurityChange(mSelectedSecurityType);
134         refreshUi();
135         return true;
136     }
137 
notifySecurityChange(int securityType)138     private void notifySecurityChange(int securityType) {
139         Intent intent = new Intent(ACTION_SECURITY_CHANGE);
140         intent.putExtra(KEY_SECURITY_TYPE, securityType);
141         LocalBroadcastManager.getInstance(getContext()).sendBroadcastSync(intent);
142     }
143 
createSecurityTypeDescMap()144     private static Map<Integer, Integer> createSecurityTypeDescMap() {
145         Map<Integer, Integer> map = new HashMap<>();
146         map.put(WifiEntry.SECURITY_NONE, R.string.wifi_security_none);
147         map.put(WifiEntry.SECURITY_WEP, R.string.wifi_security_wep);
148         map.put(WifiEntry.SECURITY_PSK, R.string.wifi_security_psk_generic);
149         map.put(WifiEntry.SECURITY_EAP, R.string.wifi_security_eap);
150         map.put(WifiEntry.SECURITY_SAE, R.string.wifi_security_sae);
151         map.put(WifiEntry.SECURITY_OWE, R.string.wifi_security_owe);
152         map.put(WifiEntry.SECURITY_EAP_SUITE_B, R.string.wifi_security_eap_suiteb);
153         return map;
154     }
155 }
156