1 /*
2  * Copyright (C) 2014 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;
18 
19 import com.android.tv.settings.R;
20 
21 import android.content.Context;
22 import android.net.wifi.ScanResult;
23 import android.net.wifi.WifiConfiguration;
24 import android.net.wifi.WifiConfiguration.KeyMgmt;
25 
26 /**
27  * Used to identify different wifi security types
28  */
29 public enum WifiSecurity {
30     WEP(R.string.wifi_security_type_wep),
31     PSK(R.string.wifi_security_type_wpa),
32     EAP(R.string.wifi_security_type_eap),
33     NONE(R.string.wifi_security_type_none);
34 
getSecurity(ScanResult result)35     public static WifiSecurity getSecurity(ScanResult result) {
36         if (result.capabilities.contains("WEP")) {
37             return WEP;
38         } else if (result.capabilities.contains("PSK")) {
39             return PSK;
40         } else if (result.capabilities.contains("EAP")) {
41             return EAP;
42         }
43         return NONE;
44     }
45 
getSecurity(WifiConfiguration config)46     public static WifiSecurity getSecurity(WifiConfiguration config) {
47         if (config.allowedKeyManagement.get(KeyMgmt.WPA_PSK)) {
48             return PSK;
49         }
50         if (config.allowedKeyManagement.get(KeyMgmt.WPA_EAP)
51                 || config.allowedKeyManagement.get(KeyMgmt.IEEE8021X)) {
52             return EAP;
53         }
54         return (config.wepKeys[0] != null) ? WEP : NONE;
55     }
56 
getSecurity(Context context, String name)57     public static WifiSecurity getSecurity(Context context, String name) {
58         if (EAP.getName(context).equals(name)) {
59             return EAP;
60         } else if (WEP.getName(context).equals(name)) {
61             return WEP;
62         } else if (PSK.getName(context).equals(name)) {
63             return PSK;
64         } else {
65             return NONE;
66         }
67     }
68 
isOpen(ScanResult result)69     public static boolean isOpen(ScanResult result) {
70         return WifiSecurity.NONE == getSecurity(result);
71     }
72 
isOpen(WifiConfiguration config)73     public static boolean isOpen(WifiConfiguration config) {
74         return WifiSecurity.NONE == getSecurity(config);
75     }
76 
77 
78     private final int mNameResourceId;
79 
WifiSecurity(int nameResourceId)80     private WifiSecurity(int nameResourceId) {
81         mNameResourceId = nameResourceId;
82     }
83 
getName(Context context)84     public String getName(Context context) {
85         return context.getString(mNameResourceId);
86     }
87 
isOpen()88     public boolean isOpen() {
89         return this == NONE;
90     }
91 }
92