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