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