WifiEnabler.java revision afc4ab2ffbb8327ddce9907961295a32cbf49d0f
1/* 2 * Copyright (C) 2007 The Android Open Source Project 3 * 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 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 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. 15 */ 16 17package com.android.settings.wifi; 18 19import static android.net.wifi.WifiManager.WIFI_STATE_DISABLED; 20import static android.net.wifi.WifiManager.WIFI_STATE_DISABLING; 21import static android.net.wifi.WifiManager.WIFI_STATE_ENABLED; 22import static android.net.wifi.WifiManager.WIFI_STATE_ENABLING; 23import static android.net.wifi.WifiManager.WIFI_STATE_UNKNOWN; 24 25import com.android.settings.R; 26 27import android.content.BroadcastReceiver; 28import android.content.Context; 29import android.content.Intent; 30import android.content.IntentFilter; 31import android.net.NetworkInfo; 32import android.net.wifi.WifiManager; 33import android.preference.Preference; 34import android.preference.CheckBoxPreference; 35import android.text.TextUtils; 36import android.util.Config; 37import android.util.Log; 38 39public class WifiEnabler implements Preference.OnPreferenceChangeListener { 40 41 private static final boolean LOCAL_LOGD = Config.LOGD || WifiLayer.LOGV; 42 private static final String TAG = "SettingsWifiEnabler"; 43 44 private final Context mContext; 45 private final WifiManager mWifiManager; 46 private final CheckBoxPreference mWifiCheckBoxPref; 47 private final CharSequence mOriginalSummary; 48 49 private final IntentFilter mWifiStateFilter; 50 private final BroadcastReceiver mWifiStateReceiver = new BroadcastReceiver() { 51 52 @Override 53 public void onReceive(Context context, Intent intent) { 54 if (intent.getAction().equals(WifiManager.WIFI_STATE_CHANGED_ACTION)) { 55 handleWifiStateChanged( 56 intent.getIntExtra(WifiManager.EXTRA_WIFI_STATE, WIFI_STATE_UNKNOWN), 57 intent.getIntExtra(WifiManager.EXTRA_PREVIOUS_WIFI_STATE, 58 WIFI_STATE_UNKNOWN)); 59 } else if (intent.getAction().equals(WifiManager.NETWORK_STATE_CHANGED_ACTION)) { 60 handleNetworkStateChanged( 61 (NetworkInfo) intent.getParcelableExtra(WifiManager.EXTRA_NETWORK_INFO)); 62 } 63 } 64 }; 65 66 public WifiEnabler(Context context, WifiManager wifiManager, 67 CheckBoxPreference wifiCheckBoxPreference) { 68 mContext = context; 69 mWifiCheckBoxPref = wifiCheckBoxPreference; 70 mWifiManager = wifiManager; 71 72 mOriginalSummary = wifiCheckBoxPreference.getSummary(); 73 wifiCheckBoxPreference.setPersistent(false); 74 75 mWifiStateFilter = new IntentFilter(WifiManager.WIFI_STATE_CHANGED_ACTION); 76 mWifiStateFilter.addAction(WifiManager.NETWORK_STATE_CHANGED_ACTION); 77 } 78 79 public void resume() { 80 int state = mWifiManager.getWifiState(); 81 // This is the widget enabled state, not the preference toggled state 82 mWifiCheckBoxPref.setEnabled(state == WIFI_STATE_ENABLED || state == WIFI_STATE_DISABLED 83 || state == WIFI_STATE_UNKNOWN); 84 85 mContext.registerReceiver(mWifiStateReceiver, mWifiStateFilter); 86 mWifiCheckBoxPref.setOnPreferenceChangeListener(this); 87 } 88 89 public void pause() { 90 mContext.unregisterReceiver(mWifiStateReceiver); 91 mWifiCheckBoxPref.setOnPreferenceChangeListener(null); 92 } 93 94 public boolean onPreferenceChange(Preference preference, Object value) { 95 // Turn on/off Wi-Fi 96 setWifiEnabled((Boolean) value); 97 98 // Don't update UI to opposite state until we're sure 99 return false; 100 } 101 102 private void setWifiEnabled(final boolean enable) { 103 // Disable button 104 mWifiCheckBoxPref.setEnabled(false); 105 106 if (!mWifiManager.setWifiEnabled(enable)) { 107 mWifiCheckBoxPref.setSummary(enable ? R.string.error_starting : R.string.error_stopping); 108 } 109 } 110 111 private void handleWifiStateChanged(int wifiState, int previousWifiState) { 112 113 if (LOCAL_LOGD) { 114 Log.d(TAG, "Received wifi state changed from " 115 + getHumanReadableWifiState(previousWifiState) + " to " 116 + getHumanReadableWifiState(wifiState)); 117 } 118 119 if (wifiState == WIFI_STATE_DISABLED || wifiState == WIFI_STATE_ENABLED) { 120 mWifiCheckBoxPref.setChecked(wifiState == WIFI_STATE_ENABLED); 121 mWifiCheckBoxPref 122 .setSummary(wifiState == WIFI_STATE_DISABLED ? mOriginalSummary : null); 123 124 mWifiCheckBoxPref.setEnabled(isEnabledByDependency()); 125 126 } else if (wifiState == WIFI_STATE_DISABLING || wifiState == WIFI_STATE_ENABLING) { 127 mWifiCheckBoxPref.setSummary(wifiState == WIFI_STATE_ENABLING ? R.string.wifi_starting 128 : R.string.wifi_stopping); 129 130 } else if (wifiState == WIFI_STATE_UNKNOWN) { 131 int message = R.string.wifi_error; 132 if (previousWifiState == WIFI_STATE_ENABLING) message = R.string.error_starting; 133 else if (previousWifiState == WIFI_STATE_DISABLING) message = R.string.error_stopping; 134 135 mWifiCheckBoxPref.setChecked(false); 136 mWifiCheckBoxPref.setSummary(message); 137 mWifiCheckBoxPref.setEnabled(true); 138 } 139 } 140 141 private void handleNetworkStateChanged(NetworkInfo networkInfo) { 142 143 if (LOCAL_LOGD) { 144 Log.d(TAG, "Received network state changed to " + networkInfo); 145 } 146 147 if (mWifiManager.isWifiEnabled()) { 148 String summary = WifiStatus.getStatus(mContext, 149 mWifiManager.getConnectionInfo().getSSID(), networkInfo.getDetailedState()); 150 mWifiCheckBoxPref.setSummary(summary); 151 } 152 } 153 154 private boolean isEnabledByDependency() { 155 Preference dep = getDependencyPreference(); 156 if (dep == null) { 157 return true; 158 } 159 160 return !dep.shouldDisableDependents(); 161 } 162 163 private Preference getDependencyPreference() { 164 String depKey = mWifiCheckBoxPref.getDependency(); 165 if (TextUtils.isEmpty(depKey)) { 166 return null; 167 } 168 169 return mWifiCheckBoxPref.getPreferenceManager().findPreference(depKey); 170 } 171 172 private static String getHumanReadableWifiState(int wifiState) { 173 switch (wifiState) { 174 case WIFI_STATE_DISABLED: 175 return "Disabled"; 176 case WIFI_STATE_DISABLING: 177 return "Disabling"; 178 case WIFI_STATE_ENABLED: 179 return "Enabled"; 180 case WIFI_STATE_ENABLING: 181 return "Enabling"; 182 case WIFI_STATE_UNKNOWN: 183 return "Unknown"; 184 default: 185 return "Some other state!"; 186 } 187 } 188} 189