1 /* 2 * Copyright (C) 2020 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.tv.settings.connectivity.security; 18 19 import android.content.Context; 20 import android.net.wifi.WifiConfiguration; 21 import android.net.wifi.WifiManager; 22 import android.text.TextUtils; 23 24 import androidx.fragment.app.FragmentActivity; 25 import androidx.leanback.widget.GuidedAction; 26 import androidx.lifecycle.ViewModelProviders; 27 28 import com.android.settingslib.wifi.AccessPoint; 29 import com.android.tv.settings.R; 30 import com.android.tv.settings.connectivity.setup.AdvancedOptionsFlowInfo; 31 import com.android.tv.settings.connectivity.setup.UserChoiceInfo; 32 33 import java.util.ArrayList; 34 import java.util.List; 35 36 /** Helper class to handle Wi-Fi security */ 37 public class WifiSecurityHelper { 38 39 private static final String TAG = "WifiSecurityConfigInfo"; 40 getSecurityTypes(Context context)41 public static List<GuidedAction> getSecurityTypes(Context context) { 42 WifiManager wifiManager = context.getSystemService(WifiManager.class); 43 List<GuidedAction> securityTypes = new ArrayList<>(); 44 securityTypes.add(new GuidedAction.Builder(context) 45 .title(context.getString(R.string.wifi_security_type_none)) 46 .id(AccessPoint.SECURITY_NONE) 47 .build()); 48 if (wifiManager.isEnhancedOpenSupported()) { 49 securityTypes.add(new GuidedAction.Builder(context) 50 .title(context.getString(R.string.wifi_security_owe)) 51 .id(AccessPoint.SECURITY_OWE) 52 .build()); 53 } 54 securityTypes.add(new GuidedAction.Builder(context) 55 .title(context.getString(R.string.wifi_security_wep)) 56 .id(AccessPoint.SECURITY_WEP) 57 .build()); 58 securityTypes.add(new GuidedAction.Builder(context) 59 .title(context.getString(R.string.wifi_security_wpa_wpa2)) 60 .id(AccessPoint.SECURITY_PSK) 61 .build()); 62 if (wifiManager.isWpa3SaeSupported()) { 63 securityTypes.add(new GuidedAction.Builder(context) 64 .title(context.getString(R.string.wifi_security_sae)) 65 .id(AccessPoint.SECURITY_SAE) 66 .build()); 67 } 68 return securityTypes; 69 } 70 getActionsList(FragmentActivity context, @UserChoiceInfo.PAGE int page)71 public static List<GuidedAction> getActionsList(FragmentActivity context, 72 @UserChoiceInfo.PAGE int page) { 73 switch (page) { 74 case UserChoiceInfo.SECURITY: 75 return getSecurityTypes(context); 76 default: 77 return null; 78 } 79 } 80 getSsid(FragmentActivity context)81 public static String getSsid(FragmentActivity context) { 82 UserChoiceInfo userChoiceInfo = ViewModelProviders.of(context).get(UserChoiceInfo.class); 83 String savedSsid = userChoiceInfo.getPageSummary(UserChoiceInfo.SSID); 84 return savedSsid != null 85 ? savedSsid : userChoiceInfo.getWifiConfiguration().getPrintableSsid(); 86 } 87 getSecurity(FragmentActivity context)88 public static Integer getSecurity(FragmentActivity context) { 89 UserChoiceInfo userChoiceInfo = ViewModelProviders.of(context).get(UserChoiceInfo.class); 90 Integer savedSecurity = userChoiceInfo.getChoice(UserChoiceInfo.SECURITY); 91 return savedSecurity != null ? savedSecurity : userChoiceInfo.getWifiSecurity(); 92 } 93 getConfig(FragmentActivity context)94 public static WifiConfiguration getConfig(FragmentActivity context) { 95 UserChoiceInfo userChoiceInfo = ViewModelProviders.of(context).get(UserChoiceInfo.class); 96 AdvancedOptionsFlowInfo advancedOptionsFlowInfo = ViewModelProviders.of(context).get( 97 AdvancedOptionsFlowInfo.class); 98 WifiConfiguration config = userChoiceInfo.getWifiConfiguration(); 99 100 if (TextUtils.isEmpty(config.SSID)) { 101 // If the user adds a network manually, assume that it is hidden. 102 config.hiddenSSID = true; 103 } 104 105 if (userChoiceInfo.getPageSummary(UserChoiceInfo.SSID) != null) { 106 config.SSID = AccessPoint.convertToQuotedString( 107 userChoiceInfo.getPageSummary(UserChoiceInfo.SSID)); 108 } 109 int accessPointSecurity = getSecurity(context); 110 String password = userChoiceInfo.getPageSummary(UserChoiceInfo.PASSWORD); 111 int length = password != null ? password.length() : 0; 112 113 switch (accessPointSecurity) { 114 case AccessPoint.SECURITY_NONE: 115 config.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE); 116 break; 117 118 case AccessPoint.SECURITY_WEP: 119 config.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE); 120 config.allowedAuthAlgorithms.set(WifiConfiguration.AuthAlgorithm.OPEN); 121 config.allowedAuthAlgorithms.set(WifiConfiguration.AuthAlgorithm.SHARED); 122 // WEP-40, WEP-104, and 256-bit WEP (WEP-232?) 123 if ((length == 10 || length == 26 || length == 58) 124 && password.matches("[0-9A-Fa-f]*")) { 125 config.wepKeys[0] = password; 126 } else { 127 config.wepKeys[0] = '"' + password + '"'; 128 } 129 break; 130 131 case AccessPoint.SECURITY_PSK: 132 config.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.WPA_PSK); 133 if (length != 0) { 134 if (password.matches("[0-9A-Fa-f]{64}")) { 135 config.preSharedKey = password; 136 } else { 137 config.preSharedKey = '"' + password + '"'; 138 } 139 } 140 break; 141 142 case AccessPoint.SECURITY_SAE: 143 config.setSecurityParams(WifiConfiguration.SECURITY_TYPE_SAE); 144 if (password.length() != 0) { 145 config.preSharedKey = '"' + password + '"'; 146 } 147 break; 148 case AccessPoint.SECURITY_OWE: 149 config.setSecurityParams(WifiConfiguration.SECURITY_TYPE_OWE); 150 break; 151 152 default: 153 return null; 154 } 155 156 config.setIpConfiguration(advancedOptionsFlowInfo.getIpConfiguration()); 157 return config; 158 } 159 } 160