blob: aef3426b40c0dda4d6e9eaf0d8688f44c781e41a [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
Irfan Sheriff5401f0b2011-12-07 16:27:49 -0800155 /* Persisted state that tracks the wifi & airplane interaction from settings */
156 private AtomicInteger mPersistWifiState = new AtomicInteger(WIFI_DISABLED);
157 /* Tracks current airplane mode state */
Irfan Sheriff658772f2011-03-08 14:52:31 -0800158 private AtomicBoolean mAirplaneModeOn = new AtomicBoolean(false);
Irfan Sheriff5401f0b2011-12-07 16:27:49 -0800159 /* Tracks whether wifi is enabled from WifiStateMachine's perspective */
160 private boolean mWifiEnabled;
Irfan Sheriff658772f2011-03-08 14:52:31 -0800161
Irfan Sheriffa2a1b912010-06-07 09:03:04 -0700162 private boolean mIsReceiverRegistered = false;
163
Irfan Sheriff0d255342010-07-28 09:35:20 -0700164
165 NetworkInfo mNetworkInfo = new NetworkInfo(ConnectivityManager.TYPE_WIFI, 0, "WIFI", "");
166
167 // Variables relating to the 'available networks' notification
168 /**
169 * The icon to show in the 'available networks' notification. This will also
170 * be the ID of the Notification given to the NotificationManager.
171 */
172 private static final int ICON_NETWORKS_AVAILABLE =
173 com.android.internal.R.drawable.stat_notify_wifi_in_range;
174 /**
175 * When a notification is shown, we wait this amount before possibly showing it again.
176 */
177 private final long NOTIFICATION_REPEAT_DELAY_MS;
178 /**
179 * Whether the user has set the setting to show the 'available networks' notification.
180 */
181 private boolean mNotificationEnabled;
182 /**
183 * Observes the user setting to keep {@link #mNotificationEnabled} in sync.
184 */
185 private NotificationEnabledSettingObserver mNotificationEnabledSettingObserver;
186 /**
187 * The {@link System#currentTimeMillis()} must be at least this value for us
188 * to show the notification again.
189 */
190 private long mNotificationRepeatTime;
191 /**
192 * The Notification object given to the NotificationManager.
193 */
194 private Notification mNotification;
195 /**
196 * Whether the notification is being shown, as set by us. That is, if the
197 * user cancels the notification, we will not receive the callback so this
198 * will still be true. We only guarantee if this is false, then the
199 * notification is not showing.
200 */
201 private boolean mNotificationShown;
202 /**
203 * The number of continuous scans that must occur before consider the
204 * supplicant in a scanning state. This allows supplicant to associate with
205 * remembered networks that are in the scan results.
206 */
207 private static final int NUM_SCANS_BEFORE_ACTUALLY_SCANNING = 3;
208 /**
209 * The number of scans since the last network state change. When this
210 * exceeds {@link #NUM_SCANS_BEFORE_ACTUALLY_SCANNING}, we consider the
211 * supplicant to actually be scanning. When the network state changes to
212 * something other than scanning, we reset this to 0.
213 */
214 private int mNumScansSinceNetworkStateChange;
Jaikumar Ganesh7440fc22010-09-27 17:04:14 -0700215
Dianne Hackborn03f3cb02010-09-17 23:12:26 -0700216 /**
Wink Saville4b7ba092010-10-20 15:37:41 -0700217 * Asynchronous channel to WifiStateMachine
218 */
Irfan Sheriff227bec42011-02-15 19:30:27 -0800219 private AsyncChannel mWifiStateMachineChannel;
Wink Saville4b7ba092010-10-20 15:37:41 -0700220
221 /**
Irfan Sheriff227bec42011-02-15 19:30:27 -0800222 * Clients receiving asynchronous messages
Wink Saville4b7ba092010-10-20 15:37:41 -0700223 */
Irfan Sheriff227bec42011-02-15 19:30:27 -0800224 private List<AsyncChannel> mClients = new ArrayList<AsyncChannel>();
Wink Saville4b7ba092010-10-20 15:37:41 -0700225
Irfan Sheriff227bec42011-02-15 19:30:27 -0800226 /**
227 * Handles client connections
228 */
229 private class AsyncServiceHandler extends Handler {
230
231 AsyncServiceHandler(android.os.Looper looper) {
Wink Saville4b7ba092010-10-20 15:37:41 -0700232 super(looper);
Wink Saville4b7ba092010-10-20 15:37:41 -0700233 }
234
235 @Override
236 public void handleMessage(Message msg) {
237 switch (msg.what) {
238 case AsyncChannel.CMD_CHANNEL_HALF_CONNECTED: {
239 if (msg.arg1 == AsyncChannel.STATUS_SUCCESSFUL) {
Irfan Sheriff227bec42011-02-15 19:30:27 -0800240 Slog.d(TAG, "New client listening to asynchronous messages");
241 mClients.add((AsyncChannel) msg.obj);
Wink Saville4b7ba092010-10-20 15:37:41 -0700242 } else {
Irfan Sheriff227bec42011-02-15 19:30:27 -0800243 Slog.e(TAG, "Client connection failure, error=" + msg.arg1);
244 }
245 break;
246 }
Irfan Sheriffc23971b2011-03-04 17:06:31 -0800247 case AsyncChannel.CMD_CHANNEL_DISCONNECTED: {
248 if (msg.arg1 == AsyncChannel.STATUS_SEND_UNSUCCESSFUL) {
249 Slog.d(TAG, "Send failed, client connection lost");
250 } else {
251 Slog.d(TAG, "Client connection lost with reason: " + msg.arg1);
252 }
253 mClients.remove((AsyncChannel) msg.obj);
254 break;
255 }
Irfan Sheriff227bec42011-02-15 19:30:27 -0800256 case AsyncChannel.CMD_CHANNEL_FULL_CONNECTION: {
257 AsyncChannel ac = new AsyncChannel();
258 ac.connect(mContext, this, msg.replyTo);
259 break;
260 }
Irfan Sheriffebe606f2011-02-24 11:39:15 -0800261 case WifiManager.CMD_ENABLE_TRAFFIC_STATS_POLL: {
Irfan Sheriff227bec42011-02-15 19:30:27 -0800262 mEnableTrafficStatsPoll = (msg.arg1 == 1);
263 mTrafficStatsPollToken++;
264 if (mEnableTrafficStatsPoll) {
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);
268 }
269 break;
270 }
Irfan Sheriffebe606f2011-02-24 11:39:15 -0800271 case WifiManager.CMD_TRAFFIC_STATS_POLL: {
Irfan Sheriff227bec42011-02-15 19:30:27 -0800272 if (msg.arg1 == mTrafficStatsPollToken) {
273 notifyOnDataActivity();
Irfan Sheriffebe606f2011-02-24 11:39:15 -0800274 sendMessageDelayed(Message.obtain(this, WifiManager.CMD_TRAFFIC_STATS_POLL,
Irfan Sheriff227bec42011-02-15 19:30:27 -0800275 mTrafficStatsPollToken, 0), POLL_TRAFFIC_STATS_INTERVAL_MSECS);
Wink Saville4b7ba092010-10-20 15:37:41 -0700276 }
277 break;
278 }
Irfan Sheriffebe606f2011-02-24 11:39:15 -0800279 case WifiManager.CMD_CONNECT_NETWORK: {
280 if (msg.obj != null) {
281 mWifiStateMachine.connectNetwork((WifiConfiguration)msg.obj);
282 } else {
283 mWifiStateMachine.connectNetwork(msg.arg1);
284 }
285 break;
286 }
287 case WifiManager.CMD_SAVE_NETWORK: {
288 mWifiStateMachine.saveNetwork((WifiConfiguration)msg.obj);
289 break;
290 }
291 case WifiManager.CMD_FORGET_NETWORK: {
292 mWifiStateMachine.forgetNetwork(msg.arg1);
293 break;
294 }
295 case WifiManager.CMD_START_WPS: {
296 //replyTo has the original source
Irfan Sheriff651cdfc2011-09-07 00:31:20 -0700297 mWifiStateMachine.startWps(msg.replyTo, (WpsInfo)msg.obj);
Irfan Sheriffebe606f2011-02-24 11:39:15 -0800298 break;
299 }
Isaac Levy8dc6a1b2011-07-27 08:00:03 -0700300 case WifiManager.CMD_DISABLE_NETWORK: {
301 mWifiStateMachine.disableNetwork(msg.replyTo, msg.arg1, msg.arg2);
302 break;
303 }
Wink Saville4b7ba092010-10-20 15:37:41 -0700304 default: {
305 Slog.d(TAG, "WifiServicehandler.handleMessage ignoring msg=" + msg);
306 break;
307 }
308 }
309 }
310 }
Irfan Sheriff227bec42011-02-15 19:30:27 -0800311 private AsyncServiceHandler mAsyncServiceHandler;
312
313 /**
314 * Handles interaction with WifiStateMachine
315 */
316 private class WifiStateMachineHandler extends Handler {
317 private AsyncChannel mWsmChannel;
318
319 WifiStateMachineHandler(android.os.Looper looper) {
320 super(looper);
321 mWsmChannel = new AsyncChannel();
322 mWsmChannel.connect(mContext, this, mWifiStateMachine.getHandler());
323 }
324
325 @Override
326 public void handleMessage(Message msg) {
327 switch (msg.what) {
328 case AsyncChannel.CMD_CHANNEL_HALF_CONNECTED: {
329 if (msg.arg1 == AsyncChannel.STATUS_SUCCESSFUL) {
330 mWifiStateMachineChannel = mWsmChannel;
331 } else {
332 Slog.e(TAG, "WifiStateMachine connection failure, error=" + msg.arg1);
333 mWifiStateMachineChannel = null;
334 }
335 break;
336 }
Irfan Sheriff6da83d52011-06-06 12:54:06 -0700337 case AsyncChannel.CMD_CHANNEL_DISCONNECTED: {
338 Slog.e(TAG, "WifiStateMachine channel lost, msg.arg1 =" + msg.arg1);
339 mWifiStateMachineChannel = null;
340 //Re-establish connection to state machine
341 mWsmChannel.connect(mContext, this, mWifiStateMachine.getHandler());
342 break;
343 }
Irfan Sheriff227bec42011-02-15 19:30:27 -0800344 default: {
345 Slog.d(TAG, "WifiStateMachineHandler.handleMessage ignoring msg=" + msg);
346 break;
347 }
348 }
349 }
350 }
351 WifiStateMachineHandler mWifiStateMachineHandler;
Wink Saville4b7ba092010-10-20 15:37:41 -0700352
353 /**
Dianne Hackborn03f3cb02010-09-17 23:12:26 -0700354 * Temporary for computing UIDS that are responsible for starting WIFI.
355 * Protected by mWifiStateTracker lock.
356 */
357 private final WorkSource mTmpWorkSource = new WorkSource();
Isaac Levy654f5092011-07-13 17:41:45 -0700358 private WifiWatchdogStateMachine mWifiWatchdogStateMachine;
Irfan Sheriff0d255342010-07-28 09:35:20 -0700359
360 WifiService(Context context) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800361 mContext = context;
Irfan Sheriff227bec42011-02-15 19:30:27 -0800362
363 mInterfaceName = SystemProperties.get("wifi.interface", "wlan0");
364
365 mWifiStateMachine = new WifiStateMachine(mContext, mInterfaceName);
Irfan Sheriff0d255342010-07-28 09:35:20 -0700366 mWifiStateMachine.enableRssiPolling(true);
The Android Open Source Project10592532009-03-18 17:39:46 -0700367 mBatteryStats = BatteryStatsService.getService();
Jaikumar Ganesh084c6652009-12-07 10:58:18 -0800368
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800369 mAlarmManager = (AlarmManager)mContext.getSystemService(Context.ALARM_SERVICE);
370 Intent idleIntent = new Intent(ACTION_DEVICE_IDLE, null);
371 mIdleIntent = PendingIntent.getBroadcast(mContext, IDLE_REQUEST, idleIntent, 0);
372
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800373 mContext.registerReceiver(
374 new BroadcastReceiver() {
375 @Override
376 public void onReceive(Context context, Intent intent) {
Irfan Sheriff658772f2011-03-08 14:52:31 -0800377 mAirplaneModeOn.set(isAirplaneModeOn());
378 /* On airplane mode disable, restore wifi state if necessary */
379 if (!mAirplaneModeOn.get() && (testAndClearWifiSavedState() ||
Irfan Sheriff5401f0b2011-12-07 16:27:49 -0800380 mPersistWifiState.get() == WIFI_ENABLED_AIRPLANE_OVERRIDE)) {
381 persistWifiState(true);
Irfan Sheriffb2e6c012010-04-05 11:57:56 -0700382 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800383 updateWifiState();
384 }
385 },
386 new IntentFilter(Intent.ACTION_AIRPLANE_MODE_CHANGED));
387
Irfan Sheriff0d255342010-07-28 09:35:20 -0700388 IntentFilter filter = new IntentFilter();
389 filter.addAction(WifiManager.WIFI_STATE_CHANGED_ACTION);
390 filter.addAction(WifiManager.NETWORK_STATE_CHANGED_ACTION);
391 filter.addAction(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION);
392
393 mContext.registerReceiver(
394 new BroadcastReceiver() {
395 @Override
396 public void onReceive(Context context, Intent intent) {
397 if (intent.getAction().equals(WifiManager.WIFI_STATE_CHANGED_ACTION)) {
Irfan Sheriff5401f0b2011-12-07 16:27:49 -0800398 int wifiState = intent.getIntExtra(WifiManager.EXTRA_WIFI_STATE,
399 WifiManager.WIFI_STATE_DISABLED);
400
401 mWifiEnabled = (wifiState == WifiManager.WIFI_STATE_ENABLED);
402
403 // reset & clear notification on any wifi state change
Irfan Sheriff0d255342010-07-28 09:35:20 -0700404 resetNotification();
405 } else if (intent.getAction().equals(
406 WifiManager.NETWORK_STATE_CHANGED_ACTION)) {
407 mNetworkInfo = (NetworkInfo) intent.getParcelableExtra(
408 WifiManager.EXTRA_NETWORK_INFO);
409 // reset & clear notification on a network connect & disconnect
410 switch(mNetworkInfo.getDetailedState()) {
411 case CONNECTED:
412 case DISCONNECTED:
Irfan Sheriff227bec42011-02-15 19:30:27 -0800413 evaluateTrafficStatsPolling();
Irfan Sheriff0d255342010-07-28 09:35:20 -0700414 resetNotification();
415 break;
416 }
417 } else if (intent.getAction().equals(
418 WifiManager.SCAN_RESULTS_AVAILABLE_ACTION)) {
419 checkAndSetNotification();
420 }
421 }
422 }, filter);
423
Irfan Sheriff227bec42011-02-15 19:30:27 -0800424 HandlerThread wifiThread = new HandlerThread("WifiService");
425 wifiThread.start();
426 mAsyncServiceHandler = new AsyncServiceHandler(wifiThread.getLooper());
427 mWifiStateMachineHandler = new WifiStateMachineHandler(wifiThread.getLooper());
428
Irfan Sheriff0d255342010-07-28 09:35:20 -0700429 // Setting is in seconds
430 NOTIFICATION_REPEAT_DELAY_MS = Settings.Secure.getInt(context.getContentResolver(),
431 Settings.Secure.WIFI_NETWORKS_AVAILABLE_REPEAT_DELAY, 900) * 1000l;
432 mNotificationEnabledSettingObserver = new NotificationEnabledSettingObserver(new Handler());
433 mNotificationEnabledSettingObserver.register();
Irfan Sherifffcc08452011-02-17 16:44:54 -0800434
435 mBackgroundScanSupported = mContext.getResources().getBoolean(
436 com.android.internal.R.bool.config_wifi_background_scan_support);
Irfan Sheriff7b009782010-03-11 16:37:45 -0800437 }
Irfan Sheriff5321aef2010-02-12 12:35:59 -0800438
Irfan Sheriff7b009782010-03-11 16:37:45 -0800439 /**
440 * Check if Wi-Fi needs to be enabled and start
441 * if needed
Irfan Sheriff60e3ba02010-04-02 12:18:45 -0700442 *
443 * This function is used only at boot time
Irfan Sheriff7b009782010-03-11 16:37:45 -0800444 */
Irfan Sheriff0d255342010-07-28 09:35:20 -0700445 public void checkAndStartWifi() {
Irfan Sheriff658772f2011-03-08 14:52:31 -0800446 mAirplaneModeOn.set(isAirplaneModeOn());
Irfan Sheriff5401f0b2011-12-07 16:27:49 -0800447 mPersistWifiState.set(getPersistedWifiState());
Irfan Sheriff658772f2011-03-08 14:52:31 -0800448 /* Start if Wi-Fi should be enabled or the saved state indicates Wi-Fi was on */
449 boolean wifiEnabled = shouldWifiBeEnabled() || testAndClearWifiSavedState();
Irfan Sheriff7b009782010-03-11 16:37:45 -0800450 Slog.i(TAG, "WifiService starting up with Wi-Fi " +
451 (wifiEnabled ? "enabled" : "disabled"));
Irfan Sheriffb99fe5e2010-03-26 14:56:07 -0700452 setWifiEnabled(wifiEnabled);
Isaac Levybc7dfb52011-06-06 15:34:01 -0700453
Isaac Levy654f5092011-07-13 17:41:45 -0700454 mWifiWatchdogStateMachine = WifiWatchdogStateMachine.
455 makeWifiWatchdogStateMachine(mContext);
456
Irfan Sheriff5321aef2010-02-12 12:35:59 -0800457 }
458
Irfan Sheriffa3bd4092010-03-24 17:58:59 -0700459 private boolean testAndClearWifiSavedState() {
460 final ContentResolver cr = mContext.getContentResolver();
461 int wifiSavedState = 0;
462 try {
463 wifiSavedState = Settings.Secure.getInt(cr, Settings.Secure.WIFI_SAVED_STATE);
464 if(wifiSavedState == 1)
465 Settings.Secure.putInt(cr, Settings.Secure.WIFI_SAVED_STATE, 0);
466 } catch (Settings.SettingNotFoundException e) {
467 ;
468 }
469 return (wifiSavedState == 1);
470 }
471
Irfan Sheriff658772f2011-03-08 14:52:31 -0800472 private int getPersistedWifiState() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800473 final ContentResolver cr = mContext.getContentResolver();
474 try {
Irfan Sheriff658772f2011-03-08 14:52:31 -0800475 return Settings.Secure.getInt(cr, Settings.Secure.WIFI_ON);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800476 } catch (Settings.SettingNotFoundException e) {
Irfan Sheriff658772f2011-03-08 14:52:31 -0800477 Settings.Secure.putInt(cr, Settings.Secure.WIFI_ON, WIFI_DISABLED);
478 return WIFI_DISABLED;
479 }
480 }
481
482 private boolean shouldWifiBeEnabled() {
483 if (mAirplaneModeOn.get()) {
Irfan Sheriff5401f0b2011-12-07 16:27:49 -0800484 return mPersistWifiState.get() == WIFI_ENABLED_AIRPLANE_OVERRIDE;
Irfan Sheriff658772f2011-03-08 14:52:31 -0800485 } else {
Irfan Sheriff5401f0b2011-12-07 16:27:49 -0800486 return mPersistWifiState.get() != WIFI_DISABLED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800487 }
488 }
489
Irfan Sheriff5401f0b2011-12-07 16:27:49 -0800490 private void persistWifiState(boolean enabled) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800491 final ContentResolver cr = mContext.getContentResolver();
Irfan Sheriff31b92e22011-10-03 12:13:20 -0700492 boolean airplane = mAirplaneModeOn.get() && isAirplaneToggleable();
Irfan Sheriff658772f2011-03-08 14:52:31 -0800493 if (enabled) {
Irfan Sheriff31b92e22011-10-03 12:13:20 -0700494 if (airplane) {
Irfan Sheriff5401f0b2011-12-07 16:27:49 -0800495 mPersistWifiState.set(WIFI_ENABLED_AIRPLANE_OVERRIDE);
Irfan Sheriff658772f2011-03-08 14:52:31 -0800496 } else {
Irfan Sheriff5401f0b2011-12-07 16:27:49 -0800497 mPersistWifiState.set(WIFI_ENABLED);
Irfan Sheriff658772f2011-03-08 14:52:31 -0800498 }
499 } else {
Irfan Sheriff31b92e22011-10-03 12:13:20 -0700500 if (airplane) {
Irfan Sheriff5401f0b2011-12-07 16:27:49 -0800501 mPersistWifiState.set(WIFI_DISABLED_AIRPLANE_ON);
Irfan Sheriff31b92e22011-10-03 12:13:20 -0700502 } else {
Irfan Sheriff5401f0b2011-12-07 16:27:49 -0800503 mPersistWifiState.set(WIFI_DISABLED);
Irfan Sheriff31b92e22011-10-03 12:13:20 -0700504 }
Irfan Sheriff658772f2011-03-08 14:52:31 -0800505 }
Irfan Sheriff5401f0b2011-12-07 16:27:49 -0800506
507 Settings.Secure.putInt(cr, Settings.Secure.WIFI_ON, mPersistWifiState.get());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800508 }
509
Irfan Sheriff658772f2011-03-08 14:52:31 -0800510
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800511 /**
512 * see {@link android.net.wifi.WifiManager#pingSupplicant()}
Irfan Sheriffa2a1b912010-06-07 09:03:04 -0700513 * @return {@code true} if the operation succeeds, {@code false} otherwise
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800514 */
515 public boolean pingSupplicant() {
Irfan Sheriffa2a1b912010-06-07 09:03:04 -0700516 enforceAccessPermission();
Irfan Sheriff227bec42011-02-15 19:30:27 -0800517 if (mWifiStateMachineChannel != null) {
518 return mWifiStateMachine.syncPingSupplicant(mWifiStateMachineChannel);
Irfan Sheriff1406bcb2010-10-28 14:41:39 -0700519 } else {
Irfan Sheriff227bec42011-02-15 19:30:27 -0800520 Slog.e(TAG, "mWifiStateMachineChannel is not initialized");
Irfan Sheriff1406bcb2010-10-28 14:41:39 -0700521 return false;
522 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800523 }
524
525 /**
526 * see {@link android.net.wifi.WifiManager#startScan()}
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800527 */
Irfan Sheriffe4984752010-08-19 11:29:22 -0700528 public void startScan(boolean forceActive) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800529 enforceChangePermission();
Irfan Sheriffe4984752010-08-19 11:29:22 -0700530 mWifiStateMachine.startScan(forceActive);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800531 }
532
533 private void enforceAccessPermission() {
534 mContext.enforceCallingOrSelfPermission(android.Manifest.permission.ACCESS_WIFI_STATE,
535 "WifiService");
536 }
537
538 private void enforceChangePermission() {
539 mContext.enforceCallingOrSelfPermission(android.Manifest.permission.CHANGE_WIFI_STATE,
540 "WifiService");
541
542 }
543
Robert Greenwaltfc1b15c2009-05-22 15:09:51 -0700544 private void enforceMulticastChangePermission() {
545 mContext.enforceCallingOrSelfPermission(
546 android.Manifest.permission.CHANGE_WIFI_MULTICAST_STATE,
547 "WifiService");
548 }
549
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800550 /**
Irfan Sheriffa2a1b912010-06-07 09:03:04 -0700551 * see {@link android.net.wifi.WifiManager#setWifiEnabled(boolean)}
552 * @param enable {@code true} to enable, {@code false} to disable.
553 * @return {@code true} if the enable/disable operation was
554 * started or is already in the queue.
555 */
556 public synchronized boolean setWifiEnabled(boolean enable) {
557 enforceChangePermission();
Irfan Sheriffa2a1b912010-06-07 09:03:04 -0700558 if (DBG) {
Irfan Sheriff0d255342010-07-28 09:35:20 -0700559 Slog.e(TAG, "Invoking mWifiStateMachine.setWifiEnabled\n");
Irfan Sheriffa2a1b912010-06-07 09:03:04 -0700560 }
561
Dianne Hackborn03f3cb02010-09-17 23:12:26 -0700562 if (enable) {
563 reportStartWorkSource();
564 }
Irfan Sheriff0d255342010-07-28 09:35:20 -0700565 mWifiStateMachine.setWifiEnabled(enable);
Irfan Sheriff61180692010-08-18 16:07:39 -0700566
567 /*
568 * Caller might not have WRITE_SECURE_SETTINGS,
569 * only CHANGE_WIFI_STATE is enforced
570 */
Irfan Sheriff5401f0b2011-12-07 16:27:49 -0800571
572 /* Avoids overriding of airplane state when wifi is already in the expected state */
573 if (enable != mWifiEnabled) {
574 long ident = Binder.clearCallingIdentity();
575 persistWifiState(enable);
576 Binder.restoreCallingIdentity(ident);
577 }
Irfan Sheriffa2a1b912010-06-07 09:03:04 -0700578
579 if (enable) {
580 if (!mIsReceiverRegistered) {
581 registerForBroadcasts();
582 mIsReceiverRegistered = true;
583 }
Irfan Sheriff5401f0b2011-12-07 16:27:49 -0800584 } else if (mIsReceiverRegistered) {
Irfan Sheriffa2a1b912010-06-07 09:03:04 -0700585 mContext.unregisterReceiver(mReceiver);
586 mIsReceiverRegistered = false;
587 }
588
589 return true;
590 }
591
592 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800593 * see {@link WifiManager#getWifiState()}
594 * @return One of {@link WifiManager#WIFI_STATE_DISABLED},
595 * {@link WifiManager#WIFI_STATE_DISABLING},
596 * {@link WifiManager#WIFI_STATE_ENABLED},
597 * {@link WifiManager#WIFI_STATE_ENABLING},
598 * {@link WifiManager#WIFI_STATE_UNKNOWN}
599 */
600 public int getWifiEnabledState() {
601 enforceAccessPermission();
Irfan Sheriffd8134ff2010-08-22 17:06:34 -0700602 return mWifiStateMachine.syncGetWifiState();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800603 }
604
605 /**
Irfan Sheriffc2f54c22010-03-18 14:02:22 -0700606 * see {@link android.net.wifi.WifiManager#setWifiApEnabled(WifiConfiguration, boolean)}
Irfan Sheriff5321aef2010-02-12 12:35:59 -0800607 * @param wifiConfig SSID, security and channel details as
608 * part of WifiConfiguration
Irfan Sheriffa2a1b912010-06-07 09:03:04 -0700609 * @param enabled true to enable and false to disable
Irfan Sheriff5321aef2010-02-12 12:35:59 -0800610 */
Irfan Sheriffffcea7a2011-05-10 16:26:06 -0700611 public void setWifiApEnabled(WifiConfiguration wifiConfig, boolean enabled) {
Irfan Sheriff5321aef2010-02-12 12:35:59 -0800612 enforceChangePermission();
Irfan Sheriff0d255342010-07-28 09:35:20 -0700613 mWifiStateMachine.setWifiApEnabled(wifiConfig, enabled);
Irfan Sheriff5321aef2010-02-12 12:35:59 -0800614 }
615
Irfan Sheriffa2a1b912010-06-07 09:03:04 -0700616 /**
617 * see {@link WifiManager#getWifiApState()}
618 * @return One of {@link WifiManager#WIFI_AP_STATE_DISABLED},
619 * {@link WifiManager#WIFI_AP_STATE_DISABLING},
620 * {@link WifiManager#WIFI_AP_STATE_ENABLED},
621 * {@link WifiManager#WIFI_AP_STATE_ENABLING},
622 * {@link WifiManager#WIFI_AP_STATE_FAILED}
623 */
624 public int getWifiApEnabledState() {
Irfan Sheriff17b232b2010-06-24 11:32:26 -0700625 enforceAccessPermission();
Irfan Sheriffd8134ff2010-08-22 17:06:34 -0700626 return mWifiStateMachine.syncGetWifiApState();
Irfan Sheriffa2a1b912010-06-07 09:03:04 -0700627 }
628
629 /**
630 * see {@link WifiManager#getWifiApConfiguration()}
631 * @return soft access point configuration
632 */
Irfan Sheriffffcea7a2011-05-10 16:26:06 -0700633 public WifiConfiguration getWifiApConfiguration() {
634 enforceAccessPermission();
Irfan Sheriff9575a1b2011-11-07 10:34:54 -0800635 return mWifiStateMachine.syncGetWifiApConfiguration();
Irfan Sheriff9ab518ad2010-03-12 15:48:17 -0800636 }
637
Irfan Sheriffa2a1b912010-06-07 09:03:04 -0700638 /**
639 * see {@link WifiManager#setWifiApConfiguration(WifiConfiguration)}
640 * @param wifiConfig WifiConfiguration details for soft access point
641 */
Irfan Sheriffffcea7a2011-05-10 16:26:06 -0700642 public void setWifiApConfiguration(WifiConfiguration wifiConfig) {
Irfan Sheriff17b232b2010-06-24 11:32:26 -0700643 enforceChangePermission();
Irfan Sheriff9ab518ad2010-03-12 15:48:17 -0800644 if (wifiConfig == null)
645 return;
Irfan Sheriffffcea7a2011-05-10 16:26:06 -0700646 mWifiStateMachine.setWifiApConfiguration(wifiConfig);
Irfan Sheriff9ab518ad2010-03-12 15:48:17 -0800647 }
648
Irfan Sheriff5321aef2010-02-12 12:35:59 -0800649 /**
Irfan Sheriffa2a1b912010-06-07 09:03:04 -0700650 * see {@link android.net.wifi.WifiManager#disconnect()}
Irfan Sheriff5321aef2010-02-12 12:35:59 -0800651 */
Irfan Sheriffe4984752010-08-19 11:29:22 -0700652 public void disconnect() {
Irfan Sheriffa2a1b912010-06-07 09:03:04 -0700653 enforceChangePermission();
Irfan Sheriffe4984752010-08-19 11:29:22 -0700654 mWifiStateMachine.disconnectCommand();
Irfan Sheriff5321aef2010-02-12 12:35:59 -0800655 }
656
657 /**
Irfan Sheriffa2a1b912010-06-07 09:03:04 -0700658 * see {@link android.net.wifi.WifiManager#reconnect()}
Irfan Sheriff5321aef2010-02-12 12:35:59 -0800659 */
Irfan Sheriffe4984752010-08-19 11:29:22 -0700660 public void reconnect() {
Irfan Sheriffa2a1b912010-06-07 09:03:04 -0700661 enforceChangePermission();
Irfan Sheriffe4984752010-08-19 11:29:22 -0700662 mWifiStateMachine.reconnectCommand();
Irfan Sheriff5321aef2010-02-12 12:35:59 -0800663 }
664
Irfan Sheriffa2a1b912010-06-07 09:03:04 -0700665 /**
666 * see {@link android.net.wifi.WifiManager#reassociate()}
Irfan Sheriffa2a1b912010-06-07 09:03:04 -0700667 */
Irfan Sheriffe4984752010-08-19 11:29:22 -0700668 public void reassociate() {
Irfan Sheriffa2a1b912010-06-07 09:03:04 -0700669 enforceChangePermission();
Irfan Sheriffe4984752010-08-19 11:29:22 -0700670 mWifiStateMachine.reassociateCommand();
Irfan Sheriff5321aef2010-02-12 12:35:59 -0800671 }
672
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800673 /**
674 * see {@link android.net.wifi.WifiManager#getConfiguredNetworks()}
675 * @return the list of configured networks
676 */
677 public List<WifiConfiguration> getConfiguredNetworks() {
678 enforceAccessPermission();
Irfan Sheriffd8134ff2010-08-22 17:06:34 -0700679 return mWifiStateMachine.syncGetConfiguredNetworks();
Chung-yih Wanga8d15942009-10-09 11:01:49 +0800680 }
681
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800682 /**
683 * see {@link android.net.wifi.WifiManager#addOrUpdateNetwork(WifiConfiguration)}
684 * @return the supplicant-assigned identifier for the new or updated
685 * network if the operation succeeds, or {@code -1} if it fails
686 */
Irfan Sheriff7aac5542009-12-22 21:42:17 -0800687 public int addOrUpdateNetwork(WifiConfiguration config) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800688 enforceChangePermission();
Irfan Sheriff227bec42011-02-15 19:30:27 -0800689 if (mWifiStateMachineChannel != null) {
690 return mWifiStateMachine.syncAddOrUpdateNetwork(mWifiStateMachineChannel, config);
Irfan Sheriff1406bcb2010-10-28 14:41:39 -0700691 } else {
Irfan Sheriff227bec42011-02-15 19:30:27 -0800692 Slog.e(TAG, "mWifiStateMachineChannel is not initialized");
Irfan Sheriff1406bcb2010-10-28 14:41:39 -0700693 return -1;
694 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800695 }
696
Irfan Sheriffa2a1b912010-06-07 09:03:04 -0700697 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800698 * See {@link android.net.wifi.WifiManager#removeNetwork(int)}
699 * @param netId the integer that identifies the network configuration
700 * to the supplicant
701 * @return {@code true} if the operation succeeded
702 */
703 public boolean removeNetwork(int netId) {
704 enforceChangePermission();
Irfan Sheriff227bec42011-02-15 19:30:27 -0800705 if (mWifiStateMachineChannel != null) {
706 return mWifiStateMachine.syncRemoveNetwork(mWifiStateMachineChannel, netId);
Wink Saville4b7ba092010-10-20 15:37:41 -0700707 } else {
Irfan Sheriff227bec42011-02-15 19:30:27 -0800708 Slog.e(TAG, "mWifiStateMachineChannel is not initialized");
Wink Saville4b7ba092010-10-20 15:37:41 -0700709 return false;
710 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800711 }
712
713 /**
714 * See {@link android.net.wifi.WifiManager#enableNetwork(int, boolean)}
715 * @param netId the integer that identifies the network configuration
716 * to the supplicant
717 * @param disableOthers if true, disable all other networks.
718 * @return {@code true} if the operation succeeded
719 */
720 public boolean enableNetwork(int netId, boolean disableOthers) {
721 enforceChangePermission();
Irfan Sheriff227bec42011-02-15 19:30:27 -0800722 if (mWifiStateMachineChannel != null) {
723 return mWifiStateMachine.syncEnableNetwork(mWifiStateMachineChannel, netId,
724 disableOthers);
Irfan Sheriff1406bcb2010-10-28 14:41:39 -0700725 } else {
Irfan Sheriff227bec42011-02-15 19:30:27 -0800726 Slog.e(TAG, "mWifiStateMachineChannel is not initialized");
Irfan Sheriff1406bcb2010-10-28 14:41:39 -0700727 return false;
728 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800729 }
730
731 /**
732 * See {@link android.net.wifi.WifiManager#disableNetwork(int)}
733 * @param netId the integer that identifies the network configuration
734 * to the supplicant
735 * @return {@code true} if the operation succeeded
736 */
737 public boolean disableNetwork(int netId) {
738 enforceChangePermission();
Irfan Sheriff227bec42011-02-15 19:30:27 -0800739 if (mWifiStateMachineChannel != null) {
740 return mWifiStateMachine.syncDisableNetwork(mWifiStateMachineChannel, netId);
Irfan Sheriff1406bcb2010-10-28 14:41:39 -0700741 } else {
Irfan Sheriff227bec42011-02-15 19:30:27 -0800742 Slog.e(TAG, "mWifiStateMachineChannel is not initialized");
Irfan Sheriff1406bcb2010-10-28 14:41:39 -0700743 return false;
744 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800745 }
746
747 /**
748 * See {@link android.net.wifi.WifiManager#getConnectionInfo()}
749 * @return the Wi-Fi information, contained in {@link WifiInfo}.
750 */
751 public WifiInfo getConnectionInfo() {
752 enforceAccessPermission();
753 /*
754 * Make sure we have the latest information, by sending
755 * a status request to the supplicant.
756 */
Irfan Sheriffd8134ff2010-08-22 17:06:34 -0700757 return mWifiStateMachine.syncRequestConnectionInfo();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800758 }
759
760 /**
761 * Return the results of the most recent access point scan, in the form of
762 * a list of {@link ScanResult} objects.
763 * @return the list of results
764 */
765 public List<ScanResult> getScanResults() {
766 enforceAccessPermission();
Irfan Sheriffd8134ff2010-08-22 17:06:34 -0700767 return mWifiStateMachine.syncGetScanResultsList();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800768 }
769
770 /**
771 * Tell the supplicant to persist the current list of configured networks.
772 * @return {@code true} if the operation succeeded
Irfan Sheriffa2a1b912010-06-07 09:03:04 -0700773 *
774 * TODO: deprecate this
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800775 */
776 public boolean saveConfiguration() {
Irfan Sheriffa2a1b912010-06-07 09:03:04 -0700777 boolean result = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800778 enforceChangePermission();
Irfan Sheriff227bec42011-02-15 19:30:27 -0800779 if (mWifiStateMachineChannel != null) {
780 return mWifiStateMachine.syncSaveConfig(mWifiStateMachineChannel);
Irfan Sheriff1406bcb2010-10-28 14:41:39 -0700781 } else {
Irfan Sheriff227bec42011-02-15 19:30:27 -0800782 Slog.e(TAG, "mWifiStateMachineChannel is not initialized");
Irfan Sheriff1406bcb2010-10-28 14:41:39 -0700783 return false;
784 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800785 }
786
787 /**
Irfan Sheriffed4f28b2010-10-29 15:32:10 -0700788 * Set the country code
789 * @param countryCode ISO 3166 country code.
Robert Greenwaltb5010cc2009-05-21 15:11:40 -0700790 * @param persist {@code true} if the setting should be remembered.
Irfan Sheriffed4f28b2010-10-29 15:32:10 -0700791 *
792 * The persist behavior exists so that wifi can fall back to the last
793 * persisted country code on a restart, when the locale information is
794 * not available from telephony.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800795 */
Irfan Sheriffed4f28b2010-10-29 15:32:10 -0700796 public void setCountryCode(String countryCode, boolean persist) {
797 Slog.i(TAG, "WifiService trying to set country code to " + countryCode +
798 " with persist set to " + persist);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800799 enforceChangePermission();
Irfan Sheriffed4f28b2010-10-29 15:32:10 -0700800 mWifiStateMachine.setCountryCode(countryCode, persist);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800801 }
802
803 /**
Irfan Sheriff36f74132010-11-04 16:57:37 -0700804 * Set the operational frequency band
805 * @param band One of
806 * {@link WifiManager#WIFI_FREQUENCY_BAND_AUTO},
807 * {@link WifiManager#WIFI_FREQUENCY_BAND_5GHZ},
808 * {@link WifiManager#WIFI_FREQUENCY_BAND_2GHZ},
809 * @param persist {@code true} if the setting should be remembered.
810 *
811 */
812 public void setFrequencyBand(int band, boolean persist) {
813 enforceChangePermission();
814 if (!isDualBandSupported()) return;
815 Slog.i(TAG, "WifiService trying to set frequency band to " + band +
816 " with persist set to " + persist);
817 mWifiStateMachine.setFrequencyBand(band, persist);
818 }
819
820
821 /**
822 * Get the operational frequency band
823 */
824 public int getFrequencyBand() {
825 enforceAccessPermission();
826 return mWifiStateMachine.getFrequencyBand();
827 }
828
829 public boolean isDualBandSupported() {
830 //TODO: Should move towards adding a driver API that checks at runtime
831 return mContext.getResources().getBoolean(
832 com.android.internal.R.bool.config_wifi_dual_band_support);
833 }
834
835 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800836 * Return the DHCP-assigned addresses from the last successful DHCP request,
837 * if any.
838 * @return the DHCP information
839 */
840 public DhcpInfo getDhcpInfo() {
841 enforceAccessPermission();
Irfan Sheriffd8134ff2010-08-22 17:06:34 -0700842 return mWifiStateMachine.syncGetDhcpInfo();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800843 }
844
Irfan Sheriff0d255342010-07-28 09:35:20 -0700845 /**
846 * see {@link android.net.wifi.WifiManager#startWifi}
847 *
848 */
849 public void startWifi() {
850 enforceChangePermission();
851 /* TODO: may be add permissions for access only to connectivity service
852 * TODO: if a start issued, keep wifi alive until a stop issued irrespective
853 * of WifiLock & device idle status unless wifi enabled status is toggled
854 */
855
Irfan Sheriff4494c902011-12-08 10:47:54 -0800856 mWifiStateMachine.setDriverStart(true, mEmergencyCallbackMode);
Irfan Sheriff0d255342010-07-28 09:35:20 -0700857 mWifiStateMachine.reconnectCommand();
858 }
859
860 /**
861 * see {@link android.net.wifi.WifiManager#stopWifi}
862 *
863 */
864 public void stopWifi() {
865 enforceChangePermission();
866 /* TODO: may be add permissions for access only to connectivity service
867 * TODO: if a stop is issued, wifi is brought up only by startWifi
868 * unless wifi enabled status is toggled
869 */
Irfan Sheriff4494c902011-12-08 10:47:54 -0800870 mWifiStateMachine.setDriverStart(false, mEmergencyCallbackMode);
Irfan Sheriff0d255342010-07-28 09:35:20 -0700871 }
872
873
874 /**
875 * see {@link android.net.wifi.WifiManager#addToBlacklist}
876 *
877 */
878 public void addToBlacklist(String bssid) {
879 enforceChangePermission();
880
881 mWifiStateMachine.addToBlacklist(bssid);
882 }
883
884 /**
885 * see {@link android.net.wifi.WifiManager#clearBlacklist}
886 *
887 */
888 public void clearBlacklist() {
889 enforceChangePermission();
890
891 mWifiStateMachine.clearBlacklist();
892 }
893
Irfan Sheriff227bec42011-02-15 19:30:27 -0800894 /**
895 * Get a reference to handler. This is used by a client to establish
896 * an AsyncChannel communication with WifiService
897 */
898 public Messenger getMessenger() {
Irfan Sheriffebe606f2011-02-24 11:39:15 -0800899 /* Enforce the highest permissions
900 TODO: when we consider exposing the asynchronous API, think about
901 how to provide both access and change permissions seperately
902 */
Irfan Sheriff227bec42011-02-15 19:30:27 -0800903 enforceAccessPermission();
Irfan Sheriffebe606f2011-02-24 11:39:15 -0800904 enforceChangePermission();
Irfan Sheriff227bec42011-02-15 19:30:27 -0800905 return new Messenger(mAsyncServiceHandler);
906 }
907
Irfan Sheriff4aeca7c52011-03-10 16:53:33 -0800908 /**
909 * Get the IP and proxy configuration file
910 */
911 public String getConfigFile() {
912 enforceAccessPermission();
913 return mWifiStateMachine.getConfigFile();
914 }
915
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800916 private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
917 @Override
918 public void onReceive(Context context, Intent intent) {
919 String action = intent.getAction();
920
Doug Zongker43866e02010-01-07 12:09:54 -0800921 long idleMillis =
922 Settings.Secure.getLong(mContext.getContentResolver(),
Irfan Sheriff4f5f7c92010-10-14 17:01:27 -0700923 Settings.Secure.WIFI_IDLE_MS, DEFAULT_IDLE_MS);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800924 int stayAwakeConditions =
Doug Zongker43866e02010-01-07 12:09:54 -0800925 Settings.System.getInt(mContext.getContentResolver(),
926 Settings.System.STAY_ON_WHILE_PLUGGED_IN, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800927 if (action.equals(Intent.ACTION_SCREEN_ON)) {
Joe Onorato431bb222010-10-18 19:13:23 -0400928 if (DBG) {
929 Slog.d(TAG, "ACTION_SCREEN_ON");
930 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800931 mAlarmManager.cancel(mIdleIntent);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800932 mScreenOff = false;
Irfan Sheriff227bec42011-02-15 19:30:27 -0800933 evaluateTrafficStatsPolling();
Irfan Sheriff0d255342010-07-28 09:35:20 -0700934 mWifiStateMachine.enableRssiPolling(true);
Irfan Sherifffcc08452011-02-17 16:44:54 -0800935 if (mBackgroundScanSupported) {
Irfan Sheriff2b7f6382011-03-25 14:29:19 -0700936 mWifiStateMachine.enableBackgroundScanCommand(false);
Irfan Sherifffcc08452011-02-17 16:44:54 -0800937 }
Irfan Sheriff8e86b892010-12-22 11:02:20 -0800938 mWifiStateMachine.enableAllNetworks();
Irfan Sheriffe6daca52011-11-03 15:46:50 -0700939 setDeviceIdleAndUpdateWifi(false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800940 } else if (action.equals(Intent.ACTION_SCREEN_OFF)) {
Joe Onorato431bb222010-10-18 19:13:23 -0400941 if (DBG) {
942 Slog.d(TAG, "ACTION_SCREEN_OFF");
943 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800944 mScreenOff = true;
Irfan Sheriff227bec42011-02-15 19:30:27 -0800945 evaluateTrafficStatsPolling();
Irfan Sheriff0d255342010-07-28 09:35:20 -0700946 mWifiStateMachine.enableRssiPolling(false);
Irfan Sherifffcc08452011-02-17 16:44:54 -0800947 if (mBackgroundScanSupported) {
Irfan Sheriff2b7f6382011-03-25 14:29:19 -0700948 mWifiStateMachine.enableBackgroundScanCommand(true);
Irfan Sherifffcc08452011-02-17 16:44:54 -0800949 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800950 /*
951 * Set a timer to put Wi-Fi to sleep, but only if the screen is off
952 * AND the "stay on while plugged in" setting doesn't match the
953 * current power conditions (i.e, not plugged in, plugged in to USB,
954 * or plugged in to AC).
955 */
956 if (!shouldWifiStayAwake(stayAwakeConditions, mPluggedType)) {
Irfan Sheriffe6daca52011-11-03 15:46:50 -0700957 //Delayed shutdown if wifi is connected
958 if (mNetworkInfo.getDetailedState() == DetailedState.CONNECTED) {
959 if (DBG) Slog.d(TAG, "setting ACTION_DEVICE_IDLE: " + idleMillis + " ms");
960 mAlarmManager.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis()
961 + idleMillis, mIdleIntent);
Mike Lockwoodd9c32bc2009-05-18 14:14:15 -0400962 } else {
Irfan Sheriffe6daca52011-11-03 15:46:50 -0700963 setDeviceIdleAndUpdateWifi(true);
Mike Lockwoodd9c32bc2009-05-18 14:14:15 -0400964 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800965 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800966 } else if (action.equals(ACTION_DEVICE_IDLE)) {
Irfan Sheriffe6daca52011-11-03 15:46:50 -0700967 setDeviceIdleAndUpdateWifi(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800968 } else if (action.equals(Intent.ACTION_BATTERY_CHANGED)) {
969 /*
970 * Set a timer to put Wi-Fi to sleep, but only if the screen is off
971 * AND we are transitioning from a state in which the device was supposed
972 * to stay awake to a state in which it is not supposed to stay awake.
973 * If "stay awake" state is not changing, we do nothing, to avoid resetting
974 * the already-set timer.
975 */
976 int pluggedType = intent.getIntExtra("plugged", 0);
Joe Onorato431bb222010-10-18 19:13:23 -0400977 if (DBG) {
978 Slog.d(TAG, "ACTION_BATTERY_CHANGED pluggedType: " + pluggedType);
979 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800980 if (mScreenOff && shouldWifiStayAwake(stayAwakeConditions, mPluggedType) &&
981 !shouldWifiStayAwake(stayAwakeConditions, pluggedType)) {
982 long triggerTime = System.currentTimeMillis() + idleMillis;
Joe Onorato431bb222010-10-18 19:13:23 -0400983 if (DBG) {
984 Slog.d(TAG, "setting ACTION_DEVICE_IDLE timer for " + idleMillis + "ms");
985 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800986 mAlarmManager.set(AlarmManager.RTC_WAKEUP, triggerTime, mIdleIntent);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800987 }
988 mPluggedType = pluggedType;
Irfan Sheriff65eaec82011-01-05 22:00:16 -0800989 } else if (action.equals(BluetoothAdapter.ACTION_CONNECTION_STATE_CHANGED)) {
990 int state = intent.getIntExtra(BluetoothAdapter.EXTRA_CONNECTION_STATE,
991 BluetoothAdapter.STATE_DISCONNECTED);
992 mWifiStateMachine.sendBluetoothAdapterStateChange(state);
Irfan Sheriff616f3172011-09-11 19:59:01 -0700993 } else if (action.equals(TelephonyIntents.ACTION_EMERGENCY_CALLBACK_MODE_CHANGED)) {
994 mEmergencyCallbackMode = intent.getBooleanExtra("phoneinECMState", false);
995 updateWifiState();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800996 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800997 }
998
999 /**
1000 * Determines whether the Wi-Fi chipset should stay awake or be put to
1001 * sleep. Looks at the setting for the sleep policy and the current
1002 * conditions.
Jaikumar Ganesh084c6652009-12-07 10:58:18 -08001003 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001004 * @see #shouldDeviceStayAwake(int, int)
1005 */
1006 private boolean shouldWifiStayAwake(int stayAwakeConditions, int pluggedType) {
Irfan Sheriff739f6bc2011-01-28 16:43:12 -08001007 //Never sleep as long as the user has not changed the settings
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001008 int wifiSleepPolicy = Settings.System.getInt(mContext.getContentResolver(),
Irfan Sheriff96b10d62011-01-11 15:40:35 -08001009 Settings.System.WIFI_SLEEP_POLICY,
Irfan Sheriff739f6bc2011-01-28 16:43:12 -08001010 Settings.System.WIFI_SLEEP_POLICY_NEVER);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001011
1012 if (wifiSleepPolicy == Settings.System.WIFI_SLEEP_POLICY_NEVER) {
1013 // Never sleep
1014 return true;
1015 } else if ((wifiSleepPolicy == Settings.System.WIFI_SLEEP_POLICY_NEVER_WHILE_PLUGGED) &&
1016 (pluggedType != 0)) {
1017 // Never sleep while plugged, and we're plugged
1018 return true;
1019 } else {
1020 // Default
1021 return shouldDeviceStayAwake(stayAwakeConditions, pluggedType);
1022 }
1023 }
Jaikumar Ganesh084c6652009-12-07 10:58:18 -08001024
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001025 /**
1026 * Determine whether the bit value corresponding to {@code pluggedType} is set in
1027 * the bit string {@code stayAwakeConditions}. Because a {@code pluggedType} value
1028 * of {@code 0} isn't really a plugged type, but rather an indication that the
1029 * device isn't plugged in at all, there is no bit value corresponding to a
1030 * {@code pluggedType} value of {@code 0}. That is why we shift by
Ben Dodson4e8620f2010-08-25 10:55:47 -07001031 * {@code pluggedType - 1} instead of by {@code pluggedType}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001032 * @param stayAwakeConditions a bit string specifying which "plugged types" should
1033 * keep the device (and hence Wi-Fi) awake.
1034 * @param pluggedType the type of plug (USB, AC, or none) for which the check is
1035 * being made
1036 * @return {@code true} if {@code pluggedType} indicates that the device is
1037 * supposed to stay awake, {@code false} otherwise.
1038 */
1039 private boolean shouldDeviceStayAwake(int stayAwakeConditions, int pluggedType) {
1040 return (stayAwakeConditions & pluggedType) != 0;
1041 }
1042 };
1043
Irfan Sheriffe6daca52011-11-03 15:46:50 -07001044 private void setDeviceIdleAndUpdateWifi(boolean deviceIdle) {
1045 mDeviceIdle = deviceIdle;
1046 reportStartWorkSource();
1047 updateWifiState();
1048 }
1049
Dianne Hackborn03f3cb02010-09-17 23:12:26 -07001050 private synchronized void reportStartWorkSource() {
1051 mTmpWorkSource.clear();
1052 if (mDeviceIdle) {
1053 for (int i=0; i<mLocks.mList.size(); i++) {
1054 mTmpWorkSource.add(mLocks.mList.get(i).mWorkSource);
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07001055 }
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07001056 }
Dianne Hackborn03f3cb02010-09-17 23:12:26 -07001057 mWifiStateMachine.updateBatteryWorkSource(mTmpWorkSource);
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07001058 }
Jaikumar Ganesh7440fc22010-09-27 17:04:14 -07001059
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001060 private void updateWifiState() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001061 boolean lockHeld = mLocks.hasLocks();
Irfan Sheriff5876a422010-08-12 20:26:23 -07001062 int strongestLockMode = WifiManager.WIFI_MODE_FULL;
Irfan Sheriff616f3172011-09-11 19:59:01 -07001063 boolean wifiShouldBeStarted;
1064
1065 if (mEmergencyCallbackMode) {
1066 wifiShouldBeStarted = false;
1067 } else {
1068 wifiShouldBeStarted = !mDeviceIdle || lockHeld;
1069 }
Irfan Sheriff5876a422010-08-12 20:26:23 -07001070
1071 if (lockHeld) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001072 strongestLockMode = mLocks.getStrongestLockMode();
Irfan Sheriff5876a422010-08-12 20:26:23 -07001073 }
1074 /* If device is not idle, lockmode cannot be scan only */
1075 if (!mDeviceIdle && strongestLockMode == WifiManager.WIFI_MODE_SCAN_ONLY) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001076 strongestLockMode = WifiManager.WIFI_MODE_FULL;
1077 }
1078
Irfan Sheriffa2a1b912010-06-07 09:03:04 -07001079 /* Disable tethering when airplane mode is enabled */
Irfan Sheriff658772f2011-03-08 14:52:31 -08001080 if (mAirplaneModeOn.get()) {
Irfan Sheriff0d255342010-07-28 09:35:20 -07001081 mWifiStateMachine.setWifiApEnabled(null, false);
Irfan Sheriffa2a1b912010-06-07 09:03:04 -07001082 }
Irfan Sheriffb2e6c012010-04-05 11:57:56 -07001083
Irfan Sheriff658772f2011-03-08 14:52:31 -08001084 if (shouldWifiBeEnabled()) {
Irfan Sheriffa2a1b912010-06-07 09:03:04 -07001085 if (wifiShouldBeStarted) {
Dianne Hackborn03f3cb02010-09-17 23:12:26 -07001086 reportStartWorkSource();
Irfan Sheriff0d255342010-07-28 09:35:20 -07001087 mWifiStateMachine.setWifiEnabled(true);
1088 mWifiStateMachine.setScanOnlyMode(
Irfan Sheriffa2a1b912010-06-07 09:03:04 -07001089 strongestLockMode == WifiManager.WIFI_MODE_SCAN_ONLY);
Irfan Sheriff4494c902011-12-08 10:47:54 -08001090 mWifiStateMachine.setDriverStart(true, mEmergencyCallbackMode);
Irfan Sheriff5876a422010-08-12 20:26:23 -07001091 mWifiStateMachine.setHighPerfModeEnabled(strongestLockMode
1092 == WifiManager.WIFI_MODE_FULL_HIGH_PERF);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001093 } else {
Irfan Sheriff4494c902011-12-08 10:47:54 -08001094 mWifiStateMachine.setDriverStart(false, mEmergencyCallbackMode);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001095 }
Irfan Sheriffa2a1b912010-06-07 09:03:04 -07001096 } else {
Irfan Sheriff0d255342010-07-28 09:35:20 -07001097 mWifiStateMachine.setWifiEnabled(false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001098 }
1099 }
1100
1101 private void registerForBroadcasts() {
1102 IntentFilter intentFilter = new IntentFilter();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001103 intentFilter.addAction(Intent.ACTION_SCREEN_ON);
1104 intentFilter.addAction(Intent.ACTION_SCREEN_OFF);
1105 intentFilter.addAction(Intent.ACTION_BATTERY_CHANGED);
1106 intentFilter.addAction(ACTION_DEVICE_IDLE);
Irfan Sheriff65eaec82011-01-05 22:00:16 -08001107 intentFilter.addAction(BluetoothAdapter.ACTION_CONNECTION_STATE_CHANGED);
Irfan Sheriff616f3172011-09-11 19:59:01 -07001108 intentFilter.addAction(TelephonyIntents.ACTION_EMERGENCY_CALLBACK_MODE_CHANGED);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001109 mContext.registerReceiver(mReceiver, intentFilter);
1110 }
Jaikumar Ganesh084c6652009-12-07 10:58:18 -08001111
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001112 private boolean isAirplaneSensitive() {
1113 String airplaneModeRadios = Settings.System.getString(mContext.getContentResolver(),
1114 Settings.System.AIRPLANE_MODE_RADIOS);
1115 return airplaneModeRadios == null
1116 || airplaneModeRadios.contains(Settings.System.RADIO_WIFI);
1117 }
1118
Mike Lockwoodbd5ddf02009-07-29 21:37:14 -07001119 private boolean isAirplaneToggleable() {
1120 String toggleableRadios = Settings.System.getString(mContext.getContentResolver(),
1121 Settings.System.AIRPLANE_MODE_TOGGLEABLE_RADIOS);
1122 return toggleableRadios != null
1123 && toggleableRadios.contains(Settings.System.RADIO_WIFI);
1124 }
1125
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001126 /**
1127 * Returns true if Wi-Fi is sensitive to airplane mode, and airplane mode is
1128 * currently on.
1129 * @return {@code true} if airplane mode is on.
1130 */
1131 private boolean isAirplaneModeOn() {
1132 return isAirplaneSensitive() && Settings.System.getInt(mContext.getContentResolver(),
1133 Settings.System.AIRPLANE_MODE_ON, 0) == 1;
1134 }
1135
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001136 @Override
1137 protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
1138 if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.DUMP)
1139 != PackageManager.PERMISSION_GRANTED) {
1140 pw.println("Permission Denial: can't dump WifiService from from pid="
1141 + Binder.getCallingPid()
1142 + ", uid=" + Binder.getCallingUid());
1143 return;
1144 }
Irfan Sheriffd8134ff2010-08-22 17:06:34 -07001145 pw.println("Wi-Fi is " + mWifiStateMachine.syncGetWifiStateByName());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001146 pw.println("Stay-awake conditions: " +
1147 Settings.System.getInt(mContext.getContentResolver(),
1148 Settings.System.STAY_ON_WHILE_PLUGGED_IN, 0));
1149 pw.println();
1150
1151 pw.println("Internal state:");
Irfan Sheriff0d255342010-07-28 09:35:20 -07001152 pw.println(mWifiStateMachine);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001153 pw.println();
1154 pw.println("Latest scan results:");
Irfan Sheriffd8134ff2010-08-22 17:06:34 -07001155 List<ScanResult> scanResults = mWifiStateMachine.syncGetScanResultsList();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001156 if (scanResults != null && scanResults.size() != 0) {
1157 pw.println(" BSSID Frequency RSSI Flags SSID");
1158 for (ScanResult r : scanResults) {
1159 pw.printf(" %17s %9d %5d %-16s %s%n",
1160 r.BSSID,
1161 r.frequency,
1162 r.level,
1163 r.capabilities,
1164 r.SSID == null ? "" : r.SSID);
1165 }
1166 }
1167 pw.println();
Eric Shienbrood5711fad2009-03-27 20:25:31 -07001168 pw.println("Locks acquired: " + mFullLocksAcquired + " full, " +
Irfan Sheriff5876a422010-08-12 20:26:23 -07001169 mFullHighPerfLocksAcquired + " full high perf, " +
Eric Shienbrood5711fad2009-03-27 20:25:31 -07001170 mScanLocksAcquired + " scan");
1171 pw.println("Locks released: " + mFullLocksReleased + " full, " +
Irfan Sheriff5876a422010-08-12 20:26:23 -07001172 mFullHighPerfLocksReleased + " full high perf, " +
Eric Shienbrood5711fad2009-03-27 20:25:31 -07001173 mScanLocksReleased + " scan");
1174 pw.println();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001175 pw.println("Locks held:");
1176 mLocks.dump(pw);
Isaac Levybc7dfb52011-06-06 15:34:01 -07001177
1178 pw.println();
Isaac Levy654f5092011-07-13 17:41:45 -07001179 pw.println("WifiWatchdogStateMachine dump");
1180 mWifiWatchdogStateMachine.dump(pw);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001181 }
1182
Robert Greenwalt58ff0212009-05-19 15:53:54 -07001183 private class WifiLock extends DeathRecipient {
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -07001184 WifiLock(int lockMode, String tag, IBinder binder, WorkSource ws) {
1185 super(lockMode, tag, binder, ws);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001186 }
1187
1188 public void binderDied() {
1189 synchronized (mLocks) {
1190 releaseWifiLockLocked(mBinder);
1191 }
1192 }
1193
1194 public String toString() {
Robert Greenwalt5347bd42009-05-13 15:10:16 -07001195 return "WifiLock{" + mTag + " type=" + mMode + " binder=" + mBinder + "}";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001196 }
1197 }
1198
1199 private class LockList {
1200 private List<WifiLock> mList;
1201
1202 private LockList() {
1203 mList = new ArrayList<WifiLock>();
1204 }
1205
1206 private synchronized boolean hasLocks() {
1207 return !mList.isEmpty();
1208 }
1209
1210 private synchronized int getStrongestLockMode() {
1211 if (mList.isEmpty()) {
1212 return WifiManager.WIFI_MODE_FULL;
1213 }
Irfan Sheriff5876a422010-08-12 20:26:23 -07001214
1215 if (mFullHighPerfLocksAcquired > mFullHighPerfLocksReleased) {
1216 return WifiManager.WIFI_MODE_FULL_HIGH_PERF;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001217 }
Irfan Sheriff5876a422010-08-12 20:26:23 -07001218
1219 if (mFullLocksAcquired > mFullLocksReleased) {
1220 return WifiManager.WIFI_MODE_FULL;
1221 }
1222
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001223 return WifiManager.WIFI_MODE_SCAN_ONLY;
1224 }
1225
1226 private void addLock(WifiLock lock) {
1227 if (findLockByBinder(lock.mBinder) < 0) {
1228 mList.add(lock);
1229 }
1230 }
1231
1232 private WifiLock removeLock(IBinder binder) {
1233 int index = findLockByBinder(binder);
1234 if (index >= 0) {
Suchi Amalapurapufff2fda2009-06-30 21:36:16 -07001235 WifiLock ret = mList.remove(index);
1236 ret.unlinkDeathRecipient();
1237 return ret;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001238 } else {
1239 return null;
1240 }
1241 }
1242
1243 private int findLockByBinder(IBinder binder) {
1244 int size = mList.size();
1245 for (int i = size - 1; i >= 0; i--)
1246 if (mList.get(i).mBinder == binder)
1247 return i;
1248 return -1;
1249 }
1250
1251 private void dump(PrintWriter pw) {
1252 for (WifiLock l : mList) {
1253 pw.print(" ");
1254 pw.println(l);
1255 }
1256 }
1257 }
1258
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -07001259 void enforceWakeSourcePermission(int uid, int pid) {
Dianne Hackborne746f032010-09-13 16:02:57 -07001260 if (uid == android.os.Process.myUid()) {
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -07001261 return;
1262 }
1263 mContext.enforcePermission(android.Manifest.permission.UPDATE_DEVICE_STATS,
1264 pid, uid, null);
1265 }
1266
1267 public boolean acquireWifiLock(IBinder binder, int lockMode, String tag, WorkSource ws) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001268 mContext.enforceCallingOrSelfPermission(android.Manifest.permission.WAKE_LOCK, null);
Irfan Sheriff5876a422010-08-12 20:26:23 -07001269 if (lockMode != WifiManager.WIFI_MODE_FULL &&
1270 lockMode != WifiManager.WIFI_MODE_SCAN_ONLY &&
1271 lockMode != WifiManager.WIFI_MODE_FULL_HIGH_PERF) {
1272 Slog.e(TAG, "Illegal argument, lockMode= " + lockMode);
1273 if (DBG) throw new IllegalArgumentException("lockMode=" + lockMode);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001274 return false;
1275 }
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -07001276 if (ws != null && ws.size() == 0) {
1277 ws = null;
1278 }
Dianne Hackbornecfd7f72010-10-08 14:23:40 -07001279 if (ws != null) {
1280 enforceWakeSourcePermission(Binder.getCallingUid(), Binder.getCallingPid());
1281 }
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -07001282 if (ws == null) {
1283 ws = new WorkSource(Binder.getCallingUid());
1284 }
1285 WifiLock wifiLock = new WifiLock(lockMode, tag, binder, ws);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001286 synchronized (mLocks) {
1287 return acquireWifiLockLocked(wifiLock);
1288 }
1289 }
1290
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -07001291 private void noteAcquireWifiLock(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.noteFullWifiLockAcquiredFromSource(wifiLock.mWorkSource);
1296 break;
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -07001297 case WifiManager.WIFI_MODE_SCAN_ONLY:
1298 mBatteryStats.noteScanWifiLockAcquiredFromSource(wifiLock.mWorkSource);
1299 break;
1300 }
1301 }
1302
1303 private void noteReleaseWifiLock(WifiLock wifiLock) throws RemoteException {
1304 switch(wifiLock.mMode) {
1305 case WifiManager.WIFI_MODE_FULL:
Irfan Sheriff5876a422010-08-12 20:26:23 -07001306 case WifiManager.WIFI_MODE_FULL_HIGH_PERF:
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -07001307 mBatteryStats.noteFullWifiLockReleasedFromSource(wifiLock.mWorkSource);
1308 break;
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -07001309 case WifiManager.WIFI_MODE_SCAN_ONLY:
1310 mBatteryStats.noteScanWifiLockReleasedFromSource(wifiLock.mWorkSource);
1311 break;
1312 }
1313 }
1314
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001315 private boolean acquireWifiLockLocked(WifiLock wifiLock) {
Irfan Sheriffc89dd542010-09-28 08:40:54 -07001316 if (DBG) Slog.d(TAG, "acquireWifiLockLocked: " + wifiLock);
Robert Greenwaltf1acb2d2009-10-13 08:20:55 -07001317
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001318 mLocks.addLock(wifiLock);
Robert Greenwaltf1acb2d2009-10-13 08:20:55 -07001319
The Android Open Source Project10592532009-03-18 17:39:46 -07001320 long ident = Binder.clearCallingIdentity();
1321 try {
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -07001322 noteAcquireWifiLock(wifiLock);
Robert Greenwalt5347bd42009-05-13 15:10:16 -07001323 switch(wifiLock.mMode) {
Eric Shienbrood5711fad2009-03-27 20:25:31 -07001324 case WifiManager.WIFI_MODE_FULL:
1325 ++mFullLocksAcquired;
Eric Shienbrood5711fad2009-03-27 20:25:31 -07001326 break;
Irfan Sheriff5876a422010-08-12 20:26:23 -07001327 case WifiManager.WIFI_MODE_FULL_HIGH_PERF:
1328 ++mFullHighPerfLocksAcquired;
1329 break;
1330
Eric Shienbrood5711fad2009-03-27 20:25:31 -07001331 case WifiManager.WIFI_MODE_SCAN_ONLY:
1332 ++mScanLocksAcquired;
Eric Shienbrood5711fad2009-03-27 20:25:31 -07001333 break;
The Android Open Source Project10592532009-03-18 17:39:46 -07001334 }
Dianne Hackbornecfd7f72010-10-08 14:23:40 -07001335
1336 // Be aggressive about adding new locks into the accounted state...
1337 // we want to over-report rather than under-report.
1338 reportStartWorkSource();
1339
1340 updateWifiState();
1341 return true;
The Android Open Source Project10592532009-03-18 17:39:46 -07001342 } catch (RemoteException e) {
Dianne Hackbornecfd7f72010-10-08 14:23:40 -07001343 return false;
The Android Open Source Project10592532009-03-18 17:39:46 -07001344 } finally {
1345 Binder.restoreCallingIdentity(ident);
1346 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001347 }
1348
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -07001349 public void updateWifiLockWorkSource(IBinder lock, WorkSource ws) {
1350 int uid = Binder.getCallingUid();
1351 int pid = Binder.getCallingPid();
1352 if (ws != null && ws.size() == 0) {
1353 ws = null;
1354 }
1355 if (ws != null) {
1356 enforceWakeSourcePermission(uid, pid);
1357 }
1358 long ident = Binder.clearCallingIdentity();
1359 try {
1360 synchronized (mLocks) {
1361 int index = mLocks.findLockByBinder(lock);
1362 if (index < 0) {
1363 throw new IllegalArgumentException("Wifi lock not active");
1364 }
1365 WifiLock wl = mLocks.mList.get(index);
1366 noteReleaseWifiLock(wl);
1367 wl.mWorkSource = ws != null ? new WorkSource(ws) : new WorkSource(uid);
1368 noteAcquireWifiLock(wl);
1369 }
1370 } catch (RemoteException e) {
1371 } finally {
1372 Binder.restoreCallingIdentity(ident);
1373 }
1374 }
1375
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001376 public boolean releaseWifiLock(IBinder lock) {
1377 mContext.enforceCallingOrSelfPermission(android.Manifest.permission.WAKE_LOCK, null);
1378 synchronized (mLocks) {
1379 return releaseWifiLockLocked(lock);
1380 }
1381 }
1382
1383 private boolean releaseWifiLockLocked(IBinder lock) {
Eric Shienbroodd4c5f892009-03-24 18:13:20 -07001384 boolean hadLock;
Robert Greenwaltf1acb2d2009-10-13 08:20:55 -07001385
The Android Open Source Project10592532009-03-18 17:39:46 -07001386 WifiLock wifiLock = mLocks.removeLock(lock);
Robert Greenwaltf1acb2d2009-10-13 08:20:55 -07001387
Irfan Sheriffc89dd542010-09-28 08:40:54 -07001388 if (DBG) Slog.d(TAG, "releaseWifiLockLocked: " + wifiLock);
Robert Greenwaltf1acb2d2009-10-13 08:20:55 -07001389
Eric Shienbroodd4c5f892009-03-24 18:13:20 -07001390 hadLock = (wifiLock != null);
1391
Dianne Hackbornecfd7f72010-10-08 14:23:40 -07001392 long ident = Binder.clearCallingIdentity();
1393 try {
1394 if (hadLock) {
Wink Savillece0ea1f2b2011-10-13 16:55:20 -07001395 noteReleaseWifiLock(wifiLock);
Robert Greenwalt5347bd42009-05-13 15:10:16 -07001396 switch(wifiLock.mMode) {
Eric Shienbrood5711fad2009-03-27 20:25:31 -07001397 case WifiManager.WIFI_MODE_FULL:
1398 ++mFullLocksReleased;
Eric Shienbrood5711fad2009-03-27 20:25:31 -07001399 break;
Irfan Sheriff5876a422010-08-12 20:26:23 -07001400 case WifiManager.WIFI_MODE_FULL_HIGH_PERF:
1401 ++mFullHighPerfLocksReleased;
1402 break;
Eric Shienbrood5711fad2009-03-27 20:25:31 -07001403 case WifiManager.WIFI_MODE_SCAN_ONLY:
1404 ++mScanLocksReleased;
Eric Shienbrood5711fad2009-03-27 20:25:31 -07001405 break;
Eric Shienbroodd4c5f892009-03-24 18:13:20 -07001406 }
The Android Open Source Project10592532009-03-18 17:39:46 -07001407 }
Dianne Hackbornecfd7f72010-10-08 14:23:40 -07001408
1409 // TODO - should this only happen if you hadLock?
1410 updateWifiState();
1411
1412 } catch (RemoteException e) {
1413 } finally {
1414 Binder.restoreCallingIdentity(ident);
The Android Open Source Project10592532009-03-18 17:39:46 -07001415 }
Dianne Hackbornecfd7f72010-10-08 14:23:40 -07001416
Eric Shienbroodd4c5f892009-03-24 18:13:20 -07001417 return hadLock;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001418 }
Robert Greenwalt5347bd42009-05-13 15:10:16 -07001419
Robert Greenwalt58ff0212009-05-19 15:53:54 -07001420 private abstract class DeathRecipient
Robert Greenwalt5347bd42009-05-13 15:10:16 -07001421 implements IBinder.DeathRecipient {
1422 String mTag;
1423 int mMode;
1424 IBinder mBinder;
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -07001425 WorkSource mWorkSource;
Robert Greenwalt5347bd42009-05-13 15:10:16 -07001426
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -07001427 DeathRecipient(int mode, String tag, IBinder binder, WorkSource ws) {
Robert Greenwalt5347bd42009-05-13 15:10:16 -07001428 super();
1429 mTag = tag;
1430 mMode = mode;
1431 mBinder = binder;
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -07001432 mWorkSource = ws;
Robert Greenwalt5347bd42009-05-13 15:10:16 -07001433 try {
1434 mBinder.linkToDeath(this, 0);
1435 } catch (RemoteException e) {
1436 binderDied();
1437 }
1438 }
Suchi Amalapurapufff2fda2009-06-30 21:36:16 -07001439
1440 void unlinkDeathRecipient() {
1441 mBinder.unlinkToDeath(this, 0);
1442 }
Robert Greenwalt5347bd42009-05-13 15:10:16 -07001443 }
1444
Robert Greenwalt58ff0212009-05-19 15:53:54 -07001445 private class Multicaster extends DeathRecipient {
1446 Multicaster(String tag, IBinder binder) {
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -07001447 super(Binder.getCallingUid(), tag, binder, null);
Robert Greenwalt5347bd42009-05-13 15:10:16 -07001448 }
1449
1450 public void binderDied() {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001451 Slog.e(TAG, "Multicaster binderDied");
Robert Greenwalt5347bd42009-05-13 15:10:16 -07001452 synchronized (mMulticasters) {
1453 int i = mMulticasters.indexOf(this);
1454 if (i != -1) {
1455 removeMulticasterLocked(i, mMode);
1456 }
1457 }
1458 }
1459
1460 public String toString() {
Robert Greenwalt58ff0212009-05-19 15:53:54 -07001461 return "Multicaster{" + mTag + " binder=" + mBinder + "}";
Robert Greenwalt5347bd42009-05-13 15:10:16 -07001462 }
1463
1464 public int getUid() {
1465 return mMode;
1466 }
1467 }
1468
Robert Greenwalte2d155a2009-10-21 14:58:34 -07001469 public void initializeMulticastFiltering() {
1470 enforceMulticastChangePermission();
Irfan Sheriffa8fbe1f2010-03-09 09:13:58 -08001471
Robert Greenwalte2d155a2009-10-21 14:58:34 -07001472 synchronized (mMulticasters) {
1473 // if anybody had requested filters be off, leave off
1474 if (mMulticasters.size() != 0) {
1475 return;
1476 } else {
Irfan Sheriffb0c1b80f2011-07-19 15:44:25 -07001477 mWifiStateMachine.startFilteringMulticastV4Packets();
Robert Greenwalte2d155a2009-10-21 14:58:34 -07001478 }
1479 }
1480 }
1481
Robert Greenwaltfc1b15c2009-05-22 15:09:51 -07001482 public void acquireMulticastLock(IBinder binder, String tag) {
1483 enforceMulticastChangePermission();
Robert Greenwalt5347bd42009-05-13 15:10:16 -07001484
1485 synchronized (mMulticasters) {
1486 mMulticastEnabled++;
Robert Greenwalt58ff0212009-05-19 15:53:54 -07001487 mMulticasters.add(new Multicaster(tag, binder));
Irfan Sheriffb0c1b80f2011-07-19 15:44:25 -07001488 // Note that we could call stopFilteringMulticastV4Packets only when
Robert Greenwalt5347bd42009-05-13 15:10:16 -07001489 // our new size == 1 (first call), but this function won't
1490 // be called often and by making the stopPacket call each
1491 // time we're less fragile and self-healing.
Irfan Sheriffb0c1b80f2011-07-19 15:44:25 -07001492 mWifiStateMachine.stopFilteringMulticastV4Packets();
Robert Greenwalt5347bd42009-05-13 15:10:16 -07001493 }
1494
1495 int uid = Binder.getCallingUid();
1496 Long ident = Binder.clearCallingIdentity();
1497 try {
1498 mBatteryStats.noteWifiMulticastEnabled(uid);
1499 } catch (RemoteException e) {
1500 } finally {
1501 Binder.restoreCallingIdentity(ident);
1502 }
1503 }
1504
Robert Greenwaltfc1b15c2009-05-22 15:09:51 -07001505 public void releaseMulticastLock() {
1506 enforceMulticastChangePermission();
Robert Greenwalt5347bd42009-05-13 15:10:16 -07001507
1508 int uid = Binder.getCallingUid();
1509 synchronized (mMulticasters) {
1510 mMulticastDisabled++;
1511 int size = mMulticasters.size();
1512 for (int i = size - 1; i >= 0; i--) {
Robert Greenwalt58ff0212009-05-19 15:53:54 -07001513 Multicaster m = mMulticasters.get(i);
Robert Greenwalt5347bd42009-05-13 15:10:16 -07001514 if ((m != null) && (m.getUid() == uid)) {
1515 removeMulticasterLocked(i, uid);
1516 }
1517 }
1518 }
1519 }
1520
1521 private void removeMulticasterLocked(int i, int uid)
1522 {
Suchi Amalapurapufff2fda2009-06-30 21:36:16 -07001523 Multicaster removed = mMulticasters.remove(i);
Irfan Sheriffa8fbe1f2010-03-09 09:13:58 -08001524
Suchi Amalapurapufff2fda2009-06-30 21:36:16 -07001525 if (removed != null) {
1526 removed.unlinkDeathRecipient();
1527 }
Robert Greenwalt5347bd42009-05-13 15:10:16 -07001528 if (mMulticasters.size() == 0) {
Irfan Sheriffb0c1b80f2011-07-19 15:44:25 -07001529 mWifiStateMachine.startFilteringMulticastV4Packets();
Robert Greenwalt5347bd42009-05-13 15:10:16 -07001530 }
1531
1532 Long ident = Binder.clearCallingIdentity();
1533 try {
1534 mBatteryStats.noteWifiMulticastDisabled(uid);
1535 } catch (RemoteException e) {
1536 } finally {
1537 Binder.restoreCallingIdentity(ident);
1538 }
1539 }
1540
Robert Greenwalt58ff0212009-05-19 15:53:54 -07001541 public boolean isMulticastEnabled() {
Robert Greenwalt5347bd42009-05-13 15:10:16 -07001542 enforceAccessPermission();
1543
1544 synchronized (mMulticasters) {
1545 return (mMulticasters.size() > 0);
1546 }
1547 }
Irfan Sheriff0d255342010-07-28 09:35:20 -07001548
Irfan Sheriff227bec42011-02-15 19:30:27 -08001549 /**
1550 * Evaluate if traffic stats polling is needed based on
1551 * connection and screen on status
1552 */
1553 private void evaluateTrafficStatsPolling() {
1554 Message msg;
1555 if (mNetworkInfo.getDetailedState() == DetailedState.CONNECTED && !mScreenOff) {
Irfan Sheriffebe606f2011-02-24 11:39:15 -08001556 msg = Message.obtain(mAsyncServiceHandler,
1557 WifiManager.CMD_ENABLE_TRAFFIC_STATS_POLL, 1, 0);
Irfan Sheriff227bec42011-02-15 19:30:27 -08001558 } else {
Irfan Sheriffebe606f2011-02-24 11:39:15 -08001559 msg = Message.obtain(mAsyncServiceHandler,
1560 WifiManager.CMD_ENABLE_TRAFFIC_STATS_POLL, 0, 0);
Irfan Sheriff227bec42011-02-15 19:30:27 -08001561 }
1562 msg.sendToTarget();
1563 }
1564
1565 private void notifyOnDataActivity() {
1566 long sent, received;
1567 long preTxPkts = mTxPkts, preRxPkts = mRxPkts;
1568 int dataActivity = WifiManager.DATA_ACTIVITY_NONE;
1569
1570 mTxPkts = TrafficStats.getTxPackets(mInterfaceName);
1571 mRxPkts = TrafficStats.getRxPackets(mInterfaceName);
1572
1573 if (preTxPkts > 0 || preRxPkts > 0) {
1574 sent = mTxPkts - preTxPkts;
1575 received = mRxPkts - preRxPkts;
1576 if (sent > 0) {
1577 dataActivity |= WifiManager.DATA_ACTIVITY_OUT;
1578 }
1579 if (received > 0) {
1580 dataActivity |= WifiManager.DATA_ACTIVITY_IN;
1581 }
1582
1583 if (dataActivity != mDataActivity && !mScreenOff) {
1584 mDataActivity = dataActivity;
1585 for (AsyncChannel client : mClients) {
1586 client.sendMessage(WifiManager.DATA_ACTIVITY_NOTIFICATION, mDataActivity);
1587 }
1588 }
1589 }
1590 }
1591
1592
Irfan Sheriff0d255342010-07-28 09:35:20 -07001593 private void checkAndSetNotification() {
1594 // If we shouldn't place a notification on available networks, then
1595 // don't bother doing any of the following
1596 if (!mNotificationEnabled) return;
1597
1598 State state = mNetworkInfo.getState();
1599 if ((state == NetworkInfo.State.DISCONNECTED)
1600 || (state == NetworkInfo.State.UNKNOWN)) {
1601 // Look for an open network
Irfan Sheriffd8134ff2010-08-22 17:06:34 -07001602 List<ScanResult> scanResults = mWifiStateMachine.syncGetScanResultsList();
Irfan Sheriff0d255342010-07-28 09:35:20 -07001603 if (scanResults != null) {
1604 int numOpenNetworks = 0;
1605 for (int i = scanResults.size() - 1; i >= 0; i--) {
1606 ScanResult scanResult = scanResults.get(i);
1607
Irfan Sherifffdd5f952011-08-04 16:55:54 -07001608 //A capability of [ESS] represents an open access point
1609 //that is available for an STA to connect
1610 if (scanResult.capabilities != null &&
1611 scanResult.capabilities.equals("[ESS]")) {
Irfan Sheriff0d255342010-07-28 09:35:20 -07001612 numOpenNetworks++;
1613 }
1614 }
1615
1616 if (numOpenNetworks > 0) {
1617 if (++mNumScansSinceNetworkStateChange >= NUM_SCANS_BEFORE_ACTUALLY_SCANNING) {
1618 /*
1619 * We've scanned continuously at least
1620 * NUM_SCANS_BEFORE_NOTIFICATION times. The user
1621 * probably does not have a remembered network in range,
1622 * since otherwise supplicant would have tried to
1623 * associate and thus resetting this counter.
1624 */
1625 setNotificationVisible(true, numOpenNetworks, false, 0);
1626 }
1627 return;
1628 }
1629 }
1630 }
1631
1632 // No open networks in range, remove the notification
1633 setNotificationVisible(false, 0, false, 0);
1634 }
1635
1636 /**
1637 * Clears variables related to tracking whether a notification has been
1638 * shown recently and clears the current notification.
1639 */
1640 private void resetNotification() {
1641 mNotificationRepeatTime = 0;
1642 mNumScansSinceNetworkStateChange = 0;
1643 setNotificationVisible(false, 0, false, 0);
1644 }
1645
1646 /**
1647 * Display or don't display a notification that there are open Wi-Fi networks.
1648 * @param visible {@code true} if notification should be visible, {@code false} otherwise
1649 * @param numNetworks the number networks seen
1650 * @param force {@code true} to force notification to be shown/not-shown,
1651 * even if it is already shown/not-shown.
1652 * @param delay time in milliseconds after which the notification should be made
1653 * visible or invisible.
1654 */
1655 private void setNotificationVisible(boolean visible, int numNetworks, boolean force,
1656 int delay) {
1657
1658 // Since we use auto cancel on the notification, when the
1659 // mNetworksAvailableNotificationShown is true, the notification may
1660 // have actually been canceled. However, when it is false we know
1661 // for sure that it is not being shown (it will not be shown any other
1662 // place than here)
1663
1664 // If it should be hidden and it is already hidden, then noop
1665 if (!visible && !mNotificationShown && !force) {
1666 return;
1667 }
1668
1669 NotificationManager notificationManager = (NotificationManager) mContext
1670 .getSystemService(Context.NOTIFICATION_SERVICE);
1671
1672 Message message;
1673 if (visible) {
1674
1675 // Not enough time has passed to show the notification again
1676 if (System.currentTimeMillis() < mNotificationRepeatTime) {
1677 return;
1678 }
1679
1680 if (mNotification == null) {
Wink Savillec7a98342010-08-13 16:11:42 -07001681 // Cache the Notification object.
Irfan Sheriff0d255342010-07-28 09:35:20 -07001682 mNotification = new Notification();
1683 mNotification.when = 0;
1684 mNotification.icon = ICON_NETWORKS_AVAILABLE;
1685 mNotification.flags = Notification.FLAG_AUTO_CANCEL;
1686 mNotification.contentIntent = PendingIntent.getActivity(mContext, 0,
1687 new Intent(WifiManager.ACTION_PICK_WIFI_NETWORK), 0);
1688 }
1689
1690 CharSequence title = mContext.getResources().getQuantityText(
1691 com.android.internal.R.plurals.wifi_available, numNetworks);
1692 CharSequence details = mContext.getResources().getQuantityText(
1693 com.android.internal.R.plurals.wifi_available_detailed, numNetworks);
1694 mNotification.tickerText = title;
1695 mNotification.setLatestEventInfo(mContext, title, details, mNotification.contentIntent);
1696
1697 mNotificationRepeatTime = System.currentTimeMillis() + NOTIFICATION_REPEAT_DELAY_MS;
1698
1699 notificationManager.notify(ICON_NETWORKS_AVAILABLE, mNotification);
Irfan Sheriff0d255342010-07-28 09:35:20 -07001700 } else {
Irfan Sheriff0d255342010-07-28 09:35:20 -07001701 notificationManager.cancel(ICON_NETWORKS_AVAILABLE);
Irfan Sheriff0d255342010-07-28 09:35:20 -07001702 }
1703
Irfan Sheriff0d255342010-07-28 09:35:20 -07001704 mNotificationShown = visible;
1705 }
1706
1707 private class NotificationEnabledSettingObserver extends ContentObserver {
1708
1709 public NotificationEnabledSettingObserver(Handler handler) {
1710 super(handler);
1711 }
1712
1713 public void register() {
1714 ContentResolver cr = mContext.getContentResolver();
1715 cr.registerContentObserver(Settings.Secure.getUriFor(
1716 Settings.Secure.WIFI_NETWORKS_AVAILABLE_NOTIFICATION_ON), true, this);
1717 mNotificationEnabled = getValue();
1718 }
1719
1720 @Override
1721 public void onChange(boolean selfChange) {
1722 super.onChange(selfChange);
1723
1724 mNotificationEnabled = getValue();
1725 resetNotification();
1726 }
1727
1728 private boolean getValue() {
1729 return Settings.Secure.getInt(mContext.getContentResolver(),
1730 Settings.Secure.WIFI_NETWORKS_AVAILABLE_NOTIFICATION_ON, 1) == 1;
1731 }
1732 }
1733
1734
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001735}