blob: 5f0922ec4f09240e5c5ccc165fc1b1d8ab89f7e9 [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;
Isaac Levya7bc1132011-07-06 13:54:48 -070039import android.net.wifi.WifiWatchdogService;
Irfan Sheriff02fb46a2010-12-08 11:27:37 -080040import android.net.wifi.WpsConfiguration;
Irfan Sheriffe4c56c92011-01-12 16:33:58 -080041import android.net.wifi.WpsResult;
Irfan Sheriff5321aef2010-02-12 12:35:59 -080042import android.net.ConnectivityManager;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080043import android.net.DhcpInfo;
Irfan Sheriff0d255342010-07-28 09:35:20 -070044import android.net.NetworkInfo;
45import android.net.NetworkInfo.State;
Irfan Sheriff227bec42011-02-15 19:30:27 -080046import android.net.NetworkInfo.DetailedState;
47import android.net.TrafficStats;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080048import android.os.Binder;
Irfan Sheriff0d255342010-07-28 09:35:20 -070049import android.os.Handler;
Irfan Sheriff227bec42011-02-15 19:30:27 -080050import android.os.Messenger;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080051import android.os.HandlerThread;
52import android.os.IBinder;
Irfan Sheriff5321aef2010-02-12 12:35:59 -080053import android.os.INetworkManagementService;
Irfan Sheriff0d255342010-07-28 09:35:20 -070054import android.os.Message;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080055import android.os.RemoteException;
Amith Yamasani47873e52009-07-02 12:05:32 -070056import android.os.ServiceManager;
Irfan Sheriff227bec42011-02-15 19:30:27 -080057import android.os.SystemProperties;
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -070058import android.os.WorkSource;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080059import android.provider.Settings;
Irfan Sheriff0d255342010-07-28 09:35:20 -070060import android.text.TextUtils;
Joe Onorato8a9b2202010-02-26 18:56:32 -080061import android.util.Slog;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080062
63import java.util.ArrayList;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080064import java.util.List;
Jaikumar Ganesh084c6652009-12-07 10:58:18 -080065import java.util.Set;
Irfan Sheriff658772f2011-03-08 14:52:31 -080066import java.util.concurrent.atomic.AtomicInteger;
Irfan Sheriffa2a1b912010-06-07 09:03:04 -070067import java.util.concurrent.atomic.AtomicBoolean;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080068import java.io.FileDescriptor;
69import java.io.PrintWriter;
70
The Android Open Source Project10592532009-03-18 17:39:46 -070071import com.android.internal.app.IBatteryStats;
Wink Saville4b7ba092010-10-20 15:37:41 -070072import com.android.internal.util.AsyncChannel;
The Android Open Source Project10592532009-03-18 17:39:46 -070073import com.android.server.am.BatteryStatsService;
Irfan Sheriff9ab518ad2010-03-12 15:48:17 -080074import com.android.internal.R;
The Android Open Source Project10592532009-03-18 17:39:46 -070075
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080076/**
77 * WifiService handles remote WiFi operation requests by implementing
Irfan Sheriffa2a1b912010-06-07 09:03:04 -070078 * the IWifiManager interface.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080079 *
80 * @hide
81 */
Irfan Sheriffa2a1b912010-06-07 09:03:04 -070082//TODO: Clean up multiple locks and implement WifiService
83// as a SM to track soft AP/client/adhoc bring up based
84// on device idle state, airplane mode and boot.
85
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080086public class WifiService extends IWifiManager.Stub {
87 private static final String TAG = "WifiService";
Dianne Hackborn5fd21692011-06-07 14:09:47 -070088 private static final boolean DBG = false;
Irfan Sheriffa2a1b912010-06-07 09:03:04 -070089
Irfan Sheriff0d255342010-07-28 09:35:20 -070090 private final WifiStateMachine mWifiStateMachine;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080091
92 private Context mContext;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080093
94 private AlarmManager mAlarmManager;
95 private PendingIntent mIdleIntent;
96 private static final int IDLE_REQUEST = 0;
97 private boolean mScreenOff;
98 private boolean mDeviceIdle;
99 private int mPluggedType;
100
Irfan Sherifffcc08452011-02-17 16:44:54 -0800101 /* Chipset supports background scan */
102 private final boolean mBackgroundScanSupported;
103
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800104 private final LockList mLocks = new LockList();
Eric Shienbrood5711fad2009-03-27 20:25:31 -0700105 // some wifi lock statistics
Irfan Sheriff5876a422010-08-12 20:26:23 -0700106 private int mFullHighPerfLocksAcquired;
107 private int mFullHighPerfLocksReleased;
Eric Shienbrood5711fad2009-03-27 20:25:31 -0700108 private int mFullLocksAcquired;
109 private int mFullLocksReleased;
110 private int mScanLocksAcquired;
111 private int mScanLocksReleased;
The Android Open Source Project10592532009-03-18 17:39:46 -0700112
Robert Greenwalt58ff0212009-05-19 15:53:54 -0700113 private final List<Multicaster> mMulticasters =
114 new ArrayList<Multicaster>();
Robert Greenwalt5347bd42009-05-13 15:10:16 -0700115 private int mMulticastEnabled;
116 private int mMulticastDisabled;
117
The Android Open Source Project10592532009-03-18 17:39:46 -0700118 private final IBatteryStats mBatteryStats;
Jaikumar Ganesh084c6652009-12-07 10:58:18 -0800119
Irfan Sheriff227bec42011-02-15 19:30:27 -0800120 private boolean mEnableTrafficStatsPoll = false;
121 private int mTrafficStatsPollToken = 0;
122 private long mTxPkts;
123 private long mRxPkts;
124 /* Tracks last reported data activity */
125 private int mDataActivity;
126 private String mInterfaceName;
127
128 /**
129 * Interval in milliseconds between polling for traffic
130 * statistics
131 */
132 private static final int POLL_TRAFFIC_STATS_INTERVAL_MSECS = 1000;
133
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800134 /**
Doug Zongker43866e02010-01-07 12:09:54 -0800135 * See {@link Settings.Secure#WIFI_IDLE_MS}. This is the default value if a
136 * Settings.Secure value is not present. This timeout value is chosen as
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800137 * the approximate point at which the battery drain caused by Wi-Fi
138 * being enabled but not active exceeds the battery drain caused by
139 * re-establishing a connection to the mobile data network.
140 */
Irfan Sheriff4f5f7c92010-10-14 17:01:27 -0700141 private static final long DEFAULT_IDLE_MS = 15 * 60 * 1000; /* 15 minutes */
142
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800143 private static final String ACTION_DEVICE_IDLE =
144 "com.android.server.WifiManager.action.DEVICE_IDLE";
145
Irfan Sheriff658772f2011-03-08 14:52:31 -0800146 private static final int WIFI_DISABLED = 0;
147 private static final int WIFI_ENABLED = 1;
148 /* Wifi enabled while in airplane mode */
149 private static final int WIFI_ENABLED_AIRPLANE_OVERRIDE = 2;
150
151 private AtomicInteger mWifiState = new AtomicInteger(WIFI_DISABLED);
152 private AtomicBoolean mAirplaneModeOn = new AtomicBoolean(false);
153
Irfan Sheriffa2a1b912010-06-07 09:03:04 -0700154 private boolean mIsReceiverRegistered = false;
155
Irfan Sheriff0d255342010-07-28 09:35:20 -0700156
157 NetworkInfo mNetworkInfo = new NetworkInfo(ConnectivityManager.TYPE_WIFI, 0, "WIFI", "");
158
159 // Variables relating to the 'available networks' notification
160 /**
161 * The icon to show in the 'available networks' notification. This will also
162 * be the ID of the Notification given to the NotificationManager.
163 */
164 private static final int ICON_NETWORKS_AVAILABLE =
165 com.android.internal.R.drawable.stat_notify_wifi_in_range;
166 /**
167 * When a notification is shown, we wait this amount before possibly showing it again.
168 */
169 private final long NOTIFICATION_REPEAT_DELAY_MS;
170 /**
171 * Whether the user has set the setting to show the 'available networks' notification.
172 */
173 private boolean mNotificationEnabled;
174 /**
175 * Observes the user setting to keep {@link #mNotificationEnabled} in sync.
176 */
177 private NotificationEnabledSettingObserver mNotificationEnabledSettingObserver;
178 /**
179 * The {@link System#currentTimeMillis()} must be at least this value for us
180 * to show the notification again.
181 */
182 private long mNotificationRepeatTime;
183 /**
184 * The Notification object given to the NotificationManager.
185 */
186 private Notification mNotification;
187 /**
188 * Whether the notification is being shown, as set by us. That is, if the
189 * user cancels the notification, we will not receive the callback so this
190 * will still be true. We only guarantee if this is false, then the
191 * notification is not showing.
192 */
193 private boolean mNotificationShown;
194 /**
195 * The number of continuous scans that must occur before consider the
196 * supplicant in a scanning state. This allows supplicant to associate with
197 * remembered networks that are in the scan results.
198 */
199 private static final int NUM_SCANS_BEFORE_ACTUALLY_SCANNING = 3;
200 /**
201 * The number of scans since the last network state change. When this
202 * exceeds {@link #NUM_SCANS_BEFORE_ACTUALLY_SCANNING}, we consider the
203 * supplicant to actually be scanning. When the network state changes to
204 * something other than scanning, we reset this to 0.
205 */
206 private int mNumScansSinceNetworkStateChange;
Jaikumar Ganesh7440fc22010-09-27 17:04:14 -0700207
Dianne Hackborn03f3cb02010-09-17 23:12:26 -0700208 /**
Wink Saville4b7ba092010-10-20 15:37:41 -0700209 * Asynchronous channel to WifiStateMachine
210 */
Irfan Sheriff227bec42011-02-15 19:30:27 -0800211 private AsyncChannel mWifiStateMachineChannel;
Wink Saville4b7ba092010-10-20 15:37:41 -0700212
213 /**
Irfan Sheriff227bec42011-02-15 19:30:27 -0800214 * Clients receiving asynchronous messages
Wink Saville4b7ba092010-10-20 15:37:41 -0700215 */
Irfan Sheriff227bec42011-02-15 19:30:27 -0800216 private List<AsyncChannel> mClients = new ArrayList<AsyncChannel>();
Wink Saville4b7ba092010-10-20 15:37:41 -0700217
Irfan Sheriff227bec42011-02-15 19:30:27 -0800218 /**
219 * Handles client connections
220 */
221 private class AsyncServiceHandler extends Handler {
222
223 AsyncServiceHandler(android.os.Looper looper) {
Wink Saville4b7ba092010-10-20 15:37:41 -0700224 super(looper);
Wink Saville4b7ba092010-10-20 15:37:41 -0700225 }
226
227 @Override
228 public void handleMessage(Message msg) {
229 switch (msg.what) {
230 case AsyncChannel.CMD_CHANNEL_HALF_CONNECTED: {
231 if (msg.arg1 == AsyncChannel.STATUS_SUCCESSFUL) {
Irfan Sheriff227bec42011-02-15 19:30:27 -0800232 Slog.d(TAG, "New client listening to asynchronous messages");
233 mClients.add((AsyncChannel) msg.obj);
Wink Saville4b7ba092010-10-20 15:37:41 -0700234 } else {
Irfan Sheriff227bec42011-02-15 19:30:27 -0800235 Slog.e(TAG, "Client connection failure, error=" + msg.arg1);
236 }
237 break;
238 }
Irfan Sheriffc23971b2011-03-04 17:06:31 -0800239 case AsyncChannel.CMD_CHANNEL_DISCONNECTED: {
240 if (msg.arg1 == AsyncChannel.STATUS_SEND_UNSUCCESSFUL) {
241 Slog.d(TAG, "Send failed, client connection lost");
242 } else {
243 Slog.d(TAG, "Client connection lost with reason: " + msg.arg1);
244 }
245 mClients.remove((AsyncChannel) msg.obj);
246 break;
247 }
Irfan Sheriff227bec42011-02-15 19:30:27 -0800248 case AsyncChannel.CMD_CHANNEL_FULL_CONNECTION: {
249 AsyncChannel ac = new AsyncChannel();
250 ac.connect(mContext, this, msg.replyTo);
251 break;
252 }
Irfan Sheriffebe606f2011-02-24 11:39:15 -0800253 case WifiManager.CMD_ENABLE_TRAFFIC_STATS_POLL: {
Irfan Sheriff227bec42011-02-15 19:30:27 -0800254 mEnableTrafficStatsPoll = (msg.arg1 == 1);
255 mTrafficStatsPollToken++;
256 if (mEnableTrafficStatsPoll) {
257 notifyOnDataActivity();
Irfan Sheriffebe606f2011-02-24 11:39:15 -0800258 sendMessageDelayed(Message.obtain(this, WifiManager.CMD_TRAFFIC_STATS_POLL,
Irfan Sheriff227bec42011-02-15 19:30:27 -0800259 mTrafficStatsPollToken, 0), POLL_TRAFFIC_STATS_INTERVAL_MSECS);
260 }
261 break;
262 }
Irfan Sheriffebe606f2011-02-24 11:39:15 -0800263 case WifiManager.CMD_TRAFFIC_STATS_POLL: {
Irfan Sheriff227bec42011-02-15 19:30:27 -0800264 if (msg.arg1 == mTrafficStatsPollToken) {
265 notifyOnDataActivity();
Irfan Sheriffebe606f2011-02-24 11:39:15 -0800266 sendMessageDelayed(Message.obtain(this, WifiManager.CMD_TRAFFIC_STATS_POLL,
Irfan Sheriff227bec42011-02-15 19:30:27 -0800267 mTrafficStatsPollToken, 0), POLL_TRAFFIC_STATS_INTERVAL_MSECS);
Wink Saville4b7ba092010-10-20 15:37:41 -0700268 }
269 break;
270 }
Irfan Sheriffebe606f2011-02-24 11:39:15 -0800271 case WifiManager.CMD_CONNECT_NETWORK: {
272 if (msg.obj != null) {
273 mWifiStateMachine.connectNetwork((WifiConfiguration)msg.obj);
274 } else {
275 mWifiStateMachine.connectNetwork(msg.arg1);
276 }
277 break;
278 }
279 case WifiManager.CMD_SAVE_NETWORK: {
280 mWifiStateMachine.saveNetwork((WifiConfiguration)msg.obj);
281 break;
282 }
283 case WifiManager.CMD_FORGET_NETWORK: {
284 mWifiStateMachine.forgetNetwork(msg.arg1);
285 break;
286 }
287 case WifiManager.CMD_START_WPS: {
288 //replyTo has the original source
289 mWifiStateMachine.startWps(msg.replyTo, (WpsConfiguration)msg.obj);
290 break;
291 }
Wink Saville4b7ba092010-10-20 15:37:41 -0700292 default: {
293 Slog.d(TAG, "WifiServicehandler.handleMessage ignoring msg=" + msg);
294 break;
295 }
296 }
297 }
298 }
Irfan Sheriff227bec42011-02-15 19:30:27 -0800299 private AsyncServiceHandler mAsyncServiceHandler;
300
301 /**
302 * Handles interaction with WifiStateMachine
303 */
304 private class WifiStateMachineHandler extends Handler {
305 private AsyncChannel mWsmChannel;
306
307 WifiStateMachineHandler(android.os.Looper looper) {
308 super(looper);
309 mWsmChannel = new AsyncChannel();
310 mWsmChannel.connect(mContext, this, mWifiStateMachine.getHandler());
311 }
312
313 @Override
314 public void handleMessage(Message msg) {
315 switch (msg.what) {
316 case AsyncChannel.CMD_CHANNEL_HALF_CONNECTED: {
317 if (msg.arg1 == AsyncChannel.STATUS_SUCCESSFUL) {
318 mWifiStateMachineChannel = mWsmChannel;
319 } else {
320 Slog.e(TAG, "WifiStateMachine connection failure, error=" + msg.arg1);
321 mWifiStateMachineChannel = null;
322 }
323 break;
324 }
Irfan Sheriff6da83d52011-06-06 12:54:06 -0700325 case AsyncChannel.CMD_CHANNEL_DISCONNECTED: {
326 Slog.e(TAG, "WifiStateMachine channel lost, msg.arg1 =" + msg.arg1);
327 mWifiStateMachineChannel = null;
328 //Re-establish connection to state machine
329 mWsmChannel.connect(mContext, this, mWifiStateMachine.getHandler());
330 break;
331 }
Irfan Sheriff227bec42011-02-15 19:30:27 -0800332 default: {
333 Slog.d(TAG, "WifiStateMachineHandler.handleMessage ignoring msg=" + msg);
334 break;
335 }
336 }
337 }
338 }
339 WifiStateMachineHandler mWifiStateMachineHandler;
Wink Saville4b7ba092010-10-20 15:37:41 -0700340
341 /**
Dianne Hackborn03f3cb02010-09-17 23:12:26 -0700342 * Temporary for computing UIDS that are responsible for starting WIFI.
343 * Protected by mWifiStateTracker lock.
344 */
345 private final WorkSource mTmpWorkSource = new WorkSource();
Isaac Levybc7dfb52011-06-06 15:34:01 -0700346 private WifiWatchdogService mWifiWatchdogService;
Irfan Sheriff0d255342010-07-28 09:35:20 -0700347
348 WifiService(Context context) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800349 mContext = context;
Irfan Sheriff227bec42011-02-15 19:30:27 -0800350
351 mInterfaceName = SystemProperties.get("wifi.interface", "wlan0");
352
353 mWifiStateMachine = new WifiStateMachine(mContext, mInterfaceName);
Irfan Sheriff0d255342010-07-28 09:35:20 -0700354 mWifiStateMachine.enableRssiPolling(true);
The Android Open Source Project10592532009-03-18 17:39:46 -0700355 mBatteryStats = BatteryStatsService.getService();
Jaikumar Ganesh084c6652009-12-07 10:58:18 -0800356
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800357 mAlarmManager = (AlarmManager)mContext.getSystemService(Context.ALARM_SERVICE);
358 Intent idleIntent = new Intent(ACTION_DEVICE_IDLE, null);
359 mIdleIntent = PendingIntent.getBroadcast(mContext, IDLE_REQUEST, idleIntent, 0);
360
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800361 mContext.registerReceiver(
362 new BroadcastReceiver() {
363 @Override
364 public void onReceive(Context context, Intent intent) {
Irfan Sheriff658772f2011-03-08 14:52:31 -0800365 mAirplaneModeOn.set(isAirplaneModeOn());
366 /* On airplane mode disable, restore wifi state if necessary */
367 if (!mAirplaneModeOn.get() && (testAndClearWifiSavedState() ||
368 mWifiState.get() == WIFI_ENABLED_AIRPLANE_OVERRIDE)) {
369 persistWifiEnabled(true);
Irfan Sheriffb2e6c012010-04-05 11:57:56 -0700370 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800371 updateWifiState();
372 }
373 },
374 new IntentFilter(Intent.ACTION_AIRPLANE_MODE_CHANGED));
375
Irfan Sheriff0d255342010-07-28 09:35:20 -0700376 IntentFilter filter = new IntentFilter();
377 filter.addAction(WifiManager.WIFI_STATE_CHANGED_ACTION);
378 filter.addAction(WifiManager.NETWORK_STATE_CHANGED_ACTION);
379 filter.addAction(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION);
380
381 mContext.registerReceiver(
382 new BroadcastReceiver() {
383 @Override
384 public void onReceive(Context context, Intent intent) {
385 if (intent.getAction().equals(WifiManager.WIFI_STATE_CHANGED_ACTION)) {
386 // reset & clear notification on any wifi state change
387 resetNotification();
388 } else if (intent.getAction().equals(
389 WifiManager.NETWORK_STATE_CHANGED_ACTION)) {
390 mNetworkInfo = (NetworkInfo) intent.getParcelableExtra(
391 WifiManager.EXTRA_NETWORK_INFO);
392 // reset & clear notification on a network connect & disconnect
393 switch(mNetworkInfo.getDetailedState()) {
394 case CONNECTED:
395 case DISCONNECTED:
Irfan Sheriff227bec42011-02-15 19:30:27 -0800396 evaluateTrafficStatsPolling();
Irfan Sheriff0d255342010-07-28 09:35:20 -0700397 resetNotification();
398 break;
399 }
400 } else if (intent.getAction().equals(
401 WifiManager.SCAN_RESULTS_AVAILABLE_ACTION)) {
402 checkAndSetNotification();
403 }
404 }
405 }, filter);
406
Irfan Sheriff227bec42011-02-15 19:30:27 -0800407 HandlerThread wifiThread = new HandlerThread("WifiService");
408 wifiThread.start();
409 mAsyncServiceHandler = new AsyncServiceHandler(wifiThread.getLooper());
410 mWifiStateMachineHandler = new WifiStateMachineHandler(wifiThread.getLooper());
411
Irfan Sheriff0d255342010-07-28 09:35:20 -0700412 // Setting is in seconds
413 NOTIFICATION_REPEAT_DELAY_MS = Settings.Secure.getInt(context.getContentResolver(),
414 Settings.Secure.WIFI_NETWORKS_AVAILABLE_REPEAT_DELAY, 900) * 1000l;
415 mNotificationEnabledSettingObserver = new NotificationEnabledSettingObserver(new Handler());
416 mNotificationEnabledSettingObserver.register();
Irfan Sherifffcc08452011-02-17 16:44:54 -0800417
418 mBackgroundScanSupported = mContext.getResources().getBoolean(
419 com.android.internal.R.bool.config_wifi_background_scan_support);
Irfan Sheriff7b009782010-03-11 16:37:45 -0800420 }
Irfan Sheriff5321aef2010-02-12 12:35:59 -0800421
Irfan Sheriff7b009782010-03-11 16:37:45 -0800422 /**
423 * Check if Wi-Fi needs to be enabled and start
424 * if needed
Irfan Sheriff60e3ba02010-04-02 12:18:45 -0700425 *
426 * This function is used only at boot time
Irfan Sheriff7b009782010-03-11 16:37:45 -0800427 */
Irfan Sheriff0d255342010-07-28 09:35:20 -0700428 public void checkAndStartWifi() {
Irfan Sheriff658772f2011-03-08 14:52:31 -0800429 mAirplaneModeOn.set(isAirplaneModeOn());
430 mWifiState.set(getPersistedWifiState());
431 /* Start if Wi-Fi should be enabled or the saved state indicates Wi-Fi was on */
432 boolean wifiEnabled = shouldWifiBeEnabled() || testAndClearWifiSavedState();
Irfan Sheriff7b009782010-03-11 16:37:45 -0800433 Slog.i(TAG, "WifiService starting up with Wi-Fi " +
434 (wifiEnabled ? "enabled" : "disabled"));
Irfan Sheriffb99fe5e2010-03-26 14:56:07 -0700435 setWifiEnabled(wifiEnabled);
Isaac Levybc7dfb52011-06-06 15:34:01 -0700436
437 //TODO: as part of WWS refactor, create only when needed
438 mWifiWatchdogService = new WifiWatchdogService(mContext);
Irfan Sheriff5321aef2010-02-12 12:35:59 -0800439 }
440
Irfan Sheriffa3bd4092010-03-24 17:58:59 -0700441 private boolean testAndClearWifiSavedState() {
442 final ContentResolver cr = mContext.getContentResolver();
443 int wifiSavedState = 0;
444 try {
445 wifiSavedState = Settings.Secure.getInt(cr, Settings.Secure.WIFI_SAVED_STATE);
446 if(wifiSavedState == 1)
447 Settings.Secure.putInt(cr, Settings.Secure.WIFI_SAVED_STATE, 0);
448 } catch (Settings.SettingNotFoundException e) {
449 ;
450 }
451 return (wifiSavedState == 1);
452 }
453
Irfan Sheriff658772f2011-03-08 14:52:31 -0800454 private int getPersistedWifiState() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800455 final ContentResolver cr = mContext.getContentResolver();
456 try {
Irfan Sheriff658772f2011-03-08 14:52:31 -0800457 return Settings.Secure.getInt(cr, Settings.Secure.WIFI_ON);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800458 } catch (Settings.SettingNotFoundException e) {
Irfan Sheriff658772f2011-03-08 14:52:31 -0800459 Settings.Secure.putInt(cr, Settings.Secure.WIFI_ON, WIFI_DISABLED);
460 return WIFI_DISABLED;
461 }
462 }
463
464 private boolean shouldWifiBeEnabled() {
465 if (mAirplaneModeOn.get()) {
466 return mWifiState.get() == WIFI_ENABLED_AIRPLANE_OVERRIDE;
467 } else {
468 return mWifiState.get() != WIFI_DISABLED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800469 }
470 }
471
472 private void persistWifiEnabled(boolean enabled) {
473 final ContentResolver cr = mContext.getContentResolver();
Irfan Sheriff658772f2011-03-08 14:52:31 -0800474 if (enabled) {
475 if (isAirplaneModeOn() && isAirplaneToggleable()) {
476 mWifiState.set(WIFI_ENABLED_AIRPLANE_OVERRIDE);
477 } else {
478 mWifiState.set(WIFI_ENABLED);
479 }
480 } else {
481 mWifiState.set(WIFI_DISABLED);
482 }
483 Settings.Secure.putInt(cr, Settings.Secure.WIFI_ON, mWifiState.get());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800484 }
485
Irfan Sheriff658772f2011-03-08 14:52:31 -0800486
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800487 /**
488 * see {@link android.net.wifi.WifiManager#pingSupplicant()}
Irfan Sheriffa2a1b912010-06-07 09:03:04 -0700489 * @return {@code true} if the operation succeeds, {@code false} otherwise
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800490 */
491 public boolean pingSupplicant() {
Irfan Sheriffa2a1b912010-06-07 09:03:04 -0700492 enforceAccessPermission();
Irfan Sheriff227bec42011-02-15 19:30:27 -0800493 if (mWifiStateMachineChannel != null) {
494 return mWifiStateMachine.syncPingSupplicant(mWifiStateMachineChannel);
Irfan Sheriff1406bcb2010-10-28 14:41:39 -0700495 } else {
Irfan Sheriff227bec42011-02-15 19:30:27 -0800496 Slog.e(TAG, "mWifiStateMachineChannel is not initialized");
Irfan Sheriff1406bcb2010-10-28 14:41:39 -0700497 return false;
498 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800499 }
500
501 /**
502 * see {@link android.net.wifi.WifiManager#startScan()}
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800503 */
Irfan Sheriffe4984752010-08-19 11:29:22 -0700504 public void startScan(boolean forceActive) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800505 enforceChangePermission();
Irfan Sheriffe4984752010-08-19 11:29:22 -0700506 mWifiStateMachine.startScan(forceActive);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800507 }
508
509 private void enforceAccessPermission() {
510 mContext.enforceCallingOrSelfPermission(android.Manifest.permission.ACCESS_WIFI_STATE,
511 "WifiService");
512 }
513
514 private void enforceChangePermission() {
515 mContext.enforceCallingOrSelfPermission(android.Manifest.permission.CHANGE_WIFI_STATE,
516 "WifiService");
517
518 }
519
Robert Greenwaltfc1b15c2009-05-22 15:09:51 -0700520 private void enforceMulticastChangePermission() {
521 mContext.enforceCallingOrSelfPermission(
522 android.Manifest.permission.CHANGE_WIFI_MULTICAST_STATE,
523 "WifiService");
524 }
525
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800526 /**
Irfan Sheriffa2a1b912010-06-07 09:03:04 -0700527 * see {@link android.net.wifi.WifiManager#setWifiEnabled(boolean)}
528 * @param enable {@code true} to enable, {@code false} to disable.
529 * @return {@code true} if the enable/disable operation was
530 * started or is already in the queue.
531 */
532 public synchronized boolean setWifiEnabled(boolean enable) {
533 enforceChangePermission();
534
535 if (DBG) {
Irfan Sheriff0d255342010-07-28 09:35:20 -0700536 Slog.e(TAG, "Invoking mWifiStateMachine.setWifiEnabled\n");
Irfan Sheriffa2a1b912010-06-07 09:03:04 -0700537 }
538
Dianne Hackborn03f3cb02010-09-17 23:12:26 -0700539 if (enable) {
540 reportStartWorkSource();
541 }
Irfan Sheriff0d255342010-07-28 09:35:20 -0700542 mWifiStateMachine.setWifiEnabled(enable);
Irfan Sheriff61180692010-08-18 16:07:39 -0700543
544 /*
545 * Caller might not have WRITE_SECURE_SETTINGS,
546 * only CHANGE_WIFI_STATE is enforced
547 */
548 long ident = Binder.clearCallingIdentity();
Irfan Sheriffa2a1b912010-06-07 09:03:04 -0700549 persistWifiEnabled(enable);
Irfan Sheriff61180692010-08-18 16:07:39 -0700550 Binder.restoreCallingIdentity(ident);
Irfan Sheriffa2a1b912010-06-07 09:03:04 -0700551
552 if (enable) {
553 if (!mIsReceiverRegistered) {
554 registerForBroadcasts();
555 mIsReceiverRegistered = true;
556 }
557 } else if (mIsReceiverRegistered){
558 mContext.unregisterReceiver(mReceiver);
559 mIsReceiverRegistered = false;
560 }
561
562 return true;
563 }
564
565 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800566 * see {@link WifiManager#getWifiState()}
567 * @return One of {@link WifiManager#WIFI_STATE_DISABLED},
568 * {@link WifiManager#WIFI_STATE_DISABLING},
569 * {@link WifiManager#WIFI_STATE_ENABLED},
570 * {@link WifiManager#WIFI_STATE_ENABLING},
571 * {@link WifiManager#WIFI_STATE_UNKNOWN}
572 */
573 public int getWifiEnabledState() {
574 enforceAccessPermission();
Irfan Sheriffd8134ff2010-08-22 17:06:34 -0700575 return mWifiStateMachine.syncGetWifiState();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800576 }
577
578 /**
Irfan Sheriffc2f54c22010-03-18 14:02:22 -0700579 * see {@link android.net.wifi.WifiManager#setWifiApEnabled(WifiConfiguration, boolean)}
Irfan Sheriff5321aef2010-02-12 12:35:59 -0800580 * @param wifiConfig SSID, security and channel details as
581 * part of WifiConfiguration
Irfan Sheriffa2a1b912010-06-07 09:03:04 -0700582 * @param enabled true to enable and false to disable
Irfan Sheriff5321aef2010-02-12 12:35:59 -0800583 */
Irfan Sheriffffcea7a2011-05-10 16:26:06 -0700584 public void setWifiApEnabled(WifiConfiguration wifiConfig, boolean enabled) {
Irfan Sheriff5321aef2010-02-12 12:35:59 -0800585 enforceChangePermission();
Irfan Sheriff0d255342010-07-28 09:35:20 -0700586 mWifiStateMachine.setWifiApEnabled(wifiConfig, enabled);
Irfan Sheriff5321aef2010-02-12 12:35:59 -0800587 }
588
Irfan Sheriffa2a1b912010-06-07 09:03:04 -0700589 /**
590 * see {@link WifiManager#getWifiApState()}
591 * @return One of {@link WifiManager#WIFI_AP_STATE_DISABLED},
592 * {@link WifiManager#WIFI_AP_STATE_DISABLING},
593 * {@link WifiManager#WIFI_AP_STATE_ENABLED},
594 * {@link WifiManager#WIFI_AP_STATE_ENABLING},
595 * {@link WifiManager#WIFI_AP_STATE_FAILED}
596 */
597 public int getWifiApEnabledState() {
Irfan Sheriff17b232b2010-06-24 11:32:26 -0700598 enforceAccessPermission();
Irfan Sheriffd8134ff2010-08-22 17:06:34 -0700599 return mWifiStateMachine.syncGetWifiApState();
Irfan Sheriffa2a1b912010-06-07 09:03:04 -0700600 }
601
602 /**
603 * see {@link WifiManager#getWifiApConfiguration()}
604 * @return soft access point configuration
605 */
Irfan Sheriffffcea7a2011-05-10 16:26:06 -0700606 public WifiConfiguration getWifiApConfiguration() {
607 enforceAccessPermission();
Irfan Sheriff6da83d52011-06-06 12:54:06 -0700608 if (mWifiStateMachineChannel != null) {
609 return mWifiStateMachine.syncGetWifiApConfiguration(mWifiStateMachineChannel);
610 } else {
611 Slog.e(TAG, "mWifiStateMachineChannel is not initialized");
612 return null;
613 }
Irfan Sheriff9ab518ad2010-03-12 15:48:17 -0800614 }
615
Irfan Sheriffa2a1b912010-06-07 09:03:04 -0700616 /**
617 * see {@link WifiManager#setWifiApConfiguration(WifiConfiguration)}
618 * @param wifiConfig WifiConfiguration details for soft access point
619 */
Irfan Sheriffffcea7a2011-05-10 16:26:06 -0700620 public void setWifiApConfiguration(WifiConfiguration wifiConfig) {
Irfan Sheriff17b232b2010-06-24 11:32:26 -0700621 enforceChangePermission();
Irfan Sheriff9ab518ad2010-03-12 15:48:17 -0800622 if (wifiConfig == null)
623 return;
Irfan Sheriffffcea7a2011-05-10 16:26:06 -0700624 mWifiStateMachine.setWifiApConfiguration(wifiConfig);
Irfan Sheriff9ab518ad2010-03-12 15:48:17 -0800625 }
626
Irfan Sheriff5321aef2010-02-12 12:35:59 -0800627 /**
Irfan Sheriffa2a1b912010-06-07 09:03:04 -0700628 * see {@link android.net.wifi.WifiManager#disconnect()}
Irfan Sheriff5321aef2010-02-12 12:35:59 -0800629 */
Irfan Sheriffe4984752010-08-19 11:29:22 -0700630 public void disconnect() {
Irfan Sheriffa2a1b912010-06-07 09:03:04 -0700631 enforceChangePermission();
Irfan Sheriffe4984752010-08-19 11:29:22 -0700632 mWifiStateMachine.disconnectCommand();
Irfan Sheriff5321aef2010-02-12 12:35:59 -0800633 }
634
635 /**
Irfan Sheriffa2a1b912010-06-07 09:03:04 -0700636 * see {@link android.net.wifi.WifiManager#reconnect()}
Irfan Sheriff5321aef2010-02-12 12:35:59 -0800637 */
Irfan Sheriffe4984752010-08-19 11:29:22 -0700638 public void reconnect() {
Irfan Sheriffa2a1b912010-06-07 09:03:04 -0700639 enforceChangePermission();
Irfan Sheriffe4984752010-08-19 11:29:22 -0700640 mWifiStateMachine.reconnectCommand();
Irfan Sheriff5321aef2010-02-12 12:35:59 -0800641 }
642
Irfan Sheriffa2a1b912010-06-07 09:03:04 -0700643 /**
644 * see {@link android.net.wifi.WifiManager#reassociate()}
Irfan Sheriffa2a1b912010-06-07 09:03:04 -0700645 */
Irfan Sheriffe4984752010-08-19 11:29:22 -0700646 public void reassociate() {
Irfan Sheriffa2a1b912010-06-07 09:03:04 -0700647 enforceChangePermission();
Irfan Sheriffe4984752010-08-19 11:29:22 -0700648 mWifiStateMachine.reassociateCommand();
Irfan Sheriff5321aef2010-02-12 12:35:59 -0800649 }
650
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800651 /**
652 * see {@link android.net.wifi.WifiManager#getConfiguredNetworks()}
653 * @return the list of configured networks
654 */
655 public List<WifiConfiguration> getConfiguredNetworks() {
656 enforceAccessPermission();
Irfan Sheriffd8134ff2010-08-22 17:06:34 -0700657 return mWifiStateMachine.syncGetConfiguredNetworks();
Chung-yih Wanga8d15942009-10-09 11:01:49 +0800658 }
659
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800660 /**
661 * see {@link android.net.wifi.WifiManager#addOrUpdateNetwork(WifiConfiguration)}
662 * @return the supplicant-assigned identifier for the new or updated
663 * network if the operation succeeds, or {@code -1} if it fails
664 */
Irfan Sheriff7aac5542009-12-22 21:42:17 -0800665 public int addOrUpdateNetwork(WifiConfiguration config) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800666 enforceChangePermission();
Irfan Sheriff227bec42011-02-15 19:30:27 -0800667 if (mWifiStateMachineChannel != null) {
668 return mWifiStateMachine.syncAddOrUpdateNetwork(mWifiStateMachineChannel, config);
Irfan Sheriff1406bcb2010-10-28 14:41:39 -0700669 } else {
Irfan Sheriff227bec42011-02-15 19:30:27 -0800670 Slog.e(TAG, "mWifiStateMachineChannel is not initialized");
Irfan Sheriff1406bcb2010-10-28 14:41:39 -0700671 return -1;
672 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800673 }
674
Irfan Sheriffa2a1b912010-06-07 09:03:04 -0700675 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800676 * See {@link android.net.wifi.WifiManager#removeNetwork(int)}
677 * @param netId the integer that identifies the network configuration
678 * to the supplicant
679 * @return {@code true} if the operation succeeded
680 */
681 public boolean removeNetwork(int netId) {
682 enforceChangePermission();
Irfan Sheriff227bec42011-02-15 19:30:27 -0800683 if (mWifiStateMachineChannel != null) {
684 return mWifiStateMachine.syncRemoveNetwork(mWifiStateMachineChannel, netId);
Wink Saville4b7ba092010-10-20 15:37:41 -0700685 } else {
Irfan Sheriff227bec42011-02-15 19:30:27 -0800686 Slog.e(TAG, "mWifiStateMachineChannel is not initialized");
Wink Saville4b7ba092010-10-20 15:37:41 -0700687 return false;
688 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800689 }
690
691 /**
692 * See {@link android.net.wifi.WifiManager#enableNetwork(int, boolean)}
693 * @param netId the integer that identifies the network configuration
694 * to the supplicant
695 * @param disableOthers if true, disable all other networks.
696 * @return {@code true} if the operation succeeded
697 */
698 public boolean enableNetwork(int netId, boolean disableOthers) {
699 enforceChangePermission();
Irfan Sheriff227bec42011-02-15 19:30:27 -0800700 if (mWifiStateMachineChannel != null) {
701 return mWifiStateMachine.syncEnableNetwork(mWifiStateMachineChannel, netId,
702 disableOthers);
Irfan Sheriff1406bcb2010-10-28 14:41:39 -0700703 } else {
Irfan Sheriff227bec42011-02-15 19:30:27 -0800704 Slog.e(TAG, "mWifiStateMachineChannel is not initialized");
Irfan Sheriff1406bcb2010-10-28 14:41:39 -0700705 return false;
706 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800707 }
708
709 /**
710 * See {@link android.net.wifi.WifiManager#disableNetwork(int)}
711 * @param netId the integer that identifies the network configuration
712 * to the supplicant
713 * @return {@code true} if the operation succeeded
714 */
715 public boolean disableNetwork(int netId) {
716 enforceChangePermission();
Irfan Sheriff227bec42011-02-15 19:30:27 -0800717 if (mWifiStateMachineChannel != null) {
718 return mWifiStateMachine.syncDisableNetwork(mWifiStateMachineChannel, netId);
Irfan Sheriff1406bcb2010-10-28 14:41:39 -0700719 } else {
Irfan Sheriff227bec42011-02-15 19:30:27 -0800720 Slog.e(TAG, "mWifiStateMachineChannel is not initialized");
Irfan Sheriff1406bcb2010-10-28 14:41:39 -0700721 return false;
722 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800723 }
724
725 /**
726 * See {@link android.net.wifi.WifiManager#getConnectionInfo()}
727 * @return the Wi-Fi information, contained in {@link WifiInfo}.
728 */
729 public WifiInfo getConnectionInfo() {
730 enforceAccessPermission();
731 /*
732 * Make sure we have the latest information, by sending
733 * a status request to the supplicant.
734 */
Irfan Sheriffd8134ff2010-08-22 17:06:34 -0700735 return mWifiStateMachine.syncRequestConnectionInfo();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800736 }
737
738 /**
739 * Return the results of the most recent access point scan, in the form of
740 * a list of {@link ScanResult} objects.
741 * @return the list of results
742 */
743 public List<ScanResult> getScanResults() {
744 enforceAccessPermission();
Irfan Sheriffd8134ff2010-08-22 17:06:34 -0700745 return mWifiStateMachine.syncGetScanResultsList();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800746 }
747
748 /**
749 * Tell the supplicant to persist the current list of configured networks.
750 * @return {@code true} if the operation succeeded
Irfan Sheriffa2a1b912010-06-07 09:03:04 -0700751 *
752 * TODO: deprecate this
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800753 */
754 public boolean saveConfiguration() {
Irfan Sheriffa2a1b912010-06-07 09:03:04 -0700755 boolean result = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800756 enforceChangePermission();
Irfan Sheriff227bec42011-02-15 19:30:27 -0800757 if (mWifiStateMachineChannel != null) {
758 return mWifiStateMachine.syncSaveConfig(mWifiStateMachineChannel);
Irfan Sheriff1406bcb2010-10-28 14:41:39 -0700759 } else {
Irfan Sheriff227bec42011-02-15 19:30:27 -0800760 Slog.e(TAG, "mWifiStateMachineChannel is not initialized");
Irfan Sheriff1406bcb2010-10-28 14:41:39 -0700761 return false;
762 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800763 }
764
765 /**
Irfan Sheriffed4f28b2010-10-29 15:32:10 -0700766 * Set the country code
767 * @param countryCode ISO 3166 country code.
Robert Greenwaltb5010cc2009-05-21 15:11:40 -0700768 * @param persist {@code true} if the setting should be remembered.
Irfan Sheriffed4f28b2010-10-29 15:32:10 -0700769 *
770 * The persist behavior exists so that wifi can fall back to the last
771 * persisted country code on a restart, when the locale information is
772 * not available from telephony.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800773 */
Irfan Sheriffed4f28b2010-10-29 15:32:10 -0700774 public void setCountryCode(String countryCode, boolean persist) {
775 Slog.i(TAG, "WifiService trying to set country code to " + countryCode +
776 " with persist set to " + persist);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800777 enforceChangePermission();
Irfan Sheriffed4f28b2010-10-29 15:32:10 -0700778 mWifiStateMachine.setCountryCode(countryCode, persist);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800779 }
780
781 /**
Irfan Sheriff36f74132010-11-04 16:57:37 -0700782 * Set the operational frequency band
783 * @param band One of
784 * {@link WifiManager#WIFI_FREQUENCY_BAND_AUTO},
785 * {@link WifiManager#WIFI_FREQUENCY_BAND_5GHZ},
786 * {@link WifiManager#WIFI_FREQUENCY_BAND_2GHZ},
787 * @param persist {@code true} if the setting should be remembered.
788 *
789 */
790 public void setFrequencyBand(int band, boolean persist) {
791 enforceChangePermission();
792 if (!isDualBandSupported()) return;
793 Slog.i(TAG, "WifiService trying to set frequency band to " + band +
794 " with persist set to " + persist);
795 mWifiStateMachine.setFrequencyBand(band, persist);
796 }
797
798
799 /**
800 * Get the operational frequency band
801 */
802 public int getFrequencyBand() {
803 enforceAccessPermission();
804 return mWifiStateMachine.getFrequencyBand();
805 }
806
807 public boolean isDualBandSupported() {
808 //TODO: Should move towards adding a driver API that checks at runtime
809 return mContext.getResources().getBoolean(
810 com.android.internal.R.bool.config_wifi_dual_band_support);
811 }
812
813 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800814 * Return the DHCP-assigned addresses from the last successful DHCP request,
815 * if any.
816 * @return the DHCP information
817 */
818 public DhcpInfo getDhcpInfo() {
819 enforceAccessPermission();
Irfan Sheriffd8134ff2010-08-22 17:06:34 -0700820 return mWifiStateMachine.syncGetDhcpInfo();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800821 }
822
Irfan Sheriff0d255342010-07-28 09:35:20 -0700823 /**
824 * see {@link android.net.wifi.WifiManager#startWifi}
825 *
826 */
827 public void startWifi() {
828 enforceChangePermission();
829 /* TODO: may be add permissions for access only to connectivity service
830 * TODO: if a start issued, keep wifi alive until a stop issued irrespective
831 * of WifiLock & device idle status unless wifi enabled status is toggled
832 */
833
834 mWifiStateMachine.setDriverStart(true);
835 mWifiStateMachine.reconnectCommand();
836 }
837
838 /**
839 * see {@link android.net.wifi.WifiManager#stopWifi}
840 *
841 */
842 public void stopWifi() {
843 enforceChangePermission();
844 /* TODO: may be add permissions for access only to connectivity service
845 * TODO: if a stop is issued, wifi is brought up only by startWifi
846 * unless wifi enabled status is toggled
847 */
848 mWifiStateMachine.setDriverStart(false);
849 }
850
851
852 /**
853 * see {@link android.net.wifi.WifiManager#addToBlacklist}
854 *
855 */
856 public void addToBlacklist(String bssid) {
857 enforceChangePermission();
858
859 mWifiStateMachine.addToBlacklist(bssid);
860 }
861
862 /**
863 * see {@link android.net.wifi.WifiManager#clearBlacklist}
864 *
865 */
866 public void clearBlacklist() {
867 enforceChangePermission();
868
869 mWifiStateMachine.clearBlacklist();
870 }
871
Irfan Sheriff227bec42011-02-15 19:30:27 -0800872 /**
873 * Get a reference to handler. This is used by a client to establish
874 * an AsyncChannel communication with WifiService
875 */
876 public Messenger getMessenger() {
Irfan Sheriffebe606f2011-02-24 11:39:15 -0800877 /* Enforce the highest permissions
878 TODO: when we consider exposing the asynchronous API, think about
879 how to provide both access and change permissions seperately
880 */
Irfan Sheriff227bec42011-02-15 19:30:27 -0800881 enforceAccessPermission();
Irfan Sheriffebe606f2011-02-24 11:39:15 -0800882 enforceChangePermission();
Irfan Sheriff227bec42011-02-15 19:30:27 -0800883 return new Messenger(mAsyncServiceHandler);
884 }
885
Irfan Sheriff4aeca7c52011-03-10 16:53:33 -0800886 /**
887 * Get the IP and proxy configuration file
888 */
889 public String getConfigFile() {
890 enforceAccessPermission();
891 return mWifiStateMachine.getConfigFile();
892 }
893
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800894 private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
895 @Override
896 public void onReceive(Context context, Intent intent) {
897 String action = intent.getAction();
898
Doug Zongker43866e02010-01-07 12:09:54 -0800899 long idleMillis =
900 Settings.Secure.getLong(mContext.getContentResolver(),
Irfan Sheriff4f5f7c92010-10-14 17:01:27 -0700901 Settings.Secure.WIFI_IDLE_MS, DEFAULT_IDLE_MS);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800902 int stayAwakeConditions =
Doug Zongker43866e02010-01-07 12:09:54 -0800903 Settings.System.getInt(mContext.getContentResolver(),
904 Settings.System.STAY_ON_WHILE_PLUGGED_IN, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800905 if (action.equals(Intent.ACTION_SCREEN_ON)) {
Joe Onorato431bb222010-10-18 19:13:23 -0400906 if (DBG) {
907 Slog.d(TAG, "ACTION_SCREEN_ON");
908 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800909 mAlarmManager.cancel(mIdleIntent);
910 mDeviceIdle = false;
911 mScreenOff = false;
Dianne Hackborn58e0eef2010-09-16 01:22:10 -0700912 // Once the screen is on, we are not keeping WIFI running
913 // because of any locks so clear that tracking immediately.
914 reportStartWorkSource();
Irfan Sheriff227bec42011-02-15 19:30:27 -0800915 evaluateTrafficStatsPolling();
Irfan Sheriff0d255342010-07-28 09:35:20 -0700916 mWifiStateMachine.enableRssiPolling(true);
Irfan Sherifffcc08452011-02-17 16:44:54 -0800917 if (mBackgroundScanSupported) {
Irfan Sheriff2b7f6382011-03-25 14:29:19 -0700918 mWifiStateMachine.enableBackgroundScanCommand(false);
Irfan Sherifffcc08452011-02-17 16:44:54 -0800919 }
Irfan Sheriff8e86b892010-12-22 11:02:20 -0800920 mWifiStateMachine.enableAllNetworks();
Irfan Sheriff4f5f7c92010-10-14 17:01:27 -0700921 updateWifiState();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800922 } else if (action.equals(Intent.ACTION_SCREEN_OFF)) {
Joe Onorato431bb222010-10-18 19:13:23 -0400923 if (DBG) {
924 Slog.d(TAG, "ACTION_SCREEN_OFF");
925 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800926 mScreenOff = true;
Irfan Sheriff227bec42011-02-15 19:30:27 -0800927 evaluateTrafficStatsPolling();
Irfan Sheriff0d255342010-07-28 09:35:20 -0700928 mWifiStateMachine.enableRssiPolling(false);
Irfan Sherifffcc08452011-02-17 16:44:54 -0800929 if (mBackgroundScanSupported) {
Irfan Sheriff2b7f6382011-03-25 14:29:19 -0700930 mWifiStateMachine.enableBackgroundScanCommand(true);
Irfan Sherifffcc08452011-02-17 16:44:54 -0800931 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800932 /*
933 * Set a timer to put Wi-Fi to sleep, but only if the screen is off
934 * AND the "stay on while plugged in" setting doesn't match the
935 * current power conditions (i.e, not plugged in, plugged in to USB,
936 * or plugged in to AC).
937 */
938 if (!shouldWifiStayAwake(stayAwakeConditions, mPluggedType)) {
Irfan Sheriffd8134ff2010-08-22 17:06:34 -0700939 WifiInfo info = mWifiStateMachine.syncRequestConnectionInfo();
San Mehatfa6c7112009-07-07 09:34:44 -0700940 if (info.getSupplicantState() != SupplicantState.COMPLETED) {
Robert Greenwalt84612ea62009-09-30 09:04:22 -0700941 // we used to go to sleep immediately, but this caused some race conditions
Irfan Sheriffa2a1b912010-06-07 09:03:04 -0700942 // we don't have time to track down for this release. Delay instead,
943 // but not as long as we would if connected (below)
Robert Greenwalt84612ea62009-09-30 09:04:22 -0700944 // TODO - fix the race conditions and switch back to the immediate turn-off
945 long triggerTime = System.currentTimeMillis() + (2*60*1000); // 2 min
Joe Onorato431bb222010-10-18 19:13:23 -0400946 if (DBG) {
947 Slog.d(TAG, "setting ACTION_DEVICE_IDLE timer for 120,000 ms");
948 }
Robert Greenwalt84612ea62009-09-30 09:04:22 -0700949 mAlarmManager.set(AlarmManager.RTC_WAKEUP, triggerTime, mIdleIntent);
950 // // do not keep Wifi awake when screen is off if Wifi is not associated
951 // mDeviceIdle = true;
952 // updateWifiState();
Mike Lockwoodd9c32bc2009-05-18 14:14:15 -0400953 } else {
954 long triggerTime = System.currentTimeMillis() + idleMillis;
Joe Onorato431bb222010-10-18 19:13:23 -0400955 if (DBG) {
956 Slog.d(TAG, "setting ACTION_DEVICE_IDLE timer for " + idleMillis
957 + "ms");
958 }
Mike Lockwoodd9c32bc2009-05-18 14:14:15 -0400959 mAlarmManager.set(AlarmManager.RTC_WAKEUP, triggerTime, mIdleIntent);
960 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800961 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800962 } else if (action.equals(ACTION_DEVICE_IDLE)) {
Joe Onorato431bb222010-10-18 19:13:23 -0400963 if (DBG) {
964 Slog.d(TAG, "got ACTION_DEVICE_IDLE");
965 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800966 mDeviceIdle = true;
Dianne Hackborn58e0eef2010-09-16 01:22:10 -0700967 reportStartWorkSource();
Irfan Sheriff4f5f7c92010-10-14 17:01:27 -0700968 updateWifiState();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800969 } else if (action.equals(Intent.ACTION_BATTERY_CHANGED)) {
970 /*
971 * Set a timer to put Wi-Fi to sleep, but only if the screen is off
972 * AND we are transitioning from a state in which the device was supposed
973 * to stay awake to a state in which it is not supposed to stay awake.
974 * If "stay awake" state is not changing, we do nothing, to avoid resetting
975 * the already-set timer.
976 */
977 int pluggedType = intent.getIntExtra("plugged", 0);
Joe Onorato431bb222010-10-18 19:13:23 -0400978 if (DBG) {
979 Slog.d(TAG, "ACTION_BATTERY_CHANGED pluggedType: " + pluggedType);
980 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800981 if (mScreenOff && shouldWifiStayAwake(stayAwakeConditions, mPluggedType) &&
982 !shouldWifiStayAwake(stayAwakeConditions, pluggedType)) {
983 long triggerTime = System.currentTimeMillis() + idleMillis;
Joe Onorato431bb222010-10-18 19:13:23 -0400984 if (DBG) {
985 Slog.d(TAG, "setting ACTION_DEVICE_IDLE timer for " + idleMillis + "ms");
986 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800987 mAlarmManager.set(AlarmManager.RTC_WAKEUP, triggerTime, mIdleIntent);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800988 }
989 mPluggedType = pluggedType;
Irfan Sheriff65eaec82011-01-05 22:00:16 -0800990 } else if (action.equals(BluetoothAdapter.ACTION_CONNECTION_STATE_CHANGED)) {
991 int state = intent.getIntExtra(BluetoothAdapter.EXTRA_CONNECTION_STATE,
992 BluetoothAdapter.STATE_DISCONNECTED);
993 mWifiStateMachine.sendBluetoothAdapterStateChange(state);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800994 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800995 }
996
997 /**
998 * Determines whether the Wi-Fi chipset should stay awake or be put to
999 * sleep. Looks at the setting for the sleep policy and the current
1000 * conditions.
Jaikumar Ganesh084c6652009-12-07 10:58:18 -08001001 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001002 * @see #shouldDeviceStayAwake(int, int)
1003 */
1004 private boolean shouldWifiStayAwake(int stayAwakeConditions, int pluggedType) {
Irfan Sheriff739f6bc2011-01-28 16:43:12 -08001005 //Never sleep as long as the user has not changed the settings
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001006 int wifiSleepPolicy = Settings.System.getInt(mContext.getContentResolver(),
Irfan Sheriff96b10d62011-01-11 15:40:35 -08001007 Settings.System.WIFI_SLEEP_POLICY,
Irfan Sheriff739f6bc2011-01-28 16:43:12 -08001008 Settings.System.WIFI_SLEEP_POLICY_NEVER);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001009
1010 if (wifiSleepPolicy == Settings.System.WIFI_SLEEP_POLICY_NEVER) {
1011 // Never sleep
1012 return true;
1013 } else if ((wifiSleepPolicy == Settings.System.WIFI_SLEEP_POLICY_NEVER_WHILE_PLUGGED) &&
1014 (pluggedType != 0)) {
1015 // Never sleep while plugged, and we're plugged
1016 return true;
1017 } else {
1018 // Default
1019 return shouldDeviceStayAwake(stayAwakeConditions, pluggedType);
1020 }
1021 }
Jaikumar Ganesh084c6652009-12-07 10:58:18 -08001022
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001023 /**
1024 * Determine whether the bit value corresponding to {@code pluggedType} is set in
1025 * the bit string {@code stayAwakeConditions}. Because a {@code pluggedType} value
1026 * of {@code 0} isn't really a plugged type, but rather an indication that the
1027 * device isn't plugged in at all, there is no bit value corresponding to a
1028 * {@code pluggedType} value of {@code 0}. That is why we shift by
Ben Dodson4e8620f2010-08-25 10:55:47 -07001029 * {@code pluggedType - 1} instead of by {@code pluggedType}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001030 * @param stayAwakeConditions a bit string specifying which "plugged types" should
1031 * keep the device (and hence Wi-Fi) awake.
1032 * @param pluggedType the type of plug (USB, AC, or none) for which the check is
1033 * being made
1034 * @return {@code true} if {@code pluggedType} indicates that the device is
1035 * supposed to stay awake, {@code false} otherwise.
1036 */
1037 private boolean shouldDeviceStayAwake(int stayAwakeConditions, int pluggedType) {
1038 return (stayAwakeConditions & pluggedType) != 0;
1039 }
1040 };
1041
Dianne Hackborn03f3cb02010-09-17 23:12:26 -07001042 private synchronized void reportStartWorkSource() {
1043 mTmpWorkSource.clear();
1044 if (mDeviceIdle) {
1045 for (int i=0; i<mLocks.mList.size(); i++) {
1046 mTmpWorkSource.add(mLocks.mList.get(i).mWorkSource);
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07001047 }
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07001048 }
Dianne Hackborn03f3cb02010-09-17 23:12:26 -07001049 mWifiStateMachine.updateBatteryWorkSource(mTmpWorkSource);
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07001050 }
Jaikumar Ganesh7440fc22010-09-27 17:04:14 -07001051
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001052 private void updateWifiState() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001053 boolean lockHeld = mLocks.hasLocks();
Irfan Sheriff5876a422010-08-12 20:26:23 -07001054 int strongestLockMode = WifiManager.WIFI_MODE_FULL;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001055 boolean wifiShouldBeStarted = !mDeviceIdle || lockHeld;
Irfan Sheriff5876a422010-08-12 20:26:23 -07001056
1057 if (lockHeld) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001058 strongestLockMode = mLocks.getStrongestLockMode();
Irfan Sheriff5876a422010-08-12 20:26:23 -07001059 }
1060 /* If device is not idle, lockmode cannot be scan only */
1061 if (!mDeviceIdle && strongestLockMode == WifiManager.WIFI_MODE_SCAN_ONLY) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001062 strongestLockMode = WifiManager.WIFI_MODE_FULL;
1063 }
1064
Irfan Sheriffa2a1b912010-06-07 09:03:04 -07001065 /* Disable tethering when airplane mode is enabled */
Irfan Sheriff658772f2011-03-08 14:52:31 -08001066 if (mAirplaneModeOn.get()) {
Irfan Sheriff0d255342010-07-28 09:35:20 -07001067 mWifiStateMachine.setWifiApEnabled(null, false);
Irfan Sheriffa2a1b912010-06-07 09:03:04 -07001068 }
Irfan Sheriffb2e6c012010-04-05 11:57:56 -07001069
Irfan Sheriff658772f2011-03-08 14:52:31 -08001070 if (shouldWifiBeEnabled()) {
Irfan Sheriffa2a1b912010-06-07 09:03:04 -07001071 if (wifiShouldBeStarted) {
Dianne Hackborn03f3cb02010-09-17 23:12:26 -07001072 reportStartWorkSource();
Irfan Sheriff0d255342010-07-28 09:35:20 -07001073 mWifiStateMachine.setWifiEnabled(true);
1074 mWifiStateMachine.setScanOnlyMode(
Irfan Sheriffa2a1b912010-06-07 09:03:04 -07001075 strongestLockMode == WifiManager.WIFI_MODE_SCAN_ONLY);
Irfan Sheriff0d255342010-07-28 09:35:20 -07001076 mWifiStateMachine.setDriverStart(true);
Irfan Sheriff5876a422010-08-12 20:26:23 -07001077 mWifiStateMachine.setHighPerfModeEnabled(strongestLockMode
1078 == WifiManager.WIFI_MODE_FULL_HIGH_PERF);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001079 } else {
Irfan Sheriff0d255342010-07-28 09:35:20 -07001080 mWifiStateMachine.requestCmWakeLock();
1081 mWifiStateMachine.setDriverStart(false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001082 }
Irfan Sheriffa2a1b912010-06-07 09:03:04 -07001083 } else {
Irfan Sheriff0d255342010-07-28 09:35:20 -07001084 mWifiStateMachine.setWifiEnabled(false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001085 }
1086 }
1087
1088 private void registerForBroadcasts() {
1089 IntentFilter intentFilter = new IntentFilter();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001090 intentFilter.addAction(Intent.ACTION_SCREEN_ON);
1091 intentFilter.addAction(Intent.ACTION_SCREEN_OFF);
1092 intentFilter.addAction(Intent.ACTION_BATTERY_CHANGED);
1093 intentFilter.addAction(ACTION_DEVICE_IDLE);
Irfan Sheriff65eaec82011-01-05 22:00:16 -08001094 intentFilter.addAction(BluetoothAdapter.ACTION_CONNECTION_STATE_CHANGED);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001095 mContext.registerReceiver(mReceiver, intentFilter);
1096 }
Jaikumar Ganesh084c6652009-12-07 10:58:18 -08001097
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001098 private boolean isAirplaneSensitive() {
1099 String airplaneModeRadios = Settings.System.getString(mContext.getContentResolver(),
1100 Settings.System.AIRPLANE_MODE_RADIOS);
1101 return airplaneModeRadios == null
1102 || airplaneModeRadios.contains(Settings.System.RADIO_WIFI);
1103 }
1104
Mike Lockwoodbd5ddf02009-07-29 21:37:14 -07001105 private boolean isAirplaneToggleable() {
1106 String toggleableRadios = Settings.System.getString(mContext.getContentResolver(),
1107 Settings.System.AIRPLANE_MODE_TOGGLEABLE_RADIOS);
1108 return toggleableRadios != null
1109 && toggleableRadios.contains(Settings.System.RADIO_WIFI);
1110 }
1111
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001112 /**
1113 * Returns true if Wi-Fi is sensitive to airplane mode, and airplane mode is
1114 * currently on.
1115 * @return {@code true} if airplane mode is on.
1116 */
1117 private boolean isAirplaneModeOn() {
1118 return isAirplaneSensitive() && Settings.System.getInt(mContext.getContentResolver(),
1119 Settings.System.AIRPLANE_MODE_ON, 0) == 1;
1120 }
1121
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001122 @Override
1123 protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
1124 if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.DUMP)
1125 != PackageManager.PERMISSION_GRANTED) {
1126 pw.println("Permission Denial: can't dump WifiService from from pid="
1127 + Binder.getCallingPid()
1128 + ", uid=" + Binder.getCallingUid());
1129 return;
1130 }
Irfan Sheriffd8134ff2010-08-22 17:06:34 -07001131 pw.println("Wi-Fi is " + mWifiStateMachine.syncGetWifiStateByName());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001132 pw.println("Stay-awake conditions: " +
1133 Settings.System.getInt(mContext.getContentResolver(),
1134 Settings.System.STAY_ON_WHILE_PLUGGED_IN, 0));
1135 pw.println();
1136
1137 pw.println("Internal state:");
Irfan Sheriff0d255342010-07-28 09:35:20 -07001138 pw.println(mWifiStateMachine);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001139 pw.println();
1140 pw.println("Latest scan results:");
Irfan Sheriffd8134ff2010-08-22 17:06:34 -07001141 List<ScanResult> scanResults = mWifiStateMachine.syncGetScanResultsList();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001142 if (scanResults != null && scanResults.size() != 0) {
1143 pw.println(" BSSID Frequency RSSI Flags SSID");
1144 for (ScanResult r : scanResults) {
1145 pw.printf(" %17s %9d %5d %-16s %s%n",
1146 r.BSSID,
1147 r.frequency,
1148 r.level,
1149 r.capabilities,
1150 r.SSID == null ? "" : r.SSID);
1151 }
1152 }
1153 pw.println();
Eric Shienbrood5711fad2009-03-27 20:25:31 -07001154 pw.println("Locks acquired: " + mFullLocksAcquired + " full, " +
Irfan Sheriff5876a422010-08-12 20:26:23 -07001155 mFullHighPerfLocksAcquired + " full high perf, " +
Eric Shienbrood5711fad2009-03-27 20:25:31 -07001156 mScanLocksAcquired + " scan");
1157 pw.println("Locks released: " + mFullLocksReleased + " full, " +
Irfan Sheriff5876a422010-08-12 20:26:23 -07001158 mFullHighPerfLocksReleased + " full high perf, " +
Eric Shienbrood5711fad2009-03-27 20:25:31 -07001159 mScanLocksReleased + " scan");
1160 pw.println();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001161 pw.println("Locks held:");
1162 mLocks.dump(pw);
Isaac Levybc7dfb52011-06-06 15:34:01 -07001163
1164 pw.println();
1165 pw.println("WifiWatchdogService dump");
1166 mWifiWatchdogService.dump(pw);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001167 }
1168
Robert Greenwalt58ff0212009-05-19 15:53:54 -07001169 private class WifiLock extends DeathRecipient {
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -07001170 WifiLock(int lockMode, String tag, IBinder binder, WorkSource ws) {
1171 super(lockMode, tag, binder, ws);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001172 }
1173
1174 public void binderDied() {
1175 synchronized (mLocks) {
1176 releaseWifiLockLocked(mBinder);
1177 }
1178 }
1179
1180 public String toString() {
Robert Greenwalt5347bd42009-05-13 15:10:16 -07001181 return "WifiLock{" + mTag + " type=" + mMode + " binder=" + mBinder + "}";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001182 }
1183 }
1184
1185 private class LockList {
1186 private List<WifiLock> mList;
1187
1188 private LockList() {
1189 mList = new ArrayList<WifiLock>();
1190 }
1191
1192 private synchronized boolean hasLocks() {
1193 return !mList.isEmpty();
1194 }
1195
1196 private synchronized int getStrongestLockMode() {
1197 if (mList.isEmpty()) {
1198 return WifiManager.WIFI_MODE_FULL;
1199 }
Irfan Sheriff5876a422010-08-12 20:26:23 -07001200
1201 if (mFullHighPerfLocksAcquired > mFullHighPerfLocksReleased) {
1202 return WifiManager.WIFI_MODE_FULL_HIGH_PERF;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001203 }
Irfan Sheriff5876a422010-08-12 20:26:23 -07001204
1205 if (mFullLocksAcquired > mFullLocksReleased) {
1206 return WifiManager.WIFI_MODE_FULL;
1207 }
1208
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001209 return WifiManager.WIFI_MODE_SCAN_ONLY;
1210 }
1211
1212 private void addLock(WifiLock lock) {
1213 if (findLockByBinder(lock.mBinder) < 0) {
1214 mList.add(lock);
1215 }
1216 }
1217
1218 private WifiLock removeLock(IBinder binder) {
1219 int index = findLockByBinder(binder);
1220 if (index >= 0) {
Suchi Amalapurapufff2fda2009-06-30 21:36:16 -07001221 WifiLock ret = mList.remove(index);
1222 ret.unlinkDeathRecipient();
1223 return ret;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001224 } else {
1225 return null;
1226 }
1227 }
1228
1229 private int findLockByBinder(IBinder binder) {
1230 int size = mList.size();
1231 for (int i = size - 1; i >= 0; i--)
1232 if (mList.get(i).mBinder == binder)
1233 return i;
1234 return -1;
1235 }
1236
1237 private void dump(PrintWriter pw) {
1238 for (WifiLock l : mList) {
1239 pw.print(" ");
1240 pw.println(l);
1241 }
1242 }
1243 }
1244
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -07001245 void enforceWakeSourcePermission(int uid, int pid) {
Dianne Hackborne746f032010-09-13 16:02:57 -07001246 if (uid == android.os.Process.myUid()) {
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -07001247 return;
1248 }
1249 mContext.enforcePermission(android.Manifest.permission.UPDATE_DEVICE_STATS,
1250 pid, uid, null);
1251 }
1252
1253 public boolean acquireWifiLock(IBinder binder, int lockMode, String tag, WorkSource ws) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001254 mContext.enforceCallingOrSelfPermission(android.Manifest.permission.WAKE_LOCK, null);
Irfan Sheriff5876a422010-08-12 20:26:23 -07001255 if (lockMode != WifiManager.WIFI_MODE_FULL &&
1256 lockMode != WifiManager.WIFI_MODE_SCAN_ONLY &&
1257 lockMode != WifiManager.WIFI_MODE_FULL_HIGH_PERF) {
1258 Slog.e(TAG, "Illegal argument, lockMode= " + lockMode);
1259 if (DBG) throw new IllegalArgumentException("lockMode=" + lockMode);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001260 return false;
1261 }
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -07001262 if (ws != null && ws.size() == 0) {
1263 ws = null;
1264 }
Dianne Hackbornecfd7f72010-10-08 14:23:40 -07001265 if (ws != null) {
1266 enforceWakeSourcePermission(Binder.getCallingUid(), Binder.getCallingPid());
1267 }
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -07001268 if (ws == null) {
1269 ws = new WorkSource(Binder.getCallingUid());
1270 }
1271 WifiLock wifiLock = new WifiLock(lockMode, tag, binder, ws);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001272 synchronized (mLocks) {
1273 return acquireWifiLockLocked(wifiLock);
1274 }
1275 }
1276
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -07001277 private void noteAcquireWifiLock(WifiLock wifiLock) throws RemoteException {
1278 switch(wifiLock.mMode) {
1279 case WifiManager.WIFI_MODE_FULL:
Irfan Sheriff5876a422010-08-12 20:26:23 -07001280 case WifiManager.WIFI_MODE_FULL_HIGH_PERF:
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -07001281 mBatteryStats.noteFullWifiLockAcquiredFromSource(wifiLock.mWorkSource);
1282 break;
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -07001283 case WifiManager.WIFI_MODE_SCAN_ONLY:
1284 mBatteryStats.noteScanWifiLockAcquiredFromSource(wifiLock.mWorkSource);
1285 break;
1286 }
1287 }
1288
1289 private void noteReleaseWifiLock(WifiLock wifiLock) throws RemoteException {
1290 switch(wifiLock.mMode) {
1291 case WifiManager.WIFI_MODE_FULL:
Irfan Sheriff5876a422010-08-12 20:26:23 -07001292 case WifiManager.WIFI_MODE_FULL_HIGH_PERF:
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -07001293 mBatteryStats.noteFullWifiLockReleasedFromSource(wifiLock.mWorkSource);
1294 break;
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -07001295 case WifiManager.WIFI_MODE_SCAN_ONLY:
1296 mBatteryStats.noteScanWifiLockReleasedFromSource(wifiLock.mWorkSource);
1297 break;
1298 }
1299 }
1300
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001301 private boolean acquireWifiLockLocked(WifiLock wifiLock) {
Irfan Sheriffc89dd542010-09-28 08:40:54 -07001302 if (DBG) Slog.d(TAG, "acquireWifiLockLocked: " + wifiLock);
Robert Greenwaltf1acb2d2009-10-13 08:20:55 -07001303
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001304 mLocks.addLock(wifiLock);
Robert Greenwaltf1acb2d2009-10-13 08:20:55 -07001305
The Android Open Source Project10592532009-03-18 17:39:46 -07001306 long ident = Binder.clearCallingIdentity();
1307 try {
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -07001308 noteAcquireWifiLock(wifiLock);
Robert Greenwalt5347bd42009-05-13 15:10:16 -07001309 switch(wifiLock.mMode) {
Eric Shienbrood5711fad2009-03-27 20:25:31 -07001310 case WifiManager.WIFI_MODE_FULL:
1311 ++mFullLocksAcquired;
Eric Shienbrood5711fad2009-03-27 20:25:31 -07001312 break;
Irfan Sheriff5876a422010-08-12 20:26:23 -07001313 case WifiManager.WIFI_MODE_FULL_HIGH_PERF:
1314 ++mFullHighPerfLocksAcquired;
1315 break;
1316
Eric Shienbrood5711fad2009-03-27 20:25:31 -07001317 case WifiManager.WIFI_MODE_SCAN_ONLY:
1318 ++mScanLocksAcquired;
Eric Shienbrood5711fad2009-03-27 20:25:31 -07001319 break;
The Android Open Source Project10592532009-03-18 17:39:46 -07001320 }
Dianne Hackbornecfd7f72010-10-08 14:23:40 -07001321
1322 // Be aggressive about adding new locks into the accounted state...
1323 // we want to over-report rather than under-report.
1324 reportStartWorkSource();
1325
1326 updateWifiState();
1327 return true;
The Android Open Source Project10592532009-03-18 17:39:46 -07001328 } catch (RemoteException e) {
Dianne Hackbornecfd7f72010-10-08 14:23:40 -07001329 return false;
The Android Open Source Project10592532009-03-18 17:39:46 -07001330 } finally {
1331 Binder.restoreCallingIdentity(ident);
1332 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001333 }
1334
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -07001335 public void updateWifiLockWorkSource(IBinder lock, WorkSource ws) {
1336 int uid = Binder.getCallingUid();
1337 int pid = Binder.getCallingPid();
1338 if (ws != null && ws.size() == 0) {
1339 ws = null;
1340 }
1341 if (ws != null) {
1342 enforceWakeSourcePermission(uid, pid);
1343 }
1344 long ident = Binder.clearCallingIdentity();
1345 try {
1346 synchronized (mLocks) {
1347 int index = mLocks.findLockByBinder(lock);
1348 if (index < 0) {
1349 throw new IllegalArgumentException("Wifi lock not active");
1350 }
1351 WifiLock wl = mLocks.mList.get(index);
1352 noteReleaseWifiLock(wl);
1353 wl.mWorkSource = ws != null ? new WorkSource(ws) : new WorkSource(uid);
1354 noteAcquireWifiLock(wl);
1355 }
1356 } catch (RemoteException e) {
1357 } finally {
1358 Binder.restoreCallingIdentity(ident);
1359 }
1360 }
1361
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001362 public boolean releaseWifiLock(IBinder lock) {
1363 mContext.enforceCallingOrSelfPermission(android.Manifest.permission.WAKE_LOCK, null);
1364 synchronized (mLocks) {
1365 return releaseWifiLockLocked(lock);
1366 }
1367 }
1368
1369 private boolean releaseWifiLockLocked(IBinder lock) {
Eric Shienbroodd4c5f892009-03-24 18:13:20 -07001370 boolean hadLock;
Robert Greenwaltf1acb2d2009-10-13 08:20:55 -07001371
The Android Open Source Project10592532009-03-18 17:39:46 -07001372 WifiLock wifiLock = mLocks.removeLock(lock);
Robert Greenwaltf1acb2d2009-10-13 08:20:55 -07001373
Irfan Sheriffc89dd542010-09-28 08:40:54 -07001374 if (DBG) Slog.d(TAG, "releaseWifiLockLocked: " + wifiLock);
Robert Greenwaltf1acb2d2009-10-13 08:20:55 -07001375
Eric Shienbroodd4c5f892009-03-24 18:13:20 -07001376 hadLock = (wifiLock != null);
1377
Dianne Hackbornecfd7f72010-10-08 14:23:40 -07001378 long ident = Binder.clearCallingIdentity();
1379 try {
1380 if (hadLock) {
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -07001381 noteAcquireWifiLock(wifiLock);
Robert Greenwalt5347bd42009-05-13 15:10:16 -07001382 switch(wifiLock.mMode) {
Eric Shienbrood5711fad2009-03-27 20:25:31 -07001383 case WifiManager.WIFI_MODE_FULL:
1384 ++mFullLocksReleased;
Eric Shienbrood5711fad2009-03-27 20:25:31 -07001385 break;
Irfan Sheriff5876a422010-08-12 20:26:23 -07001386 case WifiManager.WIFI_MODE_FULL_HIGH_PERF:
1387 ++mFullHighPerfLocksReleased;
1388 break;
Eric Shienbrood5711fad2009-03-27 20:25:31 -07001389 case WifiManager.WIFI_MODE_SCAN_ONLY:
1390 ++mScanLocksReleased;
Eric Shienbrood5711fad2009-03-27 20:25:31 -07001391 break;
Eric Shienbroodd4c5f892009-03-24 18:13:20 -07001392 }
The Android Open Source Project10592532009-03-18 17:39:46 -07001393 }
Dianne Hackbornecfd7f72010-10-08 14:23:40 -07001394
1395 // TODO - should this only happen if you hadLock?
1396 updateWifiState();
1397
1398 } catch (RemoteException e) {
1399 } finally {
1400 Binder.restoreCallingIdentity(ident);
The Android Open Source Project10592532009-03-18 17:39:46 -07001401 }
Dianne Hackbornecfd7f72010-10-08 14:23:40 -07001402
Eric Shienbroodd4c5f892009-03-24 18:13:20 -07001403 return hadLock;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001404 }
Robert Greenwalt5347bd42009-05-13 15:10:16 -07001405
Robert Greenwalt58ff0212009-05-19 15:53:54 -07001406 private abstract class DeathRecipient
Robert Greenwalt5347bd42009-05-13 15:10:16 -07001407 implements IBinder.DeathRecipient {
1408 String mTag;
1409 int mMode;
1410 IBinder mBinder;
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -07001411 WorkSource mWorkSource;
Robert Greenwalt5347bd42009-05-13 15:10:16 -07001412
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -07001413 DeathRecipient(int mode, String tag, IBinder binder, WorkSource ws) {
Robert Greenwalt5347bd42009-05-13 15:10:16 -07001414 super();
1415 mTag = tag;
1416 mMode = mode;
1417 mBinder = binder;
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -07001418 mWorkSource = ws;
Robert Greenwalt5347bd42009-05-13 15:10:16 -07001419 try {
1420 mBinder.linkToDeath(this, 0);
1421 } catch (RemoteException e) {
1422 binderDied();
1423 }
1424 }
Suchi Amalapurapufff2fda2009-06-30 21:36:16 -07001425
1426 void unlinkDeathRecipient() {
1427 mBinder.unlinkToDeath(this, 0);
1428 }
Robert Greenwalt5347bd42009-05-13 15:10:16 -07001429 }
1430
Robert Greenwalt58ff0212009-05-19 15:53:54 -07001431 private class Multicaster extends DeathRecipient {
1432 Multicaster(String tag, IBinder binder) {
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -07001433 super(Binder.getCallingUid(), tag, binder, null);
Robert Greenwalt5347bd42009-05-13 15:10:16 -07001434 }
1435
1436 public void binderDied() {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001437 Slog.e(TAG, "Multicaster binderDied");
Robert Greenwalt5347bd42009-05-13 15:10:16 -07001438 synchronized (mMulticasters) {
1439 int i = mMulticasters.indexOf(this);
1440 if (i != -1) {
1441 removeMulticasterLocked(i, mMode);
1442 }
1443 }
1444 }
1445
1446 public String toString() {
Robert Greenwalt58ff0212009-05-19 15:53:54 -07001447 return "Multicaster{" + mTag + " binder=" + mBinder + "}";
Robert Greenwalt5347bd42009-05-13 15:10:16 -07001448 }
1449
1450 public int getUid() {
1451 return mMode;
1452 }
1453 }
1454
Robert Greenwalte2d155a2009-10-21 14:58:34 -07001455 public void initializeMulticastFiltering() {
1456 enforceMulticastChangePermission();
Irfan Sheriffa8fbe1f2010-03-09 09:13:58 -08001457
Robert Greenwalte2d155a2009-10-21 14:58:34 -07001458 synchronized (mMulticasters) {
1459 // if anybody had requested filters be off, leave off
1460 if (mMulticasters.size() != 0) {
1461 return;
1462 } else {
Irfan Sheriff0d255342010-07-28 09:35:20 -07001463 mWifiStateMachine.startPacketFiltering();
Robert Greenwalte2d155a2009-10-21 14:58:34 -07001464 }
1465 }
1466 }
1467
Robert Greenwaltfc1b15c2009-05-22 15:09:51 -07001468 public void acquireMulticastLock(IBinder binder, String tag) {
1469 enforceMulticastChangePermission();
Robert Greenwalt5347bd42009-05-13 15:10:16 -07001470
1471 synchronized (mMulticasters) {
1472 mMulticastEnabled++;
Robert Greenwalt58ff0212009-05-19 15:53:54 -07001473 mMulticasters.add(new Multicaster(tag, binder));
Robert Greenwalt5347bd42009-05-13 15:10:16 -07001474 // Note that we could call stopPacketFiltering only when
1475 // our new size == 1 (first call), but this function won't
1476 // be called often and by making the stopPacket call each
1477 // time we're less fragile and self-healing.
Irfan Sheriff0d255342010-07-28 09:35:20 -07001478 mWifiStateMachine.stopPacketFiltering();
Robert Greenwalt5347bd42009-05-13 15:10:16 -07001479 }
1480
1481 int uid = Binder.getCallingUid();
1482 Long ident = Binder.clearCallingIdentity();
1483 try {
1484 mBatteryStats.noteWifiMulticastEnabled(uid);
1485 } catch (RemoteException e) {
1486 } finally {
1487 Binder.restoreCallingIdentity(ident);
1488 }
1489 }
1490
Robert Greenwaltfc1b15c2009-05-22 15:09:51 -07001491 public void releaseMulticastLock() {
1492 enforceMulticastChangePermission();
Robert Greenwalt5347bd42009-05-13 15:10:16 -07001493
1494 int uid = Binder.getCallingUid();
1495 synchronized (mMulticasters) {
1496 mMulticastDisabled++;
1497 int size = mMulticasters.size();
1498 for (int i = size - 1; i >= 0; i--) {
Robert Greenwalt58ff0212009-05-19 15:53:54 -07001499 Multicaster m = mMulticasters.get(i);
Robert Greenwalt5347bd42009-05-13 15:10:16 -07001500 if ((m != null) && (m.getUid() == uid)) {
1501 removeMulticasterLocked(i, uid);
1502 }
1503 }
1504 }
1505 }
1506
1507 private void removeMulticasterLocked(int i, int uid)
1508 {
Suchi Amalapurapufff2fda2009-06-30 21:36:16 -07001509 Multicaster removed = mMulticasters.remove(i);
Irfan Sheriffa8fbe1f2010-03-09 09:13:58 -08001510
Suchi Amalapurapufff2fda2009-06-30 21:36:16 -07001511 if (removed != null) {
1512 removed.unlinkDeathRecipient();
1513 }
Robert Greenwalt5347bd42009-05-13 15:10:16 -07001514 if (mMulticasters.size() == 0) {
Irfan Sheriff0d255342010-07-28 09:35:20 -07001515 mWifiStateMachine.startPacketFiltering();
Robert Greenwalt5347bd42009-05-13 15:10:16 -07001516 }
1517
1518 Long ident = Binder.clearCallingIdentity();
1519 try {
1520 mBatteryStats.noteWifiMulticastDisabled(uid);
1521 } catch (RemoteException e) {
1522 } finally {
1523 Binder.restoreCallingIdentity(ident);
1524 }
1525 }
1526
Robert Greenwalt58ff0212009-05-19 15:53:54 -07001527 public boolean isMulticastEnabled() {
Robert Greenwalt5347bd42009-05-13 15:10:16 -07001528 enforceAccessPermission();
1529
1530 synchronized (mMulticasters) {
1531 return (mMulticasters.size() > 0);
1532 }
1533 }
Irfan Sheriff0d255342010-07-28 09:35:20 -07001534
Irfan Sheriff227bec42011-02-15 19:30:27 -08001535 /**
1536 * Evaluate if traffic stats polling is needed based on
1537 * connection and screen on status
1538 */
1539 private void evaluateTrafficStatsPolling() {
1540 Message msg;
1541 if (mNetworkInfo.getDetailedState() == DetailedState.CONNECTED && !mScreenOff) {
Irfan Sheriffebe606f2011-02-24 11:39:15 -08001542 msg = Message.obtain(mAsyncServiceHandler,
1543 WifiManager.CMD_ENABLE_TRAFFIC_STATS_POLL, 1, 0);
Irfan Sheriff227bec42011-02-15 19:30:27 -08001544 } else {
Irfan Sheriffebe606f2011-02-24 11:39:15 -08001545 msg = Message.obtain(mAsyncServiceHandler,
1546 WifiManager.CMD_ENABLE_TRAFFIC_STATS_POLL, 0, 0);
Irfan Sheriff227bec42011-02-15 19:30:27 -08001547 }
1548 msg.sendToTarget();
1549 }
1550
1551 private void notifyOnDataActivity() {
1552 long sent, received;
1553 long preTxPkts = mTxPkts, preRxPkts = mRxPkts;
1554 int dataActivity = WifiManager.DATA_ACTIVITY_NONE;
1555
1556 mTxPkts = TrafficStats.getTxPackets(mInterfaceName);
1557 mRxPkts = TrafficStats.getRxPackets(mInterfaceName);
1558
1559 if (preTxPkts > 0 || preRxPkts > 0) {
1560 sent = mTxPkts - preTxPkts;
1561 received = mRxPkts - preRxPkts;
1562 if (sent > 0) {
1563 dataActivity |= WifiManager.DATA_ACTIVITY_OUT;
1564 }
1565 if (received > 0) {
1566 dataActivity |= WifiManager.DATA_ACTIVITY_IN;
1567 }
1568
1569 if (dataActivity != mDataActivity && !mScreenOff) {
1570 mDataActivity = dataActivity;
1571 for (AsyncChannel client : mClients) {
1572 client.sendMessage(WifiManager.DATA_ACTIVITY_NOTIFICATION, mDataActivity);
1573 }
1574 }
1575 }
1576 }
1577
1578
Irfan Sheriff0d255342010-07-28 09:35:20 -07001579 private void checkAndSetNotification() {
1580 // If we shouldn't place a notification on available networks, then
1581 // don't bother doing any of the following
1582 if (!mNotificationEnabled) return;
1583
1584 State state = mNetworkInfo.getState();
1585 if ((state == NetworkInfo.State.DISCONNECTED)
1586 || (state == NetworkInfo.State.UNKNOWN)) {
1587 // Look for an open network
Irfan Sheriffd8134ff2010-08-22 17:06:34 -07001588 List<ScanResult> scanResults = mWifiStateMachine.syncGetScanResultsList();
Irfan Sheriff0d255342010-07-28 09:35:20 -07001589 if (scanResults != null) {
1590 int numOpenNetworks = 0;
1591 for (int i = scanResults.size() - 1; i >= 0; i--) {
1592 ScanResult scanResult = scanResults.get(i);
1593
1594 if (TextUtils.isEmpty(scanResult.capabilities)) {
1595 numOpenNetworks++;
1596 }
1597 }
1598
1599 if (numOpenNetworks > 0) {
1600 if (++mNumScansSinceNetworkStateChange >= NUM_SCANS_BEFORE_ACTUALLY_SCANNING) {
1601 /*
1602 * We've scanned continuously at least
1603 * NUM_SCANS_BEFORE_NOTIFICATION times. The user
1604 * probably does not have a remembered network in range,
1605 * since otherwise supplicant would have tried to
1606 * associate and thus resetting this counter.
1607 */
1608 setNotificationVisible(true, numOpenNetworks, false, 0);
1609 }
1610 return;
1611 }
1612 }
1613 }
1614
1615 // No open networks in range, remove the notification
1616 setNotificationVisible(false, 0, false, 0);
1617 }
1618
1619 /**
1620 * Clears variables related to tracking whether a notification has been
1621 * shown recently and clears the current notification.
1622 */
1623 private void resetNotification() {
1624 mNotificationRepeatTime = 0;
1625 mNumScansSinceNetworkStateChange = 0;
1626 setNotificationVisible(false, 0, false, 0);
1627 }
1628
1629 /**
1630 * Display or don't display a notification that there are open Wi-Fi networks.
1631 * @param visible {@code true} if notification should be visible, {@code false} otherwise
1632 * @param numNetworks the number networks seen
1633 * @param force {@code true} to force notification to be shown/not-shown,
1634 * even if it is already shown/not-shown.
1635 * @param delay time in milliseconds after which the notification should be made
1636 * visible or invisible.
1637 */
1638 private void setNotificationVisible(boolean visible, int numNetworks, boolean force,
1639 int delay) {
1640
1641 // Since we use auto cancel on the notification, when the
1642 // mNetworksAvailableNotificationShown is true, the notification may
1643 // have actually been canceled. However, when it is false we know
1644 // for sure that it is not being shown (it will not be shown any other
1645 // place than here)
1646
1647 // If it should be hidden and it is already hidden, then noop
1648 if (!visible && !mNotificationShown && !force) {
1649 return;
1650 }
1651
1652 NotificationManager notificationManager = (NotificationManager) mContext
1653 .getSystemService(Context.NOTIFICATION_SERVICE);
1654
1655 Message message;
1656 if (visible) {
1657
1658 // Not enough time has passed to show the notification again
1659 if (System.currentTimeMillis() < mNotificationRepeatTime) {
1660 return;
1661 }
1662
1663 if (mNotification == null) {
Wink Savillec7a98342010-08-13 16:11:42 -07001664 // Cache the Notification object.
Irfan Sheriff0d255342010-07-28 09:35:20 -07001665 mNotification = new Notification();
1666 mNotification.when = 0;
1667 mNotification.icon = ICON_NETWORKS_AVAILABLE;
1668 mNotification.flags = Notification.FLAG_AUTO_CANCEL;
1669 mNotification.contentIntent = PendingIntent.getActivity(mContext, 0,
1670 new Intent(WifiManager.ACTION_PICK_WIFI_NETWORK), 0);
1671 }
1672
1673 CharSequence title = mContext.getResources().getQuantityText(
1674 com.android.internal.R.plurals.wifi_available, numNetworks);
1675 CharSequence details = mContext.getResources().getQuantityText(
1676 com.android.internal.R.plurals.wifi_available_detailed, numNetworks);
1677 mNotification.tickerText = title;
1678 mNotification.setLatestEventInfo(mContext, title, details, mNotification.contentIntent);
1679
1680 mNotificationRepeatTime = System.currentTimeMillis() + NOTIFICATION_REPEAT_DELAY_MS;
1681
1682 notificationManager.notify(ICON_NETWORKS_AVAILABLE, mNotification);
Irfan Sheriff0d255342010-07-28 09:35:20 -07001683 } else {
Irfan Sheriff0d255342010-07-28 09:35:20 -07001684 notificationManager.cancel(ICON_NETWORKS_AVAILABLE);
Irfan Sheriff0d255342010-07-28 09:35:20 -07001685 }
1686
Irfan Sheriff0d255342010-07-28 09:35:20 -07001687 mNotificationShown = visible;
1688 }
1689
1690 private class NotificationEnabledSettingObserver extends ContentObserver {
1691
1692 public NotificationEnabledSettingObserver(Handler handler) {
1693 super(handler);
1694 }
1695
1696 public void register() {
1697 ContentResolver cr = mContext.getContentResolver();
1698 cr.registerContentObserver(Settings.Secure.getUriFor(
1699 Settings.Secure.WIFI_NETWORKS_AVAILABLE_NOTIFICATION_ON), true, this);
1700 mNotificationEnabled = getValue();
1701 }
1702
1703 @Override
1704 public void onChange(boolean selfChange) {
1705 super.onChange(selfChange);
1706
1707 mNotificationEnabled = getValue();
1708 resetNotification();
1709 }
1710
1711 private boolean getValue() {
1712 return Settings.Secure.getInt(mContext.getContentResolver(),
1713 Settings.Secure.WIFI_NETWORKS_AVAILABLE_NOTIFICATION_ON, 1) == 1;
1714 }
1715 }
1716
1717
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001718}