blob: 35b250e6ba44b9dd5c2579bc66947d9b1c6a7aa0 [file] [log] [blame]
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001/*
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
17package com.android.server;
18
19import static android.net.wifi.WifiManager.WIFI_STATE_DISABLED;
20import static android.net.wifi.WifiManager.WIFI_STATE_DISABLING;
21import static android.net.wifi.WifiManager.WIFI_STATE_ENABLED;
22import static android.net.wifi.WifiManager.WIFI_STATE_ENABLING;
23import static android.net.wifi.WifiManager.WIFI_STATE_UNKNOWN;
24
Irfan Sheriff5321aef2010-02-12 12:35:59 -080025import static android.net.wifi.WifiManager.WIFI_AP_STATE_DISABLED;
26import static android.net.wifi.WifiManager.WIFI_AP_STATE_DISABLING;
27import static android.net.wifi.WifiManager.WIFI_AP_STATE_ENABLED;
28import static android.net.wifi.WifiManager.WIFI_AP_STATE_ENABLING;
29import static android.net.wifi.WifiManager.WIFI_AP_STATE_FAILED;
30
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080031import android.app.AlarmManager;
32import android.app.PendingIntent;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070033import android.bluetooth.BluetoothA2dp;
Jaikumar Ganesh084c6652009-12-07 10:58:18 -080034import android.bluetooth.BluetoothDevice;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080035import android.content.BroadcastReceiver;
36import android.content.ContentResolver;
37import android.content.Context;
38import android.content.Intent;
39import android.content.IntentFilter;
40import android.content.pm.PackageManager;
41import android.net.wifi.IWifiManager;
42import android.net.wifi.WifiInfo;
43import android.net.wifi.WifiManager;
44import android.net.wifi.WifiNative;
45import android.net.wifi.WifiStateTracker;
46import android.net.wifi.ScanResult;
47import android.net.wifi.WifiConfiguration;
San Mehat0310f9a2009-07-07 10:49:47 -070048import android.net.wifi.SupplicantState;
Irfan Sheriff9ab518ad2010-03-12 15:48:17 -080049import android.net.wifi.WifiConfiguration.KeyMgmt;
Irfan Sheriff5321aef2010-02-12 12:35:59 -080050import android.net.ConnectivityManager;
51import android.net.InterfaceConfiguration;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080052import android.net.NetworkStateTracker;
53import android.net.DhcpInfo;
Mike Lockwood0900f362009-07-10 17:24:07 -040054import android.net.NetworkUtils;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080055import android.os.Binder;
56import android.os.Handler;
57import android.os.HandlerThread;
58import android.os.IBinder;
Irfan Sheriff5321aef2010-02-12 12:35:59 -080059import android.os.INetworkManagementService;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080060import android.os.Looper;
61import android.os.Message;
62import android.os.PowerManager;
Dianne Hackborn617f8772009-03-31 15:04:46 -070063import android.os.Process;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080064import android.os.RemoteException;
Amith Yamasani47873e52009-07-02 12:05:32 -070065import android.os.ServiceManager;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080066import android.provider.Settings;
Joe Onorato8a9b2202010-02-26 18:56:32 -080067import android.util.Slog;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080068import android.text.TextUtils;
69
70import java.util.ArrayList;
71import java.util.BitSet;
72import java.util.HashMap;
73import java.util.LinkedHashMap;
74import java.util.List;
75import java.util.Map;
Jaikumar Ganesh084c6652009-12-07 10:58:18 -080076import java.util.Set;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080077import java.util.regex.Pattern;
78import java.io.FileDescriptor;
79import java.io.PrintWriter;
Irfan Sheriff5321aef2010-02-12 12:35:59 -080080import java.net.UnknownHostException;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080081
The Android Open Source Project10592532009-03-18 17:39:46 -070082import com.android.internal.app.IBatteryStats;
Christopher Tate45281862010-03-05 15:46:30 -080083import android.app.backup.IBackupManager;
The Android Open Source Project10592532009-03-18 17:39:46 -070084import com.android.server.am.BatteryStatsService;
Irfan Sheriff9ab518ad2010-03-12 15:48:17 -080085import com.android.internal.R;
The Android Open Source Project10592532009-03-18 17:39:46 -070086
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080087/**
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 */
94public 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 Sheriffc2f54c22010-03-18 14:02:22 -070099 /* TODO: fetch a configurable interface */
100 private static final String SOFTAP_IFACE = "wl0.1";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800101
102 private Context mContext;
Irfan Sheriff5321aef2010-02-12 12:35:59 -0800103 private int mWifiApState;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800104
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 Sheriff9ab518ad2010-03-12 15:48:17 -0800112 private enum DriverAction {DRIVER_UNLOAD, NO_DRIVER_UNLOAD};
113
Mike Lockwoodbd5ddf02009-07-29 21:37:14 -0700114 // true if the user enabled Wifi while in airplane mode
115 private boolean mAirplaneModeOverwridden;
116
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800117 private final LockList mLocks = new LockList();
Eric Shienbrood5711fad2009-03-27 20:25:31 -0700118 // some wifi lock statistics
119 private int mFullLocksAcquired;
120 private int mFullLocksReleased;
121 private int mScanLocksAcquired;
122 private int mScanLocksReleased;
The Android Open Source Project10592532009-03-18 17:39:46 -0700123
Robert Greenwalt58ff0212009-05-19 15:53:54 -0700124 private final List<Multicaster> mMulticasters =
125 new ArrayList<Multicaster>();
Robert Greenwalt5347bd42009-05-13 15:10:16 -0700126 private int mMulticastEnabled;
127 private int mMulticastDisabled;
128
The Android Open Source Project10592532009-03-18 17:39:46 -0700129 private final IBatteryStats mBatteryStats;
Jaikumar Ganesh084c6652009-12-07 10:58:18 -0800130
Irfan Sheriff5321aef2010-02-12 12:35:59 -0800131 private INetworkManagementService nwService;
132 ConnectivityManager mCm;
Irfan Sheriff7b009782010-03-11 16:37:45 -0800133 private WifiWatchdogService mWifiWatchdogService = null;
Irfan Sheriff5321aef2010-02-12 12:35:59 -0800134 private String[] mWifiRegexs;
135
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800136 /**
Doug Zongker43866e02010-01-07 12:09:54 -0800137 * 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 Project9066cfe2009-03-03 19:31:44 -0800139 * 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
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;
Robert Greenwaltf75aa36fc2009-10-22 17:03:47 -0700168 private static final int MESSAGE_UPDATE_STATE = 5;
Irfan Sheriff5321aef2010-02-12 12:35:59 -0800169 private static final int MESSAGE_START_ACCESS_POINT = 6;
170 private static final int MESSAGE_STOP_ACCESS_POINT = 7;
171
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800172
173 private final WifiHandler mWifiHandler;
174
175 /*
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800176 * Cache of scan results objects (size is somewhat arbitrary)
177 */
178 private static final int SCAN_RESULT_CACHE_SIZE = 80;
179 private final LinkedHashMap<String, ScanResult> mScanResultCache;
180
181 /*
182 * Character buffer used to parse scan results (optimization)
183 */
184 private static final int SCAN_RESULT_BUFFER_SIZE = 512;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800185 private boolean mNeedReconfig;
186
Dianne Hackborn617f8772009-03-31 15:04:46 -0700187 /*
188 * Last UID that asked to enable WIFI.
189 */
190 private int mLastEnableUid = Process.myUid();
Jaikumar Ganesh084c6652009-12-07 10:58:18 -0800191
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800192 /**
193 * Number of allowed radio frequency channels in various regulatory domains.
194 * This list is sufficient for 802.11b/g networks (2.4GHz range).
195 */
196 private static int[] sValidRegulatoryChannelCounts = new int[] {11, 13, 14};
197
198 private static final String ACTION_DEVICE_IDLE =
199 "com.android.server.WifiManager.action.DEVICE_IDLE";
200
201 WifiService(Context context, WifiStateTracker tracker) {
202 mContext = context;
203 mWifiStateTracker = tracker;
Mike Lockwoodf32be162009-07-14 17:44:37 -0400204 mWifiStateTracker.enableRssiPolling(true);
The Android Open Source Project10592532009-03-18 17:39:46 -0700205 mBatteryStats = BatteryStatsService.getService();
Jaikumar Ganesh084c6652009-12-07 10:58:18 -0800206
Irfan Sheriff5321aef2010-02-12 12:35:59 -0800207 IBinder b = ServiceManager.getService(Context.NETWORKMANAGEMENT_SERVICE);
208 nwService = INetworkManagementService.Stub.asInterface(b);
209
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800210 mScanResultCache = new LinkedHashMap<String, ScanResult>(
211 SCAN_RESULT_CACHE_SIZE, 0.75f, true) {
212 /*
213 * Limit the cache size by SCAN_RESULT_CACHE_SIZE
214 * elements
215 */
216 public boolean removeEldestEntry(Map.Entry eldest) {
217 return SCAN_RESULT_CACHE_SIZE < this.size();
218 }
219 };
220
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800221 HandlerThread wifiThread = new HandlerThread("WifiService");
222 wifiThread.start();
223 mWifiHandler = new WifiHandler(wifiThread.getLooper());
224
Irfan Sheriff0f344060092010-03-10 10:05:51 -0800225 mWifiStateTracker.setWifiState(WIFI_STATE_DISABLED);
Irfan Sheriff5321aef2010-02-12 12:35:59 -0800226 mWifiApState = WIFI_AP_STATE_DISABLED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800227
228 mAlarmManager = (AlarmManager)mContext.getSystemService(Context.ALARM_SERVICE);
229 Intent idleIntent = new Intent(ACTION_DEVICE_IDLE, null);
230 mIdleIntent = PendingIntent.getBroadcast(mContext, IDLE_REQUEST, idleIntent, 0);
231
232 PowerManager powerManager = (PowerManager)mContext.getSystemService(Context.POWER_SERVICE);
233 sWakeLock = powerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, WAKELOCK_TAG);
234 sDriverStopWakeLock = powerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, WAKELOCK_TAG);
235 mWifiStateTracker.setReleaseWakeLockCallback(
236 new Runnable() {
237 public void run() {
238 mWifiHandler.removeMessages(MESSAGE_RELEASE_WAKELOCK);
239 synchronized (sDriverStopWakeLock) {
240 if (sDriverStopWakeLock.isHeld()) {
241 sDriverStopWakeLock.release();
242 }
243 }
244 }
245 }
246 );
247
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800248 mContext.registerReceiver(
249 new BroadcastReceiver() {
250 @Override
251 public void onReceive(Context context, Intent intent) {
Mike Lockwoodbd5ddf02009-07-29 21:37:14 -0700252 // clear our flag indicating the user has overwridden airplane mode
253 mAirplaneModeOverwridden = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800254 updateWifiState();
255 }
256 },
257 new IntentFilter(Intent.ACTION_AIRPLANE_MODE_CHANGED));
258
Irfan Sheriff5321aef2010-02-12 12:35:59 -0800259 mContext.registerReceiver(
260 new BroadcastReceiver() {
261 @Override
262 public void onReceive(Context context, Intent intent) {
263
264 ArrayList<String> available = intent.getStringArrayListExtra(
265 ConnectivityManager.EXTRA_AVAILABLE_TETHER);
266 ArrayList<String> active = intent.getStringArrayListExtra(
267 ConnectivityManager.EXTRA_ACTIVE_TETHER);
268 updateTetherState(available, active);
269
270 }
271 },new IntentFilter(ConnectivityManager.ACTION_TETHER_STATE_CHANGED));
Irfan Sheriff7b009782010-03-11 16:37:45 -0800272 }
Irfan Sheriff5321aef2010-02-12 12:35:59 -0800273
Irfan Sheriff7b009782010-03-11 16:37:45 -0800274 /**
275 * Check if Wi-Fi needs to be enabled and start
276 * if needed
277 */
278 public void startWifi() {
279 boolean wifiEnabled = getPersistedWifiEnabled();
Irfan Sheriff7b009782010-03-11 16:37:45 -0800280 Slog.i(TAG, "WifiService starting up with Wi-Fi " +
281 (wifiEnabled ? "enabled" : "disabled"));
Dianne Hackborn617f8772009-03-31 15:04:46 -0700282 setWifiEnabledBlocking(wifiEnabled, false, Process.myUid());
Irfan Sheriff5321aef2010-02-12 12:35:59 -0800283 }
284
285 private void updateTetherState(ArrayList<String> available, ArrayList<String> tethered) {
286
287 boolean wifiTethered = false;
288 boolean wifiAvailable = false;
289
290 IBinder b = ServiceManager.getService(Context.NETWORKMANAGEMENT_SERVICE);
291 INetworkManagementService service = INetworkManagementService.Stub.asInterface(b);
292
293 mCm = (ConnectivityManager)mContext.getSystemService(Context.CONNECTIVITY_SERVICE);
294 mWifiRegexs = mCm.getTetherableWifiRegexs();
295
296 for (String intf : available) {
297 for (String regex : mWifiRegexs) {
298 if (intf.matches(regex)) {
299
300 InterfaceConfiguration ifcg = null;
301 try {
302 ifcg = service.getInterfaceConfig(intf);
303 if (ifcg != null) {
Irfan Sheriff9ab518ad2010-03-12 15:48:17 -0800304 /* IP/netmask: 169.254.2.2/255.255.255.0 */
305 ifcg.ipAddr = (169 << 24) + (254 << 16) + (2 << 8) + 2;
Irfan Sheriff5321aef2010-02-12 12:35:59 -0800306 ifcg.netmask = (255 << 24) + (255 << 16) + (255 << 8) + 0;
307 ifcg.interfaceFlags = "up";
308
309 service.setInterfaceConfig(intf, ifcg);
310 }
311 } catch (Exception e) {
Irfan Sheriff5321aef2010-02-12 12:35:59 -0800312 Slog.e(TAG, "Error configuring interface " + intf + ", :" + e);
Irfan Sheriff1a543012010-03-17 19:46:32 -0700313 try {
314 nwService.stopAccessPoint();
315 } catch (Exception ee) {
316 Slog.e(TAG, "Could not stop AP, :" + ee);
317 }
Irfan Sheriff9ab518ad2010-03-12 15:48:17 -0800318 setWifiApEnabledState(WIFI_AP_STATE_FAILED, 0, DriverAction.DRIVER_UNLOAD);
Irfan Sheriff5321aef2010-02-12 12:35:59 -0800319 return;
320 }
321
Irfan Sheriff9ab518ad2010-03-12 15:48:17 -0800322 if(mCm.tether(intf) != ConnectivityManager.TETHER_ERROR_NO_ERROR) {
Irfan Sheriff5321aef2010-02-12 12:35:59 -0800323 Slog.e(TAG, "Error tethering "+intf);
324 }
325 break;
326 }
327 }
328 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800329 }
330
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800331 private boolean getPersistedWifiEnabled() {
332 final ContentResolver cr = mContext.getContentResolver();
333 try {
334 return Settings.Secure.getInt(cr, Settings.Secure.WIFI_ON) == 1;
335 } catch (Settings.SettingNotFoundException e) {
336 Settings.Secure.putInt(cr, Settings.Secure.WIFI_ON, 0);
337 return false;
338 }
339 }
340
341 private void persistWifiEnabled(boolean enabled) {
342 final ContentResolver cr = mContext.getContentResolver();
343 Settings.Secure.putInt(cr, Settings.Secure.WIFI_ON, enabled ? 1 : 0);
344 }
345
346 NetworkStateTracker getNetworkStateTracker() {
347 return mWifiStateTracker;
348 }
349
350 /**
351 * see {@link android.net.wifi.WifiManager#pingSupplicant()}
352 * @return {@code true} if the operation succeeds
353 */
354 public boolean pingSupplicant() {
355 enforceChangePermission();
Irfan Sheriffa8fbe1f2010-03-09 09:13:58 -0800356
357 return mWifiStateTracker.ping();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800358 }
359
360 /**
361 * see {@link android.net.wifi.WifiManager#startScan()}
362 * @return {@code true} if the operation succeeds
363 */
Mike Lockwooda5ec95c2009-07-08 17:11:17 -0400364 public boolean startScan(boolean forceActive) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800365 enforceChangePermission();
Irfan Sheriffa8fbe1f2010-03-09 09:13:58 -0800366
367 switch (mWifiStateTracker.getSupplicantState()) {
368 case DISCONNECTED:
369 case INACTIVE:
370 case SCANNING:
371 case DORMANT:
372 break;
373 default:
374 mWifiStateTracker.setScanResultHandling(
375 WifiStateTracker.SUPPL_SCAN_HANDLING_LIST_ONLY);
376 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800377 }
Irfan Sheriffa8fbe1f2010-03-09 09:13:58 -0800378 return mWifiStateTracker.scan(forceActive);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800379 }
380
381 /**
382 * see {@link android.net.wifi.WifiManager#setWifiEnabled(boolean)}
383 * @param enable {@code true} to enable, {@code false} to disable.
384 * @return {@code true} if the enable/disable operation was
385 * started or is already in the queue.
386 */
387 public boolean setWifiEnabled(boolean enable) {
388 enforceChangePermission();
389 if (mWifiHandler == null) return false;
390
391 synchronized (mWifiHandler) {
Robert Greenwalta99f4612009-09-19 18:14:32 -0700392 // caller may not have WAKE_LOCK permission - it's not required here
393 long ident = Binder.clearCallingIdentity();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800394 sWakeLock.acquire();
Robert Greenwalta99f4612009-09-19 18:14:32 -0700395 Binder.restoreCallingIdentity(ident);
396
Dianne Hackborn617f8772009-03-31 15:04:46 -0700397 mLastEnableUid = Binder.getCallingUid();
Mike Lockwoodbd5ddf02009-07-29 21:37:14 -0700398 // set a flag if the user is enabling Wifi while in airplane mode
399 mAirplaneModeOverwridden = (enable && isAirplaneModeOn() && isAirplaneToggleable());
Dianne Hackborn617f8772009-03-31 15:04:46 -0700400 sendEnableMessage(enable, true, Binder.getCallingUid());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800401 }
402
403 return true;
404 }
405
406 /**
407 * Enables/disables Wi-Fi synchronously.
408 * @param enable {@code true} to turn Wi-Fi on, {@code false} to turn it off.
409 * @param persist {@code true} if the setting should be persisted.
Dianne Hackborn617f8772009-03-31 15:04:46 -0700410 * @param uid The UID of the process making the request.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800411 * @return {@code true} if the operation succeeds (or if the existing state
412 * is the same as the requested state)
413 */
Dianne Hackborn617f8772009-03-31 15:04:46 -0700414 private boolean setWifiEnabledBlocking(boolean enable, boolean persist, int uid) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800415 final int eventualWifiState = enable ? WIFI_STATE_ENABLED : WIFI_STATE_DISABLED;
Irfan Sheriff0f344060092010-03-10 10:05:51 -0800416 final int wifiState = mWifiStateTracker.getWifiState();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800417
Irfan Sheriff0f344060092010-03-10 10:05:51 -0800418 if (wifiState == eventualWifiState) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800419 return true;
420 }
Mike Lockwoodbd5ddf02009-07-29 21:37:14 -0700421 if (enable && isAirplaneModeOn() && !mAirplaneModeOverwridden) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800422 return false;
423 }
424
Irfan Sheriffcd770372010-01-08 09:36:04 -0800425 /**
426 * Multiple calls to unregisterReceiver() cause exception and a system crash.
427 * This can happen if a supplicant is lost (or firmware crash occurs) and user indicates
428 * disable wifi at the same time.
429 * Avoid doing a disable when the current Wifi state is UNKNOWN
430 * TODO: Handle driver load fail and supplicant lost as seperate states
431 */
Irfan Sheriff0f344060092010-03-10 10:05:51 -0800432 if ((wifiState == WIFI_STATE_UNKNOWN) && !enable) {
Irfan Sheriffcd770372010-01-08 09:36:04 -0800433 return false;
434 }
435
Dianne Hackborn617f8772009-03-31 15:04:46 -0700436 setWifiEnabledState(enable ? WIFI_STATE_ENABLING : WIFI_STATE_DISABLING, uid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800437
Irfan Sheriff5321aef2010-02-12 12:35:59 -0800438 if ((mWifiApState == WIFI_AP_STATE_ENABLED) && enable) {
Irfan Sheriff9ab518ad2010-03-12 15:48:17 -0800439 setWifiApEnabledBlocking(false, Process.myUid(), null);
Irfan Sheriff5321aef2010-02-12 12:35:59 -0800440 }
441
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800442 if (enable) {
Irfan Sheriffa8fbe1f2010-03-09 09:13:58 -0800443 if (!mWifiStateTracker.loadDriver()) {
444 Slog.e(TAG, "Failed to load Wi-Fi driver.");
445 setWifiEnabledState(WIFI_STATE_UNKNOWN, uid);
446 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800447 }
Irfan Sheriffa8fbe1f2010-03-09 09:13:58 -0800448 if (!mWifiStateTracker.startSupplicant()) {
449 mWifiStateTracker.unloadDriver();
450 Slog.e(TAG, "Failed to start supplicant daemon.");
451 setWifiEnabledState(WIFI_STATE_UNKNOWN, uid);
452 return false;
453 }
454
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800455 registerForBroadcasts();
456 mWifiStateTracker.startEventLoop();
Irfan Sheriff7b009782010-03-11 16:37:45 -0800457
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800458 } else {
459
460 mContext.unregisterReceiver(mReceiver);
461 // Remove notification (it will no-op if it isn't visible)
462 mWifiStateTracker.setNotificationVisible(false, 0, false, 0);
463
464 boolean failedToStopSupplicantOrUnloadDriver = false;
Irfan Sheriffa8fbe1f2010-03-09 09:13:58 -0800465
466 if (!mWifiStateTracker.stopSupplicant()) {
467 Slog.e(TAG, "Failed to stop supplicant daemon.");
468 setWifiEnabledState(WIFI_STATE_UNKNOWN, uid);
469 failedToStopSupplicantOrUnloadDriver = true;
470 }
471
472 /**
473 * Reset connections and disable interface
474 * before we unload the driver
475 */
476 mWifiStateTracker.resetConnections(true);
477
478 if (!mWifiStateTracker.unloadDriver()) {
479 Slog.e(TAG, "Failed to unload Wi-Fi driver.");
480 if (!failedToStopSupplicantOrUnloadDriver) {
Dianne Hackborn617f8772009-03-31 15:04:46 -0700481 setWifiEnabledState(WIFI_STATE_UNKNOWN, uid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800482 failedToStopSupplicantOrUnloadDriver = true;
483 }
484 }
Irfan Sheriffa8fbe1f2010-03-09 09:13:58 -0800485
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800486 if (failedToStopSupplicantOrUnloadDriver) {
487 return false;
488 }
489 }
490
491 // Success!
492
493 if (persist) {
494 persistWifiEnabled(enable);
495 }
Dianne Hackborn617f8772009-03-31 15:04:46 -0700496 setWifiEnabledState(eventualWifiState, uid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800497 return true;
498 }
499
Dianne Hackborn617f8772009-03-31 15:04:46 -0700500 private void setWifiEnabledState(int wifiState, int uid) {
Irfan Sheriff0f344060092010-03-10 10:05:51 -0800501 final int previousWifiState = mWifiStateTracker.getWifiState();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800502
The Android Open Source Project10592532009-03-18 17:39:46 -0700503 long ident = Binder.clearCallingIdentity();
504 try {
505 if (wifiState == WIFI_STATE_ENABLED) {
Dianne Hackborn617f8772009-03-31 15:04:46 -0700506 mBatteryStats.noteWifiOn(uid);
The Android Open Source Project10592532009-03-18 17:39:46 -0700507 } else if (wifiState == WIFI_STATE_DISABLED) {
Dianne Hackborn617f8772009-03-31 15:04:46 -0700508 mBatteryStats.noteWifiOff(uid);
The Android Open Source Project10592532009-03-18 17:39:46 -0700509 }
510 } catch (RemoteException e) {
511 } finally {
512 Binder.restoreCallingIdentity(ident);
513 }
Jaikumar Ganesh084c6652009-12-07 10:58:18 -0800514
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800515 // Update state
Irfan Sheriff0f344060092010-03-10 10:05:51 -0800516 mWifiStateTracker.setWifiState(wifiState);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800517
518 // Broadcast
519 final Intent intent = new Intent(WifiManager.WIFI_STATE_CHANGED_ACTION);
520 intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY_BEFORE_BOOT);
521 intent.putExtra(WifiManager.EXTRA_WIFI_STATE, wifiState);
522 intent.putExtra(WifiManager.EXTRA_PREVIOUS_WIFI_STATE, previousWifiState);
523 mContext.sendStickyBroadcast(intent);
524 }
525
526 private void enforceAccessPermission() {
527 mContext.enforceCallingOrSelfPermission(android.Manifest.permission.ACCESS_WIFI_STATE,
528 "WifiService");
529 }
530
531 private void enforceChangePermission() {
532 mContext.enforceCallingOrSelfPermission(android.Manifest.permission.CHANGE_WIFI_STATE,
533 "WifiService");
534
535 }
536
Robert Greenwaltfc1b15c2009-05-22 15:09:51 -0700537 private void enforceMulticastChangePermission() {
538 mContext.enforceCallingOrSelfPermission(
539 android.Manifest.permission.CHANGE_WIFI_MULTICAST_STATE,
540 "WifiService");
541 }
542
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800543 /**
544 * see {@link WifiManager#getWifiState()}
545 * @return One of {@link WifiManager#WIFI_STATE_DISABLED},
546 * {@link WifiManager#WIFI_STATE_DISABLING},
547 * {@link WifiManager#WIFI_STATE_ENABLED},
548 * {@link WifiManager#WIFI_STATE_ENABLING},
549 * {@link WifiManager#WIFI_STATE_UNKNOWN}
550 */
551 public int getWifiEnabledState() {
552 enforceAccessPermission();
Irfan Sheriff0f344060092010-03-10 10:05:51 -0800553 return mWifiStateTracker.getWifiState();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800554 }
555
556 /**
557 * see {@link android.net.wifi.WifiManager#disconnect()}
558 * @return {@code true} if the operation succeeds
559 */
560 public boolean disconnect() {
561 enforceChangePermission();
Irfan Sheriffa8fbe1f2010-03-09 09:13:58 -0800562
563 return mWifiStateTracker.disconnect();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800564 }
565
566 /**
567 * see {@link android.net.wifi.WifiManager#reconnect()}
568 * @return {@code true} if the operation succeeds
569 */
570 public boolean reconnect() {
571 enforceChangePermission();
Irfan Sheriffa8fbe1f2010-03-09 09:13:58 -0800572
573 return mWifiStateTracker.reconnectCommand();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800574 }
575
576 /**
577 * see {@link android.net.wifi.WifiManager#reassociate()}
578 * @return {@code true} if the operation succeeds
579 */
580 public boolean reassociate() {
581 enforceChangePermission();
Irfan Sheriffa8fbe1f2010-03-09 09:13:58 -0800582
583 return mWifiStateTracker.reassociate();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800584 }
585
Irfan Sheriff5321aef2010-02-12 12:35:59 -0800586 /**
Irfan Sheriffc2f54c22010-03-18 14:02:22 -0700587 * see {@link android.net.wifi.WifiManager#setWifiApEnabled(WifiConfiguration, boolean)}
Irfan Sheriff5321aef2010-02-12 12:35:59 -0800588 * @param wifiConfig SSID, security and channel details as
589 * part of WifiConfiguration
Irfan Sheriffc2f54c22010-03-18 14:02:22 -0700590 * @param enabled, true to enable and false to disable
Irfan Sheriff5321aef2010-02-12 12:35:59 -0800591 * @return {@code true} if the start operation was
592 * started or is already in the queue.
593 */
594 public boolean setWifiApEnabled(WifiConfiguration wifiConfig, boolean enabled) {
595 enforceChangePermission();
596 if (mWifiHandler == null) return false;
597
598 synchronized (mWifiHandler) {
599
600 long ident = Binder.clearCallingIdentity();
601 sWakeLock.acquire();
602 Binder.restoreCallingIdentity(ident);
603
604 mLastEnableUid = Binder.getCallingUid();
605
606 sendAccessPointMessage(enabled, wifiConfig, Binder.getCallingUid());
607 }
608
609 return true;
610 }
611
Irfan Sheriff9ab518ad2010-03-12 15:48:17 -0800612 public WifiConfiguration getWifiApConfiguration() {
613 final ContentResolver cr = mContext.getContentResolver();
614 WifiConfiguration wifiConfig = new WifiConfiguration();
615 int authType;
616 try {
617 wifiConfig.SSID = Settings.Secure.getString(cr, Settings.Secure.WIFI_AP_SSID);
618 if (wifiConfig.SSID == null)
619 return null;
620 authType = Settings.Secure.getInt(cr, Settings.Secure.WIFI_AP_SECURITY);
621 wifiConfig.allowedKeyManagement.set(authType);
622 wifiConfig.preSharedKey = Settings.Secure.getString(cr, Settings.Secure.WIFI_AP_PASSWD);
623 return wifiConfig;
624 } catch (Settings.SettingNotFoundException e) {
625 Slog.e(TAG,"AP settings not found, returning");
626 return null;
627 }
628 }
629
630 private void persistApConfiguration(WifiConfiguration wifiConfig) {
631 final ContentResolver cr = mContext.getContentResolver();
632 boolean isWpa;
633 if (wifiConfig == null)
634 return;
635 Settings.Secure.putString(cr, Settings.Secure.WIFI_AP_SSID, wifiConfig.SSID);
636 isWpa = wifiConfig.allowedKeyManagement.get(KeyMgmt.WPA_PSK);
637 Settings.Secure.putInt(cr,
638 Settings.Secure.WIFI_AP_SECURITY,
639 isWpa ? KeyMgmt.WPA_PSK : KeyMgmt.NONE);
640 if (isWpa)
641 Settings.Secure.putString(cr, Settings.Secure.WIFI_AP_PASSWD, wifiConfig.preSharedKey);
642 }
643
Irfan Sheriff5321aef2010-02-12 12:35:59 -0800644 /**
645 * Enables/disables Wi-Fi AP synchronously. The driver is loaded
646 * and soft access point configured as a single operation.
647 * @param enable {@code true} to turn Wi-Fi on, {@code false} to turn it off.
Irfan Sheriff5321aef2010-02-12 12:35:59 -0800648 * @param uid The UID of the process making the request.
Irfan Sheriff9ab518ad2010-03-12 15:48:17 -0800649 * @param wifiConfig The WifiConfiguration for AP
Irfan Sheriff5321aef2010-02-12 12:35:59 -0800650 * @return {@code true} if the operation succeeds (or if the existing state
651 * is the same as the requested state)
652 */
Irfan Sheriff5321aef2010-02-12 12:35:59 -0800653 private boolean setWifiApEnabledBlocking(boolean enable,
Irfan Sheriff9ab518ad2010-03-12 15:48:17 -0800654 int uid, WifiConfiguration wifiConfig) {
Irfan Sheriff5321aef2010-02-12 12:35:59 -0800655 final int eventualWifiApState = enable ? WIFI_AP_STATE_ENABLED : WIFI_AP_STATE_DISABLED;
656
657 if (mWifiApState == eventualWifiApState) {
Irfan Sheriff9ab518ad2010-03-12 15:48:17 -0800658 /* Configuration changed on a running access point */
659 if(enable && (wifiConfig != null)) {
660 try {
661 persistApConfiguration(wifiConfig);
Irfan Sheriffc2f54c22010-03-18 14:02:22 -0700662 nwService.setAccessPoint(wifiConfig, mWifiStateTracker.getInterfaceName(),
663 SOFTAP_IFACE);
Irfan Sheriff9ab518ad2010-03-12 15:48:17 -0800664 return true;
665 } catch(Exception e) {
666 Slog.e(TAG, "Exception in nwService during AP restart");
Irfan Sheriffc2f54c22010-03-18 14:02:22 -0700667 try {
668 nwService.stopAccessPoint();
669 } catch (Exception ee) {
670 Slog.e(TAG, "Could not stop AP, :" + ee);
671 }
Irfan Sheriff9ab518ad2010-03-12 15:48:17 -0800672 setWifiApEnabledState(WIFI_AP_STATE_FAILED, uid, DriverAction.DRIVER_UNLOAD);
673 return false;
674 }
675 } else {
676 return true;
677 }
Irfan Sheriff5321aef2010-02-12 12:35:59 -0800678 }
679
Irfan Sheriff9ab518ad2010-03-12 15:48:17 -0800680 setWifiApEnabledState(enable ? WIFI_AP_STATE_ENABLING :
681 WIFI_AP_STATE_DISABLING, uid, DriverAction.NO_DRIVER_UNLOAD);
Irfan Sheriff5321aef2010-02-12 12:35:59 -0800682
683 if (enable) {
Irfan Sheriff9ab518ad2010-03-12 15:48:17 -0800684
685 /**
686 * Disable client mode for starting AP
687 */
688 if (mWifiStateTracker.getWifiState() == WIFI_STATE_ENABLED) {
689 setWifiEnabledBlocking(false, true, Process.myUid());
690 }
691
692 /* Use default config if there is no existing config */
693 if (wifiConfig == null && ((wifiConfig = getWifiApConfiguration()) == null)) {
694 wifiConfig = new WifiConfiguration();
695 wifiConfig.SSID = mContext.getString(R.string.wifi_tether_configure_ssid_default);
696 wifiConfig.allowedKeyManagement.set(KeyMgmt.NONE);
697 }
698 persistApConfiguration(wifiConfig);
699
Irfan Sheriffa8fbe1f2010-03-09 09:13:58 -0800700 if (!mWifiStateTracker.loadDriver()) {
701 Slog.e(TAG, "Failed to load Wi-Fi driver for AP mode");
Irfan Sheriff9ab518ad2010-03-12 15:48:17 -0800702 setWifiApEnabledState(WIFI_AP_STATE_FAILED, uid, DriverAction.NO_DRIVER_UNLOAD);
Irfan Sheriffa8fbe1f2010-03-09 09:13:58 -0800703 return false;
Irfan Sheriff5321aef2010-02-12 12:35:59 -0800704 }
705
706 try {
Irfan Sheriffc2f54c22010-03-18 14:02:22 -0700707 nwService.startAccessPoint(wifiConfig, mWifiStateTracker.getInterfaceName(),
708 SOFTAP_IFACE);
Irfan Sheriff5321aef2010-02-12 12:35:59 -0800709 } catch(Exception e) {
710 Slog.e(TAG, "Exception in startAccessPoint()");
Irfan Sheriff9ab518ad2010-03-12 15:48:17 -0800711 setWifiApEnabledState(WIFI_AP_STATE_FAILED, uid, DriverAction.DRIVER_UNLOAD);
712 return false;
Irfan Sheriff5321aef2010-02-12 12:35:59 -0800713 }
714
715 } else {
716
717 try {
718 nwService.stopAccessPoint();
719 } catch(Exception e) {
720 Slog.e(TAG, "Exception in stopAccessPoint()");
Irfan Sheriff9ab518ad2010-03-12 15:48:17 -0800721 setWifiApEnabledState(WIFI_AP_STATE_FAILED, uid, DriverAction.DRIVER_UNLOAD);
722 return false;
Irfan Sheriff5321aef2010-02-12 12:35:59 -0800723 }
724
Irfan Sheriffa8fbe1f2010-03-09 09:13:58 -0800725 if (!mWifiStateTracker.unloadDriver()) {
726 Slog.e(TAG, "Failed to unload Wi-Fi driver for AP mode");
Irfan Sheriff9ab518ad2010-03-12 15:48:17 -0800727 setWifiApEnabledState(WIFI_AP_STATE_FAILED, uid, DriverAction.NO_DRIVER_UNLOAD);
Irfan Sheriffa8fbe1f2010-03-09 09:13:58 -0800728 return false;
Irfan Sheriff5321aef2010-02-12 12:35:59 -0800729 }
730 }
731
Irfan Sheriff9ab518ad2010-03-12 15:48:17 -0800732 setWifiApEnabledState(eventualWifiApState, uid, DriverAction.NO_DRIVER_UNLOAD);
Irfan Sheriff5321aef2010-02-12 12:35:59 -0800733 return true;
734 }
735
736 /**
737 * see {@link WifiManager#getWifiApState()}
738 * @return One of {@link WifiManager#WIFI_AP_STATE_DISABLED},
739 * {@link WifiManager#WIFI_AP_STATE_DISABLING},
740 * {@link WifiManager#WIFI_AP_STATE_ENABLED},
741 * {@link WifiManager#WIFI_AP_STATE_ENABLING},
742 * {@link WifiManager#WIFI_AP_STATE_FAILED}
743 */
744 public int getWifiApEnabledState() {
745 enforceAccessPermission();
746 return mWifiApState;
747 }
748
Irfan Sheriff9ab518ad2010-03-12 15:48:17 -0800749 private void setWifiApEnabledState(int wifiAPState, int uid, DriverAction flag) {
Irfan Sheriff5321aef2010-02-12 12:35:59 -0800750 final int previousWifiApState = mWifiApState;
751
Irfan Sheriff9ab518ad2010-03-12 15:48:17 -0800752 /**
753 * Unload the driver if going to a failed state
754 */
755 if ((mWifiApState == WIFI_AP_STATE_FAILED) && (flag == DriverAction.DRIVER_UNLOAD)) {
756 mWifiStateTracker.unloadDriver();
757 }
758
Irfan Sheriff5321aef2010-02-12 12:35:59 -0800759 long ident = Binder.clearCallingIdentity();
760 try {
761 if (wifiAPState == WIFI_AP_STATE_ENABLED) {
762 mBatteryStats.noteWifiOn(uid);
763 } else if (wifiAPState == WIFI_AP_STATE_DISABLED) {
764 mBatteryStats.noteWifiOff(uid);
765 }
766 } catch (RemoteException e) {
767 } finally {
768 Binder.restoreCallingIdentity(ident);
769 }
770
771 // Update state
772 mWifiApState = wifiAPState;
773
774 // Broadcast
775 final Intent intent = new Intent(WifiManager.WIFI_AP_STATE_CHANGED_ACTION);
776 intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY_BEFORE_BOOT);
777 intent.putExtra(WifiManager.EXTRA_WIFI_AP_STATE, wifiAPState);
778 intent.putExtra(WifiManager.EXTRA_PREVIOUS_WIFI_AP_STATE, previousWifiApState);
779 mContext.sendStickyBroadcast(intent);
780 }
781
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800782 /**
783 * see {@link android.net.wifi.WifiManager#getConfiguredNetworks()}
784 * @return the list of configured networks
785 */
786 public List<WifiConfiguration> getConfiguredNetworks() {
787 enforceAccessPermission();
788 String listStr;
Irfan Sheriffa8fbe1f2010-03-09 09:13:58 -0800789
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800790 /*
791 * We don't cache the list, because we want to allow
792 * for the possibility that the configuration file
793 * has been modified through some external means,
794 * such as the wpa_cli command line program.
795 */
Irfan Sheriffa8fbe1f2010-03-09 09:13:58 -0800796 listStr = mWifiStateTracker.listNetworks();
797
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800798 List<WifiConfiguration> networks =
799 new ArrayList<WifiConfiguration>();
800 if (listStr == null)
801 return networks;
802
803 String[] lines = listStr.split("\n");
804 // Skip the first line, which is a header
805 for (int i = 1; i < lines.length; i++) {
806 String[] result = lines[i].split("\t");
807 // network-id | ssid | bssid | flags
808 WifiConfiguration config = new WifiConfiguration();
809 try {
810 config.networkId = Integer.parseInt(result[0]);
811 } catch(NumberFormatException e) {
812 continue;
813 }
814 if (result.length > 3) {
815 if (result[3].indexOf("[CURRENT]") != -1)
816 config.status = WifiConfiguration.Status.CURRENT;
817 else if (result[3].indexOf("[DISABLED]") != -1)
818 config.status = WifiConfiguration.Status.DISABLED;
819 else
820 config.status = WifiConfiguration.Status.ENABLED;
Irfan Sheriffa8fbe1f2010-03-09 09:13:58 -0800821 } else {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800822 config.status = WifiConfiguration.Status.ENABLED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800823 }
Irfan Sheriffa8fbe1f2010-03-09 09:13:58 -0800824 readNetworkVariables(config);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800825 networks.add(config);
826 }
827
828 return networks;
829 }
830
831 /**
832 * Read the variables from the supplicant daemon that are needed to
833 * fill in the WifiConfiguration object.
834 * <p/>
835 * The caller must hold the synchronization monitor.
836 * @param config the {@link WifiConfiguration} object to be filled in.
837 */
Irfan Sheriffa8fbe1f2010-03-09 09:13:58 -0800838 private void readNetworkVariables(WifiConfiguration config) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800839
840 int netId = config.networkId;
841 if (netId < 0)
842 return;
843
844 /*
845 * TODO: maybe should have a native method that takes an array of
846 * variable names and returns an array of values. But we'd still
847 * be doing a round trip to the supplicant daemon for each variable.
848 */
849 String value;
850
Irfan Sheriffa8fbe1f2010-03-09 09:13:58 -0800851 value = mWifiStateTracker.getNetworkVariable(netId, WifiConfiguration.ssidVarName);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800852 if (!TextUtils.isEmpty(value)) {
Chung-yih Wanga8d15942009-10-09 11:01:49 +0800853 config.SSID = removeDoubleQuotes(value);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800854 } else {
855 config.SSID = null;
856 }
857
Irfan Sheriffa8fbe1f2010-03-09 09:13:58 -0800858 value = mWifiStateTracker.getNetworkVariable(netId, WifiConfiguration.bssidVarName);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800859 if (!TextUtils.isEmpty(value)) {
860 config.BSSID = value;
861 } else {
862 config.BSSID = null;
863 }
864
Irfan Sheriffa8fbe1f2010-03-09 09:13:58 -0800865 value = mWifiStateTracker.getNetworkVariable(netId, WifiConfiguration.priorityVarName);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800866 config.priority = -1;
867 if (!TextUtils.isEmpty(value)) {
868 try {
869 config.priority = Integer.parseInt(value);
870 } catch (NumberFormatException ignore) {
871 }
872 }
873
Irfan Sheriffa8fbe1f2010-03-09 09:13:58 -0800874 value = mWifiStateTracker.getNetworkVariable(netId, WifiConfiguration.hiddenSSIDVarName);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800875 config.hiddenSSID = false;
876 if (!TextUtils.isEmpty(value)) {
877 try {
878 config.hiddenSSID = Integer.parseInt(value) != 0;
879 } catch (NumberFormatException ignore) {
880 }
881 }
882
Irfan Sheriffa8fbe1f2010-03-09 09:13:58 -0800883 value = mWifiStateTracker.getNetworkVariable(netId, WifiConfiguration.wepTxKeyIdxVarName);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800884 config.wepTxKeyIndex = -1;
885 if (!TextUtils.isEmpty(value)) {
886 try {
887 config.wepTxKeyIndex = Integer.parseInt(value);
888 } catch (NumberFormatException ignore) {
889 }
890 }
891
892 /*
893 * Get up to 4 WEP keys. Note that the actual keys are not passed back,
894 * just a "*" if the key is set, or the null string otherwise.
895 */
896 for (int i = 0; i < 4; i++) {
Irfan Sheriffa8fbe1f2010-03-09 09:13:58 -0800897 value = mWifiStateTracker.getNetworkVariable(netId, WifiConfiguration.wepKeyVarNames[i]);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800898 if (!TextUtils.isEmpty(value)) {
899 config.wepKeys[i] = value;
900 } else {
901 config.wepKeys[i] = null;
902 }
903 }
904
905 /*
906 * Get the private shared key. Note that the actual keys are not passed back,
907 * just a "*" if the key is set, or the null string otherwise.
908 */
Irfan Sheriffa8fbe1f2010-03-09 09:13:58 -0800909 value = mWifiStateTracker.getNetworkVariable(netId, WifiConfiguration.pskVarName);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800910 if (!TextUtils.isEmpty(value)) {
911 config.preSharedKey = value;
912 } else {
913 config.preSharedKey = null;
914 }
915
Irfan Sheriffa8fbe1f2010-03-09 09:13:58 -0800916 value = mWifiStateTracker.getNetworkVariable(config.networkId,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800917 WifiConfiguration.Protocol.varName);
918 if (!TextUtils.isEmpty(value)) {
919 String vals[] = value.split(" ");
920 for (String val : vals) {
921 int index =
922 lookupString(val, WifiConfiguration.Protocol.strings);
923 if (0 <= index) {
924 config.allowedProtocols.set(index);
925 }
926 }
927 }
928
Irfan Sheriffa8fbe1f2010-03-09 09:13:58 -0800929 value = mWifiStateTracker.getNetworkVariable(config.networkId,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800930 WifiConfiguration.KeyMgmt.varName);
931 if (!TextUtils.isEmpty(value)) {
932 String vals[] = value.split(" ");
933 for (String val : vals) {
934 int index =
935 lookupString(val, WifiConfiguration.KeyMgmt.strings);
936 if (0 <= index) {
937 config.allowedKeyManagement.set(index);
938 }
939 }
940 }
941
Irfan Sheriffa8fbe1f2010-03-09 09:13:58 -0800942 value = mWifiStateTracker.getNetworkVariable(config.networkId,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800943 WifiConfiguration.AuthAlgorithm.varName);
944 if (!TextUtils.isEmpty(value)) {
945 String vals[] = value.split(" ");
946 for (String val : vals) {
947 int index =
948 lookupString(val, WifiConfiguration.AuthAlgorithm.strings);
949 if (0 <= index) {
950 config.allowedAuthAlgorithms.set(index);
951 }
952 }
953 }
954
Irfan Sheriffa8fbe1f2010-03-09 09:13:58 -0800955 value = mWifiStateTracker.getNetworkVariable(config.networkId,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800956 WifiConfiguration.PairwiseCipher.varName);
957 if (!TextUtils.isEmpty(value)) {
958 String vals[] = value.split(" ");
959 for (String val : vals) {
960 int index =
961 lookupString(val, WifiConfiguration.PairwiseCipher.strings);
962 if (0 <= index) {
963 config.allowedPairwiseCiphers.set(index);
964 }
965 }
966 }
967
Irfan Sheriffa8fbe1f2010-03-09 09:13:58 -0800968 value = mWifiStateTracker.getNetworkVariable(config.networkId,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800969 WifiConfiguration.GroupCipher.varName);
970 if (!TextUtils.isEmpty(value)) {
971 String vals[] = value.split(" ");
972 for (String val : vals) {
973 int index =
974 lookupString(val, WifiConfiguration.GroupCipher.strings);
975 if (0 <= index) {
976 config.allowedGroupCiphers.set(index);
977 }
978 }
979 }
Chung-yih Wang43374762009-09-16 14:28:42 +0800980
981 for (WifiConfiguration.EnterpriseField field :
982 config.enterpriseFields) {
Irfan Sheriffa8fbe1f2010-03-09 09:13:58 -0800983 value = mWifiStateTracker.getNetworkVariable(netId,
Chung-yih Wang43374762009-09-16 14:28:42 +0800984 field.varName());
985 if (!TextUtils.isEmpty(value)) {
Chung-yih Wanga8d15942009-10-09 11:01:49 +0800986 if (field != config.eap) value = removeDoubleQuotes(value);
Chung-yih Wang43374762009-09-16 14:28:42 +0800987 field.setValue(value);
988 }
989 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800990 }
991
Chung-yih Wanga8d15942009-10-09 11:01:49 +0800992 private static String removeDoubleQuotes(String string) {
993 if (string.length() <= 2) return "";
994 return string.substring(1, string.length() - 1);
995 }
996
997 private static String convertToQuotedString(String string) {
998 return "\"" + string + "\"";
999 }
1000
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001001 /**
1002 * see {@link android.net.wifi.WifiManager#addOrUpdateNetwork(WifiConfiguration)}
1003 * @return the supplicant-assigned identifier for the new or updated
1004 * network if the operation succeeds, or {@code -1} if it fails
1005 */
Irfan Sheriff7aac5542009-12-22 21:42:17 -08001006 public int addOrUpdateNetwork(WifiConfiguration config) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001007 enforceChangePermission();
Irfan Sheriffa8fbe1f2010-03-09 09:13:58 -08001008
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001009 /*
1010 * If the supplied networkId is -1, we create a new empty
1011 * network configuration. Otherwise, the networkId should
1012 * refer to an existing configuration.
1013 */
1014 int netId = config.networkId;
1015 boolean newNetwork = netId == -1;
Irfan Sheriff44113ba32010-03-16 14:54:07 -07001016 boolean doReconfig = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001017 // networkId of -1 means we want to create a new network
Irfan Sheriff7aac5542009-12-22 21:42:17 -08001018 synchronized (mWifiStateTracker) {
1019 if (newNetwork) {
Irfan Sheriffa8fbe1f2010-03-09 09:13:58 -08001020 netId = mWifiStateTracker.addNetwork();
Irfan Sheriff7aac5542009-12-22 21:42:17 -08001021 if (netId < 0) {
1022 if (DBG) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001023 Slog.d(TAG, "Failed to add a network!");
Irfan Sheriff7aac5542009-12-22 21:42:17 -08001024 }
1025 return -1;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001026 }
Irfan Sheriff7aac5542009-12-22 21:42:17 -08001027 doReconfig = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001028 }
Irfan Sheriff7aac5542009-12-22 21:42:17 -08001029 mNeedReconfig = mNeedReconfig || doReconfig;
Irfan Sheriffa8fbe1f2010-03-09 09:13:58 -08001030 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001031
Irfan Sheriffa8fbe1f2010-03-09 09:13:58 -08001032 setVariables: {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001033 /*
1034 * Note that if a networkId for a non-existent network
Irfan Sheriffa8fbe1f2010-03-09 09:13:58 -08001035 * was supplied, then the first setNetworkVariable()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001036 * will fail, so we don't bother to make a separate check
1037 * for the validity of the ID up front.
1038 */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001039 if (config.SSID != null &&
Irfan Sheriffa8fbe1f2010-03-09 09:13:58 -08001040 !mWifiStateTracker.setNetworkVariable(
Irfan Sheriff7aac5542009-12-22 21:42:17 -08001041 netId,
1042 WifiConfiguration.ssidVarName,
1043 convertToQuotedString(config.SSID))) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001044 if (DBG) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001045 Slog.d(TAG, "failed to set SSID: "+config.SSID);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001046 }
1047 break setVariables;
1048 }
1049
1050 if (config.BSSID != null &&
Irfan Sheriffa8fbe1f2010-03-09 09:13:58 -08001051 !mWifiStateTracker.setNetworkVariable(
Irfan Sheriff7aac5542009-12-22 21:42:17 -08001052 netId,
1053 WifiConfiguration.bssidVarName,
1054 config.BSSID)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001055 if (DBG) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001056 Slog.d(TAG, "failed to set BSSID: "+config.BSSID);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001057 }
1058 break setVariables;
1059 }
1060
1061 String allowedKeyManagementString =
1062 makeString(config.allowedKeyManagement, WifiConfiguration.KeyMgmt.strings);
1063 if (config.allowedKeyManagement.cardinality() != 0 &&
Irfan Sheriffa8fbe1f2010-03-09 09:13:58 -08001064 !mWifiStateTracker.setNetworkVariable(
Irfan Sheriff7aac5542009-12-22 21:42:17 -08001065 netId,
1066 WifiConfiguration.KeyMgmt.varName,
1067 allowedKeyManagementString)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001068 if (DBG) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001069 Slog.d(TAG, "failed to set key_mgmt: "+
Irfan Sheriff7aac5542009-12-22 21:42:17 -08001070 allowedKeyManagementString);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001071 }
1072 break setVariables;
1073 }
1074
1075 String allowedProtocolsString =
1076 makeString(config.allowedProtocols, WifiConfiguration.Protocol.strings);
1077 if (config.allowedProtocols.cardinality() != 0 &&
Irfan Sheriffa8fbe1f2010-03-09 09:13:58 -08001078 !mWifiStateTracker.setNetworkVariable(
Irfan Sheriff7aac5542009-12-22 21:42:17 -08001079 netId,
1080 WifiConfiguration.Protocol.varName,
1081 allowedProtocolsString)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001082 if (DBG) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001083 Slog.d(TAG, "failed to set proto: "+
Irfan Sheriff7aac5542009-12-22 21:42:17 -08001084 allowedProtocolsString);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001085 }
1086 break setVariables;
1087 }
1088
1089 String allowedAuthAlgorithmsString =
1090 makeString(config.allowedAuthAlgorithms, WifiConfiguration.AuthAlgorithm.strings);
1091 if (config.allowedAuthAlgorithms.cardinality() != 0 &&
Irfan Sheriffa8fbe1f2010-03-09 09:13:58 -08001092 !mWifiStateTracker.setNetworkVariable(
Irfan Sheriff7aac5542009-12-22 21:42:17 -08001093 netId,
1094 WifiConfiguration.AuthAlgorithm.varName,
1095 allowedAuthAlgorithmsString)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001096 if (DBG) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001097 Slog.d(TAG, "failed to set auth_alg: "+
Irfan Sheriff7aac5542009-12-22 21:42:17 -08001098 allowedAuthAlgorithmsString);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001099 }
1100 break setVariables;
1101 }
1102
1103 String allowedPairwiseCiphersString =
1104 makeString(config.allowedPairwiseCiphers, WifiConfiguration.PairwiseCipher.strings);
1105 if (config.allowedPairwiseCiphers.cardinality() != 0 &&
Irfan Sheriffa8fbe1f2010-03-09 09:13:58 -08001106 !mWifiStateTracker.setNetworkVariable(
Irfan Sheriff7aac5542009-12-22 21:42:17 -08001107 netId,
1108 WifiConfiguration.PairwiseCipher.varName,
1109 allowedPairwiseCiphersString)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001110 if (DBG) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001111 Slog.d(TAG, "failed to set pairwise: "+
Irfan Sheriff7aac5542009-12-22 21:42:17 -08001112 allowedPairwiseCiphersString);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001113 }
1114 break setVariables;
1115 }
1116
1117 String allowedGroupCiphersString =
1118 makeString(config.allowedGroupCiphers, WifiConfiguration.GroupCipher.strings);
1119 if (config.allowedGroupCiphers.cardinality() != 0 &&
Irfan Sheriffa8fbe1f2010-03-09 09:13:58 -08001120 !mWifiStateTracker.setNetworkVariable(
Irfan Sheriff7aac5542009-12-22 21:42:17 -08001121 netId,
1122 WifiConfiguration.GroupCipher.varName,
1123 allowedGroupCiphersString)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001124 if (DBG) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001125 Slog.d(TAG, "failed to set group: "+
Irfan Sheriff7aac5542009-12-22 21:42:17 -08001126 allowedGroupCiphersString);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001127 }
1128 break setVariables;
1129 }
1130
1131 // Prevent client screw-up by passing in a WifiConfiguration we gave it
1132 // by preventing "*" as a key.
1133 if (config.preSharedKey != null && !config.preSharedKey.equals("*") &&
Irfan Sheriffa8fbe1f2010-03-09 09:13:58 -08001134 !mWifiStateTracker.setNetworkVariable(
Irfan Sheriff7aac5542009-12-22 21:42:17 -08001135 netId,
1136 WifiConfiguration.pskVarName,
1137 config.preSharedKey)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001138 if (DBG) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001139 Slog.d(TAG, "failed to set psk: "+config.preSharedKey);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001140 }
1141 break setVariables;
1142 }
1143
1144 boolean hasSetKey = false;
1145 if (config.wepKeys != null) {
1146 for (int i = 0; i < config.wepKeys.length; i++) {
1147 // Prevent client screw-up by passing in a WifiConfiguration we gave it
1148 // by preventing "*" as a key.
1149 if (config.wepKeys[i] != null && !config.wepKeys[i].equals("*")) {
Irfan Sheriffa8fbe1f2010-03-09 09:13:58 -08001150 if (!mWifiStateTracker.setNetworkVariable(
Irfan Sheriff7aac5542009-12-22 21:42:17 -08001151 netId,
1152 WifiConfiguration.wepKeyVarNames[i],
1153 config.wepKeys[i])) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001154 if (DBG) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001155 Slog.d(TAG,
Irfan Sheriff7aac5542009-12-22 21:42:17 -08001156 "failed to set wep_key"+i+": " +
1157 config.wepKeys[i]);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001158 }
1159 break setVariables;
1160 }
1161 hasSetKey = true;
1162 }
1163 }
1164 }
1165
1166 if (hasSetKey) {
Irfan Sheriffa8fbe1f2010-03-09 09:13:58 -08001167 if (!mWifiStateTracker.setNetworkVariable(
Irfan Sheriff7aac5542009-12-22 21:42:17 -08001168 netId,
1169 WifiConfiguration.wepTxKeyIdxVarName,
1170 Integer.toString(config.wepTxKeyIndex))) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001171 if (DBG) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001172 Slog.d(TAG,
Irfan Sheriff7aac5542009-12-22 21:42:17 -08001173 "failed to set wep_tx_keyidx: "+
1174 config.wepTxKeyIndex);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001175 }
1176 break setVariables;
1177 }
1178 }
1179
Irfan Sheriffa8fbe1f2010-03-09 09:13:58 -08001180 if (!mWifiStateTracker.setNetworkVariable(
Irfan Sheriff7aac5542009-12-22 21:42:17 -08001181 netId,
1182 WifiConfiguration.priorityVarName,
1183 Integer.toString(config.priority))) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001184 if (DBG) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001185 Slog.d(TAG, config.SSID + ": failed to set priority: "
Irfan Sheriff7aac5542009-12-22 21:42:17 -08001186 +config.priority);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001187 }
1188 break setVariables;
1189 }
1190
Irfan Sheriffa8fbe1f2010-03-09 09:13:58 -08001191 if (config.hiddenSSID && !mWifiStateTracker.setNetworkVariable(
Irfan Sheriff7aac5542009-12-22 21:42:17 -08001192 netId,
1193 WifiConfiguration.hiddenSSIDVarName,
1194 Integer.toString(config.hiddenSSID ? 1 : 0))) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001195 if (DBG) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001196 Slog.d(TAG, config.SSID + ": failed to set hiddenSSID: "+
Irfan Sheriff7aac5542009-12-22 21:42:17 -08001197 config.hiddenSSID);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001198 }
1199 break setVariables;
1200 }
1201
Chung-yih Wang43374762009-09-16 14:28:42 +08001202 for (WifiConfiguration.EnterpriseField field
1203 : config.enterpriseFields) {
1204 String varName = field.varName();
1205 String value = field.value();
Chung-yih Wanga8d15942009-10-09 11:01:49 +08001206 if (value != null) {
1207 if (field != config.eap) {
Chia-chi Yeh784d53e2010-01-29 16:26:28 +08001208 value = (value.length() == 0) ? "NULL" : convertToQuotedString(value);
Chung-yih Wang43374762009-09-16 14:28:42 +08001209 }
Irfan Sheriffa8fbe1f2010-03-09 09:13:58 -08001210 if (!mWifiStateTracker.setNetworkVariable(
Irfan Sheriff7aac5542009-12-22 21:42:17 -08001211 netId,
1212 varName,
1213 value)) {
Chung-yih Wanga8d15942009-10-09 11:01:49 +08001214 if (DBG) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001215 Slog.d(TAG, config.SSID + ": failed to set " + varName +
Irfan Sheriff7aac5542009-12-22 21:42:17 -08001216 ": " + value);
Chung-yih Wanga8d15942009-10-09 11:01:49 +08001217 }
1218 break setVariables;
1219 }
Chung-yih Wang5069cc72009-06-03 17:33:47 +08001220 }
Chung-yih Wang5069cc72009-06-03 17:33:47 +08001221 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001222 return netId;
Irfan Sheriffa8fbe1f2010-03-09 09:13:58 -08001223 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001224
Irfan Sheriffa8fbe1f2010-03-09 09:13:58 -08001225 /*
1226 * For an update, if one of the setNetworkVariable operations fails,
1227 * we might want to roll back all the changes already made. But the
1228 * chances are that if anything is going to go wrong, it'll happen
1229 * the first time we try to set one of the variables.
1230 */
1231 if (newNetwork) {
1232 removeNetwork(netId);
1233 if (DBG) {
1234 Slog.d(TAG,
1235 "Failed to set a network variable, removed network: "
1236 + netId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001237 }
1238 }
1239 return -1;
1240 }
1241
1242 private static String makeString(BitSet set, String[] strings) {
1243 StringBuffer buf = new StringBuffer();
1244 int nextSetBit = -1;
1245
1246 /* Make sure all set bits are in [0, strings.length) to avoid
1247 * going out of bounds on strings. (Shouldn't happen, but...) */
1248 set = set.get(0, strings.length);
1249
1250 while ((nextSetBit = set.nextSetBit(nextSetBit + 1)) != -1) {
1251 buf.append(strings[nextSetBit].replace('_', '-')).append(' ');
1252 }
1253
1254 // remove trailing space
1255 if (set.cardinality() > 0) {
1256 buf.setLength(buf.length() - 1);
1257 }
1258
1259 return buf.toString();
1260 }
1261
1262 private static int lookupString(String string, String[] strings) {
1263 int size = strings.length;
1264
1265 string = string.replace('-', '_');
1266
1267 for (int i = 0; i < size; i++)
1268 if (string.equals(strings[i]))
1269 return i;
1270
1271 if (DBG) {
1272 // if we ever get here, we should probably add the
1273 // value to WifiConfiguration to reflect that it's
1274 // supported by the WPA supplicant
Joe Onorato8a9b2202010-02-26 18:56:32 -08001275 Slog.w(TAG, "Failed to look-up a string: " + string);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001276 }
1277
1278 return -1;
1279 }
1280
1281 /**
1282 * See {@link android.net.wifi.WifiManager#removeNetwork(int)}
1283 * @param netId the integer that identifies the network configuration
1284 * to the supplicant
1285 * @return {@code true} if the operation succeeded
1286 */
1287 public boolean removeNetwork(int netId) {
1288 enforceChangePermission();
1289
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001290 return mWifiStateTracker.removeNetwork(netId);
1291 }
1292
1293 /**
1294 * See {@link android.net.wifi.WifiManager#enableNetwork(int, boolean)}
1295 * @param netId the integer that identifies the network configuration
1296 * to the supplicant
1297 * @param disableOthers if true, disable all other networks.
1298 * @return {@code true} if the operation succeeded
1299 */
1300 public boolean enableNetwork(int netId, boolean disableOthers) {
1301 enforceChangePermission();
1302
Irfan Sheriffa8fbe1f2010-03-09 09:13:58 -08001303 String ifname = mWifiStateTracker.getInterfaceName();
1304 NetworkUtils.enableInterface(ifname);
1305 boolean result = mWifiStateTracker.enableNetwork(netId, disableOthers);
1306 if (!result) {
1307 NetworkUtils.disableInterface(ifname);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001308 }
Irfan Sheriffa8fbe1f2010-03-09 09:13:58 -08001309 return result;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001310 }
1311
1312 /**
1313 * See {@link android.net.wifi.WifiManager#disableNetwork(int)}
1314 * @param netId the integer that identifies the network configuration
1315 * to the supplicant
1316 * @return {@code true} if the operation succeeded
1317 */
1318 public boolean disableNetwork(int netId) {
1319 enforceChangePermission();
1320
Irfan Sheriffa8fbe1f2010-03-09 09:13:58 -08001321 return mWifiStateTracker.disableNetwork(netId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001322 }
1323
1324 /**
1325 * See {@link android.net.wifi.WifiManager#getConnectionInfo()}
1326 * @return the Wi-Fi information, contained in {@link WifiInfo}.
1327 */
1328 public WifiInfo getConnectionInfo() {
1329 enforceAccessPermission();
1330 /*
1331 * Make sure we have the latest information, by sending
1332 * a status request to the supplicant.
1333 */
1334 return mWifiStateTracker.requestConnectionInfo();
1335 }
1336
1337 /**
1338 * Return the results of the most recent access point scan, in the form of
1339 * a list of {@link ScanResult} objects.
1340 * @return the list of results
1341 */
1342 public List<ScanResult> getScanResults() {
1343 enforceAccessPermission();
1344 String reply;
Irfan Sheriffa8fbe1f2010-03-09 09:13:58 -08001345
1346 reply = mWifiStateTracker.scanResults();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001347 if (reply == null) {
1348 return null;
1349 }
1350
1351 List<ScanResult> scanList = new ArrayList<ScanResult>();
1352
1353 int lineCount = 0;
1354
1355 int replyLen = reply.length();
1356 // Parse the result string, keeping in mind that the last line does
1357 // not end with a newline.
1358 for (int lineBeg = 0, lineEnd = 0; lineEnd <= replyLen; ++lineEnd) {
1359 if (lineEnd == replyLen || reply.charAt(lineEnd) == '\n') {
1360 ++lineCount;
1361 /*
1362 * Skip the first line, which is a header
1363 */
1364 if (lineCount == 1) {
1365 lineBeg = lineEnd + 1;
1366 continue;
1367 }
Mike Lockwoodb30475e2009-04-21 13:55:07 -07001368 if (lineEnd > lineBeg) {
1369 String line = reply.substring(lineBeg, lineEnd);
1370 ScanResult scanResult = parseScanResult(line);
1371 if (scanResult != null) {
1372 scanList.add(scanResult);
1373 } else if (DBG) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001374 Slog.w(TAG, "misformatted scan result for: " + line);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001375 }
1376 }
1377 lineBeg = lineEnd + 1;
1378 }
1379 }
1380 mWifiStateTracker.setScanResultsList(scanList);
1381 return scanList;
1382 }
1383
1384 /**
1385 * Parse the scan result line passed to us by wpa_supplicant (helper).
1386 * @param line the line to parse
1387 * @return the {@link ScanResult} object
1388 */
1389 private ScanResult parseScanResult(String line) {
1390 ScanResult scanResult = null;
1391 if (line != null) {
1392 /*
1393 * Cache implementation (LinkedHashMap) is not synchronized, thus,
1394 * must synchronized here!
1395 */
1396 synchronized (mScanResultCache) {
Mike Lockwoodb30475e2009-04-21 13:55:07 -07001397 String[] result = scanResultPattern.split(line);
1398 if (3 <= result.length && result.length <= 5) {
1399 String bssid = result[0];
1400 // bssid | frequency | level | flags | ssid
1401 int frequency;
1402 int level;
1403 try {
1404 frequency = Integer.parseInt(result[1]);
1405 level = Integer.parseInt(result[2]);
1406 /* some implementations avoid negative values by adding 256
1407 * so we need to adjust for that here.
1408 */
1409 if (level > 0) level -= 256;
1410 } catch (NumberFormatException e) {
1411 frequency = 0;
1412 level = 0;
1413 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001414
Mike Lockwood1a645052009-06-25 13:01:12 -04001415 /*
1416 * The formatting of the results returned by
1417 * wpa_supplicant is intended to make the fields
1418 * line up nicely when printed,
1419 * not to make them easy to parse. So we have to
1420 * apply some heuristics to figure out which field
1421 * is the SSID and which field is the flags.
1422 */
1423 String ssid;
1424 String flags;
1425 if (result.length == 4) {
1426 if (result[3].charAt(0) == '[') {
1427 flags = result[3];
1428 ssid = "";
1429 } else {
1430 flags = "";
1431 ssid = result[3];
1432 }
1433 } else if (result.length == 5) {
1434 flags = result[3];
1435 ssid = result[4];
1436 } else {
1437 // Here, we must have 3 fields: no flags and ssid
1438 // set
1439 flags = "";
1440 ssid = "";
1441 }
1442
Mike Lockwood00717e22009-08-17 10:09:36 -04001443 // bssid + ssid is the hash key
1444 String key = bssid + ssid;
1445 scanResult = mScanResultCache.get(key);
Mike Lockwoodb30475e2009-04-21 13:55:07 -07001446 if (scanResult != null) {
1447 scanResult.level = level;
Mike Lockwood1a645052009-06-25 13:01:12 -04001448 scanResult.SSID = ssid;
1449 scanResult.capabilities = flags;
1450 scanResult.frequency = frequency;
Mike Lockwoodb30475e2009-04-21 13:55:07 -07001451 } else {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001452 // Do not add scan results that have no SSID set
1453 if (0 < ssid.trim().length()) {
1454 scanResult =
1455 new ScanResult(
Mike Lockwoodb30475e2009-04-21 13:55:07 -07001456 ssid, bssid, flags, level, frequency);
Mike Lockwood00717e22009-08-17 10:09:36 -04001457 mScanResultCache.put(key, scanResult);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001458 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001459 }
Mike Lockwoodb30475e2009-04-21 13:55:07 -07001460 } else {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001461 Slog.w(TAG, "Misformatted scan result text with " +
Mike Lockwoodb30475e2009-04-21 13:55:07 -07001462 result.length + " fields: " + line);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001463 }
1464 }
1465 }
1466
1467 return scanResult;
1468 }
1469
1470 /**
1471 * Parse the "flags" field passed back in a scan result by wpa_supplicant,
1472 * and construct a {@code WifiConfiguration} that describes the encryption,
1473 * key management, and authenticaion capabilities of the access point.
1474 * @param flags the string returned by wpa_supplicant
1475 * @return the {@link WifiConfiguration} object, filled in
1476 */
1477 WifiConfiguration parseScanFlags(String flags) {
1478 WifiConfiguration config = new WifiConfiguration();
1479
1480 if (flags.length() == 0) {
1481 config.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE);
1482 }
1483 // ... to be implemented
1484 return config;
1485 }
1486
1487 /**
1488 * Tell the supplicant to persist the current list of configured networks.
1489 * @return {@code true} if the operation succeeded
1490 */
1491 public boolean saveConfiguration() {
1492 boolean result;
1493 enforceChangePermission();
Irfan Sheriffa8fbe1f2010-03-09 09:13:58 -08001494
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001495 synchronized (mWifiStateTracker) {
Irfan Sheriffa8fbe1f2010-03-09 09:13:58 -08001496 result = mWifiStateTracker.saveConfig();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001497 if (result && mNeedReconfig) {
1498 mNeedReconfig = false;
Irfan Sheriffa8fbe1f2010-03-09 09:13:58 -08001499 result = mWifiStateTracker.reloadConfig();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001500
1501 if (result) {
1502 Intent intent = new Intent(WifiManager.NETWORK_IDS_CHANGED_ACTION);
1503 mContext.sendBroadcast(intent);
1504 }
1505 }
1506 }
Amith Yamasani47873e52009-07-02 12:05:32 -07001507 // Inform the backup manager about a data change
1508 IBackupManager ibm = IBackupManager.Stub.asInterface(
1509 ServiceManager.getService(Context.BACKUP_SERVICE));
1510 if (ibm != null) {
1511 try {
1512 ibm.dataChanged("com.android.providers.settings");
1513 } catch (Exception e) {
1514 // Try again later
1515 }
1516 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001517 return result;
1518 }
1519
1520 /**
1521 * Set the number of radio frequency channels that are allowed to be used
1522 * in the current regulatory domain. This method should be used only
1523 * if the correct number of channels cannot be determined automatically
Robert Greenwaltb5010cc2009-05-21 15:11:40 -07001524 * for some reason. If the operation is successful, the new value may be
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001525 * persisted as a Secure setting.
1526 * @param numChannels the number of allowed channels. Must be greater than 0
1527 * and less than or equal to 16.
Robert Greenwaltb5010cc2009-05-21 15:11:40 -07001528 * @param persist {@code true} if the setting should be remembered.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001529 * @return {@code true} if the operation succeeds, {@code false} otherwise, e.g.,
1530 * {@code numChannels} is outside the valid range.
1531 */
Robert Greenwaltb5010cc2009-05-21 15:11:40 -07001532 public boolean setNumAllowedChannels(int numChannels, boolean persist) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001533 Slog.i(TAG, "WifiService trying to setNumAllowed to "+numChannels+
Robert Greenwaltb5010cc2009-05-21 15:11:40 -07001534 " with persist set to "+persist);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001535 enforceChangePermission();
1536 /*
1537 * Validate the argument. We'd like to let the Wi-Fi driver do this,
1538 * but if Wi-Fi isn't currently enabled, that's not possible, and
1539 * we want to persist the setting anyway,so that it will take
1540 * effect when Wi-Fi does become enabled.
1541 */
1542 boolean found = false;
1543 for (int validChan : sValidRegulatoryChannelCounts) {
1544 if (validChan == numChannels) {
1545 found = true;
1546 break;
1547 }
1548 }
1549 if (!found) {
1550 return false;
1551 }
1552
Robert Greenwaltb5010cc2009-05-21 15:11:40 -07001553 if (persist) {
1554 Settings.Secure.putInt(mContext.getContentResolver(),
1555 Settings.Secure.WIFI_NUM_ALLOWED_CHANNELS,
1556 numChannels);
1557 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001558 mWifiStateTracker.setNumAllowedChannels(numChannels);
1559 return true;
1560 }
1561
1562 /**
1563 * Return the number of frequency channels that are allowed
1564 * to be used in the current regulatory domain.
1565 * @return the number of allowed channels, or {@code -1} if an error occurs
1566 */
1567 public int getNumAllowedChannels() {
1568 int numChannels;
1569
1570 enforceAccessPermission();
Irfan Sheriffa8fbe1f2010-03-09 09:13:58 -08001571
1572 /*
1573 * If we can't get the value from the driver (e.g., because
1574 * Wi-Fi is not currently enabled), get the value from
1575 * Settings.
1576 */
1577 numChannels = mWifiStateTracker.getNumAllowedChannels();
1578 if (numChannels < 0) {
1579 numChannels = Settings.Secure.getInt(mContext.getContentResolver(),
1580 Settings.Secure.WIFI_NUM_ALLOWED_CHANNELS,
1581 -1);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001582 }
1583 return numChannels;
1584 }
1585
1586 /**
1587 * Return the list of valid values for the number of allowed radio channels
1588 * for various regulatory domains.
1589 * @return the list of channel counts
1590 */
1591 public int[] getValidChannelCounts() {
1592 enforceAccessPermission();
1593 return sValidRegulatoryChannelCounts;
1594 }
1595
1596 /**
1597 * Return the DHCP-assigned addresses from the last successful DHCP request,
1598 * if any.
1599 * @return the DHCP information
1600 */
1601 public DhcpInfo getDhcpInfo() {
1602 enforceAccessPermission();
1603 return mWifiStateTracker.getDhcpInfo();
1604 }
1605
1606 private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
1607 @Override
1608 public void onReceive(Context context, Intent intent) {
1609 String action = intent.getAction();
1610
Doug Zongker43866e02010-01-07 12:09:54 -08001611 long idleMillis =
1612 Settings.Secure.getLong(mContext.getContentResolver(),
1613 Settings.Secure.WIFI_IDLE_MS, DEFAULT_IDLE_MILLIS);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001614 int stayAwakeConditions =
Doug Zongker43866e02010-01-07 12:09:54 -08001615 Settings.System.getInt(mContext.getContentResolver(),
1616 Settings.System.STAY_ON_WHILE_PLUGGED_IN, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001617 if (action.equals(Intent.ACTION_SCREEN_ON)) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001618 Slog.d(TAG, "ACTION_SCREEN_ON");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001619 mAlarmManager.cancel(mIdleIntent);
1620 mDeviceIdle = false;
1621 mScreenOff = false;
Mike Lockwoodf32be162009-07-14 17:44:37 -04001622 mWifiStateTracker.enableRssiPolling(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001623 } else if (action.equals(Intent.ACTION_SCREEN_OFF)) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001624 Slog.d(TAG, "ACTION_SCREEN_OFF");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001625 mScreenOff = true;
Mike Lockwoodf32be162009-07-14 17:44:37 -04001626 mWifiStateTracker.enableRssiPolling(false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001627 /*
1628 * Set a timer to put Wi-Fi to sleep, but only if the screen is off
1629 * AND the "stay on while plugged in" setting doesn't match the
1630 * current power conditions (i.e, not plugged in, plugged in to USB,
1631 * or plugged in to AC).
1632 */
1633 if (!shouldWifiStayAwake(stayAwakeConditions, mPluggedType)) {
San Mehatfa6c7112009-07-07 09:34:44 -07001634 WifiInfo info = mWifiStateTracker.requestConnectionInfo();
1635 if (info.getSupplicantState() != SupplicantState.COMPLETED) {
Robert Greenwalt84612ea62009-09-30 09:04:22 -07001636 // we used to go to sleep immediately, but this caused some race conditions
1637 // we don't have time to track down for this release. Delay instead, but not
1638 // as long as we would if connected (below)
1639 // TODO - fix the race conditions and switch back to the immediate turn-off
1640 long triggerTime = System.currentTimeMillis() + (2*60*1000); // 2 min
Joe Onorato8a9b2202010-02-26 18:56:32 -08001641 Slog.d(TAG, "setting ACTION_DEVICE_IDLE timer for 120,000 ms");
Robert Greenwalt84612ea62009-09-30 09:04:22 -07001642 mAlarmManager.set(AlarmManager.RTC_WAKEUP, triggerTime, mIdleIntent);
1643 // // do not keep Wifi awake when screen is off if Wifi is not associated
1644 // mDeviceIdle = true;
1645 // updateWifiState();
Mike Lockwoodd9c32bc2009-05-18 14:14:15 -04001646 } else {
1647 long triggerTime = System.currentTimeMillis() + idleMillis;
Joe Onorato8a9b2202010-02-26 18:56:32 -08001648 Slog.d(TAG, "setting ACTION_DEVICE_IDLE timer for " + idleMillis + "ms");
Mike Lockwoodd9c32bc2009-05-18 14:14:15 -04001649 mAlarmManager.set(AlarmManager.RTC_WAKEUP, triggerTime, mIdleIntent);
1650 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001651 }
1652 /* we can return now -- there's nothing to do until we get the idle intent back */
1653 return;
1654 } else if (action.equals(ACTION_DEVICE_IDLE)) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001655 Slog.d(TAG, "got ACTION_DEVICE_IDLE");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001656 mDeviceIdle = true;
1657 } else if (action.equals(Intent.ACTION_BATTERY_CHANGED)) {
1658 /*
1659 * Set a timer to put Wi-Fi to sleep, but only if the screen is off
1660 * AND we are transitioning from a state in which the device was supposed
1661 * to stay awake to a state in which it is not supposed to stay awake.
1662 * If "stay awake" state is not changing, we do nothing, to avoid resetting
1663 * the already-set timer.
1664 */
1665 int pluggedType = intent.getIntExtra("plugged", 0);
Joe Onorato8a9b2202010-02-26 18:56:32 -08001666 Slog.d(TAG, "ACTION_BATTERY_CHANGED pluggedType: " + pluggedType);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001667 if (mScreenOff && shouldWifiStayAwake(stayAwakeConditions, mPluggedType) &&
1668 !shouldWifiStayAwake(stayAwakeConditions, pluggedType)) {
1669 long triggerTime = System.currentTimeMillis() + idleMillis;
Joe Onorato8a9b2202010-02-26 18:56:32 -08001670 Slog.d(TAG, "setting ACTION_DEVICE_IDLE timer for " + idleMillis + "ms");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001671 mAlarmManager.set(AlarmManager.RTC_WAKEUP, triggerTime, mIdleIntent);
1672 mPluggedType = pluggedType;
1673 return;
1674 }
1675 mPluggedType = pluggedType;
Nick Pelly005b2282009-09-10 10:21:56 -07001676 } else if (action.equals(BluetoothA2dp.ACTION_SINK_STATE_CHANGED)) {
Jaikumar Ganesh084c6652009-12-07 10:58:18 -08001677 BluetoothA2dp a2dp = new BluetoothA2dp(mContext);
1678 Set<BluetoothDevice> sinks = a2dp.getConnectedSinks();
1679 boolean isBluetoothPlaying = false;
1680 for (BluetoothDevice sink : sinks) {
1681 if (a2dp.getSinkState(sink) == BluetoothA2dp.STATE_PLAYING) {
1682 isBluetoothPlaying = true;
1683 }
1684 }
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001685 mWifiStateTracker.setBluetoothScanMode(isBluetoothPlaying);
Jaikumar Ganesh084c6652009-12-07 10:58:18 -08001686
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001687 } else {
1688 return;
1689 }
1690
1691 updateWifiState();
1692 }
1693
1694 /**
1695 * Determines whether the Wi-Fi chipset should stay awake or be put to
1696 * sleep. Looks at the setting for the sleep policy and the current
1697 * conditions.
Jaikumar Ganesh084c6652009-12-07 10:58:18 -08001698 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001699 * @see #shouldDeviceStayAwake(int, int)
1700 */
1701 private boolean shouldWifiStayAwake(int stayAwakeConditions, int pluggedType) {
1702 int wifiSleepPolicy = Settings.System.getInt(mContext.getContentResolver(),
1703 Settings.System.WIFI_SLEEP_POLICY, Settings.System.WIFI_SLEEP_POLICY_DEFAULT);
1704
1705 if (wifiSleepPolicy == Settings.System.WIFI_SLEEP_POLICY_NEVER) {
1706 // Never sleep
1707 return true;
1708 } else if ((wifiSleepPolicy == Settings.System.WIFI_SLEEP_POLICY_NEVER_WHILE_PLUGGED) &&
1709 (pluggedType != 0)) {
1710 // Never sleep while plugged, and we're plugged
1711 return true;
1712 } else {
1713 // Default
1714 return shouldDeviceStayAwake(stayAwakeConditions, pluggedType);
1715 }
1716 }
Jaikumar Ganesh084c6652009-12-07 10:58:18 -08001717
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001718 /**
1719 * Determine whether the bit value corresponding to {@code pluggedType} is set in
1720 * the bit string {@code stayAwakeConditions}. Because a {@code pluggedType} value
1721 * of {@code 0} isn't really a plugged type, but rather an indication that the
1722 * device isn't plugged in at all, there is no bit value corresponding to a
1723 * {@code pluggedType} value of {@code 0}. That is why we shift by
1724 * {@code pluggedType&nbsp;&#8212;&nbsp;1} instead of by {@code pluggedType}.
1725 * @param stayAwakeConditions a bit string specifying which "plugged types" should
1726 * keep the device (and hence Wi-Fi) awake.
1727 * @param pluggedType the type of plug (USB, AC, or none) for which the check is
1728 * being made
1729 * @return {@code true} if {@code pluggedType} indicates that the device is
1730 * supposed to stay awake, {@code false} otherwise.
1731 */
1732 private boolean shouldDeviceStayAwake(int stayAwakeConditions, int pluggedType) {
1733 return (stayAwakeConditions & pluggedType) != 0;
1734 }
1735 };
1736
Dianne Hackborn617f8772009-03-31 15:04:46 -07001737 private void sendEnableMessage(boolean enable, boolean persist, int uid) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001738 Message msg = Message.obtain(mWifiHandler,
1739 (enable ? MESSAGE_ENABLE_WIFI : MESSAGE_DISABLE_WIFI),
Dianne Hackborn617f8772009-03-31 15:04:46 -07001740 (persist ? 1 : 0), uid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001741 msg.sendToTarget();
1742 }
1743
1744 private void sendStartMessage(boolean scanOnlyMode) {
1745 Message.obtain(mWifiHandler, MESSAGE_START_WIFI, scanOnlyMode ? 1 : 0, 0).sendToTarget();
1746 }
1747
Irfan Sheriff5321aef2010-02-12 12:35:59 -08001748 private void sendAccessPointMessage(boolean enable, WifiConfiguration wifiConfig, int uid) {
1749 Message.obtain(mWifiHandler,
1750 (enable ? MESSAGE_START_ACCESS_POINT : MESSAGE_STOP_ACCESS_POINT),
Irfan Sheriff9ab518ad2010-03-12 15:48:17 -08001751 uid, 0, wifiConfig).sendToTarget();
Irfan Sheriff5321aef2010-02-12 12:35:59 -08001752 }
1753
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001754 private void updateWifiState() {
Robert Greenwaltf75aa36fc2009-10-22 17:03:47 -07001755 // send a message so it's all serialized
1756 Message.obtain(mWifiHandler, MESSAGE_UPDATE_STATE, 0, 0).sendToTarget();
1757 }
1758
1759 private void doUpdateWifiState() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001760 boolean wifiEnabled = getPersistedWifiEnabled();
Mike Lockwoodbd5ddf02009-07-29 21:37:14 -07001761 boolean airplaneMode = isAirplaneModeOn() && !mAirplaneModeOverwridden;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001762 boolean lockHeld = mLocks.hasLocks();
1763 int strongestLockMode;
1764 boolean wifiShouldBeEnabled = wifiEnabled && !airplaneMode;
1765 boolean wifiShouldBeStarted = !mDeviceIdle || lockHeld;
1766 if (mDeviceIdle && lockHeld) {
1767 strongestLockMode = mLocks.getStrongestLockMode();
1768 } else {
1769 strongestLockMode = WifiManager.WIFI_MODE_FULL;
1770 }
1771
1772 synchronized (mWifiHandler) {
Irfan Sheriff0f344060092010-03-10 10:05:51 -08001773 if ((mWifiStateTracker.getWifiState() == WIFI_STATE_ENABLING) && !airplaneMode) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001774 return;
1775 }
1776 if (wifiShouldBeEnabled) {
1777 if (wifiShouldBeStarted) {
1778 sWakeLock.acquire();
Dianne Hackborn617f8772009-03-31 15:04:46 -07001779 sendEnableMessage(true, false, mLastEnableUid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001780 sWakeLock.acquire();
1781 sendStartMessage(strongestLockMode == WifiManager.WIFI_MODE_SCAN_ONLY);
1782 } else {
1783 int wakeLockTimeout =
1784 Settings.Secure.getInt(
1785 mContext.getContentResolver(),
1786 Settings.Secure.WIFI_MOBILE_DATA_TRANSITION_WAKELOCK_TIMEOUT_MS,
1787 DEFAULT_WAKELOCK_TIMEOUT);
1788 /*
1789 * The following wakelock is held in order to ensure
1790 * that the connectivity manager has time to fail over
1791 * to the mobile data network. The connectivity manager
1792 * releases it once mobile data connectivity has been
1793 * established. If connectivity cannot be established,
1794 * the wakelock is released after wakeLockTimeout
1795 * milliseconds have elapsed.
1796 */
1797 sDriverStopWakeLock.acquire();
1798 mWifiHandler.sendEmptyMessage(MESSAGE_STOP_WIFI);
1799 mWifiHandler.sendEmptyMessageDelayed(MESSAGE_RELEASE_WAKELOCK, wakeLockTimeout);
1800 }
1801 } else {
1802 sWakeLock.acquire();
Dianne Hackborn617f8772009-03-31 15:04:46 -07001803 sendEnableMessage(false, false, mLastEnableUid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001804 }
1805 }
1806 }
1807
1808 private void registerForBroadcasts() {
1809 IntentFilter intentFilter = new IntentFilter();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001810 intentFilter.addAction(Intent.ACTION_SCREEN_ON);
1811 intentFilter.addAction(Intent.ACTION_SCREEN_OFF);
1812 intentFilter.addAction(Intent.ACTION_BATTERY_CHANGED);
1813 intentFilter.addAction(ACTION_DEVICE_IDLE);
Nick Pelly005b2282009-09-10 10:21:56 -07001814 intentFilter.addAction(BluetoothA2dp.ACTION_SINK_STATE_CHANGED);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001815 mContext.registerReceiver(mReceiver, intentFilter);
1816 }
Jaikumar Ganesh084c6652009-12-07 10:58:18 -08001817
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001818 private boolean isAirplaneSensitive() {
1819 String airplaneModeRadios = Settings.System.getString(mContext.getContentResolver(),
1820 Settings.System.AIRPLANE_MODE_RADIOS);
1821 return airplaneModeRadios == null
1822 || airplaneModeRadios.contains(Settings.System.RADIO_WIFI);
1823 }
1824
Mike Lockwoodbd5ddf02009-07-29 21:37:14 -07001825 private boolean isAirplaneToggleable() {
1826 String toggleableRadios = Settings.System.getString(mContext.getContentResolver(),
1827 Settings.System.AIRPLANE_MODE_TOGGLEABLE_RADIOS);
1828 return toggleableRadios != null
1829 && toggleableRadios.contains(Settings.System.RADIO_WIFI);
1830 }
1831
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001832 /**
1833 * Returns true if Wi-Fi is sensitive to airplane mode, and airplane mode is
1834 * currently on.
1835 * @return {@code true} if airplane mode is on.
1836 */
1837 private boolean isAirplaneModeOn() {
1838 return isAirplaneSensitive() && Settings.System.getInt(mContext.getContentResolver(),
1839 Settings.System.AIRPLANE_MODE_ON, 0) == 1;
1840 }
1841
1842 /**
1843 * Handler that allows posting to the WifiThread.
1844 */
1845 private class WifiHandler extends Handler {
1846 public WifiHandler(Looper looper) {
1847 super(looper);
1848 }
1849
1850 @Override
1851 public void handleMessage(Message msg) {
1852 switch (msg.what) {
1853
1854 case MESSAGE_ENABLE_WIFI:
Irfan Sheriff7b009782010-03-11 16:37:45 -08001855 if (mWifiWatchdogService == null) {
1856 mWifiWatchdogService = new WifiWatchdogService(mContext, mWifiStateTracker);
1857 }
Dianne Hackborn617f8772009-03-31 15:04:46 -07001858 setWifiEnabledBlocking(true, msg.arg1 == 1, msg.arg2);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001859 sWakeLock.release();
1860 break;
1861
1862 case MESSAGE_START_WIFI:
1863 mWifiStateTracker.setScanOnlyMode(msg.arg1 != 0);
1864 mWifiStateTracker.restart();
1865 sWakeLock.release();
1866 break;
1867
Robert Greenwaltf75aa36fc2009-10-22 17:03:47 -07001868 case MESSAGE_UPDATE_STATE:
1869 doUpdateWifiState();
1870 break;
1871
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001872 case MESSAGE_DISABLE_WIFI:
1873 // a non-zero msg.arg1 value means the "enabled" setting
1874 // should be persisted
Dianne Hackborn617f8772009-03-31 15:04:46 -07001875 setWifiEnabledBlocking(false, msg.arg1 == 1, msg.arg2);
Irfan Sheriff7b009782010-03-11 16:37:45 -08001876 if (mWifiWatchdogService != null) {
1877 mWifiWatchdogService.quit();
1878 mWifiWatchdogService = null;
1879 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001880 sWakeLock.release();
1881 break;
1882
1883 case MESSAGE_STOP_WIFI:
1884 mWifiStateTracker.disconnectAndStop();
1885 // don't release wakelock
1886 break;
1887
1888 case MESSAGE_RELEASE_WAKELOCK:
1889 synchronized (sDriverStopWakeLock) {
1890 if (sDriverStopWakeLock.isHeld()) {
1891 sDriverStopWakeLock.release();
1892 }
1893 }
1894 break;
Irfan Sheriff5321aef2010-02-12 12:35:59 -08001895
1896 case MESSAGE_START_ACCESS_POINT:
1897 setWifiApEnabledBlocking(true,
Irfan Sheriff9ab518ad2010-03-12 15:48:17 -08001898 msg.arg1,
Irfan Sheriff5321aef2010-02-12 12:35:59 -08001899 (WifiConfiguration) msg.obj);
Irfan Sheriff80cb5982010-03-19 10:40:18 -07001900 sWakeLock.release();
Irfan Sheriff5321aef2010-02-12 12:35:59 -08001901 break;
1902
1903 case MESSAGE_STOP_ACCESS_POINT:
1904 setWifiApEnabledBlocking(false,
Irfan Sheriff9ab518ad2010-03-12 15:48:17 -08001905 msg.arg1,
Irfan Sheriff5321aef2010-02-12 12:35:59 -08001906 (WifiConfiguration) msg.obj);
1907 sWakeLock.release();
1908 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001909 }
1910 }
1911 }
1912
1913 @Override
1914 protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
1915 if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.DUMP)
1916 != PackageManager.PERMISSION_GRANTED) {
1917 pw.println("Permission Denial: can't dump WifiService from from pid="
1918 + Binder.getCallingPid()
1919 + ", uid=" + Binder.getCallingUid());
1920 return;
1921 }
Irfan Sheriff0f344060092010-03-10 10:05:51 -08001922 pw.println("Wi-Fi is " + stateName(mWifiStateTracker.getWifiState()));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001923 pw.println("Stay-awake conditions: " +
1924 Settings.System.getInt(mContext.getContentResolver(),
1925 Settings.System.STAY_ON_WHILE_PLUGGED_IN, 0));
1926 pw.println();
1927
1928 pw.println("Internal state:");
1929 pw.println(mWifiStateTracker);
1930 pw.println();
1931 pw.println("Latest scan results:");
1932 List<ScanResult> scanResults = mWifiStateTracker.getScanResultsList();
1933 if (scanResults != null && scanResults.size() != 0) {
1934 pw.println(" BSSID Frequency RSSI Flags SSID");
1935 for (ScanResult r : scanResults) {
1936 pw.printf(" %17s %9d %5d %-16s %s%n",
1937 r.BSSID,
1938 r.frequency,
1939 r.level,
1940 r.capabilities,
1941 r.SSID == null ? "" : r.SSID);
1942 }
1943 }
1944 pw.println();
Eric Shienbrood5711fad2009-03-27 20:25:31 -07001945 pw.println("Locks acquired: " + mFullLocksAcquired + " full, " +
1946 mScanLocksAcquired + " scan");
1947 pw.println("Locks released: " + mFullLocksReleased + " full, " +
1948 mScanLocksReleased + " scan");
1949 pw.println();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001950 pw.println("Locks held:");
1951 mLocks.dump(pw);
1952 }
1953
1954 private static String stateName(int wifiState) {
1955 switch (wifiState) {
1956 case WIFI_STATE_DISABLING:
1957 return "disabling";
1958 case WIFI_STATE_DISABLED:
1959 return "disabled";
1960 case WIFI_STATE_ENABLING:
1961 return "enabling";
1962 case WIFI_STATE_ENABLED:
1963 return "enabled";
1964 case WIFI_STATE_UNKNOWN:
1965 return "unknown state";
1966 default:
1967 return "[invalid state]";
1968 }
1969 }
1970
Robert Greenwalt58ff0212009-05-19 15:53:54 -07001971 private class WifiLock extends DeathRecipient {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001972 WifiLock(int lockMode, String tag, IBinder binder) {
Robert Greenwalt5347bd42009-05-13 15:10:16 -07001973 super(lockMode, tag, binder);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001974 }
1975
1976 public void binderDied() {
1977 synchronized (mLocks) {
1978 releaseWifiLockLocked(mBinder);
1979 }
1980 }
1981
1982 public String toString() {
Robert Greenwalt5347bd42009-05-13 15:10:16 -07001983 return "WifiLock{" + mTag + " type=" + mMode + " binder=" + mBinder + "}";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001984 }
1985 }
1986
1987 private class LockList {
1988 private List<WifiLock> mList;
1989
1990 private LockList() {
1991 mList = new ArrayList<WifiLock>();
1992 }
1993
1994 private synchronized boolean hasLocks() {
1995 return !mList.isEmpty();
1996 }
1997
1998 private synchronized int getStrongestLockMode() {
1999 if (mList.isEmpty()) {
2000 return WifiManager.WIFI_MODE_FULL;
2001 }
2002 for (WifiLock l : mList) {
Robert Greenwalt5347bd42009-05-13 15:10:16 -07002003 if (l.mMode == WifiManager.WIFI_MODE_FULL) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002004 return WifiManager.WIFI_MODE_FULL;
2005 }
2006 }
2007 return WifiManager.WIFI_MODE_SCAN_ONLY;
2008 }
2009
2010 private void addLock(WifiLock lock) {
2011 if (findLockByBinder(lock.mBinder) < 0) {
2012 mList.add(lock);
2013 }
2014 }
2015
2016 private WifiLock removeLock(IBinder binder) {
2017 int index = findLockByBinder(binder);
2018 if (index >= 0) {
Suchi Amalapurapufff2fda2009-06-30 21:36:16 -07002019 WifiLock ret = mList.remove(index);
2020 ret.unlinkDeathRecipient();
2021 return ret;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002022 } else {
2023 return null;
2024 }
2025 }
2026
2027 private int findLockByBinder(IBinder binder) {
2028 int size = mList.size();
2029 for (int i = size - 1; i >= 0; i--)
2030 if (mList.get(i).mBinder == binder)
2031 return i;
2032 return -1;
2033 }
2034
2035 private void dump(PrintWriter pw) {
2036 for (WifiLock l : mList) {
2037 pw.print(" ");
2038 pw.println(l);
2039 }
2040 }
2041 }
2042
2043 public boolean acquireWifiLock(IBinder binder, int lockMode, String tag) {
2044 mContext.enforceCallingOrSelfPermission(android.Manifest.permission.WAKE_LOCK, null);
2045 if (lockMode != WifiManager.WIFI_MODE_FULL && lockMode != WifiManager.WIFI_MODE_SCAN_ONLY) {
2046 return false;
2047 }
2048 WifiLock wifiLock = new WifiLock(lockMode, tag, binder);
2049 synchronized (mLocks) {
2050 return acquireWifiLockLocked(wifiLock);
2051 }
2052 }
2053
2054 private boolean acquireWifiLockLocked(WifiLock wifiLock) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002055 Slog.d(TAG, "acquireWifiLockLocked: " + wifiLock);
Robert Greenwaltf1acb2d2009-10-13 08:20:55 -07002056
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002057 mLocks.addLock(wifiLock);
Robert Greenwaltf1acb2d2009-10-13 08:20:55 -07002058
The Android Open Source Project10592532009-03-18 17:39:46 -07002059 int uid = Binder.getCallingUid();
2060 long ident = Binder.clearCallingIdentity();
2061 try {
Robert Greenwalt5347bd42009-05-13 15:10:16 -07002062 switch(wifiLock.mMode) {
Eric Shienbrood5711fad2009-03-27 20:25:31 -07002063 case WifiManager.WIFI_MODE_FULL:
2064 ++mFullLocksAcquired;
2065 mBatteryStats.noteFullWifiLockAcquired(uid);
2066 break;
2067 case WifiManager.WIFI_MODE_SCAN_ONLY:
2068 ++mScanLocksAcquired;
2069 mBatteryStats.noteScanWifiLockAcquired(uid);
2070 break;
The Android Open Source Project10592532009-03-18 17:39:46 -07002071 }
2072 } catch (RemoteException e) {
2073 } finally {
2074 Binder.restoreCallingIdentity(ident);
2075 }
Robert Greenwaltf1acb2d2009-10-13 08:20:55 -07002076
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002077 updateWifiState();
2078 return true;
2079 }
2080
2081 public boolean releaseWifiLock(IBinder lock) {
2082 mContext.enforceCallingOrSelfPermission(android.Manifest.permission.WAKE_LOCK, null);
2083 synchronized (mLocks) {
2084 return releaseWifiLockLocked(lock);
2085 }
2086 }
2087
2088 private boolean releaseWifiLockLocked(IBinder lock) {
Eric Shienbroodd4c5f892009-03-24 18:13:20 -07002089 boolean hadLock;
Robert Greenwaltf1acb2d2009-10-13 08:20:55 -07002090
The Android Open Source Project10592532009-03-18 17:39:46 -07002091 WifiLock wifiLock = mLocks.removeLock(lock);
Robert Greenwaltf1acb2d2009-10-13 08:20:55 -07002092
Joe Onorato8a9b2202010-02-26 18:56:32 -08002093 Slog.d(TAG, "releaseWifiLockLocked: " + wifiLock);
Robert Greenwaltf1acb2d2009-10-13 08:20:55 -07002094
Eric Shienbroodd4c5f892009-03-24 18:13:20 -07002095 hadLock = (wifiLock != null);
2096
2097 if (hadLock) {
2098 int uid = Binder.getCallingUid();
2099 long ident = Binder.clearCallingIdentity();
2100 try {
Robert Greenwalt5347bd42009-05-13 15:10:16 -07002101 switch(wifiLock.mMode) {
Eric Shienbrood5711fad2009-03-27 20:25:31 -07002102 case WifiManager.WIFI_MODE_FULL:
2103 ++mFullLocksReleased;
2104 mBatteryStats.noteFullWifiLockReleased(uid);
2105 break;
2106 case WifiManager.WIFI_MODE_SCAN_ONLY:
2107 ++mScanLocksReleased;
2108 mBatteryStats.noteScanWifiLockReleased(uid);
2109 break;
Eric Shienbroodd4c5f892009-03-24 18:13:20 -07002110 }
2111 } catch (RemoteException e) {
2112 } finally {
2113 Binder.restoreCallingIdentity(ident);
The Android Open Source Project10592532009-03-18 17:39:46 -07002114 }
The Android Open Source Project10592532009-03-18 17:39:46 -07002115 }
Robert Greenwaltf1acb2d2009-10-13 08:20:55 -07002116 // TODO - should this only happen if you hadLock?
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002117 updateWifiState();
Eric Shienbroodd4c5f892009-03-24 18:13:20 -07002118 return hadLock;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002119 }
Robert Greenwalt5347bd42009-05-13 15:10:16 -07002120
Robert Greenwalt58ff0212009-05-19 15:53:54 -07002121 private abstract class DeathRecipient
Robert Greenwalt5347bd42009-05-13 15:10:16 -07002122 implements IBinder.DeathRecipient {
2123 String mTag;
2124 int mMode;
2125 IBinder mBinder;
2126
Robert Greenwalt58ff0212009-05-19 15:53:54 -07002127 DeathRecipient(int mode, String tag, IBinder binder) {
Robert Greenwalt5347bd42009-05-13 15:10:16 -07002128 super();
2129 mTag = tag;
2130 mMode = mode;
2131 mBinder = binder;
2132 try {
2133 mBinder.linkToDeath(this, 0);
2134 } catch (RemoteException e) {
2135 binderDied();
2136 }
2137 }
Suchi Amalapurapufff2fda2009-06-30 21:36:16 -07002138
2139 void unlinkDeathRecipient() {
2140 mBinder.unlinkToDeath(this, 0);
2141 }
Robert Greenwalt5347bd42009-05-13 15:10:16 -07002142 }
2143
Robert Greenwalt58ff0212009-05-19 15:53:54 -07002144 private class Multicaster extends DeathRecipient {
2145 Multicaster(String tag, IBinder binder) {
Robert Greenwalt5347bd42009-05-13 15:10:16 -07002146 super(Binder.getCallingUid(), tag, binder);
2147 }
2148
2149 public void binderDied() {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002150 Slog.e(TAG, "Multicaster binderDied");
Robert Greenwalt5347bd42009-05-13 15:10:16 -07002151 synchronized (mMulticasters) {
2152 int i = mMulticasters.indexOf(this);
2153 if (i != -1) {
2154 removeMulticasterLocked(i, mMode);
2155 }
2156 }
2157 }
2158
2159 public String toString() {
Robert Greenwalt58ff0212009-05-19 15:53:54 -07002160 return "Multicaster{" + mTag + " binder=" + mBinder + "}";
Robert Greenwalt5347bd42009-05-13 15:10:16 -07002161 }
2162
2163 public int getUid() {
2164 return mMode;
2165 }
2166 }
2167
Robert Greenwalte2d155a2009-10-21 14:58:34 -07002168 public void initializeMulticastFiltering() {
2169 enforceMulticastChangePermission();
Irfan Sheriffa8fbe1f2010-03-09 09:13:58 -08002170
Robert Greenwalte2d155a2009-10-21 14:58:34 -07002171 synchronized (mMulticasters) {
2172 // if anybody had requested filters be off, leave off
2173 if (mMulticasters.size() != 0) {
2174 return;
2175 } else {
Irfan Sheriffa8fbe1f2010-03-09 09:13:58 -08002176 mWifiStateTracker.startPacketFiltering();
Robert Greenwalte2d155a2009-10-21 14:58:34 -07002177 }
2178 }
2179 }
2180
Robert Greenwaltfc1b15c2009-05-22 15:09:51 -07002181 public void acquireMulticastLock(IBinder binder, String tag) {
2182 enforceMulticastChangePermission();
Robert Greenwalt5347bd42009-05-13 15:10:16 -07002183
2184 synchronized (mMulticasters) {
2185 mMulticastEnabled++;
Robert Greenwalt58ff0212009-05-19 15:53:54 -07002186 mMulticasters.add(new Multicaster(tag, binder));
Robert Greenwalt5347bd42009-05-13 15:10:16 -07002187 // Note that we could call stopPacketFiltering only when
2188 // our new size == 1 (first call), but this function won't
2189 // be called often and by making the stopPacket call each
2190 // time we're less fragile and self-healing.
Irfan Sheriffa8fbe1f2010-03-09 09:13:58 -08002191 mWifiStateTracker.stopPacketFiltering();
Robert Greenwalt5347bd42009-05-13 15:10:16 -07002192 }
2193
2194 int uid = Binder.getCallingUid();
2195 Long ident = Binder.clearCallingIdentity();
2196 try {
2197 mBatteryStats.noteWifiMulticastEnabled(uid);
2198 } catch (RemoteException e) {
2199 } finally {
2200 Binder.restoreCallingIdentity(ident);
2201 }
2202 }
2203
Robert Greenwaltfc1b15c2009-05-22 15:09:51 -07002204 public void releaseMulticastLock() {
2205 enforceMulticastChangePermission();
Robert Greenwalt5347bd42009-05-13 15:10:16 -07002206
2207 int uid = Binder.getCallingUid();
2208 synchronized (mMulticasters) {
2209 mMulticastDisabled++;
2210 int size = mMulticasters.size();
2211 for (int i = size - 1; i >= 0; i--) {
Robert Greenwalt58ff0212009-05-19 15:53:54 -07002212 Multicaster m = mMulticasters.get(i);
Robert Greenwalt5347bd42009-05-13 15:10:16 -07002213 if ((m != null) && (m.getUid() == uid)) {
2214 removeMulticasterLocked(i, uid);
2215 }
2216 }
2217 }
2218 }
2219
2220 private void removeMulticasterLocked(int i, int uid)
2221 {
Suchi Amalapurapufff2fda2009-06-30 21:36:16 -07002222 Multicaster removed = mMulticasters.remove(i);
Irfan Sheriffa8fbe1f2010-03-09 09:13:58 -08002223
Suchi Amalapurapufff2fda2009-06-30 21:36:16 -07002224 if (removed != null) {
2225 removed.unlinkDeathRecipient();
2226 }
Robert Greenwalt5347bd42009-05-13 15:10:16 -07002227 if (mMulticasters.size() == 0) {
Irfan Sheriffa8fbe1f2010-03-09 09:13:58 -08002228 mWifiStateTracker.startPacketFiltering();
Robert Greenwalt5347bd42009-05-13 15:10:16 -07002229 }
2230
2231 Long ident = Binder.clearCallingIdentity();
2232 try {
2233 mBatteryStats.noteWifiMulticastDisabled(uid);
2234 } catch (RemoteException e) {
2235 } finally {
2236 Binder.restoreCallingIdentity(ident);
2237 }
2238 }
2239
Robert Greenwalt58ff0212009-05-19 15:53:54 -07002240 public boolean isMulticastEnabled() {
Robert Greenwalt5347bd42009-05-13 15:10:16 -07002241 enforceAccessPermission();
2242
2243 synchronized (mMulticasters) {
2244 return (mMulticasters.size() > 0);
2245 }
2246 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002247}