WifiSettings: revise and add support for editing EAP networks.
Use arrays.xml to store translations for enumerations; avoid race conditions happened when pop up menu or dialog; avoid saving configurations with networks disabled; use the same layout for all the dialogs; support editing EAP networks; only unlock keystore before connecting; and many bug fixes I cannot remember. The number of lines in the new code is about 1/3 of the old one, and it improves the readability a lot!
This commit is contained in:
212
src/com/android/settings/wifi/AccessPoint.java
Normal file
212
src/com/android/settings/wifi/AccessPoint.java
Normal file
@@ -0,0 +1,212 @@
|
||||
/*
|
||||
* 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.settings.wifi;
|
||||
|
||||
import com.android.settings.R;
|
||||
|
||||
import android.content.Context;
|
||||
import android.net.NetworkInfo.DetailedState;
|
||||
import android.net.wifi.WifiConfiguration;
|
||||
import android.net.wifi.WifiConfiguration.KeyMgmt;
|
||||
import android.net.wifi.WifiInfo;
|
||||
import android.net.wifi.WifiManager;
|
||||
import android.net.wifi.ScanResult;
|
||||
import android.preference.Preference;
|
||||
import android.text.TextUtils;
|
||||
import android.view.View;
|
||||
import android.widget.ImageView;
|
||||
|
||||
class AccessPoint extends Preference {
|
||||
private static final int[] STATE_SECURED = {R.attr.state_encrypted};
|
||||
private static final int[] STATE_NONE = {};
|
||||
|
||||
static final int SECURITY_NONE = 0;
|
||||
static final int SECURITY_WEP = 1;
|
||||
static final int SECURITY_PSK = 2;
|
||||
static final int SECURITY_EAP = 3;
|
||||
|
||||
final String ssid;
|
||||
final int security;
|
||||
final int networkId;
|
||||
|
||||
private WifiConfiguration mConfig;
|
||||
private int mRssi;
|
||||
private WifiInfo mInfo;
|
||||
private DetailedState mState;
|
||||
private ImageView mSignal;
|
||||
|
||||
private static int getSecurity(WifiConfiguration config) {
|
||||
if (config.allowedKeyManagement.get(KeyMgmt.WPA_PSK)) {
|
||||
return SECURITY_PSK;
|
||||
}
|
||||
if (config.allowedKeyManagement.get(KeyMgmt.WPA_EAP) ||
|
||||
config.allowedKeyManagement.get(KeyMgmt.IEEE8021X)) {
|
||||
return SECURITY_EAP;
|
||||
}
|
||||
return (config.wepKeys[0] != null) ? SECURITY_WEP : SECURITY_NONE;
|
||||
}
|
||||
|
||||
private static int getSecurity(ScanResult result) {
|
||||
if (result.capabilities.contains("WEP")) {
|
||||
return SECURITY_WEP;
|
||||
} else if (result.capabilities.contains("PSK")) {
|
||||
return SECURITY_PSK;
|
||||
} else if (result.capabilities.contains("EAP")) {
|
||||
return SECURITY_EAP;
|
||||
}
|
||||
return SECURITY_NONE;
|
||||
}
|
||||
|
||||
AccessPoint(Context context, WifiConfiguration config) {
|
||||
super(context);
|
||||
setWidgetLayoutResource(R.layout.preference_widget_wifi_signal);
|
||||
ssid = (config.SSID == null ? "" : config.SSID);
|
||||
security = getSecurity(config);
|
||||
networkId = config.networkId;
|
||||
mConfig = config;
|
||||
mRssi = Integer.MAX_VALUE;
|
||||
}
|
||||
|
||||
AccessPoint(Context context, ScanResult result) {
|
||||
super(context);
|
||||
setWidgetLayoutResource(R.layout.preference_widget_wifi_signal);
|
||||
ssid = result.SSID;
|
||||
security = getSecurity(result);
|
||||
networkId = -1;
|
||||
mRssi = result.level;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onBindView(View view) {
|
||||
setTitle(ssid);
|
||||
mSignal = (ImageView) view.findViewById(R.id.signal);
|
||||
if (mRssi == Integer.MAX_VALUE) {
|
||||
mSignal.setImageDrawable(null);
|
||||
} else {
|
||||
mSignal.setImageResource(R.drawable.wifi_signal);
|
||||
mSignal.setImageState((security != SECURITY_NONE) ?
|
||||
STATE_SECURED : STATE_NONE, true);
|
||||
}
|
||||
refresh();
|
||||
super.onBindView(view);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int compareTo(Preference preference) {
|
||||
if (!(preference instanceof AccessPoint)) {
|
||||
return 1;
|
||||
}
|
||||
AccessPoint other = (AccessPoint) preference;
|
||||
// Active one goes first.
|
||||
if (mInfo != other.mInfo) {
|
||||
return (mInfo != null) ? -1 : 1;
|
||||
}
|
||||
// Reachable one goes before unreachable one.
|
||||
if ((mRssi ^ other.mRssi) < 0) {
|
||||
return (mRssi != Integer.MAX_VALUE) ? -1 : 1;
|
||||
}
|
||||
// Configured one goes before unconfigured one.
|
||||
if ((networkId ^ other.networkId) < 0) {
|
||||
return (networkId != -1) ? -1 : 1;
|
||||
}
|
||||
// Sort by signal strength.
|
||||
int difference = WifiManager.compareSignalLevel(other.mRssi, mRssi);
|
||||
if (difference != 0) {
|
||||
return difference;
|
||||
}
|
||||
// Sort by ssid.
|
||||
return ssid.compareToIgnoreCase(other.ssid);
|
||||
}
|
||||
|
||||
boolean update(ScanResult result) {
|
||||
// We do not call refresh() since this is called before onBindView().
|
||||
if (ssid.equals(result.SSID) && security == getSecurity(result)) {
|
||||
if (WifiManager.compareSignalLevel(result.level, mRssi) > 0) {
|
||||
mRssi = result.level;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
void update(WifiInfo info, DetailedState state) {
|
||||
boolean reorder = false;
|
||||
if (info != null && networkId != -1 && networkId == info.getNetworkId()) {
|
||||
reorder = (mInfo == null);
|
||||
mRssi = info.getRssi();
|
||||
mInfo = info;
|
||||
mState = state;
|
||||
refresh();
|
||||
} else if (mInfo != null) {
|
||||
reorder = true;
|
||||
mInfo = null;
|
||||
mState = null;
|
||||
refresh();
|
||||
}
|
||||
if (reorder) {
|
||||
notifyHierarchyChanged();
|
||||
}
|
||||
}
|
||||
|
||||
int getLevel() {
|
||||
if (mRssi == Integer.MAX_VALUE) {
|
||||
return -1;
|
||||
}
|
||||
return WifiManager.calculateSignalLevel(mRssi, 4);
|
||||
}
|
||||
|
||||
WifiConfiguration getConfig() {
|
||||
return mConfig;
|
||||
}
|
||||
|
||||
WifiInfo getInfo() {
|
||||
return mInfo;
|
||||
}
|
||||
|
||||
DetailedState getState() {
|
||||
return mState;
|
||||
}
|
||||
|
||||
private void refresh() {
|
||||
if (mSignal == null) {
|
||||
return;
|
||||
}
|
||||
Context context = getContext();
|
||||
mSignal.setImageLevel(getLevel());
|
||||
|
||||
if (mState != null) {
|
||||
setSummary(Summary.get(context, mState));
|
||||
} else {
|
||||
String status = null;
|
||||
if (mRssi == Integer.MAX_VALUE) {
|
||||
status = context.getString(R.string.wifi_not_in_range);
|
||||
} else if (mConfig != null) {
|
||||
status = context.getString((mConfig.status == WifiConfiguration.Status.DISABLED) ?
|
||||
R.string.wifi_disabled : R.string.wifi_remembered);
|
||||
}
|
||||
|
||||
if (security == SECURITY_NONE) {
|
||||
setSummary(status);
|
||||
} else {
|
||||
String format = context.getString((status == null) ?
|
||||
R.string.wifi_secured : R.string.wifi_secured_with_status);
|
||||
String[] type = context.getResources().getStringArray(R.array.wifi_security);
|
||||
setSummary(String.format(format, type[security], status));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
362
src/com/android/settings/wifi/WifiDialog.java
Normal file
362
src/com/android/settings/wifi/WifiDialog.java
Normal file
@@ -0,0 +1,362 @@
|
||||
/*
|
||||
* 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.settings.wifi;
|
||||
|
||||
import com.android.settings.R;
|
||||
|
||||
import android.app.AlertDialog;
|
||||
import android.content.Context;
|
||||
import android.content.DialogInterface;
|
||||
import android.content.res.Resources;
|
||||
import android.net.NetworkInfo.DetailedState;
|
||||
import android.net.wifi.WifiConfiguration;
|
||||
import android.net.wifi.WifiConfiguration.AuthAlgorithm;
|
||||
import android.net.wifi.WifiConfiguration.KeyMgmt;
|
||||
import android.net.wifi.WifiInfo;
|
||||
import android.os.Bundle;
|
||||
import android.security.Credentials;
|
||||
import android.security.KeyStore;
|
||||
import android.text.Editable;
|
||||
import android.text.InputType;
|
||||
import android.text.TextWatcher;
|
||||
import android.text.format.Formatter;
|
||||
import android.view.View;
|
||||
import android.view.ViewGroup;
|
||||
import android.widget.AdapterView;
|
||||
import android.widget.ArrayAdapter;
|
||||
import android.widget.CheckBox;
|
||||
import android.widget.Spinner;
|
||||
import android.widget.TextView;
|
||||
|
||||
class WifiDialog extends AlertDialog implements View.OnClickListener,
|
||||
TextWatcher, AdapterView.OnItemSelectedListener {
|
||||
private static final String KEYSTORE_SPACE = "keystore://";
|
||||
|
||||
static final int BUTTON_SUBMIT = DialogInterface.BUTTON_POSITIVE;
|
||||
static final int BUTTON_FORGET = DialogInterface.BUTTON_NEUTRAL;
|
||||
|
||||
final boolean edit;
|
||||
private final DialogInterface.OnClickListener mListener;
|
||||
private final AccessPoint mAccessPoint;
|
||||
|
||||
private View mView;
|
||||
private TextView mSsid;
|
||||
private int mSecurity;
|
||||
private TextView mPassword;
|
||||
|
||||
private Spinner mEapMethod;
|
||||
private Spinner mEapCaCert;
|
||||
private Spinner mEapUserCert;
|
||||
private TextView mEapIdentity;
|
||||
private TextView mEapAnonymous;
|
||||
|
||||
static boolean requireKeyStore(WifiConfiguration config) {
|
||||
String values[] = {config.ca_cert.value(), config.client_cert.value(),
|
||||
config.private_key.value()};
|
||||
for (String value : values) {
|
||||
if (value != null && value.startsWith(KEYSTORE_SPACE)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
WifiDialog(Context context, DialogInterface.OnClickListener listener,
|
||||
AccessPoint accessPoint, boolean edit) {
|
||||
super(context);
|
||||
this.edit = edit;
|
||||
mListener = listener;
|
||||
mAccessPoint = accessPoint;
|
||||
mSecurity = (accessPoint == null) ? AccessPoint.SECURITY_NONE : accessPoint.security;
|
||||
}
|
||||
|
||||
WifiConfiguration getConfig() {
|
||||
if (mAccessPoint != null && mAccessPoint.networkId != -1 && !edit) {
|
||||
return null;
|
||||
}
|
||||
|
||||
WifiConfiguration config = new WifiConfiguration();
|
||||
|
||||
if (mAccessPoint == null) {
|
||||
config.SSID = mSsid.getText().toString();
|
||||
// If the user adds a network manually, assume that it is hidden.
|
||||
config.hiddenSSID = true;
|
||||
} else if (mAccessPoint.networkId == -1) {
|
||||
config.SSID = mAccessPoint.ssid;
|
||||
} else {
|
||||
config.networkId = mAccessPoint.networkId;
|
||||
}
|
||||
|
||||
switch (mSecurity) {
|
||||
case AccessPoint.SECURITY_NONE:
|
||||
config.allowedKeyManagement.set(KeyMgmt.NONE);
|
||||
return config;
|
||||
|
||||
case AccessPoint.SECURITY_WEP:
|
||||
config.allowedKeyManagement.set(KeyMgmt.NONE);
|
||||
config.allowedAuthAlgorithms.set(AuthAlgorithm.SHARED);
|
||||
if (mPassword.length() != 0) {
|
||||
int length = mPassword.length();
|
||||
String password = mPassword.getText().toString();
|
||||
// WEP-40, WEP-104, and 256-bit WEP (WEP-232?)
|
||||
if ((length == 10 || length == 26 || length == 58) &&
|
||||
password.matches("[0-9A-Fa-f]*")) {
|
||||
config.wepKeys[0] = password;
|
||||
} else {
|
||||
config.wepKeys[0] = '"' + password + '"';
|
||||
}
|
||||
}
|
||||
return config;
|
||||
|
||||
case AccessPoint.SECURITY_PSK:
|
||||
config.allowedKeyManagement.set(KeyMgmt.WPA_PSK);
|
||||
config.allowedAuthAlgorithms.set(AuthAlgorithm.OPEN);
|
||||
if (mPassword.length() != 0) {
|
||||
String password = mPassword.getText().toString();
|
||||
if (password.matches("[0-9A-Fa-f]{64}")) {
|
||||
config.preSharedKey = password;
|
||||
} else {
|
||||
config.preSharedKey = '"' + password + '"';
|
||||
}
|
||||
}
|
||||
return config;
|
||||
|
||||
case AccessPoint.SECURITY_EAP:
|
||||
config.allowedKeyManagement.set(KeyMgmt.WPA_EAP);
|
||||
config.allowedKeyManagement.set(KeyMgmt.IEEE8021X);
|
||||
config.eap.setValue((String) mEapMethod.getSelectedItem());
|
||||
config.ca_cert.setValue((mEapCaCert.getSelectedItemPosition() == 0) ? "" :
|
||||
KEYSTORE_SPACE + Credentials.CA_CERTIFICATE +
|
||||
(String) mEapCaCert.getSelectedItem());
|
||||
config.client_cert.setValue((mEapUserCert.getSelectedItemPosition() == 0) ? "" :
|
||||
KEYSTORE_SPACE + Credentials.USER_CERTIFICATE +
|
||||
(String) mEapUserCert.getSelectedItem());
|
||||
config.private_key.setValue((mEapUserCert.getSelectedItemPosition() == 0) ? "" :
|
||||
KEYSTORE_SPACE + Credentials.PRIVATE_KEY +
|
||||
(String) mEapUserCert.getSelectedItem());
|
||||
config.identity.setValue((mEapIdentity.length() == 0) ? "" :
|
||||
mEapIdentity.getText().toString());
|
||||
config.anonymous_identity.setValue((mEapAnonymous.length() == 0) ? "" :
|
||||
mEapAnonymous.getText().toString());
|
||||
if (mPassword.length() != 0) {
|
||||
config.password.setValue(mPassword.getText().toString());
|
||||
}
|
||||
return config;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onCreate(Bundle savedInstanceState) {
|
||||
mView = getLayoutInflater().inflate(R.layout.wifi_dialog, null);
|
||||
setView(mView);
|
||||
setInverseBackgroundForced(true);
|
||||
|
||||
Context context = getContext();
|
||||
Resources resources = context.getResources();
|
||||
|
||||
if (mAccessPoint == null) {
|
||||
setTitle(R.string.wifi_add_network);
|
||||
mView.findViewById(R.id.type).setVisibility(View.VISIBLE);
|
||||
mSsid = (TextView) mView.findViewById(R.id.ssid);
|
||||
mSsid.addTextChangedListener(this);
|
||||
((Spinner) mView.findViewById(R.id.security)).setOnItemSelectedListener(this);
|
||||
setButton(BUTTON_SUBMIT, context.getString(R.string.wifi_save), mListener);
|
||||
} else {
|
||||
setTitle(mAccessPoint.ssid);
|
||||
ViewGroup group = (ViewGroup) mView.findViewById(R.id.info);
|
||||
|
||||
DetailedState state = mAccessPoint.getState();
|
||||
if (state != null) {
|
||||
addRow(group, R.string.wifi_status, Summary.get(getContext(), state));
|
||||
}
|
||||
|
||||
String[] type = resources.getStringArray(R.array.wifi_security);
|
||||
addRow(group, R.string.wifi_security, type[mAccessPoint.security]);
|
||||
|
||||
int level = mAccessPoint.getLevel();
|
||||
if (level != -1) {
|
||||
String[] signal = resources.getStringArray(R.array.wifi_signal);
|
||||
addRow(group, R.string.wifi_signal, signal[level]);
|
||||
}
|
||||
|
||||
WifiInfo info = mAccessPoint.getInfo();
|
||||
if (info != null) {
|
||||
addRow(group, R.string.wifi_speed, info.getLinkSpeed() + WifiInfo.LINK_SPEED_UNITS);
|
||||
// TODO: fix the ip address for IPv6.
|
||||
int address = info.getIpAddress();
|
||||
if (address != 0) {
|
||||
addRow(group, R.string.wifi_ip_address, Formatter.formatIpAddress(address));
|
||||
}
|
||||
}
|
||||
|
||||
if (mAccessPoint.networkId == -1 || edit) {
|
||||
showSecurityFields();
|
||||
}
|
||||
|
||||
if (edit) {
|
||||
setButton(BUTTON_SUBMIT, context.getString(R.string.wifi_save), mListener);
|
||||
} else {
|
||||
if (state == null && level != -1) {
|
||||
setButton(BUTTON_SUBMIT, context.getString(R.string.wifi_connect), mListener);
|
||||
}
|
||||
if (mAccessPoint.networkId != -1) {
|
||||
setButton(BUTTON_FORGET, context.getString(R.string.wifi_forget), mListener);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
setButton(DialogInterface.BUTTON_NEGATIVE,
|
||||
context.getString(R.string.wifi_cancel), mListener);
|
||||
|
||||
super.onCreate(savedInstanceState);
|
||||
|
||||
if (getButton(BUTTON_SUBMIT) != null) {
|
||||
validate();
|
||||
}
|
||||
}
|
||||
|
||||
private void addRow(ViewGroup group, int name, String value) {
|
||||
View row = getLayoutInflater().inflate(R.layout.wifi_dialog_row, group, false);
|
||||
((TextView) row.findViewById(R.id.name)).setText(name);
|
||||
((TextView) row.findViewById(R.id.value)).setText(value);
|
||||
group.addView(row);
|
||||
}
|
||||
|
||||
private void validate() {
|
||||
// TODO: make sure this is complete.
|
||||
if ((mSsid != null && mSsid.length() == 0) ||
|
||||
((mAccessPoint == null || mAccessPoint.networkId == -1) &&
|
||||
((mSecurity == AccessPoint.SECURITY_WEP && mPassword.length() == 0) ||
|
||||
(mSecurity == AccessPoint.SECURITY_PSK && mPassword.length() < 8)))) {
|
||||
getButton(BUTTON_SUBMIT).setEnabled(false);
|
||||
} else {
|
||||
getButton(BUTTON_SUBMIT).setEnabled(true);
|
||||
}
|
||||
}
|
||||
|
||||
public void onClick(View view) {
|
||||
mPassword.setInputType(
|
||||
InputType.TYPE_CLASS_TEXT | (((CheckBox) view).isChecked() ?
|
||||
InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD :
|
||||
InputType.TYPE_TEXT_VARIATION_PASSWORD));
|
||||
}
|
||||
|
||||
public void onTextChanged(CharSequence s, int start, int before, int count) {
|
||||
}
|
||||
|
||||
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
|
||||
}
|
||||
|
||||
public void afterTextChanged(Editable editable) {
|
||||
validate();
|
||||
}
|
||||
|
||||
public void onItemSelected(AdapterView parent, View view, int position, long id) {
|
||||
mSecurity = position;
|
||||
showSecurityFields();
|
||||
validate();
|
||||
}
|
||||
|
||||
public void onNothingSelected(AdapterView parent) {
|
||||
}
|
||||
|
||||
private void showSecurityFields() {
|
||||
if (mSecurity == AccessPoint.SECURITY_NONE) {
|
||||
mView.findViewById(R.id.fields).setVisibility(View.GONE);
|
||||
return;
|
||||
}
|
||||
mView.findViewById(R.id.fields).setVisibility(View.VISIBLE);
|
||||
|
||||
if (mPassword == null) {
|
||||
mPassword = (TextView) mView.findViewById(R.id.password);
|
||||
mPassword.addTextChangedListener(this);
|
||||
((CheckBox) mView.findViewById(R.id.show_password)).setOnClickListener(this);
|
||||
|
||||
if (mAccessPoint != null && mAccessPoint.networkId != -1) {
|
||||
mPassword.setHint(R.string.wifi_unchanged);
|
||||
}
|
||||
}
|
||||
|
||||
if (mSecurity != AccessPoint.SECURITY_EAP) {
|
||||
mView.findViewById(R.id.eap).setVisibility(View.GONE);
|
||||
return;
|
||||
}
|
||||
mView.findViewById(R.id.eap).setVisibility(View.VISIBLE);
|
||||
|
||||
if (mEapMethod == null) {
|
||||
mEapMethod = (Spinner) mView.findViewById(R.id.method);
|
||||
mEapCaCert = (Spinner) mView.findViewById(R.id.ca_cert);
|
||||
mEapUserCert = (Spinner) mView.findViewById(R.id.user_cert);
|
||||
mEapIdentity = (TextView) mView.findViewById(R.id.identity);
|
||||
mEapAnonymous = (TextView) mView.findViewById(R.id.anonymous);
|
||||
|
||||
loadCertificates(mEapCaCert, Credentials.CA_CERTIFICATE);
|
||||
loadCertificates(mEapUserCert, Credentials.USER_PRIVATE_KEY);
|
||||
|
||||
if (mAccessPoint != null && mAccessPoint.networkId != -1) {
|
||||
WifiConfiguration config = mAccessPoint.getConfig();
|
||||
setSelection(mEapMethod, config.eap.value());
|
||||
setCertificate(mEapCaCert, Credentials.CA_CERTIFICATE,
|
||||
config.ca_cert.value());
|
||||
setCertificate(mEapUserCert, Credentials.USER_PRIVATE_KEY,
|
||||
config.private_key.value());
|
||||
mEapIdentity.setText(config.identity.value());
|
||||
mEapAnonymous.setText(config.anonymous_identity.value());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void loadCertificates(Spinner spinner, String prefix) {
|
||||
String[] certs = KeyStore.getInstance().saw(prefix);
|
||||
Context context = getContext();
|
||||
String unspecified = context.getString(R.string.wifi_unspecified);
|
||||
|
||||
if (certs == null || certs.length == 0) {
|
||||
certs = new String[] {unspecified};
|
||||
} else {
|
||||
String[] array = new String[certs.length + 1];
|
||||
array[0] = unspecified;
|
||||
System.arraycopy(certs, 0, array, 1, certs.length);
|
||||
certs = array;
|
||||
}
|
||||
|
||||
ArrayAdapter<String> adapter = new ArrayAdapter<String>(
|
||||
context, android.R.layout.simple_spinner_item, certs);
|
||||
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
|
||||
spinner.setAdapter(adapter);
|
||||
}
|
||||
|
||||
private void setCertificate(Spinner spinner, String prefix, String cert) {
|
||||
prefix = KEYSTORE_SPACE + prefix;
|
||||
if (cert != null && cert.startsWith(prefix)) {
|
||||
setSelection(spinner, cert.substring(prefix.length()));
|
||||
}
|
||||
}
|
||||
|
||||
private void setSelection(Spinner spinner, String value) {
|
||||
if (value != null) {
|
||||
ArrayAdapter<String> adapter = (ArrayAdapter<String>) spinner.getAdapter();
|
||||
for (int i = adapter.getCount() - 1; i >= 0; --i) {
|
||||
if (value.equals(adapter.getItem(i))) {
|
||||
spinner.setSelection(i);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
484
src/com/android/settings/wifi/WifiSettings2.java
Normal file
484
src/com/android/settings/wifi/WifiSettings2.java
Normal file
@@ -0,0 +1,484 @@
|
||||
/*
|
||||
* 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.settings.wifi;
|
||||
|
||||
import com.android.settings.ProgressCategory;
|
||||
import com.android.settings.R;
|
||||
|
||||
import android.content.BroadcastReceiver;
|
||||
import android.content.Context;
|
||||
import android.content.DialogInterface;
|
||||
import android.content.Intent;
|
||||
import android.content.IntentFilter;
|
||||
import android.net.NetworkInfo;
|
||||
import android.net.wifi.ScanResult;
|
||||
import android.net.wifi.SupplicantState;
|
||||
import android.net.wifi.WifiConfiguration;
|
||||
import android.net.wifi.WifiConfiguration.KeyMgmt;
|
||||
import android.net.wifi.WifiConfiguration.Status;
|
||||
import android.net.wifi.WifiInfo;
|
||||
import android.net.wifi.WifiManager;
|
||||
import android.os.Bundle;
|
||||
import android.os.Handler;
|
||||
import android.os.Message;
|
||||
import android.preference.CheckBoxPreference;
|
||||
import android.preference.Preference;
|
||||
import android.preference.PreferenceActivity;
|
||||
import android.preference.PreferenceScreen;
|
||||
import android.provider.Settings.Secure;
|
||||
import android.security.Credentials;
|
||||
import android.security.KeyStore;
|
||||
import android.text.TextUtils;
|
||||
import android.view.ContextMenu;
|
||||
import android.view.ContextMenu.ContextMenuInfo;
|
||||
import android.view.Menu;
|
||||
import android.view.MenuItem;
|
||||
import android.view.View;
|
||||
import android.widget.AdapterView.AdapterContextMenuInfo;
|
||||
import android.widget.Toast;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
public class WifiSettings2 extends PreferenceActivity implements DialogInterface.OnClickListener {
|
||||
private static final int MENU_ID_SCAN = Menu.FIRST;
|
||||
private static final int MENU_ID_ADVANCED = Menu.FIRST + 1;
|
||||
private static final int MENU_ID_CONNECT = Menu.FIRST + 2;
|
||||
private static final int MENU_ID_FORGET = Menu.FIRST + 3;
|
||||
private static final int MENU_ID_MODIFY = Menu.FIRST + 4;
|
||||
|
||||
private final IntentFilter mFilter;
|
||||
private final BroadcastReceiver mReceiver;
|
||||
private final Scanner mScanner;
|
||||
|
||||
private WifiManager mWifiManager;
|
||||
private WifiEnabler mWifiEnabler;
|
||||
private CheckBoxPreference mNotifyOpenNetworks;
|
||||
private ProgressCategory mAccessPoints;
|
||||
private Preference mAddNetwork;
|
||||
|
||||
private NetworkInfo.DetailedState mLastState;
|
||||
private WifiInfo mLastInfo;
|
||||
private int mLastPriority;
|
||||
|
||||
private boolean mResetNetworks = false;
|
||||
private int mKeyStoreNetworkId = -1;
|
||||
|
||||
private AccessPoint mSelected;
|
||||
private WifiDialog mDialog;
|
||||
|
||||
public WifiSettings2() {
|
||||
mFilter = new IntentFilter();
|
||||
mFilter.addAction(WifiManager.WIFI_STATE_CHANGED_ACTION);
|
||||
mFilter.addAction(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION);
|
||||
mFilter.addAction(WifiManager.NETWORK_IDS_CHANGED_ACTION);
|
||||
mFilter.addAction(WifiManager.SUPPLICANT_STATE_CHANGED_ACTION);
|
||||
mFilter.addAction(WifiManager.NETWORK_STATE_CHANGED_ACTION);
|
||||
mFilter.addAction(WifiManager.RSSI_CHANGED_ACTION);
|
||||
|
||||
mReceiver = new BroadcastReceiver() {
|
||||
@Override
|
||||
public void onReceive(Context context, Intent intent) {
|
||||
handleEvent(intent);
|
||||
}
|
||||
};
|
||||
|
||||
mScanner = new Scanner();
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onCreate(Bundle savedInstanceState) {
|
||||
super.onCreate(savedInstanceState);
|
||||
|
||||
mWifiManager = (WifiManager) getSystemService(Context.WIFI_SERVICE);
|
||||
|
||||
if (getIntent().getBooleanExtra("only_access_points", false)) {
|
||||
addPreferencesFromResource(R.xml.wifi_access_points2);
|
||||
} else {
|
||||
addPreferencesFromResource(R.xml.wifi_settings2);
|
||||
mWifiEnabler = new WifiEnabler(this, mWifiManager,
|
||||
(CheckBoxPreference) findPreference("enable_wifi"));
|
||||
mNotifyOpenNetworks =
|
||||
(CheckBoxPreference) findPreference("notify_open_networks");
|
||||
mNotifyOpenNetworks.setChecked(Secure.getInt(getContentResolver(),
|
||||
Secure.WIFI_NETWORKS_AVAILABLE_NOTIFICATION_ON, 0) == 1);
|
||||
}
|
||||
|
||||
mAccessPoints = (ProgressCategory) findPreference("access_points");
|
||||
mAccessPoints.setOrderingAsAdded(false);
|
||||
mAddNetwork = findPreference("add_network");
|
||||
|
||||
registerForContextMenu(getListView());
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onResume() {
|
||||
super.onResume();
|
||||
registerReceiver(mReceiver, mFilter);
|
||||
if (mWifiEnabler != null) {
|
||||
mWifiEnabler.resume();
|
||||
}
|
||||
if (mKeyStoreNetworkId != -1 && KeyStore.getInstance().test() == KeyStore.NO_ERROR) {
|
||||
connect(mKeyStoreNetworkId);
|
||||
}
|
||||
mKeyStoreNetworkId = -1;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onPause() {
|
||||
super.onPause();
|
||||
unregisterReceiver(mReceiver);
|
||||
if (mWifiEnabler != null) {
|
||||
mWifiEnabler.pause();
|
||||
}
|
||||
mScanner.pause();
|
||||
if (mDialog != null) {
|
||||
mDialog.dismiss();
|
||||
mDialog = null;
|
||||
}
|
||||
if (mResetNetworks) {
|
||||
enableNetworks();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean onCreateOptionsMenu(Menu menu) {
|
||||
menu.add(Menu.NONE, MENU_ID_SCAN, 0, R.string.wifi_menu_scan)
|
||||
.setIcon(R.drawable.ic_menu_scan_network);
|
||||
menu.add(Menu.NONE, MENU_ID_ADVANCED, 0, R.string.wifi_menu_advanced)
|
||||
.setIcon(android.R.drawable.ic_menu_manage);
|
||||
return super.onCreateOptionsMenu(menu);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean onOptionsItemSelected(MenuItem item) {
|
||||
switch (item.getItemId()) {
|
||||
case MENU_ID_SCAN:
|
||||
mScanner.resume();
|
||||
return true;
|
||||
case MENU_ID_ADVANCED:
|
||||
startActivity(new Intent(this, AdvancedSettings.class));
|
||||
return true;
|
||||
}
|
||||
return super.onOptionsItemSelected(item);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onCreateContextMenu(ContextMenu menu, View view, ContextMenuInfo info) {
|
||||
if (info instanceof AdapterContextMenuInfo) {
|
||||
Preference preference = (Preference) getListView().getItemAtPosition(
|
||||
((AdapterContextMenuInfo) info).position);
|
||||
|
||||
if (preference instanceof AccessPoint) {
|
||||
mSelected = (AccessPoint) preference;
|
||||
menu.setHeaderTitle(mSelected.ssid);
|
||||
if (mSelected.getLevel() != -1 && mSelected.getState() == null) {
|
||||
menu.add(Menu.NONE, MENU_ID_CONNECT, 0, R.string.wifi_menu_connect);
|
||||
}
|
||||
if (mSelected.networkId != -1) {
|
||||
menu.add(Menu.NONE, MENU_ID_FORGET, 0, R.string.wifi_menu_forget);
|
||||
if (mSelected.security != AccessPoint.SECURITY_NONE) {
|
||||
menu.add(Menu.NONE, MENU_ID_MODIFY, 0, R.string.wifi_menu_modify);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean onContextItemSelected(MenuItem item) {
|
||||
if (mSelected == null) {
|
||||
return super.onContextItemSelected(item);
|
||||
}
|
||||
switch (item.getItemId()) {
|
||||
case MENU_ID_CONNECT:
|
||||
if (mSelected.networkId != -1) {
|
||||
if (!requireKeyStore(mSelected.getConfig())) {
|
||||
connect(mSelected.networkId);
|
||||
}
|
||||
} else if (mSelected.security == AccessPoint.SECURITY_NONE) {
|
||||
// Shortcut for open networks.
|
||||
WifiConfiguration config = new WifiConfiguration();
|
||||
config.SSID = mSelected.ssid;
|
||||
config.allowedKeyManagement.set(KeyMgmt.NONE);
|
||||
int networkId = mWifiManager.addNetwork(config);
|
||||
mWifiManager.enableNetwork(networkId, false);
|
||||
connect(networkId);
|
||||
} else {
|
||||
showDialog(mSelected, false);
|
||||
}
|
||||
return true;
|
||||
case MENU_ID_FORGET:
|
||||
forget(mSelected.networkId);
|
||||
return true;
|
||||
case MENU_ID_MODIFY:
|
||||
showDialog(mSelected, true);
|
||||
return true;
|
||||
}
|
||||
return super.onContextItemSelected(item);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean onPreferenceTreeClick(PreferenceScreen screen, Preference preference) {
|
||||
if (preference instanceof AccessPoint) {
|
||||
mSelected = (AccessPoint) preference;
|
||||
showDialog(mSelected, false);
|
||||
} else if (preference == mAddNetwork) {
|
||||
mSelected = null;
|
||||
showDialog(null, true);
|
||||
} else if (preference == mNotifyOpenNetworks) {
|
||||
Secure.putInt(getContentResolver(),
|
||||
Secure.WIFI_NETWORKS_AVAILABLE_NOTIFICATION_ON,
|
||||
mNotifyOpenNetworks.isChecked() ? 1 : 0);
|
||||
} else {
|
||||
return super.onPreferenceTreeClick(screen, preference);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
public void onClick(DialogInterface dialogInterface, int button) {
|
||||
if (button == WifiDialog.BUTTON_FORGET && mSelected != null) {
|
||||
forget(mSelected.networkId);
|
||||
} else if (button == WifiDialog.BUTTON_SUBMIT) {
|
||||
WifiConfiguration config = mDialog.getConfig();
|
||||
|
||||
if (config == null) {
|
||||
if (mSelected != null && !requireKeyStore(mSelected.getConfig())) {
|
||||
connect(mSelected.networkId);
|
||||
}
|
||||
} else if (config.networkId != -1) {
|
||||
if (mSelected != null) {
|
||||
mWifiManager.updateNetwork(config);
|
||||
saveNetworks();
|
||||
}
|
||||
} else {
|
||||
int networkId = mWifiManager.addNetwork(config);
|
||||
if (networkId != -1) {
|
||||
mWifiManager.enableNetwork(networkId, false);
|
||||
config.networkId = networkId;
|
||||
if (mDialog.edit || requireKeyStore(config)) {
|
||||
saveNetworks();
|
||||
} else {
|
||||
connect(networkId);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void showDialog(AccessPoint accessPoint, boolean edit) {
|
||||
if (mDialog != null) {
|
||||
mDialog.dismiss();
|
||||
}
|
||||
mDialog = new WifiDialog(this, this, accessPoint, edit);
|
||||
mDialog.show();
|
||||
}
|
||||
|
||||
private boolean requireKeyStore(WifiConfiguration config) {
|
||||
if (WifiDialog.requireKeyStore(config) &&
|
||||
KeyStore.getInstance().test() != KeyStore.NO_ERROR) {
|
||||
mKeyStoreNetworkId = config.networkId;
|
||||
Credentials.getInstance().unlock(this);
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
private void forget(int networkId) {
|
||||
mWifiManager.removeNetwork(networkId);
|
||||
saveNetworks();
|
||||
}
|
||||
|
||||
private void connect(int networkId) {
|
||||
if (networkId == -1) {
|
||||
return;
|
||||
}
|
||||
|
||||
// Reset the priority of each network if it goes too high.
|
||||
if (mLastPriority > 1000000) {
|
||||
for (int i = mAccessPoints.getPreferenceCount() - 1; i >= 0; --i) {
|
||||
AccessPoint accessPoint = (AccessPoint) mAccessPoints.getPreference(i);
|
||||
if (accessPoint.networkId != -1) {
|
||||
WifiConfiguration config = new WifiConfiguration();
|
||||
config.networkId = accessPoint.networkId;
|
||||
config.priority = 0;
|
||||
mWifiManager.updateNetwork(config);
|
||||
}
|
||||
}
|
||||
mLastPriority = 0;
|
||||
}
|
||||
|
||||
// Set to the highest priority and save the configuration.
|
||||
WifiConfiguration config = new WifiConfiguration();
|
||||
config.networkId = networkId;
|
||||
config.priority = ++mLastPriority;
|
||||
mWifiManager.updateNetwork(config);
|
||||
saveNetworks();
|
||||
|
||||
// Connect to network by disabling others.
|
||||
mWifiManager.enableNetwork(networkId, true);
|
||||
mWifiManager.reconnect();
|
||||
mResetNetworks = true;
|
||||
}
|
||||
|
||||
private void enableNetworks() {
|
||||
for (int i = mAccessPoints.getPreferenceCount() - 1; i >= 0; --i) {
|
||||
WifiConfiguration config = ((AccessPoint) mAccessPoints.getPreference(i)).getConfig();
|
||||
if (config != null && config.status != Status.ENABLED) {
|
||||
mWifiManager.enableNetwork(config.networkId, false);
|
||||
}
|
||||
}
|
||||
mResetNetworks = false;
|
||||
}
|
||||
|
||||
private void saveNetworks() {
|
||||
// Always save the configuration with all networks enabled.
|
||||
enableNetworks();
|
||||
mWifiManager.saveConfiguration();
|
||||
updateAccessPoints();
|
||||
}
|
||||
|
||||
private void updateAccessPoints() {
|
||||
List<AccessPoint> accessPoints = new ArrayList<AccessPoint>();
|
||||
|
||||
List<WifiConfiguration> configs = mWifiManager.getConfiguredNetworks();
|
||||
if (configs != null) {
|
||||
mLastPriority = 0;
|
||||
for (WifiConfiguration config : configs) {
|
||||
if (config.priority > mLastPriority) {
|
||||
mLastPriority = config.priority;
|
||||
}
|
||||
|
||||
// Shift the status to make enableNetworks() more efficient.
|
||||
if (config.status == Status.CURRENT) {
|
||||
config.status = Status.ENABLED;
|
||||
} else if (mResetNetworks && config.status == Status.DISABLED) {
|
||||
config.status = Status.CURRENT;
|
||||
}
|
||||
|
||||
AccessPoint accessPoint = new AccessPoint(this, config);
|
||||
accessPoint.update(mLastInfo, mLastState);
|
||||
accessPoints.add(accessPoint);
|
||||
}
|
||||
}
|
||||
|
||||
List<ScanResult> results = mWifiManager.getScanResults();
|
||||
if (results != null) {
|
||||
for (ScanResult result : results) {
|
||||
// Ignore hidden and ad-hoc networks.
|
||||
if (result.SSID == null || result.SSID.length() == 0 ||
|
||||
result.capabilities.contains("[IBSS]")) {
|
||||
continue;
|
||||
}
|
||||
|
||||
boolean found = false;
|
||||
for (AccessPoint accessPoint : accessPoints) {
|
||||
if (accessPoint.update(result)) {
|
||||
found = true;
|
||||
}
|
||||
}
|
||||
if (!found) {
|
||||
accessPoints.add(new AccessPoint(this, result));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
mAccessPoints.removeAll();
|
||||
for (AccessPoint accessPoint : accessPoints) {
|
||||
mAccessPoints.addPreference(accessPoint);
|
||||
}
|
||||
}
|
||||
|
||||
private void handleEvent(Intent intent) {
|
||||
String action = intent.getAction();
|
||||
if (WifiManager.WIFI_STATE_CHANGED_ACTION.equals(action)) {
|
||||
updateWifiState(intent.getIntExtra(WifiManager.EXTRA_WIFI_STATE,
|
||||
WifiManager.WIFI_STATE_UNKNOWN));
|
||||
} else if (WifiManager.SCAN_RESULTS_AVAILABLE_ACTION.equals(action)) {
|
||||
updateAccessPoints();
|
||||
} else if (WifiManager.NETWORK_IDS_CHANGED_ACTION.equals(action)) {
|
||||
if (mSelected != null && mSelected.networkId != -1) {
|
||||
mSelected = null;
|
||||
}
|
||||
updateAccessPoints();
|
||||
} else if (WifiManager.SUPPLICANT_STATE_CHANGED_ACTION.equals(action)) {
|
||||
updateConnectionState(WifiInfo.getDetailedStateOf((SupplicantState)
|
||||
intent.getParcelableExtra(WifiManager.EXTRA_NEW_STATE)));
|
||||
} else if (WifiManager.NETWORK_STATE_CHANGED_ACTION.equals(action)) {
|
||||
updateConnectionState(((NetworkInfo) intent.getParcelableExtra(
|
||||
WifiManager.EXTRA_NETWORK_INFO)).getDetailedState());
|
||||
} else if (WifiManager.RSSI_CHANGED_ACTION.equals(action)) {
|
||||
updateConnectionState(null);
|
||||
}
|
||||
}
|
||||
|
||||
private void updateConnectionState(NetworkInfo.DetailedState state) {
|
||||
if (state == NetworkInfo.DetailedState.OBTAINING_IPADDR) {
|
||||
mScanner.pause();
|
||||
} else {
|
||||
mScanner.resume();
|
||||
}
|
||||
|
||||
mLastInfo = mWifiManager.getConnectionInfo();
|
||||
if (state != null) {
|
||||
mLastState = state;
|
||||
}
|
||||
|
||||
for (int i = mAccessPoints.getPreferenceCount() - 1; i >= 0; --i) {
|
||||
((AccessPoint) mAccessPoints.getPreference(i)).update(mLastInfo, mLastState);
|
||||
}
|
||||
}
|
||||
|
||||
private void updateWifiState(int state) {
|
||||
if (state == WifiManager.WIFI_STATE_ENABLED) {
|
||||
mScanner.resume();
|
||||
updateAccessPoints();
|
||||
} else {
|
||||
mScanner.pause();
|
||||
mAccessPoints.removeAll();
|
||||
}
|
||||
}
|
||||
|
||||
private class Scanner extends Handler {
|
||||
private int mRetry = 0;
|
||||
|
||||
void resume() {
|
||||
if (!hasMessages(0)) {
|
||||
sendEmptyMessage(0);
|
||||
}
|
||||
}
|
||||
|
||||
void pause() {
|
||||
mRetry = 0;
|
||||
mAccessPoints.setProgress(false);
|
||||
removeMessages(0);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void handleMessage(Message message) {
|
||||
if (mWifiManager.startScanActive()) {
|
||||
mRetry = 0;
|
||||
} else if (++mRetry >= 3) {
|
||||
mRetry = 0;
|
||||
Toast.makeText(WifiSettings2.this, R.string.wifi_fail_to_scan,
|
||||
Toast.LENGTH_LONG).show();
|
||||
}
|
||||
mAccessPoints.setProgress(mRetry != 0);
|
||||
sendEmptyMessageDelayed(0, 6000);
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user