1 /*
2  * Copyright (C) 2017 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.util;
18 
19 import android.content.Context;
20 import android.net.wifi.ScanResult;
21 import android.net.wifi.WifiConfiguration;
22 
23 import com.android.tv.settings.library.network.AccessPoint;
24 import com.android.tv.settings.R;
25 
26 /**
27  * Used to get different wifi security types.
28  */
29 public class WifiSecurityUtil {
30     /**
31      * Get security based on the {@link ScanResult}
32      *
33      * @param result {@link ScanResult}
34      * @return the category of wifi security.
35      */
getSecurity(ScanResult result)36     public static int getSecurity(ScanResult result) {
37         if (result.capabilities.contains("WEP")) {
38             return AccessPoint.SECURITY_WEP;
39         } else if (result.capabilities.contains("PSK")) {
40             return AccessPoint.SECURITY_PSK;
41         } else if (result.capabilities.contains("EAP")) {
42             return AccessPoint.SECURITY_EAP;
43         }
44         return AccessPoint.SECURITY_NONE;
45     }
46 
47     /**
48      * Get security based on {@link WifiConfiguration}
49      *
50      * @param config {@link WifiConfiguration}
51      * @return the category of wifi security.
52      */
getSecurity(WifiConfiguration config)53     public static int getSecurity(WifiConfiguration config) {
54         if (config.allowedKeyManagement.get(WifiConfiguration.KeyMgmt.SAE)) {
55             return AccessPoint.SECURITY_SAE;
56         }
57         if (config.allowedKeyManagement.get(WifiConfiguration.KeyMgmt.WPA_PSK)) {
58             return AccessPoint.SECURITY_PSK;
59         }
60         if (config.allowedKeyManagement.get(WifiConfiguration.KeyMgmt.SAE)) {
61             return AccessPoint.SECURITY_SAE;
62         }
63         if (config.allowedKeyManagement.get(WifiConfiguration.KeyMgmt.WPA_EAP)
64                 || config.allowedKeyManagement.get(WifiConfiguration.KeyMgmt.IEEE8021X)) {
65             return AccessPoint.SECURITY_EAP;
66         }
67         return (config.wepKeys[0] != null) ? AccessPoint.SECURITY_WEP : AccessPoint.SECURITY_NONE;
68     }
69 
70     /**
71      * Get the name of a specified wifi security.
72      *
73      * @param context      context of application.
74      * @param wifiSecurity the value of wifiSecurity, defined in {@link AccessPoint}.
75      * @return the name
76      */
getName(Context context, int wifiSecurity)77     public static String getName(Context context, int wifiSecurity) {
78         switch (wifiSecurity) {
79             case AccessPoint.SECURITY_WEP:
80                 return context.getString(R.string.wifi_security_type_wep);
81             case AccessPoint.SECURITY_PSK:
82                 return context.getString(R.string.wifi_security_type_wpa);
83             case AccessPoint.SECURITY_EAP:
84                 return context.getString(R.string.wifi_security_type_eap);
85             case AccessPoint.SECURITY_NONE:
86                 return context.getString(R.string.wifi_security_type_none);
87             default:
88                 return null;
89         }
90     }
91 
92     /**
93      * Return whether wifiSecurity is open.
94      *
95      * @param wifiSecurity the value of wifiSecurity. Defined in {@link AccessPoint}.
96      * @return true if open.
97      */
isOpen(int wifiSecurity)98     public static boolean isOpen(int wifiSecurity) {
99         return wifiSecurity == AccessPoint.SECURITY_NONE;
100     }
101 
isEnhancedOpen(int wifiSecurity)102     public static boolean isEnhancedOpen(int wifiSecurity) {
103         return wifiSecurity == AccessPoint.SECURITY_OWE;
104     }
105 }
106