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 android.content.Context;
2065a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Laneimport android.net.wifi.ScanResult;
2165a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Laneimport android.net.wifi.WifiConfiguration;
227e552863cea67e57b375f3db15e0baf70e22c39eTony Mantler
237e552863cea67e57b375f3db15e0baf70e22c39eTony Mantlerimport com.android.settingslib.wifi.AccessPoint;
247e552863cea67e57b375f3db15e0baf70e22c39eTony Mantlerimport com.android.tv.settings.R;
2565a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane
2665a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane/**
2765a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane * Used to identify different wifi security types
287e552863cea67e57b375f3db15e0baf70e22c39eTony Mantler * TODO: delete this
2965a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane */
3065a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lanepublic enum WifiSecurity {
3165a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane    WEP(R.string.wifi_security_type_wep),
3265a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane    PSK(R.string.wifi_security_type_wpa),
3365a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane    EAP(R.string.wifi_security_type_eap),
3465a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane    NONE(R.string.wifi_security_type_none);
3565a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane
3665a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane    public static WifiSecurity getSecurity(ScanResult result) {
3765a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        if (result.capabilities.contains("WEP")) {
3865a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane            return WEP;
3965a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        } else if (result.capabilities.contains("PSK")) {
4065a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane            return PSK;
4165a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        } else if (result.capabilities.contains("EAP")) {
4265a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane            return EAP;
4365a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        }
4465a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        return NONE;
4565a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane    }
4665a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane
4765a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane    public static WifiSecurity getSecurity(WifiConfiguration config) {
487e552863cea67e57b375f3db15e0baf70e22c39eTony Mantler        if (config.allowedKeyManagement.get(WifiConfiguration.KeyMgmt.WPA_PSK)) {
4965a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane            return PSK;
5065a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        }
517e552863cea67e57b375f3db15e0baf70e22c39eTony Mantler        if (config.allowedKeyManagement.get(WifiConfiguration.KeyMgmt.WPA_EAP)
527e552863cea67e57b375f3db15e0baf70e22c39eTony Mantler                || config.allowedKeyManagement.get(WifiConfiguration.KeyMgmt.IEEE8021X)) {
5365a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane            return EAP;
5465a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        }
5565a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        return (config.wepKeys[0] != null) ? WEP : NONE;
5665a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane    }
5765a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane
587e552863cea67e57b375f3db15e0baf70e22c39eTony Mantler    public static WifiSecurity getSecurity(AccessPoint accessPoint) {
597e552863cea67e57b375f3db15e0baf70e22c39eTony Mantler        switch (accessPoint.getSecurity()) {
607e552863cea67e57b375f3db15e0baf70e22c39eTony Mantler            case AccessPoint.SECURITY_WEP: return WEP;
617e552863cea67e57b375f3db15e0baf70e22c39eTony Mantler            case AccessPoint.SECURITY_PSK: return PSK;
627e552863cea67e57b375f3db15e0baf70e22c39eTony Mantler            case AccessPoint.SECURITY_EAP: return EAP;
637e552863cea67e57b375f3db15e0baf70e22c39eTony Mantler            case AccessPoint.SECURITY_NONE: return NONE;
6465a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        }
6565a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane
667e552863cea67e57b375f3db15e0baf70e22c39eTony Mantler        throw new IllegalArgumentException("Unknown security type");
6765a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane    }
6865a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane
6965a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane    private final int mNameResourceId;
7065a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane
7165a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane    private WifiSecurity(int nameResourceId) {
7265a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        mNameResourceId = nameResourceId;
7365a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane    }
7465a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane
7565a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane    public String getName(Context context) {
7665a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        return context.getString(mNameResourceId);
7765a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane    }
7865a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane
7965a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane    public boolean isOpen() {
8065a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        return this == NONE;
8165a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane    }
8265a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane}
83