blob: 41ec63a53c904f6d98a73231a93aa53c2a7fa09c [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;
Irfan Sheriff9ab518ad2010-03-12 15:48:17 -080038import android.net.wifi.WifiConfiguration.KeyMgmt;
Irfan Sheriff02fb46a2010-12-08 11:27:37 -080039import android.net.wifi.WpsConfiguration;
Irfan Sheriffe4c56c92011-01-12 16:33:58 -080040import android.net.wifi.WpsResult;
Irfan Sheriff5321aef2010-02-12 12:35:59 -080041import android.net.ConnectivityManager;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080042import android.net.DhcpInfo;
Irfan Sheriff0d255342010-07-28 09:35:20 -070043import android.net.NetworkInfo;
44import android.net.NetworkInfo.State;
Irfan Sheriff227bec42011-02-15 19:30:27 -080045import android.net.NetworkInfo.DetailedState;
46import android.net.TrafficStats;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080047import android.os.Binder;
Irfan Sheriff0d255342010-07-28 09:35:20 -070048import android.os.Handler;
Irfan Sheriff227bec42011-02-15 19:30:27 -080049import android.os.Messenger;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080050import android.os.HandlerThread;
51import android.os.IBinder;
Irfan Sheriff5321aef2010-02-12 12:35:59 -080052import android.os.INetworkManagementService;
Irfan Sheriff0d255342010-07-28 09:35:20 -070053import android.os.Message;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080054import android.os.RemoteException;
Amith Yamasani47873e52009-07-02 12:05:32 -070055import android.os.ServiceManager;
Irfan Sheriff227bec42011-02-15 19:30:27 -080056import android.os.SystemProperties;
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -070057import android.os.WorkSource;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080058import android.provider.Settings;
Irfan Sheriff0d255342010-07-28 09:35:20 -070059import android.text.TextUtils;
Joe Onorato8a9b2202010-02-26 18:56:32 -080060import android.util.Slog;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080061
62import java.util.ArrayList;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080063import java.util.List;
Jaikumar Ganesh084c6652009-12-07 10:58:18 -080064import java.util.Set;
Irfan Sheriff658772f2011-03-08 14:52:31 -080065import java.util.concurrent.atomic.AtomicInteger;
Irfan Sheriffa2a1b912010-06-07 09:03:04 -070066import java.util.concurrent.atomic.AtomicBoolean;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080067import java.io.FileDescriptor;
68import java.io.PrintWriter;
69
The Android Open Source Project10592532009-03-18 17:39:46 -070070import com.android.internal.app.IBatteryStats;
Wink Saville4b7ba092010-10-20 15:37:41 -070071import com.android.internal.util.AsyncChannel;
The Android Open Source Project10592532009-03-18 17:39:46 -070072import com.android.server.am.BatteryStatsService;
Irfan Sheriff9ab518ad2010-03-12 15:48:17 -080073import com.android.internal.R;
The Android Open Source Project10592532009-03-18 17:39:46 -070074
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080075/**
76 * WifiService handles remote WiFi operation requests by implementing
Irfan Sheriffa2a1b912010-06-07 09:03:04 -070077 * the IWifiManager interface.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080078 *
79 * @hide
80 */
Irfan Sheriffa2a1b912010-06-07 09:03:04 -070081//TODO: Clean up multiple locks and implement WifiService
82// as a SM to track soft AP/client/adhoc bring up based
83// on device idle state, airplane mode and boot.
84
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080085public class WifiService extends IWifiManager.Stub {
86 private static final String TAG = "WifiService";
Irfan Sheriffa2a1b912010-06-07 09:03:04 -070087 private static final boolean DBG = true;
88
Irfan Sheriff0d255342010-07-28 09:35:20 -070089 private final WifiStateMachine mWifiStateMachine;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080090
91 private Context mContext;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080092
93 private AlarmManager mAlarmManager;
94 private PendingIntent mIdleIntent;
95 private static final int IDLE_REQUEST = 0;
96 private boolean mScreenOff;
97 private boolean mDeviceIdle;
98 private int mPluggedType;
99
Irfan Sherifffcc08452011-02-17 16:44:54 -0800100 /* Chipset supports background scan */
101 private final boolean mBackgroundScanSupported;
102
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800103 private final LockList mLocks = new LockList();
Eric Shienbrood5711fad2009-03-27 20:25:31 -0700104 // some wifi lock statistics
Irfan Sheriff5876a422010-08-12 20:26:23 -0700105 private int mFullHighPerfLocksAcquired;
106 private int mFullHighPerfLocksReleased;
Eric Shienbrood5711fad2009-03-27 20:25:31 -0700107 private int mFullLocksAcquired;
108 private int mFullLocksReleased;
109 private int mScanLocksAcquired;
110 private int mScanLocksReleased;
The Android Open Source Project10592532009-03-18 17:39:46 -0700111
Robert Greenwalt58ff0212009-05-19 15:53:54 -0700112 private final List<Multicaster> mMulticasters =
113 new ArrayList<Multicaster>();
Robert Greenwalt5347bd42009-05-13 15:10:16 -0700114 private int mMulticastEnabled;
115 private int mMulticastDisabled;
116
The Android Open Source Project10592532009-03-18 17:39:46 -0700117 private final IBatteryStats mBatteryStats;
Jaikumar Ganesh084c6652009-12-07 10:58:18 -0800118
Irfan Sheriff227bec42011-02-15 19:30:27 -0800119 private boolean mEnableTrafficStatsPoll = false;
120 private int mTrafficStatsPollToken = 0;
121 private long mTxPkts;
122 private long mRxPkts;
123 /* Tracks last reported data activity */
124 private int mDataActivity;
125 private String mInterfaceName;
126
127 /**
128 * Interval in milliseconds between polling for traffic
129 * statistics
130 */
131 private static final int POLL_TRAFFIC_STATS_INTERVAL_MSECS = 1000;
132
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800133 /**
Doug Zongker43866e02010-01-07 12:09:54 -0800134 * See {@link Settings.Secure#WIFI_IDLE_MS}. This is the default value if a
135 * Settings.Secure value is not present. This timeout value is chosen as
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800136 * the approximate point at which the battery drain caused by Wi-Fi
137 * being enabled but not active exceeds the battery drain caused by
138 * re-establishing a connection to the mobile data network.
139 */
Irfan Sheriff4f5f7c92010-10-14 17:01:27 -0700140 private static final long DEFAULT_IDLE_MS = 15 * 60 * 1000; /* 15 minutes */
141
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800142 private static final String ACTION_DEVICE_IDLE =
143 "com.android.server.WifiManager.action.DEVICE_IDLE";
144
Irfan Sheriff658772f2011-03-08 14:52:31 -0800145 private static final int WIFI_DISABLED = 0;
146 private static final int WIFI_ENABLED = 1;
147 /* Wifi enabled while in airplane mode */
148 private static final int WIFI_ENABLED_AIRPLANE_OVERRIDE = 2;
149
150 private AtomicInteger mWifiState = new AtomicInteger(WIFI_DISABLED);
151 private AtomicBoolean mAirplaneModeOn = new AtomicBoolean(false);
152
Irfan Sheriffa2a1b912010-06-07 09:03:04 -0700153 private boolean mIsReceiverRegistered = false;
154
Irfan Sheriff0d255342010-07-28 09:35:20 -0700155
156 NetworkInfo mNetworkInfo = new NetworkInfo(ConnectivityManager.TYPE_WIFI, 0, "WIFI", "");
157
158 // Variables relating to the 'available networks' notification
159 /**
160 * The icon to show in the 'available networks' notification. This will also
161 * be the ID of the Notification given to the NotificationManager.
162 */
163 private static final int ICON_NETWORKS_AVAILABLE =
164 com.android.internal.R.drawable.stat_notify_wifi_in_range;
165 /**
166 * When a notification is shown, we wait this amount before possibly showing it again.
167 */
168 private final long NOTIFICATION_REPEAT_DELAY_MS;
169 /**
170 * Whether the user has set the setting to show the 'available networks' notification.
171 */
172 private boolean mNotificationEnabled;
173 /**
174 * Observes the user setting to keep {@link #mNotificationEnabled} in sync.
175 */
176 private NotificationEnabledSettingObserver mNotificationEnabledSettingObserver;
177 /**
178 * The {@link System#currentTimeMillis()} must be at least this value for us
179 * to show the notification again.
180 */
181 private long mNotificationRepeatTime;
182 /**
183 * The Notification object given to the NotificationManager.
184 */
185 private Notification mNotification;
186 /**
187 * Whether the notification is being shown, as set by us. That is, if the
188 * user cancels the notification, we will not receive the callback so this
189 * will still be true. We only guarantee if this is false, then the
190 * notification is not showing.
191 */
192 private boolean mNotificationShown;
193 /**
194 * The number of continuous scans that must occur before consider the
195 * supplicant in a scanning state. This allows supplicant to associate with
196 * remembered networks that are in the scan results.
197 */
198 private static final int NUM_SCANS_BEFORE_ACTUALLY_SCANNING = 3;
199 /**
200 * The number of scans since the last network state change. When this
201 * exceeds {@link #NUM_SCANS_BEFORE_ACTUALLY_SCANNING}, we consider the
202 * supplicant to actually be scanning. When the network state changes to
203 * something other than scanning, we reset this to 0.
204 */
205 private int mNumScansSinceNetworkStateChange;
Jaikumar Ganesh7440fc22010-09-27 17:04:14 -0700206
Dianne Hackborn03f3cb02010-09-17 23:12:26 -0700207 /**
Wink Saville4b7ba092010-10-20 15:37:41 -0700208 * Asynchronous channel to WifiStateMachine
209 */
Irfan Sheriff227bec42011-02-15 19:30:27 -0800210 private AsyncChannel mWifiStateMachineChannel;
Wink Saville4b7ba092010-10-20 15:37:41 -0700211
212 /**
Irfan Sheriff227bec42011-02-15 19:30:27 -0800213 * Clients receiving asynchronous messages
Wink Saville4b7ba092010-10-20 15:37:41 -0700214 */
Irfan Sheriff227bec42011-02-15 19:30:27 -0800215 private List<AsyncChannel> mClients = new ArrayList<AsyncChannel>();
Wink Saville4b7ba092010-10-20 15:37:41 -0700216
Irfan Sheriff227bec42011-02-15 19:30:27 -0800217 /**
218 * Handles client connections
219 */
220 private class AsyncServiceHandler extends Handler {
221
222 AsyncServiceHandler(android.os.Looper looper) {
Wink Saville4b7ba092010-10-20 15:37:41 -0700223 super(looper);
Wink Saville4b7ba092010-10-20 15:37:41 -0700224 }
225
226 @Override
227 public void handleMessage(Message msg) {
228 switch (msg.what) {
229 case AsyncChannel.CMD_CHANNEL_HALF_CONNECTED: {
230 if (msg.arg1 == AsyncChannel.STATUS_SUCCESSFUL) {
Irfan Sheriff227bec42011-02-15 19:30:27 -0800231 Slog.d(TAG, "New client listening to asynchronous messages");
232 mClients.add((AsyncChannel) msg.obj);
Wink Saville4b7ba092010-10-20 15:37:41 -0700233 } else {
Irfan Sheriff227bec42011-02-15 19:30:27 -0800234 Slog.e(TAG, "Client connection failure, error=" + msg.arg1);
235 }
236 break;
237 }
Irfan Sheriffc23971b2011-03-04 17:06:31 -0800238 case AsyncChannel.CMD_CHANNEL_DISCONNECTED: {
239 if (msg.arg1 == AsyncChannel.STATUS_SEND_UNSUCCESSFUL) {
240 Slog.d(TAG, "Send failed, client connection lost");
241 } else {
242 Slog.d(TAG, "Client connection lost with reason: " + msg.arg1);
243 }
244 mClients.remove((AsyncChannel) msg.obj);
245 break;
246 }
Irfan Sheriff227bec42011-02-15 19:30:27 -0800247 case AsyncChannel.CMD_CHANNEL_FULL_CONNECTION: {
248 AsyncChannel ac = new AsyncChannel();
249 ac.connect(mContext, this, msg.replyTo);
250 break;
251 }
Irfan Sheriffebe606f2011-02-24 11:39:15 -0800252 case WifiManager.CMD_ENABLE_TRAFFIC_STATS_POLL: {
Irfan Sheriff227bec42011-02-15 19:30:27 -0800253 mEnableTrafficStatsPoll = (msg.arg1 == 1);
254 mTrafficStatsPollToken++;
255 if (mEnableTrafficStatsPoll) {
256 notifyOnDataActivity();
Irfan Sheriffebe606f2011-02-24 11:39:15 -0800257 sendMessageDelayed(Message.obtain(this, WifiManager.CMD_TRAFFIC_STATS_POLL,
Irfan Sheriff227bec42011-02-15 19:30:27 -0800258 mTrafficStatsPollToken, 0), POLL_TRAFFIC_STATS_INTERVAL_MSECS);
259 }
260 break;
261 }
Irfan Sheriffebe606f2011-02-24 11:39:15 -0800262 case WifiManager.CMD_TRAFFIC_STATS_POLL: {
Irfan Sheriff227bec42011-02-15 19:30:27 -0800263 if (msg.arg1 == mTrafficStatsPollToken) {
264 notifyOnDataActivity();
Irfan Sheriffebe606f2011-02-24 11:39:15 -0800265 sendMessageDelayed(Message.obtain(this, WifiManager.CMD_TRAFFIC_STATS_POLL,
Irfan Sheriff227bec42011-02-15 19:30:27 -0800266 mTrafficStatsPollToken, 0), POLL_TRAFFIC_STATS_INTERVAL_MSECS);
Wink Saville4b7ba092010-10-20 15:37:41 -0700267 }
268 break;
269 }
Irfan Sheriffebe606f2011-02-24 11:39:15 -0800270 case WifiManager.CMD_CONNECT_NETWORK: {
271 if (msg.obj != null) {
272 mWifiStateMachine.connectNetwork((WifiConfiguration)msg.obj);
273 } else {
274 mWifiStateMachine.connectNetwork(msg.arg1);
275 }
276 break;
277 }
278 case WifiManager.CMD_SAVE_NETWORK: {
279 mWifiStateMachine.saveNetwork((WifiConfiguration)msg.obj);
280 break;
281 }
282 case WifiManager.CMD_FORGET_NETWORK: {
283 mWifiStateMachine.forgetNetwork(msg.arg1);
284 break;
285 }
286 case WifiManager.CMD_START_WPS: {
287 //replyTo has the original source
288 mWifiStateMachine.startWps(msg.replyTo, (WpsConfiguration)msg.obj);
289 break;
290 }
Wink Saville4b7ba092010-10-20 15:37:41 -0700291 default: {
292 Slog.d(TAG, "WifiServicehandler.handleMessage ignoring msg=" + msg);
293 break;
294 }
295 }
296 }
297 }
Irfan Sheriff227bec42011-02-15 19:30:27 -0800298 private AsyncServiceHandler mAsyncServiceHandler;
299
300 /**
301 * Handles interaction with WifiStateMachine
302 */
303 private class WifiStateMachineHandler extends Handler {
304 private AsyncChannel mWsmChannel;
305
306 WifiStateMachineHandler(android.os.Looper looper) {
307 super(looper);
308 mWsmChannel = new AsyncChannel();
309 mWsmChannel.connect(mContext, this, mWifiStateMachine.getHandler());
310 }
311
312 @Override
313 public void handleMessage(Message msg) {
314 switch (msg.what) {
315 case AsyncChannel.CMD_CHANNEL_HALF_CONNECTED: {
316 if (msg.arg1 == AsyncChannel.STATUS_SUCCESSFUL) {
317 mWifiStateMachineChannel = mWsmChannel;
318 } else {
319 Slog.e(TAG, "WifiStateMachine connection failure, error=" + msg.arg1);
320 mWifiStateMachineChannel = null;
321 }
322 break;
323 }
324 default: {
325 Slog.d(TAG, "WifiStateMachineHandler.handleMessage ignoring msg=" + msg);
326 break;
327 }
328 }
329 }
330 }
331 WifiStateMachineHandler mWifiStateMachineHandler;
Wink Saville4b7ba092010-10-20 15:37:41 -0700332
333 /**
Dianne Hackborn03f3cb02010-09-17 23:12:26 -0700334 * Temporary for computing UIDS that are responsible for starting WIFI.
335 * Protected by mWifiStateTracker lock.
336 */
337 private final WorkSource mTmpWorkSource = new WorkSource();
Irfan Sheriff0d255342010-07-28 09:35:20 -0700338
339 WifiService(Context context) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800340 mContext = context;
Irfan Sheriff227bec42011-02-15 19:30:27 -0800341
342 mInterfaceName = SystemProperties.get("wifi.interface", "wlan0");
343
344 mWifiStateMachine = new WifiStateMachine(mContext, mInterfaceName);
Irfan Sheriff0d255342010-07-28 09:35:20 -0700345 mWifiStateMachine.enableRssiPolling(true);
The Android Open Source Project10592532009-03-18 17:39:46 -0700346 mBatteryStats = BatteryStatsService.getService();
Jaikumar Ganesh084c6652009-12-07 10:58:18 -0800347
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800348 mAlarmManager = (AlarmManager)mContext.getSystemService(Context.ALARM_SERVICE);
349 Intent idleIntent = new Intent(ACTION_DEVICE_IDLE, null);
350 mIdleIntent = PendingIntent.getBroadcast(mContext, IDLE_REQUEST, idleIntent, 0);
351
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800352 mContext.registerReceiver(
353 new BroadcastReceiver() {
354 @Override
355 public void onReceive(Context context, Intent intent) {
Irfan Sheriff658772f2011-03-08 14:52:31 -0800356 mAirplaneModeOn.set(isAirplaneModeOn());
357 /* On airplane mode disable, restore wifi state if necessary */
358 if (!mAirplaneModeOn.get() && (testAndClearWifiSavedState() ||
359 mWifiState.get() == WIFI_ENABLED_AIRPLANE_OVERRIDE)) {
360 persistWifiEnabled(true);
Irfan Sheriffb2e6c012010-04-05 11:57:56 -0700361 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800362 updateWifiState();
363 }
364 },
365 new IntentFilter(Intent.ACTION_AIRPLANE_MODE_CHANGED));
366
Irfan Sheriff0d255342010-07-28 09:35:20 -0700367 IntentFilter filter = new IntentFilter();
368 filter.addAction(WifiManager.WIFI_STATE_CHANGED_ACTION);
369 filter.addAction(WifiManager.NETWORK_STATE_CHANGED_ACTION);
370 filter.addAction(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION);
371
372 mContext.registerReceiver(
373 new BroadcastReceiver() {
374 @Override
375 public void onReceive(Context context, Intent intent) {
376 if (intent.getAction().equals(WifiManager.WIFI_STATE_CHANGED_ACTION)) {
377 // reset & clear notification on any wifi state change
378 resetNotification();
379 } else if (intent.getAction().equals(
380 WifiManager.NETWORK_STATE_CHANGED_ACTION)) {
381 mNetworkInfo = (NetworkInfo) intent.getParcelableExtra(
382 WifiManager.EXTRA_NETWORK_INFO);
383 // reset & clear notification on a network connect & disconnect
384 switch(mNetworkInfo.getDetailedState()) {
385 case CONNECTED:
386 case DISCONNECTED:
Irfan Sheriff227bec42011-02-15 19:30:27 -0800387 evaluateTrafficStatsPolling();
Irfan Sheriff0d255342010-07-28 09:35:20 -0700388 resetNotification();
389 break;
390 }
391 } else if (intent.getAction().equals(
392 WifiManager.SCAN_RESULTS_AVAILABLE_ACTION)) {
393 checkAndSetNotification();
394 }
395 }
396 }, filter);
397
Irfan Sheriff227bec42011-02-15 19:30:27 -0800398 HandlerThread wifiThread = new HandlerThread("WifiService");
399 wifiThread.start();
400 mAsyncServiceHandler = new AsyncServiceHandler(wifiThread.getLooper());
401 mWifiStateMachineHandler = new WifiStateMachineHandler(wifiThread.getLooper());
402
Irfan Sheriff0d255342010-07-28 09:35:20 -0700403 // Setting is in seconds
404 NOTIFICATION_REPEAT_DELAY_MS = Settings.Secure.getInt(context.getContentResolver(),
405 Settings.Secure.WIFI_NETWORKS_AVAILABLE_REPEAT_DELAY, 900) * 1000l;
406 mNotificationEnabledSettingObserver = new NotificationEnabledSettingObserver(new Handler());
407 mNotificationEnabledSettingObserver.register();
Irfan Sherifffcc08452011-02-17 16:44:54 -0800408
409 mBackgroundScanSupported = mContext.getResources().getBoolean(
410 com.android.internal.R.bool.config_wifi_background_scan_support);
Irfan Sheriff7b009782010-03-11 16:37:45 -0800411 }
Irfan Sheriff5321aef2010-02-12 12:35:59 -0800412
Irfan Sheriff7b009782010-03-11 16:37:45 -0800413 /**
414 * Check if Wi-Fi needs to be enabled and start
415 * if needed
Irfan Sheriff60e3ba02010-04-02 12:18:45 -0700416 *
417 * This function is used only at boot time
Irfan Sheriff7b009782010-03-11 16:37:45 -0800418 */
Irfan Sheriff0d255342010-07-28 09:35:20 -0700419 public void checkAndStartWifi() {
Irfan Sheriff658772f2011-03-08 14:52:31 -0800420 mAirplaneModeOn.set(isAirplaneModeOn());
421 mWifiState.set(getPersistedWifiState());
422 /* Start if Wi-Fi should be enabled or the saved state indicates Wi-Fi was on */
423 boolean wifiEnabled = shouldWifiBeEnabled() || testAndClearWifiSavedState();
Irfan Sheriff7b009782010-03-11 16:37:45 -0800424 Slog.i(TAG, "WifiService starting up with Wi-Fi " +
425 (wifiEnabled ? "enabled" : "disabled"));
Irfan Sheriffb99fe5e2010-03-26 14:56:07 -0700426 setWifiEnabled(wifiEnabled);
Irfan Sheriff5321aef2010-02-12 12:35:59 -0800427 }
428
Irfan Sheriffa3bd4092010-03-24 17:58:59 -0700429 private boolean testAndClearWifiSavedState() {
430 final ContentResolver cr = mContext.getContentResolver();
431 int wifiSavedState = 0;
432 try {
433 wifiSavedState = Settings.Secure.getInt(cr, Settings.Secure.WIFI_SAVED_STATE);
434 if(wifiSavedState == 1)
435 Settings.Secure.putInt(cr, Settings.Secure.WIFI_SAVED_STATE, 0);
436 } catch (Settings.SettingNotFoundException e) {
437 ;
438 }
439 return (wifiSavedState == 1);
440 }
441
Irfan Sheriff658772f2011-03-08 14:52:31 -0800442 private int getPersistedWifiState() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800443 final ContentResolver cr = mContext.getContentResolver();
444 try {
Irfan Sheriff658772f2011-03-08 14:52:31 -0800445 return Settings.Secure.getInt(cr, Settings.Secure.WIFI_ON);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800446 } catch (Settings.SettingNotFoundException e) {
Irfan Sheriff658772f2011-03-08 14:52:31 -0800447 Settings.Secure.putInt(cr, Settings.Secure.WIFI_ON, WIFI_DISABLED);
448 return WIFI_DISABLED;
449 }
450 }
451
452 private boolean shouldWifiBeEnabled() {
453 if (mAirplaneModeOn.get()) {
454 return mWifiState.get() == WIFI_ENABLED_AIRPLANE_OVERRIDE;
455 } else {
456 return mWifiState.get() != WIFI_DISABLED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800457 }
458 }
459
460 private void persistWifiEnabled(boolean enabled) {
461 final ContentResolver cr = mContext.getContentResolver();
Irfan Sheriff658772f2011-03-08 14:52:31 -0800462 if (enabled) {
463 if (isAirplaneModeOn() && isAirplaneToggleable()) {
464 mWifiState.set(WIFI_ENABLED_AIRPLANE_OVERRIDE);
465 } else {
466 mWifiState.set(WIFI_ENABLED);
467 }
468 } else {
469 mWifiState.set(WIFI_DISABLED);
470 }
471 Settings.Secure.putInt(cr, Settings.Secure.WIFI_ON, mWifiState.get());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800472 }
473
Irfan Sheriff658772f2011-03-08 14:52:31 -0800474
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800475 /**
476 * see {@link android.net.wifi.WifiManager#pingSupplicant()}
Irfan Sheriffa2a1b912010-06-07 09:03:04 -0700477 * @return {@code true} if the operation succeeds, {@code false} otherwise
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800478 */
479 public boolean pingSupplicant() {
Irfan Sheriffa2a1b912010-06-07 09:03:04 -0700480 enforceAccessPermission();
Irfan Sheriff227bec42011-02-15 19:30:27 -0800481 if (mWifiStateMachineChannel != null) {
482 return mWifiStateMachine.syncPingSupplicant(mWifiStateMachineChannel);
Irfan Sheriff1406bcb2010-10-28 14:41:39 -0700483 } else {
Irfan Sheriff227bec42011-02-15 19:30:27 -0800484 Slog.e(TAG, "mWifiStateMachineChannel is not initialized");
Irfan Sheriff1406bcb2010-10-28 14:41:39 -0700485 return false;
486 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800487 }
488
489 /**
490 * see {@link android.net.wifi.WifiManager#startScan()}
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800491 */
Irfan Sheriffe4984752010-08-19 11:29:22 -0700492 public void startScan(boolean forceActive) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800493 enforceChangePermission();
Irfan Sheriffe4984752010-08-19 11:29:22 -0700494 mWifiStateMachine.startScan(forceActive);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800495 }
496
497 private void enforceAccessPermission() {
498 mContext.enforceCallingOrSelfPermission(android.Manifest.permission.ACCESS_WIFI_STATE,
499 "WifiService");
500 }
501
502 private void enforceChangePermission() {
503 mContext.enforceCallingOrSelfPermission(android.Manifest.permission.CHANGE_WIFI_STATE,
504 "WifiService");
505
506 }
507
Robert Greenwaltfc1b15c2009-05-22 15:09:51 -0700508 private void enforceMulticastChangePermission() {
509 mContext.enforceCallingOrSelfPermission(
510 android.Manifest.permission.CHANGE_WIFI_MULTICAST_STATE,
511 "WifiService");
512 }
513
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800514 /**
Irfan Sheriffa2a1b912010-06-07 09:03:04 -0700515 * see {@link android.net.wifi.WifiManager#setWifiEnabled(boolean)}
516 * @param enable {@code true} to enable, {@code false} to disable.
517 * @return {@code true} if the enable/disable operation was
518 * started or is already in the queue.
519 */
520 public synchronized boolean setWifiEnabled(boolean enable) {
521 enforceChangePermission();
522
523 if (DBG) {
Irfan Sheriff0d255342010-07-28 09:35:20 -0700524 Slog.e(TAG, "Invoking mWifiStateMachine.setWifiEnabled\n");
Irfan Sheriffa2a1b912010-06-07 09:03:04 -0700525 }
526
Dianne Hackborn03f3cb02010-09-17 23:12:26 -0700527 if (enable) {
528 reportStartWorkSource();
529 }
Irfan Sheriff0d255342010-07-28 09:35:20 -0700530 mWifiStateMachine.setWifiEnabled(enable);
Irfan Sheriff61180692010-08-18 16:07:39 -0700531
532 /*
533 * Caller might not have WRITE_SECURE_SETTINGS,
534 * only CHANGE_WIFI_STATE is enforced
535 */
536 long ident = Binder.clearCallingIdentity();
Irfan Sheriffa2a1b912010-06-07 09:03:04 -0700537 persistWifiEnabled(enable);
Irfan Sheriff61180692010-08-18 16:07:39 -0700538 Binder.restoreCallingIdentity(ident);
Irfan Sheriffa2a1b912010-06-07 09:03:04 -0700539
540 if (enable) {
541 if (!mIsReceiverRegistered) {
542 registerForBroadcasts();
543 mIsReceiverRegistered = true;
544 }
545 } else if (mIsReceiverRegistered){
546 mContext.unregisterReceiver(mReceiver);
547 mIsReceiverRegistered = false;
548 }
549
550 return true;
551 }
552
553 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800554 * see {@link WifiManager#getWifiState()}
555 * @return One of {@link WifiManager#WIFI_STATE_DISABLED},
556 * {@link WifiManager#WIFI_STATE_DISABLING},
557 * {@link WifiManager#WIFI_STATE_ENABLED},
558 * {@link WifiManager#WIFI_STATE_ENABLING},
559 * {@link WifiManager#WIFI_STATE_UNKNOWN}
560 */
561 public int getWifiEnabledState() {
562 enforceAccessPermission();
Irfan Sheriffd8134ff2010-08-22 17:06:34 -0700563 return mWifiStateMachine.syncGetWifiState();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800564 }
565
566 /**
Irfan Sheriffc2f54c22010-03-18 14:02:22 -0700567 * see {@link android.net.wifi.WifiManager#setWifiApEnabled(WifiConfiguration, boolean)}
Irfan Sheriff5321aef2010-02-12 12:35:59 -0800568 * @param wifiConfig SSID, security and channel details as
569 * part of WifiConfiguration
Irfan Sheriffa2a1b912010-06-07 09:03:04 -0700570 * @param enabled true to enable and false to disable
Irfan Sheriff5321aef2010-02-12 12:35:59 -0800571 */
Irfan Sheriffffcea7a2011-05-10 16:26:06 -0700572 public void setWifiApEnabled(WifiConfiguration wifiConfig, boolean enabled) {
Irfan Sheriff5321aef2010-02-12 12:35:59 -0800573 enforceChangePermission();
Irfan Sheriff0d255342010-07-28 09:35:20 -0700574 mWifiStateMachine.setWifiApEnabled(wifiConfig, enabled);
Irfan Sheriff5321aef2010-02-12 12:35:59 -0800575 }
576
Irfan Sheriffa2a1b912010-06-07 09:03:04 -0700577 /**
578 * see {@link WifiManager#getWifiApState()}
579 * @return One of {@link WifiManager#WIFI_AP_STATE_DISABLED},
580 * {@link WifiManager#WIFI_AP_STATE_DISABLING},
581 * {@link WifiManager#WIFI_AP_STATE_ENABLED},
582 * {@link WifiManager#WIFI_AP_STATE_ENABLING},
583 * {@link WifiManager#WIFI_AP_STATE_FAILED}
584 */
585 public int getWifiApEnabledState() {
Irfan Sheriff17b232b2010-06-24 11:32:26 -0700586 enforceAccessPermission();
Irfan Sheriffd8134ff2010-08-22 17:06:34 -0700587 return mWifiStateMachine.syncGetWifiApState();
Irfan Sheriffa2a1b912010-06-07 09:03:04 -0700588 }
589
590 /**
591 * see {@link WifiManager#getWifiApConfiguration()}
592 * @return soft access point configuration
593 */
Irfan Sheriffffcea7a2011-05-10 16:26:06 -0700594 public WifiConfiguration getWifiApConfiguration() {
595 enforceAccessPermission();
596 return mWifiStateMachine.syncGetWifiApConfiguration(mWifiStateMachineChannel);
Irfan Sheriff9ab518ad2010-03-12 15:48:17 -0800597 }
598
Irfan Sheriffa2a1b912010-06-07 09:03:04 -0700599 /**
600 * see {@link WifiManager#setWifiApConfiguration(WifiConfiguration)}
601 * @param wifiConfig WifiConfiguration details for soft access point
602 */
Irfan Sheriffffcea7a2011-05-10 16:26:06 -0700603 public void setWifiApConfiguration(WifiConfiguration wifiConfig) {
Irfan Sheriff17b232b2010-06-24 11:32:26 -0700604 enforceChangePermission();
Irfan Sheriff9ab518ad2010-03-12 15:48:17 -0800605 if (wifiConfig == null)
606 return;
Irfan Sheriffffcea7a2011-05-10 16:26:06 -0700607 mWifiStateMachine.setWifiApConfiguration(wifiConfig);
Irfan Sheriff9ab518ad2010-03-12 15:48:17 -0800608 }
609
Irfan Sheriff5321aef2010-02-12 12:35:59 -0800610 /**
Irfan Sheriffa2a1b912010-06-07 09:03:04 -0700611 * see {@link android.net.wifi.WifiManager#disconnect()}
Irfan Sheriff5321aef2010-02-12 12:35:59 -0800612 */
Irfan Sheriffe4984752010-08-19 11:29:22 -0700613 public void disconnect() {
Irfan Sheriffa2a1b912010-06-07 09:03:04 -0700614 enforceChangePermission();
Irfan Sheriffe4984752010-08-19 11:29:22 -0700615 mWifiStateMachine.disconnectCommand();
Irfan Sheriff5321aef2010-02-12 12:35:59 -0800616 }
617
618 /**
Irfan Sheriffa2a1b912010-06-07 09:03:04 -0700619 * see {@link android.net.wifi.WifiManager#reconnect()}
Irfan Sheriff5321aef2010-02-12 12:35:59 -0800620 */
Irfan Sheriffe4984752010-08-19 11:29:22 -0700621 public void reconnect() {
Irfan Sheriffa2a1b912010-06-07 09:03:04 -0700622 enforceChangePermission();
Irfan Sheriffe4984752010-08-19 11:29:22 -0700623 mWifiStateMachine.reconnectCommand();
Irfan Sheriff5321aef2010-02-12 12:35:59 -0800624 }
625
Irfan Sheriffa2a1b912010-06-07 09:03:04 -0700626 /**
627 * see {@link android.net.wifi.WifiManager#reassociate()}
Irfan Sheriffa2a1b912010-06-07 09:03:04 -0700628 */
Irfan Sheriffe4984752010-08-19 11:29:22 -0700629 public void reassociate() {
Irfan Sheriffa2a1b912010-06-07 09:03:04 -0700630 enforceChangePermission();
Irfan Sheriffe4984752010-08-19 11:29:22 -0700631 mWifiStateMachine.reassociateCommand();
Irfan Sheriff5321aef2010-02-12 12:35:59 -0800632 }
633
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800634 /**
635 * see {@link android.net.wifi.WifiManager#getConfiguredNetworks()}
636 * @return the list of configured networks
637 */
638 public List<WifiConfiguration> getConfiguredNetworks() {
639 enforceAccessPermission();
Irfan Sheriffd8134ff2010-08-22 17:06:34 -0700640 return mWifiStateMachine.syncGetConfiguredNetworks();
Chung-yih Wanga8d15942009-10-09 11:01:49 +0800641 }
642
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800643 /**
644 * see {@link android.net.wifi.WifiManager#addOrUpdateNetwork(WifiConfiguration)}
645 * @return the supplicant-assigned identifier for the new or updated
646 * network if the operation succeeds, or {@code -1} if it fails
647 */
Irfan Sheriff7aac5542009-12-22 21:42:17 -0800648 public int addOrUpdateNetwork(WifiConfiguration config) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800649 enforceChangePermission();
Irfan Sheriff227bec42011-02-15 19:30:27 -0800650 if (mWifiStateMachineChannel != null) {
651 return mWifiStateMachine.syncAddOrUpdateNetwork(mWifiStateMachineChannel, config);
Irfan Sheriff1406bcb2010-10-28 14:41:39 -0700652 } else {
Irfan Sheriff227bec42011-02-15 19:30:27 -0800653 Slog.e(TAG, "mWifiStateMachineChannel is not initialized");
Irfan Sheriff1406bcb2010-10-28 14:41:39 -0700654 return -1;
655 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800656 }
657
Irfan Sheriffa2a1b912010-06-07 09:03:04 -0700658 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800659 * See {@link android.net.wifi.WifiManager#removeNetwork(int)}
660 * @param netId the integer that identifies the network configuration
661 * to the supplicant
662 * @return {@code true} if the operation succeeded
663 */
664 public boolean removeNetwork(int netId) {
665 enforceChangePermission();
Irfan Sheriff227bec42011-02-15 19:30:27 -0800666 if (mWifiStateMachineChannel != null) {
667 return mWifiStateMachine.syncRemoveNetwork(mWifiStateMachineChannel, netId);
Wink Saville4b7ba092010-10-20 15:37:41 -0700668 } else {
Irfan Sheriff227bec42011-02-15 19:30:27 -0800669 Slog.e(TAG, "mWifiStateMachineChannel is not initialized");
Wink Saville4b7ba092010-10-20 15:37:41 -0700670 return false;
671 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800672 }
673
674 /**
675 * See {@link android.net.wifi.WifiManager#enableNetwork(int, boolean)}
676 * @param netId the integer that identifies the network configuration
677 * to the supplicant
678 * @param disableOthers if true, disable all other networks.
679 * @return {@code true} if the operation succeeded
680 */
681 public boolean enableNetwork(int netId, boolean disableOthers) {
682 enforceChangePermission();
Irfan Sheriff227bec42011-02-15 19:30:27 -0800683 if (mWifiStateMachineChannel != null) {
684 return mWifiStateMachine.syncEnableNetwork(mWifiStateMachineChannel, netId,
685 disableOthers);
Irfan Sheriff1406bcb2010-10-28 14:41:39 -0700686 } else {
Irfan Sheriff227bec42011-02-15 19:30:27 -0800687 Slog.e(TAG, "mWifiStateMachineChannel is not initialized");
Irfan Sheriff1406bcb2010-10-28 14:41:39 -0700688 return false;
689 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800690 }
691
692 /**
693 * See {@link android.net.wifi.WifiManager#disableNetwork(int)}
694 * @param netId the integer that identifies the network configuration
695 * to the supplicant
696 * @return {@code true} if the operation succeeded
697 */
698 public boolean disableNetwork(int netId) {
699 enforceChangePermission();
Irfan Sheriff227bec42011-02-15 19:30:27 -0800700 if (mWifiStateMachineChannel != null) {
701 return mWifiStateMachine.syncDisableNetwork(mWifiStateMachineChannel, netId);
Irfan Sheriff1406bcb2010-10-28 14:41:39 -0700702 } else {
Irfan Sheriff227bec42011-02-15 19:30:27 -0800703 Slog.e(TAG, "mWifiStateMachineChannel is not initialized");
Irfan Sheriff1406bcb2010-10-28 14:41:39 -0700704 return false;
705 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800706 }
707
708 /**
709 * See {@link android.net.wifi.WifiManager#getConnectionInfo()}
710 * @return the Wi-Fi information, contained in {@link WifiInfo}.
711 */
712 public WifiInfo getConnectionInfo() {
713 enforceAccessPermission();
714 /*
715 * Make sure we have the latest information, by sending
716 * a status request to the supplicant.
717 */
Irfan Sheriffd8134ff2010-08-22 17:06:34 -0700718 return mWifiStateMachine.syncRequestConnectionInfo();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800719 }
720
721 /**
722 * Return the results of the most recent access point scan, in the form of
723 * a list of {@link ScanResult} objects.
724 * @return the list of results
725 */
726 public List<ScanResult> getScanResults() {
727 enforceAccessPermission();
Irfan Sheriffd8134ff2010-08-22 17:06:34 -0700728 return mWifiStateMachine.syncGetScanResultsList();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800729 }
730
731 /**
732 * Tell the supplicant to persist the current list of configured networks.
733 * @return {@code true} if the operation succeeded
Irfan Sheriffa2a1b912010-06-07 09:03:04 -0700734 *
735 * TODO: deprecate this
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800736 */
737 public boolean saveConfiguration() {
Irfan Sheriffa2a1b912010-06-07 09:03:04 -0700738 boolean result = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800739 enforceChangePermission();
Irfan Sheriff227bec42011-02-15 19:30:27 -0800740 if (mWifiStateMachineChannel != null) {
741 return mWifiStateMachine.syncSaveConfig(mWifiStateMachineChannel);
Irfan Sheriff1406bcb2010-10-28 14:41:39 -0700742 } else {
Irfan Sheriff227bec42011-02-15 19:30:27 -0800743 Slog.e(TAG, "mWifiStateMachineChannel is not initialized");
Irfan Sheriff1406bcb2010-10-28 14:41:39 -0700744 return false;
745 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800746 }
747
748 /**
Irfan Sheriffed4f28b2010-10-29 15:32:10 -0700749 * Set the country code
750 * @param countryCode ISO 3166 country code.
Robert Greenwaltb5010cc2009-05-21 15:11:40 -0700751 * @param persist {@code true} if the setting should be remembered.
Irfan Sheriffed4f28b2010-10-29 15:32:10 -0700752 *
753 * The persist behavior exists so that wifi can fall back to the last
754 * persisted country code on a restart, when the locale information is
755 * not available from telephony.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800756 */
Irfan Sheriffed4f28b2010-10-29 15:32:10 -0700757 public void setCountryCode(String countryCode, boolean persist) {
758 Slog.i(TAG, "WifiService trying to set country code to " + countryCode +
759 " with persist set to " + persist);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800760 enforceChangePermission();
Irfan Sheriffed4f28b2010-10-29 15:32:10 -0700761 mWifiStateMachine.setCountryCode(countryCode, persist);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800762 }
763
764 /**
Irfan Sheriff36f74132010-11-04 16:57:37 -0700765 * Set the operational frequency band
766 * @param band One of
767 * {@link WifiManager#WIFI_FREQUENCY_BAND_AUTO},
768 * {@link WifiManager#WIFI_FREQUENCY_BAND_5GHZ},
769 * {@link WifiManager#WIFI_FREQUENCY_BAND_2GHZ},
770 * @param persist {@code true} if the setting should be remembered.
771 *
772 */
773 public void setFrequencyBand(int band, boolean persist) {
774 enforceChangePermission();
775 if (!isDualBandSupported()) return;
776 Slog.i(TAG, "WifiService trying to set frequency band to " + band +
777 " with persist set to " + persist);
778 mWifiStateMachine.setFrequencyBand(band, persist);
779 }
780
781
782 /**
783 * Get the operational frequency band
784 */
785 public int getFrequencyBand() {
786 enforceAccessPermission();
787 return mWifiStateMachine.getFrequencyBand();
788 }
789
790 public boolean isDualBandSupported() {
791 //TODO: Should move towards adding a driver API that checks at runtime
792 return mContext.getResources().getBoolean(
793 com.android.internal.R.bool.config_wifi_dual_band_support);
794 }
795
796 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800797 * Return the DHCP-assigned addresses from the last successful DHCP request,
798 * if any.
799 * @return the DHCP information
800 */
801 public DhcpInfo getDhcpInfo() {
802 enforceAccessPermission();
Irfan Sheriffd8134ff2010-08-22 17:06:34 -0700803 return mWifiStateMachine.syncGetDhcpInfo();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800804 }
805
Irfan Sheriff0d255342010-07-28 09:35:20 -0700806 /**
807 * see {@link android.net.wifi.WifiManager#startWifi}
808 *
809 */
810 public void startWifi() {
811 enforceChangePermission();
812 /* TODO: may be add permissions for access only to connectivity service
813 * TODO: if a start issued, keep wifi alive until a stop issued irrespective
814 * of WifiLock & device idle status unless wifi enabled status is toggled
815 */
816
817 mWifiStateMachine.setDriverStart(true);
818 mWifiStateMachine.reconnectCommand();
819 }
820
821 /**
822 * see {@link android.net.wifi.WifiManager#stopWifi}
823 *
824 */
825 public void stopWifi() {
826 enforceChangePermission();
827 /* TODO: may be add permissions for access only to connectivity service
828 * TODO: if a stop is issued, wifi is brought up only by startWifi
829 * unless wifi enabled status is toggled
830 */
831 mWifiStateMachine.setDriverStart(false);
832 }
833
834
835 /**
836 * see {@link android.net.wifi.WifiManager#addToBlacklist}
837 *
838 */
839 public void addToBlacklist(String bssid) {
840 enforceChangePermission();
841
842 mWifiStateMachine.addToBlacklist(bssid);
843 }
844
845 /**
846 * see {@link android.net.wifi.WifiManager#clearBlacklist}
847 *
848 */
849 public void clearBlacklist() {
850 enforceChangePermission();
851
852 mWifiStateMachine.clearBlacklist();
853 }
854
Irfan Sheriff227bec42011-02-15 19:30:27 -0800855 /**
856 * Get a reference to handler. This is used by a client to establish
857 * an AsyncChannel communication with WifiService
858 */
859 public Messenger getMessenger() {
Irfan Sheriffebe606f2011-02-24 11:39:15 -0800860 /* Enforce the highest permissions
861 TODO: when we consider exposing the asynchronous API, think about
862 how to provide both access and change permissions seperately
863 */
Irfan Sheriff227bec42011-02-15 19:30:27 -0800864 enforceAccessPermission();
Irfan Sheriffebe606f2011-02-24 11:39:15 -0800865 enforceChangePermission();
Irfan Sheriff227bec42011-02-15 19:30:27 -0800866 return new Messenger(mAsyncServiceHandler);
867 }
868
Irfan Sheriff4aeca7c52011-03-10 16:53:33 -0800869 /**
870 * Get the IP and proxy configuration file
871 */
872 public String getConfigFile() {
873 enforceAccessPermission();
874 return mWifiStateMachine.getConfigFile();
875 }
876
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800877 private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
878 @Override
879 public void onReceive(Context context, Intent intent) {
880 String action = intent.getAction();
881
Doug Zongker43866e02010-01-07 12:09:54 -0800882 long idleMillis =
883 Settings.Secure.getLong(mContext.getContentResolver(),
Irfan Sheriff4f5f7c92010-10-14 17:01:27 -0700884 Settings.Secure.WIFI_IDLE_MS, DEFAULT_IDLE_MS);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800885 int stayAwakeConditions =
Doug Zongker43866e02010-01-07 12:09:54 -0800886 Settings.System.getInt(mContext.getContentResolver(),
887 Settings.System.STAY_ON_WHILE_PLUGGED_IN, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800888 if (action.equals(Intent.ACTION_SCREEN_ON)) {
Joe Onorato431bb222010-10-18 19:13:23 -0400889 if (DBG) {
890 Slog.d(TAG, "ACTION_SCREEN_ON");
891 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800892 mAlarmManager.cancel(mIdleIntent);
893 mDeviceIdle = false;
894 mScreenOff = false;
Dianne Hackborn58e0eef2010-09-16 01:22:10 -0700895 // Once the screen is on, we are not keeping WIFI running
896 // because of any locks so clear that tracking immediately.
897 reportStartWorkSource();
Irfan Sheriff227bec42011-02-15 19:30:27 -0800898 evaluateTrafficStatsPolling();
Irfan Sheriff0d255342010-07-28 09:35:20 -0700899 mWifiStateMachine.enableRssiPolling(true);
Irfan Sherifffcc08452011-02-17 16:44:54 -0800900 if (mBackgroundScanSupported) {
Irfan Sheriff2b7f6382011-03-25 14:29:19 -0700901 mWifiStateMachine.enableBackgroundScanCommand(false);
Irfan Sherifffcc08452011-02-17 16:44:54 -0800902 }
Irfan Sheriff8e86b892010-12-22 11:02:20 -0800903 mWifiStateMachine.enableAllNetworks();
Irfan Sheriff4f5f7c92010-10-14 17:01:27 -0700904 updateWifiState();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800905 } else if (action.equals(Intent.ACTION_SCREEN_OFF)) {
Joe Onorato431bb222010-10-18 19:13:23 -0400906 if (DBG) {
907 Slog.d(TAG, "ACTION_SCREEN_OFF");
908 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800909 mScreenOff = true;
Irfan Sheriff227bec42011-02-15 19:30:27 -0800910 evaluateTrafficStatsPolling();
Irfan Sheriff0d255342010-07-28 09:35:20 -0700911 mWifiStateMachine.enableRssiPolling(false);
Irfan Sherifffcc08452011-02-17 16:44:54 -0800912 if (mBackgroundScanSupported) {
Irfan Sheriff2b7f6382011-03-25 14:29:19 -0700913 mWifiStateMachine.enableBackgroundScanCommand(true);
Irfan Sherifffcc08452011-02-17 16:44:54 -0800914 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800915 /*
916 * Set a timer to put Wi-Fi to sleep, but only if the screen is off
917 * AND the "stay on while plugged in" setting doesn't match the
918 * current power conditions (i.e, not plugged in, plugged in to USB,
919 * or plugged in to AC).
920 */
921 if (!shouldWifiStayAwake(stayAwakeConditions, mPluggedType)) {
Irfan Sheriffd8134ff2010-08-22 17:06:34 -0700922 WifiInfo info = mWifiStateMachine.syncRequestConnectionInfo();
San Mehatfa6c7112009-07-07 09:34:44 -0700923 if (info.getSupplicantState() != SupplicantState.COMPLETED) {
Robert Greenwalt84612ea62009-09-30 09:04:22 -0700924 // we used to go to sleep immediately, but this caused some race conditions
Irfan Sheriffa2a1b912010-06-07 09:03:04 -0700925 // we don't have time to track down for this release. Delay instead,
926 // but not as long as we would if connected (below)
Robert Greenwalt84612ea62009-09-30 09:04:22 -0700927 // TODO - fix the race conditions and switch back to the immediate turn-off
928 long triggerTime = System.currentTimeMillis() + (2*60*1000); // 2 min
Joe Onorato431bb222010-10-18 19:13:23 -0400929 if (DBG) {
930 Slog.d(TAG, "setting ACTION_DEVICE_IDLE timer for 120,000 ms");
931 }
Robert Greenwalt84612ea62009-09-30 09:04:22 -0700932 mAlarmManager.set(AlarmManager.RTC_WAKEUP, triggerTime, mIdleIntent);
933 // // do not keep Wifi awake when screen is off if Wifi is not associated
934 // mDeviceIdle = true;
935 // updateWifiState();
Mike Lockwoodd9c32bc2009-05-18 14:14:15 -0400936 } else {
937 long triggerTime = System.currentTimeMillis() + idleMillis;
Joe Onorato431bb222010-10-18 19:13:23 -0400938 if (DBG) {
939 Slog.d(TAG, "setting ACTION_DEVICE_IDLE timer for " + idleMillis
940 + "ms");
941 }
Mike Lockwoodd9c32bc2009-05-18 14:14:15 -0400942 mAlarmManager.set(AlarmManager.RTC_WAKEUP, triggerTime, mIdleIntent);
943 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800944 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800945 } else if (action.equals(ACTION_DEVICE_IDLE)) {
Joe Onorato431bb222010-10-18 19:13:23 -0400946 if (DBG) {
947 Slog.d(TAG, "got ACTION_DEVICE_IDLE");
948 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800949 mDeviceIdle = true;
Dianne Hackborn58e0eef2010-09-16 01:22:10 -0700950 reportStartWorkSource();
Irfan Sheriff4f5f7c92010-10-14 17:01:27 -0700951 updateWifiState();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800952 } else if (action.equals(Intent.ACTION_BATTERY_CHANGED)) {
953 /*
954 * Set a timer to put Wi-Fi to sleep, but only if the screen is off
955 * AND we are transitioning from a state in which the device was supposed
956 * to stay awake to a state in which it is not supposed to stay awake.
957 * If "stay awake" state is not changing, we do nothing, to avoid resetting
958 * the already-set timer.
959 */
960 int pluggedType = intent.getIntExtra("plugged", 0);
Joe Onorato431bb222010-10-18 19:13:23 -0400961 if (DBG) {
962 Slog.d(TAG, "ACTION_BATTERY_CHANGED pluggedType: " + pluggedType);
963 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800964 if (mScreenOff && shouldWifiStayAwake(stayAwakeConditions, mPluggedType) &&
965 !shouldWifiStayAwake(stayAwakeConditions, pluggedType)) {
966 long triggerTime = System.currentTimeMillis() + idleMillis;
Joe Onorato431bb222010-10-18 19:13:23 -0400967 if (DBG) {
968 Slog.d(TAG, "setting ACTION_DEVICE_IDLE timer for " + idleMillis + "ms");
969 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800970 mAlarmManager.set(AlarmManager.RTC_WAKEUP, triggerTime, mIdleIntent);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800971 }
972 mPluggedType = pluggedType;
Irfan Sheriff65eaec82011-01-05 22:00:16 -0800973 } else if (action.equals(BluetoothAdapter.ACTION_CONNECTION_STATE_CHANGED)) {
974 int state = intent.getIntExtra(BluetoothAdapter.EXTRA_CONNECTION_STATE,
975 BluetoothAdapter.STATE_DISCONNECTED);
976 mWifiStateMachine.sendBluetoothAdapterStateChange(state);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800977 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800978 }
979
980 /**
981 * Determines whether the Wi-Fi chipset should stay awake or be put to
982 * sleep. Looks at the setting for the sleep policy and the current
983 * conditions.
Jaikumar Ganesh084c6652009-12-07 10:58:18 -0800984 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800985 * @see #shouldDeviceStayAwake(int, int)
986 */
987 private boolean shouldWifiStayAwake(int stayAwakeConditions, int pluggedType) {
Irfan Sheriff739f6bc2011-01-28 16:43:12 -0800988 //Never sleep as long as the user has not changed the settings
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800989 int wifiSleepPolicy = Settings.System.getInt(mContext.getContentResolver(),
Irfan Sheriff96b10d62011-01-11 15:40:35 -0800990 Settings.System.WIFI_SLEEP_POLICY,
Irfan Sheriff739f6bc2011-01-28 16:43:12 -0800991 Settings.System.WIFI_SLEEP_POLICY_NEVER);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800992
993 if (wifiSleepPolicy == Settings.System.WIFI_SLEEP_POLICY_NEVER) {
994 // Never sleep
995 return true;
996 } else if ((wifiSleepPolicy == Settings.System.WIFI_SLEEP_POLICY_NEVER_WHILE_PLUGGED) &&
997 (pluggedType != 0)) {
998 // Never sleep while plugged, and we're plugged
999 return true;
1000 } else {
1001 // Default
1002 return shouldDeviceStayAwake(stayAwakeConditions, pluggedType);
1003 }
1004 }
Jaikumar Ganesh084c6652009-12-07 10:58:18 -08001005
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001006 /**
1007 * Determine whether the bit value corresponding to {@code pluggedType} is set in
1008 * the bit string {@code stayAwakeConditions}. Because a {@code pluggedType} value
1009 * of {@code 0} isn't really a plugged type, but rather an indication that the
1010 * device isn't plugged in at all, there is no bit value corresponding to a
1011 * {@code pluggedType} value of {@code 0}. That is why we shift by
Ben Dodson4e8620f2010-08-25 10:55:47 -07001012 * {@code pluggedType - 1} instead of by {@code pluggedType}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001013 * @param stayAwakeConditions a bit string specifying which "plugged types" should
1014 * keep the device (and hence Wi-Fi) awake.
1015 * @param pluggedType the type of plug (USB, AC, or none) for which the check is
1016 * being made
1017 * @return {@code true} if {@code pluggedType} indicates that the device is
1018 * supposed to stay awake, {@code false} otherwise.
1019 */
1020 private boolean shouldDeviceStayAwake(int stayAwakeConditions, int pluggedType) {
1021 return (stayAwakeConditions & pluggedType) != 0;
1022 }
1023 };
1024
Dianne Hackborn03f3cb02010-09-17 23:12:26 -07001025 private synchronized void reportStartWorkSource() {
1026 mTmpWorkSource.clear();
1027 if (mDeviceIdle) {
1028 for (int i=0; i<mLocks.mList.size(); i++) {
1029 mTmpWorkSource.add(mLocks.mList.get(i).mWorkSource);
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07001030 }
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07001031 }
Dianne Hackborn03f3cb02010-09-17 23:12:26 -07001032 mWifiStateMachine.updateBatteryWorkSource(mTmpWorkSource);
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07001033 }
Jaikumar Ganesh7440fc22010-09-27 17:04:14 -07001034
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001035 private void updateWifiState() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001036 boolean lockHeld = mLocks.hasLocks();
Irfan Sheriff5876a422010-08-12 20:26:23 -07001037 int strongestLockMode = WifiManager.WIFI_MODE_FULL;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001038 boolean wifiShouldBeStarted = !mDeviceIdle || lockHeld;
Irfan Sheriff5876a422010-08-12 20:26:23 -07001039
1040 if (lockHeld) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001041 strongestLockMode = mLocks.getStrongestLockMode();
Irfan Sheriff5876a422010-08-12 20:26:23 -07001042 }
1043 /* If device is not idle, lockmode cannot be scan only */
1044 if (!mDeviceIdle && strongestLockMode == WifiManager.WIFI_MODE_SCAN_ONLY) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001045 strongestLockMode = WifiManager.WIFI_MODE_FULL;
1046 }
1047
Irfan Sheriffa2a1b912010-06-07 09:03:04 -07001048 /* Disable tethering when airplane mode is enabled */
Irfan Sheriff658772f2011-03-08 14:52:31 -08001049 if (mAirplaneModeOn.get()) {
Irfan Sheriff0d255342010-07-28 09:35:20 -07001050 mWifiStateMachine.setWifiApEnabled(null, false);
Irfan Sheriffa2a1b912010-06-07 09:03:04 -07001051 }
Irfan Sheriffb2e6c012010-04-05 11:57:56 -07001052
Irfan Sheriff658772f2011-03-08 14:52:31 -08001053 if (shouldWifiBeEnabled()) {
Irfan Sheriffa2a1b912010-06-07 09:03:04 -07001054 if (wifiShouldBeStarted) {
Dianne Hackborn03f3cb02010-09-17 23:12:26 -07001055 reportStartWorkSource();
Irfan Sheriff0d255342010-07-28 09:35:20 -07001056 mWifiStateMachine.setWifiEnabled(true);
1057 mWifiStateMachine.setScanOnlyMode(
Irfan Sheriffa2a1b912010-06-07 09:03:04 -07001058 strongestLockMode == WifiManager.WIFI_MODE_SCAN_ONLY);
Irfan Sheriff0d255342010-07-28 09:35:20 -07001059 mWifiStateMachine.setDriverStart(true);
Irfan Sheriff5876a422010-08-12 20:26:23 -07001060 mWifiStateMachine.setHighPerfModeEnabled(strongestLockMode
1061 == WifiManager.WIFI_MODE_FULL_HIGH_PERF);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001062 } else {
Irfan Sheriff0d255342010-07-28 09:35:20 -07001063 mWifiStateMachine.requestCmWakeLock();
1064 mWifiStateMachine.setDriverStart(false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001065 }
Irfan Sheriffa2a1b912010-06-07 09:03:04 -07001066 } else {
Irfan Sheriff0d255342010-07-28 09:35:20 -07001067 mWifiStateMachine.setWifiEnabled(false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001068 }
1069 }
1070
1071 private void registerForBroadcasts() {
1072 IntentFilter intentFilter = new IntentFilter();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001073 intentFilter.addAction(Intent.ACTION_SCREEN_ON);
1074 intentFilter.addAction(Intent.ACTION_SCREEN_OFF);
1075 intentFilter.addAction(Intent.ACTION_BATTERY_CHANGED);
1076 intentFilter.addAction(ACTION_DEVICE_IDLE);
Irfan Sheriff65eaec82011-01-05 22:00:16 -08001077 intentFilter.addAction(BluetoothAdapter.ACTION_CONNECTION_STATE_CHANGED);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001078 mContext.registerReceiver(mReceiver, intentFilter);
1079 }
Jaikumar Ganesh084c6652009-12-07 10:58:18 -08001080
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001081 private boolean isAirplaneSensitive() {
1082 String airplaneModeRadios = Settings.System.getString(mContext.getContentResolver(),
1083 Settings.System.AIRPLANE_MODE_RADIOS);
1084 return airplaneModeRadios == null
1085 || airplaneModeRadios.contains(Settings.System.RADIO_WIFI);
1086 }
1087
Mike Lockwoodbd5ddf02009-07-29 21:37:14 -07001088 private boolean isAirplaneToggleable() {
1089 String toggleableRadios = Settings.System.getString(mContext.getContentResolver(),
1090 Settings.System.AIRPLANE_MODE_TOGGLEABLE_RADIOS);
1091 return toggleableRadios != null
1092 && toggleableRadios.contains(Settings.System.RADIO_WIFI);
1093 }
1094
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001095 /**
1096 * Returns true if Wi-Fi is sensitive to airplane mode, and airplane mode is
1097 * currently on.
1098 * @return {@code true} if airplane mode is on.
1099 */
1100 private boolean isAirplaneModeOn() {
1101 return isAirplaneSensitive() && Settings.System.getInt(mContext.getContentResolver(),
1102 Settings.System.AIRPLANE_MODE_ON, 0) == 1;
1103 }
1104
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001105 @Override
1106 protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
1107 if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.DUMP)
1108 != PackageManager.PERMISSION_GRANTED) {
1109 pw.println("Permission Denial: can't dump WifiService from from pid="
1110 + Binder.getCallingPid()
1111 + ", uid=" + Binder.getCallingUid());
1112 return;
1113 }
Irfan Sheriffd8134ff2010-08-22 17:06:34 -07001114 pw.println("Wi-Fi is " + mWifiStateMachine.syncGetWifiStateByName());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001115 pw.println("Stay-awake conditions: " +
1116 Settings.System.getInt(mContext.getContentResolver(),
1117 Settings.System.STAY_ON_WHILE_PLUGGED_IN, 0));
1118 pw.println();
1119
1120 pw.println("Internal state:");
Irfan Sheriff0d255342010-07-28 09:35:20 -07001121 pw.println(mWifiStateMachine);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001122 pw.println();
1123 pw.println("Latest scan results:");
Irfan Sheriffd8134ff2010-08-22 17:06:34 -07001124 List<ScanResult> scanResults = mWifiStateMachine.syncGetScanResultsList();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001125 if (scanResults != null && scanResults.size() != 0) {
1126 pw.println(" BSSID Frequency RSSI Flags SSID");
1127 for (ScanResult r : scanResults) {
1128 pw.printf(" %17s %9d %5d %-16s %s%n",
1129 r.BSSID,
1130 r.frequency,
1131 r.level,
1132 r.capabilities,
1133 r.SSID == null ? "" : r.SSID);
1134 }
1135 }
1136 pw.println();
Eric Shienbrood5711fad2009-03-27 20:25:31 -07001137 pw.println("Locks acquired: " + mFullLocksAcquired + " full, " +
Irfan Sheriff5876a422010-08-12 20:26:23 -07001138 mFullHighPerfLocksAcquired + " full high perf, " +
Eric Shienbrood5711fad2009-03-27 20:25:31 -07001139 mScanLocksAcquired + " scan");
1140 pw.println("Locks released: " + mFullLocksReleased + " full, " +
Irfan Sheriff5876a422010-08-12 20:26:23 -07001141 mFullHighPerfLocksReleased + " full high perf, " +
Eric Shienbrood5711fad2009-03-27 20:25:31 -07001142 mScanLocksReleased + " scan");
1143 pw.println();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001144 pw.println("Locks held:");
1145 mLocks.dump(pw);
1146 }
1147
Robert Greenwalt58ff0212009-05-19 15:53:54 -07001148 private class WifiLock extends DeathRecipient {
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -07001149 WifiLock(int lockMode, String tag, IBinder binder, WorkSource ws) {
1150 super(lockMode, tag, binder, ws);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001151 }
1152
1153 public void binderDied() {
1154 synchronized (mLocks) {
1155 releaseWifiLockLocked(mBinder);
1156 }
1157 }
1158
1159 public String toString() {
Robert Greenwalt5347bd42009-05-13 15:10:16 -07001160 return "WifiLock{" + mTag + " type=" + mMode + " binder=" + mBinder + "}";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001161 }
1162 }
1163
1164 private class LockList {
1165 private List<WifiLock> mList;
1166
1167 private LockList() {
1168 mList = new ArrayList<WifiLock>();
1169 }
1170
1171 private synchronized boolean hasLocks() {
1172 return !mList.isEmpty();
1173 }
1174
1175 private synchronized int getStrongestLockMode() {
1176 if (mList.isEmpty()) {
1177 return WifiManager.WIFI_MODE_FULL;
1178 }
Irfan Sheriff5876a422010-08-12 20:26:23 -07001179
1180 if (mFullHighPerfLocksAcquired > mFullHighPerfLocksReleased) {
1181 return WifiManager.WIFI_MODE_FULL_HIGH_PERF;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001182 }
Irfan Sheriff5876a422010-08-12 20:26:23 -07001183
1184 if (mFullLocksAcquired > mFullLocksReleased) {
1185 return WifiManager.WIFI_MODE_FULL;
1186 }
1187
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001188 return WifiManager.WIFI_MODE_SCAN_ONLY;
1189 }
1190
1191 private void addLock(WifiLock lock) {
1192 if (findLockByBinder(lock.mBinder) < 0) {
1193 mList.add(lock);
1194 }
1195 }
1196
1197 private WifiLock removeLock(IBinder binder) {
1198 int index = findLockByBinder(binder);
1199 if (index >= 0) {
Suchi Amalapurapufff2fda2009-06-30 21:36:16 -07001200 WifiLock ret = mList.remove(index);
1201 ret.unlinkDeathRecipient();
1202 return ret;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001203 } else {
1204 return null;
1205 }
1206 }
1207
1208 private int findLockByBinder(IBinder binder) {
1209 int size = mList.size();
1210 for (int i = size - 1; i >= 0; i--)
1211 if (mList.get(i).mBinder == binder)
1212 return i;
1213 return -1;
1214 }
1215
1216 private void dump(PrintWriter pw) {
1217 for (WifiLock l : mList) {
1218 pw.print(" ");
1219 pw.println(l);
1220 }
1221 }
1222 }
1223
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -07001224 void enforceWakeSourcePermission(int uid, int pid) {
Dianne Hackborne746f032010-09-13 16:02:57 -07001225 if (uid == android.os.Process.myUid()) {
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -07001226 return;
1227 }
1228 mContext.enforcePermission(android.Manifest.permission.UPDATE_DEVICE_STATS,
1229 pid, uid, null);
1230 }
1231
1232 public boolean acquireWifiLock(IBinder binder, int lockMode, String tag, WorkSource ws) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001233 mContext.enforceCallingOrSelfPermission(android.Manifest.permission.WAKE_LOCK, null);
Irfan Sheriff5876a422010-08-12 20:26:23 -07001234 if (lockMode != WifiManager.WIFI_MODE_FULL &&
1235 lockMode != WifiManager.WIFI_MODE_SCAN_ONLY &&
1236 lockMode != WifiManager.WIFI_MODE_FULL_HIGH_PERF) {
1237 Slog.e(TAG, "Illegal argument, lockMode= " + lockMode);
1238 if (DBG) throw new IllegalArgumentException("lockMode=" + lockMode);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001239 return false;
1240 }
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -07001241 if (ws != null && ws.size() == 0) {
1242 ws = null;
1243 }
Dianne Hackbornecfd7f72010-10-08 14:23:40 -07001244 if (ws != null) {
1245 enforceWakeSourcePermission(Binder.getCallingUid(), Binder.getCallingPid());
1246 }
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -07001247 if (ws == null) {
1248 ws = new WorkSource(Binder.getCallingUid());
1249 }
1250 WifiLock wifiLock = new WifiLock(lockMode, tag, binder, ws);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001251 synchronized (mLocks) {
1252 return acquireWifiLockLocked(wifiLock);
1253 }
1254 }
1255
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -07001256 private void noteAcquireWifiLock(WifiLock wifiLock) throws RemoteException {
1257 switch(wifiLock.mMode) {
1258 case WifiManager.WIFI_MODE_FULL:
Irfan Sheriff5876a422010-08-12 20:26:23 -07001259 case WifiManager.WIFI_MODE_FULL_HIGH_PERF:
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -07001260 mBatteryStats.noteFullWifiLockAcquiredFromSource(wifiLock.mWorkSource);
1261 break;
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -07001262 case WifiManager.WIFI_MODE_SCAN_ONLY:
1263 mBatteryStats.noteScanWifiLockAcquiredFromSource(wifiLock.mWorkSource);
1264 break;
1265 }
1266 }
1267
1268 private void noteReleaseWifiLock(WifiLock wifiLock) throws RemoteException {
1269 switch(wifiLock.mMode) {
1270 case WifiManager.WIFI_MODE_FULL:
Irfan Sheriff5876a422010-08-12 20:26:23 -07001271 case WifiManager.WIFI_MODE_FULL_HIGH_PERF:
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -07001272 mBatteryStats.noteFullWifiLockReleasedFromSource(wifiLock.mWorkSource);
1273 break;
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -07001274 case WifiManager.WIFI_MODE_SCAN_ONLY:
1275 mBatteryStats.noteScanWifiLockReleasedFromSource(wifiLock.mWorkSource);
1276 break;
1277 }
1278 }
1279
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001280 private boolean acquireWifiLockLocked(WifiLock wifiLock) {
Irfan Sheriffc89dd542010-09-28 08:40:54 -07001281 if (DBG) Slog.d(TAG, "acquireWifiLockLocked: " + wifiLock);
Robert Greenwaltf1acb2d2009-10-13 08:20:55 -07001282
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001283 mLocks.addLock(wifiLock);
Robert Greenwaltf1acb2d2009-10-13 08:20:55 -07001284
The Android Open Source Project10592532009-03-18 17:39:46 -07001285 long ident = Binder.clearCallingIdentity();
1286 try {
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -07001287 noteAcquireWifiLock(wifiLock);
Robert Greenwalt5347bd42009-05-13 15:10:16 -07001288 switch(wifiLock.mMode) {
Eric Shienbrood5711fad2009-03-27 20:25:31 -07001289 case WifiManager.WIFI_MODE_FULL:
1290 ++mFullLocksAcquired;
Eric Shienbrood5711fad2009-03-27 20:25:31 -07001291 break;
Irfan Sheriff5876a422010-08-12 20:26:23 -07001292 case WifiManager.WIFI_MODE_FULL_HIGH_PERF:
1293 ++mFullHighPerfLocksAcquired;
1294 break;
1295
Eric Shienbrood5711fad2009-03-27 20:25:31 -07001296 case WifiManager.WIFI_MODE_SCAN_ONLY:
1297 ++mScanLocksAcquired;
Eric Shienbrood5711fad2009-03-27 20:25:31 -07001298 break;
The Android Open Source Project10592532009-03-18 17:39:46 -07001299 }
Dianne Hackbornecfd7f72010-10-08 14:23:40 -07001300
1301 // Be aggressive about adding new locks into the accounted state...
1302 // we want to over-report rather than under-report.
1303 reportStartWorkSource();
1304
1305 updateWifiState();
1306 return true;
The Android Open Source Project10592532009-03-18 17:39:46 -07001307 } catch (RemoteException e) {
Dianne Hackbornecfd7f72010-10-08 14:23:40 -07001308 return false;
The Android Open Source Project10592532009-03-18 17:39:46 -07001309 } finally {
1310 Binder.restoreCallingIdentity(ident);
1311 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001312 }
1313
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -07001314 public void updateWifiLockWorkSource(IBinder lock, WorkSource ws) {
1315 int uid = Binder.getCallingUid();
1316 int pid = Binder.getCallingPid();
1317 if (ws != null && ws.size() == 0) {
1318 ws = null;
1319 }
1320 if (ws != null) {
1321 enforceWakeSourcePermission(uid, pid);
1322 }
1323 long ident = Binder.clearCallingIdentity();
1324 try {
1325 synchronized (mLocks) {
1326 int index = mLocks.findLockByBinder(lock);
1327 if (index < 0) {
1328 throw new IllegalArgumentException("Wifi lock not active");
1329 }
1330 WifiLock wl = mLocks.mList.get(index);
1331 noteReleaseWifiLock(wl);
1332 wl.mWorkSource = ws != null ? new WorkSource(ws) : new WorkSource(uid);
1333 noteAcquireWifiLock(wl);
1334 }
1335 } catch (RemoteException e) {
1336 } finally {
1337 Binder.restoreCallingIdentity(ident);
1338 }
1339 }
1340
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001341 public boolean releaseWifiLock(IBinder lock) {
1342 mContext.enforceCallingOrSelfPermission(android.Manifest.permission.WAKE_LOCK, null);
1343 synchronized (mLocks) {
1344 return releaseWifiLockLocked(lock);
1345 }
1346 }
1347
1348 private boolean releaseWifiLockLocked(IBinder lock) {
Eric Shienbroodd4c5f892009-03-24 18:13:20 -07001349 boolean hadLock;
Robert Greenwaltf1acb2d2009-10-13 08:20:55 -07001350
The Android Open Source Project10592532009-03-18 17:39:46 -07001351 WifiLock wifiLock = mLocks.removeLock(lock);
Robert Greenwaltf1acb2d2009-10-13 08:20:55 -07001352
Irfan Sheriffc89dd542010-09-28 08:40:54 -07001353 if (DBG) Slog.d(TAG, "releaseWifiLockLocked: " + wifiLock);
Robert Greenwaltf1acb2d2009-10-13 08:20:55 -07001354
Eric Shienbroodd4c5f892009-03-24 18:13:20 -07001355 hadLock = (wifiLock != null);
1356
Dianne Hackbornecfd7f72010-10-08 14:23:40 -07001357 long ident = Binder.clearCallingIdentity();
1358 try {
1359 if (hadLock) {
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -07001360 noteAcquireWifiLock(wifiLock);
Robert Greenwalt5347bd42009-05-13 15:10:16 -07001361 switch(wifiLock.mMode) {
Eric Shienbrood5711fad2009-03-27 20:25:31 -07001362 case WifiManager.WIFI_MODE_FULL:
1363 ++mFullLocksReleased;
Eric Shienbrood5711fad2009-03-27 20:25:31 -07001364 break;
Irfan Sheriff5876a422010-08-12 20:26:23 -07001365 case WifiManager.WIFI_MODE_FULL_HIGH_PERF:
1366 ++mFullHighPerfLocksReleased;
1367 break;
Eric Shienbrood5711fad2009-03-27 20:25:31 -07001368 case WifiManager.WIFI_MODE_SCAN_ONLY:
1369 ++mScanLocksReleased;
Eric Shienbrood5711fad2009-03-27 20:25:31 -07001370 break;
Eric Shienbroodd4c5f892009-03-24 18:13:20 -07001371 }
The Android Open Source Project10592532009-03-18 17:39:46 -07001372 }
Dianne Hackbornecfd7f72010-10-08 14:23:40 -07001373
1374 // TODO - should this only happen if you hadLock?
1375 updateWifiState();
1376
1377 } catch (RemoteException e) {
1378 } finally {
1379 Binder.restoreCallingIdentity(ident);
The Android Open Source Project10592532009-03-18 17:39:46 -07001380 }
Dianne Hackbornecfd7f72010-10-08 14:23:40 -07001381
Eric Shienbroodd4c5f892009-03-24 18:13:20 -07001382 return hadLock;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001383 }
Robert Greenwalt5347bd42009-05-13 15:10:16 -07001384
Robert Greenwalt58ff0212009-05-19 15:53:54 -07001385 private abstract class DeathRecipient
Robert Greenwalt5347bd42009-05-13 15:10:16 -07001386 implements IBinder.DeathRecipient {
1387 String mTag;
1388 int mMode;
1389 IBinder mBinder;
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -07001390 WorkSource mWorkSource;
Robert Greenwalt5347bd42009-05-13 15:10:16 -07001391
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -07001392 DeathRecipient(int mode, String tag, IBinder binder, WorkSource ws) {
Robert Greenwalt5347bd42009-05-13 15:10:16 -07001393 super();
1394 mTag = tag;
1395 mMode = mode;
1396 mBinder = binder;
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -07001397 mWorkSource = ws;
Robert Greenwalt5347bd42009-05-13 15:10:16 -07001398 try {
1399 mBinder.linkToDeath(this, 0);
1400 } catch (RemoteException e) {
1401 binderDied();
1402 }
1403 }
Suchi Amalapurapufff2fda2009-06-30 21:36:16 -07001404
1405 void unlinkDeathRecipient() {
1406 mBinder.unlinkToDeath(this, 0);
1407 }
Robert Greenwalt5347bd42009-05-13 15:10:16 -07001408 }
1409
Robert Greenwalt58ff0212009-05-19 15:53:54 -07001410 private class Multicaster extends DeathRecipient {
1411 Multicaster(String tag, IBinder binder) {
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -07001412 super(Binder.getCallingUid(), tag, binder, null);
Robert Greenwalt5347bd42009-05-13 15:10:16 -07001413 }
1414
1415 public void binderDied() {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001416 Slog.e(TAG, "Multicaster binderDied");
Robert Greenwalt5347bd42009-05-13 15:10:16 -07001417 synchronized (mMulticasters) {
1418 int i = mMulticasters.indexOf(this);
1419 if (i != -1) {
1420 removeMulticasterLocked(i, mMode);
1421 }
1422 }
1423 }
1424
1425 public String toString() {
Robert Greenwalt58ff0212009-05-19 15:53:54 -07001426 return "Multicaster{" + mTag + " binder=" + mBinder + "}";
Robert Greenwalt5347bd42009-05-13 15:10:16 -07001427 }
1428
1429 public int getUid() {
1430 return mMode;
1431 }
1432 }
1433
Robert Greenwalte2d155a2009-10-21 14:58:34 -07001434 public void initializeMulticastFiltering() {
1435 enforceMulticastChangePermission();
Irfan Sheriffa8fbe1f2010-03-09 09:13:58 -08001436
Robert Greenwalte2d155a2009-10-21 14:58:34 -07001437 synchronized (mMulticasters) {
1438 // if anybody had requested filters be off, leave off
1439 if (mMulticasters.size() != 0) {
1440 return;
1441 } else {
Irfan Sheriff0d255342010-07-28 09:35:20 -07001442 mWifiStateMachine.startPacketFiltering();
Robert Greenwalte2d155a2009-10-21 14:58:34 -07001443 }
1444 }
1445 }
1446
Robert Greenwaltfc1b15c2009-05-22 15:09:51 -07001447 public void acquireMulticastLock(IBinder binder, String tag) {
1448 enforceMulticastChangePermission();
Robert Greenwalt5347bd42009-05-13 15:10:16 -07001449
1450 synchronized (mMulticasters) {
1451 mMulticastEnabled++;
Robert Greenwalt58ff0212009-05-19 15:53:54 -07001452 mMulticasters.add(new Multicaster(tag, binder));
Robert Greenwalt5347bd42009-05-13 15:10:16 -07001453 // Note that we could call stopPacketFiltering only when
1454 // our new size == 1 (first call), but this function won't
1455 // be called often and by making the stopPacket call each
1456 // time we're less fragile and self-healing.
Irfan Sheriff0d255342010-07-28 09:35:20 -07001457 mWifiStateMachine.stopPacketFiltering();
Robert Greenwalt5347bd42009-05-13 15:10:16 -07001458 }
1459
1460 int uid = Binder.getCallingUid();
1461 Long ident = Binder.clearCallingIdentity();
1462 try {
1463 mBatteryStats.noteWifiMulticastEnabled(uid);
1464 } catch (RemoteException e) {
1465 } finally {
1466 Binder.restoreCallingIdentity(ident);
1467 }
1468 }
1469
Robert Greenwaltfc1b15c2009-05-22 15:09:51 -07001470 public void releaseMulticastLock() {
1471 enforceMulticastChangePermission();
Robert Greenwalt5347bd42009-05-13 15:10:16 -07001472
1473 int uid = Binder.getCallingUid();
1474 synchronized (mMulticasters) {
1475 mMulticastDisabled++;
1476 int size = mMulticasters.size();
1477 for (int i = size - 1; i >= 0; i--) {
Robert Greenwalt58ff0212009-05-19 15:53:54 -07001478 Multicaster m = mMulticasters.get(i);
Robert Greenwalt5347bd42009-05-13 15:10:16 -07001479 if ((m != null) && (m.getUid() == uid)) {
1480 removeMulticasterLocked(i, uid);
1481 }
1482 }
1483 }
1484 }
1485
1486 private void removeMulticasterLocked(int i, int uid)
1487 {
Suchi Amalapurapufff2fda2009-06-30 21:36:16 -07001488 Multicaster removed = mMulticasters.remove(i);
Irfan Sheriffa8fbe1f2010-03-09 09:13:58 -08001489
Suchi Amalapurapufff2fda2009-06-30 21:36:16 -07001490 if (removed != null) {
1491 removed.unlinkDeathRecipient();
1492 }
Robert Greenwalt5347bd42009-05-13 15:10:16 -07001493 if (mMulticasters.size() == 0) {
Irfan Sheriff0d255342010-07-28 09:35:20 -07001494 mWifiStateMachine.startPacketFiltering();
Robert Greenwalt5347bd42009-05-13 15:10:16 -07001495 }
1496
1497 Long ident = Binder.clearCallingIdentity();
1498 try {
1499 mBatteryStats.noteWifiMulticastDisabled(uid);
1500 } catch (RemoteException e) {
1501 } finally {
1502 Binder.restoreCallingIdentity(ident);
1503 }
1504 }
1505
Robert Greenwalt58ff0212009-05-19 15:53:54 -07001506 public boolean isMulticastEnabled() {
Robert Greenwalt5347bd42009-05-13 15:10:16 -07001507 enforceAccessPermission();
1508
1509 synchronized (mMulticasters) {
1510 return (mMulticasters.size() > 0);
1511 }
1512 }
Irfan Sheriff0d255342010-07-28 09:35:20 -07001513
Irfan Sheriff227bec42011-02-15 19:30:27 -08001514 /**
1515 * Evaluate if traffic stats polling is needed based on
1516 * connection and screen on status
1517 */
1518 private void evaluateTrafficStatsPolling() {
1519 Message msg;
1520 if (mNetworkInfo.getDetailedState() == DetailedState.CONNECTED && !mScreenOff) {
Irfan Sheriffebe606f2011-02-24 11:39:15 -08001521 msg = Message.obtain(mAsyncServiceHandler,
1522 WifiManager.CMD_ENABLE_TRAFFIC_STATS_POLL, 1, 0);
Irfan Sheriff227bec42011-02-15 19:30:27 -08001523 } else {
Irfan Sheriffebe606f2011-02-24 11:39:15 -08001524 msg = Message.obtain(mAsyncServiceHandler,
1525 WifiManager.CMD_ENABLE_TRAFFIC_STATS_POLL, 0, 0);
Irfan Sheriff227bec42011-02-15 19:30:27 -08001526 }
1527 msg.sendToTarget();
1528 }
1529
1530 private void notifyOnDataActivity() {
1531 long sent, received;
1532 long preTxPkts = mTxPkts, preRxPkts = mRxPkts;
1533 int dataActivity = WifiManager.DATA_ACTIVITY_NONE;
1534
1535 mTxPkts = TrafficStats.getTxPackets(mInterfaceName);
1536 mRxPkts = TrafficStats.getRxPackets(mInterfaceName);
1537
1538 if (preTxPkts > 0 || preRxPkts > 0) {
1539 sent = mTxPkts - preTxPkts;
1540 received = mRxPkts - preRxPkts;
1541 if (sent > 0) {
1542 dataActivity |= WifiManager.DATA_ACTIVITY_OUT;
1543 }
1544 if (received > 0) {
1545 dataActivity |= WifiManager.DATA_ACTIVITY_IN;
1546 }
1547
1548 if (dataActivity != mDataActivity && !mScreenOff) {
1549 mDataActivity = dataActivity;
1550 for (AsyncChannel client : mClients) {
1551 client.sendMessage(WifiManager.DATA_ACTIVITY_NOTIFICATION, mDataActivity);
1552 }
1553 }
1554 }
1555 }
1556
1557
Irfan Sheriff0d255342010-07-28 09:35:20 -07001558 private void checkAndSetNotification() {
1559 // If we shouldn't place a notification on available networks, then
1560 // don't bother doing any of the following
1561 if (!mNotificationEnabled) return;
1562
1563 State state = mNetworkInfo.getState();
1564 if ((state == NetworkInfo.State.DISCONNECTED)
1565 || (state == NetworkInfo.State.UNKNOWN)) {
1566 // Look for an open network
Irfan Sheriffd8134ff2010-08-22 17:06:34 -07001567 List<ScanResult> scanResults = mWifiStateMachine.syncGetScanResultsList();
Irfan Sheriff0d255342010-07-28 09:35:20 -07001568 if (scanResults != null) {
1569 int numOpenNetworks = 0;
1570 for (int i = scanResults.size() - 1; i >= 0; i--) {
1571 ScanResult scanResult = scanResults.get(i);
1572
1573 if (TextUtils.isEmpty(scanResult.capabilities)) {
1574 numOpenNetworks++;
1575 }
1576 }
1577
1578 if (numOpenNetworks > 0) {
1579 if (++mNumScansSinceNetworkStateChange >= NUM_SCANS_BEFORE_ACTUALLY_SCANNING) {
1580 /*
1581 * We've scanned continuously at least
1582 * NUM_SCANS_BEFORE_NOTIFICATION times. The user
1583 * probably does not have a remembered network in range,
1584 * since otherwise supplicant would have tried to
1585 * associate and thus resetting this counter.
1586 */
1587 setNotificationVisible(true, numOpenNetworks, false, 0);
1588 }
1589 return;
1590 }
1591 }
1592 }
1593
1594 // No open networks in range, remove the notification
1595 setNotificationVisible(false, 0, false, 0);
1596 }
1597
1598 /**
1599 * Clears variables related to tracking whether a notification has been
1600 * shown recently and clears the current notification.
1601 */
1602 private void resetNotification() {
1603 mNotificationRepeatTime = 0;
1604 mNumScansSinceNetworkStateChange = 0;
1605 setNotificationVisible(false, 0, false, 0);
1606 }
1607
1608 /**
1609 * Display or don't display a notification that there are open Wi-Fi networks.
1610 * @param visible {@code true} if notification should be visible, {@code false} otherwise
1611 * @param numNetworks the number networks seen
1612 * @param force {@code true} to force notification to be shown/not-shown,
1613 * even if it is already shown/not-shown.
1614 * @param delay time in milliseconds after which the notification should be made
1615 * visible or invisible.
1616 */
1617 private void setNotificationVisible(boolean visible, int numNetworks, boolean force,
1618 int delay) {
1619
1620 // Since we use auto cancel on the notification, when the
1621 // mNetworksAvailableNotificationShown is true, the notification may
1622 // have actually been canceled. However, when it is false we know
1623 // for sure that it is not being shown (it will not be shown any other
1624 // place than here)
1625
1626 // If it should be hidden and it is already hidden, then noop
1627 if (!visible && !mNotificationShown && !force) {
1628 return;
1629 }
1630
1631 NotificationManager notificationManager = (NotificationManager) mContext
1632 .getSystemService(Context.NOTIFICATION_SERVICE);
1633
1634 Message message;
1635 if (visible) {
1636
1637 // Not enough time has passed to show the notification again
1638 if (System.currentTimeMillis() < mNotificationRepeatTime) {
1639 return;
1640 }
1641
1642 if (mNotification == null) {
Wink Savillec7a98342010-08-13 16:11:42 -07001643 // Cache the Notification object.
Irfan Sheriff0d255342010-07-28 09:35:20 -07001644 mNotification = new Notification();
1645 mNotification.when = 0;
1646 mNotification.icon = ICON_NETWORKS_AVAILABLE;
1647 mNotification.flags = Notification.FLAG_AUTO_CANCEL;
1648 mNotification.contentIntent = PendingIntent.getActivity(mContext, 0,
1649 new Intent(WifiManager.ACTION_PICK_WIFI_NETWORK), 0);
1650 }
1651
1652 CharSequence title = mContext.getResources().getQuantityText(
1653 com.android.internal.R.plurals.wifi_available, numNetworks);
1654 CharSequence details = mContext.getResources().getQuantityText(
1655 com.android.internal.R.plurals.wifi_available_detailed, numNetworks);
1656 mNotification.tickerText = title;
1657 mNotification.setLatestEventInfo(mContext, title, details, mNotification.contentIntent);
1658
1659 mNotificationRepeatTime = System.currentTimeMillis() + NOTIFICATION_REPEAT_DELAY_MS;
1660
1661 notificationManager.notify(ICON_NETWORKS_AVAILABLE, mNotification);
Irfan Sheriff0d255342010-07-28 09:35:20 -07001662 } else {
Irfan Sheriff0d255342010-07-28 09:35:20 -07001663 notificationManager.cancel(ICON_NETWORKS_AVAILABLE);
Irfan Sheriff0d255342010-07-28 09:35:20 -07001664 }
1665
Irfan Sheriff0d255342010-07-28 09:35:20 -07001666 mNotificationShown = visible;
1667 }
1668
1669 private class NotificationEnabledSettingObserver extends ContentObserver {
1670
1671 public NotificationEnabledSettingObserver(Handler handler) {
1672 super(handler);
1673 }
1674
1675 public void register() {
1676 ContentResolver cr = mContext.getContentResolver();
1677 cr.registerContentObserver(Settings.Secure.getUriFor(
1678 Settings.Secure.WIFI_NETWORKS_AVAILABLE_NOTIFICATION_ON), true, this);
1679 mNotificationEnabled = getValue();
1680 }
1681
1682 @Override
1683 public void onChange(boolean selfChange) {
1684 super.onChange(selfChange);
1685
1686 mNotificationEnabled = getValue();
1687 resetNotification();
1688 }
1689
1690 private boolean getValue() {
1691 return Settings.Secure.getInt(mContext.getContentResolver(),
1692 Settings.Secure.WIFI_NETWORKS_AVAILABLE_NOTIFICATION_ON, 1) == 1;
1693 }
1694 }
1695
1696
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001697}