165a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane/*
265a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane * Copyright (C) 2014 The Android Open Source Project
365a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane *
465a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane * Licensed under the Apache License, Version 2.0 (the "License");
565a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane * you may not use this file except in compliance with the License.
665a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane * You may obtain a copy of the License at
765a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane *
865a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane *      http://www.apache.org/licenses/LICENSE-2.0
965a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane *
1065a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane * Unless required by applicable law or agreed to in writing, software
1165a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane * distributed under the License is distributed on an "AS IS" BASIS,
1265a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1365a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane * See the License for the specific language governing permissions and
1465a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane * limitations under the License.
1565a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane */
1665a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane
1765a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lanepackage com.android.tv.settings.connectivity;
1865a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane
1965a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Laneimport com.android.tv.settings.R;
2065a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane
2165a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Laneimport android.content.Context;
2265a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Laneimport android.net.wifi.ScanResult;
2365a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Laneimport android.net.wifi.WifiConfiguration;
2465a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Laneimport android.net.wifi.WifiConfiguration.KeyMgmt;
2565a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane
2665a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane/**
2765a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane * Used to identify different wifi security types
2865a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane */
2965a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lanepublic enum WifiSecurity {
3065a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane    WEP(R.string.wifi_security_type_wep),
3165a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane    PSK(R.string.wifi_security_type_wpa),
3265a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane    EAP(R.string.wifi_security_type_eap),
3365a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane    NONE(R.string.wifi_security_type_none);
3465a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane
3565a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane    public static WifiSecurity getSecurity(ScanResult result) {
3665a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        if (result.capabilities.contains("WEP")) {
3765a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane            return WEP;
3865a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        } else if (result.capabilities.contains("PSK")) {
3965a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane            return PSK;
4065a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        } else if (result.capabilities.contains("EAP")) {
4165a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane            return EAP;
4265a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        }
4365a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        return NONE;
4465a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane    }
4565a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane
4665a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane    public static WifiSecurity getSecurity(WifiConfiguration config) {
4765a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        if (config.allowedKeyManagement.get(KeyMgmt.WPA_PSK)) {
4865a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane            return PSK;
4965a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        }
5065a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        if (config.allowedKeyManagement.get(KeyMgmt.WPA_EAP)
5165a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane                || config.allowedKeyManagement.get(KeyMgmt.IEEE8021X)) {
5265a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane            return EAP;
5365a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        }
5465a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        return (config.wepKeys[0] != null) ? WEP : NONE;
5565a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane    }
5665a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane
5765a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane    public static WifiSecurity getSecurity(Context context, String name) {
5865a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        if (EAP.getName(context).equals(name)) {
5965a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane            return EAP;
6065a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        } else if (WEP.getName(context).equals(name)) {
6165a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane            return WEP;
6265a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        } else if (PSK.getName(context).equals(name)) {
6365a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane            return PSK;
6465a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        } else {
6565a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane            return NONE;
6665a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        }
6765a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane    }
6865a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane
6965a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane    public static boolean isOpen(ScanResult result) {
7065a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        return WifiSecurity.NONE == getSecurity(result);
7165a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane    }
7265a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane
7365a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane    public static boolean isOpen(WifiConfiguration config) {
7465a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        return WifiSecurity.NONE == getSecurity(config);
7565a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane    }
7665a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane
7765a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane
7865a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane    private final int mNameResourceId;
7965a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane
8065a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane    private WifiSecurity(int nameResourceId) {
8165a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        mNameResourceId = nameResourceId;
8265a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane    }
8365a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane
8465a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane    public String getName(Context context) {
8565a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        return context.getString(mNameResourceId);
8665a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane    }
8765a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane
8865a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane    public boolean isOpen() {
8965a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        return this == NONE;
9065a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane    }
9165a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane}
92