blob: cb55451f5494c00048e530687d68bfe7b74a51b1 [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;
Irfan Sheriff4aeca7c52011-03-10 16:53:33 -080032import android.net.wifi.ScanResult;
33import android.net.wifi.SupplicantState;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080034import android.net.wifi.WifiInfo;
35import android.net.wifi.WifiManager;
Irfan Sheriff0d255342010-07-28 09:35:20 -070036import android.net.wifi.WifiStateMachine;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080037import android.net.wifi.WifiConfiguration;
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 Sheriff658772f2011-03-08 14:52:31 -080065import java.util.concurrent.atomic.AtomicInteger;
Irfan Sheriffa2a1b912010-06-07 09:03:04 -070066import java.util.concurrent.atomic.AtomicBoolean;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080067import java.io.FileDescriptor;
68import java.io.PrintWriter;
69
The Android Open Source Project10592532009-03-18 17:39:46 -070070import com.android.internal.app.IBatteryStats;
Wink Saville4b7ba092010-10-20 15:37:41 -070071import com.android.internal.util.AsyncChannel;
The Android Open Source Project10592532009-03-18 17:39:46 -070072import com.android.server.am.BatteryStatsService;
Irfan Sheriff9ab518ad2010-03-12 15:48:17 -080073import com.android.internal.R;
The Android Open Source Project10592532009-03-18 17:39:46 -070074
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080075/**
76 * WifiService handles remote WiFi operation requests by implementing
Irfan Sheriffa2a1b912010-06-07 09:03:04 -070077 * the IWifiManager interface.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080078 *
79 * @hide
80 */
Irfan Sheriffa2a1b912010-06-07 09:03:04 -070081//TODO: Clean up multiple locks and implement WifiService
82// as a SM to track soft AP/client/adhoc bring up based
83// on device idle state, airplane mode and boot.
84
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080085public class WifiService extends IWifiManager.Stub {
86 private static final String TAG = "WifiService";
Dianne Hackborn5fd21692011-06-07 14:09:47 -070087 private static final boolean DBG = false;
Irfan Sheriffa2a1b912010-06-07 09:03:04 -070088
Irfan Sheriff0d255342010-07-28 09:35:20 -070089 private final WifiStateMachine mWifiStateMachine;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080090
91 private Context mContext;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080092
93 private AlarmManager mAlarmManager;
94 private PendingIntent mIdleIntent;
95 private static final int IDLE_REQUEST = 0;
96 private boolean mScreenOff;
97 private boolean mDeviceIdle;
98 private int mPluggedType;
99
Irfan Sherifffcc08452011-02-17 16:44:54 -0800100 /* Chipset supports background scan */
101 private final boolean mBackgroundScanSupported;
102
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800103 private final LockList mLocks = new LockList();
Eric Shienbrood5711fad2009-03-27 20:25:31 -0700104 // some wifi lock statistics
Irfan Sheriff5876a422010-08-12 20:26:23 -0700105 private int mFullHighPerfLocksAcquired;
106 private int mFullHighPerfLocksReleased;
Eric Shienbrood5711fad2009-03-27 20:25:31 -0700107 private int mFullLocksAcquired;
108 private int mFullLocksReleased;
109 private int mScanLocksAcquired;
110 private int mScanLocksReleased;
The Android Open Source Project10592532009-03-18 17:39:46 -0700111
Robert Greenwalt58ff0212009-05-19 15:53:54 -0700112 private final List<Multicaster> mMulticasters =
113 new ArrayList<Multicaster>();
Robert Greenwalt5347bd42009-05-13 15:10:16 -0700114 private int mMulticastEnabled;
115 private int mMulticastDisabled;
116
The Android Open Source Project10592532009-03-18 17:39:46 -0700117 private final IBatteryStats mBatteryStats;
Jaikumar Ganesh084c6652009-12-07 10:58:18 -0800118
Irfan Sheriff227bec42011-02-15 19:30:27 -0800119 private boolean mEnableTrafficStatsPoll = false;
120 private int mTrafficStatsPollToken = 0;
121 private long mTxPkts;
122 private long mRxPkts;
123 /* Tracks last reported data activity */
124 private int mDataActivity;
125 private String mInterfaceName;
126
127 /**
128 * Interval in milliseconds between polling for traffic
129 * statistics
130 */
131 private static final int POLL_TRAFFIC_STATS_INTERVAL_MSECS = 1000;
132
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800133 /**
Doug Zongker43866e02010-01-07 12:09:54 -0800134 * See {@link Settings.Secure#WIFI_IDLE_MS}. This is the default value if a
135 * Settings.Secure value is not present. This timeout value is chosen as
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800136 * the approximate point at which the battery drain caused by Wi-Fi
137 * being enabled but not active exceeds the battery drain caused by
138 * re-establishing a connection to the mobile data network.
139 */
Irfan Sheriff4f5f7c92010-10-14 17:01:27 -0700140 private static final long DEFAULT_IDLE_MS = 15 * 60 * 1000; /* 15 minutes */
141
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800142 private static final String ACTION_DEVICE_IDLE =
143 "com.android.server.WifiManager.action.DEVICE_IDLE";
144
Irfan Sheriff658772f2011-03-08 14:52:31 -0800145 private static final int WIFI_DISABLED = 0;
146 private static final int WIFI_ENABLED = 1;
147 /* Wifi enabled while in airplane mode */
148 private static final int WIFI_ENABLED_AIRPLANE_OVERRIDE = 2;
149
150 private AtomicInteger mWifiState = new AtomicInteger(WIFI_DISABLED);
151 private AtomicBoolean mAirplaneModeOn = new AtomicBoolean(false);
152
Irfan Sheriffa2a1b912010-06-07 09:03:04 -0700153 private boolean mIsReceiverRegistered = false;
154
Irfan Sheriff0d255342010-07-28 09:35:20 -0700155
156 NetworkInfo mNetworkInfo = new NetworkInfo(ConnectivityManager.TYPE_WIFI, 0, "WIFI", "");
157
158 // Variables relating to the 'available networks' notification
159 /**
160 * The icon to show in the 'available networks' notification. This will also
161 * be the ID of the Notification given to the NotificationManager.
162 */
163 private static final int ICON_NETWORKS_AVAILABLE =
164 com.android.internal.R.drawable.stat_notify_wifi_in_range;
165 /**
166 * When a notification is shown, we wait this amount before possibly showing it again.
167 */
168 private final long NOTIFICATION_REPEAT_DELAY_MS;
169 /**
170 * Whether the user has set the setting to show the 'available networks' notification.
171 */
172 private boolean mNotificationEnabled;
173 /**
174 * Observes the user setting to keep {@link #mNotificationEnabled} in sync.
175 */
176 private NotificationEnabledSettingObserver mNotificationEnabledSettingObserver;
177 /**
178 * The {@link System#currentTimeMillis()} must be at least this value for us
179 * to show the notification again.
180 */
181 private long mNotificationRepeatTime;
182 /**
183 * The Notification object given to the NotificationManager.
184 */
185 private Notification mNotification;
186 /**
187 * Whether the notification is being shown, as set by us. That is, if the
188 * user cancels the notification, we will not receive the callback so this
189 * will still be true. We only guarantee if this is false, then the
190 * notification is not showing.
191 */
192 private boolean mNotificationShown;
193 /**
194 * The number of continuous scans that must occur before consider the
195 * supplicant in a scanning state. This allows supplicant to associate with
196 * remembered networks that are in the scan results.
197 */
198 private static final int NUM_SCANS_BEFORE_ACTUALLY_SCANNING = 3;
199 /**
200 * The number of scans since the last network state change. When this
201 * exceeds {@link #NUM_SCANS_BEFORE_ACTUALLY_SCANNING}, we consider the
202 * supplicant to actually be scanning. When the network state changes to
203 * something other than scanning, we reset this to 0.
204 */
205 private int mNumScansSinceNetworkStateChange;
Jaikumar Ganesh7440fc22010-09-27 17:04:14 -0700206
Dianne Hackborn03f3cb02010-09-17 23:12:26 -0700207 /**
Wink Saville4b7ba092010-10-20 15:37:41 -0700208 * Asynchronous channel to WifiStateMachine
209 */
Irfan Sheriff227bec42011-02-15 19:30:27 -0800210 private AsyncChannel mWifiStateMachineChannel;
Wink Saville4b7ba092010-10-20 15:37:41 -0700211
212 /**
Irfan Sheriff227bec42011-02-15 19:30:27 -0800213 * Clients receiving asynchronous messages
Wink Saville4b7ba092010-10-20 15:37:41 -0700214 */
Irfan Sheriff227bec42011-02-15 19:30:27 -0800215 private List<AsyncChannel> mClients = new ArrayList<AsyncChannel>();
Wink Saville4b7ba092010-10-20 15:37:41 -0700216
Irfan Sheriff227bec42011-02-15 19:30:27 -0800217 /**
218 * Handles client connections
219 */
220 private class AsyncServiceHandler extends Handler {
221
222 AsyncServiceHandler(android.os.Looper looper) {
Wink Saville4b7ba092010-10-20 15:37:41 -0700223 super(looper);
Wink Saville4b7ba092010-10-20 15:37:41 -0700224 }
225
226 @Override
227 public void handleMessage(Message msg) {
228 switch (msg.what) {
229 case AsyncChannel.CMD_CHANNEL_HALF_CONNECTED: {
230 if (msg.arg1 == AsyncChannel.STATUS_SUCCESSFUL) {
Irfan Sheriff227bec42011-02-15 19:30:27 -0800231 Slog.d(TAG, "New client listening to asynchronous messages");
232 mClients.add((AsyncChannel) msg.obj);
Wink Saville4b7ba092010-10-20 15:37:41 -0700233 } else {
Irfan Sheriff227bec42011-02-15 19:30:27 -0800234 Slog.e(TAG, "Client connection failure, error=" + msg.arg1);
235 }
236 break;
237 }
Irfan Sheriffc23971b2011-03-04 17:06:31 -0800238 case AsyncChannel.CMD_CHANNEL_DISCONNECTED: {
239 if (msg.arg1 == AsyncChannel.STATUS_SEND_UNSUCCESSFUL) {
240 Slog.d(TAG, "Send failed, client connection lost");
241 } else {
242 Slog.d(TAG, "Client connection lost with reason: " + msg.arg1);
243 }
244 mClients.remove((AsyncChannel) msg.obj);
245 break;
246 }
Irfan Sheriff227bec42011-02-15 19:30:27 -0800247 case AsyncChannel.CMD_CHANNEL_FULL_CONNECTION: {
248 AsyncChannel ac = new AsyncChannel();
249 ac.connect(mContext, this, msg.replyTo);
250 break;
251 }
Irfan Sheriffebe606f2011-02-24 11:39:15 -0800252 case WifiManager.CMD_ENABLE_TRAFFIC_STATS_POLL: {
Irfan Sheriff227bec42011-02-15 19:30:27 -0800253 mEnableTrafficStatsPoll = (msg.arg1 == 1);
254 mTrafficStatsPollToken++;
255 if (mEnableTrafficStatsPoll) {
256 notifyOnDataActivity();
Irfan Sheriffebe606f2011-02-24 11:39:15 -0800257 sendMessageDelayed(Message.obtain(this, WifiManager.CMD_TRAFFIC_STATS_POLL,
Irfan Sheriff227bec42011-02-15 19:30:27 -0800258 mTrafficStatsPollToken, 0), POLL_TRAFFIC_STATS_INTERVAL_MSECS);
259 }
260 break;
261 }
Irfan Sheriffebe606f2011-02-24 11:39:15 -0800262 case WifiManager.CMD_TRAFFIC_STATS_POLL: {
Irfan Sheriff227bec42011-02-15 19:30:27 -0800263 if (msg.arg1 == mTrafficStatsPollToken) {
264 notifyOnDataActivity();
Irfan Sheriffebe606f2011-02-24 11:39:15 -0800265 sendMessageDelayed(Message.obtain(this, WifiManager.CMD_TRAFFIC_STATS_POLL,
Irfan Sheriff227bec42011-02-15 19:30:27 -0800266 mTrafficStatsPollToken, 0), POLL_TRAFFIC_STATS_INTERVAL_MSECS);
Wink Saville4b7ba092010-10-20 15:37:41 -0700267 }
268 break;
269 }
Irfan Sheriffebe606f2011-02-24 11:39:15 -0800270 case WifiManager.CMD_CONNECT_NETWORK: {
271 if (msg.obj != null) {
272 mWifiStateMachine.connectNetwork((WifiConfiguration)msg.obj);
273 } else {
274 mWifiStateMachine.connectNetwork(msg.arg1);
275 }
276 break;
277 }
278 case WifiManager.CMD_SAVE_NETWORK: {
279 mWifiStateMachine.saveNetwork((WifiConfiguration)msg.obj);
280 break;
281 }
282 case WifiManager.CMD_FORGET_NETWORK: {
283 mWifiStateMachine.forgetNetwork(msg.arg1);
284 break;
285 }
286 case WifiManager.CMD_START_WPS: {
287 //replyTo has the original source
288 mWifiStateMachine.startWps(msg.replyTo, (WpsConfiguration)msg.obj);
289 break;
290 }
Wink Saville4b7ba092010-10-20 15:37:41 -0700291 default: {
292 Slog.d(TAG, "WifiServicehandler.handleMessage ignoring msg=" + msg);
293 break;
294 }
295 }
296 }
297 }
Irfan Sheriff227bec42011-02-15 19:30:27 -0800298 private AsyncServiceHandler mAsyncServiceHandler;
299
300 /**
301 * Handles interaction with WifiStateMachine
302 */
303 private class WifiStateMachineHandler extends Handler {
304 private AsyncChannel mWsmChannel;
305
306 WifiStateMachineHandler(android.os.Looper looper) {
307 super(looper);
308 mWsmChannel = new AsyncChannel();
309 mWsmChannel.connect(mContext, this, mWifiStateMachine.getHandler());
310 }
311
312 @Override
313 public void handleMessage(Message msg) {
314 switch (msg.what) {
315 case AsyncChannel.CMD_CHANNEL_HALF_CONNECTED: {
316 if (msg.arg1 == AsyncChannel.STATUS_SUCCESSFUL) {
317 mWifiStateMachineChannel = mWsmChannel;
318 } else {
319 Slog.e(TAG, "WifiStateMachine connection failure, error=" + msg.arg1);
320 mWifiStateMachineChannel = null;
321 }
322 break;
323 }
Irfan Sheriff6da83d52011-06-06 12:54:06 -0700324 case AsyncChannel.CMD_CHANNEL_DISCONNECTED: {
325 Slog.e(TAG, "WifiStateMachine channel lost, msg.arg1 =" + msg.arg1);
326 mWifiStateMachineChannel = null;
327 //Re-establish connection to state machine
328 mWsmChannel.connect(mContext, this, mWifiStateMachine.getHandler());
329 break;
330 }
Irfan Sheriff227bec42011-02-15 19:30:27 -0800331 default: {
332 Slog.d(TAG, "WifiStateMachineHandler.handleMessage ignoring msg=" + msg);
333 break;
334 }
335 }
336 }
337 }
338 WifiStateMachineHandler mWifiStateMachineHandler;
Wink Saville4b7ba092010-10-20 15:37:41 -0700339
340 /**
Dianne Hackborn03f3cb02010-09-17 23:12:26 -0700341 * Temporary for computing UIDS that are responsible for starting WIFI.
342 * Protected by mWifiStateTracker lock.
343 */
344 private final WorkSource mTmpWorkSource = new WorkSource();
Irfan Sheriff0d255342010-07-28 09:35:20 -0700345
346 WifiService(Context context) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800347 mContext = context;
Irfan Sheriff227bec42011-02-15 19:30:27 -0800348
349 mInterfaceName = SystemProperties.get("wifi.interface", "wlan0");
350
351 mWifiStateMachine = new WifiStateMachine(mContext, mInterfaceName);
Irfan Sheriff0d255342010-07-28 09:35:20 -0700352 mWifiStateMachine.enableRssiPolling(true);
The Android Open Source Project10592532009-03-18 17:39:46 -0700353 mBatteryStats = BatteryStatsService.getService();
Jaikumar Ganesh084c6652009-12-07 10:58:18 -0800354
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800355 mAlarmManager = (AlarmManager)mContext.getSystemService(Context.ALARM_SERVICE);
356 Intent idleIntent = new Intent(ACTION_DEVICE_IDLE, null);
357 mIdleIntent = PendingIntent.getBroadcast(mContext, IDLE_REQUEST, idleIntent, 0);
358
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800359 mContext.registerReceiver(
360 new BroadcastReceiver() {
361 @Override
362 public void onReceive(Context context, Intent intent) {
Irfan Sheriff658772f2011-03-08 14:52:31 -0800363 mAirplaneModeOn.set(isAirplaneModeOn());
364 /* On airplane mode disable, restore wifi state if necessary */
365 if (!mAirplaneModeOn.get() && (testAndClearWifiSavedState() ||
366 mWifiState.get() == WIFI_ENABLED_AIRPLANE_OVERRIDE)) {
367 persistWifiEnabled(true);
Irfan Sheriffb2e6c012010-04-05 11:57:56 -0700368 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800369 updateWifiState();
370 }
371 },
372 new IntentFilter(Intent.ACTION_AIRPLANE_MODE_CHANGED));
373
Irfan Sheriff0d255342010-07-28 09:35:20 -0700374 IntentFilter filter = new IntentFilter();
375 filter.addAction(WifiManager.WIFI_STATE_CHANGED_ACTION);
376 filter.addAction(WifiManager.NETWORK_STATE_CHANGED_ACTION);
377 filter.addAction(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION);
378
379 mContext.registerReceiver(
380 new BroadcastReceiver() {
381 @Override
382 public void onReceive(Context context, Intent intent) {
383 if (intent.getAction().equals(WifiManager.WIFI_STATE_CHANGED_ACTION)) {
384 // reset & clear notification on any wifi state change
385 resetNotification();
386 } else if (intent.getAction().equals(
387 WifiManager.NETWORK_STATE_CHANGED_ACTION)) {
388 mNetworkInfo = (NetworkInfo) intent.getParcelableExtra(
389 WifiManager.EXTRA_NETWORK_INFO);
390 // reset & clear notification on a network connect & disconnect
391 switch(mNetworkInfo.getDetailedState()) {
392 case CONNECTED:
393 case DISCONNECTED:
Irfan Sheriff227bec42011-02-15 19:30:27 -0800394 evaluateTrafficStatsPolling();
Irfan Sheriff0d255342010-07-28 09:35:20 -0700395 resetNotification();
396 break;
397 }
398 } else if (intent.getAction().equals(
399 WifiManager.SCAN_RESULTS_AVAILABLE_ACTION)) {
400 checkAndSetNotification();
401 }
402 }
403 }, filter);
404
Irfan Sheriff227bec42011-02-15 19:30:27 -0800405 HandlerThread wifiThread = new HandlerThread("WifiService");
406 wifiThread.start();
407 mAsyncServiceHandler = new AsyncServiceHandler(wifiThread.getLooper());
408 mWifiStateMachineHandler = new WifiStateMachineHandler(wifiThread.getLooper());
409
Irfan Sheriff0d255342010-07-28 09:35:20 -0700410 // Setting is in seconds
411 NOTIFICATION_REPEAT_DELAY_MS = Settings.Secure.getInt(context.getContentResolver(),
412 Settings.Secure.WIFI_NETWORKS_AVAILABLE_REPEAT_DELAY, 900) * 1000l;
413 mNotificationEnabledSettingObserver = new NotificationEnabledSettingObserver(new Handler());
414 mNotificationEnabledSettingObserver.register();
Irfan Sherifffcc08452011-02-17 16:44:54 -0800415
416 mBackgroundScanSupported = mContext.getResources().getBoolean(
417 com.android.internal.R.bool.config_wifi_background_scan_support);
Irfan Sheriff7b009782010-03-11 16:37:45 -0800418 }
Irfan Sheriff5321aef2010-02-12 12:35:59 -0800419
Irfan Sheriff7b009782010-03-11 16:37:45 -0800420 /**
421 * Check if Wi-Fi needs to be enabled and start
422 * if needed
Irfan Sheriff60e3ba02010-04-02 12:18:45 -0700423 *
424 * This function is used only at boot time
Irfan Sheriff7b009782010-03-11 16:37:45 -0800425 */
Irfan Sheriff0d255342010-07-28 09:35:20 -0700426 public void checkAndStartWifi() {
Irfan Sheriff658772f2011-03-08 14:52:31 -0800427 mAirplaneModeOn.set(isAirplaneModeOn());
428 mWifiState.set(getPersistedWifiState());
429 /* Start if Wi-Fi should be enabled or the saved state indicates Wi-Fi was on */
430 boolean wifiEnabled = shouldWifiBeEnabled() || testAndClearWifiSavedState();
Irfan Sheriff7b009782010-03-11 16:37:45 -0800431 Slog.i(TAG, "WifiService starting up with Wi-Fi " +
432 (wifiEnabled ? "enabled" : "disabled"));
Irfan Sheriffb99fe5e2010-03-26 14:56:07 -0700433 setWifiEnabled(wifiEnabled);
Irfan Sheriff5321aef2010-02-12 12:35:59 -0800434 }
435
Irfan Sheriffa3bd4092010-03-24 17:58:59 -0700436 private boolean testAndClearWifiSavedState() {
437 final ContentResolver cr = mContext.getContentResolver();
438 int wifiSavedState = 0;
439 try {
440 wifiSavedState = Settings.Secure.getInt(cr, Settings.Secure.WIFI_SAVED_STATE);
441 if(wifiSavedState == 1)
442 Settings.Secure.putInt(cr, Settings.Secure.WIFI_SAVED_STATE, 0);
443 } catch (Settings.SettingNotFoundException e) {
444 ;
445 }
446 return (wifiSavedState == 1);
447 }
448
Irfan Sheriff658772f2011-03-08 14:52:31 -0800449 private int getPersistedWifiState() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800450 final ContentResolver cr = mContext.getContentResolver();
451 try {
Irfan Sheriff658772f2011-03-08 14:52:31 -0800452 return Settings.Secure.getInt(cr, Settings.Secure.WIFI_ON);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800453 } catch (Settings.SettingNotFoundException e) {
Irfan Sheriff658772f2011-03-08 14:52:31 -0800454 Settings.Secure.putInt(cr, Settings.Secure.WIFI_ON, WIFI_DISABLED);
455 return WIFI_DISABLED;
456 }
457 }
458
459 private boolean shouldWifiBeEnabled() {
460 if (mAirplaneModeOn.get()) {
461 return mWifiState.get() == WIFI_ENABLED_AIRPLANE_OVERRIDE;
462 } else {
463 return mWifiState.get() != WIFI_DISABLED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800464 }
465 }
466
467 private void persistWifiEnabled(boolean enabled) {
468 final ContentResolver cr = mContext.getContentResolver();
Irfan Sheriff658772f2011-03-08 14:52:31 -0800469 if (enabled) {
470 if (isAirplaneModeOn() && isAirplaneToggleable()) {
471 mWifiState.set(WIFI_ENABLED_AIRPLANE_OVERRIDE);
472 } else {
473 mWifiState.set(WIFI_ENABLED);
474 }
475 } else {
476 mWifiState.set(WIFI_DISABLED);
477 }
478 Settings.Secure.putInt(cr, Settings.Secure.WIFI_ON, mWifiState.get());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800479 }
480
Irfan Sheriff658772f2011-03-08 14:52:31 -0800481
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800482 /**
483 * see {@link android.net.wifi.WifiManager#pingSupplicant()}
Irfan Sheriffa2a1b912010-06-07 09:03:04 -0700484 * @return {@code true} if the operation succeeds, {@code false} otherwise
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800485 */
486 public boolean pingSupplicant() {
Irfan Sheriffa2a1b912010-06-07 09:03:04 -0700487 enforceAccessPermission();
Irfan Sheriff227bec42011-02-15 19:30:27 -0800488 if (mWifiStateMachineChannel != null) {
489 return mWifiStateMachine.syncPingSupplicant(mWifiStateMachineChannel);
Irfan Sheriff1406bcb2010-10-28 14:41:39 -0700490 } else {
Irfan Sheriff227bec42011-02-15 19:30:27 -0800491 Slog.e(TAG, "mWifiStateMachineChannel is not initialized");
Irfan Sheriff1406bcb2010-10-28 14:41:39 -0700492 return false;
493 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800494 }
495
496 /**
497 * see {@link android.net.wifi.WifiManager#startScan()}
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800498 */
Irfan Sheriffe4984752010-08-19 11:29:22 -0700499 public void startScan(boolean forceActive) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800500 enforceChangePermission();
Irfan Sheriffe4984752010-08-19 11:29:22 -0700501 mWifiStateMachine.startScan(forceActive);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800502 }
503
504 private void enforceAccessPermission() {
505 mContext.enforceCallingOrSelfPermission(android.Manifest.permission.ACCESS_WIFI_STATE,
506 "WifiService");
507 }
508
509 private void enforceChangePermission() {
510 mContext.enforceCallingOrSelfPermission(android.Manifest.permission.CHANGE_WIFI_STATE,
511 "WifiService");
512
513 }
514
Robert Greenwaltfc1b15c2009-05-22 15:09:51 -0700515 private void enforceMulticastChangePermission() {
516 mContext.enforceCallingOrSelfPermission(
517 android.Manifest.permission.CHANGE_WIFI_MULTICAST_STATE,
518 "WifiService");
519 }
520
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800521 /**
Irfan Sheriffa2a1b912010-06-07 09:03:04 -0700522 * see {@link android.net.wifi.WifiManager#setWifiEnabled(boolean)}
523 * @param enable {@code true} to enable, {@code false} to disable.
524 * @return {@code true} if the enable/disable operation was
525 * started or is already in the queue.
526 */
527 public synchronized boolean setWifiEnabled(boolean enable) {
528 enforceChangePermission();
529
530 if (DBG) {
Irfan Sheriff0d255342010-07-28 09:35:20 -0700531 Slog.e(TAG, "Invoking mWifiStateMachine.setWifiEnabled\n");
Irfan Sheriffa2a1b912010-06-07 09:03:04 -0700532 }
533
Dianne Hackborn03f3cb02010-09-17 23:12:26 -0700534 if (enable) {
535 reportStartWorkSource();
536 }
Irfan Sheriff0d255342010-07-28 09:35:20 -0700537 mWifiStateMachine.setWifiEnabled(enable);
Irfan Sheriff61180692010-08-18 16:07:39 -0700538
539 /*
540 * Caller might not have WRITE_SECURE_SETTINGS,
541 * only CHANGE_WIFI_STATE is enforced
542 */
543 long ident = Binder.clearCallingIdentity();
Irfan Sheriffa2a1b912010-06-07 09:03:04 -0700544 persistWifiEnabled(enable);
Irfan Sheriff61180692010-08-18 16:07:39 -0700545 Binder.restoreCallingIdentity(ident);
Irfan Sheriffa2a1b912010-06-07 09:03:04 -0700546
547 if (enable) {
548 if (!mIsReceiverRegistered) {
549 registerForBroadcasts();
550 mIsReceiverRegistered = true;
551 }
552 } else if (mIsReceiverRegistered){
553 mContext.unregisterReceiver(mReceiver);
554 mIsReceiverRegistered = false;
555 }
556
557 return true;
558 }
559
560 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800561 * see {@link WifiManager#getWifiState()}
562 * @return One of {@link WifiManager#WIFI_STATE_DISABLED},
563 * {@link WifiManager#WIFI_STATE_DISABLING},
564 * {@link WifiManager#WIFI_STATE_ENABLED},
565 * {@link WifiManager#WIFI_STATE_ENABLING},
566 * {@link WifiManager#WIFI_STATE_UNKNOWN}
567 */
568 public int getWifiEnabledState() {
569 enforceAccessPermission();
Irfan Sheriffd8134ff2010-08-22 17:06:34 -0700570 return mWifiStateMachine.syncGetWifiState();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800571 }
572
573 /**
Irfan Sheriffc2f54c22010-03-18 14:02:22 -0700574 * see {@link android.net.wifi.WifiManager#setWifiApEnabled(WifiConfiguration, boolean)}
Irfan Sheriff5321aef2010-02-12 12:35:59 -0800575 * @param wifiConfig SSID, security and channel details as
576 * part of WifiConfiguration
Irfan Sheriffa2a1b912010-06-07 09:03:04 -0700577 * @param enabled true to enable and false to disable
Irfan Sheriff5321aef2010-02-12 12:35:59 -0800578 */
Irfan Sheriffffcea7a2011-05-10 16:26:06 -0700579 public void setWifiApEnabled(WifiConfiguration wifiConfig, boolean enabled) {
Irfan Sheriff5321aef2010-02-12 12:35:59 -0800580 enforceChangePermission();
Irfan Sheriff0d255342010-07-28 09:35:20 -0700581 mWifiStateMachine.setWifiApEnabled(wifiConfig, enabled);
Irfan Sheriff5321aef2010-02-12 12:35:59 -0800582 }
583
Irfan Sheriffa2a1b912010-06-07 09:03:04 -0700584 /**
585 * see {@link WifiManager#getWifiApState()}
586 * @return One of {@link WifiManager#WIFI_AP_STATE_DISABLED},
587 * {@link WifiManager#WIFI_AP_STATE_DISABLING},
588 * {@link WifiManager#WIFI_AP_STATE_ENABLED},
589 * {@link WifiManager#WIFI_AP_STATE_ENABLING},
590 * {@link WifiManager#WIFI_AP_STATE_FAILED}
591 */
592 public int getWifiApEnabledState() {
Irfan Sheriff17b232b2010-06-24 11:32:26 -0700593 enforceAccessPermission();
Irfan Sheriffd8134ff2010-08-22 17:06:34 -0700594 return mWifiStateMachine.syncGetWifiApState();
Irfan Sheriffa2a1b912010-06-07 09:03:04 -0700595 }
596
597 /**
598 * see {@link WifiManager#getWifiApConfiguration()}
599 * @return soft access point configuration
600 */
Irfan Sheriffffcea7a2011-05-10 16:26:06 -0700601 public WifiConfiguration getWifiApConfiguration() {
602 enforceAccessPermission();
Irfan Sheriff6da83d52011-06-06 12:54:06 -0700603 if (mWifiStateMachineChannel != null) {
604 return mWifiStateMachine.syncGetWifiApConfiguration(mWifiStateMachineChannel);
605 } else {
606 Slog.e(TAG, "mWifiStateMachineChannel is not initialized");
607 return null;
608 }
Irfan Sheriff9ab518ad2010-03-12 15:48:17 -0800609 }
610
Irfan Sheriffa2a1b912010-06-07 09:03:04 -0700611 /**
612 * see {@link WifiManager#setWifiApConfiguration(WifiConfiguration)}
613 * @param wifiConfig WifiConfiguration details for soft access point
614 */
Irfan Sheriffffcea7a2011-05-10 16:26:06 -0700615 public void setWifiApConfiguration(WifiConfiguration wifiConfig) {
Irfan Sheriff17b232b2010-06-24 11:32:26 -0700616 enforceChangePermission();
Irfan Sheriff9ab518ad2010-03-12 15:48:17 -0800617 if (wifiConfig == null)
618 return;
Irfan Sheriffffcea7a2011-05-10 16:26:06 -0700619 mWifiStateMachine.setWifiApConfiguration(wifiConfig);
Irfan Sheriff9ab518ad2010-03-12 15:48:17 -0800620 }
621
Irfan Sheriff5321aef2010-02-12 12:35:59 -0800622 /**
Irfan Sheriffa2a1b912010-06-07 09:03:04 -0700623 * see {@link android.net.wifi.WifiManager#disconnect()}
Irfan Sheriff5321aef2010-02-12 12:35:59 -0800624 */
Irfan Sheriffe4984752010-08-19 11:29:22 -0700625 public void disconnect() {
Irfan Sheriffa2a1b912010-06-07 09:03:04 -0700626 enforceChangePermission();
Irfan Sheriffe4984752010-08-19 11:29:22 -0700627 mWifiStateMachine.disconnectCommand();
Irfan Sheriff5321aef2010-02-12 12:35:59 -0800628 }
629
630 /**
Irfan Sheriffa2a1b912010-06-07 09:03:04 -0700631 * see {@link android.net.wifi.WifiManager#reconnect()}
Irfan Sheriff5321aef2010-02-12 12:35:59 -0800632 */
Irfan Sheriffe4984752010-08-19 11:29:22 -0700633 public void reconnect() {
Irfan Sheriffa2a1b912010-06-07 09:03:04 -0700634 enforceChangePermission();
Irfan Sheriffe4984752010-08-19 11:29:22 -0700635 mWifiStateMachine.reconnectCommand();
Irfan Sheriff5321aef2010-02-12 12:35:59 -0800636 }
637
Irfan Sheriffa2a1b912010-06-07 09:03:04 -0700638 /**
639 * see {@link android.net.wifi.WifiManager#reassociate()}
Irfan Sheriffa2a1b912010-06-07 09:03:04 -0700640 */
Irfan Sheriffe4984752010-08-19 11:29:22 -0700641 public void reassociate() {
Irfan Sheriffa2a1b912010-06-07 09:03:04 -0700642 enforceChangePermission();
Irfan Sheriffe4984752010-08-19 11:29:22 -0700643 mWifiStateMachine.reassociateCommand();
Irfan Sheriff5321aef2010-02-12 12:35:59 -0800644 }
645
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800646 /**
647 * see {@link android.net.wifi.WifiManager#getConfiguredNetworks()}
648 * @return the list of configured networks
649 */
650 public List<WifiConfiguration> getConfiguredNetworks() {
651 enforceAccessPermission();
Irfan Sheriffd8134ff2010-08-22 17:06:34 -0700652 return mWifiStateMachine.syncGetConfiguredNetworks();
Chung-yih Wanga8d15942009-10-09 11:01:49 +0800653 }
654
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800655 /**
656 * see {@link android.net.wifi.WifiManager#addOrUpdateNetwork(WifiConfiguration)}
657 * @return the supplicant-assigned identifier for the new or updated
658 * network if the operation succeeds, or {@code -1} if it fails
659 */
Irfan Sheriff7aac5542009-12-22 21:42:17 -0800660 public int addOrUpdateNetwork(WifiConfiguration config) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800661 enforceChangePermission();
Irfan Sheriff227bec42011-02-15 19:30:27 -0800662 if (mWifiStateMachineChannel != null) {
663 return mWifiStateMachine.syncAddOrUpdateNetwork(mWifiStateMachineChannel, config);
Irfan Sheriff1406bcb2010-10-28 14:41:39 -0700664 } else {
Irfan Sheriff227bec42011-02-15 19:30:27 -0800665 Slog.e(TAG, "mWifiStateMachineChannel is not initialized");
Irfan Sheriff1406bcb2010-10-28 14:41:39 -0700666 return -1;
667 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800668 }
669
Irfan Sheriffa2a1b912010-06-07 09:03:04 -0700670 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800671 * See {@link android.net.wifi.WifiManager#removeNetwork(int)}
672 * @param netId the integer that identifies the network configuration
673 * to the supplicant
674 * @return {@code true} if the operation succeeded
675 */
676 public boolean removeNetwork(int netId) {
677 enforceChangePermission();
Irfan Sheriff227bec42011-02-15 19:30:27 -0800678 if (mWifiStateMachineChannel != null) {
679 return mWifiStateMachine.syncRemoveNetwork(mWifiStateMachineChannel, netId);
Wink Saville4b7ba092010-10-20 15:37:41 -0700680 } else {
Irfan Sheriff227bec42011-02-15 19:30:27 -0800681 Slog.e(TAG, "mWifiStateMachineChannel is not initialized");
Wink Saville4b7ba092010-10-20 15:37:41 -0700682 return false;
683 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800684 }
685
686 /**
687 * See {@link android.net.wifi.WifiManager#enableNetwork(int, boolean)}
688 * @param netId the integer that identifies the network configuration
689 * to the supplicant
690 * @param disableOthers if true, disable all other networks.
691 * @return {@code true} if the operation succeeded
692 */
693 public boolean enableNetwork(int netId, boolean disableOthers) {
694 enforceChangePermission();
Irfan Sheriff227bec42011-02-15 19:30:27 -0800695 if (mWifiStateMachineChannel != null) {
696 return mWifiStateMachine.syncEnableNetwork(mWifiStateMachineChannel, netId,
697 disableOthers);
Irfan Sheriff1406bcb2010-10-28 14:41:39 -0700698 } else {
Irfan Sheriff227bec42011-02-15 19:30:27 -0800699 Slog.e(TAG, "mWifiStateMachineChannel is not initialized");
Irfan Sheriff1406bcb2010-10-28 14:41:39 -0700700 return false;
701 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800702 }
703
704 /**
705 * See {@link android.net.wifi.WifiManager#disableNetwork(int)}
706 * @param netId the integer that identifies the network configuration
707 * to the supplicant
708 * @return {@code true} if the operation succeeded
709 */
710 public boolean disableNetwork(int netId) {
711 enforceChangePermission();
Irfan Sheriff227bec42011-02-15 19:30:27 -0800712 if (mWifiStateMachineChannel != null) {
713 return mWifiStateMachine.syncDisableNetwork(mWifiStateMachineChannel, netId);
Irfan Sheriff1406bcb2010-10-28 14:41:39 -0700714 } else {
Irfan Sheriff227bec42011-02-15 19:30:27 -0800715 Slog.e(TAG, "mWifiStateMachineChannel is not initialized");
Irfan Sheriff1406bcb2010-10-28 14:41:39 -0700716 return false;
717 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800718 }
719
720 /**
721 * See {@link android.net.wifi.WifiManager#getConnectionInfo()}
722 * @return the Wi-Fi information, contained in {@link WifiInfo}.
723 */
724 public WifiInfo getConnectionInfo() {
725 enforceAccessPermission();
726 /*
727 * Make sure we have the latest information, by sending
728 * a status request to the supplicant.
729 */
Irfan Sheriffd8134ff2010-08-22 17:06:34 -0700730 return mWifiStateMachine.syncRequestConnectionInfo();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800731 }
732
733 /**
734 * Return the results of the most recent access point scan, in the form of
735 * a list of {@link ScanResult} objects.
736 * @return the list of results
737 */
738 public List<ScanResult> getScanResults() {
739 enforceAccessPermission();
Irfan Sheriffd8134ff2010-08-22 17:06:34 -0700740 return mWifiStateMachine.syncGetScanResultsList();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800741 }
742
743 /**
744 * Tell the supplicant to persist the current list of configured networks.
745 * @return {@code true} if the operation succeeded
Irfan Sheriffa2a1b912010-06-07 09:03:04 -0700746 *
747 * TODO: deprecate this
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800748 */
749 public boolean saveConfiguration() {
Irfan Sheriffa2a1b912010-06-07 09:03:04 -0700750 boolean result = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800751 enforceChangePermission();
Irfan Sheriff227bec42011-02-15 19:30:27 -0800752 if (mWifiStateMachineChannel != null) {
753 return mWifiStateMachine.syncSaveConfig(mWifiStateMachineChannel);
Irfan Sheriff1406bcb2010-10-28 14:41:39 -0700754 } else {
Irfan Sheriff227bec42011-02-15 19:30:27 -0800755 Slog.e(TAG, "mWifiStateMachineChannel is not initialized");
Irfan Sheriff1406bcb2010-10-28 14:41:39 -0700756 return false;
757 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800758 }
759
760 /**
Irfan Sheriffed4f28b2010-10-29 15:32:10 -0700761 * Set the country code
762 * @param countryCode ISO 3166 country code.
Robert Greenwaltb5010cc2009-05-21 15:11:40 -0700763 * @param persist {@code true} if the setting should be remembered.
Irfan Sheriffed4f28b2010-10-29 15:32:10 -0700764 *
765 * The persist behavior exists so that wifi can fall back to the last
766 * persisted country code on a restart, when the locale information is
767 * not available from telephony.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800768 */
Irfan Sheriffed4f28b2010-10-29 15:32:10 -0700769 public void setCountryCode(String countryCode, boolean persist) {
770 Slog.i(TAG, "WifiService trying to set country code to " + countryCode +
771 " with persist set to " + persist);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800772 enforceChangePermission();
Irfan Sheriffed4f28b2010-10-29 15:32:10 -0700773 mWifiStateMachine.setCountryCode(countryCode, persist);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800774 }
775
776 /**
Irfan Sheriff36f74132010-11-04 16:57:37 -0700777 * Set the operational frequency band
778 * @param band One of
779 * {@link WifiManager#WIFI_FREQUENCY_BAND_AUTO},
780 * {@link WifiManager#WIFI_FREQUENCY_BAND_5GHZ},
781 * {@link WifiManager#WIFI_FREQUENCY_BAND_2GHZ},
782 * @param persist {@code true} if the setting should be remembered.
783 *
784 */
785 public void setFrequencyBand(int band, boolean persist) {
786 enforceChangePermission();
787 if (!isDualBandSupported()) return;
788 Slog.i(TAG, "WifiService trying to set frequency band to " + band +
789 " with persist set to " + persist);
790 mWifiStateMachine.setFrequencyBand(band, persist);
791 }
792
793
794 /**
795 * Get the operational frequency band
796 */
797 public int getFrequencyBand() {
798 enforceAccessPermission();
799 return mWifiStateMachine.getFrequencyBand();
800 }
801
802 public boolean isDualBandSupported() {
803 //TODO: Should move towards adding a driver API that checks at runtime
804 return mContext.getResources().getBoolean(
805 com.android.internal.R.bool.config_wifi_dual_band_support);
806 }
807
808 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800809 * Return the DHCP-assigned addresses from the last successful DHCP request,
810 * if any.
811 * @return the DHCP information
812 */
813 public DhcpInfo getDhcpInfo() {
814 enforceAccessPermission();
Irfan Sheriffd8134ff2010-08-22 17:06:34 -0700815 return mWifiStateMachine.syncGetDhcpInfo();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800816 }
817
Irfan Sheriff0d255342010-07-28 09:35:20 -0700818 /**
819 * see {@link android.net.wifi.WifiManager#startWifi}
820 *
821 */
822 public void startWifi() {
823 enforceChangePermission();
824 /* TODO: may be add permissions for access only to connectivity service
825 * TODO: if a start issued, keep wifi alive until a stop issued irrespective
826 * of WifiLock & device idle status unless wifi enabled status is toggled
827 */
828
829 mWifiStateMachine.setDriverStart(true);
830 mWifiStateMachine.reconnectCommand();
831 }
832
833 /**
834 * see {@link android.net.wifi.WifiManager#stopWifi}
835 *
836 */
837 public void stopWifi() {
838 enforceChangePermission();
839 /* TODO: may be add permissions for access only to connectivity service
840 * TODO: if a stop is issued, wifi is brought up only by startWifi
841 * unless wifi enabled status is toggled
842 */
843 mWifiStateMachine.setDriverStart(false);
844 }
845
846
847 /**
848 * see {@link android.net.wifi.WifiManager#addToBlacklist}
849 *
850 */
851 public void addToBlacklist(String bssid) {
852 enforceChangePermission();
853
854 mWifiStateMachine.addToBlacklist(bssid);
855 }
856
857 /**
858 * see {@link android.net.wifi.WifiManager#clearBlacklist}
859 *
860 */
861 public void clearBlacklist() {
862 enforceChangePermission();
863
864 mWifiStateMachine.clearBlacklist();
865 }
866
Irfan Sheriff227bec42011-02-15 19:30:27 -0800867 /**
868 * Get a reference to handler. This is used by a client to establish
869 * an AsyncChannel communication with WifiService
870 */
871 public Messenger getMessenger() {
Irfan Sheriffebe606f2011-02-24 11:39:15 -0800872 /* Enforce the highest permissions
873 TODO: when we consider exposing the asynchronous API, think about
874 how to provide both access and change permissions seperately
875 */
Irfan Sheriff227bec42011-02-15 19:30:27 -0800876 enforceAccessPermission();
Irfan Sheriffebe606f2011-02-24 11:39:15 -0800877 enforceChangePermission();
Irfan Sheriff227bec42011-02-15 19:30:27 -0800878 return new Messenger(mAsyncServiceHandler);
879 }
880
Irfan Sheriff4aeca7c52011-03-10 16:53:33 -0800881 /**
882 * Get the IP and proxy configuration file
883 */
884 public String getConfigFile() {
885 enforceAccessPermission();
886 return mWifiStateMachine.getConfigFile();
887 }
888
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800889 private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
890 @Override
891 public void onReceive(Context context, Intent intent) {
892 String action = intent.getAction();
893
Doug Zongker43866e02010-01-07 12:09:54 -0800894 long idleMillis =
895 Settings.Secure.getLong(mContext.getContentResolver(),
Irfan Sheriff4f5f7c92010-10-14 17:01:27 -0700896 Settings.Secure.WIFI_IDLE_MS, DEFAULT_IDLE_MS);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800897 int stayAwakeConditions =
Doug Zongker43866e02010-01-07 12:09:54 -0800898 Settings.System.getInt(mContext.getContentResolver(),
899 Settings.System.STAY_ON_WHILE_PLUGGED_IN, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800900 if (action.equals(Intent.ACTION_SCREEN_ON)) {
Joe Onorato431bb222010-10-18 19:13:23 -0400901 if (DBG) {
902 Slog.d(TAG, "ACTION_SCREEN_ON");
903 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800904 mAlarmManager.cancel(mIdleIntent);
905 mDeviceIdle = false;
906 mScreenOff = false;
Dianne Hackborn58e0eef2010-09-16 01:22:10 -0700907 // Once the screen is on, we are not keeping WIFI running
908 // because of any locks so clear that tracking immediately.
909 reportStartWorkSource();
Irfan Sheriff227bec42011-02-15 19:30:27 -0800910 evaluateTrafficStatsPolling();
Irfan Sheriff0d255342010-07-28 09:35:20 -0700911 mWifiStateMachine.enableRssiPolling(true);
Irfan Sherifffcc08452011-02-17 16:44:54 -0800912 if (mBackgroundScanSupported) {
Irfan Sheriff2b7f6382011-03-25 14:29:19 -0700913 mWifiStateMachine.enableBackgroundScanCommand(false);
Irfan Sherifffcc08452011-02-17 16:44:54 -0800914 }
Irfan Sheriff8e86b892010-12-22 11:02:20 -0800915 mWifiStateMachine.enableAllNetworks();
Irfan Sheriff4f5f7c92010-10-14 17:01:27 -0700916 updateWifiState();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800917 } else if (action.equals(Intent.ACTION_SCREEN_OFF)) {
Joe Onorato431bb222010-10-18 19:13:23 -0400918 if (DBG) {
919 Slog.d(TAG, "ACTION_SCREEN_OFF");
920 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800921 mScreenOff = true;
Irfan Sheriff227bec42011-02-15 19:30:27 -0800922 evaluateTrafficStatsPolling();
Irfan Sheriff0d255342010-07-28 09:35:20 -0700923 mWifiStateMachine.enableRssiPolling(false);
Irfan Sherifffcc08452011-02-17 16:44:54 -0800924 if (mBackgroundScanSupported) {
Irfan Sheriff2b7f6382011-03-25 14:29:19 -0700925 mWifiStateMachine.enableBackgroundScanCommand(true);
Irfan Sherifffcc08452011-02-17 16:44:54 -0800926 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800927 /*
928 * Set a timer to put Wi-Fi to sleep, but only if the screen is off
929 * AND the "stay on while plugged in" setting doesn't match the
930 * current power conditions (i.e, not plugged in, plugged in to USB,
931 * or plugged in to AC).
932 */
933 if (!shouldWifiStayAwake(stayAwakeConditions, mPluggedType)) {
Irfan Sheriffd8134ff2010-08-22 17:06:34 -0700934 WifiInfo info = mWifiStateMachine.syncRequestConnectionInfo();
San Mehatfa6c7112009-07-07 09:34:44 -0700935 if (info.getSupplicantState() != SupplicantState.COMPLETED) {
Robert Greenwalt84612ea62009-09-30 09:04:22 -0700936 // we used to go to sleep immediately, but this caused some race conditions
Irfan Sheriffa2a1b912010-06-07 09:03:04 -0700937 // we don't have time to track down for this release. Delay instead,
938 // but not as long as we would if connected (below)
Robert Greenwalt84612ea62009-09-30 09:04:22 -0700939 // TODO - fix the race conditions and switch back to the immediate turn-off
940 long triggerTime = System.currentTimeMillis() + (2*60*1000); // 2 min
Joe Onorato431bb222010-10-18 19:13:23 -0400941 if (DBG) {
942 Slog.d(TAG, "setting ACTION_DEVICE_IDLE timer for 120,000 ms");
943 }
Robert Greenwalt84612ea62009-09-30 09:04:22 -0700944 mAlarmManager.set(AlarmManager.RTC_WAKEUP, triggerTime, mIdleIntent);
945 // // do not keep Wifi awake when screen is off if Wifi is not associated
946 // mDeviceIdle = true;
947 // updateWifiState();
Mike Lockwoodd9c32bc2009-05-18 14:14:15 -0400948 } else {
949 long triggerTime = System.currentTimeMillis() + idleMillis;
Joe Onorato431bb222010-10-18 19:13:23 -0400950 if (DBG) {
951 Slog.d(TAG, "setting ACTION_DEVICE_IDLE timer for " + idleMillis
952 + "ms");
953 }
Mike Lockwoodd9c32bc2009-05-18 14:14:15 -0400954 mAlarmManager.set(AlarmManager.RTC_WAKEUP, triggerTime, mIdleIntent);
955 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800956 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800957 } else if (action.equals(ACTION_DEVICE_IDLE)) {
Joe Onorato431bb222010-10-18 19:13:23 -0400958 if (DBG) {
959 Slog.d(TAG, "got ACTION_DEVICE_IDLE");
960 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800961 mDeviceIdle = true;
Dianne Hackborn58e0eef2010-09-16 01:22:10 -0700962 reportStartWorkSource();
Irfan Sheriff4f5f7c92010-10-14 17:01:27 -0700963 updateWifiState();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800964 } else if (action.equals(Intent.ACTION_BATTERY_CHANGED)) {
965 /*
966 * Set a timer to put Wi-Fi to sleep, but only if the screen is off
967 * AND we are transitioning from a state in which the device was supposed
968 * to stay awake to a state in which it is not supposed to stay awake.
969 * If "stay awake" state is not changing, we do nothing, to avoid resetting
970 * the already-set timer.
971 */
972 int pluggedType = intent.getIntExtra("plugged", 0);
Joe Onorato431bb222010-10-18 19:13:23 -0400973 if (DBG) {
974 Slog.d(TAG, "ACTION_BATTERY_CHANGED pluggedType: " + pluggedType);
975 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800976 if (mScreenOff && shouldWifiStayAwake(stayAwakeConditions, mPluggedType) &&
977 !shouldWifiStayAwake(stayAwakeConditions, pluggedType)) {
978 long triggerTime = System.currentTimeMillis() + idleMillis;
Joe Onorato431bb222010-10-18 19:13:23 -0400979 if (DBG) {
980 Slog.d(TAG, "setting ACTION_DEVICE_IDLE timer for " + idleMillis + "ms");
981 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800982 mAlarmManager.set(AlarmManager.RTC_WAKEUP, triggerTime, mIdleIntent);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800983 }
984 mPluggedType = pluggedType;
Irfan Sheriff65eaec82011-01-05 22:00:16 -0800985 } else if (action.equals(BluetoothAdapter.ACTION_CONNECTION_STATE_CHANGED)) {
986 int state = intent.getIntExtra(BluetoothAdapter.EXTRA_CONNECTION_STATE,
987 BluetoothAdapter.STATE_DISCONNECTED);
988 mWifiStateMachine.sendBluetoothAdapterStateChange(state);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800989 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800990 }
991
992 /**
993 * Determines whether the Wi-Fi chipset should stay awake or be put to
994 * sleep. Looks at the setting for the sleep policy and the current
995 * conditions.
Jaikumar Ganesh084c6652009-12-07 10:58:18 -0800996 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800997 * @see #shouldDeviceStayAwake(int, int)
998 */
999 private boolean shouldWifiStayAwake(int stayAwakeConditions, int pluggedType) {
Irfan Sheriff739f6bc2011-01-28 16:43:12 -08001000 //Never sleep as long as the user has not changed the settings
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001001 int wifiSleepPolicy = Settings.System.getInt(mContext.getContentResolver(),
Irfan Sheriff96b10d62011-01-11 15:40:35 -08001002 Settings.System.WIFI_SLEEP_POLICY,
Irfan Sheriff739f6bc2011-01-28 16:43:12 -08001003 Settings.System.WIFI_SLEEP_POLICY_NEVER);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001004
1005 if (wifiSleepPolicy == Settings.System.WIFI_SLEEP_POLICY_NEVER) {
1006 // Never sleep
1007 return true;
1008 } else if ((wifiSleepPolicy == Settings.System.WIFI_SLEEP_POLICY_NEVER_WHILE_PLUGGED) &&
1009 (pluggedType != 0)) {
1010 // Never sleep while plugged, and we're plugged
1011 return true;
1012 } else {
1013 // Default
1014 return shouldDeviceStayAwake(stayAwakeConditions, pluggedType);
1015 }
1016 }
Jaikumar Ganesh084c6652009-12-07 10:58:18 -08001017
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001018 /**
1019 * Determine whether the bit value corresponding to {@code pluggedType} is set in
1020 * the bit string {@code stayAwakeConditions}. Because a {@code pluggedType} value
1021 * of {@code 0} isn't really a plugged type, but rather an indication that the
1022 * device isn't plugged in at all, there is no bit value corresponding to a
1023 * {@code pluggedType} value of {@code 0}. That is why we shift by
Ben Dodson4e8620f2010-08-25 10:55:47 -07001024 * {@code pluggedType - 1} instead of by {@code pluggedType}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001025 * @param stayAwakeConditions a bit string specifying which "plugged types" should
1026 * keep the device (and hence Wi-Fi) awake.
1027 * @param pluggedType the type of plug (USB, AC, or none) for which the check is
1028 * being made
1029 * @return {@code true} if {@code pluggedType} indicates that the device is
1030 * supposed to stay awake, {@code false} otherwise.
1031 */
1032 private boolean shouldDeviceStayAwake(int stayAwakeConditions, int pluggedType) {
1033 return (stayAwakeConditions & pluggedType) != 0;
1034 }
1035 };
1036
Dianne Hackborn03f3cb02010-09-17 23:12:26 -07001037 private synchronized void reportStartWorkSource() {
1038 mTmpWorkSource.clear();
1039 if (mDeviceIdle) {
1040 for (int i=0; i<mLocks.mList.size(); i++) {
1041 mTmpWorkSource.add(mLocks.mList.get(i).mWorkSource);
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07001042 }
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07001043 }
Dianne Hackborn03f3cb02010-09-17 23:12:26 -07001044 mWifiStateMachine.updateBatteryWorkSource(mTmpWorkSource);
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07001045 }
Jaikumar Ganesh7440fc22010-09-27 17:04:14 -07001046
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001047 private void updateWifiState() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001048 boolean lockHeld = mLocks.hasLocks();
Irfan Sheriff5876a422010-08-12 20:26:23 -07001049 int strongestLockMode = WifiManager.WIFI_MODE_FULL;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001050 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 */
Irfan Sheriff658772f2011-03-08 14:52:31 -08001061 if (mAirplaneModeOn.get()) {
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 Sheriff658772f2011-03-08 14:52:31 -08001065 if (shouldWifiBeEnabled()) {
Irfan Sheriffa2a1b912010-06-07 09:03:04 -07001066 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) {
Irfan Sheriffebe606f2011-02-24 11:39:15 -08001533 msg = Message.obtain(mAsyncServiceHandler,
1534 WifiManager.CMD_ENABLE_TRAFFIC_STATS_POLL, 1, 0);
Irfan Sheriff227bec42011-02-15 19:30:27 -08001535 } else {
Irfan Sheriffebe606f2011-02-24 11:39:15 -08001536 msg = Message.obtain(mAsyncServiceHandler,
1537 WifiManager.CMD_ENABLE_TRAFFIC_STATS_POLL, 0, 0);
Irfan Sheriff227bec42011-02-15 19:30:27 -08001538 }
1539 msg.sendToTarget();
1540 }
1541
1542 private void notifyOnDataActivity() {
1543 long sent, received;
1544 long preTxPkts = mTxPkts, preRxPkts = mRxPkts;
1545 int dataActivity = WifiManager.DATA_ACTIVITY_NONE;
1546
1547 mTxPkts = TrafficStats.getTxPackets(mInterfaceName);
1548 mRxPkts = TrafficStats.getRxPackets(mInterfaceName);
1549
1550 if (preTxPkts > 0 || preRxPkts > 0) {
1551 sent = mTxPkts - preTxPkts;
1552 received = mRxPkts - preRxPkts;
1553 if (sent > 0) {
1554 dataActivity |= WifiManager.DATA_ACTIVITY_OUT;
1555 }
1556 if (received > 0) {
1557 dataActivity |= WifiManager.DATA_ACTIVITY_IN;
1558 }
1559
1560 if (dataActivity != mDataActivity && !mScreenOff) {
1561 mDataActivity = dataActivity;
1562 for (AsyncChannel client : mClients) {
1563 client.sendMessage(WifiManager.DATA_ACTIVITY_NOTIFICATION, mDataActivity);
1564 }
1565 }
1566 }
1567 }
1568
1569
Irfan Sheriff0d255342010-07-28 09:35:20 -07001570 private void checkAndSetNotification() {
1571 // If we shouldn't place a notification on available networks, then
1572 // don't bother doing any of the following
1573 if (!mNotificationEnabled) return;
1574
1575 State state = mNetworkInfo.getState();
1576 if ((state == NetworkInfo.State.DISCONNECTED)
1577 || (state == NetworkInfo.State.UNKNOWN)) {
1578 // Look for an open network
Irfan Sheriffd8134ff2010-08-22 17:06:34 -07001579 List<ScanResult> scanResults = mWifiStateMachine.syncGetScanResultsList();
Irfan Sheriff0d255342010-07-28 09:35:20 -07001580 if (scanResults != null) {
1581 int numOpenNetworks = 0;
1582 for (int i = scanResults.size() - 1; i >= 0; i--) {
1583 ScanResult scanResult = scanResults.get(i);
1584
1585 if (TextUtils.isEmpty(scanResult.capabilities)) {
1586 numOpenNetworks++;
1587 }
1588 }
1589
1590 if (numOpenNetworks > 0) {
1591 if (++mNumScansSinceNetworkStateChange >= NUM_SCANS_BEFORE_ACTUALLY_SCANNING) {
1592 /*
1593 * We've scanned continuously at least
1594 * NUM_SCANS_BEFORE_NOTIFICATION times. The user
1595 * probably does not have a remembered network in range,
1596 * since otherwise supplicant would have tried to
1597 * associate and thus resetting this counter.
1598 */
1599 setNotificationVisible(true, numOpenNetworks, false, 0);
1600 }
1601 return;
1602 }
1603 }
1604 }
1605
1606 // No open networks in range, remove the notification
1607 setNotificationVisible(false, 0, false, 0);
1608 }
1609
1610 /**
1611 * Clears variables related to tracking whether a notification has been
1612 * shown recently and clears the current notification.
1613 */
1614 private void resetNotification() {
1615 mNotificationRepeatTime = 0;
1616 mNumScansSinceNetworkStateChange = 0;
1617 setNotificationVisible(false, 0, false, 0);
1618 }
1619
1620 /**
1621 * Display or don't display a notification that there are open Wi-Fi networks.
1622 * @param visible {@code true} if notification should be visible, {@code false} otherwise
1623 * @param numNetworks the number networks seen
1624 * @param force {@code true} to force notification to be shown/not-shown,
1625 * even if it is already shown/not-shown.
1626 * @param delay time in milliseconds after which the notification should be made
1627 * visible or invisible.
1628 */
1629 private void setNotificationVisible(boolean visible, int numNetworks, boolean force,
1630 int delay) {
1631
1632 // Since we use auto cancel on the notification, when the
1633 // mNetworksAvailableNotificationShown is true, the notification may
1634 // have actually been canceled. However, when it is false we know
1635 // for sure that it is not being shown (it will not be shown any other
1636 // place than here)
1637
1638 // If it should be hidden and it is already hidden, then noop
1639 if (!visible && !mNotificationShown && !force) {
1640 return;
1641 }
1642
1643 NotificationManager notificationManager = (NotificationManager) mContext
1644 .getSystemService(Context.NOTIFICATION_SERVICE);
1645
1646 Message message;
1647 if (visible) {
1648
1649 // Not enough time has passed to show the notification again
1650 if (System.currentTimeMillis() < mNotificationRepeatTime) {
1651 return;
1652 }
1653
1654 if (mNotification == null) {
Wink Savillec7a98342010-08-13 16:11:42 -07001655 // Cache the Notification object.
Irfan Sheriff0d255342010-07-28 09:35:20 -07001656 mNotification = new Notification();
1657 mNotification.when = 0;
1658 mNotification.icon = ICON_NETWORKS_AVAILABLE;
1659 mNotification.flags = Notification.FLAG_AUTO_CANCEL;
1660 mNotification.contentIntent = PendingIntent.getActivity(mContext, 0,
1661 new Intent(WifiManager.ACTION_PICK_WIFI_NETWORK), 0);
1662 }
1663
1664 CharSequence title = mContext.getResources().getQuantityText(
1665 com.android.internal.R.plurals.wifi_available, numNetworks);
1666 CharSequence details = mContext.getResources().getQuantityText(
1667 com.android.internal.R.plurals.wifi_available_detailed, numNetworks);
1668 mNotification.tickerText = title;
1669 mNotification.setLatestEventInfo(mContext, title, details, mNotification.contentIntent);
1670
1671 mNotificationRepeatTime = System.currentTimeMillis() + NOTIFICATION_REPEAT_DELAY_MS;
1672
1673 notificationManager.notify(ICON_NETWORKS_AVAILABLE, mNotification);
Irfan Sheriff0d255342010-07-28 09:35:20 -07001674 } else {
Irfan Sheriff0d255342010-07-28 09:35:20 -07001675 notificationManager.cancel(ICON_NETWORKS_AVAILABLE);
Irfan Sheriff0d255342010-07-28 09:35:20 -07001676 }
1677
Irfan Sheriff0d255342010-07-28 09:35:20 -07001678 mNotificationShown = visible;
1679 }
1680
1681 private class NotificationEnabledSettingObserver extends ContentObserver {
1682
1683 public NotificationEnabledSettingObserver(Handler handler) {
1684 super(handler);
1685 }
1686
1687 public void register() {
1688 ContentResolver cr = mContext.getContentResolver();
1689 cr.registerContentObserver(Settings.Secure.getUriFor(
1690 Settings.Secure.WIFI_NETWORKS_AVAILABLE_NOTIFICATION_ON), true, this);
1691 mNotificationEnabled = getValue();
1692 }
1693
1694 @Override
1695 public void onChange(boolean selfChange) {
1696 super.onChange(selfChange);
1697
1698 mNotificationEnabled = getValue();
1699 resetNotification();
1700 }
1701
1702 private boolean getValue() {
1703 return Settings.Secure.getInt(mContext.getContentResolver(),
1704 Settings.Secure.WIFI_NETWORKS_AVAILABLE_NOTIFICATION_ON, 1) == 1;
1705 }
1706 }
1707
1708
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001709}