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