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