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