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