| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2008 The Android Open Source Project |
| 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 | |
| 19 | import static android.net.wifi.WifiManager.WIFI_STATE_DISABLED; |
| 20 | import static android.net.wifi.WifiManager.WIFI_STATE_DISABLING; |
| 21 | import static android.net.wifi.WifiManager.WIFI_STATE_ENABLED; |
| 22 | import static android.net.wifi.WifiManager.WIFI_STATE_ENABLING; |
| 23 | import static android.net.wifi.WifiManager.WIFI_STATE_UNKNOWN; |
| 24 | |
| Irfan Sheriff | 5321aef | 2010-02-12 12:35:59 -0800 | [diff] [blame] | 25 | import static android.net.wifi.WifiManager.WIFI_AP_STATE_DISABLED; |
| 26 | import static android.net.wifi.WifiManager.WIFI_AP_STATE_DISABLING; |
| 27 | import static android.net.wifi.WifiManager.WIFI_AP_STATE_ENABLED; |
| 28 | import static android.net.wifi.WifiManager.WIFI_AP_STATE_ENABLING; |
| 29 | import static android.net.wifi.WifiManager.WIFI_AP_STATE_FAILED; |
| 30 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 31 | import android.app.AlarmManager; |
| 32 | import android.app.PendingIntent; |
| The Android Open Source Project | b2a3dd8 | 2009-03-09 11:52:12 -0700 | [diff] [blame] | 33 | import android.bluetooth.BluetoothA2dp; |
| Jaikumar Ganesh | 084c665 | 2009-12-07 10:58:18 -0800 | [diff] [blame] | 34 | import android.bluetooth.BluetoothDevice; |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 35 | import android.content.BroadcastReceiver; |
| 36 | import android.content.ContentResolver; |
| 37 | import android.content.Context; |
| 38 | import android.content.Intent; |
| 39 | import android.content.IntentFilter; |
| 40 | import android.content.pm.PackageManager; |
| 41 | import android.net.wifi.IWifiManager; |
| 42 | import android.net.wifi.WifiInfo; |
| 43 | import android.net.wifi.WifiManager; |
| 44 | import android.net.wifi.WifiNative; |
| 45 | import android.net.wifi.WifiStateTracker; |
| 46 | import android.net.wifi.ScanResult; |
| 47 | import android.net.wifi.WifiConfiguration; |
| San Mehat | 0310f9a | 2009-07-07 10:49:47 -0700 | [diff] [blame] | 48 | import android.net.wifi.SupplicantState; |
| Irfan Sheriff | 9ab518ad | 2010-03-12 15:48:17 -0800 | [diff] [blame] | 49 | import android.net.wifi.WifiConfiguration.KeyMgmt; |
| Irfan Sheriff | 5321aef | 2010-02-12 12:35:59 -0800 | [diff] [blame] | 50 | import android.net.ConnectivityManager; |
| 51 | import android.net.InterfaceConfiguration; |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 52 | import android.net.NetworkStateTracker; |
| 53 | import android.net.DhcpInfo; |
| Mike Lockwood | 0900f36 | 2009-07-10 17:24:07 -0400 | [diff] [blame] | 54 | import android.net.NetworkUtils; |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 55 | import android.os.Binder; |
| 56 | import android.os.Handler; |
| 57 | import android.os.HandlerThread; |
| 58 | import android.os.IBinder; |
| Irfan Sheriff | 5321aef | 2010-02-12 12:35:59 -0800 | [diff] [blame] | 59 | import android.os.INetworkManagementService; |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 60 | import android.os.Looper; |
| 61 | import android.os.Message; |
| 62 | import android.os.PowerManager; |
| Dianne Hackborn | 617f877 | 2009-03-31 15:04:46 -0700 | [diff] [blame] | 63 | import android.os.Process; |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 64 | import android.os.RemoteException; |
| Amith Yamasani | 47873e5 | 2009-07-02 12:05:32 -0700 | [diff] [blame] | 65 | import android.os.ServiceManager; |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 66 | import android.provider.Settings; |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 67 | import android.util.Slog; |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 68 | import android.text.TextUtils; |
| 69 | |
| 70 | import java.util.ArrayList; |
| 71 | import java.util.BitSet; |
| 72 | import java.util.HashMap; |
| 73 | import java.util.LinkedHashMap; |
| 74 | import java.util.List; |
| 75 | import java.util.Map; |
| Jaikumar Ganesh | 084c665 | 2009-12-07 10:58:18 -0800 | [diff] [blame] | 76 | import java.util.Set; |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 77 | import java.util.regex.Pattern; |
| 78 | import java.io.FileDescriptor; |
| 79 | import java.io.PrintWriter; |
| Irfan Sheriff | 5321aef | 2010-02-12 12:35:59 -0800 | [diff] [blame] | 80 | import java.net.UnknownHostException; |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 81 | |
| The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 82 | import com.android.internal.app.IBatteryStats; |
| Christopher Tate | 4528186 | 2010-03-05 15:46:30 -0800 | [diff] [blame] | 83 | import android.app.backup.IBackupManager; |
| The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 84 | import com.android.server.am.BatteryStatsService; |
| Irfan Sheriff | 9ab518ad | 2010-03-12 15:48:17 -0800 | [diff] [blame] | 85 | import com.android.internal.R; |
| The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 86 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 87 | /** |
| 88 | * WifiService handles remote WiFi operation requests by implementing |
| 89 | * the IWifiManager interface. It also creates a WifiMonitor to listen |
| 90 | * for Wifi-related events. |
| 91 | * |
| 92 | * @hide |
| 93 | */ |
| 94 | public class WifiService extends IWifiManager.Stub { |
| 95 | private static final String TAG = "WifiService"; |
| 96 | private static final boolean DBG = false; |
| 97 | private static final Pattern scanResultPattern = Pattern.compile("\t+"); |
| 98 | private final WifiStateTracker mWifiStateTracker; |
| Irfan Sheriff | c2f54c2 | 2010-03-18 14:02:22 -0700 | [diff] [blame] | 99 | /* TODO: fetch a configurable interface */ |
| 100 | private static final String SOFTAP_IFACE = "wl0.1"; |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 101 | |
| 102 | private Context mContext; |
| Irfan Sheriff | 5321aef | 2010-02-12 12:35:59 -0800 | [diff] [blame] | 103 | private int mWifiApState; |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 104 | |
| 105 | private AlarmManager mAlarmManager; |
| 106 | private PendingIntent mIdleIntent; |
| 107 | private static final int IDLE_REQUEST = 0; |
| 108 | private boolean mScreenOff; |
| 109 | private boolean mDeviceIdle; |
| 110 | private int mPluggedType; |
| 111 | |
| Irfan Sheriff | 9ab518ad | 2010-03-12 15:48:17 -0800 | [diff] [blame] | 112 | private enum DriverAction {DRIVER_UNLOAD, NO_DRIVER_UNLOAD}; |
| 113 | |
| Mike Lockwood | bd5ddf0 | 2009-07-29 21:37:14 -0700 | [diff] [blame] | 114 | // true if the user enabled Wifi while in airplane mode |
| 115 | private boolean mAirplaneModeOverwridden; |
| 116 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 117 | private final LockList mLocks = new LockList(); |
| Eric Shienbrood | 5711fad | 2009-03-27 20:25:31 -0700 | [diff] [blame] | 118 | // some wifi lock statistics |
| 119 | private int mFullLocksAcquired; |
| 120 | private int mFullLocksReleased; |
| 121 | private int mScanLocksAcquired; |
| 122 | private int mScanLocksReleased; |
| The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 123 | |
| Robert Greenwalt | 58ff021 | 2009-05-19 15:53:54 -0700 | [diff] [blame] | 124 | private final List<Multicaster> mMulticasters = |
| 125 | new ArrayList<Multicaster>(); |
| Robert Greenwalt | 5347bd4 | 2009-05-13 15:10:16 -0700 | [diff] [blame] | 126 | private int mMulticastEnabled; |
| 127 | private int mMulticastDisabled; |
| 128 | |
| The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 129 | private final IBatteryStats mBatteryStats; |
| Jaikumar Ganesh | 084c665 | 2009-12-07 10:58:18 -0800 | [diff] [blame] | 130 | |
| Irfan Sheriff | 5321aef | 2010-02-12 12:35:59 -0800 | [diff] [blame] | 131 | private INetworkManagementService nwService; |
| 132 | ConnectivityManager mCm; |
| Irfan Sheriff | 7b00978 | 2010-03-11 16:37:45 -0800 | [diff] [blame] | 133 | private WifiWatchdogService mWifiWatchdogService = null; |
| Irfan Sheriff | 5321aef | 2010-02-12 12:35:59 -0800 | [diff] [blame] | 134 | private String[] mWifiRegexs; |
| 135 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 136 | /** |
| Doug Zongker | 43866e0 | 2010-01-07 12:09:54 -0800 | [diff] [blame] | 137 | * See {@link Settings.Secure#WIFI_IDLE_MS}. This is the default value if a |
| 138 | * 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] | 139 | * the approximate point at which the battery drain caused by Wi-Fi |
| 140 | * being enabled but not active exceeds the battery drain caused by |
| 141 | * re-establishing a connection to the mobile data network. |
| 142 | */ |
| 143 | private static final long DEFAULT_IDLE_MILLIS = 15 * 60 * 1000; /* 15 minutes */ |
| 144 | |
| 145 | private static final String WAKELOCK_TAG = "WifiService"; |
| 146 | |
| 147 | /** |
| 148 | * The maximum amount of time to hold the wake lock after a disconnect |
| 149 | * caused by stopping the driver. Establishing an EDGE connection has been |
| 150 | * observed to take about 5 seconds under normal circumstances. This |
| 151 | * provides a bit of extra margin. |
| 152 | * <p> |
| 153 | * See {@link android.provider.Settings.Secure#WIFI_MOBILE_DATA_TRANSITION_WAKELOCK_TIMEOUT_MS}. |
| 154 | * This is the default value if a Settings.Secure value is not present. |
| 155 | */ |
| 156 | private static final int DEFAULT_WAKELOCK_TIMEOUT = 8000; |
| 157 | |
| 158 | // Wake lock used by driver-stop operation |
| 159 | private static PowerManager.WakeLock sDriverStopWakeLock; |
| 160 | // Wake lock used by other operations |
| 161 | private static PowerManager.WakeLock sWakeLock; |
| 162 | |
| 163 | private static final int MESSAGE_ENABLE_WIFI = 0; |
| 164 | private static final int MESSAGE_DISABLE_WIFI = 1; |
| 165 | private static final int MESSAGE_STOP_WIFI = 2; |
| 166 | private static final int MESSAGE_START_WIFI = 3; |
| 167 | private static final int MESSAGE_RELEASE_WAKELOCK = 4; |
| Robert Greenwalt | f75aa36fc | 2009-10-22 17:03:47 -0700 | [diff] [blame] | 168 | private static final int MESSAGE_UPDATE_STATE = 5; |
| Irfan Sheriff | 5321aef | 2010-02-12 12:35:59 -0800 | [diff] [blame] | 169 | private static final int MESSAGE_START_ACCESS_POINT = 6; |
| 170 | private static final int MESSAGE_STOP_ACCESS_POINT = 7; |
| 171 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 172 | |
| 173 | private final WifiHandler mWifiHandler; |
| 174 | |
| 175 | /* |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 176 | * Cache of scan results objects (size is somewhat arbitrary) |
| 177 | */ |
| 178 | private static final int SCAN_RESULT_CACHE_SIZE = 80; |
| 179 | private final LinkedHashMap<String, ScanResult> mScanResultCache; |
| 180 | |
| 181 | /* |
| 182 | * Character buffer used to parse scan results (optimization) |
| 183 | */ |
| 184 | private static final int SCAN_RESULT_BUFFER_SIZE = 512; |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 185 | private boolean mNeedReconfig; |
| 186 | |
| Dianne Hackborn | 617f877 | 2009-03-31 15:04:46 -0700 | [diff] [blame] | 187 | /* |
| 188 | * Last UID that asked to enable WIFI. |
| 189 | */ |
| 190 | private int mLastEnableUid = Process.myUid(); |
| Jaikumar Ganesh | 084c665 | 2009-12-07 10:58:18 -0800 | [diff] [blame] | 191 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 192 | /** |
| 193 | * Number of allowed radio frequency channels in various regulatory domains. |
| 194 | * This list is sufficient for 802.11b/g networks (2.4GHz range). |
| 195 | */ |
| 196 | private static int[] sValidRegulatoryChannelCounts = new int[] {11, 13, 14}; |
| 197 | |
| 198 | private static final String ACTION_DEVICE_IDLE = |
| 199 | "com.android.server.WifiManager.action.DEVICE_IDLE"; |
| 200 | |
| 201 | WifiService(Context context, WifiStateTracker tracker) { |
| 202 | mContext = context; |
| 203 | mWifiStateTracker = tracker; |
| Mike Lockwood | f32be16 | 2009-07-14 17:44:37 -0400 | [diff] [blame] | 204 | mWifiStateTracker.enableRssiPolling(true); |
| The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 205 | mBatteryStats = BatteryStatsService.getService(); |
| Jaikumar Ganesh | 084c665 | 2009-12-07 10:58:18 -0800 | [diff] [blame] | 206 | |
| Irfan Sheriff | 5321aef | 2010-02-12 12:35:59 -0800 | [diff] [blame] | 207 | IBinder b = ServiceManager.getService(Context.NETWORKMANAGEMENT_SERVICE); |
| 208 | nwService = INetworkManagementService.Stub.asInterface(b); |
| 209 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 210 | mScanResultCache = new LinkedHashMap<String, ScanResult>( |
| 211 | SCAN_RESULT_CACHE_SIZE, 0.75f, true) { |
| 212 | /* |
| 213 | * Limit the cache size by SCAN_RESULT_CACHE_SIZE |
| 214 | * elements |
| 215 | */ |
| 216 | public boolean removeEldestEntry(Map.Entry eldest) { |
| 217 | return SCAN_RESULT_CACHE_SIZE < this.size(); |
| 218 | } |
| 219 | }; |
| 220 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 221 | HandlerThread wifiThread = new HandlerThread("WifiService"); |
| 222 | wifiThread.start(); |
| 223 | mWifiHandler = new WifiHandler(wifiThread.getLooper()); |
| 224 | |
| Irfan Sheriff | 0f34406009 | 2010-03-10 10:05:51 -0800 | [diff] [blame] | 225 | mWifiStateTracker.setWifiState(WIFI_STATE_DISABLED); |
| Irfan Sheriff | 5321aef | 2010-02-12 12:35:59 -0800 | [diff] [blame] | 226 | mWifiApState = WIFI_AP_STATE_DISABLED; |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 227 | |
| 228 | mAlarmManager = (AlarmManager)mContext.getSystemService(Context.ALARM_SERVICE); |
| 229 | Intent idleIntent = new Intent(ACTION_DEVICE_IDLE, null); |
| 230 | mIdleIntent = PendingIntent.getBroadcast(mContext, IDLE_REQUEST, idleIntent, 0); |
| 231 | |
| 232 | PowerManager powerManager = (PowerManager)mContext.getSystemService(Context.POWER_SERVICE); |
| 233 | sWakeLock = powerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, WAKELOCK_TAG); |
| 234 | sDriverStopWakeLock = powerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, WAKELOCK_TAG); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 235 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 236 | mContext.registerReceiver( |
| 237 | new BroadcastReceiver() { |
| 238 | @Override |
| 239 | public void onReceive(Context context, Intent intent) { |
| Mike Lockwood | bd5ddf0 | 2009-07-29 21:37:14 -0700 | [diff] [blame] | 240 | // clear our flag indicating the user has overwridden airplane mode |
| 241 | mAirplaneModeOverwridden = false; |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 242 | updateWifiState(); |
| 243 | } |
| 244 | }, |
| 245 | new IntentFilter(Intent.ACTION_AIRPLANE_MODE_CHANGED)); |
| 246 | |
| Irfan Sheriff | 5321aef | 2010-02-12 12:35:59 -0800 | [diff] [blame] | 247 | mContext.registerReceiver( |
| 248 | new BroadcastReceiver() { |
| 249 | @Override |
| 250 | public void onReceive(Context context, Intent intent) { |
| 251 | |
| 252 | ArrayList<String> available = intent.getStringArrayListExtra( |
| 253 | ConnectivityManager.EXTRA_AVAILABLE_TETHER); |
| 254 | ArrayList<String> active = intent.getStringArrayListExtra( |
| 255 | ConnectivityManager.EXTRA_ACTIVE_TETHER); |
| 256 | updateTetherState(available, active); |
| 257 | |
| 258 | } |
| 259 | },new IntentFilter(ConnectivityManager.ACTION_TETHER_STATE_CHANGED)); |
| Irfan Sheriff | 7b00978 | 2010-03-11 16:37:45 -0800 | [diff] [blame] | 260 | } |
| Irfan Sheriff | 5321aef | 2010-02-12 12:35:59 -0800 | [diff] [blame] | 261 | |
| Irfan Sheriff | 7b00978 | 2010-03-11 16:37:45 -0800 | [diff] [blame] | 262 | /** |
| 263 | * Check if Wi-Fi needs to be enabled and start |
| 264 | * if needed |
| 265 | */ |
| 266 | public void startWifi() { |
| 267 | boolean wifiEnabled = getPersistedWifiEnabled(); |
| Irfan Sheriff | 7b00978 | 2010-03-11 16:37:45 -0800 | [diff] [blame] | 268 | Slog.i(TAG, "WifiService starting up with Wi-Fi " + |
| 269 | (wifiEnabled ? "enabled" : "disabled")); |
| Dianne Hackborn | 617f877 | 2009-03-31 15:04:46 -0700 | [diff] [blame] | 270 | setWifiEnabledBlocking(wifiEnabled, false, Process.myUid()); |
| Irfan Sheriff | 5321aef | 2010-02-12 12:35:59 -0800 | [diff] [blame] | 271 | } |
| 272 | |
| 273 | private void updateTetherState(ArrayList<String> available, ArrayList<String> tethered) { |
| 274 | |
| 275 | boolean wifiTethered = false; |
| 276 | boolean wifiAvailable = false; |
| 277 | |
| 278 | IBinder b = ServiceManager.getService(Context.NETWORKMANAGEMENT_SERVICE); |
| 279 | INetworkManagementService service = INetworkManagementService.Stub.asInterface(b); |
| 280 | |
| 281 | mCm = (ConnectivityManager)mContext.getSystemService(Context.CONNECTIVITY_SERVICE); |
| 282 | mWifiRegexs = mCm.getTetherableWifiRegexs(); |
| 283 | |
| 284 | for (String intf : available) { |
| 285 | for (String regex : mWifiRegexs) { |
| 286 | if (intf.matches(regex)) { |
| 287 | |
| 288 | InterfaceConfiguration ifcg = null; |
| 289 | try { |
| 290 | ifcg = service.getInterfaceConfig(intf); |
| 291 | if (ifcg != null) { |
| Robert Greenwalt | bfb7bfa | 2010-03-24 16:03:21 -0700 | [diff] [blame^] | 292 | /* IP/netmask: 192.168.43.1/255.255.255.0 */ |
| 293 | ifcg.ipAddr = (192 << 24) + (168 << 16) + (43 << 8) + 1; |
| Irfan Sheriff | 5321aef | 2010-02-12 12:35:59 -0800 | [diff] [blame] | 294 | ifcg.netmask = (255 << 24) + (255 << 16) + (255 << 8) + 0; |
| 295 | ifcg.interfaceFlags = "up"; |
| 296 | |
| 297 | service.setInterfaceConfig(intf, ifcg); |
| 298 | } |
| 299 | } catch (Exception e) { |
| Irfan Sheriff | 5321aef | 2010-02-12 12:35:59 -0800 | [diff] [blame] | 300 | Slog.e(TAG, "Error configuring interface " + intf + ", :" + e); |
| Irfan Sheriff | 1a54301 | 2010-03-17 19:46:32 -0700 | [diff] [blame] | 301 | try { |
| 302 | nwService.stopAccessPoint(); |
| 303 | } catch (Exception ee) { |
| 304 | Slog.e(TAG, "Could not stop AP, :" + ee); |
| 305 | } |
| Irfan Sheriff | 9ab518ad | 2010-03-12 15:48:17 -0800 | [diff] [blame] | 306 | setWifiApEnabledState(WIFI_AP_STATE_FAILED, 0, DriverAction.DRIVER_UNLOAD); |
| Irfan Sheriff | 5321aef | 2010-02-12 12:35:59 -0800 | [diff] [blame] | 307 | return; |
| 308 | } |
| 309 | |
| Irfan Sheriff | 9ab518ad | 2010-03-12 15:48:17 -0800 | [diff] [blame] | 310 | if(mCm.tether(intf) != ConnectivityManager.TETHER_ERROR_NO_ERROR) { |
| Irfan Sheriff | 5321aef | 2010-02-12 12:35:59 -0800 | [diff] [blame] | 311 | Slog.e(TAG, "Error tethering "+intf); |
| 312 | } |
| 313 | break; |
| 314 | } |
| 315 | } |
| 316 | } |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 317 | } |
| 318 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 319 | private boolean getPersistedWifiEnabled() { |
| 320 | final ContentResolver cr = mContext.getContentResolver(); |
| 321 | try { |
| 322 | return Settings.Secure.getInt(cr, Settings.Secure.WIFI_ON) == 1; |
| 323 | } catch (Settings.SettingNotFoundException e) { |
| 324 | Settings.Secure.putInt(cr, Settings.Secure.WIFI_ON, 0); |
| 325 | return false; |
| 326 | } |
| 327 | } |
| 328 | |
| 329 | private void persistWifiEnabled(boolean enabled) { |
| 330 | final ContentResolver cr = mContext.getContentResolver(); |
| 331 | Settings.Secure.putInt(cr, Settings.Secure.WIFI_ON, enabled ? 1 : 0); |
| 332 | } |
| 333 | |
| 334 | NetworkStateTracker getNetworkStateTracker() { |
| 335 | return mWifiStateTracker; |
| 336 | } |
| 337 | |
| 338 | /** |
| 339 | * see {@link android.net.wifi.WifiManager#pingSupplicant()} |
| 340 | * @return {@code true} if the operation succeeds |
| 341 | */ |
| 342 | public boolean pingSupplicant() { |
| 343 | enforceChangePermission(); |
| Irfan Sheriff | a8fbe1f | 2010-03-09 09:13:58 -0800 | [diff] [blame] | 344 | |
| 345 | return mWifiStateTracker.ping(); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 346 | } |
| 347 | |
| 348 | /** |
| 349 | * see {@link android.net.wifi.WifiManager#startScan()} |
| 350 | * @return {@code true} if the operation succeeds |
| 351 | */ |
| Mike Lockwood | a5ec95c | 2009-07-08 17:11:17 -0400 | [diff] [blame] | 352 | public boolean startScan(boolean forceActive) { |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 353 | enforceChangePermission(); |
| Irfan Sheriff | a8fbe1f | 2010-03-09 09:13:58 -0800 | [diff] [blame] | 354 | |
| 355 | switch (mWifiStateTracker.getSupplicantState()) { |
| 356 | case DISCONNECTED: |
| 357 | case INACTIVE: |
| 358 | case SCANNING: |
| 359 | case DORMANT: |
| 360 | break; |
| 361 | default: |
| 362 | mWifiStateTracker.setScanResultHandling( |
| 363 | WifiStateTracker.SUPPL_SCAN_HANDLING_LIST_ONLY); |
| 364 | break; |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 365 | } |
| Irfan Sheriff | a8fbe1f | 2010-03-09 09:13:58 -0800 | [diff] [blame] | 366 | return mWifiStateTracker.scan(forceActive); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 367 | } |
| 368 | |
| 369 | /** |
| 370 | * see {@link android.net.wifi.WifiManager#setWifiEnabled(boolean)} |
| 371 | * @param enable {@code true} to enable, {@code false} to disable. |
| 372 | * @return {@code true} if the enable/disable operation was |
| 373 | * started or is already in the queue. |
| 374 | */ |
| 375 | public boolean setWifiEnabled(boolean enable) { |
| 376 | enforceChangePermission(); |
| 377 | if (mWifiHandler == null) return false; |
| 378 | |
| 379 | synchronized (mWifiHandler) { |
| Robert Greenwalt | a99f461 | 2009-09-19 18:14:32 -0700 | [diff] [blame] | 380 | // caller may not have WAKE_LOCK permission - it's not required here |
| 381 | long ident = Binder.clearCallingIdentity(); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 382 | sWakeLock.acquire(); |
| Robert Greenwalt | a99f461 | 2009-09-19 18:14:32 -0700 | [diff] [blame] | 383 | Binder.restoreCallingIdentity(ident); |
| 384 | |
| Dianne Hackborn | 617f877 | 2009-03-31 15:04:46 -0700 | [diff] [blame] | 385 | mLastEnableUid = Binder.getCallingUid(); |
| Mike Lockwood | bd5ddf0 | 2009-07-29 21:37:14 -0700 | [diff] [blame] | 386 | // set a flag if the user is enabling Wifi while in airplane mode |
| 387 | mAirplaneModeOverwridden = (enable && isAirplaneModeOn() && isAirplaneToggleable()); |
| Dianne Hackborn | 617f877 | 2009-03-31 15:04:46 -0700 | [diff] [blame] | 388 | sendEnableMessage(enable, true, Binder.getCallingUid()); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 389 | } |
| 390 | |
| 391 | return true; |
| 392 | } |
| 393 | |
| 394 | /** |
| 395 | * Enables/disables Wi-Fi synchronously. |
| 396 | * @param enable {@code true} to turn Wi-Fi on, {@code false} to turn it off. |
| 397 | * @param persist {@code true} if the setting should be persisted. |
| Dianne Hackborn | 617f877 | 2009-03-31 15:04:46 -0700 | [diff] [blame] | 398 | * @param uid The UID of the process making the request. |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 399 | * @return {@code true} if the operation succeeds (or if the existing state |
| 400 | * is the same as the requested state) |
| 401 | */ |
| Dianne Hackborn | 617f877 | 2009-03-31 15:04:46 -0700 | [diff] [blame] | 402 | private boolean setWifiEnabledBlocking(boolean enable, boolean persist, int uid) { |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 403 | final int eventualWifiState = enable ? WIFI_STATE_ENABLED : WIFI_STATE_DISABLED; |
| Irfan Sheriff | 0f34406009 | 2010-03-10 10:05:51 -0800 | [diff] [blame] | 404 | final int wifiState = mWifiStateTracker.getWifiState(); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 405 | |
| Irfan Sheriff | 0f34406009 | 2010-03-10 10:05:51 -0800 | [diff] [blame] | 406 | if (wifiState == eventualWifiState) { |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 407 | return true; |
| 408 | } |
| Mike Lockwood | bd5ddf0 | 2009-07-29 21:37:14 -0700 | [diff] [blame] | 409 | if (enable && isAirplaneModeOn() && !mAirplaneModeOverwridden) { |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 410 | return false; |
| 411 | } |
| 412 | |
| Irfan Sheriff | cd77037 | 2010-01-08 09:36:04 -0800 | [diff] [blame] | 413 | /** |
| 414 | * Multiple calls to unregisterReceiver() cause exception and a system crash. |
| 415 | * This can happen if a supplicant is lost (or firmware crash occurs) and user indicates |
| 416 | * disable wifi at the same time. |
| 417 | * Avoid doing a disable when the current Wifi state is UNKNOWN |
| 418 | * TODO: Handle driver load fail and supplicant lost as seperate states |
| 419 | */ |
| Irfan Sheriff | 0f34406009 | 2010-03-10 10:05:51 -0800 | [diff] [blame] | 420 | if ((wifiState == WIFI_STATE_UNKNOWN) && !enable) { |
| Irfan Sheriff | cd77037 | 2010-01-08 09:36:04 -0800 | [diff] [blame] | 421 | return false; |
| 422 | } |
| 423 | |
| Irfan Sheriff | f91444c | 2010-03-24 12:11:00 -0700 | [diff] [blame] | 424 | /** |
| 425 | * Fail Wifi if AP is enabled |
| 426 | * TODO: Deprecate WIFI_STATE_UNKNOWN and rename it |
| 427 | * WIFI_STATE_FAILED |
| 428 | */ |
| Irfan Sheriff | 5321aef | 2010-02-12 12:35:59 -0800 | [diff] [blame] | 429 | if ((mWifiApState == WIFI_AP_STATE_ENABLED) && enable) { |
| Irfan Sheriff | f91444c | 2010-03-24 12:11:00 -0700 | [diff] [blame] | 430 | setWifiEnabledState(WIFI_STATE_UNKNOWN, uid); |
| 431 | return false; |
| Irfan Sheriff | 5321aef | 2010-02-12 12:35:59 -0800 | [diff] [blame] | 432 | } |
| 433 | |
| Irfan Sheriff | f91444c | 2010-03-24 12:11:00 -0700 | [diff] [blame] | 434 | setWifiEnabledState(enable ? WIFI_STATE_ENABLING : WIFI_STATE_DISABLING, uid); |
| 435 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 436 | if (enable) { |
| Irfan Sheriff | a8fbe1f | 2010-03-09 09:13:58 -0800 | [diff] [blame] | 437 | if (!mWifiStateTracker.loadDriver()) { |
| 438 | Slog.e(TAG, "Failed to load Wi-Fi driver."); |
| 439 | setWifiEnabledState(WIFI_STATE_UNKNOWN, uid); |
| 440 | return false; |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 441 | } |
| Irfan Sheriff | a8fbe1f | 2010-03-09 09:13:58 -0800 | [diff] [blame] | 442 | if (!mWifiStateTracker.startSupplicant()) { |
| 443 | mWifiStateTracker.unloadDriver(); |
| 444 | Slog.e(TAG, "Failed to start supplicant daemon."); |
| 445 | setWifiEnabledState(WIFI_STATE_UNKNOWN, uid); |
| 446 | return false; |
| 447 | } |
| 448 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 449 | registerForBroadcasts(); |
| 450 | mWifiStateTracker.startEventLoop(); |
| Irfan Sheriff | 7b00978 | 2010-03-11 16:37:45 -0800 | [diff] [blame] | 451 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 452 | } else { |
| 453 | |
| 454 | mContext.unregisterReceiver(mReceiver); |
| 455 | // Remove notification (it will no-op if it isn't visible) |
| 456 | mWifiStateTracker.setNotificationVisible(false, 0, false, 0); |
| 457 | |
| 458 | boolean failedToStopSupplicantOrUnloadDriver = false; |
| Irfan Sheriff | a8fbe1f | 2010-03-09 09:13:58 -0800 | [diff] [blame] | 459 | |
| 460 | if (!mWifiStateTracker.stopSupplicant()) { |
| 461 | Slog.e(TAG, "Failed to stop supplicant daemon."); |
| 462 | setWifiEnabledState(WIFI_STATE_UNKNOWN, uid); |
| 463 | failedToStopSupplicantOrUnloadDriver = true; |
| 464 | } |
| 465 | |
| 466 | /** |
| 467 | * Reset connections and disable interface |
| 468 | * before we unload the driver |
| 469 | */ |
| 470 | mWifiStateTracker.resetConnections(true); |
| 471 | |
| 472 | if (!mWifiStateTracker.unloadDriver()) { |
| 473 | Slog.e(TAG, "Failed to unload Wi-Fi driver."); |
| 474 | if (!failedToStopSupplicantOrUnloadDriver) { |
| Dianne Hackborn | 617f877 | 2009-03-31 15:04:46 -0700 | [diff] [blame] | 475 | setWifiEnabledState(WIFI_STATE_UNKNOWN, uid); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 476 | failedToStopSupplicantOrUnloadDriver = true; |
| 477 | } |
| 478 | } |
| Irfan Sheriff | a8fbe1f | 2010-03-09 09:13:58 -0800 | [diff] [blame] | 479 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 480 | if (failedToStopSupplicantOrUnloadDriver) { |
| 481 | return false; |
| 482 | } |
| 483 | } |
| 484 | |
| 485 | // Success! |
| 486 | |
| 487 | if (persist) { |
| 488 | persistWifiEnabled(enable); |
| 489 | } |
| Dianne Hackborn | 617f877 | 2009-03-31 15:04:46 -0700 | [diff] [blame] | 490 | setWifiEnabledState(eventualWifiState, uid); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 491 | return true; |
| 492 | } |
| 493 | |
| Dianne Hackborn | 617f877 | 2009-03-31 15:04:46 -0700 | [diff] [blame] | 494 | private void setWifiEnabledState(int wifiState, int uid) { |
| Irfan Sheriff | 0f34406009 | 2010-03-10 10:05:51 -0800 | [diff] [blame] | 495 | final int previousWifiState = mWifiStateTracker.getWifiState(); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 496 | |
| The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 497 | long ident = Binder.clearCallingIdentity(); |
| 498 | try { |
| 499 | if (wifiState == WIFI_STATE_ENABLED) { |
| Dianne Hackborn | 617f877 | 2009-03-31 15:04:46 -0700 | [diff] [blame] | 500 | mBatteryStats.noteWifiOn(uid); |
| The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 501 | } else if (wifiState == WIFI_STATE_DISABLED) { |
| Dianne Hackborn | 617f877 | 2009-03-31 15:04:46 -0700 | [diff] [blame] | 502 | mBatteryStats.noteWifiOff(uid); |
| The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 503 | } |
| 504 | } catch (RemoteException e) { |
| 505 | } finally { |
| 506 | Binder.restoreCallingIdentity(ident); |
| 507 | } |
| Jaikumar Ganesh | 084c665 | 2009-12-07 10:58:18 -0800 | [diff] [blame] | 508 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 509 | // Update state |
| Irfan Sheriff | 0f34406009 | 2010-03-10 10:05:51 -0800 | [diff] [blame] | 510 | mWifiStateTracker.setWifiState(wifiState); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 511 | |
| 512 | // Broadcast |
| 513 | final Intent intent = new Intent(WifiManager.WIFI_STATE_CHANGED_ACTION); |
| 514 | intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY_BEFORE_BOOT); |
| 515 | intent.putExtra(WifiManager.EXTRA_WIFI_STATE, wifiState); |
| 516 | intent.putExtra(WifiManager.EXTRA_PREVIOUS_WIFI_STATE, previousWifiState); |
| 517 | mContext.sendStickyBroadcast(intent); |
| 518 | } |
| 519 | |
| 520 | private void enforceAccessPermission() { |
| 521 | mContext.enforceCallingOrSelfPermission(android.Manifest.permission.ACCESS_WIFI_STATE, |
| 522 | "WifiService"); |
| 523 | } |
| 524 | |
| 525 | private void enforceChangePermission() { |
| 526 | mContext.enforceCallingOrSelfPermission(android.Manifest.permission.CHANGE_WIFI_STATE, |
| 527 | "WifiService"); |
| 528 | |
| 529 | } |
| 530 | |
| Robert Greenwalt | fc1b15c | 2009-05-22 15:09:51 -0700 | [diff] [blame] | 531 | private void enforceMulticastChangePermission() { |
| 532 | mContext.enforceCallingOrSelfPermission( |
| 533 | android.Manifest.permission.CHANGE_WIFI_MULTICAST_STATE, |
| 534 | "WifiService"); |
| 535 | } |
| 536 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 537 | /** |
| 538 | * see {@link WifiManager#getWifiState()} |
| 539 | * @return One of {@link WifiManager#WIFI_STATE_DISABLED}, |
| 540 | * {@link WifiManager#WIFI_STATE_DISABLING}, |
| 541 | * {@link WifiManager#WIFI_STATE_ENABLED}, |
| 542 | * {@link WifiManager#WIFI_STATE_ENABLING}, |
| 543 | * {@link WifiManager#WIFI_STATE_UNKNOWN} |
| 544 | */ |
| 545 | public int getWifiEnabledState() { |
| 546 | enforceAccessPermission(); |
| Irfan Sheriff | 0f34406009 | 2010-03-10 10:05:51 -0800 | [diff] [blame] | 547 | return mWifiStateTracker.getWifiState(); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 548 | } |
| 549 | |
| 550 | /** |
| 551 | * see {@link android.net.wifi.WifiManager#disconnect()} |
| 552 | * @return {@code true} if the operation succeeds |
| 553 | */ |
| 554 | public boolean disconnect() { |
| 555 | enforceChangePermission(); |
| Irfan Sheriff | a8fbe1f | 2010-03-09 09:13:58 -0800 | [diff] [blame] | 556 | |
| 557 | return mWifiStateTracker.disconnect(); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 558 | } |
| 559 | |
| 560 | /** |
| 561 | * see {@link android.net.wifi.WifiManager#reconnect()} |
| 562 | * @return {@code true} if the operation succeeds |
| 563 | */ |
| 564 | public boolean reconnect() { |
| 565 | enforceChangePermission(); |
| Irfan Sheriff | a8fbe1f | 2010-03-09 09:13:58 -0800 | [diff] [blame] | 566 | |
| 567 | return mWifiStateTracker.reconnectCommand(); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 568 | } |
| 569 | |
| 570 | /** |
| 571 | * see {@link android.net.wifi.WifiManager#reassociate()} |
| 572 | * @return {@code true} if the operation succeeds |
| 573 | */ |
| 574 | public boolean reassociate() { |
| 575 | enforceChangePermission(); |
| Irfan Sheriff | a8fbe1f | 2010-03-09 09:13:58 -0800 | [diff] [blame] | 576 | |
| 577 | return mWifiStateTracker.reassociate(); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 578 | } |
| 579 | |
| Irfan Sheriff | 5321aef | 2010-02-12 12:35:59 -0800 | [diff] [blame] | 580 | /** |
| Irfan Sheriff | c2f54c2 | 2010-03-18 14:02:22 -0700 | [diff] [blame] | 581 | * see {@link android.net.wifi.WifiManager#setWifiApEnabled(WifiConfiguration, boolean)} |
| Irfan Sheriff | 5321aef | 2010-02-12 12:35:59 -0800 | [diff] [blame] | 582 | * @param wifiConfig SSID, security and channel details as |
| 583 | * part of WifiConfiguration |
| Irfan Sheriff | c2f54c2 | 2010-03-18 14:02:22 -0700 | [diff] [blame] | 584 | * @param enabled, true to enable and false to disable |
| Irfan Sheriff | 5321aef | 2010-02-12 12:35:59 -0800 | [diff] [blame] | 585 | * @return {@code true} if the start operation was |
| 586 | * started or is already in the queue. |
| 587 | */ |
| 588 | public boolean setWifiApEnabled(WifiConfiguration wifiConfig, boolean enabled) { |
| 589 | enforceChangePermission(); |
| 590 | if (mWifiHandler == null) return false; |
| 591 | |
| 592 | synchronized (mWifiHandler) { |
| 593 | |
| 594 | long ident = Binder.clearCallingIdentity(); |
| 595 | sWakeLock.acquire(); |
| 596 | Binder.restoreCallingIdentity(ident); |
| 597 | |
| 598 | mLastEnableUid = Binder.getCallingUid(); |
| 599 | |
| 600 | sendAccessPointMessage(enabled, wifiConfig, Binder.getCallingUid()); |
| 601 | } |
| 602 | |
| 603 | return true; |
| 604 | } |
| 605 | |
| Irfan Sheriff | 9ab518ad | 2010-03-12 15:48:17 -0800 | [diff] [blame] | 606 | public WifiConfiguration getWifiApConfiguration() { |
| 607 | final ContentResolver cr = mContext.getContentResolver(); |
| 608 | WifiConfiguration wifiConfig = new WifiConfiguration(); |
| 609 | int authType; |
| 610 | try { |
| 611 | wifiConfig.SSID = Settings.Secure.getString(cr, Settings.Secure.WIFI_AP_SSID); |
| 612 | if (wifiConfig.SSID == null) |
| 613 | return null; |
| 614 | authType = Settings.Secure.getInt(cr, Settings.Secure.WIFI_AP_SECURITY); |
| 615 | wifiConfig.allowedKeyManagement.set(authType); |
| 616 | wifiConfig.preSharedKey = Settings.Secure.getString(cr, Settings.Secure.WIFI_AP_PASSWD); |
| 617 | return wifiConfig; |
| 618 | } catch (Settings.SettingNotFoundException e) { |
| 619 | Slog.e(TAG,"AP settings not found, returning"); |
| 620 | return null; |
| 621 | } |
| 622 | } |
| 623 | |
| 624 | private void persistApConfiguration(WifiConfiguration wifiConfig) { |
| 625 | final ContentResolver cr = mContext.getContentResolver(); |
| 626 | boolean isWpa; |
| 627 | if (wifiConfig == null) |
| 628 | return; |
| 629 | Settings.Secure.putString(cr, Settings.Secure.WIFI_AP_SSID, wifiConfig.SSID); |
| 630 | isWpa = wifiConfig.allowedKeyManagement.get(KeyMgmt.WPA_PSK); |
| 631 | Settings.Secure.putInt(cr, |
| 632 | Settings.Secure.WIFI_AP_SECURITY, |
| 633 | isWpa ? KeyMgmt.WPA_PSK : KeyMgmt.NONE); |
| 634 | if (isWpa) |
| 635 | Settings.Secure.putString(cr, Settings.Secure.WIFI_AP_PASSWD, wifiConfig.preSharedKey); |
| 636 | } |
| 637 | |
| Irfan Sheriff | 5321aef | 2010-02-12 12:35:59 -0800 | [diff] [blame] | 638 | /** |
| 639 | * Enables/disables Wi-Fi AP synchronously. The driver is loaded |
| 640 | * and soft access point configured as a single operation. |
| 641 | * @param enable {@code true} to turn Wi-Fi on, {@code false} to turn it off. |
| Irfan Sheriff | 5321aef | 2010-02-12 12:35:59 -0800 | [diff] [blame] | 642 | * @param uid The UID of the process making the request. |
| Irfan Sheriff | 9ab518ad | 2010-03-12 15:48:17 -0800 | [diff] [blame] | 643 | * @param wifiConfig The WifiConfiguration for AP |
| Irfan Sheriff | 5321aef | 2010-02-12 12:35:59 -0800 | [diff] [blame] | 644 | * @return {@code true} if the operation succeeds (or if the existing state |
| 645 | * is the same as the requested state) |
| 646 | */ |
| Irfan Sheriff | 5321aef | 2010-02-12 12:35:59 -0800 | [diff] [blame] | 647 | private boolean setWifiApEnabledBlocking(boolean enable, |
| Irfan Sheriff | 9ab518ad | 2010-03-12 15:48:17 -0800 | [diff] [blame] | 648 | int uid, WifiConfiguration wifiConfig) { |
| Irfan Sheriff | 5321aef | 2010-02-12 12:35:59 -0800 | [diff] [blame] | 649 | final int eventualWifiApState = enable ? WIFI_AP_STATE_ENABLED : WIFI_AP_STATE_DISABLED; |
| 650 | |
| 651 | if (mWifiApState == eventualWifiApState) { |
| Irfan Sheriff | 9ab518ad | 2010-03-12 15:48:17 -0800 | [diff] [blame] | 652 | /* Configuration changed on a running access point */ |
| 653 | if(enable && (wifiConfig != null)) { |
| 654 | try { |
| 655 | persistApConfiguration(wifiConfig); |
| Irfan Sheriff | c2f54c2 | 2010-03-18 14:02:22 -0700 | [diff] [blame] | 656 | nwService.setAccessPoint(wifiConfig, mWifiStateTracker.getInterfaceName(), |
| 657 | SOFTAP_IFACE); |
| Irfan Sheriff | 9ab518ad | 2010-03-12 15:48:17 -0800 | [diff] [blame] | 658 | return true; |
| 659 | } catch(Exception e) { |
| 660 | Slog.e(TAG, "Exception in nwService during AP restart"); |
| Irfan Sheriff | c2f54c2 | 2010-03-18 14:02:22 -0700 | [diff] [blame] | 661 | try { |
| 662 | nwService.stopAccessPoint(); |
| 663 | } catch (Exception ee) { |
| 664 | Slog.e(TAG, "Could not stop AP, :" + ee); |
| 665 | } |
| Irfan Sheriff | 9ab518ad | 2010-03-12 15:48:17 -0800 | [diff] [blame] | 666 | setWifiApEnabledState(WIFI_AP_STATE_FAILED, uid, DriverAction.DRIVER_UNLOAD); |
| 667 | return false; |
| 668 | } |
| 669 | } else { |
| 670 | return true; |
| 671 | } |
| Irfan Sheriff | 5321aef | 2010-02-12 12:35:59 -0800 | [diff] [blame] | 672 | } |
| 673 | |
| Irfan Sheriff | f91444c | 2010-03-24 12:11:00 -0700 | [diff] [blame] | 674 | /** |
| 675 | * Fail AP if Wifi is enabled |
| 676 | */ |
| 677 | if ((mWifiStateTracker.getWifiState() == WIFI_STATE_ENABLED) && enable) { |
| 678 | setWifiApEnabledState(WIFI_AP_STATE_FAILED, uid, DriverAction.NO_DRIVER_UNLOAD); |
| 679 | return false; |
| 680 | } |
| 681 | |
| Irfan Sheriff | 9ab518ad | 2010-03-12 15:48:17 -0800 | [diff] [blame] | 682 | setWifiApEnabledState(enable ? WIFI_AP_STATE_ENABLING : |
| 683 | WIFI_AP_STATE_DISABLING, uid, DriverAction.NO_DRIVER_UNLOAD); |
| Irfan Sheriff | 5321aef | 2010-02-12 12:35:59 -0800 | [diff] [blame] | 684 | |
| 685 | if (enable) { |
| Irfan Sheriff | 9ab518ad | 2010-03-12 15:48:17 -0800 | [diff] [blame] | 686 | |
| Irfan Sheriff | 9ab518ad | 2010-03-12 15:48:17 -0800 | [diff] [blame] | 687 | /* Use default config if there is no existing config */ |
| 688 | if (wifiConfig == null && ((wifiConfig = getWifiApConfiguration()) == null)) { |
| 689 | wifiConfig = new WifiConfiguration(); |
| 690 | wifiConfig.SSID = mContext.getString(R.string.wifi_tether_configure_ssid_default); |
| 691 | wifiConfig.allowedKeyManagement.set(KeyMgmt.NONE); |
| 692 | } |
| 693 | persistApConfiguration(wifiConfig); |
| 694 | |
| Irfan Sheriff | a8fbe1f | 2010-03-09 09:13:58 -0800 | [diff] [blame] | 695 | if (!mWifiStateTracker.loadDriver()) { |
| 696 | Slog.e(TAG, "Failed to load Wi-Fi driver for AP mode"); |
| Irfan Sheriff | 9ab518ad | 2010-03-12 15:48:17 -0800 | [diff] [blame] | 697 | setWifiApEnabledState(WIFI_AP_STATE_FAILED, uid, DriverAction.NO_DRIVER_UNLOAD); |
| Irfan Sheriff | a8fbe1f | 2010-03-09 09:13:58 -0800 | [diff] [blame] | 698 | return false; |
| Irfan Sheriff | 5321aef | 2010-02-12 12:35:59 -0800 | [diff] [blame] | 699 | } |
| 700 | |
| 701 | try { |
| Irfan Sheriff | c2f54c2 | 2010-03-18 14:02:22 -0700 | [diff] [blame] | 702 | nwService.startAccessPoint(wifiConfig, mWifiStateTracker.getInterfaceName(), |
| 703 | SOFTAP_IFACE); |
| Irfan Sheriff | 5321aef | 2010-02-12 12:35:59 -0800 | [diff] [blame] | 704 | } catch(Exception e) { |
| 705 | Slog.e(TAG, "Exception in startAccessPoint()"); |
| Irfan Sheriff | 9ab518ad | 2010-03-12 15:48:17 -0800 | [diff] [blame] | 706 | setWifiApEnabledState(WIFI_AP_STATE_FAILED, uid, DriverAction.DRIVER_UNLOAD); |
| 707 | return false; |
| Irfan Sheriff | 5321aef | 2010-02-12 12:35:59 -0800 | [diff] [blame] | 708 | } |
| 709 | |
| 710 | } else { |
| 711 | |
| 712 | try { |
| 713 | nwService.stopAccessPoint(); |
| 714 | } catch(Exception e) { |
| 715 | Slog.e(TAG, "Exception in stopAccessPoint()"); |
| Irfan Sheriff | 9ab518ad | 2010-03-12 15:48:17 -0800 | [diff] [blame] | 716 | setWifiApEnabledState(WIFI_AP_STATE_FAILED, uid, DriverAction.DRIVER_UNLOAD); |
| 717 | return false; |
| Irfan Sheriff | 5321aef | 2010-02-12 12:35:59 -0800 | [diff] [blame] | 718 | } |
| 719 | |
| Irfan Sheriff | a8fbe1f | 2010-03-09 09:13:58 -0800 | [diff] [blame] | 720 | if (!mWifiStateTracker.unloadDriver()) { |
| 721 | Slog.e(TAG, "Failed to unload Wi-Fi driver for AP mode"); |
| Irfan Sheriff | 9ab518ad | 2010-03-12 15:48:17 -0800 | [diff] [blame] | 722 | setWifiApEnabledState(WIFI_AP_STATE_FAILED, uid, DriverAction.NO_DRIVER_UNLOAD); |
| Irfan Sheriff | a8fbe1f | 2010-03-09 09:13:58 -0800 | [diff] [blame] | 723 | return false; |
| Irfan Sheriff | 5321aef | 2010-02-12 12:35:59 -0800 | [diff] [blame] | 724 | } |
| 725 | } |
| 726 | |
| Irfan Sheriff | 9ab518ad | 2010-03-12 15:48:17 -0800 | [diff] [blame] | 727 | setWifiApEnabledState(eventualWifiApState, uid, DriverAction.NO_DRIVER_UNLOAD); |
| Irfan Sheriff | 5321aef | 2010-02-12 12:35:59 -0800 | [diff] [blame] | 728 | return true; |
| 729 | } |
| 730 | |
| 731 | /** |
| 732 | * see {@link WifiManager#getWifiApState()} |
| 733 | * @return One of {@link WifiManager#WIFI_AP_STATE_DISABLED}, |
| 734 | * {@link WifiManager#WIFI_AP_STATE_DISABLING}, |
| 735 | * {@link WifiManager#WIFI_AP_STATE_ENABLED}, |
| 736 | * {@link WifiManager#WIFI_AP_STATE_ENABLING}, |
| 737 | * {@link WifiManager#WIFI_AP_STATE_FAILED} |
| 738 | */ |
| 739 | public int getWifiApEnabledState() { |
| 740 | enforceAccessPermission(); |
| 741 | return mWifiApState; |
| 742 | } |
| 743 | |
| Irfan Sheriff | 9ab518ad | 2010-03-12 15:48:17 -0800 | [diff] [blame] | 744 | private void setWifiApEnabledState(int wifiAPState, int uid, DriverAction flag) { |
| Irfan Sheriff | 5321aef | 2010-02-12 12:35:59 -0800 | [diff] [blame] | 745 | final int previousWifiApState = mWifiApState; |
| 746 | |
| Irfan Sheriff | 9ab518ad | 2010-03-12 15:48:17 -0800 | [diff] [blame] | 747 | /** |
| 748 | * Unload the driver if going to a failed state |
| 749 | */ |
| 750 | if ((mWifiApState == WIFI_AP_STATE_FAILED) && (flag == DriverAction.DRIVER_UNLOAD)) { |
| 751 | mWifiStateTracker.unloadDriver(); |
| 752 | } |
| 753 | |
| Irfan Sheriff | 5321aef | 2010-02-12 12:35:59 -0800 | [diff] [blame] | 754 | long ident = Binder.clearCallingIdentity(); |
| 755 | try { |
| 756 | if (wifiAPState == WIFI_AP_STATE_ENABLED) { |
| 757 | mBatteryStats.noteWifiOn(uid); |
| 758 | } else if (wifiAPState == WIFI_AP_STATE_DISABLED) { |
| 759 | mBatteryStats.noteWifiOff(uid); |
| 760 | } |
| 761 | } catch (RemoteException e) { |
| 762 | } finally { |
| 763 | Binder.restoreCallingIdentity(ident); |
| 764 | } |
| 765 | |
| 766 | // Update state |
| 767 | mWifiApState = wifiAPState; |
| 768 | |
| 769 | // Broadcast |
| 770 | final Intent intent = new Intent(WifiManager.WIFI_AP_STATE_CHANGED_ACTION); |
| 771 | intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY_BEFORE_BOOT); |
| 772 | intent.putExtra(WifiManager.EXTRA_WIFI_AP_STATE, wifiAPState); |
| 773 | intent.putExtra(WifiManager.EXTRA_PREVIOUS_WIFI_AP_STATE, previousWifiApState); |
| 774 | mContext.sendStickyBroadcast(intent); |
| 775 | } |
| 776 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 777 | /** |
| 778 | * see {@link android.net.wifi.WifiManager#getConfiguredNetworks()} |
| 779 | * @return the list of configured networks |
| 780 | */ |
| 781 | public List<WifiConfiguration> getConfiguredNetworks() { |
| 782 | enforceAccessPermission(); |
| 783 | String listStr; |
| Irfan Sheriff | a8fbe1f | 2010-03-09 09:13:58 -0800 | [diff] [blame] | 784 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 785 | /* |
| 786 | * We don't cache the list, because we want to allow |
| 787 | * for the possibility that the configuration file |
| 788 | * has been modified through some external means, |
| 789 | * such as the wpa_cli command line program. |
| 790 | */ |
| Irfan Sheriff | a8fbe1f | 2010-03-09 09:13:58 -0800 | [diff] [blame] | 791 | listStr = mWifiStateTracker.listNetworks(); |
| 792 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 793 | List<WifiConfiguration> networks = |
| 794 | new ArrayList<WifiConfiguration>(); |
| 795 | if (listStr == null) |
| 796 | return networks; |
| 797 | |
| 798 | String[] lines = listStr.split("\n"); |
| 799 | // Skip the first line, which is a header |
| 800 | for (int i = 1; i < lines.length; i++) { |
| 801 | String[] result = lines[i].split("\t"); |
| 802 | // network-id | ssid | bssid | flags |
| 803 | WifiConfiguration config = new WifiConfiguration(); |
| 804 | try { |
| 805 | config.networkId = Integer.parseInt(result[0]); |
| 806 | } catch(NumberFormatException e) { |
| 807 | continue; |
| 808 | } |
| 809 | if (result.length > 3) { |
| 810 | if (result[3].indexOf("[CURRENT]") != -1) |
| 811 | config.status = WifiConfiguration.Status.CURRENT; |
| 812 | else if (result[3].indexOf("[DISABLED]") != -1) |
| 813 | config.status = WifiConfiguration.Status.DISABLED; |
| 814 | else |
| 815 | config.status = WifiConfiguration.Status.ENABLED; |
| Irfan Sheriff | a8fbe1f | 2010-03-09 09:13:58 -0800 | [diff] [blame] | 816 | } else { |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 817 | config.status = WifiConfiguration.Status.ENABLED; |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 818 | } |
| Irfan Sheriff | a8fbe1f | 2010-03-09 09:13:58 -0800 | [diff] [blame] | 819 | readNetworkVariables(config); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 820 | networks.add(config); |
| 821 | } |
| 822 | |
| 823 | return networks; |
| 824 | } |
| 825 | |
| 826 | /** |
| 827 | * Read the variables from the supplicant daemon that are needed to |
| 828 | * fill in the WifiConfiguration object. |
| 829 | * <p/> |
| 830 | * The caller must hold the synchronization monitor. |
| 831 | * @param config the {@link WifiConfiguration} object to be filled in. |
| 832 | */ |
| Irfan Sheriff | a8fbe1f | 2010-03-09 09:13:58 -0800 | [diff] [blame] | 833 | private void readNetworkVariables(WifiConfiguration config) { |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 834 | |
| 835 | int netId = config.networkId; |
| 836 | if (netId < 0) |
| 837 | return; |
| 838 | |
| 839 | /* |
| 840 | * TODO: maybe should have a native method that takes an array of |
| 841 | * variable names and returns an array of values. But we'd still |
| 842 | * be doing a round trip to the supplicant daemon for each variable. |
| 843 | */ |
| 844 | String value; |
| 845 | |
| Irfan Sheriff | a8fbe1f | 2010-03-09 09:13:58 -0800 | [diff] [blame] | 846 | value = mWifiStateTracker.getNetworkVariable(netId, WifiConfiguration.ssidVarName); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 847 | if (!TextUtils.isEmpty(value)) { |
| Chung-yih Wang | a8d1594 | 2009-10-09 11:01:49 +0800 | [diff] [blame] | 848 | config.SSID = removeDoubleQuotes(value); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 849 | } else { |
| 850 | config.SSID = null; |
| 851 | } |
| 852 | |
| Irfan Sheriff | a8fbe1f | 2010-03-09 09:13:58 -0800 | [diff] [blame] | 853 | value = mWifiStateTracker.getNetworkVariable(netId, WifiConfiguration.bssidVarName); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 854 | if (!TextUtils.isEmpty(value)) { |
| 855 | config.BSSID = value; |
| 856 | } else { |
| 857 | config.BSSID = null; |
| 858 | } |
| 859 | |
| Irfan Sheriff | a8fbe1f | 2010-03-09 09:13:58 -0800 | [diff] [blame] | 860 | value = mWifiStateTracker.getNetworkVariable(netId, WifiConfiguration.priorityVarName); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 861 | config.priority = -1; |
| 862 | if (!TextUtils.isEmpty(value)) { |
| 863 | try { |
| 864 | config.priority = Integer.parseInt(value); |
| 865 | } catch (NumberFormatException ignore) { |
| 866 | } |
| 867 | } |
| 868 | |
| Irfan Sheriff | a8fbe1f | 2010-03-09 09:13:58 -0800 | [diff] [blame] | 869 | value = mWifiStateTracker.getNetworkVariable(netId, WifiConfiguration.hiddenSSIDVarName); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 870 | config.hiddenSSID = false; |
| 871 | if (!TextUtils.isEmpty(value)) { |
| 872 | try { |
| 873 | config.hiddenSSID = Integer.parseInt(value) != 0; |
| 874 | } catch (NumberFormatException ignore) { |
| 875 | } |
| 876 | } |
| 877 | |
| Irfan Sheriff | a8fbe1f | 2010-03-09 09:13:58 -0800 | [diff] [blame] | 878 | value = mWifiStateTracker.getNetworkVariable(netId, WifiConfiguration.wepTxKeyIdxVarName); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 879 | config.wepTxKeyIndex = -1; |
| 880 | if (!TextUtils.isEmpty(value)) { |
| 881 | try { |
| 882 | config.wepTxKeyIndex = Integer.parseInt(value); |
| 883 | } catch (NumberFormatException ignore) { |
| 884 | } |
| 885 | } |
| 886 | |
| 887 | /* |
| 888 | * Get up to 4 WEP keys. Note that the actual keys are not passed back, |
| 889 | * just a "*" if the key is set, or the null string otherwise. |
| 890 | */ |
| 891 | for (int i = 0; i < 4; i++) { |
| Irfan Sheriff | a8fbe1f | 2010-03-09 09:13:58 -0800 | [diff] [blame] | 892 | value = mWifiStateTracker.getNetworkVariable(netId, WifiConfiguration.wepKeyVarNames[i]); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 893 | if (!TextUtils.isEmpty(value)) { |
| 894 | config.wepKeys[i] = value; |
| 895 | } else { |
| 896 | config.wepKeys[i] = null; |
| 897 | } |
| 898 | } |
| 899 | |
| 900 | /* |
| 901 | * Get the private shared key. Note that the actual keys are not passed back, |
| 902 | * just a "*" if the key is set, or the null string otherwise. |
| 903 | */ |
| Irfan Sheriff | a8fbe1f | 2010-03-09 09:13:58 -0800 | [diff] [blame] | 904 | value = mWifiStateTracker.getNetworkVariable(netId, WifiConfiguration.pskVarName); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 905 | if (!TextUtils.isEmpty(value)) { |
| 906 | config.preSharedKey = value; |
| 907 | } else { |
| 908 | config.preSharedKey = null; |
| 909 | } |
| 910 | |
| Irfan Sheriff | a8fbe1f | 2010-03-09 09:13:58 -0800 | [diff] [blame] | 911 | value = mWifiStateTracker.getNetworkVariable(config.networkId, |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 912 | WifiConfiguration.Protocol.varName); |
| 913 | if (!TextUtils.isEmpty(value)) { |
| 914 | String vals[] = value.split(" "); |
| 915 | for (String val : vals) { |
| 916 | int index = |
| 917 | lookupString(val, WifiConfiguration.Protocol.strings); |
| 918 | if (0 <= index) { |
| 919 | config.allowedProtocols.set(index); |
| 920 | } |
| 921 | } |
| 922 | } |
| 923 | |
| Irfan Sheriff | a8fbe1f | 2010-03-09 09:13:58 -0800 | [diff] [blame] | 924 | value = mWifiStateTracker.getNetworkVariable(config.networkId, |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 925 | WifiConfiguration.KeyMgmt.varName); |
| 926 | if (!TextUtils.isEmpty(value)) { |
| 927 | String vals[] = value.split(" "); |
| 928 | for (String val : vals) { |
| 929 | int index = |
| 930 | lookupString(val, WifiConfiguration.KeyMgmt.strings); |
| 931 | if (0 <= index) { |
| 932 | config.allowedKeyManagement.set(index); |
| 933 | } |
| 934 | } |
| 935 | } |
| 936 | |
| Irfan Sheriff | a8fbe1f | 2010-03-09 09:13:58 -0800 | [diff] [blame] | 937 | value = mWifiStateTracker.getNetworkVariable(config.networkId, |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 938 | WifiConfiguration.AuthAlgorithm.varName); |
| 939 | if (!TextUtils.isEmpty(value)) { |
| 940 | String vals[] = value.split(" "); |
| 941 | for (String val : vals) { |
| 942 | int index = |
| 943 | lookupString(val, WifiConfiguration.AuthAlgorithm.strings); |
| 944 | if (0 <= index) { |
| 945 | config.allowedAuthAlgorithms.set(index); |
| 946 | } |
| 947 | } |
| 948 | } |
| 949 | |
| Irfan Sheriff | a8fbe1f | 2010-03-09 09:13:58 -0800 | [diff] [blame] | 950 | value = mWifiStateTracker.getNetworkVariable(config.networkId, |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 951 | WifiConfiguration.PairwiseCipher.varName); |
| 952 | if (!TextUtils.isEmpty(value)) { |
| 953 | String vals[] = value.split(" "); |
| 954 | for (String val : vals) { |
| 955 | int index = |
| 956 | lookupString(val, WifiConfiguration.PairwiseCipher.strings); |
| 957 | if (0 <= index) { |
| 958 | config.allowedPairwiseCiphers.set(index); |
| 959 | } |
| 960 | } |
| 961 | } |
| 962 | |
| Irfan Sheriff | a8fbe1f | 2010-03-09 09:13:58 -0800 | [diff] [blame] | 963 | value = mWifiStateTracker.getNetworkVariable(config.networkId, |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 964 | WifiConfiguration.GroupCipher.varName); |
| 965 | if (!TextUtils.isEmpty(value)) { |
| 966 | String vals[] = value.split(" "); |
| 967 | for (String val : vals) { |
| 968 | int index = |
| 969 | lookupString(val, WifiConfiguration.GroupCipher.strings); |
| 970 | if (0 <= index) { |
| 971 | config.allowedGroupCiphers.set(index); |
| 972 | } |
| 973 | } |
| 974 | } |
| Chung-yih Wang | 4337476 | 2009-09-16 14:28:42 +0800 | [diff] [blame] | 975 | |
| 976 | for (WifiConfiguration.EnterpriseField field : |
| 977 | config.enterpriseFields) { |
| Irfan Sheriff | a8fbe1f | 2010-03-09 09:13:58 -0800 | [diff] [blame] | 978 | value = mWifiStateTracker.getNetworkVariable(netId, |
| Chung-yih Wang | 4337476 | 2009-09-16 14:28:42 +0800 | [diff] [blame] | 979 | field.varName()); |
| 980 | if (!TextUtils.isEmpty(value)) { |
| Chung-yih Wang | a8d1594 | 2009-10-09 11:01:49 +0800 | [diff] [blame] | 981 | if (field != config.eap) value = removeDoubleQuotes(value); |
| Chung-yih Wang | 4337476 | 2009-09-16 14:28:42 +0800 | [diff] [blame] | 982 | field.setValue(value); |
| 983 | } |
| 984 | } |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 985 | } |
| 986 | |
| Chung-yih Wang | a8d1594 | 2009-10-09 11:01:49 +0800 | [diff] [blame] | 987 | private static String removeDoubleQuotes(String string) { |
| 988 | if (string.length() <= 2) return ""; |
| 989 | return string.substring(1, string.length() - 1); |
| 990 | } |
| 991 | |
| 992 | private static String convertToQuotedString(String string) { |
| 993 | return "\"" + string + "\""; |
| 994 | } |
| 995 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 996 | /** |
| 997 | * see {@link android.net.wifi.WifiManager#addOrUpdateNetwork(WifiConfiguration)} |
| 998 | * @return the supplicant-assigned identifier for the new or updated |
| 999 | * network if the operation succeeds, or {@code -1} if it fails |
| 1000 | */ |
| Irfan Sheriff | 7aac554 | 2009-12-22 21:42:17 -0800 | [diff] [blame] | 1001 | public int addOrUpdateNetwork(WifiConfiguration config) { |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1002 | enforceChangePermission(); |
| Irfan Sheriff | a8fbe1f | 2010-03-09 09:13:58 -0800 | [diff] [blame] | 1003 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1004 | /* |
| 1005 | * If the supplied networkId is -1, we create a new empty |
| 1006 | * network configuration. Otherwise, the networkId should |
| 1007 | * refer to an existing configuration. |
| 1008 | */ |
| 1009 | int netId = config.networkId; |
| 1010 | boolean newNetwork = netId == -1; |
| Irfan Sheriff | 44113ba3 | 2010-03-16 14:54:07 -0700 | [diff] [blame] | 1011 | boolean doReconfig = false; |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1012 | // networkId of -1 means we want to create a new network |
| Irfan Sheriff | 7aac554 | 2009-12-22 21:42:17 -0800 | [diff] [blame] | 1013 | synchronized (mWifiStateTracker) { |
| 1014 | if (newNetwork) { |
| Irfan Sheriff | a8fbe1f | 2010-03-09 09:13:58 -0800 | [diff] [blame] | 1015 | netId = mWifiStateTracker.addNetwork(); |
| Irfan Sheriff | 7aac554 | 2009-12-22 21:42:17 -0800 | [diff] [blame] | 1016 | if (netId < 0) { |
| 1017 | if (DBG) { |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 1018 | Slog.d(TAG, "Failed to add a network!"); |
| Irfan Sheriff | 7aac554 | 2009-12-22 21:42:17 -0800 | [diff] [blame] | 1019 | } |
| 1020 | return -1; |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1021 | } |
| Irfan Sheriff | 7aac554 | 2009-12-22 21:42:17 -0800 | [diff] [blame] | 1022 | doReconfig = true; |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1023 | } |
| Irfan Sheriff | 7aac554 | 2009-12-22 21:42:17 -0800 | [diff] [blame] | 1024 | mNeedReconfig = mNeedReconfig || doReconfig; |
| Irfan Sheriff | a8fbe1f | 2010-03-09 09:13:58 -0800 | [diff] [blame] | 1025 | } |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1026 | |
| Irfan Sheriff | a8fbe1f | 2010-03-09 09:13:58 -0800 | [diff] [blame] | 1027 | setVariables: { |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1028 | /* |
| 1029 | * Note that if a networkId for a non-existent network |
| Irfan Sheriff | a8fbe1f | 2010-03-09 09:13:58 -0800 | [diff] [blame] | 1030 | * was supplied, then the first setNetworkVariable() |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1031 | * will fail, so we don't bother to make a separate check |
| 1032 | * for the validity of the ID up front. |
| 1033 | */ |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1034 | if (config.SSID != null && |
| Irfan Sheriff | a8fbe1f | 2010-03-09 09:13:58 -0800 | [diff] [blame] | 1035 | !mWifiStateTracker.setNetworkVariable( |
| Irfan Sheriff | 7aac554 | 2009-12-22 21:42:17 -0800 | [diff] [blame] | 1036 | netId, |
| 1037 | WifiConfiguration.ssidVarName, |
| 1038 | convertToQuotedString(config.SSID))) { |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1039 | if (DBG) { |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 1040 | Slog.d(TAG, "failed to set SSID: "+config.SSID); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1041 | } |
| 1042 | break setVariables; |
| 1043 | } |
| 1044 | |
| 1045 | if (config.BSSID != null && |
| Irfan Sheriff | a8fbe1f | 2010-03-09 09:13:58 -0800 | [diff] [blame] | 1046 | !mWifiStateTracker.setNetworkVariable( |
| Irfan Sheriff | 7aac554 | 2009-12-22 21:42:17 -0800 | [diff] [blame] | 1047 | netId, |
| 1048 | WifiConfiguration.bssidVarName, |
| 1049 | config.BSSID)) { |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1050 | if (DBG) { |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 1051 | Slog.d(TAG, "failed to set BSSID: "+config.BSSID); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1052 | } |
| 1053 | break setVariables; |
| 1054 | } |
| 1055 | |
| 1056 | String allowedKeyManagementString = |
| 1057 | makeString(config.allowedKeyManagement, WifiConfiguration.KeyMgmt.strings); |
| 1058 | if (config.allowedKeyManagement.cardinality() != 0 && |
| Irfan Sheriff | a8fbe1f | 2010-03-09 09:13:58 -0800 | [diff] [blame] | 1059 | !mWifiStateTracker.setNetworkVariable( |
| Irfan Sheriff | 7aac554 | 2009-12-22 21:42:17 -0800 | [diff] [blame] | 1060 | netId, |
| 1061 | WifiConfiguration.KeyMgmt.varName, |
| 1062 | allowedKeyManagementString)) { |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1063 | if (DBG) { |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 1064 | Slog.d(TAG, "failed to set key_mgmt: "+ |
| Irfan Sheriff | 7aac554 | 2009-12-22 21:42:17 -0800 | [diff] [blame] | 1065 | allowedKeyManagementString); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1066 | } |
| 1067 | break setVariables; |
| 1068 | } |
| 1069 | |
| 1070 | String allowedProtocolsString = |
| 1071 | makeString(config.allowedProtocols, WifiConfiguration.Protocol.strings); |
| 1072 | if (config.allowedProtocols.cardinality() != 0 && |
| Irfan Sheriff | a8fbe1f | 2010-03-09 09:13:58 -0800 | [diff] [blame] | 1073 | !mWifiStateTracker.setNetworkVariable( |
| Irfan Sheriff | 7aac554 | 2009-12-22 21:42:17 -0800 | [diff] [blame] | 1074 | netId, |
| 1075 | WifiConfiguration.Protocol.varName, |
| 1076 | allowedProtocolsString)) { |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1077 | if (DBG) { |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 1078 | Slog.d(TAG, "failed to set proto: "+ |
| Irfan Sheriff | 7aac554 | 2009-12-22 21:42:17 -0800 | [diff] [blame] | 1079 | allowedProtocolsString); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1080 | } |
| 1081 | break setVariables; |
| 1082 | } |
| 1083 | |
| 1084 | String allowedAuthAlgorithmsString = |
| 1085 | makeString(config.allowedAuthAlgorithms, WifiConfiguration.AuthAlgorithm.strings); |
| 1086 | if (config.allowedAuthAlgorithms.cardinality() != 0 && |
| Irfan Sheriff | a8fbe1f | 2010-03-09 09:13:58 -0800 | [diff] [blame] | 1087 | !mWifiStateTracker.setNetworkVariable( |
| Irfan Sheriff | 7aac554 | 2009-12-22 21:42:17 -0800 | [diff] [blame] | 1088 | netId, |
| 1089 | WifiConfiguration.AuthAlgorithm.varName, |
| 1090 | allowedAuthAlgorithmsString)) { |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1091 | if (DBG) { |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 1092 | Slog.d(TAG, "failed to set auth_alg: "+ |
| Irfan Sheriff | 7aac554 | 2009-12-22 21:42:17 -0800 | [diff] [blame] | 1093 | allowedAuthAlgorithmsString); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1094 | } |
| 1095 | break setVariables; |
| 1096 | } |
| 1097 | |
| 1098 | String allowedPairwiseCiphersString = |
| 1099 | makeString(config.allowedPairwiseCiphers, WifiConfiguration.PairwiseCipher.strings); |
| 1100 | if (config.allowedPairwiseCiphers.cardinality() != 0 && |
| Irfan Sheriff | a8fbe1f | 2010-03-09 09:13:58 -0800 | [diff] [blame] | 1101 | !mWifiStateTracker.setNetworkVariable( |
| Irfan Sheriff | 7aac554 | 2009-12-22 21:42:17 -0800 | [diff] [blame] | 1102 | netId, |
| 1103 | WifiConfiguration.PairwiseCipher.varName, |
| 1104 | allowedPairwiseCiphersString)) { |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1105 | if (DBG) { |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 1106 | Slog.d(TAG, "failed to set pairwise: "+ |
| Irfan Sheriff | 7aac554 | 2009-12-22 21:42:17 -0800 | [diff] [blame] | 1107 | allowedPairwiseCiphersString); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1108 | } |
| 1109 | break setVariables; |
| 1110 | } |
| 1111 | |
| 1112 | String allowedGroupCiphersString = |
| 1113 | makeString(config.allowedGroupCiphers, WifiConfiguration.GroupCipher.strings); |
| 1114 | if (config.allowedGroupCiphers.cardinality() != 0 && |
| Irfan Sheriff | a8fbe1f | 2010-03-09 09:13:58 -0800 | [diff] [blame] | 1115 | !mWifiStateTracker.setNetworkVariable( |
| Irfan Sheriff | 7aac554 | 2009-12-22 21:42:17 -0800 | [diff] [blame] | 1116 | netId, |
| 1117 | WifiConfiguration.GroupCipher.varName, |
| 1118 | allowedGroupCiphersString)) { |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1119 | if (DBG) { |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 1120 | Slog.d(TAG, "failed to set group: "+ |
| Irfan Sheriff | 7aac554 | 2009-12-22 21:42:17 -0800 | [diff] [blame] | 1121 | allowedGroupCiphersString); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1122 | } |
| 1123 | break setVariables; |
| 1124 | } |
| 1125 | |
| 1126 | // Prevent client screw-up by passing in a WifiConfiguration we gave it |
| 1127 | // by preventing "*" as a key. |
| 1128 | if (config.preSharedKey != null && !config.preSharedKey.equals("*") && |
| Irfan Sheriff | a8fbe1f | 2010-03-09 09:13:58 -0800 | [diff] [blame] | 1129 | !mWifiStateTracker.setNetworkVariable( |
| Irfan Sheriff | 7aac554 | 2009-12-22 21:42:17 -0800 | [diff] [blame] | 1130 | netId, |
| 1131 | WifiConfiguration.pskVarName, |
| 1132 | config.preSharedKey)) { |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1133 | if (DBG) { |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 1134 | Slog.d(TAG, "failed to set psk: "+config.preSharedKey); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1135 | } |
| 1136 | break setVariables; |
| 1137 | } |
| 1138 | |
| 1139 | boolean hasSetKey = false; |
| 1140 | if (config.wepKeys != null) { |
| 1141 | for (int i = 0; i < config.wepKeys.length; i++) { |
| 1142 | // Prevent client screw-up by passing in a WifiConfiguration we gave it |
| 1143 | // by preventing "*" as a key. |
| 1144 | if (config.wepKeys[i] != null && !config.wepKeys[i].equals("*")) { |
| Irfan Sheriff | a8fbe1f | 2010-03-09 09:13:58 -0800 | [diff] [blame] | 1145 | if (!mWifiStateTracker.setNetworkVariable( |
| Irfan Sheriff | 7aac554 | 2009-12-22 21:42:17 -0800 | [diff] [blame] | 1146 | netId, |
| 1147 | WifiConfiguration.wepKeyVarNames[i], |
| 1148 | config.wepKeys[i])) { |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1149 | if (DBG) { |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 1150 | Slog.d(TAG, |
| Irfan Sheriff | 7aac554 | 2009-12-22 21:42:17 -0800 | [diff] [blame] | 1151 | "failed to set wep_key"+i+": " + |
| 1152 | config.wepKeys[i]); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1153 | } |
| 1154 | break setVariables; |
| 1155 | } |
| 1156 | hasSetKey = true; |
| 1157 | } |
| 1158 | } |
| 1159 | } |
| 1160 | |
| 1161 | if (hasSetKey) { |
| Irfan Sheriff | a8fbe1f | 2010-03-09 09:13:58 -0800 | [diff] [blame] | 1162 | if (!mWifiStateTracker.setNetworkVariable( |
| Irfan Sheriff | 7aac554 | 2009-12-22 21:42:17 -0800 | [diff] [blame] | 1163 | netId, |
| 1164 | WifiConfiguration.wepTxKeyIdxVarName, |
| 1165 | Integer.toString(config.wepTxKeyIndex))) { |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1166 | if (DBG) { |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 1167 | Slog.d(TAG, |
| Irfan Sheriff | 7aac554 | 2009-12-22 21:42:17 -0800 | [diff] [blame] | 1168 | "failed to set wep_tx_keyidx: "+ |
| 1169 | config.wepTxKeyIndex); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1170 | } |
| 1171 | break setVariables; |
| 1172 | } |
| 1173 | } |
| 1174 | |
| Irfan Sheriff | a8fbe1f | 2010-03-09 09:13:58 -0800 | [diff] [blame] | 1175 | if (!mWifiStateTracker.setNetworkVariable( |
| Irfan Sheriff | 7aac554 | 2009-12-22 21:42:17 -0800 | [diff] [blame] | 1176 | netId, |
| 1177 | WifiConfiguration.priorityVarName, |
| 1178 | Integer.toString(config.priority))) { |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1179 | if (DBG) { |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 1180 | Slog.d(TAG, config.SSID + ": failed to set priority: " |
| Irfan Sheriff | 7aac554 | 2009-12-22 21:42:17 -0800 | [diff] [blame] | 1181 | +config.priority); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1182 | } |
| 1183 | break setVariables; |
| 1184 | } |
| 1185 | |
| Irfan Sheriff | a8fbe1f | 2010-03-09 09:13:58 -0800 | [diff] [blame] | 1186 | if (config.hiddenSSID && !mWifiStateTracker.setNetworkVariable( |
| Irfan Sheriff | 7aac554 | 2009-12-22 21:42:17 -0800 | [diff] [blame] | 1187 | netId, |
| 1188 | WifiConfiguration.hiddenSSIDVarName, |
| 1189 | Integer.toString(config.hiddenSSID ? 1 : 0))) { |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1190 | if (DBG) { |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 1191 | Slog.d(TAG, config.SSID + ": failed to set hiddenSSID: "+ |
| Irfan Sheriff | 7aac554 | 2009-12-22 21:42:17 -0800 | [diff] [blame] | 1192 | config.hiddenSSID); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1193 | } |
| 1194 | break setVariables; |
| 1195 | } |
| 1196 | |
| Chung-yih Wang | 4337476 | 2009-09-16 14:28:42 +0800 | [diff] [blame] | 1197 | for (WifiConfiguration.EnterpriseField field |
| 1198 | : config.enterpriseFields) { |
| 1199 | String varName = field.varName(); |
| 1200 | String value = field.value(); |
| Chung-yih Wang | a8d1594 | 2009-10-09 11:01:49 +0800 | [diff] [blame] | 1201 | if (value != null) { |
| 1202 | if (field != config.eap) { |
| Chia-chi Yeh | 784d53e | 2010-01-29 16:26:28 +0800 | [diff] [blame] | 1203 | value = (value.length() == 0) ? "NULL" : convertToQuotedString(value); |
| Chung-yih Wang | 4337476 | 2009-09-16 14:28:42 +0800 | [diff] [blame] | 1204 | } |
| Irfan Sheriff | a8fbe1f | 2010-03-09 09:13:58 -0800 | [diff] [blame] | 1205 | if (!mWifiStateTracker.setNetworkVariable( |
| Irfan Sheriff | 7aac554 | 2009-12-22 21:42:17 -0800 | [diff] [blame] | 1206 | netId, |
| 1207 | varName, |
| 1208 | value)) { |
| Chung-yih Wang | a8d1594 | 2009-10-09 11:01:49 +0800 | [diff] [blame] | 1209 | if (DBG) { |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 1210 | Slog.d(TAG, config.SSID + ": failed to set " + varName + |
| Irfan Sheriff | 7aac554 | 2009-12-22 21:42:17 -0800 | [diff] [blame] | 1211 | ": " + value); |
| Chung-yih Wang | a8d1594 | 2009-10-09 11:01:49 +0800 | [diff] [blame] | 1212 | } |
| 1213 | break setVariables; |
| 1214 | } |
| Chung-yih Wang | 5069cc7 | 2009-06-03 17:33:47 +0800 | [diff] [blame] | 1215 | } |
| Chung-yih Wang | 5069cc7 | 2009-06-03 17:33:47 +0800 | [diff] [blame] | 1216 | } |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1217 | return netId; |
| Irfan Sheriff | a8fbe1f | 2010-03-09 09:13:58 -0800 | [diff] [blame] | 1218 | } |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1219 | |
| Irfan Sheriff | a8fbe1f | 2010-03-09 09:13:58 -0800 | [diff] [blame] | 1220 | /* |
| 1221 | * For an update, if one of the setNetworkVariable operations fails, |
| 1222 | * we might want to roll back all the changes already made. But the |
| 1223 | * chances are that if anything is going to go wrong, it'll happen |
| 1224 | * the first time we try to set one of the variables. |
| 1225 | */ |
| 1226 | if (newNetwork) { |
| 1227 | removeNetwork(netId); |
| 1228 | if (DBG) { |
| 1229 | Slog.d(TAG, |
| 1230 | "Failed to set a network variable, removed network: " |
| 1231 | + netId); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1232 | } |
| 1233 | } |
| 1234 | return -1; |
| 1235 | } |
| 1236 | |
| 1237 | private static String makeString(BitSet set, String[] strings) { |
| 1238 | StringBuffer buf = new StringBuffer(); |
| 1239 | int nextSetBit = -1; |
| 1240 | |
| 1241 | /* Make sure all set bits are in [0, strings.length) to avoid |
| 1242 | * going out of bounds on strings. (Shouldn't happen, but...) */ |
| 1243 | set = set.get(0, strings.length); |
| 1244 | |
| 1245 | while ((nextSetBit = set.nextSetBit(nextSetBit + 1)) != -1) { |
| 1246 | buf.append(strings[nextSetBit].replace('_', '-')).append(' '); |
| 1247 | } |
| 1248 | |
| 1249 | // remove trailing space |
| 1250 | if (set.cardinality() > 0) { |
| 1251 | buf.setLength(buf.length() - 1); |
| 1252 | } |
| 1253 | |
| 1254 | return buf.toString(); |
| 1255 | } |
| 1256 | |
| 1257 | private static int lookupString(String string, String[] strings) { |
| 1258 | int size = strings.length; |
| 1259 | |
| 1260 | string = string.replace('-', '_'); |
| 1261 | |
| 1262 | for (int i = 0; i < size; i++) |
| 1263 | if (string.equals(strings[i])) |
| 1264 | return i; |
| 1265 | |
| 1266 | if (DBG) { |
| 1267 | // if we ever get here, we should probably add the |
| 1268 | // value to WifiConfiguration to reflect that it's |
| 1269 | // supported by the WPA supplicant |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 1270 | Slog.w(TAG, "Failed to look-up a string: " + string); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1271 | } |
| 1272 | |
| 1273 | return -1; |
| 1274 | } |
| 1275 | |
| 1276 | /** |
| 1277 | * See {@link android.net.wifi.WifiManager#removeNetwork(int)} |
| 1278 | * @param netId the integer that identifies the network configuration |
| 1279 | * to the supplicant |
| 1280 | * @return {@code true} if the operation succeeded |
| 1281 | */ |
| 1282 | public boolean removeNetwork(int netId) { |
| 1283 | enforceChangePermission(); |
| 1284 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1285 | return mWifiStateTracker.removeNetwork(netId); |
| 1286 | } |
| 1287 | |
| 1288 | /** |
| 1289 | * See {@link android.net.wifi.WifiManager#enableNetwork(int, boolean)} |
| 1290 | * @param netId the integer that identifies the network configuration |
| 1291 | * to the supplicant |
| 1292 | * @param disableOthers if true, disable all other networks. |
| 1293 | * @return {@code true} if the operation succeeded |
| 1294 | */ |
| 1295 | public boolean enableNetwork(int netId, boolean disableOthers) { |
| 1296 | enforceChangePermission(); |
| 1297 | |
| Irfan Sheriff | a8fbe1f | 2010-03-09 09:13:58 -0800 | [diff] [blame] | 1298 | String ifname = mWifiStateTracker.getInterfaceName(); |
| 1299 | NetworkUtils.enableInterface(ifname); |
| 1300 | boolean result = mWifiStateTracker.enableNetwork(netId, disableOthers); |
| 1301 | if (!result) { |
| 1302 | NetworkUtils.disableInterface(ifname); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1303 | } |
| Irfan Sheriff | a8fbe1f | 2010-03-09 09:13:58 -0800 | [diff] [blame] | 1304 | return result; |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1305 | } |
| 1306 | |
| 1307 | /** |
| 1308 | * See {@link android.net.wifi.WifiManager#disableNetwork(int)} |
| 1309 | * @param netId the integer that identifies the network configuration |
| 1310 | * to the supplicant |
| 1311 | * @return {@code true} if the operation succeeded |
| 1312 | */ |
| 1313 | public boolean disableNetwork(int netId) { |
| 1314 | enforceChangePermission(); |
| 1315 | |
| Irfan Sheriff | a8fbe1f | 2010-03-09 09:13:58 -0800 | [diff] [blame] | 1316 | return mWifiStateTracker.disableNetwork(netId); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1317 | } |
| 1318 | |
| 1319 | /** |
| 1320 | * See {@link android.net.wifi.WifiManager#getConnectionInfo()} |
| 1321 | * @return the Wi-Fi information, contained in {@link WifiInfo}. |
| 1322 | */ |
| 1323 | public WifiInfo getConnectionInfo() { |
| 1324 | enforceAccessPermission(); |
| 1325 | /* |
| 1326 | * Make sure we have the latest information, by sending |
| 1327 | * a status request to the supplicant. |
| 1328 | */ |
| 1329 | return mWifiStateTracker.requestConnectionInfo(); |
| 1330 | } |
| 1331 | |
| 1332 | /** |
| 1333 | * Return the results of the most recent access point scan, in the form of |
| 1334 | * a list of {@link ScanResult} objects. |
| 1335 | * @return the list of results |
| 1336 | */ |
| 1337 | public List<ScanResult> getScanResults() { |
| 1338 | enforceAccessPermission(); |
| 1339 | String reply; |
| Irfan Sheriff | a8fbe1f | 2010-03-09 09:13:58 -0800 | [diff] [blame] | 1340 | |
| 1341 | reply = mWifiStateTracker.scanResults(); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1342 | if (reply == null) { |
| 1343 | return null; |
| 1344 | } |
| 1345 | |
| 1346 | List<ScanResult> scanList = new ArrayList<ScanResult>(); |
| 1347 | |
| 1348 | int lineCount = 0; |
| 1349 | |
| 1350 | int replyLen = reply.length(); |
| 1351 | // Parse the result string, keeping in mind that the last line does |
| 1352 | // not end with a newline. |
| 1353 | for (int lineBeg = 0, lineEnd = 0; lineEnd <= replyLen; ++lineEnd) { |
| 1354 | if (lineEnd == replyLen || reply.charAt(lineEnd) == '\n') { |
| 1355 | ++lineCount; |
| 1356 | /* |
| 1357 | * Skip the first line, which is a header |
| 1358 | */ |
| 1359 | if (lineCount == 1) { |
| 1360 | lineBeg = lineEnd + 1; |
| 1361 | continue; |
| 1362 | } |
| Mike Lockwood | b30475e | 2009-04-21 13:55:07 -0700 | [diff] [blame] | 1363 | if (lineEnd > lineBeg) { |
| 1364 | String line = reply.substring(lineBeg, lineEnd); |
| 1365 | ScanResult scanResult = parseScanResult(line); |
| 1366 | if (scanResult != null) { |
| 1367 | scanList.add(scanResult); |
| 1368 | } else if (DBG) { |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 1369 | Slog.w(TAG, "misformatted scan result for: " + line); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1370 | } |
| 1371 | } |
| 1372 | lineBeg = lineEnd + 1; |
| 1373 | } |
| 1374 | } |
| 1375 | mWifiStateTracker.setScanResultsList(scanList); |
| 1376 | return scanList; |
| 1377 | } |
| 1378 | |
| 1379 | /** |
| 1380 | * Parse the scan result line passed to us by wpa_supplicant (helper). |
| 1381 | * @param line the line to parse |
| 1382 | * @return the {@link ScanResult} object |
| 1383 | */ |
| 1384 | private ScanResult parseScanResult(String line) { |
| 1385 | ScanResult scanResult = null; |
| 1386 | if (line != null) { |
| 1387 | /* |
| 1388 | * Cache implementation (LinkedHashMap) is not synchronized, thus, |
| 1389 | * must synchronized here! |
| 1390 | */ |
| 1391 | synchronized (mScanResultCache) { |
| Mike Lockwood | b30475e | 2009-04-21 13:55:07 -0700 | [diff] [blame] | 1392 | String[] result = scanResultPattern.split(line); |
| 1393 | if (3 <= result.length && result.length <= 5) { |
| 1394 | String bssid = result[0]; |
| 1395 | // bssid | frequency | level | flags | ssid |
| 1396 | int frequency; |
| 1397 | int level; |
| 1398 | try { |
| 1399 | frequency = Integer.parseInt(result[1]); |
| 1400 | level = Integer.parseInt(result[2]); |
| 1401 | /* some implementations avoid negative values by adding 256 |
| 1402 | * so we need to adjust for that here. |
| 1403 | */ |
| 1404 | if (level > 0) level -= 256; |
| 1405 | } catch (NumberFormatException e) { |
| 1406 | frequency = 0; |
| 1407 | level = 0; |
| 1408 | } |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1409 | |
| Mike Lockwood | 1a64505 | 2009-06-25 13:01:12 -0400 | [diff] [blame] | 1410 | /* |
| 1411 | * The formatting of the results returned by |
| 1412 | * wpa_supplicant is intended to make the fields |
| 1413 | * line up nicely when printed, |
| 1414 | * not to make them easy to parse. So we have to |
| 1415 | * apply some heuristics to figure out which field |
| 1416 | * is the SSID and which field is the flags. |
| 1417 | */ |
| 1418 | String ssid; |
| 1419 | String flags; |
| 1420 | if (result.length == 4) { |
| 1421 | if (result[3].charAt(0) == '[') { |
| 1422 | flags = result[3]; |
| 1423 | ssid = ""; |
| 1424 | } else { |
| 1425 | flags = ""; |
| 1426 | ssid = result[3]; |
| 1427 | } |
| 1428 | } else if (result.length == 5) { |
| 1429 | flags = result[3]; |
| 1430 | ssid = result[4]; |
| 1431 | } else { |
| 1432 | // Here, we must have 3 fields: no flags and ssid |
| 1433 | // set |
| 1434 | flags = ""; |
| 1435 | ssid = ""; |
| 1436 | } |
| 1437 | |
| Mike Lockwood | 00717e2 | 2009-08-17 10:09:36 -0400 | [diff] [blame] | 1438 | // bssid + ssid is the hash key |
| 1439 | String key = bssid + ssid; |
| 1440 | scanResult = mScanResultCache.get(key); |
| Mike Lockwood | b30475e | 2009-04-21 13:55:07 -0700 | [diff] [blame] | 1441 | if (scanResult != null) { |
| 1442 | scanResult.level = level; |
| Mike Lockwood | 1a64505 | 2009-06-25 13:01:12 -0400 | [diff] [blame] | 1443 | scanResult.SSID = ssid; |
| 1444 | scanResult.capabilities = flags; |
| 1445 | scanResult.frequency = frequency; |
| Mike Lockwood | b30475e | 2009-04-21 13:55:07 -0700 | [diff] [blame] | 1446 | } else { |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1447 | // Do not add scan results that have no SSID set |
| 1448 | if (0 < ssid.trim().length()) { |
| 1449 | scanResult = |
| 1450 | new ScanResult( |
| Mike Lockwood | b30475e | 2009-04-21 13:55:07 -0700 | [diff] [blame] | 1451 | ssid, bssid, flags, level, frequency); |
| Mike Lockwood | 00717e2 | 2009-08-17 10:09:36 -0400 | [diff] [blame] | 1452 | mScanResultCache.put(key, scanResult); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1453 | } |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1454 | } |
| Mike Lockwood | b30475e | 2009-04-21 13:55:07 -0700 | [diff] [blame] | 1455 | } else { |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 1456 | Slog.w(TAG, "Misformatted scan result text with " + |
| Mike Lockwood | b30475e | 2009-04-21 13:55:07 -0700 | [diff] [blame] | 1457 | result.length + " fields: " + line); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1458 | } |
| 1459 | } |
| 1460 | } |
| 1461 | |
| 1462 | return scanResult; |
| 1463 | } |
| 1464 | |
| 1465 | /** |
| 1466 | * Parse the "flags" field passed back in a scan result by wpa_supplicant, |
| 1467 | * and construct a {@code WifiConfiguration} that describes the encryption, |
| 1468 | * key management, and authenticaion capabilities of the access point. |
| 1469 | * @param flags the string returned by wpa_supplicant |
| 1470 | * @return the {@link WifiConfiguration} object, filled in |
| 1471 | */ |
| 1472 | WifiConfiguration parseScanFlags(String flags) { |
| 1473 | WifiConfiguration config = new WifiConfiguration(); |
| 1474 | |
| 1475 | if (flags.length() == 0) { |
| 1476 | config.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE); |
| 1477 | } |
| 1478 | // ... to be implemented |
| 1479 | return config; |
| 1480 | } |
| 1481 | |
| 1482 | /** |
| 1483 | * Tell the supplicant to persist the current list of configured networks. |
| 1484 | * @return {@code true} if the operation succeeded |
| 1485 | */ |
| 1486 | public boolean saveConfiguration() { |
| 1487 | boolean result; |
| 1488 | enforceChangePermission(); |
| Irfan Sheriff | a8fbe1f | 2010-03-09 09:13:58 -0800 | [diff] [blame] | 1489 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1490 | synchronized (mWifiStateTracker) { |
| Irfan Sheriff | a8fbe1f | 2010-03-09 09:13:58 -0800 | [diff] [blame] | 1491 | result = mWifiStateTracker.saveConfig(); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1492 | if (result && mNeedReconfig) { |
| 1493 | mNeedReconfig = false; |
| Irfan Sheriff | a8fbe1f | 2010-03-09 09:13:58 -0800 | [diff] [blame] | 1494 | result = mWifiStateTracker.reloadConfig(); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1495 | |
| 1496 | if (result) { |
| 1497 | Intent intent = new Intent(WifiManager.NETWORK_IDS_CHANGED_ACTION); |
| 1498 | mContext.sendBroadcast(intent); |
| 1499 | } |
| 1500 | } |
| 1501 | } |
| Amith Yamasani | 47873e5 | 2009-07-02 12:05:32 -0700 | [diff] [blame] | 1502 | // Inform the backup manager about a data change |
| 1503 | IBackupManager ibm = IBackupManager.Stub.asInterface( |
| 1504 | ServiceManager.getService(Context.BACKUP_SERVICE)); |
| 1505 | if (ibm != null) { |
| 1506 | try { |
| 1507 | ibm.dataChanged("com.android.providers.settings"); |
| 1508 | } catch (Exception e) { |
| 1509 | // Try again later |
| 1510 | } |
| 1511 | } |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1512 | return result; |
| 1513 | } |
| 1514 | |
| 1515 | /** |
| 1516 | * Set the number of radio frequency channels that are allowed to be used |
| 1517 | * in the current regulatory domain. This method should be used only |
| 1518 | * if the correct number of channels cannot be determined automatically |
| Robert Greenwalt | b5010cc | 2009-05-21 15:11:40 -0700 | [diff] [blame] | 1519 | * 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] | 1520 | * persisted as a Secure setting. |
| 1521 | * @param numChannels the number of allowed channels. Must be greater than 0 |
| 1522 | * and less than or equal to 16. |
| Robert Greenwalt | b5010cc | 2009-05-21 15:11:40 -0700 | [diff] [blame] | 1523 | * @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] | 1524 | * @return {@code true} if the operation succeeds, {@code false} otherwise, e.g., |
| 1525 | * {@code numChannels} is outside the valid range. |
| 1526 | */ |
| Robert Greenwalt | b5010cc | 2009-05-21 15:11:40 -0700 | [diff] [blame] | 1527 | public boolean setNumAllowedChannels(int numChannels, boolean persist) { |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 1528 | Slog.i(TAG, "WifiService trying to setNumAllowed to "+numChannels+ |
| Robert Greenwalt | b5010cc | 2009-05-21 15:11:40 -0700 | [diff] [blame] | 1529 | " with persist set to "+persist); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1530 | enforceChangePermission(); |
| 1531 | /* |
| 1532 | * Validate the argument. We'd like to let the Wi-Fi driver do this, |
| 1533 | * but if Wi-Fi isn't currently enabled, that's not possible, and |
| 1534 | * we want to persist the setting anyway,so that it will take |
| 1535 | * effect when Wi-Fi does become enabled. |
| 1536 | */ |
| 1537 | boolean found = false; |
| 1538 | for (int validChan : sValidRegulatoryChannelCounts) { |
| 1539 | if (validChan == numChannels) { |
| 1540 | found = true; |
| 1541 | break; |
| 1542 | } |
| 1543 | } |
| 1544 | if (!found) { |
| 1545 | return false; |
| 1546 | } |
| 1547 | |
| Robert Greenwalt | b5010cc | 2009-05-21 15:11:40 -0700 | [diff] [blame] | 1548 | if (persist) { |
| 1549 | Settings.Secure.putInt(mContext.getContentResolver(), |
| 1550 | Settings.Secure.WIFI_NUM_ALLOWED_CHANNELS, |
| 1551 | numChannels); |
| 1552 | } |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1553 | mWifiStateTracker.setNumAllowedChannels(numChannels); |
| 1554 | return true; |
| 1555 | } |
| 1556 | |
| 1557 | /** |
| 1558 | * Return the number of frequency channels that are allowed |
| 1559 | * to be used in the current regulatory domain. |
| 1560 | * @return the number of allowed channels, or {@code -1} if an error occurs |
| 1561 | */ |
| 1562 | public int getNumAllowedChannels() { |
| 1563 | int numChannels; |
| 1564 | |
| 1565 | enforceAccessPermission(); |
| Irfan Sheriff | a8fbe1f | 2010-03-09 09:13:58 -0800 | [diff] [blame] | 1566 | |
| 1567 | /* |
| 1568 | * If we can't get the value from the driver (e.g., because |
| 1569 | * Wi-Fi is not currently enabled), get the value from |
| 1570 | * Settings. |
| 1571 | */ |
| 1572 | numChannels = mWifiStateTracker.getNumAllowedChannels(); |
| 1573 | if (numChannels < 0) { |
| 1574 | numChannels = Settings.Secure.getInt(mContext.getContentResolver(), |
| 1575 | Settings.Secure.WIFI_NUM_ALLOWED_CHANNELS, |
| 1576 | -1); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1577 | } |
| 1578 | return numChannels; |
| 1579 | } |
| 1580 | |
| 1581 | /** |
| 1582 | * Return the list of valid values for the number of allowed radio channels |
| 1583 | * for various regulatory domains. |
| 1584 | * @return the list of channel counts |
| 1585 | */ |
| 1586 | public int[] getValidChannelCounts() { |
| 1587 | enforceAccessPermission(); |
| 1588 | return sValidRegulatoryChannelCounts; |
| 1589 | } |
| 1590 | |
| 1591 | /** |
| 1592 | * Return the DHCP-assigned addresses from the last successful DHCP request, |
| 1593 | * if any. |
| 1594 | * @return the DHCP information |
| 1595 | */ |
| 1596 | public DhcpInfo getDhcpInfo() { |
| 1597 | enforceAccessPermission(); |
| 1598 | return mWifiStateTracker.getDhcpInfo(); |
| 1599 | } |
| 1600 | |
| 1601 | private final BroadcastReceiver mReceiver = new BroadcastReceiver() { |
| 1602 | @Override |
| 1603 | public void onReceive(Context context, Intent intent) { |
| 1604 | String action = intent.getAction(); |
| 1605 | |
| Doug Zongker | 43866e0 | 2010-01-07 12:09:54 -0800 | [diff] [blame] | 1606 | long idleMillis = |
| 1607 | Settings.Secure.getLong(mContext.getContentResolver(), |
| 1608 | Settings.Secure.WIFI_IDLE_MS, DEFAULT_IDLE_MILLIS); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1609 | int stayAwakeConditions = |
| Doug Zongker | 43866e0 | 2010-01-07 12:09:54 -0800 | [diff] [blame] | 1610 | Settings.System.getInt(mContext.getContentResolver(), |
| 1611 | Settings.System.STAY_ON_WHILE_PLUGGED_IN, 0); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1612 | if (action.equals(Intent.ACTION_SCREEN_ON)) { |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 1613 | Slog.d(TAG, "ACTION_SCREEN_ON"); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1614 | mAlarmManager.cancel(mIdleIntent); |
| 1615 | mDeviceIdle = false; |
| 1616 | mScreenOff = false; |
| Mike Lockwood | f32be16 | 2009-07-14 17:44:37 -0400 | [diff] [blame] | 1617 | mWifiStateTracker.enableRssiPolling(true); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1618 | } else if (action.equals(Intent.ACTION_SCREEN_OFF)) { |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 1619 | Slog.d(TAG, "ACTION_SCREEN_OFF"); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1620 | mScreenOff = true; |
| Mike Lockwood | f32be16 | 2009-07-14 17:44:37 -0400 | [diff] [blame] | 1621 | mWifiStateTracker.enableRssiPolling(false); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1622 | /* |
| 1623 | * Set a timer to put Wi-Fi to sleep, but only if the screen is off |
| 1624 | * AND the "stay on while plugged in" setting doesn't match the |
| 1625 | * current power conditions (i.e, not plugged in, plugged in to USB, |
| 1626 | * or plugged in to AC). |
| 1627 | */ |
| 1628 | if (!shouldWifiStayAwake(stayAwakeConditions, mPluggedType)) { |
| San Mehat | fa6c711 | 2009-07-07 09:34:44 -0700 | [diff] [blame] | 1629 | WifiInfo info = mWifiStateTracker.requestConnectionInfo(); |
| 1630 | if (info.getSupplicantState() != SupplicantState.COMPLETED) { |
| Robert Greenwalt | 84612ea6 | 2009-09-30 09:04:22 -0700 | [diff] [blame] | 1631 | // we used to go to sleep immediately, but this caused some race conditions |
| 1632 | // we don't have time to track down for this release. Delay instead, but not |
| 1633 | // as long as we would if connected (below) |
| 1634 | // TODO - fix the race conditions and switch back to the immediate turn-off |
| 1635 | long triggerTime = System.currentTimeMillis() + (2*60*1000); // 2 min |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 1636 | Slog.d(TAG, "setting ACTION_DEVICE_IDLE timer for 120,000 ms"); |
| Robert Greenwalt | 84612ea6 | 2009-09-30 09:04:22 -0700 | [diff] [blame] | 1637 | mAlarmManager.set(AlarmManager.RTC_WAKEUP, triggerTime, mIdleIntent); |
| 1638 | // // do not keep Wifi awake when screen is off if Wifi is not associated |
| 1639 | // mDeviceIdle = true; |
| 1640 | // updateWifiState(); |
| Mike Lockwood | d9c32bc | 2009-05-18 14:14:15 -0400 | [diff] [blame] | 1641 | } else { |
| 1642 | long triggerTime = System.currentTimeMillis() + idleMillis; |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 1643 | Slog.d(TAG, "setting ACTION_DEVICE_IDLE timer for " + idleMillis + "ms"); |
| Mike Lockwood | d9c32bc | 2009-05-18 14:14:15 -0400 | [diff] [blame] | 1644 | mAlarmManager.set(AlarmManager.RTC_WAKEUP, triggerTime, mIdleIntent); |
| 1645 | } |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1646 | } |
| 1647 | /* we can return now -- there's nothing to do until we get the idle intent back */ |
| 1648 | return; |
| 1649 | } else if (action.equals(ACTION_DEVICE_IDLE)) { |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 1650 | Slog.d(TAG, "got ACTION_DEVICE_IDLE"); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1651 | mDeviceIdle = true; |
| 1652 | } else if (action.equals(Intent.ACTION_BATTERY_CHANGED)) { |
| 1653 | /* |
| 1654 | * Set a timer to put Wi-Fi to sleep, but only if the screen is off |
| 1655 | * AND we are transitioning from a state in which the device was supposed |
| 1656 | * to stay awake to a state in which it is not supposed to stay awake. |
| 1657 | * If "stay awake" state is not changing, we do nothing, to avoid resetting |
| 1658 | * the already-set timer. |
| 1659 | */ |
| 1660 | int pluggedType = intent.getIntExtra("plugged", 0); |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 1661 | Slog.d(TAG, "ACTION_BATTERY_CHANGED pluggedType: " + pluggedType); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1662 | if (mScreenOff && shouldWifiStayAwake(stayAwakeConditions, mPluggedType) && |
| 1663 | !shouldWifiStayAwake(stayAwakeConditions, pluggedType)) { |
| 1664 | long triggerTime = System.currentTimeMillis() + idleMillis; |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 1665 | 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] | 1666 | mAlarmManager.set(AlarmManager.RTC_WAKEUP, triggerTime, mIdleIntent); |
| 1667 | mPluggedType = pluggedType; |
| 1668 | return; |
| 1669 | } |
| 1670 | mPluggedType = pluggedType; |
| Nick Pelly | 005b228 | 2009-09-10 10:21:56 -0700 | [diff] [blame] | 1671 | } else if (action.equals(BluetoothA2dp.ACTION_SINK_STATE_CHANGED)) { |
| Jaikumar Ganesh | 084c665 | 2009-12-07 10:58:18 -0800 | [diff] [blame] | 1672 | BluetoothA2dp a2dp = new BluetoothA2dp(mContext); |
| 1673 | Set<BluetoothDevice> sinks = a2dp.getConnectedSinks(); |
| 1674 | boolean isBluetoothPlaying = false; |
| 1675 | for (BluetoothDevice sink : sinks) { |
| 1676 | if (a2dp.getSinkState(sink) == BluetoothA2dp.STATE_PLAYING) { |
| 1677 | isBluetoothPlaying = true; |
| 1678 | } |
| 1679 | } |
| The Android Open Source Project | b2a3dd8 | 2009-03-09 11:52:12 -0700 | [diff] [blame] | 1680 | mWifiStateTracker.setBluetoothScanMode(isBluetoothPlaying); |
| Jaikumar Ganesh | 084c665 | 2009-12-07 10:58:18 -0800 | [diff] [blame] | 1681 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1682 | } else { |
| 1683 | return; |
| 1684 | } |
| 1685 | |
| 1686 | updateWifiState(); |
| 1687 | } |
| 1688 | |
| 1689 | /** |
| 1690 | * Determines whether the Wi-Fi chipset should stay awake or be put to |
| 1691 | * sleep. Looks at the setting for the sleep policy and the current |
| 1692 | * conditions. |
| Jaikumar Ganesh | 084c665 | 2009-12-07 10:58:18 -0800 | [diff] [blame] | 1693 | * |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1694 | * @see #shouldDeviceStayAwake(int, int) |
| 1695 | */ |
| 1696 | private boolean shouldWifiStayAwake(int stayAwakeConditions, int pluggedType) { |
| 1697 | int wifiSleepPolicy = Settings.System.getInt(mContext.getContentResolver(), |
| 1698 | Settings.System.WIFI_SLEEP_POLICY, Settings.System.WIFI_SLEEP_POLICY_DEFAULT); |
| 1699 | |
| 1700 | if (wifiSleepPolicy == Settings.System.WIFI_SLEEP_POLICY_NEVER) { |
| 1701 | // Never sleep |
| 1702 | return true; |
| 1703 | } else if ((wifiSleepPolicy == Settings.System.WIFI_SLEEP_POLICY_NEVER_WHILE_PLUGGED) && |
| 1704 | (pluggedType != 0)) { |
| 1705 | // Never sleep while plugged, and we're plugged |
| 1706 | return true; |
| 1707 | } else { |
| 1708 | // Default |
| 1709 | return shouldDeviceStayAwake(stayAwakeConditions, pluggedType); |
| 1710 | } |
| 1711 | } |
| Jaikumar Ganesh | 084c665 | 2009-12-07 10:58:18 -0800 | [diff] [blame] | 1712 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1713 | /** |
| 1714 | * Determine whether the bit value corresponding to {@code pluggedType} is set in |
| 1715 | * the bit string {@code stayAwakeConditions}. Because a {@code pluggedType} value |
| 1716 | * of {@code 0} isn't really a plugged type, but rather an indication that the |
| 1717 | * device isn't plugged in at all, there is no bit value corresponding to a |
| 1718 | * {@code pluggedType} value of {@code 0}. That is why we shift by |
| 1719 | * {@code pluggedType — 1} instead of by {@code pluggedType}. |
| 1720 | * @param stayAwakeConditions a bit string specifying which "plugged types" should |
| 1721 | * keep the device (and hence Wi-Fi) awake. |
| 1722 | * @param pluggedType the type of plug (USB, AC, or none) for which the check is |
| 1723 | * being made |
| 1724 | * @return {@code true} if {@code pluggedType} indicates that the device is |
| 1725 | * supposed to stay awake, {@code false} otherwise. |
| 1726 | */ |
| 1727 | private boolean shouldDeviceStayAwake(int stayAwakeConditions, int pluggedType) { |
| 1728 | return (stayAwakeConditions & pluggedType) != 0; |
| 1729 | } |
| 1730 | }; |
| 1731 | |
| Dianne Hackborn | 617f877 | 2009-03-31 15:04:46 -0700 | [diff] [blame] | 1732 | private void sendEnableMessage(boolean enable, boolean persist, int uid) { |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1733 | Message msg = Message.obtain(mWifiHandler, |
| 1734 | (enable ? MESSAGE_ENABLE_WIFI : MESSAGE_DISABLE_WIFI), |
| Dianne Hackborn | 617f877 | 2009-03-31 15:04:46 -0700 | [diff] [blame] | 1735 | (persist ? 1 : 0), uid); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1736 | msg.sendToTarget(); |
| 1737 | } |
| 1738 | |
| 1739 | private void sendStartMessage(boolean scanOnlyMode) { |
| 1740 | Message.obtain(mWifiHandler, MESSAGE_START_WIFI, scanOnlyMode ? 1 : 0, 0).sendToTarget(); |
| 1741 | } |
| 1742 | |
| Irfan Sheriff | 5321aef | 2010-02-12 12:35:59 -0800 | [diff] [blame] | 1743 | private void sendAccessPointMessage(boolean enable, WifiConfiguration wifiConfig, int uid) { |
| 1744 | Message.obtain(mWifiHandler, |
| 1745 | (enable ? MESSAGE_START_ACCESS_POINT : MESSAGE_STOP_ACCESS_POINT), |
| Irfan Sheriff | 9ab518ad | 2010-03-12 15:48:17 -0800 | [diff] [blame] | 1746 | uid, 0, wifiConfig).sendToTarget(); |
| Irfan Sheriff | 5321aef | 2010-02-12 12:35:59 -0800 | [diff] [blame] | 1747 | } |
| 1748 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1749 | private void updateWifiState() { |
| Robert Greenwalt | f75aa36fc | 2009-10-22 17:03:47 -0700 | [diff] [blame] | 1750 | // send a message so it's all serialized |
| 1751 | Message.obtain(mWifiHandler, MESSAGE_UPDATE_STATE, 0, 0).sendToTarget(); |
| 1752 | } |
| 1753 | |
| 1754 | private void doUpdateWifiState() { |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1755 | boolean wifiEnabled = getPersistedWifiEnabled(); |
| Mike Lockwood | bd5ddf0 | 2009-07-29 21:37:14 -0700 | [diff] [blame] | 1756 | boolean airplaneMode = isAirplaneModeOn() && !mAirplaneModeOverwridden; |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1757 | boolean lockHeld = mLocks.hasLocks(); |
| 1758 | int strongestLockMode; |
| 1759 | boolean wifiShouldBeEnabled = wifiEnabled && !airplaneMode; |
| 1760 | boolean wifiShouldBeStarted = !mDeviceIdle || lockHeld; |
| 1761 | if (mDeviceIdle && lockHeld) { |
| 1762 | strongestLockMode = mLocks.getStrongestLockMode(); |
| 1763 | } else { |
| 1764 | strongestLockMode = WifiManager.WIFI_MODE_FULL; |
| 1765 | } |
| 1766 | |
| 1767 | synchronized (mWifiHandler) { |
| Irfan Sheriff | 0f34406009 | 2010-03-10 10:05:51 -0800 | [diff] [blame] | 1768 | if ((mWifiStateTracker.getWifiState() == WIFI_STATE_ENABLING) && !airplaneMode) { |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1769 | return; |
| 1770 | } |
| 1771 | if (wifiShouldBeEnabled) { |
| 1772 | if (wifiShouldBeStarted) { |
| 1773 | sWakeLock.acquire(); |
| Dianne Hackborn | 617f877 | 2009-03-31 15:04:46 -0700 | [diff] [blame] | 1774 | sendEnableMessage(true, false, mLastEnableUid); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1775 | sWakeLock.acquire(); |
| 1776 | sendStartMessage(strongestLockMode == WifiManager.WIFI_MODE_SCAN_ONLY); |
| Irfan Sheriff | 3bf504d | 2010-03-23 12:24:33 -0700 | [diff] [blame] | 1777 | } else if (!mWifiStateTracker.isDriverStopped()) { |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1778 | int wakeLockTimeout = |
| 1779 | Settings.Secure.getInt( |
| 1780 | mContext.getContentResolver(), |
| 1781 | Settings.Secure.WIFI_MOBILE_DATA_TRANSITION_WAKELOCK_TIMEOUT_MS, |
| 1782 | DEFAULT_WAKELOCK_TIMEOUT); |
| 1783 | /* |
| Irfan Sheriff | 3bf504d | 2010-03-23 12:24:33 -0700 | [diff] [blame] | 1784 | * We are assuming that ConnectivityService can make |
| 1785 | * a transition to cellular data within wakeLockTimeout time. |
| 1786 | * The wakelock is released by the delayed message. |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1787 | */ |
| 1788 | sDriverStopWakeLock.acquire(); |
| 1789 | mWifiHandler.sendEmptyMessage(MESSAGE_STOP_WIFI); |
| 1790 | mWifiHandler.sendEmptyMessageDelayed(MESSAGE_RELEASE_WAKELOCK, wakeLockTimeout); |
| 1791 | } |
| 1792 | } else { |
| 1793 | sWakeLock.acquire(); |
| Dianne Hackborn | 617f877 | 2009-03-31 15:04:46 -0700 | [diff] [blame] | 1794 | sendEnableMessage(false, false, mLastEnableUid); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1795 | } |
| 1796 | } |
| 1797 | } |
| 1798 | |
| 1799 | private void registerForBroadcasts() { |
| 1800 | IntentFilter intentFilter = new IntentFilter(); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1801 | intentFilter.addAction(Intent.ACTION_SCREEN_ON); |
| 1802 | intentFilter.addAction(Intent.ACTION_SCREEN_OFF); |
| 1803 | intentFilter.addAction(Intent.ACTION_BATTERY_CHANGED); |
| 1804 | intentFilter.addAction(ACTION_DEVICE_IDLE); |
| Nick Pelly | 005b228 | 2009-09-10 10:21:56 -0700 | [diff] [blame] | 1805 | intentFilter.addAction(BluetoothA2dp.ACTION_SINK_STATE_CHANGED); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1806 | mContext.registerReceiver(mReceiver, intentFilter); |
| 1807 | } |
| Jaikumar Ganesh | 084c665 | 2009-12-07 10:58:18 -0800 | [diff] [blame] | 1808 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1809 | private boolean isAirplaneSensitive() { |
| 1810 | String airplaneModeRadios = Settings.System.getString(mContext.getContentResolver(), |
| 1811 | Settings.System.AIRPLANE_MODE_RADIOS); |
| 1812 | return airplaneModeRadios == null |
| 1813 | || airplaneModeRadios.contains(Settings.System.RADIO_WIFI); |
| 1814 | } |
| 1815 | |
| Mike Lockwood | bd5ddf0 | 2009-07-29 21:37:14 -0700 | [diff] [blame] | 1816 | private boolean isAirplaneToggleable() { |
| 1817 | String toggleableRadios = Settings.System.getString(mContext.getContentResolver(), |
| 1818 | Settings.System.AIRPLANE_MODE_TOGGLEABLE_RADIOS); |
| 1819 | return toggleableRadios != null |
| 1820 | && toggleableRadios.contains(Settings.System.RADIO_WIFI); |
| 1821 | } |
| 1822 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1823 | /** |
| 1824 | * Returns true if Wi-Fi is sensitive to airplane mode, and airplane mode is |
| 1825 | * currently on. |
| 1826 | * @return {@code true} if airplane mode is on. |
| 1827 | */ |
| 1828 | private boolean isAirplaneModeOn() { |
| 1829 | return isAirplaneSensitive() && Settings.System.getInt(mContext.getContentResolver(), |
| 1830 | Settings.System.AIRPLANE_MODE_ON, 0) == 1; |
| 1831 | } |
| 1832 | |
| 1833 | /** |
| 1834 | * Handler that allows posting to the WifiThread. |
| 1835 | */ |
| 1836 | private class WifiHandler extends Handler { |
| 1837 | public WifiHandler(Looper looper) { |
| 1838 | super(looper); |
| 1839 | } |
| 1840 | |
| 1841 | @Override |
| 1842 | public void handleMessage(Message msg) { |
| 1843 | switch (msg.what) { |
| 1844 | |
| 1845 | case MESSAGE_ENABLE_WIFI: |
| Irfan Sheriff | 7b00978 | 2010-03-11 16:37:45 -0800 | [diff] [blame] | 1846 | if (mWifiWatchdogService == null) { |
| 1847 | mWifiWatchdogService = new WifiWatchdogService(mContext, mWifiStateTracker); |
| 1848 | } |
| Dianne Hackborn | 617f877 | 2009-03-31 15:04:46 -0700 | [diff] [blame] | 1849 | setWifiEnabledBlocking(true, msg.arg1 == 1, msg.arg2); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1850 | sWakeLock.release(); |
| 1851 | break; |
| 1852 | |
| 1853 | case MESSAGE_START_WIFI: |
| 1854 | mWifiStateTracker.setScanOnlyMode(msg.arg1 != 0); |
| 1855 | mWifiStateTracker.restart(); |
| 1856 | sWakeLock.release(); |
| 1857 | break; |
| 1858 | |
| Robert Greenwalt | f75aa36fc | 2009-10-22 17:03:47 -0700 | [diff] [blame] | 1859 | case MESSAGE_UPDATE_STATE: |
| 1860 | doUpdateWifiState(); |
| 1861 | break; |
| 1862 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1863 | case MESSAGE_DISABLE_WIFI: |
| 1864 | // a non-zero msg.arg1 value means the "enabled" setting |
| 1865 | // should be persisted |
| Dianne Hackborn | 617f877 | 2009-03-31 15:04:46 -0700 | [diff] [blame] | 1866 | setWifiEnabledBlocking(false, msg.arg1 == 1, msg.arg2); |
| Irfan Sheriff | 7b00978 | 2010-03-11 16:37:45 -0800 | [diff] [blame] | 1867 | if (mWifiWatchdogService != null) { |
| 1868 | mWifiWatchdogService.quit(); |
| 1869 | mWifiWatchdogService = null; |
| 1870 | } |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1871 | sWakeLock.release(); |
| 1872 | break; |
| 1873 | |
| 1874 | case MESSAGE_STOP_WIFI: |
| 1875 | mWifiStateTracker.disconnectAndStop(); |
| 1876 | // don't release wakelock |
| 1877 | break; |
| 1878 | |
| 1879 | case MESSAGE_RELEASE_WAKELOCK: |
| Irfan Sheriff | 3bf504d | 2010-03-23 12:24:33 -0700 | [diff] [blame] | 1880 | sDriverStopWakeLock.release(); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1881 | break; |
| Irfan Sheriff | 5321aef | 2010-02-12 12:35:59 -0800 | [diff] [blame] | 1882 | |
| 1883 | case MESSAGE_START_ACCESS_POINT: |
| 1884 | setWifiApEnabledBlocking(true, |
| Irfan Sheriff | 9ab518ad | 2010-03-12 15:48:17 -0800 | [diff] [blame] | 1885 | msg.arg1, |
| Irfan Sheriff | 5321aef | 2010-02-12 12:35:59 -0800 | [diff] [blame] | 1886 | (WifiConfiguration) msg.obj); |
| Irfan Sheriff | 80cb598 | 2010-03-19 10:40:18 -0700 | [diff] [blame] | 1887 | sWakeLock.release(); |
| Irfan Sheriff | 5321aef | 2010-02-12 12:35:59 -0800 | [diff] [blame] | 1888 | break; |
| 1889 | |
| 1890 | case MESSAGE_STOP_ACCESS_POINT: |
| 1891 | setWifiApEnabledBlocking(false, |
| Irfan Sheriff | 9ab518ad | 2010-03-12 15:48:17 -0800 | [diff] [blame] | 1892 | msg.arg1, |
| Irfan Sheriff | 5321aef | 2010-02-12 12:35:59 -0800 | [diff] [blame] | 1893 | (WifiConfiguration) msg.obj); |
| 1894 | sWakeLock.release(); |
| 1895 | break; |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1896 | } |
| 1897 | } |
| 1898 | } |
| 1899 | |
| 1900 | @Override |
| 1901 | protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) { |
| 1902 | if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.DUMP) |
| 1903 | != PackageManager.PERMISSION_GRANTED) { |
| 1904 | pw.println("Permission Denial: can't dump WifiService from from pid=" |
| 1905 | + Binder.getCallingPid() |
| 1906 | + ", uid=" + Binder.getCallingUid()); |
| 1907 | return; |
| 1908 | } |
| Irfan Sheriff | 0f34406009 | 2010-03-10 10:05:51 -0800 | [diff] [blame] | 1909 | pw.println("Wi-Fi is " + stateName(mWifiStateTracker.getWifiState())); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1910 | pw.println("Stay-awake conditions: " + |
| 1911 | Settings.System.getInt(mContext.getContentResolver(), |
| 1912 | Settings.System.STAY_ON_WHILE_PLUGGED_IN, 0)); |
| 1913 | pw.println(); |
| 1914 | |
| 1915 | pw.println("Internal state:"); |
| 1916 | pw.println(mWifiStateTracker); |
| 1917 | pw.println(); |
| 1918 | pw.println("Latest scan results:"); |
| 1919 | List<ScanResult> scanResults = mWifiStateTracker.getScanResultsList(); |
| 1920 | if (scanResults != null && scanResults.size() != 0) { |
| 1921 | pw.println(" BSSID Frequency RSSI Flags SSID"); |
| 1922 | for (ScanResult r : scanResults) { |
| 1923 | pw.printf(" %17s %9d %5d %-16s %s%n", |
| 1924 | r.BSSID, |
| 1925 | r.frequency, |
| 1926 | r.level, |
| 1927 | r.capabilities, |
| 1928 | r.SSID == null ? "" : r.SSID); |
| 1929 | } |
| 1930 | } |
| 1931 | pw.println(); |
| Eric Shienbrood | 5711fad | 2009-03-27 20:25:31 -0700 | [diff] [blame] | 1932 | pw.println("Locks acquired: " + mFullLocksAcquired + " full, " + |
| 1933 | mScanLocksAcquired + " scan"); |
| 1934 | pw.println("Locks released: " + mFullLocksReleased + " full, " + |
| 1935 | mScanLocksReleased + " scan"); |
| 1936 | pw.println(); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1937 | pw.println("Locks held:"); |
| 1938 | mLocks.dump(pw); |
| 1939 | } |
| 1940 | |
| 1941 | private static String stateName(int wifiState) { |
| 1942 | switch (wifiState) { |
| 1943 | case WIFI_STATE_DISABLING: |
| 1944 | return "disabling"; |
| 1945 | case WIFI_STATE_DISABLED: |
| 1946 | return "disabled"; |
| 1947 | case WIFI_STATE_ENABLING: |
| 1948 | return "enabling"; |
| 1949 | case WIFI_STATE_ENABLED: |
| 1950 | return "enabled"; |
| 1951 | case WIFI_STATE_UNKNOWN: |
| 1952 | return "unknown state"; |
| 1953 | default: |
| 1954 | return "[invalid state]"; |
| 1955 | } |
| 1956 | } |
| 1957 | |
| Robert Greenwalt | 58ff021 | 2009-05-19 15:53:54 -0700 | [diff] [blame] | 1958 | private class WifiLock extends DeathRecipient { |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1959 | WifiLock(int lockMode, String tag, IBinder binder) { |
| Robert Greenwalt | 5347bd4 | 2009-05-13 15:10:16 -0700 | [diff] [blame] | 1960 | super(lockMode, tag, binder); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1961 | } |
| 1962 | |
| 1963 | public void binderDied() { |
| 1964 | synchronized (mLocks) { |
| 1965 | releaseWifiLockLocked(mBinder); |
| 1966 | } |
| 1967 | } |
| 1968 | |
| 1969 | public String toString() { |
| Robert Greenwalt | 5347bd4 | 2009-05-13 15:10:16 -0700 | [diff] [blame] | 1970 | return "WifiLock{" + mTag + " type=" + mMode + " binder=" + mBinder + "}"; |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1971 | } |
| 1972 | } |
| 1973 | |
| 1974 | private class LockList { |
| 1975 | private List<WifiLock> mList; |
| 1976 | |
| 1977 | private LockList() { |
| 1978 | mList = new ArrayList<WifiLock>(); |
| 1979 | } |
| 1980 | |
| 1981 | private synchronized boolean hasLocks() { |
| 1982 | return !mList.isEmpty(); |
| 1983 | } |
| 1984 | |
| 1985 | private synchronized int getStrongestLockMode() { |
| 1986 | if (mList.isEmpty()) { |
| 1987 | return WifiManager.WIFI_MODE_FULL; |
| 1988 | } |
| 1989 | for (WifiLock l : mList) { |
| Robert Greenwalt | 5347bd4 | 2009-05-13 15:10:16 -0700 | [diff] [blame] | 1990 | if (l.mMode == WifiManager.WIFI_MODE_FULL) { |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1991 | return WifiManager.WIFI_MODE_FULL; |
| 1992 | } |
| 1993 | } |
| 1994 | return WifiManager.WIFI_MODE_SCAN_ONLY; |
| 1995 | } |
| 1996 | |
| 1997 | private void addLock(WifiLock lock) { |
| 1998 | if (findLockByBinder(lock.mBinder) < 0) { |
| 1999 | mList.add(lock); |
| 2000 | } |
| 2001 | } |
| 2002 | |
| 2003 | private WifiLock removeLock(IBinder binder) { |
| 2004 | int index = findLockByBinder(binder); |
| 2005 | if (index >= 0) { |
| Suchi Amalapurapu | fff2fda | 2009-06-30 21:36:16 -0700 | [diff] [blame] | 2006 | WifiLock ret = mList.remove(index); |
| 2007 | ret.unlinkDeathRecipient(); |
| 2008 | return ret; |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2009 | } else { |
| 2010 | return null; |
| 2011 | } |
| 2012 | } |
| 2013 | |
| 2014 | private int findLockByBinder(IBinder binder) { |
| 2015 | int size = mList.size(); |
| 2016 | for (int i = size - 1; i >= 0; i--) |
| 2017 | if (mList.get(i).mBinder == binder) |
| 2018 | return i; |
| 2019 | return -1; |
| 2020 | } |
| 2021 | |
| 2022 | private void dump(PrintWriter pw) { |
| 2023 | for (WifiLock l : mList) { |
| 2024 | pw.print(" "); |
| 2025 | pw.println(l); |
| 2026 | } |
| 2027 | } |
| 2028 | } |
| 2029 | |
| 2030 | public boolean acquireWifiLock(IBinder binder, int lockMode, String tag) { |
| 2031 | mContext.enforceCallingOrSelfPermission(android.Manifest.permission.WAKE_LOCK, null); |
| 2032 | if (lockMode != WifiManager.WIFI_MODE_FULL && lockMode != WifiManager.WIFI_MODE_SCAN_ONLY) { |
| 2033 | return false; |
| 2034 | } |
| 2035 | WifiLock wifiLock = new WifiLock(lockMode, tag, binder); |
| 2036 | synchronized (mLocks) { |
| 2037 | return acquireWifiLockLocked(wifiLock); |
| 2038 | } |
| 2039 | } |
| 2040 | |
| 2041 | private boolean acquireWifiLockLocked(WifiLock wifiLock) { |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 2042 | Slog.d(TAG, "acquireWifiLockLocked: " + wifiLock); |
| Robert Greenwalt | f1acb2d | 2009-10-13 08:20:55 -0700 | [diff] [blame] | 2043 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2044 | mLocks.addLock(wifiLock); |
| Robert Greenwalt | f1acb2d | 2009-10-13 08:20:55 -0700 | [diff] [blame] | 2045 | |
| The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 2046 | int uid = Binder.getCallingUid(); |
| 2047 | long ident = Binder.clearCallingIdentity(); |
| 2048 | try { |
| Robert Greenwalt | 5347bd4 | 2009-05-13 15:10:16 -0700 | [diff] [blame] | 2049 | switch(wifiLock.mMode) { |
| Eric Shienbrood | 5711fad | 2009-03-27 20:25:31 -0700 | [diff] [blame] | 2050 | case WifiManager.WIFI_MODE_FULL: |
| 2051 | ++mFullLocksAcquired; |
| 2052 | mBatteryStats.noteFullWifiLockAcquired(uid); |
| 2053 | break; |
| 2054 | case WifiManager.WIFI_MODE_SCAN_ONLY: |
| 2055 | ++mScanLocksAcquired; |
| 2056 | mBatteryStats.noteScanWifiLockAcquired(uid); |
| 2057 | break; |
| The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 2058 | } |
| 2059 | } catch (RemoteException e) { |
| 2060 | } finally { |
| 2061 | Binder.restoreCallingIdentity(ident); |
| 2062 | } |
| Robert Greenwalt | f1acb2d | 2009-10-13 08:20:55 -0700 | [diff] [blame] | 2063 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2064 | updateWifiState(); |
| 2065 | return true; |
| 2066 | } |
| 2067 | |
| 2068 | public boolean releaseWifiLock(IBinder lock) { |
| 2069 | mContext.enforceCallingOrSelfPermission(android.Manifest.permission.WAKE_LOCK, null); |
| 2070 | synchronized (mLocks) { |
| 2071 | return releaseWifiLockLocked(lock); |
| 2072 | } |
| 2073 | } |
| 2074 | |
| 2075 | private boolean releaseWifiLockLocked(IBinder lock) { |
| Eric Shienbrood | d4c5f89 | 2009-03-24 18:13:20 -0700 | [diff] [blame] | 2076 | boolean hadLock; |
| Robert Greenwalt | f1acb2d | 2009-10-13 08:20:55 -0700 | [diff] [blame] | 2077 | |
| The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 2078 | WifiLock wifiLock = mLocks.removeLock(lock); |
| Robert Greenwalt | f1acb2d | 2009-10-13 08:20:55 -0700 | [diff] [blame] | 2079 | |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 2080 | Slog.d(TAG, "releaseWifiLockLocked: " + wifiLock); |
| Robert Greenwalt | f1acb2d | 2009-10-13 08:20:55 -0700 | [diff] [blame] | 2081 | |
| Eric Shienbrood | d4c5f89 | 2009-03-24 18:13:20 -0700 | [diff] [blame] | 2082 | hadLock = (wifiLock != null); |
| 2083 | |
| 2084 | if (hadLock) { |
| 2085 | int uid = Binder.getCallingUid(); |
| 2086 | long ident = Binder.clearCallingIdentity(); |
| 2087 | try { |
| Robert Greenwalt | 5347bd4 | 2009-05-13 15:10:16 -0700 | [diff] [blame] | 2088 | switch(wifiLock.mMode) { |
| Eric Shienbrood | 5711fad | 2009-03-27 20:25:31 -0700 | [diff] [blame] | 2089 | case WifiManager.WIFI_MODE_FULL: |
| 2090 | ++mFullLocksReleased; |
| 2091 | mBatteryStats.noteFullWifiLockReleased(uid); |
| 2092 | break; |
| 2093 | case WifiManager.WIFI_MODE_SCAN_ONLY: |
| 2094 | ++mScanLocksReleased; |
| 2095 | mBatteryStats.noteScanWifiLockReleased(uid); |
| 2096 | break; |
| Eric Shienbrood | d4c5f89 | 2009-03-24 18:13:20 -0700 | [diff] [blame] | 2097 | } |
| 2098 | } catch (RemoteException e) { |
| 2099 | } finally { |
| 2100 | Binder.restoreCallingIdentity(ident); |
| The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 2101 | } |
| The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 2102 | } |
| Robert Greenwalt | f1acb2d | 2009-10-13 08:20:55 -0700 | [diff] [blame] | 2103 | // TODO - should this only happen if you hadLock? |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2104 | updateWifiState(); |
| Eric Shienbrood | d4c5f89 | 2009-03-24 18:13:20 -0700 | [diff] [blame] | 2105 | return hadLock; |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2106 | } |
| Robert Greenwalt | 5347bd4 | 2009-05-13 15:10:16 -0700 | [diff] [blame] | 2107 | |
| Robert Greenwalt | 58ff021 | 2009-05-19 15:53:54 -0700 | [diff] [blame] | 2108 | private abstract class DeathRecipient |
| Robert Greenwalt | 5347bd4 | 2009-05-13 15:10:16 -0700 | [diff] [blame] | 2109 | implements IBinder.DeathRecipient { |
| 2110 | String mTag; |
| 2111 | int mMode; |
| 2112 | IBinder mBinder; |
| 2113 | |
| Robert Greenwalt | 58ff021 | 2009-05-19 15:53:54 -0700 | [diff] [blame] | 2114 | DeathRecipient(int mode, String tag, IBinder binder) { |
| Robert Greenwalt | 5347bd4 | 2009-05-13 15:10:16 -0700 | [diff] [blame] | 2115 | super(); |
| 2116 | mTag = tag; |
| 2117 | mMode = mode; |
| 2118 | mBinder = binder; |
| 2119 | try { |
| 2120 | mBinder.linkToDeath(this, 0); |
| 2121 | } catch (RemoteException e) { |
| 2122 | binderDied(); |
| 2123 | } |
| 2124 | } |
| Suchi Amalapurapu | fff2fda | 2009-06-30 21:36:16 -0700 | [diff] [blame] | 2125 | |
| 2126 | void unlinkDeathRecipient() { |
| 2127 | mBinder.unlinkToDeath(this, 0); |
| 2128 | } |
| Robert Greenwalt | 5347bd4 | 2009-05-13 15:10:16 -0700 | [diff] [blame] | 2129 | } |
| 2130 | |
| Robert Greenwalt | 58ff021 | 2009-05-19 15:53:54 -0700 | [diff] [blame] | 2131 | private class Multicaster extends DeathRecipient { |
| 2132 | Multicaster(String tag, IBinder binder) { |
| Robert Greenwalt | 5347bd4 | 2009-05-13 15:10:16 -0700 | [diff] [blame] | 2133 | super(Binder.getCallingUid(), tag, binder); |
| 2134 | } |
| 2135 | |
| 2136 | public void binderDied() { |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 2137 | Slog.e(TAG, "Multicaster binderDied"); |
| Robert Greenwalt | 5347bd4 | 2009-05-13 15:10:16 -0700 | [diff] [blame] | 2138 | synchronized (mMulticasters) { |
| 2139 | int i = mMulticasters.indexOf(this); |
| 2140 | if (i != -1) { |
| 2141 | removeMulticasterLocked(i, mMode); |
| 2142 | } |
| 2143 | } |
| 2144 | } |
| 2145 | |
| 2146 | public String toString() { |
| Robert Greenwalt | 58ff021 | 2009-05-19 15:53:54 -0700 | [diff] [blame] | 2147 | return "Multicaster{" + mTag + " binder=" + mBinder + "}"; |
| Robert Greenwalt | 5347bd4 | 2009-05-13 15:10:16 -0700 | [diff] [blame] | 2148 | } |
| 2149 | |
| 2150 | public int getUid() { |
| 2151 | return mMode; |
| 2152 | } |
| 2153 | } |
| 2154 | |
| Robert Greenwalt | e2d155a | 2009-10-21 14:58:34 -0700 | [diff] [blame] | 2155 | public void initializeMulticastFiltering() { |
| 2156 | enforceMulticastChangePermission(); |
| Irfan Sheriff | a8fbe1f | 2010-03-09 09:13:58 -0800 | [diff] [blame] | 2157 | |
| Robert Greenwalt | e2d155a | 2009-10-21 14:58:34 -0700 | [diff] [blame] | 2158 | synchronized (mMulticasters) { |
| 2159 | // if anybody had requested filters be off, leave off |
| 2160 | if (mMulticasters.size() != 0) { |
| 2161 | return; |
| 2162 | } else { |
| Irfan Sheriff | a8fbe1f | 2010-03-09 09:13:58 -0800 | [diff] [blame] | 2163 | mWifiStateTracker.startPacketFiltering(); |
| Robert Greenwalt | e2d155a | 2009-10-21 14:58:34 -0700 | [diff] [blame] | 2164 | } |
| 2165 | } |
| 2166 | } |
| 2167 | |
| Robert Greenwalt | fc1b15c | 2009-05-22 15:09:51 -0700 | [diff] [blame] | 2168 | public void acquireMulticastLock(IBinder binder, String tag) { |
| 2169 | enforceMulticastChangePermission(); |
| Robert Greenwalt | 5347bd4 | 2009-05-13 15:10:16 -0700 | [diff] [blame] | 2170 | |
| 2171 | synchronized (mMulticasters) { |
| 2172 | mMulticastEnabled++; |
| Robert Greenwalt | 58ff021 | 2009-05-19 15:53:54 -0700 | [diff] [blame] | 2173 | mMulticasters.add(new Multicaster(tag, binder)); |
| Robert Greenwalt | 5347bd4 | 2009-05-13 15:10:16 -0700 | [diff] [blame] | 2174 | // Note that we could call stopPacketFiltering only when |
| 2175 | // our new size == 1 (first call), but this function won't |
| 2176 | // be called often and by making the stopPacket call each |
| 2177 | // time we're less fragile and self-healing. |
| Irfan Sheriff | a8fbe1f | 2010-03-09 09:13:58 -0800 | [diff] [blame] | 2178 | mWifiStateTracker.stopPacketFiltering(); |
| Robert Greenwalt | 5347bd4 | 2009-05-13 15:10:16 -0700 | [diff] [blame] | 2179 | } |
| 2180 | |
| 2181 | int uid = Binder.getCallingUid(); |
| 2182 | Long ident = Binder.clearCallingIdentity(); |
| 2183 | try { |
| 2184 | mBatteryStats.noteWifiMulticastEnabled(uid); |
| 2185 | } catch (RemoteException e) { |
| 2186 | } finally { |
| 2187 | Binder.restoreCallingIdentity(ident); |
| 2188 | } |
| 2189 | } |
| 2190 | |
| Robert Greenwalt | fc1b15c | 2009-05-22 15:09:51 -0700 | [diff] [blame] | 2191 | public void releaseMulticastLock() { |
| 2192 | enforceMulticastChangePermission(); |
| Robert Greenwalt | 5347bd4 | 2009-05-13 15:10:16 -0700 | [diff] [blame] | 2193 | |
| 2194 | int uid = Binder.getCallingUid(); |
| 2195 | synchronized (mMulticasters) { |
| 2196 | mMulticastDisabled++; |
| 2197 | int size = mMulticasters.size(); |
| 2198 | for (int i = size - 1; i >= 0; i--) { |
| Robert Greenwalt | 58ff021 | 2009-05-19 15:53:54 -0700 | [diff] [blame] | 2199 | Multicaster m = mMulticasters.get(i); |
| Robert Greenwalt | 5347bd4 | 2009-05-13 15:10:16 -0700 | [diff] [blame] | 2200 | if ((m != null) && (m.getUid() == uid)) { |
| 2201 | removeMulticasterLocked(i, uid); |
| 2202 | } |
| 2203 | } |
| 2204 | } |
| 2205 | } |
| 2206 | |
| 2207 | private void removeMulticasterLocked(int i, int uid) |
| 2208 | { |
| Suchi Amalapurapu | fff2fda | 2009-06-30 21:36:16 -0700 | [diff] [blame] | 2209 | Multicaster removed = mMulticasters.remove(i); |
| Irfan Sheriff | a8fbe1f | 2010-03-09 09:13:58 -0800 | [diff] [blame] | 2210 | |
| Suchi Amalapurapu | fff2fda | 2009-06-30 21:36:16 -0700 | [diff] [blame] | 2211 | if (removed != null) { |
| 2212 | removed.unlinkDeathRecipient(); |
| 2213 | } |
| Robert Greenwalt | 5347bd4 | 2009-05-13 15:10:16 -0700 | [diff] [blame] | 2214 | if (mMulticasters.size() == 0) { |
| Irfan Sheriff | a8fbe1f | 2010-03-09 09:13:58 -0800 | [diff] [blame] | 2215 | mWifiStateTracker.startPacketFiltering(); |
| Robert Greenwalt | 5347bd4 | 2009-05-13 15:10:16 -0700 | [diff] [blame] | 2216 | } |
| 2217 | |
| 2218 | Long ident = Binder.clearCallingIdentity(); |
| 2219 | try { |
| 2220 | mBatteryStats.noteWifiMulticastDisabled(uid); |
| 2221 | } catch (RemoteException e) { |
| 2222 | } finally { |
| 2223 | Binder.restoreCallingIdentity(ident); |
| 2224 | } |
| 2225 | } |
| 2226 | |
| Robert Greenwalt | 58ff021 | 2009-05-19 15:53:54 -0700 | [diff] [blame] | 2227 | public boolean isMulticastEnabled() { |
| Robert Greenwalt | 5347bd4 | 2009-05-13 15:10:16 -0700 | [diff] [blame] | 2228 | enforceAccessPermission(); |
| 2229 | |
| 2230 | synchronized (mMulticasters) { |
| 2231 | return (mMulticasters.size() > 0); |
| 2232 | } |
| 2233 | } |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2234 | } |