blob: a80a2b852ed35b33e066128ff7e94458b1a8da91 [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;
Isaac Levy654f5092011-07-13 17:41:45 -070038import android.net.wifi.WifiWatchdogStateMachine;
Irfan Sheriff9ab518ad2010-03-12 15:48:17 -080039import android.net.wifi.WifiConfiguration.KeyMgmt;
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 }
Isaac Levy8dc6a1b2011-07-27 08:00:03 -0700292 case WifiManager.CMD_DISABLE_NETWORK: {
293 mWifiStateMachine.disableNetwork(msg.replyTo, msg.arg1, msg.arg2);
294 break;
295 }
Wink Saville4b7ba092010-10-20 15:37:41 -0700296 default: {
297 Slog.d(TAG, "WifiServicehandler.handleMessage ignoring msg=" + msg);
298 break;
299 }
300 }
301 }
302 }
Irfan Sheriff227bec42011-02-15 19:30:27 -0800303 private AsyncServiceHandler mAsyncServiceHandler;
304
305 /**
306 * Handles interaction with WifiStateMachine
307 */
308 private class WifiStateMachineHandler extends Handler {
309 private AsyncChannel mWsmChannel;
310
311 WifiStateMachineHandler(android.os.Looper looper) {
312 super(looper);
313 mWsmChannel = new AsyncChannel();
314 mWsmChannel.connect(mContext, this, mWifiStateMachine.getHandler());
315 }
316
317 @Override
318 public void handleMessage(Message msg) {
319 switch (msg.what) {
320 case AsyncChannel.CMD_CHANNEL_HALF_CONNECTED: {
321 if (msg.arg1 == AsyncChannel.STATUS_SUCCESSFUL) {
322 mWifiStateMachineChannel = mWsmChannel;
323 } else {
324 Slog.e(TAG, "WifiStateMachine connection failure, error=" + msg.arg1);
325 mWifiStateMachineChannel = null;
326 }
327 break;
328 }
Irfan Sheriff6da83d52011-06-06 12:54:06 -0700329 case AsyncChannel.CMD_CHANNEL_DISCONNECTED: {
330 Slog.e(TAG, "WifiStateMachine channel lost, msg.arg1 =" + msg.arg1);
331 mWifiStateMachineChannel = null;
332 //Re-establish connection to state machine
333 mWsmChannel.connect(mContext, this, mWifiStateMachine.getHandler());
334 break;
335 }
Irfan Sheriff227bec42011-02-15 19:30:27 -0800336 default: {
337 Slog.d(TAG, "WifiStateMachineHandler.handleMessage ignoring msg=" + msg);
338 break;
339 }
340 }
341 }
342 }
343 WifiStateMachineHandler mWifiStateMachineHandler;
Wink Saville4b7ba092010-10-20 15:37:41 -0700344
345 /**
Dianne Hackborn03f3cb02010-09-17 23:12:26 -0700346 * Temporary for computing UIDS that are responsible for starting WIFI.
347 * Protected by mWifiStateTracker lock.
348 */
349 private final WorkSource mTmpWorkSource = new WorkSource();
Isaac Levy654f5092011-07-13 17:41:45 -0700350 private WifiWatchdogStateMachine mWifiWatchdogStateMachine;
Irfan Sheriff0d255342010-07-28 09:35:20 -0700351
352 WifiService(Context context) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800353 mContext = context;
Irfan Sheriff227bec42011-02-15 19:30:27 -0800354
355 mInterfaceName = SystemProperties.get("wifi.interface", "wlan0");
356
357 mWifiStateMachine = new WifiStateMachine(mContext, mInterfaceName);
Irfan Sheriff0d255342010-07-28 09:35:20 -0700358 mWifiStateMachine.enableRssiPolling(true);
The Android Open Source Project10592532009-03-18 17:39:46 -0700359 mBatteryStats = BatteryStatsService.getService();
Jaikumar Ganesh084c6652009-12-07 10:58:18 -0800360
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800361 mAlarmManager = (AlarmManager)mContext.getSystemService(Context.ALARM_SERVICE);
362 Intent idleIntent = new Intent(ACTION_DEVICE_IDLE, null);
363 mIdleIntent = PendingIntent.getBroadcast(mContext, IDLE_REQUEST, idleIntent, 0);
364
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800365 mContext.registerReceiver(
366 new BroadcastReceiver() {
367 @Override
368 public void onReceive(Context context, Intent intent) {
Irfan Sheriff658772f2011-03-08 14:52:31 -0800369 mAirplaneModeOn.set(isAirplaneModeOn());
370 /* On airplane mode disable, restore wifi state if necessary */
371 if (!mAirplaneModeOn.get() && (testAndClearWifiSavedState() ||
372 mWifiState.get() == WIFI_ENABLED_AIRPLANE_OVERRIDE)) {
373 persistWifiEnabled(true);
Irfan Sheriffb2e6c012010-04-05 11:57:56 -0700374 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800375 updateWifiState();
376 }
377 },
378 new IntentFilter(Intent.ACTION_AIRPLANE_MODE_CHANGED));
379
Irfan Sheriff0d255342010-07-28 09:35:20 -0700380 IntentFilter filter = new IntentFilter();
381 filter.addAction(WifiManager.WIFI_STATE_CHANGED_ACTION);
382 filter.addAction(WifiManager.NETWORK_STATE_CHANGED_ACTION);
383 filter.addAction(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION);
384
385 mContext.registerReceiver(
386 new BroadcastReceiver() {
387 @Override
388 public void onReceive(Context context, Intent intent) {
389 if (intent.getAction().equals(WifiManager.WIFI_STATE_CHANGED_ACTION)) {
390 // reset & clear notification on any wifi state change
391 resetNotification();
392 } else if (intent.getAction().equals(
393 WifiManager.NETWORK_STATE_CHANGED_ACTION)) {
394 mNetworkInfo = (NetworkInfo) intent.getParcelableExtra(
395 WifiManager.EXTRA_NETWORK_INFO);
396 // reset & clear notification on a network connect & disconnect
397 switch(mNetworkInfo.getDetailedState()) {
398 case CONNECTED:
399 case DISCONNECTED:
Irfan Sheriff227bec42011-02-15 19:30:27 -0800400 evaluateTrafficStatsPolling();
Irfan Sheriff0d255342010-07-28 09:35:20 -0700401 resetNotification();
402 break;
403 }
404 } else if (intent.getAction().equals(
405 WifiManager.SCAN_RESULTS_AVAILABLE_ACTION)) {
406 checkAndSetNotification();
407 }
408 }
409 }, filter);
410
Irfan Sheriff227bec42011-02-15 19:30:27 -0800411 HandlerThread wifiThread = new HandlerThread("WifiService");
412 wifiThread.start();
413 mAsyncServiceHandler = new AsyncServiceHandler(wifiThread.getLooper());
414 mWifiStateMachineHandler = new WifiStateMachineHandler(wifiThread.getLooper());
415
Irfan Sheriff0d255342010-07-28 09:35:20 -0700416 // Setting is in seconds
417 NOTIFICATION_REPEAT_DELAY_MS = Settings.Secure.getInt(context.getContentResolver(),
418 Settings.Secure.WIFI_NETWORKS_AVAILABLE_REPEAT_DELAY, 900) * 1000l;
419 mNotificationEnabledSettingObserver = new NotificationEnabledSettingObserver(new Handler());
420 mNotificationEnabledSettingObserver.register();
Irfan Sherifffcc08452011-02-17 16:44:54 -0800421
422 mBackgroundScanSupported = mContext.getResources().getBoolean(
423 com.android.internal.R.bool.config_wifi_background_scan_support);
Irfan Sheriff7b009782010-03-11 16:37:45 -0800424 }
Irfan Sheriff5321aef2010-02-12 12:35:59 -0800425
Irfan Sheriff7b009782010-03-11 16:37:45 -0800426 /**
427 * Check if Wi-Fi needs to be enabled and start
428 * if needed
Irfan Sheriff60e3ba02010-04-02 12:18:45 -0700429 *
430 * This function is used only at boot time
Irfan Sheriff7b009782010-03-11 16:37:45 -0800431 */
Irfan Sheriff0d255342010-07-28 09:35:20 -0700432 public void checkAndStartWifi() {
Irfan Sheriff658772f2011-03-08 14:52:31 -0800433 mAirplaneModeOn.set(isAirplaneModeOn());
434 mWifiState.set(getPersistedWifiState());
435 /* Start if Wi-Fi should be enabled or the saved state indicates Wi-Fi was on */
436 boolean wifiEnabled = shouldWifiBeEnabled() || testAndClearWifiSavedState();
Irfan Sheriff7b009782010-03-11 16:37:45 -0800437 Slog.i(TAG, "WifiService starting up with Wi-Fi " +
438 (wifiEnabled ? "enabled" : "disabled"));
Irfan Sheriffb99fe5e2010-03-26 14:56:07 -0700439 setWifiEnabled(wifiEnabled);
Isaac Levybc7dfb52011-06-06 15:34:01 -0700440
Isaac Levy654f5092011-07-13 17:41:45 -0700441 mWifiWatchdogStateMachine = WifiWatchdogStateMachine.
442 makeWifiWatchdogStateMachine(mContext);
443
Irfan Sheriff5321aef2010-02-12 12:35:59 -0800444 }
445
Irfan Sheriffa3bd4092010-03-24 17:58:59 -0700446 private boolean testAndClearWifiSavedState() {
447 final ContentResolver cr = mContext.getContentResolver();
448 int wifiSavedState = 0;
449 try {
450 wifiSavedState = Settings.Secure.getInt(cr, Settings.Secure.WIFI_SAVED_STATE);
451 if(wifiSavedState == 1)
452 Settings.Secure.putInt(cr, Settings.Secure.WIFI_SAVED_STATE, 0);
453 } catch (Settings.SettingNotFoundException e) {
454 ;
455 }
456 return (wifiSavedState == 1);
457 }
458
Irfan Sheriff658772f2011-03-08 14:52:31 -0800459 private int getPersistedWifiState() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800460 final ContentResolver cr = mContext.getContentResolver();
461 try {
Irfan Sheriff658772f2011-03-08 14:52:31 -0800462 return Settings.Secure.getInt(cr, Settings.Secure.WIFI_ON);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800463 } catch (Settings.SettingNotFoundException e) {
Irfan Sheriff658772f2011-03-08 14:52:31 -0800464 Settings.Secure.putInt(cr, Settings.Secure.WIFI_ON, WIFI_DISABLED);
465 return WIFI_DISABLED;
466 }
467 }
468
469 private boolean shouldWifiBeEnabled() {
470 if (mAirplaneModeOn.get()) {
471 return mWifiState.get() == WIFI_ENABLED_AIRPLANE_OVERRIDE;
472 } else {
473 return mWifiState.get() != WIFI_DISABLED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800474 }
475 }
476
477 private void persistWifiEnabled(boolean enabled) {
478 final ContentResolver cr = mContext.getContentResolver();
Irfan Sheriff658772f2011-03-08 14:52:31 -0800479 if (enabled) {
480 if (isAirplaneModeOn() && isAirplaneToggleable()) {
481 mWifiState.set(WIFI_ENABLED_AIRPLANE_OVERRIDE);
482 } else {
483 mWifiState.set(WIFI_ENABLED);
484 }
485 } else {
486 mWifiState.set(WIFI_DISABLED);
487 }
488 Settings.Secure.putInt(cr, Settings.Secure.WIFI_ON, mWifiState.get());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800489 }
490
Irfan Sheriff658772f2011-03-08 14:52:31 -0800491
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800492 /**
493 * see {@link android.net.wifi.WifiManager#pingSupplicant()}
Irfan Sheriffa2a1b912010-06-07 09:03:04 -0700494 * @return {@code true} if the operation succeeds, {@code false} otherwise
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800495 */
496 public boolean pingSupplicant() {
Irfan Sheriffa2a1b912010-06-07 09:03:04 -0700497 enforceAccessPermission();
Irfan Sheriff227bec42011-02-15 19:30:27 -0800498 if (mWifiStateMachineChannel != null) {
499 return mWifiStateMachine.syncPingSupplicant(mWifiStateMachineChannel);
Irfan Sheriff1406bcb2010-10-28 14:41:39 -0700500 } else {
Irfan Sheriff227bec42011-02-15 19:30:27 -0800501 Slog.e(TAG, "mWifiStateMachineChannel is not initialized");
Irfan Sheriff1406bcb2010-10-28 14:41:39 -0700502 return false;
503 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800504 }
505
506 /**
507 * see {@link android.net.wifi.WifiManager#startScan()}
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800508 */
Irfan Sheriffe4984752010-08-19 11:29:22 -0700509 public void startScan(boolean forceActive) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800510 enforceChangePermission();
Irfan Sheriffe4984752010-08-19 11:29:22 -0700511 mWifiStateMachine.startScan(forceActive);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800512 }
513
514 private void enforceAccessPermission() {
515 mContext.enforceCallingOrSelfPermission(android.Manifest.permission.ACCESS_WIFI_STATE,
516 "WifiService");
517 }
518
519 private void enforceChangePermission() {
520 mContext.enforceCallingOrSelfPermission(android.Manifest.permission.CHANGE_WIFI_STATE,
521 "WifiService");
522
523 }
524
Robert Greenwaltfc1b15c2009-05-22 15:09:51 -0700525 private void enforceMulticastChangePermission() {
526 mContext.enforceCallingOrSelfPermission(
527 android.Manifest.permission.CHANGE_WIFI_MULTICAST_STATE,
528 "WifiService");
529 }
530
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800531 /**
Irfan Sheriffa2a1b912010-06-07 09:03:04 -0700532 * see {@link android.net.wifi.WifiManager#setWifiEnabled(boolean)}
533 * @param enable {@code true} to enable, {@code false} to disable.
534 * @return {@code true} if the enable/disable operation was
535 * started or is already in the queue.
536 */
537 public synchronized boolean setWifiEnabled(boolean enable) {
538 enforceChangePermission();
539
540 if (DBG) {
Irfan Sheriff0d255342010-07-28 09:35:20 -0700541 Slog.e(TAG, "Invoking mWifiStateMachine.setWifiEnabled\n");
Irfan Sheriffa2a1b912010-06-07 09:03:04 -0700542 }
543
Dianne Hackborn03f3cb02010-09-17 23:12:26 -0700544 if (enable) {
545 reportStartWorkSource();
546 }
Irfan Sheriff0d255342010-07-28 09:35:20 -0700547 mWifiStateMachine.setWifiEnabled(enable);
Irfan Sheriff61180692010-08-18 16:07:39 -0700548
549 /*
550 * Caller might not have WRITE_SECURE_SETTINGS,
551 * only CHANGE_WIFI_STATE is enforced
552 */
553 long ident = Binder.clearCallingIdentity();
Irfan Sheriffa2a1b912010-06-07 09:03:04 -0700554 persistWifiEnabled(enable);
Irfan Sheriff61180692010-08-18 16:07:39 -0700555 Binder.restoreCallingIdentity(ident);
Irfan Sheriffa2a1b912010-06-07 09:03:04 -0700556
557 if (enable) {
558 if (!mIsReceiverRegistered) {
559 registerForBroadcasts();
560 mIsReceiverRegistered = true;
561 }
562 } else if (mIsReceiverRegistered){
563 mContext.unregisterReceiver(mReceiver);
564 mIsReceiverRegistered = false;
565 }
566
567 return true;
568 }
569
570 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800571 * see {@link WifiManager#getWifiState()}
572 * @return One of {@link WifiManager#WIFI_STATE_DISABLED},
573 * {@link WifiManager#WIFI_STATE_DISABLING},
574 * {@link WifiManager#WIFI_STATE_ENABLED},
575 * {@link WifiManager#WIFI_STATE_ENABLING},
576 * {@link WifiManager#WIFI_STATE_UNKNOWN}
577 */
578 public int getWifiEnabledState() {
579 enforceAccessPermission();
Irfan Sheriffd8134ff2010-08-22 17:06:34 -0700580 return mWifiStateMachine.syncGetWifiState();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800581 }
582
583 /**
Irfan Sheriffc2f54c22010-03-18 14:02:22 -0700584 * see {@link android.net.wifi.WifiManager#setWifiApEnabled(WifiConfiguration, boolean)}
Irfan Sheriff5321aef2010-02-12 12:35:59 -0800585 * @param wifiConfig SSID, security and channel details as
586 * part of WifiConfiguration
Irfan Sheriffa2a1b912010-06-07 09:03:04 -0700587 * @param enabled true to enable and false to disable
Irfan Sheriff5321aef2010-02-12 12:35:59 -0800588 */
Irfan Sheriffffcea7a2011-05-10 16:26:06 -0700589 public void setWifiApEnabled(WifiConfiguration wifiConfig, boolean enabled) {
Irfan Sheriff5321aef2010-02-12 12:35:59 -0800590 enforceChangePermission();
Irfan Sheriff0d255342010-07-28 09:35:20 -0700591 mWifiStateMachine.setWifiApEnabled(wifiConfig, enabled);
Irfan Sheriff5321aef2010-02-12 12:35:59 -0800592 }
593
Irfan Sheriffa2a1b912010-06-07 09:03:04 -0700594 /**
595 * see {@link WifiManager#getWifiApState()}
596 * @return One of {@link WifiManager#WIFI_AP_STATE_DISABLED},
597 * {@link WifiManager#WIFI_AP_STATE_DISABLING},
598 * {@link WifiManager#WIFI_AP_STATE_ENABLED},
599 * {@link WifiManager#WIFI_AP_STATE_ENABLING},
600 * {@link WifiManager#WIFI_AP_STATE_FAILED}
601 */
602 public int getWifiApEnabledState() {
Irfan Sheriff17b232b2010-06-24 11:32:26 -0700603 enforceAccessPermission();
Irfan Sheriffd8134ff2010-08-22 17:06:34 -0700604 return mWifiStateMachine.syncGetWifiApState();
Irfan Sheriffa2a1b912010-06-07 09:03:04 -0700605 }
606
607 /**
608 * see {@link WifiManager#getWifiApConfiguration()}
609 * @return soft access point configuration
610 */
Irfan Sheriffffcea7a2011-05-10 16:26:06 -0700611 public WifiConfiguration getWifiApConfiguration() {
612 enforceAccessPermission();
Irfan Sheriff6da83d52011-06-06 12:54:06 -0700613 if (mWifiStateMachineChannel != null) {
614 return mWifiStateMachine.syncGetWifiApConfiguration(mWifiStateMachineChannel);
615 } else {
616 Slog.e(TAG, "mWifiStateMachineChannel is not initialized");
617 return null;
618 }
Irfan Sheriff9ab518ad2010-03-12 15:48:17 -0800619 }
620
Irfan Sheriffa2a1b912010-06-07 09:03:04 -0700621 /**
622 * see {@link WifiManager#setWifiApConfiguration(WifiConfiguration)}
623 * @param wifiConfig WifiConfiguration details for soft access point
624 */
Irfan Sheriffffcea7a2011-05-10 16:26:06 -0700625 public void setWifiApConfiguration(WifiConfiguration wifiConfig) {
Irfan Sheriff17b232b2010-06-24 11:32:26 -0700626 enforceChangePermission();
Irfan Sheriff9ab518ad2010-03-12 15:48:17 -0800627 if (wifiConfig == null)
628 return;
Irfan Sheriffffcea7a2011-05-10 16:26:06 -0700629 mWifiStateMachine.setWifiApConfiguration(wifiConfig);
Irfan Sheriff9ab518ad2010-03-12 15:48:17 -0800630 }
631
Irfan Sheriff5321aef2010-02-12 12:35:59 -0800632 /**
Irfan Sheriffa2a1b912010-06-07 09:03:04 -0700633 * see {@link android.net.wifi.WifiManager#disconnect()}
Irfan Sheriff5321aef2010-02-12 12:35:59 -0800634 */
Irfan Sheriffe4984752010-08-19 11:29:22 -0700635 public void disconnect() {
Irfan Sheriffa2a1b912010-06-07 09:03:04 -0700636 enforceChangePermission();
Irfan Sheriffe4984752010-08-19 11:29:22 -0700637 mWifiStateMachine.disconnectCommand();
Irfan Sheriff5321aef2010-02-12 12:35:59 -0800638 }
639
640 /**
Irfan Sheriffa2a1b912010-06-07 09:03:04 -0700641 * see {@link android.net.wifi.WifiManager#reconnect()}
Irfan Sheriff5321aef2010-02-12 12:35:59 -0800642 */
Irfan Sheriffe4984752010-08-19 11:29:22 -0700643 public void reconnect() {
Irfan Sheriffa2a1b912010-06-07 09:03:04 -0700644 enforceChangePermission();
Irfan Sheriffe4984752010-08-19 11:29:22 -0700645 mWifiStateMachine.reconnectCommand();
Irfan Sheriff5321aef2010-02-12 12:35:59 -0800646 }
647
Irfan Sheriffa2a1b912010-06-07 09:03:04 -0700648 /**
649 * see {@link android.net.wifi.WifiManager#reassociate()}
Irfan Sheriffa2a1b912010-06-07 09:03:04 -0700650 */
Irfan Sheriffe4984752010-08-19 11:29:22 -0700651 public void reassociate() {
Irfan Sheriffa2a1b912010-06-07 09:03:04 -0700652 enforceChangePermission();
Irfan Sheriffe4984752010-08-19 11:29:22 -0700653 mWifiStateMachine.reassociateCommand();
Irfan Sheriff5321aef2010-02-12 12:35:59 -0800654 }
655
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800656 /**
657 * see {@link android.net.wifi.WifiManager#getConfiguredNetworks()}
658 * @return the list of configured networks
659 */
660 public List<WifiConfiguration> getConfiguredNetworks() {
661 enforceAccessPermission();
Irfan Sheriffd8134ff2010-08-22 17:06:34 -0700662 return mWifiStateMachine.syncGetConfiguredNetworks();
Chung-yih Wanga8d15942009-10-09 11:01:49 +0800663 }
664
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800665 /**
666 * see {@link android.net.wifi.WifiManager#addOrUpdateNetwork(WifiConfiguration)}
667 * @return the supplicant-assigned identifier for the new or updated
668 * network if the operation succeeds, or {@code -1} if it fails
669 */
Irfan Sheriff7aac5542009-12-22 21:42:17 -0800670 public int addOrUpdateNetwork(WifiConfiguration config) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800671 enforceChangePermission();
Irfan Sheriff227bec42011-02-15 19:30:27 -0800672 if (mWifiStateMachineChannel != null) {
673 return mWifiStateMachine.syncAddOrUpdateNetwork(mWifiStateMachineChannel, config);
Irfan Sheriff1406bcb2010-10-28 14:41:39 -0700674 } else {
Irfan Sheriff227bec42011-02-15 19:30:27 -0800675 Slog.e(TAG, "mWifiStateMachineChannel is not initialized");
Irfan Sheriff1406bcb2010-10-28 14:41:39 -0700676 return -1;
677 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800678 }
679
Irfan Sheriffa2a1b912010-06-07 09:03:04 -0700680 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800681 * See {@link android.net.wifi.WifiManager#removeNetwork(int)}
682 * @param netId the integer that identifies the network configuration
683 * to the supplicant
684 * @return {@code true} if the operation succeeded
685 */
686 public boolean removeNetwork(int netId) {
687 enforceChangePermission();
Irfan Sheriff227bec42011-02-15 19:30:27 -0800688 if (mWifiStateMachineChannel != null) {
689 return mWifiStateMachine.syncRemoveNetwork(mWifiStateMachineChannel, netId);
Wink Saville4b7ba092010-10-20 15:37:41 -0700690 } else {
Irfan Sheriff227bec42011-02-15 19:30:27 -0800691 Slog.e(TAG, "mWifiStateMachineChannel is not initialized");
Wink Saville4b7ba092010-10-20 15:37:41 -0700692 return false;
693 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800694 }
695
696 /**
697 * See {@link android.net.wifi.WifiManager#enableNetwork(int, boolean)}
698 * @param netId the integer that identifies the network configuration
699 * to the supplicant
700 * @param disableOthers if true, disable all other networks.
701 * @return {@code true} if the operation succeeded
702 */
703 public boolean enableNetwork(int netId, boolean disableOthers) {
704 enforceChangePermission();
Irfan Sheriff227bec42011-02-15 19:30:27 -0800705 if (mWifiStateMachineChannel != null) {
706 return mWifiStateMachine.syncEnableNetwork(mWifiStateMachineChannel, netId,
707 disableOthers);
Irfan Sheriff1406bcb2010-10-28 14:41:39 -0700708 } else {
Irfan Sheriff227bec42011-02-15 19:30:27 -0800709 Slog.e(TAG, "mWifiStateMachineChannel is not initialized");
Irfan Sheriff1406bcb2010-10-28 14:41:39 -0700710 return false;
711 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800712 }
713
714 /**
715 * See {@link android.net.wifi.WifiManager#disableNetwork(int)}
716 * @param netId the integer that identifies the network configuration
717 * to the supplicant
718 * @return {@code true} if the operation succeeded
719 */
720 public boolean disableNetwork(int netId) {
721 enforceChangePermission();
Irfan Sheriff227bec42011-02-15 19:30:27 -0800722 if (mWifiStateMachineChannel != null) {
723 return mWifiStateMachine.syncDisableNetwork(mWifiStateMachineChannel, netId);
Irfan Sheriff1406bcb2010-10-28 14:41:39 -0700724 } else {
Irfan Sheriff227bec42011-02-15 19:30:27 -0800725 Slog.e(TAG, "mWifiStateMachineChannel is not initialized");
Irfan Sheriff1406bcb2010-10-28 14:41:39 -0700726 return false;
727 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800728 }
729
730 /**
731 * See {@link android.net.wifi.WifiManager#getConnectionInfo()}
732 * @return the Wi-Fi information, contained in {@link WifiInfo}.
733 */
734 public WifiInfo getConnectionInfo() {
735 enforceAccessPermission();
736 /*
737 * Make sure we have the latest information, by sending
738 * a status request to the supplicant.
739 */
Irfan Sheriffd8134ff2010-08-22 17:06:34 -0700740 return mWifiStateMachine.syncRequestConnectionInfo();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800741 }
742
743 /**
744 * Return the results of the most recent access point scan, in the form of
745 * a list of {@link ScanResult} objects.
746 * @return the list of results
747 */
748 public List<ScanResult> getScanResults() {
749 enforceAccessPermission();
Irfan Sheriffd8134ff2010-08-22 17:06:34 -0700750 return mWifiStateMachine.syncGetScanResultsList();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800751 }
752
753 /**
754 * Tell the supplicant to persist the current list of configured networks.
755 * @return {@code true} if the operation succeeded
Irfan Sheriffa2a1b912010-06-07 09:03:04 -0700756 *
757 * TODO: deprecate this
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800758 */
759 public boolean saveConfiguration() {
Irfan Sheriffa2a1b912010-06-07 09:03:04 -0700760 boolean result = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800761 enforceChangePermission();
Irfan Sheriff227bec42011-02-15 19:30:27 -0800762 if (mWifiStateMachineChannel != null) {
763 return mWifiStateMachine.syncSaveConfig(mWifiStateMachineChannel);
Irfan Sheriff1406bcb2010-10-28 14:41:39 -0700764 } else {
Irfan Sheriff227bec42011-02-15 19:30:27 -0800765 Slog.e(TAG, "mWifiStateMachineChannel is not initialized");
Irfan Sheriff1406bcb2010-10-28 14:41:39 -0700766 return false;
767 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800768 }
769
770 /**
Irfan Sheriffed4f28b2010-10-29 15:32:10 -0700771 * Set the country code
772 * @param countryCode ISO 3166 country code.
Robert Greenwaltb5010cc2009-05-21 15:11:40 -0700773 * @param persist {@code true} if the setting should be remembered.
Irfan Sheriffed4f28b2010-10-29 15:32:10 -0700774 *
775 * The persist behavior exists so that wifi can fall back to the last
776 * persisted country code on a restart, when the locale information is
777 * not available from telephony.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800778 */
Irfan Sheriffed4f28b2010-10-29 15:32:10 -0700779 public void setCountryCode(String countryCode, boolean persist) {
780 Slog.i(TAG, "WifiService trying to set country code to " + countryCode +
781 " with persist set to " + persist);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800782 enforceChangePermission();
Irfan Sheriffed4f28b2010-10-29 15:32:10 -0700783 mWifiStateMachine.setCountryCode(countryCode, persist);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800784 }
785
786 /**
Irfan Sheriff36f74132010-11-04 16:57:37 -0700787 * Set the operational frequency band
788 * @param band One of
789 * {@link WifiManager#WIFI_FREQUENCY_BAND_AUTO},
790 * {@link WifiManager#WIFI_FREQUENCY_BAND_5GHZ},
791 * {@link WifiManager#WIFI_FREQUENCY_BAND_2GHZ},
792 * @param persist {@code true} if the setting should be remembered.
793 *
794 */
795 public void setFrequencyBand(int band, boolean persist) {
796 enforceChangePermission();
797 if (!isDualBandSupported()) return;
798 Slog.i(TAG, "WifiService trying to set frequency band to " + band +
799 " with persist set to " + persist);
800 mWifiStateMachine.setFrequencyBand(band, persist);
801 }
802
803
804 /**
805 * Get the operational frequency band
806 */
807 public int getFrequencyBand() {
808 enforceAccessPermission();
809 return mWifiStateMachine.getFrequencyBand();
810 }
811
812 public boolean isDualBandSupported() {
813 //TODO: Should move towards adding a driver API that checks at runtime
814 return mContext.getResources().getBoolean(
815 com.android.internal.R.bool.config_wifi_dual_band_support);
816 }
817
818 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800819 * Return the DHCP-assigned addresses from the last successful DHCP request,
820 * if any.
821 * @return the DHCP information
822 */
823 public DhcpInfo getDhcpInfo() {
824 enforceAccessPermission();
Irfan Sheriffd8134ff2010-08-22 17:06:34 -0700825 return mWifiStateMachine.syncGetDhcpInfo();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800826 }
827
Irfan Sheriff0d255342010-07-28 09:35:20 -0700828 /**
829 * see {@link android.net.wifi.WifiManager#startWifi}
830 *
831 */
832 public void startWifi() {
833 enforceChangePermission();
834 /* TODO: may be add permissions for access only to connectivity service
835 * TODO: if a start issued, keep wifi alive until a stop issued irrespective
836 * of WifiLock & device idle status unless wifi enabled status is toggled
837 */
838
839 mWifiStateMachine.setDriverStart(true);
840 mWifiStateMachine.reconnectCommand();
841 }
842
843 /**
844 * see {@link android.net.wifi.WifiManager#stopWifi}
845 *
846 */
847 public void stopWifi() {
848 enforceChangePermission();
849 /* TODO: may be add permissions for access only to connectivity service
850 * TODO: if a stop is issued, wifi is brought up only by startWifi
851 * unless wifi enabled status is toggled
852 */
853 mWifiStateMachine.setDriverStart(false);
854 }
855
856
857 /**
858 * see {@link android.net.wifi.WifiManager#addToBlacklist}
859 *
860 */
861 public void addToBlacklist(String bssid) {
862 enforceChangePermission();
863
864 mWifiStateMachine.addToBlacklist(bssid);
865 }
866
867 /**
868 * see {@link android.net.wifi.WifiManager#clearBlacklist}
869 *
870 */
871 public void clearBlacklist() {
872 enforceChangePermission();
873
874 mWifiStateMachine.clearBlacklist();
875 }
876
Irfan Sheriff227bec42011-02-15 19:30:27 -0800877 /**
878 * Get a reference to handler. This is used by a client to establish
879 * an AsyncChannel communication with WifiService
880 */
881 public Messenger getMessenger() {
Irfan Sheriffebe606f2011-02-24 11:39:15 -0800882 /* Enforce the highest permissions
883 TODO: when we consider exposing the asynchronous API, think about
884 how to provide both access and change permissions seperately
885 */
Irfan Sheriff227bec42011-02-15 19:30:27 -0800886 enforceAccessPermission();
Irfan Sheriffebe606f2011-02-24 11:39:15 -0800887 enforceChangePermission();
Irfan Sheriff227bec42011-02-15 19:30:27 -0800888 return new Messenger(mAsyncServiceHandler);
889 }
890
Irfan Sheriff4aeca7c52011-03-10 16:53:33 -0800891 /**
892 * Get the IP and proxy configuration file
893 */
894 public String getConfigFile() {
895 enforceAccessPermission();
896 return mWifiStateMachine.getConfigFile();
897 }
898
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800899 private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
900 @Override
901 public void onReceive(Context context, Intent intent) {
902 String action = intent.getAction();
903
Doug Zongker43866e02010-01-07 12:09:54 -0800904 long idleMillis =
905 Settings.Secure.getLong(mContext.getContentResolver(),
Irfan Sheriff4f5f7c92010-10-14 17:01:27 -0700906 Settings.Secure.WIFI_IDLE_MS, DEFAULT_IDLE_MS);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800907 int stayAwakeConditions =
Doug Zongker43866e02010-01-07 12:09:54 -0800908 Settings.System.getInt(mContext.getContentResolver(),
909 Settings.System.STAY_ON_WHILE_PLUGGED_IN, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800910 if (action.equals(Intent.ACTION_SCREEN_ON)) {
Joe Onorato431bb222010-10-18 19:13:23 -0400911 if (DBG) {
912 Slog.d(TAG, "ACTION_SCREEN_ON");
913 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800914 mAlarmManager.cancel(mIdleIntent);
915 mDeviceIdle = false;
916 mScreenOff = false;
Dianne Hackborn58e0eef2010-09-16 01:22:10 -0700917 // Once the screen is on, we are not keeping WIFI running
918 // because of any locks so clear that tracking immediately.
919 reportStartWorkSource();
Irfan Sheriff227bec42011-02-15 19:30:27 -0800920 evaluateTrafficStatsPolling();
Irfan Sheriff0d255342010-07-28 09:35:20 -0700921 mWifiStateMachine.enableRssiPolling(true);
Irfan Sherifffcc08452011-02-17 16:44:54 -0800922 if (mBackgroundScanSupported) {
Irfan Sheriff2b7f6382011-03-25 14:29:19 -0700923 mWifiStateMachine.enableBackgroundScanCommand(false);
Irfan Sherifffcc08452011-02-17 16:44:54 -0800924 }
Irfan Sheriff8e86b892010-12-22 11:02:20 -0800925 mWifiStateMachine.enableAllNetworks();
Irfan Sheriff4f5f7c92010-10-14 17:01:27 -0700926 updateWifiState();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800927 } else if (action.equals(Intent.ACTION_SCREEN_OFF)) {
Joe Onorato431bb222010-10-18 19:13:23 -0400928 if (DBG) {
929 Slog.d(TAG, "ACTION_SCREEN_OFF");
930 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800931 mScreenOff = true;
Irfan Sheriff227bec42011-02-15 19:30:27 -0800932 evaluateTrafficStatsPolling();
Irfan Sheriff0d255342010-07-28 09:35:20 -0700933 mWifiStateMachine.enableRssiPolling(false);
Irfan Sherifffcc08452011-02-17 16:44:54 -0800934 if (mBackgroundScanSupported) {
Irfan Sheriff2b7f6382011-03-25 14:29:19 -0700935 mWifiStateMachine.enableBackgroundScanCommand(true);
Irfan Sherifffcc08452011-02-17 16:44:54 -0800936 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800937 /*
938 * Set a timer to put Wi-Fi to sleep, but only if the screen is off
939 * AND the "stay on while plugged in" setting doesn't match the
940 * current power conditions (i.e, not plugged in, plugged in to USB,
941 * or plugged in to AC).
942 */
943 if (!shouldWifiStayAwake(stayAwakeConditions, mPluggedType)) {
Irfan Sheriffd8134ff2010-08-22 17:06:34 -0700944 WifiInfo info = mWifiStateMachine.syncRequestConnectionInfo();
San Mehatfa6c7112009-07-07 09:34:44 -0700945 if (info.getSupplicantState() != SupplicantState.COMPLETED) {
Robert Greenwalt84612ea62009-09-30 09:04:22 -0700946 // we used to go to sleep immediately, but this caused some race conditions
Irfan Sheriffa2a1b912010-06-07 09:03:04 -0700947 // we don't have time to track down for this release. Delay instead,
948 // but not as long as we would if connected (below)
Robert Greenwalt84612ea62009-09-30 09:04:22 -0700949 // TODO - fix the race conditions and switch back to the immediate turn-off
950 long triggerTime = System.currentTimeMillis() + (2*60*1000); // 2 min
Joe Onorato431bb222010-10-18 19:13:23 -0400951 if (DBG) {
952 Slog.d(TAG, "setting ACTION_DEVICE_IDLE timer for 120,000 ms");
953 }
Robert Greenwalt84612ea62009-09-30 09:04:22 -0700954 mAlarmManager.set(AlarmManager.RTC_WAKEUP, triggerTime, mIdleIntent);
955 // // do not keep Wifi awake when screen is off if Wifi is not associated
956 // mDeviceIdle = true;
957 // updateWifiState();
Mike Lockwoodd9c32bc2009-05-18 14:14:15 -0400958 } else {
959 long triggerTime = System.currentTimeMillis() + idleMillis;
Joe Onorato431bb222010-10-18 19:13:23 -0400960 if (DBG) {
961 Slog.d(TAG, "setting ACTION_DEVICE_IDLE timer for " + idleMillis
962 + "ms");
963 }
Mike Lockwoodd9c32bc2009-05-18 14:14:15 -0400964 mAlarmManager.set(AlarmManager.RTC_WAKEUP, triggerTime, mIdleIntent);
965 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800966 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800967 } else if (action.equals(ACTION_DEVICE_IDLE)) {
Joe Onorato431bb222010-10-18 19:13:23 -0400968 if (DBG) {
969 Slog.d(TAG, "got ACTION_DEVICE_IDLE");
970 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800971 mDeviceIdle = true;
Dianne Hackborn58e0eef2010-09-16 01:22:10 -0700972 reportStartWorkSource();
Irfan Sheriff4f5f7c92010-10-14 17:01:27 -0700973 updateWifiState();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800974 } else if (action.equals(Intent.ACTION_BATTERY_CHANGED)) {
975 /*
976 * Set a timer to put Wi-Fi to sleep, but only if the screen is off
977 * AND we are transitioning from a state in which the device was supposed
978 * to stay awake to a state in which it is not supposed to stay awake.
979 * If "stay awake" state is not changing, we do nothing, to avoid resetting
980 * the already-set timer.
981 */
982 int pluggedType = intent.getIntExtra("plugged", 0);
Joe Onorato431bb222010-10-18 19:13:23 -0400983 if (DBG) {
984 Slog.d(TAG, "ACTION_BATTERY_CHANGED pluggedType: " + pluggedType);
985 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800986 if (mScreenOff && shouldWifiStayAwake(stayAwakeConditions, mPluggedType) &&
987 !shouldWifiStayAwake(stayAwakeConditions, pluggedType)) {
988 long triggerTime = System.currentTimeMillis() + idleMillis;
Joe Onorato431bb222010-10-18 19:13:23 -0400989 if (DBG) {
990 Slog.d(TAG, "setting ACTION_DEVICE_IDLE timer for " + idleMillis + "ms");
991 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800992 mAlarmManager.set(AlarmManager.RTC_WAKEUP, triggerTime, mIdleIntent);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800993 }
994 mPluggedType = pluggedType;
Irfan Sheriff65eaec82011-01-05 22:00:16 -0800995 } else if (action.equals(BluetoothAdapter.ACTION_CONNECTION_STATE_CHANGED)) {
996 int state = intent.getIntExtra(BluetoothAdapter.EXTRA_CONNECTION_STATE,
997 BluetoothAdapter.STATE_DISCONNECTED);
998 mWifiStateMachine.sendBluetoothAdapterStateChange(state);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800999 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001000 }
1001
1002 /**
1003 * Determines whether the Wi-Fi chipset should stay awake or be put to
1004 * sleep. Looks at the setting for the sleep policy and the current
1005 * conditions.
Jaikumar Ganesh084c6652009-12-07 10:58:18 -08001006 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001007 * @see #shouldDeviceStayAwake(int, int)
1008 */
1009 private boolean shouldWifiStayAwake(int stayAwakeConditions, int pluggedType) {
Irfan Sheriff739f6bc2011-01-28 16:43:12 -08001010 //Never sleep as long as the user has not changed the settings
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001011 int wifiSleepPolicy = Settings.System.getInt(mContext.getContentResolver(),
Irfan Sheriff96b10d62011-01-11 15:40:35 -08001012 Settings.System.WIFI_SLEEP_POLICY,
Irfan Sheriff739f6bc2011-01-28 16:43:12 -08001013 Settings.System.WIFI_SLEEP_POLICY_NEVER);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001014
1015 if (wifiSleepPolicy == Settings.System.WIFI_SLEEP_POLICY_NEVER) {
1016 // Never sleep
1017 return true;
1018 } else if ((wifiSleepPolicy == Settings.System.WIFI_SLEEP_POLICY_NEVER_WHILE_PLUGGED) &&
1019 (pluggedType != 0)) {
1020 // Never sleep while plugged, and we're plugged
1021 return true;
1022 } else {
1023 // Default
1024 return shouldDeviceStayAwake(stayAwakeConditions, pluggedType);
1025 }
1026 }
Jaikumar Ganesh084c6652009-12-07 10:58:18 -08001027
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001028 /**
1029 * Determine whether the bit value corresponding to {@code pluggedType} is set in
1030 * the bit string {@code stayAwakeConditions}. Because a {@code pluggedType} value
1031 * of {@code 0} isn't really a plugged type, but rather an indication that the
1032 * device isn't plugged in at all, there is no bit value corresponding to a
1033 * {@code pluggedType} value of {@code 0}. That is why we shift by
Ben Dodson4e8620f2010-08-25 10:55:47 -07001034 * {@code pluggedType - 1} instead of by {@code pluggedType}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001035 * @param stayAwakeConditions a bit string specifying which "plugged types" should
1036 * keep the device (and hence Wi-Fi) awake.
1037 * @param pluggedType the type of plug (USB, AC, or none) for which the check is
1038 * being made
1039 * @return {@code true} if {@code pluggedType} indicates that the device is
1040 * supposed to stay awake, {@code false} otherwise.
1041 */
1042 private boolean shouldDeviceStayAwake(int stayAwakeConditions, int pluggedType) {
1043 return (stayAwakeConditions & pluggedType) != 0;
1044 }
1045 };
1046
Dianne Hackborn03f3cb02010-09-17 23:12:26 -07001047 private synchronized void reportStartWorkSource() {
1048 mTmpWorkSource.clear();
1049 if (mDeviceIdle) {
1050 for (int i=0; i<mLocks.mList.size(); i++) {
1051 mTmpWorkSource.add(mLocks.mList.get(i).mWorkSource);
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07001052 }
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07001053 }
Dianne Hackborn03f3cb02010-09-17 23:12:26 -07001054 mWifiStateMachine.updateBatteryWorkSource(mTmpWorkSource);
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07001055 }
Jaikumar Ganesh7440fc22010-09-27 17:04:14 -07001056
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001057 private void updateWifiState() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001058 boolean lockHeld = mLocks.hasLocks();
Irfan Sheriff5876a422010-08-12 20:26:23 -07001059 int strongestLockMode = WifiManager.WIFI_MODE_FULL;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001060 boolean wifiShouldBeStarted = !mDeviceIdle || lockHeld;
Irfan Sheriff5876a422010-08-12 20:26:23 -07001061
1062 if (lockHeld) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001063 strongestLockMode = mLocks.getStrongestLockMode();
Irfan Sheriff5876a422010-08-12 20:26:23 -07001064 }
1065 /* If device is not idle, lockmode cannot be scan only */
1066 if (!mDeviceIdle && strongestLockMode == WifiManager.WIFI_MODE_SCAN_ONLY) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001067 strongestLockMode = WifiManager.WIFI_MODE_FULL;
1068 }
1069
Irfan Sheriffa2a1b912010-06-07 09:03:04 -07001070 /* Disable tethering when airplane mode is enabled */
Irfan Sheriff658772f2011-03-08 14:52:31 -08001071 if (mAirplaneModeOn.get()) {
Irfan Sheriff0d255342010-07-28 09:35:20 -07001072 mWifiStateMachine.setWifiApEnabled(null, false);
Irfan Sheriffa2a1b912010-06-07 09:03:04 -07001073 }
Irfan Sheriffb2e6c012010-04-05 11:57:56 -07001074
Irfan Sheriff658772f2011-03-08 14:52:31 -08001075 if (shouldWifiBeEnabled()) {
Irfan Sheriffa2a1b912010-06-07 09:03:04 -07001076 if (wifiShouldBeStarted) {
Dianne Hackborn03f3cb02010-09-17 23:12:26 -07001077 reportStartWorkSource();
Irfan Sheriff0d255342010-07-28 09:35:20 -07001078 mWifiStateMachine.setWifiEnabled(true);
1079 mWifiStateMachine.setScanOnlyMode(
Irfan Sheriffa2a1b912010-06-07 09:03:04 -07001080 strongestLockMode == WifiManager.WIFI_MODE_SCAN_ONLY);
Irfan Sheriff0d255342010-07-28 09:35:20 -07001081 mWifiStateMachine.setDriverStart(true);
Irfan Sheriff5876a422010-08-12 20:26:23 -07001082 mWifiStateMachine.setHighPerfModeEnabled(strongestLockMode
1083 == WifiManager.WIFI_MODE_FULL_HIGH_PERF);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001084 } else {
Irfan Sheriff0d255342010-07-28 09:35:20 -07001085 mWifiStateMachine.requestCmWakeLock();
1086 mWifiStateMachine.setDriverStart(false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001087 }
Irfan Sheriffa2a1b912010-06-07 09:03:04 -07001088 } else {
Irfan Sheriff0d255342010-07-28 09:35:20 -07001089 mWifiStateMachine.setWifiEnabled(false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001090 }
1091 }
1092
1093 private void registerForBroadcasts() {
1094 IntentFilter intentFilter = new IntentFilter();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001095 intentFilter.addAction(Intent.ACTION_SCREEN_ON);
1096 intentFilter.addAction(Intent.ACTION_SCREEN_OFF);
1097 intentFilter.addAction(Intent.ACTION_BATTERY_CHANGED);
1098 intentFilter.addAction(ACTION_DEVICE_IDLE);
Irfan Sheriff65eaec82011-01-05 22:00:16 -08001099 intentFilter.addAction(BluetoothAdapter.ACTION_CONNECTION_STATE_CHANGED);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001100 mContext.registerReceiver(mReceiver, intentFilter);
1101 }
Jaikumar Ganesh084c6652009-12-07 10:58:18 -08001102
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001103 private boolean isAirplaneSensitive() {
1104 String airplaneModeRadios = Settings.System.getString(mContext.getContentResolver(),
1105 Settings.System.AIRPLANE_MODE_RADIOS);
1106 return airplaneModeRadios == null
1107 || airplaneModeRadios.contains(Settings.System.RADIO_WIFI);
1108 }
1109
Mike Lockwoodbd5ddf02009-07-29 21:37:14 -07001110 private boolean isAirplaneToggleable() {
1111 String toggleableRadios = Settings.System.getString(mContext.getContentResolver(),
1112 Settings.System.AIRPLANE_MODE_TOGGLEABLE_RADIOS);
1113 return toggleableRadios != null
1114 && toggleableRadios.contains(Settings.System.RADIO_WIFI);
1115 }
1116
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001117 /**
1118 * Returns true if Wi-Fi is sensitive to airplane mode, and airplane mode is
1119 * currently on.
1120 * @return {@code true} if airplane mode is on.
1121 */
1122 private boolean isAirplaneModeOn() {
1123 return isAirplaneSensitive() && Settings.System.getInt(mContext.getContentResolver(),
1124 Settings.System.AIRPLANE_MODE_ON, 0) == 1;
1125 }
1126
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001127 @Override
1128 protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
1129 if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.DUMP)
1130 != PackageManager.PERMISSION_GRANTED) {
1131 pw.println("Permission Denial: can't dump WifiService from from pid="
1132 + Binder.getCallingPid()
1133 + ", uid=" + Binder.getCallingUid());
1134 return;
1135 }
Irfan Sheriffd8134ff2010-08-22 17:06:34 -07001136 pw.println("Wi-Fi is " + mWifiStateMachine.syncGetWifiStateByName());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001137 pw.println("Stay-awake conditions: " +
1138 Settings.System.getInt(mContext.getContentResolver(),
1139 Settings.System.STAY_ON_WHILE_PLUGGED_IN, 0));
1140 pw.println();
1141
1142 pw.println("Internal state:");
Irfan Sheriff0d255342010-07-28 09:35:20 -07001143 pw.println(mWifiStateMachine);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001144 pw.println();
1145 pw.println("Latest scan results:");
Irfan Sheriffd8134ff2010-08-22 17:06:34 -07001146 List<ScanResult> scanResults = mWifiStateMachine.syncGetScanResultsList();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001147 if (scanResults != null && scanResults.size() != 0) {
1148 pw.println(" BSSID Frequency RSSI Flags SSID");
1149 for (ScanResult r : scanResults) {
1150 pw.printf(" %17s %9d %5d %-16s %s%n",
1151 r.BSSID,
1152 r.frequency,
1153 r.level,
1154 r.capabilities,
1155 r.SSID == null ? "" : r.SSID);
1156 }
1157 }
1158 pw.println();
Eric Shienbrood5711fad2009-03-27 20:25:31 -07001159 pw.println("Locks acquired: " + mFullLocksAcquired + " full, " +
Irfan Sheriff5876a422010-08-12 20:26:23 -07001160 mFullHighPerfLocksAcquired + " full high perf, " +
Eric Shienbrood5711fad2009-03-27 20:25:31 -07001161 mScanLocksAcquired + " scan");
1162 pw.println("Locks released: " + mFullLocksReleased + " full, " +
Irfan Sheriff5876a422010-08-12 20:26:23 -07001163 mFullHighPerfLocksReleased + " full high perf, " +
Eric Shienbrood5711fad2009-03-27 20:25:31 -07001164 mScanLocksReleased + " scan");
1165 pw.println();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001166 pw.println("Locks held:");
1167 mLocks.dump(pw);
Isaac Levybc7dfb52011-06-06 15:34:01 -07001168
1169 pw.println();
Isaac Levy654f5092011-07-13 17:41:45 -07001170 pw.println("WifiWatchdogStateMachine dump");
1171 mWifiWatchdogStateMachine.dump(pw);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001172 }
1173
Robert Greenwalt58ff0212009-05-19 15:53:54 -07001174 private class WifiLock extends DeathRecipient {
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -07001175 WifiLock(int lockMode, String tag, IBinder binder, WorkSource ws) {
1176 super(lockMode, tag, binder, ws);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001177 }
1178
1179 public void binderDied() {
1180 synchronized (mLocks) {
1181 releaseWifiLockLocked(mBinder);
1182 }
1183 }
1184
1185 public String toString() {
Robert Greenwalt5347bd42009-05-13 15:10:16 -07001186 return "WifiLock{" + mTag + " type=" + mMode + " binder=" + mBinder + "}";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001187 }
1188 }
1189
1190 private class LockList {
1191 private List<WifiLock> mList;
1192
1193 private LockList() {
1194 mList = new ArrayList<WifiLock>();
1195 }
1196
1197 private synchronized boolean hasLocks() {
1198 return !mList.isEmpty();
1199 }
1200
1201 private synchronized int getStrongestLockMode() {
1202 if (mList.isEmpty()) {
1203 return WifiManager.WIFI_MODE_FULL;
1204 }
Irfan Sheriff5876a422010-08-12 20:26:23 -07001205
1206 if (mFullHighPerfLocksAcquired > mFullHighPerfLocksReleased) {
1207 return WifiManager.WIFI_MODE_FULL_HIGH_PERF;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001208 }
Irfan Sheriff5876a422010-08-12 20:26:23 -07001209
1210 if (mFullLocksAcquired > mFullLocksReleased) {
1211 return WifiManager.WIFI_MODE_FULL;
1212 }
1213
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001214 return WifiManager.WIFI_MODE_SCAN_ONLY;
1215 }
1216
1217 private void addLock(WifiLock lock) {
1218 if (findLockByBinder(lock.mBinder) < 0) {
1219 mList.add(lock);
1220 }
1221 }
1222
1223 private WifiLock removeLock(IBinder binder) {
1224 int index = findLockByBinder(binder);
1225 if (index >= 0) {
Suchi Amalapurapufff2fda2009-06-30 21:36:16 -07001226 WifiLock ret = mList.remove(index);
1227 ret.unlinkDeathRecipient();
1228 return ret;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001229 } else {
1230 return null;
1231 }
1232 }
1233
1234 private int findLockByBinder(IBinder binder) {
1235 int size = mList.size();
1236 for (int i = size - 1; i >= 0; i--)
1237 if (mList.get(i).mBinder == binder)
1238 return i;
1239 return -1;
1240 }
1241
1242 private void dump(PrintWriter pw) {
1243 for (WifiLock l : mList) {
1244 pw.print(" ");
1245 pw.println(l);
1246 }
1247 }
1248 }
1249
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -07001250 void enforceWakeSourcePermission(int uid, int pid) {
Dianne Hackborne746f032010-09-13 16:02:57 -07001251 if (uid == android.os.Process.myUid()) {
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -07001252 return;
1253 }
1254 mContext.enforcePermission(android.Manifest.permission.UPDATE_DEVICE_STATS,
1255 pid, uid, null);
1256 }
1257
1258 public boolean acquireWifiLock(IBinder binder, int lockMode, String tag, WorkSource ws) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001259 mContext.enforceCallingOrSelfPermission(android.Manifest.permission.WAKE_LOCK, null);
Irfan Sheriff5876a422010-08-12 20:26:23 -07001260 if (lockMode != WifiManager.WIFI_MODE_FULL &&
1261 lockMode != WifiManager.WIFI_MODE_SCAN_ONLY &&
1262 lockMode != WifiManager.WIFI_MODE_FULL_HIGH_PERF) {
1263 Slog.e(TAG, "Illegal argument, lockMode= " + lockMode);
1264 if (DBG) throw new IllegalArgumentException("lockMode=" + lockMode);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001265 return false;
1266 }
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -07001267 if (ws != null && ws.size() == 0) {
1268 ws = null;
1269 }
Dianne Hackbornecfd7f72010-10-08 14:23:40 -07001270 if (ws != null) {
1271 enforceWakeSourcePermission(Binder.getCallingUid(), Binder.getCallingPid());
1272 }
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -07001273 if (ws == null) {
1274 ws = new WorkSource(Binder.getCallingUid());
1275 }
1276 WifiLock wifiLock = new WifiLock(lockMode, tag, binder, ws);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001277 synchronized (mLocks) {
1278 return acquireWifiLockLocked(wifiLock);
1279 }
1280 }
1281
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -07001282 private void noteAcquireWifiLock(WifiLock wifiLock) throws RemoteException {
1283 switch(wifiLock.mMode) {
1284 case WifiManager.WIFI_MODE_FULL:
Irfan Sheriff5876a422010-08-12 20:26:23 -07001285 case WifiManager.WIFI_MODE_FULL_HIGH_PERF:
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -07001286 mBatteryStats.noteFullWifiLockAcquiredFromSource(wifiLock.mWorkSource);
1287 break;
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -07001288 case WifiManager.WIFI_MODE_SCAN_ONLY:
1289 mBatteryStats.noteScanWifiLockAcquiredFromSource(wifiLock.mWorkSource);
1290 break;
1291 }
1292 }
1293
1294 private void noteReleaseWifiLock(WifiLock wifiLock) throws RemoteException {
1295 switch(wifiLock.mMode) {
1296 case WifiManager.WIFI_MODE_FULL:
Irfan Sheriff5876a422010-08-12 20:26:23 -07001297 case WifiManager.WIFI_MODE_FULL_HIGH_PERF:
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -07001298 mBatteryStats.noteFullWifiLockReleasedFromSource(wifiLock.mWorkSource);
1299 break;
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -07001300 case WifiManager.WIFI_MODE_SCAN_ONLY:
1301 mBatteryStats.noteScanWifiLockReleasedFromSource(wifiLock.mWorkSource);
1302 break;
1303 }
1304 }
1305
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001306 private boolean acquireWifiLockLocked(WifiLock wifiLock) {
Irfan Sheriffc89dd542010-09-28 08:40:54 -07001307 if (DBG) Slog.d(TAG, "acquireWifiLockLocked: " + wifiLock);
Robert Greenwaltf1acb2d2009-10-13 08:20:55 -07001308
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001309 mLocks.addLock(wifiLock);
Robert Greenwaltf1acb2d2009-10-13 08:20:55 -07001310
The Android Open Source Project10592532009-03-18 17:39:46 -07001311 long ident = Binder.clearCallingIdentity();
1312 try {
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -07001313 noteAcquireWifiLock(wifiLock);
Robert Greenwalt5347bd42009-05-13 15:10:16 -07001314 switch(wifiLock.mMode) {
Eric Shienbrood5711fad2009-03-27 20:25:31 -07001315 case WifiManager.WIFI_MODE_FULL:
1316 ++mFullLocksAcquired;
Eric Shienbrood5711fad2009-03-27 20:25:31 -07001317 break;
Irfan Sheriff5876a422010-08-12 20:26:23 -07001318 case WifiManager.WIFI_MODE_FULL_HIGH_PERF:
1319 ++mFullHighPerfLocksAcquired;
1320 break;
1321
Eric Shienbrood5711fad2009-03-27 20:25:31 -07001322 case WifiManager.WIFI_MODE_SCAN_ONLY:
1323 ++mScanLocksAcquired;
Eric Shienbrood5711fad2009-03-27 20:25:31 -07001324 break;
The Android Open Source Project10592532009-03-18 17:39:46 -07001325 }
Dianne Hackbornecfd7f72010-10-08 14:23:40 -07001326
1327 // Be aggressive about adding new locks into the accounted state...
1328 // we want to over-report rather than under-report.
1329 reportStartWorkSource();
1330
1331 updateWifiState();
1332 return true;
The Android Open Source Project10592532009-03-18 17:39:46 -07001333 } catch (RemoteException e) {
Dianne Hackbornecfd7f72010-10-08 14:23:40 -07001334 return false;
The Android Open Source Project10592532009-03-18 17:39:46 -07001335 } finally {
1336 Binder.restoreCallingIdentity(ident);
1337 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001338 }
1339
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -07001340 public void updateWifiLockWorkSource(IBinder lock, WorkSource ws) {
1341 int uid = Binder.getCallingUid();
1342 int pid = Binder.getCallingPid();
1343 if (ws != null && ws.size() == 0) {
1344 ws = null;
1345 }
1346 if (ws != null) {
1347 enforceWakeSourcePermission(uid, pid);
1348 }
1349 long ident = Binder.clearCallingIdentity();
1350 try {
1351 synchronized (mLocks) {
1352 int index = mLocks.findLockByBinder(lock);
1353 if (index < 0) {
1354 throw new IllegalArgumentException("Wifi lock not active");
1355 }
1356 WifiLock wl = mLocks.mList.get(index);
1357 noteReleaseWifiLock(wl);
1358 wl.mWorkSource = ws != null ? new WorkSource(ws) : new WorkSource(uid);
1359 noteAcquireWifiLock(wl);
1360 }
1361 } catch (RemoteException e) {
1362 } finally {
1363 Binder.restoreCallingIdentity(ident);
1364 }
1365 }
1366
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001367 public boolean releaseWifiLock(IBinder lock) {
1368 mContext.enforceCallingOrSelfPermission(android.Manifest.permission.WAKE_LOCK, null);
1369 synchronized (mLocks) {
1370 return releaseWifiLockLocked(lock);
1371 }
1372 }
1373
1374 private boolean releaseWifiLockLocked(IBinder lock) {
Eric Shienbroodd4c5f892009-03-24 18:13:20 -07001375 boolean hadLock;
Robert Greenwaltf1acb2d2009-10-13 08:20:55 -07001376
The Android Open Source Project10592532009-03-18 17:39:46 -07001377 WifiLock wifiLock = mLocks.removeLock(lock);
Robert Greenwaltf1acb2d2009-10-13 08:20:55 -07001378
Irfan Sheriffc89dd542010-09-28 08:40:54 -07001379 if (DBG) Slog.d(TAG, "releaseWifiLockLocked: " + wifiLock);
Robert Greenwaltf1acb2d2009-10-13 08:20:55 -07001380
Eric Shienbroodd4c5f892009-03-24 18:13:20 -07001381 hadLock = (wifiLock != null);
1382
Dianne Hackbornecfd7f72010-10-08 14:23:40 -07001383 long ident = Binder.clearCallingIdentity();
1384 try {
1385 if (hadLock) {
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -07001386 noteAcquireWifiLock(wifiLock);
Robert Greenwalt5347bd42009-05-13 15:10:16 -07001387 switch(wifiLock.mMode) {
Eric Shienbrood5711fad2009-03-27 20:25:31 -07001388 case WifiManager.WIFI_MODE_FULL:
1389 ++mFullLocksReleased;
Eric Shienbrood5711fad2009-03-27 20:25:31 -07001390 break;
Irfan Sheriff5876a422010-08-12 20:26:23 -07001391 case WifiManager.WIFI_MODE_FULL_HIGH_PERF:
1392 ++mFullHighPerfLocksReleased;
1393 break;
Eric Shienbrood5711fad2009-03-27 20:25:31 -07001394 case WifiManager.WIFI_MODE_SCAN_ONLY:
1395 ++mScanLocksReleased;
Eric Shienbrood5711fad2009-03-27 20:25:31 -07001396 break;
Eric Shienbroodd4c5f892009-03-24 18:13:20 -07001397 }
The Android Open Source Project10592532009-03-18 17:39:46 -07001398 }
Dianne Hackbornecfd7f72010-10-08 14:23:40 -07001399
1400 // TODO - should this only happen if you hadLock?
1401 updateWifiState();
1402
1403 } catch (RemoteException e) {
1404 } finally {
1405 Binder.restoreCallingIdentity(ident);
The Android Open Source Project10592532009-03-18 17:39:46 -07001406 }
Dianne Hackbornecfd7f72010-10-08 14:23:40 -07001407
Eric Shienbroodd4c5f892009-03-24 18:13:20 -07001408 return hadLock;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001409 }
Robert Greenwalt5347bd42009-05-13 15:10:16 -07001410
Robert Greenwalt58ff0212009-05-19 15:53:54 -07001411 private abstract class DeathRecipient
Robert Greenwalt5347bd42009-05-13 15:10:16 -07001412 implements IBinder.DeathRecipient {
1413 String mTag;
1414 int mMode;
1415 IBinder mBinder;
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -07001416 WorkSource mWorkSource;
Robert Greenwalt5347bd42009-05-13 15:10:16 -07001417
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -07001418 DeathRecipient(int mode, String tag, IBinder binder, WorkSource ws) {
Robert Greenwalt5347bd42009-05-13 15:10:16 -07001419 super();
1420 mTag = tag;
1421 mMode = mode;
1422 mBinder = binder;
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -07001423 mWorkSource = ws;
Robert Greenwalt5347bd42009-05-13 15:10:16 -07001424 try {
1425 mBinder.linkToDeath(this, 0);
1426 } catch (RemoteException e) {
1427 binderDied();
1428 }
1429 }
Suchi Amalapurapufff2fda2009-06-30 21:36:16 -07001430
1431 void unlinkDeathRecipient() {
1432 mBinder.unlinkToDeath(this, 0);
1433 }
Robert Greenwalt5347bd42009-05-13 15:10:16 -07001434 }
1435
Robert Greenwalt58ff0212009-05-19 15:53:54 -07001436 private class Multicaster extends DeathRecipient {
1437 Multicaster(String tag, IBinder binder) {
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -07001438 super(Binder.getCallingUid(), tag, binder, null);
Robert Greenwalt5347bd42009-05-13 15:10:16 -07001439 }
1440
1441 public void binderDied() {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001442 Slog.e(TAG, "Multicaster binderDied");
Robert Greenwalt5347bd42009-05-13 15:10:16 -07001443 synchronized (mMulticasters) {
1444 int i = mMulticasters.indexOf(this);
1445 if (i != -1) {
1446 removeMulticasterLocked(i, mMode);
1447 }
1448 }
1449 }
1450
1451 public String toString() {
Robert Greenwalt58ff0212009-05-19 15:53:54 -07001452 return "Multicaster{" + mTag + " binder=" + mBinder + "}";
Robert Greenwalt5347bd42009-05-13 15:10:16 -07001453 }
1454
1455 public int getUid() {
1456 return mMode;
1457 }
1458 }
1459
Robert Greenwalte2d155a2009-10-21 14:58:34 -07001460 public void initializeMulticastFiltering() {
1461 enforceMulticastChangePermission();
Irfan Sheriffa8fbe1f2010-03-09 09:13:58 -08001462
Robert Greenwalte2d155a2009-10-21 14:58:34 -07001463 synchronized (mMulticasters) {
1464 // if anybody had requested filters be off, leave off
1465 if (mMulticasters.size() != 0) {
1466 return;
1467 } else {
Irfan Sheriffb0c1b80f2011-07-19 15:44:25 -07001468 mWifiStateMachine.startFilteringMulticastV4Packets();
Robert Greenwalte2d155a2009-10-21 14:58:34 -07001469 }
1470 }
1471 }
1472
Robert Greenwaltfc1b15c2009-05-22 15:09:51 -07001473 public void acquireMulticastLock(IBinder binder, String tag) {
1474 enforceMulticastChangePermission();
Robert Greenwalt5347bd42009-05-13 15:10:16 -07001475
1476 synchronized (mMulticasters) {
1477 mMulticastEnabled++;
Robert Greenwalt58ff0212009-05-19 15:53:54 -07001478 mMulticasters.add(new Multicaster(tag, binder));
Irfan Sheriffb0c1b80f2011-07-19 15:44:25 -07001479 // Note that we could call stopFilteringMulticastV4Packets only when
Robert Greenwalt5347bd42009-05-13 15:10:16 -07001480 // our new size == 1 (first call), but this function won't
1481 // be called often and by making the stopPacket call each
1482 // time we're less fragile and self-healing.
Irfan Sheriffb0c1b80f2011-07-19 15:44:25 -07001483 mWifiStateMachine.stopFilteringMulticastV4Packets();
Robert Greenwalt5347bd42009-05-13 15:10:16 -07001484 }
1485
1486 int uid = Binder.getCallingUid();
1487 Long ident = Binder.clearCallingIdentity();
1488 try {
1489 mBatteryStats.noteWifiMulticastEnabled(uid);
1490 } catch (RemoteException e) {
1491 } finally {
1492 Binder.restoreCallingIdentity(ident);
1493 }
1494 }
1495
Robert Greenwaltfc1b15c2009-05-22 15:09:51 -07001496 public void releaseMulticastLock() {
1497 enforceMulticastChangePermission();
Robert Greenwalt5347bd42009-05-13 15:10:16 -07001498
1499 int uid = Binder.getCallingUid();
1500 synchronized (mMulticasters) {
1501 mMulticastDisabled++;
1502 int size = mMulticasters.size();
1503 for (int i = size - 1; i >= 0; i--) {
Robert Greenwalt58ff0212009-05-19 15:53:54 -07001504 Multicaster m = mMulticasters.get(i);
Robert Greenwalt5347bd42009-05-13 15:10:16 -07001505 if ((m != null) && (m.getUid() == uid)) {
1506 removeMulticasterLocked(i, uid);
1507 }
1508 }
1509 }
1510 }
1511
1512 private void removeMulticasterLocked(int i, int uid)
1513 {
Suchi Amalapurapufff2fda2009-06-30 21:36:16 -07001514 Multicaster removed = mMulticasters.remove(i);
Irfan Sheriffa8fbe1f2010-03-09 09:13:58 -08001515
Suchi Amalapurapufff2fda2009-06-30 21:36:16 -07001516 if (removed != null) {
1517 removed.unlinkDeathRecipient();
1518 }
Robert Greenwalt5347bd42009-05-13 15:10:16 -07001519 if (mMulticasters.size() == 0) {
Irfan Sheriffb0c1b80f2011-07-19 15:44:25 -07001520 mWifiStateMachine.startFilteringMulticastV4Packets();
Robert Greenwalt5347bd42009-05-13 15:10:16 -07001521 }
1522
1523 Long ident = Binder.clearCallingIdentity();
1524 try {
1525 mBatteryStats.noteWifiMulticastDisabled(uid);
1526 } catch (RemoteException e) {
1527 } finally {
1528 Binder.restoreCallingIdentity(ident);
1529 }
1530 }
1531
Robert Greenwalt58ff0212009-05-19 15:53:54 -07001532 public boolean isMulticastEnabled() {
Robert Greenwalt5347bd42009-05-13 15:10:16 -07001533 enforceAccessPermission();
1534
1535 synchronized (mMulticasters) {
1536 return (mMulticasters.size() > 0);
1537 }
1538 }
Irfan Sheriff0d255342010-07-28 09:35:20 -07001539
Irfan Sheriff227bec42011-02-15 19:30:27 -08001540 /**
1541 * Evaluate if traffic stats polling is needed based on
1542 * connection and screen on status
1543 */
1544 private void evaluateTrafficStatsPolling() {
1545 Message msg;
1546 if (mNetworkInfo.getDetailedState() == DetailedState.CONNECTED && !mScreenOff) {
Irfan Sheriffebe606f2011-02-24 11:39:15 -08001547 msg = Message.obtain(mAsyncServiceHandler,
1548 WifiManager.CMD_ENABLE_TRAFFIC_STATS_POLL, 1, 0);
Irfan Sheriff227bec42011-02-15 19:30:27 -08001549 } else {
Irfan Sheriffebe606f2011-02-24 11:39:15 -08001550 msg = Message.obtain(mAsyncServiceHandler,
1551 WifiManager.CMD_ENABLE_TRAFFIC_STATS_POLL, 0, 0);
Irfan Sheriff227bec42011-02-15 19:30:27 -08001552 }
1553 msg.sendToTarget();
1554 }
1555
1556 private void notifyOnDataActivity() {
1557 long sent, received;
1558 long preTxPkts = mTxPkts, preRxPkts = mRxPkts;
1559 int dataActivity = WifiManager.DATA_ACTIVITY_NONE;
1560
1561 mTxPkts = TrafficStats.getTxPackets(mInterfaceName);
1562 mRxPkts = TrafficStats.getRxPackets(mInterfaceName);
1563
1564 if (preTxPkts > 0 || preRxPkts > 0) {
1565 sent = mTxPkts - preTxPkts;
1566 received = mRxPkts - preRxPkts;
1567 if (sent > 0) {
1568 dataActivity |= WifiManager.DATA_ACTIVITY_OUT;
1569 }
1570 if (received > 0) {
1571 dataActivity |= WifiManager.DATA_ACTIVITY_IN;
1572 }
1573
1574 if (dataActivity != mDataActivity && !mScreenOff) {
1575 mDataActivity = dataActivity;
1576 for (AsyncChannel client : mClients) {
1577 client.sendMessage(WifiManager.DATA_ACTIVITY_NOTIFICATION, mDataActivity);
1578 }
1579 }
1580 }
1581 }
1582
1583
Irfan Sheriff0d255342010-07-28 09:35:20 -07001584 private void checkAndSetNotification() {
1585 // If we shouldn't place a notification on available networks, then
1586 // don't bother doing any of the following
1587 if (!mNotificationEnabled) return;
1588
1589 State state = mNetworkInfo.getState();
1590 if ((state == NetworkInfo.State.DISCONNECTED)
1591 || (state == NetworkInfo.State.UNKNOWN)) {
1592 // Look for an open network
Irfan Sheriffd8134ff2010-08-22 17:06:34 -07001593 List<ScanResult> scanResults = mWifiStateMachine.syncGetScanResultsList();
Irfan Sheriff0d255342010-07-28 09:35:20 -07001594 if (scanResults != null) {
1595 int numOpenNetworks = 0;
1596 for (int i = scanResults.size() - 1; i >= 0; i--) {
1597 ScanResult scanResult = scanResults.get(i);
1598
Irfan Sherifffdd5f952011-08-04 16:55:54 -07001599 //A capability of [ESS] represents an open access point
1600 //that is available for an STA to connect
1601 if (scanResult.capabilities != null &&
1602 scanResult.capabilities.equals("[ESS]")) {
Irfan Sheriff0d255342010-07-28 09:35:20 -07001603 numOpenNetworks++;
1604 }
1605 }
1606
1607 if (numOpenNetworks > 0) {
1608 if (++mNumScansSinceNetworkStateChange >= NUM_SCANS_BEFORE_ACTUALLY_SCANNING) {
1609 /*
1610 * We've scanned continuously at least
1611 * NUM_SCANS_BEFORE_NOTIFICATION times. The user
1612 * probably does not have a remembered network in range,
1613 * since otherwise supplicant would have tried to
1614 * associate and thus resetting this counter.
1615 */
1616 setNotificationVisible(true, numOpenNetworks, false, 0);
1617 }
1618 return;
1619 }
1620 }
1621 }
1622
1623 // No open networks in range, remove the notification
1624 setNotificationVisible(false, 0, false, 0);
1625 }
1626
1627 /**
1628 * Clears variables related to tracking whether a notification has been
1629 * shown recently and clears the current notification.
1630 */
1631 private void resetNotification() {
1632 mNotificationRepeatTime = 0;
1633 mNumScansSinceNetworkStateChange = 0;
1634 setNotificationVisible(false, 0, false, 0);
1635 }
1636
1637 /**
1638 * Display or don't display a notification that there are open Wi-Fi networks.
1639 * @param visible {@code true} if notification should be visible, {@code false} otherwise
1640 * @param numNetworks the number networks seen
1641 * @param force {@code true} to force notification to be shown/not-shown,
1642 * even if it is already shown/not-shown.
1643 * @param delay time in milliseconds after which the notification should be made
1644 * visible or invisible.
1645 */
1646 private void setNotificationVisible(boolean visible, int numNetworks, boolean force,
1647 int delay) {
1648
1649 // Since we use auto cancel on the notification, when the
1650 // mNetworksAvailableNotificationShown is true, the notification may
1651 // have actually been canceled. However, when it is false we know
1652 // for sure that it is not being shown (it will not be shown any other
1653 // place than here)
1654
1655 // If it should be hidden and it is already hidden, then noop
1656 if (!visible && !mNotificationShown && !force) {
1657 return;
1658 }
1659
1660 NotificationManager notificationManager = (NotificationManager) mContext
1661 .getSystemService(Context.NOTIFICATION_SERVICE);
1662
1663 Message message;
1664 if (visible) {
1665
1666 // Not enough time has passed to show the notification again
1667 if (System.currentTimeMillis() < mNotificationRepeatTime) {
1668 return;
1669 }
1670
1671 if (mNotification == null) {
Wink Savillec7a98342010-08-13 16:11:42 -07001672 // Cache the Notification object.
Irfan Sheriff0d255342010-07-28 09:35:20 -07001673 mNotification = new Notification();
1674 mNotification.when = 0;
1675 mNotification.icon = ICON_NETWORKS_AVAILABLE;
1676 mNotification.flags = Notification.FLAG_AUTO_CANCEL;
1677 mNotification.contentIntent = PendingIntent.getActivity(mContext, 0,
1678 new Intent(WifiManager.ACTION_PICK_WIFI_NETWORK), 0);
1679 }
1680
1681 CharSequence title = mContext.getResources().getQuantityText(
1682 com.android.internal.R.plurals.wifi_available, numNetworks);
1683 CharSequence details = mContext.getResources().getQuantityText(
1684 com.android.internal.R.plurals.wifi_available_detailed, numNetworks);
1685 mNotification.tickerText = title;
1686 mNotification.setLatestEventInfo(mContext, title, details, mNotification.contentIntent);
1687
1688 mNotificationRepeatTime = System.currentTimeMillis() + NOTIFICATION_REPEAT_DELAY_MS;
1689
1690 notificationManager.notify(ICON_NETWORKS_AVAILABLE, mNotification);
Irfan Sheriff0d255342010-07-28 09:35:20 -07001691 } else {
Irfan Sheriff0d255342010-07-28 09:35:20 -07001692 notificationManager.cancel(ICON_NETWORKS_AVAILABLE);
Irfan Sheriff0d255342010-07-28 09:35:20 -07001693 }
1694
Irfan Sheriff0d255342010-07-28 09:35:20 -07001695 mNotificationShown = visible;
1696 }
1697
1698 private class NotificationEnabledSettingObserver extends ContentObserver {
1699
1700 public NotificationEnabledSettingObserver(Handler handler) {
1701 super(handler);
1702 }
1703
1704 public void register() {
1705 ContentResolver cr = mContext.getContentResolver();
1706 cr.registerContentObserver(Settings.Secure.getUriFor(
1707 Settings.Secure.WIFI_NETWORKS_AVAILABLE_NOTIFICATION_ON), true, this);
1708 mNotificationEnabled = getValue();
1709 }
1710
1711 @Override
1712 public void onChange(boolean selfChange) {
1713 super.onChange(selfChange);
1714
1715 mNotificationEnabled = getValue();
1716 resetNotification();
1717 }
1718
1719 private boolean getValue() {
1720 return Settings.Secure.getInt(mContext.getContentResolver(),
1721 Settings.Secure.WIFI_NETWORKS_AVAILABLE_NOTIFICATION_ON, 1) == 1;
1722 }
1723 }
1724
1725
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001726}