2 * Copyright (C) 2010 The Android Open Source Project
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 package com.android.settings.wifi;
19 import com.android.settings.R;
20 import com.android.settings.WirelessSettings;
22 import android.content.BroadcastReceiver;
23 import android.content.Context;
24 import android.content.Intent;
25 import android.content.IntentFilter;
26 import android.net.NetworkInfo;
27 import android.net.wifi.SupplicantState;
28 import android.net.wifi.WifiInfo;
29 import android.net.wifi.WifiManager;
30 import android.preference.Preference;
31 import android.preference.CheckBoxPreference;
32 import android.provider.Settings;
33 import android.text.TextUtils;
34 import android.widget.Toast;
36 public class WifiEnabler implements Preference.OnPreferenceChangeListener {
37 private final Context mContext;
38 private final CheckBoxPreference mCheckBox;
39 private final CharSequence mOriginalSummary;
41 private final WifiManager mWifiManager;
42 private final IntentFilter mIntentFilter;
43 private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
45 public void onReceive(Context context, Intent intent) {
46 String action = intent.getAction();
47 if (WifiManager.WIFI_STATE_CHANGED_ACTION.equals(action)) {
48 handleWifiStateChanged(intent.getIntExtra(
49 WifiManager.EXTRA_WIFI_STATE, WifiManager.WIFI_STATE_UNKNOWN));
50 } else if (WifiManager.SUPPLICANT_STATE_CHANGED_ACTION.equals(action)) {
51 handleStateChanged(WifiInfo.getDetailedStateOf((SupplicantState)
52 intent.getParcelableExtra(WifiManager.EXTRA_NEW_STATE)));
53 } else if (WifiManager.NETWORK_STATE_CHANGED_ACTION.equals(action)) {
54 handleStateChanged(((NetworkInfo) intent.getParcelableExtra(
55 WifiManager.EXTRA_NETWORK_INFO)).getDetailedState());
60 public WifiEnabler(Context context, CheckBoxPreference checkBox) {
63 mOriginalSummary = checkBox.getSummary();
64 checkBox.setPersistent(false);
66 mWifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
67 mIntentFilter = new IntentFilter(WifiManager.WIFI_STATE_CHANGED_ACTION);
68 // The order matters! We really should not depend on this. :(
69 mIntentFilter.addAction(WifiManager.SUPPLICANT_STATE_CHANGED_ACTION);
70 mIntentFilter.addAction(WifiManager.NETWORK_STATE_CHANGED_ACTION);
73 public void resume() {
74 // Wi-Fi state is sticky, so just let the receiver update UI
75 mContext.registerReceiver(mReceiver, mIntentFilter);
76 mCheckBox.setOnPreferenceChangeListener(this);
80 mContext.unregisterReceiver(mReceiver);
81 mCheckBox.setOnPreferenceChangeListener(null);
84 public boolean onPreferenceChange(Preference preference, Object value) {
85 boolean enable = (Boolean) value;
87 // Show toast message if Wi-Fi is not allowed in airplane mode
88 if (enable && !WirelessSettings
89 .isRadioAllowed(mContext, Settings.System.RADIO_WIFI)) {
90 Toast.makeText(mContext, R.string.wifi_in_airplane_mode,
91 Toast.LENGTH_SHORT).show();
96 * Disable tethering if enabling Wifi
98 int wifiApState = mWifiManager.getWifiApState();
99 if (enable && ((wifiApState == WifiManager.WIFI_AP_STATE_ENABLING) ||
100 (wifiApState == WifiManager.WIFI_AP_STATE_ENABLED))) {
101 mWifiManager.setWifiApEnabled(null, false);
103 if (mWifiManager.setWifiEnabled(enable)) {
104 mCheckBox.setEnabled(false);
106 mCheckBox.setSummary(R.string.wifi_error);
109 // Don't update UI to opposite state until we're sure
113 private void handleWifiStateChanged(int state) {
115 case WifiManager.WIFI_STATE_ENABLING:
116 mCheckBox.setSummary(R.string.wifi_starting);
117 mCheckBox.setEnabled(false);
119 case WifiManager.WIFI_STATE_ENABLED:
120 mCheckBox.setChecked(true);
121 mCheckBox.setSummary(null);
122 mCheckBox.setEnabled(true);
124 case WifiManager.WIFI_STATE_DISABLING:
125 mCheckBox.setSummary(R.string.wifi_stopping);
126 mCheckBox.setEnabled(false);
128 case WifiManager.WIFI_STATE_DISABLED:
129 mCheckBox.setChecked(false);
130 mCheckBox.setSummary(mOriginalSummary);
131 mCheckBox.setEnabled(true);
134 mCheckBox.setChecked(false);
135 mCheckBox.setSummary(R.string.wifi_error);
136 mCheckBox.setEnabled(true);
140 private void handleStateChanged(NetworkInfo.DetailedState state) {
141 // WifiInfo is valid if and only if Wi-Fi is enabled.
142 // Here we use the state of the check box as an optimization.
143 if (state != null && mCheckBox.isChecked()) {
144 WifiInfo info = mWifiManager.getConnectionInfo();
146 mCheckBox.setSummary(Summary.get(mContext, info.getSSID(), state));