/* * Copyright (C) 2010, 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.connectivitymanagertest; import android.net.IpConfiguration.IpAssignment; import android.net.IpConfiguration.ProxySettings; import android.net.LinkAddress; import android.net.StaticIpConfiguration; import android.net.wifi.WifiConfiguration; import android.net.wifi.WifiConfiguration.AuthAlgorithm; import android.net.wifi.WifiConfiguration.KeyMgmt; import android.net.wifi.WifiEnterpriseConfig; import org.json.JSONArray; import org.json.JSONException; import org.json.JSONObject; import java.net.InetAddress; import java.net.UnknownHostException; import java.util.ArrayList; import java.util.List; /** * Helper for dealing with creating {@link WifiConfiguration} objects. */ public class WifiConfigurationHelper { private static final int NONE = 0; private static final int WEP = 1; private static final int PSK = 2; private static final int EAP = 3; /** * Private constructor since this a static class. */ private WifiConfigurationHelper() {} /** * Create a {@link WifiConfiguration} for an open network * * @param ssid The SSID of the wifi network * @return The {@link WifiConfiguration} */ public static WifiConfiguration createOpenConfig(String ssid) { WifiConfiguration config = createGenericConfig(ssid); config.allowedKeyManagement.set(KeyMgmt.NONE); return config; } /** * Create a {@link WifiConfiguration} for a WEP secured network * * @param ssid The SSID of the wifi network * @param password Either a 10, 26, or 58 character hex string or the plain text password * @return The {@link WifiConfiguration} */ public static WifiConfiguration createWepConfig(String ssid, String password) { WifiConfiguration config = createGenericConfig(ssid); if (isHex(password, 10) || isHex(password, 26) || isHex(password, 58)) { config.wepKeys[0] = password; } else { config.wepKeys[0] = quotedString(password); } config.allowedKeyManagement.set(KeyMgmt.NONE); config.allowedAuthAlgorithms.set(AuthAlgorithm.OPEN); config.allowedAuthAlgorithms.set(AuthAlgorithm.SHARED); return config; } /** * Create a {@link WifiConfiguration} for a PSK secured network * * @param ssid The SSID of the wifi network * @param password Either a 64 character hex string or the plain text password * @return The {@link WifiConfiguration} */ public static WifiConfiguration createPskConfig(String ssid, String password) { WifiConfiguration config = createGenericConfig(ssid); if (isHex(password, 64)) { config.preSharedKey = password; } else { config.preSharedKey = quotedString(password); } config.allowedKeyManagement.set(KeyMgmt.WPA_PSK); return config; } /** * Create a {@link WifiConfiguration} for an EAP secured network * * @param ssid The SSID of the wifi network * @param password The password * @param eapMethod The EAP method * @param phase2 The phase 2 method or null * @param identity The identity or null * @param anonymousIdentity The anonymous identity or null * @param caCert The CA certificate or null * @param clientCert The client certificate or null * @return The {@link WifiConfiguration} */ public static WifiConfiguration createEapConfig(String ssid, String password, int eapMethod, Integer phase2, String identity, String anonymousIdentity, String caCert, String clientCert) { WifiConfiguration config = new WifiConfiguration(); config.SSID = quotedString(ssid); config.allowedKeyManagement.set(KeyMgmt.WPA_EAP); config.allowedKeyManagement.set(KeyMgmt.IEEE8021X); // Set defaults if (phase2 == null) phase2 = WifiEnterpriseConfig.Phase2.NONE; if (identity == null) identity = ""; if (anonymousIdentity == null) anonymousIdentity = ""; if (caCert == null) caCert = ""; if (clientCert == null) clientCert = ""; config.enterpriseConfig.setPassword(password); config.enterpriseConfig.setEapMethod(eapMethod); config.enterpriseConfig.setPhase2Method(phase2); config.enterpriseConfig.setIdentity(identity); config.enterpriseConfig.setAnonymousIdentity(anonymousIdentity); config.enterpriseConfig.setCaCertificateAlias(caCert); config.enterpriseConfig.setClientCertificateAlias(clientCert); return config; } /** * Create a generic {@link WifiConfiguration} used by the other create methods. */ private static WifiConfiguration createGenericConfig(String ssid) { WifiConfiguration config = new WifiConfiguration(); config.SSID = quotedString(ssid); config.setIpAssignment(IpAssignment.DHCP); config.setProxySettings(ProxySettings.NONE); return config; } /** * Parse a JSON string for WiFi configurations stored as a JSON string. *
* This json string should be a list of dictionaries, with each dictionary containing a single * wifi configuration. The wifi configuration requires the fields "ssid" and "security" with * security being one of NONE, WEP, PSK, or EAP. If WEP, PSK, or EAP are selected, the field * "password" must also be provided. If EAP is selected, then the fiels "eap", "phase2", * "identity", "ananymous_identity", "ca_cert", and "client_cert" are also required. Lastly, * static IP settings are also supported. If the field "ip" is set, then the fields "gateway", * "prefix_length", "dns1", and "dns2" are required. *
* @throws IllegalArgumentException if the input string was not valid JSON or if any mandatory * fields are missing. */ public static List