blob: f9b94a309f255f4c494b14669ed35cd27b1c50e9 [file] [log] [blame]
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001/*
Irfan Sheriffa2a1b912010-06-07 09:03:04 -07002 * Copyright (C) 2010 The Android Open Source Project
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003 *
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
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080019import android.app.AlarmManager;
Irfan Sheriff0d255342010-07-28 09:35:20 -070020import android.app.Notification;
21import android.app.NotificationManager;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080022import android.app.PendingIntent;
Jaikumar Ganesh7440fc22010-09-27 17:04:14 -070023import android.bluetooth.BluetoothAdapter;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080024import android.content.BroadcastReceiver;
25import android.content.ContentResolver;
26import android.content.Context;
27import android.content.Intent;
28import android.content.IntentFilter;
29import android.content.pm.PackageManager;
Irfan Sheriff0d255342010-07-28 09:35:20 -070030import android.database.ContentObserver;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080031import android.net.wifi.IWifiManager;
32import android.net.wifi.WifiInfo;
33import android.net.wifi.WifiManager;
Irfan Sheriff0d255342010-07-28 09:35:20 -070034import android.net.wifi.WifiStateMachine;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080035import android.net.wifi.ScanResult;
36import android.net.wifi.WifiConfiguration;
San Mehat0310f9a2009-07-07 10:49:47 -070037import android.net.wifi.SupplicantState;
Irfan Sheriff9ab518ad2010-03-12 15:48:17 -080038import android.net.wifi.WifiConfiguration.KeyMgmt;
Irfan Sheriff02fb46a2010-12-08 11:27:37 -080039import android.net.wifi.WpsConfiguration;
Irfan Sheriffe4c56c92011-01-12 16:33:58 -080040import android.net.wifi.WpsResult;
Irfan Sheriff5321aef2010-02-12 12:35:59 -080041import android.net.ConnectivityManager;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080042import android.net.DhcpInfo;
Irfan Sheriff0d255342010-07-28 09:35:20 -070043import android.net.NetworkInfo;
44import android.net.NetworkInfo.State;
Irfan Sheriff227bec42011-02-15 19:30:27 -080045import android.net.NetworkInfo.DetailedState;
46import android.net.TrafficStats;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080047import android.os.Binder;
Irfan Sheriff0d255342010-07-28 09:35:20 -070048import android.os.Handler;
Irfan Sheriff227bec42011-02-15 19:30:27 -080049import android.os.Messenger;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080050import android.os.HandlerThread;
51import android.os.IBinder;
Irfan Sheriff5321aef2010-02-12 12:35:59 -080052import android.os.INetworkManagementService;
Irfan Sheriff0d255342010-07-28 09:35:20 -070053import android.os.Message;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080054import android.os.RemoteException;
Amith Yamasani47873e52009-07-02 12:05:32 -070055import android.os.ServiceManager;
Irfan Sheriff227bec42011-02-15 19:30:27 -080056import android.os.SystemProperties;
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -070057import android.os.WorkSource;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080058import android.provider.Settings;
Irfan Sheriff0d255342010-07-28 09:35:20 -070059import android.text.TextUtils;
Joe Onorato8a9b2202010-02-26 18:56:32 -080060import android.util.Slog;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080061
62import java.util.ArrayList;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080063import java.util.List;
Jaikumar Ganesh084c6652009-12-07 10:58:18 -080064import java.util.Set;
Irfan Sheriffa2a1b912010-06-07 09:03:04 -070065import java.util.concurrent.atomic.AtomicBoolean;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080066import java.io.FileDescriptor;
67import java.io.PrintWriter;
68
The Android Open Source Project10592532009-03-18 17:39:46 -070069import com.android.internal.app.IBatteryStats;
Wink Saville4b7ba092010-10-20 15:37:41 -070070import com.android.internal.util.AsyncChannel;
The Android Open Source Project10592532009-03-18 17:39:46 -070071import com.android.server.am.BatteryStatsService;
Irfan Sheriff9ab518ad2010-03-12 15:48:17 -080072import com.android.internal.R;
The Android Open Source Project10592532009-03-18 17:39:46 -070073
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080074/**
75 * WifiService handles remote WiFi operation requests by implementing
Irfan Sheriffa2a1b912010-06-07 09:03:04 -070076 * the IWifiManager interface.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080077 *
78 * @hide
79 */
Irfan Sheriffa2a1b912010-06-07 09:03:04 -070080//TODO: Clean up multiple locks and implement WifiService
81// as a SM to track soft AP/client/adhoc bring up based
82// on device idle state, airplane mode and boot.
83
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080084public class WifiService extends IWifiManager.Stub {
85 private static final String TAG = "WifiService";
Irfan Sheriffa2a1b912010-06-07 09:03:04 -070086 private static final boolean DBG = true;
87
Irfan Sheriff0d255342010-07-28 09:35:20 -070088 private final WifiStateMachine mWifiStateMachine;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080089
90 private Context mContext;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080091
92 private AlarmManager mAlarmManager;
93 private PendingIntent mIdleIntent;
94 private static final int IDLE_REQUEST = 0;
95 private boolean mScreenOff;
96 private boolean mDeviceIdle;
97 private int mPluggedType;
98
Irfan Sherifffcc08452011-02-17 16:44:54 -080099 /* Chipset supports background scan */
100 private final boolean mBackgroundScanSupported;
101
Mike Lockwoodbd5ddf02009-07-29 21:37:14 -0700102 // true if the user enabled Wifi while in airplane mode
Irfan Sheriffa2a1b912010-06-07 09:03:04 -0700103 private AtomicBoolean mAirplaneModeOverwridden = new AtomicBoolean(false);
Mike Lockwoodbd5ddf02009-07-29 21:37:14 -0700104
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800105 private final LockList mLocks = new LockList();
Eric Shienbrood5711fad2009-03-27 20:25:31 -0700106 // some wifi lock statistics
Irfan Sheriff5876a422010-08-12 20:26:23 -0700107 private int mFullHighPerfLocksAcquired;
108 private int mFullHighPerfLocksReleased;
Eric Shienbrood5711fad2009-03-27 20:25:31 -0700109 private int mFullLocksAcquired;
110 private int mFullLocksReleased;
111 private int mScanLocksAcquired;
112 private int mScanLocksReleased;
The Android Open Source Project10592532009-03-18 17:39:46 -0700113
Robert Greenwalt58ff0212009-05-19 15:53:54 -0700114 private final List<Multicaster> mMulticasters =
115 new ArrayList<Multicaster>();
Robert Greenwalt5347bd42009-05-13 15:10:16 -0700116 private int mMulticastEnabled;
117 private int mMulticastDisabled;
118
The Android Open Source Project10592532009-03-18 17:39:46 -0700119 private final IBatteryStats mBatteryStats;
Jaikumar Ganesh084c6652009-12-07 10:58:18 -0800120
Irfan Sheriff227bec42011-02-15 19:30:27 -0800121 private boolean mEnableTrafficStatsPoll = false;
122 private int mTrafficStatsPollToken = 0;
123 private long mTxPkts;
124 private long mRxPkts;
125 /* Tracks last reported data activity */
126 private int mDataActivity;
127 private String mInterfaceName;
128
129 /**
130 * Interval in milliseconds between polling for traffic
131 * statistics
132 */
133 private static final int POLL_TRAFFIC_STATS_INTERVAL_MSECS = 1000;
134
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800135 /**
Doug Zongker43866e02010-01-07 12:09:54 -0800136 * See {@link Settings.Secure#WIFI_IDLE_MS}. This is the default value if a
137 * Settings.Secure value is not present. This timeout value is chosen as
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800138 * the approximate point at which the battery drain caused by Wi-Fi
139 * being enabled but not active exceeds the battery drain caused by
140 * re-establishing a connection to the mobile data network.
141 */
Irfan Sheriff4f5f7c92010-10-14 17:01:27 -0700142 private static final long DEFAULT_IDLE_MS = 15 * 60 * 1000; /* 15 minutes */
143
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800144 private static final String ACTION_DEVICE_IDLE =
145 "com.android.server.WifiManager.action.DEVICE_IDLE";
146
Irfan Sheriff227bec42011-02-15 19:30:27 -0800147 private static final int CMD_ENABLE_TRAFFIC_STATS_POLL = 1;
148 private static final int CMD_TRAFFIC_STATS_POLL = 2;
149
Irfan Sheriffa2a1b912010-06-07 09:03:04 -0700150 private boolean mIsReceiverRegistered = false;
151
Irfan Sheriff0d255342010-07-28 09:35:20 -0700152
153 NetworkInfo mNetworkInfo = new NetworkInfo(ConnectivityManager.TYPE_WIFI, 0, "WIFI", "");
154
155 // Variables relating to the 'available networks' notification
156 /**
157 * The icon to show in the 'available networks' notification. This will also
158 * be the ID of the Notification given to the NotificationManager.
159 */
160 private static final int ICON_NETWORKS_AVAILABLE =
161 com.android.internal.R.drawable.stat_notify_wifi_in_range;
162 /**
163 * When a notification is shown, we wait this amount before possibly showing it again.
164 */
165 private final long NOTIFICATION_REPEAT_DELAY_MS;
166 /**
167 * Whether the user has set the setting to show the 'available networks' notification.
168 */
169 private boolean mNotificationEnabled;
170 /**
171 * Observes the user setting to keep {@link #mNotificationEnabled} in sync.
172 */
173 private NotificationEnabledSettingObserver mNotificationEnabledSettingObserver;
174 /**
175 * The {@link System#currentTimeMillis()} must be at least this value for us
176 * to show the notification again.
177 */
178 private long mNotificationRepeatTime;
179 /**
180 * The Notification object given to the NotificationManager.
181 */
182 private Notification mNotification;
183 /**
184 * Whether the notification is being shown, as set by us. That is, if the
185 * user cancels the notification, we will not receive the callback so this
186 * will still be true. We only guarantee if this is false, then the
187 * notification is not showing.
188 */
189 private boolean mNotificationShown;
190 /**
191 * The number of continuous scans that must occur before consider the
192 * supplicant in a scanning state. This allows supplicant to associate with
193 * remembered networks that are in the scan results.
194 */
195 private static final int NUM_SCANS_BEFORE_ACTUALLY_SCANNING = 3;
196 /**
197 * The number of scans since the last network state change. When this
198 * exceeds {@link #NUM_SCANS_BEFORE_ACTUALLY_SCANNING}, we consider the
199 * supplicant to actually be scanning. When the network state changes to
200 * something other than scanning, we reset this to 0.
201 */
202 private int mNumScansSinceNetworkStateChange;
Jaikumar Ganesh7440fc22010-09-27 17:04:14 -0700203
Dianne Hackborn03f3cb02010-09-17 23:12:26 -0700204 /**
Wink Saville4b7ba092010-10-20 15:37:41 -0700205 * Asynchronous channel to WifiStateMachine
206 */
Irfan Sheriff227bec42011-02-15 19:30:27 -0800207 private AsyncChannel mWifiStateMachineChannel;
Wink Saville4b7ba092010-10-20 15:37:41 -0700208
209 /**
Irfan Sheriff227bec42011-02-15 19:30:27 -0800210 * Clients receiving asynchronous messages
Wink Saville4b7ba092010-10-20 15:37:41 -0700211 */
Irfan Sheriff227bec42011-02-15 19:30:27 -0800212 private List<AsyncChannel> mClients = new ArrayList<AsyncChannel>();
Wink Saville4b7ba092010-10-20 15:37:41 -0700213
Irfan Sheriff227bec42011-02-15 19:30:27 -0800214 /**
215 * Handles client connections
216 */
217 private class AsyncServiceHandler extends Handler {
218
219 AsyncServiceHandler(android.os.Looper looper) {
Wink Saville4b7ba092010-10-20 15:37:41 -0700220 super(looper);
Wink Saville4b7ba092010-10-20 15:37:41 -0700221 }
222
223 @Override
224 public void handleMessage(Message msg) {
225 switch (msg.what) {
226 case AsyncChannel.CMD_CHANNEL_HALF_CONNECTED: {
227 if (msg.arg1 == AsyncChannel.STATUS_SUCCESSFUL) {
Irfan Sheriff227bec42011-02-15 19:30:27 -0800228 Slog.d(TAG, "New client listening to asynchronous messages");
229 mClients.add((AsyncChannel) msg.obj);
Wink Saville4b7ba092010-10-20 15:37:41 -0700230 } else {
Irfan Sheriff227bec42011-02-15 19:30:27 -0800231 Slog.e(TAG, "Client connection failure, error=" + msg.arg1);
232 }
233 break;
234 }
235 case AsyncChannel.CMD_CHANNEL_FULL_CONNECTION: {
236 AsyncChannel ac = new AsyncChannel();
237 ac.connect(mContext, this, msg.replyTo);
238 break;
239 }
240 case CMD_ENABLE_TRAFFIC_STATS_POLL: {
241 mEnableTrafficStatsPoll = (msg.arg1 == 1);
242 mTrafficStatsPollToken++;
243 if (mEnableTrafficStatsPoll) {
244 notifyOnDataActivity();
245 sendMessageDelayed(Message.obtain(this, CMD_TRAFFIC_STATS_POLL,
246 mTrafficStatsPollToken, 0), POLL_TRAFFIC_STATS_INTERVAL_MSECS);
247 }
248 break;
249 }
250 case CMD_TRAFFIC_STATS_POLL: {
251 if (msg.arg1 == mTrafficStatsPollToken) {
252 notifyOnDataActivity();
253 sendMessageDelayed(Message.obtain(this, CMD_TRAFFIC_STATS_POLL,
254 mTrafficStatsPollToken, 0), POLL_TRAFFIC_STATS_INTERVAL_MSECS);
Wink Saville4b7ba092010-10-20 15:37:41 -0700255 }
256 break;
257 }
258 default: {
259 Slog.d(TAG, "WifiServicehandler.handleMessage ignoring msg=" + msg);
260 break;
261 }
262 }
263 }
264 }
Irfan Sheriff227bec42011-02-15 19:30:27 -0800265 private AsyncServiceHandler mAsyncServiceHandler;
266
267 /**
268 * Handles interaction with WifiStateMachine
269 */
270 private class WifiStateMachineHandler extends Handler {
271 private AsyncChannel mWsmChannel;
272
273 WifiStateMachineHandler(android.os.Looper looper) {
274 super(looper);
275 mWsmChannel = new AsyncChannel();
276 mWsmChannel.connect(mContext, this, mWifiStateMachine.getHandler());
277 }
278
279 @Override
280 public void handleMessage(Message msg) {
281 switch (msg.what) {
282 case AsyncChannel.CMD_CHANNEL_HALF_CONNECTED: {
283 if (msg.arg1 == AsyncChannel.STATUS_SUCCESSFUL) {
284 mWifiStateMachineChannel = mWsmChannel;
285 } else {
286 Slog.e(TAG, "WifiStateMachine connection failure, error=" + msg.arg1);
287 mWifiStateMachineChannel = null;
288 }
289 break;
290 }
291 default: {
292 Slog.d(TAG, "WifiStateMachineHandler.handleMessage ignoring msg=" + msg);
293 break;
294 }
295 }
296 }
297 }
298 WifiStateMachineHandler mWifiStateMachineHandler;
Wink Saville4b7ba092010-10-20 15:37:41 -0700299
300 /**
Dianne Hackborn03f3cb02010-09-17 23:12:26 -0700301 * Temporary for computing UIDS that are responsible for starting WIFI.
302 * Protected by mWifiStateTracker lock.
303 */
304 private final WorkSource mTmpWorkSource = new WorkSource();
Irfan Sheriff0d255342010-07-28 09:35:20 -0700305
306 WifiService(Context context) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800307 mContext = context;
Irfan Sheriff227bec42011-02-15 19:30:27 -0800308
309 mInterfaceName = SystemProperties.get("wifi.interface", "wlan0");
310
311 mWifiStateMachine = new WifiStateMachine(mContext, mInterfaceName);
Irfan Sheriff0d255342010-07-28 09:35:20 -0700312 mWifiStateMachine.enableRssiPolling(true);
The Android Open Source Project10592532009-03-18 17:39:46 -0700313 mBatteryStats = BatteryStatsService.getService();
Jaikumar Ganesh084c6652009-12-07 10:58:18 -0800314
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800315 mAlarmManager = (AlarmManager)mContext.getSystemService(Context.ALARM_SERVICE);
316 Intent idleIntent = new Intent(ACTION_DEVICE_IDLE, null);
317 mIdleIntent = PendingIntent.getBroadcast(mContext, IDLE_REQUEST, idleIntent, 0);
318
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800319 mContext.registerReceiver(
320 new BroadcastReceiver() {
321 @Override
322 public void onReceive(Context context, Intent intent) {
Mike Lockwoodbd5ddf02009-07-29 21:37:14 -0700323 // clear our flag indicating the user has overwridden airplane mode
Irfan Sheriffa2a1b912010-06-07 09:03:04 -0700324 mAirplaneModeOverwridden.set(false);
Irfan Sheriffb2e6c012010-04-05 11:57:56 -0700325 // on airplane disable, restore Wifi if the saved state indicates so
326 if (!isAirplaneModeOn() && testAndClearWifiSavedState()) {
327 persistWifiEnabled(true);
328 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800329 updateWifiState();
330 }
331 },
332 new IntentFilter(Intent.ACTION_AIRPLANE_MODE_CHANGED));
333
Irfan Sheriff0d255342010-07-28 09:35:20 -0700334 IntentFilter filter = new IntentFilter();
335 filter.addAction(WifiManager.WIFI_STATE_CHANGED_ACTION);
336 filter.addAction(WifiManager.NETWORK_STATE_CHANGED_ACTION);
337 filter.addAction(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION);
338
339 mContext.registerReceiver(
340 new BroadcastReceiver() {
341 @Override
342 public void onReceive(Context context, Intent intent) {
343 if (intent.getAction().equals(WifiManager.WIFI_STATE_CHANGED_ACTION)) {
344 // reset & clear notification on any wifi state change
345 resetNotification();
346 } else if (intent.getAction().equals(
347 WifiManager.NETWORK_STATE_CHANGED_ACTION)) {
348 mNetworkInfo = (NetworkInfo) intent.getParcelableExtra(
349 WifiManager.EXTRA_NETWORK_INFO);
350 // reset & clear notification on a network connect & disconnect
351 switch(mNetworkInfo.getDetailedState()) {
352 case CONNECTED:
353 case DISCONNECTED:
Irfan Sheriff227bec42011-02-15 19:30:27 -0800354 evaluateTrafficStatsPolling();
Irfan Sheriff0d255342010-07-28 09:35:20 -0700355 resetNotification();
356 break;
357 }
358 } else if (intent.getAction().equals(
359 WifiManager.SCAN_RESULTS_AVAILABLE_ACTION)) {
360 checkAndSetNotification();
361 }
362 }
363 }, filter);
364
Irfan Sheriff227bec42011-02-15 19:30:27 -0800365 HandlerThread wifiThread = new HandlerThread("WifiService");
366 wifiThread.start();
367 mAsyncServiceHandler = new AsyncServiceHandler(wifiThread.getLooper());
368 mWifiStateMachineHandler = new WifiStateMachineHandler(wifiThread.getLooper());
369
Irfan Sheriff0d255342010-07-28 09:35:20 -0700370 // Setting is in seconds
371 NOTIFICATION_REPEAT_DELAY_MS = Settings.Secure.getInt(context.getContentResolver(),
372 Settings.Secure.WIFI_NETWORKS_AVAILABLE_REPEAT_DELAY, 900) * 1000l;
373 mNotificationEnabledSettingObserver = new NotificationEnabledSettingObserver(new Handler());
374 mNotificationEnabledSettingObserver.register();
Irfan Sherifffcc08452011-02-17 16:44:54 -0800375
376 mBackgroundScanSupported = mContext.getResources().getBoolean(
377 com.android.internal.R.bool.config_wifi_background_scan_support);
Irfan Sheriff7b009782010-03-11 16:37:45 -0800378 }
Irfan Sheriff5321aef2010-02-12 12:35:59 -0800379
Irfan Sheriff7b009782010-03-11 16:37:45 -0800380 /**
381 * Check if Wi-Fi needs to be enabled and start
382 * if needed
Irfan Sheriff60e3ba02010-04-02 12:18:45 -0700383 *
384 * This function is used only at boot time
Irfan Sheriff7b009782010-03-11 16:37:45 -0800385 */
Irfan Sheriff0d255342010-07-28 09:35:20 -0700386 public void checkAndStartWifi() {
Irfan Sheriffa3bd4092010-03-24 17:58:59 -0700387 /* Start if Wi-Fi is enabled or the saved state indicates Wi-Fi was on */
Irfan Sheriff60e3ba02010-04-02 12:18:45 -0700388 boolean wifiEnabled = !isAirplaneModeOn()
389 && (getPersistedWifiEnabled() || testAndClearWifiSavedState());
Irfan Sheriff7b009782010-03-11 16:37:45 -0800390 Slog.i(TAG, "WifiService starting up with Wi-Fi " +
391 (wifiEnabled ? "enabled" : "disabled"));
Irfan Sheriffb99fe5e2010-03-26 14:56:07 -0700392 setWifiEnabled(wifiEnabled);
Irfan Sheriff5321aef2010-02-12 12:35:59 -0800393 }
394
Irfan Sheriffa3bd4092010-03-24 17:58:59 -0700395 private boolean testAndClearWifiSavedState() {
396 final ContentResolver cr = mContext.getContentResolver();
397 int wifiSavedState = 0;
398 try {
399 wifiSavedState = Settings.Secure.getInt(cr, Settings.Secure.WIFI_SAVED_STATE);
400 if(wifiSavedState == 1)
401 Settings.Secure.putInt(cr, Settings.Secure.WIFI_SAVED_STATE, 0);
402 } catch (Settings.SettingNotFoundException e) {
403 ;
404 }
405 return (wifiSavedState == 1);
406 }
407
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800408 private boolean getPersistedWifiEnabled() {
409 final ContentResolver cr = mContext.getContentResolver();
410 try {
411 return Settings.Secure.getInt(cr, Settings.Secure.WIFI_ON) == 1;
412 } catch (Settings.SettingNotFoundException e) {
413 Settings.Secure.putInt(cr, Settings.Secure.WIFI_ON, 0);
414 return false;
415 }
416 }
417
418 private void persistWifiEnabled(boolean enabled) {
419 final ContentResolver cr = mContext.getContentResolver();
420 Settings.Secure.putInt(cr, Settings.Secure.WIFI_ON, enabled ? 1 : 0);
421 }
422
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800423 /**
424 * see {@link android.net.wifi.WifiManager#pingSupplicant()}
Irfan Sheriffa2a1b912010-06-07 09:03:04 -0700425 * @return {@code true} if the operation succeeds, {@code false} otherwise
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800426 */
427 public boolean pingSupplicant() {
Irfan Sheriffa2a1b912010-06-07 09:03:04 -0700428 enforceAccessPermission();
Irfan Sheriff227bec42011-02-15 19:30:27 -0800429 if (mWifiStateMachineChannel != null) {
430 return mWifiStateMachine.syncPingSupplicant(mWifiStateMachineChannel);
Irfan Sheriff1406bcb2010-10-28 14:41:39 -0700431 } else {
Irfan Sheriff227bec42011-02-15 19:30:27 -0800432 Slog.e(TAG, "mWifiStateMachineChannel is not initialized");
Irfan Sheriff1406bcb2010-10-28 14:41:39 -0700433 return false;
434 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800435 }
436
437 /**
438 * see {@link android.net.wifi.WifiManager#startScan()}
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800439 */
Irfan Sheriffe4984752010-08-19 11:29:22 -0700440 public void startScan(boolean forceActive) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800441 enforceChangePermission();
Irfan Sheriffe4984752010-08-19 11:29:22 -0700442 mWifiStateMachine.startScan(forceActive);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800443 }
444
445 private void enforceAccessPermission() {
446 mContext.enforceCallingOrSelfPermission(android.Manifest.permission.ACCESS_WIFI_STATE,
447 "WifiService");
448 }
449
450 private void enforceChangePermission() {
451 mContext.enforceCallingOrSelfPermission(android.Manifest.permission.CHANGE_WIFI_STATE,
452 "WifiService");
453
454 }
455
Robert Greenwaltfc1b15c2009-05-22 15:09:51 -0700456 private void enforceMulticastChangePermission() {
457 mContext.enforceCallingOrSelfPermission(
458 android.Manifest.permission.CHANGE_WIFI_MULTICAST_STATE,
459 "WifiService");
460 }
461
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800462 /**
Irfan Sheriffa2a1b912010-06-07 09:03:04 -0700463 * see {@link android.net.wifi.WifiManager#setWifiEnabled(boolean)}
464 * @param enable {@code true} to enable, {@code false} to disable.
465 * @return {@code true} if the enable/disable operation was
466 * started or is already in the queue.
467 */
468 public synchronized boolean setWifiEnabled(boolean enable) {
469 enforceChangePermission();
470
471 if (DBG) {
Irfan Sheriff0d255342010-07-28 09:35:20 -0700472 Slog.e(TAG, "Invoking mWifiStateMachine.setWifiEnabled\n");
Irfan Sheriffa2a1b912010-06-07 09:03:04 -0700473 }
474
475 // set a flag if the user is enabling Wifi while in airplane mode
476 if (enable && isAirplaneModeOn() && isAirplaneToggleable()) {
477 mAirplaneModeOverwridden.set(true);
478 }
479
Dianne Hackborn03f3cb02010-09-17 23:12:26 -0700480 if (enable) {
481 reportStartWorkSource();
482 }
Irfan Sheriff0d255342010-07-28 09:35:20 -0700483 mWifiStateMachine.setWifiEnabled(enable);
Irfan Sheriff61180692010-08-18 16:07:39 -0700484
485 /*
486 * Caller might not have WRITE_SECURE_SETTINGS,
487 * only CHANGE_WIFI_STATE is enforced
488 */
489 long ident = Binder.clearCallingIdentity();
Irfan Sheriffa2a1b912010-06-07 09:03:04 -0700490 persistWifiEnabled(enable);
Irfan Sheriff61180692010-08-18 16:07:39 -0700491 Binder.restoreCallingIdentity(ident);
Irfan Sheriffa2a1b912010-06-07 09:03:04 -0700492
493 if (enable) {
494 if (!mIsReceiverRegistered) {
495 registerForBroadcasts();
496 mIsReceiverRegistered = true;
497 }
498 } else if (mIsReceiverRegistered){
499 mContext.unregisterReceiver(mReceiver);
500 mIsReceiverRegistered = false;
501 }
502
503 return true;
504 }
505
506 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800507 * see {@link WifiManager#getWifiState()}
508 * @return One of {@link WifiManager#WIFI_STATE_DISABLED},
509 * {@link WifiManager#WIFI_STATE_DISABLING},
510 * {@link WifiManager#WIFI_STATE_ENABLED},
511 * {@link WifiManager#WIFI_STATE_ENABLING},
512 * {@link WifiManager#WIFI_STATE_UNKNOWN}
513 */
514 public int getWifiEnabledState() {
515 enforceAccessPermission();
Irfan Sheriffd8134ff2010-08-22 17:06:34 -0700516 return mWifiStateMachine.syncGetWifiState();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800517 }
518
519 /**
Irfan Sheriffc2f54c22010-03-18 14:02:22 -0700520 * see {@link android.net.wifi.WifiManager#setWifiApEnabled(WifiConfiguration, boolean)}
Irfan Sheriff5321aef2010-02-12 12:35:59 -0800521 * @param wifiConfig SSID, security and channel details as
522 * part of WifiConfiguration
Irfan Sheriffa2a1b912010-06-07 09:03:04 -0700523 * @param enabled true to enable and false to disable
Irfan Sheriff5321aef2010-02-12 12:35:59 -0800524 * @return {@code true} if the start operation was
525 * started or is already in the queue.
526 */
Irfan Sheriffa2a1b912010-06-07 09:03:04 -0700527 public synchronized boolean setWifiApEnabled(WifiConfiguration wifiConfig, boolean enabled) {
Irfan Sheriff5321aef2010-02-12 12:35:59 -0800528 enforceChangePermission();
Irfan Sheriff5321aef2010-02-12 12:35:59 -0800529
Irfan Sheriffa2a1b912010-06-07 09:03:04 -0700530 if (enabled) {
531 /* Use default config if there is no existing config */
532 if (wifiConfig == null && ((wifiConfig = getWifiApConfiguration()) == null)) {
533 wifiConfig = new WifiConfiguration();
534 wifiConfig.SSID = mContext.getString(R.string.wifi_tether_configure_ssid_default);
535 wifiConfig.allowedKeyManagement.set(KeyMgmt.NONE);
536 }
Irfan Sheriff61180692010-08-18 16:07:39 -0700537 /*
538 * Caller might not have WRITE_SECURE_SETTINGS,
539 * only CHANGE_WIFI_STATE is enforced
540 */
541 long ident = Binder.clearCallingIdentity();
Irfan Sheriffa2a1b912010-06-07 09:03:04 -0700542 setWifiApConfiguration(wifiConfig);
Irfan Sheriff61180692010-08-18 16:07:39 -0700543 Binder.restoreCallingIdentity(ident);
Irfan Sheriff5321aef2010-02-12 12:35:59 -0800544 }
545
Irfan Sheriff0d255342010-07-28 09:35:20 -0700546 mWifiStateMachine.setWifiApEnabled(wifiConfig, enabled);
Irfan Sheriffa2a1b912010-06-07 09:03:04 -0700547
Irfan Sheriff5321aef2010-02-12 12:35:59 -0800548 return true;
549 }
550
Irfan Sheriffa2a1b912010-06-07 09:03:04 -0700551 /**
552 * see {@link WifiManager#getWifiApState()}
553 * @return One of {@link WifiManager#WIFI_AP_STATE_DISABLED},
554 * {@link WifiManager#WIFI_AP_STATE_DISABLING},
555 * {@link WifiManager#WIFI_AP_STATE_ENABLED},
556 * {@link WifiManager#WIFI_AP_STATE_ENABLING},
557 * {@link WifiManager#WIFI_AP_STATE_FAILED}
558 */
559 public int getWifiApEnabledState() {
Irfan Sheriff17b232b2010-06-24 11:32:26 -0700560 enforceAccessPermission();
Irfan Sheriffd8134ff2010-08-22 17:06:34 -0700561 return mWifiStateMachine.syncGetWifiApState();
Irfan Sheriffa2a1b912010-06-07 09:03:04 -0700562 }
563
564 /**
565 * see {@link WifiManager#getWifiApConfiguration()}
566 * @return soft access point configuration
567 */
568 public synchronized WifiConfiguration getWifiApConfiguration() {
Irfan Sheriff9ab518ad2010-03-12 15:48:17 -0800569 final ContentResolver cr = mContext.getContentResolver();
570 WifiConfiguration wifiConfig = new WifiConfiguration();
571 int authType;
572 try {
573 wifiConfig.SSID = Settings.Secure.getString(cr, Settings.Secure.WIFI_AP_SSID);
574 if (wifiConfig.SSID == null)
575 return null;
576 authType = Settings.Secure.getInt(cr, Settings.Secure.WIFI_AP_SECURITY);
577 wifiConfig.allowedKeyManagement.set(authType);
578 wifiConfig.preSharedKey = Settings.Secure.getString(cr, Settings.Secure.WIFI_AP_PASSWD);
579 return wifiConfig;
580 } catch (Settings.SettingNotFoundException e) {
581 Slog.e(TAG,"AP settings not found, returning");
582 return null;
583 }
584 }
585
Irfan Sheriffa2a1b912010-06-07 09:03:04 -0700586 /**
587 * see {@link WifiManager#setWifiApConfiguration(WifiConfiguration)}
588 * @param wifiConfig WifiConfiguration details for soft access point
589 */
590 public synchronized void setWifiApConfiguration(WifiConfiguration wifiConfig) {
Irfan Sheriff17b232b2010-06-24 11:32:26 -0700591 enforceChangePermission();
Irfan Sheriff9ab518ad2010-03-12 15:48:17 -0800592 final ContentResolver cr = mContext.getContentResolver();
Irfan Sheriff9ab518ad2010-03-12 15:48:17 -0800593 if (wifiConfig == null)
594 return;
Irfan Sheriffec8d23a2011-02-16 17:00:33 -0800595 int authType = wifiConfig.getAuthType();
Irfan Sheriff9ab518ad2010-03-12 15:48:17 -0800596 Settings.Secure.putString(cr, Settings.Secure.WIFI_AP_SSID, wifiConfig.SSID);
Irfan Sheriffec8d23a2011-02-16 17:00:33 -0800597 Settings.Secure.putInt(cr, Settings.Secure.WIFI_AP_SECURITY, authType);
598 if (authType != KeyMgmt.NONE)
Irfan Sheriff9ab518ad2010-03-12 15:48:17 -0800599 Settings.Secure.putString(cr, Settings.Secure.WIFI_AP_PASSWD, wifiConfig.preSharedKey);
600 }
601
Irfan Sheriff5321aef2010-02-12 12:35:59 -0800602 /**
Irfan Sheriffa2a1b912010-06-07 09:03:04 -0700603 * see {@link android.net.wifi.WifiManager#disconnect()}
Irfan Sheriff5321aef2010-02-12 12:35:59 -0800604 */
Irfan Sheriffe4984752010-08-19 11:29:22 -0700605 public void disconnect() {
Irfan Sheriffa2a1b912010-06-07 09:03:04 -0700606 enforceChangePermission();
Irfan Sheriffe4984752010-08-19 11:29:22 -0700607 mWifiStateMachine.disconnectCommand();
Irfan Sheriff5321aef2010-02-12 12:35:59 -0800608 }
609
610 /**
Irfan Sheriffa2a1b912010-06-07 09:03:04 -0700611 * see {@link android.net.wifi.WifiManager#reconnect()}
Irfan Sheriff5321aef2010-02-12 12:35:59 -0800612 */
Irfan Sheriffe4984752010-08-19 11:29:22 -0700613 public void reconnect() {
Irfan Sheriffa2a1b912010-06-07 09:03:04 -0700614 enforceChangePermission();
Irfan Sheriffe4984752010-08-19 11:29:22 -0700615 mWifiStateMachine.reconnectCommand();
Irfan Sheriff5321aef2010-02-12 12:35:59 -0800616 }
617
Irfan Sheriffa2a1b912010-06-07 09:03:04 -0700618 /**
619 * see {@link android.net.wifi.WifiManager#reassociate()}
Irfan Sheriffa2a1b912010-06-07 09:03:04 -0700620 */
Irfan Sheriffe4984752010-08-19 11:29:22 -0700621 public void reassociate() {
Irfan Sheriffa2a1b912010-06-07 09:03:04 -0700622 enforceChangePermission();
Irfan Sheriffe4984752010-08-19 11:29:22 -0700623 mWifiStateMachine.reassociateCommand();
Irfan Sheriff5321aef2010-02-12 12:35:59 -0800624 }
625
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800626 /**
627 * see {@link android.net.wifi.WifiManager#getConfiguredNetworks()}
628 * @return the list of configured networks
629 */
630 public List<WifiConfiguration> getConfiguredNetworks() {
631 enforceAccessPermission();
Irfan Sheriffd8134ff2010-08-22 17:06:34 -0700632 return mWifiStateMachine.syncGetConfiguredNetworks();
Chung-yih Wanga8d15942009-10-09 11:01:49 +0800633 }
634
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800635 /**
636 * see {@link android.net.wifi.WifiManager#addOrUpdateNetwork(WifiConfiguration)}
637 * @return the supplicant-assigned identifier for the new or updated
638 * network if the operation succeeds, or {@code -1} if it fails
639 */
Irfan Sheriff7aac5542009-12-22 21:42:17 -0800640 public int addOrUpdateNetwork(WifiConfiguration config) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800641 enforceChangePermission();
Irfan Sheriff227bec42011-02-15 19:30:27 -0800642 if (mWifiStateMachineChannel != null) {
643 return mWifiStateMachine.syncAddOrUpdateNetwork(mWifiStateMachineChannel, config);
Irfan Sheriff1406bcb2010-10-28 14:41:39 -0700644 } else {
Irfan Sheriff227bec42011-02-15 19:30:27 -0800645 Slog.e(TAG, "mWifiStateMachineChannel is not initialized");
Irfan Sheriff1406bcb2010-10-28 14:41:39 -0700646 return -1;
647 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800648 }
649
Irfan Sheriffa2a1b912010-06-07 09:03:04 -0700650 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800651 * See {@link android.net.wifi.WifiManager#removeNetwork(int)}
652 * @param netId the integer that identifies the network configuration
653 * to the supplicant
654 * @return {@code true} if the operation succeeded
655 */
656 public boolean removeNetwork(int netId) {
657 enforceChangePermission();
Irfan Sheriff227bec42011-02-15 19:30:27 -0800658 if (mWifiStateMachineChannel != null) {
659 return mWifiStateMachine.syncRemoveNetwork(mWifiStateMachineChannel, netId);
Wink Saville4b7ba092010-10-20 15:37:41 -0700660 } else {
Irfan Sheriff227bec42011-02-15 19:30:27 -0800661 Slog.e(TAG, "mWifiStateMachineChannel is not initialized");
Wink Saville4b7ba092010-10-20 15:37:41 -0700662 return false;
663 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800664 }
665
666 /**
667 * See {@link android.net.wifi.WifiManager#enableNetwork(int, boolean)}
668 * @param netId the integer that identifies the network configuration
669 * to the supplicant
670 * @param disableOthers if true, disable all other networks.
671 * @return {@code true} if the operation succeeded
672 */
673 public boolean enableNetwork(int netId, boolean disableOthers) {
674 enforceChangePermission();
Irfan Sheriff227bec42011-02-15 19:30:27 -0800675 if (mWifiStateMachineChannel != null) {
676 return mWifiStateMachine.syncEnableNetwork(mWifiStateMachineChannel, netId,
677 disableOthers);
Irfan Sheriff1406bcb2010-10-28 14:41:39 -0700678 } else {
Irfan Sheriff227bec42011-02-15 19:30:27 -0800679 Slog.e(TAG, "mWifiStateMachineChannel is not initialized");
Irfan Sheriff1406bcb2010-10-28 14:41:39 -0700680 return false;
681 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800682 }
683
684 /**
685 * See {@link android.net.wifi.WifiManager#disableNetwork(int)}
686 * @param netId the integer that identifies the network configuration
687 * to the supplicant
688 * @return {@code true} if the operation succeeded
689 */
690 public boolean disableNetwork(int netId) {
691 enforceChangePermission();
Irfan Sheriff227bec42011-02-15 19:30:27 -0800692 if (mWifiStateMachineChannel != null) {
693 return mWifiStateMachine.syncDisableNetwork(mWifiStateMachineChannel, netId);
Irfan Sheriff1406bcb2010-10-28 14:41:39 -0700694 } else {
Irfan Sheriff227bec42011-02-15 19:30:27 -0800695 Slog.e(TAG, "mWifiStateMachineChannel is not initialized");
Irfan Sheriff1406bcb2010-10-28 14:41:39 -0700696 return false;
697 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800698 }
699
700 /**
701 * See {@link android.net.wifi.WifiManager#getConnectionInfo()}
702 * @return the Wi-Fi information, contained in {@link WifiInfo}.
703 */
704 public WifiInfo getConnectionInfo() {
705 enforceAccessPermission();
706 /*
707 * Make sure we have the latest information, by sending
708 * a status request to the supplicant.
709 */
Irfan Sheriffd8134ff2010-08-22 17:06:34 -0700710 return mWifiStateMachine.syncRequestConnectionInfo();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800711 }
712
713 /**
714 * Return the results of the most recent access point scan, in the form of
715 * a list of {@link ScanResult} objects.
716 * @return the list of results
717 */
718 public List<ScanResult> getScanResults() {
719 enforceAccessPermission();
Irfan Sheriffd8134ff2010-08-22 17:06:34 -0700720 return mWifiStateMachine.syncGetScanResultsList();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800721 }
722
723 /**
724 * Tell the supplicant to persist the current list of configured networks.
725 * @return {@code true} if the operation succeeded
Irfan Sheriffa2a1b912010-06-07 09:03:04 -0700726 *
727 * TODO: deprecate this
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800728 */
729 public boolean saveConfiguration() {
Irfan Sheriffa2a1b912010-06-07 09:03:04 -0700730 boolean result = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800731 enforceChangePermission();
Irfan Sheriff227bec42011-02-15 19:30:27 -0800732 if (mWifiStateMachineChannel != null) {
733 return mWifiStateMachine.syncSaveConfig(mWifiStateMachineChannel);
Irfan Sheriff1406bcb2010-10-28 14:41:39 -0700734 } else {
Irfan Sheriff227bec42011-02-15 19:30:27 -0800735 Slog.e(TAG, "mWifiStateMachineChannel is not initialized");
Irfan Sheriff1406bcb2010-10-28 14:41:39 -0700736 return false;
737 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800738 }
739
740 /**
Irfan Sheriffed4f28b2010-10-29 15:32:10 -0700741 * Set the country code
742 * @param countryCode ISO 3166 country code.
Robert Greenwaltb5010cc2009-05-21 15:11:40 -0700743 * @param persist {@code true} if the setting should be remembered.
Irfan Sheriffed4f28b2010-10-29 15:32:10 -0700744 *
745 * The persist behavior exists so that wifi can fall back to the last
746 * persisted country code on a restart, when the locale information is
747 * not available from telephony.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800748 */
Irfan Sheriffed4f28b2010-10-29 15:32:10 -0700749 public void setCountryCode(String countryCode, boolean persist) {
750 Slog.i(TAG, "WifiService trying to set country code to " + countryCode +
751 " with persist set to " + persist);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800752 enforceChangePermission();
Irfan Sheriffed4f28b2010-10-29 15:32:10 -0700753 mWifiStateMachine.setCountryCode(countryCode, persist);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800754 }
755
756 /**
Irfan Sheriff36f74132010-11-04 16:57:37 -0700757 * Set the operational frequency band
758 * @param band One of
759 * {@link WifiManager#WIFI_FREQUENCY_BAND_AUTO},
760 * {@link WifiManager#WIFI_FREQUENCY_BAND_5GHZ},
761 * {@link WifiManager#WIFI_FREQUENCY_BAND_2GHZ},
762 * @param persist {@code true} if the setting should be remembered.
763 *
764 */
765 public void setFrequencyBand(int band, boolean persist) {
766 enforceChangePermission();
767 if (!isDualBandSupported()) return;
768 Slog.i(TAG, "WifiService trying to set frequency band to " + band +
769 " with persist set to " + persist);
770 mWifiStateMachine.setFrequencyBand(band, persist);
771 }
772
773
774 /**
775 * Get the operational frequency band
776 */
777 public int getFrequencyBand() {
778 enforceAccessPermission();
779 return mWifiStateMachine.getFrequencyBand();
780 }
781
782 public boolean isDualBandSupported() {
783 //TODO: Should move towards adding a driver API that checks at runtime
784 return mContext.getResources().getBoolean(
785 com.android.internal.R.bool.config_wifi_dual_band_support);
786 }
787
788 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800789 * Return the DHCP-assigned addresses from the last successful DHCP request,
790 * if any.
791 * @return the DHCP information
792 */
793 public DhcpInfo getDhcpInfo() {
794 enforceAccessPermission();
Irfan Sheriffd8134ff2010-08-22 17:06:34 -0700795 return mWifiStateMachine.syncGetDhcpInfo();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800796 }
797
Irfan Sheriff0d255342010-07-28 09:35:20 -0700798 /**
799 * see {@link android.net.wifi.WifiManager#startWifi}
800 *
801 */
802 public void startWifi() {
803 enforceChangePermission();
804 /* TODO: may be add permissions for access only to connectivity service
805 * TODO: if a start issued, keep wifi alive until a stop issued irrespective
806 * of WifiLock & device idle status unless wifi enabled status is toggled
807 */
808
809 mWifiStateMachine.setDriverStart(true);
810 mWifiStateMachine.reconnectCommand();
811 }
812
813 /**
814 * see {@link android.net.wifi.WifiManager#stopWifi}
815 *
816 */
817 public void stopWifi() {
818 enforceChangePermission();
819 /* TODO: may be add permissions for access only to connectivity service
820 * TODO: if a stop is issued, wifi is brought up only by startWifi
821 * unless wifi enabled status is toggled
822 */
823 mWifiStateMachine.setDriverStart(false);
824 }
825
826
827 /**
828 * see {@link android.net.wifi.WifiManager#addToBlacklist}
829 *
830 */
831 public void addToBlacklist(String bssid) {
832 enforceChangePermission();
833
834 mWifiStateMachine.addToBlacklist(bssid);
835 }
836
837 /**
838 * see {@link android.net.wifi.WifiManager#clearBlacklist}
839 *
840 */
841 public void clearBlacklist() {
842 enforceChangePermission();
843
844 mWifiStateMachine.clearBlacklist();
845 }
846
Irfan Sheriffe04653c2010-08-09 09:09:59 -0700847 public void connectNetworkWithId(int networkId) {
848 enforceChangePermission();
849 mWifiStateMachine.connectNetwork(networkId);
850 }
851
852 public void connectNetworkWithConfig(WifiConfiguration config) {
853 enforceChangePermission();
854 mWifiStateMachine.connectNetwork(config);
855 }
856
857 public void saveNetwork(WifiConfiguration config) {
858 enforceChangePermission();
859 mWifiStateMachine.saveNetwork(config);
860 }
861
862 public void forgetNetwork(int netId) {
863 enforceChangePermission();
864 mWifiStateMachine.forgetNetwork(netId);
865 }
Irfan Sheriff0d255342010-07-28 09:35:20 -0700866
Irfan Sheriffe4c56c92011-01-12 16:33:58 -0800867 public WpsResult startWps(WpsConfiguration config) {
Irfan Sherifff235c5a2010-10-21 16:44:48 -0700868 enforceChangePermission();
Irfan Sheriff227bec42011-02-15 19:30:27 -0800869 if (mWifiStateMachineChannel != null) {
870 return mWifiStateMachine.startWps(mWifiStateMachineChannel, config);
Irfan Sherifff235c5a2010-10-21 16:44:48 -0700871 } else {
Irfan Sheriff227bec42011-02-15 19:30:27 -0800872 Slog.e(TAG, "mWifiStateMachineChannel is not initialized");
Irfan Sheriffe4c56c92011-01-12 16:33:58 -0800873 return new WpsResult(WpsResult.Status.FAILURE);
Irfan Sherifff235c5a2010-10-21 16:44:48 -0700874 }
Irfan Sheriff5ee89802010-09-16 17:53:34 -0700875 }
876
Irfan Sheriff227bec42011-02-15 19:30:27 -0800877 /**
878 * Get a reference to handler. This is used by a client to establish
879 * an AsyncChannel communication with WifiService
880 */
881 public Messenger getMessenger() {
882 enforceAccessPermission();
883 return new Messenger(mAsyncServiceHandler);
884 }
885
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800886 private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
887 @Override
888 public void onReceive(Context context, Intent intent) {
889 String action = intent.getAction();
890
Doug Zongker43866e02010-01-07 12:09:54 -0800891 long idleMillis =
892 Settings.Secure.getLong(mContext.getContentResolver(),
Irfan Sheriff4f5f7c92010-10-14 17:01:27 -0700893 Settings.Secure.WIFI_IDLE_MS, DEFAULT_IDLE_MS);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800894 int stayAwakeConditions =
Doug Zongker43866e02010-01-07 12:09:54 -0800895 Settings.System.getInt(mContext.getContentResolver(),
896 Settings.System.STAY_ON_WHILE_PLUGGED_IN, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800897 if (action.equals(Intent.ACTION_SCREEN_ON)) {
Joe Onorato431bb222010-10-18 19:13:23 -0400898 if (DBG) {
899 Slog.d(TAG, "ACTION_SCREEN_ON");
900 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800901 mAlarmManager.cancel(mIdleIntent);
902 mDeviceIdle = false;
903 mScreenOff = false;
Dianne Hackborn58e0eef2010-09-16 01:22:10 -0700904 // Once the screen is on, we are not keeping WIFI running
905 // because of any locks so clear that tracking immediately.
906 reportStartWorkSource();
Irfan Sheriff227bec42011-02-15 19:30:27 -0800907 evaluateTrafficStatsPolling();
Irfan Sheriff0d255342010-07-28 09:35:20 -0700908 mWifiStateMachine.enableRssiPolling(true);
Irfan Sherifffcc08452011-02-17 16:44:54 -0800909 if (mBackgroundScanSupported) {
910 mWifiStateMachine.enableBackgroundScan(false);
911 }
Irfan Sheriff8e86b892010-12-22 11:02:20 -0800912 mWifiStateMachine.enableAllNetworks();
Irfan Sheriff4f5f7c92010-10-14 17:01:27 -0700913 updateWifiState();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800914 } else if (action.equals(Intent.ACTION_SCREEN_OFF)) {
Joe Onorato431bb222010-10-18 19:13:23 -0400915 if (DBG) {
916 Slog.d(TAG, "ACTION_SCREEN_OFF");
917 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800918 mScreenOff = true;
Irfan Sheriff227bec42011-02-15 19:30:27 -0800919 evaluateTrafficStatsPolling();
Irfan Sheriff0d255342010-07-28 09:35:20 -0700920 mWifiStateMachine.enableRssiPolling(false);
Irfan Sherifffcc08452011-02-17 16:44:54 -0800921 if (mBackgroundScanSupported) {
922 mWifiStateMachine.enableBackgroundScan(true);
923 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800924 /*
925 * Set a timer to put Wi-Fi to sleep, but only if the screen is off
926 * AND the "stay on while plugged in" setting doesn't match the
927 * current power conditions (i.e, not plugged in, plugged in to USB,
928 * or plugged in to AC).
929 */
930 if (!shouldWifiStayAwake(stayAwakeConditions, mPluggedType)) {
Irfan Sheriffd8134ff2010-08-22 17:06:34 -0700931 WifiInfo info = mWifiStateMachine.syncRequestConnectionInfo();
San Mehatfa6c7112009-07-07 09:34:44 -0700932 if (info.getSupplicantState() != SupplicantState.COMPLETED) {
Robert Greenwalt84612ea62009-09-30 09:04:22 -0700933 // we used to go to sleep immediately, but this caused some race conditions
Irfan Sheriffa2a1b912010-06-07 09:03:04 -0700934 // we don't have time to track down for this release. Delay instead,
935 // but not as long as we would if connected (below)
Robert Greenwalt84612ea62009-09-30 09:04:22 -0700936 // TODO - fix the race conditions and switch back to the immediate turn-off
937 long triggerTime = System.currentTimeMillis() + (2*60*1000); // 2 min
Joe Onorato431bb222010-10-18 19:13:23 -0400938 if (DBG) {
939 Slog.d(TAG, "setting ACTION_DEVICE_IDLE timer for 120,000 ms");
940 }
Robert Greenwalt84612ea62009-09-30 09:04:22 -0700941 mAlarmManager.set(AlarmManager.RTC_WAKEUP, triggerTime, mIdleIntent);
942 // // do not keep Wifi awake when screen is off if Wifi is not associated
943 // mDeviceIdle = true;
944 // updateWifiState();
Mike Lockwoodd9c32bc2009-05-18 14:14:15 -0400945 } else {
946 long triggerTime = System.currentTimeMillis() + idleMillis;
Joe Onorato431bb222010-10-18 19:13:23 -0400947 if (DBG) {
948 Slog.d(TAG, "setting ACTION_DEVICE_IDLE timer for " + idleMillis
949 + "ms");
950 }
Mike Lockwoodd9c32bc2009-05-18 14:14:15 -0400951 mAlarmManager.set(AlarmManager.RTC_WAKEUP, triggerTime, mIdleIntent);
952 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800953 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800954 } else if (action.equals(ACTION_DEVICE_IDLE)) {
Joe Onorato431bb222010-10-18 19:13:23 -0400955 if (DBG) {
956 Slog.d(TAG, "got ACTION_DEVICE_IDLE");
957 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800958 mDeviceIdle = true;
Dianne Hackborn58e0eef2010-09-16 01:22:10 -0700959 reportStartWorkSource();
Irfan Sheriff4f5f7c92010-10-14 17:01:27 -0700960 updateWifiState();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800961 } else if (action.equals(Intent.ACTION_BATTERY_CHANGED)) {
962 /*
963 * Set a timer to put Wi-Fi to sleep, but only if the screen is off
964 * AND we are transitioning from a state in which the device was supposed
965 * to stay awake to a state in which it is not supposed to stay awake.
966 * If "stay awake" state is not changing, we do nothing, to avoid resetting
967 * the already-set timer.
968 */
969 int pluggedType = intent.getIntExtra("plugged", 0);
Joe Onorato431bb222010-10-18 19:13:23 -0400970 if (DBG) {
971 Slog.d(TAG, "ACTION_BATTERY_CHANGED pluggedType: " + pluggedType);
972 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800973 if (mScreenOff && shouldWifiStayAwake(stayAwakeConditions, mPluggedType) &&
974 !shouldWifiStayAwake(stayAwakeConditions, pluggedType)) {
975 long triggerTime = System.currentTimeMillis() + idleMillis;
Joe Onorato431bb222010-10-18 19:13:23 -0400976 if (DBG) {
977 Slog.d(TAG, "setting ACTION_DEVICE_IDLE timer for " + idleMillis + "ms");
978 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800979 mAlarmManager.set(AlarmManager.RTC_WAKEUP, triggerTime, mIdleIntent);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800980 }
981 mPluggedType = pluggedType;
Irfan Sheriff65eaec82011-01-05 22:00:16 -0800982 } else if (action.equals(BluetoothAdapter.ACTION_CONNECTION_STATE_CHANGED)) {
983 int state = intent.getIntExtra(BluetoothAdapter.EXTRA_CONNECTION_STATE,
984 BluetoothAdapter.STATE_DISCONNECTED);
985 mWifiStateMachine.sendBluetoothAdapterStateChange(state);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800986 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800987 }
988
989 /**
990 * Determines whether the Wi-Fi chipset should stay awake or be put to
991 * sleep. Looks at the setting for the sleep policy and the current
992 * conditions.
Jaikumar Ganesh084c6652009-12-07 10:58:18 -0800993 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800994 * @see #shouldDeviceStayAwake(int, int)
995 */
996 private boolean shouldWifiStayAwake(int stayAwakeConditions, int pluggedType) {
Irfan Sheriff739f6bc2011-01-28 16:43:12 -0800997 //Never sleep as long as the user has not changed the settings
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800998 int wifiSleepPolicy = Settings.System.getInt(mContext.getContentResolver(),
Irfan Sheriff96b10d62011-01-11 15:40:35 -0800999 Settings.System.WIFI_SLEEP_POLICY,
Irfan Sheriff739f6bc2011-01-28 16:43:12 -08001000 Settings.System.WIFI_SLEEP_POLICY_NEVER);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001001
1002 if (wifiSleepPolicy == Settings.System.WIFI_SLEEP_POLICY_NEVER) {
1003 // Never sleep
1004 return true;
1005 } else if ((wifiSleepPolicy == Settings.System.WIFI_SLEEP_POLICY_NEVER_WHILE_PLUGGED) &&
1006 (pluggedType != 0)) {
1007 // Never sleep while plugged, and we're plugged
1008 return true;
1009 } else {
1010 // Default
1011 return shouldDeviceStayAwake(stayAwakeConditions, pluggedType);
1012 }
1013 }
Jaikumar Ganesh084c6652009-12-07 10:58:18 -08001014
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001015 /**
1016 * Determine whether the bit value corresponding to {@code pluggedType} is set in
1017 * the bit string {@code stayAwakeConditions}. Because a {@code pluggedType} value
1018 * of {@code 0} isn't really a plugged type, but rather an indication that the
1019 * device isn't plugged in at all, there is no bit value corresponding to a
1020 * {@code pluggedType} value of {@code 0}. That is why we shift by
Ben Dodson4e8620f2010-08-25 10:55:47 -07001021 * {@code pluggedType - 1} instead of by {@code pluggedType}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001022 * @param stayAwakeConditions a bit string specifying which "plugged types" should
1023 * keep the device (and hence Wi-Fi) awake.
1024 * @param pluggedType the type of plug (USB, AC, or none) for which the check is
1025 * being made
1026 * @return {@code true} if {@code pluggedType} indicates that the device is
1027 * supposed to stay awake, {@code false} otherwise.
1028 */
1029 private boolean shouldDeviceStayAwake(int stayAwakeConditions, int pluggedType) {
1030 return (stayAwakeConditions & pluggedType) != 0;
1031 }
1032 };
1033
Dianne Hackborn03f3cb02010-09-17 23:12:26 -07001034 private synchronized void reportStartWorkSource() {
1035 mTmpWorkSource.clear();
1036 if (mDeviceIdle) {
1037 for (int i=0; i<mLocks.mList.size(); i++) {
1038 mTmpWorkSource.add(mLocks.mList.get(i).mWorkSource);
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07001039 }
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07001040 }
Dianne Hackborn03f3cb02010-09-17 23:12:26 -07001041 mWifiStateMachine.updateBatteryWorkSource(mTmpWorkSource);
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07001042 }
Jaikumar Ganesh7440fc22010-09-27 17:04:14 -07001043
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001044 private void updateWifiState() {
1045 boolean wifiEnabled = getPersistedWifiEnabled();
Irfan Sheriffa2a1b912010-06-07 09:03:04 -07001046 boolean airplaneMode = isAirplaneModeOn() && !mAirplaneModeOverwridden.get();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001047 boolean lockHeld = mLocks.hasLocks();
Irfan Sheriff5876a422010-08-12 20:26:23 -07001048 int strongestLockMode = WifiManager.WIFI_MODE_FULL;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001049 boolean wifiShouldBeEnabled = wifiEnabled && !airplaneMode;
1050 boolean wifiShouldBeStarted = !mDeviceIdle || lockHeld;
Irfan Sheriff5876a422010-08-12 20:26:23 -07001051
1052 if (lockHeld) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001053 strongestLockMode = mLocks.getStrongestLockMode();
Irfan Sheriff5876a422010-08-12 20:26:23 -07001054 }
1055 /* If device is not idle, lockmode cannot be scan only */
1056 if (!mDeviceIdle && strongestLockMode == WifiManager.WIFI_MODE_SCAN_ONLY) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001057 strongestLockMode = WifiManager.WIFI_MODE_FULL;
1058 }
1059
Irfan Sheriffa2a1b912010-06-07 09:03:04 -07001060 /* Disable tethering when airplane mode is enabled */
1061 if (airplaneMode) {
Irfan Sheriff0d255342010-07-28 09:35:20 -07001062 mWifiStateMachine.setWifiApEnabled(null, false);
Irfan Sheriffa2a1b912010-06-07 09:03:04 -07001063 }
Irfan Sheriffb2e6c012010-04-05 11:57:56 -07001064
Irfan Sheriffa2a1b912010-06-07 09:03:04 -07001065 if (wifiShouldBeEnabled) {
1066 if (wifiShouldBeStarted) {
Dianne Hackborn03f3cb02010-09-17 23:12:26 -07001067 reportStartWorkSource();
Irfan Sheriff0d255342010-07-28 09:35:20 -07001068 mWifiStateMachine.setWifiEnabled(true);
1069 mWifiStateMachine.setScanOnlyMode(
Irfan Sheriffa2a1b912010-06-07 09:03:04 -07001070 strongestLockMode == WifiManager.WIFI_MODE_SCAN_ONLY);
Irfan Sheriff0d255342010-07-28 09:35:20 -07001071 mWifiStateMachine.setDriverStart(true);
Irfan Sheriff5876a422010-08-12 20:26:23 -07001072 mWifiStateMachine.setHighPerfModeEnabled(strongestLockMode
1073 == WifiManager.WIFI_MODE_FULL_HIGH_PERF);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001074 } else {
Irfan Sheriff0d255342010-07-28 09:35:20 -07001075 mWifiStateMachine.requestCmWakeLock();
1076 mWifiStateMachine.setDriverStart(false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001077 }
Irfan Sheriffa2a1b912010-06-07 09:03:04 -07001078 } else {
Irfan Sheriff0d255342010-07-28 09:35:20 -07001079 mWifiStateMachine.setWifiEnabled(false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001080 }
1081 }
1082
1083 private void registerForBroadcasts() {
1084 IntentFilter intentFilter = new IntentFilter();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001085 intentFilter.addAction(Intent.ACTION_SCREEN_ON);
1086 intentFilter.addAction(Intent.ACTION_SCREEN_OFF);
1087 intentFilter.addAction(Intent.ACTION_BATTERY_CHANGED);
1088 intentFilter.addAction(ACTION_DEVICE_IDLE);
Irfan Sheriff65eaec82011-01-05 22:00:16 -08001089 intentFilter.addAction(BluetoothAdapter.ACTION_CONNECTION_STATE_CHANGED);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001090 mContext.registerReceiver(mReceiver, intentFilter);
1091 }
Jaikumar Ganesh084c6652009-12-07 10:58:18 -08001092
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001093 private boolean isAirplaneSensitive() {
1094 String airplaneModeRadios = Settings.System.getString(mContext.getContentResolver(),
1095 Settings.System.AIRPLANE_MODE_RADIOS);
1096 return airplaneModeRadios == null
1097 || airplaneModeRadios.contains(Settings.System.RADIO_WIFI);
1098 }
1099
Mike Lockwoodbd5ddf02009-07-29 21:37:14 -07001100 private boolean isAirplaneToggleable() {
1101 String toggleableRadios = Settings.System.getString(mContext.getContentResolver(),
1102 Settings.System.AIRPLANE_MODE_TOGGLEABLE_RADIOS);
1103 return toggleableRadios != null
1104 && toggleableRadios.contains(Settings.System.RADIO_WIFI);
1105 }
1106
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001107 /**
1108 * Returns true if Wi-Fi is sensitive to airplane mode, and airplane mode is
1109 * currently on.
1110 * @return {@code true} if airplane mode is on.
1111 */
1112 private boolean isAirplaneModeOn() {
1113 return isAirplaneSensitive() && Settings.System.getInt(mContext.getContentResolver(),
1114 Settings.System.AIRPLANE_MODE_ON, 0) == 1;
1115 }
1116
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001117 @Override
1118 protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
1119 if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.DUMP)
1120 != PackageManager.PERMISSION_GRANTED) {
1121 pw.println("Permission Denial: can't dump WifiService from from pid="
1122 + Binder.getCallingPid()
1123 + ", uid=" + Binder.getCallingUid());
1124 return;
1125 }
Irfan Sheriffd8134ff2010-08-22 17:06:34 -07001126 pw.println("Wi-Fi is " + mWifiStateMachine.syncGetWifiStateByName());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001127 pw.println("Stay-awake conditions: " +
1128 Settings.System.getInt(mContext.getContentResolver(),
1129 Settings.System.STAY_ON_WHILE_PLUGGED_IN, 0));
1130 pw.println();
1131
1132 pw.println("Internal state:");
Irfan Sheriff0d255342010-07-28 09:35:20 -07001133 pw.println(mWifiStateMachine);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001134 pw.println();
1135 pw.println("Latest scan results:");
Irfan Sheriffd8134ff2010-08-22 17:06:34 -07001136 List<ScanResult> scanResults = mWifiStateMachine.syncGetScanResultsList();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001137 if (scanResults != null && scanResults.size() != 0) {
1138 pw.println(" BSSID Frequency RSSI Flags SSID");
1139 for (ScanResult r : scanResults) {
1140 pw.printf(" %17s %9d %5d %-16s %s%n",
1141 r.BSSID,
1142 r.frequency,
1143 r.level,
1144 r.capabilities,
1145 r.SSID == null ? "" : r.SSID);
1146 }
1147 }
1148 pw.println();
Eric Shienbrood5711fad2009-03-27 20:25:31 -07001149 pw.println("Locks acquired: " + mFullLocksAcquired + " full, " +
Irfan Sheriff5876a422010-08-12 20:26:23 -07001150 mFullHighPerfLocksAcquired + " full high perf, " +
Eric Shienbrood5711fad2009-03-27 20:25:31 -07001151 mScanLocksAcquired + " scan");
1152 pw.println("Locks released: " + mFullLocksReleased + " full, " +
Irfan Sheriff5876a422010-08-12 20:26:23 -07001153 mFullHighPerfLocksReleased + " full high perf, " +
Eric Shienbrood5711fad2009-03-27 20:25:31 -07001154 mScanLocksReleased + " scan");
1155 pw.println();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001156 pw.println("Locks held:");
1157 mLocks.dump(pw);
1158 }
1159
Robert Greenwalt58ff0212009-05-19 15:53:54 -07001160 private class WifiLock extends DeathRecipient {
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -07001161 WifiLock(int lockMode, String tag, IBinder binder, WorkSource ws) {
1162 super(lockMode, tag, binder, ws);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001163 }
1164
1165 public void binderDied() {
1166 synchronized (mLocks) {
1167 releaseWifiLockLocked(mBinder);
1168 }
1169 }
1170
1171 public String toString() {
Robert Greenwalt5347bd42009-05-13 15:10:16 -07001172 return "WifiLock{" + mTag + " type=" + mMode + " binder=" + mBinder + "}";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001173 }
1174 }
1175
1176 private class LockList {
1177 private List<WifiLock> mList;
1178
1179 private LockList() {
1180 mList = new ArrayList<WifiLock>();
1181 }
1182
1183 private synchronized boolean hasLocks() {
1184 return !mList.isEmpty();
1185 }
1186
1187 private synchronized int getStrongestLockMode() {
1188 if (mList.isEmpty()) {
1189 return WifiManager.WIFI_MODE_FULL;
1190 }
Irfan Sheriff5876a422010-08-12 20:26:23 -07001191
1192 if (mFullHighPerfLocksAcquired > mFullHighPerfLocksReleased) {
1193 return WifiManager.WIFI_MODE_FULL_HIGH_PERF;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001194 }
Irfan Sheriff5876a422010-08-12 20:26:23 -07001195
1196 if (mFullLocksAcquired > mFullLocksReleased) {
1197 return WifiManager.WIFI_MODE_FULL;
1198 }
1199
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001200 return WifiManager.WIFI_MODE_SCAN_ONLY;
1201 }
1202
1203 private void addLock(WifiLock lock) {
1204 if (findLockByBinder(lock.mBinder) < 0) {
1205 mList.add(lock);
1206 }
1207 }
1208
1209 private WifiLock removeLock(IBinder binder) {
1210 int index = findLockByBinder(binder);
1211 if (index >= 0) {
Suchi Amalapurapufff2fda2009-06-30 21:36:16 -07001212 WifiLock ret = mList.remove(index);
1213 ret.unlinkDeathRecipient();
1214 return ret;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001215 } else {
1216 return null;
1217 }
1218 }
1219
1220 private int findLockByBinder(IBinder binder) {
1221 int size = mList.size();
1222 for (int i = size - 1; i >= 0; i--)
1223 if (mList.get(i).mBinder == binder)
1224 return i;
1225 return -1;
1226 }
1227
1228 private void dump(PrintWriter pw) {
1229 for (WifiLock l : mList) {
1230 pw.print(" ");
1231 pw.println(l);
1232 }
1233 }
1234 }
1235
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -07001236 void enforceWakeSourcePermission(int uid, int pid) {
Dianne Hackborne746f032010-09-13 16:02:57 -07001237 if (uid == android.os.Process.myUid()) {
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -07001238 return;
1239 }
1240 mContext.enforcePermission(android.Manifest.permission.UPDATE_DEVICE_STATS,
1241 pid, uid, null);
1242 }
1243
1244 public boolean acquireWifiLock(IBinder binder, int lockMode, String tag, WorkSource ws) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001245 mContext.enforceCallingOrSelfPermission(android.Manifest.permission.WAKE_LOCK, null);
Irfan Sheriff5876a422010-08-12 20:26:23 -07001246 if (lockMode != WifiManager.WIFI_MODE_FULL &&
1247 lockMode != WifiManager.WIFI_MODE_SCAN_ONLY &&
1248 lockMode != WifiManager.WIFI_MODE_FULL_HIGH_PERF) {
1249 Slog.e(TAG, "Illegal argument, lockMode= " + lockMode);
1250 if (DBG) throw new IllegalArgumentException("lockMode=" + lockMode);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001251 return false;
1252 }
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -07001253 if (ws != null && ws.size() == 0) {
1254 ws = null;
1255 }
Dianne Hackbornecfd7f72010-10-08 14:23:40 -07001256 if (ws != null) {
1257 enforceWakeSourcePermission(Binder.getCallingUid(), Binder.getCallingPid());
1258 }
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -07001259 if (ws == null) {
1260 ws = new WorkSource(Binder.getCallingUid());
1261 }
1262 WifiLock wifiLock = new WifiLock(lockMode, tag, binder, ws);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001263 synchronized (mLocks) {
1264 return acquireWifiLockLocked(wifiLock);
1265 }
1266 }
1267
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -07001268 private void noteAcquireWifiLock(WifiLock wifiLock) throws RemoteException {
1269 switch(wifiLock.mMode) {
1270 case WifiManager.WIFI_MODE_FULL:
Irfan Sheriff5876a422010-08-12 20:26:23 -07001271 case WifiManager.WIFI_MODE_FULL_HIGH_PERF:
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -07001272 mBatteryStats.noteFullWifiLockAcquiredFromSource(wifiLock.mWorkSource);
1273 break;
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -07001274 case WifiManager.WIFI_MODE_SCAN_ONLY:
1275 mBatteryStats.noteScanWifiLockAcquiredFromSource(wifiLock.mWorkSource);
1276 break;
1277 }
1278 }
1279
1280 private void noteReleaseWifiLock(WifiLock wifiLock) throws RemoteException {
1281 switch(wifiLock.mMode) {
1282 case WifiManager.WIFI_MODE_FULL:
Irfan Sheriff5876a422010-08-12 20:26:23 -07001283 case WifiManager.WIFI_MODE_FULL_HIGH_PERF:
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -07001284 mBatteryStats.noteFullWifiLockReleasedFromSource(wifiLock.mWorkSource);
1285 break;
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -07001286 case WifiManager.WIFI_MODE_SCAN_ONLY:
1287 mBatteryStats.noteScanWifiLockReleasedFromSource(wifiLock.mWorkSource);
1288 break;
1289 }
1290 }
1291
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001292 private boolean acquireWifiLockLocked(WifiLock wifiLock) {
Irfan Sheriffc89dd542010-09-28 08:40:54 -07001293 if (DBG) Slog.d(TAG, "acquireWifiLockLocked: " + wifiLock);
Robert Greenwaltf1acb2d2009-10-13 08:20:55 -07001294
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001295 mLocks.addLock(wifiLock);
Robert Greenwaltf1acb2d2009-10-13 08:20:55 -07001296
The Android Open Source Project10592532009-03-18 17:39:46 -07001297 long ident = Binder.clearCallingIdentity();
1298 try {
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -07001299 noteAcquireWifiLock(wifiLock);
Robert Greenwalt5347bd42009-05-13 15:10:16 -07001300 switch(wifiLock.mMode) {
Eric Shienbrood5711fad2009-03-27 20:25:31 -07001301 case WifiManager.WIFI_MODE_FULL:
1302 ++mFullLocksAcquired;
Eric Shienbrood5711fad2009-03-27 20:25:31 -07001303 break;
Irfan Sheriff5876a422010-08-12 20:26:23 -07001304 case WifiManager.WIFI_MODE_FULL_HIGH_PERF:
1305 ++mFullHighPerfLocksAcquired;
1306 break;
1307
Eric Shienbrood5711fad2009-03-27 20:25:31 -07001308 case WifiManager.WIFI_MODE_SCAN_ONLY:
1309 ++mScanLocksAcquired;
Eric Shienbrood5711fad2009-03-27 20:25:31 -07001310 break;
The Android Open Source Project10592532009-03-18 17:39:46 -07001311 }
Dianne Hackbornecfd7f72010-10-08 14:23:40 -07001312
1313 // Be aggressive about adding new locks into the accounted state...
1314 // we want to over-report rather than under-report.
1315 reportStartWorkSource();
1316
1317 updateWifiState();
1318 return true;
The Android Open Source Project10592532009-03-18 17:39:46 -07001319 } catch (RemoteException e) {
Dianne Hackbornecfd7f72010-10-08 14:23:40 -07001320 return false;
The Android Open Source Project10592532009-03-18 17:39:46 -07001321 } finally {
1322 Binder.restoreCallingIdentity(ident);
1323 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001324 }
1325
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -07001326 public void updateWifiLockWorkSource(IBinder lock, WorkSource ws) {
1327 int uid = Binder.getCallingUid();
1328 int pid = Binder.getCallingPid();
1329 if (ws != null && ws.size() == 0) {
1330 ws = null;
1331 }
1332 if (ws != null) {
1333 enforceWakeSourcePermission(uid, pid);
1334 }
1335 long ident = Binder.clearCallingIdentity();
1336 try {
1337 synchronized (mLocks) {
1338 int index = mLocks.findLockByBinder(lock);
1339 if (index < 0) {
1340 throw new IllegalArgumentException("Wifi lock not active");
1341 }
1342 WifiLock wl = mLocks.mList.get(index);
1343 noteReleaseWifiLock(wl);
1344 wl.mWorkSource = ws != null ? new WorkSource(ws) : new WorkSource(uid);
1345 noteAcquireWifiLock(wl);
1346 }
1347 } catch (RemoteException e) {
1348 } finally {
1349 Binder.restoreCallingIdentity(ident);
1350 }
1351 }
1352
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001353 public boolean releaseWifiLock(IBinder lock) {
1354 mContext.enforceCallingOrSelfPermission(android.Manifest.permission.WAKE_LOCK, null);
1355 synchronized (mLocks) {
1356 return releaseWifiLockLocked(lock);
1357 }
1358 }
1359
1360 private boolean releaseWifiLockLocked(IBinder lock) {
Eric Shienbroodd4c5f892009-03-24 18:13:20 -07001361 boolean hadLock;
Robert Greenwaltf1acb2d2009-10-13 08:20:55 -07001362
The Android Open Source Project10592532009-03-18 17:39:46 -07001363 WifiLock wifiLock = mLocks.removeLock(lock);
Robert Greenwaltf1acb2d2009-10-13 08:20:55 -07001364
Irfan Sheriffc89dd542010-09-28 08:40:54 -07001365 if (DBG) Slog.d(TAG, "releaseWifiLockLocked: " + wifiLock);
Robert Greenwaltf1acb2d2009-10-13 08:20:55 -07001366
Eric Shienbroodd4c5f892009-03-24 18:13:20 -07001367 hadLock = (wifiLock != null);
1368
Dianne Hackbornecfd7f72010-10-08 14:23:40 -07001369 long ident = Binder.clearCallingIdentity();
1370 try {
1371 if (hadLock) {
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -07001372 noteAcquireWifiLock(wifiLock);
Robert Greenwalt5347bd42009-05-13 15:10:16 -07001373 switch(wifiLock.mMode) {
Eric Shienbrood5711fad2009-03-27 20:25:31 -07001374 case WifiManager.WIFI_MODE_FULL:
1375 ++mFullLocksReleased;
Eric Shienbrood5711fad2009-03-27 20:25:31 -07001376 break;
Irfan Sheriff5876a422010-08-12 20:26:23 -07001377 case WifiManager.WIFI_MODE_FULL_HIGH_PERF:
1378 ++mFullHighPerfLocksReleased;
1379 break;
Eric Shienbrood5711fad2009-03-27 20:25:31 -07001380 case WifiManager.WIFI_MODE_SCAN_ONLY:
1381 ++mScanLocksReleased;
Eric Shienbrood5711fad2009-03-27 20:25:31 -07001382 break;
Eric Shienbroodd4c5f892009-03-24 18:13:20 -07001383 }
The Android Open Source Project10592532009-03-18 17:39:46 -07001384 }
Dianne Hackbornecfd7f72010-10-08 14:23:40 -07001385
1386 // TODO - should this only happen if you hadLock?
1387 updateWifiState();
1388
1389 } catch (RemoteException e) {
1390 } finally {
1391 Binder.restoreCallingIdentity(ident);
The Android Open Source Project10592532009-03-18 17:39:46 -07001392 }
Dianne Hackbornecfd7f72010-10-08 14:23:40 -07001393
Eric Shienbroodd4c5f892009-03-24 18:13:20 -07001394 return hadLock;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001395 }
Robert Greenwalt5347bd42009-05-13 15:10:16 -07001396
Robert Greenwalt58ff0212009-05-19 15:53:54 -07001397 private abstract class DeathRecipient
Robert Greenwalt5347bd42009-05-13 15:10:16 -07001398 implements IBinder.DeathRecipient {
1399 String mTag;
1400 int mMode;
1401 IBinder mBinder;
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -07001402 WorkSource mWorkSource;
Robert Greenwalt5347bd42009-05-13 15:10:16 -07001403
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -07001404 DeathRecipient(int mode, String tag, IBinder binder, WorkSource ws) {
Robert Greenwalt5347bd42009-05-13 15:10:16 -07001405 super();
1406 mTag = tag;
1407 mMode = mode;
1408 mBinder = binder;
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -07001409 mWorkSource = ws;
Robert Greenwalt5347bd42009-05-13 15:10:16 -07001410 try {
1411 mBinder.linkToDeath(this, 0);
1412 } catch (RemoteException e) {
1413 binderDied();
1414 }
1415 }
Suchi Amalapurapufff2fda2009-06-30 21:36:16 -07001416
1417 void unlinkDeathRecipient() {
1418 mBinder.unlinkToDeath(this, 0);
1419 }
Robert Greenwalt5347bd42009-05-13 15:10:16 -07001420 }
1421
Robert Greenwalt58ff0212009-05-19 15:53:54 -07001422 private class Multicaster extends DeathRecipient {
1423 Multicaster(String tag, IBinder binder) {
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -07001424 super(Binder.getCallingUid(), tag, binder, null);
Robert Greenwalt5347bd42009-05-13 15:10:16 -07001425 }
1426
1427 public void binderDied() {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001428 Slog.e(TAG, "Multicaster binderDied");
Robert Greenwalt5347bd42009-05-13 15:10:16 -07001429 synchronized (mMulticasters) {
1430 int i = mMulticasters.indexOf(this);
1431 if (i != -1) {
1432 removeMulticasterLocked(i, mMode);
1433 }
1434 }
1435 }
1436
1437 public String toString() {
Robert Greenwalt58ff0212009-05-19 15:53:54 -07001438 return "Multicaster{" + mTag + " binder=" + mBinder + "}";
Robert Greenwalt5347bd42009-05-13 15:10:16 -07001439 }
1440
1441 public int getUid() {
1442 return mMode;
1443 }
1444 }
1445
Robert Greenwalte2d155a2009-10-21 14:58:34 -07001446 public void initializeMulticastFiltering() {
1447 enforceMulticastChangePermission();
Irfan Sheriffa8fbe1f2010-03-09 09:13:58 -08001448
Robert Greenwalte2d155a2009-10-21 14:58:34 -07001449 synchronized (mMulticasters) {
1450 // if anybody had requested filters be off, leave off
1451 if (mMulticasters.size() != 0) {
1452 return;
1453 } else {
Irfan Sheriff0d255342010-07-28 09:35:20 -07001454 mWifiStateMachine.startPacketFiltering();
Robert Greenwalte2d155a2009-10-21 14:58:34 -07001455 }
1456 }
1457 }
1458
Robert Greenwaltfc1b15c2009-05-22 15:09:51 -07001459 public void acquireMulticastLock(IBinder binder, String tag) {
1460 enforceMulticastChangePermission();
Robert Greenwalt5347bd42009-05-13 15:10:16 -07001461
1462 synchronized (mMulticasters) {
1463 mMulticastEnabled++;
Robert Greenwalt58ff0212009-05-19 15:53:54 -07001464 mMulticasters.add(new Multicaster(tag, binder));
Robert Greenwalt5347bd42009-05-13 15:10:16 -07001465 // Note that we could call stopPacketFiltering only when
1466 // our new size == 1 (first call), but this function won't
1467 // be called often and by making the stopPacket call each
1468 // time we're less fragile and self-healing.
Irfan Sheriff0d255342010-07-28 09:35:20 -07001469 mWifiStateMachine.stopPacketFiltering();
Robert Greenwalt5347bd42009-05-13 15:10:16 -07001470 }
1471
1472 int uid = Binder.getCallingUid();
1473 Long ident = Binder.clearCallingIdentity();
1474 try {
1475 mBatteryStats.noteWifiMulticastEnabled(uid);
1476 } catch (RemoteException e) {
1477 } finally {
1478 Binder.restoreCallingIdentity(ident);
1479 }
1480 }
1481
Robert Greenwaltfc1b15c2009-05-22 15:09:51 -07001482 public void releaseMulticastLock() {
1483 enforceMulticastChangePermission();
Robert Greenwalt5347bd42009-05-13 15:10:16 -07001484
1485 int uid = Binder.getCallingUid();
1486 synchronized (mMulticasters) {
1487 mMulticastDisabled++;
1488 int size = mMulticasters.size();
1489 for (int i = size - 1; i >= 0; i--) {
Robert Greenwalt58ff0212009-05-19 15:53:54 -07001490 Multicaster m = mMulticasters.get(i);
Robert Greenwalt5347bd42009-05-13 15:10:16 -07001491 if ((m != null) && (m.getUid() == uid)) {
1492 removeMulticasterLocked(i, uid);
1493 }
1494 }
1495 }
1496 }
1497
1498 private void removeMulticasterLocked(int i, int uid)
1499 {
Suchi Amalapurapufff2fda2009-06-30 21:36:16 -07001500 Multicaster removed = mMulticasters.remove(i);
Irfan Sheriffa8fbe1f2010-03-09 09:13:58 -08001501
Suchi Amalapurapufff2fda2009-06-30 21:36:16 -07001502 if (removed != null) {
1503 removed.unlinkDeathRecipient();
1504 }
Robert Greenwalt5347bd42009-05-13 15:10:16 -07001505 if (mMulticasters.size() == 0) {
Irfan Sheriff0d255342010-07-28 09:35:20 -07001506 mWifiStateMachine.startPacketFiltering();
Robert Greenwalt5347bd42009-05-13 15:10:16 -07001507 }
1508
1509 Long ident = Binder.clearCallingIdentity();
1510 try {
1511 mBatteryStats.noteWifiMulticastDisabled(uid);
1512 } catch (RemoteException e) {
1513 } finally {
1514 Binder.restoreCallingIdentity(ident);
1515 }
1516 }
1517
Robert Greenwalt58ff0212009-05-19 15:53:54 -07001518 public boolean isMulticastEnabled() {
Robert Greenwalt5347bd42009-05-13 15:10:16 -07001519 enforceAccessPermission();
1520
1521 synchronized (mMulticasters) {
1522 return (mMulticasters.size() > 0);
1523 }
1524 }
Irfan Sheriff0d255342010-07-28 09:35:20 -07001525
Irfan Sheriff227bec42011-02-15 19:30:27 -08001526 /**
1527 * Evaluate if traffic stats polling is needed based on
1528 * connection and screen on status
1529 */
1530 private void evaluateTrafficStatsPolling() {
1531 Message msg;
1532 if (mNetworkInfo.getDetailedState() == DetailedState.CONNECTED && !mScreenOff) {
1533 msg = Message.obtain(mAsyncServiceHandler, CMD_ENABLE_TRAFFIC_STATS_POLL, 1, 0);
1534 } else {
1535 msg = Message.obtain(mAsyncServiceHandler, CMD_ENABLE_TRAFFIC_STATS_POLL, 0, 0);
1536 }
1537 msg.sendToTarget();
1538 }
1539
1540 private void notifyOnDataActivity() {
1541 long sent, received;
1542 long preTxPkts = mTxPkts, preRxPkts = mRxPkts;
1543 int dataActivity = WifiManager.DATA_ACTIVITY_NONE;
1544
1545 mTxPkts = TrafficStats.getTxPackets(mInterfaceName);
1546 mRxPkts = TrafficStats.getRxPackets(mInterfaceName);
1547
1548 if (preTxPkts > 0 || preRxPkts > 0) {
1549 sent = mTxPkts - preTxPkts;
1550 received = mRxPkts - preRxPkts;
1551 if (sent > 0) {
1552 dataActivity |= WifiManager.DATA_ACTIVITY_OUT;
1553 }
1554 if (received > 0) {
1555 dataActivity |= WifiManager.DATA_ACTIVITY_IN;
1556 }
1557
1558 if (dataActivity != mDataActivity && !mScreenOff) {
1559 mDataActivity = dataActivity;
1560 for (AsyncChannel client : mClients) {
1561 client.sendMessage(WifiManager.DATA_ACTIVITY_NOTIFICATION, mDataActivity);
1562 }
1563 }
1564 }
1565 }
1566
1567
Irfan Sheriff0d255342010-07-28 09:35:20 -07001568 private void checkAndSetNotification() {
1569 // If we shouldn't place a notification on available networks, then
1570 // don't bother doing any of the following
1571 if (!mNotificationEnabled) return;
1572
1573 State state = mNetworkInfo.getState();
1574 if ((state == NetworkInfo.State.DISCONNECTED)
1575 || (state == NetworkInfo.State.UNKNOWN)) {
1576 // Look for an open network
Irfan Sheriffd8134ff2010-08-22 17:06:34 -07001577 List<ScanResult> scanResults = mWifiStateMachine.syncGetScanResultsList();
Irfan Sheriff0d255342010-07-28 09:35:20 -07001578 if (scanResults != null) {
1579 int numOpenNetworks = 0;
1580 for (int i = scanResults.size() - 1; i >= 0; i--) {
1581 ScanResult scanResult = scanResults.get(i);
1582
1583 if (TextUtils.isEmpty(scanResult.capabilities)) {
1584 numOpenNetworks++;
1585 }
1586 }
1587
1588 if (numOpenNetworks > 0) {
1589 if (++mNumScansSinceNetworkStateChange >= NUM_SCANS_BEFORE_ACTUALLY_SCANNING) {
1590 /*
1591 * We've scanned continuously at least
1592 * NUM_SCANS_BEFORE_NOTIFICATION times. The user
1593 * probably does not have a remembered network in range,
1594 * since otherwise supplicant would have tried to
1595 * associate and thus resetting this counter.
1596 */
1597 setNotificationVisible(true, numOpenNetworks, false, 0);
1598 }
1599 return;
1600 }
1601 }
1602 }
1603
1604 // No open networks in range, remove the notification
1605 setNotificationVisible(false, 0, false, 0);
1606 }
1607
1608 /**
1609 * Clears variables related to tracking whether a notification has been
1610 * shown recently and clears the current notification.
1611 */
1612 private void resetNotification() {
1613 mNotificationRepeatTime = 0;
1614 mNumScansSinceNetworkStateChange = 0;
1615 setNotificationVisible(false, 0, false, 0);
1616 }
1617
1618 /**
1619 * Display or don't display a notification that there are open Wi-Fi networks.
1620 * @param visible {@code true} if notification should be visible, {@code false} otherwise
1621 * @param numNetworks the number networks seen
1622 * @param force {@code true} to force notification to be shown/not-shown,
1623 * even if it is already shown/not-shown.
1624 * @param delay time in milliseconds after which the notification should be made
1625 * visible or invisible.
1626 */
1627 private void setNotificationVisible(boolean visible, int numNetworks, boolean force,
1628 int delay) {
1629
1630 // Since we use auto cancel on the notification, when the
1631 // mNetworksAvailableNotificationShown is true, the notification may
1632 // have actually been canceled. However, when it is false we know
1633 // for sure that it is not being shown (it will not be shown any other
1634 // place than here)
1635
1636 // If it should be hidden and it is already hidden, then noop
1637 if (!visible && !mNotificationShown && !force) {
1638 return;
1639 }
1640
1641 NotificationManager notificationManager = (NotificationManager) mContext
1642 .getSystemService(Context.NOTIFICATION_SERVICE);
1643
1644 Message message;
1645 if (visible) {
1646
1647 // Not enough time has passed to show the notification again
1648 if (System.currentTimeMillis() < mNotificationRepeatTime) {
1649 return;
1650 }
1651
1652 if (mNotification == null) {
Wink Savillec7a98342010-08-13 16:11:42 -07001653 // Cache the Notification object.
Irfan Sheriff0d255342010-07-28 09:35:20 -07001654 mNotification = new Notification();
1655 mNotification.when = 0;
1656 mNotification.icon = ICON_NETWORKS_AVAILABLE;
1657 mNotification.flags = Notification.FLAG_AUTO_CANCEL;
1658 mNotification.contentIntent = PendingIntent.getActivity(mContext, 0,
1659 new Intent(WifiManager.ACTION_PICK_WIFI_NETWORK), 0);
1660 }
1661
1662 CharSequence title = mContext.getResources().getQuantityText(
1663 com.android.internal.R.plurals.wifi_available, numNetworks);
1664 CharSequence details = mContext.getResources().getQuantityText(
1665 com.android.internal.R.plurals.wifi_available_detailed, numNetworks);
1666 mNotification.tickerText = title;
1667 mNotification.setLatestEventInfo(mContext, title, details, mNotification.contentIntent);
1668
1669 mNotificationRepeatTime = System.currentTimeMillis() + NOTIFICATION_REPEAT_DELAY_MS;
1670
1671 notificationManager.notify(ICON_NETWORKS_AVAILABLE, mNotification);
Irfan Sheriff0d255342010-07-28 09:35:20 -07001672 } else {
Irfan Sheriff0d255342010-07-28 09:35:20 -07001673 notificationManager.cancel(ICON_NETWORKS_AVAILABLE);
Irfan Sheriff0d255342010-07-28 09:35:20 -07001674 }
1675
Irfan Sheriff0d255342010-07-28 09:35:20 -07001676 mNotificationShown = visible;
1677 }
1678
1679 private class NotificationEnabledSettingObserver extends ContentObserver {
1680
1681 public NotificationEnabledSettingObserver(Handler handler) {
1682 super(handler);
1683 }
1684
1685 public void register() {
1686 ContentResolver cr = mContext.getContentResolver();
1687 cr.registerContentObserver(Settings.Secure.getUriFor(
1688 Settings.Secure.WIFI_NETWORKS_AVAILABLE_NOTIFICATION_ON), true, this);
1689 mNotificationEnabled = getValue();
1690 }
1691
1692 @Override
1693 public void onChange(boolean selfChange) {
1694 super.onChange(selfChange);
1695
1696 mNotificationEnabled = getValue();
1697 resetNotification();
1698 }
1699
1700 private boolean getValue() {
1701 return Settings.Secure.getInt(mContext.getContentResolver(),
1702 Settings.Secure.WIFI_NETWORKS_AVAILABLE_NOTIFICATION_ON, 1) == 1;
1703 }
1704 }
1705
1706
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001707}