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