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