blob: 3c65255ca5bfffb178ea6a783f10585304066855 [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 Sheriff651cdfc2011-09-07 00:31:20 -070040import android.net.wifi.WpsInfo;
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;
Irfan Sheriff616f3172011-09-11 19:59:01 -070072import com.android.internal.telephony.TelephonyIntents;
Wink Saville4b7ba092010-10-20 15:37:41 -070073import com.android.internal.util.AsyncChannel;
The Android Open Source Project10592532009-03-18 17:39:46 -070074import com.android.server.am.BatteryStatsService;
Irfan Sheriff9ab518ad2010-03-12 15:48:17 -080075import com.android.internal.R;
The Android Open Source Project10592532009-03-18 17:39:46 -070076
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080077/**
78 * WifiService handles remote WiFi operation requests by implementing
Irfan Sheriffa2a1b912010-06-07 09:03:04 -070079 * the IWifiManager interface.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080080 *
81 * @hide
82 */
Irfan Sheriffa2a1b912010-06-07 09:03:04 -070083//TODO: Clean up multiple locks and implement WifiService
84// as a SM to track soft AP/client/adhoc bring up based
85// on device idle state, airplane mode and boot.
86
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080087public class WifiService extends IWifiManager.Stub {
88 private static final String TAG = "WifiService";
Dianne Hackborn5fd21692011-06-07 14:09:47 -070089 private static final boolean DBG = false;
Irfan Sheriffa2a1b912010-06-07 09:03:04 -070090
Irfan Sheriff0d255342010-07-28 09:35:20 -070091 private final WifiStateMachine mWifiStateMachine;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080092
93 private Context mContext;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080094
95 private AlarmManager mAlarmManager;
96 private PendingIntent mIdleIntent;
97 private static final int IDLE_REQUEST = 0;
98 private boolean mScreenOff;
99 private boolean mDeviceIdle;
Irfan Sheriff616f3172011-09-11 19:59:01 -0700100 private boolean mEmergencyCallbackMode = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800101 private int mPluggedType;
102
Irfan Sherifffcc08452011-02-17 16:44:54 -0800103 /* Chipset supports background scan */
104 private final boolean mBackgroundScanSupported;
105
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800106 private final LockList mLocks = new LockList();
Eric Shienbrood5711fad2009-03-27 20:25:31 -0700107 // some wifi lock statistics
Irfan Sheriff5876a422010-08-12 20:26:23 -0700108 private int mFullHighPerfLocksAcquired;
109 private int mFullHighPerfLocksReleased;
Eric Shienbrood5711fad2009-03-27 20:25:31 -0700110 private int mFullLocksAcquired;
111 private int mFullLocksReleased;
112 private int mScanLocksAcquired;
113 private int mScanLocksReleased;
The Android Open Source Project10592532009-03-18 17:39:46 -0700114
Robert Greenwalt58ff0212009-05-19 15:53:54 -0700115 private final List<Multicaster> mMulticasters =
116 new ArrayList<Multicaster>();
Robert Greenwalt5347bd42009-05-13 15:10:16 -0700117 private int mMulticastEnabled;
118 private int mMulticastDisabled;
119
The Android Open Source Project10592532009-03-18 17:39:46 -0700120 private final IBatteryStats mBatteryStats;
Jaikumar Ganesh084c6652009-12-07 10:58:18 -0800121
Irfan Sheriff227bec42011-02-15 19:30:27 -0800122 private boolean mEnableTrafficStatsPoll = false;
123 private int mTrafficStatsPollToken = 0;
124 private long mTxPkts;
125 private long mRxPkts;
126 /* Tracks last reported data activity */
127 private int mDataActivity;
128 private String mInterfaceName;
129
130 /**
131 * Interval in milliseconds between polling for traffic
132 * statistics
133 */
134 private static final int POLL_TRAFFIC_STATS_INTERVAL_MSECS = 1000;
135
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800136 /**
Doug Zongker43866e02010-01-07 12:09:54 -0800137 * See {@link Settings.Secure#WIFI_IDLE_MS}. This is the default value if a
138 * Settings.Secure value is not present. This timeout value is chosen as
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800139 * the approximate point at which the battery drain caused by Wi-Fi
140 * being enabled but not active exceeds the battery drain caused by
141 * re-establishing a connection to the mobile data network.
142 */
Irfan Sheriff4f5f7c92010-10-14 17:01:27 -0700143 private static final long DEFAULT_IDLE_MS = 15 * 60 * 1000; /* 15 minutes */
144
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800145 private static final String ACTION_DEVICE_IDLE =
146 "com.android.server.WifiManager.action.DEVICE_IDLE";
147
Irfan Sheriff658772f2011-03-08 14:52:31 -0800148 private static final int WIFI_DISABLED = 0;
149 private static final int WIFI_ENABLED = 1;
150 /* Wifi enabled while in airplane mode */
151 private static final int WIFI_ENABLED_AIRPLANE_OVERRIDE = 2;
Irfan Sheriff31b92e22011-10-03 12:13:20 -0700152 /* Wifi disabled due to airplane mode on */
153 private static final int WIFI_DISABLED_AIRPLANE_ON = 3;
Irfan Sheriff658772f2011-03-08 14:52:31 -0800154
155 private AtomicInteger mWifiState = new AtomicInteger(WIFI_DISABLED);
156 private AtomicBoolean mAirplaneModeOn = new AtomicBoolean(false);
157
Irfan Sheriffa2a1b912010-06-07 09:03:04 -0700158 private boolean mIsReceiverRegistered = false;
159
Irfan Sheriff0d255342010-07-28 09:35:20 -0700160
161 NetworkInfo mNetworkInfo = new NetworkInfo(ConnectivityManager.TYPE_WIFI, 0, "WIFI", "");
162
163 // Variables relating to the 'available networks' notification
164 /**
165 * The icon to show in the 'available networks' notification. This will also
166 * be the ID of the Notification given to the NotificationManager.
167 */
168 private static final int ICON_NETWORKS_AVAILABLE =
169 com.android.internal.R.drawable.stat_notify_wifi_in_range;
170 /**
171 * When a notification is shown, we wait this amount before possibly showing it again.
172 */
173 private final long NOTIFICATION_REPEAT_DELAY_MS;
174 /**
175 * Whether the user has set the setting to show the 'available networks' notification.
176 */
177 private boolean mNotificationEnabled;
178 /**
179 * Observes the user setting to keep {@link #mNotificationEnabled} in sync.
180 */
181 private NotificationEnabledSettingObserver mNotificationEnabledSettingObserver;
182 /**
183 * The {@link System#currentTimeMillis()} must be at least this value for us
184 * to show the notification again.
185 */
186 private long mNotificationRepeatTime;
187 /**
188 * The Notification object given to the NotificationManager.
189 */
190 private Notification mNotification;
191 /**
192 * Whether the notification is being shown, as set by us. That is, if the
193 * user cancels the notification, we will not receive the callback so this
194 * will still be true. We only guarantee if this is false, then the
195 * notification is not showing.
196 */
197 private boolean mNotificationShown;
198 /**
199 * The number of continuous scans that must occur before consider the
200 * supplicant in a scanning state. This allows supplicant to associate with
201 * remembered networks that are in the scan results.
202 */
203 private static final int NUM_SCANS_BEFORE_ACTUALLY_SCANNING = 3;
204 /**
205 * The number of scans since the last network state change. When this
206 * exceeds {@link #NUM_SCANS_BEFORE_ACTUALLY_SCANNING}, we consider the
207 * supplicant to actually be scanning. When the network state changes to
208 * something other than scanning, we reset this to 0.
209 */
210 private int mNumScansSinceNetworkStateChange;
Jaikumar Ganesh7440fc22010-09-27 17:04:14 -0700211
Dianne Hackborn03f3cb02010-09-17 23:12:26 -0700212 /**
Wink Saville4b7ba092010-10-20 15:37:41 -0700213 * Asynchronous channel to WifiStateMachine
214 */
Irfan Sheriff227bec42011-02-15 19:30:27 -0800215 private AsyncChannel mWifiStateMachineChannel;
Wink Saville4b7ba092010-10-20 15:37:41 -0700216
217 /**
Irfan Sheriff227bec42011-02-15 19:30:27 -0800218 * Clients receiving asynchronous messages
Wink Saville4b7ba092010-10-20 15:37:41 -0700219 */
Irfan Sheriff227bec42011-02-15 19:30:27 -0800220 private List<AsyncChannel> mClients = new ArrayList<AsyncChannel>();
Wink Saville4b7ba092010-10-20 15:37:41 -0700221
Irfan Sheriff227bec42011-02-15 19:30:27 -0800222 /**
223 * Handles client connections
224 */
225 private class AsyncServiceHandler extends Handler {
226
227 AsyncServiceHandler(android.os.Looper looper) {
Wink Saville4b7ba092010-10-20 15:37:41 -0700228 super(looper);
Wink Saville4b7ba092010-10-20 15:37:41 -0700229 }
230
231 @Override
232 public void handleMessage(Message msg) {
233 switch (msg.what) {
234 case AsyncChannel.CMD_CHANNEL_HALF_CONNECTED: {
235 if (msg.arg1 == AsyncChannel.STATUS_SUCCESSFUL) {
Irfan Sheriff227bec42011-02-15 19:30:27 -0800236 Slog.d(TAG, "New client listening to asynchronous messages");
237 mClients.add((AsyncChannel) msg.obj);
Wink Saville4b7ba092010-10-20 15:37:41 -0700238 } else {
Irfan Sheriff227bec42011-02-15 19:30:27 -0800239 Slog.e(TAG, "Client connection failure, error=" + msg.arg1);
240 }
241 break;
242 }
Irfan Sheriffc23971b2011-03-04 17:06:31 -0800243 case AsyncChannel.CMD_CHANNEL_DISCONNECTED: {
244 if (msg.arg1 == AsyncChannel.STATUS_SEND_UNSUCCESSFUL) {
245 Slog.d(TAG, "Send failed, client connection lost");
246 } else {
247 Slog.d(TAG, "Client connection lost with reason: " + msg.arg1);
248 }
249 mClients.remove((AsyncChannel) msg.obj);
250 break;
251 }
Irfan Sheriff227bec42011-02-15 19:30:27 -0800252 case AsyncChannel.CMD_CHANNEL_FULL_CONNECTION: {
253 AsyncChannel ac = new AsyncChannel();
254 ac.connect(mContext, this, msg.replyTo);
255 break;
256 }
Irfan Sheriffebe606f2011-02-24 11:39:15 -0800257 case WifiManager.CMD_ENABLE_TRAFFIC_STATS_POLL: {
Irfan Sheriff227bec42011-02-15 19:30:27 -0800258 mEnableTrafficStatsPoll = (msg.arg1 == 1);
259 mTrafficStatsPollToken++;
260 if (mEnableTrafficStatsPoll) {
261 notifyOnDataActivity();
Irfan Sheriffebe606f2011-02-24 11:39:15 -0800262 sendMessageDelayed(Message.obtain(this, WifiManager.CMD_TRAFFIC_STATS_POLL,
Irfan Sheriff227bec42011-02-15 19:30:27 -0800263 mTrafficStatsPollToken, 0), POLL_TRAFFIC_STATS_INTERVAL_MSECS);
264 }
265 break;
266 }
Irfan Sheriffebe606f2011-02-24 11:39:15 -0800267 case WifiManager.CMD_TRAFFIC_STATS_POLL: {
Irfan Sheriff227bec42011-02-15 19:30:27 -0800268 if (msg.arg1 == mTrafficStatsPollToken) {
269 notifyOnDataActivity();
Irfan Sheriffebe606f2011-02-24 11:39:15 -0800270 sendMessageDelayed(Message.obtain(this, WifiManager.CMD_TRAFFIC_STATS_POLL,
Irfan Sheriff227bec42011-02-15 19:30:27 -0800271 mTrafficStatsPollToken, 0), POLL_TRAFFIC_STATS_INTERVAL_MSECS);
Wink Saville4b7ba092010-10-20 15:37:41 -0700272 }
273 break;
274 }
Irfan Sheriffebe606f2011-02-24 11:39:15 -0800275 case WifiManager.CMD_CONNECT_NETWORK: {
276 if (msg.obj != null) {
277 mWifiStateMachine.connectNetwork((WifiConfiguration)msg.obj);
278 } else {
279 mWifiStateMachine.connectNetwork(msg.arg1);
280 }
281 break;
282 }
283 case WifiManager.CMD_SAVE_NETWORK: {
284 mWifiStateMachine.saveNetwork((WifiConfiguration)msg.obj);
285 break;
286 }
287 case WifiManager.CMD_FORGET_NETWORK: {
288 mWifiStateMachine.forgetNetwork(msg.arg1);
289 break;
290 }
291 case WifiManager.CMD_START_WPS: {
292 //replyTo has the original source
Irfan Sheriff651cdfc2011-09-07 00:31:20 -0700293 mWifiStateMachine.startWps(msg.replyTo, (WpsInfo)msg.obj);
Irfan Sheriffebe606f2011-02-24 11:39:15 -0800294 break;
295 }
Isaac Levy8dc6a1b2011-07-27 08:00:03 -0700296 case WifiManager.CMD_DISABLE_NETWORK: {
297 mWifiStateMachine.disableNetwork(msg.replyTo, msg.arg1, msg.arg2);
298 break;
299 }
Wink Saville4b7ba092010-10-20 15:37:41 -0700300 default: {
301 Slog.d(TAG, "WifiServicehandler.handleMessage ignoring msg=" + msg);
302 break;
303 }
304 }
305 }
306 }
Irfan Sheriff227bec42011-02-15 19:30:27 -0800307 private AsyncServiceHandler mAsyncServiceHandler;
308
309 /**
310 * Handles interaction with WifiStateMachine
311 */
312 private class WifiStateMachineHandler extends Handler {
313 private AsyncChannel mWsmChannel;
314
315 WifiStateMachineHandler(android.os.Looper looper) {
316 super(looper);
317 mWsmChannel = new AsyncChannel();
318 mWsmChannel.connect(mContext, this, mWifiStateMachine.getHandler());
319 }
320
321 @Override
322 public void handleMessage(Message msg) {
323 switch (msg.what) {
324 case AsyncChannel.CMD_CHANNEL_HALF_CONNECTED: {
325 if (msg.arg1 == AsyncChannel.STATUS_SUCCESSFUL) {
326 mWifiStateMachineChannel = mWsmChannel;
327 } else {
328 Slog.e(TAG, "WifiStateMachine connection failure, error=" + msg.arg1);
329 mWifiStateMachineChannel = null;
330 }
331 break;
332 }
Irfan Sheriff6da83d52011-06-06 12:54:06 -0700333 case AsyncChannel.CMD_CHANNEL_DISCONNECTED: {
334 Slog.e(TAG, "WifiStateMachine channel lost, msg.arg1 =" + msg.arg1);
335 mWifiStateMachineChannel = null;
336 //Re-establish connection to state machine
337 mWsmChannel.connect(mContext, this, mWifiStateMachine.getHandler());
338 break;
339 }
Irfan Sheriff227bec42011-02-15 19:30:27 -0800340 default: {
341 Slog.d(TAG, "WifiStateMachineHandler.handleMessage ignoring msg=" + msg);
342 break;
343 }
344 }
345 }
346 }
347 WifiStateMachineHandler mWifiStateMachineHandler;
Wink Saville4b7ba092010-10-20 15:37:41 -0700348
349 /**
Dianne Hackborn03f3cb02010-09-17 23:12:26 -0700350 * Temporary for computing UIDS that are responsible for starting WIFI.
351 * Protected by mWifiStateTracker lock.
352 */
353 private final WorkSource mTmpWorkSource = new WorkSource();
Isaac Levy654f5092011-07-13 17:41:45 -0700354 private WifiWatchdogStateMachine mWifiWatchdogStateMachine;
Irfan Sheriff0d255342010-07-28 09:35:20 -0700355
356 WifiService(Context context) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800357 mContext = context;
Irfan Sheriff227bec42011-02-15 19:30:27 -0800358
359 mInterfaceName = SystemProperties.get("wifi.interface", "wlan0");
360
361 mWifiStateMachine = new WifiStateMachine(mContext, mInterfaceName);
Irfan Sheriff0d255342010-07-28 09:35:20 -0700362 mWifiStateMachine.enableRssiPolling(true);
The Android Open Source Project10592532009-03-18 17:39:46 -0700363 mBatteryStats = BatteryStatsService.getService();
Jaikumar Ganesh084c6652009-12-07 10:58:18 -0800364
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800365 mAlarmManager = (AlarmManager)mContext.getSystemService(Context.ALARM_SERVICE);
366 Intent idleIntent = new Intent(ACTION_DEVICE_IDLE, null);
367 mIdleIntent = PendingIntent.getBroadcast(mContext, IDLE_REQUEST, idleIntent, 0);
368
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800369 mContext.registerReceiver(
370 new BroadcastReceiver() {
371 @Override
372 public void onReceive(Context context, Intent intent) {
Irfan Sheriff658772f2011-03-08 14:52:31 -0800373 mAirplaneModeOn.set(isAirplaneModeOn());
374 /* On airplane mode disable, restore wifi state if necessary */
375 if (!mAirplaneModeOn.get() && (testAndClearWifiSavedState() ||
376 mWifiState.get() == WIFI_ENABLED_AIRPLANE_OVERRIDE)) {
377 persistWifiEnabled(true);
Irfan Sheriffb2e6c012010-04-05 11:57:56 -0700378 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800379 updateWifiState();
380 }
381 },
382 new IntentFilter(Intent.ACTION_AIRPLANE_MODE_CHANGED));
383
Irfan Sheriff0d255342010-07-28 09:35:20 -0700384 IntentFilter filter = new IntentFilter();
385 filter.addAction(WifiManager.WIFI_STATE_CHANGED_ACTION);
386 filter.addAction(WifiManager.NETWORK_STATE_CHANGED_ACTION);
387 filter.addAction(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION);
388
389 mContext.registerReceiver(
390 new BroadcastReceiver() {
391 @Override
392 public void onReceive(Context context, Intent intent) {
393 if (intent.getAction().equals(WifiManager.WIFI_STATE_CHANGED_ACTION)) {
394 // reset & clear notification on any wifi state change
395 resetNotification();
396 } else if (intent.getAction().equals(
397 WifiManager.NETWORK_STATE_CHANGED_ACTION)) {
398 mNetworkInfo = (NetworkInfo) intent.getParcelableExtra(
399 WifiManager.EXTRA_NETWORK_INFO);
400 // reset & clear notification on a network connect & disconnect
401 switch(mNetworkInfo.getDetailedState()) {
402 case CONNECTED:
403 case DISCONNECTED:
Irfan Sheriff227bec42011-02-15 19:30:27 -0800404 evaluateTrafficStatsPolling();
Irfan Sheriff0d255342010-07-28 09:35:20 -0700405 resetNotification();
406 break;
407 }
408 } else if (intent.getAction().equals(
409 WifiManager.SCAN_RESULTS_AVAILABLE_ACTION)) {
410 checkAndSetNotification();
411 }
412 }
413 }, filter);
414
Irfan Sheriff227bec42011-02-15 19:30:27 -0800415 HandlerThread wifiThread = new HandlerThread("WifiService");
416 wifiThread.start();
417 mAsyncServiceHandler = new AsyncServiceHandler(wifiThread.getLooper());
418 mWifiStateMachineHandler = new WifiStateMachineHandler(wifiThread.getLooper());
419
Irfan Sheriff0d255342010-07-28 09:35:20 -0700420 // Setting is in seconds
421 NOTIFICATION_REPEAT_DELAY_MS = Settings.Secure.getInt(context.getContentResolver(),
422 Settings.Secure.WIFI_NETWORKS_AVAILABLE_REPEAT_DELAY, 900) * 1000l;
423 mNotificationEnabledSettingObserver = new NotificationEnabledSettingObserver(new Handler());
424 mNotificationEnabledSettingObserver.register();
Irfan Sherifffcc08452011-02-17 16:44:54 -0800425
426 mBackgroundScanSupported = mContext.getResources().getBoolean(
427 com.android.internal.R.bool.config_wifi_background_scan_support);
Irfan Sheriff7b009782010-03-11 16:37:45 -0800428 }
Irfan Sheriff5321aef2010-02-12 12:35:59 -0800429
Irfan Sheriff7b009782010-03-11 16:37:45 -0800430 /**
431 * Check if Wi-Fi needs to be enabled and start
432 * if needed
Irfan Sheriff60e3ba02010-04-02 12:18:45 -0700433 *
434 * This function is used only at boot time
Irfan Sheriff7b009782010-03-11 16:37:45 -0800435 */
Irfan Sheriff0d255342010-07-28 09:35:20 -0700436 public void checkAndStartWifi() {
Irfan Sheriff658772f2011-03-08 14:52:31 -0800437 mAirplaneModeOn.set(isAirplaneModeOn());
438 mWifiState.set(getPersistedWifiState());
439 /* Start if Wi-Fi should be enabled or the saved state indicates Wi-Fi was on */
440 boolean wifiEnabled = shouldWifiBeEnabled() || testAndClearWifiSavedState();
Irfan Sheriff7b009782010-03-11 16:37:45 -0800441 Slog.i(TAG, "WifiService starting up with Wi-Fi " +
442 (wifiEnabled ? "enabled" : "disabled"));
Irfan Sheriffb99fe5e2010-03-26 14:56:07 -0700443 setWifiEnabled(wifiEnabled);
Isaac Levybc7dfb52011-06-06 15:34:01 -0700444
Isaac Levy654f5092011-07-13 17:41:45 -0700445 mWifiWatchdogStateMachine = WifiWatchdogStateMachine.
446 makeWifiWatchdogStateMachine(mContext);
447
Irfan Sheriff5321aef2010-02-12 12:35:59 -0800448 }
449
Irfan Sheriffa3bd4092010-03-24 17:58:59 -0700450 private boolean testAndClearWifiSavedState() {
451 final ContentResolver cr = mContext.getContentResolver();
452 int wifiSavedState = 0;
453 try {
454 wifiSavedState = Settings.Secure.getInt(cr, Settings.Secure.WIFI_SAVED_STATE);
455 if(wifiSavedState == 1)
456 Settings.Secure.putInt(cr, Settings.Secure.WIFI_SAVED_STATE, 0);
457 } catch (Settings.SettingNotFoundException e) {
458 ;
459 }
460 return (wifiSavedState == 1);
461 }
462
Irfan Sheriff658772f2011-03-08 14:52:31 -0800463 private int getPersistedWifiState() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800464 final ContentResolver cr = mContext.getContentResolver();
465 try {
Irfan Sheriff658772f2011-03-08 14:52:31 -0800466 return Settings.Secure.getInt(cr, Settings.Secure.WIFI_ON);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800467 } catch (Settings.SettingNotFoundException e) {
Irfan Sheriff658772f2011-03-08 14:52:31 -0800468 Settings.Secure.putInt(cr, Settings.Secure.WIFI_ON, WIFI_DISABLED);
469 return WIFI_DISABLED;
470 }
471 }
472
473 private boolean shouldWifiBeEnabled() {
474 if (mAirplaneModeOn.get()) {
475 return mWifiState.get() == WIFI_ENABLED_AIRPLANE_OVERRIDE;
476 } else {
477 return mWifiState.get() != WIFI_DISABLED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800478 }
479 }
480
481 private void persistWifiEnabled(boolean enabled) {
482 final ContentResolver cr = mContext.getContentResolver();
Irfan Sheriff31b92e22011-10-03 12:13:20 -0700483 boolean airplane = mAirplaneModeOn.get() && isAirplaneToggleable();
Irfan Sheriff658772f2011-03-08 14:52:31 -0800484 if (enabled) {
Irfan Sheriff31b92e22011-10-03 12:13:20 -0700485 if (airplane) {
Irfan Sheriff658772f2011-03-08 14:52:31 -0800486 mWifiState.set(WIFI_ENABLED_AIRPLANE_OVERRIDE);
487 } else {
488 mWifiState.set(WIFI_ENABLED);
489 }
490 } else {
Irfan Sheriff31b92e22011-10-03 12:13:20 -0700491 if (airplane) {
492 mWifiState.set(WIFI_DISABLED_AIRPLANE_ON);
493 } else {
494 mWifiState.set(WIFI_DISABLED);
495 }
Irfan Sheriff658772f2011-03-08 14:52:31 -0800496 }
497 Settings.Secure.putInt(cr, Settings.Secure.WIFI_ON, mWifiState.get());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800498 }
499
Irfan Sheriff658772f2011-03-08 14:52:31 -0800500
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800501 /**
502 * see {@link android.net.wifi.WifiManager#pingSupplicant()}
Irfan Sheriffa2a1b912010-06-07 09:03:04 -0700503 * @return {@code true} if the operation succeeds, {@code false} otherwise
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800504 */
505 public boolean pingSupplicant() {
Irfan Sheriffa2a1b912010-06-07 09:03:04 -0700506 enforceAccessPermission();
Irfan Sheriff227bec42011-02-15 19:30:27 -0800507 if (mWifiStateMachineChannel != null) {
508 return mWifiStateMachine.syncPingSupplicant(mWifiStateMachineChannel);
Irfan Sheriff1406bcb2010-10-28 14:41:39 -0700509 } else {
Irfan Sheriff227bec42011-02-15 19:30:27 -0800510 Slog.e(TAG, "mWifiStateMachineChannel is not initialized");
Irfan Sheriff1406bcb2010-10-28 14:41:39 -0700511 return false;
512 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800513 }
514
515 /**
516 * see {@link android.net.wifi.WifiManager#startScan()}
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800517 */
Irfan Sheriffe4984752010-08-19 11:29:22 -0700518 public void startScan(boolean forceActive) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800519 enforceChangePermission();
Irfan Sheriffe4984752010-08-19 11:29:22 -0700520 mWifiStateMachine.startScan(forceActive);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800521 }
522
523 private void enforceAccessPermission() {
524 mContext.enforceCallingOrSelfPermission(android.Manifest.permission.ACCESS_WIFI_STATE,
525 "WifiService");
526 }
527
528 private void enforceChangePermission() {
529 mContext.enforceCallingOrSelfPermission(android.Manifest.permission.CHANGE_WIFI_STATE,
530 "WifiService");
531
532 }
533
Robert Greenwaltfc1b15c2009-05-22 15:09:51 -0700534 private void enforceMulticastChangePermission() {
535 mContext.enforceCallingOrSelfPermission(
536 android.Manifest.permission.CHANGE_WIFI_MULTICAST_STATE,
537 "WifiService");
538 }
539
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800540 /**
Irfan Sheriffa2a1b912010-06-07 09:03:04 -0700541 * see {@link android.net.wifi.WifiManager#setWifiEnabled(boolean)}
542 * @param enable {@code true} to enable, {@code false} to disable.
543 * @return {@code true} if the enable/disable operation was
544 * started or is already in the queue.
545 */
546 public synchronized boolean setWifiEnabled(boolean enable) {
547 enforceChangePermission();
548
549 if (DBG) {
Irfan Sheriff0d255342010-07-28 09:35:20 -0700550 Slog.e(TAG, "Invoking mWifiStateMachine.setWifiEnabled\n");
Irfan Sheriffa2a1b912010-06-07 09:03:04 -0700551 }
552
Dianne Hackborn03f3cb02010-09-17 23:12:26 -0700553 if (enable) {
554 reportStartWorkSource();
555 }
Irfan Sheriff0d255342010-07-28 09:35:20 -0700556 mWifiStateMachine.setWifiEnabled(enable);
Irfan Sheriff61180692010-08-18 16:07:39 -0700557
558 /*
559 * Caller might not have WRITE_SECURE_SETTINGS,
560 * only CHANGE_WIFI_STATE is enforced
561 */
562 long ident = Binder.clearCallingIdentity();
Irfan Sheriffa2a1b912010-06-07 09:03:04 -0700563 persistWifiEnabled(enable);
Irfan Sheriff61180692010-08-18 16:07:39 -0700564 Binder.restoreCallingIdentity(ident);
Irfan Sheriffa2a1b912010-06-07 09:03:04 -0700565
566 if (enable) {
567 if (!mIsReceiverRegistered) {
568 registerForBroadcasts();
569 mIsReceiverRegistered = true;
570 }
571 } else if (mIsReceiverRegistered){
572 mContext.unregisterReceiver(mReceiver);
573 mIsReceiverRegistered = false;
574 }
575
576 return true;
577 }
578
579 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800580 * see {@link WifiManager#getWifiState()}
581 * @return One of {@link WifiManager#WIFI_STATE_DISABLED},
582 * {@link WifiManager#WIFI_STATE_DISABLING},
583 * {@link WifiManager#WIFI_STATE_ENABLED},
584 * {@link WifiManager#WIFI_STATE_ENABLING},
585 * {@link WifiManager#WIFI_STATE_UNKNOWN}
586 */
587 public int getWifiEnabledState() {
588 enforceAccessPermission();
Irfan Sheriffd8134ff2010-08-22 17:06:34 -0700589 return mWifiStateMachine.syncGetWifiState();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800590 }
591
592 /**
Irfan Sheriffc2f54c22010-03-18 14:02:22 -0700593 * see {@link android.net.wifi.WifiManager#setWifiApEnabled(WifiConfiguration, boolean)}
Irfan Sheriff5321aef2010-02-12 12:35:59 -0800594 * @param wifiConfig SSID, security and channel details as
595 * part of WifiConfiguration
Irfan Sheriffa2a1b912010-06-07 09:03:04 -0700596 * @param enabled true to enable and false to disable
Irfan Sheriff5321aef2010-02-12 12:35:59 -0800597 */
Irfan Sheriffffcea7a2011-05-10 16:26:06 -0700598 public void setWifiApEnabled(WifiConfiguration wifiConfig, boolean enabled) {
Irfan Sheriff5321aef2010-02-12 12:35:59 -0800599 enforceChangePermission();
Irfan Sheriff0d255342010-07-28 09:35:20 -0700600 mWifiStateMachine.setWifiApEnabled(wifiConfig, enabled);
Irfan Sheriff5321aef2010-02-12 12:35:59 -0800601 }
602
Irfan Sheriffa2a1b912010-06-07 09:03:04 -0700603 /**
604 * see {@link WifiManager#getWifiApState()}
605 * @return One of {@link WifiManager#WIFI_AP_STATE_DISABLED},
606 * {@link WifiManager#WIFI_AP_STATE_DISABLING},
607 * {@link WifiManager#WIFI_AP_STATE_ENABLED},
608 * {@link WifiManager#WIFI_AP_STATE_ENABLING},
609 * {@link WifiManager#WIFI_AP_STATE_FAILED}
610 */
611 public int getWifiApEnabledState() {
Irfan Sheriff17b232b2010-06-24 11:32:26 -0700612 enforceAccessPermission();
Irfan Sheriffd8134ff2010-08-22 17:06:34 -0700613 return mWifiStateMachine.syncGetWifiApState();
Irfan Sheriffa2a1b912010-06-07 09:03:04 -0700614 }
615
616 /**
617 * see {@link WifiManager#getWifiApConfiguration()}
618 * @return soft access point configuration
619 */
Irfan Sheriffffcea7a2011-05-10 16:26:06 -0700620 public WifiConfiguration getWifiApConfiguration() {
621 enforceAccessPermission();
Irfan Sheriff9575a1b2011-11-07 10:34:54 -0800622 return mWifiStateMachine.syncGetWifiApConfiguration();
Irfan Sheriff9ab518ad2010-03-12 15:48:17 -0800623 }
624
Irfan Sheriffa2a1b912010-06-07 09:03:04 -0700625 /**
626 * see {@link WifiManager#setWifiApConfiguration(WifiConfiguration)}
627 * @param wifiConfig WifiConfiguration details for soft access point
628 */
Irfan Sheriffffcea7a2011-05-10 16:26:06 -0700629 public void setWifiApConfiguration(WifiConfiguration wifiConfig) {
Irfan Sheriff17b232b2010-06-24 11:32:26 -0700630 enforceChangePermission();
Irfan Sheriff9ab518ad2010-03-12 15:48:17 -0800631 if (wifiConfig == null)
632 return;
Irfan Sheriffffcea7a2011-05-10 16:26:06 -0700633 mWifiStateMachine.setWifiApConfiguration(wifiConfig);
Irfan Sheriff9ab518ad2010-03-12 15:48:17 -0800634 }
635
Irfan Sheriff5321aef2010-02-12 12:35:59 -0800636 /**
Irfan Sheriffa2a1b912010-06-07 09:03:04 -0700637 * see {@link android.net.wifi.WifiManager#disconnect()}
Irfan Sheriff5321aef2010-02-12 12:35:59 -0800638 */
Irfan Sheriffe4984752010-08-19 11:29:22 -0700639 public void disconnect() {
Irfan Sheriffa2a1b912010-06-07 09:03:04 -0700640 enforceChangePermission();
Irfan Sheriffe4984752010-08-19 11:29:22 -0700641 mWifiStateMachine.disconnectCommand();
Irfan Sheriff5321aef2010-02-12 12:35:59 -0800642 }
643
644 /**
Irfan Sheriffa2a1b912010-06-07 09:03:04 -0700645 * see {@link android.net.wifi.WifiManager#reconnect()}
Irfan Sheriff5321aef2010-02-12 12:35:59 -0800646 */
Irfan Sheriffe4984752010-08-19 11:29:22 -0700647 public void reconnect() {
Irfan Sheriffa2a1b912010-06-07 09:03:04 -0700648 enforceChangePermission();
Irfan Sheriffe4984752010-08-19 11:29:22 -0700649 mWifiStateMachine.reconnectCommand();
Irfan Sheriff5321aef2010-02-12 12:35:59 -0800650 }
651
Irfan Sheriffa2a1b912010-06-07 09:03:04 -0700652 /**
653 * see {@link android.net.wifi.WifiManager#reassociate()}
Irfan Sheriffa2a1b912010-06-07 09:03:04 -0700654 */
Irfan Sheriffe4984752010-08-19 11:29:22 -0700655 public void reassociate() {
Irfan Sheriffa2a1b912010-06-07 09:03:04 -0700656 enforceChangePermission();
Irfan Sheriffe4984752010-08-19 11:29:22 -0700657 mWifiStateMachine.reassociateCommand();
Irfan Sheriff5321aef2010-02-12 12:35:59 -0800658 }
659
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800660 /**
661 * see {@link android.net.wifi.WifiManager#getConfiguredNetworks()}
662 * @return the list of configured networks
663 */
664 public List<WifiConfiguration> getConfiguredNetworks() {
665 enforceAccessPermission();
Irfan Sheriffd8134ff2010-08-22 17:06:34 -0700666 return mWifiStateMachine.syncGetConfiguredNetworks();
Chung-yih Wanga8d15942009-10-09 11:01:49 +0800667 }
668
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800669 /**
670 * see {@link android.net.wifi.WifiManager#addOrUpdateNetwork(WifiConfiguration)}
671 * @return the supplicant-assigned identifier for the new or updated
672 * network if the operation succeeds, or {@code -1} if it fails
673 */
Irfan Sheriff7aac5542009-12-22 21:42:17 -0800674 public int addOrUpdateNetwork(WifiConfiguration config) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800675 enforceChangePermission();
Irfan Sheriff227bec42011-02-15 19:30:27 -0800676 if (mWifiStateMachineChannel != null) {
677 return mWifiStateMachine.syncAddOrUpdateNetwork(mWifiStateMachineChannel, config);
Irfan Sheriff1406bcb2010-10-28 14:41:39 -0700678 } else {
Irfan Sheriff227bec42011-02-15 19:30:27 -0800679 Slog.e(TAG, "mWifiStateMachineChannel is not initialized");
Irfan Sheriff1406bcb2010-10-28 14:41:39 -0700680 return -1;
681 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800682 }
683
Irfan Sheriffa2a1b912010-06-07 09:03:04 -0700684 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800685 * See {@link android.net.wifi.WifiManager#removeNetwork(int)}
686 * @param netId the integer that identifies the network configuration
687 * to the supplicant
688 * @return {@code true} if the operation succeeded
689 */
690 public boolean removeNetwork(int netId) {
691 enforceChangePermission();
Irfan Sheriff227bec42011-02-15 19:30:27 -0800692 if (mWifiStateMachineChannel != null) {
693 return mWifiStateMachine.syncRemoveNetwork(mWifiStateMachineChannel, netId);
Wink Saville4b7ba092010-10-20 15:37:41 -0700694 } else {
Irfan Sheriff227bec42011-02-15 19:30:27 -0800695 Slog.e(TAG, "mWifiStateMachineChannel is not initialized");
Wink Saville4b7ba092010-10-20 15:37:41 -0700696 return false;
697 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800698 }
699
700 /**
701 * See {@link android.net.wifi.WifiManager#enableNetwork(int, boolean)}
702 * @param netId the integer that identifies the network configuration
703 * to the supplicant
704 * @param disableOthers if true, disable all other networks.
705 * @return {@code true} if the operation succeeded
706 */
707 public boolean enableNetwork(int netId, boolean disableOthers) {
708 enforceChangePermission();
Irfan Sheriff227bec42011-02-15 19:30:27 -0800709 if (mWifiStateMachineChannel != null) {
710 return mWifiStateMachine.syncEnableNetwork(mWifiStateMachineChannel, netId,
711 disableOthers);
Irfan Sheriff1406bcb2010-10-28 14:41:39 -0700712 } else {
Irfan Sheriff227bec42011-02-15 19:30:27 -0800713 Slog.e(TAG, "mWifiStateMachineChannel is not initialized");
Irfan Sheriff1406bcb2010-10-28 14:41:39 -0700714 return false;
715 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800716 }
717
718 /**
719 * See {@link android.net.wifi.WifiManager#disableNetwork(int)}
720 * @param netId the integer that identifies the network configuration
721 * to the supplicant
722 * @return {@code true} if the operation succeeded
723 */
724 public boolean disableNetwork(int netId) {
725 enforceChangePermission();
Irfan Sheriff227bec42011-02-15 19:30:27 -0800726 if (mWifiStateMachineChannel != null) {
727 return mWifiStateMachine.syncDisableNetwork(mWifiStateMachineChannel, netId);
Irfan Sheriff1406bcb2010-10-28 14:41:39 -0700728 } else {
Irfan Sheriff227bec42011-02-15 19:30:27 -0800729 Slog.e(TAG, "mWifiStateMachineChannel is not initialized");
Irfan Sheriff1406bcb2010-10-28 14:41:39 -0700730 return false;
731 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800732 }
733
734 /**
735 * See {@link android.net.wifi.WifiManager#getConnectionInfo()}
736 * @return the Wi-Fi information, contained in {@link WifiInfo}.
737 */
738 public WifiInfo getConnectionInfo() {
739 enforceAccessPermission();
740 /*
741 * Make sure we have the latest information, by sending
742 * a status request to the supplicant.
743 */
Irfan Sheriffd8134ff2010-08-22 17:06:34 -0700744 return mWifiStateMachine.syncRequestConnectionInfo();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800745 }
746
747 /**
748 * Return the results of the most recent access point scan, in the form of
749 * a list of {@link ScanResult} objects.
750 * @return the list of results
751 */
752 public List<ScanResult> getScanResults() {
753 enforceAccessPermission();
Irfan Sheriffd8134ff2010-08-22 17:06:34 -0700754 return mWifiStateMachine.syncGetScanResultsList();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800755 }
756
757 /**
758 * Tell the supplicant to persist the current list of configured networks.
759 * @return {@code true} if the operation succeeded
Irfan Sheriffa2a1b912010-06-07 09:03:04 -0700760 *
761 * TODO: deprecate this
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800762 */
763 public boolean saveConfiguration() {
Irfan Sheriffa2a1b912010-06-07 09:03:04 -0700764 boolean result = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800765 enforceChangePermission();
Irfan Sheriff227bec42011-02-15 19:30:27 -0800766 if (mWifiStateMachineChannel != null) {
767 return mWifiStateMachine.syncSaveConfig(mWifiStateMachineChannel);
Irfan Sheriff1406bcb2010-10-28 14:41:39 -0700768 } else {
Irfan Sheriff227bec42011-02-15 19:30:27 -0800769 Slog.e(TAG, "mWifiStateMachineChannel is not initialized");
Irfan Sheriff1406bcb2010-10-28 14:41:39 -0700770 return false;
771 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800772 }
773
774 /**
Irfan Sheriffed4f28b2010-10-29 15:32:10 -0700775 * Set the country code
776 * @param countryCode ISO 3166 country code.
Robert Greenwaltb5010cc2009-05-21 15:11:40 -0700777 * @param persist {@code true} if the setting should be remembered.
Irfan Sheriffed4f28b2010-10-29 15:32:10 -0700778 *
779 * The persist behavior exists so that wifi can fall back to the last
780 * persisted country code on a restart, when the locale information is
781 * not available from telephony.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800782 */
Irfan Sheriffed4f28b2010-10-29 15:32:10 -0700783 public void setCountryCode(String countryCode, boolean persist) {
784 Slog.i(TAG, "WifiService trying to set country code to " + countryCode +
785 " with persist set to " + persist);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800786 enforceChangePermission();
Irfan Sheriffed4f28b2010-10-29 15:32:10 -0700787 mWifiStateMachine.setCountryCode(countryCode, persist);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800788 }
789
790 /**
Irfan Sheriff36f74132010-11-04 16:57:37 -0700791 * Set the operational frequency band
792 * @param band One of
793 * {@link WifiManager#WIFI_FREQUENCY_BAND_AUTO},
794 * {@link WifiManager#WIFI_FREQUENCY_BAND_5GHZ},
795 * {@link WifiManager#WIFI_FREQUENCY_BAND_2GHZ},
796 * @param persist {@code true} if the setting should be remembered.
797 *
798 */
799 public void setFrequencyBand(int band, boolean persist) {
800 enforceChangePermission();
801 if (!isDualBandSupported()) return;
802 Slog.i(TAG, "WifiService trying to set frequency band to " + band +
803 " with persist set to " + persist);
804 mWifiStateMachine.setFrequencyBand(band, persist);
805 }
806
807
808 /**
809 * Get the operational frequency band
810 */
811 public int getFrequencyBand() {
812 enforceAccessPermission();
813 return mWifiStateMachine.getFrequencyBand();
814 }
815
816 public boolean isDualBandSupported() {
817 //TODO: Should move towards adding a driver API that checks at runtime
818 return mContext.getResources().getBoolean(
819 com.android.internal.R.bool.config_wifi_dual_band_support);
820 }
821
822 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800823 * Return the DHCP-assigned addresses from the last successful DHCP request,
824 * if any.
825 * @return the DHCP information
826 */
827 public DhcpInfo getDhcpInfo() {
828 enforceAccessPermission();
Irfan Sheriffd8134ff2010-08-22 17:06:34 -0700829 return mWifiStateMachine.syncGetDhcpInfo();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800830 }
831
Irfan Sheriff0d255342010-07-28 09:35:20 -0700832 /**
833 * see {@link android.net.wifi.WifiManager#startWifi}
834 *
835 */
836 public void startWifi() {
837 enforceChangePermission();
838 /* TODO: may be add permissions for access only to connectivity service
839 * TODO: if a start issued, keep wifi alive until a stop issued irrespective
840 * of WifiLock & device idle status unless wifi enabled status is toggled
841 */
842
843 mWifiStateMachine.setDriverStart(true);
844 mWifiStateMachine.reconnectCommand();
845 }
846
847 /**
848 * see {@link android.net.wifi.WifiManager#stopWifi}
849 *
850 */
851 public void stopWifi() {
852 enforceChangePermission();
853 /* TODO: may be add permissions for access only to connectivity service
854 * TODO: if a stop is issued, wifi is brought up only by startWifi
855 * unless wifi enabled status is toggled
856 */
857 mWifiStateMachine.setDriverStart(false);
858 }
859
860
861 /**
862 * see {@link android.net.wifi.WifiManager#addToBlacklist}
863 *
864 */
865 public void addToBlacklist(String bssid) {
866 enforceChangePermission();
867
868 mWifiStateMachine.addToBlacklist(bssid);
869 }
870
871 /**
872 * see {@link android.net.wifi.WifiManager#clearBlacklist}
873 *
874 */
875 public void clearBlacklist() {
876 enforceChangePermission();
877
878 mWifiStateMachine.clearBlacklist();
879 }
880
Irfan Sheriff227bec42011-02-15 19:30:27 -0800881 /**
882 * Get a reference to handler. This is used by a client to establish
883 * an AsyncChannel communication with WifiService
884 */
885 public Messenger getMessenger() {
Irfan Sheriffebe606f2011-02-24 11:39:15 -0800886 /* Enforce the highest permissions
887 TODO: when we consider exposing the asynchronous API, think about
888 how to provide both access and change permissions seperately
889 */
Irfan Sheriff227bec42011-02-15 19:30:27 -0800890 enforceAccessPermission();
Irfan Sheriffebe606f2011-02-24 11:39:15 -0800891 enforceChangePermission();
Irfan Sheriff227bec42011-02-15 19:30:27 -0800892 return new Messenger(mAsyncServiceHandler);
893 }
894
Irfan Sheriff4aeca7c52011-03-10 16:53:33 -0800895 /**
896 * Get the IP and proxy configuration file
897 */
898 public String getConfigFile() {
899 enforceAccessPermission();
900 return mWifiStateMachine.getConfigFile();
901 }
902
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800903 private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
904 @Override
905 public void onReceive(Context context, Intent intent) {
906 String action = intent.getAction();
907
Doug Zongker43866e02010-01-07 12:09:54 -0800908 long idleMillis =
909 Settings.Secure.getLong(mContext.getContentResolver(),
Irfan Sheriff4f5f7c92010-10-14 17:01:27 -0700910 Settings.Secure.WIFI_IDLE_MS, DEFAULT_IDLE_MS);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800911 int stayAwakeConditions =
Doug Zongker43866e02010-01-07 12:09:54 -0800912 Settings.System.getInt(mContext.getContentResolver(),
913 Settings.System.STAY_ON_WHILE_PLUGGED_IN, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800914 if (action.equals(Intent.ACTION_SCREEN_ON)) {
Joe Onorato431bb222010-10-18 19:13:23 -0400915 if (DBG) {
916 Slog.d(TAG, "ACTION_SCREEN_ON");
917 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800918 mAlarmManager.cancel(mIdleIntent);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800919 mScreenOff = false;
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 Sheriffe6daca52011-11-03 15:46:50 -0700926 setDeviceIdleAndUpdateWifi(false);
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 Sheriffe6daca52011-11-03 15:46:50 -0700944 //Delayed shutdown if wifi is connected
945 if (mNetworkInfo.getDetailedState() == DetailedState.CONNECTED) {
946 if (DBG) Slog.d(TAG, "setting ACTION_DEVICE_IDLE: " + idleMillis + " ms");
947 mAlarmManager.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis()
948 + idleMillis, mIdleIntent);
Mike Lockwoodd9c32bc2009-05-18 14:14:15 -0400949 } else {
Irfan Sheriffe6daca52011-11-03 15:46:50 -0700950 setDeviceIdleAndUpdateWifi(true);
Mike Lockwoodd9c32bc2009-05-18 14:14:15 -0400951 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800952 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800953 } else if (action.equals(ACTION_DEVICE_IDLE)) {
Irfan Sheriffe6daca52011-11-03 15:46:50 -0700954 setDeviceIdleAndUpdateWifi(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800955 } else if (action.equals(Intent.ACTION_BATTERY_CHANGED)) {
956 /*
957 * Set a timer to put Wi-Fi to sleep, but only if the screen is off
958 * AND we are transitioning from a state in which the device was supposed
959 * to stay awake to a state in which it is not supposed to stay awake.
960 * If "stay awake" state is not changing, we do nothing, to avoid resetting
961 * the already-set timer.
962 */
963 int pluggedType = intent.getIntExtra("plugged", 0);
Joe Onorato431bb222010-10-18 19:13:23 -0400964 if (DBG) {
965 Slog.d(TAG, "ACTION_BATTERY_CHANGED pluggedType: " + pluggedType);
966 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800967 if (mScreenOff && shouldWifiStayAwake(stayAwakeConditions, mPluggedType) &&
968 !shouldWifiStayAwake(stayAwakeConditions, pluggedType)) {
969 long triggerTime = System.currentTimeMillis() + idleMillis;
Joe Onorato431bb222010-10-18 19:13:23 -0400970 if (DBG) {
971 Slog.d(TAG, "setting ACTION_DEVICE_IDLE timer for " + idleMillis + "ms");
972 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800973 mAlarmManager.set(AlarmManager.RTC_WAKEUP, triggerTime, mIdleIntent);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800974 }
975 mPluggedType = pluggedType;
Irfan Sheriff65eaec82011-01-05 22:00:16 -0800976 } else if (action.equals(BluetoothAdapter.ACTION_CONNECTION_STATE_CHANGED)) {
977 int state = intent.getIntExtra(BluetoothAdapter.EXTRA_CONNECTION_STATE,
978 BluetoothAdapter.STATE_DISCONNECTED);
979 mWifiStateMachine.sendBluetoothAdapterStateChange(state);
Irfan Sheriff616f3172011-09-11 19:59:01 -0700980 } else if (action.equals(TelephonyIntents.ACTION_EMERGENCY_CALLBACK_MODE_CHANGED)) {
981 mEmergencyCallbackMode = intent.getBooleanExtra("phoneinECMState", false);
982 updateWifiState();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800983 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800984 }
985
986 /**
987 * Determines whether the Wi-Fi chipset should stay awake or be put to
988 * sleep. Looks at the setting for the sleep policy and the current
989 * conditions.
Jaikumar Ganesh084c6652009-12-07 10:58:18 -0800990 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800991 * @see #shouldDeviceStayAwake(int, int)
992 */
993 private boolean shouldWifiStayAwake(int stayAwakeConditions, int pluggedType) {
Irfan Sheriff739f6bc2011-01-28 16:43:12 -0800994 //Never sleep as long as the user has not changed the settings
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800995 int wifiSleepPolicy = Settings.System.getInt(mContext.getContentResolver(),
Irfan Sheriff96b10d62011-01-11 15:40:35 -0800996 Settings.System.WIFI_SLEEP_POLICY,
Irfan Sheriff739f6bc2011-01-28 16:43:12 -0800997 Settings.System.WIFI_SLEEP_POLICY_NEVER);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800998
999 if (wifiSleepPolicy == Settings.System.WIFI_SLEEP_POLICY_NEVER) {
1000 // Never sleep
1001 return true;
1002 } else if ((wifiSleepPolicy == Settings.System.WIFI_SLEEP_POLICY_NEVER_WHILE_PLUGGED) &&
1003 (pluggedType != 0)) {
1004 // Never sleep while plugged, and we're plugged
1005 return true;
1006 } else {
1007 // Default
1008 return shouldDeviceStayAwake(stayAwakeConditions, pluggedType);
1009 }
1010 }
Jaikumar Ganesh084c6652009-12-07 10:58:18 -08001011
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001012 /**
1013 * Determine whether the bit value corresponding to {@code pluggedType} is set in
1014 * the bit string {@code stayAwakeConditions}. Because a {@code pluggedType} value
1015 * of {@code 0} isn't really a plugged type, but rather an indication that the
1016 * device isn't plugged in at all, there is no bit value corresponding to a
1017 * {@code pluggedType} value of {@code 0}. That is why we shift by
Ben Dodson4e8620f2010-08-25 10:55:47 -07001018 * {@code pluggedType - 1} instead of by {@code pluggedType}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001019 * @param stayAwakeConditions a bit string specifying which "plugged types" should
1020 * keep the device (and hence Wi-Fi) awake.
1021 * @param pluggedType the type of plug (USB, AC, or none) for which the check is
1022 * being made
1023 * @return {@code true} if {@code pluggedType} indicates that the device is
1024 * supposed to stay awake, {@code false} otherwise.
1025 */
1026 private boolean shouldDeviceStayAwake(int stayAwakeConditions, int pluggedType) {
1027 return (stayAwakeConditions & pluggedType) != 0;
1028 }
1029 };
1030
Irfan Sheriffe6daca52011-11-03 15:46:50 -07001031 private void setDeviceIdleAndUpdateWifi(boolean deviceIdle) {
1032 mDeviceIdle = deviceIdle;
1033 reportStartWorkSource();
1034 updateWifiState();
1035 }
1036
Dianne Hackborn03f3cb02010-09-17 23:12:26 -07001037 private synchronized void reportStartWorkSource() {
1038 mTmpWorkSource.clear();
1039 if (mDeviceIdle) {
1040 for (int i=0; i<mLocks.mList.size(); i++) {
1041 mTmpWorkSource.add(mLocks.mList.get(i).mWorkSource);
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07001042 }
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07001043 }
Dianne Hackborn03f3cb02010-09-17 23:12:26 -07001044 mWifiStateMachine.updateBatteryWorkSource(mTmpWorkSource);
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07001045 }
Jaikumar Ganesh7440fc22010-09-27 17:04:14 -07001046
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001047 private void updateWifiState() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001048 boolean lockHeld = mLocks.hasLocks();
Irfan Sheriff5876a422010-08-12 20:26:23 -07001049 int strongestLockMode = WifiManager.WIFI_MODE_FULL;
Irfan Sheriff616f3172011-09-11 19:59:01 -07001050 boolean wifiShouldBeStarted;
1051
1052 if (mEmergencyCallbackMode) {
1053 wifiShouldBeStarted = false;
1054 } else {
1055 wifiShouldBeStarted = !mDeviceIdle || lockHeld;
1056 }
Irfan Sheriff5876a422010-08-12 20:26:23 -07001057
1058 if (lockHeld) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001059 strongestLockMode = mLocks.getStrongestLockMode();
Irfan Sheriff5876a422010-08-12 20:26:23 -07001060 }
1061 /* If device is not idle, lockmode cannot be scan only */
1062 if (!mDeviceIdle && strongestLockMode == WifiManager.WIFI_MODE_SCAN_ONLY) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001063 strongestLockMode = WifiManager.WIFI_MODE_FULL;
1064 }
1065
Irfan Sheriffa2a1b912010-06-07 09:03:04 -07001066 /* Disable tethering when airplane mode is enabled */
Irfan Sheriff658772f2011-03-08 14:52:31 -08001067 if (mAirplaneModeOn.get()) {
Irfan Sheriff0d255342010-07-28 09:35:20 -07001068 mWifiStateMachine.setWifiApEnabled(null, false);
Irfan Sheriffa2a1b912010-06-07 09:03:04 -07001069 }
Irfan Sheriffb2e6c012010-04-05 11:57:56 -07001070
Irfan Sheriff658772f2011-03-08 14:52:31 -08001071 if (shouldWifiBeEnabled()) {
Irfan Sheriffa2a1b912010-06-07 09:03:04 -07001072 if (wifiShouldBeStarted) {
Dianne Hackborn03f3cb02010-09-17 23:12:26 -07001073 reportStartWorkSource();
Irfan Sheriff0d255342010-07-28 09:35:20 -07001074 mWifiStateMachine.setWifiEnabled(true);
1075 mWifiStateMachine.setScanOnlyMode(
Irfan Sheriffa2a1b912010-06-07 09:03:04 -07001076 strongestLockMode == WifiManager.WIFI_MODE_SCAN_ONLY);
Irfan Sheriff0d255342010-07-28 09:35:20 -07001077 mWifiStateMachine.setDriverStart(true);
Irfan Sheriff5876a422010-08-12 20:26:23 -07001078 mWifiStateMachine.setHighPerfModeEnabled(strongestLockMode
1079 == WifiManager.WIFI_MODE_FULL_HIGH_PERF);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001080 } else {
Irfan Sheriff0d255342010-07-28 09:35:20 -07001081 mWifiStateMachine.requestCmWakeLock();
1082 mWifiStateMachine.setDriverStart(false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001083 }
Irfan Sheriffa2a1b912010-06-07 09:03:04 -07001084 } else {
Irfan Sheriff0d255342010-07-28 09:35:20 -07001085 mWifiStateMachine.setWifiEnabled(false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001086 }
1087 }
1088
1089 private void registerForBroadcasts() {
1090 IntentFilter intentFilter = new IntentFilter();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001091 intentFilter.addAction(Intent.ACTION_SCREEN_ON);
1092 intentFilter.addAction(Intent.ACTION_SCREEN_OFF);
1093 intentFilter.addAction(Intent.ACTION_BATTERY_CHANGED);
1094 intentFilter.addAction(ACTION_DEVICE_IDLE);
Irfan Sheriff65eaec82011-01-05 22:00:16 -08001095 intentFilter.addAction(BluetoothAdapter.ACTION_CONNECTION_STATE_CHANGED);
Irfan Sheriff616f3172011-09-11 19:59:01 -07001096 intentFilter.addAction(TelephonyIntents.ACTION_EMERGENCY_CALLBACK_MODE_CHANGED);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001097 mContext.registerReceiver(mReceiver, intentFilter);
1098 }
Jaikumar Ganesh084c6652009-12-07 10:58:18 -08001099
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001100 private boolean isAirplaneSensitive() {
1101 String airplaneModeRadios = Settings.System.getString(mContext.getContentResolver(),
1102 Settings.System.AIRPLANE_MODE_RADIOS);
1103 return airplaneModeRadios == null
1104 || airplaneModeRadios.contains(Settings.System.RADIO_WIFI);
1105 }
1106
Mike Lockwoodbd5ddf02009-07-29 21:37:14 -07001107 private boolean isAirplaneToggleable() {
1108 String toggleableRadios = Settings.System.getString(mContext.getContentResolver(),
1109 Settings.System.AIRPLANE_MODE_TOGGLEABLE_RADIOS);
1110 return toggleableRadios != null
1111 && toggleableRadios.contains(Settings.System.RADIO_WIFI);
1112 }
1113
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001114 /**
1115 * Returns true if Wi-Fi is sensitive to airplane mode, and airplane mode is
1116 * currently on.
1117 * @return {@code true} if airplane mode is on.
1118 */
1119 private boolean isAirplaneModeOn() {
1120 return isAirplaneSensitive() && Settings.System.getInt(mContext.getContentResolver(),
1121 Settings.System.AIRPLANE_MODE_ON, 0) == 1;
1122 }
1123
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001124 @Override
1125 protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
1126 if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.DUMP)
1127 != PackageManager.PERMISSION_GRANTED) {
1128 pw.println("Permission Denial: can't dump WifiService from from pid="
1129 + Binder.getCallingPid()
1130 + ", uid=" + Binder.getCallingUid());
1131 return;
1132 }
Irfan Sheriffd8134ff2010-08-22 17:06:34 -07001133 pw.println("Wi-Fi is " + mWifiStateMachine.syncGetWifiStateByName());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001134 pw.println("Stay-awake conditions: " +
1135 Settings.System.getInt(mContext.getContentResolver(),
1136 Settings.System.STAY_ON_WHILE_PLUGGED_IN, 0));
1137 pw.println();
1138
1139 pw.println("Internal state:");
Irfan Sheriff0d255342010-07-28 09:35:20 -07001140 pw.println(mWifiStateMachine);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001141 pw.println();
1142 pw.println("Latest scan results:");
Irfan Sheriffd8134ff2010-08-22 17:06:34 -07001143 List<ScanResult> scanResults = mWifiStateMachine.syncGetScanResultsList();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001144 if (scanResults != null && scanResults.size() != 0) {
1145 pw.println(" BSSID Frequency RSSI Flags SSID");
1146 for (ScanResult r : scanResults) {
1147 pw.printf(" %17s %9d %5d %-16s %s%n",
1148 r.BSSID,
1149 r.frequency,
1150 r.level,
1151 r.capabilities,
1152 r.SSID == null ? "" : r.SSID);
1153 }
1154 }
1155 pw.println();
Eric Shienbrood5711fad2009-03-27 20:25:31 -07001156 pw.println("Locks acquired: " + mFullLocksAcquired + " full, " +
Irfan Sheriff5876a422010-08-12 20:26:23 -07001157 mFullHighPerfLocksAcquired + " full high perf, " +
Eric Shienbrood5711fad2009-03-27 20:25:31 -07001158 mScanLocksAcquired + " scan");
1159 pw.println("Locks released: " + mFullLocksReleased + " full, " +
Irfan Sheriff5876a422010-08-12 20:26:23 -07001160 mFullHighPerfLocksReleased + " full high perf, " +
Eric Shienbrood5711fad2009-03-27 20:25:31 -07001161 mScanLocksReleased + " scan");
1162 pw.println();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001163 pw.println("Locks held:");
1164 mLocks.dump(pw);
Isaac Levybc7dfb52011-06-06 15:34:01 -07001165
1166 pw.println();
Isaac Levy654f5092011-07-13 17:41:45 -07001167 pw.println("WifiWatchdogStateMachine dump");
1168 mWifiWatchdogStateMachine.dump(pw);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001169 }
1170
Robert Greenwalt58ff0212009-05-19 15:53:54 -07001171 private class WifiLock extends DeathRecipient {
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -07001172 WifiLock(int lockMode, String tag, IBinder binder, WorkSource ws) {
1173 super(lockMode, tag, binder, ws);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001174 }
1175
1176 public void binderDied() {
1177 synchronized (mLocks) {
1178 releaseWifiLockLocked(mBinder);
1179 }
1180 }
1181
1182 public String toString() {
Robert Greenwalt5347bd42009-05-13 15:10:16 -07001183 return "WifiLock{" + mTag + " type=" + mMode + " binder=" + mBinder + "}";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001184 }
1185 }
1186
1187 private class LockList {
1188 private List<WifiLock> mList;
1189
1190 private LockList() {
1191 mList = new ArrayList<WifiLock>();
1192 }
1193
1194 private synchronized boolean hasLocks() {
1195 return !mList.isEmpty();
1196 }
1197
1198 private synchronized int getStrongestLockMode() {
1199 if (mList.isEmpty()) {
1200 return WifiManager.WIFI_MODE_FULL;
1201 }
Irfan Sheriff5876a422010-08-12 20:26:23 -07001202
1203 if (mFullHighPerfLocksAcquired > mFullHighPerfLocksReleased) {
1204 return WifiManager.WIFI_MODE_FULL_HIGH_PERF;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001205 }
Irfan Sheriff5876a422010-08-12 20:26:23 -07001206
1207 if (mFullLocksAcquired > mFullLocksReleased) {
1208 return WifiManager.WIFI_MODE_FULL;
1209 }
1210
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001211 return WifiManager.WIFI_MODE_SCAN_ONLY;
1212 }
1213
1214 private void addLock(WifiLock lock) {
1215 if (findLockByBinder(lock.mBinder) < 0) {
1216 mList.add(lock);
1217 }
1218 }
1219
1220 private WifiLock removeLock(IBinder binder) {
1221 int index = findLockByBinder(binder);
1222 if (index >= 0) {
Suchi Amalapurapufff2fda2009-06-30 21:36:16 -07001223 WifiLock ret = mList.remove(index);
1224 ret.unlinkDeathRecipient();
1225 return ret;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001226 } else {
1227 return null;
1228 }
1229 }
1230
1231 private int findLockByBinder(IBinder binder) {
1232 int size = mList.size();
1233 for (int i = size - 1; i >= 0; i--)
1234 if (mList.get(i).mBinder == binder)
1235 return i;
1236 return -1;
1237 }
1238
1239 private void dump(PrintWriter pw) {
1240 for (WifiLock l : mList) {
1241 pw.print(" ");
1242 pw.println(l);
1243 }
1244 }
1245 }
1246
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -07001247 void enforceWakeSourcePermission(int uid, int pid) {
Dianne Hackborne746f032010-09-13 16:02:57 -07001248 if (uid == android.os.Process.myUid()) {
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -07001249 return;
1250 }
1251 mContext.enforcePermission(android.Manifest.permission.UPDATE_DEVICE_STATS,
1252 pid, uid, null);
1253 }
1254
1255 public boolean acquireWifiLock(IBinder binder, int lockMode, String tag, WorkSource ws) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001256 mContext.enforceCallingOrSelfPermission(android.Manifest.permission.WAKE_LOCK, null);
Irfan Sheriff5876a422010-08-12 20:26:23 -07001257 if (lockMode != WifiManager.WIFI_MODE_FULL &&
1258 lockMode != WifiManager.WIFI_MODE_SCAN_ONLY &&
1259 lockMode != WifiManager.WIFI_MODE_FULL_HIGH_PERF) {
1260 Slog.e(TAG, "Illegal argument, lockMode= " + lockMode);
1261 if (DBG) throw new IllegalArgumentException("lockMode=" + lockMode);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001262 return false;
1263 }
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -07001264 if (ws != null && ws.size() == 0) {
1265 ws = null;
1266 }
Dianne Hackbornecfd7f72010-10-08 14:23:40 -07001267 if (ws != null) {
1268 enforceWakeSourcePermission(Binder.getCallingUid(), Binder.getCallingPid());
1269 }
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -07001270 if (ws == null) {
1271 ws = new WorkSource(Binder.getCallingUid());
1272 }
1273 WifiLock wifiLock = new WifiLock(lockMode, tag, binder, ws);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001274 synchronized (mLocks) {
1275 return acquireWifiLockLocked(wifiLock);
1276 }
1277 }
1278
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -07001279 private void noteAcquireWifiLock(WifiLock wifiLock) throws RemoteException {
1280 switch(wifiLock.mMode) {
1281 case WifiManager.WIFI_MODE_FULL:
Irfan Sheriff5876a422010-08-12 20:26:23 -07001282 case WifiManager.WIFI_MODE_FULL_HIGH_PERF:
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -07001283 mBatteryStats.noteFullWifiLockAcquiredFromSource(wifiLock.mWorkSource);
1284 break;
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -07001285 case WifiManager.WIFI_MODE_SCAN_ONLY:
1286 mBatteryStats.noteScanWifiLockAcquiredFromSource(wifiLock.mWorkSource);
1287 break;
1288 }
1289 }
1290
1291 private void noteReleaseWifiLock(WifiLock wifiLock) throws RemoteException {
1292 switch(wifiLock.mMode) {
1293 case WifiManager.WIFI_MODE_FULL:
Irfan Sheriff5876a422010-08-12 20:26:23 -07001294 case WifiManager.WIFI_MODE_FULL_HIGH_PERF:
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -07001295 mBatteryStats.noteFullWifiLockReleasedFromSource(wifiLock.mWorkSource);
1296 break;
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -07001297 case WifiManager.WIFI_MODE_SCAN_ONLY:
1298 mBatteryStats.noteScanWifiLockReleasedFromSource(wifiLock.mWorkSource);
1299 break;
1300 }
1301 }
1302
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001303 private boolean acquireWifiLockLocked(WifiLock wifiLock) {
Irfan Sheriffc89dd542010-09-28 08:40:54 -07001304 if (DBG) Slog.d(TAG, "acquireWifiLockLocked: " + wifiLock);
Robert Greenwaltf1acb2d2009-10-13 08:20:55 -07001305
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001306 mLocks.addLock(wifiLock);
Robert Greenwaltf1acb2d2009-10-13 08:20:55 -07001307
The Android Open Source Project10592532009-03-18 17:39:46 -07001308 long ident = Binder.clearCallingIdentity();
1309 try {
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -07001310 noteAcquireWifiLock(wifiLock);
Robert Greenwalt5347bd42009-05-13 15:10:16 -07001311 switch(wifiLock.mMode) {
Eric Shienbrood5711fad2009-03-27 20:25:31 -07001312 case WifiManager.WIFI_MODE_FULL:
1313 ++mFullLocksAcquired;
Eric Shienbrood5711fad2009-03-27 20:25:31 -07001314 break;
Irfan Sheriff5876a422010-08-12 20:26:23 -07001315 case WifiManager.WIFI_MODE_FULL_HIGH_PERF:
1316 ++mFullHighPerfLocksAcquired;
1317 break;
1318
Eric Shienbrood5711fad2009-03-27 20:25:31 -07001319 case WifiManager.WIFI_MODE_SCAN_ONLY:
1320 ++mScanLocksAcquired;
Eric Shienbrood5711fad2009-03-27 20:25:31 -07001321 break;
The Android Open Source Project10592532009-03-18 17:39:46 -07001322 }
Dianne Hackbornecfd7f72010-10-08 14:23:40 -07001323
1324 // Be aggressive about adding new locks into the accounted state...
1325 // we want to over-report rather than under-report.
1326 reportStartWorkSource();
1327
1328 updateWifiState();
1329 return true;
The Android Open Source Project10592532009-03-18 17:39:46 -07001330 } catch (RemoteException e) {
Dianne Hackbornecfd7f72010-10-08 14:23:40 -07001331 return false;
The Android Open Source Project10592532009-03-18 17:39:46 -07001332 } finally {
1333 Binder.restoreCallingIdentity(ident);
1334 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001335 }
1336
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -07001337 public void updateWifiLockWorkSource(IBinder lock, WorkSource ws) {
1338 int uid = Binder.getCallingUid();
1339 int pid = Binder.getCallingPid();
1340 if (ws != null && ws.size() == 0) {
1341 ws = null;
1342 }
1343 if (ws != null) {
1344 enforceWakeSourcePermission(uid, pid);
1345 }
1346 long ident = Binder.clearCallingIdentity();
1347 try {
1348 synchronized (mLocks) {
1349 int index = mLocks.findLockByBinder(lock);
1350 if (index < 0) {
1351 throw new IllegalArgumentException("Wifi lock not active");
1352 }
1353 WifiLock wl = mLocks.mList.get(index);
1354 noteReleaseWifiLock(wl);
1355 wl.mWorkSource = ws != null ? new WorkSource(ws) : new WorkSource(uid);
1356 noteAcquireWifiLock(wl);
1357 }
1358 } catch (RemoteException e) {
1359 } finally {
1360 Binder.restoreCallingIdentity(ident);
1361 }
1362 }
1363
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001364 public boolean releaseWifiLock(IBinder lock) {
1365 mContext.enforceCallingOrSelfPermission(android.Manifest.permission.WAKE_LOCK, null);
1366 synchronized (mLocks) {
1367 return releaseWifiLockLocked(lock);
1368 }
1369 }
1370
1371 private boolean releaseWifiLockLocked(IBinder lock) {
Eric Shienbroodd4c5f892009-03-24 18:13:20 -07001372 boolean hadLock;
Robert Greenwaltf1acb2d2009-10-13 08:20:55 -07001373
The Android Open Source Project10592532009-03-18 17:39:46 -07001374 WifiLock wifiLock = mLocks.removeLock(lock);
Robert Greenwaltf1acb2d2009-10-13 08:20:55 -07001375
Irfan Sheriffc89dd542010-09-28 08:40:54 -07001376 if (DBG) Slog.d(TAG, "releaseWifiLockLocked: " + wifiLock);
Robert Greenwaltf1acb2d2009-10-13 08:20:55 -07001377
Eric Shienbroodd4c5f892009-03-24 18:13:20 -07001378 hadLock = (wifiLock != null);
1379
Dianne Hackbornecfd7f72010-10-08 14:23:40 -07001380 long ident = Binder.clearCallingIdentity();
1381 try {
1382 if (hadLock) {
Wink Savillece0ea1f2b2011-10-13 16:55:20 -07001383 noteReleaseWifiLock(wifiLock);
Robert Greenwalt5347bd42009-05-13 15:10:16 -07001384 switch(wifiLock.mMode) {
Eric Shienbrood5711fad2009-03-27 20:25:31 -07001385 case WifiManager.WIFI_MODE_FULL:
1386 ++mFullLocksReleased;
Eric Shienbrood5711fad2009-03-27 20:25:31 -07001387 break;
Irfan Sheriff5876a422010-08-12 20:26:23 -07001388 case WifiManager.WIFI_MODE_FULL_HIGH_PERF:
1389 ++mFullHighPerfLocksReleased;
1390 break;
Eric Shienbrood5711fad2009-03-27 20:25:31 -07001391 case WifiManager.WIFI_MODE_SCAN_ONLY:
1392 ++mScanLocksReleased;
Eric Shienbrood5711fad2009-03-27 20:25:31 -07001393 break;
Eric Shienbroodd4c5f892009-03-24 18:13:20 -07001394 }
The Android Open Source Project10592532009-03-18 17:39:46 -07001395 }
Dianne Hackbornecfd7f72010-10-08 14:23:40 -07001396
1397 // TODO - should this only happen if you hadLock?
1398 updateWifiState();
1399
1400 } catch (RemoteException e) {
1401 } finally {
1402 Binder.restoreCallingIdentity(ident);
The Android Open Source Project10592532009-03-18 17:39:46 -07001403 }
Dianne Hackbornecfd7f72010-10-08 14:23:40 -07001404
Eric Shienbroodd4c5f892009-03-24 18:13:20 -07001405 return hadLock;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001406 }
Robert Greenwalt5347bd42009-05-13 15:10:16 -07001407
Robert Greenwalt58ff0212009-05-19 15:53:54 -07001408 private abstract class DeathRecipient
Robert Greenwalt5347bd42009-05-13 15:10:16 -07001409 implements IBinder.DeathRecipient {
1410 String mTag;
1411 int mMode;
1412 IBinder mBinder;
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -07001413 WorkSource mWorkSource;
Robert Greenwalt5347bd42009-05-13 15:10:16 -07001414
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -07001415 DeathRecipient(int mode, String tag, IBinder binder, WorkSource ws) {
Robert Greenwalt5347bd42009-05-13 15:10:16 -07001416 super();
1417 mTag = tag;
1418 mMode = mode;
1419 mBinder = binder;
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -07001420 mWorkSource = ws;
Robert Greenwalt5347bd42009-05-13 15:10:16 -07001421 try {
1422 mBinder.linkToDeath(this, 0);
1423 } catch (RemoteException e) {
1424 binderDied();
1425 }
1426 }
Suchi Amalapurapufff2fda2009-06-30 21:36:16 -07001427
1428 void unlinkDeathRecipient() {
1429 mBinder.unlinkToDeath(this, 0);
1430 }
Robert Greenwalt5347bd42009-05-13 15:10:16 -07001431 }
1432
Robert Greenwalt58ff0212009-05-19 15:53:54 -07001433 private class Multicaster extends DeathRecipient {
1434 Multicaster(String tag, IBinder binder) {
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -07001435 super(Binder.getCallingUid(), tag, binder, null);
Robert Greenwalt5347bd42009-05-13 15:10:16 -07001436 }
1437
1438 public void binderDied() {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001439 Slog.e(TAG, "Multicaster binderDied");
Robert Greenwalt5347bd42009-05-13 15:10:16 -07001440 synchronized (mMulticasters) {
1441 int i = mMulticasters.indexOf(this);
1442 if (i != -1) {
1443 removeMulticasterLocked(i, mMode);
1444 }
1445 }
1446 }
1447
1448 public String toString() {
Robert Greenwalt58ff0212009-05-19 15:53:54 -07001449 return "Multicaster{" + mTag + " binder=" + mBinder + "}";
Robert Greenwalt5347bd42009-05-13 15:10:16 -07001450 }
1451
1452 public int getUid() {
1453 return mMode;
1454 }
1455 }
1456
Robert Greenwalte2d155a2009-10-21 14:58:34 -07001457 public void initializeMulticastFiltering() {
1458 enforceMulticastChangePermission();
Irfan Sheriffa8fbe1f2010-03-09 09:13:58 -08001459
Robert Greenwalte2d155a2009-10-21 14:58:34 -07001460 synchronized (mMulticasters) {
1461 // if anybody had requested filters be off, leave off
1462 if (mMulticasters.size() != 0) {
1463 return;
1464 } else {
Irfan Sheriffb0c1b80f2011-07-19 15:44:25 -07001465 mWifiStateMachine.startFilteringMulticastV4Packets();
Robert Greenwalte2d155a2009-10-21 14:58:34 -07001466 }
1467 }
1468 }
1469
Robert Greenwaltfc1b15c2009-05-22 15:09:51 -07001470 public void acquireMulticastLock(IBinder binder, String tag) {
1471 enforceMulticastChangePermission();
Robert Greenwalt5347bd42009-05-13 15:10:16 -07001472
1473 synchronized (mMulticasters) {
1474 mMulticastEnabled++;
Robert Greenwalt58ff0212009-05-19 15:53:54 -07001475 mMulticasters.add(new Multicaster(tag, binder));
Irfan Sheriffb0c1b80f2011-07-19 15:44:25 -07001476 // Note that we could call stopFilteringMulticastV4Packets only when
Robert Greenwalt5347bd42009-05-13 15:10:16 -07001477 // our new size == 1 (first call), but this function won't
1478 // be called often and by making the stopPacket call each
1479 // time we're less fragile and self-healing.
Irfan Sheriffb0c1b80f2011-07-19 15:44:25 -07001480 mWifiStateMachine.stopFilteringMulticastV4Packets();
Robert Greenwalt5347bd42009-05-13 15:10:16 -07001481 }
1482
1483 int uid = Binder.getCallingUid();
1484 Long ident = Binder.clearCallingIdentity();
1485 try {
1486 mBatteryStats.noteWifiMulticastEnabled(uid);
1487 } catch (RemoteException e) {
1488 } finally {
1489 Binder.restoreCallingIdentity(ident);
1490 }
1491 }
1492
Robert Greenwaltfc1b15c2009-05-22 15:09:51 -07001493 public void releaseMulticastLock() {
1494 enforceMulticastChangePermission();
Robert Greenwalt5347bd42009-05-13 15:10:16 -07001495
1496 int uid = Binder.getCallingUid();
1497 synchronized (mMulticasters) {
1498 mMulticastDisabled++;
1499 int size = mMulticasters.size();
1500 for (int i = size - 1; i >= 0; i--) {
Robert Greenwalt58ff0212009-05-19 15:53:54 -07001501 Multicaster m = mMulticasters.get(i);
Robert Greenwalt5347bd42009-05-13 15:10:16 -07001502 if ((m != null) && (m.getUid() == uid)) {
1503 removeMulticasterLocked(i, uid);
1504 }
1505 }
1506 }
1507 }
1508
1509 private void removeMulticasterLocked(int i, int uid)
1510 {
Suchi Amalapurapufff2fda2009-06-30 21:36:16 -07001511 Multicaster removed = mMulticasters.remove(i);
Irfan Sheriffa8fbe1f2010-03-09 09:13:58 -08001512
Suchi Amalapurapufff2fda2009-06-30 21:36:16 -07001513 if (removed != null) {
1514 removed.unlinkDeathRecipient();
1515 }
Robert Greenwalt5347bd42009-05-13 15:10:16 -07001516 if (mMulticasters.size() == 0) {
Irfan Sheriffb0c1b80f2011-07-19 15:44:25 -07001517 mWifiStateMachine.startFilteringMulticastV4Packets();
Robert Greenwalt5347bd42009-05-13 15:10:16 -07001518 }
1519
1520 Long ident = Binder.clearCallingIdentity();
1521 try {
1522 mBatteryStats.noteWifiMulticastDisabled(uid);
1523 } catch (RemoteException e) {
1524 } finally {
1525 Binder.restoreCallingIdentity(ident);
1526 }
1527 }
1528
Robert Greenwalt58ff0212009-05-19 15:53:54 -07001529 public boolean isMulticastEnabled() {
Robert Greenwalt5347bd42009-05-13 15:10:16 -07001530 enforceAccessPermission();
1531
1532 synchronized (mMulticasters) {
1533 return (mMulticasters.size() > 0);
1534 }
1535 }
Irfan Sheriff0d255342010-07-28 09:35:20 -07001536
Irfan Sheriff227bec42011-02-15 19:30:27 -08001537 /**
1538 * Evaluate if traffic stats polling is needed based on
1539 * connection and screen on status
1540 */
1541 private void evaluateTrafficStatsPolling() {
1542 Message msg;
1543 if (mNetworkInfo.getDetailedState() == DetailedState.CONNECTED && !mScreenOff) {
Irfan Sheriffebe606f2011-02-24 11:39:15 -08001544 msg = Message.obtain(mAsyncServiceHandler,
1545 WifiManager.CMD_ENABLE_TRAFFIC_STATS_POLL, 1, 0);
Irfan Sheriff227bec42011-02-15 19:30:27 -08001546 } else {
Irfan Sheriffebe606f2011-02-24 11:39:15 -08001547 msg = Message.obtain(mAsyncServiceHandler,
1548 WifiManager.CMD_ENABLE_TRAFFIC_STATS_POLL, 0, 0);
Irfan Sheriff227bec42011-02-15 19:30:27 -08001549 }
1550 msg.sendToTarget();
1551 }
1552
1553 private void notifyOnDataActivity() {
1554 long sent, received;
1555 long preTxPkts = mTxPkts, preRxPkts = mRxPkts;
1556 int dataActivity = WifiManager.DATA_ACTIVITY_NONE;
1557
1558 mTxPkts = TrafficStats.getTxPackets(mInterfaceName);
1559 mRxPkts = TrafficStats.getRxPackets(mInterfaceName);
1560
1561 if (preTxPkts > 0 || preRxPkts > 0) {
1562 sent = mTxPkts - preTxPkts;
1563 received = mRxPkts - preRxPkts;
1564 if (sent > 0) {
1565 dataActivity |= WifiManager.DATA_ACTIVITY_OUT;
1566 }
1567 if (received > 0) {
1568 dataActivity |= WifiManager.DATA_ACTIVITY_IN;
1569 }
1570
1571 if (dataActivity != mDataActivity && !mScreenOff) {
1572 mDataActivity = dataActivity;
1573 for (AsyncChannel client : mClients) {
1574 client.sendMessage(WifiManager.DATA_ACTIVITY_NOTIFICATION, mDataActivity);
1575 }
1576 }
1577 }
1578 }
1579
1580
Irfan Sheriff0d255342010-07-28 09:35:20 -07001581 private void checkAndSetNotification() {
1582 // If we shouldn't place a notification on available networks, then
1583 // don't bother doing any of the following
1584 if (!mNotificationEnabled) return;
1585
1586 State state = mNetworkInfo.getState();
1587 if ((state == NetworkInfo.State.DISCONNECTED)
1588 || (state == NetworkInfo.State.UNKNOWN)) {
1589 // Look for an open network
Irfan Sheriffd8134ff2010-08-22 17:06:34 -07001590 List<ScanResult> scanResults = mWifiStateMachine.syncGetScanResultsList();
Irfan Sheriff0d255342010-07-28 09:35:20 -07001591 if (scanResults != null) {
1592 int numOpenNetworks = 0;
1593 for (int i = scanResults.size() - 1; i >= 0; i--) {
1594 ScanResult scanResult = scanResults.get(i);
1595
Irfan Sherifffdd5f952011-08-04 16:55:54 -07001596 //A capability of [ESS] represents an open access point
1597 //that is available for an STA to connect
1598 if (scanResult.capabilities != null &&
1599 scanResult.capabilities.equals("[ESS]")) {
Irfan Sheriff0d255342010-07-28 09:35:20 -07001600 numOpenNetworks++;
1601 }
1602 }
1603
1604 if (numOpenNetworks > 0) {
1605 if (++mNumScansSinceNetworkStateChange >= NUM_SCANS_BEFORE_ACTUALLY_SCANNING) {
1606 /*
1607 * We've scanned continuously at least
1608 * NUM_SCANS_BEFORE_NOTIFICATION times. The user
1609 * probably does not have a remembered network in range,
1610 * since otherwise supplicant would have tried to
1611 * associate and thus resetting this counter.
1612 */
1613 setNotificationVisible(true, numOpenNetworks, false, 0);
1614 }
1615 return;
1616 }
1617 }
1618 }
1619
1620 // No open networks in range, remove the notification
1621 setNotificationVisible(false, 0, false, 0);
1622 }
1623
1624 /**
1625 * Clears variables related to tracking whether a notification has been
1626 * shown recently and clears the current notification.
1627 */
1628 private void resetNotification() {
1629 mNotificationRepeatTime = 0;
1630 mNumScansSinceNetworkStateChange = 0;
1631 setNotificationVisible(false, 0, false, 0);
1632 }
1633
1634 /**
1635 * Display or don't display a notification that there are open Wi-Fi networks.
1636 * @param visible {@code true} if notification should be visible, {@code false} otherwise
1637 * @param numNetworks the number networks seen
1638 * @param force {@code true} to force notification to be shown/not-shown,
1639 * even if it is already shown/not-shown.
1640 * @param delay time in milliseconds after which the notification should be made
1641 * visible or invisible.
1642 */
1643 private void setNotificationVisible(boolean visible, int numNetworks, boolean force,
1644 int delay) {
1645
1646 // Since we use auto cancel on the notification, when the
1647 // mNetworksAvailableNotificationShown is true, the notification may
1648 // have actually been canceled. However, when it is false we know
1649 // for sure that it is not being shown (it will not be shown any other
1650 // place than here)
1651
1652 // If it should be hidden and it is already hidden, then noop
1653 if (!visible && !mNotificationShown && !force) {
1654 return;
1655 }
1656
1657 NotificationManager notificationManager = (NotificationManager) mContext
1658 .getSystemService(Context.NOTIFICATION_SERVICE);
1659
1660 Message message;
1661 if (visible) {
1662
1663 // Not enough time has passed to show the notification again
1664 if (System.currentTimeMillis() < mNotificationRepeatTime) {
1665 return;
1666 }
1667
1668 if (mNotification == null) {
Wink Savillec7a98342010-08-13 16:11:42 -07001669 // Cache the Notification object.
Irfan Sheriff0d255342010-07-28 09:35:20 -07001670 mNotification = new Notification();
1671 mNotification.when = 0;
1672 mNotification.icon = ICON_NETWORKS_AVAILABLE;
1673 mNotification.flags = Notification.FLAG_AUTO_CANCEL;
1674 mNotification.contentIntent = PendingIntent.getActivity(mContext, 0,
1675 new Intent(WifiManager.ACTION_PICK_WIFI_NETWORK), 0);
1676 }
1677
1678 CharSequence title = mContext.getResources().getQuantityText(
1679 com.android.internal.R.plurals.wifi_available, numNetworks);
1680 CharSequence details = mContext.getResources().getQuantityText(
1681 com.android.internal.R.plurals.wifi_available_detailed, numNetworks);
1682 mNotification.tickerText = title;
1683 mNotification.setLatestEventInfo(mContext, title, details, mNotification.contentIntent);
1684
1685 mNotificationRepeatTime = System.currentTimeMillis() + NOTIFICATION_REPEAT_DELAY_MS;
1686
1687 notificationManager.notify(ICON_NETWORKS_AVAILABLE, mNotification);
Irfan Sheriff0d255342010-07-28 09:35:20 -07001688 } else {
Irfan Sheriff0d255342010-07-28 09:35:20 -07001689 notificationManager.cancel(ICON_NETWORKS_AVAILABLE);
Irfan Sheriff0d255342010-07-28 09:35:20 -07001690 }
1691
Irfan Sheriff0d255342010-07-28 09:35:20 -07001692 mNotificationShown = visible;
1693 }
1694
1695 private class NotificationEnabledSettingObserver extends ContentObserver {
1696
1697 public NotificationEnabledSettingObserver(Handler handler) {
1698 super(handler);
1699 }
1700
1701 public void register() {
1702 ContentResolver cr = mContext.getContentResolver();
1703 cr.registerContentObserver(Settings.Secure.getUriFor(
1704 Settings.Secure.WIFI_NETWORKS_AVAILABLE_NOTIFICATION_ON), true, this);
1705 mNotificationEnabled = getValue();
1706 }
1707
1708 @Override
1709 public void onChange(boolean selfChange) {
1710 super.onChange(selfChange);
1711
1712 mNotificationEnabled = getValue();
1713 resetNotification();
1714 }
1715
1716 private boolean getValue() {
1717 return Settings.Secure.getInt(mContext.getContentResolver(),
1718 Settings.Secure.WIFI_NETWORKS_AVAILABLE_NOTIFICATION_ON, 1) == 1;
1719 }
1720 }
1721
1722
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001723}