blob: bfbd76184734ea1ef0241f8d28b48288234592b1 [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) {
Irfan Sheriff9ab518ad2010-03-12 15:48:17 -0800292 /* IP/netmask: 169.254.2.2/255.255.255.0 */
293 ifcg.ipAddr = (169 << 24) + (254 << 16) + (2 << 8) + 2;
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
Dianne Hackborn617f8772009-03-31 15:04:46 -0700424 setWifiEnabledState(enable ? WIFI_STATE_ENABLING : WIFI_STATE_DISABLING, uid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800425
Irfan Sheriff5321aef2010-02-12 12:35:59 -0800426 if ((mWifiApState == WIFI_AP_STATE_ENABLED) && enable) {
Irfan Sheriff9ab518ad2010-03-12 15:48:17 -0800427 setWifiApEnabledBlocking(false, Process.myUid(), null);
Irfan Sheriff5321aef2010-02-12 12:35:59 -0800428 }
429
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800430 if (enable) {
Irfan Sheriffa8fbe1f2010-03-09 09:13:58 -0800431 if (!mWifiStateTracker.loadDriver()) {
432 Slog.e(TAG, "Failed to load Wi-Fi driver.");
433 setWifiEnabledState(WIFI_STATE_UNKNOWN, uid);
434 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800435 }
Irfan Sheriffa8fbe1f2010-03-09 09:13:58 -0800436 if (!mWifiStateTracker.startSupplicant()) {
437 mWifiStateTracker.unloadDriver();
438 Slog.e(TAG, "Failed to start supplicant daemon.");
439 setWifiEnabledState(WIFI_STATE_UNKNOWN, uid);
440 return false;
441 }
442
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800443 registerForBroadcasts();
444 mWifiStateTracker.startEventLoop();
Irfan Sheriff7b009782010-03-11 16:37:45 -0800445
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800446 } else {
447
448 mContext.unregisterReceiver(mReceiver);
449 // Remove notification (it will no-op if it isn't visible)
450 mWifiStateTracker.setNotificationVisible(false, 0, false, 0);
451
452 boolean failedToStopSupplicantOrUnloadDriver = false;
Irfan Sheriffa8fbe1f2010-03-09 09:13:58 -0800453
454 if (!mWifiStateTracker.stopSupplicant()) {
455 Slog.e(TAG, "Failed to stop supplicant daemon.");
456 setWifiEnabledState(WIFI_STATE_UNKNOWN, uid);
457 failedToStopSupplicantOrUnloadDriver = true;
458 }
459
460 /**
461 * Reset connections and disable interface
462 * before we unload the driver
463 */
464 mWifiStateTracker.resetConnections(true);
465
466 if (!mWifiStateTracker.unloadDriver()) {
467 Slog.e(TAG, "Failed to unload Wi-Fi driver.");
468 if (!failedToStopSupplicantOrUnloadDriver) {
Dianne Hackborn617f8772009-03-31 15:04:46 -0700469 setWifiEnabledState(WIFI_STATE_UNKNOWN, uid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800470 failedToStopSupplicantOrUnloadDriver = true;
471 }
472 }
Irfan Sheriffa8fbe1f2010-03-09 09:13:58 -0800473
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800474 if (failedToStopSupplicantOrUnloadDriver) {
475 return false;
476 }
477 }
478
479 // Success!
480
481 if (persist) {
482 persistWifiEnabled(enable);
483 }
Dianne Hackborn617f8772009-03-31 15:04:46 -0700484 setWifiEnabledState(eventualWifiState, uid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800485 return true;
486 }
487
Dianne Hackborn617f8772009-03-31 15:04:46 -0700488 private void setWifiEnabledState(int wifiState, int uid) {
Irfan Sheriff0f344060092010-03-10 10:05:51 -0800489 final int previousWifiState = mWifiStateTracker.getWifiState();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800490
The Android Open Source Project10592532009-03-18 17:39:46 -0700491 long ident = Binder.clearCallingIdentity();
492 try {
493 if (wifiState == WIFI_STATE_ENABLED) {
Dianne Hackborn617f8772009-03-31 15:04:46 -0700494 mBatteryStats.noteWifiOn(uid);
The Android Open Source Project10592532009-03-18 17:39:46 -0700495 } else if (wifiState == WIFI_STATE_DISABLED) {
Dianne Hackborn617f8772009-03-31 15:04:46 -0700496 mBatteryStats.noteWifiOff(uid);
The Android Open Source Project10592532009-03-18 17:39:46 -0700497 }
498 } catch (RemoteException e) {
499 } finally {
500 Binder.restoreCallingIdentity(ident);
501 }
Jaikumar Ganesh084c6652009-12-07 10:58:18 -0800502
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800503 // Update state
Irfan Sheriff0f344060092010-03-10 10:05:51 -0800504 mWifiStateTracker.setWifiState(wifiState);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800505
506 // Broadcast
507 final Intent intent = new Intent(WifiManager.WIFI_STATE_CHANGED_ACTION);
508 intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY_BEFORE_BOOT);
509 intent.putExtra(WifiManager.EXTRA_WIFI_STATE, wifiState);
510 intent.putExtra(WifiManager.EXTRA_PREVIOUS_WIFI_STATE, previousWifiState);
511 mContext.sendStickyBroadcast(intent);
512 }
513
514 private void enforceAccessPermission() {
515 mContext.enforceCallingOrSelfPermission(android.Manifest.permission.ACCESS_WIFI_STATE,
516 "WifiService");
517 }
518
519 private void enforceChangePermission() {
520 mContext.enforceCallingOrSelfPermission(android.Manifest.permission.CHANGE_WIFI_STATE,
521 "WifiService");
522
523 }
524
Robert Greenwaltfc1b15c2009-05-22 15:09:51 -0700525 private void enforceMulticastChangePermission() {
526 mContext.enforceCallingOrSelfPermission(
527 android.Manifest.permission.CHANGE_WIFI_MULTICAST_STATE,
528 "WifiService");
529 }
530
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800531 /**
532 * see {@link WifiManager#getWifiState()}
533 * @return One of {@link WifiManager#WIFI_STATE_DISABLED},
534 * {@link WifiManager#WIFI_STATE_DISABLING},
535 * {@link WifiManager#WIFI_STATE_ENABLED},
536 * {@link WifiManager#WIFI_STATE_ENABLING},
537 * {@link WifiManager#WIFI_STATE_UNKNOWN}
538 */
539 public int getWifiEnabledState() {
540 enforceAccessPermission();
Irfan Sheriff0f344060092010-03-10 10:05:51 -0800541 return mWifiStateTracker.getWifiState();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800542 }
543
544 /**
545 * see {@link android.net.wifi.WifiManager#disconnect()}
546 * @return {@code true} if the operation succeeds
547 */
548 public boolean disconnect() {
549 enforceChangePermission();
Irfan Sheriffa8fbe1f2010-03-09 09:13:58 -0800550
551 return mWifiStateTracker.disconnect();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800552 }
553
554 /**
555 * see {@link android.net.wifi.WifiManager#reconnect()}
556 * @return {@code true} if the operation succeeds
557 */
558 public boolean reconnect() {
559 enforceChangePermission();
Irfan Sheriffa8fbe1f2010-03-09 09:13:58 -0800560
561 return mWifiStateTracker.reconnectCommand();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800562 }
563
564 /**
565 * see {@link android.net.wifi.WifiManager#reassociate()}
566 * @return {@code true} if the operation succeeds
567 */
568 public boolean reassociate() {
569 enforceChangePermission();
Irfan Sheriffa8fbe1f2010-03-09 09:13:58 -0800570
571 return mWifiStateTracker.reassociate();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800572 }
573
Irfan Sheriff5321aef2010-02-12 12:35:59 -0800574 /**
Irfan Sheriffc2f54c22010-03-18 14:02:22 -0700575 * see {@link android.net.wifi.WifiManager#setWifiApEnabled(WifiConfiguration, boolean)}
Irfan Sheriff5321aef2010-02-12 12:35:59 -0800576 * @param wifiConfig SSID, security and channel details as
577 * part of WifiConfiguration
Irfan Sheriffc2f54c22010-03-18 14:02:22 -0700578 * @param enabled, true to enable and false to disable
Irfan Sheriff5321aef2010-02-12 12:35:59 -0800579 * @return {@code true} if the start operation was
580 * started or is already in the queue.
581 */
582 public boolean setWifiApEnabled(WifiConfiguration wifiConfig, boolean enabled) {
583 enforceChangePermission();
584 if (mWifiHandler == null) return false;
585
586 synchronized (mWifiHandler) {
587
588 long ident = Binder.clearCallingIdentity();
589 sWakeLock.acquire();
590 Binder.restoreCallingIdentity(ident);
591
592 mLastEnableUid = Binder.getCallingUid();
593
594 sendAccessPointMessage(enabled, wifiConfig, Binder.getCallingUid());
595 }
596
597 return true;
598 }
599
Irfan Sheriff9ab518ad2010-03-12 15:48:17 -0800600 public WifiConfiguration getWifiApConfiguration() {
601 final ContentResolver cr = mContext.getContentResolver();
602 WifiConfiguration wifiConfig = new WifiConfiguration();
603 int authType;
604 try {
605 wifiConfig.SSID = Settings.Secure.getString(cr, Settings.Secure.WIFI_AP_SSID);
606 if (wifiConfig.SSID == null)
607 return null;
608 authType = Settings.Secure.getInt(cr, Settings.Secure.WIFI_AP_SECURITY);
609 wifiConfig.allowedKeyManagement.set(authType);
610 wifiConfig.preSharedKey = Settings.Secure.getString(cr, Settings.Secure.WIFI_AP_PASSWD);
611 return wifiConfig;
612 } catch (Settings.SettingNotFoundException e) {
613 Slog.e(TAG,"AP settings not found, returning");
614 return null;
615 }
616 }
617
618 private void persistApConfiguration(WifiConfiguration wifiConfig) {
619 final ContentResolver cr = mContext.getContentResolver();
620 boolean isWpa;
621 if (wifiConfig == null)
622 return;
623 Settings.Secure.putString(cr, Settings.Secure.WIFI_AP_SSID, wifiConfig.SSID);
624 isWpa = wifiConfig.allowedKeyManagement.get(KeyMgmt.WPA_PSK);
625 Settings.Secure.putInt(cr,
626 Settings.Secure.WIFI_AP_SECURITY,
627 isWpa ? KeyMgmt.WPA_PSK : KeyMgmt.NONE);
628 if (isWpa)
629 Settings.Secure.putString(cr, Settings.Secure.WIFI_AP_PASSWD, wifiConfig.preSharedKey);
630 }
631
Irfan Sheriff5321aef2010-02-12 12:35:59 -0800632 /**
633 * Enables/disables Wi-Fi AP synchronously. The driver is loaded
634 * and soft access point configured as a single operation.
635 * @param enable {@code true} to turn Wi-Fi on, {@code false} to turn it off.
Irfan Sheriff5321aef2010-02-12 12:35:59 -0800636 * @param uid The UID of the process making the request.
Irfan Sheriff9ab518ad2010-03-12 15:48:17 -0800637 * @param wifiConfig The WifiConfiguration for AP
Irfan Sheriff5321aef2010-02-12 12:35:59 -0800638 * @return {@code true} if the operation succeeds (or if the existing state
639 * is the same as the requested state)
640 */
Irfan Sheriff5321aef2010-02-12 12:35:59 -0800641 private boolean setWifiApEnabledBlocking(boolean enable,
Irfan Sheriff9ab518ad2010-03-12 15:48:17 -0800642 int uid, WifiConfiguration wifiConfig) {
Irfan Sheriff5321aef2010-02-12 12:35:59 -0800643 final int eventualWifiApState = enable ? WIFI_AP_STATE_ENABLED : WIFI_AP_STATE_DISABLED;
644
645 if (mWifiApState == eventualWifiApState) {
Irfan Sheriff9ab518ad2010-03-12 15:48:17 -0800646 /* Configuration changed on a running access point */
647 if(enable && (wifiConfig != null)) {
648 try {
649 persistApConfiguration(wifiConfig);
Irfan Sheriffc2f54c22010-03-18 14:02:22 -0700650 nwService.setAccessPoint(wifiConfig, mWifiStateTracker.getInterfaceName(),
651 SOFTAP_IFACE);
Irfan Sheriff9ab518ad2010-03-12 15:48:17 -0800652 return true;
653 } catch(Exception e) {
654 Slog.e(TAG, "Exception in nwService during AP restart");
Irfan Sheriffc2f54c22010-03-18 14:02:22 -0700655 try {
656 nwService.stopAccessPoint();
657 } catch (Exception ee) {
658 Slog.e(TAG, "Could not stop AP, :" + ee);
659 }
Irfan Sheriff9ab518ad2010-03-12 15:48:17 -0800660 setWifiApEnabledState(WIFI_AP_STATE_FAILED, uid, DriverAction.DRIVER_UNLOAD);
661 return false;
662 }
663 } else {
664 return true;
665 }
Irfan Sheriff5321aef2010-02-12 12:35:59 -0800666 }
667
Irfan Sheriff9ab518ad2010-03-12 15:48:17 -0800668 setWifiApEnabledState(enable ? WIFI_AP_STATE_ENABLING :
669 WIFI_AP_STATE_DISABLING, uid, DriverAction.NO_DRIVER_UNLOAD);
Irfan Sheriff5321aef2010-02-12 12:35:59 -0800670
671 if (enable) {
Irfan Sheriff9ab518ad2010-03-12 15:48:17 -0800672
673 /**
674 * Disable client mode for starting AP
675 */
676 if (mWifiStateTracker.getWifiState() == WIFI_STATE_ENABLED) {
677 setWifiEnabledBlocking(false, true, Process.myUid());
678 }
679
680 /* Use default config if there is no existing config */
681 if (wifiConfig == null && ((wifiConfig = getWifiApConfiguration()) == null)) {
682 wifiConfig = new WifiConfiguration();
683 wifiConfig.SSID = mContext.getString(R.string.wifi_tether_configure_ssid_default);
684 wifiConfig.allowedKeyManagement.set(KeyMgmt.NONE);
685 }
686 persistApConfiguration(wifiConfig);
687
Irfan Sheriffa8fbe1f2010-03-09 09:13:58 -0800688 if (!mWifiStateTracker.loadDriver()) {
689 Slog.e(TAG, "Failed to load Wi-Fi driver for AP mode");
Irfan Sheriff9ab518ad2010-03-12 15:48:17 -0800690 setWifiApEnabledState(WIFI_AP_STATE_FAILED, uid, DriverAction.NO_DRIVER_UNLOAD);
Irfan Sheriffa8fbe1f2010-03-09 09:13:58 -0800691 return false;
Irfan Sheriff5321aef2010-02-12 12:35:59 -0800692 }
693
694 try {
Irfan Sheriffc2f54c22010-03-18 14:02:22 -0700695 nwService.startAccessPoint(wifiConfig, mWifiStateTracker.getInterfaceName(),
696 SOFTAP_IFACE);
Irfan Sheriff5321aef2010-02-12 12:35:59 -0800697 } catch(Exception e) {
698 Slog.e(TAG, "Exception in startAccessPoint()");
Irfan Sheriff9ab518ad2010-03-12 15:48:17 -0800699 setWifiApEnabledState(WIFI_AP_STATE_FAILED, uid, DriverAction.DRIVER_UNLOAD);
700 return false;
Irfan Sheriff5321aef2010-02-12 12:35:59 -0800701 }
702
703 } else {
704
705 try {
706 nwService.stopAccessPoint();
707 } catch(Exception e) {
708 Slog.e(TAG, "Exception in stopAccessPoint()");
Irfan Sheriff9ab518ad2010-03-12 15:48:17 -0800709 setWifiApEnabledState(WIFI_AP_STATE_FAILED, uid, DriverAction.DRIVER_UNLOAD);
710 return false;
Irfan Sheriff5321aef2010-02-12 12:35:59 -0800711 }
712
Irfan Sheriffa8fbe1f2010-03-09 09:13:58 -0800713 if (!mWifiStateTracker.unloadDriver()) {
714 Slog.e(TAG, "Failed to unload Wi-Fi driver for AP mode");
Irfan Sheriff9ab518ad2010-03-12 15:48:17 -0800715 setWifiApEnabledState(WIFI_AP_STATE_FAILED, uid, DriverAction.NO_DRIVER_UNLOAD);
Irfan Sheriffa8fbe1f2010-03-09 09:13:58 -0800716 return false;
Irfan Sheriff5321aef2010-02-12 12:35:59 -0800717 }
718 }
719
Irfan Sheriff9ab518ad2010-03-12 15:48:17 -0800720 setWifiApEnabledState(eventualWifiApState, uid, DriverAction.NO_DRIVER_UNLOAD);
Irfan Sheriff5321aef2010-02-12 12:35:59 -0800721 return true;
722 }
723
724 /**
725 * see {@link WifiManager#getWifiApState()}
726 * @return One of {@link WifiManager#WIFI_AP_STATE_DISABLED},
727 * {@link WifiManager#WIFI_AP_STATE_DISABLING},
728 * {@link WifiManager#WIFI_AP_STATE_ENABLED},
729 * {@link WifiManager#WIFI_AP_STATE_ENABLING},
730 * {@link WifiManager#WIFI_AP_STATE_FAILED}
731 */
732 public int getWifiApEnabledState() {
733 enforceAccessPermission();
734 return mWifiApState;
735 }
736
Irfan Sheriff9ab518ad2010-03-12 15:48:17 -0800737 private void setWifiApEnabledState(int wifiAPState, int uid, DriverAction flag) {
Irfan Sheriff5321aef2010-02-12 12:35:59 -0800738 final int previousWifiApState = mWifiApState;
739
Irfan Sheriff9ab518ad2010-03-12 15:48:17 -0800740 /**
741 * Unload the driver if going to a failed state
742 */
743 if ((mWifiApState == WIFI_AP_STATE_FAILED) && (flag == DriverAction.DRIVER_UNLOAD)) {
744 mWifiStateTracker.unloadDriver();
745 }
746
Irfan Sheriff5321aef2010-02-12 12:35:59 -0800747 long ident = Binder.clearCallingIdentity();
748 try {
749 if (wifiAPState == WIFI_AP_STATE_ENABLED) {
750 mBatteryStats.noteWifiOn(uid);
751 } else if (wifiAPState == WIFI_AP_STATE_DISABLED) {
752 mBatteryStats.noteWifiOff(uid);
753 }
754 } catch (RemoteException e) {
755 } finally {
756 Binder.restoreCallingIdentity(ident);
757 }
758
759 // Update state
760 mWifiApState = wifiAPState;
761
762 // Broadcast
763 final Intent intent = new Intent(WifiManager.WIFI_AP_STATE_CHANGED_ACTION);
764 intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY_BEFORE_BOOT);
765 intent.putExtra(WifiManager.EXTRA_WIFI_AP_STATE, wifiAPState);
766 intent.putExtra(WifiManager.EXTRA_PREVIOUS_WIFI_AP_STATE, previousWifiApState);
767 mContext.sendStickyBroadcast(intent);
768 }
769
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800770 /**
771 * see {@link android.net.wifi.WifiManager#getConfiguredNetworks()}
772 * @return the list of configured networks
773 */
774 public List<WifiConfiguration> getConfiguredNetworks() {
775 enforceAccessPermission();
776 String listStr;
Irfan Sheriffa8fbe1f2010-03-09 09:13:58 -0800777
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800778 /*
779 * We don't cache the list, because we want to allow
780 * for the possibility that the configuration file
781 * has been modified through some external means,
782 * such as the wpa_cli command line program.
783 */
Irfan Sheriffa8fbe1f2010-03-09 09:13:58 -0800784 listStr = mWifiStateTracker.listNetworks();
785
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800786 List<WifiConfiguration> networks =
787 new ArrayList<WifiConfiguration>();
788 if (listStr == null)
789 return networks;
790
791 String[] lines = listStr.split("\n");
792 // Skip the first line, which is a header
793 for (int i = 1; i < lines.length; i++) {
794 String[] result = lines[i].split("\t");
795 // network-id | ssid | bssid | flags
796 WifiConfiguration config = new WifiConfiguration();
797 try {
798 config.networkId = Integer.parseInt(result[0]);
799 } catch(NumberFormatException e) {
800 continue;
801 }
802 if (result.length > 3) {
803 if (result[3].indexOf("[CURRENT]") != -1)
804 config.status = WifiConfiguration.Status.CURRENT;
805 else if (result[3].indexOf("[DISABLED]") != -1)
806 config.status = WifiConfiguration.Status.DISABLED;
807 else
808 config.status = WifiConfiguration.Status.ENABLED;
Irfan Sheriffa8fbe1f2010-03-09 09:13:58 -0800809 } else {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800810 config.status = WifiConfiguration.Status.ENABLED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800811 }
Irfan Sheriffa8fbe1f2010-03-09 09:13:58 -0800812 readNetworkVariables(config);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800813 networks.add(config);
814 }
815
816 return networks;
817 }
818
819 /**
820 * Read the variables from the supplicant daemon that are needed to
821 * fill in the WifiConfiguration object.
822 * <p/>
823 * The caller must hold the synchronization monitor.
824 * @param config the {@link WifiConfiguration} object to be filled in.
825 */
Irfan Sheriffa8fbe1f2010-03-09 09:13:58 -0800826 private void readNetworkVariables(WifiConfiguration config) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800827
828 int netId = config.networkId;
829 if (netId < 0)
830 return;
831
832 /*
833 * TODO: maybe should have a native method that takes an array of
834 * variable names and returns an array of values. But we'd still
835 * be doing a round trip to the supplicant daemon for each variable.
836 */
837 String value;
838
Irfan Sheriffa8fbe1f2010-03-09 09:13:58 -0800839 value = mWifiStateTracker.getNetworkVariable(netId, WifiConfiguration.ssidVarName);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800840 if (!TextUtils.isEmpty(value)) {
Chung-yih Wanga8d15942009-10-09 11:01:49 +0800841 config.SSID = removeDoubleQuotes(value);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800842 } else {
843 config.SSID = null;
844 }
845
Irfan Sheriffa8fbe1f2010-03-09 09:13:58 -0800846 value = mWifiStateTracker.getNetworkVariable(netId, WifiConfiguration.bssidVarName);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800847 if (!TextUtils.isEmpty(value)) {
848 config.BSSID = value;
849 } else {
850 config.BSSID = null;
851 }
852
Irfan Sheriffa8fbe1f2010-03-09 09:13:58 -0800853 value = mWifiStateTracker.getNetworkVariable(netId, WifiConfiguration.priorityVarName);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800854 config.priority = -1;
855 if (!TextUtils.isEmpty(value)) {
856 try {
857 config.priority = Integer.parseInt(value);
858 } catch (NumberFormatException ignore) {
859 }
860 }
861
Irfan Sheriffa8fbe1f2010-03-09 09:13:58 -0800862 value = mWifiStateTracker.getNetworkVariable(netId, WifiConfiguration.hiddenSSIDVarName);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800863 config.hiddenSSID = false;
864 if (!TextUtils.isEmpty(value)) {
865 try {
866 config.hiddenSSID = Integer.parseInt(value) != 0;
867 } catch (NumberFormatException ignore) {
868 }
869 }
870
Irfan Sheriffa8fbe1f2010-03-09 09:13:58 -0800871 value = mWifiStateTracker.getNetworkVariable(netId, WifiConfiguration.wepTxKeyIdxVarName);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800872 config.wepTxKeyIndex = -1;
873 if (!TextUtils.isEmpty(value)) {
874 try {
875 config.wepTxKeyIndex = Integer.parseInt(value);
876 } catch (NumberFormatException ignore) {
877 }
878 }
879
880 /*
881 * Get up to 4 WEP keys. Note that the actual keys are not passed back,
882 * just a "*" if the key is set, or the null string otherwise.
883 */
884 for (int i = 0; i < 4; i++) {
Irfan Sheriffa8fbe1f2010-03-09 09:13:58 -0800885 value = mWifiStateTracker.getNetworkVariable(netId, WifiConfiguration.wepKeyVarNames[i]);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800886 if (!TextUtils.isEmpty(value)) {
887 config.wepKeys[i] = value;
888 } else {
889 config.wepKeys[i] = null;
890 }
891 }
892
893 /*
894 * Get the private shared key. Note that the actual keys are not passed back,
895 * just a "*" if the key is set, or the null string otherwise.
896 */
Irfan Sheriffa8fbe1f2010-03-09 09:13:58 -0800897 value = mWifiStateTracker.getNetworkVariable(netId, WifiConfiguration.pskVarName);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800898 if (!TextUtils.isEmpty(value)) {
899 config.preSharedKey = value;
900 } else {
901 config.preSharedKey = null;
902 }
903
Irfan Sheriffa8fbe1f2010-03-09 09:13:58 -0800904 value = mWifiStateTracker.getNetworkVariable(config.networkId,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800905 WifiConfiguration.Protocol.varName);
906 if (!TextUtils.isEmpty(value)) {
907 String vals[] = value.split(" ");
908 for (String val : vals) {
909 int index =
910 lookupString(val, WifiConfiguration.Protocol.strings);
911 if (0 <= index) {
912 config.allowedProtocols.set(index);
913 }
914 }
915 }
916
Irfan Sheriffa8fbe1f2010-03-09 09:13:58 -0800917 value = mWifiStateTracker.getNetworkVariable(config.networkId,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800918 WifiConfiguration.KeyMgmt.varName);
919 if (!TextUtils.isEmpty(value)) {
920 String vals[] = value.split(" ");
921 for (String val : vals) {
922 int index =
923 lookupString(val, WifiConfiguration.KeyMgmt.strings);
924 if (0 <= index) {
925 config.allowedKeyManagement.set(index);
926 }
927 }
928 }
929
Irfan Sheriffa8fbe1f2010-03-09 09:13:58 -0800930 value = mWifiStateTracker.getNetworkVariable(config.networkId,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800931 WifiConfiguration.AuthAlgorithm.varName);
932 if (!TextUtils.isEmpty(value)) {
933 String vals[] = value.split(" ");
934 for (String val : vals) {
935 int index =
936 lookupString(val, WifiConfiguration.AuthAlgorithm.strings);
937 if (0 <= index) {
938 config.allowedAuthAlgorithms.set(index);
939 }
940 }
941 }
942
Irfan Sheriffa8fbe1f2010-03-09 09:13:58 -0800943 value = mWifiStateTracker.getNetworkVariable(config.networkId,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800944 WifiConfiguration.PairwiseCipher.varName);
945 if (!TextUtils.isEmpty(value)) {
946 String vals[] = value.split(" ");
947 for (String val : vals) {
948 int index =
949 lookupString(val, WifiConfiguration.PairwiseCipher.strings);
950 if (0 <= index) {
951 config.allowedPairwiseCiphers.set(index);
952 }
953 }
954 }
955
Irfan Sheriffa8fbe1f2010-03-09 09:13:58 -0800956 value = mWifiStateTracker.getNetworkVariable(config.networkId,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800957 WifiConfiguration.GroupCipher.varName);
958 if (!TextUtils.isEmpty(value)) {
959 String vals[] = value.split(" ");
960 for (String val : vals) {
961 int index =
962 lookupString(val, WifiConfiguration.GroupCipher.strings);
963 if (0 <= index) {
964 config.allowedGroupCiphers.set(index);
965 }
966 }
967 }
Chung-yih Wang43374762009-09-16 14:28:42 +0800968
969 for (WifiConfiguration.EnterpriseField field :
970 config.enterpriseFields) {
Irfan Sheriffa8fbe1f2010-03-09 09:13:58 -0800971 value = mWifiStateTracker.getNetworkVariable(netId,
Chung-yih Wang43374762009-09-16 14:28:42 +0800972 field.varName());
973 if (!TextUtils.isEmpty(value)) {
Chung-yih Wanga8d15942009-10-09 11:01:49 +0800974 if (field != config.eap) value = removeDoubleQuotes(value);
Chung-yih Wang43374762009-09-16 14:28:42 +0800975 field.setValue(value);
976 }
977 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800978 }
979
Chung-yih Wanga8d15942009-10-09 11:01:49 +0800980 private static String removeDoubleQuotes(String string) {
981 if (string.length() <= 2) return "";
982 return string.substring(1, string.length() - 1);
983 }
984
985 private static String convertToQuotedString(String string) {
986 return "\"" + string + "\"";
987 }
988
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800989 /**
990 * see {@link android.net.wifi.WifiManager#addOrUpdateNetwork(WifiConfiguration)}
991 * @return the supplicant-assigned identifier for the new or updated
992 * network if the operation succeeds, or {@code -1} if it fails
993 */
Irfan Sheriff7aac5542009-12-22 21:42:17 -0800994 public int addOrUpdateNetwork(WifiConfiguration config) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800995 enforceChangePermission();
Irfan Sheriffa8fbe1f2010-03-09 09:13:58 -0800996
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800997 /*
998 * If the supplied networkId is -1, we create a new empty
999 * network configuration. Otherwise, the networkId should
1000 * refer to an existing configuration.
1001 */
1002 int netId = config.networkId;
1003 boolean newNetwork = netId == -1;
Irfan Sheriff44113ba32010-03-16 14:54:07 -07001004 boolean doReconfig = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001005 // networkId of -1 means we want to create a new network
Irfan Sheriff7aac5542009-12-22 21:42:17 -08001006 synchronized (mWifiStateTracker) {
1007 if (newNetwork) {
Irfan Sheriffa8fbe1f2010-03-09 09:13:58 -08001008 netId = mWifiStateTracker.addNetwork();
Irfan Sheriff7aac5542009-12-22 21:42:17 -08001009 if (netId < 0) {
1010 if (DBG) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001011 Slog.d(TAG, "Failed to add a network!");
Irfan Sheriff7aac5542009-12-22 21:42:17 -08001012 }
1013 return -1;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001014 }
Irfan Sheriff7aac5542009-12-22 21:42:17 -08001015 doReconfig = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001016 }
Irfan Sheriff7aac5542009-12-22 21:42:17 -08001017 mNeedReconfig = mNeedReconfig || doReconfig;
Irfan Sheriffa8fbe1f2010-03-09 09:13:58 -08001018 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001019
Irfan Sheriffa8fbe1f2010-03-09 09:13:58 -08001020 setVariables: {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001021 /*
1022 * Note that if a networkId for a non-existent network
Irfan Sheriffa8fbe1f2010-03-09 09:13:58 -08001023 * was supplied, then the first setNetworkVariable()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001024 * will fail, so we don't bother to make a separate check
1025 * for the validity of the ID up front.
1026 */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001027 if (config.SSID != null &&
Irfan Sheriffa8fbe1f2010-03-09 09:13:58 -08001028 !mWifiStateTracker.setNetworkVariable(
Irfan Sheriff7aac5542009-12-22 21:42:17 -08001029 netId,
1030 WifiConfiguration.ssidVarName,
1031 convertToQuotedString(config.SSID))) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001032 if (DBG) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001033 Slog.d(TAG, "failed to set SSID: "+config.SSID);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001034 }
1035 break setVariables;
1036 }
1037
1038 if (config.BSSID != null &&
Irfan Sheriffa8fbe1f2010-03-09 09:13:58 -08001039 !mWifiStateTracker.setNetworkVariable(
Irfan Sheriff7aac5542009-12-22 21:42:17 -08001040 netId,
1041 WifiConfiguration.bssidVarName,
1042 config.BSSID)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001043 if (DBG) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001044 Slog.d(TAG, "failed to set BSSID: "+config.BSSID);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001045 }
1046 break setVariables;
1047 }
1048
1049 String allowedKeyManagementString =
1050 makeString(config.allowedKeyManagement, WifiConfiguration.KeyMgmt.strings);
1051 if (config.allowedKeyManagement.cardinality() != 0 &&
Irfan Sheriffa8fbe1f2010-03-09 09:13:58 -08001052 !mWifiStateTracker.setNetworkVariable(
Irfan Sheriff7aac5542009-12-22 21:42:17 -08001053 netId,
1054 WifiConfiguration.KeyMgmt.varName,
1055 allowedKeyManagementString)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001056 if (DBG) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001057 Slog.d(TAG, "failed to set key_mgmt: "+
Irfan Sheriff7aac5542009-12-22 21:42:17 -08001058 allowedKeyManagementString);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001059 }
1060 break setVariables;
1061 }
1062
1063 String allowedProtocolsString =
1064 makeString(config.allowedProtocols, WifiConfiguration.Protocol.strings);
1065 if (config.allowedProtocols.cardinality() != 0 &&
Irfan Sheriffa8fbe1f2010-03-09 09:13:58 -08001066 !mWifiStateTracker.setNetworkVariable(
Irfan Sheriff7aac5542009-12-22 21:42:17 -08001067 netId,
1068 WifiConfiguration.Protocol.varName,
1069 allowedProtocolsString)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001070 if (DBG) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001071 Slog.d(TAG, "failed to set proto: "+
Irfan Sheriff7aac5542009-12-22 21:42:17 -08001072 allowedProtocolsString);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001073 }
1074 break setVariables;
1075 }
1076
1077 String allowedAuthAlgorithmsString =
1078 makeString(config.allowedAuthAlgorithms, WifiConfiguration.AuthAlgorithm.strings);
1079 if (config.allowedAuthAlgorithms.cardinality() != 0 &&
Irfan Sheriffa8fbe1f2010-03-09 09:13:58 -08001080 !mWifiStateTracker.setNetworkVariable(
Irfan Sheriff7aac5542009-12-22 21:42:17 -08001081 netId,
1082 WifiConfiguration.AuthAlgorithm.varName,
1083 allowedAuthAlgorithmsString)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001084 if (DBG) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001085 Slog.d(TAG, "failed to set auth_alg: "+
Irfan Sheriff7aac5542009-12-22 21:42:17 -08001086 allowedAuthAlgorithmsString);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001087 }
1088 break setVariables;
1089 }
1090
1091 String allowedPairwiseCiphersString =
1092 makeString(config.allowedPairwiseCiphers, WifiConfiguration.PairwiseCipher.strings);
1093 if (config.allowedPairwiseCiphers.cardinality() != 0 &&
Irfan Sheriffa8fbe1f2010-03-09 09:13:58 -08001094 !mWifiStateTracker.setNetworkVariable(
Irfan Sheriff7aac5542009-12-22 21:42:17 -08001095 netId,
1096 WifiConfiguration.PairwiseCipher.varName,
1097 allowedPairwiseCiphersString)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001098 if (DBG) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001099 Slog.d(TAG, "failed to set pairwise: "+
Irfan Sheriff7aac5542009-12-22 21:42:17 -08001100 allowedPairwiseCiphersString);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001101 }
1102 break setVariables;
1103 }
1104
1105 String allowedGroupCiphersString =
1106 makeString(config.allowedGroupCiphers, WifiConfiguration.GroupCipher.strings);
1107 if (config.allowedGroupCiphers.cardinality() != 0 &&
Irfan Sheriffa8fbe1f2010-03-09 09:13:58 -08001108 !mWifiStateTracker.setNetworkVariable(
Irfan Sheriff7aac5542009-12-22 21:42:17 -08001109 netId,
1110 WifiConfiguration.GroupCipher.varName,
1111 allowedGroupCiphersString)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001112 if (DBG) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001113 Slog.d(TAG, "failed to set group: "+
Irfan Sheriff7aac5542009-12-22 21:42:17 -08001114 allowedGroupCiphersString);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001115 }
1116 break setVariables;
1117 }
1118
1119 // Prevent client screw-up by passing in a WifiConfiguration we gave it
1120 // by preventing "*" as a key.
1121 if (config.preSharedKey != null && !config.preSharedKey.equals("*") &&
Irfan Sheriffa8fbe1f2010-03-09 09:13:58 -08001122 !mWifiStateTracker.setNetworkVariable(
Irfan Sheriff7aac5542009-12-22 21:42:17 -08001123 netId,
1124 WifiConfiguration.pskVarName,
1125 config.preSharedKey)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001126 if (DBG) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001127 Slog.d(TAG, "failed to set psk: "+config.preSharedKey);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001128 }
1129 break setVariables;
1130 }
1131
1132 boolean hasSetKey = false;
1133 if (config.wepKeys != null) {
1134 for (int i = 0; i < config.wepKeys.length; i++) {
1135 // Prevent client screw-up by passing in a WifiConfiguration we gave it
1136 // by preventing "*" as a key.
1137 if (config.wepKeys[i] != null && !config.wepKeys[i].equals("*")) {
Irfan Sheriffa8fbe1f2010-03-09 09:13:58 -08001138 if (!mWifiStateTracker.setNetworkVariable(
Irfan Sheriff7aac5542009-12-22 21:42:17 -08001139 netId,
1140 WifiConfiguration.wepKeyVarNames[i],
1141 config.wepKeys[i])) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001142 if (DBG) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001143 Slog.d(TAG,
Irfan Sheriff7aac5542009-12-22 21:42:17 -08001144 "failed to set wep_key"+i+": " +
1145 config.wepKeys[i]);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001146 }
1147 break setVariables;
1148 }
1149 hasSetKey = true;
1150 }
1151 }
1152 }
1153
1154 if (hasSetKey) {
Irfan Sheriffa8fbe1f2010-03-09 09:13:58 -08001155 if (!mWifiStateTracker.setNetworkVariable(
Irfan Sheriff7aac5542009-12-22 21:42:17 -08001156 netId,
1157 WifiConfiguration.wepTxKeyIdxVarName,
1158 Integer.toString(config.wepTxKeyIndex))) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001159 if (DBG) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001160 Slog.d(TAG,
Irfan Sheriff7aac5542009-12-22 21:42:17 -08001161 "failed to set wep_tx_keyidx: "+
1162 config.wepTxKeyIndex);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001163 }
1164 break setVariables;
1165 }
1166 }
1167
Irfan Sheriffa8fbe1f2010-03-09 09:13:58 -08001168 if (!mWifiStateTracker.setNetworkVariable(
Irfan Sheriff7aac5542009-12-22 21:42:17 -08001169 netId,
1170 WifiConfiguration.priorityVarName,
1171 Integer.toString(config.priority))) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001172 if (DBG) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001173 Slog.d(TAG, config.SSID + ": failed to set priority: "
Irfan Sheriff7aac5542009-12-22 21:42:17 -08001174 +config.priority);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001175 }
1176 break setVariables;
1177 }
1178
Irfan Sheriffa8fbe1f2010-03-09 09:13:58 -08001179 if (config.hiddenSSID && !mWifiStateTracker.setNetworkVariable(
Irfan Sheriff7aac5542009-12-22 21:42:17 -08001180 netId,
1181 WifiConfiguration.hiddenSSIDVarName,
1182 Integer.toString(config.hiddenSSID ? 1 : 0))) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001183 if (DBG) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001184 Slog.d(TAG, config.SSID + ": failed to set hiddenSSID: "+
Irfan Sheriff7aac5542009-12-22 21:42:17 -08001185 config.hiddenSSID);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001186 }
1187 break setVariables;
1188 }
1189
Chung-yih Wang43374762009-09-16 14:28:42 +08001190 for (WifiConfiguration.EnterpriseField field
1191 : config.enterpriseFields) {
1192 String varName = field.varName();
1193 String value = field.value();
Chung-yih Wanga8d15942009-10-09 11:01:49 +08001194 if (value != null) {
1195 if (field != config.eap) {
Chia-chi Yeh784d53e2010-01-29 16:26:28 +08001196 value = (value.length() == 0) ? "NULL" : convertToQuotedString(value);
Chung-yih Wang43374762009-09-16 14:28:42 +08001197 }
Irfan Sheriffa8fbe1f2010-03-09 09:13:58 -08001198 if (!mWifiStateTracker.setNetworkVariable(
Irfan Sheriff7aac5542009-12-22 21:42:17 -08001199 netId,
1200 varName,
1201 value)) {
Chung-yih Wanga8d15942009-10-09 11:01:49 +08001202 if (DBG) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001203 Slog.d(TAG, config.SSID + ": failed to set " + varName +
Irfan Sheriff7aac5542009-12-22 21:42:17 -08001204 ": " + value);
Chung-yih Wanga8d15942009-10-09 11:01:49 +08001205 }
1206 break setVariables;
1207 }
Chung-yih Wang5069cc72009-06-03 17:33:47 +08001208 }
Chung-yih Wang5069cc72009-06-03 17:33:47 +08001209 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001210 return netId;
Irfan Sheriffa8fbe1f2010-03-09 09:13:58 -08001211 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001212
Irfan Sheriffa8fbe1f2010-03-09 09:13:58 -08001213 /*
1214 * For an update, if one of the setNetworkVariable operations fails,
1215 * we might want to roll back all the changes already made. But the
1216 * chances are that if anything is going to go wrong, it'll happen
1217 * the first time we try to set one of the variables.
1218 */
1219 if (newNetwork) {
1220 removeNetwork(netId);
1221 if (DBG) {
1222 Slog.d(TAG,
1223 "Failed to set a network variable, removed network: "
1224 + netId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001225 }
1226 }
1227 return -1;
1228 }
1229
1230 private static String makeString(BitSet set, String[] strings) {
1231 StringBuffer buf = new StringBuffer();
1232 int nextSetBit = -1;
1233
1234 /* Make sure all set bits are in [0, strings.length) to avoid
1235 * going out of bounds on strings. (Shouldn't happen, but...) */
1236 set = set.get(0, strings.length);
1237
1238 while ((nextSetBit = set.nextSetBit(nextSetBit + 1)) != -1) {
1239 buf.append(strings[nextSetBit].replace('_', '-')).append(' ');
1240 }
1241
1242 // remove trailing space
1243 if (set.cardinality() > 0) {
1244 buf.setLength(buf.length() - 1);
1245 }
1246
1247 return buf.toString();
1248 }
1249
1250 private static int lookupString(String string, String[] strings) {
1251 int size = strings.length;
1252
1253 string = string.replace('-', '_');
1254
1255 for (int i = 0; i < size; i++)
1256 if (string.equals(strings[i]))
1257 return i;
1258
1259 if (DBG) {
1260 // if we ever get here, we should probably add the
1261 // value to WifiConfiguration to reflect that it's
1262 // supported by the WPA supplicant
Joe Onorato8a9b2202010-02-26 18:56:32 -08001263 Slog.w(TAG, "Failed to look-up a string: " + string);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001264 }
1265
1266 return -1;
1267 }
1268
1269 /**
1270 * See {@link android.net.wifi.WifiManager#removeNetwork(int)}
1271 * @param netId the integer that identifies the network configuration
1272 * to the supplicant
1273 * @return {@code true} if the operation succeeded
1274 */
1275 public boolean removeNetwork(int netId) {
1276 enforceChangePermission();
1277
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001278 return mWifiStateTracker.removeNetwork(netId);
1279 }
1280
1281 /**
1282 * See {@link android.net.wifi.WifiManager#enableNetwork(int, boolean)}
1283 * @param netId the integer that identifies the network configuration
1284 * to the supplicant
1285 * @param disableOthers if true, disable all other networks.
1286 * @return {@code true} if the operation succeeded
1287 */
1288 public boolean enableNetwork(int netId, boolean disableOthers) {
1289 enforceChangePermission();
1290
Irfan Sheriffa8fbe1f2010-03-09 09:13:58 -08001291 String ifname = mWifiStateTracker.getInterfaceName();
1292 NetworkUtils.enableInterface(ifname);
1293 boolean result = mWifiStateTracker.enableNetwork(netId, disableOthers);
1294 if (!result) {
1295 NetworkUtils.disableInterface(ifname);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001296 }
Irfan Sheriffa8fbe1f2010-03-09 09:13:58 -08001297 return result;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001298 }
1299
1300 /**
1301 * See {@link android.net.wifi.WifiManager#disableNetwork(int)}
1302 * @param netId the integer that identifies the network configuration
1303 * to the supplicant
1304 * @return {@code true} if the operation succeeded
1305 */
1306 public boolean disableNetwork(int netId) {
1307 enforceChangePermission();
1308
Irfan Sheriffa8fbe1f2010-03-09 09:13:58 -08001309 return mWifiStateTracker.disableNetwork(netId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001310 }
1311
1312 /**
1313 * See {@link android.net.wifi.WifiManager#getConnectionInfo()}
1314 * @return the Wi-Fi information, contained in {@link WifiInfo}.
1315 */
1316 public WifiInfo getConnectionInfo() {
1317 enforceAccessPermission();
1318 /*
1319 * Make sure we have the latest information, by sending
1320 * a status request to the supplicant.
1321 */
1322 return mWifiStateTracker.requestConnectionInfo();
1323 }
1324
1325 /**
1326 * Return the results of the most recent access point scan, in the form of
1327 * a list of {@link ScanResult} objects.
1328 * @return the list of results
1329 */
1330 public List<ScanResult> getScanResults() {
1331 enforceAccessPermission();
1332 String reply;
Irfan Sheriffa8fbe1f2010-03-09 09:13:58 -08001333
1334 reply = mWifiStateTracker.scanResults();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001335 if (reply == null) {
1336 return null;
1337 }
1338
1339 List<ScanResult> scanList = new ArrayList<ScanResult>();
1340
1341 int lineCount = 0;
1342
1343 int replyLen = reply.length();
1344 // Parse the result string, keeping in mind that the last line does
1345 // not end with a newline.
1346 for (int lineBeg = 0, lineEnd = 0; lineEnd <= replyLen; ++lineEnd) {
1347 if (lineEnd == replyLen || reply.charAt(lineEnd) == '\n') {
1348 ++lineCount;
1349 /*
1350 * Skip the first line, which is a header
1351 */
1352 if (lineCount == 1) {
1353 lineBeg = lineEnd + 1;
1354 continue;
1355 }
Mike Lockwoodb30475e2009-04-21 13:55:07 -07001356 if (lineEnd > lineBeg) {
1357 String line = reply.substring(lineBeg, lineEnd);
1358 ScanResult scanResult = parseScanResult(line);
1359 if (scanResult != null) {
1360 scanList.add(scanResult);
1361 } else if (DBG) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001362 Slog.w(TAG, "misformatted scan result for: " + line);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001363 }
1364 }
1365 lineBeg = lineEnd + 1;
1366 }
1367 }
1368 mWifiStateTracker.setScanResultsList(scanList);
1369 return scanList;
1370 }
1371
1372 /**
1373 * Parse the scan result line passed to us by wpa_supplicant (helper).
1374 * @param line the line to parse
1375 * @return the {@link ScanResult} object
1376 */
1377 private ScanResult parseScanResult(String line) {
1378 ScanResult scanResult = null;
1379 if (line != null) {
1380 /*
1381 * Cache implementation (LinkedHashMap) is not synchronized, thus,
1382 * must synchronized here!
1383 */
1384 synchronized (mScanResultCache) {
Mike Lockwoodb30475e2009-04-21 13:55:07 -07001385 String[] result = scanResultPattern.split(line);
1386 if (3 <= result.length && result.length <= 5) {
1387 String bssid = result[0];
1388 // bssid | frequency | level | flags | ssid
1389 int frequency;
1390 int level;
1391 try {
1392 frequency = Integer.parseInt(result[1]);
1393 level = Integer.parseInt(result[2]);
1394 /* some implementations avoid negative values by adding 256
1395 * so we need to adjust for that here.
1396 */
1397 if (level > 0) level -= 256;
1398 } catch (NumberFormatException e) {
1399 frequency = 0;
1400 level = 0;
1401 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001402
Mike Lockwood1a645052009-06-25 13:01:12 -04001403 /*
1404 * The formatting of the results returned by
1405 * wpa_supplicant is intended to make the fields
1406 * line up nicely when printed,
1407 * not to make them easy to parse. So we have to
1408 * apply some heuristics to figure out which field
1409 * is the SSID and which field is the flags.
1410 */
1411 String ssid;
1412 String flags;
1413 if (result.length == 4) {
1414 if (result[3].charAt(0) == '[') {
1415 flags = result[3];
1416 ssid = "";
1417 } else {
1418 flags = "";
1419 ssid = result[3];
1420 }
1421 } else if (result.length == 5) {
1422 flags = result[3];
1423 ssid = result[4];
1424 } else {
1425 // Here, we must have 3 fields: no flags and ssid
1426 // set
1427 flags = "";
1428 ssid = "";
1429 }
1430
Mike Lockwood00717e22009-08-17 10:09:36 -04001431 // bssid + ssid is the hash key
1432 String key = bssid + ssid;
1433 scanResult = mScanResultCache.get(key);
Mike Lockwoodb30475e2009-04-21 13:55:07 -07001434 if (scanResult != null) {
1435 scanResult.level = level;
Mike Lockwood1a645052009-06-25 13:01:12 -04001436 scanResult.SSID = ssid;
1437 scanResult.capabilities = flags;
1438 scanResult.frequency = frequency;
Mike Lockwoodb30475e2009-04-21 13:55:07 -07001439 } else {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001440 // Do not add scan results that have no SSID set
1441 if (0 < ssid.trim().length()) {
1442 scanResult =
1443 new ScanResult(
Mike Lockwoodb30475e2009-04-21 13:55:07 -07001444 ssid, bssid, flags, level, frequency);
Mike Lockwood00717e22009-08-17 10:09:36 -04001445 mScanResultCache.put(key, scanResult);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001446 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001447 }
Mike Lockwoodb30475e2009-04-21 13:55:07 -07001448 } else {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001449 Slog.w(TAG, "Misformatted scan result text with " +
Mike Lockwoodb30475e2009-04-21 13:55:07 -07001450 result.length + " fields: " + line);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001451 }
1452 }
1453 }
1454
1455 return scanResult;
1456 }
1457
1458 /**
1459 * Parse the "flags" field passed back in a scan result by wpa_supplicant,
1460 * and construct a {@code WifiConfiguration} that describes the encryption,
1461 * key management, and authenticaion capabilities of the access point.
1462 * @param flags the string returned by wpa_supplicant
1463 * @return the {@link WifiConfiguration} object, filled in
1464 */
1465 WifiConfiguration parseScanFlags(String flags) {
1466 WifiConfiguration config = new WifiConfiguration();
1467
1468 if (flags.length() == 0) {
1469 config.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE);
1470 }
1471 // ... to be implemented
1472 return config;
1473 }
1474
1475 /**
1476 * Tell the supplicant to persist the current list of configured networks.
1477 * @return {@code true} if the operation succeeded
1478 */
1479 public boolean saveConfiguration() {
1480 boolean result;
1481 enforceChangePermission();
Irfan Sheriffa8fbe1f2010-03-09 09:13:58 -08001482
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001483 synchronized (mWifiStateTracker) {
Irfan Sheriffa8fbe1f2010-03-09 09:13:58 -08001484 result = mWifiStateTracker.saveConfig();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001485 if (result && mNeedReconfig) {
1486 mNeedReconfig = false;
Irfan Sheriffa8fbe1f2010-03-09 09:13:58 -08001487 result = mWifiStateTracker.reloadConfig();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001488
1489 if (result) {
1490 Intent intent = new Intent(WifiManager.NETWORK_IDS_CHANGED_ACTION);
1491 mContext.sendBroadcast(intent);
1492 }
1493 }
1494 }
Amith Yamasani47873e52009-07-02 12:05:32 -07001495 // Inform the backup manager about a data change
1496 IBackupManager ibm = IBackupManager.Stub.asInterface(
1497 ServiceManager.getService(Context.BACKUP_SERVICE));
1498 if (ibm != null) {
1499 try {
1500 ibm.dataChanged("com.android.providers.settings");
1501 } catch (Exception e) {
1502 // Try again later
1503 }
1504 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001505 return result;
1506 }
1507
1508 /**
1509 * Set the number of radio frequency channels that are allowed to be used
1510 * in the current regulatory domain. This method should be used only
1511 * if the correct number of channels cannot be determined automatically
Robert Greenwaltb5010cc2009-05-21 15:11:40 -07001512 * for some reason. If the operation is successful, the new value may be
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001513 * persisted as a Secure setting.
1514 * @param numChannels the number of allowed channels. Must be greater than 0
1515 * and less than or equal to 16.
Robert Greenwaltb5010cc2009-05-21 15:11:40 -07001516 * @param persist {@code true} if the setting should be remembered.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001517 * @return {@code true} if the operation succeeds, {@code false} otherwise, e.g.,
1518 * {@code numChannels} is outside the valid range.
1519 */
Robert Greenwaltb5010cc2009-05-21 15:11:40 -07001520 public boolean setNumAllowedChannels(int numChannels, boolean persist) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001521 Slog.i(TAG, "WifiService trying to setNumAllowed to "+numChannels+
Robert Greenwaltb5010cc2009-05-21 15:11:40 -07001522 " with persist set to "+persist);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001523 enforceChangePermission();
1524 /*
1525 * Validate the argument. We'd like to let the Wi-Fi driver do this,
1526 * but if Wi-Fi isn't currently enabled, that's not possible, and
1527 * we want to persist the setting anyway,so that it will take
1528 * effect when Wi-Fi does become enabled.
1529 */
1530 boolean found = false;
1531 for (int validChan : sValidRegulatoryChannelCounts) {
1532 if (validChan == numChannels) {
1533 found = true;
1534 break;
1535 }
1536 }
1537 if (!found) {
1538 return false;
1539 }
1540
Robert Greenwaltb5010cc2009-05-21 15:11:40 -07001541 if (persist) {
1542 Settings.Secure.putInt(mContext.getContentResolver(),
1543 Settings.Secure.WIFI_NUM_ALLOWED_CHANNELS,
1544 numChannels);
1545 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001546 mWifiStateTracker.setNumAllowedChannels(numChannels);
1547 return true;
1548 }
1549
1550 /**
1551 * Return the number of frequency channels that are allowed
1552 * to be used in the current regulatory domain.
1553 * @return the number of allowed channels, or {@code -1} if an error occurs
1554 */
1555 public int getNumAllowedChannels() {
1556 int numChannels;
1557
1558 enforceAccessPermission();
Irfan Sheriffa8fbe1f2010-03-09 09:13:58 -08001559
1560 /*
1561 * If we can't get the value from the driver (e.g., because
1562 * Wi-Fi is not currently enabled), get the value from
1563 * Settings.
1564 */
1565 numChannels = mWifiStateTracker.getNumAllowedChannels();
1566 if (numChannels < 0) {
1567 numChannels = Settings.Secure.getInt(mContext.getContentResolver(),
1568 Settings.Secure.WIFI_NUM_ALLOWED_CHANNELS,
1569 -1);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001570 }
1571 return numChannels;
1572 }
1573
1574 /**
1575 * Return the list of valid values for the number of allowed radio channels
1576 * for various regulatory domains.
1577 * @return the list of channel counts
1578 */
1579 public int[] getValidChannelCounts() {
1580 enforceAccessPermission();
1581 return sValidRegulatoryChannelCounts;
1582 }
1583
1584 /**
1585 * Return the DHCP-assigned addresses from the last successful DHCP request,
1586 * if any.
1587 * @return the DHCP information
1588 */
1589 public DhcpInfo getDhcpInfo() {
1590 enforceAccessPermission();
1591 return mWifiStateTracker.getDhcpInfo();
1592 }
1593
1594 private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
1595 @Override
1596 public void onReceive(Context context, Intent intent) {
1597 String action = intent.getAction();
1598
Doug Zongker43866e02010-01-07 12:09:54 -08001599 long idleMillis =
1600 Settings.Secure.getLong(mContext.getContentResolver(),
1601 Settings.Secure.WIFI_IDLE_MS, DEFAULT_IDLE_MILLIS);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001602 int stayAwakeConditions =
Doug Zongker43866e02010-01-07 12:09:54 -08001603 Settings.System.getInt(mContext.getContentResolver(),
1604 Settings.System.STAY_ON_WHILE_PLUGGED_IN, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001605 if (action.equals(Intent.ACTION_SCREEN_ON)) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001606 Slog.d(TAG, "ACTION_SCREEN_ON");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001607 mAlarmManager.cancel(mIdleIntent);
1608 mDeviceIdle = false;
1609 mScreenOff = false;
Mike Lockwoodf32be162009-07-14 17:44:37 -04001610 mWifiStateTracker.enableRssiPolling(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001611 } else if (action.equals(Intent.ACTION_SCREEN_OFF)) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001612 Slog.d(TAG, "ACTION_SCREEN_OFF");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001613 mScreenOff = true;
Mike Lockwoodf32be162009-07-14 17:44:37 -04001614 mWifiStateTracker.enableRssiPolling(false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001615 /*
1616 * Set a timer to put Wi-Fi to sleep, but only if the screen is off
1617 * AND the "stay on while plugged in" setting doesn't match the
1618 * current power conditions (i.e, not plugged in, plugged in to USB,
1619 * or plugged in to AC).
1620 */
1621 if (!shouldWifiStayAwake(stayAwakeConditions, mPluggedType)) {
San Mehatfa6c7112009-07-07 09:34:44 -07001622 WifiInfo info = mWifiStateTracker.requestConnectionInfo();
1623 if (info.getSupplicantState() != SupplicantState.COMPLETED) {
Robert Greenwalt84612ea62009-09-30 09:04:22 -07001624 // we used to go to sleep immediately, but this caused some race conditions
1625 // we don't have time to track down for this release. Delay instead, but not
1626 // as long as we would if connected (below)
1627 // TODO - fix the race conditions and switch back to the immediate turn-off
1628 long triggerTime = System.currentTimeMillis() + (2*60*1000); // 2 min
Joe Onorato8a9b2202010-02-26 18:56:32 -08001629 Slog.d(TAG, "setting ACTION_DEVICE_IDLE timer for 120,000 ms");
Robert Greenwalt84612ea62009-09-30 09:04:22 -07001630 mAlarmManager.set(AlarmManager.RTC_WAKEUP, triggerTime, mIdleIntent);
1631 // // do not keep Wifi awake when screen is off if Wifi is not associated
1632 // mDeviceIdle = true;
1633 // updateWifiState();
Mike Lockwoodd9c32bc2009-05-18 14:14:15 -04001634 } else {
1635 long triggerTime = System.currentTimeMillis() + idleMillis;
Joe Onorato8a9b2202010-02-26 18:56:32 -08001636 Slog.d(TAG, "setting ACTION_DEVICE_IDLE timer for " + idleMillis + "ms");
Mike Lockwoodd9c32bc2009-05-18 14:14:15 -04001637 mAlarmManager.set(AlarmManager.RTC_WAKEUP, triggerTime, mIdleIntent);
1638 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001639 }
1640 /* we can return now -- there's nothing to do until we get the idle intent back */
1641 return;
1642 } else if (action.equals(ACTION_DEVICE_IDLE)) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001643 Slog.d(TAG, "got ACTION_DEVICE_IDLE");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001644 mDeviceIdle = true;
1645 } else if (action.equals(Intent.ACTION_BATTERY_CHANGED)) {
1646 /*
1647 * Set a timer to put Wi-Fi to sleep, but only if the screen is off
1648 * AND we are transitioning from a state in which the device was supposed
1649 * to stay awake to a state in which it is not supposed to stay awake.
1650 * If "stay awake" state is not changing, we do nothing, to avoid resetting
1651 * the already-set timer.
1652 */
1653 int pluggedType = intent.getIntExtra("plugged", 0);
Joe Onorato8a9b2202010-02-26 18:56:32 -08001654 Slog.d(TAG, "ACTION_BATTERY_CHANGED pluggedType: " + pluggedType);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001655 if (mScreenOff && shouldWifiStayAwake(stayAwakeConditions, mPluggedType) &&
1656 !shouldWifiStayAwake(stayAwakeConditions, pluggedType)) {
1657 long triggerTime = System.currentTimeMillis() + idleMillis;
Joe Onorato8a9b2202010-02-26 18:56:32 -08001658 Slog.d(TAG, "setting ACTION_DEVICE_IDLE timer for " + idleMillis + "ms");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001659 mAlarmManager.set(AlarmManager.RTC_WAKEUP, triggerTime, mIdleIntent);
1660 mPluggedType = pluggedType;
1661 return;
1662 }
1663 mPluggedType = pluggedType;
Nick Pelly005b2282009-09-10 10:21:56 -07001664 } else if (action.equals(BluetoothA2dp.ACTION_SINK_STATE_CHANGED)) {
Jaikumar Ganesh084c6652009-12-07 10:58:18 -08001665 BluetoothA2dp a2dp = new BluetoothA2dp(mContext);
1666 Set<BluetoothDevice> sinks = a2dp.getConnectedSinks();
1667 boolean isBluetoothPlaying = false;
1668 for (BluetoothDevice sink : sinks) {
1669 if (a2dp.getSinkState(sink) == BluetoothA2dp.STATE_PLAYING) {
1670 isBluetoothPlaying = true;
1671 }
1672 }
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001673 mWifiStateTracker.setBluetoothScanMode(isBluetoothPlaying);
Jaikumar Ganesh084c6652009-12-07 10:58:18 -08001674
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001675 } else {
1676 return;
1677 }
1678
1679 updateWifiState();
1680 }
1681
1682 /**
1683 * Determines whether the Wi-Fi chipset should stay awake or be put to
1684 * sleep. Looks at the setting for the sleep policy and the current
1685 * conditions.
Jaikumar Ganesh084c6652009-12-07 10:58:18 -08001686 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001687 * @see #shouldDeviceStayAwake(int, int)
1688 */
1689 private boolean shouldWifiStayAwake(int stayAwakeConditions, int pluggedType) {
1690 int wifiSleepPolicy = Settings.System.getInt(mContext.getContentResolver(),
1691 Settings.System.WIFI_SLEEP_POLICY, Settings.System.WIFI_SLEEP_POLICY_DEFAULT);
1692
1693 if (wifiSleepPolicy == Settings.System.WIFI_SLEEP_POLICY_NEVER) {
1694 // Never sleep
1695 return true;
1696 } else if ((wifiSleepPolicy == Settings.System.WIFI_SLEEP_POLICY_NEVER_WHILE_PLUGGED) &&
1697 (pluggedType != 0)) {
1698 // Never sleep while plugged, and we're plugged
1699 return true;
1700 } else {
1701 // Default
1702 return shouldDeviceStayAwake(stayAwakeConditions, pluggedType);
1703 }
1704 }
Jaikumar Ganesh084c6652009-12-07 10:58:18 -08001705
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001706 /**
1707 * Determine whether the bit value corresponding to {@code pluggedType} is set in
1708 * the bit string {@code stayAwakeConditions}. Because a {@code pluggedType} value
1709 * of {@code 0} isn't really a plugged type, but rather an indication that the
1710 * device isn't plugged in at all, there is no bit value corresponding to a
1711 * {@code pluggedType} value of {@code 0}. That is why we shift by
1712 * {@code pluggedType&nbsp;&#8212;&nbsp;1} instead of by {@code pluggedType}.
1713 * @param stayAwakeConditions a bit string specifying which "plugged types" should
1714 * keep the device (and hence Wi-Fi) awake.
1715 * @param pluggedType the type of plug (USB, AC, or none) for which the check is
1716 * being made
1717 * @return {@code true} if {@code pluggedType} indicates that the device is
1718 * supposed to stay awake, {@code false} otherwise.
1719 */
1720 private boolean shouldDeviceStayAwake(int stayAwakeConditions, int pluggedType) {
1721 return (stayAwakeConditions & pluggedType) != 0;
1722 }
1723 };
1724
Dianne Hackborn617f8772009-03-31 15:04:46 -07001725 private void sendEnableMessage(boolean enable, boolean persist, int uid) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001726 Message msg = Message.obtain(mWifiHandler,
1727 (enable ? MESSAGE_ENABLE_WIFI : MESSAGE_DISABLE_WIFI),
Dianne Hackborn617f8772009-03-31 15:04:46 -07001728 (persist ? 1 : 0), uid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001729 msg.sendToTarget();
1730 }
1731
1732 private void sendStartMessage(boolean scanOnlyMode) {
1733 Message.obtain(mWifiHandler, MESSAGE_START_WIFI, scanOnlyMode ? 1 : 0, 0).sendToTarget();
1734 }
1735
Irfan Sheriff5321aef2010-02-12 12:35:59 -08001736 private void sendAccessPointMessage(boolean enable, WifiConfiguration wifiConfig, int uid) {
1737 Message.obtain(mWifiHandler,
1738 (enable ? MESSAGE_START_ACCESS_POINT : MESSAGE_STOP_ACCESS_POINT),
Irfan Sheriff9ab518ad2010-03-12 15:48:17 -08001739 uid, 0, wifiConfig).sendToTarget();
Irfan Sheriff5321aef2010-02-12 12:35:59 -08001740 }
1741
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001742 private void updateWifiState() {
Robert Greenwaltf75aa36fc2009-10-22 17:03:47 -07001743 // send a message so it's all serialized
1744 Message.obtain(mWifiHandler, MESSAGE_UPDATE_STATE, 0, 0).sendToTarget();
1745 }
1746
1747 private void doUpdateWifiState() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001748 boolean wifiEnabled = getPersistedWifiEnabled();
Mike Lockwoodbd5ddf02009-07-29 21:37:14 -07001749 boolean airplaneMode = isAirplaneModeOn() && !mAirplaneModeOverwridden;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001750 boolean lockHeld = mLocks.hasLocks();
1751 int strongestLockMode;
1752 boolean wifiShouldBeEnabled = wifiEnabled && !airplaneMode;
1753 boolean wifiShouldBeStarted = !mDeviceIdle || lockHeld;
1754 if (mDeviceIdle && lockHeld) {
1755 strongestLockMode = mLocks.getStrongestLockMode();
1756 } else {
1757 strongestLockMode = WifiManager.WIFI_MODE_FULL;
1758 }
1759
1760 synchronized (mWifiHandler) {
Irfan Sheriff0f344060092010-03-10 10:05:51 -08001761 if ((mWifiStateTracker.getWifiState() == WIFI_STATE_ENABLING) && !airplaneMode) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001762 return;
1763 }
1764 if (wifiShouldBeEnabled) {
1765 if (wifiShouldBeStarted) {
1766 sWakeLock.acquire();
Dianne Hackborn617f8772009-03-31 15:04:46 -07001767 sendEnableMessage(true, false, mLastEnableUid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001768 sWakeLock.acquire();
1769 sendStartMessage(strongestLockMode == WifiManager.WIFI_MODE_SCAN_ONLY);
Irfan Sheriff3bf504d2010-03-23 12:24:33 -07001770 } else if (!mWifiStateTracker.isDriverStopped()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001771 int wakeLockTimeout =
1772 Settings.Secure.getInt(
1773 mContext.getContentResolver(),
1774 Settings.Secure.WIFI_MOBILE_DATA_TRANSITION_WAKELOCK_TIMEOUT_MS,
1775 DEFAULT_WAKELOCK_TIMEOUT);
1776 /*
Irfan Sheriff3bf504d2010-03-23 12:24:33 -07001777 * We are assuming that ConnectivityService can make
1778 * a transition to cellular data within wakeLockTimeout time.
1779 * The wakelock is released by the delayed message.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001780 */
1781 sDriverStopWakeLock.acquire();
1782 mWifiHandler.sendEmptyMessage(MESSAGE_STOP_WIFI);
1783 mWifiHandler.sendEmptyMessageDelayed(MESSAGE_RELEASE_WAKELOCK, wakeLockTimeout);
1784 }
1785 } else {
1786 sWakeLock.acquire();
Dianne Hackborn617f8772009-03-31 15:04:46 -07001787 sendEnableMessage(false, false, mLastEnableUid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001788 }
1789 }
1790 }
1791
1792 private void registerForBroadcasts() {
1793 IntentFilter intentFilter = new IntentFilter();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001794 intentFilter.addAction(Intent.ACTION_SCREEN_ON);
1795 intentFilter.addAction(Intent.ACTION_SCREEN_OFF);
1796 intentFilter.addAction(Intent.ACTION_BATTERY_CHANGED);
1797 intentFilter.addAction(ACTION_DEVICE_IDLE);
Nick Pelly005b2282009-09-10 10:21:56 -07001798 intentFilter.addAction(BluetoothA2dp.ACTION_SINK_STATE_CHANGED);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001799 mContext.registerReceiver(mReceiver, intentFilter);
1800 }
Jaikumar Ganesh084c6652009-12-07 10:58:18 -08001801
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001802 private boolean isAirplaneSensitive() {
1803 String airplaneModeRadios = Settings.System.getString(mContext.getContentResolver(),
1804 Settings.System.AIRPLANE_MODE_RADIOS);
1805 return airplaneModeRadios == null
1806 || airplaneModeRadios.contains(Settings.System.RADIO_WIFI);
1807 }
1808
Mike Lockwoodbd5ddf02009-07-29 21:37:14 -07001809 private boolean isAirplaneToggleable() {
1810 String toggleableRadios = Settings.System.getString(mContext.getContentResolver(),
1811 Settings.System.AIRPLANE_MODE_TOGGLEABLE_RADIOS);
1812 return toggleableRadios != null
1813 && toggleableRadios.contains(Settings.System.RADIO_WIFI);
1814 }
1815
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001816 /**
1817 * Returns true if Wi-Fi is sensitive to airplane mode, and airplane mode is
1818 * currently on.
1819 * @return {@code true} if airplane mode is on.
1820 */
1821 private boolean isAirplaneModeOn() {
1822 return isAirplaneSensitive() && Settings.System.getInt(mContext.getContentResolver(),
1823 Settings.System.AIRPLANE_MODE_ON, 0) == 1;
1824 }
1825
1826 /**
1827 * Handler that allows posting to the WifiThread.
1828 */
1829 private class WifiHandler extends Handler {
1830 public WifiHandler(Looper looper) {
1831 super(looper);
1832 }
1833
1834 @Override
1835 public void handleMessage(Message msg) {
1836 switch (msg.what) {
1837
1838 case MESSAGE_ENABLE_WIFI:
Irfan Sheriff7b009782010-03-11 16:37:45 -08001839 if (mWifiWatchdogService == null) {
1840 mWifiWatchdogService = new WifiWatchdogService(mContext, mWifiStateTracker);
1841 }
Dianne Hackborn617f8772009-03-31 15:04:46 -07001842 setWifiEnabledBlocking(true, msg.arg1 == 1, msg.arg2);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001843 sWakeLock.release();
1844 break;
1845
1846 case MESSAGE_START_WIFI:
1847 mWifiStateTracker.setScanOnlyMode(msg.arg1 != 0);
1848 mWifiStateTracker.restart();
1849 sWakeLock.release();
1850 break;
1851
Robert Greenwaltf75aa36fc2009-10-22 17:03:47 -07001852 case MESSAGE_UPDATE_STATE:
1853 doUpdateWifiState();
1854 break;
1855
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001856 case MESSAGE_DISABLE_WIFI:
1857 // a non-zero msg.arg1 value means the "enabled" setting
1858 // should be persisted
Dianne Hackborn617f8772009-03-31 15:04:46 -07001859 setWifiEnabledBlocking(false, msg.arg1 == 1, msg.arg2);
Irfan Sheriff7b009782010-03-11 16:37:45 -08001860 if (mWifiWatchdogService != null) {
1861 mWifiWatchdogService.quit();
1862 mWifiWatchdogService = null;
1863 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001864 sWakeLock.release();
1865 break;
1866
1867 case MESSAGE_STOP_WIFI:
1868 mWifiStateTracker.disconnectAndStop();
1869 // don't release wakelock
1870 break;
1871
1872 case MESSAGE_RELEASE_WAKELOCK:
Irfan Sheriff3bf504d2010-03-23 12:24:33 -07001873 sDriverStopWakeLock.release();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001874 break;
Irfan Sheriff5321aef2010-02-12 12:35:59 -08001875
1876 case MESSAGE_START_ACCESS_POINT:
1877 setWifiApEnabledBlocking(true,
Irfan Sheriff9ab518ad2010-03-12 15:48:17 -08001878 msg.arg1,
Irfan Sheriff5321aef2010-02-12 12:35:59 -08001879 (WifiConfiguration) msg.obj);
Irfan Sheriff80cb5982010-03-19 10:40:18 -07001880 sWakeLock.release();
Irfan Sheriff5321aef2010-02-12 12:35:59 -08001881 break;
1882
1883 case MESSAGE_STOP_ACCESS_POINT:
1884 setWifiApEnabledBlocking(false,
Irfan Sheriff9ab518ad2010-03-12 15:48:17 -08001885 msg.arg1,
Irfan Sheriff5321aef2010-02-12 12:35:59 -08001886 (WifiConfiguration) msg.obj);
1887 sWakeLock.release();
1888 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001889 }
1890 }
1891 }
1892
1893 @Override
1894 protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
1895 if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.DUMP)
1896 != PackageManager.PERMISSION_GRANTED) {
1897 pw.println("Permission Denial: can't dump WifiService from from pid="
1898 + Binder.getCallingPid()
1899 + ", uid=" + Binder.getCallingUid());
1900 return;
1901 }
Irfan Sheriff0f344060092010-03-10 10:05:51 -08001902 pw.println("Wi-Fi is " + stateName(mWifiStateTracker.getWifiState()));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001903 pw.println("Stay-awake conditions: " +
1904 Settings.System.getInt(mContext.getContentResolver(),
1905 Settings.System.STAY_ON_WHILE_PLUGGED_IN, 0));
1906 pw.println();
1907
1908 pw.println("Internal state:");
1909 pw.println(mWifiStateTracker);
1910 pw.println();
1911 pw.println("Latest scan results:");
1912 List<ScanResult> scanResults = mWifiStateTracker.getScanResultsList();
1913 if (scanResults != null && scanResults.size() != 0) {
1914 pw.println(" BSSID Frequency RSSI Flags SSID");
1915 for (ScanResult r : scanResults) {
1916 pw.printf(" %17s %9d %5d %-16s %s%n",
1917 r.BSSID,
1918 r.frequency,
1919 r.level,
1920 r.capabilities,
1921 r.SSID == null ? "" : r.SSID);
1922 }
1923 }
1924 pw.println();
Eric Shienbrood5711fad2009-03-27 20:25:31 -07001925 pw.println("Locks acquired: " + mFullLocksAcquired + " full, " +
1926 mScanLocksAcquired + " scan");
1927 pw.println("Locks released: " + mFullLocksReleased + " full, " +
1928 mScanLocksReleased + " scan");
1929 pw.println();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001930 pw.println("Locks held:");
1931 mLocks.dump(pw);
1932 }
1933
1934 private static String stateName(int wifiState) {
1935 switch (wifiState) {
1936 case WIFI_STATE_DISABLING:
1937 return "disabling";
1938 case WIFI_STATE_DISABLED:
1939 return "disabled";
1940 case WIFI_STATE_ENABLING:
1941 return "enabling";
1942 case WIFI_STATE_ENABLED:
1943 return "enabled";
1944 case WIFI_STATE_UNKNOWN:
1945 return "unknown state";
1946 default:
1947 return "[invalid state]";
1948 }
1949 }
1950
Robert Greenwalt58ff0212009-05-19 15:53:54 -07001951 private class WifiLock extends DeathRecipient {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001952 WifiLock(int lockMode, String tag, IBinder binder) {
Robert Greenwalt5347bd42009-05-13 15:10:16 -07001953 super(lockMode, tag, binder);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001954 }
1955
1956 public void binderDied() {
1957 synchronized (mLocks) {
1958 releaseWifiLockLocked(mBinder);
1959 }
1960 }
1961
1962 public String toString() {
Robert Greenwalt5347bd42009-05-13 15:10:16 -07001963 return "WifiLock{" + mTag + " type=" + mMode + " binder=" + mBinder + "}";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001964 }
1965 }
1966
1967 private class LockList {
1968 private List<WifiLock> mList;
1969
1970 private LockList() {
1971 mList = new ArrayList<WifiLock>();
1972 }
1973
1974 private synchronized boolean hasLocks() {
1975 return !mList.isEmpty();
1976 }
1977
1978 private synchronized int getStrongestLockMode() {
1979 if (mList.isEmpty()) {
1980 return WifiManager.WIFI_MODE_FULL;
1981 }
1982 for (WifiLock l : mList) {
Robert Greenwalt5347bd42009-05-13 15:10:16 -07001983 if (l.mMode == WifiManager.WIFI_MODE_FULL) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001984 return WifiManager.WIFI_MODE_FULL;
1985 }
1986 }
1987 return WifiManager.WIFI_MODE_SCAN_ONLY;
1988 }
1989
1990 private void addLock(WifiLock lock) {
1991 if (findLockByBinder(lock.mBinder) < 0) {
1992 mList.add(lock);
1993 }
1994 }
1995
1996 private WifiLock removeLock(IBinder binder) {
1997 int index = findLockByBinder(binder);
1998 if (index >= 0) {
Suchi Amalapurapufff2fda2009-06-30 21:36:16 -07001999 WifiLock ret = mList.remove(index);
2000 ret.unlinkDeathRecipient();
2001 return ret;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002002 } else {
2003 return null;
2004 }
2005 }
2006
2007 private int findLockByBinder(IBinder binder) {
2008 int size = mList.size();
2009 for (int i = size - 1; i >= 0; i--)
2010 if (mList.get(i).mBinder == binder)
2011 return i;
2012 return -1;
2013 }
2014
2015 private void dump(PrintWriter pw) {
2016 for (WifiLock l : mList) {
2017 pw.print(" ");
2018 pw.println(l);
2019 }
2020 }
2021 }
2022
2023 public boolean acquireWifiLock(IBinder binder, int lockMode, String tag) {
2024 mContext.enforceCallingOrSelfPermission(android.Manifest.permission.WAKE_LOCK, null);
2025 if (lockMode != WifiManager.WIFI_MODE_FULL && lockMode != WifiManager.WIFI_MODE_SCAN_ONLY) {
2026 return false;
2027 }
2028 WifiLock wifiLock = new WifiLock(lockMode, tag, binder);
2029 synchronized (mLocks) {
2030 return acquireWifiLockLocked(wifiLock);
2031 }
2032 }
2033
2034 private boolean acquireWifiLockLocked(WifiLock wifiLock) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002035 Slog.d(TAG, "acquireWifiLockLocked: " + wifiLock);
Robert Greenwaltf1acb2d2009-10-13 08:20:55 -07002036
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002037 mLocks.addLock(wifiLock);
Robert Greenwaltf1acb2d2009-10-13 08:20:55 -07002038
The Android Open Source Project10592532009-03-18 17:39:46 -07002039 int uid = Binder.getCallingUid();
2040 long ident = Binder.clearCallingIdentity();
2041 try {
Robert Greenwalt5347bd42009-05-13 15:10:16 -07002042 switch(wifiLock.mMode) {
Eric Shienbrood5711fad2009-03-27 20:25:31 -07002043 case WifiManager.WIFI_MODE_FULL:
2044 ++mFullLocksAcquired;
2045 mBatteryStats.noteFullWifiLockAcquired(uid);
2046 break;
2047 case WifiManager.WIFI_MODE_SCAN_ONLY:
2048 ++mScanLocksAcquired;
2049 mBatteryStats.noteScanWifiLockAcquired(uid);
2050 break;
The Android Open Source Project10592532009-03-18 17:39:46 -07002051 }
2052 } catch (RemoteException e) {
2053 } finally {
2054 Binder.restoreCallingIdentity(ident);
2055 }
Robert Greenwaltf1acb2d2009-10-13 08:20:55 -07002056
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002057 updateWifiState();
2058 return true;
2059 }
2060
2061 public boolean releaseWifiLock(IBinder lock) {
2062 mContext.enforceCallingOrSelfPermission(android.Manifest.permission.WAKE_LOCK, null);
2063 synchronized (mLocks) {
2064 return releaseWifiLockLocked(lock);
2065 }
2066 }
2067
2068 private boolean releaseWifiLockLocked(IBinder lock) {
Eric Shienbroodd4c5f892009-03-24 18:13:20 -07002069 boolean hadLock;
Robert Greenwaltf1acb2d2009-10-13 08:20:55 -07002070
The Android Open Source Project10592532009-03-18 17:39:46 -07002071 WifiLock wifiLock = mLocks.removeLock(lock);
Robert Greenwaltf1acb2d2009-10-13 08:20:55 -07002072
Joe Onorato8a9b2202010-02-26 18:56:32 -08002073 Slog.d(TAG, "releaseWifiLockLocked: " + wifiLock);
Robert Greenwaltf1acb2d2009-10-13 08:20:55 -07002074
Eric Shienbroodd4c5f892009-03-24 18:13:20 -07002075 hadLock = (wifiLock != null);
2076
2077 if (hadLock) {
2078 int uid = Binder.getCallingUid();
2079 long ident = Binder.clearCallingIdentity();
2080 try {
Robert Greenwalt5347bd42009-05-13 15:10:16 -07002081 switch(wifiLock.mMode) {
Eric Shienbrood5711fad2009-03-27 20:25:31 -07002082 case WifiManager.WIFI_MODE_FULL:
2083 ++mFullLocksReleased;
2084 mBatteryStats.noteFullWifiLockReleased(uid);
2085 break;
2086 case WifiManager.WIFI_MODE_SCAN_ONLY:
2087 ++mScanLocksReleased;
2088 mBatteryStats.noteScanWifiLockReleased(uid);
2089 break;
Eric Shienbroodd4c5f892009-03-24 18:13:20 -07002090 }
2091 } catch (RemoteException e) {
2092 } finally {
2093 Binder.restoreCallingIdentity(ident);
The Android Open Source Project10592532009-03-18 17:39:46 -07002094 }
The Android Open Source Project10592532009-03-18 17:39:46 -07002095 }
Robert Greenwaltf1acb2d2009-10-13 08:20:55 -07002096 // TODO - should this only happen if you hadLock?
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002097 updateWifiState();
Eric Shienbroodd4c5f892009-03-24 18:13:20 -07002098 return hadLock;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002099 }
Robert Greenwalt5347bd42009-05-13 15:10:16 -07002100
Robert Greenwalt58ff0212009-05-19 15:53:54 -07002101 private abstract class DeathRecipient
Robert Greenwalt5347bd42009-05-13 15:10:16 -07002102 implements IBinder.DeathRecipient {
2103 String mTag;
2104 int mMode;
2105 IBinder mBinder;
2106
Robert Greenwalt58ff0212009-05-19 15:53:54 -07002107 DeathRecipient(int mode, String tag, IBinder binder) {
Robert Greenwalt5347bd42009-05-13 15:10:16 -07002108 super();
2109 mTag = tag;
2110 mMode = mode;
2111 mBinder = binder;
2112 try {
2113 mBinder.linkToDeath(this, 0);
2114 } catch (RemoteException e) {
2115 binderDied();
2116 }
2117 }
Suchi Amalapurapufff2fda2009-06-30 21:36:16 -07002118
2119 void unlinkDeathRecipient() {
2120 mBinder.unlinkToDeath(this, 0);
2121 }
Robert Greenwalt5347bd42009-05-13 15:10:16 -07002122 }
2123
Robert Greenwalt58ff0212009-05-19 15:53:54 -07002124 private class Multicaster extends DeathRecipient {
2125 Multicaster(String tag, IBinder binder) {
Robert Greenwalt5347bd42009-05-13 15:10:16 -07002126 super(Binder.getCallingUid(), tag, binder);
2127 }
2128
2129 public void binderDied() {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002130 Slog.e(TAG, "Multicaster binderDied");
Robert Greenwalt5347bd42009-05-13 15:10:16 -07002131 synchronized (mMulticasters) {
2132 int i = mMulticasters.indexOf(this);
2133 if (i != -1) {
2134 removeMulticasterLocked(i, mMode);
2135 }
2136 }
2137 }
2138
2139 public String toString() {
Robert Greenwalt58ff0212009-05-19 15:53:54 -07002140 return "Multicaster{" + mTag + " binder=" + mBinder + "}";
Robert Greenwalt5347bd42009-05-13 15:10:16 -07002141 }
2142
2143 public int getUid() {
2144 return mMode;
2145 }
2146 }
2147
Robert Greenwalte2d155a2009-10-21 14:58:34 -07002148 public void initializeMulticastFiltering() {
2149 enforceMulticastChangePermission();
Irfan Sheriffa8fbe1f2010-03-09 09:13:58 -08002150
Robert Greenwalte2d155a2009-10-21 14:58:34 -07002151 synchronized (mMulticasters) {
2152 // if anybody had requested filters be off, leave off
2153 if (mMulticasters.size() != 0) {
2154 return;
2155 } else {
Irfan Sheriffa8fbe1f2010-03-09 09:13:58 -08002156 mWifiStateTracker.startPacketFiltering();
Robert Greenwalte2d155a2009-10-21 14:58:34 -07002157 }
2158 }
2159 }
2160
Robert Greenwaltfc1b15c2009-05-22 15:09:51 -07002161 public void acquireMulticastLock(IBinder binder, String tag) {
2162 enforceMulticastChangePermission();
Robert Greenwalt5347bd42009-05-13 15:10:16 -07002163
2164 synchronized (mMulticasters) {
2165 mMulticastEnabled++;
Robert Greenwalt58ff0212009-05-19 15:53:54 -07002166 mMulticasters.add(new Multicaster(tag, binder));
Robert Greenwalt5347bd42009-05-13 15:10:16 -07002167 // Note that we could call stopPacketFiltering only when
2168 // our new size == 1 (first call), but this function won't
2169 // be called often and by making the stopPacket call each
2170 // time we're less fragile and self-healing.
Irfan Sheriffa8fbe1f2010-03-09 09:13:58 -08002171 mWifiStateTracker.stopPacketFiltering();
Robert Greenwalt5347bd42009-05-13 15:10:16 -07002172 }
2173
2174 int uid = Binder.getCallingUid();
2175 Long ident = Binder.clearCallingIdentity();
2176 try {
2177 mBatteryStats.noteWifiMulticastEnabled(uid);
2178 } catch (RemoteException e) {
2179 } finally {
2180 Binder.restoreCallingIdentity(ident);
2181 }
2182 }
2183
Robert Greenwaltfc1b15c2009-05-22 15:09:51 -07002184 public void releaseMulticastLock() {
2185 enforceMulticastChangePermission();
Robert Greenwalt5347bd42009-05-13 15:10:16 -07002186
2187 int uid = Binder.getCallingUid();
2188 synchronized (mMulticasters) {
2189 mMulticastDisabled++;
2190 int size = mMulticasters.size();
2191 for (int i = size - 1; i >= 0; i--) {
Robert Greenwalt58ff0212009-05-19 15:53:54 -07002192 Multicaster m = mMulticasters.get(i);
Robert Greenwalt5347bd42009-05-13 15:10:16 -07002193 if ((m != null) && (m.getUid() == uid)) {
2194 removeMulticasterLocked(i, uid);
2195 }
2196 }
2197 }
2198 }
2199
2200 private void removeMulticasterLocked(int i, int uid)
2201 {
Suchi Amalapurapufff2fda2009-06-30 21:36:16 -07002202 Multicaster removed = mMulticasters.remove(i);
Irfan Sheriffa8fbe1f2010-03-09 09:13:58 -08002203
Suchi Amalapurapufff2fda2009-06-30 21:36:16 -07002204 if (removed != null) {
2205 removed.unlinkDeathRecipient();
2206 }
Robert Greenwalt5347bd42009-05-13 15:10:16 -07002207 if (mMulticasters.size() == 0) {
Irfan Sheriffa8fbe1f2010-03-09 09:13:58 -08002208 mWifiStateTracker.startPacketFiltering();
Robert Greenwalt5347bd42009-05-13 15:10:16 -07002209 }
2210
2211 Long ident = Binder.clearCallingIdentity();
2212 try {
2213 mBatteryStats.noteWifiMulticastDisabled(uid);
2214 } catch (RemoteException e) {
2215 } finally {
2216 Binder.restoreCallingIdentity(ident);
2217 }
2218 }
2219
Robert Greenwalt58ff0212009-05-19 15:53:54 -07002220 public boolean isMulticastEnabled() {
Robert Greenwalt5347bd42009-05-13 15:10:16 -07002221 enforceAccessPermission();
2222
2223 synchronized (mMulticasters) {
2224 return (mMulticasters.size() > 0);
2225 }
2226 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002227}