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.content.SharedPreferences;
23 import android.net.wifi.WifiConfiguration;
24 
25 import androidx.localbroadcastmanager.content.LocalBroadcastManager;
26 import androidx.preference.ListPreference;
27 
28 import com.android.car.settings.R;
29 import com.android.car.settings.common.FragmentController;
30 
31 /**
32  * Controls WiFi Hotspot Security Type configuration.
33  */
34 public class WifiTetherSecurityPreferenceController extends
35         WifiTetherBasePreferenceController<ListPreference> {
36 
37     public static final String KEY_SECURITY_TYPE = "KEY_SECURITY_TYPE";
38     public static final String ACTION_SECURITY_TYPE_CHANGED =
39             "com.android.car.settings.wifi.ACTION_WIFI_TETHER_SECURITY_TYPE_CHANGED";
40 
41     private int mSecurityType;
42 
WifiTetherSecurityPreferenceController(Context context, String preferenceKey, FragmentController fragmentController, CarUxRestrictions uxRestrictions)43     public WifiTetherSecurityPreferenceController(Context context, String preferenceKey,
44             FragmentController fragmentController, CarUxRestrictions uxRestrictions) {
45         super(context, preferenceKey, fragmentController, uxRestrictions);
46     }
47 
48     @Override
getPreferenceType()49     protected Class<ListPreference> getPreferenceType() {
50         return ListPreference.class;
51     }
52 
53     @Override
onCreateInternal()54     protected void onCreateInternal() {
55         super.onCreateInternal();
56         mSecurityType = getCarWifiApConfig().getAuthType();
57         getPreference().setEntries(
58                 getContext().getResources().getStringArray(R.array.wifi_tether_security));
59         String[] entryValues = {Integer.toString(WifiConfiguration.KeyMgmt.WPA2_PSK),
60                 Integer.toString(WifiConfiguration.KeyMgmt.NONE)};
61         getPreference().setEntryValues(entryValues);
62         getPreference().setValue(String.valueOf(mSecurityType));
63     }
64 
65     @Override
handlePreferenceChanged(ListPreference preference, Object newValue)66     protected boolean handlePreferenceChanged(ListPreference preference,
67             Object newValue) {
68         mSecurityType = Integer.parseInt(newValue.toString());
69         updateSecurityType();
70         refreshUi();
71         return true;
72     }
73 
74     @Override
updateState(ListPreference preference)75     protected void updateState(ListPreference preference) {
76         super.updateState(preference);
77         preference.setValue(Integer.toString(mSecurityType));
78     }
79 
80     @Override
getSummary()81     protected String getSummary() {
82         int stringResId = mSecurityType == WifiConfiguration.KeyMgmt.WPA2_PSK
83                 ? R.string.wifi_hotspot_wpa2_personal : R.string.wifi_hotspot_security_none;
84         return getContext().getString(stringResId);
85     }
86 
87     @Override
getDefaultSummary()88     protected String getDefaultSummary() {
89         return null;
90     }
91 
updateSecurityType()92     private void updateSecurityType() {
93         WifiConfiguration config = getCarWifiApConfig();
94         config.allowedKeyManagement.clear();
95         config.allowedKeyManagement.set(mSecurityType);
96 
97         if (mSecurityType == WifiConfiguration.KeyMgmt.NONE) {
98             config.preSharedKey = "";
99         } else {
100             config.preSharedKey = getSavedPassword();
101         }
102 
103         setCarWifiApConfig(config);
104         broadcastSecurityTypeChanged();
105     }
106 
broadcastSecurityTypeChanged()107     private void broadcastSecurityTypeChanged() {
108         Intent intent = new Intent(ACTION_SECURITY_TYPE_CHANGED);
109         intent.putExtra(KEY_SECURITY_TYPE, mSecurityType);
110         LocalBroadcastManager.getInstance(getContext()).sendBroadcast(intent);
111     }
112 
getSavedPassword()113     private String getSavedPassword() {
114         SharedPreferences sp = getContext().getSharedPreferences(
115                 WifiTetherPasswordPreferenceController.SHARED_PREFERENCE_PATH,
116                 Context.MODE_PRIVATE);
117         String savedPassword =
118                 sp.getString(WifiTetherPasswordPreferenceController.KEY_SAVED_PASSWORD,
119                         /* defaultValue= */ null);
120         return savedPassword;
121     }
122 }
123