blob: 016dc82b10f724aea3092d8b51f65d0c6839a851 [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 Sheriffe744cff2011-12-11 09:17:50 -0800679 if (mWifiStateMachineChannel != null) {
680 return mWifiStateMachine.syncGetConfiguredNetworks(mWifiStateMachineChannel);
681 } else {
682 Slog.e(TAG, "mWifiStateMachineChannel is not initialized");
683 return null;
684 }
Chung-yih Wanga8d15942009-10-09 11:01:49 +0800685 }
686
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800687 /**
688 * see {@link android.net.wifi.WifiManager#addOrUpdateNetwork(WifiConfiguration)}
689 * @return the supplicant-assigned identifier for the new or updated
690 * network if the operation succeeds, or {@code -1} if it fails
691 */
Irfan Sheriff7aac5542009-12-22 21:42:17 -0800692 public int addOrUpdateNetwork(WifiConfiguration config) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800693 enforceChangePermission();
Irfan Sheriff227bec42011-02-15 19:30:27 -0800694 if (mWifiStateMachineChannel != null) {
695 return mWifiStateMachine.syncAddOrUpdateNetwork(mWifiStateMachineChannel, config);
Irfan Sheriff1406bcb2010-10-28 14:41:39 -0700696 } else {
Irfan Sheriff227bec42011-02-15 19:30:27 -0800697 Slog.e(TAG, "mWifiStateMachineChannel is not initialized");
Irfan Sheriff1406bcb2010-10-28 14:41:39 -0700698 return -1;
699 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800700 }
701
Irfan Sheriffa2a1b912010-06-07 09:03:04 -0700702 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800703 * See {@link android.net.wifi.WifiManager#removeNetwork(int)}
704 * @param netId the integer that identifies the network configuration
705 * to the supplicant
706 * @return {@code true} if the operation succeeded
707 */
708 public boolean removeNetwork(int netId) {
709 enforceChangePermission();
Irfan Sheriff227bec42011-02-15 19:30:27 -0800710 if (mWifiStateMachineChannel != null) {
711 return mWifiStateMachine.syncRemoveNetwork(mWifiStateMachineChannel, netId);
Wink Saville4b7ba092010-10-20 15:37:41 -0700712 } else {
Irfan Sheriff227bec42011-02-15 19:30:27 -0800713 Slog.e(TAG, "mWifiStateMachineChannel is not initialized");
Wink Saville4b7ba092010-10-20 15:37:41 -0700714 return false;
715 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800716 }
717
718 /**
719 * See {@link android.net.wifi.WifiManager#enableNetwork(int, boolean)}
720 * @param netId the integer that identifies the network configuration
721 * to the supplicant
722 * @param disableOthers if true, disable all other networks.
723 * @return {@code true} if the operation succeeded
724 */
725 public boolean enableNetwork(int netId, boolean disableOthers) {
726 enforceChangePermission();
Irfan Sheriff227bec42011-02-15 19:30:27 -0800727 if (mWifiStateMachineChannel != null) {
728 return mWifiStateMachine.syncEnableNetwork(mWifiStateMachineChannel, netId,
729 disableOthers);
Irfan Sheriff1406bcb2010-10-28 14:41:39 -0700730 } else {
Irfan Sheriff227bec42011-02-15 19:30:27 -0800731 Slog.e(TAG, "mWifiStateMachineChannel is not initialized");
Irfan Sheriff1406bcb2010-10-28 14:41:39 -0700732 return false;
733 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800734 }
735
736 /**
737 * See {@link android.net.wifi.WifiManager#disableNetwork(int)}
738 * @param netId the integer that identifies the network configuration
739 * to the supplicant
740 * @return {@code true} if the operation succeeded
741 */
742 public boolean disableNetwork(int netId) {
743 enforceChangePermission();
Irfan Sheriff227bec42011-02-15 19:30:27 -0800744 if (mWifiStateMachineChannel != null) {
745 return mWifiStateMachine.syncDisableNetwork(mWifiStateMachineChannel, netId);
Irfan Sheriff1406bcb2010-10-28 14:41:39 -0700746 } else {
Irfan Sheriff227bec42011-02-15 19:30:27 -0800747 Slog.e(TAG, "mWifiStateMachineChannel is not initialized");
Irfan Sheriff1406bcb2010-10-28 14:41:39 -0700748 return false;
749 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800750 }
751
752 /**
753 * See {@link android.net.wifi.WifiManager#getConnectionInfo()}
754 * @return the Wi-Fi information, contained in {@link WifiInfo}.
755 */
756 public WifiInfo getConnectionInfo() {
757 enforceAccessPermission();
758 /*
759 * Make sure we have the latest information, by sending
760 * a status request to the supplicant.
761 */
Irfan Sheriffd8134ff2010-08-22 17:06:34 -0700762 return mWifiStateMachine.syncRequestConnectionInfo();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800763 }
764
765 /**
766 * Return the results of the most recent access point scan, in the form of
767 * a list of {@link ScanResult} objects.
768 * @return the list of results
769 */
770 public List<ScanResult> getScanResults() {
771 enforceAccessPermission();
Irfan Sheriffd8134ff2010-08-22 17:06:34 -0700772 return mWifiStateMachine.syncGetScanResultsList();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800773 }
774
775 /**
776 * Tell the supplicant to persist the current list of configured networks.
777 * @return {@code true} if the operation succeeded
Irfan Sheriffa2a1b912010-06-07 09:03:04 -0700778 *
779 * TODO: deprecate this
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800780 */
781 public boolean saveConfiguration() {
Irfan Sheriffa2a1b912010-06-07 09:03:04 -0700782 boolean result = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800783 enforceChangePermission();
Irfan Sheriff227bec42011-02-15 19:30:27 -0800784 if (mWifiStateMachineChannel != null) {
785 return mWifiStateMachine.syncSaveConfig(mWifiStateMachineChannel);
Irfan Sheriff1406bcb2010-10-28 14:41:39 -0700786 } else {
Irfan Sheriff227bec42011-02-15 19:30:27 -0800787 Slog.e(TAG, "mWifiStateMachineChannel is not initialized");
Irfan Sheriff1406bcb2010-10-28 14:41:39 -0700788 return false;
789 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800790 }
791
792 /**
Irfan Sheriffed4f28b2010-10-29 15:32:10 -0700793 * Set the country code
794 * @param countryCode ISO 3166 country code.
Robert Greenwaltb5010cc2009-05-21 15:11:40 -0700795 * @param persist {@code true} if the setting should be remembered.
Irfan Sheriffed4f28b2010-10-29 15:32:10 -0700796 *
797 * The persist behavior exists so that wifi can fall back to the last
798 * persisted country code on a restart, when the locale information is
799 * not available from telephony.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800800 */
Irfan Sheriffed4f28b2010-10-29 15:32:10 -0700801 public void setCountryCode(String countryCode, boolean persist) {
802 Slog.i(TAG, "WifiService trying to set country code to " + countryCode +
803 " with persist set to " + persist);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800804 enforceChangePermission();
Irfan Sheriffed4f28b2010-10-29 15:32:10 -0700805 mWifiStateMachine.setCountryCode(countryCode, persist);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800806 }
807
808 /**
Irfan Sheriff36f74132010-11-04 16:57:37 -0700809 * Set the operational frequency band
810 * @param band One of
811 * {@link WifiManager#WIFI_FREQUENCY_BAND_AUTO},
812 * {@link WifiManager#WIFI_FREQUENCY_BAND_5GHZ},
813 * {@link WifiManager#WIFI_FREQUENCY_BAND_2GHZ},
814 * @param persist {@code true} if the setting should be remembered.
815 *
816 */
817 public void setFrequencyBand(int band, boolean persist) {
818 enforceChangePermission();
819 if (!isDualBandSupported()) return;
820 Slog.i(TAG, "WifiService trying to set frequency band to " + band +
821 " with persist set to " + persist);
822 mWifiStateMachine.setFrequencyBand(band, persist);
823 }
824
825
826 /**
827 * Get the operational frequency band
828 */
829 public int getFrequencyBand() {
830 enforceAccessPermission();
831 return mWifiStateMachine.getFrequencyBand();
832 }
833
834 public boolean isDualBandSupported() {
835 //TODO: Should move towards adding a driver API that checks at runtime
836 return mContext.getResources().getBoolean(
837 com.android.internal.R.bool.config_wifi_dual_band_support);
838 }
839
840 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800841 * Return the DHCP-assigned addresses from the last successful DHCP request,
842 * if any.
843 * @return the DHCP information
844 */
845 public DhcpInfo getDhcpInfo() {
846 enforceAccessPermission();
Irfan Sheriffd8134ff2010-08-22 17:06:34 -0700847 return mWifiStateMachine.syncGetDhcpInfo();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800848 }
849
Irfan Sheriff0d255342010-07-28 09:35:20 -0700850 /**
851 * see {@link android.net.wifi.WifiManager#startWifi}
852 *
853 */
854 public void startWifi() {
855 enforceChangePermission();
856 /* TODO: may be add permissions for access only to connectivity service
857 * TODO: if a start issued, keep wifi alive until a stop issued irrespective
858 * of WifiLock & device idle status unless wifi enabled status is toggled
859 */
860
Irfan Sheriff4494c902011-12-08 10:47:54 -0800861 mWifiStateMachine.setDriverStart(true, mEmergencyCallbackMode);
Irfan Sheriff0d255342010-07-28 09:35:20 -0700862 mWifiStateMachine.reconnectCommand();
863 }
864
865 /**
866 * see {@link android.net.wifi.WifiManager#stopWifi}
867 *
868 */
869 public void stopWifi() {
870 enforceChangePermission();
871 /* TODO: may be add permissions for access only to connectivity service
872 * TODO: if a stop is issued, wifi is brought up only by startWifi
873 * unless wifi enabled status is toggled
874 */
Irfan Sheriff4494c902011-12-08 10:47:54 -0800875 mWifiStateMachine.setDriverStart(false, mEmergencyCallbackMode);
Irfan Sheriff0d255342010-07-28 09:35:20 -0700876 }
877
878
879 /**
880 * see {@link android.net.wifi.WifiManager#addToBlacklist}
881 *
882 */
883 public void addToBlacklist(String bssid) {
884 enforceChangePermission();
885
886 mWifiStateMachine.addToBlacklist(bssid);
887 }
888
889 /**
890 * see {@link android.net.wifi.WifiManager#clearBlacklist}
891 *
892 */
893 public void clearBlacklist() {
894 enforceChangePermission();
895
896 mWifiStateMachine.clearBlacklist();
897 }
898
Irfan Sheriff227bec42011-02-15 19:30:27 -0800899 /**
900 * Get a reference to handler. This is used by a client to establish
901 * an AsyncChannel communication with WifiService
902 */
903 public Messenger getMessenger() {
Irfan Sheriffebe606f2011-02-24 11:39:15 -0800904 /* Enforce the highest permissions
905 TODO: when we consider exposing the asynchronous API, think about
906 how to provide both access and change permissions seperately
907 */
Irfan Sheriff227bec42011-02-15 19:30:27 -0800908 enforceAccessPermission();
Irfan Sheriffebe606f2011-02-24 11:39:15 -0800909 enforceChangePermission();
Irfan Sheriff227bec42011-02-15 19:30:27 -0800910 return new Messenger(mAsyncServiceHandler);
911 }
912
Irfan Sheriff4aeca7c52011-03-10 16:53:33 -0800913 /**
914 * Get the IP and proxy configuration file
915 */
916 public String getConfigFile() {
917 enforceAccessPermission();
918 return mWifiStateMachine.getConfigFile();
919 }
920
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800921 private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
922 @Override
923 public void onReceive(Context context, Intent intent) {
924 String action = intent.getAction();
925
Doug Zongker43866e02010-01-07 12:09:54 -0800926 long idleMillis =
927 Settings.Secure.getLong(mContext.getContentResolver(),
Irfan Sheriff4f5f7c92010-10-14 17:01:27 -0700928 Settings.Secure.WIFI_IDLE_MS, DEFAULT_IDLE_MS);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800929 int stayAwakeConditions =
Doug Zongker43866e02010-01-07 12:09:54 -0800930 Settings.System.getInt(mContext.getContentResolver(),
931 Settings.System.STAY_ON_WHILE_PLUGGED_IN, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800932 if (action.equals(Intent.ACTION_SCREEN_ON)) {
Joe Onorato431bb222010-10-18 19:13:23 -0400933 if (DBG) {
934 Slog.d(TAG, "ACTION_SCREEN_ON");
935 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800936 mAlarmManager.cancel(mIdleIntent);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800937 mScreenOff = false;
Irfan Sheriff227bec42011-02-15 19:30:27 -0800938 evaluateTrafficStatsPolling();
Irfan Sheriff0d255342010-07-28 09:35:20 -0700939 mWifiStateMachine.enableRssiPolling(true);
Irfan Sherifffcc08452011-02-17 16:44:54 -0800940 if (mBackgroundScanSupported) {
Irfan Sheriff2b7f6382011-03-25 14:29:19 -0700941 mWifiStateMachine.enableBackgroundScanCommand(false);
Irfan Sherifffcc08452011-02-17 16:44:54 -0800942 }
Irfan Sheriff8e86b892010-12-22 11:02:20 -0800943 mWifiStateMachine.enableAllNetworks();
Irfan Sheriffe6daca52011-11-03 15:46:50 -0700944 setDeviceIdleAndUpdateWifi(false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800945 } else if (action.equals(Intent.ACTION_SCREEN_OFF)) {
Joe Onorato431bb222010-10-18 19:13:23 -0400946 if (DBG) {
947 Slog.d(TAG, "ACTION_SCREEN_OFF");
948 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800949 mScreenOff = true;
Irfan Sheriff227bec42011-02-15 19:30:27 -0800950 evaluateTrafficStatsPolling();
Irfan Sheriff0d255342010-07-28 09:35:20 -0700951 mWifiStateMachine.enableRssiPolling(false);
Irfan Sherifffcc08452011-02-17 16:44:54 -0800952 if (mBackgroundScanSupported) {
Irfan Sheriff2b7f6382011-03-25 14:29:19 -0700953 mWifiStateMachine.enableBackgroundScanCommand(true);
Irfan Sherifffcc08452011-02-17 16:44:54 -0800954 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800955 /*
956 * Set a timer to put Wi-Fi to sleep, but only if the screen is off
957 * AND the "stay on while plugged in" setting doesn't match the
958 * current power conditions (i.e, not plugged in, plugged in to USB,
959 * or plugged in to AC).
960 */
961 if (!shouldWifiStayAwake(stayAwakeConditions, mPluggedType)) {
Irfan Sheriffe6daca52011-11-03 15:46:50 -0700962 //Delayed shutdown if wifi is connected
963 if (mNetworkInfo.getDetailedState() == DetailedState.CONNECTED) {
964 if (DBG) Slog.d(TAG, "setting ACTION_DEVICE_IDLE: " + idleMillis + " ms");
965 mAlarmManager.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis()
966 + idleMillis, mIdleIntent);
Mike Lockwoodd9c32bc2009-05-18 14:14:15 -0400967 } else {
Irfan Sheriffe6daca52011-11-03 15:46:50 -0700968 setDeviceIdleAndUpdateWifi(true);
Mike Lockwoodd9c32bc2009-05-18 14:14:15 -0400969 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800970 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800971 } else if (action.equals(ACTION_DEVICE_IDLE)) {
Irfan Sheriffe6daca52011-11-03 15:46:50 -0700972 setDeviceIdleAndUpdateWifi(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800973 } else if (action.equals(Intent.ACTION_BATTERY_CHANGED)) {
974 /*
975 * Set a timer to put Wi-Fi to sleep, but only if the screen is off
976 * AND we are transitioning from a state in which the device was supposed
977 * to stay awake to a state in which it is not supposed to stay awake.
978 * If "stay awake" state is not changing, we do nothing, to avoid resetting
979 * the already-set timer.
980 */
981 int pluggedType = intent.getIntExtra("plugged", 0);
Joe Onorato431bb222010-10-18 19:13:23 -0400982 if (DBG) {
983 Slog.d(TAG, "ACTION_BATTERY_CHANGED pluggedType: " + pluggedType);
984 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800985 if (mScreenOff && shouldWifiStayAwake(stayAwakeConditions, mPluggedType) &&
986 !shouldWifiStayAwake(stayAwakeConditions, pluggedType)) {
987 long triggerTime = System.currentTimeMillis() + idleMillis;
Joe Onorato431bb222010-10-18 19:13:23 -0400988 if (DBG) {
989 Slog.d(TAG, "setting ACTION_DEVICE_IDLE timer for " + idleMillis + "ms");
990 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800991 mAlarmManager.set(AlarmManager.RTC_WAKEUP, triggerTime, mIdleIntent);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800992 }
993 mPluggedType = pluggedType;
Irfan Sheriff65eaec82011-01-05 22:00:16 -0800994 } else if (action.equals(BluetoothAdapter.ACTION_CONNECTION_STATE_CHANGED)) {
995 int state = intent.getIntExtra(BluetoothAdapter.EXTRA_CONNECTION_STATE,
996 BluetoothAdapter.STATE_DISCONNECTED);
997 mWifiStateMachine.sendBluetoothAdapterStateChange(state);
Irfan Sheriff616f3172011-09-11 19:59:01 -0700998 } else if (action.equals(TelephonyIntents.ACTION_EMERGENCY_CALLBACK_MODE_CHANGED)) {
999 mEmergencyCallbackMode = intent.getBooleanExtra("phoneinECMState", false);
1000 updateWifiState();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001001 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001002 }
1003
1004 /**
1005 * Determines whether the Wi-Fi chipset should stay awake or be put to
1006 * sleep. Looks at the setting for the sleep policy and the current
1007 * conditions.
Jaikumar Ganesh084c6652009-12-07 10:58:18 -08001008 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001009 * @see #shouldDeviceStayAwake(int, int)
1010 */
1011 private boolean shouldWifiStayAwake(int stayAwakeConditions, int pluggedType) {
Irfan Sheriff739f6bc2011-01-28 16:43:12 -08001012 //Never sleep as long as the user has not changed the settings
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001013 int wifiSleepPolicy = Settings.System.getInt(mContext.getContentResolver(),
Irfan Sheriff96b10d62011-01-11 15:40:35 -08001014 Settings.System.WIFI_SLEEP_POLICY,
Irfan Sheriff739f6bc2011-01-28 16:43:12 -08001015 Settings.System.WIFI_SLEEP_POLICY_NEVER);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001016
1017 if (wifiSleepPolicy == Settings.System.WIFI_SLEEP_POLICY_NEVER) {
1018 // Never sleep
1019 return true;
1020 } else if ((wifiSleepPolicy == Settings.System.WIFI_SLEEP_POLICY_NEVER_WHILE_PLUGGED) &&
1021 (pluggedType != 0)) {
1022 // Never sleep while plugged, and we're plugged
1023 return true;
1024 } else {
1025 // Default
1026 return shouldDeviceStayAwake(stayAwakeConditions, pluggedType);
1027 }
1028 }
Jaikumar Ganesh084c6652009-12-07 10:58:18 -08001029
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001030 /**
1031 * Determine whether the bit value corresponding to {@code pluggedType} is set in
1032 * the bit string {@code stayAwakeConditions}. Because a {@code pluggedType} value
1033 * of {@code 0} isn't really a plugged type, but rather an indication that the
1034 * device isn't plugged in at all, there is no bit value corresponding to a
1035 * {@code pluggedType} value of {@code 0}. That is why we shift by
Ben Dodson4e8620f2010-08-25 10:55:47 -07001036 * {@code pluggedType - 1} instead of by {@code pluggedType}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001037 * @param stayAwakeConditions a bit string specifying which "plugged types" should
1038 * keep the device (and hence Wi-Fi) awake.
1039 * @param pluggedType the type of plug (USB, AC, or none) for which the check is
1040 * being made
1041 * @return {@code true} if {@code pluggedType} indicates that the device is
1042 * supposed to stay awake, {@code false} otherwise.
1043 */
1044 private boolean shouldDeviceStayAwake(int stayAwakeConditions, int pluggedType) {
1045 return (stayAwakeConditions & pluggedType) != 0;
1046 }
1047 };
1048
Irfan Sheriffe6daca52011-11-03 15:46:50 -07001049 private void setDeviceIdleAndUpdateWifi(boolean deviceIdle) {
1050 mDeviceIdle = deviceIdle;
1051 reportStartWorkSource();
1052 updateWifiState();
1053 }
1054
Dianne Hackborn03f3cb02010-09-17 23:12:26 -07001055 private synchronized void reportStartWorkSource() {
1056 mTmpWorkSource.clear();
1057 if (mDeviceIdle) {
1058 for (int i=0; i<mLocks.mList.size(); i++) {
1059 mTmpWorkSource.add(mLocks.mList.get(i).mWorkSource);
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07001060 }
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07001061 }
Dianne Hackborn03f3cb02010-09-17 23:12:26 -07001062 mWifiStateMachine.updateBatteryWorkSource(mTmpWorkSource);
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07001063 }
Jaikumar Ganesh7440fc22010-09-27 17:04:14 -07001064
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001065 private void updateWifiState() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001066 boolean lockHeld = mLocks.hasLocks();
Irfan Sheriff5876a422010-08-12 20:26:23 -07001067 int strongestLockMode = WifiManager.WIFI_MODE_FULL;
Irfan Sheriff616f3172011-09-11 19:59:01 -07001068 boolean wifiShouldBeStarted;
1069
1070 if (mEmergencyCallbackMode) {
1071 wifiShouldBeStarted = false;
1072 } else {
1073 wifiShouldBeStarted = !mDeviceIdle || lockHeld;
1074 }
Irfan Sheriff5876a422010-08-12 20:26:23 -07001075
1076 if (lockHeld) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001077 strongestLockMode = mLocks.getStrongestLockMode();
Irfan Sheriff5876a422010-08-12 20:26:23 -07001078 }
1079 /* If device is not idle, lockmode cannot be scan only */
1080 if (!mDeviceIdle && strongestLockMode == WifiManager.WIFI_MODE_SCAN_ONLY) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001081 strongestLockMode = WifiManager.WIFI_MODE_FULL;
1082 }
1083
Irfan Sheriffa2a1b912010-06-07 09:03:04 -07001084 /* Disable tethering when airplane mode is enabled */
Irfan Sheriff658772f2011-03-08 14:52:31 -08001085 if (mAirplaneModeOn.get()) {
Irfan Sheriff0d255342010-07-28 09:35:20 -07001086 mWifiStateMachine.setWifiApEnabled(null, false);
Irfan Sheriffa2a1b912010-06-07 09:03:04 -07001087 }
Irfan Sheriffb2e6c012010-04-05 11:57:56 -07001088
Irfan Sheriff658772f2011-03-08 14:52:31 -08001089 if (shouldWifiBeEnabled()) {
Irfan Sheriffa2a1b912010-06-07 09:03:04 -07001090 if (wifiShouldBeStarted) {
Dianne Hackborn03f3cb02010-09-17 23:12:26 -07001091 reportStartWorkSource();
Irfan Sheriff0d255342010-07-28 09:35:20 -07001092 mWifiStateMachine.setWifiEnabled(true);
1093 mWifiStateMachine.setScanOnlyMode(
Irfan Sheriffa2a1b912010-06-07 09:03:04 -07001094 strongestLockMode == WifiManager.WIFI_MODE_SCAN_ONLY);
Irfan Sheriff4494c902011-12-08 10:47:54 -08001095 mWifiStateMachine.setDriverStart(true, mEmergencyCallbackMode);
Irfan Sheriff5876a422010-08-12 20:26:23 -07001096 mWifiStateMachine.setHighPerfModeEnabled(strongestLockMode
1097 == WifiManager.WIFI_MODE_FULL_HIGH_PERF);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001098 } else {
Irfan Sheriff4494c902011-12-08 10:47:54 -08001099 mWifiStateMachine.setDriverStart(false, mEmergencyCallbackMode);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001100 }
Irfan Sheriffa2a1b912010-06-07 09:03:04 -07001101 } else {
Irfan Sheriff0d255342010-07-28 09:35:20 -07001102 mWifiStateMachine.setWifiEnabled(false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001103 }
1104 }
1105
1106 private void registerForBroadcasts() {
1107 IntentFilter intentFilter = new IntentFilter();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001108 intentFilter.addAction(Intent.ACTION_SCREEN_ON);
1109 intentFilter.addAction(Intent.ACTION_SCREEN_OFF);
1110 intentFilter.addAction(Intent.ACTION_BATTERY_CHANGED);
1111 intentFilter.addAction(ACTION_DEVICE_IDLE);
Irfan Sheriff65eaec82011-01-05 22:00:16 -08001112 intentFilter.addAction(BluetoothAdapter.ACTION_CONNECTION_STATE_CHANGED);
Irfan Sheriff616f3172011-09-11 19:59:01 -07001113 intentFilter.addAction(TelephonyIntents.ACTION_EMERGENCY_CALLBACK_MODE_CHANGED);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001114 mContext.registerReceiver(mReceiver, intentFilter);
1115 }
Jaikumar Ganesh084c6652009-12-07 10:58:18 -08001116
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001117 private boolean isAirplaneSensitive() {
1118 String airplaneModeRadios = Settings.System.getString(mContext.getContentResolver(),
1119 Settings.System.AIRPLANE_MODE_RADIOS);
1120 return airplaneModeRadios == null
1121 || airplaneModeRadios.contains(Settings.System.RADIO_WIFI);
1122 }
1123
Mike Lockwoodbd5ddf02009-07-29 21:37:14 -07001124 private boolean isAirplaneToggleable() {
1125 String toggleableRadios = Settings.System.getString(mContext.getContentResolver(),
1126 Settings.System.AIRPLANE_MODE_TOGGLEABLE_RADIOS);
1127 return toggleableRadios != null
1128 && toggleableRadios.contains(Settings.System.RADIO_WIFI);
1129 }
1130
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001131 /**
1132 * Returns true if Wi-Fi is sensitive to airplane mode, and airplane mode is
1133 * currently on.
1134 * @return {@code true} if airplane mode is on.
1135 */
1136 private boolean isAirplaneModeOn() {
1137 return isAirplaneSensitive() && Settings.System.getInt(mContext.getContentResolver(),
1138 Settings.System.AIRPLANE_MODE_ON, 0) == 1;
1139 }
1140
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001141 @Override
1142 protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
1143 if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.DUMP)
1144 != PackageManager.PERMISSION_GRANTED) {
1145 pw.println("Permission Denial: can't dump WifiService from from pid="
1146 + Binder.getCallingPid()
1147 + ", uid=" + Binder.getCallingUid());
1148 return;
1149 }
Irfan Sheriffd8134ff2010-08-22 17:06:34 -07001150 pw.println("Wi-Fi is " + mWifiStateMachine.syncGetWifiStateByName());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001151 pw.println("Stay-awake conditions: " +
1152 Settings.System.getInt(mContext.getContentResolver(),
1153 Settings.System.STAY_ON_WHILE_PLUGGED_IN, 0));
1154 pw.println();
1155
1156 pw.println("Internal state:");
Irfan Sheriff0d255342010-07-28 09:35:20 -07001157 pw.println(mWifiStateMachine);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001158 pw.println();
1159 pw.println("Latest scan results:");
Irfan Sheriffd8134ff2010-08-22 17:06:34 -07001160 List<ScanResult> scanResults = mWifiStateMachine.syncGetScanResultsList();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001161 if (scanResults != null && scanResults.size() != 0) {
1162 pw.println(" BSSID Frequency RSSI Flags SSID");
1163 for (ScanResult r : scanResults) {
1164 pw.printf(" %17s %9d %5d %-16s %s%n",
1165 r.BSSID,
1166 r.frequency,
1167 r.level,
1168 r.capabilities,
1169 r.SSID == null ? "" : r.SSID);
1170 }
1171 }
1172 pw.println();
Eric Shienbrood5711fad2009-03-27 20:25:31 -07001173 pw.println("Locks acquired: " + mFullLocksAcquired + " full, " +
Irfan Sheriff5876a422010-08-12 20:26:23 -07001174 mFullHighPerfLocksAcquired + " full high perf, " +
Eric Shienbrood5711fad2009-03-27 20:25:31 -07001175 mScanLocksAcquired + " scan");
1176 pw.println("Locks released: " + mFullLocksReleased + " full, " +
Irfan Sheriff5876a422010-08-12 20:26:23 -07001177 mFullHighPerfLocksReleased + " full high perf, " +
Eric Shienbrood5711fad2009-03-27 20:25:31 -07001178 mScanLocksReleased + " scan");
1179 pw.println();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001180 pw.println("Locks held:");
1181 mLocks.dump(pw);
Isaac Levybc7dfb52011-06-06 15:34:01 -07001182
1183 pw.println();
Isaac Levy654f5092011-07-13 17:41:45 -07001184 pw.println("WifiWatchdogStateMachine dump");
1185 mWifiWatchdogStateMachine.dump(pw);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001186 }
1187
Robert Greenwalt58ff0212009-05-19 15:53:54 -07001188 private class WifiLock extends DeathRecipient {
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -07001189 WifiLock(int lockMode, String tag, IBinder binder, WorkSource ws) {
1190 super(lockMode, tag, binder, ws);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001191 }
1192
1193 public void binderDied() {
1194 synchronized (mLocks) {
1195 releaseWifiLockLocked(mBinder);
1196 }
1197 }
1198
1199 public String toString() {
Robert Greenwalt5347bd42009-05-13 15:10:16 -07001200 return "WifiLock{" + mTag + " type=" + mMode + " binder=" + mBinder + "}";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001201 }
1202 }
1203
1204 private class LockList {
1205 private List<WifiLock> mList;
1206
1207 private LockList() {
1208 mList = new ArrayList<WifiLock>();
1209 }
1210
1211 private synchronized boolean hasLocks() {
1212 return !mList.isEmpty();
1213 }
1214
1215 private synchronized int getStrongestLockMode() {
1216 if (mList.isEmpty()) {
1217 return WifiManager.WIFI_MODE_FULL;
1218 }
Irfan Sheriff5876a422010-08-12 20:26:23 -07001219
1220 if (mFullHighPerfLocksAcquired > mFullHighPerfLocksReleased) {
1221 return WifiManager.WIFI_MODE_FULL_HIGH_PERF;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001222 }
Irfan Sheriff5876a422010-08-12 20:26:23 -07001223
1224 if (mFullLocksAcquired > mFullLocksReleased) {
1225 return WifiManager.WIFI_MODE_FULL;
1226 }
1227
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001228 return WifiManager.WIFI_MODE_SCAN_ONLY;
1229 }
1230
1231 private void addLock(WifiLock lock) {
1232 if (findLockByBinder(lock.mBinder) < 0) {
1233 mList.add(lock);
1234 }
1235 }
1236
1237 private WifiLock removeLock(IBinder binder) {
1238 int index = findLockByBinder(binder);
1239 if (index >= 0) {
Suchi Amalapurapufff2fda2009-06-30 21:36:16 -07001240 WifiLock ret = mList.remove(index);
1241 ret.unlinkDeathRecipient();
1242 return ret;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001243 } else {
1244 return null;
1245 }
1246 }
1247
1248 private int findLockByBinder(IBinder binder) {
1249 int size = mList.size();
1250 for (int i = size - 1; i >= 0; i--)
1251 if (mList.get(i).mBinder == binder)
1252 return i;
1253 return -1;
1254 }
1255
1256 private void dump(PrintWriter pw) {
1257 for (WifiLock l : mList) {
1258 pw.print(" ");
1259 pw.println(l);
1260 }
1261 }
1262 }
1263
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -07001264 void enforceWakeSourcePermission(int uid, int pid) {
Dianne Hackborne746f032010-09-13 16:02:57 -07001265 if (uid == android.os.Process.myUid()) {
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -07001266 return;
1267 }
1268 mContext.enforcePermission(android.Manifest.permission.UPDATE_DEVICE_STATS,
1269 pid, uid, null);
1270 }
1271
1272 public boolean acquireWifiLock(IBinder binder, int lockMode, String tag, WorkSource ws) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001273 mContext.enforceCallingOrSelfPermission(android.Manifest.permission.WAKE_LOCK, null);
Irfan Sheriff5876a422010-08-12 20:26:23 -07001274 if (lockMode != WifiManager.WIFI_MODE_FULL &&
1275 lockMode != WifiManager.WIFI_MODE_SCAN_ONLY &&
1276 lockMode != WifiManager.WIFI_MODE_FULL_HIGH_PERF) {
1277 Slog.e(TAG, "Illegal argument, lockMode= " + lockMode);
1278 if (DBG) throw new IllegalArgumentException("lockMode=" + lockMode);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001279 return false;
1280 }
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -07001281 if (ws != null && ws.size() == 0) {
1282 ws = null;
1283 }
Dianne Hackbornecfd7f72010-10-08 14:23:40 -07001284 if (ws != null) {
1285 enforceWakeSourcePermission(Binder.getCallingUid(), Binder.getCallingPid());
1286 }
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -07001287 if (ws == null) {
1288 ws = new WorkSource(Binder.getCallingUid());
1289 }
1290 WifiLock wifiLock = new WifiLock(lockMode, tag, binder, ws);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001291 synchronized (mLocks) {
1292 return acquireWifiLockLocked(wifiLock);
1293 }
1294 }
1295
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -07001296 private void noteAcquireWifiLock(WifiLock wifiLock) throws RemoteException {
1297 switch(wifiLock.mMode) {
1298 case WifiManager.WIFI_MODE_FULL:
Irfan Sheriff5876a422010-08-12 20:26:23 -07001299 case WifiManager.WIFI_MODE_FULL_HIGH_PERF:
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -07001300 mBatteryStats.noteFullWifiLockAcquiredFromSource(wifiLock.mWorkSource);
1301 break;
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -07001302 case WifiManager.WIFI_MODE_SCAN_ONLY:
1303 mBatteryStats.noteScanWifiLockAcquiredFromSource(wifiLock.mWorkSource);
1304 break;
1305 }
1306 }
1307
1308 private void noteReleaseWifiLock(WifiLock wifiLock) throws RemoteException {
1309 switch(wifiLock.mMode) {
1310 case WifiManager.WIFI_MODE_FULL:
Irfan Sheriff5876a422010-08-12 20:26:23 -07001311 case WifiManager.WIFI_MODE_FULL_HIGH_PERF:
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -07001312 mBatteryStats.noteFullWifiLockReleasedFromSource(wifiLock.mWorkSource);
1313 break;
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -07001314 case WifiManager.WIFI_MODE_SCAN_ONLY:
1315 mBatteryStats.noteScanWifiLockReleasedFromSource(wifiLock.mWorkSource);
1316 break;
1317 }
1318 }
1319
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001320 private boolean acquireWifiLockLocked(WifiLock wifiLock) {
Irfan Sheriffc89dd542010-09-28 08:40:54 -07001321 if (DBG) Slog.d(TAG, "acquireWifiLockLocked: " + wifiLock);
Robert Greenwaltf1acb2d2009-10-13 08:20:55 -07001322
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001323 mLocks.addLock(wifiLock);
Robert Greenwaltf1acb2d2009-10-13 08:20:55 -07001324
The Android Open Source Project10592532009-03-18 17:39:46 -07001325 long ident = Binder.clearCallingIdentity();
1326 try {
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -07001327 noteAcquireWifiLock(wifiLock);
Robert Greenwalt5347bd42009-05-13 15:10:16 -07001328 switch(wifiLock.mMode) {
Eric Shienbrood5711fad2009-03-27 20:25:31 -07001329 case WifiManager.WIFI_MODE_FULL:
1330 ++mFullLocksAcquired;
Eric Shienbrood5711fad2009-03-27 20:25:31 -07001331 break;
Irfan Sheriff5876a422010-08-12 20:26:23 -07001332 case WifiManager.WIFI_MODE_FULL_HIGH_PERF:
1333 ++mFullHighPerfLocksAcquired;
1334 break;
1335
Eric Shienbrood5711fad2009-03-27 20:25:31 -07001336 case WifiManager.WIFI_MODE_SCAN_ONLY:
1337 ++mScanLocksAcquired;
Eric Shienbrood5711fad2009-03-27 20:25:31 -07001338 break;
The Android Open Source Project10592532009-03-18 17:39:46 -07001339 }
Dianne Hackbornecfd7f72010-10-08 14:23:40 -07001340
1341 // Be aggressive about adding new locks into the accounted state...
1342 // we want to over-report rather than under-report.
1343 reportStartWorkSource();
1344
1345 updateWifiState();
1346 return true;
The Android Open Source Project10592532009-03-18 17:39:46 -07001347 } catch (RemoteException e) {
Dianne Hackbornecfd7f72010-10-08 14:23:40 -07001348 return false;
The Android Open Source Project10592532009-03-18 17:39:46 -07001349 } finally {
1350 Binder.restoreCallingIdentity(ident);
1351 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001352 }
1353
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -07001354 public void updateWifiLockWorkSource(IBinder lock, WorkSource ws) {
1355 int uid = Binder.getCallingUid();
1356 int pid = Binder.getCallingPid();
1357 if (ws != null && ws.size() == 0) {
1358 ws = null;
1359 }
1360 if (ws != null) {
1361 enforceWakeSourcePermission(uid, pid);
1362 }
1363 long ident = Binder.clearCallingIdentity();
1364 try {
1365 synchronized (mLocks) {
1366 int index = mLocks.findLockByBinder(lock);
1367 if (index < 0) {
1368 throw new IllegalArgumentException("Wifi lock not active");
1369 }
1370 WifiLock wl = mLocks.mList.get(index);
1371 noteReleaseWifiLock(wl);
1372 wl.mWorkSource = ws != null ? new WorkSource(ws) : new WorkSource(uid);
1373 noteAcquireWifiLock(wl);
1374 }
1375 } catch (RemoteException e) {
1376 } finally {
1377 Binder.restoreCallingIdentity(ident);
1378 }
1379 }
1380
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001381 public boolean releaseWifiLock(IBinder lock) {
1382 mContext.enforceCallingOrSelfPermission(android.Manifest.permission.WAKE_LOCK, null);
1383 synchronized (mLocks) {
1384 return releaseWifiLockLocked(lock);
1385 }
1386 }
1387
1388 private boolean releaseWifiLockLocked(IBinder lock) {
Eric Shienbroodd4c5f892009-03-24 18:13:20 -07001389 boolean hadLock;
Robert Greenwaltf1acb2d2009-10-13 08:20:55 -07001390
The Android Open Source Project10592532009-03-18 17:39:46 -07001391 WifiLock wifiLock = mLocks.removeLock(lock);
Robert Greenwaltf1acb2d2009-10-13 08:20:55 -07001392
Irfan Sheriffc89dd542010-09-28 08:40:54 -07001393 if (DBG) Slog.d(TAG, "releaseWifiLockLocked: " + wifiLock);
Robert Greenwaltf1acb2d2009-10-13 08:20:55 -07001394
Eric Shienbroodd4c5f892009-03-24 18:13:20 -07001395 hadLock = (wifiLock != null);
1396
Dianne Hackbornecfd7f72010-10-08 14:23:40 -07001397 long ident = Binder.clearCallingIdentity();
1398 try {
1399 if (hadLock) {
Wink Savillece0ea1f2011-10-13 16:55:20 -07001400 noteReleaseWifiLock(wifiLock);
Robert Greenwalt5347bd42009-05-13 15:10:16 -07001401 switch(wifiLock.mMode) {
Eric Shienbrood5711fad2009-03-27 20:25:31 -07001402 case WifiManager.WIFI_MODE_FULL:
1403 ++mFullLocksReleased;
Eric Shienbrood5711fad2009-03-27 20:25:31 -07001404 break;
Irfan Sheriff5876a422010-08-12 20:26:23 -07001405 case WifiManager.WIFI_MODE_FULL_HIGH_PERF:
1406 ++mFullHighPerfLocksReleased;
1407 break;
Eric Shienbrood5711fad2009-03-27 20:25:31 -07001408 case WifiManager.WIFI_MODE_SCAN_ONLY:
1409 ++mScanLocksReleased;
Eric Shienbrood5711fad2009-03-27 20:25:31 -07001410 break;
Eric Shienbroodd4c5f892009-03-24 18:13:20 -07001411 }
The Android Open Source Project10592532009-03-18 17:39:46 -07001412 }
Dianne Hackbornecfd7f72010-10-08 14:23:40 -07001413
1414 // TODO - should this only happen if you hadLock?
1415 updateWifiState();
1416
1417 } catch (RemoteException e) {
1418 } finally {
1419 Binder.restoreCallingIdentity(ident);
The Android Open Source Project10592532009-03-18 17:39:46 -07001420 }
Dianne Hackbornecfd7f72010-10-08 14:23:40 -07001421
Eric Shienbroodd4c5f892009-03-24 18:13:20 -07001422 return hadLock;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001423 }
Robert Greenwalt5347bd42009-05-13 15:10:16 -07001424
Robert Greenwalt58ff0212009-05-19 15:53:54 -07001425 private abstract class DeathRecipient
Robert Greenwalt5347bd42009-05-13 15:10:16 -07001426 implements IBinder.DeathRecipient {
1427 String mTag;
1428 int mMode;
1429 IBinder mBinder;
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -07001430 WorkSource mWorkSource;
Robert Greenwalt5347bd42009-05-13 15:10:16 -07001431
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -07001432 DeathRecipient(int mode, String tag, IBinder binder, WorkSource ws) {
Robert Greenwalt5347bd42009-05-13 15:10:16 -07001433 super();
1434 mTag = tag;
1435 mMode = mode;
1436 mBinder = binder;
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -07001437 mWorkSource = ws;
Robert Greenwalt5347bd42009-05-13 15:10:16 -07001438 try {
1439 mBinder.linkToDeath(this, 0);
1440 } catch (RemoteException e) {
1441 binderDied();
1442 }
1443 }
Suchi Amalapurapufff2fda2009-06-30 21:36:16 -07001444
1445 void unlinkDeathRecipient() {
1446 mBinder.unlinkToDeath(this, 0);
1447 }
Robert Greenwalt5347bd42009-05-13 15:10:16 -07001448 }
1449
Robert Greenwalt58ff0212009-05-19 15:53:54 -07001450 private class Multicaster extends DeathRecipient {
1451 Multicaster(String tag, IBinder binder) {
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -07001452 super(Binder.getCallingUid(), tag, binder, null);
Robert Greenwalt5347bd42009-05-13 15:10:16 -07001453 }
1454
1455 public void binderDied() {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001456 Slog.e(TAG, "Multicaster binderDied");
Robert Greenwalt5347bd42009-05-13 15:10:16 -07001457 synchronized (mMulticasters) {
1458 int i = mMulticasters.indexOf(this);
1459 if (i != -1) {
1460 removeMulticasterLocked(i, mMode);
1461 }
1462 }
1463 }
1464
1465 public String toString() {
Robert Greenwalt58ff0212009-05-19 15:53:54 -07001466 return "Multicaster{" + mTag + " binder=" + mBinder + "}";
Robert Greenwalt5347bd42009-05-13 15:10:16 -07001467 }
1468
1469 public int getUid() {
1470 return mMode;
1471 }
1472 }
1473
Robert Greenwalte2d155a2009-10-21 14:58:34 -07001474 public void initializeMulticastFiltering() {
1475 enforceMulticastChangePermission();
Irfan Sheriffa8fbe1f2010-03-09 09:13:58 -08001476
Robert Greenwalte2d155a2009-10-21 14:58:34 -07001477 synchronized (mMulticasters) {
1478 // if anybody had requested filters be off, leave off
1479 if (mMulticasters.size() != 0) {
1480 return;
1481 } else {
Irfan Sheriffb0c1b80f2011-07-19 15:44:25 -07001482 mWifiStateMachine.startFilteringMulticastV4Packets();
Robert Greenwalte2d155a2009-10-21 14:58:34 -07001483 }
1484 }
1485 }
1486
Robert Greenwaltfc1b15c2009-05-22 15:09:51 -07001487 public void acquireMulticastLock(IBinder binder, String tag) {
1488 enforceMulticastChangePermission();
Robert Greenwalt5347bd42009-05-13 15:10:16 -07001489
1490 synchronized (mMulticasters) {
1491 mMulticastEnabled++;
Robert Greenwalt58ff0212009-05-19 15:53:54 -07001492 mMulticasters.add(new Multicaster(tag, binder));
Irfan Sheriffb0c1b80f2011-07-19 15:44:25 -07001493 // Note that we could call stopFilteringMulticastV4Packets only when
Robert Greenwalt5347bd42009-05-13 15:10:16 -07001494 // our new size == 1 (first call), but this function won't
1495 // be called often and by making the stopPacket call each
1496 // time we're less fragile and self-healing.
Irfan Sheriffb0c1b80f2011-07-19 15:44:25 -07001497 mWifiStateMachine.stopFilteringMulticastV4Packets();
Robert Greenwalt5347bd42009-05-13 15:10:16 -07001498 }
1499
1500 int uid = Binder.getCallingUid();
1501 Long ident = Binder.clearCallingIdentity();
1502 try {
1503 mBatteryStats.noteWifiMulticastEnabled(uid);
1504 } catch (RemoteException e) {
1505 } finally {
1506 Binder.restoreCallingIdentity(ident);
1507 }
1508 }
1509
Robert Greenwaltfc1b15c2009-05-22 15:09:51 -07001510 public void releaseMulticastLock() {
1511 enforceMulticastChangePermission();
Robert Greenwalt5347bd42009-05-13 15:10:16 -07001512
1513 int uid = Binder.getCallingUid();
1514 synchronized (mMulticasters) {
1515 mMulticastDisabled++;
1516 int size = mMulticasters.size();
1517 for (int i = size - 1; i >= 0; i--) {
Robert Greenwalt58ff0212009-05-19 15:53:54 -07001518 Multicaster m = mMulticasters.get(i);
Robert Greenwalt5347bd42009-05-13 15:10:16 -07001519 if ((m != null) && (m.getUid() == uid)) {
1520 removeMulticasterLocked(i, uid);
1521 }
1522 }
1523 }
1524 }
1525
1526 private void removeMulticasterLocked(int i, int uid)
1527 {
Suchi Amalapurapufff2fda2009-06-30 21:36:16 -07001528 Multicaster removed = mMulticasters.remove(i);
Irfan Sheriffa8fbe1f2010-03-09 09:13:58 -08001529
Suchi Amalapurapufff2fda2009-06-30 21:36:16 -07001530 if (removed != null) {
1531 removed.unlinkDeathRecipient();
1532 }
Robert Greenwalt5347bd42009-05-13 15:10:16 -07001533 if (mMulticasters.size() == 0) {
Irfan Sheriffb0c1b80f2011-07-19 15:44:25 -07001534 mWifiStateMachine.startFilteringMulticastV4Packets();
Robert Greenwalt5347bd42009-05-13 15:10:16 -07001535 }
1536
1537 Long ident = Binder.clearCallingIdentity();
1538 try {
1539 mBatteryStats.noteWifiMulticastDisabled(uid);
1540 } catch (RemoteException e) {
1541 } finally {
1542 Binder.restoreCallingIdentity(ident);
1543 }
1544 }
1545
Robert Greenwalt58ff0212009-05-19 15:53:54 -07001546 public boolean isMulticastEnabled() {
Robert Greenwalt5347bd42009-05-13 15:10:16 -07001547 enforceAccessPermission();
1548
1549 synchronized (mMulticasters) {
1550 return (mMulticasters.size() > 0);
1551 }
1552 }
Irfan Sheriff0d255342010-07-28 09:35:20 -07001553
Irfan Sheriff227bec42011-02-15 19:30:27 -08001554 /**
1555 * Evaluate if traffic stats polling is needed based on
1556 * connection and screen on status
1557 */
1558 private void evaluateTrafficStatsPolling() {
1559 Message msg;
1560 if (mNetworkInfo.getDetailedState() == DetailedState.CONNECTED && !mScreenOff) {
Irfan Sheriffebe606f2011-02-24 11:39:15 -08001561 msg = Message.obtain(mAsyncServiceHandler,
1562 WifiManager.CMD_ENABLE_TRAFFIC_STATS_POLL, 1, 0);
Irfan Sheriff227bec42011-02-15 19:30:27 -08001563 } else {
Irfan Sheriffebe606f2011-02-24 11:39:15 -08001564 msg = Message.obtain(mAsyncServiceHandler,
1565 WifiManager.CMD_ENABLE_TRAFFIC_STATS_POLL, 0, 0);
Irfan Sheriff227bec42011-02-15 19:30:27 -08001566 }
1567 msg.sendToTarget();
1568 }
1569
1570 private void notifyOnDataActivity() {
1571 long sent, received;
1572 long preTxPkts = mTxPkts, preRxPkts = mRxPkts;
1573 int dataActivity = WifiManager.DATA_ACTIVITY_NONE;
1574
1575 mTxPkts = TrafficStats.getTxPackets(mInterfaceName);
1576 mRxPkts = TrafficStats.getRxPackets(mInterfaceName);
1577
1578 if (preTxPkts > 0 || preRxPkts > 0) {
1579 sent = mTxPkts - preTxPkts;
1580 received = mRxPkts - preRxPkts;
1581 if (sent > 0) {
1582 dataActivity |= WifiManager.DATA_ACTIVITY_OUT;
1583 }
1584 if (received > 0) {
1585 dataActivity |= WifiManager.DATA_ACTIVITY_IN;
1586 }
1587
1588 if (dataActivity != mDataActivity && !mScreenOff) {
1589 mDataActivity = dataActivity;
1590 for (AsyncChannel client : mClients) {
1591 client.sendMessage(WifiManager.DATA_ACTIVITY_NOTIFICATION, mDataActivity);
1592 }
1593 }
1594 }
1595 }
1596
1597
Irfan Sheriff0d255342010-07-28 09:35:20 -07001598 private void checkAndSetNotification() {
1599 // If we shouldn't place a notification on available networks, then
1600 // don't bother doing any of the following
1601 if (!mNotificationEnabled) return;
1602
1603 State state = mNetworkInfo.getState();
1604 if ((state == NetworkInfo.State.DISCONNECTED)
1605 || (state == NetworkInfo.State.UNKNOWN)) {
1606 // Look for an open network
Irfan Sheriffd8134ff2010-08-22 17:06:34 -07001607 List<ScanResult> scanResults = mWifiStateMachine.syncGetScanResultsList();
Irfan Sheriff0d255342010-07-28 09:35:20 -07001608 if (scanResults != null) {
1609 int numOpenNetworks = 0;
1610 for (int i = scanResults.size() - 1; i >= 0; i--) {
1611 ScanResult scanResult = scanResults.get(i);
1612
Irfan Sherifffdd5f952011-08-04 16:55:54 -07001613 //A capability of [ESS] represents an open access point
1614 //that is available for an STA to connect
1615 if (scanResult.capabilities != null &&
1616 scanResult.capabilities.equals("[ESS]")) {
Irfan Sheriff0d255342010-07-28 09:35:20 -07001617 numOpenNetworks++;
1618 }
1619 }
1620
1621 if (numOpenNetworks > 0) {
1622 if (++mNumScansSinceNetworkStateChange >= NUM_SCANS_BEFORE_ACTUALLY_SCANNING) {
1623 /*
1624 * We've scanned continuously at least
1625 * NUM_SCANS_BEFORE_NOTIFICATION times. The user
1626 * probably does not have a remembered network in range,
1627 * since otherwise supplicant would have tried to
1628 * associate and thus resetting this counter.
1629 */
1630 setNotificationVisible(true, numOpenNetworks, false, 0);
1631 }
1632 return;
1633 }
1634 }
1635 }
1636
1637 // No open networks in range, remove the notification
1638 setNotificationVisible(false, 0, false, 0);
1639 }
1640
1641 /**
1642 * Clears variables related to tracking whether a notification has been
1643 * shown recently and clears the current notification.
1644 */
1645 private void resetNotification() {
1646 mNotificationRepeatTime = 0;
1647 mNumScansSinceNetworkStateChange = 0;
1648 setNotificationVisible(false, 0, false, 0);
1649 }
1650
1651 /**
1652 * Display or don't display a notification that there are open Wi-Fi networks.
1653 * @param visible {@code true} if notification should be visible, {@code false} otherwise
1654 * @param numNetworks the number networks seen
1655 * @param force {@code true} to force notification to be shown/not-shown,
1656 * even if it is already shown/not-shown.
1657 * @param delay time in milliseconds after which the notification should be made
1658 * visible or invisible.
1659 */
1660 private void setNotificationVisible(boolean visible, int numNetworks, boolean force,
1661 int delay) {
1662
1663 // Since we use auto cancel on the notification, when the
1664 // mNetworksAvailableNotificationShown is true, the notification may
1665 // have actually been canceled. However, when it is false we know
1666 // for sure that it is not being shown (it will not be shown any other
1667 // place than here)
1668
1669 // If it should be hidden and it is already hidden, then noop
1670 if (!visible && !mNotificationShown && !force) {
1671 return;
1672 }
1673
1674 NotificationManager notificationManager = (NotificationManager) mContext
1675 .getSystemService(Context.NOTIFICATION_SERVICE);
1676
1677 Message message;
1678 if (visible) {
1679
1680 // Not enough time has passed to show the notification again
1681 if (System.currentTimeMillis() < mNotificationRepeatTime) {
1682 return;
1683 }
1684
1685 if (mNotification == null) {
Wink Savillec7a98342010-08-13 16:11:42 -07001686 // Cache the Notification object.
Irfan Sheriff0d255342010-07-28 09:35:20 -07001687 mNotification = new Notification();
1688 mNotification.when = 0;
1689 mNotification.icon = ICON_NETWORKS_AVAILABLE;
1690 mNotification.flags = Notification.FLAG_AUTO_CANCEL;
1691 mNotification.contentIntent = PendingIntent.getActivity(mContext, 0,
1692 new Intent(WifiManager.ACTION_PICK_WIFI_NETWORK), 0);
1693 }
1694
1695 CharSequence title = mContext.getResources().getQuantityText(
1696 com.android.internal.R.plurals.wifi_available, numNetworks);
1697 CharSequence details = mContext.getResources().getQuantityText(
1698 com.android.internal.R.plurals.wifi_available_detailed, numNetworks);
1699 mNotification.tickerText = title;
1700 mNotification.setLatestEventInfo(mContext, title, details, mNotification.contentIntent);
1701
1702 mNotificationRepeatTime = System.currentTimeMillis() + NOTIFICATION_REPEAT_DELAY_MS;
1703
1704 notificationManager.notify(ICON_NETWORKS_AVAILABLE, mNotification);
Irfan Sheriff0d255342010-07-28 09:35:20 -07001705 } else {
Irfan Sheriff0d255342010-07-28 09:35:20 -07001706 notificationManager.cancel(ICON_NETWORKS_AVAILABLE);
Irfan Sheriff0d255342010-07-28 09:35:20 -07001707 }
1708
Irfan Sheriff0d255342010-07-28 09:35:20 -07001709 mNotificationShown = visible;
1710 }
1711
1712 private class NotificationEnabledSettingObserver extends ContentObserver {
1713
1714 public NotificationEnabledSettingObserver(Handler handler) {
1715 super(handler);
1716 }
1717
1718 public void register() {
1719 ContentResolver cr = mContext.getContentResolver();
1720 cr.registerContentObserver(Settings.Secure.getUriFor(
1721 Settings.Secure.WIFI_NETWORKS_AVAILABLE_NOTIFICATION_ON), true, this);
1722 mNotificationEnabled = getValue();
1723 }
1724
1725 @Override
1726 public void onChange(boolean selfChange) {
1727 super.onChange(selfChange);
1728
1729 mNotificationEnabled = getValue();
1730 resetNotification();
1731 }
1732
1733 private boolean getValue() {
1734 return Settings.Secure.getInt(mContext.getContentResolver(),
1735 Settings.Secure.WIFI_NETWORKS_AVAILABLE_NOTIFICATION_ON, 1) == 1;
1736 }
1737 }
1738
1739
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001740}