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 package com.android.tv.settings.connectivity; 17 18 import android.net.wifi.ScanResult; 19 import android.net.wifi.WifiConfiguration; 20 import android.text.TextUtils; 21 22 import com.android.settingslib.wifi.AccessPoint; 23 24 /** Helper class for Wifi configuration. */ 25 public class WifiUtils { 26 /** 27 * Gets security value from ScanResult. 28 * 29 * Duplicated method from {@link AccessPoint#getSecurity(ScanResult)}. 30 * TODO(b/120827021): Should be removed if the there is have a common one in shared place (e.g. 31 * SettingsLib). 32 * 33 * @param result ScanResult 34 * @return Related security value based on {@link AccessPoint}. 35 */ getAccessPointSecurity(ScanResult result)36 public static int getAccessPointSecurity(ScanResult result) { 37 if (result.capabilities.contains("WEP")) { 38 return AccessPoint.SECURITY_WEP; 39 } else if (result.capabilities.contains("SAE")) { 40 return AccessPoint.SECURITY_SAE; 41 } else if (result.capabilities.contains("PSK")) { 42 return AccessPoint.SECURITY_PSK; 43 } else if (result.capabilities.contains("EAP_SUITE_B_192")) { 44 return AccessPoint.SECURITY_EAP_SUITE_B; 45 } else if (result.capabilities.contains("EAP")) { 46 return AccessPoint.SECURITY_EAP; 47 } else if (result.capabilities.contains("OWE")) { 48 return AccessPoint.SECURITY_OWE; 49 } 50 51 return AccessPoint.SECURITY_NONE; 52 } 53 54 /** 55 * Provides a simple way to generate a new {@link WifiConfiguration} obj from 56 * {@link ScanResult} or {@link AccessPoint}. Either {@code accessPoint} or {@code scanResult 57 * } input should be not null for retrieving information, otherwise will throw 58 * IllegalArgumentException. 59 * This method prefers to take {@link AccessPoint} input in priority. Therefore this method 60 * will take {@link AccessPoint} input as preferred data extraction source when you input 61 * both {@link AccessPoint} and {@link ScanResult}, and ignore {@link ScanResult} input. 62 * 63 * Duplicated and simplified method from {@link WifiConfigController#getConfig()}. 64 * TODO(b/120827021): Should be removed if the there is have a common one in shared place (e.g. 65 * SettingsLib). 66 * 67 * @param accessPoint Input data for retrieving WifiConfiguration. 68 * @param scanResult Input data for retrieving WifiConfiguration. 69 * @return WifiConfiguration obj based on input. 70 */ getWifiConfig(AccessPoint accessPoint, ScanResult scanResult, String password)71 public static WifiConfiguration getWifiConfig(AccessPoint accessPoint, ScanResult scanResult, 72 String password) { 73 if (accessPoint == null && scanResult == null) { 74 throw new IllegalArgumentException( 75 "At least one of AccessPoint and ScanResult input is required."); 76 } 77 78 final WifiConfiguration config = new WifiConfiguration(); 79 final int security; 80 81 if (accessPoint == null) { 82 config.SSID = AccessPoint.convertToQuotedString(scanResult.SSID); 83 security = getAccessPointSecurity(scanResult); 84 } else { 85 if (!accessPoint.isSaved()) { 86 config.SSID = AccessPoint.convertToQuotedString( 87 accessPoint.getSsidStr()); 88 } else { 89 config.networkId = accessPoint.getConfig().networkId; 90 config.hiddenSSID = accessPoint.getConfig().hiddenSSID; 91 } 92 security = accessPoint.getSecurity(); 93 } 94 95 switch (security) { 96 case AccessPoint.SECURITY_NONE: 97 config.setSecurityParams(WifiConfiguration.SECURITY_TYPE_OPEN); 98 break; 99 100 case AccessPoint.SECURITY_WEP: 101 config.setSecurityParams(WifiConfiguration.SECURITY_TYPE_WEP); 102 if (!TextUtils.isEmpty(password)) { 103 int length = password.length(); 104 // WEP-40, WEP-104, and 256-bit WEP (WEP-232?) 105 if ((length == 10 || length == 26 || length == 58) 106 && password.matches("[0-9A-Fa-f]*")) { 107 config.wepKeys[0] = password; 108 } else { 109 config.wepKeys[0] = '"' + password + '"'; 110 } 111 } 112 break; 113 114 case AccessPoint.SECURITY_PSK: 115 config.setSecurityParams(WifiConfiguration.SECURITY_TYPE_PSK); 116 if (!TextUtils.isEmpty(password)) { 117 if (password.matches("[0-9A-Fa-f]{64}")) { 118 config.preSharedKey = password; 119 } else { 120 config.preSharedKey = '"' + password + '"'; 121 } 122 } 123 break; 124 125 case AccessPoint.SECURITY_EAP: 126 case AccessPoint.SECURITY_EAP_SUITE_B: 127 if (security == AccessPoint.SECURITY_EAP_SUITE_B) { 128 // allowedSuiteBCiphers will be set according to certificate type 129 config.setSecurityParams(WifiConfiguration.SECURITY_TYPE_EAP_SUITE_B); 130 } else { 131 config.setSecurityParams(WifiConfiguration.SECURITY_TYPE_EAP); 132 } 133 134 if (!TextUtils.isEmpty(password)) { 135 config.enterpriseConfig.setPassword(password); 136 } 137 break; 138 case AccessPoint.SECURITY_SAE: 139 config.setSecurityParams(WifiConfiguration.SECURITY_TYPE_SAE); 140 if (!TextUtils.isEmpty(password)) { 141 config.preSharedKey = '"' + password + '"'; 142 } 143 break; 144 145 case AccessPoint.SECURITY_OWE: 146 config.setSecurityParams(WifiConfiguration.SECURITY_TYPE_OWE); 147 break; 148 149 default: 150 break; 151 } 152 153 return config; 154 } 155 } 156