blob: 6b4c8959e2527ac5526eeecee925f45266d1e2e6 [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
Irfan Sherifffcc08452011-02-17 16:44:54 -0800104 /* Chipset supports background scan */
105 private final boolean mBackgroundScanSupported;
106
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800107 private final LockList mLocks = new LockList();
Eric Shienbrood5711fad2009-03-27 20:25:31 -0700108 // some wifi lock statistics
Irfan Sheriff5876a422010-08-12 20:26:23 -0700109 private int mFullHighPerfLocksAcquired;
110 private int mFullHighPerfLocksReleased;
Eric Shienbrood5711fad2009-03-27 20:25:31 -0700111 private int mFullLocksAcquired;
112 private int mFullLocksReleased;
113 private int mScanLocksAcquired;
114 private int mScanLocksReleased;
The Android Open Source Project10592532009-03-18 17:39:46 -0700115
Irfan Sheriff8cef0672011-12-13 17:03:59 -0800116 /* A mapping from UID to scan count */
117 private HashMap<Integer, Integer> mScanCount =
118 new HashMap<Integer, Integer>();
119
Robert Greenwalt58ff0212009-05-19 15:53:54 -0700120 private final List<Multicaster> mMulticasters =
121 new ArrayList<Multicaster>();
Robert Greenwalt5347bd42009-05-13 15:10:16 -0700122 private int mMulticastEnabled;
123 private int mMulticastDisabled;
124
The Android Open Source Project10592532009-03-18 17:39:46 -0700125 private final IBatteryStats mBatteryStats;
Jaikumar Ganesh084c6652009-12-07 10:58:18 -0800126
Irfan Sheriff227bec42011-02-15 19:30:27 -0800127 private boolean mEnableTrafficStatsPoll = false;
128 private int mTrafficStatsPollToken = 0;
129 private long mTxPkts;
130 private long mRxPkts;
131 /* Tracks last reported data activity */
132 private int mDataActivity;
133 private String mInterfaceName;
134
135 /**
136 * Interval in milliseconds between polling for traffic
137 * statistics
138 */
139 private static final int POLL_TRAFFIC_STATS_INTERVAL_MSECS = 1000;
140
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800141 /**
Doug Zongker43866e02010-01-07 12:09:54 -0800142 * See {@link Settings.Secure#WIFI_IDLE_MS}. This is the default value if a
143 * Settings.Secure value is not present. This timeout value is chosen as
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800144 * the approximate point at which the battery drain caused by Wi-Fi
145 * being enabled but not active exceeds the battery drain caused by
146 * re-establishing a connection to the mobile data network.
147 */
Irfan Sheriff4f5f7c92010-10-14 17:01:27 -0700148 private static final long DEFAULT_IDLE_MS = 15 * 60 * 1000; /* 15 minutes */
149
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800150 private static final String ACTION_DEVICE_IDLE =
151 "com.android.server.WifiManager.action.DEVICE_IDLE";
152
Irfan Sheriff658772f2011-03-08 14:52:31 -0800153 private static final int WIFI_DISABLED = 0;
154 private static final int WIFI_ENABLED = 1;
155 /* Wifi enabled while in airplane mode */
156 private static final int WIFI_ENABLED_AIRPLANE_OVERRIDE = 2;
Irfan Sheriff31b92e22011-10-03 12:13:20 -0700157 /* Wifi disabled due to airplane mode on */
158 private static final int WIFI_DISABLED_AIRPLANE_ON = 3;
Irfan Sheriff658772f2011-03-08 14:52:31 -0800159
Irfan Sheriff5401f0b2011-12-07 16:27:49 -0800160 /* Persisted state that tracks the wifi & airplane interaction from settings */
161 private AtomicInteger mPersistWifiState = new AtomicInteger(WIFI_DISABLED);
162 /* Tracks current airplane mode state */
Irfan Sheriff658772f2011-03-08 14:52:31 -0800163 private AtomicBoolean mAirplaneModeOn = new AtomicBoolean(false);
Irfan Sheriff5401f0b2011-12-07 16:27:49 -0800164 /* Tracks whether wifi is enabled from WifiStateMachine's perspective */
165 private boolean mWifiEnabled;
Irfan Sheriff658772f2011-03-08 14:52:31 -0800166
Irfan Sheriffa2a1b912010-06-07 09:03:04 -0700167 private boolean mIsReceiverRegistered = false;
168
Irfan Sheriff0d255342010-07-28 09:35:20 -0700169
170 NetworkInfo mNetworkInfo = new NetworkInfo(ConnectivityManager.TYPE_WIFI, 0, "WIFI", "");
171
172 // Variables relating to the 'available networks' notification
173 /**
174 * The icon to show in the 'available networks' notification. This will also
175 * be the ID of the Notification given to the NotificationManager.
176 */
177 private static final int ICON_NETWORKS_AVAILABLE =
178 com.android.internal.R.drawable.stat_notify_wifi_in_range;
179 /**
180 * When a notification is shown, we wait this amount before possibly showing it again.
181 */
182 private final long NOTIFICATION_REPEAT_DELAY_MS;
183 /**
184 * Whether the user has set the setting to show the 'available networks' notification.
185 */
186 private boolean mNotificationEnabled;
187 /**
188 * Observes the user setting to keep {@link #mNotificationEnabled} in sync.
189 */
190 private NotificationEnabledSettingObserver mNotificationEnabledSettingObserver;
191 /**
192 * The {@link System#currentTimeMillis()} must be at least this value for us
193 * to show the notification again.
194 */
195 private long mNotificationRepeatTime;
196 /**
197 * The Notification object given to the NotificationManager.
198 */
199 private Notification mNotification;
200 /**
201 * Whether the notification is being shown, as set by us. That is, if the
202 * user cancels the notification, we will not receive the callback so this
203 * will still be true. We only guarantee if this is false, then the
204 * notification is not showing.
205 */
206 private boolean mNotificationShown;
207 /**
208 * The number of continuous scans that must occur before consider the
209 * supplicant in a scanning state. This allows supplicant to associate with
210 * remembered networks that are in the scan results.
211 */
212 private static final int NUM_SCANS_BEFORE_ACTUALLY_SCANNING = 3;
213 /**
214 * The number of scans since the last network state change. When this
215 * exceeds {@link #NUM_SCANS_BEFORE_ACTUALLY_SCANNING}, we consider the
216 * supplicant to actually be scanning. When the network state changes to
217 * something other than scanning, we reset this to 0.
218 */
219 private int mNumScansSinceNetworkStateChange;
Jaikumar Ganesh7440fc22010-09-27 17:04:14 -0700220
Dianne Hackborn03f3cb02010-09-17 23:12:26 -0700221 /**
Wink Saville4b7ba092010-10-20 15:37:41 -0700222 * Asynchronous channel to WifiStateMachine
223 */
Irfan Sheriff227bec42011-02-15 19:30:27 -0800224 private AsyncChannel mWifiStateMachineChannel;
Wink Saville4b7ba092010-10-20 15:37:41 -0700225
226 /**
Irfan Sheriff227bec42011-02-15 19:30:27 -0800227 * Clients receiving asynchronous messages
Wink Saville4b7ba092010-10-20 15:37:41 -0700228 */
Irfan Sheriff227bec42011-02-15 19:30:27 -0800229 private List<AsyncChannel> mClients = new ArrayList<AsyncChannel>();
Wink Saville4b7ba092010-10-20 15:37:41 -0700230
Irfan Sheriff227bec42011-02-15 19:30:27 -0800231 /**
232 * Handles client connections
233 */
234 private class AsyncServiceHandler extends Handler {
235
236 AsyncServiceHandler(android.os.Looper looper) {
Wink Saville4b7ba092010-10-20 15:37:41 -0700237 super(looper);
Wink Saville4b7ba092010-10-20 15:37:41 -0700238 }
239
240 @Override
241 public void handleMessage(Message msg) {
242 switch (msg.what) {
243 case AsyncChannel.CMD_CHANNEL_HALF_CONNECTED: {
244 if (msg.arg1 == AsyncChannel.STATUS_SUCCESSFUL) {
Irfan Sheriff227bec42011-02-15 19:30:27 -0800245 Slog.d(TAG, "New client listening to asynchronous messages");
246 mClients.add((AsyncChannel) msg.obj);
Wink Saville4b7ba092010-10-20 15:37:41 -0700247 } else {
Irfan Sheriff227bec42011-02-15 19:30:27 -0800248 Slog.e(TAG, "Client connection failure, error=" + msg.arg1);
249 }
250 break;
251 }
Irfan Sheriffc23971b2011-03-04 17:06:31 -0800252 case AsyncChannel.CMD_CHANNEL_DISCONNECTED: {
253 if (msg.arg1 == AsyncChannel.STATUS_SEND_UNSUCCESSFUL) {
254 Slog.d(TAG, "Send failed, client connection lost");
255 } else {
256 Slog.d(TAG, "Client connection lost with reason: " + msg.arg1);
257 }
258 mClients.remove((AsyncChannel) msg.obj);
259 break;
260 }
Irfan Sheriff227bec42011-02-15 19:30:27 -0800261 case AsyncChannel.CMD_CHANNEL_FULL_CONNECTION: {
262 AsyncChannel ac = new AsyncChannel();
263 ac.connect(mContext, this, msg.replyTo);
264 break;
265 }
Irfan Sheriffd3975a92012-02-24 10:54:13 -0800266 case WifiManager.ENABLE_TRAFFIC_STATS_POLL: {
Irfan Sheriff227bec42011-02-15 19:30:27 -0800267 mEnableTrafficStatsPoll = (msg.arg1 == 1);
268 mTrafficStatsPollToken++;
269 if (mEnableTrafficStatsPoll) {
270 notifyOnDataActivity();
Irfan Sheriffd3975a92012-02-24 10:54:13 -0800271 sendMessageDelayed(Message.obtain(this, WifiManager.TRAFFIC_STATS_POLL,
Irfan Sheriff227bec42011-02-15 19:30:27 -0800272 mTrafficStatsPollToken, 0), POLL_TRAFFIC_STATS_INTERVAL_MSECS);
273 }
274 break;
275 }
Irfan Sheriffd3975a92012-02-24 10:54:13 -0800276 case WifiManager.TRAFFIC_STATS_POLL: {
Irfan Sheriff227bec42011-02-15 19:30:27 -0800277 if (msg.arg1 == mTrafficStatsPollToken) {
278 notifyOnDataActivity();
Irfan Sheriffd3975a92012-02-24 10:54:13 -0800279 sendMessageDelayed(Message.obtain(this, WifiManager.TRAFFIC_STATS_POLL,
Irfan Sheriff227bec42011-02-15 19:30:27 -0800280 mTrafficStatsPollToken, 0), POLL_TRAFFIC_STATS_INTERVAL_MSECS);
Wink Saville4b7ba092010-10-20 15:37:41 -0700281 }
282 break;
283 }
Irfan Sheriffd3975a92012-02-24 10:54:13 -0800284 case WifiManager.CONNECT_NETWORK: {
285 mWifiStateMachine.sendMessage(Message.obtain(msg));
Irfan Sheriffebe606f2011-02-24 11:39:15 -0800286 break;
287 }
Irfan Sheriffd3975a92012-02-24 10:54:13 -0800288 case WifiManager.SAVE_NETWORK: {
289 mWifiStateMachine.sendMessage(Message.obtain(msg));
Irfan Sheriffebe606f2011-02-24 11:39:15 -0800290 break;
291 }
Irfan Sheriffd3975a92012-02-24 10:54:13 -0800292 case WifiManager.FORGET_NETWORK: {
293 mWifiStateMachine.sendMessage(Message.obtain(msg));
Irfan Sheriffebe606f2011-02-24 11:39:15 -0800294 break;
295 }
Irfan Sheriffd3975a92012-02-24 10:54:13 -0800296 case WifiManager.START_WPS: {
297 mWifiStateMachine.sendMessage(Message.obtain(msg));
Irfan Sheriffebe606f2011-02-24 11:39:15 -0800298 break;
299 }
Irfan Sheriffd3975a92012-02-24 10:54:13 -0800300 case WifiManager.DISABLE_NETWORK: {
301 mWifiStateMachine.sendMessage(Message.obtain(msg));
Isaac Levy8dc6a1b2011-07-27 08:00:03 -0700302 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 Sheriff8cef0672011-12-13 17:03:59 -0800530
531 int uid = Binder.getCallingUid();
532 int count = 0;
533 synchronized (mScanCount) {
534 if (mScanCount.containsKey(uid)) {
535 count = mScanCount.get(uid);
536 }
537 mScanCount.put(uid, ++count);
538 }
Irfan Sheriffe4984752010-08-19 11:29:22 -0700539 mWifiStateMachine.startScan(forceActive);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800540 }
541
542 private void enforceAccessPermission() {
543 mContext.enforceCallingOrSelfPermission(android.Manifest.permission.ACCESS_WIFI_STATE,
544 "WifiService");
545 }
546
547 private void enforceChangePermission() {
548 mContext.enforceCallingOrSelfPermission(android.Manifest.permission.CHANGE_WIFI_STATE,
549 "WifiService");
550
551 }
552
Robert Greenwaltfc1b15c2009-05-22 15:09:51 -0700553 private void enforceMulticastChangePermission() {
554 mContext.enforceCallingOrSelfPermission(
555 android.Manifest.permission.CHANGE_WIFI_MULTICAST_STATE,
556 "WifiService");
557 }
558
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800559 /**
Irfan Sheriffa2a1b912010-06-07 09:03:04 -0700560 * see {@link android.net.wifi.WifiManager#setWifiEnabled(boolean)}
561 * @param enable {@code true} to enable, {@code false} to disable.
562 * @return {@code true} if the enable/disable operation was
563 * started or is already in the queue.
564 */
565 public synchronized boolean setWifiEnabled(boolean enable) {
566 enforceChangePermission();
Irfan Sheriffa2a1b912010-06-07 09:03:04 -0700567 if (DBG) {
Irfan Sheriff0d255342010-07-28 09:35:20 -0700568 Slog.e(TAG, "Invoking mWifiStateMachine.setWifiEnabled\n");
Irfan Sheriffa2a1b912010-06-07 09:03:04 -0700569 }
570
Dianne Hackborn03f3cb02010-09-17 23:12:26 -0700571 if (enable) {
572 reportStartWorkSource();
573 }
Irfan Sheriff0d255342010-07-28 09:35:20 -0700574 mWifiStateMachine.setWifiEnabled(enable);
Irfan Sheriff61180692010-08-18 16:07:39 -0700575
576 /*
577 * Caller might not have WRITE_SECURE_SETTINGS,
578 * only CHANGE_WIFI_STATE is enforced
579 */
Irfan Sheriff5401f0b2011-12-07 16:27:49 -0800580
581 /* Avoids overriding of airplane state when wifi is already in the expected state */
582 if (enable != mWifiEnabled) {
583 long ident = Binder.clearCallingIdentity();
584 persistWifiState(enable);
585 Binder.restoreCallingIdentity(ident);
586 }
Irfan Sheriffa2a1b912010-06-07 09:03:04 -0700587
588 if (enable) {
589 if (!mIsReceiverRegistered) {
590 registerForBroadcasts();
591 mIsReceiverRegistered = true;
592 }
Irfan Sheriff5401f0b2011-12-07 16:27:49 -0800593 } else if (mIsReceiverRegistered) {
Irfan Sheriffa2a1b912010-06-07 09:03:04 -0700594 mContext.unregisterReceiver(mReceiver);
595 mIsReceiverRegistered = false;
596 }
597
598 return true;
599 }
600
601 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800602 * see {@link WifiManager#getWifiState()}
603 * @return One of {@link WifiManager#WIFI_STATE_DISABLED},
604 * {@link WifiManager#WIFI_STATE_DISABLING},
605 * {@link WifiManager#WIFI_STATE_ENABLED},
606 * {@link WifiManager#WIFI_STATE_ENABLING},
607 * {@link WifiManager#WIFI_STATE_UNKNOWN}
608 */
609 public int getWifiEnabledState() {
610 enforceAccessPermission();
Irfan Sheriffd8134ff2010-08-22 17:06:34 -0700611 return mWifiStateMachine.syncGetWifiState();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800612 }
613
614 /**
Irfan Sheriffc2f54c22010-03-18 14:02:22 -0700615 * see {@link android.net.wifi.WifiManager#setWifiApEnabled(WifiConfiguration, boolean)}
Irfan Sheriff5321aef2010-02-12 12:35:59 -0800616 * @param wifiConfig SSID, security and channel details as
617 * part of WifiConfiguration
Irfan Sheriffa2a1b912010-06-07 09:03:04 -0700618 * @param enabled true to enable and false to disable
Irfan Sheriff5321aef2010-02-12 12:35:59 -0800619 */
Irfan Sheriffffcea7a2011-05-10 16:26:06 -0700620 public void setWifiApEnabled(WifiConfiguration wifiConfig, boolean enabled) {
Irfan Sheriff5321aef2010-02-12 12:35:59 -0800621 enforceChangePermission();
Irfan Sheriff0d255342010-07-28 09:35:20 -0700622 mWifiStateMachine.setWifiApEnabled(wifiConfig, enabled);
Irfan Sheriff5321aef2010-02-12 12:35:59 -0800623 }
624
Irfan Sheriffa2a1b912010-06-07 09:03:04 -0700625 /**
626 * see {@link WifiManager#getWifiApState()}
627 * @return One of {@link WifiManager#WIFI_AP_STATE_DISABLED},
628 * {@link WifiManager#WIFI_AP_STATE_DISABLING},
629 * {@link WifiManager#WIFI_AP_STATE_ENABLED},
630 * {@link WifiManager#WIFI_AP_STATE_ENABLING},
631 * {@link WifiManager#WIFI_AP_STATE_FAILED}
632 */
633 public int getWifiApEnabledState() {
Irfan Sheriff17b232b2010-06-24 11:32:26 -0700634 enforceAccessPermission();
Irfan Sheriffd8134ff2010-08-22 17:06:34 -0700635 return mWifiStateMachine.syncGetWifiApState();
Irfan Sheriffa2a1b912010-06-07 09:03:04 -0700636 }
637
638 /**
639 * see {@link WifiManager#getWifiApConfiguration()}
640 * @return soft access point configuration
641 */
Irfan Sheriffffcea7a2011-05-10 16:26:06 -0700642 public WifiConfiguration getWifiApConfiguration() {
643 enforceAccessPermission();
Irfan Sheriff9575a1b2011-11-07 10:34:54 -0800644 return mWifiStateMachine.syncGetWifiApConfiguration();
Irfan Sheriff9ab518ad2010-03-12 15:48:17 -0800645 }
646
Irfan Sheriffa2a1b912010-06-07 09:03:04 -0700647 /**
648 * see {@link WifiManager#setWifiApConfiguration(WifiConfiguration)}
649 * @param wifiConfig WifiConfiguration details for soft access point
650 */
Irfan Sheriffffcea7a2011-05-10 16:26:06 -0700651 public void setWifiApConfiguration(WifiConfiguration wifiConfig) {
Irfan Sheriff17b232b2010-06-24 11:32:26 -0700652 enforceChangePermission();
Irfan Sheriff9ab518ad2010-03-12 15:48:17 -0800653 if (wifiConfig == null)
654 return;
Irfan Sheriffffcea7a2011-05-10 16:26:06 -0700655 mWifiStateMachine.setWifiApConfiguration(wifiConfig);
Irfan Sheriff9ab518ad2010-03-12 15:48:17 -0800656 }
657
Irfan Sheriff5321aef2010-02-12 12:35:59 -0800658 /**
Irfan Sheriffa2a1b912010-06-07 09:03:04 -0700659 * see {@link android.net.wifi.WifiManager#disconnect()}
Irfan Sheriff5321aef2010-02-12 12:35:59 -0800660 */
Irfan Sheriffe4984752010-08-19 11:29:22 -0700661 public void disconnect() {
Irfan Sheriffa2a1b912010-06-07 09:03:04 -0700662 enforceChangePermission();
Irfan Sheriffe4984752010-08-19 11:29:22 -0700663 mWifiStateMachine.disconnectCommand();
Irfan Sheriff5321aef2010-02-12 12:35:59 -0800664 }
665
666 /**
Irfan Sheriffa2a1b912010-06-07 09:03:04 -0700667 * see {@link android.net.wifi.WifiManager#reconnect()}
Irfan Sheriff5321aef2010-02-12 12:35:59 -0800668 */
Irfan Sheriffe4984752010-08-19 11:29:22 -0700669 public void reconnect() {
Irfan Sheriffa2a1b912010-06-07 09:03:04 -0700670 enforceChangePermission();
Irfan Sheriffe4984752010-08-19 11:29:22 -0700671 mWifiStateMachine.reconnectCommand();
Irfan Sheriff5321aef2010-02-12 12:35:59 -0800672 }
673
Irfan Sheriffa2a1b912010-06-07 09:03:04 -0700674 /**
675 * see {@link android.net.wifi.WifiManager#reassociate()}
Irfan Sheriffa2a1b912010-06-07 09:03:04 -0700676 */
Irfan Sheriffe4984752010-08-19 11:29:22 -0700677 public void reassociate() {
Irfan Sheriffa2a1b912010-06-07 09:03:04 -0700678 enforceChangePermission();
Irfan Sheriffe4984752010-08-19 11:29:22 -0700679 mWifiStateMachine.reassociateCommand();
Irfan Sheriff5321aef2010-02-12 12:35:59 -0800680 }
681
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800682 /**
683 * see {@link android.net.wifi.WifiManager#getConfiguredNetworks()}
684 * @return the list of configured networks
685 */
686 public List<WifiConfiguration> getConfiguredNetworks() {
687 enforceAccessPermission();
Irfan Sheriffe744cff2011-12-11 09:17:50 -0800688 if (mWifiStateMachineChannel != null) {
689 return mWifiStateMachine.syncGetConfiguredNetworks(mWifiStateMachineChannel);
690 } else {
691 Slog.e(TAG, "mWifiStateMachineChannel is not initialized");
692 return null;
693 }
Chung-yih Wanga8d15942009-10-09 11:01:49 +0800694 }
695
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800696 /**
697 * see {@link android.net.wifi.WifiManager#addOrUpdateNetwork(WifiConfiguration)}
698 * @return the supplicant-assigned identifier for the new or updated
699 * network if the operation succeeds, or {@code -1} if it fails
700 */
Irfan Sheriff7aac5542009-12-22 21:42:17 -0800701 public int addOrUpdateNetwork(WifiConfiguration config) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800702 enforceChangePermission();
Irfan Sheriff227bec42011-02-15 19:30:27 -0800703 if (mWifiStateMachineChannel != null) {
704 return mWifiStateMachine.syncAddOrUpdateNetwork(mWifiStateMachineChannel, config);
Irfan Sheriff1406bcb2010-10-28 14:41:39 -0700705 } else {
Irfan Sheriff227bec42011-02-15 19:30:27 -0800706 Slog.e(TAG, "mWifiStateMachineChannel is not initialized");
Irfan Sheriff1406bcb2010-10-28 14:41:39 -0700707 return -1;
708 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800709 }
710
Irfan Sheriffa2a1b912010-06-07 09:03:04 -0700711 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800712 * See {@link android.net.wifi.WifiManager#removeNetwork(int)}
713 * @param netId the integer that identifies the network configuration
714 * to the supplicant
715 * @return {@code true} if the operation succeeded
716 */
717 public boolean removeNetwork(int netId) {
718 enforceChangePermission();
Irfan Sheriff227bec42011-02-15 19:30:27 -0800719 if (mWifiStateMachineChannel != null) {
720 return mWifiStateMachine.syncRemoveNetwork(mWifiStateMachineChannel, netId);
Wink Saville4b7ba092010-10-20 15:37:41 -0700721 } else {
Irfan Sheriff227bec42011-02-15 19:30:27 -0800722 Slog.e(TAG, "mWifiStateMachineChannel is not initialized");
Wink Saville4b7ba092010-10-20 15:37:41 -0700723 return false;
724 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800725 }
726
727 /**
728 * See {@link android.net.wifi.WifiManager#enableNetwork(int, boolean)}
729 * @param netId the integer that identifies the network configuration
730 * to the supplicant
731 * @param disableOthers if true, disable all other networks.
732 * @return {@code true} if the operation succeeded
733 */
734 public boolean enableNetwork(int netId, boolean disableOthers) {
735 enforceChangePermission();
Irfan Sheriff227bec42011-02-15 19:30:27 -0800736 if (mWifiStateMachineChannel != null) {
737 return mWifiStateMachine.syncEnableNetwork(mWifiStateMachineChannel, netId,
738 disableOthers);
Irfan Sheriff1406bcb2010-10-28 14:41:39 -0700739 } else {
Irfan Sheriff227bec42011-02-15 19:30:27 -0800740 Slog.e(TAG, "mWifiStateMachineChannel is not initialized");
Irfan Sheriff1406bcb2010-10-28 14:41:39 -0700741 return false;
742 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800743 }
744
745 /**
746 * See {@link android.net.wifi.WifiManager#disableNetwork(int)}
747 * @param netId the integer that identifies the network configuration
748 * to the supplicant
749 * @return {@code true} if the operation succeeded
750 */
751 public boolean disableNetwork(int netId) {
752 enforceChangePermission();
Irfan Sheriff227bec42011-02-15 19:30:27 -0800753 if (mWifiStateMachineChannel != null) {
754 return mWifiStateMachine.syncDisableNetwork(mWifiStateMachineChannel, netId);
Irfan Sheriff1406bcb2010-10-28 14:41:39 -0700755 } else {
Irfan Sheriff227bec42011-02-15 19:30:27 -0800756 Slog.e(TAG, "mWifiStateMachineChannel is not initialized");
Irfan Sheriff1406bcb2010-10-28 14:41:39 -0700757 return false;
758 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800759 }
760
761 /**
762 * See {@link android.net.wifi.WifiManager#getConnectionInfo()}
763 * @return the Wi-Fi information, contained in {@link WifiInfo}.
764 */
765 public WifiInfo getConnectionInfo() {
766 enforceAccessPermission();
767 /*
768 * Make sure we have the latest information, by sending
769 * a status request to the supplicant.
770 */
Irfan Sheriffd8134ff2010-08-22 17:06:34 -0700771 return mWifiStateMachine.syncRequestConnectionInfo();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800772 }
773
774 /**
775 * Return the results of the most recent access point scan, in the form of
776 * a list of {@link ScanResult} objects.
777 * @return the list of results
778 */
779 public List<ScanResult> getScanResults() {
780 enforceAccessPermission();
Irfan Sheriffd8134ff2010-08-22 17:06:34 -0700781 return mWifiStateMachine.syncGetScanResultsList();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800782 }
783
784 /**
785 * Tell the supplicant to persist the current list of configured networks.
786 * @return {@code true} if the operation succeeded
Irfan Sheriffa2a1b912010-06-07 09:03:04 -0700787 *
788 * TODO: deprecate this
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800789 */
790 public boolean saveConfiguration() {
Irfan Sheriffa2a1b912010-06-07 09:03:04 -0700791 boolean result = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800792 enforceChangePermission();
Irfan Sheriff227bec42011-02-15 19:30:27 -0800793 if (mWifiStateMachineChannel != null) {
794 return mWifiStateMachine.syncSaveConfig(mWifiStateMachineChannel);
Irfan Sheriff1406bcb2010-10-28 14:41:39 -0700795 } else {
Irfan Sheriff227bec42011-02-15 19:30:27 -0800796 Slog.e(TAG, "mWifiStateMachineChannel is not initialized");
Irfan Sheriff1406bcb2010-10-28 14:41:39 -0700797 return false;
798 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800799 }
800
801 /**
Irfan Sheriffed4f28b2010-10-29 15:32:10 -0700802 * Set the country code
803 * @param countryCode ISO 3166 country code.
Robert Greenwaltb5010cc2009-05-21 15:11:40 -0700804 * @param persist {@code true} if the setting should be remembered.
Irfan Sheriffed4f28b2010-10-29 15:32:10 -0700805 *
806 * The persist behavior exists so that wifi can fall back to the last
807 * persisted country code on a restart, when the locale information is
808 * not available from telephony.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800809 */
Irfan Sheriffed4f28b2010-10-29 15:32:10 -0700810 public void setCountryCode(String countryCode, boolean persist) {
811 Slog.i(TAG, "WifiService trying to set country code to " + countryCode +
812 " with persist set to " + persist);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800813 enforceChangePermission();
Irfan Sheriffed4f28b2010-10-29 15:32:10 -0700814 mWifiStateMachine.setCountryCode(countryCode, persist);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800815 }
816
817 /**
Irfan Sheriff36f74132010-11-04 16:57:37 -0700818 * Set the operational frequency band
819 * @param band One of
820 * {@link WifiManager#WIFI_FREQUENCY_BAND_AUTO},
821 * {@link WifiManager#WIFI_FREQUENCY_BAND_5GHZ},
822 * {@link WifiManager#WIFI_FREQUENCY_BAND_2GHZ},
823 * @param persist {@code true} if the setting should be remembered.
824 *
825 */
826 public void setFrequencyBand(int band, boolean persist) {
827 enforceChangePermission();
828 if (!isDualBandSupported()) return;
829 Slog.i(TAG, "WifiService trying to set frequency band to " + band +
830 " with persist set to " + persist);
831 mWifiStateMachine.setFrequencyBand(band, persist);
832 }
833
834
835 /**
836 * Get the operational frequency band
837 */
838 public int getFrequencyBand() {
839 enforceAccessPermission();
840 return mWifiStateMachine.getFrequencyBand();
841 }
842
843 public boolean isDualBandSupported() {
844 //TODO: Should move towards adding a driver API that checks at runtime
845 return mContext.getResources().getBoolean(
846 com.android.internal.R.bool.config_wifi_dual_band_support);
847 }
848
849 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800850 * Return the DHCP-assigned addresses from the last successful DHCP request,
851 * if any.
852 * @return the DHCP information
853 */
854 public DhcpInfo getDhcpInfo() {
855 enforceAccessPermission();
Irfan Sheriffd8134ff2010-08-22 17:06:34 -0700856 return mWifiStateMachine.syncGetDhcpInfo();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800857 }
858
Irfan Sheriff0d255342010-07-28 09:35:20 -0700859 /**
860 * see {@link android.net.wifi.WifiManager#startWifi}
861 *
862 */
863 public void startWifi() {
864 enforceChangePermission();
865 /* TODO: may be add permissions for access only to connectivity service
866 * TODO: if a start issued, keep wifi alive until a stop issued irrespective
867 * of WifiLock & device idle status unless wifi enabled status is toggled
868 */
869
Irfan Sheriff4494c902011-12-08 10:47:54 -0800870 mWifiStateMachine.setDriverStart(true, mEmergencyCallbackMode);
Irfan Sheriff0d255342010-07-28 09:35:20 -0700871 mWifiStateMachine.reconnectCommand();
872 }
873
874 /**
875 * see {@link android.net.wifi.WifiManager#stopWifi}
876 *
877 */
878 public void stopWifi() {
879 enforceChangePermission();
880 /* TODO: may be add permissions for access only to connectivity service
881 * TODO: if a stop is issued, wifi is brought up only by startWifi
882 * unless wifi enabled status is toggled
883 */
Irfan Sheriff4494c902011-12-08 10:47:54 -0800884 mWifiStateMachine.setDriverStart(false, mEmergencyCallbackMode);
Irfan Sheriff0d255342010-07-28 09:35:20 -0700885 }
886
887
888 /**
889 * see {@link android.net.wifi.WifiManager#addToBlacklist}
890 *
891 */
892 public void addToBlacklist(String bssid) {
893 enforceChangePermission();
894
895 mWifiStateMachine.addToBlacklist(bssid);
896 }
897
898 /**
899 * see {@link android.net.wifi.WifiManager#clearBlacklist}
900 *
901 */
902 public void clearBlacklist() {
903 enforceChangePermission();
904
905 mWifiStateMachine.clearBlacklist();
906 }
907
Irfan Sheriff227bec42011-02-15 19:30:27 -0800908 /**
909 * Get a reference to handler. This is used by a client to establish
910 * an AsyncChannel communication with WifiService
911 */
Irfan Sheriff07573b32012-01-27 21:00:19 -0800912 public Messenger getWifiServiceMessenger() {
Irfan Sheriffebe606f2011-02-24 11:39:15 -0800913 /* Enforce the highest permissions
914 TODO: when we consider exposing the asynchronous API, think about
915 how to provide both access and change permissions seperately
916 */
Irfan Sheriff227bec42011-02-15 19:30:27 -0800917 enforceAccessPermission();
Irfan Sheriffebe606f2011-02-24 11:39:15 -0800918 enforceChangePermission();
Irfan Sheriff227bec42011-02-15 19:30:27 -0800919 return new Messenger(mAsyncServiceHandler);
920 }
921
Irfan Sheriff07573b32012-01-27 21:00:19 -0800922 /** Get a reference to WifiStateMachine handler for AsyncChannel communication */
923 public Messenger getWifiStateMachineMessenger() {
924 enforceAccessPermission();
925 enforceChangePermission();
926 return mWifiStateMachine.getMessenger();
927 }
928
Irfan Sheriff4aeca7c52011-03-10 16:53:33 -0800929 /**
930 * Get the IP and proxy configuration file
931 */
932 public String getConfigFile() {
933 enforceAccessPermission();
934 return mWifiStateMachine.getConfigFile();
935 }
936
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800937 private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
938 @Override
939 public void onReceive(Context context, Intent intent) {
940 String action = intent.getAction();
941
Doug Zongker43866e02010-01-07 12:09:54 -0800942 long idleMillis =
943 Settings.Secure.getLong(mContext.getContentResolver(),
Irfan Sheriff4f5f7c92010-10-14 17:01:27 -0700944 Settings.Secure.WIFI_IDLE_MS, DEFAULT_IDLE_MS);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800945 int stayAwakeConditions =
Doug Zongker43866e02010-01-07 12:09:54 -0800946 Settings.System.getInt(mContext.getContentResolver(),
947 Settings.System.STAY_ON_WHILE_PLUGGED_IN, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800948 if (action.equals(Intent.ACTION_SCREEN_ON)) {
Joe Onorato431bb222010-10-18 19:13:23 -0400949 if (DBG) {
950 Slog.d(TAG, "ACTION_SCREEN_ON");
951 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800952 mAlarmManager.cancel(mIdleIntent);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800953 mScreenOff = false;
Irfan Sheriff227bec42011-02-15 19:30:27 -0800954 evaluateTrafficStatsPolling();
Irfan Sheriff0d255342010-07-28 09:35:20 -0700955 mWifiStateMachine.enableRssiPolling(true);
Irfan Sherifffcc08452011-02-17 16:44:54 -0800956 if (mBackgroundScanSupported) {
Irfan Sheriff2b7f6382011-03-25 14:29:19 -0700957 mWifiStateMachine.enableBackgroundScanCommand(false);
Irfan Sherifffcc08452011-02-17 16:44:54 -0800958 }
Irfan Sheriff8e86b892010-12-22 11:02:20 -0800959 mWifiStateMachine.enableAllNetworks();
Irfan Sheriffe6daca52011-11-03 15:46:50 -0700960 setDeviceIdleAndUpdateWifi(false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800961 } else if (action.equals(Intent.ACTION_SCREEN_OFF)) {
Joe Onorato431bb222010-10-18 19:13:23 -0400962 if (DBG) {
963 Slog.d(TAG, "ACTION_SCREEN_OFF");
964 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800965 mScreenOff = true;
Irfan Sheriff227bec42011-02-15 19:30:27 -0800966 evaluateTrafficStatsPolling();
Irfan Sheriff0d255342010-07-28 09:35:20 -0700967 mWifiStateMachine.enableRssiPolling(false);
Irfan Sherifffcc08452011-02-17 16:44:54 -0800968 if (mBackgroundScanSupported) {
Irfan Sheriff2b7f6382011-03-25 14:29:19 -0700969 mWifiStateMachine.enableBackgroundScanCommand(true);
Irfan Sherifffcc08452011-02-17 16:44:54 -0800970 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800971 /*
972 * Set a timer to put Wi-Fi to sleep, but only if the screen is off
973 * AND the "stay on while plugged in" setting doesn't match the
974 * current power conditions (i.e, not plugged in, plugged in to USB,
975 * or plugged in to AC).
976 */
977 if (!shouldWifiStayAwake(stayAwakeConditions, mPluggedType)) {
Irfan Sheriffe6daca52011-11-03 15:46:50 -0700978 //Delayed shutdown if wifi is connected
979 if (mNetworkInfo.getDetailedState() == DetailedState.CONNECTED) {
980 if (DBG) Slog.d(TAG, "setting ACTION_DEVICE_IDLE: " + idleMillis + " ms");
981 mAlarmManager.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis()
982 + idleMillis, mIdleIntent);
Mike Lockwoodd9c32bc2009-05-18 14:14:15 -0400983 } else {
Irfan Sheriffe6daca52011-11-03 15:46:50 -0700984 setDeviceIdleAndUpdateWifi(true);
Mike Lockwoodd9c32bc2009-05-18 14:14:15 -0400985 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800986 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800987 } else if (action.equals(ACTION_DEVICE_IDLE)) {
Irfan Sheriffe6daca52011-11-03 15:46:50 -0700988 setDeviceIdleAndUpdateWifi(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800989 } else if (action.equals(Intent.ACTION_BATTERY_CHANGED)) {
990 /*
991 * Set a timer to put Wi-Fi to sleep, but only if the screen is off
992 * AND we are transitioning from a state in which the device was supposed
993 * to stay awake to a state in which it is not supposed to stay awake.
994 * If "stay awake" state is not changing, we do nothing, to avoid resetting
995 * the already-set timer.
996 */
997 int pluggedType = intent.getIntExtra("plugged", 0);
Joe Onorato431bb222010-10-18 19:13:23 -0400998 if (DBG) {
999 Slog.d(TAG, "ACTION_BATTERY_CHANGED pluggedType: " + pluggedType);
1000 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001001 if (mScreenOff && shouldWifiStayAwake(stayAwakeConditions, mPluggedType) &&
1002 !shouldWifiStayAwake(stayAwakeConditions, pluggedType)) {
1003 long triggerTime = System.currentTimeMillis() + idleMillis;
Joe Onorato431bb222010-10-18 19:13:23 -04001004 if (DBG) {
1005 Slog.d(TAG, "setting ACTION_DEVICE_IDLE timer for " + idleMillis + "ms");
1006 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001007 mAlarmManager.set(AlarmManager.RTC_WAKEUP, triggerTime, mIdleIntent);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001008 }
Irfan Sheriff8cef0672011-12-13 17:03:59 -08001009
1010 //Start scan stats tracking when device unplugged
1011 if (pluggedType == 0) {
1012 synchronized (mScanCount) {
1013 mScanCount.clear();
1014 }
1015 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001016 mPluggedType = pluggedType;
Irfan Sheriff65eaec82011-01-05 22:00:16 -08001017 } else if (action.equals(BluetoothAdapter.ACTION_CONNECTION_STATE_CHANGED)) {
1018 int state = intent.getIntExtra(BluetoothAdapter.EXTRA_CONNECTION_STATE,
1019 BluetoothAdapter.STATE_DISCONNECTED);
1020 mWifiStateMachine.sendBluetoothAdapterStateChange(state);
Irfan Sheriff616f3172011-09-11 19:59:01 -07001021 } else if (action.equals(TelephonyIntents.ACTION_EMERGENCY_CALLBACK_MODE_CHANGED)) {
1022 mEmergencyCallbackMode = intent.getBooleanExtra("phoneinECMState", false);
1023 updateWifiState();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001024 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001025 }
1026
1027 /**
1028 * Determines whether the Wi-Fi chipset should stay awake or be put to
1029 * sleep. Looks at the setting for the sleep policy and the current
1030 * conditions.
Jaikumar Ganesh084c6652009-12-07 10:58:18 -08001031 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001032 * @see #shouldDeviceStayAwake(int, int)
1033 */
1034 private boolean shouldWifiStayAwake(int stayAwakeConditions, int pluggedType) {
Irfan Sheriff739f6bc2011-01-28 16:43:12 -08001035 //Never sleep as long as the user has not changed the settings
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001036 int wifiSleepPolicy = Settings.System.getInt(mContext.getContentResolver(),
Irfan Sheriff96b10d62011-01-11 15:40:35 -08001037 Settings.System.WIFI_SLEEP_POLICY,
Irfan Sheriff739f6bc2011-01-28 16:43:12 -08001038 Settings.System.WIFI_SLEEP_POLICY_NEVER);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001039
1040 if (wifiSleepPolicy == Settings.System.WIFI_SLEEP_POLICY_NEVER) {
1041 // Never sleep
1042 return true;
1043 } else if ((wifiSleepPolicy == Settings.System.WIFI_SLEEP_POLICY_NEVER_WHILE_PLUGGED) &&
1044 (pluggedType != 0)) {
1045 // Never sleep while plugged, and we're plugged
1046 return true;
1047 } else {
1048 // Default
1049 return shouldDeviceStayAwake(stayAwakeConditions, pluggedType);
1050 }
1051 }
Jaikumar Ganesh084c6652009-12-07 10:58:18 -08001052
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001053 /**
1054 * Determine whether the bit value corresponding to {@code pluggedType} is set in
1055 * the bit string {@code stayAwakeConditions}. Because a {@code pluggedType} value
1056 * of {@code 0} isn't really a plugged type, but rather an indication that the
1057 * device isn't plugged in at all, there is no bit value corresponding to a
1058 * {@code pluggedType} value of {@code 0}. That is why we shift by
Ben Dodson4e8620f2010-08-25 10:55:47 -07001059 * {@code pluggedType - 1} instead of by {@code pluggedType}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001060 * @param stayAwakeConditions a bit string specifying which "plugged types" should
1061 * keep the device (and hence Wi-Fi) awake.
1062 * @param pluggedType the type of plug (USB, AC, or none) for which the check is
1063 * being made
1064 * @return {@code true} if {@code pluggedType} indicates that the device is
1065 * supposed to stay awake, {@code false} otherwise.
1066 */
1067 private boolean shouldDeviceStayAwake(int stayAwakeConditions, int pluggedType) {
1068 return (stayAwakeConditions & pluggedType) != 0;
1069 }
1070 };
1071
Irfan Sheriffe6daca52011-11-03 15:46:50 -07001072 private void setDeviceIdleAndUpdateWifi(boolean deviceIdle) {
1073 mDeviceIdle = deviceIdle;
1074 reportStartWorkSource();
1075 updateWifiState();
1076 }
1077
Dianne Hackborn03f3cb02010-09-17 23:12:26 -07001078 private synchronized void reportStartWorkSource() {
1079 mTmpWorkSource.clear();
1080 if (mDeviceIdle) {
1081 for (int i=0; i<mLocks.mList.size(); i++) {
1082 mTmpWorkSource.add(mLocks.mList.get(i).mWorkSource);
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07001083 }
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07001084 }
Dianne Hackborn03f3cb02010-09-17 23:12:26 -07001085 mWifiStateMachine.updateBatteryWorkSource(mTmpWorkSource);
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07001086 }
Jaikumar Ganesh7440fc22010-09-27 17:04:14 -07001087
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001088 private void updateWifiState() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001089 boolean lockHeld = mLocks.hasLocks();
Irfan Sheriff5876a422010-08-12 20:26:23 -07001090 int strongestLockMode = WifiManager.WIFI_MODE_FULL;
Irfan Sheriff616f3172011-09-11 19:59:01 -07001091 boolean wifiShouldBeStarted;
1092
1093 if (mEmergencyCallbackMode) {
1094 wifiShouldBeStarted = false;
1095 } else {
1096 wifiShouldBeStarted = !mDeviceIdle || lockHeld;
1097 }
Irfan Sheriff5876a422010-08-12 20:26:23 -07001098
1099 if (lockHeld) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001100 strongestLockMode = mLocks.getStrongestLockMode();
Irfan Sheriff5876a422010-08-12 20:26:23 -07001101 }
1102 /* If device is not idle, lockmode cannot be scan only */
1103 if (!mDeviceIdle && strongestLockMode == WifiManager.WIFI_MODE_SCAN_ONLY) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001104 strongestLockMode = WifiManager.WIFI_MODE_FULL;
1105 }
1106
Irfan Sheriffa2a1b912010-06-07 09:03:04 -07001107 /* Disable tethering when airplane mode is enabled */
Irfan Sheriff658772f2011-03-08 14:52:31 -08001108 if (mAirplaneModeOn.get()) {
Irfan Sheriff0d255342010-07-28 09:35:20 -07001109 mWifiStateMachine.setWifiApEnabled(null, false);
Irfan Sheriffa2a1b912010-06-07 09:03:04 -07001110 }
Irfan Sheriffb2e6c012010-04-05 11:57:56 -07001111
Irfan Sheriff658772f2011-03-08 14:52:31 -08001112 if (shouldWifiBeEnabled()) {
Irfan Sheriffa2a1b912010-06-07 09:03:04 -07001113 if (wifiShouldBeStarted) {
Dianne Hackborn03f3cb02010-09-17 23:12:26 -07001114 reportStartWorkSource();
Irfan Sheriff0d255342010-07-28 09:35:20 -07001115 mWifiStateMachine.setWifiEnabled(true);
1116 mWifiStateMachine.setScanOnlyMode(
Irfan Sheriffa2a1b912010-06-07 09:03:04 -07001117 strongestLockMode == WifiManager.WIFI_MODE_SCAN_ONLY);
Irfan Sheriff4494c902011-12-08 10:47:54 -08001118 mWifiStateMachine.setDriverStart(true, mEmergencyCallbackMode);
Irfan Sheriff5876a422010-08-12 20:26:23 -07001119 mWifiStateMachine.setHighPerfModeEnabled(strongestLockMode
1120 == WifiManager.WIFI_MODE_FULL_HIGH_PERF);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001121 } else {
Irfan Sheriff4494c902011-12-08 10:47:54 -08001122 mWifiStateMachine.setDriverStart(false, mEmergencyCallbackMode);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001123 }
Irfan Sheriffa2a1b912010-06-07 09:03:04 -07001124 } else {
Irfan Sheriff0d255342010-07-28 09:35:20 -07001125 mWifiStateMachine.setWifiEnabled(false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001126 }
1127 }
1128
1129 private void registerForBroadcasts() {
1130 IntentFilter intentFilter = new IntentFilter();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001131 intentFilter.addAction(Intent.ACTION_SCREEN_ON);
1132 intentFilter.addAction(Intent.ACTION_SCREEN_OFF);
1133 intentFilter.addAction(Intent.ACTION_BATTERY_CHANGED);
1134 intentFilter.addAction(ACTION_DEVICE_IDLE);
Irfan Sheriff65eaec82011-01-05 22:00:16 -08001135 intentFilter.addAction(BluetoothAdapter.ACTION_CONNECTION_STATE_CHANGED);
Irfan Sheriff616f3172011-09-11 19:59:01 -07001136 intentFilter.addAction(TelephonyIntents.ACTION_EMERGENCY_CALLBACK_MODE_CHANGED);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001137 mContext.registerReceiver(mReceiver, intentFilter);
1138 }
Jaikumar Ganesh084c6652009-12-07 10:58:18 -08001139
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001140 private boolean isAirplaneSensitive() {
1141 String airplaneModeRadios = Settings.System.getString(mContext.getContentResolver(),
1142 Settings.System.AIRPLANE_MODE_RADIOS);
1143 return airplaneModeRadios == null
1144 || airplaneModeRadios.contains(Settings.System.RADIO_WIFI);
1145 }
1146
Mike Lockwoodbd5ddf02009-07-29 21:37:14 -07001147 private boolean isAirplaneToggleable() {
1148 String toggleableRadios = Settings.System.getString(mContext.getContentResolver(),
1149 Settings.System.AIRPLANE_MODE_TOGGLEABLE_RADIOS);
1150 return toggleableRadios != null
1151 && toggleableRadios.contains(Settings.System.RADIO_WIFI);
1152 }
1153
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001154 /**
1155 * Returns true if Wi-Fi is sensitive to airplane mode, and airplane mode is
1156 * currently on.
1157 * @return {@code true} if airplane mode is on.
1158 */
1159 private boolean isAirplaneModeOn() {
1160 return isAirplaneSensitive() && Settings.System.getInt(mContext.getContentResolver(),
1161 Settings.System.AIRPLANE_MODE_ON, 0) == 1;
1162 }
1163
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001164 @Override
1165 protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
1166 if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.DUMP)
1167 != PackageManager.PERMISSION_GRANTED) {
1168 pw.println("Permission Denial: can't dump WifiService from from pid="
1169 + Binder.getCallingPid()
1170 + ", uid=" + Binder.getCallingUid());
1171 return;
1172 }
Irfan Sheriffd8134ff2010-08-22 17:06:34 -07001173 pw.println("Wi-Fi is " + mWifiStateMachine.syncGetWifiStateByName());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001174 pw.println("Stay-awake conditions: " +
1175 Settings.System.getInt(mContext.getContentResolver(),
1176 Settings.System.STAY_ON_WHILE_PLUGGED_IN, 0));
1177 pw.println();
1178
1179 pw.println("Internal state:");
Irfan Sheriff0d255342010-07-28 09:35:20 -07001180 pw.println(mWifiStateMachine);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001181 pw.println();
1182 pw.println("Latest scan results:");
Irfan Sheriffd8134ff2010-08-22 17:06:34 -07001183 List<ScanResult> scanResults = mWifiStateMachine.syncGetScanResultsList();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001184 if (scanResults != null && scanResults.size() != 0) {
1185 pw.println(" BSSID Frequency RSSI Flags SSID");
1186 for (ScanResult r : scanResults) {
1187 pw.printf(" %17s %9d %5d %-16s %s%n",
1188 r.BSSID,
1189 r.frequency,
1190 r.level,
1191 r.capabilities,
1192 r.SSID == null ? "" : r.SSID);
1193 }
1194 }
1195 pw.println();
Eric Shienbrood5711fad2009-03-27 20:25:31 -07001196 pw.println("Locks acquired: " + mFullLocksAcquired + " full, " +
Irfan Sheriff5876a422010-08-12 20:26:23 -07001197 mFullHighPerfLocksAcquired + " full high perf, " +
Eric Shienbrood5711fad2009-03-27 20:25:31 -07001198 mScanLocksAcquired + " scan");
1199 pw.println("Locks released: " + mFullLocksReleased + " full, " +
Irfan Sheriff5876a422010-08-12 20:26:23 -07001200 mFullHighPerfLocksReleased + " full high perf, " +
Eric Shienbrood5711fad2009-03-27 20:25:31 -07001201 mScanLocksReleased + " scan");
1202 pw.println();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001203 pw.println("Locks held:");
1204 mLocks.dump(pw);
Isaac Levybc7dfb52011-06-06 15:34:01 -07001205
Irfan Sheriff8cef0672011-12-13 17:03:59 -08001206 pw.println("Scan count since last plugged in");
1207 synchronized (mScanCount) {
1208 for(int sc : mScanCount.keySet()) {
1209 pw.println("UID: " + sc + " Scan count: " + mScanCount.get(sc));
1210 }
1211 }
1212
Isaac Levybc7dfb52011-06-06 15:34:01 -07001213 pw.println();
Isaac Levy654f5092011-07-13 17:41:45 -07001214 pw.println("WifiWatchdogStateMachine dump");
1215 mWifiWatchdogStateMachine.dump(pw);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001216 }
1217
Robert Greenwalt58ff0212009-05-19 15:53:54 -07001218 private class WifiLock extends DeathRecipient {
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -07001219 WifiLock(int lockMode, String tag, IBinder binder, WorkSource ws) {
1220 super(lockMode, tag, binder, ws);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001221 }
1222
1223 public void binderDied() {
1224 synchronized (mLocks) {
1225 releaseWifiLockLocked(mBinder);
1226 }
1227 }
1228
1229 public String toString() {
Robert Greenwalt5347bd42009-05-13 15:10:16 -07001230 return "WifiLock{" + mTag + " type=" + mMode + " binder=" + mBinder + "}";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001231 }
1232 }
1233
1234 private class LockList {
1235 private List<WifiLock> mList;
1236
1237 private LockList() {
1238 mList = new ArrayList<WifiLock>();
1239 }
1240
1241 private synchronized boolean hasLocks() {
1242 return !mList.isEmpty();
1243 }
1244
1245 private synchronized int getStrongestLockMode() {
1246 if (mList.isEmpty()) {
1247 return WifiManager.WIFI_MODE_FULL;
1248 }
Irfan Sheriff5876a422010-08-12 20:26:23 -07001249
1250 if (mFullHighPerfLocksAcquired > mFullHighPerfLocksReleased) {
1251 return WifiManager.WIFI_MODE_FULL_HIGH_PERF;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001252 }
Irfan Sheriff5876a422010-08-12 20:26:23 -07001253
1254 if (mFullLocksAcquired > mFullLocksReleased) {
1255 return WifiManager.WIFI_MODE_FULL;
1256 }
1257
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001258 return WifiManager.WIFI_MODE_SCAN_ONLY;
1259 }
1260
1261 private void addLock(WifiLock lock) {
1262 if (findLockByBinder(lock.mBinder) < 0) {
1263 mList.add(lock);
1264 }
1265 }
1266
1267 private WifiLock removeLock(IBinder binder) {
1268 int index = findLockByBinder(binder);
1269 if (index >= 0) {
Suchi Amalapurapufff2fda2009-06-30 21:36:16 -07001270 WifiLock ret = mList.remove(index);
1271 ret.unlinkDeathRecipient();
1272 return ret;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001273 } else {
1274 return null;
1275 }
1276 }
1277
1278 private int findLockByBinder(IBinder binder) {
1279 int size = mList.size();
1280 for (int i = size - 1; i >= 0; i--)
1281 if (mList.get(i).mBinder == binder)
1282 return i;
1283 return -1;
1284 }
1285
1286 private void dump(PrintWriter pw) {
1287 for (WifiLock l : mList) {
1288 pw.print(" ");
1289 pw.println(l);
1290 }
1291 }
1292 }
1293
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -07001294 void enforceWakeSourcePermission(int uid, int pid) {
Dianne Hackborne746f032010-09-13 16:02:57 -07001295 if (uid == android.os.Process.myUid()) {
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -07001296 return;
1297 }
1298 mContext.enforcePermission(android.Manifest.permission.UPDATE_DEVICE_STATS,
1299 pid, uid, null);
1300 }
1301
1302 public boolean acquireWifiLock(IBinder binder, int lockMode, String tag, WorkSource ws) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001303 mContext.enforceCallingOrSelfPermission(android.Manifest.permission.WAKE_LOCK, null);
Irfan Sheriff5876a422010-08-12 20:26:23 -07001304 if (lockMode != WifiManager.WIFI_MODE_FULL &&
1305 lockMode != WifiManager.WIFI_MODE_SCAN_ONLY &&
1306 lockMode != WifiManager.WIFI_MODE_FULL_HIGH_PERF) {
1307 Slog.e(TAG, "Illegal argument, lockMode= " + lockMode);
1308 if (DBG) throw new IllegalArgumentException("lockMode=" + lockMode);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001309 return false;
1310 }
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -07001311 if (ws != null && ws.size() == 0) {
1312 ws = null;
1313 }
Dianne Hackbornecfd7f72010-10-08 14:23:40 -07001314 if (ws != null) {
1315 enforceWakeSourcePermission(Binder.getCallingUid(), Binder.getCallingPid());
1316 }
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -07001317 if (ws == null) {
1318 ws = new WorkSource(Binder.getCallingUid());
1319 }
1320 WifiLock wifiLock = new WifiLock(lockMode, tag, binder, ws);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001321 synchronized (mLocks) {
1322 return acquireWifiLockLocked(wifiLock);
1323 }
1324 }
1325
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -07001326 private void noteAcquireWifiLock(WifiLock wifiLock) throws RemoteException {
1327 switch(wifiLock.mMode) {
1328 case WifiManager.WIFI_MODE_FULL:
Irfan Sheriff5876a422010-08-12 20:26:23 -07001329 case WifiManager.WIFI_MODE_FULL_HIGH_PERF:
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -07001330 mBatteryStats.noteFullWifiLockAcquiredFromSource(wifiLock.mWorkSource);
1331 break;
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -07001332 case WifiManager.WIFI_MODE_SCAN_ONLY:
1333 mBatteryStats.noteScanWifiLockAcquiredFromSource(wifiLock.mWorkSource);
1334 break;
1335 }
1336 }
1337
1338 private void noteReleaseWifiLock(WifiLock wifiLock) throws RemoteException {
1339 switch(wifiLock.mMode) {
1340 case WifiManager.WIFI_MODE_FULL:
Irfan Sheriff5876a422010-08-12 20:26:23 -07001341 case WifiManager.WIFI_MODE_FULL_HIGH_PERF:
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -07001342 mBatteryStats.noteFullWifiLockReleasedFromSource(wifiLock.mWorkSource);
1343 break;
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -07001344 case WifiManager.WIFI_MODE_SCAN_ONLY:
1345 mBatteryStats.noteScanWifiLockReleasedFromSource(wifiLock.mWorkSource);
1346 break;
1347 }
1348 }
1349
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001350 private boolean acquireWifiLockLocked(WifiLock wifiLock) {
Irfan Sheriffc89dd542010-09-28 08:40:54 -07001351 if (DBG) Slog.d(TAG, "acquireWifiLockLocked: " + wifiLock);
Robert Greenwaltf1acb2d2009-10-13 08:20:55 -07001352
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001353 mLocks.addLock(wifiLock);
Robert Greenwaltf1acb2d2009-10-13 08:20:55 -07001354
The Android Open Source Project10592532009-03-18 17:39:46 -07001355 long ident = Binder.clearCallingIdentity();
1356 try {
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -07001357 noteAcquireWifiLock(wifiLock);
Robert Greenwalt5347bd42009-05-13 15:10:16 -07001358 switch(wifiLock.mMode) {
Eric Shienbrood5711fad2009-03-27 20:25:31 -07001359 case WifiManager.WIFI_MODE_FULL:
1360 ++mFullLocksAcquired;
Eric Shienbrood5711fad2009-03-27 20:25:31 -07001361 break;
Irfan Sheriff5876a422010-08-12 20:26:23 -07001362 case WifiManager.WIFI_MODE_FULL_HIGH_PERF:
1363 ++mFullHighPerfLocksAcquired;
1364 break;
1365
Eric Shienbrood5711fad2009-03-27 20:25:31 -07001366 case WifiManager.WIFI_MODE_SCAN_ONLY:
1367 ++mScanLocksAcquired;
Eric Shienbrood5711fad2009-03-27 20:25:31 -07001368 break;
The Android Open Source Project10592532009-03-18 17:39:46 -07001369 }
Dianne Hackbornecfd7f72010-10-08 14:23:40 -07001370
1371 // Be aggressive about adding new locks into the accounted state...
1372 // we want to over-report rather than under-report.
1373 reportStartWorkSource();
1374
1375 updateWifiState();
1376 return true;
The Android Open Source Project10592532009-03-18 17:39:46 -07001377 } catch (RemoteException e) {
Dianne Hackbornecfd7f72010-10-08 14:23:40 -07001378 return false;
The Android Open Source Project10592532009-03-18 17:39:46 -07001379 } finally {
1380 Binder.restoreCallingIdentity(ident);
1381 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001382 }
1383
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -07001384 public void updateWifiLockWorkSource(IBinder lock, WorkSource ws) {
1385 int uid = Binder.getCallingUid();
1386 int pid = Binder.getCallingPid();
1387 if (ws != null && ws.size() == 0) {
1388 ws = null;
1389 }
1390 if (ws != null) {
1391 enforceWakeSourcePermission(uid, pid);
1392 }
1393 long ident = Binder.clearCallingIdentity();
1394 try {
1395 synchronized (mLocks) {
1396 int index = mLocks.findLockByBinder(lock);
1397 if (index < 0) {
1398 throw new IllegalArgumentException("Wifi lock not active");
1399 }
1400 WifiLock wl = mLocks.mList.get(index);
1401 noteReleaseWifiLock(wl);
1402 wl.mWorkSource = ws != null ? new WorkSource(ws) : new WorkSource(uid);
1403 noteAcquireWifiLock(wl);
1404 }
1405 } catch (RemoteException e) {
1406 } finally {
1407 Binder.restoreCallingIdentity(ident);
1408 }
1409 }
1410
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001411 public boolean releaseWifiLock(IBinder lock) {
1412 mContext.enforceCallingOrSelfPermission(android.Manifest.permission.WAKE_LOCK, null);
1413 synchronized (mLocks) {
1414 return releaseWifiLockLocked(lock);
1415 }
1416 }
1417
1418 private boolean releaseWifiLockLocked(IBinder lock) {
Eric Shienbroodd4c5f892009-03-24 18:13:20 -07001419 boolean hadLock;
Robert Greenwaltf1acb2d2009-10-13 08:20:55 -07001420
The Android Open Source Project10592532009-03-18 17:39:46 -07001421 WifiLock wifiLock = mLocks.removeLock(lock);
Robert Greenwaltf1acb2d2009-10-13 08:20:55 -07001422
Irfan Sheriffc89dd542010-09-28 08:40:54 -07001423 if (DBG) Slog.d(TAG, "releaseWifiLockLocked: " + wifiLock);
Robert Greenwaltf1acb2d2009-10-13 08:20:55 -07001424
Eric Shienbroodd4c5f892009-03-24 18:13:20 -07001425 hadLock = (wifiLock != null);
1426
Dianne Hackbornecfd7f72010-10-08 14:23:40 -07001427 long ident = Binder.clearCallingIdentity();
1428 try {
1429 if (hadLock) {
Wink Savillece0ea1f2b2011-10-13 16:55:20 -07001430 noteReleaseWifiLock(wifiLock);
Robert Greenwalt5347bd42009-05-13 15:10:16 -07001431 switch(wifiLock.mMode) {
Eric Shienbrood5711fad2009-03-27 20:25:31 -07001432 case WifiManager.WIFI_MODE_FULL:
1433 ++mFullLocksReleased;
Eric Shienbrood5711fad2009-03-27 20:25:31 -07001434 break;
Irfan Sheriff5876a422010-08-12 20:26:23 -07001435 case WifiManager.WIFI_MODE_FULL_HIGH_PERF:
1436 ++mFullHighPerfLocksReleased;
1437 break;
Eric Shienbrood5711fad2009-03-27 20:25:31 -07001438 case WifiManager.WIFI_MODE_SCAN_ONLY:
1439 ++mScanLocksReleased;
Eric Shienbrood5711fad2009-03-27 20:25:31 -07001440 break;
Eric Shienbroodd4c5f892009-03-24 18:13:20 -07001441 }
The Android Open Source Project10592532009-03-18 17:39:46 -07001442 }
Dianne Hackbornecfd7f72010-10-08 14:23:40 -07001443
1444 // TODO - should this only happen if you hadLock?
1445 updateWifiState();
1446
1447 } catch (RemoteException e) {
1448 } finally {
1449 Binder.restoreCallingIdentity(ident);
The Android Open Source Project10592532009-03-18 17:39:46 -07001450 }
Dianne Hackbornecfd7f72010-10-08 14:23:40 -07001451
Eric Shienbroodd4c5f892009-03-24 18:13:20 -07001452 return hadLock;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001453 }
Robert Greenwalt5347bd42009-05-13 15:10:16 -07001454
Robert Greenwalt58ff0212009-05-19 15:53:54 -07001455 private abstract class DeathRecipient
Robert Greenwalt5347bd42009-05-13 15:10:16 -07001456 implements IBinder.DeathRecipient {
1457 String mTag;
1458 int mMode;
1459 IBinder mBinder;
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -07001460 WorkSource mWorkSource;
Robert Greenwalt5347bd42009-05-13 15:10:16 -07001461
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -07001462 DeathRecipient(int mode, String tag, IBinder binder, WorkSource ws) {
Robert Greenwalt5347bd42009-05-13 15:10:16 -07001463 super();
1464 mTag = tag;
1465 mMode = mode;
1466 mBinder = binder;
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -07001467 mWorkSource = ws;
Robert Greenwalt5347bd42009-05-13 15:10:16 -07001468 try {
1469 mBinder.linkToDeath(this, 0);
1470 } catch (RemoteException e) {
1471 binderDied();
1472 }
1473 }
Suchi Amalapurapufff2fda2009-06-30 21:36:16 -07001474
1475 void unlinkDeathRecipient() {
1476 mBinder.unlinkToDeath(this, 0);
1477 }
Robert Greenwalt5347bd42009-05-13 15:10:16 -07001478 }
1479
Robert Greenwalt58ff0212009-05-19 15:53:54 -07001480 private class Multicaster extends DeathRecipient {
1481 Multicaster(String tag, IBinder binder) {
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -07001482 super(Binder.getCallingUid(), tag, binder, null);
Robert Greenwalt5347bd42009-05-13 15:10:16 -07001483 }
1484
1485 public void binderDied() {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001486 Slog.e(TAG, "Multicaster binderDied");
Robert Greenwalt5347bd42009-05-13 15:10:16 -07001487 synchronized (mMulticasters) {
1488 int i = mMulticasters.indexOf(this);
1489 if (i != -1) {
1490 removeMulticasterLocked(i, mMode);
1491 }
1492 }
1493 }
1494
1495 public String toString() {
Robert Greenwalt58ff0212009-05-19 15:53:54 -07001496 return "Multicaster{" + mTag + " binder=" + mBinder + "}";
Robert Greenwalt5347bd42009-05-13 15:10:16 -07001497 }
1498
1499 public int getUid() {
1500 return mMode;
1501 }
1502 }
1503
Robert Greenwalte2d155a2009-10-21 14:58:34 -07001504 public void initializeMulticastFiltering() {
1505 enforceMulticastChangePermission();
Irfan Sheriffa8fbe1f2010-03-09 09:13:58 -08001506
Robert Greenwalte2d155a2009-10-21 14:58:34 -07001507 synchronized (mMulticasters) {
1508 // if anybody had requested filters be off, leave off
1509 if (mMulticasters.size() != 0) {
1510 return;
1511 } else {
Irfan Sheriffb0c1b80f2011-07-19 15:44:25 -07001512 mWifiStateMachine.startFilteringMulticastV4Packets();
Robert Greenwalte2d155a2009-10-21 14:58:34 -07001513 }
1514 }
1515 }
1516
Robert Greenwaltfc1b15c2009-05-22 15:09:51 -07001517 public void acquireMulticastLock(IBinder binder, String tag) {
1518 enforceMulticastChangePermission();
Robert Greenwalt5347bd42009-05-13 15:10:16 -07001519
1520 synchronized (mMulticasters) {
1521 mMulticastEnabled++;
Robert Greenwalt58ff0212009-05-19 15:53:54 -07001522 mMulticasters.add(new Multicaster(tag, binder));
Irfan Sheriffb0c1b80f2011-07-19 15:44:25 -07001523 // Note that we could call stopFilteringMulticastV4Packets only when
Robert Greenwalt5347bd42009-05-13 15:10:16 -07001524 // our new size == 1 (first call), but this function won't
1525 // be called often and by making the stopPacket call each
1526 // time we're less fragile and self-healing.
Irfan Sheriffb0c1b80f2011-07-19 15:44:25 -07001527 mWifiStateMachine.stopFilteringMulticastV4Packets();
Robert Greenwalt5347bd42009-05-13 15:10:16 -07001528 }
1529
1530 int uid = Binder.getCallingUid();
1531 Long ident = Binder.clearCallingIdentity();
1532 try {
1533 mBatteryStats.noteWifiMulticastEnabled(uid);
1534 } catch (RemoteException e) {
1535 } finally {
1536 Binder.restoreCallingIdentity(ident);
1537 }
1538 }
1539
Robert Greenwaltfc1b15c2009-05-22 15:09:51 -07001540 public void releaseMulticastLock() {
1541 enforceMulticastChangePermission();
Robert Greenwalt5347bd42009-05-13 15:10:16 -07001542
1543 int uid = Binder.getCallingUid();
1544 synchronized (mMulticasters) {
1545 mMulticastDisabled++;
1546 int size = mMulticasters.size();
1547 for (int i = size - 1; i >= 0; i--) {
Robert Greenwalt58ff0212009-05-19 15:53:54 -07001548 Multicaster m = mMulticasters.get(i);
Robert Greenwalt5347bd42009-05-13 15:10:16 -07001549 if ((m != null) && (m.getUid() == uid)) {
1550 removeMulticasterLocked(i, uid);
1551 }
1552 }
1553 }
1554 }
1555
1556 private void removeMulticasterLocked(int i, int uid)
1557 {
Suchi Amalapurapufff2fda2009-06-30 21:36:16 -07001558 Multicaster removed = mMulticasters.remove(i);
Irfan Sheriffa8fbe1f2010-03-09 09:13:58 -08001559
Suchi Amalapurapufff2fda2009-06-30 21:36:16 -07001560 if (removed != null) {
1561 removed.unlinkDeathRecipient();
1562 }
Robert Greenwalt5347bd42009-05-13 15:10:16 -07001563 if (mMulticasters.size() == 0) {
Irfan Sheriffb0c1b80f2011-07-19 15:44:25 -07001564 mWifiStateMachine.startFilteringMulticastV4Packets();
Robert Greenwalt5347bd42009-05-13 15:10:16 -07001565 }
1566
1567 Long ident = Binder.clearCallingIdentity();
1568 try {
1569 mBatteryStats.noteWifiMulticastDisabled(uid);
1570 } catch (RemoteException e) {
1571 } finally {
1572 Binder.restoreCallingIdentity(ident);
1573 }
1574 }
1575
Robert Greenwalt58ff0212009-05-19 15:53:54 -07001576 public boolean isMulticastEnabled() {
Robert Greenwalt5347bd42009-05-13 15:10:16 -07001577 enforceAccessPermission();
1578
1579 synchronized (mMulticasters) {
1580 return (mMulticasters.size() > 0);
1581 }
1582 }
Irfan Sheriff0d255342010-07-28 09:35:20 -07001583
Irfan Sheriff227bec42011-02-15 19:30:27 -08001584 /**
1585 * Evaluate if traffic stats polling is needed based on
1586 * connection and screen on status
1587 */
1588 private void evaluateTrafficStatsPolling() {
1589 Message msg;
1590 if (mNetworkInfo.getDetailedState() == DetailedState.CONNECTED && !mScreenOff) {
Irfan Sheriffebe606f2011-02-24 11:39:15 -08001591 msg = Message.obtain(mAsyncServiceHandler,
Irfan Sheriffd3975a92012-02-24 10:54:13 -08001592 WifiManager.ENABLE_TRAFFIC_STATS_POLL, 1, 0);
Irfan Sheriff227bec42011-02-15 19:30:27 -08001593 } else {
Irfan Sheriffebe606f2011-02-24 11:39:15 -08001594 msg = Message.obtain(mAsyncServiceHandler,
Irfan Sheriffd3975a92012-02-24 10:54:13 -08001595 WifiManager.ENABLE_TRAFFIC_STATS_POLL, 0, 0);
Irfan Sheriff227bec42011-02-15 19:30:27 -08001596 }
1597 msg.sendToTarget();
1598 }
1599
1600 private void notifyOnDataActivity() {
1601 long sent, received;
1602 long preTxPkts = mTxPkts, preRxPkts = mRxPkts;
1603 int dataActivity = WifiManager.DATA_ACTIVITY_NONE;
1604
1605 mTxPkts = TrafficStats.getTxPackets(mInterfaceName);
1606 mRxPkts = TrafficStats.getRxPackets(mInterfaceName);
1607
1608 if (preTxPkts > 0 || preRxPkts > 0) {
1609 sent = mTxPkts - preTxPkts;
1610 received = mRxPkts - preRxPkts;
1611 if (sent > 0) {
1612 dataActivity |= WifiManager.DATA_ACTIVITY_OUT;
1613 }
1614 if (received > 0) {
1615 dataActivity |= WifiManager.DATA_ACTIVITY_IN;
1616 }
1617
1618 if (dataActivity != mDataActivity && !mScreenOff) {
1619 mDataActivity = dataActivity;
1620 for (AsyncChannel client : mClients) {
1621 client.sendMessage(WifiManager.DATA_ACTIVITY_NOTIFICATION, mDataActivity);
1622 }
1623 }
1624 }
1625 }
1626
1627
Irfan Sheriff0d255342010-07-28 09:35:20 -07001628 private void checkAndSetNotification() {
1629 // If we shouldn't place a notification on available networks, then
1630 // don't bother doing any of the following
1631 if (!mNotificationEnabled) return;
1632
1633 State state = mNetworkInfo.getState();
1634 if ((state == NetworkInfo.State.DISCONNECTED)
1635 || (state == NetworkInfo.State.UNKNOWN)) {
1636 // Look for an open network
Irfan Sheriffd8134ff2010-08-22 17:06:34 -07001637 List<ScanResult> scanResults = mWifiStateMachine.syncGetScanResultsList();
Irfan Sheriff0d255342010-07-28 09:35:20 -07001638 if (scanResults != null) {
1639 int numOpenNetworks = 0;
1640 for (int i = scanResults.size() - 1; i >= 0; i--) {
1641 ScanResult scanResult = scanResults.get(i);
1642
Irfan Sherifffdd5f952011-08-04 16:55:54 -07001643 //A capability of [ESS] represents an open access point
1644 //that is available for an STA to connect
1645 if (scanResult.capabilities != null &&
1646 scanResult.capabilities.equals("[ESS]")) {
Irfan Sheriff0d255342010-07-28 09:35:20 -07001647 numOpenNetworks++;
1648 }
1649 }
1650
1651 if (numOpenNetworks > 0) {
1652 if (++mNumScansSinceNetworkStateChange >= NUM_SCANS_BEFORE_ACTUALLY_SCANNING) {
1653 /*
1654 * We've scanned continuously at least
1655 * NUM_SCANS_BEFORE_NOTIFICATION times. The user
1656 * probably does not have a remembered network in range,
1657 * since otherwise supplicant would have tried to
1658 * associate and thus resetting this counter.
1659 */
1660 setNotificationVisible(true, numOpenNetworks, false, 0);
1661 }
1662 return;
1663 }
1664 }
1665 }
1666
1667 // No open networks in range, remove the notification
1668 setNotificationVisible(false, 0, false, 0);
1669 }
1670
1671 /**
1672 * Clears variables related to tracking whether a notification has been
1673 * shown recently and clears the current notification.
1674 */
1675 private void resetNotification() {
1676 mNotificationRepeatTime = 0;
1677 mNumScansSinceNetworkStateChange = 0;
1678 setNotificationVisible(false, 0, false, 0);
1679 }
1680
1681 /**
1682 * Display or don't display a notification that there are open Wi-Fi networks.
1683 * @param visible {@code true} if notification should be visible, {@code false} otherwise
1684 * @param numNetworks the number networks seen
1685 * @param force {@code true} to force notification to be shown/not-shown,
1686 * even if it is already shown/not-shown.
1687 * @param delay time in milliseconds after which the notification should be made
1688 * visible or invisible.
1689 */
1690 private void setNotificationVisible(boolean visible, int numNetworks, boolean force,
1691 int delay) {
1692
1693 // Since we use auto cancel on the notification, when the
1694 // mNetworksAvailableNotificationShown is true, the notification may
1695 // have actually been canceled. However, when it is false we know
1696 // for sure that it is not being shown (it will not be shown any other
1697 // place than here)
1698
1699 // If it should be hidden and it is already hidden, then noop
1700 if (!visible && !mNotificationShown && !force) {
1701 return;
1702 }
1703
1704 NotificationManager notificationManager = (NotificationManager) mContext
1705 .getSystemService(Context.NOTIFICATION_SERVICE);
1706
1707 Message message;
1708 if (visible) {
1709
1710 // Not enough time has passed to show the notification again
1711 if (System.currentTimeMillis() < mNotificationRepeatTime) {
1712 return;
1713 }
1714
1715 if (mNotification == null) {
Wink Savillec7a98342010-08-13 16:11:42 -07001716 // Cache the Notification object.
Irfan Sheriff0d255342010-07-28 09:35:20 -07001717 mNotification = new Notification();
1718 mNotification.when = 0;
1719 mNotification.icon = ICON_NETWORKS_AVAILABLE;
1720 mNotification.flags = Notification.FLAG_AUTO_CANCEL;
1721 mNotification.contentIntent = PendingIntent.getActivity(mContext, 0,
1722 new Intent(WifiManager.ACTION_PICK_WIFI_NETWORK), 0);
1723 }
1724
1725 CharSequence title = mContext.getResources().getQuantityText(
1726 com.android.internal.R.plurals.wifi_available, numNetworks);
1727 CharSequence details = mContext.getResources().getQuantityText(
1728 com.android.internal.R.plurals.wifi_available_detailed, numNetworks);
1729 mNotification.tickerText = title;
1730 mNotification.setLatestEventInfo(mContext, title, details, mNotification.contentIntent);
1731
1732 mNotificationRepeatTime = System.currentTimeMillis() + NOTIFICATION_REPEAT_DELAY_MS;
1733
1734 notificationManager.notify(ICON_NETWORKS_AVAILABLE, mNotification);
Irfan Sheriff0d255342010-07-28 09:35:20 -07001735 } else {
Irfan Sheriff0d255342010-07-28 09:35:20 -07001736 notificationManager.cancel(ICON_NETWORKS_AVAILABLE);
Irfan Sheriff0d255342010-07-28 09:35:20 -07001737 }
1738
Irfan Sheriff0d255342010-07-28 09:35:20 -07001739 mNotificationShown = visible;
1740 }
1741
1742 private class NotificationEnabledSettingObserver extends ContentObserver {
1743
1744 public NotificationEnabledSettingObserver(Handler handler) {
1745 super(handler);
1746 }
1747
1748 public void register() {
1749 ContentResolver cr = mContext.getContentResolver();
1750 cr.registerContentObserver(Settings.Secure.getUriFor(
1751 Settings.Secure.WIFI_NETWORKS_AVAILABLE_NOTIFICATION_ON), true, this);
1752 mNotificationEnabled = getValue();
1753 }
1754
1755 @Override
1756 public void onChange(boolean selfChange) {
1757 super.onChange(selfChange);
1758
1759 mNotificationEnabled = getValue();
1760 resetNotification();
1761 }
1762
1763 private boolean getValue() {
1764 return Settings.Secure.getInt(mContext.getContentResolver(),
1765 Settings.Secure.WIFI_NETWORKS_AVAILABLE_NOTIFICATION_ON, 1) == 1;
1766 }
1767 }
1768
1769
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001770}