blob: adc49aed9fa1f804a5b1ace2593a94000acc5d55 [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;
32import android.net.wifi.WifiInfo;
33import android.net.wifi.WifiManager;
Irfan Sheriff0d255342010-07-28 09:35:20 -070034import android.net.wifi.WifiStateMachine;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080035import android.net.wifi.ScanResult;
36import android.net.wifi.WifiConfiguration;
San Mehat0310f9a2009-07-07 10:49:47 -070037import android.net.wifi.SupplicantState;
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 Sheriffa2a1b912010-06-07 09:03:04 -070065import java.util.concurrent.atomic.AtomicBoolean;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080066import java.io.FileDescriptor;
67import java.io.PrintWriter;
68
The Android Open Source Project10592532009-03-18 17:39:46 -070069import com.android.internal.app.IBatteryStats;
Wink Saville4b7ba092010-10-20 15:37:41 -070070import com.android.internal.util.AsyncChannel;
The Android Open Source Project10592532009-03-18 17:39:46 -070071import com.android.server.am.BatteryStatsService;
Irfan Sheriff9ab518ad2010-03-12 15:48:17 -080072import com.android.internal.R;
The Android Open Source Project10592532009-03-18 17:39:46 -070073
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080074/**
75 * WifiService handles remote WiFi operation requests by implementing
Irfan Sheriffa2a1b912010-06-07 09:03:04 -070076 * the IWifiManager interface.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080077 *
78 * @hide
79 */
Irfan Sheriffa2a1b912010-06-07 09:03:04 -070080//TODO: Clean up multiple locks and implement WifiService
81// as a SM to track soft AP/client/adhoc bring up based
82// on device idle state, airplane mode and boot.
83
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080084public class WifiService extends IWifiManager.Stub {
85 private static final String TAG = "WifiService";
Irfan Sheriffa2a1b912010-06-07 09:03:04 -070086 private static final boolean DBG = true;
87
Irfan Sheriff0d255342010-07-28 09:35:20 -070088 private final WifiStateMachine mWifiStateMachine;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080089
90 private Context mContext;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080091
92 private AlarmManager mAlarmManager;
93 private PendingIntent mIdleIntent;
94 private static final int IDLE_REQUEST = 0;
95 private boolean mScreenOff;
96 private boolean mDeviceIdle;
97 private int mPluggedType;
98
Irfan Sherifffcc08452011-02-17 16:44:54 -080099 /* Chipset supports background scan */
100 private final boolean mBackgroundScanSupported;
101
Mike Lockwoodbd5ddf02009-07-29 21:37:14 -0700102 // true if the user enabled Wifi while in airplane mode
Irfan Sheriffa2a1b912010-06-07 09:03:04 -0700103 private AtomicBoolean mAirplaneModeOverwridden = new AtomicBoolean(false);
Mike Lockwoodbd5ddf02009-07-29 21:37:14 -0700104
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800105 private final LockList mLocks = new LockList();
Eric Shienbrood5711fad2009-03-27 20:25:31 -0700106 // some wifi lock statistics
Irfan Sheriff5876a422010-08-12 20:26:23 -0700107 private int mFullHighPerfLocksAcquired;
108 private int mFullHighPerfLocksReleased;
Eric Shienbrood5711fad2009-03-27 20:25:31 -0700109 private int mFullLocksAcquired;
110 private int mFullLocksReleased;
111 private int mScanLocksAcquired;
112 private int mScanLocksReleased;
The Android Open Source Project10592532009-03-18 17:39:46 -0700113
Robert Greenwalt58ff0212009-05-19 15:53:54 -0700114 private final List<Multicaster> mMulticasters =
115 new ArrayList<Multicaster>();
Robert Greenwalt5347bd42009-05-13 15:10:16 -0700116 private int mMulticastEnabled;
117 private int mMulticastDisabled;
118
The Android Open Source Project10592532009-03-18 17:39:46 -0700119 private final IBatteryStats mBatteryStats;
Jaikumar Ganesh084c6652009-12-07 10:58:18 -0800120
Irfan Sheriff227bec42011-02-15 19:30:27 -0800121 private boolean mEnableTrafficStatsPoll = false;
122 private int mTrafficStatsPollToken = 0;
123 private long mTxPkts;
124 private long mRxPkts;
125 /* Tracks last reported data activity */
126 private int mDataActivity;
127 private String mInterfaceName;
128
129 /**
130 * Interval in milliseconds between polling for traffic
131 * statistics
132 */
133 private static final int POLL_TRAFFIC_STATS_INTERVAL_MSECS = 1000;
134
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800135 /**
Doug Zongker43866e02010-01-07 12:09:54 -0800136 * See {@link Settings.Secure#WIFI_IDLE_MS}. This is the default value if a
137 * Settings.Secure value is not present. This timeout value is chosen as
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800138 * the approximate point at which the battery drain caused by Wi-Fi
139 * being enabled but not active exceeds the battery drain caused by
140 * re-establishing a connection to the mobile data network.
141 */
Irfan Sheriff4f5f7c92010-10-14 17:01:27 -0700142 private static final long DEFAULT_IDLE_MS = 15 * 60 * 1000; /* 15 minutes */
143
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800144 private static final String ACTION_DEVICE_IDLE =
145 "com.android.server.WifiManager.action.DEVICE_IDLE";
146
Irfan Sheriffa2a1b912010-06-07 09:03:04 -0700147 private boolean mIsReceiverRegistered = false;
148
Irfan Sheriff0d255342010-07-28 09:35:20 -0700149
150 NetworkInfo mNetworkInfo = new NetworkInfo(ConnectivityManager.TYPE_WIFI, 0, "WIFI", "");
151
152 // Variables relating to the 'available networks' notification
153 /**
154 * The icon to show in the 'available networks' notification. This will also
155 * be the ID of the Notification given to the NotificationManager.
156 */
157 private static final int ICON_NETWORKS_AVAILABLE =
158 com.android.internal.R.drawable.stat_notify_wifi_in_range;
159 /**
160 * When a notification is shown, we wait this amount before possibly showing it again.
161 */
162 private final long NOTIFICATION_REPEAT_DELAY_MS;
163 /**
164 * Whether the user has set the setting to show the 'available networks' notification.
165 */
166 private boolean mNotificationEnabled;
167 /**
168 * Observes the user setting to keep {@link #mNotificationEnabled} in sync.
169 */
170 private NotificationEnabledSettingObserver mNotificationEnabledSettingObserver;
171 /**
172 * The {@link System#currentTimeMillis()} must be at least this value for us
173 * to show the notification again.
174 */
175 private long mNotificationRepeatTime;
176 /**
177 * The Notification object given to the NotificationManager.
178 */
179 private Notification mNotification;
180 /**
181 * Whether the notification is being shown, as set by us. That is, if the
182 * user cancels the notification, we will not receive the callback so this
183 * will still be true. We only guarantee if this is false, then the
184 * notification is not showing.
185 */
186 private boolean mNotificationShown;
187 /**
188 * The number of continuous scans that must occur before consider the
189 * supplicant in a scanning state. This allows supplicant to associate with
190 * remembered networks that are in the scan results.
191 */
192 private static final int NUM_SCANS_BEFORE_ACTUALLY_SCANNING = 3;
193 /**
194 * The number of scans since the last network state change. When this
195 * exceeds {@link #NUM_SCANS_BEFORE_ACTUALLY_SCANNING}, we consider the
196 * supplicant to actually be scanning. When the network state changes to
197 * something other than scanning, we reset this to 0.
198 */
199 private int mNumScansSinceNetworkStateChange;
Jaikumar Ganesh7440fc22010-09-27 17:04:14 -0700200
Dianne Hackborn03f3cb02010-09-17 23:12:26 -0700201 /**
Wink Saville4b7ba092010-10-20 15:37:41 -0700202 * Asynchronous channel to WifiStateMachine
203 */
Irfan Sheriff227bec42011-02-15 19:30:27 -0800204 private AsyncChannel mWifiStateMachineChannel;
Wink Saville4b7ba092010-10-20 15:37:41 -0700205
206 /**
Irfan Sheriff227bec42011-02-15 19:30:27 -0800207 * Clients receiving asynchronous messages
Wink Saville4b7ba092010-10-20 15:37:41 -0700208 */
Irfan Sheriff227bec42011-02-15 19:30:27 -0800209 private List<AsyncChannel> mClients = new ArrayList<AsyncChannel>();
Wink Saville4b7ba092010-10-20 15:37:41 -0700210
Irfan Sheriff227bec42011-02-15 19:30:27 -0800211 /**
212 * Handles client connections
213 */
214 private class AsyncServiceHandler extends Handler {
215
216 AsyncServiceHandler(android.os.Looper looper) {
Wink Saville4b7ba092010-10-20 15:37:41 -0700217 super(looper);
Wink Saville4b7ba092010-10-20 15:37:41 -0700218 }
219
220 @Override
221 public void handleMessage(Message msg) {
222 switch (msg.what) {
223 case AsyncChannel.CMD_CHANNEL_HALF_CONNECTED: {
224 if (msg.arg1 == AsyncChannel.STATUS_SUCCESSFUL) {
Irfan Sheriff227bec42011-02-15 19:30:27 -0800225 Slog.d(TAG, "New client listening to asynchronous messages");
226 mClients.add((AsyncChannel) msg.obj);
Wink Saville4b7ba092010-10-20 15:37:41 -0700227 } else {
Irfan Sheriff227bec42011-02-15 19:30:27 -0800228 Slog.e(TAG, "Client connection failure, error=" + msg.arg1);
229 }
230 break;
231 }
232 case AsyncChannel.CMD_CHANNEL_FULL_CONNECTION: {
233 AsyncChannel ac = new AsyncChannel();
234 ac.connect(mContext, this, msg.replyTo);
235 break;
236 }
Irfan Sheriffebe606f2011-02-24 11:39:15 -0800237 case WifiManager.CMD_ENABLE_TRAFFIC_STATS_POLL: {
Irfan Sheriff227bec42011-02-15 19:30:27 -0800238 mEnableTrafficStatsPoll = (msg.arg1 == 1);
239 mTrafficStatsPollToken++;
240 if (mEnableTrafficStatsPoll) {
241 notifyOnDataActivity();
Irfan Sheriffebe606f2011-02-24 11:39:15 -0800242 sendMessageDelayed(Message.obtain(this, WifiManager.CMD_TRAFFIC_STATS_POLL,
Irfan Sheriff227bec42011-02-15 19:30:27 -0800243 mTrafficStatsPollToken, 0), POLL_TRAFFIC_STATS_INTERVAL_MSECS);
244 }
245 break;
246 }
Irfan Sheriffebe606f2011-02-24 11:39:15 -0800247 case WifiManager.CMD_TRAFFIC_STATS_POLL: {
Irfan Sheriff227bec42011-02-15 19:30:27 -0800248 if (msg.arg1 == mTrafficStatsPollToken) {
249 notifyOnDataActivity();
Irfan Sheriffebe606f2011-02-24 11:39:15 -0800250 sendMessageDelayed(Message.obtain(this, WifiManager.CMD_TRAFFIC_STATS_POLL,
Irfan Sheriff227bec42011-02-15 19:30:27 -0800251 mTrafficStatsPollToken, 0), POLL_TRAFFIC_STATS_INTERVAL_MSECS);
Wink Saville4b7ba092010-10-20 15:37:41 -0700252 }
253 break;
254 }
Irfan Sheriffebe606f2011-02-24 11:39:15 -0800255 case WifiManager.CMD_CONNECT_NETWORK: {
256 if (msg.obj != null) {
257 mWifiStateMachine.connectNetwork((WifiConfiguration)msg.obj);
258 } else {
259 mWifiStateMachine.connectNetwork(msg.arg1);
260 }
261 break;
262 }
263 case WifiManager.CMD_SAVE_NETWORK: {
264 mWifiStateMachine.saveNetwork((WifiConfiguration)msg.obj);
265 break;
266 }
267 case WifiManager.CMD_FORGET_NETWORK: {
268 mWifiStateMachine.forgetNetwork(msg.arg1);
269 break;
270 }
271 case WifiManager.CMD_START_WPS: {
272 //replyTo has the original source
273 mWifiStateMachine.startWps(msg.replyTo, (WpsConfiguration)msg.obj);
274 break;
275 }
Wink Saville4b7ba092010-10-20 15:37:41 -0700276 default: {
277 Slog.d(TAG, "WifiServicehandler.handleMessage ignoring msg=" + msg);
278 break;
279 }
280 }
281 }
282 }
Irfan Sheriff227bec42011-02-15 19:30:27 -0800283 private AsyncServiceHandler mAsyncServiceHandler;
284
285 /**
286 * Handles interaction with WifiStateMachine
287 */
288 private class WifiStateMachineHandler extends Handler {
289 private AsyncChannel mWsmChannel;
290
291 WifiStateMachineHandler(android.os.Looper looper) {
292 super(looper);
293 mWsmChannel = new AsyncChannel();
294 mWsmChannel.connect(mContext, this, mWifiStateMachine.getHandler());
295 }
296
297 @Override
298 public void handleMessage(Message msg) {
299 switch (msg.what) {
300 case AsyncChannel.CMD_CHANNEL_HALF_CONNECTED: {
301 if (msg.arg1 == AsyncChannel.STATUS_SUCCESSFUL) {
302 mWifiStateMachineChannel = mWsmChannel;
303 } else {
304 Slog.e(TAG, "WifiStateMachine connection failure, error=" + msg.arg1);
305 mWifiStateMachineChannel = null;
306 }
307 break;
308 }
309 default: {
310 Slog.d(TAG, "WifiStateMachineHandler.handleMessage ignoring msg=" + msg);
311 break;
312 }
313 }
314 }
315 }
316 WifiStateMachineHandler mWifiStateMachineHandler;
Wink Saville4b7ba092010-10-20 15:37:41 -0700317
318 /**
Dianne Hackborn03f3cb02010-09-17 23:12:26 -0700319 * Temporary for computing UIDS that are responsible for starting WIFI.
320 * Protected by mWifiStateTracker lock.
321 */
322 private final WorkSource mTmpWorkSource = new WorkSource();
Irfan Sheriff0d255342010-07-28 09:35:20 -0700323
324 WifiService(Context context) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800325 mContext = context;
Irfan Sheriff227bec42011-02-15 19:30:27 -0800326
327 mInterfaceName = SystemProperties.get("wifi.interface", "wlan0");
328
329 mWifiStateMachine = new WifiStateMachine(mContext, mInterfaceName);
Irfan Sheriff0d255342010-07-28 09:35:20 -0700330 mWifiStateMachine.enableRssiPolling(true);
The Android Open Source Project10592532009-03-18 17:39:46 -0700331 mBatteryStats = BatteryStatsService.getService();
Jaikumar Ganesh084c6652009-12-07 10:58:18 -0800332
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800333 mAlarmManager = (AlarmManager)mContext.getSystemService(Context.ALARM_SERVICE);
334 Intent idleIntent = new Intent(ACTION_DEVICE_IDLE, null);
335 mIdleIntent = PendingIntent.getBroadcast(mContext, IDLE_REQUEST, idleIntent, 0);
336
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800337 mContext.registerReceiver(
338 new BroadcastReceiver() {
339 @Override
340 public void onReceive(Context context, Intent intent) {
Mike Lockwoodbd5ddf02009-07-29 21:37:14 -0700341 // clear our flag indicating the user has overwridden airplane mode
Irfan Sheriffa2a1b912010-06-07 09:03:04 -0700342 mAirplaneModeOverwridden.set(false);
Irfan Sheriffb2e6c012010-04-05 11:57:56 -0700343 // on airplane disable, restore Wifi if the saved state indicates so
344 if (!isAirplaneModeOn() && testAndClearWifiSavedState()) {
345 persistWifiEnabled(true);
346 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800347 updateWifiState();
348 }
349 },
350 new IntentFilter(Intent.ACTION_AIRPLANE_MODE_CHANGED));
351
Irfan Sheriff0d255342010-07-28 09:35:20 -0700352 IntentFilter filter = new IntentFilter();
353 filter.addAction(WifiManager.WIFI_STATE_CHANGED_ACTION);
354 filter.addAction(WifiManager.NETWORK_STATE_CHANGED_ACTION);
355 filter.addAction(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION);
356
357 mContext.registerReceiver(
358 new BroadcastReceiver() {
359 @Override
360 public void onReceive(Context context, Intent intent) {
361 if (intent.getAction().equals(WifiManager.WIFI_STATE_CHANGED_ACTION)) {
362 // reset & clear notification on any wifi state change
363 resetNotification();
364 } else if (intent.getAction().equals(
365 WifiManager.NETWORK_STATE_CHANGED_ACTION)) {
366 mNetworkInfo = (NetworkInfo) intent.getParcelableExtra(
367 WifiManager.EXTRA_NETWORK_INFO);
368 // reset & clear notification on a network connect & disconnect
369 switch(mNetworkInfo.getDetailedState()) {
370 case CONNECTED:
371 case DISCONNECTED:
Irfan Sheriff227bec42011-02-15 19:30:27 -0800372 evaluateTrafficStatsPolling();
Irfan Sheriff0d255342010-07-28 09:35:20 -0700373 resetNotification();
374 break;
375 }
376 } else if (intent.getAction().equals(
377 WifiManager.SCAN_RESULTS_AVAILABLE_ACTION)) {
378 checkAndSetNotification();
379 }
380 }
381 }, filter);
382
Irfan Sheriff227bec42011-02-15 19:30:27 -0800383 HandlerThread wifiThread = new HandlerThread("WifiService");
384 wifiThread.start();
385 mAsyncServiceHandler = new AsyncServiceHandler(wifiThread.getLooper());
386 mWifiStateMachineHandler = new WifiStateMachineHandler(wifiThread.getLooper());
387
Irfan Sheriff0d255342010-07-28 09:35:20 -0700388 // Setting is in seconds
389 NOTIFICATION_REPEAT_DELAY_MS = Settings.Secure.getInt(context.getContentResolver(),
390 Settings.Secure.WIFI_NETWORKS_AVAILABLE_REPEAT_DELAY, 900) * 1000l;
391 mNotificationEnabledSettingObserver = new NotificationEnabledSettingObserver(new Handler());
392 mNotificationEnabledSettingObserver.register();
Irfan Sherifffcc08452011-02-17 16:44:54 -0800393
394 mBackgroundScanSupported = mContext.getResources().getBoolean(
395 com.android.internal.R.bool.config_wifi_background_scan_support);
Irfan Sheriff7b009782010-03-11 16:37:45 -0800396 }
Irfan Sheriff5321aef2010-02-12 12:35:59 -0800397
Irfan Sheriff7b009782010-03-11 16:37:45 -0800398 /**
399 * Check if Wi-Fi needs to be enabled and start
400 * if needed
Irfan Sheriff60e3ba02010-04-02 12:18:45 -0700401 *
402 * This function is used only at boot time
Irfan Sheriff7b009782010-03-11 16:37:45 -0800403 */
Irfan Sheriff0d255342010-07-28 09:35:20 -0700404 public void checkAndStartWifi() {
Irfan Sheriffa3bd4092010-03-24 17:58:59 -0700405 /* Start if Wi-Fi is enabled or the saved state indicates Wi-Fi was on */
Irfan Sheriff60e3ba02010-04-02 12:18:45 -0700406 boolean wifiEnabled = !isAirplaneModeOn()
407 && (getPersistedWifiEnabled() || testAndClearWifiSavedState());
Irfan Sheriff7b009782010-03-11 16:37:45 -0800408 Slog.i(TAG, "WifiService starting up with Wi-Fi " +
409 (wifiEnabled ? "enabled" : "disabled"));
Irfan Sheriffb99fe5e2010-03-26 14:56:07 -0700410 setWifiEnabled(wifiEnabled);
Irfan Sheriff5321aef2010-02-12 12:35:59 -0800411 }
412
Irfan Sheriffa3bd4092010-03-24 17:58:59 -0700413 private boolean testAndClearWifiSavedState() {
414 final ContentResolver cr = mContext.getContentResolver();
415 int wifiSavedState = 0;
416 try {
417 wifiSavedState = Settings.Secure.getInt(cr, Settings.Secure.WIFI_SAVED_STATE);
418 if(wifiSavedState == 1)
419 Settings.Secure.putInt(cr, Settings.Secure.WIFI_SAVED_STATE, 0);
420 } catch (Settings.SettingNotFoundException e) {
421 ;
422 }
423 return (wifiSavedState == 1);
424 }
425
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800426 private boolean getPersistedWifiEnabled() {
427 final ContentResolver cr = mContext.getContentResolver();
428 try {
429 return Settings.Secure.getInt(cr, Settings.Secure.WIFI_ON) == 1;
430 } catch (Settings.SettingNotFoundException e) {
431 Settings.Secure.putInt(cr, Settings.Secure.WIFI_ON, 0);
432 return false;
433 }
434 }
435
436 private void persistWifiEnabled(boolean enabled) {
437 final ContentResolver cr = mContext.getContentResolver();
438 Settings.Secure.putInt(cr, Settings.Secure.WIFI_ON, enabled ? 1 : 0);
439 }
440
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800441 /**
442 * see {@link android.net.wifi.WifiManager#pingSupplicant()}
Irfan Sheriffa2a1b912010-06-07 09:03:04 -0700443 * @return {@code true} if the operation succeeds, {@code false} otherwise
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800444 */
445 public boolean pingSupplicant() {
Irfan Sheriffa2a1b912010-06-07 09:03:04 -0700446 enforceAccessPermission();
Irfan Sheriff227bec42011-02-15 19:30:27 -0800447 if (mWifiStateMachineChannel != null) {
448 return mWifiStateMachine.syncPingSupplicant(mWifiStateMachineChannel);
Irfan Sheriff1406bcb2010-10-28 14:41:39 -0700449 } else {
Irfan Sheriff227bec42011-02-15 19:30:27 -0800450 Slog.e(TAG, "mWifiStateMachineChannel is not initialized");
Irfan Sheriff1406bcb2010-10-28 14:41:39 -0700451 return false;
452 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800453 }
454
455 /**
456 * see {@link android.net.wifi.WifiManager#startScan()}
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800457 */
Irfan Sheriffe4984752010-08-19 11:29:22 -0700458 public void startScan(boolean forceActive) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800459 enforceChangePermission();
Irfan Sheriffe4984752010-08-19 11:29:22 -0700460 mWifiStateMachine.startScan(forceActive);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800461 }
462
463 private void enforceAccessPermission() {
464 mContext.enforceCallingOrSelfPermission(android.Manifest.permission.ACCESS_WIFI_STATE,
465 "WifiService");
466 }
467
468 private void enforceChangePermission() {
469 mContext.enforceCallingOrSelfPermission(android.Manifest.permission.CHANGE_WIFI_STATE,
470 "WifiService");
471
472 }
473
Robert Greenwaltfc1b15c2009-05-22 15:09:51 -0700474 private void enforceMulticastChangePermission() {
475 mContext.enforceCallingOrSelfPermission(
476 android.Manifest.permission.CHANGE_WIFI_MULTICAST_STATE,
477 "WifiService");
478 }
479
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800480 /**
Irfan Sheriffa2a1b912010-06-07 09:03:04 -0700481 * see {@link android.net.wifi.WifiManager#setWifiEnabled(boolean)}
482 * @param enable {@code true} to enable, {@code false} to disable.
483 * @return {@code true} if the enable/disable operation was
484 * started or is already in the queue.
485 */
486 public synchronized boolean setWifiEnabled(boolean enable) {
487 enforceChangePermission();
488
489 if (DBG) {
Irfan Sheriff0d255342010-07-28 09:35:20 -0700490 Slog.e(TAG, "Invoking mWifiStateMachine.setWifiEnabled\n");
Irfan Sheriffa2a1b912010-06-07 09:03:04 -0700491 }
492
493 // set a flag if the user is enabling Wifi while in airplane mode
494 if (enable && isAirplaneModeOn() && isAirplaneToggleable()) {
495 mAirplaneModeOverwridden.set(true);
496 }
497
Dianne Hackborn03f3cb02010-09-17 23:12:26 -0700498 if (enable) {
499 reportStartWorkSource();
500 }
Irfan Sheriff0d255342010-07-28 09:35:20 -0700501 mWifiStateMachine.setWifiEnabled(enable);
Irfan Sheriff61180692010-08-18 16:07:39 -0700502
503 /*
504 * Caller might not have WRITE_SECURE_SETTINGS,
505 * only CHANGE_WIFI_STATE is enforced
506 */
507 long ident = Binder.clearCallingIdentity();
Irfan Sheriffa2a1b912010-06-07 09:03:04 -0700508 persistWifiEnabled(enable);
Irfan Sheriff61180692010-08-18 16:07:39 -0700509 Binder.restoreCallingIdentity(ident);
Irfan Sheriffa2a1b912010-06-07 09:03:04 -0700510
511 if (enable) {
512 if (!mIsReceiverRegistered) {
513 registerForBroadcasts();
514 mIsReceiverRegistered = true;
515 }
516 } else if (mIsReceiverRegistered){
517 mContext.unregisterReceiver(mReceiver);
518 mIsReceiverRegistered = false;
519 }
520
521 return true;
522 }
523
524 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800525 * see {@link WifiManager#getWifiState()}
526 * @return One of {@link WifiManager#WIFI_STATE_DISABLED},
527 * {@link WifiManager#WIFI_STATE_DISABLING},
528 * {@link WifiManager#WIFI_STATE_ENABLED},
529 * {@link WifiManager#WIFI_STATE_ENABLING},
530 * {@link WifiManager#WIFI_STATE_UNKNOWN}
531 */
532 public int getWifiEnabledState() {
533 enforceAccessPermission();
Irfan Sheriffd8134ff2010-08-22 17:06:34 -0700534 return mWifiStateMachine.syncGetWifiState();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800535 }
536
537 /**
Irfan Sheriffc2f54c22010-03-18 14:02:22 -0700538 * see {@link android.net.wifi.WifiManager#setWifiApEnabled(WifiConfiguration, boolean)}
Irfan Sheriff5321aef2010-02-12 12:35:59 -0800539 * @param wifiConfig SSID, security and channel details as
540 * part of WifiConfiguration
Irfan Sheriffa2a1b912010-06-07 09:03:04 -0700541 * @param enabled true to enable and false to disable
Irfan Sheriff5321aef2010-02-12 12:35:59 -0800542 * @return {@code true} if the start operation was
543 * started or is already in the queue.
544 */
Irfan Sheriffa2a1b912010-06-07 09:03:04 -0700545 public synchronized boolean setWifiApEnabled(WifiConfiguration wifiConfig, boolean enabled) {
Irfan Sheriff5321aef2010-02-12 12:35:59 -0800546 enforceChangePermission();
Irfan Sheriff5321aef2010-02-12 12:35:59 -0800547
Irfan Sheriffa2a1b912010-06-07 09:03:04 -0700548 if (enabled) {
549 /* Use default config if there is no existing config */
550 if (wifiConfig == null && ((wifiConfig = getWifiApConfiguration()) == null)) {
551 wifiConfig = new WifiConfiguration();
552 wifiConfig.SSID = mContext.getString(R.string.wifi_tether_configure_ssid_default);
553 wifiConfig.allowedKeyManagement.set(KeyMgmt.NONE);
554 }
Irfan Sheriff61180692010-08-18 16:07:39 -0700555 /*
556 * Caller might not have WRITE_SECURE_SETTINGS,
557 * only CHANGE_WIFI_STATE is enforced
558 */
559 long ident = Binder.clearCallingIdentity();
Irfan Sheriffa2a1b912010-06-07 09:03:04 -0700560 setWifiApConfiguration(wifiConfig);
Irfan Sheriff61180692010-08-18 16:07:39 -0700561 Binder.restoreCallingIdentity(ident);
Irfan Sheriff5321aef2010-02-12 12:35:59 -0800562 }
563
Irfan Sheriff0d255342010-07-28 09:35:20 -0700564 mWifiStateMachine.setWifiApEnabled(wifiConfig, enabled);
Irfan Sheriffa2a1b912010-06-07 09:03:04 -0700565
Irfan Sheriff5321aef2010-02-12 12:35:59 -0800566 return true;
567 }
568
Irfan Sheriffa2a1b912010-06-07 09:03:04 -0700569 /**
570 * see {@link WifiManager#getWifiApState()}
571 * @return One of {@link WifiManager#WIFI_AP_STATE_DISABLED},
572 * {@link WifiManager#WIFI_AP_STATE_DISABLING},
573 * {@link WifiManager#WIFI_AP_STATE_ENABLED},
574 * {@link WifiManager#WIFI_AP_STATE_ENABLING},
575 * {@link WifiManager#WIFI_AP_STATE_FAILED}
576 */
577 public int getWifiApEnabledState() {
Irfan Sheriff17b232b2010-06-24 11:32:26 -0700578 enforceAccessPermission();
Irfan Sheriffd8134ff2010-08-22 17:06:34 -0700579 return mWifiStateMachine.syncGetWifiApState();
Irfan Sheriffa2a1b912010-06-07 09:03:04 -0700580 }
581
582 /**
583 * see {@link WifiManager#getWifiApConfiguration()}
584 * @return soft access point configuration
585 */
586 public synchronized WifiConfiguration getWifiApConfiguration() {
Irfan Sheriff9ab518ad2010-03-12 15:48:17 -0800587 final ContentResolver cr = mContext.getContentResolver();
588 WifiConfiguration wifiConfig = new WifiConfiguration();
589 int authType;
590 try {
591 wifiConfig.SSID = Settings.Secure.getString(cr, Settings.Secure.WIFI_AP_SSID);
592 if (wifiConfig.SSID == null)
593 return null;
594 authType = Settings.Secure.getInt(cr, Settings.Secure.WIFI_AP_SECURITY);
595 wifiConfig.allowedKeyManagement.set(authType);
596 wifiConfig.preSharedKey = Settings.Secure.getString(cr, Settings.Secure.WIFI_AP_PASSWD);
597 return wifiConfig;
598 } catch (Settings.SettingNotFoundException e) {
599 Slog.e(TAG,"AP settings not found, returning");
600 return null;
601 }
602 }
603
Irfan Sheriffa2a1b912010-06-07 09:03:04 -0700604 /**
605 * see {@link WifiManager#setWifiApConfiguration(WifiConfiguration)}
606 * @param wifiConfig WifiConfiguration details for soft access point
607 */
608 public synchronized void setWifiApConfiguration(WifiConfiguration wifiConfig) {
Irfan Sheriff17b232b2010-06-24 11:32:26 -0700609 enforceChangePermission();
Irfan Sheriff9ab518ad2010-03-12 15:48:17 -0800610 final ContentResolver cr = mContext.getContentResolver();
Irfan Sheriff9ab518ad2010-03-12 15:48:17 -0800611 if (wifiConfig == null)
612 return;
Irfan Sheriffec8d23a2011-02-16 17:00:33 -0800613 int authType = wifiConfig.getAuthType();
Irfan Sheriff9ab518ad2010-03-12 15:48:17 -0800614 Settings.Secure.putString(cr, Settings.Secure.WIFI_AP_SSID, wifiConfig.SSID);
Irfan Sheriffec8d23a2011-02-16 17:00:33 -0800615 Settings.Secure.putInt(cr, Settings.Secure.WIFI_AP_SECURITY, authType);
616 if (authType != KeyMgmt.NONE)
Irfan Sheriff9ab518ad2010-03-12 15:48:17 -0800617 Settings.Secure.putString(cr, Settings.Secure.WIFI_AP_PASSWD, wifiConfig.preSharedKey);
618 }
619
Irfan Sheriff5321aef2010-02-12 12:35:59 -0800620 /**
Irfan Sheriffa2a1b912010-06-07 09:03:04 -0700621 * see {@link android.net.wifi.WifiManager#disconnect()}
Irfan Sheriff5321aef2010-02-12 12:35:59 -0800622 */
Irfan Sheriffe4984752010-08-19 11:29:22 -0700623 public void disconnect() {
Irfan Sheriffa2a1b912010-06-07 09:03:04 -0700624 enforceChangePermission();
Irfan Sheriffe4984752010-08-19 11:29:22 -0700625 mWifiStateMachine.disconnectCommand();
Irfan Sheriff5321aef2010-02-12 12:35:59 -0800626 }
627
628 /**
Irfan Sheriffa2a1b912010-06-07 09:03:04 -0700629 * see {@link android.net.wifi.WifiManager#reconnect()}
Irfan Sheriff5321aef2010-02-12 12:35:59 -0800630 */
Irfan Sheriffe4984752010-08-19 11:29:22 -0700631 public void reconnect() {
Irfan Sheriffa2a1b912010-06-07 09:03:04 -0700632 enforceChangePermission();
Irfan Sheriffe4984752010-08-19 11:29:22 -0700633 mWifiStateMachine.reconnectCommand();
Irfan Sheriff5321aef2010-02-12 12:35:59 -0800634 }
635
Irfan Sheriffa2a1b912010-06-07 09:03:04 -0700636 /**
637 * see {@link android.net.wifi.WifiManager#reassociate()}
Irfan Sheriffa2a1b912010-06-07 09:03:04 -0700638 */
Irfan Sheriffe4984752010-08-19 11:29:22 -0700639 public void reassociate() {
Irfan Sheriffa2a1b912010-06-07 09:03:04 -0700640 enforceChangePermission();
Irfan Sheriffe4984752010-08-19 11:29:22 -0700641 mWifiStateMachine.reassociateCommand();
Irfan Sheriff5321aef2010-02-12 12:35:59 -0800642 }
643
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800644 /**
645 * see {@link android.net.wifi.WifiManager#getConfiguredNetworks()}
646 * @return the list of configured networks
647 */
648 public List<WifiConfiguration> getConfiguredNetworks() {
649 enforceAccessPermission();
Irfan Sheriffd8134ff2010-08-22 17:06:34 -0700650 return mWifiStateMachine.syncGetConfiguredNetworks();
Chung-yih Wanga8d15942009-10-09 11:01:49 +0800651 }
652
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800653 /**
654 * see {@link android.net.wifi.WifiManager#addOrUpdateNetwork(WifiConfiguration)}
655 * @return the supplicant-assigned identifier for the new or updated
656 * network if the operation succeeds, or {@code -1} if it fails
657 */
Irfan Sheriff7aac5542009-12-22 21:42:17 -0800658 public int addOrUpdateNetwork(WifiConfiguration config) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800659 enforceChangePermission();
Irfan Sheriff227bec42011-02-15 19:30:27 -0800660 if (mWifiStateMachineChannel != null) {
661 return mWifiStateMachine.syncAddOrUpdateNetwork(mWifiStateMachineChannel, config);
Irfan Sheriff1406bcb2010-10-28 14:41:39 -0700662 } else {
Irfan Sheriff227bec42011-02-15 19:30:27 -0800663 Slog.e(TAG, "mWifiStateMachineChannel is not initialized");
Irfan Sheriff1406bcb2010-10-28 14:41:39 -0700664 return -1;
665 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800666 }
667
Irfan Sheriffa2a1b912010-06-07 09:03:04 -0700668 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800669 * See {@link android.net.wifi.WifiManager#removeNetwork(int)}
670 * @param netId the integer that identifies the network configuration
671 * to the supplicant
672 * @return {@code true} if the operation succeeded
673 */
674 public boolean removeNetwork(int netId) {
675 enforceChangePermission();
Irfan Sheriff227bec42011-02-15 19:30:27 -0800676 if (mWifiStateMachineChannel != null) {
677 return mWifiStateMachine.syncRemoveNetwork(mWifiStateMachineChannel, netId);
Wink Saville4b7ba092010-10-20 15:37:41 -0700678 } else {
Irfan Sheriff227bec42011-02-15 19:30:27 -0800679 Slog.e(TAG, "mWifiStateMachineChannel is not initialized");
Wink Saville4b7ba092010-10-20 15:37:41 -0700680 return false;
681 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800682 }
683
684 /**
685 * See {@link android.net.wifi.WifiManager#enableNetwork(int, boolean)}
686 * @param netId the integer that identifies the network configuration
687 * to the supplicant
688 * @param disableOthers if true, disable all other networks.
689 * @return {@code true} if the operation succeeded
690 */
691 public boolean enableNetwork(int netId, boolean disableOthers) {
692 enforceChangePermission();
Irfan Sheriff227bec42011-02-15 19:30:27 -0800693 if (mWifiStateMachineChannel != null) {
694 return mWifiStateMachine.syncEnableNetwork(mWifiStateMachineChannel, netId,
695 disableOthers);
Irfan Sheriff1406bcb2010-10-28 14:41:39 -0700696 } else {
Irfan Sheriff227bec42011-02-15 19:30:27 -0800697 Slog.e(TAG, "mWifiStateMachineChannel is not initialized");
Irfan Sheriff1406bcb2010-10-28 14:41:39 -0700698 return false;
699 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800700 }
701
702 /**
703 * See {@link android.net.wifi.WifiManager#disableNetwork(int)}
704 * @param netId the integer that identifies the network configuration
705 * to the supplicant
706 * @return {@code true} if the operation succeeded
707 */
708 public boolean disableNetwork(int netId) {
709 enforceChangePermission();
Irfan Sheriff227bec42011-02-15 19:30:27 -0800710 if (mWifiStateMachineChannel != null) {
711 return mWifiStateMachine.syncDisableNetwork(mWifiStateMachineChannel, netId);
Irfan Sheriff1406bcb2010-10-28 14:41:39 -0700712 } else {
Irfan Sheriff227bec42011-02-15 19:30:27 -0800713 Slog.e(TAG, "mWifiStateMachineChannel is not initialized");
Irfan Sheriff1406bcb2010-10-28 14:41:39 -0700714 return false;
715 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800716 }
717
718 /**
719 * See {@link android.net.wifi.WifiManager#getConnectionInfo()}
720 * @return the Wi-Fi information, contained in {@link WifiInfo}.
721 */
722 public WifiInfo getConnectionInfo() {
723 enforceAccessPermission();
724 /*
725 * Make sure we have the latest information, by sending
726 * a status request to the supplicant.
727 */
Irfan Sheriffd8134ff2010-08-22 17:06:34 -0700728 return mWifiStateMachine.syncRequestConnectionInfo();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800729 }
730
731 /**
732 * Return the results of the most recent access point scan, in the form of
733 * a list of {@link ScanResult} objects.
734 * @return the list of results
735 */
736 public List<ScanResult> getScanResults() {
737 enforceAccessPermission();
Irfan Sheriffd8134ff2010-08-22 17:06:34 -0700738 return mWifiStateMachine.syncGetScanResultsList();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800739 }
740
741 /**
742 * Tell the supplicant to persist the current list of configured networks.
743 * @return {@code true} if the operation succeeded
Irfan Sheriffa2a1b912010-06-07 09:03:04 -0700744 *
745 * TODO: deprecate this
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800746 */
747 public boolean saveConfiguration() {
Irfan Sheriffa2a1b912010-06-07 09:03:04 -0700748 boolean result = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800749 enforceChangePermission();
Irfan Sheriff227bec42011-02-15 19:30:27 -0800750 if (mWifiStateMachineChannel != null) {
751 return mWifiStateMachine.syncSaveConfig(mWifiStateMachineChannel);
Irfan Sheriff1406bcb2010-10-28 14:41:39 -0700752 } else {
Irfan Sheriff227bec42011-02-15 19:30:27 -0800753 Slog.e(TAG, "mWifiStateMachineChannel is not initialized");
Irfan Sheriff1406bcb2010-10-28 14:41:39 -0700754 return false;
755 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800756 }
757
758 /**
Irfan Sheriffed4f28b2010-10-29 15:32:10 -0700759 * Set the country code
760 * @param countryCode ISO 3166 country code.
Robert Greenwaltb5010cc2009-05-21 15:11:40 -0700761 * @param persist {@code true} if the setting should be remembered.
Irfan Sheriffed4f28b2010-10-29 15:32:10 -0700762 *
763 * The persist behavior exists so that wifi can fall back to the last
764 * persisted country code on a restart, when the locale information is
765 * not available from telephony.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800766 */
Irfan Sheriffed4f28b2010-10-29 15:32:10 -0700767 public void setCountryCode(String countryCode, boolean persist) {
768 Slog.i(TAG, "WifiService trying to set country code to " + countryCode +
769 " with persist set to " + persist);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800770 enforceChangePermission();
Irfan Sheriffed4f28b2010-10-29 15:32:10 -0700771 mWifiStateMachine.setCountryCode(countryCode, persist);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800772 }
773
774 /**
Irfan Sheriff36f74132010-11-04 16:57:37 -0700775 * Set the operational frequency band
776 * @param band One of
777 * {@link WifiManager#WIFI_FREQUENCY_BAND_AUTO},
778 * {@link WifiManager#WIFI_FREQUENCY_BAND_5GHZ},
779 * {@link WifiManager#WIFI_FREQUENCY_BAND_2GHZ},
780 * @param persist {@code true} if the setting should be remembered.
781 *
782 */
783 public void setFrequencyBand(int band, boolean persist) {
784 enforceChangePermission();
785 if (!isDualBandSupported()) return;
786 Slog.i(TAG, "WifiService trying to set frequency band to " + band +
787 " with persist set to " + persist);
788 mWifiStateMachine.setFrequencyBand(band, persist);
789 }
790
791
792 /**
793 * Get the operational frequency band
794 */
795 public int getFrequencyBand() {
796 enforceAccessPermission();
797 return mWifiStateMachine.getFrequencyBand();
798 }
799
800 public boolean isDualBandSupported() {
801 //TODO: Should move towards adding a driver API that checks at runtime
802 return mContext.getResources().getBoolean(
803 com.android.internal.R.bool.config_wifi_dual_band_support);
804 }
805
806 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800807 * Return the DHCP-assigned addresses from the last successful DHCP request,
808 * if any.
809 * @return the DHCP information
810 */
811 public DhcpInfo getDhcpInfo() {
812 enforceAccessPermission();
Irfan Sheriffd8134ff2010-08-22 17:06:34 -0700813 return mWifiStateMachine.syncGetDhcpInfo();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800814 }
815
Irfan Sheriff0d255342010-07-28 09:35:20 -0700816 /**
817 * see {@link android.net.wifi.WifiManager#startWifi}
818 *
819 */
820 public void startWifi() {
821 enforceChangePermission();
822 /* TODO: may be add permissions for access only to connectivity service
823 * TODO: if a start issued, keep wifi alive until a stop issued irrespective
824 * of WifiLock & device idle status unless wifi enabled status is toggled
825 */
826
827 mWifiStateMachine.setDriverStart(true);
828 mWifiStateMachine.reconnectCommand();
829 }
830
831 /**
832 * see {@link android.net.wifi.WifiManager#stopWifi}
833 *
834 */
835 public void stopWifi() {
836 enforceChangePermission();
837 /* TODO: may be add permissions for access only to connectivity service
838 * TODO: if a stop is issued, wifi is brought up only by startWifi
839 * unless wifi enabled status is toggled
840 */
841 mWifiStateMachine.setDriverStart(false);
842 }
843
844
845 /**
846 * see {@link android.net.wifi.WifiManager#addToBlacklist}
847 *
848 */
849 public void addToBlacklist(String bssid) {
850 enforceChangePermission();
851
852 mWifiStateMachine.addToBlacklist(bssid);
853 }
854
855 /**
856 * see {@link android.net.wifi.WifiManager#clearBlacklist}
857 *
858 */
859 public void clearBlacklist() {
860 enforceChangePermission();
861
862 mWifiStateMachine.clearBlacklist();
863 }
864
Irfan Sheriffe04653c2010-08-09 09:09:59 -0700865
Irfan Sheriff5ee89802010-09-16 17:53:34 -0700866
Irfan Sheriff227bec42011-02-15 19:30:27 -0800867 /**
868 * Get a reference to handler. This is used by a client to establish
869 * an AsyncChannel communication with WifiService
870 */
871 public Messenger getMessenger() {
Irfan Sheriffebe606f2011-02-24 11:39:15 -0800872 /* Enforce the highest permissions
873 TODO: when we consider exposing the asynchronous API, think about
874 how to provide both access and change permissions seperately
875 */
Irfan Sheriff227bec42011-02-15 19:30:27 -0800876 enforceAccessPermission();
Irfan Sheriffebe606f2011-02-24 11:39:15 -0800877 enforceChangePermission();
Irfan Sheriff227bec42011-02-15 19:30:27 -0800878 return new Messenger(mAsyncServiceHandler);
879 }
880
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800881 private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
882 @Override
883 public void onReceive(Context context, Intent intent) {
884 String action = intent.getAction();
885
Doug Zongker43866e02010-01-07 12:09:54 -0800886 long idleMillis =
887 Settings.Secure.getLong(mContext.getContentResolver(),
Irfan Sheriff4f5f7c92010-10-14 17:01:27 -0700888 Settings.Secure.WIFI_IDLE_MS, DEFAULT_IDLE_MS);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800889 int stayAwakeConditions =
Doug Zongker43866e02010-01-07 12:09:54 -0800890 Settings.System.getInt(mContext.getContentResolver(),
891 Settings.System.STAY_ON_WHILE_PLUGGED_IN, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800892 if (action.equals(Intent.ACTION_SCREEN_ON)) {
Joe Onorato431bb222010-10-18 19:13:23 -0400893 if (DBG) {
894 Slog.d(TAG, "ACTION_SCREEN_ON");
895 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800896 mAlarmManager.cancel(mIdleIntent);
897 mDeviceIdle = false;
898 mScreenOff = false;
Dianne Hackborn58e0eef2010-09-16 01:22:10 -0700899 // Once the screen is on, we are not keeping WIFI running
900 // because of any locks so clear that tracking immediately.
901 reportStartWorkSource();
Irfan Sheriff227bec42011-02-15 19:30:27 -0800902 evaluateTrafficStatsPolling();
Irfan Sheriff0d255342010-07-28 09:35:20 -0700903 mWifiStateMachine.enableRssiPolling(true);
Irfan Sherifffcc08452011-02-17 16:44:54 -0800904 if (mBackgroundScanSupported) {
905 mWifiStateMachine.enableBackgroundScan(false);
906 }
Irfan Sheriff8e86b892010-12-22 11:02:20 -0800907 mWifiStateMachine.enableAllNetworks();
Irfan Sheriff4f5f7c92010-10-14 17:01:27 -0700908 updateWifiState();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800909 } else if (action.equals(Intent.ACTION_SCREEN_OFF)) {
Joe Onorato431bb222010-10-18 19:13:23 -0400910 if (DBG) {
911 Slog.d(TAG, "ACTION_SCREEN_OFF");
912 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800913 mScreenOff = true;
Irfan Sheriff227bec42011-02-15 19:30:27 -0800914 evaluateTrafficStatsPolling();
Irfan Sheriff0d255342010-07-28 09:35:20 -0700915 mWifiStateMachine.enableRssiPolling(false);
Irfan Sherifffcc08452011-02-17 16:44:54 -0800916 if (mBackgroundScanSupported) {
917 mWifiStateMachine.enableBackgroundScan(true);
918 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800919 /*
920 * Set a timer to put Wi-Fi to sleep, but only if the screen is off
921 * AND the "stay on while plugged in" setting doesn't match the
922 * current power conditions (i.e, not plugged in, plugged in to USB,
923 * or plugged in to AC).
924 */
925 if (!shouldWifiStayAwake(stayAwakeConditions, mPluggedType)) {
Irfan Sheriffd8134ff2010-08-22 17:06:34 -0700926 WifiInfo info = mWifiStateMachine.syncRequestConnectionInfo();
San Mehatfa6c7112009-07-07 09:34:44 -0700927 if (info.getSupplicantState() != SupplicantState.COMPLETED) {
Robert Greenwalt84612ea62009-09-30 09:04:22 -0700928 // we used to go to sleep immediately, but this caused some race conditions
Irfan Sheriffa2a1b912010-06-07 09:03:04 -0700929 // we don't have time to track down for this release. Delay instead,
930 // but not as long as we would if connected (below)
Robert Greenwalt84612ea62009-09-30 09:04:22 -0700931 // TODO - fix the race conditions and switch back to the immediate turn-off
932 long triggerTime = System.currentTimeMillis() + (2*60*1000); // 2 min
Joe Onorato431bb222010-10-18 19:13:23 -0400933 if (DBG) {
934 Slog.d(TAG, "setting ACTION_DEVICE_IDLE timer for 120,000 ms");
935 }
Robert Greenwalt84612ea62009-09-30 09:04:22 -0700936 mAlarmManager.set(AlarmManager.RTC_WAKEUP, triggerTime, mIdleIntent);
937 // // do not keep Wifi awake when screen is off if Wifi is not associated
938 // mDeviceIdle = true;
939 // updateWifiState();
Mike Lockwoodd9c32bc2009-05-18 14:14:15 -0400940 } else {
941 long triggerTime = System.currentTimeMillis() + idleMillis;
Joe Onorato431bb222010-10-18 19:13:23 -0400942 if (DBG) {
943 Slog.d(TAG, "setting ACTION_DEVICE_IDLE timer for " + idleMillis
944 + "ms");
945 }
Mike Lockwoodd9c32bc2009-05-18 14:14:15 -0400946 mAlarmManager.set(AlarmManager.RTC_WAKEUP, triggerTime, mIdleIntent);
947 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800948 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800949 } else if (action.equals(ACTION_DEVICE_IDLE)) {
Joe Onorato431bb222010-10-18 19:13:23 -0400950 if (DBG) {
951 Slog.d(TAG, "got ACTION_DEVICE_IDLE");
952 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800953 mDeviceIdle = true;
Dianne Hackborn58e0eef2010-09-16 01:22:10 -0700954 reportStartWorkSource();
Irfan Sheriff4f5f7c92010-10-14 17:01:27 -0700955 updateWifiState();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800956 } else if (action.equals(Intent.ACTION_BATTERY_CHANGED)) {
957 /*
958 * Set a timer to put Wi-Fi to sleep, but only if the screen is off
959 * AND we are transitioning from a state in which the device was supposed
960 * to stay awake to a state in which it is not supposed to stay awake.
961 * If "stay awake" state is not changing, we do nothing, to avoid resetting
962 * the already-set timer.
963 */
964 int pluggedType = intent.getIntExtra("plugged", 0);
Joe Onorato431bb222010-10-18 19:13:23 -0400965 if (DBG) {
966 Slog.d(TAG, "ACTION_BATTERY_CHANGED pluggedType: " + pluggedType);
967 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800968 if (mScreenOff && shouldWifiStayAwake(stayAwakeConditions, mPluggedType) &&
969 !shouldWifiStayAwake(stayAwakeConditions, pluggedType)) {
970 long triggerTime = System.currentTimeMillis() + idleMillis;
Joe Onorato431bb222010-10-18 19:13:23 -0400971 if (DBG) {
972 Slog.d(TAG, "setting ACTION_DEVICE_IDLE timer for " + idleMillis + "ms");
973 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800974 mAlarmManager.set(AlarmManager.RTC_WAKEUP, triggerTime, mIdleIntent);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800975 }
976 mPluggedType = pluggedType;
Irfan Sheriff65eaec82011-01-05 22:00:16 -0800977 } else if (action.equals(BluetoothAdapter.ACTION_CONNECTION_STATE_CHANGED)) {
978 int state = intent.getIntExtra(BluetoothAdapter.EXTRA_CONNECTION_STATE,
979 BluetoothAdapter.STATE_DISCONNECTED);
980 mWifiStateMachine.sendBluetoothAdapterStateChange(state);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800981 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800982 }
983
984 /**
985 * Determines whether the Wi-Fi chipset should stay awake or be put to
986 * sleep. Looks at the setting for the sleep policy and the current
987 * conditions.
Jaikumar Ganesh084c6652009-12-07 10:58:18 -0800988 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800989 * @see #shouldDeviceStayAwake(int, int)
990 */
991 private boolean shouldWifiStayAwake(int stayAwakeConditions, int pluggedType) {
Irfan Sheriff739f6bc2011-01-28 16:43:12 -0800992 //Never sleep as long as the user has not changed the settings
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800993 int wifiSleepPolicy = Settings.System.getInt(mContext.getContentResolver(),
Irfan Sheriff96b10d62011-01-11 15:40:35 -0800994 Settings.System.WIFI_SLEEP_POLICY,
Irfan Sheriff739f6bc2011-01-28 16:43:12 -0800995 Settings.System.WIFI_SLEEP_POLICY_NEVER);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800996
997 if (wifiSleepPolicy == Settings.System.WIFI_SLEEP_POLICY_NEVER) {
998 // Never sleep
999 return true;
1000 } else if ((wifiSleepPolicy == Settings.System.WIFI_SLEEP_POLICY_NEVER_WHILE_PLUGGED) &&
1001 (pluggedType != 0)) {
1002 // Never sleep while plugged, and we're plugged
1003 return true;
1004 } else {
1005 // Default
1006 return shouldDeviceStayAwake(stayAwakeConditions, pluggedType);
1007 }
1008 }
Jaikumar Ganesh084c6652009-12-07 10:58:18 -08001009
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001010 /**
1011 * Determine whether the bit value corresponding to {@code pluggedType} is set in
1012 * the bit string {@code stayAwakeConditions}. Because a {@code pluggedType} value
1013 * of {@code 0} isn't really a plugged type, but rather an indication that the
1014 * device isn't plugged in at all, there is no bit value corresponding to a
1015 * {@code pluggedType} value of {@code 0}. That is why we shift by
Ben Dodson4e8620f2010-08-25 10:55:47 -07001016 * {@code pluggedType - 1} instead of by {@code pluggedType}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001017 * @param stayAwakeConditions a bit string specifying which "plugged types" should
1018 * keep the device (and hence Wi-Fi) awake.
1019 * @param pluggedType the type of plug (USB, AC, or none) for which the check is
1020 * being made
1021 * @return {@code true} if {@code pluggedType} indicates that the device is
1022 * supposed to stay awake, {@code false} otherwise.
1023 */
1024 private boolean shouldDeviceStayAwake(int stayAwakeConditions, int pluggedType) {
1025 return (stayAwakeConditions & pluggedType) != 0;
1026 }
1027 };
1028
Dianne Hackborn03f3cb02010-09-17 23:12:26 -07001029 private synchronized void reportStartWorkSource() {
1030 mTmpWorkSource.clear();
1031 if (mDeviceIdle) {
1032 for (int i=0; i<mLocks.mList.size(); i++) {
1033 mTmpWorkSource.add(mLocks.mList.get(i).mWorkSource);
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07001034 }
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07001035 }
Dianne Hackborn03f3cb02010-09-17 23:12:26 -07001036 mWifiStateMachine.updateBatteryWorkSource(mTmpWorkSource);
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07001037 }
Jaikumar Ganesh7440fc22010-09-27 17:04:14 -07001038
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001039 private void updateWifiState() {
1040 boolean wifiEnabled = getPersistedWifiEnabled();
Irfan Sheriffa2a1b912010-06-07 09:03:04 -07001041 boolean airplaneMode = isAirplaneModeOn() && !mAirplaneModeOverwridden.get();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001042 boolean lockHeld = mLocks.hasLocks();
Irfan Sheriff5876a422010-08-12 20:26:23 -07001043 int strongestLockMode = WifiManager.WIFI_MODE_FULL;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001044 boolean wifiShouldBeEnabled = wifiEnabled && !airplaneMode;
1045 boolean wifiShouldBeStarted = !mDeviceIdle || lockHeld;
Irfan Sheriff5876a422010-08-12 20:26:23 -07001046
1047 if (lockHeld) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001048 strongestLockMode = mLocks.getStrongestLockMode();
Irfan Sheriff5876a422010-08-12 20:26:23 -07001049 }
1050 /* If device is not idle, lockmode cannot be scan only */
1051 if (!mDeviceIdle && strongestLockMode == WifiManager.WIFI_MODE_SCAN_ONLY) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001052 strongestLockMode = WifiManager.WIFI_MODE_FULL;
1053 }
1054
Irfan Sheriffa2a1b912010-06-07 09:03:04 -07001055 /* Disable tethering when airplane mode is enabled */
1056 if (airplaneMode) {
Irfan Sheriff0d255342010-07-28 09:35:20 -07001057 mWifiStateMachine.setWifiApEnabled(null, false);
Irfan Sheriffa2a1b912010-06-07 09:03:04 -07001058 }
Irfan Sheriffb2e6c012010-04-05 11:57:56 -07001059
Irfan Sheriffa2a1b912010-06-07 09:03:04 -07001060 if (wifiShouldBeEnabled) {
1061 if (wifiShouldBeStarted) {
Dianne Hackborn03f3cb02010-09-17 23:12:26 -07001062 reportStartWorkSource();
Irfan Sheriff0d255342010-07-28 09:35:20 -07001063 mWifiStateMachine.setWifiEnabled(true);
1064 mWifiStateMachine.setScanOnlyMode(
Irfan Sheriffa2a1b912010-06-07 09:03:04 -07001065 strongestLockMode == WifiManager.WIFI_MODE_SCAN_ONLY);
Irfan Sheriff0d255342010-07-28 09:35:20 -07001066 mWifiStateMachine.setDriverStart(true);
Irfan Sheriff5876a422010-08-12 20:26:23 -07001067 mWifiStateMachine.setHighPerfModeEnabled(strongestLockMode
1068 == WifiManager.WIFI_MODE_FULL_HIGH_PERF);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001069 } else {
Irfan Sheriff0d255342010-07-28 09:35:20 -07001070 mWifiStateMachine.requestCmWakeLock();
1071 mWifiStateMachine.setDriverStart(false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001072 }
Irfan Sheriffa2a1b912010-06-07 09:03:04 -07001073 } else {
Irfan Sheriff0d255342010-07-28 09:35:20 -07001074 mWifiStateMachine.setWifiEnabled(false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001075 }
1076 }
1077
1078 private void registerForBroadcasts() {
1079 IntentFilter intentFilter = new IntentFilter();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001080 intentFilter.addAction(Intent.ACTION_SCREEN_ON);
1081 intentFilter.addAction(Intent.ACTION_SCREEN_OFF);
1082 intentFilter.addAction(Intent.ACTION_BATTERY_CHANGED);
1083 intentFilter.addAction(ACTION_DEVICE_IDLE);
Irfan Sheriff65eaec82011-01-05 22:00:16 -08001084 intentFilter.addAction(BluetoothAdapter.ACTION_CONNECTION_STATE_CHANGED);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001085 mContext.registerReceiver(mReceiver, intentFilter);
1086 }
Jaikumar Ganesh084c6652009-12-07 10:58:18 -08001087
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001088 private boolean isAirplaneSensitive() {
1089 String airplaneModeRadios = Settings.System.getString(mContext.getContentResolver(),
1090 Settings.System.AIRPLANE_MODE_RADIOS);
1091 return airplaneModeRadios == null
1092 || airplaneModeRadios.contains(Settings.System.RADIO_WIFI);
1093 }
1094
Mike Lockwoodbd5ddf02009-07-29 21:37:14 -07001095 private boolean isAirplaneToggleable() {
1096 String toggleableRadios = Settings.System.getString(mContext.getContentResolver(),
1097 Settings.System.AIRPLANE_MODE_TOGGLEABLE_RADIOS);
1098 return toggleableRadios != null
1099 && toggleableRadios.contains(Settings.System.RADIO_WIFI);
1100 }
1101
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001102 /**
1103 * Returns true if Wi-Fi is sensitive to airplane mode, and airplane mode is
1104 * currently on.
1105 * @return {@code true} if airplane mode is on.
1106 */
1107 private boolean isAirplaneModeOn() {
1108 return isAirplaneSensitive() && Settings.System.getInt(mContext.getContentResolver(),
1109 Settings.System.AIRPLANE_MODE_ON, 0) == 1;
1110 }
1111
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001112 @Override
1113 protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
1114 if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.DUMP)
1115 != PackageManager.PERMISSION_GRANTED) {
1116 pw.println("Permission Denial: can't dump WifiService from from pid="
1117 + Binder.getCallingPid()
1118 + ", uid=" + Binder.getCallingUid());
1119 return;
1120 }
Irfan Sheriffd8134ff2010-08-22 17:06:34 -07001121 pw.println("Wi-Fi is " + mWifiStateMachine.syncGetWifiStateByName());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001122 pw.println("Stay-awake conditions: " +
1123 Settings.System.getInt(mContext.getContentResolver(),
1124 Settings.System.STAY_ON_WHILE_PLUGGED_IN, 0));
1125 pw.println();
1126
1127 pw.println("Internal state:");
Irfan Sheriff0d255342010-07-28 09:35:20 -07001128 pw.println(mWifiStateMachine);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001129 pw.println();
1130 pw.println("Latest scan results:");
Irfan Sheriffd8134ff2010-08-22 17:06:34 -07001131 List<ScanResult> scanResults = mWifiStateMachine.syncGetScanResultsList();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001132 if (scanResults != null && scanResults.size() != 0) {
1133 pw.println(" BSSID Frequency RSSI Flags SSID");
1134 for (ScanResult r : scanResults) {
1135 pw.printf(" %17s %9d %5d %-16s %s%n",
1136 r.BSSID,
1137 r.frequency,
1138 r.level,
1139 r.capabilities,
1140 r.SSID == null ? "" : r.SSID);
1141 }
1142 }
1143 pw.println();
Eric Shienbrood5711fad2009-03-27 20:25:31 -07001144 pw.println("Locks acquired: " + mFullLocksAcquired + " full, " +
Irfan Sheriff5876a422010-08-12 20:26:23 -07001145 mFullHighPerfLocksAcquired + " full high perf, " +
Eric Shienbrood5711fad2009-03-27 20:25:31 -07001146 mScanLocksAcquired + " scan");
1147 pw.println("Locks released: " + mFullLocksReleased + " full, " +
Irfan Sheriff5876a422010-08-12 20:26:23 -07001148 mFullHighPerfLocksReleased + " full high perf, " +
Eric Shienbrood5711fad2009-03-27 20:25:31 -07001149 mScanLocksReleased + " scan");
1150 pw.println();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001151 pw.println("Locks held:");
1152 mLocks.dump(pw);
1153 }
1154
Robert Greenwalt58ff0212009-05-19 15:53:54 -07001155 private class WifiLock extends DeathRecipient {
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -07001156 WifiLock(int lockMode, String tag, IBinder binder, WorkSource ws) {
1157 super(lockMode, tag, binder, ws);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001158 }
1159
1160 public void binderDied() {
1161 synchronized (mLocks) {
1162 releaseWifiLockLocked(mBinder);
1163 }
1164 }
1165
1166 public String toString() {
Robert Greenwalt5347bd42009-05-13 15:10:16 -07001167 return "WifiLock{" + mTag + " type=" + mMode + " binder=" + mBinder + "}";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001168 }
1169 }
1170
1171 private class LockList {
1172 private List<WifiLock> mList;
1173
1174 private LockList() {
1175 mList = new ArrayList<WifiLock>();
1176 }
1177
1178 private synchronized boolean hasLocks() {
1179 return !mList.isEmpty();
1180 }
1181
1182 private synchronized int getStrongestLockMode() {
1183 if (mList.isEmpty()) {
1184 return WifiManager.WIFI_MODE_FULL;
1185 }
Irfan Sheriff5876a422010-08-12 20:26:23 -07001186
1187 if (mFullHighPerfLocksAcquired > mFullHighPerfLocksReleased) {
1188 return WifiManager.WIFI_MODE_FULL_HIGH_PERF;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001189 }
Irfan Sheriff5876a422010-08-12 20:26:23 -07001190
1191 if (mFullLocksAcquired > mFullLocksReleased) {
1192 return WifiManager.WIFI_MODE_FULL;
1193 }
1194
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001195 return WifiManager.WIFI_MODE_SCAN_ONLY;
1196 }
1197
1198 private void addLock(WifiLock lock) {
1199 if (findLockByBinder(lock.mBinder) < 0) {
1200 mList.add(lock);
1201 }
1202 }
1203
1204 private WifiLock removeLock(IBinder binder) {
1205 int index = findLockByBinder(binder);
1206 if (index >= 0) {
Suchi Amalapurapufff2fda2009-06-30 21:36:16 -07001207 WifiLock ret = mList.remove(index);
1208 ret.unlinkDeathRecipient();
1209 return ret;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001210 } else {
1211 return null;
1212 }
1213 }
1214
1215 private int findLockByBinder(IBinder binder) {
1216 int size = mList.size();
1217 for (int i = size - 1; i >= 0; i--)
1218 if (mList.get(i).mBinder == binder)
1219 return i;
1220 return -1;
1221 }
1222
1223 private void dump(PrintWriter pw) {
1224 for (WifiLock l : mList) {
1225 pw.print(" ");
1226 pw.println(l);
1227 }
1228 }
1229 }
1230
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -07001231 void enforceWakeSourcePermission(int uid, int pid) {
Dianne Hackborne746f032010-09-13 16:02:57 -07001232 if (uid == android.os.Process.myUid()) {
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -07001233 return;
1234 }
1235 mContext.enforcePermission(android.Manifest.permission.UPDATE_DEVICE_STATS,
1236 pid, uid, null);
1237 }
1238
1239 public boolean acquireWifiLock(IBinder binder, int lockMode, String tag, WorkSource ws) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001240 mContext.enforceCallingOrSelfPermission(android.Manifest.permission.WAKE_LOCK, null);
Irfan Sheriff5876a422010-08-12 20:26:23 -07001241 if (lockMode != WifiManager.WIFI_MODE_FULL &&
1242 lockMode != WifiManager.WIFI_MODE_SCAN_ONLY &&
1243 lockMode != WifiManager.WIFI_MODE_FULL_HIGH_PERF) {
1244 Slog.e(TAG, "Illegal argument, lockMode= " + lockMode);
1245 if (DBG) throw new IllegalArgumentException("lockMode=" + lockMode);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001246 return false;
1247 }
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -07001248 if (ws != null && ws.size() == 0) {
1249 ws = null;
1250 }
Dianne Hackbornecfd7f72010-10-08 14:23:40 -07001251 if (ws != null) {
1252 enforceWakeSourcePermission(Binder.getCallingUid(), Binder.getCallingPid());
1253 }
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -07001254 if (ws == null) {
1255 ws = new WorkSource(Binder.getCallingUid());
1256 }
1257 WifiLock wifiLock = new WifiLock(lockMode, tag, binder, ws);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001258 synchronized (mLocks) {
1259 return acquireWifiLockLocked(wifiLock);
1260 }
1261 }
1262
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -07001263 private void noteAcquireWifiLock(WifiLock wifiLock) throws RemoteException {
1264 switch(wifiLock.mMode) {
1265 case WifiManager.WIFI_MODE_FULL:
Irfan Sheriff5876a422010-08-12 20:26:23 -07001266 case WifiManager.WIFI_MODE_FULL_HIGH_PERF:
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -07001267 mBatteryStats.noteFullWifiLockAcquiredFromSource(wifiLock.mWorkSource);
1268 break;
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -07001269 case WifiManager.WIFI_MODE_SCAN_ONLY:
1270 mBatteryStats.noteScanWifiLockAcquiredFromSource(wifiLock.mWorkSource);
1271 break;
1272 }
1273 }
1274
1275 private void noteReleaseWifiLock(WifiLock wifiLock) throws RemoteException {
1276 switch(wifiLock.mMode) {
1277 case WifiManager.WIFI_MODE_FULL:
Irfan Sheriff5876a422010-08-12 20:26:23 -07001278 case WifiManager.WIFI_MODE_FULL_HIGH_PERF:
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -07001279 mBatteryStats.noteFullWifiLockReleasedFromSource(wifiLock.mWorkSource);
1280 break;
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -07001281 case WifiManager.WIFI_MODE_SCAN_ONLY:
1282 mBatteryStats.noteScanWifiLockReleasedFromSource(wifiLock.mWorkSource);
1283 break;
1284 }
1285 }
1286
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001287 private boolean acquireWifiLockLocked(WifiLock wifiLock) {
Irfan Sheriffc89dd542010-09-28 08:40:54 -07001288 if (DBG) Slog.d(TAG, "acquireWifiLockLocked: " + wifiLock);
Robert Greenwaltf1acb2d2009-10-13 08:20:55 -07001289
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001290 mLocks.addLock(wifiLock);
Robert Greenwaltf1acb2d2009-10-13 08:20:55 -07001291
The Android Open Source Project10592532009-03-18 17:39:46 -07001292 long ident = Binder.clearCallingIdentity();
1293 try {
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -07001294 noteAcquireWifiLock(wifiLock);
Robert Greenwalt5347bd42009-05-13 15:10:16 -07001295 switch(wifiLock.mMode) {
Eric Shienbrood5711fad2009-03-27 20:25:31 -07001296 case WifiManager.WIFI_MODE_FULL:
1297 ++mFullLocksAcquired;
Eric Shienbrood5711fad2009-03-27 20:25:31 -07001298 break;
Irfan Sheriff5876a422010-08-12 20:26:23 -07001299 case WifiManager.WIFI_MODE_FULL_HIGH_PERF:
1300 ++mFullHighPerfLocksAcquired;
1301 break;
1302
Eric Shienbrood5711fad2009-03-27 20:25:31 -07001303 case WifiManager.WIFI_MODE_SCAN_ONLY:
1304 ++mScanLocksAcquired;
Eric Shienbrood5711fad2009-03-27 20:25:31 -07001305 break;
The Android Open Source Project10592532009-03-18 17:39:46 -07001306 }
Dianne Hackbornecfd7f72010-10-08 14:23:40 -07001307
1308 // Be aggressive about adding new locks into the accounted state...
1309 // we want to over-report rather than under-report.
1310 reportStartWorkSource();
1311
1312 updateWifiState();
1313 return true;
The Android Open Source Project10592532009-03-18 17:39:46 -07001314 } catch (RemoteException e) {
Dianne Hackbornecfd7f72010-10-08 14:23:40 -07001315 return false;
The Android Open Source Project10592532009-03-18 17:39:46 -07001316 } finally {
1317 Binder.restoreCallingIdentity(ident);
1318 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001319 }
1320
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -07001321 public void updateWifiLockWorkSource(IBinder lock, WorkSource ws) {
1322 int uid = Binder.getCallingUid();
1323 int pid = Binder.getCallingPid();
1324 if (ws != null && ws.size() == 0) {
1325 ws = null;
1326 }
1327 if (ws != null) {
1328 enforceWakeSourcePermission(uid, pid);
1329 }
1330 long ident = Binder.clearCallingIdentity();
1331 try {
1332 synchronized (mLocks) {
1333 int index = mLocks.findLockByBinder(lock);
1334 if (index < 0) {
1335 throw new IllegalArgumentException("Wifi lock not active");
1336 }
1337 WifiLock wl = mLocks.mList.get(index);
1338 noteReleaseWifiLock(wl);
1339 wl.mWorkSource = ws != null ? new WorkSource(ws) : new WorkSource(uid);
1340 noteAcquireWifiLock(wl);
1341 }
1342 } catch (RemoteException e) {
1343 } finally {
1344 Binder.restoreCallingIdentity(ident);
1345 }
1346 }
1347
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001348 public boolean releaseWifiLock(IBinder lock) {
1349 mContext.enforceCallingOrSelfPermission(android.Manifest.permission.WAKE_LOCK, null);
1350 synchronized (mLocks) {
1351 return releaseWifiLockLocked(lock);
1352 }
1353 }
1354
1355 private boolean releaseWifiLockLocked(IBinder lock) {
Eric Shienbroodd4c5f892009-03-24 18:13:20 -07001356 boolean hadLock;
Robert Greenwaltf1acb2d2009-10-13 08:20:55 -07001357
The Android Open Source Project10592532009-03-18 17:39:46 -07001358 WifiLock wifiLock = mLocks.removeLock(lock);
Robert Greenwaltf1acb2d2009-10-13 08:20:55 -07001359
Irfan Sheriffc89dd542010-09-28 08:40:54 -07001360 if (DBG) Slog.d(TAG, "releaseWifiLockLocked: " + wifiLock);
Robert Greenwaltf1acb2d2009-10-13 08:20:55 -07001361
Eric Shienbroodd4c5f892009-03-24 18:13:20 -07001362 hadLock = (wifiLock != null);
1363
Dianne Hackbornecfd7f72010-10-08 14:23:40 -07001364 long ident = Binder.clearCallingIdentity();
1365 try {
1366 if (hadLock) {
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -07001367 noteAcquireWifiLock(wifiLock);
Robert Greenwalt5347bd42009-05-13 15:10:16 -07001368 switch(wifiLock.mMode) {
Eric Shienbrood5711fad2009-03-27 20:25:31 -07001369 case WifiManager.WIFI_MODE_FULL:
1370 ++mFullLocksReleased;
Eric Shienbrood5711fad2009-03-27 20:25:31 -07001371 break;
Irfan Sheriff5876a422010-08-12 20:26:23 -07001372 case WifiManager.WIFI_MODE_FULL_HIGH_PERF:
1373 ++mFullHighPerfLocksReleased;
1374 break;
Eric Shienbrood5711fad2009-03-27 20:25:31 -07001375 case WifiManager.WIFI_MODE_SCAN_ONLY:
1376 ++mScanLocksReleased;
Eric Shienbrood5711fad2009-03-27 20:25:31 -07001377 break;
Eric Shienbroodd4c5f892009-03-24 18:13:20 -07001378 }
The Android Open Source Project10592532009-03-18 17:39:46 -07001379 }
Dianne Hackbornecfd7f72010-10-08 14:23:40 -07001380
1381 // TODO - should this only happen if you hadLock?
1382 updateWifiState();
1383
1384 } catch (RemoteException e) {
1385 } finally {
1386 Binder.restoreCallingIdentity(ident);
The Android Open Source Project10592532009-03-18 17:39:46 -07001387 }
Dianne Hackbornecfd7f72010-10-08 14:23:40 -07001388
Eric Shienbroodd4c5f892009-03-24 18:13:20 -07001389 return hadLock;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001390 }
Robert Greenwalt5347bd42009-05-13 15:10:16 -07001391
Robert Greenwalt58ff0212009-05-19 15:53:54 -07001392 private abstract class DeathRecipient
Robert Greenwalt5347bd42009-05-13 15:10:16 -07001393 implements IBinder.DeathRecipient {
1394 String mTag;
1395 int mMode;
1396 IBinder mBinder;
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -07001397 WorkSource mWorkSource;
Robert Greenwalt5347bd42009-05-13 15:10:16 -07001398
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -07001399 DeathRecipient(int mode, String tag, IBinder binder, WorkSource ws) {
Robert Greenwalt5347bd42009-05-13 15:10:16 -07001400 super();
1401 mTag = tag;
1402 mMode = mode;
1403 mBinder = binder;
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -07001404 mWorkSource = ws;
Robert Greenwalt5347bd42009-05-13 15:10:16 -07001405 try {
1406 mBinder.linkToDeath(this, 0);
1407 } catch (RemoteException e) {
1408 binderDied();
1409 }
1410 }
Suchi Amalapurapufff2fda2009-06-30 21:36:16 -07001411
1412 void unlinkDeathRecipient() {
1413 mBinder.unlinkToDeath(this, 0);
1414 }
Robert Greenwalt5347bd42009-05-13 15:10:16 -07001415 }
1416
Robert Greenwalt58ff0212009-05-19 15:53:54 -07001417 private class Multicaster extends DeathRecipient {
1418 Multicaster(String tag, IBinder binder) {
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -07001419 super(Binder.getCallingUid(), tag, binder, null);
Robert Greenwalt5347bd42009-05-13 15:10:16 -07001420 }
1421
1422 public void binderDied() {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001423 Slog.e(TAG, "Multicaster binderDied");
Robert Greenwalt5347bd42009-05-13 15:10:16 -07001424 synchronized (mMulticasters) {
1425 int i = mMulticasters.indexOf(this);
1426 if (i != -1) {
1427 removeMulticasterLocked(i, mMode);
1428 }
1429 }
1430 }
1431
1432 public String toString() {
Robert Greenwalt58ff0212009-05-19 15:53:54 -07001433 return "Multicaster{" + mTag + " binder=" + mBinder + "}";
Robert Greenwalt5347bd42009-05-13 15:10:16 -07001434 }
1435
1436 public int getUid() {
1437 return mMode;
1438 }
1439 }
1440
Robert Greenwalte2d155a2009-10-21 14:58:34 -07001441 public void initializeMulticastFiltering() {
1442 enforceMulticastChangePermission();
Irfan Sheriffa8fbe1f2010-03-09 09:13:58 -08001443
Robert Greenwalte2d155a2009-10-21 14:58:34 -07001444 synchronized (mMulticasters) {
1445 // if anybody had requested filters be off, leave off
1446 if (mMulticasters.size() != 0) {
1447 return;
1448 } else {
Irfan Sheriff0d255342010-07-28 09:35:20 -07001449 mWifiStateMachine.startPacketFiltering();
Robert Greenwalte2d155a2009-10-21 14:58:34 -07001450 }
1451 }
1452 }
1453
Robert Greenwaltfc1b15c2009-05-22 15:09:51 -07001454 public void acquireMulticastLock(IBinder binder, String tag) {
1455 enforceMulticastChangePermission();
Robert Greenwalt5347bd42009-05-13 15:10:16 -07001456
1457 synchronized (mMulticasters) {
1458 mMulticastEnabled++;
Robert Greenwalt58ff0212009-05-19 15:53:54 -07001459 mMulticasters.add(new Multicaster(tag, binder));
Robert Greenwalt5347bd42009-05-13 15:10:16 -07001460 // Note that we could call stopPacketFiltering only when
1461 // our new size == 1 (first call), but this function won't
1462 // be called often and by making the stopPacket call each
1463 // time we're less fragile and self-healing.
Irfan Sheriff0d255342010-07-28 09:35:20 -07001464 mWifiStateMachine.stopPacketFiltering();
Robert Greenwalt5347bd42009-05-13 15:10:16 -07001465 }
1466
1467 int uid = Binder.getCallingUid();
1468 Long ident = Binder.clearCallingIdentity();
1469 try {
1470 mBatteryStats.noteWifiMulticastEnabled(uid);
1471 } catch (RemoteException e) {
1472 } finally {
1473 Binder.restoreCallingIdentity(ident);
1474 }
1475 }
1476
Robert Greenwaltfc1b15c2009-05-22 15:09:51 -07001477 public void releaseMulticastLock() {
1478 enforceMulticastChangePermission();
Robert Greenwalt5347bd42009-05-13 15:10:16 -07001479
1480 int uid = Binder.getCallingUid();
1481 synchronized (mMulticasters) {
1482 mMulticastDisabled++;
1483 int size = mMulticasters.size();
1484 for (int i = size - 1; i >= 0; i--) {
Robert Greenwalt58ff0212009-05-19 15:53:54 -07001485 Multicaster m = mMulticasters.get(i);
Robert Greenwalt5347bd42009-05-13 15:10:16 -07001486 if ((m != null) && (m.getUid() == uid)) {
1487 removeMulticasterLocked(i, uid);
1488 }
1489 }
1490 }
1491 }
1492
1493 private void removeMulticasterLocked(int i, int uid)
1494 {
Suchi Amalapurapufff2fda2009-06-30 21:36:16 -07001495 Multicaster removed = mMulticasters.remove(i);
Irfan Sheriffa8fbe1f2010-03-09 09:13:58 -08001496
Suchi Amalapurapufff2fda2009-06-30 21:36:16 -07001497 if (removed != null) {
1498 removed.unlinkDeathRecipient();
1499 }
Robert Greenwalt5347bd42009-05-13 15:10:16 -07001500 if (mMulticasters.size() == 0) {
Irfan Sheriff0d255342010-07-28 09:35:20 -07001501 mWifiStateMachine.startPacketFiltering();
Robert Greenwalt5347bd42009-05-13 15:10:16 -07001502 }
1503
1504 Long ident = Binder.clearCallingIdentity();
1505 try {
1506 mBatteryStats.noteWifiMulticastDisabled(uid);
1507 } catch (RemoteException e) {
1508 } finally {
1509 Binder.restoreCallingIdentity(ident);
1510 }
1511 }
1512
Robert Greenwalt58ff0212009-05-19 15:53:54 -07001513 public boolean isMulticastEnabled() {
Robert Greenwalt5347bd42009-05-13 15:10:16 -07001514 enforceAccessPermission();
1515
1516 synchronized (mMulticasters) {
1517 return (mMulticasters.size() > 0);
1518 }
1519 }
Irfan Sheriff0d255342010-07-28 09:35:20 -07001520
Irfan Sheriff227bec42011-02-15 19:30:27 -08001521 /**
1522 * Evaluate if traffic stats polling is needed based on
1523 * connection and screen on status
1524 */
1525 private void evaluateTrafficStatsPolling() {
1526 Message msg;
1527 if (mNetworkInfo.getDetailedState() == DetailedState.CONNECTED && !mScreenOff) {
Irfan Sheriffebe606f2011-02-24 11:39:15 -08001528 msg = Message.obtain(mAsyncServiceHandler,
1529 WifiManager.CMD_ENABLE_TRAFFIC_STATS_POLL, 1, 0);
Irfan Sheriff227bec42011-02-15 19:30:27 -08001530 } else {
Irfan Sheriffebe606f2011-02-24 11:39:15 -08001531 msg = Message.obtain(mAsyncServiceHandler,
1532 WifiManager.CMD_ENABLE_TRAFFIC_STATS_POLL, 0, 0);
Irfan Sheriff227bec42011-02-15 19:30:27 -08001533 }
1534 msg.sendToTarget();
1535 }
1536
1537 private void notifyOnDataActivity() {
1538 long sent, received;
1539 long preTxPkts = mTxPkts, preRxPkts = mRxPkts;
1540 int dataActivity = WifiManager.DATA_ACTIVITY_NONE;
1541
1542 mTxPkts = TrafficStats.getTxPackets(mInterfaceName);
1543 mRxPkts = TrafficStats.getRxPackets(mInterfaceName);
1544
1545 if (preTxPkts > 0 || preRxPkts > 0) {
1546 sent = mTxPkts - preTxPkts;
1547 received = mRxPkts - preRxPkts;
1548 if (sent > 0) {
1549 dataActivity |= WifiManager.DATA_ACTIVITY_OUT;
1550 }
1551 if (received > 0) {
1552 dataActivity |= WifiManager.DATA_ACTIVITY_IN;
1553 }
1554
1555 if (dataActivity != mDataActivity && !mScreenOff) {
1556 mDataActivity = dataActivity;
1557 for (AsyncChannel client : mClients) {
1558 client.sendMessage(WifiManager.DATA_ACTIVITY_NOTIFICATION, mDataActivity);
1559 }
1560 }
1561 }
1562 }
1563
1564
Irfan Sheriff0d255342010-07-28 09:35:20 -07001565 private void checkAndSetNotification() {
1566 // If we shouldn't place a notification on available networks, then
1567 // don't bother doing any of the following
1568 if (!mNotificationEnabled) return;
1569
1570 State state = mNetworkInfo.getState();
1571 if ((state == NetworkInfo.State.DISCONNECTED)
1572 || (state == NetworkInfo.State.UNKNOWN)) {
1573 // Look for an open network
Irfan Sheriffd8134ff2010-08-22 17:06:34 -07001574 List<ScanResult> scanResults = mWifiStateMachine.syncGetScanResultsList();
Irfan Sheriff0d255342010-07-28 09:35:20 -07001575 if (scanResults != null) {
1576 int numOpenNetworks = 0;
1577 for (int i = scanResults.size() - 1; i >= 0; i--) {
1578 ScanResult scanResult = scanResults.get(i);
1579
1580 if (TextUtils.isEmpty(scanResult.capabilities)) {
1581 numOpenNetworks++;
1582 }
1583 }
1584
1585 if (numOpenNetworks > 0) {
1586 if (++mNumScansSinceNetworkStateChange >= NUM_SCANS_BEFORE_ACTUALLY_SCANNING) {
1587 /*
1588 * We've scanned continuously at least
1589 * NUM_SCANS_BEFORE_NOTIFICATION times. The user
1590 * probably does not have a remembered network in range,
1591 * since otherwise supplicant would have tried to
1592 * associate and thus resetting this counter.
1593 */
1594 setNotificationVisible(true, numOpenNetworks, false, 0);
1595 }
1596 return;
1597 }
1598 }
1599 }
1600
1601 // No open networks in range, remove the notification
1602 setNotificationVisible(false, 0, false, 0);
1603 }
1604
1605 /**
1606 * Clears variables related to tracking whether a notification has been
1607 * shown recently and clears the current notification.
1608 */
1609 private void resetNotification() {
1610 mNotificationRepeatTime = 0;
1611 mNumScansSinceNetworkStateChange = 0;
1612 setNotificationVisible(false, 0, false, 0);
1613 }
1614
1615 /**
1616 * Display or don't display a notification that there are open Wi-Fi networks.
1617 * @param visible {@code true} if notification should be visible, {@code false} otherwise
1618 * @param numNetworks the number networks seen
1619 * @param force {@code true} to force notification to be shown/not-shown,
1620 * even if it is already shown/not-shown.
1621 * @param delay time in milliseconds after which the notification should be made
1622 * visible or invisible.
1623 */
1624 private void setNotificationVisible(boolean visible, int numNetworks, boolean force,
1625 int delay) {
1626
1627 // Since we use auto cancel on the notification, when the
1628 // mNetworksAvailableNotificationShown is true, the notification may
1629 // have actually been canceled. However, when it is false we know
1630 // for sure that it is not being shown (it will not be shown any other
1631 // place than here)
1632
1633 // If it should be hidden and it is already hidden, then noop
1634 if (!visible && !mNotificationShown && !force) {
1635 return;
1636 }
1637
1638 NotificationManager notificationManager = (NotificationManager) mContext
1639 .getSystemService(Context.NOTIFICATION_SERVICE);
1640
1641 Message message;
1642 if (visible) {
1643
1644 // Not enough time has passed to show the notification again
1645 if (System.currentTimeMillis() < mNotificationRepeatTime) {
1646 return;
1647 }
1648
1649 if (mNotification == null) {
Wink Savillec7a98342010-08-13 16:11:42 -07001650 // Cache the Notification object.
Irfan Sheriff0d255342010-07-28 09:35:20 -07001651 mNotification = new Notification();
1652 mNotification.when = 0;
1653 mNotification.icon = ICON_NETWORKS_AVAILABLE;
1654 mNotification.flags = Notification.FLAG_AUTO_CANCEL;
1655 mNotification.contentIntent = PendingIntent.getActivity(mContext, 0,
1656 new Intent(WifiManager.ACTION_PICK_WIFI_NETWORK), 0);
1657 }
1658
1659 CharSequence title = mContext.getResources().getQuantityText(
1660 com.android.internal.R.plurals.wifi_available, numNetworks);
1661 CharSequence details = mContext.getResources().getQuantityText(
1662 com.android.internal.R.plurals.wifi_available_detailed, numNetworks);
1663 mNotification.tickerText = title;
1664 mNotification.setLatestEventInfo(mContext, title, details, mNotification.contentIntent);
1665
1666 mNotificationRepeatTime = System.currentTimeMillis() + NOTIFICATION_REPEAT_DELAY_MS;
1667
1668 notificationManager.notify(ICON_NETWORKS_AVAILABLE, mNotification);
Irfan Sheriff0d255342010-07-28 09:35:20 -07001669 } else {
Irfan Sheriff0d255342010-07-28 09:35:20 -07001670 notificationManager.cancel(ICON_NETWORKS_AVAILABLE);
Irfan Sheriff0d255342010-07-28 09:35:20 -07001671 }
1672
Irfan Sheriff0d255342010-07-28 09:35:20 -07001673 mNotificationShown = visible;
1674 }
1675
1676 private class NotificationEnabledSettingObserver extends ContentObserver {
1677
1678 public NotificationEnabledSettingObserver(Handler handler) {
1679 super(handler);
1680 }
1681
1682 public void register() {
1683 ContentResolver cr = mContext.getContentResolver();
1684 cr.registerContentObserver(Settings.Secure.getUriFor(
1685 Settings.Secure.WIFI_NETWORKS_AVAILABLE_NOTIFICATION_ON), true, this);
1686 mNotificationEnabled = getValue();
1687 }
1688
1689 @Override
1690 public void onChange(boolean selfChange) {
1691 super.onChange(selfChange);
1692
1693 mNotificationEnabled = getValue();
1694 resetNotification();
1695 }
1696
1697 private boolean getValue() {
1698 return Settings.Secure.getInt(mContext.getContentResolver(),
1699 Settings.Secure.WIFI_NETWORKS_AVAILABLE_NOTIFICATION_ON, 1) == 1;
1700 }
1701 }
1702
1703
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001704}