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