| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2008 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 | |
| 17 | package com.android.server; |
| 18 | |
| 19 | import static android.net.wifi.WifiManager.WIFI_STATE_DISABLED; |
| 20 | import static android.net.wifi.WifiManager.WIFI_STATE_DISABLING; |
| 21 | import static android.net.wifi.WifiManager.WIFI_STATE_ENABLED; |
| 22 | import static android.net.wifi.WifiManager.WIFI_STATE_ENABLING; |
| 23 | import static android.net.wifi.WifiManager.WIFI_STATE_UNKNOWN; |
| 24 | |
| Irfan Sheriff | 5321aef | 2010-02-12 12:35:59 -0800 | [diff] [blame] | 25 | import static android.net.wifi.WifiManager.WIFI_AP_STATE_DISABLED; |
| 26 | import static android.net.wifi.WifiManager.WIFI_AP_STATE_DISABLING; |
| 27 | import static android.net.wifi.WifiManager.WIFI_AP_STATE_ENABLED; |
| 28 | import static android.net.wifi.WifiManager.WIFI_AP_STATE_ENABLING; |
| 29 | import static android.net.wifi.WifiManager.WIFI_AP_STATE_FAILED; |
| 30 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 31 | import android.app.AlarmManager; |
| 32 | import android.app.PendingIntent; |
| The Android Open Source Project | b2a3dd8 | 2009-03-09 11:52:12 -0700 | [diff] [blame] | 33 | import android.bluetooth.BluetoothA2dp; |
| Jaikumar Ganesh | 084c665 | 2009-12-07 10:58:18 -0800 | [diff] [blame] | 34 | import android.bluetooth.BluetoothDevice; |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 35 | import android.content.BroadcastReceiver; |
| 36 | import android.content.ContentResolver; |
| 37 | import android.content.Context; |
| 38 | import android.content.Intent; |
| 39 | import android.content.IntentFilter; |
| 40 | import android.content.pm.PackageManager; |
| 41 | import android.net.wifi.IWifiManager; |
| 42 | import android.net.wifi.WifiInfo; |
| 43 | import android.net.wifi.WifiManager; |
| 44 | import android.net.wifi.WifiNative; |
| 45 | import android.net.wifi.WifiStateTracker; |
| 46 | import android.net.wifi.ScanResult; |
| 47 | import android.net.wifi.WifiConfiguration; |
| San Mehat | 0310f9a | 2009-07-07 10:49:47 -0700 | [diff] [blame] | 48 | import android.net.wifi.SupplicantState; |
| Irfan Sheriff | 9ab518ad | 2010-03-12 15:48:17 -0800 | [diff] [blame] | 49 | import android.net.wifi.WifiConfiguration.KeyMgmt; |
| Irfan Sheriff | 5321aef | 2010-02-12 12:35:59 -0800 | [diff] [blame] | 50 | import android.net.ConnectivityManager; |
| 51 | import android.net.InterfaceConfiguration; |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 52 | import android.net.NetworkStateTracker; |
| 53 | import android.net.DhcpInfo; |
| Mike Lockwood | 0900f36 | 2009-07-10 17:24:07 -0400 | [diff] [blame] | 54 | import android.net.NetworkUtils; |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 55 | import android.os.Binder; |
| 56 | import android.os.Handler; |
| 57 | import android.os.HandlerThread; |
| 58 | import android.os.IBinder; |
| Irfan Sheriff | 5321aef | 2010-02-12 12:35:59 -0800 | [diff] [blame] | 59 | import android.os.INetworkManagementService; |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 60 | import android.os.Looper; |
| 61 | import android.os.Message; |
| 62 | import android.os.PowerManager; |
| Dianne Hackborn | 617f877 | 2009-03-31 15:04:46 -0700 | [diff] [blame] | 63 | import android.os.Process; |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 64 | import android.os.RemoteException; |
| Amith Yamasani | 47873e5 | 2009-07-02 12:05:32 -0700 | [diff] [blame] | 65 | import android.os.ServiceManager; |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 66 | import android.provider.Settings; |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 67 | import android.util.Slog; |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 68 | import android.text.TextUtils; |
| 69 | |
| 70 | import java.util.ArrayList; |
| 71 | import java.util.BitSet; |
| 72 | import java.util.HashMap; |
| 73 | import java.util.LinkedHashMap; |
| 74 | import java.util.List; |
| 75 | import java.util.Map; |
| Jaikumar Ganesh | 084c665 | 2009-12-07 10:58:18 -0800 | [diff] [blame] | 76 | import java.util.Set; |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 77 | import java.util.regex.Pattern; |
| 78 | import java.io.FileDescriptor; |
| 79 | import java.io.PrintWriter; |
| Irfan Sheriff | 5321aef | 2010-02-12 12:35:59 -0800 | [diff] [blame] | 80 | import java.net.UnknownHostException; |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 81 | |
| The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 82 | import com.android.internal.app.IBatteryStats; |
| Christopher Tate | 4528186 | 2010-03-05 15:46:30 -0800 | [diff] [blame] | 83 | import android.app.backup.IBackupManager; |
| The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 84 | import com.android.server.am.BatteryStatsService; |
| Irfan Sheriff | 9ab518ad | 2010-03-12 15:48:17 -0800 | [diff] [blame] | 85 | import com.android.internal.R; |
| The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 86 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 87 | /** |
| 88 | * WifiService handles remote WiFi operation requests by implementing |
| 89 | * the IWifiManager interface. It also creates a WifiMonitor to listen |
| 90 | * for Wifi-related events. |
| 91 | * |
| 92 | * @hide |
| 93 | */ |
| 94 | public class WifiService extends IWifiManager.Stub { |
| 95 | private static final String TAG = "WifiService"; |
| 96 | private static final boolean DBG = false; |
| 97 | private static final Pattern scanResultPattern = Pattern.compile("\t+"); |
| 98 | private final WifiStateTracker mWifiStateTracker; |
| Irfan Sheriff | c2f54c2 | 2010-03-18 14:02:22 -0700 | [diff] [blame] | 99 | /* TODO: fetch a configurable interface */ |
| 100 | private static final String SOFTAP_IFACE = "wl0.1"; |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 101 | |
| 102 | private Context mContext; |
| Irfan Sheriff | 5321aef | 2010-02-12 12:35:59 -0800 | [diff] [blame] | 103 | private int mWifiApState; |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 104 | |
| 105 | private AlarmManager mAlarmManager; |
| 106 | private PendingIntent mIdleIntent; |
| 107 | private static final int IDLE_REQUEST = 0; |
| 108 | private boolean mScreenOff; |
| 109 | private boolean mDeviceIdle; |
| 110 | private int mPluggedType; |
| 111 | |
| Irfan Sheriff | 9ab518ad | 2010-03-12 15:48:17 -0800 | [diff] [blame] | 112 | private enum DriverAction {DRIVER_UNLOAD, NO_DRIVER_UNLOAD}; |
| 113 | |
| Mike Lockwood | bd5ddf0 | 2009-07-29 21:37:14 -0700 | [diff] [blame] | 114 | // true if the user enabled Wifi while in airplane mode |
| 115 | private boolean mAirplaneModeOverwridden; |
| 116 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 117 | private final LockList mLocks = new LockList(); |
| Eric Shienbrood | 5711fad | 2009-03-27 20:25:31 -0700 | [diff] [blame] | 118 | // some wifi lock statistics |
| 119 | private int mFullLocksAcquired; |
| 120 | private int mFullLocksReleased; |
| 121 | private int mScanLocksAcquired; |
| 122 | private int mScanLocksReleased; |
| The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 123 | |
| Robert Greenwalt | 58ff021 | 2009-05-19 15:53:54 -0700 | [diff] [blame] | 124 | private final List<Multicaster> mMulticasters = |
| 125 | new ArrayList<Multicaster>(); |
| Robert Greenwalt | 5347bd4 | 2009-05-13 15:10:16 -0700 | [diff] [blame] | 126 | private int mMulticastEnabled; |
| 127 | private int mMulticastDisabled; |
| 128 | |
| The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 129 | private final IBatteryStats mBatteryStats; |
| Jaikumar Ganesh | 084c665 | 2009-12-07 10:58:18 -0800 | [diff] [blame] | 130 | |
| Irfan Sheriff | 5321aef | 2010-02-12 12:35:59 -0800 | [diff] [blame] | 131 | private INetworkManagementService nwService; |
| 132 | ConnectivityManager mCm; |
| Irfan Sheriff | 7b00978 | 2010-03-11 16:37:45 -0800 | [diff] [blame] | 133 | private WifiWatchdogService mWifiWatchdogService = null; |
| Irfan Sheriff | 5321aef | 2010-02-12 12:35:59 -0800 | [diff] [blame] | 134 | private String[] mWifiRegexs; |
| 135 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 136 | /** |
| Doug Zongker | 43866e0 | 2010-01-07 12:09:54 -0800 | [diff] [blame] | 137 | * See {@link Settings.Secure#WIFI_IDLE_MS}. This is the default value if a |
| 138 | * Settings.Secure value is not present. This timeout value is chosen as |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 139 | * the approximate point at which the battery drain caused by Wi-Fi |
| 140 | * being enabled but not active exceeds the battery drain caused by |
| 141 | * re-establishing a connection to the mobile data network. |
| 142 | */ |
| 143 | private static final long DEFAULT_IDLE_MILLIS = 15 * 60 * 1000; /* 15 minutes */ |
| 144 | |
| 145 | private static final String WAKELOCK_TAG = "WifiService"; |
| 146 | |
| 147 | /** |
| 148 | * The maximum amount of time to hold the wake lock after a disconnect |
| 149 | * caused by stopping the driver. Establishing an EDGE connection has been |
| 150 | * observed to take about 5 seconds under normal circumstances. This |
| 151 | * provides a bit of extra margin. |
| 152 | * <p> |
| 153 | * See {@link android.provider.Settings.Secure#WIFI_MOBILE_DATA_TRANSITION_WAKELOCK_TIMEOUT_MS}. |
| 154 | * This is the default value if a Settings.Secure value is not present. |
| 155 | */ |
| 156 | private static final int DEFAULT_WAKELOCK_TIMEOUT = 8000; |
| 157 | |
| 158 | // Wake lock used by driver-stop operation |
| 159 | private static PowerManager.WakeLock sDriverStopWakeLock; |
| 160 | // Wake lock used by other operations |
| 161 | private static PowerManager.WakeLock sWakeLock; |
| 162 | |
| Irfan Sheriff | 59610c0 | 2010-03-30 11:00:41 -0700 | [diff] [blame] | 163 | private static final int MESSAGE_ENABLE_WIFI = 0; |
| 164 | private static final int MESSAGE_DISABLE_WIFI = 1; |
| 165 | private static final int MESSAGE_STOP_WIFI = 2; |
| 166 | private static final int MESSAGE_START_WIFI = 3; |
| 167 | private static final int MESSAGE_RELEASE_WAKELOCK = 4; |
| 168 | private static final int MESSAGE_UPDATE_STATE = 5; |
| Irfan Sheriff | 5321aef | 2010-02-12 12:35:59 -0800 | [diff] [blame] | 169 | private static final int MESSAGE_START_ACCESS_POINT = 6; |
| 170 | private static final int MESSAGE_STOP_ACCESS_POINT = 7; |
| Irfan Sheriff | 59610c0 | 2010-03-30 11:00:41 -0700 | [diff] [blame] | 171 | private static final int MESSAGE_SET_CHANNELS = 8; |
| Irfan Sheriff | 5321aef | 2010-02-12 12:35:59 -0800 | [diff] [blame] | 172 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 173 | |
| 174 | private final WifiHandler mWifiHandler; |
| 175 | |
| 176 | /* |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 177 | * Cache of scan results objects (size is somewhat arbitrary) |
| 178 | */ |
| 179 | private static final int SCAN_RESULT_CACHE_SIZE = 80; |
| 180 | private final LinkedHashMap<String, ScanResult> mScanResultCache; |
| 181 | |
| 182 | /* |
| 183 | * Character buffer used to parse scan results (optimization) |
| 184 | */ |
| 185 | private static final int SCAN_RESULT_BUFFER_SIZE = 512; |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 186 | private boolean mNeedReconfig; |
| 187 | |
| Dianne Hackborn | 617f877 | 2009-03-31 15:04:46 -0700 | [diff] [blame] | 188 | /* |
| 189 | * Last UID that asked to enable WIFI. |
| 190 | */ |
| 191 | private int mLastEnableUid = Process.myUid(); |
| Jaikumar Ganesh | 084c665 | 2009-12-07 10:58:18 -0800 | [diff] [blame] | 192 | |
| Irfan Sheriff | b2e6c01 | 2010-04-05 11:57:56 -0700 | [diff] [blame] | 193 | /* |
| 194 | * Last UID that asked to enable WIFI AP. |
| 195 | */ |
| 196 | private int mLastApEnableUid = Process.myUid(); |
| 197 | |
| 198 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 199 | /** |
| 200 | * Number of allowed radio frequency channels in various regulatory domains. |
| 201 | * This list is sufficient for 802.11b/g networks (2.4GHz range). |
| 202 | */ |
| 203 | private static int[] sValidRegulatoryChannelCounts = new int[] {11, 13, 14}; |
| 204 | |
| 205 | private static final String ACTION_DEVICE_IDLE = |
| 206 | "com.android.server.WifiManager.action.DEVICE_IDLE"; |
| 207 | |
| 208 | WifiService(Context context, WifiStateTracker tracker) { |
| 209 | mContext = context; |
| 210 | mWifiStateTracker = tracker; |
| Mike Lockwood | f32be16 | 2009-07-14 17:44:37 -0400 | [diff] [blame] | 211 | mWifiStateTracker.enableRssiPolling(true); |
| The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 212 | mBatteryStats = BatteryStatsService.getService(); |
| Jaikumar Ganesh | 084c665 | 2009-12-07 10:58:18 -0800 | [diff] [blame] | 213 | |
| Irfan Sheriff | 5321aef | 2010-02-12 12:35:59 -0800 | [diff] [blame] | 214 | IBinder b = ServiceManager.getService(Context.NETWORKMANAGEMENT_SERVICE); |
| 215 | nwService = INetworkManagementService.Stub.asInterface(b); |
| 216 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 217 | mScanResultCache = new LinkedHashMap<String, ScanResult>( |
| 218 | SCAN_RESULT_CACHE_SIZE, 0.75f, true) { |
| 219 | /* |
| 220 | * Limit the cache size by SCAN_RESULT_CACHE_SIZE |
| 221 | * elements |
| 222 | */ |
| 223 | public boolean removeEldestEntry(Map.Entry eldest) { |
| 224 | return SCAN_RESULT_CACHE_SIZE < this.size(); |
| 225 | } |
| 226 | }; |
| 227 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 228 | HandlerThread wifiThread = new HandlerThread("WifiService"); |
| 229 | wifiThread.start(); |
| 230 | mWifiHandler = new WifiHandler(wifiThread.getLooper()); |
| 231 | |
| Irfan Sheriff | 0f34406009 | 2010-03-10 10:05:51 -0800 | [diff] [blame] | 232 | mWifiStateTracker.setWifiState(WIFI_STATE_DISABLED); |
| Irfan Sheriff | 5321aef | 2010-02-12 12:35:59 -0800 | [diff] [blame] | 233 | mWifiApState = WIFI_AP_STATE_DISABLED; |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 234 | |
| 235 | mAlarmManager = (AlarmManager)mContext.getSystemService(Context.ALARM_SERVICE); |
| 236 | Intent idleIntent = new Intent(ACTION_DEVICE_IDLE, null); |
| 237 | mIdleIntent = PendingIntent.getBroadcast(mContext, IDLE_REQUEST, idleIntent, 0); |
| 238 | |
| 239 | PowerManager powerManager = (PowerManager)mContext.getSystemService(Context.POWER_SERVICE); |
| 240 | sWakeLock = powerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, WAKELOCK_TAG); |
| 241 | sDriverStopWakeLock = powerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, WAKELOCK_TAG); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 242 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 243 | mContext.registerReceiver( |
| 244 | new BroadcastReceiver() { |
| 245 | @Override |
| 246 | public void onReceive(Context context, Intent intent) { |
| Mike Lockwood | bd5ddf0 | 2009-07-29 21:37:14 -0700 | [diff] [blame] | 247 | // clear our flag indicating the user has overwridden airplane mode |
| 248 | mAirplaneModeOverwridden = false; |
| Irfan Sheriff | b2e6c01 | 2010-04-05 11:57:56 -0700 | [diff] [blame] | 249 | // on airplane disable, restore Wifi if the saved state indicates so |
| 250 | if (!isAirplaneModeOn() && testAndClearWifiSavedState()) { |
| 251 | persistWifiEnabled(true); |
| 252 | } |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 253 | updateWifiState(); |
| 254 | } |
| 255 | }, |
| 256 | new IntentFilter(Intent.ACTION_AIRPLANE_MODE_CHANGED)); |
| 257 | |
| Irfan Sheriff | 5321aef | 2010-02-12 12:35:59 -0800 | [diff] [blame] | 258 | mContext.registerReceiver( |
| 259 | new BroadcastReceiver() { |
| 260 | @Override |
| 261 | public void onReceive(Context context, Intent intent) { |
| 262 | |
| 263 | ArrayList<String> available = intent.getStringArrayListExtra( |
| 264 | ConnectivityManager.EXTRA_AVAILABLE_TETHER); |
| 265 | ArrayList<String> active = intent.getStringArrayListExtra( |
| 266 | ConnectivityManager.EXTRA_ACTIVE_TETHER); |
| 267 | updateTetherState(available, active); |
| 268 | |
| 269 | } |
| 270 | },new IntentFilter(ConnectivityManager.ACTION_TETHER_STATE_CHANGED)); |
| Irfan Sheriff | 7b00978 | 2010-03-11 16:37:45 -0800 | [diff] [blame] | 271 | } |
| Irfan Sheriff | 5321aef | 2010-02-12 12:35:59 -0800 | [diff] [blame] | 272 | |
| Irfan Sheriff | 7b00978 | 2010-03-11 16:37:45 -0800 | [diff] [blame] | 273 | /** |
| 274 | * Check if Wi-Fi needs to be enabled and start |
| 275 | * if needed |
| Irfan Sheriff | 60e3ba0 | 2010-04-02 12:18:45 -0700 | [diff] [blame] | 276 | * |
| 277 | * This function is used only at boot time |
| Irfan Sheriff | 7b00978 | 2010-03-11 16:37:45 -0800 | [diff] [blame] | 278 | */ |
| 279 | public void startWifi() { |
| Irfan Sheriff | a3bd409 | 2010-03-24 17:58:59 -0700 | [diff] [blame] | 280 | /* Start if Wi-Fi is enabled or the saved state indicates Wi-Fi was on */ |
| Irfan Sheriff | 60e3ba0 | 2010-04-02 12:18:45 -0700 | [diff] [blame] | 281 | boolean wifiEnabled = !isAirplaneModeOn() |
| 282 | && (getPersistedWifiEnabled() || testAndClearWifiSavedState()); |
| Irfan Sheriff | 7b00978 | 2010-03-11 16:37:45 -0800 | [diff] [blame] | 283 | Slog.i(TAG, "WifiService starting up with Wi-Fi " + |
| 284 | (wifiEnabled ? "enabled" : "disabled")); |
| Irfan Sheriff | b99fe5e | 2010-03-26 14:56:07 -0700 | [diff] [blame] | 285 | setWifiEnabled(wifiEnabled); |
| Irfan Sheriff | 5321aef | 2010-02-12 12:35:59 -0800 | [diff] [blame] | 286 | } |
| 287 | |
| 288 | private void updateTetherState(ArrayList<String> available, ArrayList<String> tethered) { |
| 289 | |
| 290 | boolean wifiTethered = false; |
| 291 | boolean wifiAvailable = false; |
| 292 | |
| 293 | IBinder b = ServiceManager.getService(Context.NETWORKMANAGEMENT_SERVICE); |
| 294 | INetworkManagementService service = INetworkManagementService.Stub.asInterface(b); |
| 295 | |
| 296 | mCm = (ConnectivityManager)mContext.getSystemService(Context.CONNECTIVITY_SERVICE); |
| 297 | mWifiRegexs = mCm.getTetherableWifiRegexs(); |
| 298 | |
| 299 | for (String intf : available) { |
| 300 | for (String regex : mWifiRegexs) { |
| 301 | if (intf.matches(regex)) { |
| 302 | |
| 303 | InterfaceConfiguration ifcg = null; |
| 304 | try { |
| 305 | ifcg = service.getInterfaceConfig(intf); |
| 306 | if (ifcg != null) { |
| Robert Greenwalt | bfb7bfa | 2010-03-24 16:03:21 -0700 | [diff] [blame] | 307 | /* IP/netmask: 192.168.43.1/255.255.255.0 */ |
| 308 | ifcg.ipAddr = (192 << 24) + (168 << 16) + (43 << 8) + 1; |
| Irfan Sheriff | 5321aef | 2010-02-12 12:35:59 -0800 | [diff] [blame] | 309 | ifcg.netmask = (255 << 24) + (255 << 16) + (255 << 8) + 0; |
| 310 | ifcg.interfaceFlags = "up"; |
| 311 | |
| 312 | service.setInterfaceConfig(intf, ifcg); |
| 313 | } |
| 314 | } catch (Exception e) { |
| Irfan Sheriff | 5321aef | 2010-02-12 12:35:59 -0800 | [diff] [blame] | 315 | Slog.e(TAG, "Error configuring interface " + intf + ", :" + e); |
| Irfan Sheriff | 1a54301 | 2010-03-17 19:46:32 -0700 | [diff] [blame] | 316 | try { |
| 317 | nwService.stopAccessPoint(); |
| 318 | } catch (Exception ee) { |
| 319 | Slog.e(TAG, "Could not stop AP, :" + ee); |
| 320 | } |
| Irfan Sheriff | 9ab518ad | 2010-03-12 15:48:17 -0800 | [diff] [blame] | 321 | setWifiApEnabledState(WIFI_AP_STATE_FAILED, 0, DriverAction.DRIVER_UNLOAD); |
| Irfan Sheriff | 5321aef | 2010-02-12 12:35:59 -0800 | [diff] [blame] | 322 | return; |
| 323 | } |
| 324 | |
| Irfan Sheriff | 9ab518ad | 2010-03-12 15:48:17 -0800 | [diff] [blame] | 325 | if(mCm.tether(intf) != ConnectivityManager.TETHER_ERROR_NO_ERROR) { |
| Irfan Sheriff | 5321aef | 2010-02-12 12:35:59 -0800 | [diff] [blame] | 326 | Slog.e(TAG, "Error tethering "+intf); |
| 327 | } |
| 328 | break; |
| 329 | } |
| 330 | } |
| 331 | } |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 332 | } |
| 333 | |
| Irfan Sheriff | a3bd409 | 2010-03-24 17:58:59 -0700 | [diff] [blame] | 334 | private boolean testAndClearWifiSavedState() { |
| 335 | final ContentResolver cr = mContext.getContentResolver(); |
| 336 | int wifiSavedState = 0; |
| 337 | try { |
| 338 | wifiSavedState = Settings.Secure.getInt(cr, Settings.Secure.WIFI_SAVED_STATE); |
| 339 | if(wifiSavedState == 1) |
| 340 | Settings.Secure.putInt(cr, Settings.Secure.WIFI_SAVED_STATE, 0); |
| 341 | } catch (Settings.SettingNotFoundException e) { |
| 342 | ; |
| 343 | } |
| 344 | return (wifiSavedState == 1); |
| 345 | } |
| 346 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 347 | private boolean getPersistedWifiEnabled() { |
| 348 | final ContentResolver cr = mContext.getContentResolver(); |
| 349 | try { |
| 350 | return Settings.Secure.getInt(cr, Settings.Secure.WIFI_ON) == 1; |
| 351 | } catch (Settings.SettingNotFoundException e) { |
| 352 | Settings.Secure.putInt(cr, Settings.Secure.WIFI_ON, 0); |
| 353 | return false; |
| 354 | } |
| 355 | } |
| 356 | |
| 357 | private void persistWifiEnabled(boolean enabled) { |
| 358 | final ContentResolver cr = mContext.getContentResolver(); |
| 359 | Settings.Secure.putInt(cr, Settings.Secure.WIFI_ON, enabled ? 1 : 0); |
| 360 | } |
| 361 | |
| 362 | NetworkStateTracker getNetworkStateTracker() { |
| 363 | return mWifiStateTracker; |
| 364 | } |
| 365 | |
| 366 | /** |
| 367 | * see {@link android.net.wifi.WifiManager#pingSupplicant()} |
| 368 | * @return {@code true} if the operation succeeds |
| 369 | */ |
| 370 | public boolean pingSupplicant() { |
| 371 | enforceChangePermission(); |
| Irfan Sheriff | a8fbe1f | 2010-03-09 09:13:58 -0800 | [diff] [blame] | 372 | |
| 373 | return mWifiStateTracker.ping(); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 374 | } |
| 375 | |
| 376 | /** |
| 377 | * see {@link android.net.wifi.WifiManager#startScan()} |
| 378 | * @return {@code true} if the operation succeeds |
| 379 | */ |
| Mike Lockwood | a5ec95c | 2009-07-08 17:11:17 -0400 | [diff] [blame] | 380 | public boolean startScan(boolean forceActive) { |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 381 | enforceChangePermission(); |
| Irfan Sheriff | a8fbe1f | 2010-03-09 09:13:58 -0800 | [diff] [blame] | 382 | |
| 383 | switch (mWifiStateTracker.getSupplicantState()) { |
| 384 | case DISCONNECTED: |
| 385 | case INACTIVE: |
| 386 | case SCANNING: |
| 387 | case DORMANT: |
| 388 | break; |
| 389 | default: |
| 390 | mWifiStateTracker.setScanResultHandling( |
| 391 | WifiStateTracker.SUPPL_SCAN_HANDLING_LIST_ONLY); |
| 392 | break; |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 393 | } |
| Irfan Sheriff | a8fbe1f | 2010-03-09 09:13:58 -0800 | [diff] [blame] | 394 | return mWifiStateTracker.scan(forceActive); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 395 | } |
| 396 | |
| 397 | /** |
| 398 | * see {@link android.net.wifi.WifiManager#setWifiEnabled(boolean)} |
| 399 | * @param enable {@code true} to enable, {@code false} to disable. |
| 400 | * @return {@code true} if the enable/disable operation was |
| 401 | * started or is already in the queue. |
| 402 | */ |
| 403 | public boolean setWifiEnabled(boolean enable) { |
| 404 | enforceChangePermission(); |
| 405 | if (mWifiHandler == null) return false; |
| 406 | |
| 407 | synchronized (mWifiHandler) { |
| Robert Greenwalt | a99f461 | 2009-09-19 18:14:32 -0700 | [diff] [blame] | 408 | // caller may not have WAKE_LOCK permission - it's not required here |
| 409 | long ident = Binder.clearCallingIdentity(); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 410 | sWakeLock.acquire(); |
| Robert Greenwalt | a99f461 | 2009-09-19 18:14:32 -0700 | [diff] [blame] | 411 | Binder.restoreCallingIdentity(ident); |
| 412 | |
| Dianne Hackborn | 617f877 | 2009-03-31 15:04:46 -0700 | [diff] [blame] | 413 | mLastEnableUid = Binder.getCallingUid(); |
| Mike Lockwood | bd5ddf0 | 2009-07-29 21:37:14 -0700 | [diff] [blame] | 414 | // set a flag if the user is enabling Wifi while in airplane mode |
| 415 | mAirplaneModeOverwridden = (enable && isAirplaneModeOn() && isAirplaneToggleable()); |
| Dianne Hackborn | 617f877 | 2009-03-31 15:04:46 -0700 | [diff] [blame] | 416 | sendEnableMessage(enable, true, Binder.getCallingUid()); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 417 | } |
| 418 | |
| 419 | return true; |
| 420 | } |
| 421 | |
| 422 | /** |
| 423 | * Enables/disables Wi-Fi synchronously. |
| 424 | * @param enable {@code true} to turn Wi-Fi on, {@code false} to turn it off. |
| 425 | * @param persist {@code true} if the setting should be persisted. |
| Dianne Hackborn | 617f877 | 2009-03-31 15:04:46 -0700 | [diff] [blame] | 426 | * @param uid The UID of the process making the request. |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 427 | * @return {@code true} if the operation succeeds (or if the existing state |
| 428 | * is the same as the requested state) |
| 429 | */ |
| Dianne Hackborn | 617f877 | 2009-03-31 15:04:46 -0700 | [diff] [blame] | 430 | private boolean setWifiEnabledBlocking(boolean enable, boolean persist, int uid) { |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 431 | final int eventualWifiState = enable ? WIFI_STATE_ENABLED : WIFI_STATE_DISABLED; |
| Irfan Sheriff | 0f34406009 | 2010-03-10 10:05:51 -0800 | [diff] [blame] | 432 | final int wifiState = mWifiStateTracker.getWifiState(); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 433 | |
| Irfan Sheriff | 0f34406009 | 2010-03-10 10:05:51 -0800 | [diff] [blame] | 434 | if (wifiState == eventualWifiState) { |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 435 | return true; |
| 436 | } |
| Mike Lockwood | bd5ddf0 | 2009-07-29 21:37:14 -0700 | [diff] [blame] | 437 | if (enable && isAirplaneModeOn() && !mAirplaneModeOverwridden) { |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 438 | return false; |
| 439 | } |
| 440 | |
| Irfan Sheriff | cd77037 | 2010-01-08 09:36:04 -0800 | [diff] [blame] | 441 | /** |
| 442 | * Multiple calls to unregisterReceiver() cause exception and a system crash. |
| 443 | * This can happen if a supplicant is lost (or firmware crash occurs) and user indicates |
| 444 | * disable wifi at the same time. |
| 445 | * Avoid doing a disable when the current Wifi state is UNKNOWN |
| 446 | * TODO: Handle driver load fail and supplicant lost as seperate states |
| 447 | */ |
| Irfan Sheriff | 0f34406009 | 2010-03-10 10:05:51 -0800 | [diff] [blame] | 448 | if ((wifiState == WIFI_STATE_UNKNOWN) && !enable) { |
| Irfan Sheriff | cd77037 | 2010-01-08 09:36:04 -0800 | [diff] [blame] | 449 | return false; |
| 450 | } |
| 451 | |
| Irfan Sheriff | f91444c | 2010-03-24 12:11:00 -0700 | [diff] [blame] | 452 | /** |
| 453 | * Fail Wifi if AP is enabled |
| 454 | * TODO: Deprecate WIFI_STATE_UNKNOWN and rename it |
| 455 | * WIFI_STATE_FAILED |
| 456 | */ |
| Irfan Sheriff | 5321aef | 2010-02-12 12:35:59 -0800 | [diff] [blame] | 457 | if ((mWifiApState == WIFI_AP_STATE_ENABLED) && enable) { |
| Irfan Sheriff | f91444c | 2010-03-24 12:11:00 -0700 | [diff] [blame] | 458 | setWifiEnabledState(WIFI_STATE_UNKNOWN, uid); |
| 459 | return false; |
| Irfan Sheriff | 5321aef | 2010-02-12 12:35:59 -0800 | [diff] [blame] | 460 | } |
| 461 | |
| Irfan Sheriff | f91444c | 2010-03-24 12:11:00 -0700 | [diff] [blame] | 462 | setWifiEnabledState(enable ? WIFI_STATE_ENABLING : WIFI_STATE_DISABLING, uid); |
| 463 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 464 | if (enable) { |
| Irfan Sheriff | a8fbe1f | 2010-03-09 09:13:58 -0800 | [diff] [blame] | 465 | if (!mWifiStateTracker.loadDriver()) { |
| 466 | Slog.e(TAG, "Failed to load Wi-Fi driver."); |
| 467 | setWifiEnabledState(WIFI_STATE_UNKNOWN, uid); |
| 468 | return false; |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 469 | } |
| Irfan Sheriff | a8fbe1f | 2010-03-09 09:13:58 -0800 | [diff] [blame] | 470 | if (!mWifiStateTracker.startSupplicant()) { |
| 471 | mWifiStateTracker.unloadDriver(); |
| 472 | Slog.e(TAG, "Failed to start supplicant daemon."); |
| 473 | setWifiEnabledState(WIFI_STATE_UNKNOWN, uid); |
| 474 | return false; |
| 475 | } |
| 476 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 477 | registerForBroadcasts(); |
| 478 | mWifiStateTracker.startEventLoop(); |
| Irfan Sheriff | 7b00978 | 2010-03-11 16:37:45 -0800 | [diff] [blame] | 479 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 480 | } else { |
| 481 | |
| 482 | mContext.unregisterReceiver(mReceiver); |
| 483 | // Remove notification (it will no-op if it isn't visible) |
| 484 | mWifiStateTracker.setNotificationVisible(false, 0, false, 0); |
| 485 | |
| 486 | boolean failedToStopSupplicantOrUnloadDriver = false; |
| Irfan Sheriff | a8fbe1f | 2010-03-09 09:13:58 -0800 | [diff] [blame] | 487 | |
| 488 | if (!mWifiStateTracker.stopSupplicant()) { |
| 489 | Slog.e(TAG, "Failed to stop supplicant daemon."); |
| 490 | setWifiEnabledState(WIFI_STATE_UNKNOWN, uid); |
| 491 | failedToStopSupplicantOrUnloadDriver = true; |
| 492 | } |
| 493 | |
| 494 | /** |
| 495 | * Reset connections and disable interface |
| 496 | * before we unload the driver |
| 497 | */ |
| 498 | mWifiStateTracker.resetConnections(true); |
| 499 | |
| 500 | if (!mWifiStateTracker.unloadDriver()) { |
| 501 | Slog.e(TAG, "Failed to unload Wi-Fi driver."); |
| 502 | if (!failedToStopSupplicantOrUnloadDriver) { |
| Dianne Hackborn | 617f877 | 2009-03-31 15:04:46 -0700 | [diff] [blame] | 503 | setWifiEnabledState(WIFI_STATE_UNKNOWN, uid); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 504 | failedToStopSupplicantOrUnloadDriver = true; |
| 505 | } |
| 506 | } |
| Irfan Sheriff | a8fbe1f | 2010-03-09 09:13:58 -0800 | [diff] [blame] | 507 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 508 | if (failedToStopSupplicantOrUnloadDriver) { |
| 509 | return false; |
| 510 | } |
| 511 | } |
| 512 | |
| 513 | // Success! |
| 514 | |
| 515 | if (persist) { |
| 516 | persistWifiEnabled(enable); |
| 517 | } |
| Dianne Hackborn | 617f877 | 2009-03-31 15:04:46 -0700 | [diff] [blame] | 518 | setWifiEnabledState(eventualWifiState, uid); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 519 | return true; |
| 520 | } |
| 521 | |
| Dianne Hackborn | 617f877 | 2009-03-31 15:04:46 -0700 | [diff] [blame] | 522 | private void setWifiEnabledState(int wifiState, int uid) { |
| Irfan Sheriff | 0f34406009 | 2010-03-10 10:05:51 -0800 | [diff] [blame] | 523 | final int previousWifiState = mWifiStateTracker.getWifiState(); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 524 | |
| The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 525 | long ident = Binder.clearCallingIdentity(); |
| 526 | try { |
| 527 | if (wifiState == WIFI_STATE_ENABLED) { |
| Dianne Hackborn | 617f877 | 2009-03-31 15:04:46 -0700 | [diff] [blame] | 528 | mBatteryStats.noteWifiOn(uid); |
| The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 529 | } else if (wifiState == WIFI_STATE_DISABLED) { |
| Dianne Hackborn | 617f877 | 2009-03-31 15:04:46 -0700 | [diff] [blame] | 530 | mBatteryStats.noteWifiOff(uid); |
| The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 531 | } |
| 532 | } catch (RemoteException e) { |
| 533 | } finally { |
| 534 | Binder.restoreCallingIdentity(ident); |
| 535 | } |
| Jaikumar Ganesh | 084c665 | 2009-12-07 10:58:18 -0800 | [diff] [blame] | 536 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 537 | // Update state |
| Irfan Sheriff | 0f34406009 | 2010-03-10 10:05:51 -0800 | [diff] [blame] | 538 | mWifiStateTracker.setWifiState(wifiState); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 539 | |
| 540 | // Broadcast |
| 541 | final Intent intent = new Intent(WifiManager.WIFI_STATE_CHANGED_ACTION); |
| 542 | intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY_BEFORE_BOOT); |
| 543 | intent.putExtra(WifiManager.EXTRA_WIFI_STATE, wifiState); |
| 544 | intent.putExtra(WifiManager.EXTRA_PREVIOUS_WIFI_STATE, previousWifiState); |
| 545 | mContext.sendStickyBroadcast(intent); |
| 546 | } |
| 547 | |
| 548 | private void enforceAccessPermission() { |
| 549 | mContext.enforceCallingOrSelfPermission(android.Manifest.permission.ACCESS_WIFI_STATE, |
| 550 | "WifiService"); |
| 551 | } |
| 552 | |
| 553 | private void enforceChangePermission() { |
| 554 | mContext.enforceCallingOrSelfPermission(android.Manifest.permission.CHANGE_WIFI_STATE, |
| 555 | "WifiService"); |
| 556 | |
| 557 | } |
| 558 | |
| Robert Greenwalt | fc1b15c | 2009-05-22 15:09:51 -0700 | [diff] [blame] | 559 | private void enforceMulticastChangePermission() { |
| 560 | mContext.enforceCallingOrSelfPermission( |
| 561 | android.Manifest.permission.CHANGE_WIFI_MULTICAST_STATE, |
| 562 | "WifiService"); |
| 563 | } |
| 564 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 565 | /** |
| 566 | * see {@link WifiManager#getWifiState()} |
| 567 | * @return One of {@link WifiManager#WIFI_STATE_DISABLED}, |
| 568 | * {@link WifiManager#WIFI_STATE_DISABLING}, |
| 569 | * {@link WifiManager#WIFI_STATE_ENABLED}, |
| 570 | * {@link WifiManager#WIFI_STATE_ENABLING}, |
| 571 | * {@link WifiManager#WIFI_STATE_UNKNOWN} |
| 572 | */ |
| 573 | public int getWifiEnabledState() { |
| 574 | enforceAccessPermission(); |
| Irfan Sheriff | 0f34406009 | 2010-03-10 10:05:51 -0800 | [diff] [blame] | 575 | return mWifiStateTracker.getWifiState(); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 576 | } |
| 577 | |
| 578 | /** |
| 579 | * see {@link android.net.wifi.WifiManager#disconnect()} |
| 580 | * @return {@code true} if the operation succeeds |
| 581 | */ |
| 582 | public boolean disconnect() { |
| 583 | enforceChangePermission(); |
| Irfan Sheriff | a8fbe1f | 2010-03-09 09:13:58 -0800 | [diff] [blame] | 584 | |
| 585 | return mWifiStateTracker.disconnect(); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 586 | } |
| 587 | |
| 588 | /** |
| 589 | * see {@link android.net.wifi.WifiManager#reconnect()} |
| 590 | * @return {@code true} if the operation succeeds |
| 591 | */ |
| 592 | public boolean reconnect() { |
| 593 | enforceChangePermission(); |
| Irfan Sheriff | a8fbe1f | 2010-03-09 09:13:58 -0800 | [diff] [blame] | 594 | |
| 595 | return mWifiStateTracker.reconnectCommand(); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 596 | } |
| 597 | |
| 598 | /** |
| 599 | * see {@link android.net.wifi.WifiManager#reassociate()} |
| 600 | * @return {@code true} if the operation succeeds |
| 601 | */ |
| 602 | public boolean reassociate() { |
| 603 | enforceChangePermission(); |
| Irfan Sheriff | a8fbe1f | 2010-03-09 09:13:58 -0800 | [diff] [blame] | 604 | |
| 605 | return mWifiStateTracker.reassociate(); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 606 | } |
| 607 | |
| Irfan Sheriff | 5321aef | 2010-02-12 12:35:59 -0800 | [diff] [blame] | 608 | /** |
| Irfan Sheriff | c2f54c2 | 2010-03-18 14:02:22 -0700 | [diff] [blame] | 609 | * see {@link android.net.wifi.WifiManager#setWifiApEnabled(WifiConfiguration, boolean)} |
| Irfan Sheriff | 5321aef | 2010-02-12 12:35:59 -0800 | [diff] [blame] | 610 | * @param wifiConfig SSID, security and channel details as |
| 611 | * part of WifiConfiguration |
| Irfan Sheriff | c2f54c2 | 2010-03-18 14:02:22 -0700 | [diff] [blame] | 612 | * @param enabled, true to enable and false to disable |
| Irfan Sheriff | 5321aef | 2010-02-12 12:35:59 -0800 | [diff] [blame] | 613 | * @return {@code true} if the start operation was |
| 614 | * started or is already in the queue. |
| 615 | */ |
| 616 | public boolean setWifiApEnabled(WifiConfiguration wifiConfig, boolean enabled) { |
| 617 | enforceChangePermission(); |
| 618 | if (mWifiHandler == null) return false; |
| 619 | |
| 620 | synchronized (mWifiHandler) { |
| 621 | |
| 622 | long ident = Binder.clearCallingIdentity(); |
| 623 | sWakeLock.acquire(); |
| 624 | Binder.restoreCallingIdentity(ident); |
| 625 | |
| Irfan Sheriff | b2e6c01 | 2010-04-05 11:57:56 -0700 | [diff] [blame] | 626 | mLastApEnableUid = Binder.getCallingUid(); |
| Irfan Sheriff | 5321aef | 2010-02-12 12:35:59 -0800 | [diff] [blame] | 627 | sendAccessPointMessage(enabled, wifiConfig, Binder.getCallingUid()); |
| 628 | } |
| 629 | |
| 630 | return true; |
| 631 | } |
| 632 | |
| Irfan Sheriff | 9ab518ad | 2010-03-12 15:48:17 -0800 | [diff] [blame] | 633 | public WifiConfiguration getWifiApConfiguration() { |
| 634 | final ContentResolver cr = mContext.getContentResolver(); |
| 635 | WifiConfiguration wifiConfig = new WifiConfiguration(); |
| 636 | int authType; |
| 637 | try { |
| 638 | wifiConfig.SSID = Settings.Secure.getString(cr, Settings.Secure.WIFI_AP_SSID); |
| 639 | if (wifiConfig.SSID == null) |
| 640 | return null; |
| 641 | authType = Settings.Secure.getInt(cr, Settings.Secure.WIFI_AP_SECURITY); |
| 642 | wifiConfig.allowedKeyManagement.set(authType); |
| 643 | wifiConfig.preSharedKey = Settings.Secure.getString(cr, Settings.Secure.WIFI_AP_PASSWD); |
| 644 | return wifiConfig; |
| 645 | } catch (Settings.SettingNotFoundException e) { |
| 646 | Slog.e(TAG,"AP settings not found, returning"); |
| 647 | return null; |
| 648 | } |
| 649 | } |
| 650 | |
| 651 | private void persistApConfiguration(WifiConfiguration wifiConfig) { |
| 652 | final ContentResolver cr = mContext.getContentResolver(); |
| 653 | boolean isWpa; |
| 654 | if (wifiConfig == null) |
| 655 | return; |
| 656 | Settings.Secure.putString(cr, Settings.Secure.WIFI_AP_SSID, wifiConfig.SSID); |
| 657 | isWpa = wifiConfig.allowedKeyManagement.get(KeyMgmt.WPA_PSK); |
| 658 | Settings.Secure.putInt(cr, |
| 659 | Settings.Secure.WIFI_AP_SECURITY, |
| 660 | isWpa ? KeyMgmt.WPA_PSK : KeyMgmt.NONE); |
| 661 | if (isWpa) |
| 662 | Settings.Secure.putString(cr, Settings.Secure.WIFI_AP_PASSWD, wifiConfig.preSharedKey); |
| 663 | } |
| 664 | |
| Irfan Sheriff | 5321aef | 2010-02-12 12:35:59 -0800 | [diff] [blame] | 665 | /** |
| 666 | * Enables/disables Wi-Fi AP synchronously. The driver is loaded |
| 667 | * and soft access point configured as a single operation. |
| 668 | * @param enable {@code true} to turn Wi-Fi on, {@code false} to turn it off. |
| Irfan Sheriff | 5321aef | 2010-02-12 12:35:59 -0800 | [diff] [blame] | 669 | * @param uid The UID of the process making the request. |
| Irfan Sheriff | 9ab518ad | 2010-03-12 15:48:17 -0800 | [diff] [blame] | 670 | * @param wifiConfig The WifiConfiguration for AP |
| Irfan Sheriff | 5321aef | 2010-02-12 12:35:59 -0800 | [diff] [blame] | 671 | * @return {@code true} if the operation succeeds (or if the existing state |
| 672 | * is the same as the requested state) |
| 673 | */ |
| Irfan Sheriff | 5321aef | 2010-02-12 12:35:59 -0800 | [diff] [blame] | 674 | private boolean setWifiApEnabledBlocking(boolean enable, |
| Irfan Sheriff | 9ab518ad | 2010-03-12 15:48:17 -0800 | [diff] [blame] | 675 | int uid, WifiConfiguration wifiConfig) { |
| Irfan Sheriff | 5321aef | 2010-02-12 12:35:59 -0800 | [diff] [blame] | 676 | final int eventualWifiApState = enable ? WIFI_AP_STATE_ENABLED : WIFI_AP_STATE_DISABLED; |
| 677 | |
| 678 | if (mWifiApState == eventualWifiApState) { |
| Irfan Sheriff | 9ab518ad | 2010-03-12 15:48:17 -0800 | [diff] [blame] | 679 | /* Configuration changed on a running access point */ |
| 680 | if(enable && (wifiConfig != null)) { |
| 681 | try { |
| Irfan Sheriff | c2f54c2 | 2010-03-18 14:02:22 -0700 | [diff] [blame] | 682 | nwService.setAccessPoint(wifiConfig, mWifiStateTracker.getInterfaceName(), |
| 683 | SOFTAP_IFACE); |
| Irfan Sheriff | afadc8b | 2010-06-11 14:43:14 -0700 | [diff] [blame^] | 684 | persistApConfiguration(wifiConfig); |
| Irfan Sheriff | 9ab518ad | 2010-03-12 15:48:17 -0800 | [diff] [blame] | 685 | return true; |
| 686 | } catch(Exception e) { |
| 687 | Slog.e(TAG, "Exception in nwService during AP restart"); |
| Irfan Sheriff | c2f54c2 | 2010-03-18 14:02:22 -0700 | [diff] [blame] | 688 | try { |
| 689 | nwService.stopAccessPoint(); |
| 690 | } catch (Exception ee) { |
| 691 | Slog.e(TAG, "Could not stop AP, :" + ee); |
| 692 | } |
| Irfan Sheriff | 9ab518ad | 2010-03-12 15:48:17 -0800 | [diff] [blame] | 693 | setWifiApEnabledState(WIFI_AP_STATE_FAILED, uid, DriverAction.DRIVER_UNLOAD); |
| 694 | return false; |
| 695 | } |
| 696 | } else { |
| 697 | return true; |
| 698 | } |
| Irfan Sheriff | 5321aef | 2010-02-12 12:35:59 -0800 | [diff] [blame] | 699 | } |
| 700 | |
| Irfan Sheriff | f91444c | 2010-03-24 12:11:00 -0700 | [diff] [blame] | 701 | /** |
| 702 | * Fail AP if Wifi is enabled |
| 703 | */ |
| 704 | if ((mWifiStateTracker.getWifiState() == WIFI_STATE_ENABLED) && enable) { |
| 705 | setWifiApEnabledState(WIFI_AP_STATE_FAILED, uid, DriverAction.NO_DRIVER_UNLOAD); |
| 706 | return false; |
| 707 | } |
| 708 | |
| Irfan Sheriff | 9ab518ad | 2010-03-12 15:48:17 -0800 | [diff] [blame] | 709 | setWifiApEnabledState(enable ? WIFI_AP_STATE_ENABLING : |
| 710 | WIFI_AP_STATE_DISABLING, uid, DriverAction.NO_DRIVER_UNLOAD); |
| Irfan Sheriff | 5321aef | 2010-02-12 12:35:59 -0800 | [diff] [blame] | 711 | |
| 712 | if (enable) { |
| Irfan Sheriff | 9ab518ad | 2010-03-12 15:48:17 -0800 | [diff] [blame] | 713 | |
| Irfan Sheriff | 9ab518ad | 2010-03-12 15:48:17 -0800 | [diff] [blame] | 714 | /* Use default config if there is no existing config */ |
| 715 | if (wifiConfig == null && ((wifiConfig = getWifiApConfiguration()) == null)) { |
| 716 | wifiConfig = new WifiConfiguration(); |
| 717 | wifiConfig.SSID = mContext.getString(R.string.wifi_tether_configure_ssid_default); |
| 718 | wifiConfig.allowedKeyManagement.set(KeyMgmt.NONE); |
| 719 | } |
| Irfan Sheriff | 9ab518ad | 2010-03-12 15:48:17 -0800 | [diff] [blame] | 720 | |
| Irfan Sheriff | a8fbe1f | 2010-03-09 09:13:58 -0800 | [diff] [blame] | 721 | if (!mWifiStateTracker.loadDriver()) { |
| 722 | Slog.e(TAG, "Failed to load Wi-Fi driver for AP mode"); |
| Irfan Sheriff | 9ab518ad | 2010-03-12 15:48:17 -0800 | [diff] [blame] | 723 | setWifiApEnabledState(WIFI_AP_STATE_FAILED, uid, DriverAction.NO_DRIVER_UNLOAD); |
| Irfan Sheriff | a8fbe1f | 2010-03-09 09:13:58 -0800 | [diff] [blame] | 724 | return false; |
| Irfan Sheriff | 5321aef | 2010-02-12 12:35:59 -0800 | [diff] [blame] | 725 | } |
| 726 | |
| 727 | try { |
| Irfan Sheriff | c2f54c2 | 2010-03-18 14:02:22 -0700 | [diff] [blame] | 728 | nwService.startAccessPoint(wifiConfig, mWifiStateTracker.getInterfaceName(), |
| 729 | SOFTAP_IFACE); |
| Irfan Sheriff | 5321aef | 2010-02-12 12:35:59 -0800 | [diff] [blame] | 730 | } catch(Exception e) { |
| 731 | Slog.e(TAG, "Exception in startAccessPoint()"); |
| Irfan Sheriff | 9ab518ad | 2010-03-12 15:48:17 -0800 | [diff] [blame] | 732 | setWifiApEnabledState(WIFI_AP_STATE_FAILED, uid, DriverAction.DRIVER_UNLOAD); |
| 733 | return false; |
| Irfan Sheriff | 5321aef | 2010-02-12 12:35:59 -0800 | [diff] [blame] | 734 | } |
| 735 | |
| Irfan Sheriff | afadc8b | 2010-06-11 14:43:14 -0700 | [diff] [blame^] | 736 | persistApConfiguration(wifiConfig); |
| 737 | |
| Irfan Sheriff | 5321aef | 2010-02-12 12:35:59 -0800 | [diff] [blame] | 738 | } else { |
| 739 | |
| 740 | try { |
| 741 | nwService.stopAccessPoint(); |
| 742 | } catch(Exception e) { |
| 743 | Slog.e(TAG, "Exception in stopAccessPoint()"); |
| Irfan Sheriff | 9ab518ad | 2010-03-12 15:48:17 -0800 | [diff] [blame] | 744 | setWifiApEnabledState(WIFI_AP_STATE_FAILED, uid, DriverAction.DRIVER_UNLOAD); |
| 745 | return false; |
| Irfan Sheriff | 5321aef | 2010-02-12 12:35:59 -0800 | [diff] [blame] | 746 | } |
| 747 | |
| Irfan Sheriff | a8fbe1f | 2010-03-09 09:13:58 -0800 | [diff] [blame] | 748 | if (!mWifiStateTracker.unloadDriver()) { |
| 749 | Slog.e(TAG, "Failed to unload Wi-Fi driver for AP mode"); |
| Irfan Sheriff | 9ab518ad | 2010-03-12 15:48:17 -0800 | [diff] [blame] | 750 | setWifiApEnabledState(WIFI_AP_STATE_FAILED, uid, DriverAction.NO_DRIVER_UNLOAD); |
| Irfan Sheriff | a8fbe1f | 2010-03-09 09:13:58 -0800 | [diff] [blame] | 751 | return false; |
| Irfan Sheriff | 5321aef | 2010-02-12 12:35:59 -0800 | [diff] [blame] | 752 | } |
| 753 | } |
| 754 | |
| Irfan Sheriff | 9ab518ad | 2010-03-12 15:48:17 -0800 | [diff] [blame] | 755 | setWifiApEnabledState(eventualWifiApState, uid, DriverAction.NO_DRIVER_UNLOAD); |
| Irfan Sheriff | 5321aef | 2010-02-12 12:35:59 -0800 | [diff] [blame] | 756 | return true; |
| 757 | } |
| 758 | |
| 759 | /** |
| 760 | * see {@link WifiManager#getWifiApState()} |
| 761 | * @return One of {@link WifiManager#WIFI_AP_STATE_DISABLED}, |
| 762 | * {@link WifiManager#WIFI_AP_STATE_DISABLING}, |
| 763 | * {@link WifiManager#WIFI_AP_STATE_ENABLED}, |
| 764 | * {@link WifiManager#WIFI_AP_STATE_ENABLING}, |
| 765 | * {@link WifiManager#WIFI_AP_STATE_FAILED} |
| 766 | */ |
| 767 | public int getWifiApEnabledState() { |
| 768 | enforceAccessPermission(); |
| 769 | return mWifiApState; |
| 770 | } |
| 771 | |
| Irfan Sheriff | 9ab518ad | 2010-03-12 15:48:17 -0800 | [diff] [blame] | 772 | private void setWifiApEnabledState(int wifiAPState, int uid, DriverAction flag) { |
| Irfan Sheriff | 5321aef | 2010-02-12 12:35:59 -0800 | [diff] [blame] | 773 | final int previousWifiApState = mWifiApState; |
| 774 | |
| Irfan Sheriff | 9ab518ad | 2010-03-12 15:48:17 -0800 | [diff] [blame] | 775 | /** |
| 776 | * Unload the driver if going to a failed state |
| 777 | */ |
| 778 | if ((mWifiApState == WIFI_AP_STATE_FAILED) && (flag == DriverAction.DRIVER_UNLOAD)) { |
| 779 | mWifiStateTracker.unloadDriver(); |
| 780 | } |
| 781 | |
| Irfan Sheriff | 5321aef | 2010-02-12 12:35:59 -0800 | [diff] [blame] | 782 | long ident = Binder.clearCallingIdentity(); |
| 783 | try { |
| 784 | if (wifiAPState == WIFI_AP_STATE_ENABLED) { |
| 785 | mBatteryStats.noteWifiOn(uid); |
| 786 | } else if (wifiAPState == WIFI_AP_STATE_DISABLED) { |
| 787 | mBatteryStats.noteWifiOff(uid); |
| 788 | } |
| 789 | } catch (RemoteException e) { |
| 790 | } finally { |
| 791 | Binder.restoreCallingIdentity(ident); |
| 792 | } |
| 793 | |
| 794 | // Update state |
| 795 | mWifiApState = wifiAPState; |
| 796 | |
| 797 | // Broadcast |
| 798 | final Intent intent = new Intent(WifiManager.WIFI_AP_STATE_CHANGED_ACTION); |
| 799 | intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY_BEFORE_BOOT); |
| 800 | intent.putExtra(WifiManager.EXTRA_WIFI_AP_STATE, wifiAPState); |
| 801 | intent.putExtra(WifiManager.EXTRA_PREVIOUS_WIFI_AP_STATE, previousWifiApState); |
| 802 | mContext.sendStickyBroadcast(intent); |
| 803 | } |
| 804 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 805 | /** |
| 806 | * see {@link android.net.wifi.WifiManager#getConfiguredNetworks()} |
| 807 | * @return the list of configured networks |
| 808 | */ |
| 809 | public List<WifiConfiguration> getConfiguredNetworks() { |
| 810 | enforceAccessPermission(); |
| 811 | String listStr; |
| Irfan Sheriff | a8fbe1f | 2010-03-09 09:13:58 -0800 | [diff] [blame] | 812 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 813 | /* |
| 814 | * We don't cache the list, because we want to allow |
| 815 | * for the possibility that the configuration file |
| 816 | * has been modified through some external means, |
| 817 | * such as the wpa_cli command line program. |
| 818 | */ |
| Irfan Sheriff | a8fbe1f | 2010-03-09 09:13:58 -0800 | [diff] [blame] | 819 | listStr = mWifiStateTracker.listNetworks(); |
| 820 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 821 | List<WifiConfiguration> networks = |
| 822 | new ArrayList<WifiConfiguration>(); |
| 823 | if (listStr == null) |
| 824 | return networks; |
| 825 | |
| 826 | String[] lines = listStr.split("\n"); |
| 827 | // Skip the first line, which is a header |
| 828 | for (int i = 1; i < lines.length; i++) { |
| 829 | String[] result = lines[i].split("\t"); |
| 830 | // network-id | ssid | bssid | flags |
| 831 | WifiConfiguration config = new WifiConfiguration(); |
| 832 | try { |
| 833 | config.networkId = Integer.parseInt(result[0]); |
| 834 | } catch(NumberFormatException e) { |
| 835 | continue; |
| 836 | } |
| 837 | if (result.length > 3) { |
| 838 | if (result[3].indexOf("[CURRENT]") != -1) |
| 839 | config.status = WifiConfiguration.Status.CURRENT; |
| 840 | else if (result[3].indexOf("[DISABLED]") != -1) |
| 841 | config.status = WifiConfiguration.Status.DISABLED; |
| 842 | else |
| 843 | config.status = WifiConfiguration.Status.ENABLED; |
| Irfan Sheriff | a8fbe1f | 2010-03-09 09:13:58 -0800 | [diff] [blame] | 844 | } else { |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 845 | config.status = WifiConfiguration.Status.ENABLED; |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 846 | } |
| Irfan Sheriff | a8fbe1f | 2010-03-09 09:13:58 -0800 | [diff] [blame] | 847 | readNetworkVariables(config); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 848 | networks.add(config); |
| 849 | } |
| 850 | |
| 851 | return networks; |
| 852 | } |
| 853 | |
| 854 | /** |
| 855 | * Read the variables from the supplicant daemon that are needed to |
| 856 | * fill in the WifiConfiguration object. |
| 857 | * <p/> |
| 858 | * The caller must hold the synchronization monitor. |
| 859 | * @param config the {@link WifiConfiguration} object to be filled in. |
| 860 | */ |
| Irfan Sheriff | a8fbe1f | 2010-03-09 09:13:58 -0800 | [diff] [blame] | 861 | private void readNetworkVariables(WifiConfiguration config) { |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 862 | |
| 863 | int netId = config.networkId; |
| 864 | if (netId < 0) |
| 865 | return; |
| 866 | |
| 867 | /* |
| 868 | * TODO: maybe should have a native method that takes an array of |
| 869 | * variable names and returns an array of values. But we'd still |
| 870 | * be doing a round trip to the supplicant daemon for each variable. |
| 871 | */ |
| 872 | String value; |
| 873 | |
| Irfan Sheriff | a8fbe1f | 2010-03-09 09:13:58 -0800 | [diff] [blame] | 874 | value = mWifiStateTracker.getNetworkVariable(netId, WifiConfiguration.ssidVarName); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 875 | if (!TextUtils.isEmpty(value)) { |
| Chung-yih Wang | 047076d | 2010-05-15 11:03:30 +0800 | [diff] [blame] | 876 | config.SSID = value; |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 877 | } else { |
| 878 | config.SSID = null; |
| 879 | } |
| 880 | |
| Irfan Sheriff | a8fbe1f | 2010-03-09 09:13:58 -0800 | [diff] [blame] | 881 | value = mWifiStateTracker.getNetworkVariable(netId, WifiConfiguration.bssidVarName); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 882 | if (!TextUtils.isEmpty(value)) { |
| 883 | config.BSSID = value; |
| 884 | } else { |
| 885 | config.BSSID = null; |
| 886 | } |
| 887 | |
| Irfan Sheriff | a8fbe1f | 2010-03-09 09:13:58 -0800 | [diff] [blame] | 888 | value = mWifiStateTracker.getNetworkVariable(netId, WifiConfiguration.priorityVarName); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 889 | config.priority = -1; |
| 890 | if (!TextUtils.isEmpty(value)) { |
| 891 | try { |
| 892 | config.priority = Integer.parseInt(value); |
| 893 | } catch (NumberFormatException ignore) { |
| 894 | } |
| 895 | } |
| 896 | |
| Irfan Sheriff | a8fbe1f | 2010-03-09 09:13:58 -0800 | [diff] [blame] | 897 | value = mWifiStateTracker.getNetworkVariable(netId, WifiConfiguration.hiddenSSIDVarName); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 898 | config.hiddenSSID = false; |
| 899 | if (!TextUtils.isEmpty(value)) { |
| 900 | try { |
| 901 | config.hiddenSSID = Integer.parseInt(value) != 0; |
| 902 | } catch (NumberFormatException ignore) { |
| 903 | } |
| 904 | } |
| 905 | |
| Irfan Sheriff | a8fbe1f | 2010-03-09 09:13:58 -0800 | [diff] [blame] | 906 | value = mWifiStateTracker.getNetworkVariable(netId, WifiConfiguration.wepTxKeyIdxVarName); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 907 | config.wepTxKeyIndex = -1; |
| 908 | if (!TextUtils.isEmpty(value)) { |
| 909 | try { |
| 910 | config.wepTxKeyIndex = Integer.parseInt(value); |
| 911 | } catch (NumberFormatException ignore) { |
| 912 | } |
| 913 | } |
| 914 | |
| 915 | /* |
| 916 | * Get up to 4 WEP keys. Note that the actual keys are not passed back, |
| 917 | * just a "*" if the key is set, or the null string otherwise. |
| 918 | */ |
| 919 | for (int i = 0; i < 4; i++) { |
| Irfan Sheriff | a8fbe1f | 2010-03-09 09:13:58 -0800 | [diff] [blame] | 920 | value = mWifiStateTracker.getNetworkVariable(netId, WifiConfiguration.wepKeyVarNames[i]); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 921 | if (!TextUtils.isEmpty(value)) { |
| 922 | config.wepKeys[i] = value; |
| 923 | } else { |
| 924 | config.wepKeys[i] = null; |
| 925 | } |
| 926 | } |
| 927 | |
| 928 | /* |
| 929 | * Get the private shared key. Note that the actual keys are not passed back, |
| 930 | * just a "*" if the key is set, or the null string otherwise. |
| 931 | */ |
| Irfan Sheriff | a8fbe1f | 2010-03-09 09:13:58 -0800 | [diff] [blame] | 932 | value = mWifiStateTracker.getNetworkVariable(netId, WifiConfiguration.pskVarName); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 933 | if (!TextUtils.isEmpty(value)) { |
| 934 | config.preSharedKey = value; |
| 935 | } else { |
| 936 | config.preSharedKey = null; |
| 937 | } |
| 938 | |
| Irfan Sheriff | a8fbe1f | 2010-03-09 09:13:58 -0800 | [diff] [blame] | 939 | value = mWifiStateTracker.getNetworkVariable(config.networkId, |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 940 | WifiConfiguration.Protocol.varName); |
| 941 | if (!TextUtils.isEmpty(value)) { |
| 942 | String vals[] = value.split(" "); |
| 943 | for (String val : vals) { |
| 944 | int index = |
| 945 | lookupString(val, WifiConfiguration.Protocol.strings); |
| 946 | if (0 <= index) { |
| 947 | config.allowedProtocols.set(index); |
| 948 | } |
| 949 | } |
| 950 | } |
| 951 | |
| Irfan Sheriff | a8fbe1f | 2010-03-09 09:13:58 -0800 | [diff] [blame] | 952 | value = mWifiStateTracker.getNetworkVariable(config.networkId, |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 953 | WifiConfiguration.KeyMgmt.varName); |
| 954 | if (!TextUtils.isEmpty(value)) { |
| 955 | String vals[] = value.split(" "); |
| 956 | for (String val : vals) { |
| 957 | int index = |
| 958 | lookupString(val, WifiConfiguration.KeyMgmt.strings); |
| 959 | if (0 <= index) { |
| 960 | config.allowedKeyManagement.set(index); |
| 961 | } |
| 962 | } |
| 963 | } |
| 964 | |
| Irfan Sheriff | a8fbe1f | 2010-03-09 09:13:58 -0800 | [diff] [blame] | 965 | value = mWifiStateTracker.getNetworkVariable(config.networkId, |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 966 | WifiConfiguration.AuthAlgorithm.varName); |
| 967 | if (!TextUtils.isEmpty(value)) { |
| 968 | String vals[] = value.split(" "); |
| 969 | for (String val : vals) { |
| 970 | int index = |
| 971 | lookupString(val, WifiConfiguration.AuthAlgorithm.strings); |
| 972 | if (0 <= index) { |
| 973 | config.allowedAuthAlgorithms.set(index); |
| 974 | } |
| 975 | } |
| 976 | } |
| 977 | |
| Irfan Sheriff | a8fbe1f | 2010-03-09 09:13:58 -0800 | [diff] [blame] | 978 | value = mWifiStateTracker.getNetworkVariable(config.networkId, |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 979 | WifiConfiguration.PairwiseCipher.varName); |
| 980 | if (!TextUtils.isEmpty(value)) { |
| 981 | String vals[] = value.split(" "); |
| 982 | for (String val : vals) { |
| 983 | int index = |
| 984 | lookupString(val, WifiConfiguration.PairwiseCipher.strings); |
| 985 | if (0 <= index) { |
| 986 | config.allowedPairwiseCiphers.set(index); |
| 987 | } |
| 988 | } |
| 989 | } |
| 990 | |
| Irfan Sheriff | a8fbe1f | 2010-03-09 09:13:58 -0800 | [diff] [blame] | 991 | value = mWifiStateTracker.getNetworkVariable(config.networkId, |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 992 | WifiConfiguration.GroupCipher.varName); |
| 993 | if (!TextUtils.isEmpty(value)) { |
| 994 | String vals[] = value.split(" "); |
| 995 | for (String val : vals) { |
| 996 | int index = |
| 997 | lookupString(val, WifiConfiguration.GroupCipher.strings); |
| 998 | if (0 <= index) { |
| 999 | config.allowedGroupCiphers.set(index); |
| 1000 | } |
| 1001 | } |
| 1002 | } |
| Chung-yih Wang | 4337476 | 2009-09-16 14:28:42 +0800 | [diff] [blame] | 1003 | |
| 1004 | for (WifiConfiguration.EnterpriseField field : |
| 1005 | config.enterpriseFields) { |
| Irfan Sheriff | a8fbe1f | 2010-03-09 09:13:58 -0800 | [diff] [blame] | 1006 | value = mWifiStateTracker.getNetworkVariable(netId, |
| Chung-yih Wang | 4337476 | 2009-09-16 14:28:42 +0800 | [diff] [blame] | 1007 | field.varName()); |
| 1008 | if (!TextUtils.isEmpty(value)) { |
| Chung-yih Wang | a8d1594 | 2009-10-09 11:01:49 +0800 | [diff] [blame] | 1009 | if (field != config.eap) value = removeDoubleQuotes(value); |
| Chung-yih Wang | 4337476 | 2009-09-16 14:28:42 +0800 | [diff] [blame] | 1010 | field.setValue(value); |
| 1011 | } |
| 1012 | } |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1013 | } |
| 1014 | |
| Chung-yih Wang | a8d1594 | 2009-10-09 11:01:49 +0800 | [diff] [blame] | 1015 | private static String removeDoubleQuotes(String string) { |
| 1016 | if (string.length() <= 2) return ""; |
| 1017 | return string.substring(1, string.length() - 1); |
| 1018 | } |
| 1019 | |
| 1020 | private static String convertToQuotedString(String string) { |
| 1021 | return "\"" + string + "\""; |
| 1022 | } |
| 1023 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1024 | /** |
| 1025 | * see {@link android.net.wifi.WifiManager#addOrUpdateNetwork(WifiConfiguration)} |
| 1026 | * @return the supplicant-assigned identifier for the new or updated |
| 1027 | * network if the operation succeeds, or {@code -1} if it fails |
| 1028 | */ |
| Irfan Sheriff | 7aac554 | 2009-12-22 21:42:17 -0800 | [diff] [blame] | 1029 | public int addOrUpdateNetwork(WifiConfiguration config) { |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1030 | enforceChangePermission(); |
| Irfan Sheriff | a8fbe1f | 2010-03-09 09:13:58 -0800 | [diff] [blame] | 1031 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1032 | /* |
| 1033 | * If the supplied networkId is -1, we create a new empty |
| 1034 | * network configuration. Otherwise, the networkId should |
| 1035 | * refer to an existing configuration. |
| 1036 | */ |
| 1037 | int netId = config.networkId; |
| 1038 | boolean newNetwork = netId == -1; |
| Irfan Sheriff | 44113ba3 | 2010-03-16 14:54:07 -0700 | [diff] [blame] | 1039 | boolean doReconfig = false; |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1040 | // networkId of -1 means we want to create a new network |
| Irfan Sheriff | 7aac554 | 2009-12-22 21:42:17 -0800 | [diff] [blame] | 1041 | synchronized (mWifiStateTracker) { |
| 1042 | if (newNetwork) { |
| Irfan Sheriff | a8fbe1f | 2010-03-09 09:13:58 -0800 | [diff] [blame] | 1043 | netId = mWifiStateTracker.addNetwork(); |
| Irfan Sheriff | 7aac554 | 2009-12-22 21:42:17 -0800 | [diff] [blame] | 1044 | if (netId < 0) { |
| 1045 | if (DBG) { |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 1046 | Slog.d(TAG, "Failed to add a network!"); |
| Irfan Sheriff | 7aac554 | 2009-12-22 21:42:17 -0800 | [diff] [blame] | 1047 | } |
| 1048 | return -1; |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1049 | } |
| Irfan Sheriff | 7aac554 | 2009-12-22 21:42:17 -0800 | [diff] [blame] | 1050 | doReconfig = true; |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1051 | } |
| Irfan Sheriff | 7aac554 | 2009-12-22 21:42:17 -0800 | [diff] [blame] | 1052 | mNeedReconfig = mNeedReconfig || doReconfig; |
| Irfan Sheriff | a8fbe1f | 2010-03-09 09:13:58 -0800 | [diff] [blame] | 1053 | } |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1054 | |
| Irfan Sheriff | a8fbe1f | 2010-03-09 09:13:58 -0800 | [diff] [blame] | 1055 | setVariables: { |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1056 | /* |
| 1057 | * Note that if a networkId for a non-existent network |
| Irfan Sheriff | a8fbe1f | 2010-03-09 09:13:58 -0800 | [diff] [blame] | 1058 | * was supplied, then the first setNetworkVariable() |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1059 | * will fail, so we don't bother to make a separate check |
| 1060 | * for the validity of the ID up front. |
| 1061 | */ |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1062 | if (config.SSID != null && |
| Irfan Sheriff | a8fbe1f | 2010-03-09 09:13:58 -0800 | [diff] [blame] | 1063 | !mWifiStateTracker.setNetworkVariable( |
| Irfan Sheriff | 7aac554 | 2009-12-22 21:42:17 -0800 | [diff] [blame] | 1064 | netId, |
| 1065 | WifiConfiguration.ssidVarName, |
| Chung-yih Wang | 047076d | 2010-05-15 11:03:30 +0800 | [diff] [blame] | 1066 | config.SSID)) { |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1067 | if (DBG) { |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 1068 | Slog.d(TAG, "failed to set SSID: "+config.SSID); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1069 | } |
| 1070 | break setVariables; |
| 1071 | } |
| 1072 | |
| 1073 | if (config.BSSID != null && |
| Irfan Sheriff | a8fbe1f | 2010-03-09 09:13:58 -0800 | [diff] [blame] | 1074 | !mWifiStateTracker.setNetworkVariable( |
| Irfan Sheriff | 7aac554 | 2009-12-22 21:42:17 -0800 | [diff] [blame] | 1075 | netId, |
| 1076 | WifiConfiguration.bssidVarName, |
| 1077 | config.BSSID)) { |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1078 | if (DBG) { |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 1079 | Slog.d(TAG, "failed to set BSSID: "+config.BSSID); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1080 | } |
| 1081 | break setVariables; |
| 1082 | } |
| 1083 | |
| 1084 | String allowedKeyManagementString = |
| 1085 | makeString(config.allowedKeyManagement, WifiConfiguration.KeyMgmt.strings); |
| 1086 | if (config.allowedKeyManagement.cardinality() != 0 && |
| Irfan Sheriff | a8fbe1f | 2010-03-09 09:13:58 -0800 | [diff] [blame] | 1087 | !mWifiStateTracker.setNetworkVariable( |
| Irfan Sheriff | 7aac554 | 2009-12-22 21:42:17 -0800 | [diff] [blame] | 1088 | netId, |
| 1089 | WifiConfiguration.KeyMgmt.varName, |
| 1090 | allowedKeyManagementString)) { |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1091 | if (DBG) { |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 1092 | Slog.d(TAG, "failed to set key_mgmt: "+ |
| Irfan Sheriff | 7aac554 | 2009-12-22 21:42:17 -0800 | [diff] [blame] | 1093 | allowedKeyManagementString); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1094 | } |
| 1095 | break setVariables; |
| 1096 | } |
| 1097 | |
| 1098 | String allowedProtocolsString = |
| 1099 | makeString(config.allowedProtocols, WifiConfiguration.Protocol.strings); |
| 1100 | if (config.allowedProtocols.cardinality() != 0 && |
| Irfan Sheriff | a8fbe1f | 2010-03-09 09:13:58 -0800 | [diff] [blame] | 1101 | !mWifiStateTracker.setNetworkVariable( |
| Irfan Sheriff | 7aac554 | 2009-12-22 21:42:17 -0800 | [diff] [blame] | 1102 | netId, |
| 1103 | WifiConfiguration.Protocol.varName, |
| 1104 | allowedProtocolsString)) { |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1105 | if (DBG) { |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 1106 | Slog.d(TAG, "failed to set proto: "+ |
| Irfan Sheriff | 7aac554 | 2009-12-22 21:42:17 -0800 | [diff] [blame] | 1107 | allowedProtocolsString); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1108 | } |
| 1109 | break setVariables; |
| 1110 | } |
| 1111 | |
| 1112 | String allowedAuthAlgorithmsString = |
| 1113 | makeString(config.allowedAuthAlgorithms, WifiConfiguration.AuthAlgorithm.strings); |
| 1114 | if (config.allowedAuthAlgorithms.cardinality() != 0 && |
| Irfan Sheriff | a8fbe1f | 2010-03-09 09:13:58 -0800 | [diff] [blame] | 1115 | !mWifiStateTracker.setNetworkVariable( |
| Irfan Sheriff | 7aac554 | 2009-12-22 21:42:17 -0800 | [diff] [blame] | 1116 | netId, |
| 1117 | WifiConfiguration.AuthAlgorithm.varName, |
| 1118 | allowedAuthAlgorithmsString)) { |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1119 | if (DBG) { |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 1120 | Slog.d(TAG, "failed to set auth_alg: "+ |
| Irfan Sheriff | 7aac554 | 2009-12-22 21:42:17 -0800 | [diff] [blame] | 1121 | allowedAuthAlgorithmsString); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1122 | } |
| 1123 | break setVariables; |
| 1124 | } |
| 1125 | |
| 1126 | String allowedPairwiseCiphersString = |
| 1127 | makeString(config.allowedPairwiseCiphers, WifiConfiguration.PairwiseCipher.strings); |
| 1128 | if (config.allowedPairwiseCiphers.cardinality() != 0 && |
| Irfan Sheriff | a8fbe1f | 2010-03-09 09:13:58 -0800 | [diff] [blame] | 1129 | !mWifiStateTracker.setNetworkVariable( |
| Irfan Sheriff | 7aac554 | 2009-12-22 21:42:17 -0800 | [diff] [blame] | 1130 | netId, |
| 1131 | WifiConfiguration.PairwiseCipher.varName, |
| 1132 | allowedPairwiseCiphersString)) { |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1133 | if (DBG) { |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 1134 | Slog.d(TAG, "failed to set pairwise: "+ |
| Irfan Sheriff | 7aac554 | 2009-12-22 21:42:17 -0800 | [diff] [blame] | 1135 | allowedPairwiseCiphersString); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1136 | } |
| 1137 | break setVariables; |
| 1138 | } |
| 1139 | |
| 1140 | String allowedGroupCiphersString = |
| 1141 | makeString(config.allowedGroupCiphers, WifiConfiguration.GroupCipher.strings); |
| 1142 | if (config.allowedGroupCiphers.cardinality() != 0 && |
| Irfan Sheriff | a8fbe1f | 2010-03-09 09:13:58 -0800 | [diff] [blame] | 1143 | !mWifiStateTracker.setNetworkVariable( |
| Irfan Sheriff | 7aac554 | 2009-12-22 21:42:17 -0800 | [diff] [blame] | 1144 | netId, |
| 1145 | WifiConfiguration.GroupCipher.varName, |
| 1146 | allowedGroupCiphersString)) { |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1147 | if (DBG) { |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 1148 | Slog.d(TAG, "failed to set group: "+ |
| Irfan Sheriff | 7aac554 | 2009-12-22 21:42:17 -0800 | [diff] [blame] | 1149 | allowedGroupCiphersString); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1150 | } |
| 1151 | break setVariables; |
| 1152 | } |
| 1153 | |
| 1154 | // Prevent client screw-up by passing in a WifiConfiguration we gave it |
| 1155 | // by preventing "*" as a key. |
| 1156 | if (config.preSharedKey != null && !config.preSharedKey.equals("*") && |
| Irfan Sheriff | a8fbe1f | 2010-03-09 09:13:58 -0800 | [diff] [blame] | 1157 | !mWifiStateTracker.setNetworkVariable( |
| Irfan Sheriff | 7aac554 | 2009-12-22 21:42:17 -0800 | [diff] [blame] | 1158 | netId, |
| 1159 | WifiConfiguration.pskVarName, |
| 1160 | config.preSharedKey)) { |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1161 | if (DBG) { |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 1162 | Slog.d(TAG, "failed to set psk: "+config.preSharedKey); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1163 | } |
| 1164 | break setVariables; |
| 1165 | } |
| 1166 | |
| 1167 | boolean hasSetKey = false; |
| 1168 | if (config.wepKeys != null) { |
| 1169 | for (int i = 0; i < config.wepKeys.length; i++) { |
| 1170 | // Prevent client screw-up by passing in a WifiConfiguration we gave it |
| 1171 | // by preventing "*" as a key. |
| 1172 | if (config.wepKeys[i] != null && !config.wepKeys[i].equals("*")) { |
| Irfan Sheriff | a8fbe1f | 2010-03-09 09:13:58 -0800 | [diff] [blame] | 1173 | if (!mWifiStateTracker.setNetworkVariable( |
| Irfan Sheriff | 7aac554 | 2009-12-22 21:42:17 -0800 | [diff] [blame] | 1174 | netId, |
| 1175 | WifiConfiguration.wepKeyVarNames[i], |
| 1176 | config.wepKeys[i])) { |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1177 | if (DBG) { |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 1178 | Slog.d(TAG, |
| Irfan Sheriff | 7aac554 | 2009-12-22 21:42:17 -0800 | [diff] [blame] | 1179 | "failed to set wep_key"+i+": " + |
| 1180 | config.wepKeys[i]); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1181 | } |
| 1182 | break setVariables; |
| 1183 | } |
| 1184 | hasSetKey = true; |
| 1185 | } |
| 1186 | } |
| 1187 | } |
| 1188 | |
| 1189 | if (hasSetKey) { |
| Irfan Sheriff | a8fbe1f | 2010-03-09 09:13:58 -0800 | [diff] [blame] | 1190 | if (!mWifiStateTracker.setNetworkVariable( |
| Irfan Sheriff | 7aac554 | 2009-12-22 21:42:17 -0800 | [diff] [blame] | 1191 | netId, |
| 1192 | WifiConfiguration.wepTxKeyIdxVarName, |
| 1193 | Integer.toString(config.wepTxKeyIndex))) { |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1194 | if (DBG) { |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 1195 | Slog.d(TAG, |
| Irfan Sheriff | 7aac554 | 2009-12-22 21:42:17 -0800 | [diff] [blame] | 1196 | "failed to set wep_tx_keyidx: "+ |
| 1197 | config.wepTxKeyIndex); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1198 | } |
| 1199 | break setVariables; |
| 1200 | } |
| 1201 | } |
| 1202 | |
| Irfan Sheriff | a8fbe1f | 2010-03-09 09:13:58 -0800 | [diff] [blame] | 1203 | if (!mWifiStateTracker.setNetworkVariable( |
| Irfan Sheriff | 7aac554 | 2009-12-22 21:42:17 -0800 | [diff] [blame] | 1204 | netId, |
| 1205 | WifiConfiguration.priorityVarName, |
| 1206 | Integer.toString(config.priority))) { |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1207 | if (DBG) { |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 1208 | Slog.d(TAG, config.SSID + ": failed to set priority: " |
| Irfan Sheriff | 7aac554 | 2009-12-22 21:42:17 -0800 | [diff] [blame] | 1209 | +config.priority); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1210 | } |
| 1211 | break setVariables; |
| 1212 | } |
| 1213 | |
| Irfan Sheriff | a8fbe1f | 2010-03-09 09:13:58 -0800 | [diff] [blame] | 1214 | if (config.hiddenSSID && !mWifiStateTracker.setNetworkVariable( |
| Irfan Sheriff | 7aac554 | 2009-12-22 21:42:17 -0800 | [diff] [blame] | 1215 | netId, |
| 1216 | WifiConfiguration.hiddenSSIDVarName, |
| 1217 | Integer.toString(config.hiddenSSID ? 1 : 0))) { |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1218 | if (DBG) { |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 1219 | Slog.d(TAG, config.SSID + ": failed to set hiddenSSID: "+ |
| Irfan Sheriff | 7aac554 | 2009-12-22 21:42:17 -0800 | [diff] [blame] | 1220 | config.hiddenSSID); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1221 | } |
| 1222 | break setVariables; |
| 1223 | } |
| 1224 | |
| Chung-yih Wang | 4337476 | 2009-09-16 14:28:42 +0800 | [diff] [blame] | 1225 | for (WifiConfiguration.EnterpriseField field |
| 1226 | : config.enterpriseFields) { |
| 1227 | String varName = field.varName(); |
| 1228 | String value = field.value(); |
| Chung-yih Wang | a8d1594 | 2009-10-09 11:01:49 +0800 | [diff] [blame] | 1229 | if (value != null) { |
| 1230 | if (field != config.eap) { |
| Chia-chi Yeh | 784d53e | 2010-01-29 16:26:28 +0800 | [diff] [blame] | 1231 | value = (value.length() == 0) ? "NULL" : convertToQuotedString(value); |
| Chung-yih Wang | 4337476 | 2009-09-16 14:28:42 +0800 | [diff] [blame] | 1232 | } |
| Irfan Sheriff | a8fbe1f | 2010-03-09 09:13:58 -0800 | [diff] [blame] | 1233 | if (!mWifiStateTracker.setNetworkVariable( |
| Irfan Sheriff | 7aac554 | 2009-12-22 21:42:17 -0800 | [diff] [blame] | 1234 | netId, |
| 1235 | varName, |
| 1236 | value)) { |
| Chung-yih Wang | a8d1594 | 2009-10-09 11:01:49 +0800 | [diff] [blame] | 1237 | if (DBG) { |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 1238 | Slog.d(TAG, config.SSID + ": failed to set " + varName + |
| Irfan Sheriff | 7aac554 | 2009-12-22 21:42:17 -0800 | [diff] [blame] | 1239 | ": " + value); |
| Chung-yih Wang | a8d1594 | 2009-10-09 11:01:49 +0800 | [diff] [blame] | 1240 | } |
| 1241 | break setVariables; |
| 1242 | } |
| Chung-yih Wang | 5069cc7 | 2009-06-03 17:33:47 +0800 | [diff] [blame] | 1243 | } |
| Chung-yih Wang | 5069cc7 | 2009-06-03 17:33:47 +0800 | [diff] [blame] | 1244 | } |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1245 | return netId; |
| Irfan Sheriff | a8fbe1f | 2010-03-09 09:13:58 -0800 | [diff] [blame] | 1246 | } |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1247 | |
| Irfan Sheriff | a8fbe1f | 2010-03-09 09:13:58 -0800 | [diff] [blame] | 1248 | /* |
| 1249 | * For an update, if one of the setNetworkVariable operations fails, |
| 1250 | * we might want to roll back all the changes already made. But the |
| 1251 | * chances are that if anything is going to go wrong, it'll happen |
| 1252 | * the first time we try to set one of the variables. |
| 1253 | */ |
| 1254 | if (newNetwork) { |
| 1255 | removeNetwork(netId); |
| 1256 | if (DBG) { |
| 1257 | Slog.d(TAG, |
| 1258 | "Failed to set a network variable, removed network: " |
| 1259 | + netId); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1260 | } |
| 1261 | } |
| 1262 | return -1; |
| 1263 | } |
| 1264 | |
| 1265 | private static String makeString(BitSet set, String[] strings) { |
| 1266 | StringBuffer buf = new StringBuffer(); |
| 1267 | int nextSetBit = -1; |
| 1268 | |
| 1269 | /* Make sure all set bits are in [0, strings.length) to avoid |
| 1270 | * going out of bounds on strings. (Shouldn't happen, but...) */ |
| 1271 | set = set.get(0, strings.length); |
| 1272 | |
| 1273 | while ((nextSetBit = set.nextSetBit(nextSetBit + 1)) != -1) { |
| 1274 | buf.append(strings[nextSetBit].replace('_', '-')).append(' '); |
| 1275 | } |
| 1276 | |
| 1277 | // remove trailing space |
| 1278 | if (set.cardinality() > 0) { |
| 1279 | buf.setLength(buf.length() - 1); |
| 1280 | } |
| 1281 | |
| 1282 | return buf.toString(); |
| 1283 | } |
| 1284 | |
| 1285 | private static int lookupString(String string, String[] strings) { |
| 1286 | int size = strings.length; |
| 1287 | |
| 1288 | string = string.replace('-', '_'); |
| 1289 | |
| 1290 | for (int i = 0; i < size; i++) |
| 1291 | if (string.equals(strings[i])) |
| 1292 | return i; |
| 1293 | |
| 1294 | if (DBG) { |
| 1295 | // if we ever get here, we should probably add the |
| 1296 | // value to WifiConfiguration to reflect that it's |
| 1297 | // supported by the WPA supplicant |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 1298 | Slog.w(TAG, "Failed to look-up a string: " + string); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1299 | } |
| 1300 | |
| 1301 | return -1; |
| 1302 | } |
| 1303 | |
| 1304 | /** |
| 1305 | * See {@link android.net.wifi.WifiManager#removeNetwork(int)} |
| 1306 | * @param netId the integer that identifies the network configuration |
| 1307 | * to the supplicant |
| 1308 | * @return {@code true} if the operation succeeded |
| 1309 | */ |
| 1310 | public boolean removeNetwork(int netId) { |
| 1311 | enforceChangePermission(); |
| 1312 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1313 | return mWifiStateTracker.removeNetwork(netId); |
| 1314 | } |
| 1315 | |
| 1316 | /** |
| 1317 | * See {@link android.net.wifi.WifiManager#enableNetwork(int, boolean)} |
| 1318 | * @param netId the integer that identifies the network configuration |
| 1319 | * to the supplicant |
| 1320 | * @param disableOthers if true, disable all other networks. |
| 1321 | * @return {@code true} if the operation succeeded |
| 1322 | */ |
| 1323 | public boolean enableNetwork(int netId, boolean disableOthers) { |
| 1324 | enforceChangePermission(); |
| 1325 | |
| Irfan Sheriff | a8fbe1f | 2010-03-09 09:13:58 -0800 | [diff] [blame] | 1326 | String ifname = mWifiStateTracker.getInterfaceName(); |
| 1327 | NetworkUtils.enableInterface(ifname); |
| 1328 | boolean result = mWifiStateTracker.enableNetwork(netId, disableOthers); |
| 1329 | if (!result) { |
| 1330 | NetworkUtils.disableInterface(ifname); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1331 | } |
| Irfan Sheriff | a8fbe1f | 2010-03-09 09:13:58 -0800 | [diff] [blame] | 1332 | return result; |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1333 | } |
| 1334 | |
| 1335 | /** |
| 1336 | * See {@link android.net.wifi.WifiManager#disableNetwork(int)} |
| 1337 | * @param netId the integer that identifies the network configuration |
| 1338 | * to the supplicant |
| 1339 | * @return {@code true} if the operation succeeded |
| 1340 | */ |
| 1341 | public boolean disableNetwork(int netId) { |
| 1342 | enforceChangePermission(); |
| 1343 | |
| Irfan Sheriff | a8fbe1f | 2010-03-09 09:13:58 -0800 | [diff] [blame] | 1344 | return mWifiStateTracker.disableNetwork(netId); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1345 | } |
| 1346 | |
| 1347 | /** |
| 1348 | * See {@link android.net.wifi.WifiManager#getConnectionInfo()} |
| 1349 | * @return the Wi-Fi information, contained in {@link WifiInfo}. |
| 1350 | */ |
| 1351 | public WifiInfo getConnectionInfo() { |
| 1352 | enforceAccessPermission(); |
| 1353 | /* |
| 1354 | * Make sure we have the latest information, by sending |
| 1355 | * a status request to the supplicant. |
| 1356 | */ |
| 1357 | return mWifiStateTracker.requestConnectionInfo(); |
| 1358 | } |
| 1359 | |
| 1360 | /** |
| 1361 | * Return the results of the most recent access point scan, in the form of |
| 1362 | * a list of {@link ScanResult} objects. |
| 1363 | * @return the list of results |
| 1364 | */ |
| 1365 | public List<ScanResult> getScanResults() { |
| 1366 | enforceAccessPermission(); |
| 1367 | String reply; |
| Irfan Sheriff | a8fbe1f | 2010-03-09 09:13:58 -0800 | [diff] [blame] | 1368 | |
| 1369 | reply = mWifiStateTracker.scanResults(); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1370 | if (reply == null) { |
| 1371 | return null; |
| 1372 | } |
| 1373 | |
| 1374 | List<ScanResult> scanList = new ArrayList<ScanResult>(); |
| 1375 | |
| 1376 | int lineCount = 0; |
| 1377 | |
| 1378 | int replyLen = reply.length(); |
| 1379 | // Parse the result string, keeping in mind that the last line does |
| 1380 | // not end with a newline. |
| 1381 | for (int lineBeg = 0, lineEnd = 0; lineEnd <= replyLen; ++lineEnd) { |
| 1382 | if (lineEnd == replyLen || reply.charAt(lineEnd) == '\n') { |
| 1383 | ++lineCount; |
| 1384 | /* |
| 1385 | * Skip the first line, which is a header |
| 1386 | */ |
| 1387 | if (lineCount == 1) { |
| 1388 | lineBeg = lineEnd + 1; |
| 1389 | continue; |
| 1390 | } |
| Mike Lockwood | b30475e | 2009-04-21 13:55:07 -0700 | [diff] [blame] | 1391 | if (lineEnd > lineBeg) { |
| 1392 | String line = reply.substring(lineBeg, lineEnd); |
| 1393 | ScanResult scanResult = parseScanResult(line); |
| 1394 | if (scanResult != null) { |
| 1395 | scanList.add(scanResult); |
| 1396 | } else if (DBG) { |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 1397 | Slog.w(TAG, "misformatted scan result for: " + line); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1398 | } |
| 1399 | } |
| 1400 | lineBeg = lineEnd + 1; |
| 1401 | } |
| 1402 | } |
| 1403 | mWifiStateTracker.setScanResultsList(scanList); |
| 1404 | return scanList; |
| 1405 | } |
| 1406 | |
| 1407 | /** |
| 1408 | * Parse the scan result line passed to us by wpa_supplicant (helper). |
| 1409 | * @param line the line to parse |
| 1410 | * @return the {@link ScanResult} object |
| 1411 | */ |
| 1412 | private ScanResult parseScanResult(String line) { |
| 1413 | ScanResult scanResult = null; |
| 1414 | if (line != null) { |
| 1415 | /* |
| 1416 | * Cache implementation (LinkedHashMap) is not synchronized, thus, |
| 1417 | * must synchronized here! |
| 1418 | */ |
| 1419 | synchronized (mScanResultCache) { |
| Mike Lockwood | b30475e | 2009-04-21 13:55:07 -0700 | [diff] [blame] | 1420 | String[] result = scanResultPattern.split(line); |
| 1421 | if (3 <= result.length && result.length <= 5) { |
| 1422 | String bssid = result[0]; |
| 1423 | // bssid | frequency | level | flags | ssid |
| 1424 | int frequency; |
| 1425 | int level; |
| 1426 | try { |
| 1427 | frequency = Integer.parseInt(result[1]); |
| 1428 | level = Integer.parseInt(result[2]); |
| 1429 | /* some implementations avoid negative values by adding 256 |
| 1430 | * so we need to adjust for that here. |
| 1431 | */ |
| 1432 | if (level > 0) level -= 256; |
| 1433 | } catch (NumberFormatException e) { |
| 1434 | frequency = 0; |
| 1435 | level = 0; |
| 1436 | } |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1437 | |
| Mike Lockwood | 1a64505 | 2009-06-25 13:01:12 -0400 | [diff] [blame] | 1438 | /* |
| 1439 | * The formatting of the results returned by |
| 1440 | * wpa_supplicant is intended to make the fields |
| 1441 | * line up nicely when printed, |
| 1442 | * not to make them easy to parse. So we have to |
| 1443 | * apply some heuristics to figure out which field |
| 1444 | * is the SSID and which field is the flags. |
| 1445 | */ |
| 1446 | String ssid; |
| 1447 | String flags; |
| 1448 | if (result.length == 4) { |
| 1449 | if (result[3].charAt(0) == '[') { |
| 1450 | flags = result[3]; |
| 1451 | ssid = ""; |
| 1452 | } else { |
| 1453 | flags = ""; |
| 1454 | ssid = result[3]; |
| 1455 | } |
| 1456 | } else if (result.length == 5) { |
| 1457 | flags = result[3]; |
| 1458 | ssid = result[4]; |
| 1459 | } else { |
| 1460 | // Here, we must have 3 fields: no flags and ssid |
| 1461 | // set |
| 1462 | flags = ""; |
| 1463 | ssid = ""; |
| 1464 | } |
| 1465 | |
| Mike Lockwood | 00717e2 | 2009-08-17 10:09:36 -0400 | [diff] [blame] | 1466 | // bssid + ssid is the hash key |
| 1467 | String key = bssid + ssid; |
| 1468 | scanResult = mScanResultCache.get(key); |
| Mike Lockwood | b30475e | 2009-04-21 13:55:07 -0700 | [diff] [blame] | 1469 | if (scanResult != null) { |
| 1470 | scanResult.level = level; |
| Mike Lockwood | 1a64505 | 2009-06-25 13:01:12 -0400 | [diff] [blame] | 1471 | scanResult.SSID = ssid; |
| 1472 | scanResult.capabilities = flags; |
| 1473 | scanResult.frequency = frequency; |
| Mike Lockwood | b30475e | 2009-04-21 13:55:07 -0700 | [diff] [blame] | 1474 | } else { |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1475 | // Do not add scan results that have no SSID set |
| 1476 | if (0 < ssid.trim().length()) { |
| 1477 | scanResult = |
| 1478 | new ScanResult( |
| Mike Lockwood | b30475e | 2009-04-21 13:55:07 -0700 | [diff] [blame] | 1479 | ssid, bssid, flags, level, frequency); |
| Mike Lockwood | 00717e2 | 2009-08-17 10:09:36 -0400 | [diff] [blame] | 1480 | mScanResultCache.put(key, scanResult); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1481 | } |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1482 | } |
| Mike Lockwood | b30475e | 2009-04-21 13:55:07 -0700 | [diff] [blame] | 1483 | } else { |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 1484 | Slog.w(TAG, "Misformatted scan result text with " + |
| Mike Lockwood | b30475e | 2009-04-21 13:55:07 -0700 | [diff] [blame] | 1485 | result.length + " fields: " + line); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1486 | } |
| 1487 | } |
| 1488 | } |
| 1489 | |
| 1490 | return scanResult; |
| 1491 | } |
| 1492 | |
| 1493 | /** |
| 1494 | * Parse the "flags" field passed back in a scan result by wpa_supplicant, |
| 1495 | * and construct a {@code WifiConfiguration} that describes the encryption, |
| 1496 | * key management, and authenticaion capabilities of the access point. |
| 1497 | * @param flags the string returned by wpa_supplicant |
| 1498 | * @return the {@link WifiConfiguration} object, filled in |
| 1499 | */ |
| 1500 | WifiConfiguration parseScanFlags(String flags) { |
| 1501 | WifiConfiguration config = new WifiConfiguration(); |
| 1502 | |
| 1503 | if (flags.length() == 0) { |
| 1504 | config.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE); |
| 1505 | } |
| 1506 | // ... to be implemented |
| 1507 | return config; |
| 1508 | } |
| 1509 | |
| 1510 | /** |
| 1511 | * Tell the supplicant to persist the current list of configured networks. |
| 1512 | * @return {@code true} if the operation succeeded |
| 1513 | */ |
| 1514 | public boolean saveConfiguration() { |
| 1515 | boolean result; |
| 1516 | enforceChangePermission(); |
| Irfan Sheriff | a8fbe1f | 2010-03-09 09:13:58 -0800 | [diff] [blame] | 1517 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1518 | synchronized (mWifiStateTracker) { |
| Irfan Sheriff | a8fbe1f | 2010-03-09 09:13:58 -0800 | [diff] [blame] | 1519 | result = mWifiStateTracker.saveConfig(); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1520 | if (result && mNeedReconfig) { |
| 1521 | mNeedReconfig = false; |
| Irfan Sheriff | a8fbe1f | 2010-03-09 09:13:58 -0800 | [diff] [blame] | 1522 | result = mWifiStateTracker.reloadConfig(); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1523 | |
| 1524 | if (result) { |
| 1525 | Intent intent = new Intent(WifiManager.NETWORK_IDS_CHANGED_ACTION); |
| 1526 | mContext.sendBroadcast(intent); |
| 1527 | } |
| 1528 | } |
| 1529 | } |
| Amith Yamasani | 47873e5 | 2009-07-02 12:05:32 -0700 | [diff] [blame] | 1530 | // Inform the backup manager about a data change |
| 1531 | IBackupManager ibm = IBackupManager.Stub.asInterface( |
| 1532 | ServiceManager.getService(Context.BACKUP_SERVICE)); |
| 1533 | if (ibm != null) { |
| 1534 | try { |
| 1535 | ibm.dataChanged("com.android.providers.settings"); |
| 1536 | } catch (Exception e) { |
| 1537 | // Try again later |
| 1538 | } |
| 1539 | } |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1540 | return result; |
| 1541 | } |
| 1542 | |
| 1543 | /** |
| 1544 | * Set the number of radio frequency channels that are allowed to be used |
| 1545 | * in the current regulatory domain. This method should be used only |
| 1546 | * if the correct number of channels cannot be determined automatically |
| Robert Greenwalt | b5010cc | 2009-05-21 15:11:40 -0700 | [diff] [blame] | 1547 | * for some reason. If the operation is successful, the new value may be |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1548 | * persisted as a Secure setting. |
| 1549 | * @param numChannels the number of allowed channels. Must be greater than 0 |
| 1550 | * and less than or equal to 16. |
| Robert Greenwalt | b5010cc | 2009-05-21 15:11:40 -0700 | [diff] [blame] | 1551 | * @param persist {@code true} if the setting should be remembered. |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1552 | * @return {@code true} if the operation succeeds, {@code false} otherwise, e.g., |
| 1553 | * {@code numChannels} is outside the valid range. |
| 1554 | */ |
| Robert Greenwalt | b5010cc | 2009-05-21 15:11:40 -0700 | [diff] [blame] | 1555 | public boolean setNumAllowedChannels(int numChannels, boolean persist) { |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 1556 | Slog.i(TAG, "WifiService trying to setNumAllowed to "+numChannels+ |
| Robert Greenwalt | b5010cc | 2009-05-21 15:11:40 -0700 | [diff] [blame] | 1557 | " with persist set to "+persist); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1558 | enforceChangePermission(); |
| Irfan Sheriff | 59610c0 | 2010-03-30 11:00:41 -0700 | [diff] [blame] | 1559 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1560 | /* |
| 1561 | * Validate the argument. We'd like to let the Wi-Fi driver do this, |
| 1562 | * but if Wi-Fi isn't currently enabled, that's not possible, and |
| 1563 | * we want to persist the setting anyway,so that it will take |
| 1564 | * effect when Wi-Fi does become enabled. |
| 1565 | */ |
| 1566 | boolean found = false; |
| 1567 | for (int validChan : sValidRegulatoryChannelCounts) { |
| 1568 | if (validChan == numChannels) { |
| 1569 | found = true; |
| 1570 | break; |
| 1571 | } |
| 1572 | } |
| 1573 | if (!found) { |
| 1574 | return false; |
| 1575 | } |
| 1576 | |
| Irfan Sheriff | 59610c0 | 2010-03-30 11:00:41 -0700 | [diff] [blame] | 1577 | if (mWifiHandler == null) return false; |
| 1578 | |
| 1579 | Message.obtain(mWifiHandler, |
| 1580 | MESSAGE_SET_CHANNELS, numChannels, (persist ? 1 : 0)).sendToTarget(); |
| 1581 | |
| 1582 | return true; |
| 1583 | } |
| 1584 | |
| 1585 | /** |
| 1586 | * sets the number of allowed radio frequency channels synchronously |
| 1587 | * @param numChannels the number of allowed channels. Must be greater than 0 |
| 1588 | * and less than or equal to 16. |
| 1589 | * @param persist {@code true} if the setting should be remembered. |
| 1590 | * @return {@code true} if the operation succeeds, {@code false} otherwise |
| 1591 | */ |
| 1592 | private boolean setNumAllowedChannelsBlocking(int numChannels, boolean persist) { |
| Robert Greenwalt | b5010cc | 2009-05-21 15:11:40 -0700 | [diff] [blame] | 1593 | if (persist) { |
| 1594 | Settings.Secure.putInt(mContext.getContentResolver(), |
| Irfan Sheriff | 59610c0 | 2010-03-30 11:00:41 -0700 | [diff] [blame] | 1595 | Settings.Secure.WIFI_NUM_ALLOWED_CHANNELS, |
| 1596 | numChannels); |
| Robert Greenwalt | b5010cc | 2009-05-21 15:11:40 -0700 | [diff] [blame] | 1597 | } |
| Irfan Sheriff | 59610c0 | 2010-03-30 11:00:41 -0700 | [diff] [blame] | 1598 | return mWifiStateTracker.setNumAllowedChannels(numChannels); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1599 | } |
| 1600 | |
| 1601 | /** |
| 1602 | * Return the number of frequency channels that are allowed |
| 1603 | * to be used in the current regulatory domain. |
| 1604 | * @return the number of allowed channels, or {@code -1} if an error occurs |
| 1605 | */ |
| 1606 | public int getNumAllowedChannels() { |
| 1607 | int numChannels; |
| 1608 | |
| 1609 | enforceAccessPermission(); |
| Irfan Sheriff | a8fbe1f | 2010-03-09 09:13:58 -0800 | [diff] [blame] | 1610 | |
| 1611 | /* |
| 1612 | * If we can't get the value from the driver (e.g., because |
| 1613 | * Wi-Fi is not currently enabled), get the value from |
| 1614 | * Settings. |
| 1615 | */ |
| 1616 | numChannels = mWifiStateTracker.getNumAllowedChannels(); |
| 1617 | if (numChannels < 0) { |
| 1618 | numChannels = Settings.Secure.getInt(mContext.getContentResolver(), |
| 1619 | Settings.Secure.WIFI_NUM_ALLOWED_CHANNELS, |
| 1620 | -1); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1621 | } |
| 1622 | return numChannels; |
| 1623 | } |
| 1624 | |
| 1625 | /** |
| 1626 | * Return the list of valid values for the number of allowed radio channels |
| 1627 | * for various regulatory domains. |
| 1628 | * @return the list of channel counts |
| 1629 | */ |
| 1630 | public int[] getValidChannelCounts() { |
| 1631 | enforceAccessPermission(); |
| 1632 | return sValidRegulatoryChannelCounts; |
| 1633 | } |
| 1634 | |
| 1635 | /** |
| 1636 | * Return the DHCP-assigned addresses from the last successful DHCP request, |
| 1637 | * if any. |
| 1638 | * @return the DHCP information |
| 1639 | */ |
| 1640 | public DhcpInfo getDhcpInfo() { |
| 1641 | enforceAccessPermission(); |
| 1642 | return mWifiStateTracker.getDhcpInfo(); |
| 1643 | } |
| 1644 | |
| 1645 | private final BroadcastReceiver mReceiver = new BroadcastReceiver() { |
| 1646 | @Override |
| 1647 | public void onReceive(Context context, Intent intent) { |
| 1648 | String action = intent.getAction(); |
| 1649 | |
| Doug Zongker | 43866e0 | 2010-01-07 12:09:54 -0800 | [diff] [blame] | 1650 | long idleMillis = |
| 1651 | Settings.Secure.getLong(mContext.getContentResolver(), |
| 1652 | Settings.Secure.WIFI_IDLE_MS, DEFAULT_IDLE_MILLIS); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1653 | int stayAwakeConditions = |
| Doug Zongker | 43866e0 | 2010-01-07 12:09:54 -0800 | [diff] [blame] | 1654 | Settings.System.getInt(mContext.getContentResolver(), |
| 1655 | Settings.System.STAY_ON_WHILE_PLUGGED_IN, 0); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1656 | if (action.equals(Intent.ACTION_SCREEN_ON)) { |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 1657 | Slog.d(TAG, "ACTION_SCREEN_ON"); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1658 | mAlarmManager.cancel(mIdleIntent); |
| 1659 | mDeviceIdle = false; |
| 1660 | mScreenOff = false; |
| Mike Lockwood | f32be16 | 2009-07-14 17:44:37 -0400 | [diff] [blame] | 1661 | mWifiStateTracker.enableRssiPolling(true); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1662 | } else if (action.equals(Intent.ACTION_SCREEN_OFF)) { |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 1663 | Slog.d(TAG, "ACTION_SCREEN_OFF"); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1664 | mScreenOff = true; |
| Mike Lockwood | f32be16 | 2009-07-14 17:44:37 -0400 | [diff] [blame] | 1665 | mWifiStateTracker.enableRssiPolling(false); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1666 | /* |
| 1667 | * Set a timer to put Wi-Fi to sleep, but only if the screen is off |
| 1668 | * AND the "stay on while plugged in" setting doesn't match the |
| 1669 | * current power conditions (i.e, not plugged in, plugged in to USB, |
| 1670 | * or plugged in to AC). |
| 1671 | */ |
| 1672 | if (!shouldWifiStayAwake(stayAwakeConditions, mPluggedType)) { |
| San Mehat | fa6c711 | 2009-07-07 09:34:44 -0700 | [diff] [blame] | 1673 | WifiInfo info = mWifiStateTracker.requestConnectionInfo(); |
| 1674 | if (info.getSupplicantState() != SupplicantState.COMPLETED) { |
| Robert Greenwalt | 84612ea6 | 2009-09-30 09:04:22 -0700 | [diff] [blame] | 1675 | // we used to go to sleep immediately, but this caused some race conditions |
| 1676 | // we don't have time to track down for this release. Delay instead, but not |
| 1677 | // as long as we would if connected (below) |
| 1678 | // TODO - fix the race conditions and switch back to the immediate turn-off |
| 1679 | long triggerTime = System.currentTimeMillis() + (2*60*1000); // 2 min |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 1680 | Slog.d(TAG, "setting ACTION_DEVICE_IDLE timer for 120,000 ms"); |
| Robert Greenwalt | 84612ea6 | 2009-09-30 09:04:22 -0700 | [diff] [blame] | 1681 | mAlarmManager.set(AlarmManager.RTC_WAKEUP, triggerTime, mIdleIntent); |
| 1682 | // // do not keep Wifi awake when screen is off if Wifi is not associated |
| 1683 | // mDeviceIdle = true; |
| 1684 | // updateWifiState(); |
| Mike Lockwood | d9c32bc | 2009-05-18 14:14:15 -0400 | [diff] [blame] | 1685 | } else { |
| 1686 | long triggerTime = System.currentTimeMillis() + idleMillis; |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 1687 | Slog.d(TAG, "setting ACTION_DEVICE_IDLE timer for " + idleMillis + "ms"); |
| Mike Lockwood | d9c32bc | 2009-05-18 14:14:15 -0400 | [diff] [blame] | 1688 | mAlarmManager.set(AlarmManager.RTC_WAKEUP, triggerTime, mIdleIntent); |
| 1689 | } |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1690 | } |
| 1691 | /* we can return now -- there's nothing to do until we get the idle intent back */ |
| 1692 | return; |
| 1693 | } else if (action.equals(ACTION_DEVICE_IDLE)) { |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 1694 | Slog.d(TAG, "got ACTION_DEVICE_IDLE"); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1695 | mDeviceIdle = true; |
| 1696 | } else if (action.equals(Intent.ACTION_BATTERY_CHANGED)) { |
| 1697 | /* |
| 1698 | * Set a timer to put Wi-Fi to sleep, but only if the screen is off |
| 1699 | * AND we are transitioning from a state in which the device was supposed |
| 1700 | * to stay awake to a state in which it is not supposed to stay awake. |
| 1701 | * If "stay awake" state is not changing, we do nothing, to avoid resetting |
| 1702 | * the already-set timer. |
| 1703 | */ |
| 1704 | int pluggedType = intent.getIntExtra("plugged", 0); |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 1705 | Slog.d(TAG, "ACTION_BATTERY_CHANGED pluggedType: " + pluggedType); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1706 | if (mScreenOff && shouldWifiStayAwake(stayAwakeConditions, mPluggedType) && |
| 1707 | !shouldWifiStayAwake(stayAwakeConditions, pluggedType)) { |
| 1708 | long triggerTime = System.currentTimeMillis() + idleMillis; |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 1709 | Slog.d(TAG, "setting ACTION_DEVICE_IDLE timer for " + idleMillis + "ms"); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1710 | mAlarmManager.set(AlarmManager.RTC_WAKEUP, triggerTime, mIdleIntent); |
| 1711 | mPluggedType = pluggedType; |
| 1712 | return; |
| 1713 | } |
| 1714 | mPluggedType = pluggedType; |
| Nick Pelly | 005b228 | 2009-09-10 10:21:56 -0700 | [diff] [blame] | 1715 | } else if (action.equals(BluetoothA2dp.ACTION_SINK_STATE_CHANGED)) { |
| Jaikumar Ganesh | 084c665 | 2009-12-07 10:58:18 -0800 | [diff] [blame] | 1716 | BluetoothA2dp a2dp = new BluetoothA2dp(mContext); |
| 1717 | Set<BluetoothDevice> sinks = a2dp.getConnectedSinks(); |
| 1718 | boolean isBluetoothPlaying = false; |
| 1719 | for (BluetoothDevice sink : sinks) { |
| 1720 | if (a2dp.getSinkState(sink) == BluetoothA2dp.STATE_PLAYING) { |
| 1721 | isBluetoothPlaying = true; |
| 1722 | } |
| 1723 | } |
| The Android Open Source Project | b2a3dd8 | 2009-03-09 11:52:12 -0700 | [diff] [blame] | 1724 | mWifiStateTracker.setBluetoothScanMode(isBluetoothPlaying); |
| Jaikumar Ganesh | 084c665 | 2009-12-07 10:58:18 -0800 | [diff] [blame] | 1725 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1726 | } else { |
| 1727 | return; |
| 1728 | } |
| 1729 | |
| 1730 | updateWifiState(); |
| 1731 | } |
| 1732 | |
| 1733 | /** |
| 1734 | * Determines whether the Wi-Fi chipset should stay awake or be put to |
| 1735 | * sleep. Looks at the setting for the sleep policy and the current |
| 1736 | * conditions. |
| Jaikumar Ganesh | 084c665 | 2009-12-07 10:58:18 -0800 | [diff] [blame] | 1737 | * |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1738 | * @see #shouldDeviceStayAwake(int, int) |
| 1739 | */ |
| 1740 | private boolean shouldWifiStayAwake(int stayAwakeConditions, int pluggedType) { |
| 1741 | int wifiSleepPolicy = Settings.System.getInt(mContext.getContentResolver(), |
| 1742 | Settings.System.WIFI_SLEEP_POLICY, Settings.System.WIFI_SLEEP_POLICY_DEFAULT); |
| 1743 | |
| 1744 | if (wifiSleepPolicy == Settings.System.WIFI_SLEEP_POLICY_NEVER) { |
| 1745 | // Never sleep |
| 1746 | return true; |
| 1747 | } else if ((wifiSleepPolicy == Settings.System.WIFI_SLEEP_POLICY_NEVER_WHILE_PLUGGED) && |
| 1748 | (pluggedType != 0)) { |
| 1749 | // Never sleep while plugged, and we're plugged |
| 1750 | return true; |
| 1751 | } else { |
| 1752 | // Default |
| 1753 | return shouldDeviceStayAwake(stayAwakeConditions, pluggedType); |
| 1754 | } |
| 1755 | } |
| Jaikumar Ganesh | 084c665 | 2009-12-07 10:58:18 -0800 | [diff] [blame] | 1756 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1757 | /** |
| 1758 | * Determine whether the bit value corresponding to {@code pluggedType} is set in |
| 1759 | * the bit string {@code stayAwakeConditions}. Because a {@code pluggedType} value |
| 1760 | * of {@code 0} isn't really a plugged type, but rather an indication that the |
| 1761 | * device isn't plugged in at all, there is no bit value corresponding to a |
| 1762 | * {@code pluggedType} value of {@code 0}. That is why we shift by |
| 1763 | * {@code pluggedType — 1} instead of by {@code pluggedType}. |
| 1764 | * @param stayAwakeConditions a bit string specifying which "plugged types" should |
| 1765 | * keep the device (and hence Wi-Fi) awake. |
| 1766 | * @param pluggedType the type of plug (USB, AC, or none) for which the check is |
| 1767 | * being made |
| 1768 | * @return {@code true} if {@code pluggedType} indicates that the device is |
| 1769 | * supposed to stay awake, {@code false} otherwise. |
| 1770 | */ |
| 1771 | private boolean shouldDeviceStayAwake(int stayAwakeConditions, int pluggedType) { |
| 1772 | return (stayAwakeConditions & pluggedType) != 0; |
| 1773 | } |
| 1774 | }; |
| 1775 | |
| Dianne Hackborn | 617f877 | 2009-03-31 15:04:46 -0700 | [diff] [blame] | 1776 | private void sendEnableMessage(boolean enable, boolean persist, int uid) { |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1777 | Message msg = Message.obtain(mWifiHandler, |
| 1778 | (enable ? MESSAGE_ENABLE_WIFI : MESSAGE_DISABLE_WIFI), |
| Dianne Hackborn | 617f877 | 2009-03-31 15:04:46 -0700 | [diff] [blame] | 1779 | (persist ? 1 : 0), uid); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1780 | msg.sendToTarget(); |
| 1781 | } |
| 1782 | |
| 1783 | private void sendStartMessage(boolean scanOnlyMode) { |
| 1784 | Message.obtain(mWifiHandler, MESSAGE_START_WIFI, scanOnlyMode ? 1 : 0, 0).sendToTarget(); |
| 1785 | } |
| 1786 | |
| Irfan Sheriff | 5321aef | 2010-02-12 12:35:59 -0800 | [diff] [blame] | 1787 | private void sendAccessPointMessage(boolean enable, WifiConfiguration wifiConfig, int uid) { |
| 1788 | Message.obtain(mWifiHandler, |
| 1789 | (enable ? MESSAGE_START_ACCESS_POINT : MESSAGE_STOP_ACCESS_POINT), |
| Irfan Sheriff | 9ab518ad | 2010-03-12 15:48:17 -0800 | [diff] [blame] | 1790 | uid, 0, wifiConfig).sendToTarget(); |
| Irfan Sheriff | 5321aef | 2010-02-12 12:35:59 -0800 | [diff] [blame] | 1791 | } |
| 1792 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1793 | private void updateWifiState() { |
| Robert Greenwalt | f75aa36fc | 2009-10-22 17:03:47 -0700 | [diff] [blame] | 1794 | // send a message so it's all serialized |
| 1795 | Message.obtain(mWifiHandler, MESSAGE_UPDATE_STATE, 0, 0).sendToTarget(); |
| 1796 | } |
| 1797 | |
| 1798 | private void doUpdateWifiState() { |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1799 | boolean wifiEnabled = getPersistedWifiEnabled(); |
| Mike Lockwood | bd5ddf0 | 2009-07-29 21:37:14 -0700 | [diff] [blame] | 1800 | boolean airplaneMode = isAirplaneModeOn() && !mAirplaneModeOverwridden; |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1801 | boolean lockHeld = mLocks.hasLocks(); |
| 1802 | int strongestLockMode; |
| 1803 | boolean wifiShouldBeEnabled = wifiEnabled && !airplaneMode; |
| 1804 | boolean wifiShouldBeStarted = !mDeviceIdle || lockHeld; |
| 1805 | if (mDeviceIdle && lockHeld) { |
| 1806 | strongestLockMode = mLocks.getStrongestLockMode(); |
| 1807 | } else { |
| 1808 | strongestLockMode = WifiManager.WIFI_MODE_FULL; |
| 1809 | } |
| 1810 | |
| 1811 | synchronized (mWifiHandler) { |
| Irfan Sheriff | 0f34406009 | 2010-03-10 10:05:51 -0800 | [diff] [blame] | 1812 | if ((mWifiStateTracker.getWifiState() == WIFI_STATE_ENABLING) && !airplaneMode) { |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1813 | return; |
| 1814 | } |
| Irfan Sheriff | b2e6c01 | 2010-04-05 11:57:56 -0700 | [diff] [blame] | 1815 | |
| 1816 | /* Disable tethering when airplane mode is enabled */ |
| 1817 | if (airplaneMode && |
| 1818 | (mWifiApState == WIFI_AP_STATE_ENABLING || mWifiApState == WIFI_AP_STATE_ENABLED)) { |
| 1819 | sWakeLock.acquire(); |
| 1820 | sendAccessPointMessage(false, null, mLastApEnableUid); |
| 1821 | } |
| 1822 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1823 | if (wifiShouldBeEnabled) { |
| 1824 | if (wifiShouldBeStarted) { |
| 1825 | sWakeLock.acquire(); |
| Dianne Hackborn | 617f877 | 2009-03-31 15:04:46 -0700 | [diff] [blame] | 1826 | sendEnableMessage(true, false, mLastEnableUid); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1827 | sWakeLock.acquire(); |
| 1828 | sendStartMessage(strongestLockMode == WifiManager.WIFI_MODE_SCAN_ONLY); |
| Irfan Sheriff | 3bf504d | 2010-03-23 12:24:33 -0700 | [diff] [blame] | 1829 | } else if (!mWifiStateTracker.isDriverStopped()) { |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1830 | int wakeLockTimeout = |
| 1831 | Settings.Secure.getInt( |
| 1832 | mContext.getContentResolver(), |
| 1833 | Settings.Secure.WIFI_MOBILE_DATA_TRANSITION_WAKELOCK_TIMEOUT_MS, |
| 1834 | DEFAULT_WAKELOCK_TIMEOUT); |
| 1835 | /* |
| Irfan Sheriff | 3bf504d | 2010-03-23 12:24:33 -0700 | [diff] [blame] | 1836 | * We are assuming that ConnectivityService can make |
| 1837 | * a transition to cellular data within wakeLockTimeout time. |
| 1838 | * The wakelock is released by the delayed message. |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1839 | */ |
| 1840 | sDriverStopWakeLock.acquire(); |
| 1841 | mWifiHandler.sendEmptyMessage(MESSAGE_STOP_WIFI); |
| 1842 | mWifiHandler.sendEmptyMessageDelayed(MESSAGE_RELEASE_WAKELOCK, wakeLockTimeout); |
| 1843 | } |
| 1844 | } else { |
| 1845 | sWakeLock.acquire(); |
| Dianne Hackborn | 617f877 | 2009-03-31 15:04:46 -0700 | [diff] [blame] | 1846 | sendEnableMessage(false, false, mLastEnableUid); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1847 | } |
| 1848 | } |
| 1849 | } |
| 1850 | |
| 1851 | private void registerForBroadcasts() { |
| 1852 | IntentFilter intentFilter = new IntentFilter(); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1853 | intentFilter.addAction(Intent.ACTION_SCREEN_ON); |
| 1854 | intentFilter.addAction(Intent.ACTION_SCREEN_OFF); |
| 1855 | intentFilter.addAction(Intent.ACTION_BATTERY_CHANGED); |
| 1856 | intentFilter.addAction(ACTION_DEVICE_IDLE); |
| Nick Pelly | 005b228 | 2009-09-10 10:21:56 -0700 | [diff] [blame] | 1857 | intentFilter.addAction(BluetoothA2dp.ACTION_SINK_STATE_CHANGED); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1858 | mContext.registerReceiver(mReceiver, intentFilter); |
| 1859 | } |
| Jaikumar Ganesh | 084c665 | 2009-12-07 10:58:18 -0800 | [diff] [blame] | 1860 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1861 | private boolean isAirplaneSensitive() { |
| 1862 | String airplaneModeRadios = Settings.System.getString(mContext.getContentResolver(), |
| 1863 | Settings.System.AIRPLANE_MODE_RADIOS); |
| 1864 | return airplaneModeRadios == null |
| 1865 | || airplaneModeRadios.contains(Settings.System.RADIO_WIFI); |
| 1866 | } |
| 1867 | |
| Mike Lockwood | bd5ddf0 | 2009-07-29 21:37:14 -0700 | [diff] [blame] | 1868 | private boolean isAirplaneToggleable() { |
| 1869 | String toggleableRadios = Settings.System.getString(mContext.getContentResolver(), |
| 1870 | Settings.System.AIRPLANE_MODE_TOGGLEABLE_RADIOS); |
| 1871 | return toggleableRadios != null |
| 1872 | && toggleableRadios.contains(Settings.System.RADIO_WIFI); |
| 1873 | } |
| 1874 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1875 | /** |
| 1876 | * Returns true if Wi-Fi is sensitive to airplane mode, and airplane mode is |
| 1877 | * currently on. |
| 1878 | * @return {@code true} if airplane mode is on. |
| 1879 | */ |
| 1880 | private boolean isAirplaneModeOn() { |
| 1881 | return isAirplaneSensitive() && Settings.System.getInt(mContext.getContentResolver(), |
| 1882 | Settings.System.AIRPLANE_MODE_ON, 0) == 1; |
| 1883 | } |
| 1884 | |
| 1885 | /** |
| 1886 | * Handler that allows posting to the WifiThread. |
| 1887 | */ |
| 1888 | private class WifiHandler extends Handler { |
| 1889 | public WifiHandler(Looper looper) { |
| 1890 | super(looper); |
| 1891 | } |
| 1892 | |
| 1893 | @Override |
| 1894 | public void handleMessage(Message msg) { |
| 1895 | switch (msg.what) { |
| 1896 | |
| 1897 | case MESSAGE_ENABLE_WIFI: |
| Irfan Sheriff | b99fe5e | 2010-03-26 14:56:07 -0700 | [diff] [blame] | 1898 | setWifiEnabledBlocking(true, msg.arg1 == 1, msg.arg2); |
| Irfan Sheriff | 7b00978 | 2010-03-11 16:37:45 -0800 | [diff] [blame] | 1899 | if (mWifiWatchdogService == null) { |
| 1900 | mWifiWatchdogService = new WifiWatchdogService(mContext, mWifiStateTracker); |
| 1901 | } |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1902 | sWakeLock.release(); |
| 1903 | break; |
| 1904 | |
| 1905 | case MESSAGE_START_WIFI: |
| 1906 | mWifiStateTracker.setScanOnlyMode(msg.arg1 != 0); |
| 1907 | mWifiStateTracker.restart(); |
| 1908 | sWakeLock.release(); |
| 1909 | break; |
| 1910 | |
| Robert Greenwalt | f75aa36fc | 2009-10-22 17:03:47 -0700 | [diff] [blame] | 1911 | case MESSAGE_UPDATE_STATE: |
| 1912 | doUpdateWifiState(); |
| 1913 | break; |
| 1914 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1915 | case MESSAGE_DISABLE_WIFI: |
| 1916 | // a non-zero msg.arg1 value means the "enabled" setting |
| 1917 | // should be persisted |
| Dianne Hackborn | 617f877 | 2009-03-31 15:04:46 -0700 | [diff] [blame] | 1918 | setWifiEnabledBlocking(false, msg.arg1 == 1, msg.arg2); |
| Irfan Sheriff | b99fe5e | 2010-03-26 14:56:07 -0700 | [diff] [blame] | 1919 | mWifiWatchdogService = null; |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1920 | sWakeLock.release(); |
| 1921 | break; |
| 1922 | |
| 1923 | case MESSAGE_STOP_WIFI: |
| 1924 | mWifiStateTracker.disconnectAndStop(); |
| 1925 | // don't release wakelock |
| 1926 | break; |
| 1927 | |
| 1928 | case MESSAGE_RELEASE_WAKELOCK: |
| Irfan Sheriff | 3bf504d | 2010-03-23 12:24:33 -0700 | [diff] [blame] | 1929 | sDriverStopWakeLock.release(); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1930 | break; |
| Irfan Sheriff | 5321aef | 2010-02-12 12:35:59 -0800 | [diff] [blame] | 1931 | |
| 1932 | case MESSAGE_START_ACCESS_POINT: |
| 1933 | setWifiApEnabledBlocking(true, |
| Irfan Sheriff | 9ab518ad | 2010-03-12 15:48:17 -0800 | [diff] [blame] | 1934 | msg.arg1, |
| Irfan Sheriff | 5321aef | 2010-02-12 12:35:59 -0800 | [diff] [blame] | 1935 | (WifiConfiguration) msg.obj); |
| Irfan Sheriff | 80cb598 | 2010-03-19 10:40:18 -0700 | [diff] [blame] | 1936 | sWakeLock.release(); |
| Irfan Sheriff | 5321aef | 2010-02-12 12:35:59 -0800 | [diff] [blame] | 1937 | break; |
| 1938 | |
| 1939 | case MESSAGE_STOP_ACCESS_POINT: |
| 1940 | setWifiApEnabledBlocking(false, |
| Irfan Sheriff | 9ab518ad | 2010-03-12 15:48:17 -0800 | [diff] [blame] | 1941 | msg.arg1, |
| Irfan Sheriff | 5321aef | 2010-02-12 12:35:59 -0800 | [diff] [blame] | 1942 | (WifiConfiguration) msg.obj); |
| 1943 | sWakeLock.release(); |
| 1944 | break; |
| Irfan Sheriff | 59610c0 | 2010-03-30 11:00:41 -0700 | [diff] [blame] | 1945 | |
| 1946 | case MESSAGE_SET_CHANNELS: |
| 1947 | setNumAllowedChannelsBlocking(msg.arg1, msg.arg2 == 1); |
| 1948 | break; |
| 1949 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1950 | } |
| 1951 | } |
| 1952 | } |
| 1953 | |
| 1954 | @Override |
| 1955 | protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) { |
| 1956 | if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.DUMP) |
| 1957 | != PackageManager.PERMISSION_GRANTED) { |
| 1958 | pw.println("Permission Denial: can't dump WifiService from from pid=" |
| 1959 | + Binder.getCallingPid() |
| 1960 | + ", uid=" + Binder.getCallingUid()); |
| 1961 | return; |
| 1962 | } |
| Irfan Sheriff | 0f34406009 | 2010-03-10 10:05:51 -0800 | [diff] [blame] | 1963 | pw.println("Wi-Fi is " + stateName(mWifiStateTracker.getWifiState())); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1964 | pw.println("Stay-awake conditions: " + |
| 1965 | Settings.System.getInt(mContext.getContentResolver(), |
| 1966 | Settings.System.STAY_ON_WHILE_PLUGGED_IN, 0)); |
| 1967 | pw.println(); |
| 1968 | |
| 1969 | pw.println("Internal state:"); |
| 1970 | pw.println(mWifiStateTracker); |
| 1971 | pw.println(); |
| 1972 | pw.println("Latest scan results:"); |
| 1973 | List<ScanResult> scanResults = mWifiStateTracker.getScanResultsList(); |
| 1974 | if (scanResults != null && scanResults.size() != 0) { |
| 1975 | pw.println(" BSSID Frequency RSSI Flags SSID"); |
| 1976 | for (ScanResult r : scanResults) { |
| 1977 | pw.printf(" %17s %9d %5d %-16s %s%n", |
| 1978 | r.BSSID, |
| 1979 | r.frequency, |
| 1980 | r.level, |
| 1981 | r.capabilities, |
| 1982 | r.SSID == null ? "" : r.SSID); |
| 1983 | } |
| 1984 | } |
| 1985 | pw.println(); |
| Eric Shienbrood | 5711fad | 2009-03-27 20:25:31 -0700 | [diff] [blame] | 1986 | pw.println("Locks acquired: " + mFullLocksAcquired + " full, " + |
| 1987 | mScanLocksAcquired + " scan"); |
| 1988 | pw.println("Locks released: " + mFullLocksReleased + " full, " + |
| 1989 | mScanLocksReleased + " scan"); |
| 1990 | pw.println(); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1991 | pw.println("Locks held:"); |
| 1992 | mLocks.dump(pw); |
| 1993 | } |
| 1994 | |
| 1995 | private static String stateName(int wifiState) { |
| 1996 | switch (wifiState) { |
| 1997 | case WIFI_STATE_DISABLING: |
| 1998 | return "disabling"; |
| 1999 | case WIFI_STATE_DISABLED: |
| 2000 | return "disabled"; |
| 2001 | case WIFI_STATE_ENABLING: |
| 2002 | return "enabling"; |
| 2003 | case WIFI_STATE_ENABLED: |
| 2004 | return "enabled"; |
| 2005 | case WIFI_STATE_UNKNOWN: |
| 2006 | return "unknown state"; |
| 2007 | default: |
| 2008 | return "[invalid state]"; |
| 2009 | } |
| 2010 | } |
| 2011 | |
| Robert Greenwalt | 58ff021 | 2009-05-19 15:53:54 -0700 | [diff] [blame] | 2012 | private class WifiLock extends DeathRecipient { |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2013 | WifiLock(int lockMode, String tag, IBinder binder) { |
| Robert Greenwalt | 5347bd4 | 2009-05-13 15:10:16 -0700 | [diff] [blame] | 2014 | super(lockMode, tag, binder); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2015 | } |
| 2016 | |
| 2017 | public void binderDied() { |
| 2018 | synchronized (mLocks) { |
| 2019 | releaseWifiLockLocked(mBinder); |
| 2020 | } |
| 2021 | } |
| 2022 | |
| 2023 | public String toString() { |
| Robert Greenwalt | 5347bd4 | 2009-05-13 15:10:16 -0700 | [diff] [blame] | 2024 | return "WifiLock{" + mTag + " type=" + mMode + " binder=" + mBinder + "}"; |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2025 | } |
| 2026 | } |
| 2027 | |
| 2028 | private class LockList { |
| 2029 | private List<WifiLock> mList; |
| 2030 | |
| 2031 | private LockList() { |
| 2032 | mList = new ArrayList<WifiLock>(); |
| 2033 | } |
| 2034 | |
| 2035 | private synchronized boolean hasLocks() { |
| 2036 | return !mList.isEmpty(); |
| 2037 | } |
| 2038 | |
| 2039 | private synchronized int getStrongestLockMode() { |
| 2040 | if (mList.isEmpty()) { |
| 2041 | return WifiManager.WIFI_MODE_FULL; |
| 2042 | } |
| 2043 | for (WifiLock l : mList) { |
| Robert Greenwalt | 5347bd4 | 2009-05-13 15:10:16 -0700 | [diff] [blame] | 2044 | if (l.mMode == WifiManager.WIFI_MODE_FULL) { |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2045 | return WifiManager.WIFI_MODE_FULL; |
| 2046 | } |
| 2047 | } |
| 2048 | return WifiManager.WIFI_MODE_SCAN_ONLY; |
| 2049 | } |
| 2050 | |
| 2051 | private void addLock(WifiLock lock) { |
| 2052 | if (findLockByBinder(lock.mBinder) < 0) { |
| 2053 | mList.add(lock); |
| 2054 | } |
| 2055 | } |
| 2056 | |
| 2057 | private WifiLock removeLock(IBinder binder) { |
| 2058 | int index = findLockByBinder(binder); |
| 2059 | if (index >= 0) { |
| Suchi Amalapurapu | fff2fda | 2009-06-30 21:36:16 -0700 | [diff] [blame] | 2060 | WifiLock ret = mList.remove(index); |
| 2061 | ret.unlinkDeathRecipient(); |
| 2062 | return ret; |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2063 | } else { |
| 2064 | return null; |
| 2065 | } |
| 2066 | } |
| 2067 | |
| 2068 | private int findLockByBinder(IBinder binder) { |
| 2069 | int size = mList.size(); |
| 2070 | for (int i = size - 1; i >= 0; i--) |
| 2071 | if (mList.get(i).mBinder == binder) |
| 2072 | return i; |
| 2073 | return -1; |
| 2074 | } |
| 2075 | |
| 2076 | private void dump(PrintWriter pw) { |
| 2077 | for (WifiLock l : mList) { |
| 2078 | pw.print(" "); |
| 2079 | pw.println(l); |
| 2080 | } |
| 2081 | } |
| 2082 | } |
| 2083 | |
| 2084 | public boolean acquireWifiLock(IBinder binder, int lockMode, String tag) { |
| 2085 | mContext.enforceCallingOrSelfPermission(android.Manifest.permission.WAKE_LOCK, null); |
| 2086 | if (lockMode != WifiManager.WIFI_MODE_FULL && lockMode != WifiManager.WIFI_MODE_SCAN_ONLY) { |
| 2087 | return false; |
| 2088 | } |
| 2089 | WifiLock wifiLock = new WifiLock(lockMode, tag, binder); |
| 2090 | synchronized (mLocks) { |
| 2091 | return acquireWifiLockLocked(wifiLock); |
| 2092 | } |
| 2093 | } |
| 2094 | |
| 2095 | private boolean acquireWifiLockLocked(WifiLock wifiLock) { |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 2096 | Slog.d(TAG, "acquireWifiLockLocked: " + wifiLock); |
| Robert Greenwalt | f1acb2d | 2009-10-13 08:20:55 -0700 | [diff] [blame] | 2097 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2098 | mLocks.addLock(wifiLock); |
| Robert Greenwalt | f1acb2d | 2009-10-13 08:20:55 -0700 | [diff] [blame] | 2099 | |
| The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 2100 | int uid = Binder.getCallingUid(); |
| 2101 | long ident = Binder.clearCallingIdentity(); |
| 2102 | try { |
| Robert Greenwalt | 5347bd4 | 2009-05-13 15:10:16 -0700 | [diff] [blame] | 2103 | switch(wifiLock.mMode) { |
| Eric Shienbrood | 5711fad | 2009-03-27 20:25:31 -0700 | [diff] [blame] | 2104 | case WifiManager.WIFI_MODE_FULL: |
| 2105 | ++mFullLocksAcquired; |
| 2106 | mBatteryStats.noteFullWifiLockAcquired(uid); |
| 2107 | break; |
| 2108 | case WifiManager.WIFI_MODE_SCAN_ONLY: |
| 2109 | ++mScanLocksAcquired; |
| 2110 | mBatteryStats.noteScanWifiLockAcquired(uid); |
| 2111 | break; |
| The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 2112 | } |
| 2113 | } catch (RemoteException e) { |
| 2114 | } finally { |
| 2115 | Binder.restoreCallingIdentity(ident); |
| 2116 | } |
| Robert Greenwalt | f1acb2d | 2009-10-13 08:20:55 -0700 | [diff] [blame] | 2117 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2118 | updateWifiState(); |
| 2119 | return true; |
| 2120 | } |
| 2121 | |
| 2122 | public boolean releaseWifiLock(IBinder lock) { |
| 2123 | mContext.enforceCallingOrSelfPermission(android.Manifest.permission.WAKE_LOCK, null); |
| 2124 | synchronized (mLocks) { |
| 2125 | return releaseWifiLockLocked(lock); |
| 2126 | } |
| 2127 | } |
| 2128 | |
| 2129 | private boolean releaseWifiLockLocked(IBinder lock) { |
| Eric Shienbrood | d4c5f89 | 2009-03-24 18:13:20 -0700 | [diff] [blame] | 2130 | boolean hadLock; |
| Robert Greenwalt | f1acb2d | 2009-10-13 08:20:55 -0700 | [diff] [blame] | 2131 | |
| The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 2132 | WifiLock wifiLock = mLocks.removeLock(lock); |
| Robert Greenwalt | f1acb2d | 2009-10-13 08:20:55 -0700 | [diff] [blame] | 2133 | |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 2134 | Slog.d(TAG, "releaseWifiLockLocked: " + wifiLock); |
| Robert Greenwalt | f1acb2d | 2009-10-13 08:20:55 -0700 | [diff] [blame] | 2135 | |
| Eric Shienbrood | d4c5f89 | 2009-03-24 18:13:20 -0700 | [diff] [blame] | 2136 | hadLock = (wifiLock != null); |
| 2137 | |
| 2138 | if (hadLock) { |
| 2139 | int uid = Binder.getCallingUid(); |
| 2140 | long ident = Binder.clearCallingIdentity(); |
| 2141 | try { |
| Robert Greenwalt | 5347bd4 | 2009-05-13 15:10:16 -0700 | [diff] [blame] | 2142 | switch(wifiLock.mMode) { |
| Eric Shienbrood | 5711fad | 2009-03-27 20:25:31 -0700 | [diff] [blame] | 2143 | case WifiManager.WIFI_MODE_FULL: |
| 2144 | ++mFullLocksReleased; |
| 2145 | mBatteryStats.noteFullWifiLockReleased(uid); |
| 2146 | break; |
| 2147 | case WifiManager.WIFI_MODE_SCAN_ONLY: |
| 2148 | ++mScanLocksReleased; |
| 2149 | mBatteryStats.noteScanWifiLockReleased(uid); |
| 2150 | break; |
| Eric Shienbrood | d4c5f89 | 2009-03-24 18:13:20 -0700 | [diff] [blame] | 2151 | } |
| 2152 | } catch (RemoteException e) { |
| 2153 | } finally { |
| 2154 | Binder.restoreCallingIdentity(ident); |
| The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 2155 | } |
| The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 2156 | } |
| Robert Greenwalt | f1acb2d | 2009-10-13 08:20:55 -0700 | [diff] [blame] | 2157 | // TODO - should this only happen if you hadLock? |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2158 | updateWifiState(); |
| Eric Shienbrood | d4c5f89 | 2009-03-24 18:13:20 -0700 | [diff] [blame] | 2159 | return hadLock; |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2160 | } |
| Robert Greenwalt | 5347bd4 | 2009-05-13 15:10:16 -0700 | [diff] [blame] | 2161 | |
| Robert Greenwalt | 58ff021 | 2009-05-19 15:53:54 -0700 | [diff] [blame] | 2162 | private abstract class DeathRecipient |
| Robert Greenwalt | 5347bd4 | 2009-05-13 15:10:16 -0700 | [diff] [blame] | 2163 | implements IBinder.DeathRecipient { |
| 2164 | String mTag; |
| 2165 | int mMode; |
| 2166 | IBinder mBinder; |
| 2167 | |
| Robert Greenwalt | 58ff021 | 2009-05-19 15:53:54 -0700 | [diff] [blame] | 2168 | DeathRecipient(int mode, String tag, IBinder binder) { |
| Robert Greenwalt | 5347bd4 | 2009-05-13 15:10:16 -0700 | [diff] [blame] | 2169 | super(); |
| 2170 | mTag = tag; |
| 2171 | mMode = mode; |
| 2172 | mBinder = binder; |
| 2173 | try { |
| 2174 | mBinder.linkToDeath(this, 0); |
| 2175 | } catch (RemoteException e) { |
| 2176 | binderDied(); |
| 2177 | } |
| 2178 | } |
| Suchi Amalapurapu | fff2fda | 2009-06-30 21:36:16 -0700 | [diff] [blame] | 2179 | |
| 2180 | void unlinkDeathRecipient() { |
| 2181 | mBinder.unlinkToDeath(this, 0); |
| 2182 | } |
| Robert Greenwalt | 5347bd4 | 2009-05-13 15:10:16 -0700 | [diff] [blame] | 2183 | } |
| 2184 | |
| Robert Greenwalt | 58ff021 | 2009-05-19 15:53:54 -0700 | [diff] [blame] | 2185 | private class Multicaster extends DeathRecipient { |
| 2186 | Multicaster(String tag, IBinder binder) { |
| Robert Greenwalt | 5347bd4 | 2009-05-13 15:10:16 -0700 | [diff] [blame] | 2187 | super(Binder.getCallingUid(), tag, binder); |
| 2188 | } |
| 2189 | |
| 2190 | public void binderDied() { |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 2191 | Slog.e(TAG, "Multicaster binderDied"); |
| Robert Greenwalt | 5347bd4 | 2009-05-13 15:10:16 -0700 | [diff] [blame] | 2192 | synchronized (mMulticasters) { |
| 2193 | int i = mMulticasters.indexOf(this); |
| 2194 | if (i != -1) { |
| 2195 | removeMulticasterLocked(i, mMode); |
| 2196 | } |
| 2197 | } |
| 2198 | } |
| 2199 | |
| 2200 | public String toString() { |
| Robert Greenwalt | 58ff021 | 2009-05-19 15:53:54 -0700 | [diff] [blame] | 2201 | return "Multicaster{" + mTag + " binder=" + mBinder + "}"; |
| Robert Greenwalt | 5347bd4 | 2009-05-13 15:10:16 -0700 | [diff] [blame] | 2202 | } |
| 2203 | |
| 2204 | public int getUid() { |
| 2205 | return mMode; |
| 2206 | } |
| 2207 | } |
| 2208 | |
| Robert Greenwalt | e2d155a | 2009-10-21 14:58:34 -0700 | [diff] [blame] | 2209 | public void initializeMulticastFiltering() { |
| 2210 | enforceMulticastChangePermission(); |
| Irfan Sheriff | a8fbe1f | 2010-03-09 09:13:58 -0800 | [diff] [blame] | 2211 | |
| Robert Greenwalt | e2d155a | 2009-10-21 14:58:34 -0700 | [diff] [blame] | 2212 | synchronized (mMulticasters) { |
| 2213 | // if anybody had requested filters be off, leave off |
| 2214 | if (mMulticasters.size() != 0) { |
| 2215 | return; |
| 2216 | } else { |
| Irfan Sheriff | a8fbe1f | 2010-03-09 09:13:58 -0800 | [diff] [blame] | 2217 | mWifiStateTracker.startPacketFiltering(); |
| Robert Greenwalt | e2d155a | 2009-10-21 14:58:34 -0700 | [diff] [blame] | 2218 | } |
| 2219 | } |
| 2220 | } |
| 2221 | |
| Robert Greenwalt | fc1b15c | 2009-05-22 15:09:51 -0700 | [diff] [blame] | 2222 | public void acquireMulticastLock(IBinder binder, String tag) { |
| 2223 | enforceMulticastChangePermission(); |
| Robert Greenwalt | 5347bd4 | 2009-05-13 15:10:16 -0700 | [diff] [blame] | 2224 | |
| 2225 | synchronized (mMulticasters) { |
| 2226 | mMulticastEnabled++; |
| Robert Greenwalt | 58ff021 | 2009-05-19 15:53:54 -0700 | [diff] [blame] | 2227 | mMulticasters.add(new Multicaster(tag, binder)); |
| Robert Greenwalt | 5347bd4 | 2009-05-13 15:10:16 -0700 | [diff] [blame] | 2228 | // Note that we could call stopPacketFiltering only when |
| 2229 | // our new size == 1 (first call), but this function won't |
| 2230 | // be called often and by making the stopPacket call each |
| 2231 | // time we're less fragile and self-healing. |
| Irfan Sheriff | a8fbe1f | 2010-03-09 09:13:58 -0800 | [diff] [blame] | 2232 | mWifiStateTracker.stopPacketFiltering(); |
| Robert Greenwalt | 5347bd4 | 2009-05-13 15:10:16 -0700 | [diff] [blame] | 2233 | } |
| 2234 | |
| 2235 | int uid = Binder.getCallingUid(); |
| 2236 | Long ident = Binder.clearCallingIdentity(); |
| 2237 | try { |
| 2238 | mBatteryStats.noteWifiMulticastEnabled(uid); |
| 2239 | } catch (RemoteException e) { |
| 2240 | } finally { |
| 2241 | Binder.restoreCallingIdentity(ident); |
| 2242 | } |
| 2243 | } |
| 2244 | |
| Robert Greenwalt | fc1b15c | 2009-05-22 15:09:51 -0700 | [diff] [blame] | 2245 | public void releaseMulticastLock() { |
| 2246 | enforceMulticastChangePermission(); |
| Robert Greenwalt | 5347bd4 | 2009-05-13 15:10:16 -0700 | [diff] [blame] | 2247 | |
| 2248 | int uid = Binder.getCallingUid(); |
| 2249 | synchronized (mMulticasters) { |
| 2250 | mMulticastDisabled++; |
| 2251 | int size = mMulticasters.size(); |
| 2252 | for (int i = size - 1; i >= 0; i--) { |
| Robert Greenwalt | 58ff021 | 2009-05-19 15:53:54 -0700 | [diff] [blame] | 2253 | Multicaster m = mMulticasters.get(i); |
| Robert Greenwalt | 5347bd4 | 2009-05-13 15:10:16 -0700 | [diff] [blame] | 2254 | if ((m != null) && (m.getUid() == uid)) { |
| 2255 | removeMulticasterLocked(i, uid); |
| 2256 | } |
| 2257 | } |
| 2258 | } |
| 2259 | } |
| 2260 | |
| 2261 | private void removeMulticasterLocked(int i, int uid) |
| 2262 | { |
| Suchi Amalapurapu | fff2fda | 2009-06-30 21:36:16 -0700 | [diff] [blame] | 2263 | Multicaster removed = mMulticasters.remove(i); |
| Irfan Sheriff | a8fbe1f | 2010-03-09 09:13:58 -0800 | [diff] [blame] | 2264 | |
| Suchi Amalapurapu | fff2fda | 2009-06-30 21:36:16 -0700 | [diff] [blame] | 2265 | if (removed != null) { |
| 2266 | removed.unlinkDeathRecipient(); |
| 2267 | } |
| Robert Greenwalt | 5347bd4 | 2009-05-13 15:10:16 -0700 | [diff] [blame] | 2268 | if (mMulticasters.size() == 0) { |
| Irfan Sheriff | a8fbe1f | 2010-03-09 09:13:58 -0800 | [diff] [blame] | 2269 | mWifiStateTracker.startPacketFiltering(); |
| Robert Greenwalt | 5347bd4 | 2009-05-13 15:10:16 -0700 | [diff] [blame] | 2270 | } |
| 2271 | |
| 2272 | Long ident = Binder.clearCallingIdentity(); |
| 2273 | try { |
| 2274 | mBatteryStats.noteWifiMulticastDisabled(uid); |
| 2275 | } catch (RemoteException e) { |
| 2276 | } finally { |
| 2277 | Binder.restoreCallingIdentity(ident); |
| 2278 | } |
| 2279 | } |
| 2280 | |
| Robert Greenwalt | 58ff021 | 2009-05-19 15:53:54 -0700 | [diff] [blame] | 2281 | public boolean isMulticastEnabled() { |
| Robert Greenwalt | 5347bd4 | 2009-05-13 15:10:16 -0700 | [diff] [blame] | 2282 | enforceAccessPermission(); |
| 2283 | |
| 2284 | synchronized (mMulticasters) { |
| 2285 | return (mMulticasters.size() > 0); |
| 2286 | } |
| 2287 | } |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2288 | } |