diff --git a/AndroidManifest.xml b/AndroidManifest.xml index 0b6d011f7c5..64962ab6b0a 100644 --- a/AndroidManifest.xml +++ b/AndroidManifest.xml @@ -93,7 +93,23 @@ - + + + + + + + + + + + diff --git a/res/layout/wifi_dialog.xml b/res/layout/wifi_dialog.xml new file mode 100644 index 00000000000..65584535030 --- /dev/null +++ b/res/layout/wifi_dialog.xml @@ -0,0 +1,161 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/res/layout/wifi_dialog_row.xml b/res/layout/wifi_dialog_row.xml new file mode 100644 index 00000000000..52819d49c88 --- /dev/null +++ b/res/layout/wifi_dialog_row.xml @@ -0,0 +1,32 @@ + + + + + + + diff --git a/res/values/arrays.xml b/res/values/arrays.xml index 0e5c9208c50..d7be2aed9ae 100644 --- a/res/values/arrays.xml +++ b/res/values/arrays.xml @@ -230,6 +230,39 @@ Unsuccessful + + + + + Open + + WEP + + WPA/WPA2 PSK + + 802.1x EAP + + + + + + + PEAP + + TLS + + TTLS + + + + + + Poor + Fair + Good + Excellent + + diff --git a/res/values/strings.xml b/res/values/strings.xml index 60f4ebd6d3f..a27b05b209b 100644 --- a/res/values/strings.xml +++ b/res/values/strings.xml @@ -660,7 +660,7 @@ Remember settings - + Wi-Fi @@ -671,12 +671,127 @@ Wi-Fi settings Wi-Fi settings - + Set up & manage wireless access points + + Turning on\u2026 + + Turning off\u2026 + + Error + + In airplane mode + + Unable to scan for networks + + Network notification + + Notify me when an open network is available + + Add Wi-Fi network + + Wi-Fi networks + + Scan + + Advanced + + Connect to network + + Forget network + + Modify network + + + + Network SSID + + Security + + Signal strength + + Status + + Link speed + + IP address + + EAP method + + CA certificate + + User certificate + + Identity + + Anonymous identity + + Password + + Show password. + + (unchanged) + + (unspecified) + + Remembered + + Disabled + + Not in range + + Secured with %1$s + + %2$s, secured with %1$s + + Connect + + Forget + + Save + + Cancel + + + + Advanced + + Regulatory domain + + Set the number of channels to use + + There was a problem setting the regulatory domain. + + %1$d channels + + Wi-Fi sleep policy + + Specify when to switch from Wi-Fi to mobile data + + There was a problem setting the sleep policy. + + MAC address + + IP settings + + Save + + Cancel + + Please type a valid IP address. + + Use static IP + + DNS 1 + + DNS 2 + + Gateway + + Netmask + Forget - - Status Speed @@ -709,19 +824,9 @@ secured with WPA/WPA2 PSK secured with 802.1x EAP - - IP address Signal strength - - Turning on\u2026 - - Turning off\u2026 - - Error - - In airplane mode - + Unable to start Wi-Fi Unable to stop Wi-Fi @@ -734,7 +839,7 @@ Connect - Connect to %1$s + %1$s EAP method @@ -751,8 +856,6 @@ Wireless password WEP hex key (0-9, A-F) - - Show password. Scan @@ -761,48 +864,18 @@ remembered Connection unsuccessful, touch to try again - - Wi-Fi networks Network SSID - - Security Save (unchanged) Add Wi-Fi network - - Network notification - - Notify me when an open network is available The network password you typed is not correct. Please try again. There is a problem connecting to the network. Please try again. - - Advanced - - IP settings - - Save - - Cancel - - Please type a valid IP address. - - Use static IP - - IP address - - DNS 1 - - DNS 2 - - Gateway - - Netmask Connect to network @@ -810,31 +883,43 @@ Change password - - Advanced - - Regulatory domain - - Set the number of channels to use - - There was a problem setting the regulatory domain. - - %1$d channels + + Scanning\u2026 + + Connecting to %1$s\u2026 + + Authenticating with %1$s\u2026 + + Obtaining IP address from %1$s\u2026 + + Connected to %1$s + + Disconnecting from %1$s\u2026 + + Disconnected + + Unsuccessful - - Wi-Fi sleep policy - - Specify when to switch from Wi-Fi to mobile data - - There was a problem setting the sleep policy. - - - MAC address + + Scanning\u2026 + + Connecting\u2026 + + Authenticating\u2026 + + Obtaining address\u2026 + + Connected + + Disconnecting\u2026 + + Disconnected + + Unsuccessful Wifi information - disableNetwork @@ -853,7 +938,6 @@ Wifi API - Wifi Status @@ -916,41 +1000,6 @@ Label on Wifi Configuration screen--> Configured Networks - - - Scanning\u2026 - - Connecting to %1$s\u2026 - - Authenticating with %1$s\u2026 - - Obtaining IP address from %1$s\u2026 - - Connected to %1$s - - Disconnecting from %1$s\u2026 - - Disconnected - - Unsuccessful - - - Scanning\u2026 - - Connecting\u2026 - - Authenticating\u2026 - - Obtaining address\u2026 - - Connected - - Disconnecting\u2026 - - Disconnected - - Unsuccessful - Sound diff --git a/res/xml/wifi_access_points2.xml b/res/xml/wifi_access_points2.xml new file mode 100644 index 00000000000..48104dd0387 --- /dev/null +++ b/res/xml/wifi_access_points2.xml @@ -0,0 +1,30 @@ + + + + + + + + + + diff --git a/res/xml/wifi_settings2.xml b/res/xml/wifi_settings2.xml new file mode 100644 index 00000000000..35f1173cd8c --- /dev/null +++ b/res/xml/wifi_settings2.xml @@ -0,0 +1,45 @@ + + + + + + + + + + + + + + diff --git a/res/xml/wireless_settings.xml b/res/xml/wireless_settings.xml index 24930212b8a..d3c593f3949 100644 --- a/res/xml/wireless_settings.xml +++ b/res/xml/wireless_settings.xml @@ -37,7 +37,7 @@ + android:targetClass="com.android.settings.wifi.WifiSettings2" /> 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)); + } + } + } +} diff --git a/src/com/android/settings/wifi/WifiDialog.java b/src/com/android/settings/wifi/WifiDialog.java new file mode 100644 index 00000000000..9250ee08b3e --- /dev/null +++ b/src/com/android/settings/wifi/WifiDialog.java @@ -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 adapter = new ArrayAdapter( + 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 adapter = (ArrayAdapter) spinner.getAdapter(); + for (int i = adapter.getCount() - 1; i >= 0; --i) { + if (value.equals(adapter.getItem(i))) { + spinner.setSelection(i); + break; + } + } + } + } +} diff --git a/src/com/android/settings/wifi/WifiSettings2.java b/src/com/android/settings/wifi/WifiSettings2.java new file mode 100644 index 00000000000..0c177b1d967 --- /dev/null +++ b/src/com/android/settings/wifi/WifiSettings2.java @@ -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 accessPoints = new ArrayList(); + + List 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 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); + } + } +}