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.WifiInfo; 22 import android.net.wifi.WifiManager; 23 24 import androidx.fragment.app.FragmentActivity; 25 import androidx.leanback.widget.GuidedAction; 26 import androidx.lifecycle.ViewModelProviders; 27 28 import com.android.tv.settings.R; 29 import com.android.tv.settings.connectivity.WifiConfigHelper; 30 import com.android.tv.settings.connectivity.setup.AdvancedOptionsFlowInfo; 31 import com.android.tv.settings.connectivity.setup.UserChoiceInfo; 32 import com.android.tv.settings.library.network.AccessPoint; 33 import com.android.wifitrackerlib.WifiEntry; 34 35 import java.util.ArrayList; 36 import java.util.List; 37 38 /** Helper class to handle Wi-Fi security */ 39 public class WifiSecurityHelper { getSecurityTypes(Context context)40 public static List<GuidedAction> getSecurityTypes(Context context) { 41 WifiManager wifiManager = context.getSystemService(WifiManager.class); 42 List<GuidedAction> securityTypes = new ArrayList<>(); 43 securityTypes.add(new GuidedAction.Builder(context) 44 .title(context.getString(R.string.wifi_security_type_none)) 45 .id(AccessPoint.SECURITY_NONE) 46 .build()); 47 if (wifiManager.isEnhancedOpenSupported()) { 48 securityTypes.add(new GuidedAction.Builder(context) 49 .title(context.getString(R.string.wifi_security_owe)) 50 .id(AccessPoint.SECURITY_OWE) 51 .build()); 52 } 53 securityTypes.add(new GuidedAction.Builder(context) 54 .title(context.getString(R.string.wifi_security_wep)) 55 .id(AccessPoint.SECURITY_WEP) 56 .build()); 57 securityTypes.add(new GuidedAction.Builder(context) 58 .title(context.getString(R.string.wifi_security_wpa_wpa2)) 59 .id(AccessPoint.SECURITY_PSK) 60 .build()); 61 if (wifiManager.isWpa3SaeSupported()) { 62 securityTypes.add(new GuidedAction.Builder(context) 63 .title(context.getString(R.string.wifi_security_sae)) 64 .id(AccessPoint.SECURITY_SAE) 65 .build()); 66 } 67 return securityTypes; 68 } 69 getSsid(FragmentActivity context)70 public static String getSsid(FragmentActivity context) { 71 UserChoiceInfo userChoiceInfo = ViewModelProviders.of(context).get(UserChoiceInfo.class); 72 String savedSsid = userChoiceInfo.getPageSummary(UserChoiceInfo.SSID); 73 return savedSsid != null 74 ? savedSsid : userChoiceInfo.getWifiConfiguration().getPrintableSsid(); 75 } 76 getSecurity(FragmentActivity context)77 public static Integer getSecurity(FragmentActivity context) { 78 UserChoiceInfo userChoiceInfo = ViewModelProviders.of(context).get(UserChoiceInfo.class); 79 Integer savedSecurity = userChoiceInfo.getChoice(UserChoiceInfo.SECURITY); 80 return savedSecurity != null ? savedSecurity : userChoiceInfo.getWifiSecurity(); 81 } 82 getConfig(FragmentActivity context)83 public static WifiConfiguration getConfig(FragmentActivity context) { 84 UserChoiceInfo userChoiceInfo = ViewModelProviders.of(context).get(UserChoiceInfo.class); 85 AdvancedOptionsFlowInfo advancedOptionsFlowInfo = ViewModelProviders.of(context).get( 86 AdvancedOptionsFlowInfo.class); 87 WifiConfiguration config = userChoiceInfo.getWifiConfiguration(); 88 String ssid = userChoiceInfo.getPageSummary(UserChoiceInfo.SSID); 89 if (ssid != null) { 90 config.SSID = AccessPoint.convertToQuotedString( 91 userChoiceInfo.getPageSummary(UserChoiceInfo.SSID)); 92 } 93 Integer security = userChoiceInfo.getChoice(UserChoiceInfo.SECURITY); 94 if (security != null) { 95 WifiConfigHelper.setConfigKeyManagementBySecurity(config, security); 96 } 97 98 String password = userChoiceInfo.getPageSummary(UserChoiceInfo.PASSWORD); 99 int passwordLength = password != null ? password.length() : 0; 100 101 if (passwordLength > 0) { 102 switch (getSecurity(context)) { 103 case WifiInfo.SECURITY_TYPE_WEP: 104 // WEP-40, WEP-104, and 256-bit WEP (WEP-232?) 105 if ((passwordLength == 10 || passwordLength == 26 || passwordLength == 58) 106 && password.matches("[0-9A-Fa-f]*")) { 107 config.wepKeys[0] = password; 108 } else { 109 config.wepKeys[0] = '"' + password + '"'; 110 } 111 break; 112 case WifiInfo.SECURITY_TYPE_PSK: 113 if (password.matches("[0-9A-Fa-f]{64}")) { 114 config.preSharedKey = password; 115 } else { 116 config.preSharedKey = '"' + password + '"'; 117 } 118 break; 119 case WifiInfo.SECURITY_TYPE_SAE: 120 config.preSharedKey = '"' + password + '"'; 121 break; 122 } 123 } 124 125 if (advancedOptionsFlowInfo.getIpConfiguration() != null) { 126 config.setIpConfiguration(advancedOptionsFlowInfo.getIpConfiguration()); 127 } 128 return config; 129 } 130 } 131