2011年9月19日月曜日

MyWifiConfiguration

WifiConfigurationの全項目をハンドリングしやすくするため作成したクラスです。
コンストラクタでWifiConfiguration を取り込み、getで設定した値を反映したWifiConfiguration が取得できます。
修正にあたっては、現在ではほとんど利用されていないでしょうがAndroid1.6にも対応しました。(自分が使っているAndroidが1.6なもので)

UI関連やデータの保存については各自プログラムしてください。


/* Copyright 2010 OddRain
 * Copyright 2010 marcus905 
 * Copyright 2011 koichirosa10 
 *
 *  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.
 */

/* modifyed 2011, koichirosa10@gmail.com
 *
 *  -  It class-ized.
 *  -  It corrected so that it might operate by Android1.6.
 *  -  The code for CM6 was deleted.
 *
 */
package jp.hoge;

import java.io.Serializable;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.BitSet;

import android.net.wifi.WifiConfiguration;

public class MyWifiConfiguration implements Serializable {
	private static final long serialVersionUID = 1554241197910887802L;

	private static final String INT_PRIVATE_KEY = "private_key";
	private static final String INT_PHASE2 = "phase2";
	private static final String INT_PASSWORD = "password";
	private static final String INT_IDENTITY = "identity";
	private static final String INT_EAP = "eap";
	private static final String INT_CLIENT_CERT = "client_cert";
	private static final String INT_CA_CERT = "ca_cert";
	private static final String INT_ANONYMOUS_IDENTITY = "anonymous_identity";

	private static final String INT16_PRIVATE_KEY = "privateKey";
	private static final String INT16_PRIVATE_KEY_PASSWORD = "privateKeyPasswd";
	private static final String INT16_PHASE2 = "phase2";
	private static final String INT16_PASSWORD = "password";
	private static final String INT16_IDENTITY = "identity";
	private static final String INT16_EAP = "eap";
	private static final String INT16_CLIENT_CERT = "clientCert";
	private static final String INT16_CA_CERT = "caCert";
	private static final String INT16_ANONYMOUS_IDENTITY = "anonymousIdentity";

	private static final String INT_ENTERPRISEFIELD_NAME = "android.net.wifi.WifiConfiguration$EnterpriseField";
	protected static final int SHOW_PREFERENCES = 0;

	public int networkId;
    public int status;
    public String SSID;
    public String BSSID;
    public String preSharedKey;
    public String[] wepKeys;
    public int wepTxKeyIndex;
    public int priority;
    public boolean hiddenSSID;

    public BitSet allowedKeyManagement;
    public BitSet allowedProtocols;
    public BitSet allowedAuthAlgorithms;
    public BitSet allowedPairwiseCiphers;
    public BitSet allowedGroupCiphers;

    public String eap;
    public String phase2;
    public String identity;
    public String anonymous_identity;
    public String password;
    public String client_cert;
    public String private_key;
    public String ca_cert;

    public MyWifiConfiguration() {
    	allowedKeyManagement = new BitSet();
    	allowedProtocols = new BitSet();
    	allowedAuthAlgorithms = new BitSet();
    	allowedPairwiseCiphers = new BitSet();
    	allowedGroupCiphers = new BitSet();
    	wepKeys = new String[4];
    }
    public MyWifiConfiguration(WifiConfiguration conf) throws Exception {
        networkId = conf.networkId;
        status = conf.status;
        SSID = MyWifiTools.removeQuotes(conf.SSID);
        BSSID = conf.BSSID;
        String val = MyWifiTools.removeQuotes(conf.preSharedKey);
        preSharedKey = ((val == null || val.equals("*")) ? null : val);
        wepKeys = new String[conf.wepKeys.length];
        for (int j = 0; j < wepKeys.length; j++) {
            wepKeys[j] = ((conf.wepKeys[j] == null || conf.wepKeys[j].equals("*")) ? null : MyWifiTools.removeQuotes(conf.wepKeys[j]));
        }
        wepTxKeyIndex = conf.wepTxKeyIndex;
        priority = conf.priority;
        hiddenSSID = conf.hiddenSSID;

        allowedKeyManagement = conf.allowedKeyManagement;
        allowedProtocols = conf.allowedProtocols;
        allowedAuthAlgorithms = conf.allowedAuthAlgorithms;
        allowedPairwiseCiphers = conf.allowedPairwiseCiphers;
        allowedGroupCiphers = conf.allowedGroupCiphers;

		setEnterprisefield(conf);
    }
	private void setEnterprisefield(WifiConfiguration conf) throws Exception {
		Class[] wcClasses = WifiConfiguration.class.getClasses();
		Class wcEnterpriseField = null;
		for (Class wcClass : wcClasses)
			if (wcClass.getName().equals(INT_ENTERPRISEFIELD_NAME)) {
				wcEnterpriseField = wcClass;
				break;
			}
		boolean noEnterpriseFieldType = false;
		if(wcEnterpriseField == null)
			noEnterpriseFieldType = true;

		// EnterpriseField
		Field wcefAnonymousId = null;
		Field wcefCaCert = null;
		Field wcefClientCert = null;
		Field  wcefEap = null;
		Field  wcefIdentity = null;
		Field  wcefPassword = null;
		Field  wcefPhase2 = null;
		Field  wcefPrivateKey = null;
		@SuppressWarnings("unused")
		Field  wcefPrivateKeyPassword = null;

		Field[] wcefFields = WifiConfiguration.class.getFields();
		for (Field wcefField : wcefFields) {
			if (wcefField.getName().trim().equals(INT_ANONYMOUS_IDENTITY))
				wcefAnonymousId = wcefField;
			else if (wcefField.getName().trim().equals(INT_CA_CERT))
				wcefCaCert = wcefField;
			else if (wcefField.getName().trim().equals(INT_CLIENT_CERT))
				wcefClientCert = wcefField;
			else if (wcefField.getName().trim().equals(INT_EAP))
				wcefEap = wcefField;
			else if (wcefField.getName().trim().equals(INT_IDENTITY))
				wcefIdentity = wcefField;
			else if (wcefField.getName().trim().equals(INT_PASSWORD))
				wcefPassword = wcefField;
			else if (wcefField.getName().trim().equals(INT_PHASE2))
				wcefPhase2 = wcefField;
			else if (wcefField.getName().trim().equals(INT_PRIVATE_KEY))
				wcefPrivateKey = wcefField;

			// for Android1.6
			else if (wcefField.getName().trim().equals(INT16_ANONYMOUS_IDENTITY))
				wcefAnonymousId = wcefField;
			else if (wcefField.getName().trim().equals(INT16_CA_CERT))
				wcefCaCert = wcefField;
			else if (wcefField.getName().trim().equals(INT16_CLIENT_CERT))
				wcefClientCert = wcefField;
			else if (wcefField.getName().trim().equals(INT16_EAP))
				wcefEap = wcefField;
			else if (wcefField.getName().trim().equals(INT16_IDENTITY))
				wcefIdentity = wcefField;
			else if (wcefField.getName().trim().equals(INT16_PASSWORD))
				wcefPassword = wcefField;
			else if (wcefField.getName().trim().equals(INT16_PHASE2))
				wcefPhase2 = wcefField;
			else if (wcefField.getName().trim().equals(INT16_PRIVATE_KEY))
				wcefPrivateKey = wcefField;
			else if (wcefField.getName().trim().equals(INT16_PRIVATE_KEY_PASSWORD))
				wcefPrivateKeyPassword = wcefField;
		}

		Method wcefValue = null;
		if(!noEnterpriseFieldType){
		for(Method m: wcEnterpriseField.getMethods())
			//System.out.println(m.getName());
			if(m.getName().trim().equals("value")){
				wcefValue = m;
				break;
			}
		}
		String tVal;
		eap = noEnterpriseFieldType
				? (String) wcefEap.get(conf)
				: (String) wcefValue.invoke(wcefEap.get(conf), (Object)null);
		tVal = noEnterpriseFieldType
				? (String) wcefPhase2.get(conf)
				: (String) wcefValue.invoke(wcefPhase2.get(conf), (Object)null);
		phase2 = MyWifiTools.removeQuotes(tVal);

		tVal = noEnterpriseFieldType
				? (String) wcefIdentity.get(conf)
				: (String) wcefValue.invoke(wcefIdentity.get(conf), (Object)null);
		identity = MyWifiTools.removeQuotes(tVal);

		tVal = noEnterpriseFieldType
				? (String) wcefAnonymousId.get(conf)
				: (String) wcefValue.invoke(wcefAnonymousId.get(conf), (Object)null);
		anonymous_identity = MyWifiTools.removeQuotes(tVal);

		tVal = noEnterpriseFieldType
				? (String) wcefPassword.get(conf)
				: (String) wcefValue.invoke(wcefPassword.get(conf), (Object)null);
        String val = MyWifiTools.removeQuotes(tVal);
        password = ((val == null || val.equals("*")) ? null : val);

		tVal = noEnterpriseFieldType
				? (String) wcefClientCert.get(conf)
				: (String) wcefValue.invoke(wcefClientCert.get(conf), (Object)null);
		client_cert = MyWifiTools.removeQuotes(tVal);

		tVal = noEnterpriseFieldType
				? (String) wcefPrivateKey.get(conf)
				: (String) wcefValue.invoke(wcefPrivateKey.get(conf), (Object)null);
		private_key = MyWifiTools.removeQuotes(tVal);

		tVal = noEnterpriseFieldType
				? (String) wcefCaCert.get(conf)
				: (String) wcefValue.invoke(wcefCaCert.get(conf), (Object)null);
		ca_cert = MyWifiTools.removeQuotes(tVal);
	}

	public WifiConfiguration get() {
		WifiConfiguration conf = new WifiConfiguration();

		conf.networkId = networkId;
		conf.status = status;
		conf.SSID = MyWifiTools.surroundWithQuotes(SSID);
		conf.BSSID = BSSID;
		conf.preSharedKey = getKey(preSharedKey);
		conf.wepKeys = new String[wepKeys.length];
        for (int j = 0; j < wepKeys.length; j++) {
        	conf.wepKeys[j] = getKey(wepKeys[j]);
        }
        conf.wepTxKeyIndex = wepTxKeyIndex;
        conf.priority = priority;
        conf.hiddenSSID = hiddenSSID;

        conf.allowedKeyManagement = allowedKeyManagement;
        conf.allowedProtocols = allowedProtocols;
        conf.allowedAuthAlgorithms = allowedAuthAlgorithms;
        conf.allowedPairwiseCiphers = allowedPairwiseCiphers;
        conf.allowedGroupCiphers = allowedGroupCiphers;

        try {
			getEnterprisefield(conf);
		} catch (Exception e) {
		}
		return conf;
	}
	private String getKey(String value) {
		if(value == null || value.length() <= 0)
			return null;
		switch(value.length()){
		case 10:
		case 26:
		case 58:
			if(value.matches("[0-9A-Fa-f]*"))
				return value;
			break;
		}
		return MyWifiTools.surroundWithQuotes(value);
	}
	private void getEnterprisefield(WifiConfiguration conf) throws Exception {
		Class[] wcClasses = WifiConfiguration.class.getClasses();
		Class wcEnterpriseField = null;

		for (Class wcClass : wcClasses)
			if (wcClass.getName().equals(INT_ENTERPRISEFIELD_NAME)) {
				wcEnterpriseField = wcClass;
				break;
			}
		boolean noEnterpriseFieldType = false;
		if(wcEnterpriseField == null)
			noEnterpriseFieldType = true;

		Field wcefAnonymousId = null;
		Field wcefCaCert = null;
		Field wcefClientCert = null;
		Field wcefEap = null;
		Field wcefIdentity = null;
		Field wcefPassword = null;
		Field wcefPhase2 = null;
		Field wcefPrivateKey = null;
		@SuppressWarnings("unused")
		Field wcefPrivateKeyPassword = null;

		Field[] wcefFields = WifiConfiguration.class.getFields();
		for (Field wcefField : wcefFields) {
			if (wcefField.getName().equals(INT_ANONYMOUS_IDENTITY))
				wcefAnonymousId = wcefField;
			else if (wcefField.getName().equals(INT_CA_CERT))
				wcefCaCert = wcefField;
			else if (wcefField.getName().equals(INT_CLIENT_CERT))
				wcefClientCert = wcefField;
			else if (wcefField.getName().equals(INT_EAP))
				wcefEap = wcefField;
			else if (wcefField.getName().equals(INT_IDENTITY))
				wcefIdentity = wcefField;
			else if (wcefField.getName().equals(INT_PASSWORD))
				wcefPassword = wcefField;
			else if (wcefField.getName().equals(INT_PHASE2))
				wcefPhase2 = wcefField;
			else if (wcefField.getName().equals(INT_PRIVATE_KEY))
				wcefPrivateKey = wcefField;

			else if (wcefField.getName().trim().equals(INT16_ANONYMOUS_IDENTITY))
				wcefAnonymousId = wcefField;
			else if (wcefField.getName().trim().equals(INT16_CA_CERT))
				wcefCaCert = wcefField;
			else if (wcefField.getName().trim().equals(INT16_CLIENT_CERT))
				wcefClientCert = wcefField;
			else if (wcefField.getName().trim().equals(INT16_EAP))
				wcefEap = wcefField;
			else if (wcefField.getName().trim().equals(INT16_IDENTITY))
				wcefIdentity = wcefField;
			else if (wcefField.getName().trim().equals(INT16_PASSWORD))
				wcefPassword = wcefField;
			else if (wcefField.getName().trim().equals(INT16_PHASE2))
				wcefPhase2 = wcefField;
			else if (wcefField.getName().trim().equals(INT16_PRIVATE_KEY))
				wcefPrivateKey = wcefField;
			else if (wcefField.getName().trim().equals(INT16_PRIVATE_KEY_PASSWORD))
				wcefPrivateKeyPassword = wcefField;
		}

		Method wcefSetValue = null;
		if(!noEnterpriseFieldType){
		for(Method m: wcEnterpriseField.getMethods())
			if(m.getName().trim().equals("setValue"))
				wcefSetValue = m;
		}
		if(eap != null) {
			if(!noEnterpriseFieldType)
				wcefSetValue.invoke(wcefEap.get(conf), eap);
			else
				wcefEap.set(conf, eap);
		}
		if (phase2 != null && !phase2.equals("---")) {
			if(!noEnterpriseFieldType)
				wcefSetValue.invoke(wcefPhase2.get(conf), phase2);
			else
				wcefPhase2.set(conf, MyWifiTools.surroundWithQuotes(phase2));
		}
		if (identity != null && identity.length() > 0) {
			if(!noEnterpriseFieldType)
				wcefSetValue.invoke(wcefIdentity.get(conf), identity);
			else
				wcefIdentity.set(conf, MyWifiTools.surroundWithQuotes(identity));
		}
		if (anonymous_identity != null && anonymous_identity.length() > 0) {
			if(!noEnterpriseFieldType)
				wcefSetValue.invoke(wcefAnonymousId.get(conf), anonymous_identity);
			else
				wcefAnonymousId.set(conf, MyWifiTools.surroundWithQuotes(anonymous_identity));
		}
		if (password != null && password.length() > 0) {
			if(!noEnterpriseFieldType)
				wcefSetValue.invoke(wcefPassword.get(conf), password);
			else
				wcefPassword.set(conf, MyWifiTools.surroundWithQuotes(password));
		}
		if (client_cert != null && client_cert.length() > 0) {
			if(!noEnterpriseFieldType)
				wcefSetValue.invoke(wcefClientCert.get(conf), client_cert);
			else
				wcefClientCert.set(conf, MyWifiTools.surroundWithQuotes(client_cert));
		}
		if (ca_cert != null && ca_cert.length() > 0) {
			if(!noEnterpriseFieldType)
				wcefSetValue.invoke(wcefCaCert.get(conf), ca_cert);
			else
				wcefCaCert.set(conf, MyWifiTools.surroundWithQuotes(ca_cert));
		}
		if (private_key != null && private_key.length() > 0) {
			if(!noEnterpriseFieldType)
				wcefSetValue.invoke(wcefPrivateKey.get(conf), private_key);
			else
				wcefPrivateKey.set(conf, MyWifiTools.surroundWithQuotes(private_key));
		}
	}
}

0 件のコメント:

コメントを投稿