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 23 import androidx.localbroadcastmanager.content.LocalBroadcastManager; 24 import androidx.preference.ListPreference; 25 26 import com.android.car.settings.R; 27 import com.android.car.settings.common.FragmentController; 28 import com.android.car.settings.common.PreferenceController; 29 import com.android.settingslib.wifi.AccessPoint; 30 31 import java.util.Arrays; 32 import java.util.HashMap; 33 import java.util.List; 34 import java.util.Map; 35 36 /** Business logic to select the security type when adding a hidden network. */ 37 public class NetworkSecurityPreferenceController extends PreferenceController<ListPreference> { 38 39 /** Action used in the {@link Intent} sent by the {@link LocalBroadcastManager}. */ 40 public static final String ACTION_SECURITY_CHANGE = 41 "com.android.car.settings.wifi.SecurityChangeAction"; 42 /** Key used to store the selected security type. */ 43 public static final String KEY_SECURITY_TYPE = "security_type"; 44 45 private static final Map<Integer, Integer> SECURITY_TYPE_TO_DESC_RES = 46 createSecurityTypeDescMap(); 47 48 private static final List<Integer> SECURITY_TYPES = Arrays.asList( 49 AccessPoint.SECURITY_NONE, 50 AccessPoint.SECURITY_WEP, 51 AccessPoint.SECURITY_PSK, 52 AccessPoint.SECURITY_EAP); 53 54 private CharSequence[] mSecurityTypeNames; 55 private CharSequence[] mSecurityTypeIds; 56 private int mSelectedSecurityType; 57 NetworkSecurityPreferenceController(Context context, String preferenceKey, FragmentController fragmentController, CarUxRestrictions uxRestrictions)58 public NetworkSecurityPreferenceController(Context context, String preferenceKey, 59 FragmentController fragmentController, CarUxRestrictions uxRestrictions) { 60 super(context, preferenceKey, fragmentController, uxRestrictions); 61 } 62 63 @Override getPreferenceType()64 protected Class<ListPreference> getPreferenceType() { 65 return ListPreference.class; 66 } 67 68 @Override onCreateInternal()69 protected void onCreateInternal() { 70 // Security type setup. 71 mSecurityTypeNames = new CharSequence[SECURITY_TYPES.size()]; 72 mSecurityTypeIds = new CharSequence[SECURITY_TYPES.size()]; 73 mSelectedSecurityType = AccessPoint.SECURITY_NONE; 74 75 for (int i = 0; i < SECURITY_TYPES.size(); i++) { 76 int type = SECURITY_TYPES.get(i); 77 mSecurityTypeNames[i] = getContext().getString(SECURITY_TYPE_TO_DESC_RES.get(type)); 78 mSecurityTypeIds[i] = Integer.toString(type); 79 } 80 81 getPreference().setEntries(mSecurityTypeNames); 82 getPreference().setEntryValues(mSecurityTypeIds); 83 getPreference().setDefaultValue(Integer.toString(AccessPoint.SECURITY_NONE)); 84 } 85 86 @Override updateState(ListPreference preference)87 protected void updateState(ListPreference preference) { 88 preference.setSummary(SECURITY_TYPE_TO_DESC_RES.get(mSelectedSecurityType)); 89 } 90 91 @Override handlePreferenceChanged(ListPreference preference, Object newValue)92 protected boolean handlePreferenceChanged(ListPreference preference, Object newValue) { 93 mSelectedSecurityType = Integer.parseInt(newValue.toString()); 94 notifySecurityChange(mSelectedSecurityType); 95 refreshUi(); 96 return true; 97 } 98 notifySecurityChange(int securityType)99 private void notifySecurityChange(int securityType) { 100 Intent intent = new Intent(ACTION_SECURITY_CHANGE); 101 intent.putExtra(KEY_SECURITY_TYPE, securityType); 102 LocalBroadcastManager.getInstance(getContext()).sendBroadcastSync(intent); 103 } 104 createSecurityTypeDescMap()105 private static Map<Integer, Integer> createSecurityTypeDescMap() { 106 Map<Integer, Integer> map = new HashMap<>(); 107 map.put(AccessPoint.SECURITY_NONE, R.string.wifi_security_none); 108 map.put(AccessPoint.SECURITY_WEP, R.string.wifi_security_wep); 109 map.put(AccessPoint.SECURITY_PSK, R.string.wifi_security_psk_generic); 110 map.put(AccessPoint.SECURITY_EAP, R.string.wifi_security_eap); 111 return map; 112 } 113 } 114