Search in sources :

Example 41 with WifiConfiguration

use of android.net.wifi.WifiConfiguration in project android_frameworks_base by ResurrectionRemix.

the class WifiClientTest method testSaveConfig.

// Test case 4: save config
@LargeTest
public void testSaveConfig() {
    WifiConfiguration config = new WifiConfiguration();
    config.SSID = "\"TestSSID3\"";
    config.allowedKeyManagement.set(KeyMgmt.NONE);
    //add
    int netId = mWifiManager.addNetwork(config);
    assertTrue(netId != -1);
    mWifiManager.saveConfiguration();
    //restart wifi
    mWifiManager.setWifiEnabled(false);
    mWifiManager.setWifiEnabled(true);
    sleepAfterWifiEnable();
    //check config list
    List<WifiConfiguration> configList = mWifiManager.getConfiguredNetworks();
    boolean found = false;
    for (WifiConfiguration c : configList) {
        if (c.SSID.equals("TestSSID3")) {
            found = true;
        }
    }
    assertTrue(found);
    //restore config
    boolean ret = mWifiManager.removeNetwork(netId);
    assertTrue(ret);
    mWifiManager.saveConfiguration();
}
Also used : WifiConfiguration(android.net.wifi.WifiConfiguration) LargeTest(android.test.suitebuilder.annotation.LargeTest)

Example 42 with WifiConfiguration

use of android.net.wifi.WifiConfiguration in project android_frameworks_base by ResurrectionRemix.

the class OSUManager method setOSUSelection.

public void setOSUSelection(int osuID) {
    OSUInfo selection = null;
    for (OSUInfo osuInfo : mOSUMap.values()) {
        Log.d("ZXZ", "In select: " + osuInfo + ", id " + osuInfo.getOsuID());
        if (osuInfo.getOsuID() == osuID && osuInfo.getIconStatus() == OSUInfo.IconStatus.Available) {
            selection = osuInfo;
            break;
        }
    }
    Log.d(TAG, "Selected OSU ID " + osuID + ", matches " + selection);
    if (selection == null) {
        mPendingOSU = null;
        return;
    }
    mPendingOSU = selection;
    WifiConfiguration config = mWifiNetworkAdapter.getActiveWifiConfig();
    if (config != null && bssidMatch(selection) && Utils.unquote(config.SSID).equals(selection.getSSID())) {
        try {
            // Go straight to provisioning if the network is already selected.
            // Also note that mOSUNwkID is left unset to leave the network around after
            // flow completion since it was not added by the OSU flow.
            initiateProvisioning(mPendingOSU, mWifiNetworkAdapter.getCurrentNetwork());
        } catch (IOException ioe) {
            notifyUser(OSUOperationStatus.ProvisioningFailure, ioe.getMessage(), mPendingOSU.getName(LOCALE));
        } finally {
            mPendingOSU = null;
        }
    } else {
        try {
            mOSUNwkID = mWifiNetworkAdapter.connect(selection, mPendingOSU.getName(LOCALE));
        } catch (IOException ioe) {
            notifyUser(OSUOperationStatus.ProvisioningFailure, ioe.getMessage(), selection.getName(LOCALE));
        }
    }
}
Also used : WifiConfiguration(android.net.wifi.WifiConfiguration) IOException(java.io.IOException)

Example 43 with WifiConfiguration

use of android.net.wifi.WifiConfiguration in project android_frameworks_base by ResurrectionRemix.

the class SettingsBackupAgent method restoreSoftApConfiguration.

private void restoreSoftApConfiguration(byte[] data) {
    WifiManager wifiManager = (WifiManager) getSystemService(Context.WIFI_SERVICE);
    try {
        WifiConfiguration config = WifiConfiguration.getWifiConfigFromBackup(new DataInputStream(new ByteArrayInputStream(data)));
        if (DEBUG)
            Log.d(TAG, "Successfully unMarshaled WifiConfiguration ");
        wifiManager.setWifiApConfiguration(config);
    } catch (IOException | BackupUtils.BadVersionException e) {
        Log.e(TAG, "Failed to unMarshal SoftAPConfiguration " + e.getMessage());
    }
}
Also used : WifiManager(android.net.wifi.WifiManager) WifiConfiguration(android.net.wifi.WifiConfiguration) ByteArrayInputStream(java.io.ByteArrayInputStream) IOException(java.io.IOException) DataInputStream(java.io.DataInputStream)

Example 44 with WifiConfiguration

use of android.net.wifi.WifiConfiguration in project android_frameworks_base by ResurrectionRemix.

the class WifiConfigurationHelper method getWifiConfiguration.

/**
     * Parse a {@link JSONObject} and return the wifi configuration.
     *
     * @throws IllegalArgumentException if any mandatory fields are missing.
     */
private static WifiConfiguration getWifiConfiguration(JSONObject jsonConfig) throws JSONException {
    String ssid = jsonConfig.getString("ssid");
    String password = null;
    WifiConfiguration config;
    int securityType = getSecurityType(jsonConfig.getString("security"));
    switch(securityType) {
        case NONE:
            config = createOpenConfig(ssid);
            break;
        case WEP:
            password = jsonConfig.getString("password");
            config = createWepConfig(ssid, password);
            break;
        case PSK:
            password = jsonConfig.getString("password");
            config = createPskConfig(ssid, password);
            break;
        case EAP:
            password = jsonConfig.getString("password");
            int eapMethod = getEapMethod(jsonConfig.getString("eap"));
            Integer phase2 = null;
            if (jsonConfig.has("phase2")) {
                phase2 = getPhase2(jsonConfig.getString("phase2"));
            }
            String identity = null;
            if (jsonConfig.has("identity")) {
                identity = jsonConfig.getString("identity");
            }
            String anonymousIdentity = null;
            if (jsonConfig.has("anonymous_identity")) {
                anonymousIdentity = jsonConfig.getString("anonymous_identity");
            }
            String caCert = null;
            if (jsonConfig.has("ca_cert")) {
                caCert = (jsonConfig.getString("ca_cert"));
            }
            String clientCert = null;
            if (jsonConfig.has("client_cert")) {
                clientCert = jsonConfig.getString("client_cert");
            }
            config = createEapConfig(ssid, password, eapMethod, phase2, identity, anonymousIdentity, caCert, clientCert);
            break;
        default:
            // Should never reach here as getSecurityType will already throw an exception
            throw new IllegalArgumentException();
    }
    if (jsonConfig.has("ip")) {
        StaticIpConfiguration staticIpConfig = new StaticIpConfiguration();
        InetAddress ipAddress = getInetAddress(jsonConfig.getString("ip"));
        int prefixLength = getPrefixLength(jsonConfig.getInt("prefix_length"));
        staticIpConfig.ipAddress = new LinkAddress(ipAddress, prefixLength);
        staticIpConfig.gateway = getInetAddress(jsonConfig.getString("gateway"));
        staticIpConfig.dnsServers.add(getInetAddress(jsonConfig.getString("dns1")));
        staticIpConfig.dnsServers.add(getInetAddress(jsonConfig.getString("dns2")));
        config.setIpAssignment(IpAssignment.STATIC);
        config.setStaticIpConfiguration(staticIpConfig);
    } else {
        config.setIpAssignment(IpAssignment.DHCP);
    }
    config.setProxySettings(ProxySettings.NONE);
    return config;
}
Also used : LinkAddress(android.net.LinkAddress) WifiConfiguration(android.net.wifi.WifiConfiguration) StaticIpConfiguration(android.net.StaticIpConfiguration) InetAddress(java.net.InetAddress)

Example 45 with WifiConfiguration

use of android.net.wifi.WifiConfiguration in project android_frameworks_base by ResurrectionRemix.

the class WifiConfigurationHelper method parseJson.

/**
     * Parse a JSON string for WiFi configurations stored as a JSON string.
     * <p>
     * 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.
     * </p>
     * @throws IllegalArgumentException if the input string was not valid JSON or if any mandatory
     * fields are missing.
     */
public static List<WifiConfiguration> parseJson(String in) {
    try {
        JSONArray jsonConfigs = new JSONArray(in);
        List<WifiConfiguration> wifiConfigs = new ArrayList<>(jsonConfigs.length());
        for (int i = 0; i < jsonConfigs.length(); i++) {
            JSONObject jsonConfig = jsonConfigs.getJSONObject(i);
            wifiConfigs.add(getWifiConfiguration(jsonConfig));
        }
        return wifiConfigs;
    } catch (JSONException e) {
        throw new IllegalArgumentException(e);
    }
}
Also used : WifiConfiguration(android.net.wifi.WifiConfiguration) JSONObject(org.json.JSONObject) JSONArray(org.json.JSONArray) ArrayList(java.util.ArrayList) JSONException(org.json.JSONException)

Aggregations

WifiConfiguration (android.net.wifi.WifiConfiguration)316 WifiManager (android.net.wifi.WifiManager)54 LargeTest (android.test.suitebuilder.annotation.LargeTest)53 IOException (java.io.IOException)44 ArrayList (java.util.ArrayList)38 WifiEnterpriseConfig (android.net.wifi.WifiEnterpriseConfig)32 ScanResult (android.net.wifi.ScanResult)27 Credential (com.android.hotspot2.pps.Credential)20 Scanner (com.android.settingslib.wifi.WifiTracker.Scanner)20 WifiInfo (android.net.wifi.WifiInfo)16 Bundle (android.os.Bundle)12 NetworkPolicy (android.net.NetworkPolicy)10 SpannableString (android.text.SpannableString)10 EAP (com.android.anqp.eap.EAP)10 X509Certificate (java.security.cert.X509Certificate)10 StaticIpConfiguration (android.net.StaticIpConfiguration)7 PackageManager (android.content.pm.PackageManager)6 NetworkInfo (android.net.NetworkInfo)6 PowerManager (android.os.PowerManager)6 RemoteException (android.os.RemoteException)6