summaryrefslogtreecommitdiff
path: root/Settings/src/com/android/tv/settings/connectivity/util/WifiSecurityUtil.java
blob: 9f90599926a11e4eb38f08811c7a586f63796830 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
/*
 * Copyright (C) 2017 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.android.tv.settings.connectivity.util;

import android.content.Context;
import android.net.wifi.ScanResult;
import android.net.wifi.WifiConfiguration;

import com.android.settingslib.wifi.AccessPoint;
import com.android.tv.settings.R;

/**
 * Used to get different wifi security types.
 */
public class WifiSecurityUtil {
    /**
     * Get security based on the {@link ScanResult}
     *
     * @param result {@link ScanResult}
     * @return the category of wifi security.
     */
    public static int getSecurity(ScanResult result) {
        if (result.capabilities.contains("WEP")) {
            return AccessPoint.SECURITY_WEP;
        } else if (result.capabilities.contains("PSK")) {
            return AccessPoint.SECURITY_PSK;
        } else if (result.capabilities.contains("EAP")) {
            return AccessPoint.SECURITY_EAP;
        }
        return AccessPoint.SECURITY_NONE;
    }

    /**
     * Get security based on {@link WifiConfiguration}
     *
     * @param config {@link WifiConfiguration}
     * @return the category of wifi security.
     */
    public static int getSecurity(WifiConfiguration config) {
        if (config.allowedKeyManagement.get(WifiConfiguration.KeyMgmt.SAE)) {
            return AccessPoint.SECURITY_SAE;
        }
        if (config.allowedKeyManagement.get(WifiConfiguration.KeyMgmt.WPA_PSK)) {
            return AccessPoint.SECURITY_PSK;
        }
        if (config.allowedKeyManagement.get(WifiConfiguration.KeyMgmt.SAE)) {
            return AccessPoint.SECURITY_SAE;
        }
        if (config.allowedKeyManagement.get(WifiConfiguration.KeyMgmt.WPA_EAP)
                || config.allowedKeyManagement.get(WifiConfiguration.KeyMgmt.IEEE8021X)) {
            return AccessPoint.SECURITY_EAP;
        }
        return (config.wepKeys[0] != null) ? AccessPoint.SECURITY_WEP : AccessPoint.SECURITY_NONE;
    }

    /**
     * Get the name of a specified wifi security.
     *
     * @param context      context of application.
     * @param wifiSecurity the value of wifiSecurity, defined in {@link AccessPoint}.
     * @return the name
     */
    public static String getName(Context context, int wifiSecurity) {
        switch (wifiSecurity) {
            case AccessPoint.SECURITY_WEP:
                return context.getString(R.string.wifi_security_type_wep);
            case AccessPoint.SECURITY_PSK:
                return context.getString(R.string.wifi_security_type_wpa);
            case AccessPoint.SECURITY_EAP:
                return context.getString(R.string.wifi_security_type_eap);
            case AccessPoint.SECURITY_NONE:
                return context.getString(R.string.wifi_security_type_none);
            default:
                return null;
        }
    }

    /**
     * Return whether wifiSecurity is open.
     *
     * @param wifiSecurity the value of wifiSecurity. Defined in {@link AccessPoint}.
     * @return true if open.
     */
    public static boolean isOpen(int wifiSecurity) {
        return wifiSecurity == AccessPoint.SECURITY_NONE;
    }
}