blob: 6fe4c982449e227e0ec9578c07d1a21824b1dea0 [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);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800235
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800236 mContext.registerReceiver(
237 new BroadcastReceiver() {
238 @Override
239 public void onReceive(Context context, Intent intent) {
Mike Lockwoodbd5ddf02009-07-29 21:37:14 -0700240 // clear our flag indicating the user has overwridden airplane mode
241 mAirplaneModeOverwridden = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800242 updateWifiState();
243 }
244 },
245 new IntentFilter(Intent.ACTION_AIRPLANE_MODE_CHANGED));
246
Irfan Sheriff5321aef2010-02-12 12:35:59 -0800247 mContext.registerReceiver(
248 new BroadcastReceiver() {
249 @Override
250 public void onReceive(Context context, Intent intent) {
251
252 ArrayList<String> available = intent.getStringArrayListExtra(
253 ConnectivityManager.EXTRA_AVAILABLE_TETHER);
254 ArrayList<String> active = intent.getStringArrayListExtra(
255 ConnectivityManager.EXTRA_ACTIVE_TETHER);
256 updateTetherState(available, active);
257
258 }
259 },new IntentFilter(ConnectivityManager.ACTION_TETHER_STATE_CHANGED));
Irfan Sheriff7b009782010-03-11 16:37:45 -0800260 }
Irfan Sheriff5321aef2010-02-12 12:35:59 -0800261
Irfan Sheriff7b009782010-03-11 16:37:45 -0800262 /**
263 * Check if Wi-Fi needs to be enabled and start
264 * if needed
265 */
266 public void startWifi() {
267 boolean wifiEnabled = getPersistedWifiEnabled();
Irfan Sheriff7b009782010-03-11 16:37:45 -0800268 Slog.i(TAG, "WifiService starting up with Wi-Fi " +
269 (wifiEnabled ? "enabled" : "disabled"));
Dianne Hackborn617f8772009-03-31 15:04:46 -0700270 setWifiEnabledBlocking(wifiEnabled, false, Process.myUid());
Irfan Sheriff5321aef2010-02-12 12:35:59 -0800271 }
272
273 private void updateTetherState(ArrayList<String> available, ArrayList<String> tethered) {
274
275 boolean wifiTethered = false;
276 boolean wifiAvailable = false;
277
278 IBinder b = ServiceManager.getService(Context.NETWORKMANAGEMENT_SERVICE);
279 INetworkManagementService service = INetworkManagementService.Stub.asInterface(b);
280
281 mCm = (ConnectivityManager)mContext.getSystemService(Context.CONNECTIVITY_SERVICE);
282 mWifiRegexs = mCm.getTetherableWifiRegexs();
283
284 for (String intf : available) {
285 for (String regex : mWifiRegexs) {
286 if (intf.matches(regex)) {
287
288 InterfaceConfiguration ifcg = null;
289 try {
290 ifcg = service.getInterfaceConfig(intf);
291 if (ifcg != null) {
Robert Greenwaltbfb7bfa2010-03-24 16:03:21 -0700292 /* IP/netmask: 192.168.43.1/255.255.255.0 */
293 ifcg.ipAddr = (192 << 24) + (168 << 16) + (43 << 8) + 1;
Irfan Sheriff5321aef2010-02-12 12:35:59 -0800294 ifcg.netmask = (255 << 24) + (255 << 16) + (255 << 8) + 0;
295 ifcg.interfaceFlags = "up";
296
297 service.setInterfaceConfig(intf, ifcg);
298 }
299 } catch (Exception e) {
Irfan Sheriff5321aef2010-02-12 12:35:59 -0800300 Slog.e(TAG, "Error configuring interface " + intf + ", :" + e);
Irfan Sheriff1a543012010-03-17 19:46:32 -0700301 try {
302 nwService.stopAccessPoint();
303 } catch (Exception ee) {
304 Slog.e(TAG, "Could not stop AP, :" + ee);
305 }
Irfan Sheriff9ab518ad2010-03-12 15:48:17 -0800306 setWifiApEnabledState(WIFI_AP_STATE_FAILED, 0, DriverAction.DRIVER_UNLOAD);
Irfan Sheriff5321aef2010-02-12 12:35:59 -0800307 return;
308 }
309
Irfan Sheriff9ab518ad2010-03-12 15:48:17 -0800310 if(mCm.tether(intf) != ConnectivityManager.TETHER_ERROR_NO_ERROR) {
Irfan Sheriff5321aef2010-02-12 12:35:59 -0800311 Slog.e(TAG, "Error tethering "+intf);
312 }
313 break;
314 }
315 }
316 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800317 }
318
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800319 private boolean getPersistedWifiEnabled() {
320 final ContentResolver cr = mContext.getContentResolver();
321 try {
322 return Settings.Secure.getInt(cr, Settings.Secure.WIFI_ON) == 1;
323 } catch (Settings.SettingNotFoundException e) {
324 Settings.Secure.putInt(cr, Settings.Secure.WIFI_ON, 0);
325 return false;
326 }
327 }
328
329 private void persistWifiEnabled(boolean enabled) {
330 final ContentResolver cr = mContext.getContentResolver();
331 Settings.Secure.putInt(cr, Settings.Secure.WIFI_ON, enabled ? 1 : 0);
332 }
333
334 NetworkStateTracker getNetworkStateTracker() {
335 return mWifiStateTracker;
336 }
337
338 /**
339 * see {@link android.net.wifi.WifiManager#pingSupplicant()}
340 * @return {@code true} if the operation succeeds
341 */
342 public boolean pingSupplicant() {
343 enforceChangePermission();
Irfan Sheriffa8fbe1f2010-03-09 09:13:58 -0800344
345 return mWifiStateTracker.ping();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800346 }
347
348 /**
349 * see {@link android.net.wifi.WifiManager#startScan()}
350 * @return {@code true} if the operation succeeds
351 */
Mike Lockwooda5ec95c2009-07-08 17:11:17 -0400352 public boolean startScan(boolean forceActive) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800353 enforceChangePermission();
Irfan Sheriffa8fbe1f2010-03-09 09:13:58 -0800354
355 switch (mWifiStateTracker.getSupplicantState()) {
356 case DISCONNECTED:
357 case INACTIVE:
358 case SCANNING:
359 case DORMANT:
360 break;
361 default:
362 mWifiStateTracker.setScanResultHandling(
363 WifiStateTracker.SUPPL_SCAN_HANDLING_LIST_ONLY);
364 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800365 }
Irfan Sheriffa8fbe1f2010-03-09 09:13:58 -0800366 return mWifiStateTracker.scan(forceActive);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800367 }
368
369 /**
370 * see {@link android.net.wifi.WifiManager#setWifiEnabled(boolean)}
371 * @param enable {@code true} to enable, {@code false} to disable.
372 * @return {@code true} if the enable/disable operation was
373 * started or is already in the queue.
374 */
375 public boolean setWifiEnabled(boolean enable) {
376 enforceChangePermission();
377 if (mWifiHandler == null) return false;
378
379 synchronized (mWifiHandler) {
Robert Greenwalta99f4612009-09-19 18:14:32 -0700380 // caller may not have WAKE_LOCK permission - it's not required here
381 long ident = Binder.clearCallingIdentity();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800382 sWakeLock.acquire();
Robert Greenwalta99f4612009-09-19 18:14:32 -0700383 Binder.restoreCallingIdentity(ident);
384
Dianne Hackborn617f8772009-03-31 15:04:46 -0700385 mLastEnableUid = Binder.getCallingUid();
Mike Lockwoodbd5ddf02009-07-29 21:37:14 -0700386 // set a flag if the user is enabling Wifi while in airplane mode
387 mAirplaneModeOverwridden = (enable && isAirplaneModeOn() && isAirplaneToggleable());
Dianne Hackborn617f8772009-03-31 15:04:46 -0700388 sendEnableMessage(enable, true, Binder.getCallingUid());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800389 }
390
391 return true;
392 }
393
394 /**
395 * Enables/disables Wi-Fi synchronously.
396 * @param enable {@code true} to turn Wi-Fi on, {@code false} to turn it off.
397 * @param persist {@code true} if the setting should be persisted.
Dianne Hackborn617f8772009-03-31 15:04:46 -0700398 * @param uid The UID of the process making the request.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800399 * @return {@code true} if the operation succeeds (or if the existing state
400 * is the same as the requested state)
401 */
Dianne Hackborn617f8772009-03-31 15:04:46 -0700402 private boolean setWifiEnabledBlocking(boolean enable, boolean persist, int uid) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800403 final int eventualWifiState = enable ? WIFI_STATE_ENABLED : WIFI_STATE_DISABLED;
Irfan Sheriff0f344060092010-03-10 10:05:51 -0800404 final int wifiState = mWifiStateTracker.getWifiState();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800405
Irfan Sheriff0f344060092010-03-10 10:05:51 -0800406 if (wifiState == eventualWifiState) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800407 return true;
408 }
Mike Lockwoodbd5ddf02009-07-29 21:37:14 -0700409 if (enable && isAirplaneModeOn() && !mAirplaneModeOverwridden) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800410 return false;
411 }
412
Irfan Sheriffcd770372010-01-08 09:36:04 -0800413 /**
414 * Multiple calls to unregisterReceiver() cause exception and a system crash.
415 * This can happen if a supplicant is lost (or firmware crash occurs) and user indicates
416 * disable wifi at the same time.
417 * Avoid doing a disable when the current Wifi state is UNKNOWN
418 * TODO: Handle driver load fail and supplicant lost as seperate states
419 */
Irfan Sheriff0f344060092010-03-10 10:05:51 -0800420 if ((wifiState == WIFI_STATE_UNKNOWN) && !enable) {
Irfan Sheriffcd770372010-01-08 09:36:04 -0800421 return false;
422 }
423
Irfan Sherifff91444c2010-03-24 12:11:00 -0700424 /**
425 * Fail Wifi if AP is enabled
426 * TODO: Deprecate WIFI_STATE_UNKNOWN and rename it
427 * WIFI_STATE_FAILED
428 */
Irfan Sheriff5321aef2010-02-12 12:35:59 -0800429 if ((mWifiApState == WIFI_AP_STATE_ENABLED) && enable) {
Irfan Sherifff91444c2010-03-24 12:11:00 -0700430 setWifiEnabledState(WIFI_STATE_UNKNOWN, uid);
431 return false;
Irfan Sheriff5321aef2010-02-12 12:35:59 -0800432 }
433
Irfan Sherifff91444c2010-03-24 12:11:00 -0700434 setWifiEnabledState(enable ? WIFI_STATE_ENABLING : WIFI_STATE_DISABLING, uid);
435
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800436 if (enable) {
Irfan Sheriffa8fbe1f2010-03-09 09:13:58 -0800437 if (!mWifiStateTracker.loadDriver()) {
438 Slog.e(TAG, "Failed to load Wi-Fi driver.");
439 setWifiEnabledState(WIFI_STATE_UNKNOWN, uid);
440 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800441 }
Irfan Sheriffa8fbe1f2010-03-09 09:13:58 -0800442 if (!mWifiStateTracker.startSupplicant()) {
443 mWifiStateTracker.unloadDriver();
444 Slog.e(TAG, "Failed to start supplicant daemon.");
445 setWifiEnabledState(WIFI_STATE_UNKNOWN, uid);
446 return false;
447 }
448
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800449 registerForBroadcasts();
450 mWifiStateTracker.startEventLoop();
Irfan Sheriff7b009782010-03-11 16:37:45 -0800451
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800452 } else {
453
454 mContext.unregisterReceiver(mReceiver);
455 // Remove notification (it will no-op if it isn't visible)
456 mWifiStateTracker.setNotificationVisible(false, 0, false, 0);
457
458 boolean failedToStopSupplicantOrUnloadDriver = false;
Irfan Sheriffa8fbe1f2010-03-09 09:13:58 -0800459
460 if (!mWifiStateTracker.stopSupplicant()) {
461 Slog.e(TAG, "Failed to stop supplicant daemon.");
462 setWifiEnabledState(WIFI_STATE_UNKNOWN, uid);
463 failedToStopSupplicantOrUnloadDriver = true;
464 }
465
466 /**
467 * Reset connections and disable interface
468 * before we unload the driver
469 */
470 mWifiStateTracker.resetConnections(true);
471
472 if (!mWifiStateTracker.unloadDriver()) {
473 Slog.e(TAG, "Failed to unload Wi-Fi driver.");
474 if (!failedToStopSupplicantOrUnloadDriver) {
Dianne Hackborn617f8772009-03-31 15:04:46 -0700475 setWifiEnabledState(WIFI_STATE_UNKNOWN, uid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800476 failedToStopSupplicantOrUnloadDriver = true;
477 }
478 }
Irfan Sheriffa8fbe1f2010-03-09 09:13:58 -0800479
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800480 if (failedToStopSupplicantOrUnloadDriver) {
481 return false;
482 }
483 }
484
485 // Success!
486
487 if (persist) {
488 persistWifiEnabled(enable);
489 }
Dianne Hackborn617f8772009-03-31 15:04:46 -0700490 setWifiEnabledState(eventualWifiState, uid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800491 return true;
492 }
493
Dianne Hackborn617f8772009-03-31 15:04:46 -0700494 private void setWifiEnabledState(int wifiState, int uid) {
Irfan Sheriff0f344060092010-03-10 10:05:51 -0800495 final int previousWifiState = mWifiStateTracker.getWifiState();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800496
The Android Open Source Project10592532009-03-18 17:39:46 -0700497 long ident = Binder.clearCallingIdentity();
498 try {
499 if (wifiState == WIFI_STATE_ENABLED) {
Dianne Hackborn617f8772009-03-31 15:04:46 -0700500 mBatteryStats.noteWifiOn(uid);
The Android Open Source Project10592532009-03-18 17:39:46 -0700501 } else if (wifiState == WIFI_STATE_DISABLED) {
Dianne Hackborn617f8772009-03-31 15:04:46 -0700502 mBatteryStats.noteWifiOff(uid);
The Android Open Source Project10592532009-03-18 17:39:46 -0700503 }
504 } catch (RemoteException e) {
505 } finally {
506 Binder.restoreCallingIdentity(ident);
507 }
Jaikumar Ganesh084c6652009-12-07 10:58:18 -0800508
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800509 // Update state
Irfan Sheriff0f344060092010-03-10 10:05:51 -0800510 mWifiStateTracker.setWifiState(wifiState);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800511
512 // Broadcast
513 final Intent intent = new Intent(WifiManager.WIFI_STATE_CHANGED_ACTION);
514 intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY_BEFORE_BOOT);
515 intent.putExtra(WifiManager.EXTRA_WIFI_STATE, wifiState);
516 intent.putExtra(WifiManager.EXTRA_PREVIOUS_WIFI_STATE, previousWifiState);
517 mContext.sendStickyBroadcast(intent);
518 }
519
520 private void enforceAccessPermission() {
521 mContext.enforceCallingOrSelfPermission(android.Manifest.permission.ACCESS_WIFI_STATE,
522 "WifiService");
523 }
524
525 private void enforceChangePermission() {
526 mContext.enforceCallingOrSelfPermission(android.Manifest.permission.CHANGE_WIFI_STATE,
527 "WifiService");
528
529 }
530
Robert Greenwaltfc1b15c2009-05-22 15:09:51 -0700531 private void enforceMulticastChangePermission() {
532 mContext.enforceCallingOrSelfPermission(
533 android.Manifest.permission.CHANGE_WIFI_MULTICAST_STATE,
534 "WifiService");
535 }
536
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800537 /**
538 * see {@link WifiManager#getWifiState()}
539 * @return One of {@link WifiManager#WIFI_STATE_DISABLED},
540 * {@link WifiManager#WIFI_STATE_DISABLING},
541 * {@link WifiManager#WIFI_STATE_ENABLED},
542 * {@link WifiManager#WIFI_STATE_ENABLING},
543 * {@link WifiManager#WIFI_STATE_UNKNOWN}
544 */
545 public int getWifiEnabledState() {
546 enforceAccessPermission();
Irfan Sheriff0f344060092010-03-10 10:05:51 -0800547 return mWifiStateTracker.getWifiState();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800548 }
549
550 /**
551 * see {@link android.net.wifi.WifiManager#disconnect()}
552 * @return {@code true} if the operation succeeds
553 */
554 public boolean disconnect() {
555 enforceChangePermission();
Irfan Sheriffa8fbe1f2010-03-09 09:13:58 -0800556
557 return mWifiStateTracker.disconnect();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800558 }
559
560 /**
561 * see {@link android.net.wifi.WifiManager#reconnect()}
562 * @return {@code true} if the operation succeeds
563 */
564 public boolean reconnect() {
565 enforceChangePermission();
Irfan Sheriffa8fbe1f2010-03-09 09:13:58 -0800566
567 return mWifiStateTracker.reconnectCommand();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800568 }
569
570 /**
571 * see {@link android.net.wifi.WifiManager#reassociate()}
572 * @return {@code true} if the operation succeeds
573 */
574 public boolean reassociate() {
575 enforceChangePermission();
Irfan Sheriffa8fbe1f2010-03-09 09:13:58 -0800576
577 return mWifiStateTracker.reassociate();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800578 }
579
Irfan Sheriff5321aef2010-02-12 12:35:59 -0800580 /**
Irfan Sheriffc2f54c22010-03-18 14:02:22 -0700581 * see {@link android.net.wifi.WifiManager#setWifiApEnabled(WifiConfiguration, boolean)}
Irfan Sheriff5321aef2010-02-12 12:35:59 -0800582 * @param wifiConfig SSID, security and channel details as
583 * part of WifiConfiguration
Irfan Sheriffc2f54c22010-03-18 14:02:22 -0700584 * @param enabled, true to enable and false to disable
Irfan Sheriff5321aef2010-02-12 12:35:59 -0800585 * @return {@code true} if the start operation was
586 * started or is already in the queue.
587 */
588 public boolean setWifiApEnabled(WifiConfiguration wifiConfig, boolean enabled) {
589 enforceChangePermission();
590 if (mWifiHandler == null) return false;
591
592 synchronized (mWifiHandler) {
593
594 long ident = Binder.clearCallingIdentity();
595 sWakeLock.acquire();
596 Binder.restoreCallingIdentity(ident);
597
598 mLastEnableUid = Binder.getCallingUid();
599
600 sendAccessPointMessage(enabled, wifiConfig, Binder.getCallingUid());
601 }
602
603 return true;
604 }
605
Irfan Sheriff9ab518ad2010-03-12 15:48:17 -0800606 public WifiConfiguration getWifiApConfiguration() {
607 final ContentResolver cr = mContext.getContentResolver();
608 WifiConfiguration wifiConfig = new WifiConfiguration();
609 int authType;
610 try {
611 wifiConfig.SSID = Settings.Secure.getString(cr, Settings.Secure.WIFI_AP_SSID);
612 if (wifiConfig.SSID == null)
613 return null;
614 authType = Settings.Secure.getInt(cr, Settings.Secure.WIFI_AP_SECURITY);
615 wifiConfig.allowedKeyManagement.set(authType);
616 wifiConfig.preSharedKey = Settings.Secure.getString(cr, Settings.Secure.WIFI_AP_PASSWD);
617 return wifiConfig;
618 } catch (Settings.SettingNotFoundException e) {
619 Slog.e(TAG,"AP settings not found, returning");
620 return null;
621 }
622 }
623
624 private void persistApConfiguration(WifiConfiguration wifiConfig) {
625 final ContentResolver cr = mContext.getContentResolver();
626 boolean isWpa;
627 if (wifiConfig == null)
628 return;
629 Settings.Secure.putString(cr, Settings.Secure.WIFI_AP_SSID, wifiConfig.SSID);
630 isWpa = wifiConfig.allowedKeyManagement.get(KeyMgmt.WPA_PSK);
631 Settings.Secure.putInt(cr,
632 Settings.Secure.WIFI_AP_SECURITY,
633 isWpa ? KeyMgmt.WPA_PSK : KeyMgmt.NONE);
634 if (isWpa)
635 Settings.Secure.putString(cr, Settings.Secure.WIFI_AP_PASSWD, wifiConfig.preSharedKey);
636 }
637
Irfan Sheriff5321aef2010-02-12 12:35:59 -0800638 /**
639 * Enables/disables Wi-Fi AP synchronously. The driver is loaded
640 * and soft access point configured as a single operation.
641 * @param enable {@code true} to turn Wi-Fi on, {@code false} to turn it off.
Irfan Sheriff5321aef2010-02-12 12:35:59 -0800642 * @param uid The UID of the process making the request.
Irfan Sheriff9ab518ad2010-03-12 15:48:17 -0800643 * @param wifiConfig The WifiConfiguration for AP
Irfan Sheriff5321aef2010-02-12 12:35:59 -0800644 * @return {@code true} if the operation succeeds (or if the existing state
645 * is the same as the requested state)
646 */
Irfan Sheriff5321aef2010-02-12 12:35:59 -0800647 private boolean setWifiApEnabledBlocking(boolean enable,
Irfan Sheriff9ab518ad2010-03-12 15:48:17 -0800648 int uid, WifiConfiguration wifiConfig) {
Irfan Sheriff5321aef2010-02-12 12:35:59 -0800649 final int eventualWifiApState = enable ? WIFI_AP_STATE_ENABLED : WIFI_AP_STATE_DISABLED;
650
651 if (mWifiApState == eventualWifiApState) {
Irfan Sheriff9ab518ad2010-03-12 15:48:17 -0800652 /* Configuration changed on a running access point */
653 if(enable && (wifiConfig != null)) {
654 try {
655 persistApConfiguration(wifiConfig);
Irfan Sheriffc2f54c22010-03-18 14:02:22 -0700656 nwService.setAccessPoint(wifiConfig, mWifiStateTracker.getInterfaceName(),
657 SOFTAP_IFACE);
Irfan Sheriff9ab518ad2010-03-12 15:48:17 -0800658 return true;
659 } catch(Exception e) {
660 Slog.e(TAG, "Exception in nwService during AP restart");
Irfan Sheriffc2f54c22010-03-18 14:02:22 -0700661 try {
662 nwService.stopAccessPoint();
663 } catch (Exception ee) {
664 Slog.e(TAG, "Could not stop AP, :" + ee);
665 }
Irfan Sheriff9ab518ad2010-03-12 15:48:17 -0800666 setWifiApEnabledState(WIFI_AP_STATE_FAILED, uid, DriverAction.DRIVER_UNLOAD);
667 return false;
668 }
669 } else {
670 return true;
671 }
Irfan Sheriff5321aef2010-02-12 12:35:59 -0800672 }
673
Irfan Sherifff91444c2010-03-24 12:11:00 -0700674 /**
675 * Fail AP if Wifi is enabled
676 */
677 if ((mWifiStateTracker.getWifiState() == WIFI_STATE_ENABLED) && enable) {
678 setWifiApEnabledState(WIFI_AP_STATE_FAILED, uid, DriverAction.NO_DRIVER_UNLOAD);
679 return false;
680 }
681
Irfan Sheriff9ab518ad2010-03-12 15:48:17 -0800682 setWifiApEnabledState(enable ? WIFI_AP_STATE_ENABLING :
683 WIFI_AP_STATE_DISABLING, uid, DriverAction.NO_DRIVER_UNLOAD);
Irfan Sheriff5321aef2010-02-12 12:35:59 -0800684
685 if (enable) {
Irfan Sheriff9ab518ad2010-03-12 15:48:17 -0800686
Irfan Sheriff9ab518ad2010-03-12 15:48:17 -0800687 /* Use default config if there is no existing config */
688 if (wifiConfig == null && ((wifiConfig = getWifiApConfiguration()) == null)) {
689 wifiConfig = new WifiConfiguration();
690 wifiConfig.SSID = mContext.getString(R.string.wifi_tether_configure_ssid_default);
691 wifiConfig.allowedKeyManagement.set(KeyMgmt.NONE);
692 }
693 persistApConfiguration(wifiConfig);
694
Irfan Sheriffa8fbe1f2010-03-09 09:13:58 -0800695 if (!mWifiStateTracker.loadDriver()) {
696 Slog.e(TAG, "Failed to load Wi-Fi driver for AP mode");
Irfan Sheriff9ab518ad2010-03-12 15:48:17 -0800697 setWifiApEnabledState(WIFI_AP_STATE_FAILED, uid, DriverAction.NO_DRIVER_UNLOAD);
Irfan Sheriffa8fbe1f2010-03-09 09:13:58 -0800698 return false;
Irfan Sheriff5321aef2010-02-12 12:35:59 -0800699 }
700
701 try {
Irfan Sheriffc2f54c22010-03-18 14:02:22 -0700702 nwService.startAccessPoint(wifiConfig, mWifiStateTracker.getInterfaceName(),
703 SOFTAP_IFACE);
Irfan Sheriff5321aef2010-02-12 12:35:59 -0800704 } catch(Exception e) {
705 Slog.e(TAG, "Exception in startAccessPoint()");
Irfan Sheriff9ab518ad2010-03-12 15:48:17 -0800706 setWifiApEnabledState(WIFI_AP_STATE_FAILED, uid, DriverAction.DRIVER_UNLOAD);
707 return false;
Irfan Sheriff5321aef2010-02-12 12:35:59 -0800708 }
709
710 } else {
711
712 try {
713 nwService.stopAccessPoint();
714 } catch(Exception e) {
715 Slog.e(TAG, "Exception in stopAccessPoint()");
Irfan Sheriff9ab518ad2010-03-12 15:48:17 -0800716 setWifiApEnabledState(WIFI_AP_STATE_FAILED, uid, DriverAction.DRIVER_UNLOAD);
717 return false;
Irfan Sheriff5321aef2010-02-12 12:35:59 -0800718 }
719
Irfan Sheriffa8fbe1f2010-03-09 09:13:58 -0800720 if (!mWifiStateTracker.unloadDriver()) {
721 Slog.e(TAG, "Failed to unload Wi-Fi driver for AP mode");
Irfan Sheriff9ab518ad2010-03-12 15:48:17 -0800722 setWifiApEnabledState(WIFI_AP_STATE_FAILED, uid, DriverAction.NO_DRIVER_UNLOAD);
Irfan Sheriffa8fbe1f2010-03-09 09:13:58 -0800723 return false;
Irfan Sheriff5321aef2010-02-12 12:35:59 -0800724 }
725 }
726
Irfan Sheriff9ab518ad2010-03-12 15:48:17 -0800727 setWifiApEnabledState(eventualWifiApState, uid, DriverAction.NO_DRIVER_UNLOAD);
Irfan Sheriff5321aef2010-02-12 12:35:59 -0800728 return true;
729 }
730
731 /**
732 * see {@link WifiManager#getWifiApState()}
733 * @return One of {@link WifiManager#WIFI_AP_STATE_DISABLED},
734 * {@link WifiManager#WIFI_AP_STATE_DISABLING},
735 * {@link WifiManager#WIFI_AP_STATE_ENABLED},
736 * {@link WifiManager#WIFI_AP_STATE_ENABLING},
737 * {@link WifiManager#WIFI_AP_STATE_FAILED}
738 */
739 public int getWifiApEnabledState() {
740 enforceAccessPermission();
741 return mWifiApState;
742 }
743
Irfan Sheriff9ab518ad2010-03-12 15:48:17 -0800744 private void setWifiApEnabledState(int wifiAPState, int uid, DriverAction flag) {
Irfan Sheriff5321aef2010-02-12 12:35:59 -0800745 final int previousWifiApState = mWifiApState;
746
Irfan Sheriff9ab518ad2010-03-12 15:48:17 -0800747 /**
748 * Unload the driver if going to a failed state
749 */
750 if ((mWifiApState == WIFI_AP_STATE_FAILED) && (flag == DriverAction.DRIVER_UNLOAD)) {
751 mWifiStateTracker.unloadDriver();
752 }
753
Irfan Sheriff5321aef2010-02-12 12:35:59 -0800754 long ident = Binder.clearCallingIdentity();
755 try {
756 if (wifiAPState == WIFI_AP_STATE_ENABLED) {
757 mBatteryStats.noteWifiOn(uid);
758 } else if (wifiAPState == WIFI_AP_STATE_DISABLED) {
759 mBatteryStats.noteWifiOff(uid);
760 }
761 } catch (RemoteException e) {
762 } finally {
763 Binder.restoreCallingIdentity(ident);
764 }
765
766 // Update state
767 mWifiApState = wifiAPState;
768
769 // Broadcast
770 final Intent intent = new Intent(WifiManager.WIFI_AP_STATE_CHANGED_ACTION);
771 intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY_BEFORE_BOOT);
772 intent.putExtra(WifiManager.EXTRA_WIFI_AP_STATE, wifiAPState);
773 intent.putExtra(WifiManager.EXTRA_PREVIOUS_WIFI_AP_STATE, previousWifiApState);
774 mContext.sendStickyBroadcast(intent);
775 }
776
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800777 /**
778 * see {@link android.net.wifi.WifiManager#getConfiguredNetworks()}
779 * @return the list of configured networks
780 */
781 public List<WifiConfiguration> getConfiguredNetworks() {
782 enforceAccessPermission();
783 String listStr;
Irfan Sheriffa8fbe1f2010-03-09 09:13:58 -0800784
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800785 /*
786 * We don't cache the list, because we want to allow
787 * for the possibility that the configuration file
788 * has been modified through some external means,
789 * such as the wpa_cli command line program.
790 */
Irfan Sheriffa8fbe1f2010-03-09 09:13:58 -0800791 listStr = mWifiStateTracker.listNetworks();
792
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800793 List<WifiConfiguration> networks =
794 new ArrayList<WifiConfiguration>();
795 if (listStr == null)
796 return networks;
797
798 String[] lines = listStr.split("\n");
799 // Skip the first line, which is a header
800 for (int i = 1; i < lines.length; i++) {
801 String[] result = lines[i].split("\t");
802 // network-id | ssid | bssid | flags
803 WifiConfiguration config = new WifiConfiguration();
804 try {
805 config.networkId = Integer.parseInt(result[0]);
806 } catch(NumberFormatException e) {
807 continue;
808 }
809 if (result.length > 3) {
810 if (result[3].indexOf("[CURRENT]") != -1)
811 config.status = WifiConfiguration.Status.CURRENT;
812 else if (result[3].indexOf("[DISABLED]") != -1)
813 config.status = WifiConfiguration.Status.DISABLED;
814 else
815 config.status = WifiConfiguration.Status.ENABLED;
Irfan Sheriffa8fbe1f2010-03-09 09:13:58 -0800816 } else {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800817 config.status = WifiConfiguration.Status.ENABLED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800818 }
Irfan Sheriffa8fbe1f2010-03-09 09:13:58 -0800819 readNetworkVariables(config);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800820 networks.add(config);
821 }
822
823 return networks;
824 }
825
826 /**
827 * Read the variables from the supplicant daemon that are needed to
828 * fill in the WifiConfiguration object.
829 * <p/>
830 * The caller must hold the synchronization monitor.
831 * @param config the {@link WifiConfiguration} object to be filled in.
832 */
Irfan Sheriffa8fbe1f2010-03-09 09:13:58 -0800833 private void readNetworkVariables(WifiConfiguration config) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800834
835 int netId = config.networkId;
836 if (netId < 0)
837 return;
838
839 /*
840 * TODO: maybe should have a native method that takes an array of
841 * variable names and returns an array of values. But we'd still
842 * be doing a round trip to the supplicant daemon for each variable.
843 */
844 String value;
845
Irfan Sheriffa8fbe1f2010-03-09 09:13:58 -0800846 value = mWifiStateTracker.getNetworkVariable(netId, WifiConfiguration.ssidVarName);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800847 if (!TextUtils.isEmpty(value)) {
Chung-yih Wanga8d15942009-10-09 11:01:49 +0800848 config.SSID = removeDoubleQuotes(value);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800849 } else {
850 config.SSID = null;
851 }
852
Irfan Sheriffa8fbe1f2010-03-09 09:13:58 -0800853 value = mWifiStateTracker.getNetworkVariable(netId, WifiConfiguration.bssidVarName);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800854 if (!TextUtils.isEmpty(value)) {
855 config.BSSID = value;
856 } else {
857 config.BSSID = null;
858 }
859
Irfan Sheriffa8fbe1f2010-03-09 09:13:58 -0800860 value = mWifiStateTracker.getNetworkVariable(netId, WifiConfiguration.priorityVarName);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800861 config.priority = -1;
862 if (!TextUtils.isEmpty(value)) {
863 try {
864 config.priority = Integer.parseInt(value);
865 } catch (NumberFormatException ignore) {
866 }
867 }
868
Irfan Sheriffa8fbe1f2010-03-09 09:13:58 -0800869 value = mWifiStateTracker.getNetworkVariable(netId, WifiConfiguration.hiddenSSIDVarName);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800870 config.hiddenSSID = false;
871 if (!TextUtils.isEmpty(value)) {
872 try {
873 config.hiddenSSID = Integer.parseInt(value) != 0;
874 } catch (NumberFormatException ignore) {
875 }
876 }
877
Irfan Sheriffa8fbe1f2010-03-09 09:13:58 -0800878 value = mWifiStateTracker.getNetworkVariable(netId, WifiConfiguration.wepTxKeyIdxVarName);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800879 config.wepTxKeyIndex = -1;
880 if (!TextUtils.isEmpty(value)) {
881 try {
882 config.wepTxKeyIndex = Integer.parseInt(value);
883 } catch (NumberFormatException ignore) {
884 }
885 }
886
887 /*
888 * Get up to 4 WEP keys. Note that the actual keys are not passed back,
889 * just a "*" if the key is set, or the null string otherwise.
890 */
891 for (int i = 0; i < 4; i++) {
Irfan Sheriffa8fbe1f2010-03-09 09:13:58 -0800892 value = mWifiStateTracker.getNetworkVariable(netId, WifiConfiguration.wepKeyVarNames[i]);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800893 if (!TextUtils.isEmpty(value)) {
894 config.wepKeys[i] = value;
895 } else {
896 config.wepKeys[i] = null;
897 }
898 }
899
900 /*
901 * Get the private shared key. Note that the actual keys are not passed back,
902 * just a "*" if the key is set, or the null string otherwise.
903 */
Irfan Sheriffa8fbe1f2010-03-09 09:13:58 -0800904 value = mWifiStateTracker.getNetworkVariable(netId, WifiConfiguration.pskVarName);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800905 if (!TextUtils.isEmpty(value)) {
906 config.preSharedKey = value;
907 } else {
908 config.preSharedKey = null;
909 }
910
Irfan Sheriffa8fbe1f2010-03-09 09:13:58 -0800911 value = mWifiStateTracker.getNetworkVariable(config.networkId,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800912 WifiConfiguration.Protocol.varName);
913 if (!TextUtils.isEmpty(value)) {
914 String vals[] = value.split(" ");
915 for (String val : vals) {
916 int index =
917 lookupString(val, WifiConfiguration.Protocol.strings);
918 if (0 <= index) {
919 config.allowedProtocols.set(index);
920 }
921 }
922 }
923
Irfan Sheriffa8fbe1f2010-03-09 09:13:58 -0800924 value = mWifiStateTracker.getNetworkVariable(config.networkId,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800925 WifiConfiguration.KeyMgmt.varName);
926 if (!TextUtils.isEmpty(value)) {
927 String vals[] = value.split(" ");
928 for (String val : vals) {
929 int index =
930 lookupString(val, WifiConfiguration.KeyMgmt.strings);
931 if (0 <= index) {
932 config.allowedKeyManagement.set(index);
933 }
934 }
935 }
936
Irfan Sheriffa8fbe1f2010-03-09 09:13:58 -0800937 value = mWifiStateTracker.getNetworkVariable(config.networkId,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800938 WifiConfiguration.AuthAlgorithm.varName);
939 if (!TextUtils.isEmpty(value)) {
940 String vals[] = value.split(" ");
941 for (String val : vals) {
942 int index =
943 lookupString(val, WifiConfiguration.AuthAlgorithm.strings);
944 if (0 <= index) {
945 config.allowedAuthAlgorithms.set(index);
946 }
947 }
948 }
949
Irfan Sheriffa8fbe1f2010-03-09 09:13:58 -0800950 value = mWifiStateTracker.getNetworkVariable(config.networkId,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800951 WifiConfiguration.PairwiseCipher.varName);
952 if (!TextUtils.isEmpty(value)) {
953 String vals[] = value.split(" ");
954 for (String val : vals) {
955 int index =
956 lookupString(val, WifiConfiguration.PairwiseCipher.strings);
957 if (0 <= index) {
958 config.allowedPairwiseCiphers.set(index);
959 }
960 }
961 }
962
Irfan Sheriffa8fbe1f2010-03-09 09:13:58 -0800963 value = mWifiStateTracker.getNetworkVariable(config.networkId,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800964 WifiConfiguration.GroupCipher.varName);
965 if (!TextUtils.isEmpty(value)) {
966 String vals[] = value.split(" ");
967 for (String val : vals) {
968 int index =
969 lookupString(val, WifiConfiguration.GroupCipher.strings);
970 if (0 <= index) {
971 config.allowedGroupCiphers.set(index);
972 }
973 }
974 }
Chung-yih Wang43374762009-09-16 14:28:42 +0800975
976 for (WifiConfiguration.EnterpriseField field :
977 config.enterpriseFields) {
Irfan Sheriffa8fbe1f2010-03-09 09:13:58 -0800978 value = mWifiStateTracker.getNetworkVariable(netId,
Chung-yih Wang43374762009-09-16 14:28:42 +0800979 field.varName());
980 if (!TextUtils.isEmpty(value)) {
Chung-yih Wanga8d15942009-10-09 11:01:49 +0800981 if (field != config.eap) value = removeDoubleQuotes(value);
Chung-yih Wang43374762009-09-16 14:28:42 +0800982 field.setValue(value);
983 }
984 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800985 }
986
Chung-yih Wanga8d15942009-10-09 11:01:49 +0800987 private static String removeDoubleQuotes(String string) {
988 if (string.length() <= 2) return "";
989 return string.substring(1, string.length() - 1);
990 }
991
992 private static String convertToQuotedString(String string) {
993 return "\"" + string + "\"";
994 }
995
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800996 /**
997 * see {@link android.net.wifi.WifiManager#addOrUpdateNetwork(WifiConfiguration)}
998 * @return the supplicant-assigned identifier for the new or updated
999 * network if the operation succeeds, or {@code -1} if it fails
1000 */
Irfan Sheriff7aac5542009-12-22 21:42:17 -08001001 public int addOrUpdateNetwork(WifiConfiguration config) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001002 enforceChangePermission();
Irfan Sheriffa8fbe1f2010-03-09 09:13:58 -08001003
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001004 /*
1005 * If the supplied networkId is -1, we create a new empty
1006 * network configuration. Otherwise, the networkId should
1007 * refer to an existing configuration.
1008 */
1009 int netId = config.networkId;
1010 boolean newNetwork = netId == -1;
Irfan Sheriff44113ba32010-03-16 14:54:07 -07001011 boolean doReconfig = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001012 // networkId of -1 means we want to create a new network
Irfan Sheriff7aac5542009-12-22 21:42:17 -08001013 synchronized (mWifiStateTracker) {
1014 if (newNetwork) {
Irfan Sheriffa8fbe1f2010-03-09 09:13:58 -08001015 netId = mWifiStateTracker.addNetwork();
Irfan Sheriff7aac5542009-12-22 21:42:17 -08001016 if (netId < 0) {
1017 if (DBG) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001018 Slog.d(TAG, "Failed to add a network!");
Irfan Sheriff7aac5542009-12-22 21:42:17 -08001019 }
1020 return -1;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001021 }
Irfan Sheriff7aac5542009-12-22 21:42:17 -08001022 doReconfig = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001023 }
Irfan Sheriff7aac5542009-12-22 21:42:17 -08001024 mNeedReconfig = mNeedReconfig || doReconfig;
Irfan Sheriffa8fbe1f2010-03-09 09:13:58 -08001025 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001026
Irfan Sheriffa8fbe1f2010-03-09 09:13:58 -08001027 setVariables: {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001028 /*
1029 * Note that if a networkId for a non-existent network
Irfan Sheriffa8fbe1f2010-03-09 09:13:58 -08001030 * was supplied, then the first setNetworkVariable()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001031 * will fail, so we don't bother to make a separate check
1032 * for the validity of the ID up front.
1033 */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001034 if (config.SSID != null &&
Irfan Sheriffa8fbe1f2010-03-09 09:13:58 -08001035 !mWifiStateTracker.setNetworkVariable(
Irfan Sheriff7aac5542009-12-22 21:42:17 -08001036 netId,
1037 WifiConfiguration.ssidVarName,
1038 convertToQuotedString(config.SSID))) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001039 if (DBG) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001040 Slog.d(TAG, "failed to set SSID: "+config.SSID);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001041 }
1042 break setVariables;
1043 }
1044
1045 if (config.BSSID != null &&
Irfan Sheriffa8fbe1f2010-03-09 09:13:58 -08001046 !mWifiStateTracker.setNetworkVariable(
Irfan Sheriff7aac5542009-12-22 21:42:17 -08001047 netId,
1048 WifiConfiguration.bssidVarName,
1049 config.BSSID)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001050 if (DBG) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001051 Slog.d(TAG, "failed to set BSSID: "+config.BSSID);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001052 }
1053 break setVariables;
1054 }
1055
1056 String allowedKeyManagementString =
1057 makeString(config.allowedKeyManagement, WifiConfiguration.KeyMgmt.strings);
1058 if (config.allowedKeyManagement.cardinality() != 0 &&
Irfan Sheriffa8fbe1f2010-03-09 09:13:58 -08001059 !mWifiStateTracker.setNetworkVariable(
Irfan Sheriff7aac5542009-12-22 21:42:17 -08001060 netId,
1061 WifiConfiguration.KeyMgmt.varName,
1062 allowedKeyManagementString)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001063 if (DBG) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001064 Slog.d(TAG, "failed to set key_mgmt: "+
Irfan Sheriff7aac5542009-12-22 21:42:17 -08001065 allowedKeyManagementString);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001066 }
1067 break setVariables;
1068 }
1069
1070 String allowedProtocolsString =
1071 makeString(config.allowedProtocols, WifiConfiguration.Protocol.strings);
1072 if (config.allowedProtocols.cardinality() != 0 &&
Irfan Sheriffa8fbe1f2010-03-09 09:13:58 -08001073 !mWifiStateTracker.setNetworkVariable(
Irfan Sheriff7aac5542009-12-22 21:42:17 -08001074 netId,
1075 WifiConfiguration.Protocol.varName,
1076 allowedProtocolsString)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001077 if (DBG) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001078 Slog.d(TAG, "failed to set proto: "+
Irfan Sheriff7aac5542009-12-22 21:42:17 -08001079 allowedProtocolsString);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001080 }
1081 break setVariables;
1082 }
1083
1084 String allowedAuthAlgorithmsString =
1085 makeString(config.allowedAuthAlgorithms, WifiConfiguration.AuthAlgorithm.strings);
1086 if (config.allowedAuthAlgorithms.cardinality() != 0 &&
Irfan Sheriffa8fbe1f2010-03-09 09:13:58 -08001087 !mWifiStateTracker.setNetworkVariable(
Irfan Sheriff7aac5542009-12-22 21:42:17 -08001088 netId,
1089 WifiConfiguration.AuthAlgorithm.varName,
1090 allowedAuthAlgorithmsString)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001091 if (DBG) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001092 Slog.d(TAG, "failed to set auth_alg: "+
Irfan Sheriff7aac5542009-12-22 21:42:17 -08001093 allowedAuthAlgorithmsString);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001094 }
1095 break setVariables;
1096 }
1097
1098 String allowedPairwiseCiphersString =
1099 makeString(config.allowedPairwiseCiphers, WifiConfiguration.PairwiseCipher.strings);
1100 if (config.allowedPairwiseCiphers.cardinality() != 0 &&
Irfan Sheriffa8fbe1f2010-03-09 09:13:58 -08001101 !mWifiStateTracker.setNetworkVariable(
Irfan Sheriff7aac5542009-12-22 21:42:17 -08001102 netId,
1103 WifiConfiguration.PairwiseCipher.varName,
1104 allowedPairwiseCiphersString)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001105 if (DBG) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001106 Slog.d(TAG, "failed to set pairwise: "+
Irfan Sheriff7aac5542009-12-22 21:42:17 -08001107 allowedPairwiseCiphersString);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001108 }
1109 break setVariables;
1110 }
1111
1112 String allowedGroupCiphersString =
1113 makeString(config.allowedGroupCiphers, WifiConfiguration.GroupCipher.strings);
1114 if (config.allowedGroupCiphers.cardinality() != 0 &&
Irfan Sheriffa8fbe1f2010-03-09 09:13:58 -08001115 !mWifiStateTracker.setNetworkVariable(
Irfan Sheriff7aac5542009-12-22 21:42:17 -08001116 netId,
1117 WifiConfiguration.GroupCipher.varName,
1118 allowedGroupCiphersString)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001119 if (DBG) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001120 Slog.d(TAG, "failed to set group: "+
Irfan Sheriff7aac5542009-12-22 21:42:17 -08001121 allowedGroupCiphersString);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001122 }
1123 break setVariables;
1124 }
1125
1126 // Prevent client screw-up by passing in a WifiConfiguration we gave it
1127 // by preventing "*" as a key.
1128 if (config.preSharedKey != null && !config.preSharedKey.equals("*") &&
Irfan Sheriffa8fbe1f2010-03-09 09:13:58 -08001129 !mWifiStateTracker.setNetworkVariable(
Irfan Sheriff7aac5542009-12-22 21:42:17 -08001130 netId,
1131 WifiConfiguration.pskVarName,
1132 config.preSharedKey)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001133 if (DBG) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001134 Slog.d(TAG, "failed to set psk: "+config.preSharedKey);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001135 }
1136 break setVariables;
1137 }
1138
1139 boolean hasSetKey = false;
1140 if (config.wepKeys != null) {
1141 for (int i = 0; i < config.wepKeys.length; i++) {
1142 // Prevent client screw-up by passing in a WifiConfiguration we gave it
1143 // by preventing "*" as a key.
1144 if (config.wepKeys[i] != null && !config.wepKeys[i].equals("*")) {
Irfan Sheriffa8fbe1f2010-03-09 09:13:58 -08001145 if (!mWifiStateTracker.setNetworkVariable(
Irfan Sheriff7aac5542009-12-22 21:42:17 -08001146 netId,
1147 WifiConfiguration.wepKeyVarNames[i],
1148 config.wepKeys[i])) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001149 if (DBG) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001150 Slog.d(TAG,
Irfan Sheriff7aac5542009-12-22 21:42:17 -08001151 "failed to set wep_key"+i+": " +
1152 config.wepKeys[i]);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001153 }
1154 break setVariables;
1155 }
1156 hasSetKey = true;
1157 }
1158 }
1159 }
1160
1161 if (hasSetKey) {
Irfan Sheriffa8fbe1f2010-03-09 09:13:58 -08001162 if (!mWifiStateTracker.setNetworkVariable(
Irfan Sheriff7aac5542009-12-22 21:42:17 -08001163 netId,
1164 WifiConfiguration.wepTxKeyIdxVarName,
1165 Integer.toString(config.wepTxKeyIndex))) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001166 if (DBG) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001167 Slog.d(TAG,
Irfan Sheriff7aac5542009-12-22 21:42:17 -08001168 "failed to set wep_tx_keyidx: "+
1169 config.wepTxKeyIndex);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001170 }
1171 break setVariables;
1172 }
1173 }
1174
Irfan Sheriffa8fbe1f2010-03-09 09:13:58 -08001175 if (!mWifiStateTracker.setNetworkVariable(
Irfan Sheriff7aac5542009-12-22 21:42:17 -08001176 netId,
1177 WifiConfiguration.priorityVarName,
1178 Integer.toString(config.priority))) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001179 if (DBG) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001180 Slog.d(TAG, config.SSID + ": failed to set priority: "
Irfan Sheriff7aac5542009-12-22 21:42:17 -08001181 +config.priority);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001182 }
1183 break setVariables;
1184 }
1185
Irfan Sheriffa8fbe1f2010-03-09 09:13:58 -08001186 if (config.hiddenSSID && !mWifiStateTracker.setNetworkVariable(
Irfan Sheriff7aac5542009-12-22 21:42:17 -08001187 netId,
1188 WifiConfiguration.hiddenSSIDVarName,
1189 Integer.toString(config.hiddenSSID ? 1 : 0))) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001190 if (DBG) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001191 Slog.d(TAG, config.SSID + ": failed to set hiddenSSID: "+
Irfan Sheriff7aac5542009-12-22 21:42:17 -08001192 config.hiddenSSID);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001193 }
1194 break setVariables;
1195 }
1196
Chung-yih Wang43374762009-09-16 14:28:42 +08001197 for (WifiConfiguration.EnterpriseField field
1198 : config.enterpriseFields) {
1199 String varName = field.varName();
1200 String value = field.value();
Chung-yih Wanga8d15942009-10-09 11:01:49 +08001201 if (value != null) {
1202 if (field != config.eap) {
Chia-chi Yeh784d53e2010-01-29 16:26:28 +08001203 value = (value.length() == 0) ? "NULL" : convertToQuotedString(value);
Chung-yih Wang43374762009-09-16 14:28:42 +08001204 }
Irfan Sheriffa8fbe1f2010-03-09 09:13:58 -08001205 if (!mWifiStateTracker.setNetworkVariable(
Irfan Sheriff7aac5542009-12-22 21:42:17 -08001206 netId,
1207 varName,
1208 value)) {
Chung-yih Wanga8d15942009-10-09 11:01:49 +08001209 if (DBG) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001210 Slog.d(TAG, config.SSID + ": failed to set " + varName +
Irfan Sheriff7aac5542009-12-22 21:42:17 -08001211 ": " + value);
Chung-yih Wanga8d15942009-10-09 11:01:49 +08001212 }
1213 break setVariables;
1214 }
Chung-yih Wang5069cc72009-06-03 17:33:47 +08001215 }
Chung-yih Wang5069cc72009-06-03 17:33:47 +08001216 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001217 return netId;
Irfan Sheriffa8fbe1f2010-03-09 09:13:58 -08001218 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001219
Irfan Sheriffa8fbe1f2010-03-09 09:13:58 -08001220 /*
1221 * For an update, if one of the setNetworkVariable operations fails,
1222 * we might want to roll back all the changes already made. But the
1223 * chances are that if anything is going to go wrong, it'll happen
1224 * the first time we try to set one of the variables.
1225 */
1226 if (newNetwork) {
1227 removeNetwork(netId);
1228 if (DBG) {
1229 Slog.d(TAG,
1230 "Failed to set a network variable, removed network: "
1231 + netId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001232 }
1233 }
1234 return -1;
1235 }
1236
1237 private static String makeString(BitSet set, String[] strings) {
1238 StringBuffer buf = new StringBuffer();
1239 int nextSetBit = -1;
1240
1241 /* Make sure all set bits are in [0, strings.length) to avoid
1242 * going out of bounds on strings. (Shouldn't happen, but...) */
1243 set = set.get(0, strings.length);
1244
1245 while ((nextSetBit = set.nextSetBit(nextSetBit + 1)) != -1) {
1246 buf.append(strings[nextSetBit].replace('_', '-')).append(' ');
1247 }
1248
1249 // remove trailing space
1250 if (set.cardinality() > 0) {
1251 buf.setLength(buf.length() - 1);
1252 }
1253
1254 return buf.toString();
1255 }
1256
1257 private static int lookupString(String string, String[] strings) {
1258 int size = strings.length;
1259
1260 string = string.replace('-', '_');
1261
1262 for (int i = 0; i < size; i++)
1263 if (string.equals(strings[i]))
1264 return i;
1265
1266 if (DBG) {
1267 // if we ever get here, we should probably add the
1268 // value to WifiConfiguration to reflect that it's
1269 // supported by the WPA supplicant
Joe Onorato8a9b2202010-02-26 18:56:32 -08001270 Slog.w(TAG, "Failed to look-up a string: " + string);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001271 }
1272
1273 return -1;
1274 }
1275
1276 /**
1277 * See {@link android.net.wifi.WifiManager#removeNetwork(int)}
1278 * @param netId the integer that identifies the network configuration
1279 * to the supplicant
1280 * @return {@code true} if the operation succeeded
1281 */
1282 public boolean removeNetwork(int netId) {
1283 enforceChangePermission();
1284
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001285 return mWifiStateTracker.removeNetwork(netId);
1286 }
1287
1288 /**
1289 * See {@link android.net.wifi.WifiManager#enableNetwork(int, boolean)}
1290 * @param netId the integer that identifies the network configuration
1291 * to the supplicant
1292 * @param disableOthers if true, disable all other networks.
1293 * @return {@code true} if the operation succeeded
1294 */
1295 public boolean enableNetwork(int netId, boolean disableOthers) {
1296 enforceChangePermission();
1297
Irfan Sheriffa8fbe1f2010-03-09 09:13:58 -08001298 String ifname = mWifiStateTracker.getInterfaceName();
1299 NetworkUtils.enableInterface(ifname);
1300 boolean result = mWifiStateTracker.enableNetwork(netId, disableOthers);
1301 if (!result) {
1302 NetworkUtils.disableInterface(ifname);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001303 }
Irfan Sheriffa8fbe1f2010-03-09 09:13:58 -08001304 return result;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001305 }
1306
1307 /**
1308 * See {@link android.net.wifi.WifiManager#disableNetwork(int)}
1309 * @param netId the integer that identifies the network configuration
1310 * to the supplicant
1311 * @return {@code true} if the operation succeeded
1312 */
1313 public boolean disableNetwork(int netId) {
1314 enforceChangePermission();
1315
Irfan Sheriffa8fbe1f2010-03-09 09:13:58 -08001316 return mWifiStateTracker.disableNetwork(netId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001317 }
1318
1319 /**
1320 * See {@link android.net.wifi.WifiManager#getConnectionInfo()}
1321 * @return the Wi-Fi information, contained in {@link WifiInfo}.
1322 */
1323 public WifiInfo getConnectionInfo() {
1324 enforceAccessPermission();
1325 /*
1326 * Make sure we have the latest information, by sending
1327 * a status request to the supplicant.
1328 */
1329 return mWifiStateTracker.requestConnectionInfo();
1330 }
1331
1332 /**
1333 * Return the results of the most recent access point scan, in the form of
1334 * a list of {@link ScanResult} objects.
1335 * @return the list of results
1336 */
1337 public List<ScanResult> getScanResults() {
1338 enforceAccessPermission();
1339 String reply;
Irfan Sheriffa8fbe1f2010-03-09 09:13:58 -08001340
1341 reply = mWifiStateTracker.scanResults();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001342 if (reply == null) {
1343 return null;
1344 }
1345
1346 List<ScanResult> scanList = new ArrayList<ScanResult>();
1347
1348 int lineCount = 0;
1349
1350 int replyLen = reply.length();
1351 // Parse the result string, keeping in mind that the last line does
1352 // not end with a newline.
1353 for (int lineBeg = 0, lineEnd = 0; lineEnd <= replyLen; ++lineEnd) {
1354 if (lineEnd == replyLen || reply.charAt(lineEnd) == '\n') {
1355 ++lineCount;
1356 /*
1357 * Skip the first line, which is a header
1358 */
1359 if (lineCount == 1) {
1360 lineBeg = lineEnd + 1;
1361 continue;
1362 }
Mike Lockwoodb30475e2009-04-21 13:55:07 -07001363 if (lineEnd > lineBeg) {
1364 String line = reply.substring(lineBeg, lineEnd);
1365 ScanResult scanResult = parseScanResult(line);
1366 if (scanResult != null) {
1367 scanList.add(scanResult);
1368 } else if (DBG) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001369 Slog.w(TAG, "misformatted scan result for: " + line);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001370 }
1371 }
1372 lineBeg = lineEnd + 1;
1373 }
1374 }
1375 mWifiStateTracker.setScanResultsList(scanList);
1376 return scanList;
1377 }
1378
1379 /**
1380 * Parse the scan result line passed to us by wpa_supplicant (helper).
1381 * @param line the line to parse
1382 * @return the {@link ScanResult} object
1383 */
1384 private ScanResult parseScanResult(String line) {
1385 ScanResult scanResult = null;
1386 if (line != null) {
1387 /*
1388 * Cache implementation (LinkedHashMap) is not synchronized, thus,
1389 * must synchronized here!
1390 */
1391 synchronized (mScanResultCache) {
Mike Lockwoodb30475e2009-04-21 13:55:07 -07001392 String[] result = scanResultPattern.split(line);
1393 if (3 <= result.length && result.length <= 5) {
1394 String bssid = result[0];
1395 // bssid | frequency | level | flags | ssid
1396 int frequency;
1397 int level;
1398 try {
1399 frequency = Integer.parseInt(result[1]);
1400 level = Integer.parseInt(result[2]);
1401 /* some implementations avoid negative values by adding 256
1402 * so we need to adjust for that here.
1403 */
1404 if (level > 0) level -= 256;
1405 } catch (NumberFormatException e) {
1406 frequency = 0;
1407 level = 0;
1408 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001409
Mike Lockwood1a645052009-06-25 13:01:12 -04001410 /*
1411 * The formatting of the results returned by
1412 * wpa_supplicant is intended to make the fields
1413 * line up nicely when printed,
1414 * not to make them easy to parse. So we have to
1415 * apply some heuristics to figure out which field
1416 * is the SSID and which field is the flags.
1417 */
1418 String ssid;
1419 String flags;
1420 if (result.length == 4) {
1421 if (result[3].charAt(0) == '[') {
1422 flags = result[3];
1423 ssid = "";
1424 } else {
1425 flags = "";
1426 ssid = result[3];
1427 }
1428 } else if (result.length == 5) {
1429 flags = result[3];
1430 ssid = result[4];
1431 } else {
1432 // Here, we must have 3 fields: no flags and ssid
1433 // set
1434 flags = "";
1435 ssid = "";
1436 }
1437
Mike Lockwood00717e22009-08-17 10:09:36 -04001438 // bssid + ssid is the hash key
1439 String key = bssid + ssid;
1440 scanResult = mScanResultCache.get(key);
Mike Lockwoodb30475e2009-04-21 13:55:07 -07001441 if (scanResult != null) {
1442 scanResult.level = level;
Mike Lockwood1a645052009-06-25 13:01:12 -04001443 scanResult.SSID = ssid;
1444 scanResult.capabilities = flags;
1445 scanResult.frequency = frequency;
Mike Lockwoodb30475e2009-04-21 13:55:07 -07001446 } else {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001447 // Do not add scan results that have no SSID set
1448 if (0 < ssid.trim().length()) {
1449 scanResult =
1450 new ScanResult(
Mike Lockwoodb30475e2009-04-21 13:55:07 -07001451 ssid, bssid, flags, level, frequency);
Mike Lockwood00717e22009-08-17 10:09:36 -04001452 mScanResultCache.put(key, scanResult);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001453 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001454 }
Mike Lockwoodb30475e2009-04-21 13:55:07 -07001455 } else {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001456 Slog.w(TAG, "Misformatted scan result text with " +
Mike Lockwoodb30475e2009-04-21 13:55:07 -07001457 result.length + " fields: " + line);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001458 }
1459 }
1460 }
1461
1462 return scanResult;
1463 }
1464
1465 /**
1466 * Parse the "flags" field passed back in a scan result by wpa_supplicant,
1467 * and construct a {@code WifiConfiguration} that describes the encryption,
1468 * key management, and authenticaion capabilities of the access point.
1469 * @param flags the string returned by wpa_supplicant
1470 * @return the {@link WifiConfiguration} object, filled in
1471 */
1472 WifiConfiguration parseScanFlags(String flags) {
1473 WifiConfiguration config = new WifiConfiguration();
1474
1475 if (flags.length() == 0) {
1476 config.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE);
1477 }
1478 // ... to be implemented
1479 return config;
1480 }
1481
1482 /**
1483 * Tell the supplicant to persist the current list of configured networks.
1484 * @return {@code true} if the operation succeeded
1485 */
1486 public boolean saveConfiguration() {
1487 boolean result;
1488 enforceChangePermission();
Irfan Sheriffa8fbe1f2010-03-09 09:13:58 -08001489
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001490 synchronized (mWifiStateTracker) {
Irfan Sheriffa8fbe1f2010-03-09 09:13:58 -08001491 result = mWifiStateTracker.saveConfig();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001492 if (result && mNeedReconfig) {
1493 mNeedReconfig = false;
Irfan Sheriffa8fbe1f2010-03-09 09:13:58 -08001494 result = mWifiStateTracker.reloadConfig();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001495
1496 if (result) {
1497 Intent intent = new Intent(WifiManager.NETWORK_IDS_CHANGED_ACTION);
1498 mContext.sendBroadcast(intent);
1499 }
1500 }
1501 }
Amith Yamasani47873e52009-07-02 12:05:32 -07001502 // Inform the backup manager about a data change
1503 IBackupManager ibm = IBackupManager.Stub.asInterface(
1504 ServiceManager.getService(Context.BACKUP_SERVICE));
1505 if (ibm != null) {
1506 try {
1507 ibm.dataChanged("com.android.providers.settings");
1508 } catch (Exception e) {
1509 // Try again later
1510 }
1511 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001512 return result;
1513 }
1514
1515 /**
1516 * Set the number of radio frequency channels that are allowed to be used
1517 * in the current regulatory domain. This method should be used only
1518 * if the correct number of channels cannot be determined automatically
Robert Greenwaltb5010cc2009-05-21 15:11:40 -07001519 * for some reason. If the operation is successful, the new value may be
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001520 * persisted as a Secure setting.
1521 * @param numChannels the number of allowed channels. Must be greater than 0
1522 * and less than or equal to 16.
Robert Greenwaltb5010cc2009-05-21 15:11:40 -07001523 * @param persist {@code true} if the setting should be remembered.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001524 * @return {@code true} if the operation succeeds, {@code false} otherwise, e.g.,
1525 * {@code numChannels} is outside the valid range.
1526 */
Robert Greenwaltb5010cc2009-05-21 15:11:40 -07001527 public boolean setNumAllowedChannels(int numChannels, boolean persist) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001528 Slog.i(TAG, "WifiService trying to setNumAllowed to "+numChannels+
Robert Greenwaltb5010cc2009-05-21 15:11:40 -07001529 " with persist set to "+persist);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001530 enforceChangePermission();
1531 /*
1532 * Validate the argument. We'd like to let the Wi-Fi driver do this,
1533 * but if Wi-Fi isn't currently enabled, that's not possible, and
1534 * we want to persist the setting anyway,so that it will take
1535 * effect when Wi-Fi does become enabled.
1536 */
1537 boolean found = false;
1538 for (int validChan : sValidRegulatoryChannelCounts) {
1539 if (validChan == numChannels) {
1540 found = true;
1541 break;
1542 }
1543 }
1544 if (!found) {
1545 return false;
1546 }
1547
Robert Greenwaltb5010cc2009-05-21 15:11:40 -07001548 if (persist) {
1549 Settings.Secure.putInt(mContext.getContentResolver(),
1550 Settings.Secure.WIFI_NUM_ALLOWED_CHANNELS,
1551 numChannels);
1552 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001553 mWifiStateTracker.setNumAllowedChannels(numChannels);
1554 return true;
1555 }
1556
1557 /**
1558 * Return the number of frequency channels that are allowed
1559 * to be used in the current regulatory domain.
1560 * @return the number of allowed channels, or {@code -1} if an error occurs
1561 */
1562 public int getNumAllowedChannels() {
1563 int numChannels;
1564
1565 enforceAccessPermission();
Irfan Sheriffa8fbe1f2010-03-09 09:13:58 -08001566
1567 /*
1568 * If we can't get the value from the driver (e.g., because
1569 * Wi-Fi is not currently enabled), get the value from
1570 * Settings.
1571 */
1572 numChannels = mWifiStateTracker.getNumAllowedChannels();
1573 if (numChannels < 0) {
1574 numChannels = Settings.Secure.getInt(mContext.getContentResolver(),
1575 Settings.Secure.WIFI_NUM_ALLOWED_CHANNELS,
1576 -1);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001577 }
1578 return numChannels;
1579 }
1580
1581 /**
1582 * Return the list of valid values for the number of allowed radio channels
1583 * for various regulatory domains.
1584 * @return the list of channel counts
1585 */
1586 public int[] getValidChannelCounts() {
1587 enforceAccessPermission();
1588 return sValidRegulatoryChannelCounts;
1589 }
1590
1591 /**
1592 * Return the DHCP-assigned addresses from the last successful DHCP request,
1593 * if any.
1594 * @return the DHCP information
1595 */
1596 public DhcpInfo getDhcpInfo() {
1597 enforceAccessPermission();
1598 return mWifiStateTracker.getDhcpInfo();
1599 }
1600
1601 private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
1602 @Override
1603 public void onReceive(Context context, Intent intent) {
1604 String action = intent.getAction();
1605
Doug Zongker43866e02010-01-07 12:09:54 -08001606 long idleMillis =
1607 Settings.Secure.getLong(mContext.getContentResolver(),
1608 Settings.Secure.WIFI_IDLE_MS, DEFAULT_IDLE_MILLIS);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001609 int stayAwakeConditions =
Doug Zongker43866e02010-01-07 12:09:54 -08001610 Settings.System.getInt(mContext.getContentResolver(),
1611 Settings.System.STAY_ON_WHILE_PLUGGED_IN, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001612 if (action.equals(Intent.ACTION_SCREEN_ON)) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001613 Slog.d(TAG, "ACTION_SCREEN_ON");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001614 mAlarmManager.cancel(mIdleIntent);
1615 mDeviceIdle = false;
1616 mScreenOff = false;
Mike Lockwoodf32be162009-07-14 17:44:37 -04001617 mWifiStateTracker.enableRssiPolling(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001618 } else if (action.equals(Intent.ACTION_SCREEN_OFF)) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001619 Slog.d(TAG, "ACTION_SCREEN_OFF");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001620 mScreenOff = true;
Mike Lockwoodf32be162009-07-14 17:44:37 -04001621 mWifiStateTracker.enableRssiPolling(false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001622 /*
1623 * Set a timer to put Wi-Fi to sleep, but only if the screen is off
1624 * AND the "stay on while plugged in" setting doesn't match the
1625 * current power conditions (i.e, not plugged in, plugged in to USB,
1626 * or plugged in to AC).
1627 */
1628 if (!shouldWifiStayAwake(stayAwakeConditions, mPluggedType)) {
San Mehatfa6c7112009-07-07 09:34:44 -07001629 WifiInfo info = mWifiStateTracker.requestConnectionInfo();
1630 if (info.getSupplicantState() != SupplicantState.COMPLETED) {
Robert Greenwalt84612ea62009-09-30 09:04:22 -07001631 // we used to go to sleep immediately, but this caused some race conditions
1632 // we don't have time to track down for this release. Delay instead, but not
1633 // as long as we would if connected (below)
1634 // TODO - fix the race conditions and switch back to the immediate turn-off
1635 long triggerTime = System.currentTimeMillis() + (2*60*1000); // 2 min
Joe Onorato8a9b2202010-02-26 18:56:32 -08001636 Slog.d(TAG, "setting ACTION_DEVICE_IDLE timer for 120,000 ms");
Robert Greenwalt84612ea62009-09-30 09:04:22 -07001637 mAlarmManager.set(AlarmManager.RTC_WAKEUP, triggerTime, mIdleIntent);
1638 // // do not keep Wifi awake when screen is off if Wifi is not associated
1639 // mDeviceIdle = true;
1640 // updateWifiState();
Mike Lockwoodd9c32bc2009-05-18 14:14:15 -04001641 } else {
1642 long triggerTime = System.currentTimeMillis() + idleMillis;
Joe Onorato8a9b2202010-02-26 18:56:32 -08001643 Slog.d(TAG, "setting ACTION_DEVICE_IDLE timer for " + idleMillis + "ms");
Mike Lockwoodd9c32bc2009-05-18 14:14:15 -04001644 mAlarmManager.set(AlarmManager.RTC_WAKEUP, triggerTime, mIdleIntent);
1645 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001646 }
1647 /* we can return now -- there's nothing to do until we get the idle intent back */
1648 return;
1649 } else if (action.equals(ACTION_DEVICE_IDLE)) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001650 Slog.d(TAG, "got ACTION_DEVICE_IDLE");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001651 mDeviceIdle = true;
1652 } else if (action.equals(Intent.ACTION_BATTERY_CHANGED)) {
1653 /*
1654 * Set a timer to put Wi-Fi to sleep, but only if the screen is off
1655 * AND we are transitioning from a state in which the device was supposed
1656 * to stay awake to a state in which it is not supposed to stay awake.
1657 * If "stay awake" state is not changing, we do nothing, to avoid resetting
1658 * the already-set timer.
1659 */
1660 int pluggedType = intent.getIntExtra("plugged", 0);
Joe Onorato8a9b2202010-02-26 18:56:32 -08001661 Slog.d(TAG, "ACTION_BATTERY_CHANGED pluggedType: " + pluggedType);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001662 if (mScreenOff && shouldWifiStayAwake(stayAwakeConditions, mPluggedType) &&
1663 !shouldWifiStayAwake(stayAwakeConditions, pluggedType)) {
1664 long triggerTime = System.currentTimeMillis() + idleMillis;
Joe Onorato8a9b2202010-02-26 18:56:32 -08001665 Slog.d(TAG, "setting ACTION_DEVICE_IDLE timer for " + idleMillis + "ms");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001666 mAlarmManager.set(AlarmManager.RTC_WAKEUP, triggerTime, mIdleIntent);
1667 mPluggedType = pluggedType;
1668 return;
1669 }
1670 mPluggedType = pluggedType;
Nick Pelly005b2282009-09-10 10:21:56 -07001671 } else if (action.equals(BluetoothA2dp.ACTION_SINK_STATE_CHANGED)) {
Jaikumar Ganesh084c6652009-12-07 10:58:18 -08001672 BluetoothA2dp a2dp = new BluetoothA2dp(mContext);
1673 Set<BluetoothDevice> sinks = a2dp.getConnectedSinks();
1674 boolean isBluetoothPlaying = false;
1675 for (BluetoothDevice sink : sinks) {
1676 if (a2dp.getSinkState(sink) == BluetoothA2dp.STATE_PLAYING) {
1677 isBluetoothPlaying = true;
1678 }
1679 }
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001680 mWifiStateTracker.setBluetoothScanMode(isBluetoothPlaying);
Jaikumar Ganesh084c6652009-12-07 10:58:18 -08001681
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001682 } else {
1683 return;
1684 }
1685
1686 updateWifiState();
1687 }
1688
1689 /**
1690 * Determines whether the Wi-Fi chipset should stay awake or be put to
1691 * sleep. Looks at the setting for the sleep policy and the current
1692 * conditions.
Jaikumar Ganesh084c6652009-12-07 10:58:18 -08001693 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001694 * @see #shouldDeviceStayAwake(int, int)
1695 */
1696 private boolean shouldWifiStayAwake(int stayAwakeConditions, int pluggedType) {
1697 int wifiSleepPolicy = Settings.System.getInt(mContext.getContentResolver(),
1698 Settings.System.WIFI_SLEEP_POLICY, Settings.System.WIFI_SLEEP_POLICY_DEFAULT);
1699
1700 if (wifiSleepPolicy == Settings.System.WIFI_SLEEP_POLICY_NEVER) {
1701 // Never sleep
1702 return true;
1703 } else if ((wifiSleepPolicy == Settings.System.WIFI_SLEEP_POLICY_NEVER_WHILE_PLUGGED) &&
1704 (pluggedType != 0)) {
1705 // Never sleep while plugged, and we're plugged
1706 return true;
1707 } else {
1708 // Default
1709 return shouldDeviceStayAwake(stayAwakeConditions, pluggedType);
1710 }
1711 }
Jaikumar Ganesh084c6652009-12-07 10:58:18 -08001712
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001713 /**
1714 * Determine whether the bit value corresponding to {@code pluggedType} is set in
1715 * the bit string {@code stayAwakeConditions}. Because a {@code pluggedType} value
1716 * of {@code 0} isn't really a plugged type, but rather an indication that the
1717 * device isn't plugged in at all, there is no bit value corresponding to a
1718 * {@code pluggedType} value of {@code 0}. That is why we shift by
1719 * {@code pluggedType&nbsp;&#8212;&nbsp;1} instead of by {@code pluggedType}.
1720 * @param stayAwakeConditions a bit string specifying which "plugged types" should
1721 * keep the device (and hence Wi-Fi) awake.
1722 * @param pluggedType the type of plug (USB, AC, or none) for which the check is
1723 * being made
1724 * @return {@code true} if {@code pluggedType} indicates that the device is
1725 * supposed to stay awake, {@code false} otherwise.
1726 */
1727 private boolean shouldDeviceStayAwake(int stayAwakeConditions, int pluggedType) {
1728 return (stayAwakeConditions & pluggedType) != 0;
1729 }
1730 };
1731
Dianne Hackborn617f8772009-03-31 15:04:46 -07001732 private void sendEnableMessage(boolean enable, boolean persist, int uid) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001733 Message msg = Message.obtain(mWifiHandler,
1734 (enable ? MESSAGE_ENABLE_WIFI : MESSAGE_DISABLE_WIFI),
Dianne Hackborn617f8772009-03-31 15:04:46 -07001735 (persist ? 1 : 0), uid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001736 msg.sendToTarget();
1737 }
1738
1739 private void sendStartMessage(boolean scanOnlyMode) {
1740 Message.obtain(mWifiHandler, MESSAGE_START_WIFI, scanOnlyMode ? 1 : 0, 0).sendToTarget();
1741 }
1742
Irfan Sheriff5321aef2010-02-12 12:35:59 -08001743 private void sendAccessPointMessage(boolean enable, WifiConfiguration wifiConfig, int uid) {
1744 Message.obtain(mWifiHandler,
1745 (enable ? MESSAGE_START_ACCESS_POINT : MESSAGE_STOP_ACCESS_POINT),
Irfan Sheriff9ab518ad2010-03-12 15:48:17 -08001746 uid, 0, wifiConfig).sendToTarget();
Irfan Sheriff5321aef2010-02-12 12:35:59 -08001747 }
1748
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001749 private void updateWifiState() {
Robert Greenwaltf75aa36fc2009-10-22 17:03:47 -07001750 // send a message so it's all serialized
1751 Message.obtain(mWifiHandler, MESSAGE_UPDATE_STATE, 0, 0).sendToTarget();
1752 }
1753
1754 private void doUpdateWifiState() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001755 boolean wifiEnabled = getPersistedWifiEnabled();
Mike Lockwoodbd5ddf02009-07-29 21:37:14 -07001756 boolean airplaneMode = isAirplaneModeOn() && !mAirplaneModeOverwridden;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001757 boolean lockHeld = mLocks.hasLocks();
1758 int strongestLockMode;
1759 boolean wifiShouldBeEnabled = wifiEnabled && !airplaneMode;
1760 boolean wifiShouldBeStarted = !mDeviceIdle || lockHeld;
1761 if (mDeviceIdle && lockHeld) {
1762 strongestLockMode = mLocks.getStrongestLockMode();
1763 } else {
1764 strongestLockMode = WifiManager.WIFI_MODE_FULL;
1765 }
1766
1767 synchronized (mWifiHandler) {
Irfan Sheriff0f344060092010-03-10 10:05:51 -08001768 if ((mWifiStateTracker.getWifiState() == WIFI_STATE_ENABLING) && !airplaneMode) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001769 return;
1770 }
1771 if (wifiShouldBeEnabled) {
1772 if (wifiShouldBeStarted) {
1773 sWakeLock.acquire();
Dianne Hackborn617f8772009-03-31 15:04:46 -07001774 sendEnableMessage(true, false, mLastEnableUid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001775 sWakeLock.acquire();
1776 sendStartMessage(strongestLockMode == WifiManager.WIFI_MODE_SCAN_ONLY);
Irfan Sheriff3bf504d2010-03-23 12:24:33 -07001777 } else if (!mWifiStateTracker.isDriverStopped()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001778 int wakeLockTimeout =
1779 Settings.Secure.getInt(
1780 mContext.getContentResolver(),
1781 Settings.Secure.WIFI_MOBILE_DATA_TRANSITION_WAKELOCK_TIMEOUT_MS,
1782 DEFAULT_WAKELOCK_TIMEOUT);
1783 /*
Irfan Sheriff3bf504d2010-03-23 12:24:33 -07001784 * We are assuming that ConnectivityService can make
1785 * a transition to cellular data within wakeLockTimeout time.
1786 * The wakelock is released by the delayed message.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001787 */
1788 sDriverStopWakeLock.acquire();
1789 mWifiHandler.sendEmptyMessage(MESSAGE_STOP_WIFI);
1790 mWifiHandler.sendEmptyMessageDelayed(MESSAGE_RELEASE_WAKELOCK, wakeLockTimeout);
1791 }
1792 } else {
1793 sWakeLock.acquire();
Dianne Hackborn617f8772009-03-31 15:04:46 -07001794 sendEnableMessage(false, false, mLastEnableUid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001795 }
1796 }
1797 }
1798
1799 private void registerForBroadcasts() {
1800 IntentFilter intentFilter = new IntentFilter();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001801 intentFilter.addAction(Intent.ACTION_SCREEN_ON);
1802 intentFilter.addAction(Intent.ACTION_SCREEN_OFF);
1803 intentFilter.addAction(Intent.ACTION_BATTERY_CHANGED);
1804 intentFilter.addAction(ACTION_DEVICE_IDLE);
Nick Pelly005b2282009-09-10 10:21:56 -07001805 intentFilter.addAction(BluetoothA2dp.ACTION_SINK_STATE_CHANGED);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001806 mContext.registerReceiver(mReceiver, intentFilter);
1807 }
Jaikumar Ganesh084c6652009-12-07 10:58:18 -08001808
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001809 private boolean isAirplaneSensitive() {
1810 String airplaneModeRadios = Settings.System.getString(mContext.getContentResolver(),
1811 Settings.System.AIRPLANE_MODE_RADIOS);
1812 return airplaneModeRadios == null
1813 || airplaneModeRadios.contains(Settings.System.RADIO_WIFI);
1814 }
1815
Mike Lockwoodbd5ddf02009-07-29 21:37:14 -07001816 private boolean isAirplaneToggleable() {
1817 String toggleableRadios = Settings.System.getString(mContext.getContentResolver(),
1818 Settings.System.AIRPLANE_MODE_TOGGLEABLE_RADIOS);
1819 return toggleableRadios != null
1820 && toggleableRadios.contains(Settings.System.RADIO_WIFI);
1821 }
1822
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001823 /**
1824 * Returns true if Wi-Fi is sensitive to airplane mode, and airplane mode is
1825 * currently on.
1826 * @return {@code true} if airplane mode is on.
1827 */
1828 private boolean isAirplaneModeOn() {
1829 return isAirplaneSensitive() && Settings.System.getInt(mContext.getContentResolver(),
1830 Settings.System.AIRPLANE_MODE_ON, 0) == 1;
1831 }
1832
1833 /**
1834 * Handler that allows posting to the WifiThread.
1835 */
1836 private class WifiHandler extends Handler {
1837 public WifiHandler(Looper looper) {
1838 super(looper);
1839 }
1840
1841 @Override
1842 public void handleMessage(Message msg) {
1843 switch (msg.what) {
1844
1845 case MESSAGE_ENABLE_WIFI:
Irfan Sheriff7b009782010-03-11 16:37:45 -08001846 if (mWifiWatchdogService == null) {
1847 mWifiWatchdogService = new WifiWatchdogService(mContext, mWifiStateTracker);
1848 }
Dianne Hackborn617f8772009-03-31 15:04:46 -07001849 setWifiEnabledBlocking(true, msg.arg1 == 1, msg.arg2);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001850 sWakeLock.release();
1851 break;
1852
1853 case MESSAGE_START_WIFI:
1854 mWifiStateTracker.setScanOnlyMode(msg.arg1 != 0);
1855 mWifiStateTracker.restart();
1856 sWakeLock.release();
1857 break;
1858
Robert Greenwaltf75aa36fc2009-10-22 17:03:47 -07001859 case MESSAGE_UPDATE_STATE:
1860 doUpdateWifiState();
1861 break;
1862
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001863 case MESSAGE_DISABLE_WIFI:
1864 // a non-zero msg.arg1 value means the "enabled" setting
1865 // should be persisted
Dianne Hackborn617f8772009-03-31 15:04:46 -07001866 setWifiEnabledBlocking(false, msg.arg1 == 1, msg.arg2);
Irfan Sheriff7b009782010-03-11 16:37:45 -08001867 if (mWifiWatchdogService != null) {
1868 mWifiWatchdogService.quit();
1869 mWifiWatchdogService = null;
1870 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001871 sWakeLock.release();
1872 break;
1873
1874 case MESSAGE_STOP_WIFI:
1875 mWifiStateTracker.disconnectAndStop();
1876 // don't release wakelock
1877 break;
1878
1879 case MESSAGE_RELEASE_WAKELOCK:
Irfan Sheriff3bf504d2010-03-23 12:24:33 -07001880 sDriverStopWakeLock.release();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001881 break;
Irfan Sheriff5321aef2010-02-12 12:35:59 -08001882
1883 case MESSAGE_START_ACCESS_POINT:
1884 setWifiApEnabledBlocking(true,
Irfan Sheriff9ab518ad2010-03-12 15:48:17 -08001885 msg.arg1,
Irfan Sheriff5321aef2010-02-12 12:35:59 -08001886 (WifiConfiguration) msg.obj);
Irfan Sheriff80cb5982010-03-19 10:40:18 -07001887 sWakeLock.release();
Irfan Sheriff5321aef2010-02-12 12:35:59 -08001888 break;
1889
1890 case MESSAGE_STOP_ACCESS_POINT:
1891 setWifiApEnabledBlocking(false,
Irfan Sheriff9ab518ad2010-03-12 15:48:17 -08001892 msg.arg1,
Irfan Sheriff5321aef2010-02-12 12:35:59 -08001893 (WifiConfiguration) msg.obj);
1894 sWakeLock.release();
1895 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001896 }
1897 }
1898 }
1899
1900 @Override
1901 protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
1902 if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.DUMP)
1903 != PackageManager.PERMISSION_GRANTED) {
1904 pw.println("Permission Denial: can't dump WifiService from from pid="
1905 + Binder.getCallingPid()
1906 + ", uid=" + Binder.getCallingUid());
1907 return;
1908 }
Irfan Sheriff0f344060092010-03-10 10:05:51 -08001909 pw.println("Wi-Fi is " + stateName(mWifiStateTracker.getWifiState()));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001910 pw.println("Stay-awake conditions: " +
1911 Settings.System.getInt(mContext.getContentResolver(),
1912 Settings.System.STAY_ON_WHILE_PLUGGED_IN, 0));
1913 pw.println();
1914
1915 pw.println("Internal state:");
1916 pw.println(mWifiStateTracker);
1917 pw.println();
1918 pw.println("Latest scan results:");
1919 List<ScanResult> scanResults = mWifiStateTracker.getScanResultsList();
1920 if (scanResults != null && scanResults.size() != 0) {
1921 pw.println(" BSSID Frequency RSSI Flags SSID");
1922 for (ScanResult r : scanResults) {
1923 pw.printf(" %17s %9d %5d %-16s %s%n",
1924 r.BSSID,
1925 r.frequency,
1926 r.level,
1927 r.capabilities,
1928 r.SSID == null ? "" : r.SSID);
1929 }
1930 }
1931 pw.println();
Eric Shienbrood5711fad2009-03-27 20:25:31 -07001932 pw.println("Locks acquired: " + mFullLocksAcquired + " full, " +
1933 mScanLocksAcquired + " scan");
1934 pw.println("Locks released: " + mFullLocksReleased + " full, " +
1935 mScanLocksReleased + " scan");
1936 pw.println();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001937 pw.println("Locks held:");
1938 mLocks.dump(pw);
1939 }
1940
1941 private static String stateName(int wifiState) {
1942 switch (wifiState) {
1943 case WIFI_STATE_DISABLING:
1944 return "disabling";
1945 case WIFI_STATE_DISABLED:
1946 return "disabled";
1947 case WIFI_STATE_ENABLING:
1948 return "enabling";
1949 case WIFI_STATE_ENABLED:
1950 return "enabled";
1951 case WIFI_STATE_UNKNOWN:
1952 return "unknown state";
1953 default:
1954 return "[invalid state]";
1955 }
1956 }
1957
Robert Greenwalt58ff0212009-05-19 15:53:54 -07001958 private class WifiLock extends DeathRecipient {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001959 WifiLock(int lockMode, String tag, IBinder binder) {
Robert Greenwalt5347bd42009-05-13 15:10:16 -07001960 super(lockMode, tag, binder);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001961 }
1962
1963 public void binderDied() {
1964 synchronized (mLocks) {
1965 releaseWifiLockLocked(mBinder);
1966 }
1967 }
1968
1969 public String toString() {
Robert Greenwalt5347bd42009-05-13 15:10:16 -07001970 return "WifiLock{" + mTag + " type=" + mMode + " binder=" + mBinder + "}";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001971 }
1972 }
1973
1974 private class LockList {
1975 private List<WifiLock> mList;
1976
1977 private LockList() {
1978 mList = new ArrayList<WifiLock>();
1979 }
1980
1981 private synchronized boolean hasLocks() {
1982 return !mList.isEmpty();
1983 }
1984
1985 private synchronized int getStrongestLockMode() {
1986 if (mList.isEmpty()) {
1987 return WifiManager.WIFI_MODE_FULL;
1988 }
1989 for (WifiLock l : mList) {
Robert Greenwalt5347bd42009-05-13 15:10:16 -07001990 if (l.mMode == WifiManager.WIFI_MODE_FULL) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001991 return WifiManager.WIFI_MODE_FULL;
1992 }
1993 }
1994 return WifiManager.WIFI_MODE_SCAN_ONLY;
1995 }
1996
1997 private void addLock(WifiLock lock) {
1998 if (findLockByBinder(lock.mBinder) < 0) {
1999 mList.add(lock);
2000 }
2001 }
2002
2003 private WifiLock removeLock(IBinder binder) {
2004 int index = findLockByBinder(binder);
2005 if (index >= 0) {
Suchi Amalapurapufff2fda2009-06-30 21:36:16 -07002006 WifiLock ret = mList.remove(index);
2007 ret.unlinkDeathRecipient();
2008 return ret;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002009 } else {
2010 return null;
2011 }
2012 }
2013
2014 private int findLockByBinder(IBinder binder) {
2015 int size = mList.size();
2016 for (int i = size - 1; i >= 0; i--)
2017 if (mList.get(i).mBinder == binder)
2018 return i;
2019 return -1;
2020 }
2021
2022 private void dump(PrintWriter pw) {
2023 for (WifiLock l : mList) {
2024 pw.print(" ");
2025 pw.println(l);
2026 }
2027 }
2028 }
2029
2030 public boolean acquireWifiLock(IBinder binder, int lockMode, String tag) {
2031 mContext.enforceCallingOrSelfPermission(android.Manifest.permission.WAKE_LOCK, null);
2032 if (lockMode != WifiManager.WIFI_MODE_FULL && lockMode != WifiManager.WIFI_MODE_SCAN_ONLY) {
2033 return false;
2034 }
2035 WifiLock wifiLock = new WifiLock(lockMode, tag, binder);
2036 synchronized (mLocks) {
2037 return acquireWifiLockLocked(wifiLock);
2038 }
2039 }
2040
2041 private boolean acquireWifiLockLocked(WifiLock wifiLock) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002042 Slog.d(TAG, "acquireWifiLockLocked: " + wifiLock);
Robert Greenwaltf1acb2d2009-10-13 08:20:55 -07002043
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002044 mLocks.addLock(wifiLock);
Robert Greenwaltf1acb2d2009-10-13 08:20:55 -07002045
The Android Open Source Project10592532009-03-18 17:39:46 -07002046 int uid = Binder.getCallingUid();
2047 long ident = Binder.clearCallingIdentity();
2048 try {
Robert Greenwalt5347bd42009-05-13 15:10:16 -07002049 switch(wifiLock.mMode) {
Eric Shienbrood5711fad2009-03-27 20:25:31 -07002050 case WifiManager.WIFI_MODE_FULL:
2051 ++mFullLocksAcquired;
2052 mBatteryStats.noteFullWifiLockAcquired(uid);
2053 break;
2054 case WifiManager.WIFI_MODE_SCAN_ONLY:
2055 ++mScanLocksAcquired;
2056 mBatteryStats.noteScanWifiLockAcquired(uid);
2057 break;
The Android Open Source Project10592532009-03-18 17:39:46 -07002058 }
2059 } catch (RemoteException e) {
2060 } finally {
2061 Binder.restoreCallingIdentity(ident);
2062 }
Robert Greenwaltf1acb2d2009-10-13 08:20:55 -07002063
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002064 updateWifiState();
2065 return true;
2066 }
2067
2068 public boolean releaseWifiLock(IBinder lock) {
2069 mContext.enforceCallingOrSelfPermission(android.Manifest.permission.WAKE_LOCK, null);
2070 synchronized (mLocks) {
2071 return releaseWifiLockLocked(lock);
2072 }
2073 }
2074
2075 private boolean releaseWifiLockLocked(IBinder lock) {
Eric Shienbroodd4c5f892009-03-24 18:13:20 -07002076 boolean hadLock;
Robert Greenwaltf1acb2d2009-10-13 08:20:55 -07002077
The Android Open Source Project10592532009-03-18 17:39:46 -07002078 WifiLock wifiLock = mLocks.removeLock(lock);
Robert Greenwaltf1acb2d2009-10-13 08:20:55 -07002079
Joe Onorato8a9b2202010-02-26 18:56:32 -08002080 Slog.d(TAG, "releaseWifiLockLocked: " + wifiLock);
Robert Greenwaltf1acb2d2009-10-13 08:20:55 -07002081
Eric Shienbroodd4c5f892009-03-24 18:13:20 -07002082 hadLock = (wifiLock != null);
2083
2084 if (hadLock) {
2085 int uid = Binder.getCallingUid();
2086 long ident = Binder.clearCallingIdentity();
2087 try {
Robert Greenwalt5347bd42009-05-13 15:10:16 -07002088 switch(wifiLock.mMode) {
Eric Shienbrood5711fad2009-03-27 20:25:31 -07002089 case WifiManager.WIFI_MODE_FULL:
2090 ++mFullLocksReleased;
2091 mBatteryStats.noteFullWifiLockReleased(uid);
2092 break;
2093 case WifiManager.WIFI_MODE_SCAN_ONLY:
2094 ++mScanLocksReleased;
2095 mBatteryStats.noteScanWifiLockReleased(uid);
2096 break;
Eric Shienbroodd4c5f892009-03-24 18:13:20 -07002097 }
2098 } catch (RemoteException e) {
2099 } finally {
2100 Binder.restoreCallingIdentity(ident);
The Android Open Source Project10592532009-03-18 17:39:46 -07002101 }
The Android Open Source Project10592532009-03-18 17:39:46 -07002102 }
Robert Greenwaltf1acb2d2009-10-13 08:20:55 -07002103 // TODO - should this only happen if you hadLock?
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002104 updateWifiState();
Eric Shienbroodd4c5f892009-03-24 18:13:20 -07002105 return hadLock;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002106 }
Robert Greenwalt5347bd42009-05-13 15:10:16 -07002107
Robert Greenwalt58ff0212009-05-19 15:53:54 -07002108 private abstract class DeathRecipient
Robert Greenwalt5347bd42009-05-13 15:10:16 -07002109 implements IBinder.DeathRecipient {
2110 String mTag;
2111 int mMode;
2112 IBinder mBinder;
2113
Robert Greenwalt58ff0212009-05-19 15:53:54 -07002114 DeathRecipient(int mode, String tag, IBinder binder) {
Robert Greenwalt5347bd42009-05-13 15:10:16 -07002115 super();
2116 mTag = tag;
2117 mMode = mode;
2118 mBinder = binder;
2119 try {
2120 mBinder.linkToDeath(this, 0);
2121 } catch (RemoteException e) {
2122 binderDied();
2123 }
2124 }
Suchi Amalapurapufff2fda2009-06-30 21:36:16 -07002125
2126 void unlinkDeathRecipient() {
2127 mBinder.unlinkToDeath(this, 0);
2128 }
Robert Greenwalt5347bd42009-05-13 15:10:16 -07002129 }
2130
Robert Greenwalt58ff0212009-05-19 15:53:54 -07002131 private class Multicaster extends DeathRecipient {
2132 Multicaster(String tag, IBinder binder) {
Robert Greenwalt5347bd42009-05-13 15:10:16 -07002133 super(Binder.getCallingUid(), tag, binder);
2134 }
2135
2136 public void binderDied() {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002137 Slog.e(TAG, "Multicaster binderDied");
Robert Greenwalt5347bd42009-05-13 15:10:16 -07002138 synchronized (mMulticasters) {
2139 int i = mMulticasters.indexOf(this);
2140 if (i != -1) {
2141 removeMulticasterLocked(i, mMode);
2142 }
2143 }
2144 }
2145
2146 public String toString() {
Robert Greenwalt58ff0212009-05-19 15:53:54 -07002147 return "Multicaster{" + mTag + " binder=" + mBinder + "}";
Robert Greenwalt5347bd42009-05-13 15:10:16 -07002148 }
2149
2150 public int getUid() {
2151 return mMode;
2152 }
2153 }
2154
Robert Greenwalte2d155a2009-10-21 14:58:34 -07002155 public void initializeMulticastFiltering() {
2156 enforceMulticastChangePermission();
Irfan Sheriffa8fbe1f2010-03-09 09:13:58 -08002157
Robert Greenwalte2d155a2009-10-21 14:58:34 -07002158 synchronized (mMulticasters) {
2159 // if anybody had requested filters be off, leave off
2160 if (mMulticasters.size() != 0) {
2161 return;
2162 } else {
Irfan Sheriffa8fbe1f2010-03-09 09:13:58 -08002163 mWifiStateTracker.startPacketFiltering();
Robert Greenwalte2d155a2009-10-21 14:58:34 -07002164 }
2165 }
2166 }
2167
Robert Greenwaltfc1b15c2009-05-22 15:09:51 -07002168 public void acquireMulticastLock(IBinder binder, String tag) {
2169 enforceMulticastChangePermission();
Robert Greenwalt5347bd42009-05-13 15:10:16 -07002170
2171 synchronized (mMulticasters) {
2172 mMulticastEnabled++;
Robert Greenwalt58ff0212009-05-19 15:53:54 -07002173 mMulticasters.add(new Multicaster(tag, binder));
Robert Greenwalt5347bd42009-05-13 15:10:16 -07002174 // Note that we could call stopPacketFiltering only when
2175 // our new size == 1 (first call), but this function won't
2176 // be called often and by making the stopPacket call each
2177 // time we're less fragile and self-healing.
Irfan Sheriffa8fbe1f2010-03-09 09:13:58 -08002178 mWifiStateTracker.stopPacketFiltering();
Robert Greenwalt5347bd42009-05-13 15:10:16 -07002179 }
2180
2181 int uid = Binder.getCallingUid();
2182 Long ident = Binder.clearCallingIdentity();
2183 try {
2184 mBatteryStats.noteWifiMulticastEnabled(uid);
2185 } catch (RemoteException e) {
2186 } finally {
2187 Binder.restoreCallingIdentity(ident);
2188 }
2189 }
2190
Robert Greenwaltfc1b15c2009-05-22 15:09:51 -07002191 public void releaseMulticastLock() {
2192 enforceMulticastChangePermission();
Robert Greenwalt5347bd42009-05-13 15:10:16 -07002193
2194 int uid = Binder.getCallingUid();
2195 synchronized (mMulticasters) {
2196 mMulticastDisabled++;
2197 int size = mMulticasters.size();
2198 for (int i = size - 1; i >= 0; i--) {
Robert Greenwalt58ff0212009-05-19 15:53:54 -07002199 Multicaster m = mMulticasters.get(i);
Robert Greenwalt5347bd42009-05-13 15:10:16 -07002200 if ((m != null) && (m.getUid() == uid)) {
2201 removeMulticasterLocked(i, uid);
2202 }
2203 }
2204 }
2205 }
2206
2207 private void removeMulticasterLocked(int i, int uid)
2208 {
Suchi Amalapurapufff2fda2009-06-30 21:36:16 -07002209 Multicaster removed = mMulticasters.remove(i);
Irfan Sheriffa8fbe1f2010-03-09 09:13:58 -08002210
Suchi Amalapurapufff2fda2009-06-30 21:36:16 -07002211 if (removed != null) {
2212 removed.unlinkDeathRecipient();
2213 }
Robert Greenwalt5347bd42009-05-13 15:10:16 -07002214 if (mMulticasters.size() == 0) {
Irfan Sheriffa8fbe1f2010-03-09 09:13:58 -08002215 mWifiStateTracker.startPacketFiltering();
Robert Greenwalt5347bd42009-05-13 15:10:16 -07002216 }
2217
2218 Long ident = Binder.clearCallingIdentity();
2219 try {
2220 mBatteryStats.noteWifiMulticastDisabled(uid);
2221 } catch (RemoteException e) {
2222 } finally {
2223 Binder.restoreCallingIdentity(ident);
2224 }
2225 }
2226
Robert Greenwalt58ff0212009-05-19 15:53:54 -07002227 public boolean isMulticastEnabled() {
Robert Greenwalt5347bd42009-05-13 15:10:16 -07002228 enforceAccessPermission();
2229
2230 synchronized (mMulticasters) {
2231 return (mMulticasters.size() > 0);
2232 }
2233 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002234}