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