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