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