• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.settingslib.wifi.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.WPA_PSK)) {
55             return AccessPoint.SECURITY_PSK;
56         }
57         if (config.allowedKeyManagement.get(WifiConfiguration.KeyMgmt.WPA_EAP)
58                 || config.allowedKeyManagement.get(WifiConfiguration.KeyMgmt.IEEE8021X)) {
59             return AccessPoint.SECURITY_EAP;
60         }
61         return (config.wepKeys[0] != null) ? AccessPoint.SECURITY_WEP : AccessPoint.SECURITY_NONE;
62     }
63 
64     /**
65      * Get the name of a specified wifi security.
66      *
67      * @param context      context of application.
68      * @param wifiSecurity the value of wifiSecurity, defined in {@link AccessPoint}.
69      * @return the name
70      */
getName(Context context, int wifiSecurity)71     public static String getName(Context context, int wifiSecurity) {
72         switch (wifiSecurity) {
73             case AccessPoint.SECURITY_WEP:
74                 return context.getString(R.string.wifi_security_type_wep);
75             case AccessPoint.SECURITY_PSK:
76                 return context.getString(R.string.wifi_security_type_wpa);
77             case AccessPoint.SECURITY_EAP:
78                 return context.getString(R.string.wifi_security_type_eap);
79             case AccessPoint.SECURITY_NONE:
80                 return context.getString(R.string.wifi_security_type_none);
81             default:
82                 return null;
83         }
84     }
85 
86     /**
87      * Return whether wifiSecurity is open.
88      *
89      * @param wifiSecurity the value of wifiSecurity. Defined in {@link AccessPoint}.
90      * @return true if open.
91      */
isOpen(int wifiSecurity)92     public static boolean isOpen(int wifiSecurity) {
93         return wifiSecurity == AccessPoint.SECURITY_NONE;
94     }
95 }
96