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