blob: bb38cd9fdad5d7dc7c1bb02a09582f6380a4aff4 [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;
Irfan Sheriff8cef0672011-12-13 17:03:59 -080064import java.util.HashMap;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080065import java.util.List;
Jaikumar Ganesh084c6652009-12-07 10:58:18 -080066import java.util.Set;
Irfan Sheriff658772f2011-03-08 14:52:31 -080067import java.util.concurrent.atomic.AtomicInteger;
Irfan Sheriffa2a1b912010-06-07 09:03:04 -070068import java.util.concurrent.atomic.AtomicBoolean;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080069import java.io.FileDescriptor;
70import java.io.PrintWriter;
71
The Android Open Source Project10592532009-03-18 17:39:46 -070072import com.android.internal.app.IBatteryStats;
Irfan Sheriff616f3172011-09-11 19:59:01 -070073import com.android.internal.telephony.TelephonyIntents;
Wink Saville4b7ba092010-10-20 15:37:41 -070074import com.android.internal.util.AsyncChannel;
The Android Open Source Project10592532009-03-18 17:39:46 -070075import com.android.server.am.BatteryStatsService;
Irfan Sheriff9ab518ad2010-03-12 15:48:17 -080076import com.android.internal.R;
The Android Open Source Project10592532009-03-18 17:39:46 -070077
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080078/**
79 * WifiService handles remote WiFi operation requests by implementing
Irfan Sheriffa2a1b912010-06-07 09:03:04 -070080 * the IWifiManager interface.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080081 *
82 * @hide
83 */
Irfan Sheriffa2a1b912010-06-07 09:03:04 -070084//TODO: Clean up multiple locks and implement WifiService
85// as a SM to track soft AP/client/adhoc bring up based
86// on device idle state, airplane mode and boot.
87
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080088public class WifiService extends IWifiManager.Stub {
89 private static final String TAG = "WifiService";
Dianne Hackborn5fd21692011-06-07 14:09:47 -070090 private static final boolean DBG = false;
Irfan Sheriffa2a1b912010-06-07 09:03:04 -070091
Irfan Sheriff0d255342010-07-28 09:35:20 -070092 private final WifiStateMachine mWifiStateMachine;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080093
94 private Context mContext;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080095
96 private AlarmManager mAlarmManager;
97 private PendingIntent mIdleIntent;
98 private static final int IDLE_REQUEST = 0;
99 private boolean mScreenOff;
100 private boolean mDeviceIdle;
Irfan Sheriff616f3172011-09-11 19:59:01 -0700101 private boolean mEmergencyCallbackMode = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800102 private int mPluggedType;
103
104 private final LockList mLocks = new LockList();
Eric Shienbrood5711fad2009-03-27 20:25:31 -0700105 // some wifi lock statistics
Irfan Sheriff5876a422010-08-12 20:26:23 -0700106 private int mFullHighPerfLocksAcquired;
107 private int mFullHighPerfLocksReleased;
Eric Shienbrood5711fad2009-03-27 20:25:31 -0700108 private int mFullLocksAcquired;
109 private int mFullLocksReleased;
110 private int mScanLocksAcquired;
111 private int mScanLocksReleased;
The Android Open Source Project10592532009-03-18 17:39:46 -0700112
Irfan Sheriff8cef0672011-12-13 17:03:59 -0800113 /* A mapping from UID to scan count */
114 private HashMap<Integer, Integer> mScanCount =
115 new HashMap<Integer, Integer>();
116
Robert Greenwalt58ff0212009-05-19 15:53:54 -0700117 private final List<Multicaster> mMulticasters =
118 new ArrayList<Multicaster>();
Robert Greenwalt5347bd42009-05-13 15:10:16 -0700119 private int mMulticastEnabled;
120 private int mMulticastDisabled;
121
The Android Open Source Project10592532009-03-18 17:39:46 -0700122 private final IBatteryStats mBatteryStats;
Jaikumar Ganesh084c6652009-12-07 10:58:18 -0800123
Irfan Sheriff227bec42011-02-15 19:30:27 -0800124 private boolean mEnableTrafficStatsPoll = false;
125 private int mTrafficStatsPollToken = 0;
126 private long mTxPkts;
127 private long mRxPkts;
128 /* Tracks last reported data activity */
129 private int mDataActivity;
130 private String mInterfaceName;
131
132 /**
133 * Interval in milliseconds between polling for traffic
134 * statistics
135 */
136 private static final int POLL_TRAFFIC_STATS_INTERVAL_MSECS = 1000;
137
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800138 /**
Doug Zongker43866e02010-01-07 12:09:54 -0800139 * See {@link Settings.Secure#WIFI_IDLE_MS}. This is the default value if a
140 * Settings.Secure value is not present. This timeout value is chosen as
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800141 * the approximate point at which the battery drain caused by Wi-Fi
142 * being enabled but not active exceeds the battery drain caused by
143 * re-establishing a connection to the mobile data network.
144 */
Irfan Sheriff4f5f7c92010-10-14 17:01:27 -0700145 private static final long DEFAULT_IDLE_MS = 15 * 60 * 1000; /* 15 minutes */
146
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800147 private static final String ACTION_DEVICE_IDLE =
148 "com.android.server.WifiManager.action.DEVICE_IDLE";
149
Irfan Sheriff658772f2011-03-08 14:52:31 -0800150 private static final int WIFI_DISABLED = 0;
151 private static final int WIFI_ENABLED = 1;
152 /* Wifi enabled while in airplane mode */
153 private static final int WIFI_ENABLED_AIRPLANE_OVERRIDE = 2;
Irfan Sheriff31b92e22011-10-03 12:13:20 -0700154 /* Wifi disabled due to airplane mode on */
155 private static final int WIFI_DISABLED_AIRPLANE_ON = 3;
Irfan Sheriff658772f2011-03-08 14:52:31 -0800156
Irfan Sheriff5401f0b2011-12-07 16:27:49 -0800157 /* Persisted state that tracks the wifi & airplane interaction from settings */
158 private AtomicInteger mPersistWifiState = new AtomicInteger(WIFI_DISABLED);
159 /* Tracks current airplane mode state */
Irfan Sheriff658772f2011-03-08 14:52:31 -0800160 private AtomicBoolean mAirplaneModeOn = new AtomicBoolean(false);
Irfan Sheriff5401f0b2011-12-07 16:27:49 -0800161 /* Tracks whether wifi is enabled from WifiStateMachine's perspective */
162 private boolean mWifiEnabled;
Irfan Sheriff658772f2011-03-08 14:52:31 -0800163
Irfan Sheriffa2a1b912010-06-07 09:03:04 -0700164 private boolean mIsReceiverRegistered = false;
165
Irfan Sheriff0d255342010-07-28 09:35:20 -0700166
167 NetworkInfo mNetworkInfo = new NetworkInfo(ConnectivityManager.TYPE_WIFI, 0, "WIFI", "");
168
169 // Variables relating to the 'available networks' notification
170 /**
171 * The icon to show in the 'available networks' notification. This will also
172 * be the ID of the Notification given to the NotificationManager.
173 */
174 private static final int ICON_NETWORKS_AVAILABLE =
175 com.android.internal.R.drawable.stat_notify_wifi_in_range;
176 /**
177 * When a notification is shown, we wait this amount before possibly showing it again.
178 */
179 private final long NOTIFICATION_REPEAT_DELAY_MS;
180 /**
181 * Whether the user has set the setting to show the 'available networks' notification.
182 */
183 private boolean mNotificationEnabled;
184 /**
185 * Observes the user setting to keep {@link #mNotificationEnabled} in sync.
186 */
187 private NotificationEnabledSettingObserver mNotificationEnabledSettingObserver;
188 /**
189 * The {@link System#currentTimeMillis()} must be at least this value for us
190 * to show the notification again.
191 */
192 private long mNotificationRepeatTime;
193 /**
194 * The Notification object given to the NotificationManager.
195 */
196 private Notification mNotification;
197 /**
198 * Whether the notification is being shown, as set by us. That is, if the
199 * user cancels the notification, we will not receive the callback so this
200 * will still be true. We only guarantee if this is false, then the
201 * notification is not showing.
202 */
203 private boolean mNotificationShown;
204 /**
205 * The number of continuous scans that must occur before consider the
206 * supplicant in a scanning state. This allows supplicant to associate with
207 * remembered networks that are in the scan results.
208 */
209 private static final int NUM_SCANS_BEFORE_ACTUALLY_SCANNING = 3;
210 /**
211 * The number of scans since the last network state change. When this
212 * exceeds {@link #NUM_SCANS_BEFORE_ACTUALLY_SCANNING}, we consider the
213 * supplicant to actually be scanning. When the network state changes to
214 * something other than scanning, we reset this to 0.
215 */
216 private int mNumScansSinceNetworkStateChange;
Jaikumar Ganesh7440fc22010-09-27 17:04:14 -0700217
Dianne Hackborn03f3cb02010-09-17 23:12:26 -0700218 /**
Wink Saville4b7ba092010-10-20 15:37:41 -0700219 * Asynchronous channel to WifiStateMachine
220 */
Irfan Sheriff227bec42011-02-15 19:30:27 -0800221 private AsyncChannel mWifiStateMachineChannel;
Wink Saville4b7ba092010-10-20 15:37:41 -0700222
223 /**
Irfan Sheriff227bec42011-02-15 19:30:27 -0800224 * Clients receiving asynchronous messages
Wink Saville4b7ba092010-10-20 15:37:41 -0700225 */
Irfan Sheriff227bec42011-02-15 19:30:27 -0800226 private List<AsyncChannel> mClients = new ArrayList<AsyncChannel>();
Wink Saville4b7ba092010-10-20 15:37:41 -0700227
Irfan Sheriff227bec42011-02-15 19:30:27 -0800228 /**
229 * Handles client connections
230 */
231 private class AsyncServiceHandler extends Handler {
232
233 AsyncServiceHandler(android.os.Looper looper) {
Wink Saville4b7ba092010-10-20 15:37:41 -0700234 super(looper);
Wink Saville4b7ba092010-10-20 15:37:41 -0700235 }
236
237 @Override
238 public void handleMessage(Message msg) {
239 switch (msg.what) {
240 case AsyncChannel.CMD_CHANNEL_HALF_CONNECTED: {
241 if (msg.arg1 == AsyncChannel.STATUS_SUCCESSFUL) {
Irfan Sheriff227bec42011-02-15 19:30:27 -0800242 Slog.d(TAG, "New client listening to asynchronous messages");
243 mClients.add((AsyncChannel) msg.obj);
Wink Saville4b7ba092010-10-20 15:37:41 -0700244 } else {
Irfan Sheriff227bec42011-02-15 19:30:27 -0800245 Slog.e(TAG, "Client connection failure, error=" + msg.arg1);
246 }
247 break;
248 }
Irfan Sheriffc23971b2011-03-04 17:06:31 -0800249 case AsyncChannel.CMD_CHANNEL_DISCONNECTED: {
250 if (msg.arg1 == AsyncChannel.STATUS_SEND_UNSUCCESSFUL) {
251 Slog.d(TAG, "Send failed, client connection lost");
252 } else {
253 Slog.d(TAG, "Client connection lost with reason: " + msg.arg1);
254 }
255 mClients.remove((AsyncChannel) msg.obj);
256 break;
257 }
Irfan Sheriff227bec42011-02-15 19:30:27 -0800258 case AsyncChannel.CMD_CHANNEL_FULL_CONNECTION: {
259 AsyncChannel ac = new AsyncChannel();
260 ac.connect(mContext, this, msg.replyTo);
261 break;
262 }
Irfan Sheriffd3975a92012-02-24 10:54:13 -0800263 case WifiManager.ENABLE_TRAFFIC_STATS_POLL: {
Irfan Sheriff227bec42011-02-15 19:30:27 -0800264 mEnableTrafficStatsPoll = (msg.arg1 == 1);
265 mTrafficStatsPollToken++;
266 if (mEnableTrafficStatsPoll) {
267 notifyOnDataActivity();
Irfan Sheriffd3975a92012-02-24 10:54:13 -0800268 sendMessageDelayed(Message.obtain(this, WifiManager.TRAFFIC_STATS_POLL,
Irfan Sheriff227bec42011-02-15 19:30:27 -0800269 mTrafficStatsPollToken, 0), POLL_TRAFFIC_STATS_INTERVAL_MSECS);
270 }
271 break;
272 }
Irfan Sheriffd3975a92012-02-24 10:54:13 -0800273 case WifiManager.TRAFFIC_STATS_POLL: {
Irfan Sheriff227bec42011-02-15 19:30:27 -0800274 if (msg.arg1 == mTrafficStatsPollToken) {
275 notifyOnDataActivity();
Irfan Sheriffd3975a92012-02-24 10:54:13 -0800276 sendMessageDelayed(Message.obtain(this, WifiManager.TRAFFIC_STATS_POLL,
Irfan Sheriff227bec42011-02-15 19:30:27 -0800277 mTrafficStatsPollToken, 0), POLL_TRAFFIC_STATS_INTERVAL_MSECS);
Wink Saville4b7ba092010-10-20 15:37:41 -0700278 }
279 break;
280 }
Irfan Sheriffd3975a92012-02-24 10:54:13 -0800281 case WifiManager.CONNECT_NETWORK: {
282 mWifiStateMachine.sendMessage(Message.obtain(msg));
Irfan Sheriffebe606f2011-02-24 11:39:15 -0800283 break;
284 }
Irfan Sheriffd3975a92012-02-24 10:54:13 -0800285 case WifiManager.SAVE_NETWORK: {
286 mWifiStateMachine.sendMessage(Message.obtain(msg));
Irfan Sheriffebe606f2011-02-24 11:39:15 -0800287 break;
288 }
Irfan Sheriffd3975a92012-02-24 10:54:13 -0800289 case WifiManager.FORGET_NETWORK: {
290 mWifiStateMachine.sendMessage(Message.obtain(msg));
Irfan Sheriffebe606f2011-02-24 11:39:15 -0800291 break;
292 }
Irfan Sheriffd3975a92012-02-24 10:54:13 -0800293 case WifiManager.START_WPS: {
294 mWifiStateMachine.sendMessage(Message.obtain(msg));
Irfan Sheriffebe606f2011-02-24 11:39:15 -0800295 break;
296 }
Irfan Sheriff86a5f5b2012-02-28 17:03:56 -0800297 case WifiManager.CANCEL_WPS: {
298 mWifiStateMachine.sendMessage(Message.obtain(msg));
299 break;
300 }
Irfan Sheriffd3975a92012-02-24 10:54:13 -0800301 case WifiManager.DISABLE_NETWORK: {
302 mWifiStateMachine.sendMessage(Message.obtain(msg));
Isaac Levy8dc6a1b2011-07-27 08:00:03 -0700303 break;
304 }
Wink Saville4b7ba092010-10-20 15:37:41 -0700305 default: {
306 Slog.d(TAG, "WifiServicehandler.handleMessage ignoring msg=" + msg);
307 break;
308 }
309 }
310 }
311 }
Irfan Sheriff227bec42011-02-15 19:30:27 -0800312 private AsyncServiceHandler mAsyncServiceHandler;
313
314 /**
315 * Handles interaction with WifiStateMachine
316 */
317 private class WifiStateMachineHandler extends Handler {
318 private AsyncChannel mWsmChannel;
319
320 WifiStateMachineHandler(android.os.Looper looper) {
321 super(looper);
322 mWsmChannel = new AsyncChannel();
323 mWsmChannel.connect(mContext, this, mWifiStateMachine.getHandler());
324 }
325
326 @Override
327 public void handleMessage(Message msg) {
328 switch (msg.what) {
329 case AsyncChannel.CMD_CHANNEL_HALF_CONNECTED: {
330 if (msg.arg1 == AsyncChannel.STATUS_SUCCESSFUL) {
331 mWifiStateMachineChannel = mWsmChannel;
332 } else {
333 Slog.e(TAG, "WifiStateMachine connection failure, error=" + msg.arg1);
334 mWifiStateMachineChannel = null;
335 }
336 break;
337 }
Irfan Sheriff6da83d52011-06-06 12:54:06 -0700338 case AsyncChannel.CMD_CHANNEL_DISCONNECTED: {
339 Slog.e(TAG, "WifiStateMachine channel lost, msg.arg1 =" + msg.arg1);
340 mWifiStateMachineChannel = null;
341 //Re-establish connection to state machine
342 mWsmChannel.connect(mContext, this, mWifiStateMachine.getHandler());
343 break;
344 }
Irfan Sheriff227bec42011-02-15 19:30:27 -0800345 default: {
346 Slog.d(TAG, "WifiStateMachineHandler.handleMessage ignoring msg=" + msg);
347 break;
348 }
349 }
350 }
351 }
352 WifiStateMachineHandler mWifiStateMachineHandler;
Wink Saville4b7ba092010-10-20 15:37:41 -0700353
354 /**
Dianne Hackborn03f3cb02010-09-17 23:12:26 -0700355 * Temporary for computing UIDS that are responsible for starting WIFI.
356 * Protected by mWifiStateTracker lock.
357 */
358 private final WorkSource mTmpWorkSource = new WorkSource();
Isaac Levy654f5092011-07-13 17:41:45 -0700359 private WifiWatchdogStateMachine mWifiWatchdogStateMachine;
Irfan Sheriff0d255342010-07-28 09:35:20 -0700360
361 WifiService(Context context) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800362 mContext = context;
Irfan Sheriff227bec42011-02-15 19:30:27 -0800363
364 mInterfaceName = SystemProperties.get("wifi.interface", "wlan0");
365
366 mWifiStateMachine = new WifiStateMachine(mContext, mInterfaceName);
Irfan Sheriff0d255342010-07-28 09:35:20 -0700367 mWifiStateMachine.enableRssiPolling(true);
The Android Open Source Project10592532009-03-18 17:39:46 -0700368 mBatteryStats = BatteryStatsService.getService();
Jaikumar Ganesh084c6652009-12-07 10:58:18 -0800369
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800370 mAlarmManager = (AlarmManager)mContext.getSystemService(Context.ALARM_SERVICE);
371 Intent idleIntent = new Intent(ACTION_DEVICE_IDLE, null);
372 mIdleIntent = PendingIntent.getBroadcast(mContext, IDLE_REQUEST, idleIntent, 0);
373
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800374 mContext.registerReceiver(
375 new BroadcastReceiver() {
376 @Override
377 public void onReceive(Context context, Intent intent) {
Irfan Sheriff658772f2011-03-08 14:52:31 -0800378 mAirplaneModeOn.set(isAirplaneModeOn());
379 /* On airplane mode disable, restore wifi state if necessary */
380 if (!mAirplaneModeOn.get() && (testAndClearWifiSavedState() ||
Irfan Sheriff5401f0b2011-12-07 16:27:49 -0800381 mPersistWifiState.get() == WIFI_ENABLED_AIRPLANE_OVERRIDE)) {
382 persistWifiState(true);
Irfan Sheriffb2e6c012010-04-05 11:57:56 -0700383 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800384 updateWifiState();
385 }
386 },
387 new IntentFilter(Intent.ACTION_AIRPLANE_MODE_CHANGED));
388
Irfan Sheriff0d255342010-07-28 09:35:20 -0700389 IntentFilter filter = new IntentFilter();
390 filter.addAction(WifiManager.WIFI_STATE_CHANGED_ACTION);
391 filter.addAction(WifiManager.NETWORK_STATE_CHANGED_ACTION);
392 filter.addAction(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION);
393
394 mContext.registerReceiver(
395 new BroadcastReceiver() {
396 @Override
397 public void onReceive(Context context, Intent intent) {
398 if (intent.getAction().equals(WifiManager.WIFI_STATE_CHANGED_ACTION)) {
Irfan Sheriff5401f0b2011-12-07 16:27:49 -0800399 int wifiState = intent.getIntExtra(WifiManager.EXTRA_WIFI_STATE,
400 WifiManager.WIFI_STATE_DISABLED);
401
402 mWifiEnabled = (wifiState == WifiManager.WIFI_STATE_ENABLED);
403
404 // reset & clear notification on any wifi state change
Irfan Sheriff0d255342010-07-28 09:35:20 -0700405 resetNotification();
406 } else if (intent.getAction().equals(
407 WifiManager.NETWORK_STATE_CHANGED_ACTION)) {
408 mNetworkInfo = (NetworkInfo) intent.getParcelableExtra(
409 WifiManager.EXTRA_NETWORK_INFO);
410 // reset & clear notification on a network connect & disconnect
411 switch(mNetworkInfo.getDetailedState()) {
412 case CONNECTED:
413 case DISCONNECTED:
Irfan Sheriff227bec42011-02-15 19:30:27 -0800414 evaluateTrafficStatsPolling();
Irfan Sheriff0d255342010-07-28 09:35:20 -0700415 resetNotification();
416 break;
417 }
418 } else if (intent.getAction().equals(
419 WifiManager.SCAN_RESULTS_AVAILABLE_ACTION)) {
420 checkAndSetNotification();
421 }
422 }
423 }, filter);
424
Irfan Sheriff227bec42011-02-15 19:30:27 -0800425 HandlerThread wifiThread = new HandlerThread("WifiService");
426 wifiThread.start();
427 mAsyncServiceHandler = new AsyncServiceHandler(wifiThread.getLooper());
428 mWifiStateMachineHandler = new WifiStateMachineHandler(wifiThread.getLooper());
429
Irfan Sheriff0d255342010-07-28 09:35:20 -0700430 // Setting is in seconds
431 NOTIFICATION_REPEAT_DELAY_MS = Settings.Secure.getInt(context.getContentResolver(),
432 Settings.Secure.WIFI_NETWORKS_AVAILABLE_REPEAT_DELAY, 900) * 1000l;
433 mNotificationEnabledSettingObserver = new NotificationEnabledSettingObserver(new Handler());
434 mNotificationEnabledSettingObserver.register();
Irfan Sheriff7b009782010-03-11 16:37:45 -0800435 }
Irfan Sheriff5321aef2010-02-12 12:35:59 -0800436
Irfan Sheriff7b009782010-03-11 16:37:45 -0800437 /**
438 * Check if Wi-Fi needs to be enabled and start
439 * if needed
Irfan Sheriff60e3ba02010-04-02 12:18:45 -0700440 *
441 * This function is used only at boot time
Irfan Sheriff7b009782010-03-11 16:37:45 -0800442 */
Irfan Sheriff0d255342010-07-28 09:35:20 -0700443 public void checkAndStartWifi() {
Irfan Sheriff658772f2011-03-08 14:52:31 -0800444 mAirplaneModeOn.set(isAirplaneModeOn());
Irfan Sheriff5401f0b2011-12-07 16:27:49 -0800445 mPersistWifiState.set(getPersistedWifiState());
Irfan Sheriff658772f2011-03-08 14:52:31 -0800446 /* Start if Wi-Fi should be enabled or the saved state indicates Wi-Fi was on */
447 boolean wifiEnabled = shouldWifiBeEnabled() || testAndClearWifiSavedState();
Irfan Sheriff7b009782010-03-11 16:37:45 -0800448 Slog.i(TAG, "WifiService starting up with Wi-Fi " +
449 (wifiEnabled ? "enabled" : "disabled"));
Irfan Sheriffb99fe5e2010-03-26 14:56:07 -0700450 setWifiEnabled(wifiEnabled);
Isaac Levybc7dfb52011-06-06 15:34:01 -0700451
Isaac Levy654f5092011-07-13 17:41:45 -0700452 mWifiWatchdogStateMachine = WifiWatchdogStateMachine.
453 makeWifiWatchdogStateMachine(mContext);
454
Irfan Sheriff5321aef2010-02-12 12:35:59 -0800455 }
456
Irfan Sheriffa3bd4092010-03-24 17:58:59 -0700457 private boolean testAndClearWifiSavedState() {
458 final ContentResolver cr = mContext.getContentResolver();
459 int wifiSavedState = 0;
460 try {
461 wifiSavedState = Settings.Secure.getInt(cr, Settings.Secure.WIFI_SAVED_STATE);
462 if(wifiSavedState == 1)
463 Settings.Secure.putInt(cr, Settings.Secure.WIFI_SAVED_STATE, 0);
464 } catch (Settings.SettingNotFoundException e) {
465 ;
466 }
467 return (wifiSavedState == 1);
468 }
469
Irfan Sheriff658772f2011-03-08 14:52:31 -0800470 private int getPersistedWifiState() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800471 final ContentResolver cr = mContext.getContentResolver();
472 try {
Irfan Sheriff658772f2011-03-08 14:52:31 -0800473 return Settings.Secure.getInt(cr, Settings.Secure.WIFI_ON);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800474 } catch (Settings.SettingNotFoundException e) {
Irfan Sheriff658772f2011-03-08 14:52:31 -0800475 Settings.Secure.putInt(cr, Settings.Secure.WIFI_ON, WIFI_DISABLED);
476 return WIFI_DISABLED;
477 }
478 }
479
480 private boolean shouldWifiBeEnabled() {
481 if (mAirplaneModeOn.get()) {
Irfan Sheriff5401f0b2011-12-07 16:27:49 -0800482 return mPersistWifiState.get() == WIFI_ENABLED_AIRPLANE_OVERRIDE;
Irfan Sheriff658772f2011-03-08 14:52:31 -0800483 } else {
Irfan Sheriff5401f0b2011-12-07 16:27:49 -0800484 return mPersistWifiState.get() != WIFI_DISABLED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800485 }
486 }
487
Irfan Sheriff5401f0b2011-12-07 16:27:49 -0800488 private void persistWifiState(boolean enabled) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800489 final ContentResolver cr = mContext.getContentResolver();
Irfan Sheriff31b92e22011-10-03 12:13:20 -0700490 boolean airplane = mAirplaneModeOn.get() && isAirplaneToggleable();
Irfan Sheriff658772f2011-03-08 14:52:31 -0800491 if (enabled) {
Irfan Sheriff31b92e22011-10-03 12:13:20 -0700492 if (airplane) {
Irfan Sheriff5401f0b2011-12-07 16:27:49 -0800493 mPersistWifiState.set(WIFI_ENABLED_AIRPLANE_OVERRIDE);
Irfan Sheriff658772f2011-03-08 14:52:31 -0800494 } else {
Irfan Sheriff5401f0b2011-12-07 16:27:49 -0800495 mPersistWifiState.set(WIFI_ENABLED);
Irfan Sheriff658772f2011-03-08 14:52:31 -0800496 }
497 } else {
Irfan Sheriff31b92e22011-10-03 12:13:20 -0700498 if (airplane) {
Irfan Sheriff5401f0b2011-12-07 16:27:49 -0800499 mPersistWifiState.set(WIFI_DISABLED_AIRPLANE_ON);
Irfan Sheriff31b92e22011-10-03 12:13:20 -0700500 } else {
Irfan Sheriff5401f0b2011-12-07 16:27:49 -0800501 mPersistWifiState.set(WIFI_DISABLED);
Irfan Sheriff31b92e22011-10-03 12:13:20 -0700502 }
Irfan Sheriff658772f2011-03-08 14:52:31 -0800503 }
Irfan Sheriff5401f0b2011-12-07 16:27:49 -0800504
505 Settings.Secure.putInt(cr, Settings.Secure.WIFI_ON, mPersistWifiState.get());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800506 }
507
Irfan Sheriff658772f2011-03-08 14:52:31 -0800508
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800509 /**
510 * see {@link android.net.wifi.WifiManager#pingSupplicant()}
Irfan Sheriffa2a1b912010-06-07 09:03:04 -0700511 * @return {@code true} if the operation succeeds, {@code false} otherwise
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800512 */
513 public boolean pingSupplicant() {
Irfan Sheriffa2a1b912010-06-07 09:03:04 -0700514 enforceAccessPermission();
Irfan Sheriff227bec42011-02-15 19:30:27 -0800515 if (mWifiStateMachineChannel != null) {
516 return mWifiStateMachine.syncPingSupplicant(mWifiStateMachineChannel);
Irfan Sheriff1406bcb2010-10-28 14:41:39 -0700517 } else {
Irfan Sheriff227bec42011-02-15 19:30:27 -0800518 Slog.e(TAG, "mWifiStateMachineChannel is not initialized");
Irfan Sheriff1406bcb2010-10-28 14:41:39 -0700519 return false;
520 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800521 }
522
523 /**
524 * see {@link android.net.wifi.WifiManager#startScan()}
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800525 */
Irfan Sheriffe4984752010-08-19 11:29:22 -0700526 public void startScan(boolean forceActive) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800527 enforceChangePermission();
Irfan Sheriff8cef0672011-12-13 17:03:59 -0800528
529 int uid = Binder.getCallingUid();
530 int count = 0;
531 synchronized (mScanCount) {
532 if (mScanCount.containsKey(uid)) {
533 count = mScanCount.get(uid);
534 }
535 mScanCount.put(uid, ++count);
536 }
Irfan Sheriffe4984752010-08-19 11:29:22 -0700537 mWifiStateMachine.startScan(forceActive);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800538 }
539
540 private void enforceAccessPermission() {
541 mContext.enforceCallingOrSelfPermission(android.Manifest.permission.ACCESS_WIFI_STATE,
542 "WifiService");
543 }
544
545 private void enforceChangePermission() {
546 mContext.enforceCallingOrSelfPermission(android.Manifest.permission.CHANGE_WIFI_STATE,
547 "WifiService");
548
549 }
550
Robert Greenwaltfc1b15c2009-05-22 15:09:51 -0700551 private void enforceMulticastChangePermission() {
552 mContext.enforceCallingOrSelfPermission(
553 android.Manifest.permission.CHANGE_WIFI_MULTICAST_STATE,
554 "WifiService");
555 }
556
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800557 /**
Irfan Sheriffa2a1b912010-06-07 09:03:04 -0700558 * see {@link android.net.wifi.WifiManager#setWifiEnabled(boolean)}
559 * @param enable {@code true} to enable, {@code false} to disable.
560 * @return {@code true} if the enable/disable operation was
561 * started or is already in the queue.
562 */
563 public synchronized boolean setWifiEnabled(boolean enable) {
564 enforceChangePermission();
Irfan Sheriffa2a1b912010-06-07 09:03:04 -0700565 if (DBG) {
Irfan Sheriff0d255342010-07-28 09:35:20 -0700566 Slog.e(TAG, "Invoking mWifiStateMachine.setWifiEnabled\n");
Irfan Sheriffa2a1b912010-06-07 09:03:04 -0700567 }
568
Dianne Hackborn03f3cb02010-09-17 23:12:26 -0700569 if (enable) {
570 reportStartWorkSource();
571 }
Irfan Sheriff0d255342010-07-28 09:35:20 -0700572 mWifiStateMachine.setWifiEnabled(enable);
Irfan Sheriff61180692010-08-18 16:07:39 -0700573
574 /*
575 * Caller might not have WRITE_SECURE_SETTINGS,
576 * only CHANGE_WIFI_STATE is enforced
577 */
Irfan Sheriff5401f0b2011-12-07 16:27:49 -0800578
579 /* Avoids overriding of airplane state when wifi is already in the expected state */
580 if (enable != mWifiEnabled) {
581 long ident = Binder.clearCallingIdentity();
582 persistWifiState(enable);
583 Binder.restoreCallingIdentity(ident);
584 }
Irfan Sheriffa2a1b912010-06-07 09:03:04 -0700585
586 if (enable) {
587 if (!mIsReceiverRegistered) {
588 registerForBroadcasts();
589 mIsReceiverRegistered = true;
590 }
Irfan Sheriff5401f0b2011-12-07 16:27:49 -0800591 } else if (mIsReceiverRegistered) {
Irfan Sheriffa2a1b912010-06-07 09:03:04 -0700592 mContext.unregisterReceiver(mReceiver);
593 mIsReceiverRegistered = false;
594 }
595
596 return true;
597 }
598
599 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800600 * see {@link WifiManager#getWifiState()}
601 * @return One of {@link WifiManager#WIFI_STATE_DISABLED},
602 * {@link WifiManager#WIFI_STATE_DISABLING},
603 * {@link WifiManager#WIFI_STATE_ENABLED},
604 * {@link WifiManager#WIFI_STATE_ENABLING},
605 * {@link WifiManager#WIFI_STATE_UNKNOWN}
606 */
607 public int getWifiEnabledState() {
608 enforceAccessPermission();
Irfan Sheriffd8134ff2010-08-22 17:06:34 -0700609 return mWifiStateMachine.syncGetWifiState();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800610 }
611
612 /**
Irfan Sheriffc2f54c22010-03-18 14:02:22 -0700613 * see {@link android.net.wifi.WifiManager#setWifiApEnabled(WifiConfiguration, boolean)}
Irfan Sheriff5321aef2010-02-12 12:35:59 -0800614 * @param wifiConfig SSID, security and channel details as
615 * part of WifiConfiguration
Irfan Sheriffa2a1b912010-06-07 09:03:04 -0700616 * @param enabled true to enable and false to disable
Irfan Sheriff5321aef2010-02-12 12:35:59 -0800617 */
Irfan Sheriffffcea7a2011-05-10 16:26:06 -0700618 public void setWifiApEnabled(WifiConfiguration wifiConfig, boolean enabled) {
Irfan Sheriff5321aef2010-02-12 12:35:59 -0800619 enforceChangePermission();
Irfan Sheriff0d255342010-07-28 09:35:20 -0700620 mWifiStateMachine.setWifiApEnabled(wifiConfig, enabled);
Irfan Sheriff5321aef2010-02-12 12:35:59 -0800621 }
622
Irfan Sheriffa2a1b912010-06-07 09:03:04 -0700623 /**
624 * see {@link WifiManager#getWifiApState()}
625 * @return One of {@link WifiManager#WIFI_AP_STATE_DISABLED},
626 * {@link WifiManager#WIFI_AP_STATE_DISABLING},
627 * {@link WifiManager#WIFI_AP_STATE_ENABLED},
628 * {@link WifiManager#WIFI_AP_STATE_ENABLING},
629 * {@link WifiManager#WIFI_AP_STATE_FAILED}
630 */
631 public int getWifiApEnabledState() {
Irfan Sheriff17b232b2010-06-24 11:32:26 -0700632 enforceAccessPermission();
Irfan Sheriffd8134ff2010-08-22 17:06:34 -0700633 return mWifiStateMachine.syncGetWifiApState();
Irfan Sheriffa2a1b912010-06-07 09:03:04 -0700634 }
635
636 /**
637 * see {@link WifiManager#getWifiApConfiguration()}
638 * @return soft access point configuration
639 */
Irfan Sheriffffcea7a2011-05-10 16:26:06 -0700640 public WifiConfiguration getWifiApConfiguration() {
641 enforceAccessPermission();
Irfan Sheriff9575a1b2011-11-07 10:34:54 -0800642 return mWifiStateMachine.syncGetWifiApConfiguration();
Irfan Sheriff9ab518ad2010-03-12 15:48:17 -0800643 }
644
Irfan Sheriffa2a1b912010-06-07 09:03:04 -0700645 /**
646 * see {@link WifiManager#setWifiApConfiguration(WifiConfiguration)}
647 * @param wifiConfig WifiConfiguration details for soft access point
648 */
Irfan Sheriffffcea7a2011-05-10 16:26:06 -0700649 public void setWifiApConfiguration(WifiConfiguration wifiConfig) {
Irfan Sheriff17b232b2010-06-24 11:32:26 -0700650 enforceChangePermission();
Irfan Sheriff9ab518ad2010-03-12 15:48:17 -0800651 if (wifiConfig == null)
652 return;
Irfan Sheriffffcea7a2011-05-10 16:26:06 -0700653 mWifiStateMachine.setWifiApConfiguration(wifiConfig);
Irfan Sheriff9ab518ad2010-03-12 15:48:17 -0800654 }
655
Irfan Sheriff5321aef2010-02-12 12:35:59 -0800656 /**
Irfan Sheriffa2a1b912010-06-07 09:03:04 -0700657 * see {@link android.net.wifi.WifiManager#disconnect()}
Irfan Sheriff5321aef2010-02-12 12:35:59 -0800658 */
Irfan Sheriffe4984752010-08-19 11:29:22 -0700659 public void disconnect() {
Irfan Sheriffa2a1b912010-06-07 09:03:04 -0700660 enforceChangePermission();
Irfan Sheriffe4984752010-08-19 11:29:22 -0700661 mWifiStateMachine.disconnectCommand();
Irfan Sheriff5321aef2010-02-12 12:35:59 -0800662 }
663
664 /**
Irfan Sheriffa2a1b912010-06-07 09:03:04 -0700665 * see {@link android.net.wifi.WifiManager#reconnect()}
Irfan Sheriff5321aef2010-02-12 12:35:59 -0800666 */
Irfan Sheriffe4984752010-08-19 11:29:22 -0700667 public void reconnect() {
Irfan Sheriffa2a1b912010-06-07 09:03:04 -0700668 enforceChangePermission();
Irfan Sheriffe4984752010-08-19 11:29:22 -0700669 mWifiStateMachine.reconnectCommand();
Irfan Sheriff5321aef2010-02-12 12:35:59 -0800670 }
671
Irfan Sheriffa2a1b912010-06-07 09:03:04 -0700672 /**
673 * see {@link android.net.wifi.WifiManager#reassociate()}
Irfan Sheriffa2a1b912010-06-07 09:03:04 -0700674 */
Irfan Sheriffe4984752010-08-19 11:29:22 -0700675 public void reassociate() {
Irfan Sheriffa2a1b912010-06-07 09:03:04 -0700676 enforceChangePermission();
Irfan Sheriffe4984752010-08-19 11:29:22 -0700677 mWifiStateMachine.reassociateCommand();
Irfan Sheriff5321aef2010-02-12 12:35:59 -0800678 }
679
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800680 /**
681 * see {@link android.net.wifi.WifiManager#getConfiguredNetworks()}
682 * @return the list of configured networks
683 */
684 public List<WifiConfiguration> getConfiguredNetworks() {
685 enforceAccessPermission();
Irfan Sheriffe744cff2011-12-11 09:17:50 -0800686 if (mWifiStateMachineChannel != null) {
687 return mWifiStateMachine.syncGetConfiguredNetworks(mWifiStateMachineChannel);
688 } else {
689 Slog.e(TAG, "mWifiStateMachineChannel is not initialized");
690 return null;
691 }
Chung-yih Wanga8d15942009-10-09 11:01:49 +0800692 }
693
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800694 /**
695 * see {@link android.net.wifi.WifiManager#addOrUpdateNetwork(WifiConfiguration)}
696 * @return the supplicant-assigned identifier for the new or updated
697 * network if the operation succeeds, or {@code -1} if it fails
698 */
Irfan Sheriff7aac5542009-12-22 21:42:17 -0800699 public int addOrUpdateNetwork(WifiConfiguration config) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800700 enforceChangePermission();
Irfan Sheriff227bec42011-02-15 19:30:27 -0800701 if (mWifiStateMachineChannel != null) {
702 return mWifiStateMachine.syncAddOrUpdateNetwork(mWifiStateMachineChannel, config);
Irfan Sheriff1406bcb2010-10-28 14:41:39 -0700703 } else {
Irfan Sheriff227bec42011-02-15 19:30:27 -0800704 Slog.e(TAG, "mWifiStateMachineChannel is not initialized");
Irfan Sheriff1406bcb2010-10-28 14:41:39 -0700705 return -1;
706 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800707 }
708
Irfan Sheriffa2a1b912010-06-07 09:03:04 -0700709 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800710 * See {@link android.net.wifi.WifiManager#removeNetwork(int)}
711 * @param netId the integer that identifies the network configuration
712 * to the supplicant
713 * @return {@code true} if the operation succeeded
714 */
715 public boolean removeNetwork(int netId) {
716 enforceChangePermission();
Irfan Sheriff227bec42011-02-15 19:30:27 -0800717 if (mWifiStateMachineChannel != null) {
718 return mWifiStateMachine.syncRemoveNetwork(mWifiStateMachineChannel, netId);
Wink Saville4b7ba092010-10-20 15:37:41 -0700719 } else {
Irfan Sheriff227bec42011-02-15 19:30:27 -0800720 Slog.e(TAG, "mWifiStateMachineChannel is not initialized");
Wink Saville4b7ba092010-10-20 15:37:41 -0700721 return false;
722 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800723 }
724
725 /**
726 * See {@link android.net.wifi.WifiManager#enableNetwork(int, boolean)}
727 * @param netId the integer that identifies the network configuration
728 * to the supplicant
729 * @param disableOthers if true, disable all other networks.
730 * @return {@code true} if the operation succeeded
731 */
732 public boolean enableNetwork(int netId, boolean disableOthers) {
733 enforceChangePermission();
Irfan Sheriff227bec42011-02-15 19:30:27 -0800734 if (mWifiStateMachineChannel != null) {
735 return mWifiStateMachine.syncEnableNetwork(mWifiStateMachineChannel, netId,
736 disableOthers);
Irfan Sheriff1406bcb2010-10-28 14:41:39 -0700737 } else {
Irfan Sheriff227bec42011-02-15 19:30:27 -0800738 Slog.e(TAG, "mWifiStateMachineChannel is not initialized");
Irfan Sheriff1406bcb2010-10-28 14:41:39 -0700739 return false;
740 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800741 }
742
743 /**
744 * See {@link android.net.wifi.WifiManager#disableNetwork(int)}
745 * @param netId the integer that identifies the network configuration
746 * to the supplicant
747 * @return {@code true} if the operation succeeded
748 */
749 public boolean disableNetwork(int netId) {
750 enforceChangePermission();
Irfan Sheriff227bec42011-02-15 19:30:27 -0800751 if (mWifiStateMachineChannel != null) {
752 return mWifiStateMachine.syncDisableNetwork(mWifiStateMachineChannel, netId);
Irfan Sheriff1406bcb2010-10-28 14:41:39 -0700753 } else {
Irfan Sheriff227bec42011-02-15 19:30:27 -0800754 Slog.e(TAG, "mWifiStateMachineChannel is not initialized");
Irfan Sheriff1406bcb2010-10-28 14:41:39 -0700755 return false;
756 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800757 }
758
759 /**
760 * See {@link android.net.wifi.WifiManager#getConnectionInfo()}
761 * @return the Wi-Fi information, contained in {@link WifiInfo}.
762 */
763 public WifiInfo getConnectionInfo() {
764 enforceAccessPermission();
765 /*
766 * Make sure we have the latest information, by sending
767 * a status request to the supplicant.
768 */
Irfan Sheriffd8134ff2010-08-22 17:06:34 -0700769 return mWifiStateMachine.syncRequestConnectionInfo();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800770 }
771
772 /**
773 * Return the results of the most recent access point scan, in the form of
774 * a list of {@link ScanResult} objects.
775 * @return the list of results
776 */
777 public List<ScanResult> getScanResults() {
778 enforceAccessPermission();
Irfan Sheriffd8134ff2010-08-22 17:06:34 -0700779 return mWifiStateMachine.syncGetScanResultsList();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800780 }
781
782 /**
783 * Tell the supplicant to persist the current list of configured networks.
784 * @return {@code true} if the operation succeeded
Irfan Sheriffa2a1b912010-06-07 09:03:04 -0700785 *
786 * TODO: deprecate this
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800787 */
788 public boolean saveConfiguration() {
Irfan Sheriffa2a1b912010-06-07 09:03:04 -0700789 boolean result = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800790 enforceChangePermission();
Irfan Sheriff227bec42011-02-15 19:30:27 -0800791 if (mWifiStateMachineChannel != null) {
792 return mWifiStateMachine.syncSaveConfig(mWifiStateMachineChannel);
Irfan Sheriff1406bcb2010-10-28 14:41:39 -0700793 } else {
Irfan Sheriff227bec42011-02-15 19:30:27 -0800794 Slog.e(TAG, "mWifiStateMachineChannel is not initialized");
Irfan Sheriff1406bcb2010-10-28 14:41:39 -0700795 return false;
796 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800797 }
798
799 /**
Irfan Sheriffed4f28b2010-10-29 15:32:10 -0700800 * Set the country code
801 * @param countryCode ISO 3166 country code.
Robert Greenwaltb5010cc2009-05-21 15:11:40 -0700802 * @param persist {@code true} if the setting should be remembered.
Irfan Sheriffed4f28b2010-10-29 15:32:10 -0700803 *
804 * The persist behavior exists so that wifi can fall back to the last
805 * persisted country code on a restart, when the locale information is
806 * not available from telephony.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800807 */
Irfan Sheriffed4f28b2010-10-29 15:32:10 -0700808 public void setCountryCode(String countryCode, boolean persist) {
809 Slog.i(TAG, "WifiService trying to set country code to " + countryCode +
810 " with persist set to " + persist);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800811 enforceChangePermission();
Irfan Sheriffed4f28b2010-10-29 15:32:10 -0700812 mWifiStateMachine.setCountryCode(countryCode, persist);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800813 }
814
815 /**
Irfan Sheriff36f74132010-11-04 16:57:37 -0700816 * Set the operational frequency band
817 * @param band One of
818 * {@link WifiManager#WIFI_FREQUENCY_BAND_AUTO},
819 * {@link WifiManager#WIFI_FREQUENCY_BAND_5GHZ},
820 * {@link WifiManager#WIFI_FREQUENCY_BAND_2GHZ},
821 * @param persist {@code true} if the setting should be remembered.
822 *
823 */
824 public void setFrequencyBand(int band, boolean persist) {
825 enforceChangePermission();
826 if (!isDualBandSupported()) return;
827 Slog.i(TAG, "WifiService trying to set frequency band to " + band +
828 " with persist set to " + persist);
829 mWifiStateMachine.setFrequencyBand(band, persist);
830 }
831
832
833 /**
834 * Get the operational frequency band
835 */
836 public int getFrequencyBand() {
837 enforceAccessPermission();
838 return mWifiStateMachine.getFrequencyBand();
839 }
840
841 public boolean isDualBandSupported() {
842 //TODO: Should move towards adding a driver API that checks at runtime
843 return mContext.getResources().getBoolean(
844 com.android.internal.R.bool.config_wifi_dual_band_support);
845 }
846
847 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800848 * Return the DHCP-assigned addresses from the last successful DHCP request,
849 * if any.
850 * @return the DHCP information
851 */
852 public DhcpInfo getDhcpInfo() {
853 enforceAccessPermission();
Irfan Sheriffd8134ff2010-08-22 17:06:34 -0700854 return mWifiStateMachine.syncGetDhcpInfo();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800855 }
856
Irfan Sheriff0d255342010-07-28 09:35:20 -0700857 /**
858 * see {@link android.net.wifi.WifiManager#startWifi}
859 *
860 */
861 public void startWifi() {
862 enforceChangePermission();
863 /* TODO: may be add permissions for access only to connectivity service
864 * TODO: if a start issued, keep wifi alive until a stop issued irrespective
865 * of WifiLock & device idle status unless wifi enabled status is toggled
866 */
867
Irfan Sheriff4494c902011-12-08 10:47:54 -0800868 mWifiStateMachine.setDriverStart(true, mEmergencyCallbackMode);
Irfan Sheriff0d255342010-07-28 09:35:20 -0700869 mWifiStateMachine.reconnectCommand();
870 }
871
872 /**
873 * see {@link android.net.wifi.WifiManager#stopWifi}
874 *
875 */
876 public void stopWifi() {
877 enforceChangePermission();
878 /* TODO: may be add permissions for access only to connectivity service
879 * TODO: if a stop is issued, wifi is brought up only by startWifi
880 * unless wifi enabled status is toggled
881 */
Irfan Sheriff4494c902011-12-08 10:47:54 -0800882 mWifiStateMachine.setDriverStart(false, mEmergencyCallbackMode);
Irfan Sheriff0d255342010-07-28 09:35:20 -0700883 }
884
885
886 /**
887 * see {@link android.net.wifi.WifiManager#addToBlacklist}
888 *
889 */
890 public void addToBlacklist(String bssid) {
891 enforceChangePermission();
892
893 mWifiStateMachine.addToBlacklist(bssid);
894 }
895
896 /**
897 * see {@link android.net.wifi.WifiManager#clearBlacklist}
898 *
899 */
900 public void clearBlacklist() {
901 enforceChangePermission();
902
903 mWifiStateMachine.clearBlacklist();
904 }
905
Irfan Sheriff227bec42011-02-15 19:30:27 -0800906 /**
907 * Get a reference to handler. This is used by a client to establish
908 * an AsyncChannel communication with WifiService
909 */
Irfan Sheriff07573b32012-01-27 21:00:19 -0800910 public Messenger getWifiServiceMessenger() {
Irfan Sheriffebe606f2011-02-24 11:39:15 -0800911 /* Enforce the highest permissions
912 TODO: when we consider exposing the asynchronous API, think about
913 how to provide both access and change permissions seperately
914 */
Irfan Sheriff227bec42011-02-15 19:30:27 -0800915 enforceAccessPermission();
Irfan Sheriffebe606f2011-02-24 11:39:15 -0800916 enforceChangePermission();
Irfan Sheriff227bec42011-02-15 19:30:27 -0800917 return new Messenger(mAsyncServiceHandler);
918 }
919
Irfan Sheriff07573b32012-01-27 21:00:19 -0800920 /** Get a reference to WifiStateMachine handler for AsyncChannel communication */
921 public Messenger getWifiStateMachineMessenger() {
922 enforceAccessPermission();
923 enforceChangePermission();
924 return mWifiStateMachine.getMessenger();
925 }
926
Irfan Sheriff4aeca7c52011-03-10 16:53:33 -0800927 /**
928 * Get the IP and proxy configuration file
929 */
930 public String getConfigFile() {
931 enforceAccessPermission();
932 return mWifiStateMachine.getConfigFile();
933 }
934
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800935 private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
936 @Override
937 public void onReceive(Context context, Intent intent) {
938 String action = intent.getAction();
939
Doug Zongker43866e02010-01-07 12:09:54 -0800940 long idleMillis =
941 Settings.Secure.getLong(mContext.getContentResolver(),
Irfan Sheriff4f5f7c92010-10-14 17:01:27 -0700942 Settings.Secure.WIFI_IDLE_MS, DEFAULT_IDLE_MS);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800943 int stayAwakeConditions =
Doug Zongker43866e02010-01-07 12:09:54 -0800944 Settings.System.getInt(mContext.getContentResolver(),
945 Settings.System.STAY_ON_WHILE_PLUGGED_IN, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800946 if (action.equals(Intent.ACTION_SCREEN_ON)) {
Joe Onorato431bb222010-10-18 19:13:23 -0400947 if (DBG) {
948 Slog.d(TAG, "ACTION_SCREEN_ON");
949 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800950 mAlarmManager.cancel(mIdleIntent);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800951 mScreenOff = false;
Irfan Sheriff227bec42011-02-15 19:30:27 -0800952 evaluateTrafficStatsPolling();
Irfan Sheriffe6daca52011-11-03 15:46:50 -0700953 setDeviceIdleAndUpdateWifi(false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800954 } else if (action.equals(Intent.ACTION_SCREEN_OFF)) {
Joe Onorato431bb222010-10-18 19:13:23 -0400955 if (DBG) {
956 Slog.d(TAG, "ACTION_SCREEN_OFF");
957 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800958 mScreenOff = true;
Irfan Sheriff227bec42011-02-15 19:30:27 -0800959 evaluateTrafficStatsPolling();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800960 /*
961 * Set a timer to put Wi-Fi to sleep, but only if the screen is off
962 * AND the "stay on while plugged in" setting doesn't match the
963 * current power conditions (i.e, not plugged in, plugged in to USB,
964 * or plugged in to AC).
965 */
966 if (!shouldWifiStayAwake(stayAwakeConditions, mPluggedType)) {
Irfan Sheriffe6daca52011-11-03 15:46:50 -0700967 //Delayed shutdown if wifi is connected
968 if (mNetworkInfo.getDetailedState() == DetailedState.CONNECTED) {
969 if (DBG) Slog.d(TAG, "setting ACTION_DEVICE_IDLE: " + idleMillis + " ms");
970 mAlarmManager.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis()
971 + idleMillis, mIdleIntent);
Mike Lockwoodd9c32bc2009-05-18 14:14:15 -0400972 } else {
Irfan Sheriffe6daca52011-11-03 15:46:50 -0700973 setDeviceIdleAndUpdateWifi(true);
Mike Lockwoodd9c32bc2009-05-18 14:14:15 -0400974 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800975 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800976 } else if (action.equals(ACTION_DEVICE_IDLE)) {
Irfan Sheriffe6daca52011-11-03 15:46:50 -0700977 setDeviceIdleAndUpdateWifi(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800978 } else if (action.equals(Intent.ACTION_BATTERY_CHANGED)) {
979 /*
980 * Set a timer to put Wi-Fi to sleep, but only if the screen is off
981 * AND we are transitioning from a state in which the device was supposed
982 * to stay awake to a state in which it is not supposed to stay awake.
983 * If "stay awake" state is not changing, we do nothing, to avoid resetting
984 * the already-set timer.
985 */
986 int pluggedType = intent.getIntExtra("plugged", 0);
Joe Onorato431bb222010-10-18 19:13:23 -0400987 if (DBG) {
988 Slog.d(TAG, "ACTION_BATTERY_CHANGED pluggedType: " + pluggedType);
989 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800990 if (mScreenOff && shouldWifiStayAwake(stayAwakeConditions, mPluggedType) &&
991 !shouldWifiStayAwake(stayAwakeConditions, pluggedType)) {
992 long triggerTime = System.currentTimeMillis() + idleMillis;
Joe Onorato431bb222010-10-18 19:13:23 -0400993 if (DBG) {
994 Slog.d(TAG, "setting ACTION_DEVICE_IDLE timer for " + idleMillis + "ms");
995 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800996 mAlarmManager.set(AlarmManager.RTC_WAKEUP, triggerTime, mIdleIntent);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800997 }
Irfan Sheriff8cef0672011-12-13 17:03:59 -0800998
999 //Start scan stats tracking when device unplugged
1000 if (pluggedType == 0) {
1001 synchronized (mScanCount) {
1002 mScanCount.clear();
1003 }
1004 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001005 mPluggedType = pluggedType;
Irfan Sheriff65eaec82011-01-05 22:00:16 -08001006 } else if (action.equals(BluetoothAdapter.ACTION_CONNECTION_STATE_CHANGED)) {
1007 int state = intent.getIntExtra(BluetoothAdapter.EXTRA_CONNECTION_STATE,
1008 BluetoothAdapter.STATE_DISCONNECTED);
1009 mWifiStateMachine.sendBluetoothAdapterStateChange(state);
Irfan Sheriff616f3172011-09-11 19:59:01 -07001010 } else if (action.equals(TelephonyIntents.ACTION_EMERGENCY_CALLBACK_MODE_CHANGED)) {
1011 mEmergencyCallbackMode = intent.getBooleanExtra("phoneinECMState", false);
1012 updateWifiState();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001013 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001014 }
1015
1016 /**
1017 * Determines whether the Wi-Fi chipset should stay awake or be put to
1018 * sleep. Looks at the setting for the sleep policy and the current
1019 * conditions.
Jaikumar Ganesh084c6652009-12-07 10:58:18 -08001020 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001021 * @see #shouldDeviceStayAwake(int, int)
1022 */
1023 private boolean shouldWifiStayAwake(int stayAwakeConditions, int pluggedType) {
Irfan Sheriff739f6bc2011-01-28 16:43:12 -08001024 //Never sleep as long as the user has not changed the settings
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001025 int wifiSleepPolicy = Settings.System.getInt(mContext.getContentResolver(),
Irfan Sheriff96b10d62011-01-11 15:40:35 -08001026 Settings.System.WIFI_SLEEP_POLICY,
Irfan Sheriff739f6bc2011-01-28 16:43:12 -08001027 Settings.System.WIFI_SLEEP_POLICY_NEVER);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001028
1029 if (wifiSleepPolicy == Settings.System.WIFI_SLEEP_POLICY_NEVER) {
1030 // Never sleep
1031 return true;
1032 } else if ((wifiSleepPolicy == Settings.System.WIFI_SLEEP_POLICY_NEVER_WHILE_PLUGGED) &&
1033 (pluggedType != 0)) {
1034 // Never sleep while plugged, and we're plugged
1035 return true;
1036 } else {
1037 // Default
1038 return shouldDeviceStayAwake(stayAwakeConditions, pluggedType);
1039 }
1040 }
Jaikumar Ganesh084c6652009-12-07 10:58:18 -08001041
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001042 /**
1043 * Determine whether the bit value corresponding to {@code pluggedType} is set in
1044 * the bit string {@code stayAwakeConditions}. Because a {@code pluggedType} value
1045 * of {@code 0} isn't really a plugged type, but rather an indication that the
1046 * device isn't plugged in at all, there is no bit value corresponding to a
1047 * {@code pluggedType} value of {@code 0}. That is why we shift by
Ben Dodson4e8620f2010-08-25 10:55:47 -07001048 * {@code pluggedType - 1} instead of by {@code pluggedType}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001049 * @param stayAwakeConditions a bit string specifying which "plugged types" should
1050 * keep the device (and hence Wi-Fi) awake.
1051 * @param pluggedType the type of plug (USB, AC, or none) for which the check is
1052 * being made
1053 * @return {@code true} if {@code pluggedType} indicates that the device is
1054 * supposed to stay awake, {@code false} otherwise.
1055 */
1056 private boolean shouldDeviceStayAwake(int stayAwakeConditions, int pluggedType) {
1057 return (stayAwakeConditions & pluggedType) != 0;
1058 }
1059 };
1060
Irfan Sheriffe6daca52011-11-03 15:46:50 -07001061 private void setDeviceIdleAndUpdateWifi(boolean deviceIdle) {
1062 mDeviceIdle = deviceIdle;
1063 reportStartWorkSource();
1064 updateWifiState();
1065 }
1066
Dianne Hackborn03f3cb02010-09-17 23:12:26 -07001067 private synchronized void reportStartWorkSource() {
1068 mTmpWorkSource.clear();
1069 if (mDeviceIdle) {
1070 for (int i=0; i<mLocks.mList.size(); i++) {
1071 mTmpWorkSource.add(mLocks.mList.get(i).mWorkSource);
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07001072 }
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07001073 }
Dianne Hackborn03f3cb02010-09-17 23:12:26 -07001074 mWifiStateMachine.updateBatteryWorkSource(mTmpWorkSource);
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07001075 }
Jaikumar Ganesh7440fc22010-09-27 17:04:14 -07001076
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001077 private void updateWifiState() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001078 boolean lockHeld = mLocks.hasLocks();
Irfan Sheriff5876a422010-08-12 20:26:23 -07001079 int strongestLockMode = WifiManager.WIFI_MODE_FULL;
Irfan Sheriff616f3172011-09-11 19:59:01 -07001080 boolean wifiShouldBeStarted;
1081
1082 if (mEmergencyCallbackMode) {
1083 wifiShouldBeStarted = false;
1084 } else {
1085 wifiShouldBeStarted = !mDeviceIdle || lockHeld;
1086 }
Irfan Sheriff5876a422010-08-12 20:26:23 -07001087
1088 if (lockHeld) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001089 strongestLockMode = mLocks.getStrongestLockMode();
Irfan Sheriff5876a422010-08-12 20:26:23 -07001090 }
1091 /* If device is not idle, lockmode cannot be scan only */
1092 if (!mDeviceIdle && strongestLockMode == WifiManager.WIFI_MODE_SCAN_ONLY) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001093 strongestLockMode = WifiManager.WIFI_MODE_FULL;
1094 }
1095
Irfan Sheriffa2a1b912010-06-07 09:03:04 -07001096 /* Disable tethering when airplane mode is enabled */
Irfan Sheriff658772f2011-03-08 14:52:31 -08001097 if (mAirplaneModeOn.get()) {
Irfan Sheriff0d255342010-07-28 09:35:20 -07001098 mWifiStateMachine.setWifiApEnabled(null, false);
Irfan Sheriffa2a1b912010-06-07 09:03:04 -07001099 }
Irfan Sheriffb2e6c012010-04-05 11:57:56 -07001100
Irfan Sheriff658772f2011-03-08 14:52:31 -08001101 if (shouldWifiBeEnabled()) {
Irfan Sheriffa2a1b912010-06-07 09:03:04 -07001102 if (wifiShouldBeStarted) {
Dianne Hackborn03f3cb02010-09-17 23:12:26 -07001103 reportStartWorkSource();
Irfan Sheriff0d255342010-07-28 09:35:20 -07001104 mWifiStateMachine.setWifiEnabled(true);
1105 mWifiStateMachine.setScanOnlyMode(
Irfan Sheriffa2a1b912010-06-07 09:03:04 -07001106 strongestLockMode == WifiManager.WIFI_MODE_SCAN_ONLY);
Irfan Sheriff4494c902011-12-08 10:47:54 -08001107 mWifiStateMachine.setDriverStart(true, mEmergencyCallbackMode);
Irfan Sheriff5876a422010-08-12 20:26:23 -07001108 mWifiStateMachine.setHighPerfModeEnabled(strongestLockMode
1109 == WifiManager.WIFI_MODE_FULL_HIGH_PERF);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001110 } else {
Irfan Sheriff4494c902011-12-08 10:47:54 -08001111 mWifiStateMachine.setDriverStart(false, mEmergencyCallbackMode);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001112 }
Irfan Sheriffa2a1b912010-06-07 09:03:04 -07001113 } else {
Irfan Sheriff0d255342010-07-28 09:35:20 -07001114 mWifiStateMachine.setWifiEnabled(false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001115 }
1116 }
1117
1118 private void registerForBroadcasts() {
1119 IntentFilter intentFilter = new IntentFilter();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001120 intentFilter.addAction(Intent.ACTION_SCREEN_ON);
1121 intentFilter.addAction(Intent.ACTION_SCREEN_OFF);
1122 intentFilter.addAction(Intent.ACTION_BATTERY_CHANGED);
1123 intentFilter.addAction(ACTION_DEVICE_IDLE);
Irfan Sheriff65eaec82011-01-05 22:00:16 -08001124 intentFilter.addAction(BluetoothAdapter.ACTION_CONNECTION_STATE_CHANGED);
Irfan Sheriff616f3172011-09-11 19:59:01 -07001125 intentFilter.addAction(TelephonyIntents.ACTION_EMERGENCY_CALLBACK_MODE_CHANGED);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001126 mContext.registerReceiver(mReceiver, intentFilter);
1127 }
Jaikumar Ganesh084c6652009-12-07 10:58:18 -08001128
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001129 private boolean isAirplaneSensitive() {
1130 String airplaneModeRadios = Settings.System.getString(mContext.getContentResolver(),
1131 Settings.System.AIRPLANE_MODE_RADIOS);
1132 return airplaneModeRadios == null
1133 || airplaneModeRadios.contains(Settings.System.RADIO_WIFI);
1134 }
1135
Mike Lockwoodbd5ddf02009-07-29 21:37:14 -07001136 private boolean isAirplaneToggleable() {
1137 String toggleableRadios = Settings.System.getString(mContext.getContentResolver(),
1138 Settings.System.AIRPLANE_MODE_TOGGLEABLE_RADIOS);
1139 return toggleableRadios != null
1140 && toggleableRadios.contains(Settings.System.RADIO_WIFI);
1141 }
1142
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001143 /**
1144 * Returns true if Wi-Fi is sensitive to airplane mode, and airplane mode is
1145 * currently on.
1146 * @return {@code true} if airplane mode is on.
1147 */
1148 private boolean isAirplaneModeOn() {
1149 return isAirplaneSensitive() && Settings.System.getInt(mContext.getContentResolver(),
1150 Settings.System.AIRPLANE_MODE_ON, 0) == 1;
1151 }
1152
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001153 @Override
1154 protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
1155 if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.DUMP)
1156 != PackageManager.PERMISSION_GRANTED) {
1157 pw.println("Permission Denial: can't dump WifiService from from pid="
1158 + Binder.getCallingPid()
1159 + ", uid=" + Binder.getCallingUid());
1160 return;
1161 }
Irfan Sheriffd8134ff2010-08-22 17:06:34 -07001162 pw.println("Wi-Fi is " + mWifiStateMachine.syncGetWifiStateByName());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001163 pw.println("Stay-awake conditions: " +
1164 Settings.System.getInt(mContext.getContentResolver(),
1165 Settings.System.STAY_ON_WHILE_PLUGGED_IN, 0));
1166 pw.println();
1167
1168 pw.println("Internal state:");
Irfan Sheriff0d255342010-07-28 09:35:20 -07001169 pw.println(mWifiStateMachine);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001170 pw.println();
1171 pw.println("Latest scan results:");
Irfan Sheriffd8134ff2010-08-22 17:06:34 -07001172 List<ScanResult> scanResults = mWifiStateMachine.syncGetScanResultsList();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001173 if (scanResults != null && scanResults.size() != 0) {
1174 pw.println(" BSSID Frequency RSSI Flags SSID");
1175 for (ScanResult r : scanResults) {
1176 pw.printf(" %17s %9d %5d %-16s %s%n",
1177 r.BSSID,
1178 r.frequency,
1179 r.level,
1180 r.capabilities,
1181 r.SSID == null ? "" : r.SSID);
1182 }
1183 }
1184 pw.println();
Eric Shienbrood5711fad2009-03-27 20:25:31 -07001185 pw.println("Locks acquired: " + mFullLocksAcquired + " full, " +
Irfan Sheriff5876a422010-08-12 20:26:23 -07001186 mFullHighPerfLocksAcquired + " full high perf, " +
Eric Shienbrood5711fad2009-03-27 20:25:31 -07001187 mScanLocksAcquired + " scan");
1188 pw.println("Locks released: " + mFullLocksReleased + " full, " +
Irfan Sheriff5876a422010-08-12 20:26:23 -07001189 mFullHighPerfLocksReleased + " full high perf, " +
Eric Shienbrood5711fad2009-03-27 20:25:31 -07001190 mScanLocksReleased + " scan");
1191 pw.println();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001192 pw.println("Locks held:");
1193 mLocks.dump(pw);
Isaac Levybc7dfb52011-06-06 15:34:01 -07001194
Irfan Sheriff8cef0672011-12-13 17:03:59 -08001195 pw.println("Scan count since last plugged in");
1196 synchronized (mScanCount) {
1197 for(int sc : mScanCount.keySet()) {
1198 pw.println("UID: " + sc + " Scan count: " + mScanCount.get(sc));
1199 }
1200 }
1201
Isaac Levybc7dfb52011-06-06 15:34:01 -07001202 pw.println();
Isaac Levy654f5092011-07-13 17:41:45 -07001203 pw.println("WifiWatchdogStateMachine dump");
1204 mWifiWatchdogStateMachine.dump(pw);
Irfan Sheriff60792372012-04-16 16:47:10 -07001205 pw.println("WifiStateMachine dump");
1206 mWifiStateMachine.dump(fd, pw, args);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001207 }
1208
Robert Greenwalt58ff0212009-05-19 15:53:54 -07001209 private class WifiLock extends DeathRecipient {
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -07001210 WifiLock(int lockMode, String tag, IBinder binder, WorkSource ws) {
1211 super(lockMode, tag, binder, ws);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001212 }
1213
1214 public void binderDied() {
1215 synchronized (mLocks) {
1216 releaseWifiLockLocked(mBinder);
1217 }
1218 }
1219
1220 public String toString() {
Robert Greenwalt5347bd42009-05-13 15:10:16 -07001221 return "WifiLock{" + mTag + " type=" + mMode + " binder=" + mBinder + "}";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001222 }
1223 }
1224
1225 private class LockList {
1226 private List<WifiLock> mList;
1227
1228 private LockList() {
1229 mList = new ArrayList<WifiLock>();
1230 }
1231
1232 private synchronized boolean hasLocks() {
1233 return !mList.isEmpty();
1234 }
1235
1236 private synchronized int getStrongestLockMode() {
1237 if (mList.isEmpty()) {
1238 return WifiManager.WIFI_MODE_FULL;
1239 }
Irfan Sheriff5876a422010-08-12 20:26:23 -07001240
1241 if (mFullHighPerfLocksAcquired > mFullHighPerfLocksReleased) {
1242 return WifiManager.WIFI_MODE_FULL_HIGH_PERF;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001243 }
Irfan Sheriff5876a422010-08-12 20:26:23 -07001244
1245 if (mFullLocksAcquired > mFullLocksReleased) {
1246 return WifiManager.WIFI_MODE_FULL;
1247 }
1248
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001249 return WifiManager.WIFI_MODE_SCAN_ONLY;
1250 }
1251
1252 private void addLock(WifiLock lock) {
1253 if (findLockByBinder(lock.mBinder) < 0) {
1254 mList.add(lock);
1255 }
1256 }
1257
1258 private WifiLock removeLock(IBinder binder) {
1259 int index = findLockByBinder(binder);
1260 if (index >= 0) {
Suchi Amalapurapufff2fda2009-06-30 21:36:16 -07001261 WifiLock ret = mList.remove(index);
1262 ret.unlinkDeathRecipient();
1263 return ret;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001264 } else {
1265 return null;
1266 }
1267 }
1268
1269 private int findLockByBinder(IBinder binder) {
1270 int size = mList.size();
1271 for (int i = size - 1; i >= 0; i--)
1272 if (mList.get(i).mBinder == binder)
1273 return i;
1274 return -1;
1275 }
1276
1277 private void dump(PrintWriter pw) {
1278 for (WifiLock l : mList) {
1279 pw.print(" ");
1280 pw.println(l);
1281 }
1282 }
1283 }
1284
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -07001285 void enforceWakeSourcePermission(int uid, int pid) {
Dianne Hackborne746f032010-09-13 16:02:57 -07001286 if (uid == android.os.Process.myUid()) {
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -07001287 return;
1288 }
1289 mContext.enforcePermission(android.Manifest.permission.UPDATE_DEVICE_STATS,
1290 pid, uid, null);
1291 }
1292
1293 public boolean acquireWifiLock(IBinder binder, int lockMode, String tag, WorkSource ws) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001294 mContext.enforceCallingOrSelfPermission(android.Manifest.permission.WAKE_LOCK, null);
Irfan Sheriff5876a422010-08-12 20:26:23 -07001295 if (lockMode != WifiManager.WIFI_MODE_FULL &&
1296 lockMode != WifiManager.WIFI_MODE_SCAN_ONLY &&
1297 lockMode != WifiManager.WIFI_MODE_FULL_HIGH_PERF) {
1298 Slog.e(TAG, "Illegal argument, lockMode= " + lockMode);
1299 if (DBG) throw new IllegalArgumentException("lockMode=" + lockMode);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001300 return false;
1301 }
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -07001302 if (ws != null && ws.size() == 0) {
1303 ws = null;
1304 }
Dianne Hackbornecfd7f72010-10-08 14:23:40 -07001305 if (ws != null) {
1306 enforceWakeSourcePermission(Binder.getCallingUid(), Binder.getCallingPid());
1307 }
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -07001308 if (ws == null) {
1309 ws = new WorkSource(Binder.getCallingUid());
1310 }
1311 WifiLock wifiLock = new WifiLock(lockMode, tag, binder, ws);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001312 synchronized (mLocks) {
1313 return acquireWifiLockLocked(wifiLock);
1314 }
1315 }
1316
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -07001317 private void noteAcquireWifiLock(WifiLock wifiLock) throws RemoteException {
1318 switch(wifiLock.mMode) {
1319 case WifiManager.WIFI_MODE_FULL:
Irfan Sheriff5876a422010-08-12 20:26:23 -07001320 case WifiManager.WIFI_MODE_FULL_HIGH_PERF:
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -07001321 mBatteryStats.noteFullWifiLockAcquiredFromSource(wifiLock.mWorkSource);
1322 break;
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -07001323 case WifiManager.WIFI_MODE_SCAN_ONLY:
1324 mBatteryStats.noteScanWifiLockAcquiredFromSource(wifiLock.mWorkSource);
1325 break;
1326 }
1327 }
1328
1329 private void noteReleaseWifiLock(WifiLock wifiLock) throws RemoteException {
1330 switch(wifiLock.mMode) {
1331 case WifiManager.WIFI_MODE_FULL:
Irfan Sheriff5876a422010-08-12 20:26:23 -07001332 case WifiManager.WIFI_MODE_FULL_HIGH_PERF:
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -07001333 mBatteryStats.noteFullWifiLockReleasedFromSource(wifiLock.mWorkSource);
1334 break;
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -07001335 case WifiManager.WIFI_MODE_SCAN_ONLY:
1336 mBatteryStats.noteScanWifiLockReleasedFromSource(wifiLock.mWorkSource);
1337 break;
1338 }
1339 }
1340
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001341 private boolean acquireWifiLockLocked(WifiLock wifiLock) {
Irfan Sheriffc89dd542010-09-28 08:40:54 -07001342 if (DBG) Slog.d(TAG, "acquireWifiLockLocked: " + wifiLock);
Robert Greenwaltf1acb2d2009-10-13 08:20:55 -07001343
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001344 mLocks.addLock(wifiLock);
Robert Greenwaltf1acb2d2009-10-13 08:20:55 -07001345
The Android Open Source Project10592532009-03-18 17:39:46 -07001346 long ident = Binder.clearCallingIdentity();
1347 try {
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -07001348 noteAcquireWifiLock(wifiLock);
Robert Greenwalt5347bd42009-05-13 15:10:16 -07001349 switch(wifiLock.mMode) {
Eric Shienbrood5711fad2009-03-27 20:25:31 -07001350 case WifiManager.WIFI_MODE_FULL:
1351 ++mFullLocksAcquired;
Eric Shienbrood5711fad2009-03-27 20:25:31 -07001352 break;
Irfan Sheriff5876a422010-08-12 20:26:23 -07001353 case WifiManager.WIFI_MODE_FULL_HIGH_PERF:
1354 ++mFullHighPerfLocksAcquired;
1355 break;
1356
Eric Shienbrood5711fad2009-03-27 20:25:31 -07001357 case WifiManager.WIFI_MODE_SCAN_ONLY:
1358 ++mScanLocksAcquired;
Eric Shienbrood5711fad2009-03-27 20:25:31 -07001359 break;
The Android Open Source Project10592532009-03-18 17:39:46 -07001360 }
Dianne Hackbornecfd7f72010-10-08 14:23:40 -07001361
1362 // Be aggressive about adding new locks into the accounted state...
1363 // we want to over-report rather than under-report.
1364 reportStartWorkSource();
1365
1366 updateWifiState();
1367 return true;
The Android Open Source Project10592532009-03-18 17:39:46 -07001368 } catch (RemoteException e) {
Dianne Hackbornecfd7f72010-10-08 14:23:40 -07001369 return false;
The Android Open Source Project10592532009-03-18 17:39:46 -07001370 } finally {
1371 Binder.restoreCallingIdentity(ident);
1372 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001373 }
1374
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -07001375 public void updateWifiLockWorkSource(IBinder lock, WorkSource ws) {
1376 int uid = Binder.getCallingUid();
1377 int pid = Binder.getCallingPid();
1378 if (ws != null && ws.size() == 0) {
1379 ws = null;
1380 }
1381 if (ws != null) {
1382 enforceWakeSourcePermission(uid, pid);
1383 }
1384 long ident = Binder.clearCallingIdentity();
1385 try {
1386 synchronized (mLocks) {
1387 int index = mLocks.findLockByBinder(lock);
1388 if (index < 0) {
1389 throw new IllegalArgumentException("Wifi lock not active");
1390 }
1391 WifiLock wl = mLocks.mList.get(index);
1392 noteReleaseWifiLock(wl);
1393 wl.mWorkSource = ws != null ? new WorkSource(ws) : new WorkSource(uid);
1394 noteAcquireWifiLock(wl);
1395 }
1396 } catch (RemoteException e) {
1397 } finally {
1398 Binder.restoreCallingIdentity(ident);
1399 }
1400 }
1401
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001402 public boolean releaseWifiLock(IBinder lock) {
1403 mContext.enforceCallingOrSelfPermission(android.Manifest.permission.WAKE_LOCK, null);
1404 synchronized (mLocks) {
1405 return releaseWifiLockLocked(lock);
1406 }
1407 }
1408
1409 private boolean releaseWifiLockLocked(IBinder lock) {
Eric Shienbroodd4c5f892009-03-24 18:13:20 -07001410 boolean hadLock;
Robert Greenwaltf1acb2d2009-10-13 08:20:55 -07001411
The Android Open Source Project10592532009-03-18 17:39:46 -07001412 WifiLock wifiLock = mLocks.removeLock(lock);
Robert Greenwaltf1acb2d2009-10-13 08:20:55 -07001413
Irfan Sheriffc89dd542010-09-28 08:40:54 -07001414 if (DBG) Slog.d(TAG, "releaseWifiLockLocked: " + wifiLock);
Robert Greenwaltf1acb2d2009-10-13 08:20:55 -07001415
Eric Shienbroodd4c5f892009-03-24 18:13:20 -07001416 hadLock = (wifiLock != null);
1417
Dianne Hackbornecfd7f72010-10-08 14:23:40 -07001418 long ident = Binder.clearCallingIdentity();
1419 try {
1420 if (hadLock) {
Wink Savillece0ea1f2b2011-10-13 16:55:20 -07001421 noteReleaseWifiLock(wifiLock);
Robert Greenwalt5347bd42009-05-13 15:10:16 -07001422 switch(wifiLock.mMode) {
Eric Shienbrood5711fad2009-03-27 20:25:31 -07001423 case WifiManager.WIFI_MODE_FULL:
1424 ++mFullLocksReleased;
Eric Shienbrood5711fad2009-03-27 20:25:31 -07001425 break;
Irfan Sheriff5876a422010-08-12 20:26:23 -07001426 case WifiManager.WIFI_MODE_FULL_HIGH_PERF:
1427 ++mFullHighPerfLocksReleased;
1428 break;
Eric Shienbrood5711fad2009-03-27 20:25:31 -07001429 case WifiManager.WIFI_MODE_SCAN_ONLY:
1430 ++mScanLocksReleased;
Eric Shienbrood5711fad2009-03-27 20:25:31 -07001431 break;
Eric Shienbroodd4c5f892009-03-24 18:13:20 -07001432 }
The Android Open Source Project10592532009-03-18 17:39:46 -07001433 }
Dianne Hackbornecfd7f72010-10-08 14:23:40 -07001434
1435 // TODO - should this only happen if you hadLock?
1436 updateWifiState();
1437
1438 } catch (RemoteException e) {
1439 } finally {
1440 Binder.restoreCallingIdentity(ident);
The Android Open Source Project10592532009-03-18 17:39:46 -07001441 }
Dianne Hackbornecfd7f72010-10-08 14:23:40 -07001442
Eric Shienbroodd4c5f892009-03-24 18:13:20 -07001443 return hadLock;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001444 }
Robert Greenwalt5347bd42009-05-13 15:10:16 -07001445
Robert Greenwalt58ff0212009-05-19 15:53:54 -07001446 private abstract class DeathRecipient
Robert Greenwalt5347bd42009-05-13 15:10:16 -07001447 implements IBinder.DeathRecipient {
1448 String mTag;
1449 int mMode;
1450 IBinder mBinder;
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -07001451 WorkSource mWorkSource;
Robert Greenwalt5347bd42009-05-13 15:10:16 -07001452
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -07001453 DeathRecipient(int mode, String tag, IBinder binder, WorkSource ws) {
Robert Greenwalt5347bd42009-05-13 15:10:16 -07001454 super();
1455 mTag = tag;
1456 mMode = mode;
1457 mBinder = binder;
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -07001458 mWorkSource = ws;
Robert Greenwalt5347bd42009-05-13 15:10:16 -07001459 try {
1460 mBinder.linkToDeath(this, 0);
1461 } catch (RemoteException e) {
1462 binderDied();
1463 }
1464 }
Suchi Amalapurapufff2fda2009-06-30 21:36:16 -07001465
1466 void unlinkDeathRecipient() {
1467 mBinder.unlinkToDeath(this, 0);
1468 }
Robert Greenwalt5347bd42009-05-13 15:10:16 -07001469 }
1470
Robert Greenwalt58ff0212009-05-19 15:53:54 -07001471 private class Multicaster extends DeathRecipient {
1472 Multicaster(String tag, IBinder binder) {
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -07001473 super(Binder.getCallingUid(), tag, binder, null);
Robert Greenwalt5347bd42009-05-13 15:10:16 -07001474 }
1475
1476 public void binderDied() {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001477 Slog.e(TAG, "Multicaster binderDied");
Robert Greenwalt5347bd42009-05-13 15:10:16 -07001478 synchronized (mMulticasters) {
1479 int i = mMulticasters.indexOf(this);
1480 if (i != -1) {
1481 removeMulticasterLocked(i, mMode);
1482 }
1483 }
1484 }
1485
1486 public String toString() {
Robert Greenwalt58ff0212009-05-19 15:53:54 -07001487 return "Multicaster{" + mTag + " binder=" + mBinder + "}";
Robert Greenwalt5347bd42009-05-13 15:10:16 -07001488 }
1489
1490 public int getUid() {
1491 return mMode;
1492 }
1493 }
1494
Robert Greenwalte2d155a2009-10-21 14:58:34 -07001495 public void initializeMulticastFiltering() {
1496 enforceMulticastChangePermission();
Irfan Sheriffa8fbe1f2010-03-09 09:13:58 -08001497
Robert Greenwalte2d155a2009-10-21 14:58:34 -07001498 synchronized (mMulticasters) {
1499 // if anybody had requested filters be off, leave off
1500 if (mMulticasters.size() != 0) {
1501 return;
1502 } else {
Irfan Sheriffb0c1b80f2011-07-19 15:44:25 -07001503 mWifiStateMachine.startFilteringMulticastV4Packets();
Robert Greenwalte2d155a2009-10-21 14:58:34 -07001504 }
1505 }
1506 }
1507
Robert Greenwaltfc1b15c2009-05-22 15:09:51 -07001508 public void acquireMulticastLock(IBinder binder, String tag) {
1509 enforceMulticastChangePermission();
Robert Greenwalt5347bd42009-05-13 15:10:16 -07001510
1511 synchronized (mMulticasters) {
1512 mMulticastEnabled++;
Robert Greenwalt58ff0212009-05-19 15:53:54 -07001513 mMulticasters.add(new Multicaster(tag, binder));
Irfan Sheriffb0c1b80f2011-07-19 15:44:25 -07001514 // Note that we could call stopFilteringMulticastV4Packets only when
Robert Greenwalt5347bd42009-05-13 15:10:16 -07001515 // our new size == 1 (first call), but this function won't
1516 // be called often and by making the stopPacket call each
1517 // time we're less fragile and self-healing.
Irfan Sheriffb0c1b80f2011-07-19 15:44:25 -07001518 mWifiStateMachine.stopFilteringMulticastV4Packets();
Robert Greenwalt5347bd42009-05-13 15:10:16 -07001519 }
1520
1521 int uid = Binder.getCallingUid();
1522 Long ident = Binder.clearCallingIdentity();
1523 try {
1524 mBatteryStats.noteWifiMulticastEnabled(uid);
1525 } catch (RemoteException e) {
1526 } finally {
1527 Binder.restoreCallingIdentity(ident);
1528 }
1529 }
1530
Robert Greenwaltfc1b15c2009-05-22 15:09:51 -07001531 public void releaseMulticastLock() {
1532 enforceMulticastChangePermission();
Robert Greenwalt5347bd42009-05-13 15:10:16 -07001533
1534 int uid = Binder.getCallingUid();
1535 synchronized (mMulticasters) {
1536 mMulticastDisabled++;
1537 int size = mMulticasters.size();
1538 for (int i = size - 1; i >= 0; i--) {
Robert Greenwalt58ff0212009-05-19 15:53:54 -07001539 Multicaster m = mMulticasters.get(i);
Robert Greenwalt5347bd42009-05-13 15:10:16 -07001540 if ((m != null) && (m.getUid() == uid)) {
1541 removeMulticasterLocked(i, uid);
1542 }
1543 }
1544 }
1545 }
1546
1547 private void removeMulticasterLocked(int i, int uid)
1548 {
Suchi Amalapurapufff2fda2009-06-30 21:36:16 -07001549 Multicaster removed = mMulticasters.remove(i);
Irfan Sheriffa8fbe1f2010-03-09 09:13:58 -08001550
Suchi Amalapurapufff2fda2009-06-30 21:36:16 -07001551 if (removed != null) {
1552 removed.unlinkDeathRecipient();
1553 }
Robert Greenwalt5347bd42009-05-13 15:10:16 -07001554 if (mMulticasters.size() == 0) {
Irfan Sheriffb0c1b80f2011-07-19 15:44:25 -07001555 mWifiStateMachine.startFilteringMulticastV4Packets();
Robert Greenwalt5347bd42009-05-13 15:10:16 -07001556 }
1557
1558 Long ident = Binder.clearCallingIdentity();
1559 try {
1560 mBatteryStats.noteWifiMulticastDisabled(uid);
1561 } catch (RemoteException e) {
1562 } finally {
1563 Binder.restoreCallingIdentity(ident);
1564 }
1565 }
1566
Robert Greenwalt58ff0212009-05-19 15:53:54 -07001567 public boolean isMulticastEnabled() {
Robert Greenwalt5347bd42009-05-13 15:10:16 -07001568 enforceAccessPermission();
1569
1570 synchronized (mMulticasters) {
1571 return (mMulticasters.size() > 0);
1572 }
1573 }
Irfan Sheriff0d255342010-07-28 09:35:20 -07001574
Irfan Sheriff227bec42011-02-15 19:30:27 -08001575 /**
1576 * Evaluate if traffic stats polling is needed based on
1577 * connection and screen on status
1578 */
1579 private void evaluateTrafficStatsPolling() {
1580 Message msg;
1581 if (mNetworkInfo.getDetailedState() == DetailedState.CONNECTED && !mScreenOff) {
Irfan Sheriffebe606f2011-02-24 11:39:15 -08001582 msg = Message.obtain(mAsyncServiceHandler,
Irfan Sheriffd3975a92012-02-24 10:54:13 -08001583 WifiManager.ENABLE_TRAFFIC_STATS_POLL, 1, 0);
Irfan Sheriff227bec42011-02-15 19:30:27 -08001584 } else {
Irfan Sheriffebe606f2011-02-24 11:39:15 -08001585 msg = Message.obtain(mAsyncServiceHandler,
Irfan Sheriffd3975a92012-02-24 10:54:13 -08001586 WifiManager.ENABLE_TRAFFIC_STATS_POLL, 0, 0);
Irfan Sheriff227bec42011-02-15 19:30:27 -08001587 }
1588 msg.sendToTarget();
1589 }
1590
1591 private void notifyOnDataActivity() {
1592 long sent, received;
1593 long preTxPkts = mTxPkts, preRxPkts = mRxPkts;
1594 int dataActivity = WifiManager.DATA_ACTIVITY_NONE;
1595
1596 mTxPkts = TrafficStats.getTxPackets(mInterfaceName);
1597 mRxPkts = TrafficStats.getRxPackets(mInterfaceName);
1598
1599 if (preTxPkts > 0 || preRxPkts > 0) {
1600 sent = mTxPkts - preTxPkts;
1601 received = mRxPkts - preRxPkts;
1602 if (sent > 0) {
1603 dataActivity |= WifiManager.DATA_ACTIVITY_OUT;
1604 }
1605 if (received > 0) {
1606 dataActivity |= WifiManager.DATA_ACTIVITY_IN;
1607 }
1608
1609 if (dataActivity != mDataActivity && !mScreenOff) {
1610 mDataActivity = dataActivity;
1611 for (AsyncChannel client : mClients) {
1612 client.sendMessage(WifiManager.DATA_ACTIVITY_NOTIFICATION, mDataActivity);
1613 }
1614 }
1615 }
1616 }
1617
1618
Irfan Sheriff0d255342010-07-28 09:35:20 -07001619 private void checkAndSetNotification() {
1620 // If we shouldn't place a notification on available networks, then
1621 // don't bother doing any of the following
1622 if (!mNotificationEnabled) return;
1623
1624 State state = mNetworkInfo.getState();
1625 if ((state == NetworkInfo.State.DISCONNECTED)
1626 || (state == NetworkInfo.State.UNKNOWN)) {
1627 // Look for an open network
Irfan Sheriffd8134ff2010-08-22 17:06:34 -07001628 List<ScanResult> scanResults = mWifiStateMachine.syncGetScanResultsList();
Irfan Sheriff0d255342010-07-28 09:35:20 -07001629 if (scanResults != null) {
1630 int numOpenNetworks = 0;
1631 for (int i = scanResults.size() - 1; i >= 0; i--) {
1632 ScanResult scanResult = scanResults.get(i);
1633
Irfan Sherifffdd5f952011-08-04 16:55:54 -07001634 //A capability of [ESS] represents an open access point
1635 //that is available for an STA to connect
1636 if (scanResult.capabilities != null &&
1637 scanResult.capabilities.equals("[ESS]")) {
Irfan Sheriff0d255342010-07-28 09:35:20 -07001638 numOpenNetworks++;
1639 }
1640 }
1641
1642 if (numOpenNetworks > 0) {
1643 if (++mNumScansSinceNetworkStateChange >= NUM_SCANS_BEFORE_ACTUALLY_SCANNING) {
1644 /*
1645 * We've scanned continuously at least
1646 * NUM_SCANS_BEFORE_NOTIFICATION times. The user
1647 * probably does not have a remembered network in range,
1648 * since otherwise supplicant would have tried to
1649 * associate and thus resetting this counter.
1650 */
1651 setNotificationVisible(true, numOpenNetworks, false, 0);
1652 }
1653 return;
1654 }
1655 }
1656 }
1657
1658 // No open networks in range, remove the notification
1659 setNotificationVisible(false, 0, false, 0);
1660 }
1661
1662 /**
1663 * Clears variables related to tracking whether a notification has been
1664 * shown recently and clears the current notification.
1665 */
1666 private void resetNotification() {
1667 mNotificationRepeatTime = 0;
1668 mNumScansSinceNetworkStateChange = 0;
1669 setNotificationVisible(false, 0, false, 0);
1670 }
1671
1672 /**
1673 * Display or don't display a notification that there are open Wi-Fi networks.
1674 * @param visible {@code true} if notification should be visible, {@code false} otherwise
1675 * @param numNetworks the number networks seen
1676 * @param force {@code true} to force notification to be shown/not-shown,
1677 * even if it is already shown/not-shown.
1678 * @param delay time in milliseconds after which the notification should be made
1679 * visible or invisible.
1680 */
1681 private void setNotificationVisible(boolean visible, int numNetworks, boolean force,
1682 int delay) {
1683
1684 // Since we use auto cancel on the notification, when the
1685 // mNetworksAvailableNotificationShown is true, the notification may
1686 // have actually been canceled. However, when it is false we know
1687 // for sure that it is not being shown (it will not be shown any other
1688 // place than here)
1689
1690 // If it should be hidden and it is already hidden, then noop
1691 if (!visible && !mNotificationShown && !force) {
1692 return;
1693 }
1694
1695 NotificationManager notificationManager = (NotificationManager) mContext
1696 .getSystemService(Context.NOTIFICATION_SERVICE);
1697
1698 Message message;
1699 if (visible) {
1700
1701 // Not enough time has passed to show the notification again
1702 if (System.currentTimeMillis() < mNotificationRepeatTime) {
1703 return;
1704 }
1705
1706 if (mNotification == null) {
Wink Savillec7a98342010-08-13 16:11:42 -07001707 // Cache the Notification object.
Irfan Sheriff0d255342010-07-28 09:35:20 -07001708 mNotification = new Notification();
1709 mNotification.when = 0;
1710 mNotification.icon = ICON_NETWORKS_AVAILABLE;
1711 mNotification.flags = Notification.FLAG_AUTO_CANCEL;
1712 mNotification.contentIntent = PendingIntent.getActivity(mContext, 0,
1713 new Intent(WifiManager.ACTION_PICK_WIFI_NETWORK), 0);
1714 }
1715
1716 CharSequence title = mContext.getResources().getQuantityText(
1717 com.android.internal.R.plurals.wifi_available, numNetworks);
1718 CharSequence details = mContext.getResources().getQuantityText(
1719 com.android.internal.R.plurals.wifi_available_detailed, numNetworks);
1720 mNotification.tickerText = title;
1721 mNotification.setLatestEventInfo(mContext, title, details, mNotification.contentIntent);
1722
1723 mNotificationRepeatTime = System.currentTimeMillis() + NOTIFICATION_REPEAT_DELAY_MS;
1724
1725 notificationManager.notify(ICON_NETWORKS_AVAILABLE, mNotification);
Irfan Sheriff0d255342010-07-28 09:35:20 -07001726 } else {
Irfan Sheriff0d255342010-07-28 09:35:20 -07001727 notificationManager.cancel(ICON_NETWORKS_AVAILABLE);
Irfan Sheriff0d255342010-07-28 09:35:20 -07001728 }
1729
Irfan Sheriff0d255342010-07-28 09:35:20 -07001730 mNotificationShown = visible;
1731 }
1732
1733 private class NotificationEnabledSettingObserver extends ContentObserver {
1734
1735 public NotificationEnabledSettingObserver(Handler handler) {
1736 super(handler);
1737 }
1738
1739 public void register() {
1740 ContentResolver cr = mContext.getContentResolver();
1741 cr.registerContentObserver(Settings.Secure.getUriFor(
1742 Settings.Secure.WIFI_NETWORKS_AVAILABLE_NOTIFICATION_ON), true, this);
1743 mNotificationEnabled = getValue();
1744 }
1745
1746 @Override
1747 public void onChange(boolean selfChange) {
1748 super.onChange(selfChange);
1749
1750 mNotificationEnabled = getValue();
1751 resetNotification();
1752 }
1753
1754 private boolean getValue() {
1755 return Settings.Secure.getInt(mContext.getContentResolver(),
1756 Settings.Secure.WIFI_NETWORKS_AVAILABLE_NOTIFICATION_ON, 1) == 1;
1757 }
1758 }
1759
1760
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001761}