Merge "WifiEnterpriseConfig: Refactor set/get field values"

This commit is contained in:
Roshan Pius
2016-09-07 15:47:39 +00:00
committed by Gerrit Code Review

View File

@@ -33,7 +33,9 @@ import java.security.cert.CertificateFactory;
import java.security.cert.X509Certificate; import java.security.cert.X509Certificate;
import java.security.spec.InvalidKeySpecException; import java.security.spec.InvalidKeySpecException;
import java.security.spec.PKCS8EncodedKeySpec; import java.security.spec.PKCS8EncodedKeySpec;
import java.util.Arrays;
import java.util.HashMap; import java.util.HashMap;
import java.util.List;
import java.util.Map; import java.util.Map;
/** /**
@@ -116,7 +118,6 @@ public class WifiEnterpriseConfig implements Parcelable {
/** @hide */ /** @hide */
public static final String CA_CERT_ALIAS_DELIMITER = " "; public static final String CA_CERT_ALIAS_DELIMITER = " ";
// Fields to copy verbatim from wpa_supplicant. // Fields to copy verbatim from wpa_supplicant.
private static final String[] SUPPLICANT_CONFIG_KEYS = new String[] { private static final String[] SUPPLICANT_CONFIG_KEYS = new String[] {
IDENTITY_KEY, IDENTITY_KEY,
@@ -133,6 +134,11 @@ public class WifiEnterpriseConfig implements Parcelable {
CA_PATH_KEY CA_PATH_KEY
}; };
/**
* Fields that have unquoted values in {@link #mFields}.
*/
private static final List<String> UNQUOTED_KEYS = Arrays.asList(ENGINE_KEY, OPP_KEY_CACHING);
private HashMap<String, String> mFields = new HashMap<String, String>(); private HashMap<String, String> mFields = new HashMap<String, String>();
private X509Certificate[] mCaCerts; private X509Certificate[] mCaCerts;
private PrivateKey mClientPrivateKey; private PrivateKey mClientPrivateKey;
@@ -458,7 +464,7 @@ public class WifiEnterpriseConfig implements Parcelable {
case Eap.AKA: case Eap.AKA:
case Eap.AKA_PRIME: case Eap.AKA_PRIME:
mEapMethod = eapMethod; mEapMethod = eapMethod;
mFields.put(OPP_KEY_CACHING, "1"); setFieldValue(OPP_KEY_CACHING, "1");
break; break;
default: default:
throw new IllegalArgumentException("Unknown EAP method"); throw new IllegalArgumentException("Unknown EAP method");
@@ -517,7 +523,7 @@ public class WifiEnterpriseConfig implements Parcelable {
* @return the identity * @return the identity
*/ */
public String getIdentity() { public String getIdentity() {
return getFieldValue(IDENTITY_KEY, ""); return getFieldValue(IDENTITY_KEY);
} }
/** /**
@@ -526,7 +532,7 @@ public class WifiEnterpriseConfig implements Parcelable {
* @param anonymousIdentity the anonymous identity * @param anonymousIdentity the anonymous identity
*/ */
public void setAnonymousIdentity(String anonymousIdentity) { public void setAnonymousIdentity(String anonymousIdentity) {
setFieldValue(ANON_IDENTITY_KEY, anonymousIdentity, ""); setFieldValue(ANON_IDENTITY_KEY, anonymousIdentity);
} }
/** /**
@@ -534,7 +540,7 @@ public class WifiEnterpriseConfig implements Parcelable {
* @return anonymous identity * @return anonymous identity
*/ */
public String getAnonymousIdentity() { public String getAnonymousIdentity() {
return getFieldValue(ANON_IDENTITY_KEY, ""); return getFieldValue(ANON_IDENTITY_KEY);
} }
/** /**
@@ -542,7 +548,7 @@ public class WifiEnterpriseConfig implements Parcelable {
* @param password the password * @param password the password
*/ */
public void setPassword(String password) { public void setPassword(String password) {
setFieldValue(PASSWORD_KEY, password, ""); setFieldValue(PASSWORD_KEY, password);
} }
/** /**
@@ -552,7 +558,7 @@ public class WifiEnterpriseConfig implements Parcelable {
* framework, returns "*". * framework, returns "*".
*/ */
public String getPassword() { public String getPassword() {
return getFieldValue(PASSWORD_KEY, ""); return getFieldValue(PASSWORD_KEY);
} }
/** /**
@@ -642,7 +648,7 @@ public class WifiEnterpriseConfig implements Parcelable {
* @hide * @hide
*/ */
@Nullable public String[] getCaCertificateAliases() { @Nullable public String[] getCaCertificateAliases() {
String value = getFieldValue(CA_CERT_KEY, ""); String value = getFieldValue(CA_CERT_KEY);
if (value.startsWith(CA_CERT_PREFIX)) { if (value.startsWith(CA_CERT_PREFIX)) {
// Backwards compatibility: parse the original alias prefix. // Backwards compatibility: parse the original alias prefix.
return new String[] {getFieldValue(CA_CERT_KEY, CA_CERT_PREFIX)}; return new String[] {getFieldValue(CA_CERT_KEY, CA_CERT_PREFIX)};
@@ -769,7 +775,7 @@ public class WifiEnterpriseConfig implements Parcelable {
* @hide * @hide
*/ */
public String getCaPath() { public String getCaPath() {
return getFieldValue(CA_PATH_KEY, ""); return getFieldValue(CA_PATH_KEY);
} }
/** Set Client certificate alias. /** Set Client certificate alias.
@@ -785,11 +791,11 @@ public class WifiEnterpriseConfig implements Parcelable {
setFieldValue(PRIVATE_KEY_ID_KEY, alias, Credentials.USER_PRIVATE_KEY); setFieldValue(PRIVATE_KEY_ID_KEY, alias, Credentials.USER_PRIVATE_KEY);
// Also, set engine parameters // Also, set engine parameters
if (TextUtils.isEmpty(alias)) { if (TextUtils.isEmpty(alias)) {
mFields.put(ENGINE_KEY, ENGINE_DISABLE); setFieldValue(ENGINE_KEY, ENGINE_DISABLE);
mFields.put(ENGINE_ID_KEY, EMPTY_VALUE); setFieldValue(ENGINE_ID_KEY, "");
} else { } else {
mFields.put(ENGINE_KEY, ENGINE_ENABLE); setFieldValue(ENGINE_KEY, ENGINE_ENABLE);
mFields.put(ENGINE_ID_KEY, convertToQuotedString(ENGINE_ID_KEYSTORE)); setFieldValue(ENGINE_ID_KEY, ENGINE_ID_KEYSTORE);
} }
} }
@@ -862,7 +868,7 @@ public class WifiEnterpriseConfig implements Parcelable {
* @deprecated in favor of altSubjectMatch * @deprecated in favor of altSubjectMatch
*/ */
public void setSubjectMatch(String subjectMatch) { public void setSubjectMatch(String subjectMatch) {
setFieldValue(SUBJECT_MATCH_KEY, subjectMatch, ""); setFieldValue(SUBJECT_MATCH_KEY, subjectMatch);
} }
/** /**
@@ -871,7 +877,7 @@ public class WifiEnterpriseConfig implements Parcelable {
* @deprecated in favor of altSubjectMatch * @deprecated in favor of altSubjectMatch
*/ */
public String getSubjectMatch() { public String getSubjectMatch() {
return getFieldValue(SUBJECT_MATCH_KEY, ""); return getFieldValue(SUBJECT_MATCH_KEY);
} }
/** /**
@@ -881,7 +887,7 @@ public class WifiEnterpriseConfig implements Parcelable {
* DNS:server.example.com;EMAIL:server@example.com * DNS:server.example.com;EMAIL:server@example.com
*/ */
public void setAltSubjectMatch(String altSubjectMatch) { public void setAltSubjectMatch(String altSubjectMatch) {
setFieldValue(ALTSUBJECT_MATCH_KEY, altSubjectMatch, ""); setFieldValue(ALTSUBJECT_MATCH_KEY, altSubjectMatch);
} }
/** /**
@@ -889,7 +895,7 @@ public class WifiEnterpriseConfig implements Parcelable {
* @return the alternate subject match string * @return the alternate subject match string
*/ */
public String getAltSubjectMatch() { public String getAltSubjectMatch() {
return getFieldValue(ALTSUBJECT_MATCH_KEY, ""); return getFieldValue(ALTSUBJECT_MATCH_KEY);
} }
/** /**
@@ -919,7 +925,7 @@ public class WifiEnterpriseConfig implements Parcelable {
* @return The domain value. * @return The domain value.
*/ */
public String getDomainSuffixMatch() { public String getDomainSuffixMatch() {
return getFieldValue(DOM_SUFFIX_MATCH_KEY, ""); return getFieldValue(DOM_SUFFIX_MATCH_KEY);
} }
/** /**
@@ -928,7 +934,7 @@ public class WifiEnterpriseConfig implements Parcelable {
* @param realm the realm * @param realm the realm
*/ */
public void setRealm(String realm) { public void setRealm(String realm) {
setFieldValue(REALM_KEY, realm, ""); setFieldValue(REALM_KEY, realm);
} }
/** /**
@@ -936,7 +942,7 @@ public class WifiEnterpriseConfig implements Parcelable {
* @return the realm * @return the realm
*/ */
public String getRealm() { public String getRealm() {
return getFieldValue(REALM_KEY, ""); return getFieldValue(REALM_KEY);
} }
/** /**
@@ -944,7 +950,7 @@ public class WifiEnterpriseConfig implements Parcelable {
* @param plmn the plmn value derived from mcc (mobile country code) & mnc (mobile network code) * @param plmn the plmn value derived from mcc (mobile country code) & mnc (mobile network code)
*/ */
public void setPlmn(String plmn) { public void setPlmn(String plmn) {
setFieldValue(PLMN_KEY, plmn, ""); setFieldValue(PLMN_KEY, plmn);
} }
/** /**
@@ -953,7 +959,7 @@ public class WifiEnterpriseConfig implements Parcelable {
* @return the plmn * @return the plmn
*/ */
public String getPlmn() { public String getPlmn() {
return getFieldValue(PLMN_KEY, ""); return getFieldValue(PLMN_KEY);
} }
/** See {@link WifiConfiguration#getKeyIdForCredentials} @hide */ /** See {@link WifiConfiguration#getKeyIdForCredentials} @hide */
@@ -998,13 +1004,13 @@ public class WifiEnterpriseConfig implements Parcelable {
} }
/** /**
* Returns the field value for the key. * Returns the field value for the key with prefix removed.
* @param key into the hash * @param key into the hash
* @param prefix is the prefix that the value may have * @param prefix is the prefix that the value may have
* @return value * @return value
* @hide * @hide
*/ */
public String getFieldValue(String key, String prefix) { private String getFieldValue(String key, String prefix) {
// TODO: Should raise an exception if |key| is EAP_KEY or PHASE2_KEY since // TODO: Should raise an exception if |key| is EAP_KEY or PHASE2_KEY since
// neither of these keys should be retrieved in this manner. // neither of these keys should be retrieved in this manner.
String value = mFields.get(key); String value = mFields.get(key);
@@ -1020,23 +1026,15 @@ public class WifiEnterpriseConfig implements Parcelable {
} }
/** /**
* Set a value with an optional prefix at key * Returns the field value for the key.
* @param key into the hash * @param key into the hash
* @param value to be set * @return value
* @param prefix an optional value to be prefixed to actual value
* @hide * @hide
*/ */
public void setFieldValue(String key, String value, String prefix) { public String getFieldValue(String key) {
// TODO: Should raise an exception if |key| is EAP_KEY or PHASE2_KEY since return getFieldValue(key, "");
// neither of these keys should be set in this manner.
if (TextUtils.isEmpty(value)) {
mFields.put(key, EMPTY_VALUE);
} else {
mFields.put(key, convertToQuotedString(prefix + value));
}
} }
/** /**
* Set a value with an optional prefix at key * Set a value with an optional prefix at key
* @param key into the hash * @param key into the hash
@@ -1044,16 +1042,32 @@ public class WifiEnterpriseConfig implements Parcelable {
* @param prefix an optional value to be prefixed to actual value * @param prefix an optional value to be prefixed to actual value
* @hide * @hide
*/ */
public void setFieldValue(String key, String value) { private void setFieldValue(String key, String value, String prefix) {
// TODO: Should raise an exception if |key| is EAP_KEY or PHASE2_KEY since // TODO: Should raise an exception if |key| is EAP_KEY or PHASE2_KEY since
// neither of these keys should be set in this manner. // neither of these keys should be set in this manner.
if (TextUtils.isEmpty(value)) { if (TextUtils.isEmpty(value)) {
mFields.put(key, EMPTY_VALUE); mFields.put(key, EMPTY_VALUE);
} else { } else {
mFields.put(key, convertToQuotedString(value)); String valueToSet;
if (!UNQUOTED_KEYS.contains(key)) {
valueToSet = convertToQuotedString(prefix + value);
} else {
valueToSet = prefix + value;
}
mFields.put(key, valueToSet);
} }
} }
/**
* Set a value at key
* @param key into the hash
* @param value to be set
* @hide
*/
public void setFieldValue(String key, String value) {
setFieldValue(key, value, "");
}
@Override @Override
public String toString() { public String toString() {
StringBuffer sb = new StringBuffer(); StringBuffer sb = new StringBuffer();