1/*
2 * Copyright 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.managedprovisioning;
18
19import android.net.ProxyInfo;
20import android.net.IpConfiguration.ProxySettings;
21import android.net.wifi.WifiConfiguration;
22import android.net.wifi.WifiManager;
23import android.text.TextUtils;
24
25/**
26 * Utility class for configuring a new WiFi network.
27 */
28public class WifiConfig {
29
30    private final WifiManager mWifiManager;
31
32    enum SecurityType {
33        NONE,
34        WPA,
35        WEP;
36    }
37
38    public WifiConfig(WifiManager manager) {
39        mWifiManager = manager;
40    }
41
42    /**
43     * Adds a new WiFi network.
44     */
45    public int addNetwork(String ssid, boolean hidden, String type, String password,
46            String proxyHost, int proxyPort, String proxyBypassHosts, String pacUrl) {
47        if (!mWifiManager.isWifiEnabled()) {
48            mWifiManager.setWifiEnabled(true);
49        }
50
51        WifiConfiguration wifiConf = new WifiConfiguration();
52        SecurityType securityType;
53        if (type == null || TextUtils.isEmpty(type)) {
54            securityType = SecurityType.NONE;
55        } else {
56            securityType = Enum.valueOf(SecurityType.class, type.toUpperCase());
57        }
58        // If we have a password, and no security type, assume WPA.
59        // TODO: Remove this when the programmer supports it.
60        if (securityType.equals(SecurityType.NONE) && !TextUtils.isEmpty(password)) {
61            securityType = SecurityType.WPA;
62        }
63
64        wifiConf.SSID = ssid;
65        wifiConf.status = WifiConfiguration.Status.ENABLED;
66        wifiConf.hiddenSSID = hidden;
67        switch (securityType) {
68            case NONE:
69                wifiConf.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE);
70                wifiConf.allowedAuthAlgorithms.set(WifiConfiguration.AuthAlgorithm.OPEN);
71                break;
72            case WPA:
73                updateForWPAConfiguration(wifiConf, password);
74                break;
75            case WEP:
76                updateForWEPConfiguration(wifiConf, password);
77                break;
78        }
79
80        updateForProxy(wifiConf, proxyHost, proxyPort, proxyBypassHosts, pacUrl);
81
82        int netId = mWifiManager.addNetwork(wifiConf);
83
84        if (netId != -1) {
85            // Setting disableOthers to 'true' should trigger a connection attempt.
86            mWifiManager.enableNetwork(netId, true);
87            mWifiManager.saveConfiguration();
88        }
89
90        return netId;
91    }
92
93    protected void updateForWPAConfiguration(WifiConfiguration wifiConf, String wifiPassword) {
94        wifiConf.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.WPA_PSK);
95        wifiConf.allowedAuthAlgorithms.set(WifiConfiguration.AuthAlgorithm.OPEN);
96        wifiConf.allowedProtocols.set(WifiConfiguration.Protocol.WPA); // For WPA
97        wifiConf.allowedProtocols.set(WifiConfiguration.Protocol.RSN); // For WPA2
98        wifiConf.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.TKIP);
99        wifiConf.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.CCMP);
100        wifiConf.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.TKIP);
101        wifiConf.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.CCMP);
102        if (!TextUtils.isEmpty(wifiPassword)) {
103            wifiConf.preSharedKey = "\"" + wifiPassword + "\"";
104        }
105    }
106
107    protected void updateForWEPConfiguration(WifiConfiguration wifiConf, String password) {
108        wifiConf.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE);
109        wifiConf.allowedAuthAlgorithms.set(WifiConfiguration.AuthAlgorithm.OPEN);
110        wifiConf.allowedAuthAlgorithms.set(WifiConfiguration.AuthAlgorithm.SHARED);
111        wifiConf.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.WEP40);
112        wifiConf.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.WEP104);
113        wifiConf.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.TKIP);
114        wifiConf.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.CCMP);
115        int length = password.length();
116        if ((length == 10 || length == 26 || length == 58) && password.matches("[0-9A-Fa-f]*")) {
117            wifiConf.wepKeys[0] = password;
118        } else {
119            wifiConf.wepKeys[0] = '"' + password + '"';
120        }
121        wifiConf.wepTxKeyIndex = 0;
122    }
123
124    private void updateForProxy(WifiConfiguration wifiConf, String proxyHost, int proxyPort,
125            String proxyBypassHosts, String pacUrl) {
126        if (TextUtils.isEmpty(proxyHost) && TextUtils.isEmpty(pacUrl)) {
127            return;
128        }
129        if (!TextUtils.isEmpty(proxyHost)) {
130            ProxyInfo proxy = new ProxyInfo(proxyHost, proxyPort, proxyBypassHosts);
131            wifiConf.setProxy(ProxySettings.STATIC, proxy);
132        } else {
133            ProxyInfo proxy = new ProxyInfo(pacUrl);
134            wifiConf.setProxy(ProxySettings.PAC, proxy);
135        }
136    }
137}
138