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