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