| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1 | /* |
| Irfan Sheriff | a2a1b91 | 2010-06-07 09:03:04 -0700 | [diff] [blame] | 2 | * Copyright (C) 2010 The Android Open Source Project |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3 | * |
| 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 | |
| 17 | package com.android.server; |
| 18 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 19 | import android.app.AlarmManager; |
| Irfan Sheriff | 0d25534 | 2010-07-28 09:35:20 -0700 | [diff] [blame] | 20 | import android.app.Notification; |
| 21 | import android.app.NotificationManager; |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 22 | import android.app.PendingIntent; |
| Jaikumar Ganesh | 7440fc2 | 2010-09-27 17:04:14 -0700 | [diff] [blame] | 23 | import android.bluetooth.BluetoothAdapter; |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 24 | import android.content.BroadcastReceiver; |
| 25 | import android.content.ContentResolver; |
| 26 | import android.content.Context; |
| 27 | import android.content.Intent; |
| 28 | import android.content.IntentFilter; |
| 29 | import android.content.pm.PackageManager; |
| Irfan Sheriff | 0d25534 | 2010-07-28 09:35:20 -0700 | [diff] [blame] | 30 | import android.database.ContentObserver; |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 31 | import android.net.wifi.IWifiManager; |
| 32 | import android.net.wifi.WifiInfo; |
| 33 | import android.net.wifi.WifiManager; |
| Irfan Sheriff | 0d25534 | 2010-07-28 09:35:20 -0700 | [diff] [blame] | 34 | import android.net.wifi.WifiStateMachine; |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 35 | import android.net.wifi.ScanResult; |
| 36 | import android.net.wifi.WifiConfiguration; |
| San Mehat | 0310f9a | 2009-07-07 10:49:47 -0700 | [diff] [blame] | 37 | import android.net.wifi.SupplicantState; |
| Irfan Sheriff | 9ab518ad | 2010-03-12 15:48:17 -0800 | [diff] [blame] | 38 | import android.net.wifi.WifiConfiguration.KeyMgmt; |
| Irfan Sheriff | 02fb46a | 2010-12-08 11:27:37 -0800 | [diff] [blame] | 39 | import android.net.wifi.WpsConfiguration; |
| Irfan Sheriff | e4c56c9 | 2011-01-12 16:33:58 -0800 | [diff] [blame] | 40 | import android.net.wifi.WpsResult; |
| Irfan Sheriff | 5321aef | 2010-02-12 12:35:59 -0800 | [diff] [blame] | 41 | import android.net.ConnectivityManager; |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 42 | import android.net.DhcpInfo; |
| Irfan Sheriff | 0d25534 | 2010-07-28 09:35:20 -0700 | [diff] [blame] | 43 | import android.net.NetworkInfo; |
| 44 | import android.net.NetworkInfo.State; |
| Irfan Sheriff | 227bec4 | 2011-02-15 19:30:27 -0800 | [diff] [blame] | 45 | import android.net.NetworkInfo.DetailedState; |
| 46 | import android.net.TrafficStats; |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 47 | import android.os.Binder; |
| Irfan Sheriff | 0d25534 | 2010-07-28 09:35:20 -0700 | [diff] [blame] | 48 | import android.os.Handler; |
| Irfan Sheriff | 227bec4 | 2011-02-15 19:30:27 -0800 | [diff] [blame] | 49 | import android.os.Messenger; |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 50 | import android.os.HandlerThread; |
| 51 | import android.os.IBinder; |
| Irfan Sheriff | 5321aef | 2010-02-12 12:35:59 -0800 | [diff] [blame] | 52 | import android.os.INetworkManagementService; |
| Irfan Sheriff | 0d25534 | 2010-07-28 09:35:20 -0700 | [diff] [blame] | 53 | import android.os.Message; |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 54 | import android.os.RemoteException; |
| Amith Yamasani | 47873e5 | 2009-07-02 12:05:32 -0700 | [diff] [blame] | 55 | import android.os.ServiceManager; |
| Irfan Sheriff | 227bec4 | 2011-02-15 19:30:27 -0800 | [diff] [blame] | 56 | import android.os.SystemProperties; |
| Dianne Hackborn | 7e9f4eb | 2010-09-10 18:43:00 -0700 | [diff] [blame] | 57 | import android.os.WorkSource; |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 58 | import android.provider.Settings; |
| Irfan Sheriff | 0d25534 | 2010-07-28 09:35:20 -0700 | [diff] [blame] | 59 | import android.text.TextUtils; |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 60 | import android.util.Slog; |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 61 | |
| 62 | import java.util.ArrayList; |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 63 | import java.util.List; |
| Jaikumar Ganesh | 084c665 | 2009-12-07 10:58:18 -0800 | [diff] [blame] | 64 | import java.util.Set; |
| Irfan Sheriff | a2a1b91 | 2010-06-07 09:03:04 -0700 | [diff] [blame] | 65 | import java.util.concurrent.atomic.AtomicBoolean; |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 66 | import java.io.FileDescriptor; |
| 67 | import java.io.PrintWriter; |
| 68 | |
| The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 69 | import com.android.internal.app.IBatteryStats; |
| Wink Saville | 4b7ba09 | 2010-10-20 15:37:41 -0700 | [diff] [blame] | 70 | import com.android.internal.util.AsyncChannel; |
| The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 71 | import com.android.server.am.BatteryStatsService; |
| Irfan Sheriff | 9ab518ad | 2010-03-12 15:48:17 -0800 | [diff] [blame] | 72 | import com.android.internal.R; |
| The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 73 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 74 | /** |
| 75 | * WifiService handles remote WiFi operation requests by implementing |
| Irfan Sheriff | a2a1b91 | 2010-06-07 09:03:04 -0700 | [diff] [blame] | 76 | * the IWifiManager interface. |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 77 | * |
| 78 | * @hide |
| 79 | */ |
| Irfan Sheriff | a2a1b91 | 2010-06-07 09:03:04 -0700 | [diff] [blame] | 80 | //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 Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 84 | public class WifiService extends IWifiManager.Stub { |
| 85 | private static final String TAG = "WifiService"; |
| Irfan Sheriff | a2a1b91 | 2010-06-07 09:03:04 -0700 | [diff] [blame] | 86 | private static final boolean DBG = true; |
| 87 | |
| Irfan Sheriff | 0d25534 | 2010-07-28 09:35:20 -0700 | [diff] [blame] | 88 | private final WifiStateMachine mWifiStateMachine; |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 89 | |
| 90 | private Context mContext; |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 91 | |
| 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 Sheriff | fcc0845 | 2011-02-17 16:44:54 -0800 | [diff] [blame^] | 99 | /* Chipset supports background scan */ |
| 100 | private final boolean mBackgroundScanSupported; |
| 101 | |
| Mike Lockwood | bd5ddf0 | 2009-07-29 21:37:14 -0700 | [diff] [blame] | 102 | // true if the user enabled Wifi while in airplane mode |
| Irfan Sheriff | a2a1b91 | 2010-06-07 09:03:04 -0700 | [diff] [blame] | 103 | private AtomicBoolean mAirplaneModeOverwridden = new AtomicBoolean(false); |
| Mike Lockwood | bd5ddf0 | 2009-07-29 21:37:14 -0700 | [diff] [blame] | 104 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 105 | private final LockList mLocks = new LockList(); |
| Eric Shienbrood | 5711fad | 2009-03-27 20:25:31 -0700 | [diff] [blame] | 106 | // some wifi lock statistics |
| Irfan Sheriff | 5876a42 | 2010-08-12 20:26:23 -0700 | [diff] [blame] | 107 | private int mFullHighPerfLocksAcquired; |
| 108 | private int mFullHighPerfLocksReleased; |
| Eric Shienbrood | 5711fad | 2009-03-27 20:25:31 -0700 | [diff] [blame] | 109 | private int mFullLocksAcquired; |
| 110 | private int mFullLocksReleased; |
| 111 | private int mScanLocksAcquired; |
| 112 | private int mScanLocksReleased; |
| The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 113 | |
| Robert Greenwalt | 58ff021 | 2009-05-19 15:53:54 -0700 | [diff] [blame] | 114 | private final List<Multicaster> mMulticasters = |
| 115 | new ArrayList<Multicaster>(); |
| Robert Greenwalt | 5347bd4 | 2009-05-13 15:10:16 -0700 | [diff] [blame] | 116 | private int mMulticastEnabled; |
| 117 | private int mMulticastDisabled; |
| 118 | |
| The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 119 | private final IBatteryStats mBatteryStats; |
| Jaikumar Ganesh | 084c665 | 2009-12-07 10:58:18 -0800 | [diff] [blame] | 120 | |
| Irfan Sheriff | 227bec4 | 2011-02-15 19:30:27 -0800 | [diff] [blame] | 121 | 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 Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 135 | /** |
| Doug Zongker | 43866e0 | 2010-01-07 12:09:54 -0800 | [diff] [blame] | 136 | * 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 Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 138 | * 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 Sheriff | 4f5f7c9 | 2010-10-14 17:01:27 -0700 | [diff] [blame] | 142 | private static final long DEFAULT_IDLE_MS = 15 * 60 * 1000; /* 15 minutes */ |
| 143 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 144 | private static final String ACTION_DEVICE_IDLE = |
| 145 | "com.android.server.WifiManager.action.DEVICE_IDLE"; |
| 146 | |
| Irfan Sheriff | 227bec4 | 2011-02-15 19:30:27 -0800 | [diff] [blame] | 147 | private static final int CMD_ENABLE_TRAFFIC_STATS_POLL = 1; |
| 148 | private static final int CMD_TRAFFIC_STATS_POLL = 2; |
| 149 | |
| Irfan Sheriff | a2a1b91 | 2010-06-07 09:03:04 -0700 | [diff] [blame] | 150 | private boolean mIsReceiverRegistered = false; |
| 151 | |
| Irfan Sheriff | 0d25534 | 2010-07-28 09:35:20 -0700 | [diff] [blame] | 152 | |
| 153 | NetworkInfo mNetworkInfo = new NetworkInfo(ConnectivityManager.TYPE_WIFI, 0, "WIFI", ""); |
| 154 | |
| 155 | // Variables relating to the 'available networks' notification |
| 156 | /** |
| 157 | * The icon to show in the 'available networks' notification. This will also |
| 158 | * be the ID of the Notification given to the NotificationManager. |
| 159 | */ |
| 160 | private static final int ICON_NETWORKS_AVAILABLE = |
| 161 | com.android.internal.R.drawable.stat_notify_wifi_in_range; |
| 162 | /** |
| 163 | * When a notification is shown, we wait this amount before possibly showing it again. |
| 164 | */ |
| 165 | private final long NOTIFICATION_REPEAT_DELAY_MS; |
| 166 | /** |
| 167 | * Whether the user has set the setting to show the 'available networks' notification. |
| 168 | */ |
| 169 | private boolean mNotificationEnabled; |
| 170 | /** |
| 171 | * Observes the user setting to keep {@link #mNotificationEnabled} in sync. |
| 172 | */ |
| 173 | private NotificationEnabledSettingObserver mNotificationEnabledSettingObserver; |
| 174 | /** |
| 175 | * The {@link System#currentTimeMillis()} must be at least this value for us |
| 176 | * to show the notification again. |
| 177 | */ |
| 178 | private long mNotificationRepeatTime; |
| 179 | /** |
| 180 | * The Notification object given to the NotificationManager. |
| 181 | */ |
| 182 | private Notification mNotification; |
| 183 | /** |
| 184 | * Whether the notification is being shown, as set by us. That is, if the |
| 185 | * user cancels the notification, we will not receive the callback so this |
| 186 | * will still be true. We only guarantee if this is false, then the |
| 187 | * notification is not showing. |
| 188 | */ |
| 189 | private boolean mNotificationShown; |
| 190 | /** |
| 191 | * The number of continuous scans that must occur before consider the |
| 192 | * supplicant in a scanning state. This allows supplicant to associate with |
| 193 | * remembered networks that are in the scan results. |
| 194 | */ |
| 195 | private static final int NUM_SCANS_BEFORE_ACTUALLY_SCANNING = 3; |
| 196 | /** |
| 197 | * The number of scans since the last network state change. When this |
| 198 | * exceeds {@link #NUM_SCANS_BEFORE_ACTUALLY_SCANNING}, we consider the |
| 199 | * supplicant to actually be scanning. When the network state changes to |
| 200 | * something other than scanning, we reset this to 0. |
| 201 | */ |
| 202 | private int mNumScansSinceNetworkStateChange; |
| Jaikumar Ganesh | 7440fc2 | 2010-09-27 17:04:14 -0700 | [diff] [blame] | 203 | |
| Dianne Hackborn | 03f3cb0 | 2010-09-17 23:12:26 -0700 | [diff] [blame] | 204 | /** |
| Wink Saville | 4b7ba09 | 2010-10-20 15:37:41 -0700 | [diff] [blame] | 205 | * Asynchronous channel to WifiStateMachine |
| 206 | */ |
| Irfan Sheriff | 227bec4 | 2011-02-15 19:30:27 -0800 | [diff] [blame] | 207 | private AsyncChannel mWifiStateMachineChannel; |
| Wink Saville | 4b7ba09 | 2010-10-20 15:37:41 -0700 | [diff] [blame] | 208 | |
| 209 | /** |
| Irfan Sheriff | 227bec4 | 2011-02-15 19:30:27 -0800 | [diff] [blame] | 210 | * Clients receiving asynchronous messages |
| Wink Saville | 4b7ba09 | 2010-10-20 15:37:41 -0700 | [diff] [blame] | 211 | */ |
| Irfan Sheriff | 227bec4 | 2011-02-15 19:30:27 -0800 | [diff] [blame] | 212 | private List<AsyncChannel> mClients = new ArrayList<AsyncChannel>(); |
| Wink Saville | 4b7ba09 | 2010-10-20 15:37:41 -0700 | [diff] [blame] | 213 | |
| Irfan Sheriff | 227bec4 | 2011-02-15 19:30:27 -0800 | [diff] [blame] | 214 | /** |
| 215 | * Handles client connections |
| 216 | */ |
| 217 | private class AsyncServiceHandler extends Handler { |
| 218 | |
| 219 | AsyncServiceHandler(android.os.Looper looper) { |
| Wink Saville | 4b7ba09 | 2010-10-20 15:37:41 -0700 | [diff] [blame] | 220 | super(looper); |
| Wink Saville | 4b7ba09 | 2010-10-20 15:37:41 -0700 | [diff] [blame] | 221 | } |
| 222 | |
| 223 | @Override |
| 224 | public void handleMessage(Message msg) { |
| 225 | switch (msg.what) { |
| 226 | case AsyncChannel.CMD_CHANNEL_HALF_CONNECTED: { |
| 227 | if (msg.arg1 == AsyncChannel.STATUS_SUCCESSFUL) { |
| Irfan Sheriff | 227bec4 | 2011-02-15 19:30:27 -0800 | [diff] [blame] | 228 | Slog.d(TAG, "New client listening to asynchronous messages"); |
| 229 | mClients.add((AsyncChannel) msg.obj); |
| Wink Saville | 4b7ba09 | 2010-10-20 15:37:41 -0700 | [diff] [blame] | 230 | } else { |
| Irfan Sheriff | 227bec4 | 2011-02-15 19:30:27 -0800 | [diff] [blame] | 231 | Slog.e(TAG, "Client connection failure, error=" + msg.arg1); |
| 232 | } |
| 233 | break; |
| 234 | } |
| 235 | case AsyncChannel.CMD_CHANNEL_FULL_CONNECTION: { |
| 236 | AsyncChannel ac = new AsyncChannel(); |
| 237 | ac.connect(mContext, this, msg.replyTo); |
| 238 | break; |
| 239 | } |
| 240 | case CMD_ENABLE_TRAFFIC_STATS_POLL: { |
| 241 | mEnableTrafficStatsPoll = (msg.arg1 == 1); |
| 242 | mTrafficStatsPollToken++; |
| 243 | if (mEnableTrafficStatsPoll) { |
| 244 | notifyOnDataActivity(); |
| 245 | sendMessageDelayed(Message.obtain(this, CMD_TRAFFIC_STATS_POLL, |
| 246 | mTrafficStatsPollToken, 0), POLL_TRAFFIC_STATS_INTERVAL_MSECS); |
| 247 | } |
| 248 | break; |
| 249 | } |
| 250 | case CMD_TRAFFIC_STATS_POLL: { |
| 251 | if (msg.arg1 == mTrafficStatsPollToken) { |
| 252 | notifyOnDataActivity(); |
| 253 | sendMessageDelayed(Message.obtain(this, CMD_TRAFFIC_STATS_POLL, |
| 254 | mTrafficStatsPollToken, 0), POLL_TRAFFIC_STATS_INTERVAL_MSECS); |
| Wink Saville | 4b7ba09 | 2010-10-20 15:37:41 -0700 | [diff] [blame] | 255 | } |
| 256 | break; |
| 257 | } |
| 258 | default: { |
| 259 | Slog.d(TAG, "WifiServicehandler.handleMessage ignoring msg=" + msg); |
| 260 | break; |
| 261 | } |
| 262 | } |
| 263 | } |
| 264 | } |
| Irfan Sheriff | 227bec4 | 2011-02-15 19:30:27 -0800 | [diff] [blame] | 265 | private AsyncServiceHandler mAsyncServiceHandler; |
| 266 | |
| 267 | /** |
| 268 | * Handles interaction with WifiStateMachine |
| 269 | */ |
| 270 | private class WifiStateMachineHandler extends Handler { |
| 271 | private AsyncChannel mWsmChannel; |
| 272 | |
| 273 | WifiStateMachineHandler(android.os.Looper looper) { |
| 274 | super(looper); |
| 275 | mWsmChannel = new AsyncChannel(); |
| 276 | mWsmChannel.connect(mContext, this, mWifiStateMachine.getHandler()); |
| 277 | } |
| 278 | |
| 279 | @Override |
| 280 | public void handleMessage(Message msg) { |
| 281 | switch (msg.what) { |
| 282 | case AsyncChannel.CMD_CHANNEL_HALF_CONNECTED: { |
| 283 | if (msg.arg1 == AsyncChannel.STATUS_SUCCESSFUL) { |
| 284 | mWifiStateMachineChannel = mWsmChannel; |
| 285 | } else { |
| 286 | Slog.e(TAG, "WifiStateMachine connection failure, error=" + msg.arg1); |
| 287 | mWifiStateMachineChannel = null; |
| 288 | } |
| 289 | break; |
| 290 | } |
| 291 | default: { |
| 292 | Slog.d(TAG, "WifiStateMachineHandler.handleMessage ignoring msg=" + msg); |
| 293 | break; |
| 294 | } |
| 295 | } |
| 296 | } |
| 297 | } |
| 298 | WifiStateMachineHandler mWifiStateMachineHandler; |
| Wink Saville | 4b7ba09 | 2010-10-20 15:37:41 -0700 | [diff] [blame] | 299 | |
| 300 | /** |
| Dianne Hackborn | 03f3cb0 | 2010-09-17 23:12:26 -0700 | [diff] [blame] | 301 | * Temporary for computing UIDS that are responsible for starting WIFI. |
| 302 | * Protected by mWifiStateTracker lock. |
| 303 | */ |
| 304 | private final WorkSource mTmpWorkSource = new WorkSource(); |
| Irfan Sheriff | 0d25534 | 2010-07-28 09:35:20 -0700 | [diff] [blame] | 305 | |
| 306 | WifiService(Context context) { |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 307 | mContext = context; |
| Irfan Sheriff | 227bec4 | 2011-02-15 19:30:27 -0800 | [diff] [blame] | 308 | |
| 309 | mInterfaceName = SystemProperties.get("wifi.interface", "wlan0"); |
| 310 | |
| 311 | mWifiStateMachine = new WifiStateMachine(mContext, mInterfaceName); |
| Irfan Sheriff | 0d25534 | 2010-07-28 09:35:20 -0700 | [diff] [blame] | 312 | mWifiStateMachine.enableRssiPolling(true); |
| The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 313 | mBatteryStats = BatteryStatsService.getService(); |
| Jaikumar Ganesh | 084c665 | 2009-12-07 10:58:18 -0800 | [diff] [blame] | 314 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 315 | mAlarmManager = (AlarmManager)mContext.getSystemService(Context.ALARM_SERVICE); |
| 316 | Intent idleIntent = new Intent(ACTION_DEVICE_IDLE, null); |
| 317 | mIdleIntent = PendingIntent.getBroadcast(mContext, IDLE_REQUEST, idleIntent, 0); |
| 318 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 319 | mContext.registerReceiver( |
| 320 | new BroadcastReceiver() { |
| 321 | @Override |
| 322 | public void onReceive(Context context, Intent intent) { |
| Mike Lockwood | bd5ddf0 | 2009-07-29 21:37:14 -0700 | [diff] [blame] | 323 | // clear our flag indicating the user has overwridden airplane mode |
| Irfan Sheriff | a2a1b91 | 2010-06-07 09:03:04 -0700 | [diff] [blame] | 324 | mAirplaneModeOverwridden.set(false); |
| Irfan Sheriff | b2e6c01 | 2010-04-05 11:57:56 -0700 | [diff] [blame] | 325 | // on airplane disable, restore Wifi if the saved state indicates so |
| 326 | if (!isAirplaneModeOn() && testAndClearWifiSavedState()) { |
| 327 | persistWifiEnabled(true); |
| 328 | } |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 329 | updateWifiState(); |
| 330 | } |
| 331 | }, |
| 332 | new IntentFilter(Intent.ACTION_AIRPLANE_MODE_CHANGED)); |
| 333 | |
| Irfan Sheriff | 0d25534 | 2010-07-28 09:35:20 -0700 | [diff] [blame] | 334 | IntentFilter filter = new IntentFilter(); |
| 335 | filter.addAction(WifiManager.WIFI_STATE_CHANGED_ACTION); |
| 336 | filter.addAction(WifiManager.NETWORK_STATE_CHANGED_ACTION); |
| 337 | filter.addAction(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION); |
| 338 | |
| 339 | mContext.registerReceiver( |
| 340 | new BroadcastReceiver() { |
| 341 | @Override |
| 342 | public void onReceive(Context context, Intent intent) { |
| 343 | if (intent.getAction().equals(WifiManager.WIFI_STATE_CHANGED_ACTION)) { |
| 344 | // reset & clear notification on any wifi state change |
| 345 | resetNotification(); |
| 346 | } else if (intent.getAction().equals( |
| 347 | WifiManager.NETWORK_STATE_CHANGED_ACTION)) { |
| 348 | mNetworkInfo = (NetworkInfo) intent.getParcelableExtra( |
| 349 | WifiManager.EXTRA_NETWORK_INFO); |
| 350 | // reset & clear notification on a network connect & disconnect |
| 351 | switch(mNetworkInfo.getDetailedState()) { |
| 352 | case CONNECTED: |
| 353 | case DISCONNECTED: |
| Irfan Sheriff | 227bec4 | 2011-02-15 19:30:27 -0800 | [diff] [blame] | 354 | evaluateTrafficStatsPolling(); |
| Irfan Sheriff | 0d25534 | 2010-07-28 09:35:20 -0700 | [diff] [blame] | 355 | resetNotification(); |
| 356 | break; |
| 357 | } |
| 358 | } else if (intent.getAction().equals( |
| 359 | WifiManager.SCAN_RESULTS_AVAILABLE_ACTION)) { |
| 360 | checkAndSetNotification(); |
| 361 | } |
| 362 | } |
| 363 | }, filter); |
| 364 | |
| Irfan Sheriff | 227bec4 | 2011-02-15 19:30:27 -0800 | [diff] [blame] | 365 | HandlerThread wifiThread = new HandlerThread("WifiService"); |
| 366 | wifiThread.start(); |
| 367 | mAsyncServiceHandler = new AsyncServiceHandler(wifiThread.getLooper()); |
| 368 | mWifiStateMachineHandler = new WifiStateMachineHandler(wifiThread.getLooper()); |
| 369 | |
| Irfan Sheriff | 0d25534 | 2010-07-28 09:35:20 -0700 | [diff] [blame] | 370 | // Setting is in seconds |
| 371 | NOTIFICATION_REPEAT_DELAY_MS = Settings.Secure.getInt(context.getContentResolver(), |
| 372 | Settings.Secure.WIFI_NETWORKS_AVAILABLE_REPEAT_DELAY, 900) * 1000l; |
| 373 | mNotificationEnabledSettingObserver = new NotificationEnabledSettingObserver(new Handler()); |
| 374 | mNotificationEnabledSettingObserver.register(); |
| Irfan Sheriff | fcc0845 | 2011-02-17 16:44:54 -0800 | [diff] [blame^] | 375 | |
| 376 | mBackgroundScanSupported = mContext.getResources().getBoolean( |
| 377 | com.android.internal.R.bool.config_wifi_background_scan_support); |
| Irfan Sheriff | 7b00978 | 2010-03-11 16:37:45 -0800 | [diff] [blame] | 378 | } |
| Irfan Sheriff | 5321aef | 2010-02-12 12:35:59 -0800 | [diff] [blame] | 379 | |
| Irfan Sheriff | 7b00978 | 2010-03-11 16:37:45 -0800 | [diff] [blame] | 380 | /** |
| 381 | * Check if Wi-Fi needs to be enabled and start |
| 382 | * if needed |
| Irfan Sheriff | 60e3ba0 | 2010-04-02 12:18:45 -0700 | [diff] [blame] | 383 | * |
| 384 | * This function is used only at boot time |
| Irfan Sheriff | 7b00978 | 2010-03-11 16:37:45 -0800 | [diff] [blame] | 385 | */ |
| Irfan Sheriff | 0d25534 | 2010-07-28 09:35:20 -0700 | [diff] [blame] | 386 | public void checkAndStartWifi() { |
| Irfan Sheriff | a3bd409 | 2010-03-24 17:58:59 -0700 | [diff] [blame] | 387 | /* Start if Wi-Fi is enabled or the saved state indicates Wi-Fi was on */ |
| Irfan Sheriff | 60e3ba0 | 2010-04-02 12:18:45 -0700 | [diff] [blame] | 388 | boolean wifiEnabled = !isAirplaneModeOn() |
| 389 | && (getPersistedWifiEnabled() || testAndClearWifiSavedState()); |
| Irfan Sheriff | 7b00978 | 2010-03-11 16:37:45 -0800 | [diff] [blame] | 390 | Slog.i(TAG, "WifiService starting up with Wi-Fi " + |
| 391 | (wifiEnabled ? "enabled" : "disabled")); |
| Irfan Sheriff | b99fe5e | 2010-03-26 14:56:07 -0700 | [diff] [blame] | 392 | setWifiEnabled(wifiEnabled); |
| Irfan Sheriff | 5321aef | 2010-02-12 12:35:59 -0800 | [diff] [blame] | 393 | } |
| 394 | |
| Irfan Sheriff | a3bd409 | 2010-03-24 17:58:59 -0700 | [diff] [blame] | 395 | private boolean testAndClearWifiSavedState() { |
| 396 | final ContentResolver cr = mContext.getContentResolver(); |
| 397 | int wifiSavedState = 0; |
| 398 | try { |
| 399 | wifiSavedState = Settings.Secure.getInt(cr, Settings.Secure.WIFI_SAVED_STATE); |
| 400 | if(wifiSavedState == 1) |
| 401 | Settings.Secure.putInt(cr, Settings.Secure.WIFI_SAVED_STATE, 0); |
| 402 | } catch (Settings.SettingNotFoundException e) { |
| 403 | ; |
| 404 | } |
| 405 | return (wifiSavedState == 1); |
| 406 | } |
| 407 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 408 | private boolean getPersistedWifiEnabled() { |
| 409 | final ContentResolver cr = mContext.getContentResolver(); |
| 410 | try { |
| 411 | return Settings.Secure.getInt(cr, Settings.Secure.WIFI_ON) == 1; |
| 412 | } catch (Settings.SettingNotFoundException e) { |
| 413 | Settings.Secure.putInt(cr, Settings.Secure.WIFI_ON, 0); |
| 414 | return false; |
| 415 | } |
| 416 | } |
| 417 | |
| 418 | private void persistWifiEnabled(boolean enabled) { |
| 419 | final ContentResolver cr = mContext.getContentResolver(); |
| 420 | Settings.Secure.putInt(cr, Settings.Secure.WIFI_ON, enabled ? 1 : 0); |
| 421 | } |
| 422 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 423 | /** |
| 424 | * see {@link android.net.wifi.WifiManager#pingSupplicant()} |
| Irfan Sheriff | a2a1b91 | 2010-06-07 09:03:04 -0700 | [diff] [blame] | 425 | * @return {@code true} if the operation succeeds, {@code false} otherwise |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 426 | */ |
| 427 | public boolean pingSupplicant() { |
| Irfan Sheriff | a2a1b91 | 2010-06-07 09:03:04 -0700 | [diff] [blame] | 428 | enforceAccessPermission(); |
| Irfan Sheriff | 227bec4 | 2011-02-15 19:30:27 -0800 | [diff] [blame] | 429 | if (mWifiStateMachineChannel != null) { |
| 430 | return mWifiStateMachine.syncPingSupplicant(mWifiStateMachineChannel); |
| Irfan Sheriff | 1406bcb | 2010-10-28 14:41:39 -0700 | [diff] [blame] | 431 | } else { |
| Irfan Sheriff | 227bec4 | 2011-02-15 19:30:27 -0800 | [diff] [blame] | 432 | Slog.e(TAG, "mWifiStateMachineChannel is not initialized"); |
| Irfan Sheriff | 1406bcb | 2010-10-28 14:41:39 -0700 | [diff] [blame] | 433 | return false; |
| 434 | } |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 435 | } |
| 436 | |
| 437 | /** |
| 438 | * see {@link android.net.wifi.WifiManager#startScan()} |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 439 | */ |
| Irfan Sheriff | e498475 | 2010-08-19 11:29:22 -0700 | [diff] [blame] | 440 | public void startScan(boolean forceActive) { |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 441 | enforceChangePermission(); |
| Irfan Sheriff | e498475 | 2010-08-19 11:29:22 -0700 | [diff] [blame] | 442 | mWifiStateMachine.startScan(forceActive); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 443 | } |
| 444 | |
| 445 | private void enforceAccessPermission() { |
| 446 | mContext.enforceCallingOrSelfPermission(android.Manifest.permission.ACCESS_WIFI_STATE, |
| 447 | "WifiService"); |
| 448 | } |
| 449 | |
| 450 | private void enforceChangePermission() { |
| 451 | mContext.enforceCallingOrSelfPermission(android.Manifest.permission.CHANGE_WIFI_STATE, |
| 452 | "WifiService"); |
| 453 | |
| 454 | } |
| 455 | |
| Robert Greenwalt | fc1b15c | 2009-05-22 15:09:51 -0700 | [diff] [blame] | 456 | private void enforceMulticastChangePermission() { |
| 457 | mContext.enforceCallingOrSelfPermission( |
| 458 | android.Manifest.permission.CHANGE_WIFI_MULTICAST_STATE, |
| 459 | "WifiService"); |
| 460 | } |
| 461 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 462 | /** |
| Irfan Sheriff | a2a1b91 | 2010-06-07 09:03:04 -0700 | [diff] [blame] | 463 | * see {@link android.net.wifi.WifiManager#setWifiEnabled(boolean)} |
| 464 | * @param enable {@code true} to enable, {@code false} to disable. |
| 465 | * @return {@code true} if the enable/disable operation was |
| 466 | * started or is already in the queue. |
| 467 | */ |
| 468 | public synchronized boolean setWifiEnabled(boolean enable) { |
| 469 | enforceChangePermission(); |
| 470 | |
| 471 | if (DBG) { |
| Irfan Sheriff | 0d25534 | 2010-07-28 09:35:20 -0700 | [diff] [blame] | 472 | Slog.e(TAG, "Invoking mWifiStateMachine.setWifiEnabled\n"); |
| Irfan Sheriff | a2a1b91 | 2010-06-07 09:03:04 -0700 | [diff] [blame] | 473 | } |
| 474 | |
| 475 | // set a flag if the user is enabling Wifi while in airplane mode |
| 476 | if (enable && isAirplaneModeOn() && isAirplaneToggleable()) { |
| 477 | mAirplaneModeOverwridden.set(true); |
| 478 | } |
| 479 | |
| Dianne Hackborn | 03f3cb0 | 2010-09-17 23:12:26 -0700 | [diff] [blame] | 480 | if (enable) { |
| 481 | reportStartWorkSource(); |
| 482 | } |
| Irfan Sheriff | 0d25534 | 2010-07-28 09:35:20 -0700 | [diff] [blame] | 483 | mWifiStateMachine.setWifiEnabled(enable); |
| Irfan Sheriff | 6118069 | 2010-08-18 16:07:39 -0700 | [diff] [blame] | 484 | |
| 485 | /* |
| 486 | * Caller might not have WRITE_SECURE_SETTINGS, |
| 487 | * only CHANGE_WIFI_STATE is enforced |
| 488 | */ |
| 489 | long ident = Binder.clearCallingIdentity(); |
| Irfan Sheriff | a2a1b91 | 2010-06-07 09:03:04 -0700 | [diff] [blame] | 490 | persistWifiEnabled(enable); |
| Irfan Sheriff | 6118069 | 2010-08-18 16:07:39 -0700 | [diff] [blame] | 491 | Binder.restoreCallingIdentity(ident); |
| Irfan Sheriff | a2a1b91 | 2010-06-07 09:03:04 -0700 | [diff] [blame] | 492 | |
| 493 | if (enable) { |
| 494 | if (!mIsReceiverRegistered) { |
| 495 | registerForBroadcasts(); |
| 496 | mIsReceiverRegistered = true; |
| 497 | } |
| 498 | } else if (mIsReceiverRegistered){ |
| 499 | mContext.unregisterReceiver(mReceiver); |
| 500 | mIsReceiverRegistered = false; |
| 501 | } |
| 502 | |
| 503 | return true; |
| 504 | } |
| 505 | |
| 506 | /** |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 507 | * see {@link WifiManager#getWifiState()} |
| 508 | * @return One of {@link WifiManager#WIFI_STATE_DISABLED}, |
| 509 | * {@link WifiManager#WIFI_STATE_DISABLING}, |
| 510 | * {@link WifiManager#WIFI_STATE_ENABLED}, |
| 511 | * {@link WifiManager#WIFI_STATE_ENABLING}, |
| 512 | * {@link WifiManager#WIFI_STATE_UNKNOWN} |
| 513 | */ |
| 514 | public int getWifiEnabledState() { |
| 515 | enforceAccessPermission(); |
| Irfan Sheriff | d8134ff | 2010-08-22 17:06:34 -0700 | [diff] [blame] | 516 | return mWifiStateMachine.syncGetWifiState(); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 517 | } |
| 518 | |
| 519 | /** |
| Irfan Sheriff | c2f54c2 | 2010-03-18 14:02:22 -0700 | [diff] [blame] | 520 | * see {@link android.net.wifi.WifiManager#setWifiApEnabled(WifiConfiguration, boolean)} |
| Irfan Sheriff | 5321aef | 2010-02-12 12:35:59 -0800 | [diff] [blame] | 521 | * @param wifiConfig SSID, security and channel details as |
| 522 | * part of WifiConfiguration |
| Irfan Sheriff | a2a1b91 | 2010-06-07 09:03:04 -0700 | [diff] [blame] | 523 | * @param enabled true to enable and false to disable |
| Irfan Sheriff | 5321aef | 2010-02-12 12:35:59 -0800 | [diff] [blame] | 524 | * @return {@code true} if the start operation was |
| 525 | * started or is already in the queue. |
| 526 | */ |
| Irfan Sheriff | a2a1b91 | 2010-06-07 09:03:04 -0700 | [diff] [blame] | 527 | public synchronized boolean setWifiApEnabled(WifiConfiguration wifiConfig, boolean enabled) { |
| Irfan Sheriff | 5321aef | 2010-02-12 12:35:59 -0800 | [diff] [blame] | 528 | enforceChangePermission(); |
| Irfan Sheriff | 5321aef | 2010-02-12 12:35:59 -0800 | [diff] [blame] | 529 | |
| Irfan Sheriff | a2a1b91 | 2010-06-07 09:03:04 -0700 | [diff] [blame] | 530 | if (enabled) { |
| 531 | /* Use default config if there is no existing config */ |
| 532 | if (wifiConfig == null && ((wifiConfig = getWifiApConfiguration()) == null)) { |
| 533 | wifiConfig = new WifiConfiguration(); |
| 534 | wifiConfig.SSID = mContext.getString(R.string.wifi_tether_configure_ssid_default); |
| 535 | wifiConfig.allowedKeyManagement.set(KeyMgmt.NONE); |
| 536 | } |
| Irfan Sheriff | 6118069 | 2010-08-18 16:07:39 -0700 | [diff] [blame] | 537 | /* |
| 538 | * Caller might not have WRITE_SECURE_SETTINGS, |
| 539 | * only CHANGE_WIFI_STATE is enforced |
| 540 | */ |
| 541 | long ident = Binder.clearCallingIdentity(); |
| Irfan Sheriff | a2a1b91 | 2010-06-07 09:03:04 -0700 | [diff] [blame] | 542 | setWifiApConfiguration(wifiConfig); |
| Irfan Sheriff | 6118069 | 2010-08-18 16:07:39 -0700 | [diff] [blame] | 543 | Binder.restoreCallingIdentity(ident); |
| Irfan Sheriff | 5321aef | 2010-02-12 12:35:59 -0800 | [diff] [blame] | 544 | } |
| 545 | |
| Irfan Sheriff | 0d25534 | 2010-07-28 09:35:20 -0700 | [diff] [blame] | 546 | mWifiStateMachine.setWifiApEnabled(wifiConfig, enabled); |
| Irfan Sheriff | a2a1b91 | 2010-06-07 09:03:04 -0700 | [diff] [blame] | 547 | |
| Irfan Sheriff | 5321aef | 2010-02-12 12:35:59 -0800 | [diff] [blame] | 548 | return true; |
| 549 | } |
| 550 | |
| Irfan Sheriff | a2a1b91 | 2010-06-07 09:03:04 -0700 | [diff] [blame] | 551 | /** |
| 552 | * see {@link WifiManager#getWifiApState()} |
| 553 | * @return One of {@link WifiManager#WIFI_AP_STATE_DISABLED}, |
| 554 | * {@link WifiManager#WIFI_AP_STATE_DISABLING}, |
| 555 | * {@link WifiManager#WIFI_AP_STATE_ENABLED}, |
| 556 | * {@link WifiManager#WIFI_AP_STATE_ENABLING}, |
| 557 | * {@link WifiManager#WIFI_AP_STATE_FAILED} |
| 558 | */ |
| 559 | public int getWifiApEnabledState() { |
| Irfan Sheriff | 17b232b | 2010-06-24 11:32:26 -0700 | [diff] [blame] | 560 | enforceAccessPermission(); |
| Irfan Sheriff | d8134ff | 2010-08-22 17:06:34 -0700 | [diff] [blame] | 561 | return mWifiStateMachine.syncGetWifiApState(); |
| Irfan Sheriff | a2a1b91 | 2010-06-07 09:03:04 -0700 | [diff] [blame] | 562 | } |
| 563 | |
| 564 | /** |
| 565 | * see {@link WifiManager#getWifiApConfiguration()} |
| 566 | * @return soft access point configuration |
| 567 | */ |
| 568 | public synchronized WifiConfiguration getWifiApConfiguration() { |
| Irfan Sheriff | 9ab518ad | 2010-03-12 15:48:17 -0800 | [diff] [blame] | 569 | final ContentResolver cr = mContext.getContentResolver(); |
| 570 | WifiConfiguration wifiConfig = new WifiConfiguration(); |
| 571 | int authType; |
| 572 | try { |
| 573 | wifiConfig.SSID = Settings.Secure.getString(cr, Settings.Secure.WIFI_AP_SSID); |
| 574 | if (wifiConfig.SSID == null) |
| 575 | return null; |
| 576 | authType = Settings.Secure.getInt(cr, Settings.Secure.WIFI_AP_SECURITY); |
| 577 | wifiConfig.allowedKeyManagement.set(authType); |
| 578 | wifiConfig.preSharedKey = Settings.Secure.getString(cr, Settings.Secure.WIFI_AP_PASSWD); |
| 579 | return wifiConfig; |
| 580 | } catch (Settings.SettingNotFoundException e) { |
| 581 | Slog.e(TAG,"AP settings not found, returning"); |
| 582 | return null; |
| 583 | } |
| 584 | } |
| 585 | |
| Irfan Sheriff | a2a1b91 | 2010-06-07 09:03:04 -0700 | [diff] [blame] | 586 | /** |
| 587 | * see {@link WifiManager#setWifiApConfiguration(WifiConfiguration)} |
| 588 | * @param wifiConfig WifiConfiguration details for soft access point |
| 589 | */ |
| 590 | public synchronized void setWifiApConfiguration(WifiConfiguration wifiConfig) { |
| Irfan Sheriff | 17b232b | 2010-06-24 11:32:26 -0700 | [diff] [blame] | 591 | enforceChangePermission(); |
| Irfan Sheriff | 9ab518ad | 2010-03-12 15:48:17 -0800 | [diff] [blame] | 592 | final ContentResolver cr = mContext.getContentResolver(); |
| Irfan Sheriff | 9ab518ad | 2010-03-12 15:48:17 -0800 | [diff] [blame] | 593 | if (wifiConfig == null) |
| 594 | return; |
| Irfan Sheriff | ec8d23a | 2011-02-16 17:00:33 -0800 | [diff] [blame] | 595 | int authType = wifiConfig.getAuthType(); |
| Irfan Sheriff | 9ab518ad | 2010-03-12 15:48:17 -0800 | [diff] [blame] | 596 | Settings.Secure.putString(cr, Settings.Secure.WIFI_AP_SSID, wifiConfig.SSID); |
| Irfan Sheriff | ec8d23a | 2011-02-16 17:00:33 -0800 | [diff] [blame] | 597 | Settings.Secure.putInt(cr, Settings.Secure.WIFI_AP_SECURITY, authType); |
| 598 | if (authType != KeyMgmt.NONE) |
| Irfan Sheriff | 9ab518ad | 2010-03-12 15:48:17 -0800 | [diff] [blame] | 599 | Settings.Secure.putString(cr, Settings.Secure.WIFI_AP_PASSWD, wifiConfig.preSharedKey); |
| 600 | } |
| 601 | |
| Irfan Sheriff | 5321aef | 2010-02-12 12:35:59 -0800 | [diff] [blame] | 602 | /** |
| Irfan Sheriff | a2a1b91 | 2010-06-07 09:03:04 -0700 | [diff] [blame] | 603 | * see {@link android.net.wifi.WifiManager#disconnect()} |
| Irfan Sheriff | 5321aef | 2010-02-12 12:35:59 -0800 | [diff] [blame] | 604 | */ |
| Irfan Sheriff | e498475 | 2010-08-19 11:29:22 -0700 | [diff] [blame] | 605 | public void disconnect() { |
| Irfan Sheriff | a2a1b91 | 2010-06-07 09:03:04 -0700 | [diff] [blame] | 606 | enforceChangePermission(); |
| Irfan Sheriff | e498475 | 2010-08-19 11:29:22 -0700 | [diff] [blame] | 607 | mWifiStateMachine.disconnectCommand(); |
| Irfan Sheriff | 5321aef | 2010-02-12 12:35:59 -0800 | [diff] [blame] | 608 | } |
| 609 | |
| 610 | /** |
| Irfan Sheriff | a2a1b91 | 2010-06-07 09:03:04 -0700 | [diff] [blame] | 611 | * see {@link android.net.wifi.WifiManager#reconnect()} |
| Irfan Sheriff | 5321aef | 2010-02-12 12:35:59 -0800 | [diff] [blame] | 612 | */ |
| Irfan Sheriff | e498475 | 2010-08-19 11:29:22 -0700 | [diff] [blame] | 613 | public void reconnect() { |
| Irfan Sheriff | a2a1b91 | 2010-06-07 09:03:04 -0700 | [diff] [blame] | 614 | enforceChangePermission(); |
| Irfan Sheriff | e498475 | 2010-08-19 11:29:22 -0700 | [diff] [blame] | 615 | mWifiStateMachine.reconnectCommand(); |
| Irfan Sheriff | 5321aef | 2010-02-12 12:35:59 -0800 | [diff] [blame] | 616 | } |
| 617 | |
| Irfan Sheriff | a2a1b91 | 2010-06-07 09:03:04 -0700 | [diff] [blame] | 618 | /** |
| 619 | * see {@link android.net.wifi.WifiManager#reassociate()} |
| Irfan Sheriff | a2a1b91 | 2010-06-07 09:03:04 -0700 | [diff] [blame] | 620 | */ |
| Irfan Sheriff | e498475 | 2010-08-19 11:29:22 -0700 | [diff] [blame] | 621 | public void reassociate() { |
| Irfan Sheriff | a2a1b91 | 2010-06-07 09:03:04 -0700 | [diff] [blame] | 622 | enforceChangePermission(); |
| Irfan Sheriff | e498475 | 2010-08-19 11:29:22 -0700 | [diff] [blame] | 623 | mWifiStateMachine.reassociateCommand(); |
| Irfan Sheriff | 5321aef | 2010-02-12 12:35:59 -0800 | [diff] [blame] | 624 | } |
| 625 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 626 | /** |
| 627 | * see {@link android.net.wifi.WifiManager#getConfiguredNetworks()} |
| 628 | * @return the list of configured networks |
| 629 | */ |
| 630 | public List<WifiConfiguration> getConfiguredNetworks() { |
| 631 | enforceAccessPermission(); |
| Irfan Sheriff | d8134ff | 2010-08-22 17:06:34 -0700 | [diff] [blame] | 632 | return mWifiStateMachine.syncGetConfiguredNetworks(); |
| Chung-yih Wang | a8d1594 | 2009-10-09 11:01:49 +0800 | [diff] [blame] | 633 | } |
| 634 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 635 | /** |
| 636 | * see {@link android.net.wifi.WifiManager#addOrUpdateNetwork(WifiConfiguration)} |
| 637 | * @return the supplicant-assigned identifier for the new or updated |
| 638 | * network if the operation succeeds, or {@code -1} if it fails |
| 639 | */ |
| Irfan Sheriff | 7aac554 | 2009-12-22 21:42:17 -0800 | [diff] [blame] | 640 | public int addOrUpdateNetwork(WifiConfiguration config) { |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 641 | enforceChangePermission(); |
| Irfan Sheriff | 227bec4 | 2011-02-15 19:30:27 -0800 | [diff] [blame] | 642 | if (mWifiStateMachineChannel != null) { |
| 643 | return mWifiStateMachine.syncAddOrUpdateNetwork(mWifiStateMachineChannel, config); |
| Irfan Sheriff | 1406bcb | 2010-10-28 14:41:39 -0700 | [diff] [blame] | 644 | } else { |
| Irfan Sheriff | 227bec4 | 2011-02-15 19:30:27 -0800 | [diff] [blame] | 645 | Slog.e(TAG, "mWifiStateMachineChannel is not initialized"); |
| Irfan Sheriff | 1406bcb | 2010-10-28 14:41:39 -0700 | [diff] [blame] | 646 | return -1; |
| 647 | } |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 648 | } |
| 649 | |
| Irfan Sheriff | a2a1b91 | 2010-06-07 09:03:04 -0700 | [diff] [blame] | 650 | /** |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 651 | * See {@link android.net.wifi.WifiManager#removeNetwork(int)} |
| 652 | * @param netId the integer that identifies the network configuration |
| 653 | * to the supplicant |
| 654 | * @return {@code true} if the operation succeeded |
| 655 | */ |
| 656 | public boolean removeNetwork(int netId) { |
| 657 | enforceChangePermission(); |
| Irfan Sheriff | 227bec4 | 2011-02-15 19:30:27 -0800 | [diff] [blame] | 658 | if (mWifiStateMachineChannel != null) { |
| 659 | return mWifiStateMachine.syncRemoveNetwork(mWifiStateMachineChannel, netId); |
| Wink Saville | 4b7ba09 | 2010-10-20 15:37:41 -0700 | [diff] [blame] | 660 | } else { |
| Irfan Sheriff | 227bec4 | 2011-02-15 19:30:27 -0800 | [diff] [blame] | 661 | Slog.e(TAG, "mWifiStateMachineChannel is not initialized"); |
| Wink Saville | 4b7ba09 | 2010-10-20 15:37:41 -0700 | [diff] [blame] | 662 | return false; |
| 663 | } |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 664 | } |
| 665 | |
| 666 | /** |
| 667 | * See {@link android.net.wifi.WifiManager#enableNetwork(int, boolean)} |
| 668 | * @param netId the integer that identifies the network configuration |
| 669 | * to the supplicant |
| 670 | * @param disableOthers if true, disable all other networks. |
| 671 | * @return {@code true} if the operation succeeded |
| 672 | */ |
| 673 | public boolean enableNetwork(int netId, boolean disableOthers) { |
| 674 | enforceChangePermission(); |
| Irfan Sheriff | 227bec4 | 2011-02-15 19:30:27 -0800 | [diff] [blame] | 675 | if (mWifiStateMachineChannel != null) { |
| 676 | return mWifiStateMachine.syncEnableNetwork(mWifiStateMachineChannel, netId, |
| 677 | disableOthers); |
| Irfan Sheriff | 1406bcb | 2010-10-28 14:41:39 -0700 | [diff] [blame] | 678 | } else { |
| Irfan Sheriff | 227bec4 | 2011-02-15 19:30:27 -0800 | [diff] [blame] | 679 | Slog.e(TAG, "mWifiStateMachineChannel is not initialized"); |
| Irfan Sheriff | 1406bcb | 2010-10-28 14:41:39 -0700 | [diff] [blame] | 680 | return false; |
| 681 | } |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 682 | } |
| 683 | |
| 684 | /** |
| 685 | * See {@link android.net.wifi.WifiManager#disableNetwork(int)} |
| 686 | * @param netId the integer that identifies the network configuration |
| 687 | * to the supplicant |
| 688 | * @return {@code true} if the operation succeeded |
| 689 | */ |
| 690 | public boolean disableNetwork(int netId) { |
| 691 | enforceChangePermission(); |
| Irfan Sheriff | 227bec4 | 2011-02-15 19:30:27 -0800 | [diff] [blame] | 692 | if (mWifiStateMachineChannel != null) { |
| 693 | return mWifiStateMachine.syncDisableNetwork(mWifiStateMachineChannel, netId); |
| Irfan Sheriff | 1406bcb | 2010-10-28 14:41:39 -0700 | [diff] [blame] | 694 | } else { |
| Irfan Sheriff | 227bec4 | 2011-02-15 19:30:27 -0800 | [diff] [blame] | 695 | Slog.e(TAG, "mWifiStateMachineChannel is not initialized"); |
| Irfan Sheriff | 1406bcb | 2010-10-28 14:41:39 -0700 | [diff] [blame] | 696 | return false; |
| 697 | } |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 698 | } |
| 699 | |
| 700 | /** |
| 701 | * See {@link android.net.wifi.WifiManager#getConnectionInfo()} |
| 702 | * @return the Wi-Fi information, contained in {@link WifiInfo}. |
| 703 | */ |
| 704 | public WifiInfo getConnectionInfo() { |
| 705 | enforceAccessPermission(); |
| 706 | /* |
| 707 | * Make sure we have the latest information, by sending |
| 708 | * a status request to the supplicant. |
| 709 | */ |
| Irfan Sheriff | d8134ff | 2010-08-22 17:06:34 -0700 | [diff] [blame] | 710 | return mWifiStateMachine.syncRequestConnectionInfo(); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 711 | } |
| 712 | |
| 713 | /** |
| 714 | * Return the results of the most recent access point scan, in the form of |
| 715 | * a list of {@link ScanResult} objects. |
| 716 | * @return the list of results |
| 717 | */ |
| 718 | public List<ScanResult> getScanResults() { |
| 719 | enforceAccessPermission(); |
| Irfan Sheriff | d8134ff | 2010-08-22 17:06:34 -0700 | [diff] [blame] | 720 | return mWifiStateMachine.syncGetScanResultsList(); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 721 | } |
| 722 | |
| 723 | /** |
| 724 | * Tell the supplicant to persist the current list of configured networks. |
| 725 | * @return {@code true} if the operation succeeded |
| Irfan Sheriff | a2a1b91 | 2010-06-07 09:03:04 -0700 | [diff] [blame] | 726 | * |
| 727 | * TODO: deprecate this |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 728 | */ |
| 729 | public boolean saveConfiguration() { |
| Irfan Sheriff | a2a1b91 | 2010-06-07 09:03:04 -0700 | [diff] [blame] | 730 | boolean result = true; |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 731 | enforceChangePermission(); |
| Irfan Sheriff | 227bec4 | 2011-02-15 19:30:27 -0800 | [diff] [blame] | 732 | if (mWifiStateMachineChannel != null) { |
| 733 | return mWifiStateMachine.syncSaveConfig(mWifiStateMachineChannel); |
| Irfan Sheriff | 1406bcb | 2010-10-28 14:41:39 -0700 | [diff] [blame] | 734 | } else { |
| Irfan Sheriff | 227bec4 | 2011-02-15 19:30:27 -0800 | [diff] [blame] | 735 | Slog.e(TAG, "mWifiStateMachineChannel is not initialized"); |
| Irfan Sheriff | 1406bcb | 2010-10-28 14:41:39 -0700 | [diff] [blame] | 736 | return false; |
| 737 | } |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 738 | } |
| 739 | |
| 740 | /** |
| Irfan Sheriff | ed4f28b | 2010-10-29 15:32:10 -0700 | [diff] [blame] | 741 | * Set the country code |
| 742 | * @param countryCode ISO 3166 country code. |
| Robert Greenwalt | b5010cc | 2009-05-21 15:11:40 -0700 | [diff] [blame] | 743 | * @param persist {@code true} if the setting should be remembered. |
| Irfan Sheriff | ed4f28b | 2010-10-29 15:32:10 -0700 | [diff] [blame] | 744 | * |
| 745 | * The persist behavior exists so that wifi can fall back to the last |
| 746 | * persisted country code on a restart, when the locale information is |
| 747 | * not available from telephony. |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 748 | */ |
| Irfan Sheriff | ed4f28b | 2010-10-29 15:32:10 -0700 | [diff] [blame] | 749 | public void setCountryCode(String countryCode, boolean persist) { |
| 750 | Slog.i(TAG, "WifiService trying to set country code to " + countryCode + |
| 751 | " with persist set to " + persist); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 752 | enforceChangePermission(); |
| Irfan Sheriff | ed4f28b | 2010-10-29 15:32:10 -0700 | [diff] [blame] | 753 | mWifiStateMachine.setCountryCode(countryCode, persist); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 754 | } |
| 755 | |
| 756 | /** |
| Irfan Sheriff | 36f7413 | 2010-11-04 16:57:37 -0700 | [diff] [blame] | 757 | * Set the operational frequency band |
| 758 | * @param band One of |
| 759 | * {@link WifiManager#WIFI_FREQUENCY_BAND_AUTO}, |
| 760 | * {@link WifiManager#WIFI_FREQUENCY_BAND_5GHZ}, |
| 761 | * {@link WifiManager#WIFI_FREQUENCY_BAND_2GHZ}, |
| 762 | * @param persist {@code true} if the setting should be remembered. |
| 763 | * |
| 764 | */ |
| 765 | public void setFrequencyBand(int band, boolean persist) { |
| 766 | enforceChangePermission(); |
| 767 | if (!isDualBandSupported()) return; |
| 768 | Slog.i(TAG, "WifiService trying to set frequency band to " + band + |
| 769 | " with persist set to " + persist); |
| 770 | mWifiStateMachine.setFrequencyBand(band, persist); |
| 771 | } |
| 772 | |
| 773 | |
| 774 | /** |
| 775 | * Get the operational frequency band |
| 776 | */ |
| 777 | public int getFrequencyBand() { |
| 778 | enforceAccessPermission(); |
| 779 | return mWifiStateMachine.getFrequencyBand(); |
| 780 | } |
| 781 | |
| 782 | public boolean isDualBandSupported() { |
| 783 | //TODO: Should move towards adding a driver API that checks at runtime |
| 784 | return mContext.getResources().getBoolean( |
| 785 | com.android.internal.R.bool.config_wifi_dual_band_support); |
| 786 | } |
| 787 | |
| 788 | /** |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 789 | * Return the DHCP-assigned addresses from the last successful DHCP request, |
| 790 | * if any. |
| 791 | * @return the DHCP information |
| 792 | */ |
| 793 | public DhcpInfo getDhcpInfo() { |
| 794 | enforceAccessPermission(); |
| Irfan Sheriff | d8134ff | 2010-08-22 17:06:34 -0700 | [diff] [blame] | 795 | return mWifiStateMachine.syncGetDhcpInfo(); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 796 | } |
| 797 | |
| Irfan Sheriff | 0d25534 | 2010-07-28 09:35:20 -0700 | [diff] [blame] | 798 | /** |
| 799 | * see {@link android.net.wifi.WifiManager#startWifi} |
| 800 | * |
| 801 | */ |
| 802 | public void startWifi() { |
| 803 | enforceChangePermission(); |
| 804 | /* TODO: may be add permissions for access only to connectivity service |
| 805 | * TODO: if a start issued, keep wifi alive until a stop issued irrespective |
| 806 | * of WifiLock & device idle status unless wifi enabled status is toggled |
| 807 | */ |
| 808 | |
| 809 | mWifiStateMachine.setDriverStart(true); |
| 810 | mWifiStateMachine.reconnectCommand(); |
| 811 | } |
| 812 | |
| 813 | /** |
| 814 | * see {@link android.net.wifi.WifiManager#stopWifi} |
| 815 | * |
| 816 | */ |
| 817 | public void stopWifi() { |
| 818 | enforceChangePermission(); |
| 819 | /* TODO: may be add permissions for access only to connectivity service |
| 820 | * TODO: if a stop is issued, wifi is brought up only by startWifi |
| 821 | * unless wifi enabled status is toggled |
| 822 | */ |
| 823 | mWifiStateMachine.setDriverStart(false); |
| 824 | } |
| 825 | |
| 826 | |
| 827 | /** |
| 828 | * see {@link android.net.wifi.WifiManager#addToBlacklist} |
| 829 | * |
| 830 | */ |
| 831 | public void addToBlacklist(String bssid) { |
| 832 | enforceChangePermission(); |
| 833 | |
| 834 | mWifiStateMachine.addToBlacklist(bssid); |
| 835 | } |
| 836 | |
| 837 | /** |
| 838 | * see {@link android.net.wifi.WifiManager#clearBlacklist} |
| 839 | * |
| 840 | */ |
| 841 | public void clearBlacklist() { |
| 842 | enforceChangePermission(); |
| 843 | |
| 844 | mWifiStateMachine.clearBlacklist(); |
| 845 | } |
| 846 | |
| Irfan Sheriff | e04653c | 2010-08-09 09:09:59 -0700 | [diff] [blame] | 847 | public void connectNetworkWithId(int networkId) { |
| 848 | enforceChangePermission(); |
| 849 | mWifiStateMachine.connectNetwork(networkId); |
| 850 | } |
| 851 | |
| 852 | public void connectNetworkWithConfig(WifiConfiguration config) { |
| 853 | enforceChangePermission(); |
| 854 | mWifiStateMachine.connectNetwork(config); |
| 855 | } |
| 856 | |
| 857 | public void saveNetwork(WifiConfiguration config) { |
| 858 | enforceChangePermission(); |
| 859 | mWifiStateMachine.saveNetwork(config); |
| 860 | } |
| 861 | |
| 862 | public void forgetNetwork(int netId) { |
| 863 | enforceChangePermission(); |
| 864 | mWifiStateMachine.forgetNetwork(netId); |
| 865 | } |
| Irfan Sheriff | 0d25534 | 2010-07-28 09:35:20 -0700 | [diff] [blame] | 866 | |
| Irfan Sheriff | e4c56c9 | 2011-01-12 16:33:58 -0800 | [diff] [blame] | 867 | public WpsResult startWps(WpsConfiguration config) { |
| Irfan Sheriff | f235c5a | 2010-10-21 16:44:48 -0700 | [diff] [blame] | 868 | enforceChangePermission(); |
| Irfan Sheriff | 227bec4 | 2011-02-15 19:30:27 -0800 | [diff] [blame] | 869 | if (mWifiStateMachineChannel != null) { |
| 870 | return mWifiStateMachine.startWps(mWifiStateMachineChannel, config); |
| Irfan Sheriff | f235c5a | 2010-10-21 16:44:48 -0700 | [diff] [blame] | 871 | } else { |
| Irfan Sheriff | 227bec4 | 2011-02-15 19:30:27 -0800 | [diff] [blame] | 872 | Slog.e(TAG, "mWifiStateMachineChannel is not initialized"); |
| Irfan Sheriff | e4c56c9 | 2011-01-12 16:33:58 -0800 | [diff] [blame] | 873 | return new WpsResult(WpsResult.Status.FAILURE); |
| Irfan Sheriff | f235c5a | 2010-10-21 16:44:48 -0700 | [diff] [blame] | 874 | } |
| Irfan Sheriff | 5ee8980 | 2010-09-16 17:53:34 -0700 | [diff] [blame] | 875 | } |
| 876 | |
| Irfan Sheriff | 227bec4 | 2011-02-15 19:30:27 -0800 | [diff] [blame] | 877 | /** |
| 878 | * Get a reference to handler. This is used by a client to establish |
| 879 | * an AsyncChannel communication with WifiService |
| 880 | */ |
| 881 | public Messenger getMessenger() { |
| 882 | enforceAccessPermission(); |
| 883 | return new Messenger(mAsyncServiceHandler); |
| 884 | } |
| 885 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 886 | private final BroadcastReceiver mReceiver = new BroadcastReceiver() { |
| 887 | @Override |
| 888 | public void onReceive(Context context, Intent intent) { |
| 889 | String action = intent.getAction(); |
| 890 | |
| Doug Zongker | 43866e0 | 2010-01-07 12:09:54 -0800 | [diff] [blame] | 891 | long idleMillis = |
| 892 | Settings.Secure.getLong(mContext.getContentResolver(), |
| Irfan Sheriff | 4f5f7c9 | 2010-10-14 17:01:27 -0700 | [diff] [blame] | 893 | Settings.Secure.WIFI_IDLE_MS, DEFAULT_IDLE_MS); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 894 | int stayAwakeConditions = |
| Doug Zongker | 43866e0 | 2010-01-07 12:09:54 -0800 | [diff] [blame] | 895 | Settings.System.getInt(mContext.getContentResolver(), |
| 896 | Settings.System.STAY_ON_WHILE_PLUGGED_IN, 0); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 897 | if (action.equals(Intent.ACTION_SCREEN_ON)) { |
| Joe Onorato | 431bb22 | 2010-10-18 19:13:23 -0400 | [diff] [blame] | 898 | if (DBG) { |
| 899 | Slog.d(TAG, "ACTION_SCREEN_ON"); |
| 900 | } |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 901 | mAlarmManager.cancel(mIdleIntent); |
| 902 | mDeviceIdle = false; |
| 903 | mScreenOff = false; |
| Dianne Hackborn | 58e0eef | 2010-09-16 01:22:10 -0700 | [diff] [blame] | 904 | // Once the screen is on, we are not keeping WIFI running |
| 905 | // because of any locks so clear that tracking immediately. |
| 906 | reportStartWorkSource(); |
| Irfan Sheriff | 227bec4 | 2011-02-15 19:30:27 -0800 | [diff] [blame] | 907 | evaluateTrafficStatsPolling(); |
| Irfan Sheriff | 0d25534 | 2010-07-28 09:35:20 -0700 | [diff] [blame] | 908 | mWifiStateMachine.enableRssiPolling(true); |
| Irfan Sheriff | fcc0845 | 2011-02-17 16:44:54 -0800 | [diff] [blame^] | 909 | if (mBackgroundScanSupported) { |
| 910 | mWifiStateMachine.enableBackgroundScan(false); |
| 911 | } |
| Irfan Sheriff | 8e86b89 | 2010-12-22 11:02:20 -0800 | [diff] [blame] | 912 | mWifiStateMachine.enableAllNetworks(); |
| Irfan Sheriff | 4f5f7c9 | 2010-10-14 17:01:27 -0700 | [diff] [blame] | 913 | updateWifiState(); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 914 | } else if (action.equals(Intent.ACTION_SCREEN_OFF)) { |
| Joe Onorato | 431bb22 | 2010-10-18 19:13:23 -0400 | [diff] [blame] | 915 | if (DBG) { |
| 916 | Slog.d(TAG, "ACTION_SCREEN_OFF"); |
| 917 | } |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 918 | mScreenOff = true; |
| Irfan Sheriff | 227bec4 | 2011-02-15 19:30:27 -0800 | [diff] [blame] | 919 | evaluateTrafficStatsPolling(); |
| Irfan Sheriff | 0d25534 | 2010-07-28 09:35:20 -0700 | [diff] [blame] | 920 | mWifiStateMachine.enableRssiPolling(false); |
| Irfan Sheriff | fcc0845 | 2011-02-17 16:44:54 -0800 | [diff] [blame^] | 921 | if (mBackgroundScanSupported) { |
| 922 | mWifiStateMachine.enableBackgroundScan(true); |
| 923 | } |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 924 | /* |
| 925 | * Set a timer to put Wi-Fi to sleep, but only if the screen is off |
| 926 | * AND the "stay on while plugged in" setting doesn't match the |
| 927 | * current power conditions (i.e, not plugged in, plugged in to USB, |
| 928 | * or plugged in to AC). |
| 929 | */ |
| 930 | if (!shouldWifiStayAwake(stayAwakeConditions, mPluggedType)) { |
| Irfan Sheriff | d8134ff | 2010-08-22 17:06:34 -0700 | [diff] [blame] | 931 | WifiInfo info = mWifiStateMachine.syncRequestConnectionInfo(); |
| San Mehat | fa6c711 | 2009-07-07 09:34:44 -0700 | [diff] [blame] | 932 | if (info.getSupplicantState() != SupplicantState.COMPLETED) { |
| Robert Greenwalt | 84612ea6 | 2009-09-30 09:04:22 -0700 | [diff] [blame] | 933 | // we used to go to sleep immediately, but this caused some race conditions |
| Irfan Sheriff | a2a1b91 | 2010-06-07 09:03:04 -0700 | [diff] [blame] | 934 | // we don't have time to track down for this release. Delay instead, |
| 935 | // but not as long as we would if connected (below) |
| Robert Greenwalt | 84612ea6 | 2009-09-30 09:04:22 -0700 | [diff] [blame] | 936 | // TODO - fix the race conditions and switch back to the immediate turn-off |
| 937 | long triggerTime = System.currentTimeMillis() + (2*60*1000); // 2 min |
| Joe Onorato | 431bb22 | 2010-10-18 19:13:23 -0400 | [diff] [blame] | 938 | if (DBG) { |
| 939 | Slog.d(TAG, "setting ACTION_DEVICE_IDLE timer for 120,000 ms"); |
| 940 | } |
| Robert Greenwalt | 84612ea6 | 2009-09-30 09:04:22 -0700 | [diff] [blame] | 941 | mAlarmManager.set(AlarmManager.RTC_WAKEUP, triggerTime, mIdleIntent); |
| 942 | // // do not keep Wifi awake when screen is off if Wifi is not associated |
| 943 | // mDeviceIdle = true; |
| 944 | // updateWifiState(); |
| Mike Lockwood | d9c32bc | 2009-05-18 14:14:15 -0400 | [diff] [blame] | 945 | } else { |
| 946 | long triggerTime = System.currentTimeMillis() + idleMillis; |
| Joe Onorato | 431bb22 | 2010-10-18 19:13:23 -0400 | [diff] [blame] | 947 | if (DBG) { |
| 948 | Slog.d(TAG, "setting ACTION_DEVICE_IDLE timer for " + idleMillis |
| 949 | + "ms"); |
| 950 | } |
| Mike Lockwood | d9c32bc | 2009-05-18 14:14:15 -0400 | [diff] [blame] | 951 | mAlarmManager.set(AlarmManager.RTC_WAKEUP, triggerTime, mIdleIntent); |
| 952 | } |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 953 | } |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 954 | } else if (action.equals(ACTION_DEVICE_IDLE)) { |
| Joe Onorato | 431bb22 | 2010-10-18 19:13:23 -0400 | [diff] [blame] | 955 | if (DBG) { |
| 956 | Slog.d(TAG, "got ACTION_DEVICE_IDLE"); |
| 957 | } |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 958 | mDeviceIdle = true; |
| Dianne Hackborn | 58e0eef | 2010-09-16 01:22:10 -0700 | [diff] [blame] | 959 | reportStartWorkSource(); |
| Irfan Sheriff | 4f5f7c9 | 2010-10-14 17:01:27 -0700 | [diff] [blame] | 960 | updateWifiState(); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 961 | } else if (action.equals(Intent.ACTION_BATTERY_CHANGED)) { |
| 962 | /* |
| 963 | * Set a timer to put Wi-Fi to sleep, but only if the screen is off |
| 964 | * AND we are transitioning from a state in which the device was supposed |
| 965 | * to stay awake to a state in which it is not supposed to stay awake. |
| 966 | * If "stay awake" state is not changing, we do nothing, to avoid resetting |
| 967 | * the already-set timer. |
| 968 | */ |
| 969 | int pluggedType = intent.getIntExtra("plugged", 0); |
| Joe Onorato | 431bb22 | 2010-10-18 19:13:23 -0400 | [diff] [blame] | 970 | if (DBG) { |
| 971 | Slog.d(TAG, "ACTION_BATTERY_CHANGED pluggedType: " + pluggedType); |
| 972 | } |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 973 | if (mScreenOff && shouldWifiStayAwake(stayAwakeConditions, mPluggedType) && |
| 974 | !shouldWifiStayAwake(stayAwakeConditions, pluggedType)) { |
| 975 | long triggerTime = System.currentTimeMillis() + idleMillis; |
| Joe Onorato | 431bb22 | 2010-10-18 19:13:23 -0400 | [diff] [blame] | 976 | if (DBG) { |
| 977 | Slog.d(TAG, "setting ACTION_DEVICE_IDLE timer for " + idleMillis + "ms"); |
| 978 | } |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 979 | mAlarmManager.set(AlarmManager.RTC_WAKEUP, triggerTime, mIdleIntent); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 980 | } |
| 981 | mPluggedType = pluggedType; |
| Irfan Sheriff | 65eaec8 | 2011-01-05 22:00:16 -0800 | [diff] [blame] | 982 | } else if (action.equals(BluetoothAdapter.ACTION_CONNECTION_STATE_CHANGED)) { |
| 983 | int state = intent.getIntExtra(BluetoothAdapter.EXTRA_CONNECTION_STATE, |
| 984 | BluetoothAdapter.STATE_DISCONNECTED); |
| 985 | mWifiStateMachine.sendBluetoothAdapterStateChange(state); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 986 | } |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 987 | } |
| 988 | |
| 989 | /** |
| 990 | * Determines whether the Wi-Fi chipset should stay awake or be put to |
| 991 | * sleep. Looks at the setting for the sleep policy and the current |
| 992 | * conditions. |
| Jaikumar Ganesh | 084c665 | 2009-12-07 10:58:18 -0800 | [diff] [blame] | 993 | * |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 994 | * @see #shouldDeviceStayAwake(int, int) |
| 995 | */ |
| 996 | private boolean shouldWifiStayAwake(int stayAwakeConditions, int pluggedType) { |
| Irfan Sheriff | 739f6bc | 2011-01-28 16:43:12 -0800 | [diff] [blame] | 997 | //Never sleep as long as the user has not changed the settings |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 998 | int wifiSleepPolicy = Settings.System.getInt(mContext.getContentResolver(), |
| Irfan Sheriff | 96b10d6 | 2011-01-11 15:40:35 -0800 | [diff] [blame] | 999 | Settings.System.WIFI_SLEEP_POLICY, |
| Irfan Sheriff | 739f6bc | 2011-01-28 16:43:12 -0800 | [diff] [blame] | 1000 | Settings.System.WIFI_SLEEP_POLICY_NEVER); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1001 | |
| 1002 | if (wifiSleepPolicy == Settings.System.WIFI_SLEEP_POLICY_NEVER) { |
| 1003 | // Never sleep |
| 1004 | return true; |
| 1005 | } else if ((wifiSleepPolicy == Settings.System.WIFI_SLEEP_POLICY_NEVER_WHILE_PLUGGED) && |
| 1006 | (pluggedType != 0)) { |
| 1007 | // Never sleep while plugged, and we're plugged |
| 1008 | return true; |
| 1009 | } else { |
| 1010 | // Default |
| 1011 | return shouldDeviceStayAwake(stayAwakeConditions, pluggedType); |
| 1012 | } |
| 1013 | } |
| Jaikumar Ganesh | 084c665 | 2009-12-07 10:58:18 -0800 | [diff] [blame] | 1014 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1015 | /** |
| 1016 | * Determine whether the bit value corresponding to {@code pluggedType} is set in |
| 1017 | * the bit string {@code stayAwakeConditions}. Because a {@code pluggedType} value |
| 1018 | * of {@code 0} isn't really a plugged type, but rather an indication that the |
| 1019 | * device isn't plugged in at all, there is no bit value corresponding to a |
| 1020 | * {@code pluggedType} value of {@code 0}. That is why we shift by |
| Ben Dodson | 4e8620f | 2010-08-25 10:55:47 -0700 | [diff] [blame] | 1021 | * {@code pluggedType - 1} instead of by {@code pluggedType}. |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1022 | * @param stayAwakeConditions a bit string specifying which "plugged types" should |
| 1023 | * keep the device (and hence Wi-Fi) awake. |
| 1024 | * @param pluggedType the type of plug (USB, AC, or none) for which the check is |
| 1025 | * being made |
| 1026 | * @return {@code true} if {@code pluggedType} indicates that the device is |
| 1027 | * supposed to stay awake, {@code false} otherwise. |
| 1028 | */ |
| 1029 | private boolean shouldDeviceStayAwake(int stayAwakeConditions, int pluggedType) { |
| 1030 | return (stayAwakeConditions & pluggedType) != 0; |
| 1031 | } |
| 1032 | }; |
| 1033 | |
| Dianne Hackborn | 03f3cb0 | 2010-09-17 23:12:26 -0700 | [diff] [blame] | 1034 | private synchronized void reportStartWorkSource() { |
| 1035 | mTmpWorkSource.clear(); |
| 1036 | if (mDeviceIdle) { |
| 1037 | for (int i=0; i<mLocks.mList.size(); i++) { |
| 1038 | mTmpWorkSource.add(mLocks.mList.get(i).mWorkSource); |
| Dianne Hackborn | 58e0eef | 2010-09-16 01:22:10 -0700 | [diff] [blame] | 1039 | } |
| Dianne Hackborn | 58e0eef | 2010-09-16 01:22:10 -0700 | [diff] [blame] | 1040 | } |
| Dianne Hackborn | 03f3cb0 | 2010-09-17 23:12:26 -0700 | [diff] [blame] | 1041 | mWifiStateMachine.updateBatteryWorkSource(mTmpWorkSource); |
| Dianne Hackborn | 58e0eef | 2010-09-16 01:22:10 -0700 | [diff] [blame] | 1042 | } |
| Jaikumar Ganesh | 7440fc2 | 2010-09-27 17:04:14 -0700 | [diff] [blame] | 1043 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1044 | private void updateWifiState() { |
| 1045 | boolean wifiEnabled = getPersistedWifiEnabled(); |
| Irfan Sheriff | a2a1b91 | 2010-06-07 09:03:04 -0700 | [diff] [blame] | 1046 | boolean airplaneMode = isAirplaneModeOn() && !mAirplaneModeOverwridden.get(); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1047 | boolean lockHeld = mLocks.hasLocks(); |
| Irfan Sheriff | 5876a42 | 2010-08-12 20:26:23 -0700 | [diff] [blame] | 1048 | int strongestLockMode = WifiManager.WIFI_MODE_FULL; |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1049 | boolean wifiShouldBeEnabled = wifiEnabled && !airplaneMode; |
| 1050 | boolean wifiShouldBeStarted = !mDeviceIdle || lockHeld; |
| Irfan Sheriff | 5876a42 | 2010-08-12 20:26:23 -0700 | [diff] [blame] | 1051 | |
| 1052 | if (lockHeld) { |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1053 | strongestLockMode = mLocks.getStrongestLockMode(); |
| Irfan Sheriff | 5876a42 | 2010-08-12 20:26:23 -0700 | [diff] [blame] | 1054 | } |
| 1055 | /* If device is not idle, lockmode cannot be scan only */ |
| 1056 | if (!mDeviceIdle && strongestLockMode == WifiManager.WIFI_MODE_SCAN_ONLY) { |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1057 | strongestLockMode = WifiManager.WIFI_MODE_FULL; |
| 1058 | } |
| 1059 | |
| Irfan Sheriff | a2a1b91 | 2010-06-07 09:03:04 -0700 | [diff] [blame] | 1060 | /* Disable tethering when airplane mode is enabled */ |
| 1061 | if (airplaneMode) { |
| Irfan Sheriff | 0d25534 | 2010-07-28 09:35:20 -0700 | [diff] [blame] | 1062 | mWifiStateMachine.setWifiApEnabled(null, false); |
| Irfan Sheriff | a2a1b91 | 2010-06-07 09:03:04 -0700 | [diff] [blame] | 1063 | } |
| Irfan Sheriff | b2e6c01 | 2010-04-05 11:57:56 -0700 | [diff] [blame] | 1064 | |
| Irfan Sheriff | a2a1b91 | 2010-06-07 09:03:04 -0700 | [diff] [blame] | 1065 | if (wifiShouldBeEnabled) { |
| 1066 | if (wifiShouldBeStarted) { |
| Dianne Hackborn | 03f3cb0 | 2010-09-17 23:12:26 -0700 | [diff] [blame] | 1067 | reportStartWorkSource(); |
| Irfan Sheriff | 0d25534 | 2010-07-28 09:35:20 -0700 | [diff] [blame] | 1068 | mWifiStateMachine.setWifiEnabled(true); |
| 1069 | mWifiStateMachine.setScanOnlyMode( |
| Irfan Sheriff | a2a1b91 | 2010-06-07 09:03:04 -0700 | [diff] [blame] | 1070 | strongestLockMode == WifiManager.WIFI_MODE_SCAN_ONLY); |
| Irfan Sheriff | 0d25534 | 2010-07-28 09:35:20 -0700 | [diff] [blame] | 1071 | mWifiStateMachine.setDriverStart(true); |
| Irfan Sheriff | 5876a42 | 2010-08-12 20:26:23 -0700 | [diff] [blame] | 1072 | mWifiStateMachine.setHighPerfModeEnabled(strongestLockMode |
| 1073 | == WifiManager.WIFI_MODE_FULL_HIGH_PERF); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1074 | } else { |
| Irfan Sheriff | 0d25534 | 2010-07-28 09:35:20 -0700 | [diff] [blame] | 1075 | mWifiStateMachine.requestCmWakeLock(); |
| 1076 | mWifiStateMachine.setDriverStart(false); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1077 | } |
| Irfan Sheriff | a2a1b91 | 2010-06-07 09:03:04 -0700 | [diff] [blame] | 1078 | } else { |
| Irfan Sheriff | 0d25534 | 2010-07-28 09:35:20 -0700 | [diff] [blame] | 1079 | mWifiStateMachine.setWifiEnabled(false); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1080 | } |
| 1081 | } |
| 1082 | |
| 1083 | private void registerForBroadcasts() { |
| 1084 | IntentFilter intentFilter = new IntentFilter(); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1085 | intentFilter.addAction(Intent.ACTION_SCREEN_ON); |
| 1086 | intentFilter.addAction(Intent.ACTION_SCREEN_OFF); |
| 1087 | intentFilter.addAction(Intent.ACTION_BATTERY_CHANGED); |
| 1088 | intentFilter.addAction(ACTION_DEVICE_IDLE); |
| Irfan Sheriff | 65eaec8 | 2011-01-05 22:00:16 -0800 | [diff] [blame] | 1089 | intentFilter.addAction(BluetoothAdapter.ACTION_CONNECTION_STATE_CHANGED); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1090 | mContext.registerReceiver(mReceiver, intentFilter); |
| 1091 | } |
| Jaikumar Ganesh | 084c665 | 2009-12-07 10:58:18 -0800 | [diff] [blame] | 1092 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1093 | private boolean isAirplaneSensitive() { |
| 1094 | String airplaneModeRadios = Settings.System.getString(mContext.getContentResolver(), |
| 1095 | Settings.System.AIRPLANE_MODE_RADIOS); |
| 1096 | return airplaneModeRadios == null |
| 1097 | || airplaneModeRadios.contains(Settings.System.RADIO_WIFI); |
| 1098 | } |
| 1099 | |
| Mike Lockwood | bd5ddf0 | 2009-07-29 21:37:14 -0700 | [diff] [blame] | 1100 | private boolean isAirplaneToggleable() { |
| 1101 | String toggleableRadios = Settings.System.getString(mContext.getContentResolver(), |
| 1102 | Settings.System.AIRPLANE_MODE_TOGGLEABLE_RADIOS); |
| 1103 | return toggleableRadios != null |
| 1104 | && toggleableRadios.contains(Settings.System.RADIO_WIFI); |
| 1105 | } |
| 1106 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1107 | /** |
| 1108 | * Returns true if Wi-Fi is sensitive to airplane mode, and airplane mode is |
| 1109 | * currently on. |
| 1110 | * @return {@code true} if airplane mode is on. |
| 1111 | */ |
| 1112 | private boolean isAirplaneModeOn() { |
| 1113 | return isAirplaneSensitive() && Settings.System.getInt(mContext.getContentResolver(), |
| 1114 | Settings.System.AIRPLANE_MODE_ON, 0) == 1; |
| 1115 | } |
| 1116 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1117 | @Override |
| 1118 | protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) { |
| 1119 | if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.DUMP) |
| 1120 | != PackageManager.PERMISSION_GRANTED) { |
| 1121 | pw.println("Permission Denial: can't dump WifiService from from pid=" |
| 1122 | + Binder.getCallingPid() |
| 1123 | + ", uid=" + Binder.getCallingUid()); |
| 1124 | return; |
| 1125 | } |
| Irfan Sheriff | d8134ff | 2010-08-22 17:06:34 -0700 | [diff] [blame] | 1126 | pw.println("Wi-Fi is " + mWifiStateMachine.syncGetWifiStateByName()); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1127 | pw.println("Stay-awake conditions: " + |
| 1128 | Settings.System.getInt(mContext.getContentResolver(), |
| 1129 | Settings.System.STAY_ON_WHILE_PLUGGED_IN, 0)); |
| 1130 | pw.println(); |
| 1131 | |
| 1132 | pw.println("Internal state:"); |
| Irfan Sheriff | 0d25534 | 2010-07-28 09:35:20 -0700 | [diff] [blame] | 1133 | pw.println(mWifiStateMachine); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1134 | pw.println(); |
| 1135 | pw.println("Latest scan results:"); |
| Irfan Sheriff | d8134ff | 2010-08-22 17:06:34 -0700 | [diff] [blame] | 1136 | List<ScanResult> scanResults = mWifiStateMachine.syncGetScanResultsList(); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1137 | if (scanResults != null && scanResults.size() != 0) { |
| 1138 | pw.println(" BSSID Frequency RSSI Flags SSID"); |
| 1139 | for (ScanResult r : scanResults) { |
| 1140 | pw.printf(" %17s %9d %5d %-16s %s%n", |
| 1141 | r.BSSID, |
| 1142 | r.frequency, |
| 1143 | r.level, |
| 1144 | r.capabilities, |
| 1145 | r.SSID == null ? "" : r.SSID); |
| 1146 | } |
| 1147 | } |
| 1148 | pw.println(); |
| Eric Shienbrood | 5711fad | 2009-03-27 20:25:31 -0700 | [diff] [blame] | 1149 | pw.println("Locks acquired: " + mFullLocksAcquired + " full, " + |
| Irfan Sheriff | 5876a42 | 2010-08-12 20:26:23 -0700 | [diff] [blame] | 1150 | mFullHighPerfLocksAcquired + " full high perf, " + |
| Eric Shienbrood | 5711fad | 2009-03-27 20:25:31 -0700 | [diff] [blame] | 1151 | mScanLocksAcquired + " scan"); |
| 1152 | pw.println("Locks released: " + mFullLocksReleased + " full, " + |
| Irfan Sheriff | 5876a42 | 2010-08-12 20:26:23 -0700 | [diff] [blame] | 1153 | mFullHighPerfLocksReleased + " full high perf, " + |
| Eric Shienbrood | 5711fad | 2009-03-27 20:25:31 -0700 | [diff] [blame] | 1154 | mScanLocksReleased + " scan"); |
| 1155 | pw.println(); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1156 | pw.println("Locks held:"); |
| 1157 | mLocks.dump(pw); |
| 1158 | } |
| 1159 | |
| Robert Greenwalt | 58ff021 | 2009-05-19 15:53:54 -0700 | [diff] [blame] | 1160 | private class WifiLock extends DeathRecipient { |
| Dianne Hackborn | 7e9f4eb | 2010-09-10 18:43:00 -0700 | [diff] [blame] | 1161 | WifiLock(int lockMode, String tag, IBinder binder, WorkSource ws) { |
| 1162 | super(lockMode, tag, binder, ws); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1163 | } |
| 1164 | |
| 1165 | public void binderDied() { |
| 1166 | synchronized (mLocks) { |
| 1167 | releaseWifiLockLocked(mBinder); |
| 1168 | } |
| 1169 | } |
| 1170 | |
| 1171 | public String toString() { |
| Robert Greenwalt | 5347bd4 | 2009-05-13 15:10:16 -0700 | [diff] [blame] | 1172 | return "WifiLock{" + mTag + " type=" + mMode + " binder=" + mBinder + "}"; |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1173 | } |
| 1174 | } |
| 1175 | |
| 1176 | private class LockList { |
| 1177 | private List<WifiLock> mList; |
| 1178 | |
| 1179 | private LockList() { |
| 1180 | mList = new ArrayList<WifiLock>(); |
| 1181 | } |
| 1182 | |
| 1183 | private synchronized boolean hasLocks() { |
| 1184 | return !mList.isEmpty(); |
| 1185 | } |
| 1186 | |
| 1187 | private synchronized int getStrongestLockMode() { |
| 1188 | if (mList.isEmpty()) { |
| 1189 | return WifiManager.WIFI_MODE_FULL; |
| 1190 | } |
| Irfan Sheriff | 5876a42 | 2010-08-12 20:26:23 -0700 | [diff] [blame] | 1191 | |
| 1192 | if (mFullHighPerfLocksAcquired > mFullHighPerfLocksReleased) { |
| 1193 | return WifiManager.WIFI_MODE_FULL_HIGH_PERF; |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1194 | } |
| Irfan Sheriff | 5876a42 | 2010-08-12 20:26:23 -0700 | [diff] [blame] | 1195 | |
| 1196 | if (mFullLocksAcquired > mFullLocksReleased) { |
| 1197 | return WifiManager.WIFI_MODE_FULL; |
| 1198 | } |
| 1199 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1200 | return WifiManager.WIFI_MODE_SCAN_ONLY; |
| 1201 | } |
| 1202 | |
| 1203 | private void addLock(WifiLock lock) { |
| 1204 | if (findLockByBinder(lock.mBinder) < 0) { |
| 1205 | mList.add(lock); |
| 1206 | } |
| 1207 | } |
| 1208 | |
| 1209 | private WifiLock removeLock(IBinder binder) { |
| 1210 | int index = findLockByBinder(binder); |
| 1211 | if (index >= 0) { |
| Suchi Amalapurapu | fff2fda | 2009-06-30 21:36:16 -0700 | [diff] [blame] | 1212 | WifiLock ret = mList.remove(index); |
| 1213 | ret.unlinkDeathRecipient(); |
| 1214 | return ret; |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1215 | } else { |
| 1216 | return null; |
| 1217 | } |
| 1218 | } |
| 1219 | |
| 1220 | private int findLockByBinder(IBinder binder) { |
| 1221 | int size = mList.size(); |
| 1222 | for (int i = size - 1; i >= 0; i--) |
| 1223 | if (mList.get(i).mBinder == binder) |
| 1224 | return i; |
| 1225 | return -1; |
| 1226 | } |
| 1227 | |
| 1228 | private void dump(PrintWriter pw) { |
| 1229 | for (WifiLock l : mList) { |
| 1230 | pw.print(" "); |
| 1231 | pw.println(l); |
| 1232 | } |
| 1233 | } |
| 1234 | } |
| 1235 | |
| Dianne Hackborn | 7e9f4eb | 2010-09-10 18:43:00 -0700 | [diff] [blame] | 1236 | void enforceWakeSourcePermission(int uid, int pid) { |
| Dianne Hackborn | e746f03 | 2010-09-13 16:02:57 -0700 | [diff] [blame] | 1237 | if (uid == android.os.Process.myUid()) { |
| Dianne Hackborn | 7e9f4eb | 2010-09-10 18:43:00 -0700 | [diff] [blame] | 1238 | return; |
| 1239 | } |
| 1240 | mContext.enforcePermission(android.Manifest.permission.UPDATE_DEVICE_STATS, |
| 1241 | pid, uid, null); |
| 1242 | } |
| 1243 | |
| 1244 | public boolean acquireWifiLock(IBinder binder, int lockMode, String tag, WorkSource ws) { |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1245 | mContext.enforceCallingOrSelfPermission(android.Manifest.permission.WAKE_LOCK, null); |
| Irfan Sheriff | 5876a42 | 2010-08-12 20:26:23 -0700 | [diff] [blame] | 1246 | if (lockMode != WifiManager.WIFI_MODE_FULL && |
| 1247 | lockMode != WifiManager.WIFI_MODE_SCAN_ONLY && |
| 1248 | lockMode != WifiManager.WIFI_MODE_FULL_HIGH_PERF) { |
| 1249 | Slog.e(TAG, "Illegal argument, lockMode= " + lockMode); |
| 1250 | if (DBG) throw new IllegalArgumentException("lockMode=" + lockMode); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1251 | return false; |
| 1252 | } |
| Dianne Hackborn | 7e9f4eb | 2010-09-10 18:43:00 -0700 | [diff] [blame] | 1253 | if (ws != null && ws.size() == 0) { |
| 1254 | ws = null; |
| 1255 | } |
| Dianne Hackborn | ecfd7f7 | 2010-10-08 14:23:40 -0700 | [diff] [blame] | 1256 | if (ws != null) { |
| 1257 | enforceWakeSourcePermission(Binder.getCallingUid(), Binder.getCallingPid()); |
| 1258 | } |
| Dianne Hackborn | 7e9f4eb | 2010-09-10 18:43:00 -0700 | [diff] [blame] | 1259 | if (ws == null) { |
| 1260 | ws = new WorkSource(Binder.getCallingUid()); |
| 1261 | } |
| 1262 | WifiLock wifiLock = new WifiLock(lockMode, tag, binder, ws); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1263 | synchronized (mLocks) { |
| 1264 | return acquireWifiLockLocked(wifiLock); |
| 1265 | } |
| 1266 | } |
| 1267 | |
| Dianne Hackborn | 7e9f4eb | 2010-09-10 18:43:00 -0700 | [diff] [blame] | 1268 | private void noteAcquireWifiLock(WifiLock wifiLock) throws RemoteException { |
| 1269 | switch(wifiLock.mMode) { |
| 1270 | case WifiManager.WIFI_MODE_FULL: |
| Irfan Sheriff | 5876a42 | 2010-08-12 20:26:23 -0700 | [diff] [blame] | 1271 | case WifiManager.WIFI_MODE_FULL_HIGH_PERF: |
| Dianne Hackborn | 7e9f4eb | 2010-09-10 18:43:00 -0700 | [diff] [blame] | 1272 | mBatteryStats.noteFullWifiLockAcquiredFromSource(wifiLock.mWorkSource); |
| 1273 | break; |
| Dianne Hackborn | 7e9f4eb | 2010-09-10 18:43:00 -0700 | [diff] [blame] | 1274 | case WifiManager.WIFI_MODE_SCAN_ONLY: |
| 1275 | mBatteryStats.noteScanWifiLockAcquiredFromSource(wifiLock.mWorkSource); |
| 1276 | break; |
| 1277 | } |
| 1278 | } |
| 1279 | |
| 1280 | private void noteReleaseWifiLock(WifiLock wifiLock) throws RemoteException { |
| 1281 | switch(wifiLock.mMode) { |
| 1282 | case WifiManager.WIFI_MODE_FULL: |
| Irfan Sheriff | 5876a42 | 2010-08-12 20:26:23 -0700 | [diff] [blame] | 1283 | case WifiManager.WIFI_MODE_FULL_HIGH_PERF: |
| Dianne Hackborn | 7e9f4eb | 2010-09-10 18:43:00 -0700 | [diff] [blame] | 1284 | mBatteryStats.noteFullWifiLockReleasedFromSource(wifiLock.mWorkSource); |
| 1285 | break; |
| Dianne Hackborn | 7e9f4eb | 2010-09-10 18:43:00 -0700 | [diff] [blame] | 1286 | case WifiManager.WIFI_MODE_SCAN_ONLY: |
| 1287 | mBatteryStats.noteScanWifiLockReleasedFromSource(wifiLock.mWorkSource); |
| 1288 | break; |
| 1289 | } |
| 1290 | } |
| 1291 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1292 | private boolean acquireWifiLockLocked(WifiLock wifiLock) { |
| Irfan Sheriff | c89dd54 | 2010-09-28 08:40:54 -0700 | [diff] [blame] | 1293 | if (DBG) Slog.d(TAG, "acquireWifiLockLocked: " + wifiLock); |
| Robert Greenwalt | f1acb2d | 2009-10-13 08:20:55 -0700 | [diff] [blame] | 1294 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1295 | mLocks.addLock(wifiLock); |
| Robert Greenwalt | f1acb2d | 2009-10-13 08:20:55 -0700 | [diff] [blame] | 1296 | |
| The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 1297 | long ident = Binder.clearCallingIdentity(); |
| 1298 | try { |
| Dianne Hackborn | 7e9f4eb | 2010-09-10 18:43:00 -0700 | [diff] [blame] | 1299 | noteAcquireWifiLock(wifiLock); |
| Robert Greenwalt | 5347bd4 | 2009-05-13 15:10:16 -0700 | [diff] [blame] | 1300 | switch(wifiLock.mMode) { |
| Eric Shienbrood | 5711fad | 2009-03-27 20:25:31 -0700 | [diff] [blame] | 1301 | case WifiManager.WIFI_MODE_FULL: |
| 1302 | ++mFullLocksAcquired; |
| Eric Shienbrood | 5711fad | 2009-03-27 20:25:31 -0700 | [diff] [blame] | 1303 | break; |
| Irfan Sheriff | 5876a42 | 2010-08-12 20:26:23 -0700 | [diff] [blame] | 1304 | case WifiManager.WIFI_MODE_FULL_HIGH_PERF: |
| 1305 | ++mFullHighPerfLocksAcquired; |
| 1306 | break; |
| 1307 | |
| Eric Shienbrood | 5711fad | 2009-03-27 20:25:31 -0700 | [diff] [blame] | 1308 | case WifiManager.WIFI_MODE_SCAN_ONLY: |
| 1309 | ++mScanLocksAcquired; |
| Eric Shienbrood | 5711fad | 2009-03-27 20:25:31 -0700 | [diff] [blame] | 1310 | break; |
| The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 1311 | } |
| Dianne Hackborn | ecfd7f7 | 2010-10-08 14:23:40 -0700 | [diff] [blame] | 1312 | |
| 1313 | // Be aggressive about adding new locks into the accounted state... |
| 1314 | // we want to over-report rather than under-report. |
| 1315 | reportStartWorkSource(); |
| 1316 | |
| 1317 | updateWifiState(); |
| 1318 | return true; |
| The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 1319 | } catch (RemoteException e) { |
| Dianne Hackborn | ecfd7f7 | 2010-10-08 14:23:40 -0700 | [diff] [blame] | 1320 | return false; |
| The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 1321 | } finally { |
| 1322 | Binder.restoreCallingIdentity(ident); |
| 1323 | } |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1324 | } |
| 1325 | |
| Dianne Hackborn | 7e9f4eb | 2010-09-10 18:43:00 -0700 | [diff] [blame] | 1326 | public void updateWifiLockWorkSource(IBinder lock, WorkSource ws) { |
| 1327 | int uid = Binder.getCallingUid(); |
| 1328 | int pid = Binder.getCallingPid(); |
| 1329 | if (ws != null && ws.size() == 0) { |
| 1330 | ws = null; |
| 1331 | } |
| 1332 | if (ws != null) { |
| 1333 | enforceWakeSourcePermission(uid, pid); |
| 1334 | } |
| 1335 | long ident = Binder.clearCallingIdentity(); |
| 1336 | try { |
| 1337 | synchronized (mLocks) { |
| 1338 | int index = mLocks.findLockByBinder(lock); |
| 1339 | if (index < 0) { |
| 1340 | throw new IllegalArgumentException("Wifi lock not active"); |
| 1341 | } |
| 1342 | WifiLock wl = mLocks.mList.get(index); |
| 1343 | noteReleaseWifiLock(wl); |
| 1344 | wl.mWorkSource = ws != null ? new WorkSource(ws) : new WorkSource(uid); |
| 1345 | noteAcquireWifiLock(wl); |
| 1346 | } |
| 1347 | } catch (RemoteException e) { |
| 1348 | } finally { |
| 1349 | Binder.restoreCallingIdentity(ident); |
| 1350 | } |
| 1351 | } |
| 1352 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1353 | public boolean releaseWifiLock(IBinder lock) { |
| 1354 | mContext.enforceCallingOrSelfPermission(android.Manifest.permission.WAKE_LOCK, null); |
| 1355 | synchronized (mLocks) { |
| 1356 | return releaseWifiLockLocked(lock); |
| 1357 | } |
| 1358 | } |
| 1359 | |
| 1360 | private boolean releaseWifiLockLocked(IBinder lock) { |
| Eric Shienbrood | d4c5f89 | 2009-03-24 18:13:20 -0700 | [diff] [blame] | 1361 | boolean hadLock; |
| Robert Greenwalt | f1acb2d | 2009-10-13 08:20:55 -0700 | [diff] [blame] | 1362 | |
| The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 1363 | WifiLock wifiLock = mLocks.removeLock(lock); |
| Robert Greenwalt | f1acb2d | 2009-10-13 08:20:55 -0700 | [diff] [blame] | 1364 | |
| Irfan Sheriff | c89dd54 | 2010-09-28 08:40:54 -0700 | [diff] [blame] | 1365 | if (DBG) Slog.d(TAG, "releaseWifiLockLocked: " + wifiLock); |
| Robert Greenwalt | f1acb2d | 2009-10-13 08:20:55 -0700 | [diff] [blame] | 1366 | |
| Eric Shienbrood | d4c5f89 | 2009-03-24 18:13:20 -0700 | [diff] [blame] | 1367 | hadLock = (wifiLock != null); |
| 1368 | |
| Dianne Hackborn | ecfd7f7 | 2010-10-08 14:23:40 -0700 | [diff] [blame] | 1369 | long ident = Binder.clearCallingIdentity(); |
| 1370 | try { |
| 1371 | if (hadLock) { |
| Dianne Hackborn | 7e9f4eb | 2010-09-10 18:43:00 -0700 | [diff] [blame] | 1372 | noteAcquireWifiLock(wifiLock); |
| Robert Greenwalt | 5347bd4 | 2009-05-13 15:10:16 -0700 | [diff] [blame] | 1373 | switch(wifiLock.mMode) { |
| Eric Shienbrood | 5711fad | 2009-03-27 20:25:31 -0700 | [diff] [blame] | 1374 | case WifiManager.WIFI_MODE_FULL: |
| 1375 | ++mFullLocksReleased; |
| Eric Shienbrood | 5711fad | 2009-03-27 20:25:31 -0700 | [diff] [blame] | 1376 | break; |
| Irfan Sheriff | 5876a42 | 2010-08-12 20:26:23 -0700 | [diff] [blame] | 1377 | case WifiManager.WIFI_MODE_FULL_HIGH_PERF: |
| 1378 | ++mFullHighPerfLocksReleased; |
| 1379 | break; |
| Eric Shienbrood | 5711fad | 2009-03-27 20:25:31 -0700 | [diff] [blame] | 1380 | case WifiManager.WIFI_MODE_SCAN_ONLY: |
| 1381 | ++mScanLocksReleased; |
| Eric Shienbrood | 5711fad | 2009-03-27 20:25:31 -0700 | [diff] [blame] | 1382 | break; |
| Eric Shienbrood | d4c5f89 | 2009-03-24 18:13:20 -0700 | [diff] [blame] | 1383 | } |
| The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 1384 | } |
| Dianne Hackborn | ecfd7f7 | 2010-10-08 14:23:40 -0700 | [diff] [blame] | 1385 | |
| 1386 | // TODO - should this only happen if you hadLock? |
| 1387 | updateWifiState(); |
| 1388 | |
| 1389 | } catch (RemoteException e) { |
| 1390 | } finally { |
| 1391 | Binder.restoreCallingIdentity(ident); |
| The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 1392 | } |
| Dianne Hackborn | ecfd7f7 | 2010-10-08 14:23:40 -0700 | [diff] [blame] | 1393 | |
| Eric Shienbrood | d4c5f89 | 2009-03-24 18:13:20 -0700 | [diff] [blame] | 1394 | return hadLock; |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1395 | } |
| Robert Greenwalt | 5347bd4 | 2009-05-13 15:10:16 -0700 | [diff] [blame] | 1396 | |
| Robert Greenwalt | 58ff021 | 2009-05-19 15:53:54 -0700 | [diff] [blame] | 1397 | private abstract class DeathRecipient |
| Robert Greenwalt | 5347bd4 | 2009-05-13 15:10:16 -0700 | [diff] [blame] | 1398 | implements IBinder.DeathRecipient { |
| 1399 | String mTag; |
| 1400 | int mMode; |
| 1401 | IBinder mBinder; |
| Dianne Hackborn | 7e9f4eb | 2010-09-10 18:43:00 -0700 | [diff] [blame] | 1402 | WorkSource mWorkSource; |
| Robert Greenwalt | 5347bd4 | 2009-05-13 15:10:16 -0700 | [diff] [blame] | 1403 | |
| Dianne Hackborn | 7e9f4eb | 2010-09-10 18:43:00 -0700 | [diff] [blame] | 1404 | DeathRecipient(int mode, String tag, IBinder binder, WorkSource ws) { |
| Robert Greenwalt | 5347bd4 | 2009-05-13 15:10:16 -0700 | [diff] [blame] | 1405 | super(); |
| 1406 | mTag = tag; |
| 1407 | mMode = mode; |
| 1408 | mBinder = binder; |
| Dianne Hackborn | 7e9f4eb | 2010-09-10 18:43:00 -0700 | [diff] [blame] | 1409 | mWorkSource = ws; |
| Robert Greenwalt | 5347bd4 | 2009-05-13 15:10:16 -0700 | [diff] [blame] | 1410 | try { |
| 1411 | mBinder.linkToDeath(this, 0); |
| 1412 | } catch (RemoteException e) { |
| 1413 | binderDied(); |
| 1414 | } |
| 1415 | } |
| Suchi Amalapurapu | fff2fda | 2009-06-30 21:36:16 -0700 | [diff] [blame] | 1416 | |
| 1417 | void unlinkDeathRecipient() { |
| 1418 | mBinder.unlinkToDeath(this, 0); |
| 1419 | } |
| Robert Greenwalt | 5347bd4 | 2009-05-13 15:10:16 -0700 | [diff] [blame] | 1420 | } |
| 1421 | |
| Robert Greenwalt | 58ff021 | 2009-05-19 15:53:54 -0700 | [diff] [blame] | 1422 | private class Multicaster extends DeathRecipient { |
| 1423 | Multicaster(String tag, IBinder binder) { |
| Dianne Hackborn | 7e9f4eb | 2010-09-10 18:43:00 -0700 | [diff] [blame] | 1424 | super(Binder.getCallingUid(), tag, binder, null); |
| Robert Greenwalt | 5347bd4 | 2009-05-13 15:10:16 -0700 | [diff] [blame] | 1425 | } |
| 1426 | |
| 1427 | public void binderDied() { |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 1428 | Slog.e(TAG, "Multicaster binderDied"); |
| Robert Greenwalt | 5347bd4 | 2009-05-13 15:10:16 -0700 | [diff] [blame] | 1429 | synchronized (mMulticasters) { |
| 1430 | int i = mMulticasters.indexOf(this); |
| 1431 | if (i != -1) { |
| 1432 | removeMulticasterLocked(i, mMode); |
| 1433 | } |
| 1434 | } |
| 1435 | } |
| 1436 | |
| 1437 | public String toString() { |
| Robert Greenwalt | 58ff021 | 2009-05-19 15:53:54 -0700 | [diff] [blame] | 1438 | return "Multicaster{" + mTag + " binder=" + mBinder + "}"; |
| Robert Greenwalt | 5347bd4 | 2009-05-13 15:10:16 -0700 | [diff] [blame] | 1439 | } |
| 1440 | |
| 1441 | public int getUid() { |
| 1442 | return mMode; |
| 1443 | } |
| 1444 | } |
| 1445 | |
| Robert Greenwalt | e2d155a | 2009-10-21 14:58:34 -0700 | [diff] [blame] | 1446 | public void initializeMulticastFiltering() { |
| 1447 | enforceMulticastChangePermission(); |
| Irfan Sheriff | a8fbe1f | 2010-03-09 09:13:58 -0800 | [diff] [blame] | 1448 | |
| Robert Greenwalt | e2d155a | 2009-10-21 14:58:34 -0700 | [diff] [blame] | 1449 | synchronized (mMulticasters) { |
| 1450 | // if anybody had requested filters be off, leave off |
| 1451 | if (mMulticasters.size() != 0) { |
| 1452 | return; |
| 1453 | } else { |
| Irfan Sheriff | 0d25534 | 2010-07-28 09:35:20 -0700 | [diff] [blame] | 1454 | mWifiStateMachine.startPacketFiltering(); |
| Robert Greenwalt | e2d155a | 2009-10-21 14:58:34 -0700 | [diff] [blame] | 1455 | } |
| 1456 | } |
| 1457 | } |
| 1458 | |
| Robert Greenwalt | fc1b15c | 2009-05-22 15:09:51 -0700 | [diff] [blame] | 1459 | public void acquireMulticastLock(IBinder binder, String tag) { |
| 1460 | enforceMulticastChangePermission(); |
| Robert Greenwalt | 5347bd4 | 2009-05-13 15:10:16 -0700 | [diff] [blame] | 1461 | |
| 1462 | synchronized (mMulticasters) { |
| 1463 | mMulticastEnabled++; |
| Robert Greenwalt | 58ff021 | 2009-05-19 15:53:54 -0700 | [diff] [blame] | 1464 | mMulticasters.add(new Multicaster(tag, binder)); |
| Robert Greenwalt | 5347bd4 | 2009-05-13 15:10:16 -0700 | [diff] [blame] | 1465 | // Note that we could call stopPacketFiltering only when |
| 1466 | // our new size == 1 (first call), but this function won't |
| 1467 | // be called often and by making the stopPacket call each |
| 1468 | // time we're less fragile and self-healing. |
| Irfan Sheriff | 0d25534 | 2010-07-28 09:35:20 -0700 | [diff] [blame] | 1469 | mWifiStateMachine.stopPacketFiltering(); |
| Robert Greenwalt | 5347bd4 | 2009-05-13 15:10:16 -0700 | [diff] [blame] | 1470 | } |
| 1471 | |
| 1472 | int uid = Binder.getCallingUid(); |
| 1473 | Long ident = Binder.clearCallingIdentity(); |
| 1474 | try { |
| 1475 | mBatteryStats.noteWifiMulticastEnabled(uid); |
| 1476 | } catch (RemoteException e) { |
| 1477 | } finally { |
| 1478 | Binder.restoreCallingIdentity(ident); |
| 1479 | } |
| 1480 | } |
| 1481 | |
| Robert Greenwalt | fc1b15c | 2009-05-22 15:09:51 -0700 | [diff] [blame] | 1482 | public void releaseMulticastLock() { |
| 1483 | enforceMulticastChangePermission(); |
| Robert Greenwalt | 5347bd4 | 2009-05-13 15:10:16 -0700 | [diff] [blame] | 1484 | |
| 1485 | int uid = Binder.getCallingUid(); |
| 1486 | synchronized (mMulticasters) { |
| 1487 | mMulticastDisabled++; |
| 1488 | int size = mMulticasters.size(); |
| 1489 | for (int i = size - 1; i >= 0; i--) { |
| Robert Greenwalt | 58ff021 | 2009-05-19 15:53:54 -0700 | [diff] [blame] | 1490 | Multicaster m = mMulticasters.get(i); |
| Robert Greenwalt | 5347bd4 | 2009-05-13 15:10:16 -0700 | [diff] [blame] | 1491 | if ((m != null) && (m.getUid() == uid)) { |
| 1492 | removeMulticasterLocked(i, uid); |
| 1493 | } |
| 1494 | } |
| 1495 | } |
| 1496 | } |
| 1497 | |
| 1498 | private void removeMulticasterLocked(int i, int uid) |
| 1499 | { |
| Suchi Amalapurapu | fff2fda | 2009-06-30 21:36:16 -0700 | [diff] [blame] | 1500 | Multicaster removed = mMulticasters.remove(i); |
| Irfan Sheriff | a8fbe1f | 2010-03-09 09:13:58 -0800 | [diff] [blame] | 1501 | |
| Suchi Amalapurapu | fff2fda | 2009-06-30 21:36:16 -0700 | [diff] [blame] | 1502 | if (removed != null) { |
| 1503 | removed.unlinkDeathRecipient(); |
| 1504 | } |
| Robert Greenwalt | 5347bd4 | 2009-05-13 15:10:16 -0700 | [diff] [blame] | 1505 | if (mMulticasters.size() == 0) { |
| Irfan Sheriff | 0d25534 | 2010-07-28 09:35:20 -0700 | [diff] [blame] | 1506 | mWifiStateMachine.startPacketFiltering(); |
| Robert Greenwalt | 5347bd4 | 2009-05-13 15:10:16 -0700 | [diff] [blame] | 1507 | } |
| 1508 | |
| 1509 | Long ident = Binder.clearCallingIdentity(); |
| 1510 | try { |
| 1511 | mBatteryStats.noteWifiMulticastDisabled(uid); |
| 1512 | } catch (RemoteException e) { |
| 1513 | } finally { |
| 1514 | Binder.restoreCallingIdentity(ident); |
| 1515 | } |
| 1516 | } |
| 1517 | |
| Robert Greenwalt | 58ff021 | 2009-05-19 15:53:54 -0700 | [diff] [blame] | 1518 | public boolean isMulticastEnabled() { |
| Robert Greenwalt | 5347bd4 | 2009-05-13 15:10:16 -0700 | [diff] [blame] | 1519 | enforceAccessPermission(); |
| 1520 | |
| 1521 | synchronized (mMulticasters) { |
| 1522 | return (mMulticasters.size() > 0); |
| 1523 | } |
| 1524 | } |
| Irfan Sheriff | 0d25534 | 2010-07-28 09:35:20 -0700 | [diff] [blame] | 1525 | |
| Irfan Sheriff | 227bec4 | 2011-02-15 19:30:27 -0800 | [diff] [blame] | 1526 | /** |
| 1527 | * Evaluate if traffic stats polling is needed based on |
| 1528 | * connection and screen on status |
| 1529 | */ |
| 1530 | private void evaluateTrafficStatsPolling() { |
| 1531 | Message msg; |
| 1532 | if (mNetworkInfo.getDetailedState() == DetailedState.CONNECTED && !mScreenOff) { |
| 1533 | msg = Message.obtain(mAsyncServiceHandler, CMD_ENABLE_TRAFFIC_STATS_POLL, 1, 0); |
| 1534 | } else { |
| 1535 | msg = Message.obtain(mAsyncServiceHandler, CMD_ENABLE_TRAFFIC_STATS_POLL, 0, 0); |
| 1536 | } |
| 1537 | msg.sendToTarget(); |
| 1538 | } |
| 1539 | |
| 1540 | private void notifyOnDataActivity() { |
| 1541 | long sent, received; |
| 1542 | long preTxPkts = mTxPkts, preRxPkts = mRxPkts; |
| 1543 | int dataActivity = WifiManager.DATA_ACTIVITY_NONE; |
| 1544 | |
| 1545 | mTxPkts = TrafficStats.getTxPackets(mInterfaceName); |
| 1546 | mRxPkts = TrafficStats.getRxPackets(mInterfaceName); |
| 1547 | |
| 1548 | if (preTxPkts > 0 || preRxPkts > 0) { |
| 1549 | sent = mTxPkts - preTxPkts; |
| 1550 | received = mRxPkts - preRxPkts; |
| 1551 | if (sent > 0) { |
| 1552 | dataActivity |= WifiManager.DATA_ACTIVITY_OUT; |
| 1553 | } |
| 1554 | if (received > 0) { |
| 1555 | dataActivity |= WifiManager.DATA_ACTIVITY_IN; |
| 1556 | } |
| 1557 | |
| 1558 | if (dataActivity != mDataActivity && !mScreenOff) { |
| 1559 | mDataActivity = dataActivity; |
| 1560 | for (AsyncChannel client : mClients) { |
| 1561 | client.sendMessage(WifiManager.DATA_ACTIVITY_NOTIFICATION, mDataActivity); |
| 1562 | } |
| 1563 | } |
| 1564 | } |
| 1565 | } |
| 1566 | |
| 1567 | |
| Irfan Sheriff | 0d25534 | 2010-07-28 09:35:20 -0700 | [diff] [blame] | 1568 | private void checkAndSetNotification() { |
| 1569 | // If we shouldn't place a notification on available networks, then |
| 1570 | // don't bother doing any of the following |
| 1571 | if (!mNotificationEnabled) return; |
| 1572 | |
| 1573 | State state = mNetworkInfo.getState(); |
| 1574 | if ((state == NetworkInfo.State.DISCONNECTED) |
| 1575 | || (state == NetworkInfo.State.UNKNOWN)) { |
| 1576 | // Look for an open network |
| Irfan Sheriff | d8134ff | 2010-08-22 17:06:34 -0700 | [diff] [blame] | 1577 | List<ScanResult> scanResults = mWifiStateMachine.syncGetScanResultsList(); |
| Irfan Sheriff | 0d25534 | 2010-07-28 09:35:20 -0700 | [diff] [blame] | 1578 | if (scanResults != null) { |
| 1579 | int numOpenNetworks = 0; |
| 1580 | for (int i = scanResults.size() - 1; i >= 0; i--) { |
| 1581 | ScanResult scanResult = scanResults.get(i); |
| 1582 | |
| 1583 | if (TextUtils.isEmpty(scanResult.capabilities)) { |
| 1584 | numOpenNetworks++; |
| 1585 | } |
| 1586 | } |
| 1587 | |
| 1588 | if (numOpenNetworks > 0) { |
| 1589 | if (++mNumScansSinceNetworkStateChange >= NUM_SCANS_BEFORE_ACTUALLY_SCANNING) { |
| 1590 | /* |
| 1591 | * We've scanned continuously at least |
| 1592 | * NUM_SCANS_BEFORE_NOTIFICATION times. The user |
| 1593 | * probably does not have a remembered network in range, |
| 1594 | * since otherwise supplicant would have tried to |
| 1595 | * associate and thus resetting this counter. |
| 1596 | */ |
| 1597 | setNotificationVisible(true, numOpenNetworks, false, 0); |
| 1598 | } |
| 1599 | return; |
| 1600 | } |
| 1601 | } |
| 1602 | } |
| 1603 | |
| 1604 | // No open networks in range, remove the notification |
| 1605 | setNotificationVisible(false, 0, false, 0); |
| 1606 | } |
| 1607 | |
| 1608 | /** |
| 1609 | * Clears variables related to tracking whether a notification has been |
| 1610 | * shown recently and clears the current notification. |
| 1611 | */ |
| 1612 | private void resetNotification() { |
| 1613 | mNotificationRepeatTime = 0; |
| 1614 | mNumScansSinceNetworkStateChange = 0; |
| 1615 | setNotificationVisible(false, 0, false, 0); |
| 1616 | } |
| 1617 | |
| 1618 | /** |
| 1619 | * Display or don't display a notification that there are open Wi-Fi networks. |
| 1620 | * @param visible {@code true} if notification should be visible, {@code false} otherwise |
| 1621 | * @param numNetworks the number networks seen |
| 1622 | * @param force {@code true} to force notification to be shown/not-shown, |
| 1623 | * even if it is already shown/not-shown. |
| 1624 | * @param delay time in milliseconds after which the notification should be made |
| 1625 | * visible or invisible. |
| 1626 | */ |
| 1627 | private void setNotificationVisible(boolean visible, int numNetworks, boolean force, |
| 1628 | int delay) { |
| 1629 | |
| 1630 | // Since we use auto cancel on the notification, when the |
| 1631 | // mNetworksAvailableNotificationShown is true, the notification may |
| 1632 | // have actually been canceled. However, when it is false we know |
| 1633 | // for sure that it is not being shown (it will not be shown any other |
| 1634 | // place than here) |
| 1635 | |
| 1636 | // If it should be hidden and it is already hidden, then noop |
| 1637 | if (!visible && !mNotificationShown && !force) { |
| 1638 | return; |
| 1639 | } |
| 1640 | |
| 1641 | NotificationManager notificationManager = (NotificationManager) mContext |
| 1642 | .getSystemService(Context.NOTIFICATION_SERVICE); |
| 1643 | |
| 1644 | Message message; |
| 1645 | if (visible) { |
| 1646 | |
| 1647 | // Not enough time has passed to show the notification again |
| 1648 | if (System.currentTimeMillis() < mNotificationRepeatTime) { |
| 1649 | return; |
| 1650 | } |
| 1651 | |
| 1652 | if (mNotification == null) { |
| Wink Saville | c7a9834 | 2010-08-13 16:11:42 -0700 | [diff] [blame] | 1653 | // Cache the Notification object. |
| Irfan Sheriff | 0d25534 | 2010-07-28 09:35:20 -0700 | [diff] [blame] | 1654 | mNotification = new Notification(); |
| 1655 | mNotification.when = 0; |
| 1656 | mNotification.icon = ICON_NETWORKS_AVAILABLE; |
| 1657 | mNotification.flags = Notification.FLAG_AUTO_CANCEL; |
| 1658 | mNotification.contentIntent = PendingIntent.getActivity(mContext, 0, |
| 1659 | new Intent(WifiManager.ACTION_PICK_WIFI_NETWORK), 0); |
| 1660 | } |
| 1661 | |
| 1662 | CharSequence title = mContext.getResources().getQuantityText( |
| 1663 | com.android.internal.R.plurals.wifi_available, numNetworks); |
| 1664 | CharSequence details = mContext.getResources().getQuantityText( |
| 1665 | com.android.internal.R.plurals.wifi_available_detailed, numNetworks); |
| 1666 | mNotification.tickerText = title; |
| 1667 | mNotification.setLatestEventInfo(mContext, title, details, mNotification.contentIntent); |
| 1668 | |
| 1669 | mNotificationRepeatTime = System.currentTimeMillis() + NOTIFICATION_REPEAT_DELAY_MS; |
| 1670 | |
| 1671 | notificationManager.notify(ICON_NETWORKS_AVAILABLE, mNotification); |
| Irfan Sheriff | 0d25534 | 2010-07-28 09:35:20 -0700 | [diff] [blame] | 1672 | } else { |
| Irfan Sheriff | 0d25534 | 2010-07-28 09:35:20 -0700 | [diff] [blame] | 1673 | notificationManager.cancel(ICON_NETWORKS_AVAILABLE); |
| Irfan Sheriff | 0d25534 | 2010-07-28 09:35:20 -0700 | [diff] [blame] | 1674 | } |
| 1675 | |
| Irfan Sheriff | 0d25534 | 2010-07-28 09:35:20 -0700 | [diff] [blame] | 1676 | mNotificationShown = visible; |
| 1677 | } |
| 1678 | |
| 1679 | private class NotificationEnabledSettingObserver extends ContentObserver { |
| 1680 | |
| 1681 | public NotificationEnabledSettingObserver(Handler handler) { |
| 1682 | super(handler); |
| 1683 | } |
| 1684 | |
| 1685 | public void register() { |
| 1686 | ContentResolver cr = mContext.getContentResolver(); |
| 1687 | cr.registerContentObserver(Settings.Secure.getUriFor( |
| 1688 | Settings.Secure.WIFI_NETWORKS_AVAILABLE_NOTIFICATION_ON), true, this); |
| 1689 | mNotificationEnabled = getValue(); |
| 1690 | } |
| 1691 | |
| 1692 | @Override |
| 1693 | public void onChange(boolean selfChange) { |
| 1694 | super.onChange(selfChange); |
| 1695 | |
| 1696 | mNotificationEnabled = getValue(); |
| 1697 | resetNotification(); |
| 1698 | } |
| 1699 | |
| 1700 | private boolean getValue() { |
| 1701 | return Settings.Secure.getInt(mContext.getContentResolver(), |
| 1702 | Settings.Secure.WIFI_NETWORKS_AVAILABLE_NOTIFICATION_ON, 1) == 1; |
| 1703 | } |
| 1704 | } |
| 1705 | |
| 1706 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1707 | } |