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