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 android.net; |
| 18 | |
| 19 | import android.annotation.SdkConstant; |
| 20 | import android.annotation.SdkConstant.SdkConstantType; |
Robert Greenwalt | 42acef3 | 2009-08-12 16:08:25 -0700 | [diff] [blame] | 21 | import android.os.Binder; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 22 | import android.os.RemoteException; |
| 23 | |
| 24 | /** |
| 25 | * Class that answers queries about the state of network connectivity. It also |
| 26 | * notifies applications when network connectivity changes. Get an instance |
| 27 | * of this class by calling |
| 28 | * {@link android.content.Context#getSystemService(String) Context.getSystemService(Context.CONNECTIVITY_SERVICE)}. |
| 29 | * <p> |
| 30 | * The primary responsibilities of this class are to: |
| 31 | * <ol> |
| 32 | * <li>Monitor network connections (Wi-Fi, GPRS, UMTS, etc.)</li> |
| 33 | * <li>Send broadcast intents when network connectivity changes</li> |
| 34 | * <li>Attempt to "fail over" to another network when connectivity to a network |
| 35 | * is lost</li> |
| 36 | * <li>Provide an API that allows applications to query the coarse-grained or fine-grained |
| 37 | * state of the available networks</li> |
| 38 | * </ol> |
| 39 | */ |
| 40 | public class ConnectivityManager |
| 41 | { |
| 42 | /** |
| 43 | * A change in network connectivity has occurred. A connection has either |
| 44 | * been established or lost. The NetworkInfo for the affected network is |
| 45 | * sent as an extra; it should be consulted to see what kind of |
| 46 | * connectivity event occurred. |
| 47 | * <p/> |
| 48 | * If this is a connection that was the result of failing over from a |
| 49 | * disconnected network, then the FAILOVER_CONNECTION boolean extra is |
| 50 | * set to true. |
| 51 | * <p/> |
| 52 | * For a loss of connectivity, if the connectivity manager is attempting |
| 53 | * to connect (or has already connected) to another network, the |
| 54 | * NetworkInfo for the new network is also passed as an extra. This lets |
| 55 | * any receivers of the broadcast know that they should not necessarily |
| 56 | * tell the user that no data traffic will be possible. Instead, the |
| 57 | * reciever should expect another broadcast soon, indicating either that |
| 58 | * the failover attempt succeeded (and so there is still overall data |
| 59 | * connectivity), or that the failover attempt failed, meaning that all |
| 60 | * connectivity has been lost. |
| 61 | * <p/> |
| 62 | * For a disconnect event, the boolean extra EXTRA_NO_CONNECTIVITY |
| 63 | * is set to {@code true} if there are no connected networks at all. |
| 64 | */ |
| 65 | public static final String CONNECTIVITY_ACTION = "android.net.conn.CONNECTIVITY_CHANGE"; |
| 66 | /** |
| 67 | * The lookup key for a {@link NetworkInfo} object. Retrieve with |
| 68 | * {@link android.content.Intent#getParcelableExtra(String)}. |
| 69 | */ |
| 70 | public static final String EXTRA_NETWORK_INFO = "networkInfo"; |
| 71 | /** |
| 72 | * The lookup key for a boolean that indicates whether a connect event |
| 73 | * is for a network to which the connectivity manager was failing over |
| 74 | * following a disconnect on another network. |
| 75 | * Retrieve it with {@link android.content.Intent#getBooleanExtra(String,boolean)}. |
| 76 | */ |
| 77 | public static final String EXTRA_IS_FAILOVER = "isFailover"; |
| 78 | /** |
| 79 | * The lookup key for a {@link NetworkInfo} object. This is supplied when |
| 80 | * there is another network that it may be possible to connect to. Retrieve with |
| 81 | * {@link android.content.Intent#getParcelableExtra(String)}. |
| 82 | */ |
| 83 | public static final String EXTRA_OTHER_NETWORK_INFO = "otherNetwork"; |
| 84 | /** |
| 85 | * The lookup key for a boolean that indicates whether there is a |
| 86 | * complete lack of connectivity, i.e., no network is available. |
| 87 | * Retrieve it with {@link android.content.Intent#getBooleanExtra(String,boolean)}. |
| 88 | */ |
| 89 | public static final String EXTRA_NO_CONNECTIVITY = "noConnectivity"; |
| 90 | /** |
| 91 | * The lookup key for a string that indicates why an attempt to connect |
| 92 | * to a network failed. The string has no particular structure. It is |
| 93 | * intended to be used in notifications presented to users. Retrieve |
| 94 | * it with {@link android.content.Intent#getStringExtra(String)}. |
| 95 | */ |
| 96 | public static final String EXTRA_REASON = "reason"; |
| 97 | /** |
| 98 | * The lookup key for a string that provides optionally supplied |
| 99 | * extra information about the network state. The information |
| 100 | * may be passed up from the lower networking layers, and its |
| 101 | * meaning may be specific to a particular network type. Retrieve |
| 102 | * it with {@link android.content.Intent#getStringExtra(String)}. |
| 103 | */ |
| 104 | public static final String EXTRA_EXTRA_INFO = "extraInfo"; |
| 105 | |
| 106 | /** |
| 107 | * Broadcast Action: The setting for background data usage has changed |
| 108 | * values. Use {@link #getBackgroundDataSetting()} to get the current value. |
| 109 | * <p> |
| 110 | * If an application uses the network in the background, it should listen |
| 111 | * for this broadcast and stop using the background data if the value is |
| 112 | * false. |
| 113 | */ |
| 114 | @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION) |
| 115 | public static final String ACTION_BACKGROUND_DATA_SETTING_CHANGED = |
| 116 | "android.net.conn.BACKGROUND_DATA_SETTING_CHANGED"; |
| 117 | |
Robert Greenwalt | 42acef3 | 2009-08-12 16:08:25 -0700 | [diff] [blame] | 118 | /** |
Robert Greenwalt | d0e18ff | 2010-01-26 11:40:34 -0800 | [diff] [blame] | 119 | * Broadcast Action: A tetherable connection has come or gone |
| 120 | * TODO - finish the doc |
| 121 | * @hide |
| 122 | */ |
| 123 | public static final String ACTION_TETHER_STATE_CHANGED = |
| 124 | "android.net.conn.TETHER_STATE_CHANGED"; |
| 125 | |
| 126 | /** |
| 127 | * @hide |
Robert Greenwalt | 2a091d7 | 2010-02-11 18:18:40 -0800 | [diff] [blame^] | 128 | * gives a String[] |
Robert Greenwalt | d0e18ff | 2010-01-26 11:40:34 -0800 | [diff] [blame] | 129 | */ |
Robert Greenwalt | 2a091d7 | 2010-02-11 18:18:40 -0800 | [diff] [blame^] | 130 | public static final String EXTRA_AVAILABLE_TETHER = "availableArray"; |
Robert Greenwalt | d0e18ff | 2010-01-26 11:40:34 -0800 | [diff] [blame] | 131 | |
| 132 | /** |
| 133 | * @hide |
Robert Greenwalt | 2a091d7 | 2010-02-11 18:18:40 -0800 | [diff] [blame^] | 134 | * gives a String[] |
Robert Greenwalt | d0e18ff | 2010-01-26 11:40:34 -0800 | [diff] [blame] | 135 | */ |
Robert Greenwalt | 2a091d7 | 2010-02-11 18:18:40 -0800 | [diff] [blame^] | 136 | public static final String EXTRA_ACTIVE_TETHER = "activeArray"; |
| 137 | |
| 138 | /** |
| 139 | * @hide |
| 140 | * gives a String[] |
| 141 | */ |
| 142 | public static final String EXTRA_ERRORED_TETHER = "erroredArray"; |
Robert Greenwalt | d0e18ff | 2010-01-26 11:40:34 -0800 | [diff] [blame] | 143 | |
| 144 | /** |
Robert Greenwalt | 42acef3 | 2009-08-12 16:08:25 -0700 | [diff] [blame] | 145 | * The Default Mobile data connection. When active, all data traffic |
| 146 | * will use this connection by default. Should not coexist with other |
| 147 | * default connections. |
| 148 | */ |
| 149 | public static final int TYPE_MOBILE = 0; |
| 150 | /** |
| 151 | * The Default WIFI data connection. When active, all data traffic |
| 152 | * will use this connection by default. Should not coexist with other |
| 153 | * default connections. |
| 154 | */ |
| 155 | public static final int TYPE_WIFI = 1; |
| 156 | /** |
| 157 | * An MMS-specific Mobile data connection. This connection may be the |
Robert Greenwalt | 1bc3c37 | 2010-01-13 11:29:03 -0800 | [diff] [blame] | 158 | * same as {@link #TYPE_MOBILE} but it may be different. This is used |
Robert Greenwalt | 42acef3 | 2009-08-12 16:08:25 -0700 | [diff] [blame] | 159 | * by applications needing to talk to the carrier's Multimedia Messaging |
| 160 | * Service servers. It may coexist with default data connections. |
Robert Greenwalt | 42acef3 | 2009-08-12 16:08:25 -0700 | [diff] [blame] | 161 | */ |
| 162 | public static final int TYPE_MOBILE_MMS = 2; |
| 163 | /** |
| 164 | * A SUPL-specific Mobile data connection. This connection may be the |
Robert Greenwalt | 1bc3c37 | 2010-01-13 11:29:03 -0800 | [diff] [blame] | 165 | * same as {@link #TYPE_MOBILE} but it may be different. This is used |
Robert Greenwalt | 42acef3 | 2009-08-12 16:08:25 -0700 | [diff] [blame] | 166 | * by applications needing to talk to the carrier's Secure User Plane |
| 167 | * Location servers for help locating the device. It may coexist with |
| 168 | * default data connections. |
Robert Greenwalt | 42acef3 | 2009-08-12 16:08:25 -0700 | [diff] [blame] | 169 | */ |
| 170 | public static final int TYPE_MOBILE_SUPL = 3; |
| 171 | /** |
| 172 | * A DUN-specific Mobile data connection. This connection may be the |
Robert Greenwalt | 1bc3c37 | 2010-01-13 11:29:03 -0800 | [diff] [blame] | 173 | * same as {@link #TYPE_MOBILE} but it may be different. This is used |
Robert Greenwalt | 42acef3 | 2009-08-12 16:08:25 -0700 | [diff] [blame] | 174 | * by applicaitons performing a Dial Up Networking bridge so that |
| 175 | * the carrier is aware of DUN traffic. It may coexist with default data |
| 176 | * connections. |
Robert Greenwalt | 42acef3 | 2009-08-12 16:08:25 -0700 | [diff] [blame] | 177 | */ |
| 178 | public static final int TYPE_MOBILE_DUN = 4; |
| 179 | /** |
| 180 | * A High Priority Mobile data connection. This connection is typically |
Robert Greenwalt | 1bc3c37 | 2010-01-13 11:29:03 -0800 | [diff] [blame] | 181 | * the same as {@link #TYPE_MOBILE} but the routing setup is different. |
Robert Greenwalt | 42acef3 | 2009-08-12 16:08:25 -0700 | [diff] [blame] | 182 | * Only requesting processes will have access to the Mobile DNS servers |
| 183 | * and only IP's explicitly requested via {@link #requestRouteToHost} |
Robert Greenwalt | c849cdf | 2010-01-08 11:47:27 -0800 | [diff] [blame] | 184 | * will route over this interface if a default route exists. |
Robert Greenwalt | 42acef3 | 2009-08-12 16:08:25 -0700 | [diff] [blame] | 185 | */ |
| 186 | public static final int TYPE_MOBILE_HIPRI = 5; |
| 187 | /** {@hide} */ |
| 188 | public static final int MAX_RADIO_TYPE = TYPE_WIFI; |
| 189 | /** {@hide} */ |
| 190 | public static final int MAX_NETWORK_TYPE = TYPE_MOBILE_HIPRI; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 191 | |
| 192 | public static final int DEFAULT_NETWORK_PREFERENCE = TYPE_WIFI; |
| 193 | |
| 194 | private IConnectivityManager mService; |
| 195 | |
| 196 | static public boolean isNetworkTypeValid(int networkType) { |
Robert Greenwalt | 42acef3 | 2009-08-12 16:08:25 -0700 | [diff] [blame] | 197 | return networkType >= 0 && networkType <= MAX_NETWORK_TYPE; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 198 | } |
| 199 | |
| 200 | public void setNetworkPreference(int preference) { |
| 201 | try { |
| 202 | mService.setNetworkPreference(preference); |
| 203 | } catch (RemoteException e) { |
| 204 | } |
| 205 | } |
| 206 | |
| 207 | public int getNetworkPreference() { |
| 208 | try { |
| 209 | return mService.getNetworkPreference(); |
| 210 | } catch (RemoteException e) { |
| 211 | return -1; |
| 212 | } |
| 213 | } |
| 214 | |
| 215 | public NetworkInfo getActiveNetworkInfo() { |
| 216 | try { |
| 217 | return mService.getActiveNetworkInfo(); |
| 218 | } catch (RemoteException e) { |
| 219 | return null; |
| 220 | } |
| 221 | } |
| 222 | |
| 223 | public NetworkInfo getNetworkInfo(int networkType) { |
| 224 | try { |
| 225 | return mService.getNetworkInfo(networkType); |
| 226 | } catch (RemoteException e) { |
| 227 | return null; |
| 228 | } |
| 229 | } |
| 230 | |
| 231 | public NetworkInfo[] getAllNetworkInfo() { |
| 232 | try { |
| 233 | return mService.getAllNetworkInfo(); |
| 234 | } catch (RemoteException e) { |
| 235 | return null; |
| 236 | } |
| 237 | } |
| 238 | |
| 239 | /** {@hide} */ |
| 240 | public boolean setRadios(boolean turnOn) { |
| 241 | try { |
| 242 | return mService.setRadios(turnOn); |
| 243 | } catch (RemoteException e) { |
| 244 | return false; |
| 245 | } |
| 246 | } |
| 247 | |
| 248 | /** {@hide} */ |
| 249 | public boolean setRadio(int networkType, boolean turnOn) { |
| 250 | try { |
| 251 | return mService.setRadio(networkType, turnOn); |
| 252 | } catch (RemoteException e) { |
| 253 | return false; |
| 254 | } |
| 255 | } |
| 256 | |
| 257 | /** |
| 258 | * Tells the underlying networking system that the caller wants to |
| 259 | * begin using the named feature. The interpretation of {@code feature} |
| 260 | * is completely up to each networking implementation. |
| 261 | * @param networkType specifies which network the request pertains to |
| 262 | * @param feature the name of the feature to be used |
| 263 | * @return an integer value representing the outcome of the request. |
| 264 | * The interpretation of this value is specific to each networking |
| 265 | * implementation+feature combination, except that the value {@code -1} |
| 266 | * always indicates failure. |
| 267 | */ |
| 268 | public int startUsingNetworkFeature(int networkType, String feature) { |
| 269 | try { |
Robert Greenwalt | 42acef3 | 2009-08-12 16:08:25 -0700 | [diff] [blame] | 270 | return mService.startUsingNetworkFeature(networkType, feature, |
| 271 | new Binder()); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 272 | } catch (RemoteException e) { |
| 273 | return -1; |
| 274 | } |
| 275 | } |
| 276 | |
| 277 | /** |
| 278 | * Tells the underlying networking system that the caller is finished |
| 279 | * using the named feature. The interpretation of {@code feature} |
| 280 | * is completely up to each networking implementation. |
| 281 | * @param networkType specifies which network the request pertains to |
| 282 | * @param feature the name of the feature that is no longer needed |
| 283 | * @return an integer value representing the outcome of the request. |
| 284 | * The interpretation of this value is specific to each networking |
| 285 | * implementation+feature combination, except that the value {@code -1} |
| 286 | * always indicates failure. |
| 287 | */ |
| 288 | public int stopUsingNetworkFeature(int networkType, String feature) { |
| 289 | try { |
| 290 | return mService.stopUsingNetworkFeature(networkType, feature); |
| 291 | } catch (RemoteException e) { |
| 292 | return -1; |
| 293 | } |
| 294 | } |
| 295 | |
| 296 | /** |
| 297 | * Ensure that a network route exists to deliver traffic to the specified |
| 298 | * host via the specified network interface. An attempt to add a route that |
| 299 | * already exists is ignored, but treated as successful. |
| 300 | * @param networkType the type of the network over which traffic to the specified |
| 301 | * host is to be routed |
| 302 | * @param hostAddress the IP address of the host to which the route is desired |
| 303 | * @return {@code true} on success, {@code false} on failure |
| 304 | */ |
| 305 | public boolean requestRouteToHost(int networkType, int hostAddress) { |
| 306 | try { |
| 307 | return mService.requestRouteToHost(networkType, hostAddress); |
| 308 | } catch (RemoteException e) { |
| 309 | return false; |
| 310 | } |
| 311 | } |
| 312 | |
| 313 | /** |
| 314 | * Returns the value of the setting for background data usage. If false, |
| 315 | * applications should not use the network if the application is not in the |
| 316 | * foreground. Developers should respect this setting, and check the value |
| 317 | * of this before performing any background data operations. |
| 318 | * <p> |
| 319 | * All applications that have background services that use the network |
| 320 | * should listen to {@link #ACTION_BACKGROUND_DATA_SETTING_CHANGED}. |
| 321 | * |
| 322 | * @return Whether background data usage is allowed. |
| 323 | */ |
| 324 | public boolean getBackgroundDataSetting() { |
| 325 | try { |
| 326 | return mService.getBackgroundDataSetting(); |
| 327 | } catch (RemoteException e) { |
| 328 | // Err on the side of safety |
| 329 | return false; |
| 330 | } |
| 331 | } |
| 332 | |
| 333 | /** |
| 334 | * Sets the value of the setting for background data usage. |
| 335 | * |
| 336 | * @param allowBackgroundData Whether an application should use data while |
| 337 | * it is in the background. |
| 338 | * |
| 339 | * @attr ref android.Manifest.permission#CHANGE_BACKGROUND_DATA_SETTING |
| 340 | * @see #getBackgroundDataSetting() |
| 341 | * @hide |
| 342 | */ |
| 343 | public void setBackgroundDataSetting(boolean allowBackgroundData) { |
| 344 | try { |
| 345 | mService.setBackgroundDataSetting(allowBackgroundData); |
| 346 | } catch (RemoteException e) { |
| 347 | } |
| 348 | } |
| 349 | |
| 350 | /** |
| 351 | * Don't allow use of default constructor. |
| 352 | */ |
| 353 | @SuppressWarnings({"UnusedDeclaration"}) |
| 354 | private ConnectivityManager() { |
| 355 | } |
| 356 | |
| 357 | /** |
| 358 | * {@hide} |
| 359 | */ |
| 360 | public ConnectivityManager(IConnectivityManager service) { |
| 361 | if (service == null) { |
| 362 | throw new IllegalArgumentException( |
| 363 | "ConnectivityManager() cannot be constructed with null service"); |
| 364 | } |
| 365 | mService = service; |
| 366 | } |
Robert Greenwalt | d0e18ff | 2010-01-26 11:40:34 -0800 | [diff] [blame] | 367 | |
| 368 | /** |
| 369 | * {@hide} |
| 370 | */ |
| 371 | public String[] getTetherableIfaces() { |
| 372 | try { |
| 373 | return mService.getTetherableIfaces(); |
| 374 | } catch (RemoteException e) { |
| 375 | return new String[0]; |
| 376 | } |
| 377 | } |
| 378 | |
| 379 | /** |
| 380 | * {@hide} |
| 381 | */ |
| 382 | public String[] getTetheredIfaces() { |
| 383 | try { |
| 384 | return mService.getTetheredIfaces(); |
| 385 | } catch (RemoteException e) { |
| 386 | return new String[0]; |
| 387 | } |
| 388 | } |
| 389 | |
| 390 | /** |
| 391 | * {@hide} |
| 392 | */ |
| 393 | public boolean tether(String iface) { |
| 394 | try { |
| 395 | return mService.tether(iface); |
| 396 | } catch (RemoteException e) { |
| 397 | return false; |
| 398 | } |
| 399 | } |
| 400 | |
| 401 | /** |
| 402 | * {@hide} |
| 403 | */ |
| 404 | public boolean untether(String iface) { |
| 405 | try { |
| 406 | return mService.untether(iface); |
| 407 | } catch (RemoteException e) { |
| 408 | return false; |
| 409 | } |
| 410 | } |
Robert Greenwalt | 2a091d7 | 2010-02-11 18:18:40 -0800 | [diff] [blame^] | 411 | |
| 412 | /** |
| 413 | * {@hide} |
| 414 | */ |
| 415 | public boolean isTetheringSupported() { |
| 416 | try { |
| 417 | return mService.isTetheringSupported(); |
| 418 | } catch (RemoteException e) { |
| 419 | return false; |
| 420 | } |
| 421 | } |
| 422 | |
| 423 | /** |
| 424 | * {@hide} |
| 425 | */ |
| 426 | public String[] getTetherableUsbRegexs() { |
| 427 | try { |
| 428 | return mService.getTetherableUsbRegexs(); |
| 429 | } catch (RemoteException e) { |
| 430 | return new String[0]; |
| 431 | } |
| 432 | } |
| 433 | |
| 434 | /** |
| 435 | * {@hide} |
| 436 | */ |
| 437 | public String[] getTetherableWifiRegexs() { |
| 438 | try { |
| 439 | return mService.getTetherableWifiRegexs(); |
| 440 | } catch (RemoteException e) { |
| 441 | return new String[0]; |
| 442 | } |
| 443 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 444 | } |