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.SharedPreferences; 22 import android.net.wifi.SoftApConfiguration; 23 24 import androidx.preference.ListPreference; 25 26 import com.android.car.settings.R; 27 import com.android.car.settings.common.FragmentController; 28 29 /** 30 * Controls WiFi Hotspot Security Type configuration. 31 */ 32 public class WifiTetherSecurityPreferenceController extends 33 WifiTetherBasePreferenceController<ListPreference> { 34 35 protected static final String KEY_SECURITY_TYPE = 36 "com.android.car.settings.wifi.KEY_SECURITY_TYPE"; 37 38 private int mSecurityType; 39 40 private final SharedPreferences mSharedPreferences = getContext().getSharedPreferences( 41 WifiTetherPasswordPreferenceController.SHARED_PREFERENCE_PATH, 42 Context.MODE_PRIVATE); 43 WifiTetherSecurityPreferenceController(Context context, String preferenceKey, FragmentController fragmentController, CarUxRestrictions uxRestrictions)44 public WifiTetherSecurityPreferenceController(Context context, String preferenceKey, 45 FragmentController fragmentController, CarUxRestrictions uxRestrictions) { 46 super(context, preferenceKey, fragmentController, uxRestrictions); 47 } 48 49 @Override getPreferenceType()50 protected Class<ListPreference> getPreferenceType() { 51 return ListPreference.class; 52 } 53 54 @Override onCreateInternal()55 protected void onCreateInternal() { 56 super.onCreateInternal(); 57 mSecurityType = getCarSoftApConfig().getSecurityType(); 58 getPreference().setEntries( 59 getContext().getResources().getStringArray(R.array.wifi_tether_security)); 60 String[] entryValues = { 61 Integer.toString(SoftApConfiguration.SECURITY_TYPE_WPA2_PSK), 62 Integer.toString(SoftApConfiguration.SECURITY_TYPE_OPEN)}; 63 getPreference().setEntryValues(entryValues); 64 getPreference().setValue(String.valueOf(mSecurityType)); 65 } 66 67 @Override handlePreferenceChanged(ListPreference preference, Object newValue)68 protected boolean handlePreferenceChanged(ListPreference preference, 69 Object newValue) { 70 mSecurityType = Integer.parseInt(newValue.toString()); 71 // Rather than updating the ap config here, we will only update the security type shared 72 // preference. When the user confirms their selection by going back, the config will be 73 // updated by the WifiTetherPasswordPreferenceController. By updating the config in that 74 // controller, we avoid running into a transient state where the (securityType, passphrase) 75 // pair is invalid due to not being updated simultaneously. 76 mSharedPreferences.edit().putInt(KEY_SECURITY_TYPE, mSecurityType).commit(); 77 refreshUi(); 78 return true; 79 } 80 81 @Override updateState(ListPreference preference)82 protected void updateState(ListPreference preference) { 83 super.updateState(preference); 84 preference.setValue(Integer.toString(mSecurityType)); 85 } 86 87 @Override getSummary()88 protected String getSummary() { 89 int stringResId = mSecurityType == SoftApConfiguration.SECURITY_TYPE_WPA2_PSK 90 ? R.string.wifi_hotspot_wpa2_personal : R.string.wifi_hotspot_security_none; 91 return getContext().getString(stringResId); 92 } 93 94 @Override getDefaultSummary()95 protected String getDefaultSummary() { 96 return null; 97 } 98 } 99