blob: 331ce10a64a2b6e860c4e34e5ccbf96320cf0766 [file] [log] [blame]
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001/*
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
17package android.net;
18
19import android.annotation.SdkConstant;
20import android.annotation.SdkConstant.SdkConstantType;
Robert Greenwalt42acef32009-08-12 16:08:25 -070021import android.os.Binder;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080022import 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 */
40public 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";
Robert Greenwaltd7085fc2010-09-08 15:24:47 -0700105 /**
106 * The lookup key for an int that provides information about
107 * our connection to the internet at large. 0 indicates no connection,
108 * 100 indicates a great connection. Retrieve it with
109 * {@link android.content.Intent@getIntExtra(String)}.
110 * {@hide}
111 */
112 public static final String EXTRA_INET_CONDITION = "inetCondition";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800113
114 /**
115 * Broadcast Action: The setting for background data usage has changed
116 * values. Use {@link #getBackgroundDataSetting()} to get the current value.
117 * <p>
118 * If an application uses the network in the background, it should listen
119 * for this broadcast and stop using the background data if the value is
120 * false.
121 */
122 @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION)
123 public static final String ACTION_BACKGROUND_DATA_SETTING_CHANGED =
124 "android.net.conn.BACKGROUND_DATA_SETTING_CHANGED";
125
Robert Greenwalt42acef32009-08-12 16:08:25 -0700126 /**
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -0800127 * Broadcast Action: A tetherable connection has come or gone
128 * TODO - finish the doc
129 * @hide
130 */
131 public static final String ACTION_TETHER_STATE_CHANGED =
132 "android.net.conn.TETHER_STATE_CHANGED";
133
134 /**
135 * @hide
Robert Greenwalt2a091d72010-02-11 18:18:40 -0800136 * gives a String[]
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -0800137 */
Robert Greenwalt2a091d72010-02-11 18:18:40 -0800138 public static final String EXTRA_AVAILABLE_TETHER = "availableArray";
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -0800139
140 /**
141 * @hide
Robert Greenwalt2a091d72010-02-11 18:18:40 -0800142 * gives a String[]
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -0800143 */
Robert Greenwalt2a091d72010-02-11 18:18:40 -0800144 public static final String EXTRA_ACTIVE_TETHER = "activeArray";
145
146 /**
147 * @hide
148 * gives a String[]
149 */
150 public static final String EXTRA_ERRORED_TETHER = "erroredArray";
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -0800151
152 /**
Robert Greenwalt42acef32009-08-12 16:08:25 -0700153 * The Default Mobile data connection. When active, all data traffic
154 * will use this connection by default. Should not coexist with other
155 * default connections.
156 */
157 public static final int TYPE_MOBILE = 0;
158 /**
159 * The Default WIFI data connection. When active, all data traffic
160 * will use this connection by default. Should not coexist with other
161 * default connections.
162 */
163 public static final int TYPE_WIFI = 1;
164 /**
165 * An MMS-specific Mobile data connection. This connection may be the
Robert Greenwalt1bc3c372010-01-13 11:29:03 -0800166 * same as {@link #TYPE_MOBILE} but it may be different. This is used
Robert Greenwalt42acef32009-08-12 16:08:25 -0700167 * by applications needing to talk to the carrier's Multimedia Messaging
168 * Service servers. It may coexist with default data connections.
Robert Greenwalt42acef32009-08-12 16:08:25 -0700169 */
170 public static final int TYPE_MOBILE_MMS = 2;
171 /**
172 * A SUPL-specific Mobile data connection. This connection may be the
Robert Greenwalt1bc3c372010-01-13 11:29:03 -0800173 * same as {@link #TYPE_MOBILE} but it may be different. This is used
Robert Greenwalt42acef32009-08-12 16:08:25 -0700174 * by applications needing to talk to the carrier's Secure User Plane
175 * Location servers for help locating the device. It may coexist with
176 * default data connections.
Robert Greenwalt42acef32009-08-12 16:08:25 -0700177 */
178 public static final int TYPE_MOBILE_SUPL = 3;
179 /**
180 * A DUN-specific Mobile data connection. This connection may be the
Robert Greenwalt1bc3c372010-01-13 11:29:03 -0800181 * same as {@link #TYPE_MOBILE} but it may be different. This is used
Robert Greenwalt42acef32009-08-12 16:08:25 -0700182 * by applicaitons performing a Dial Up Networking bridge so that
183 * the carrier is aware of DUN traffic. It may coexist with default data
184 * connections.
Robert Greenwalt42acef32009-08-12 16:08:25 -0700185 */
186 public static final int TYPE_MOBILE_DUN = 4;
187 /**
188 * A High Priority Mobile data connection. This connection is typically
Robert Greenwalt1bc3c372010-01-13 11:29:03 -0800189 * the same as {@link #TYPE_MOBILE} but the routing setup is different.
Robert Greenwalt42acef32009-08-12 16:08:25 -0700190 * Only requesting processes will have access to the Mobile DNS servers
191 * and only IP's explicitly requested via {@link #requestRouteToHost}
Robert Greenwaltc849cdf2010-01-08 11:47:27 -0800192 * will route over this interface if a default route exists.
Robert Greenwalt42acef32009-08-12 16:08:25 -0700193 */
194 public static final int TYPE_MOBILE_HIPRI = 5;
jsh8214deb2010-03-11 15:04:43 -0800195 /**
196 * The Default WiMAX data connection. When active, all data traffic
197 * will use this connection by default. Should not coexist with other
198 * default connections.
199 */
200 public static final int TYPE_WIMAX = 6;
201 /** {@hide} TODO: Need to adjust this for WiMAX. */
Robert Greenwalt42acef32009-08-12 16:08:25 -0700202 public static final int MAX_RADIO_TYPE = TYPE_WIFI;
jsh8214deb2010-03-11 15:04:43 -0800203 /** {@hide} TODO: Need to adjust this for WiMAX. */
Robert Greenwalt42acef32009-08-12 16:08:25 -0700204 public static final int MAX_NETWORK_TYPE = TYPE_MOBILE_HIPRI;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800205
206 public static final int DEFAULT_NETWORK_PREFERENCE = TYPE_WIFI;
207
208 private IConnectivityManager mService;
209
210 static public boolean isNetworkTypeValid(int networkType) {
Robert Greenwalt42acef32009-08-12 16:08:25 -0700211 return networkType >= 0 && networkType <= MAX_NETWORK_TYPE;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800212 }
213
214 public void setNetworkPreference(int preference) {
215 try {
216 mService.setNetworkPreference(preference);
217 } catch (RemoteException e) {
218 }
219 }
220
221 public int getNetworkPreference() {
222 try {
223 return mService.getNetworkPreference();
224 } catch (RemoteException e) {
225 return -1;
226 }
227 }
228
229 public NetworkInfo getActiveNetworkInfo() {
230 try {
231 return mService.getActiveNetworkInfo();
232 } catch (RemoteException e) {
233 return null;
234 }
235 }
236
237 public NetworkInfo getNetworkInfo(int networkType) {
238 try {
239 return mService.getNetworkInfo(networkType);
240 } catch (RemoteException e) {
241 return null;
242 }
243 }
244
245 public NetworkInfo[] getAllNetworkInfo() {
246 try {
247 return mService.getAllNetworkInfo();
248 } catch (RemoteException e) {
249 return null;
250 }
251 }
252
253 /** {@hide} */
254 public boolean setRadios(boolean turnOn) {
255 try {
256 return mService.setRadios(turnOn);
257 } catch (RemoteException e) {
258 return false;
259 }
260 }
261
262 /** {@hide} */
263 public boolean setRadio(int networkType, boolean turnOn) {
264 try {
265 return mService.setRadio(networkType, turnOn);
266 } catch (RemoteException e) {
267 return false;
268 }
269 }
270
271 /**
272 * Tells the underlying networking system that the caller wants to
273 * begin using the named feature. The interpretation of {@code feature}
274 * is completely up to each networking implementation.
275 * @param networkType specifies which network the request pertains to
276 * @param feature the name of the feature to be used
277 * @return an integer value representing the outcome of the request.
278 * The interpretation of this value is specific to each networking
279 * implementation+feature combination, except that the value {@code -1}
280 * always indicates failure.
281 */
282 public int startUsingNetworkFeature(int networkType, String feature) {
283 try {
Robert Greenwalt42acef32009-08-12 16:08:25 -0700284 return mService.startUsingNetworkFeature(networkType, feature,
285 new Binder());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800286 } catch (RemoteException e) {
287 return -1;
288 }
289 }
290
291 /**
292 * Tells the underlying networking system that the caller is finished
293 * using the named feature. The interpretation of {@code feature}
294 * is completely up to each networking implementation.
295 * @param networkType specifies which network the request pertains to
296 * @param feature the name of the feature that is no longer needed
297 * @return an integer value representing the outcome of the request.
298 * The interpretation of this value is specific to each networking
299 * implementation+feature combination, except that the value {@code -1}
300 * always indicates failure.
301 */
302 public int stopUsingNetworkFeature(int networkType, String feature) {
303 try {
304 return mService.stopUsingNetworkFeature(networkType, feature);
305 } catch (RemoteException e) {
306 return -1;
307 }
308 }
309
310 /**
311 * Ensure that a network route exists to deliver traffic to the specified
312 * host via the specified network interface. An attempt to add a route that
313 * already exists is ignored, but treated as successful.
314 * @param networkType the type of the network over which traffic to the specified
315 * host is to be routed
316 * @param hostAddress the IP address of the host to which the route is desired
317 * @return {@code true} on success, {@code false} on failure
318 */
319 public boolean requestRouteToHost(int networkType, int hostAddress) {
320 try {
321 return mService.requestRouteToHost(networkType, hostAddress);
322 } catch (RemoteException e) {
323 return false;
324 }
325 }
326
327 /**
328 * Returns the value of the setting for background data usage. If false,
329 * applications should not use the network if the application is not in the
330 * foreground. Developers should respect this setting, and check the value
331 * of this before performing any background data operations.
332 * <p>
333 * All applications that have background services that use the network
334 * should listen to {@link #ACTION_BACKGROUND_DATA_SETTING_CHANGED}.
335 *
336 * @return Whether background data usage is allowed.
337 */
338 public boolean getBackgroundDataSetting() {
339 try {
340 return mService.getBackgroundDataSetting();
341 } catch (RemoteException e) {
342 // Err on the side of safety
343 return false;
344 }
345 }
346
347 /**
348 * Sets the value of the setting for background data usage.
Robert Greenwaltc03fa502010-02-23 18:58:05 -0800349 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800350 * @param allowBackgroundData Whether an application should use data while
351 * it is in the background.
Robert Greenwaltc03fa502010-02-23 18:58:05 -0800352 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800353 * @attr ref android.Manifest.permission#CHANGE_BACKGROUND_DATA_SETTING
354 * @see #getBackgroundDataSetting()
355 * @hide
356 */
357 public void setBackgroundDataSetting(boolean allowBackgroundData) {
358 try {
359 mService.setBackgroundDataSetting(allowBackgroundData);
360 } catch (RemoteException e) {
361 }
362 }
Robert Greenwaltc03fa502010-02-23 18:58:05 -0800363
364 /**
365 * Gets the value of the setting for enabling Mobile data.
366 *
367 * @return Whether mobile data is enabled.
368 * @hide
369 */
370 public boolean getMobileDataEnabled() {
371 try {
372 return mService.getMobileDataEnabled();
373 } catch (RemoteException e) {
374 return true;
375 }
376 }
377
378 /**
379 * Sets the persisted value for enabling/disabling Mobile data.
380 *
Robert Greenwalt5a735062010-03-02 17:25:02 -0800381 * @param enabled Whether the mobile data connection should be
Robert Greenwaltc03fa502010-02-23 18:58:05 -0800382 * used or not.
383 * @hide
384 */
385 public void setMobileDataEnabled(boolean enabled) {
386 try {
387 mService.setMobileDataEnabled(enabled);
388 } catch (RemoteException e) {
389 }
390 }
391
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800392 /**
393 * Don't allow use of default constructor.
394 */
395 @SuppressWarnings({"UnusedDeclaration"})
396 private ConnectivityManager() {
397 }
398
399 /**
400 * {@hide}
401 */
402 public ConnectivityManager(IConnectivityManager service) {
403 if (service == null) {
404 throw new IllegalArgumentException(
405 "ConnectivityManager() cannot be constructed with null service");
406 }
407 mService = service;
408 }
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -0800409
410 /**
411 * {@hide}
412 */
413 public String[] getTetherableIfaces() {
414 try {
415 return mService.getTetherableIfaces();
416 } catch (RemoteException e) {
417 return new String[0];
418 }
419 }
420
421 /**
422 * {@hide}
423 */
424 public String[] getTetheredIfaces() {
425 try {
426 return mService.getTetheredIfaces();
427 } catch (RemoteException e) {
428 return new String[0];
429 }
430 }
431
432 /**
433 * {@hide}
434 */
Robert Greenwalt5a735062010-03-02 17:25:02 -0800435 public String[] getTetheringErroredIfaces() {
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -0800436 try {
Robert Greenwalt5a735062010-03-02 17:25:02 -0800437 return mService.getTetheringErroredIfaces();
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -0800438 } catch (RemoteException e) {
Robert Greenwalt5a735062010-03-02 17:25:02 -0800439 return new String[0];
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -0800440 }
441 }
442
443 /**
Robert Greenwalt5a735062010-03-02 17:25:02 -0800444 * @return error A TETHER_ERROR value indicating success or failure type
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -0800445 * {@hide}
446 */
Robert Greenwalt5a735062010-03-02 17:25:02 -0800447 public int tether(String iface) {
448 try {
449 return mService.tether(iface);
450 } catch (RemoteException e) {
451 return TETHER_ERROR_SERVICE_UNAVAIL;
452 }
453 }
454
455 /**
456 * @return error A TETHER_ERROR value indicating success or failure type
457 * {@hide}
458 */
459 public int untether(String iface) {
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -0800460 try {
461 return mService.untether(iface);
462 } catch (RemoteException e) {
Robert Greenwalt5a735062010-03-02 17:25:02 -0800463 return TETHER_ERROR_SERVICE_UNAVAIL;
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -0800464 }
465 }
Robert Greenwalt2a091d72010-02-11 18:18:40 -0800466
467 /**
468 * {@hide}
469 */
470 public boolean isTetheringSupported() {
471 try {
472 return mService.isTetheringSupported();
473 } catch (RemoteException e) {
474 return false;
475 }
476 }
477
478 /**
479 * {@hide}
480 */
481 public String[] getTetherableUsbRegexs() {
482 try {
483 return mService.getTetherableUsbRegexs();
484 } catch (RemoteException e) {
485 return new String[0];
486 }
487 }
488
489 /**
490 * {@hide}
491 */
492 public String[] getTetherableWifiRegexs() {
493 try {
494 return mService.getTetherableWifiRegexs();
495 } catch (RemoteException e) {
496 return new String[0];
497 }
498 }
Robert Greenwalt5a735062010-03-02 17:25:02 -0800499
500 /** {@hide} */
501 public static final int TETHER_ERROR_NO_ERROR = 0;
502 /** {@hide} */
503 public static final int TETHER_ERROR_UNKNOWN_IFACE = 1;
504 /** {@hide} */
505 public static final int TETHER_ERROR_SERVICE_UNAVAIL = 2;
506 /** {@hide} */
507 public static final int TETHER_ERROR_UNSUPPORTED = 3;
508 /** {@hide} */
509 public static final int TETHER_ERROR_UNAVAIL_IFACE = 4;
510 /** {@hide} */
511 public static final int TETHER_ERROR_MASTER_ERROR = 5;
512 /** {@hide} */
513 public static final int TETHER_ERROR_TETHER_IFACE_ERROR = 6;
514 /** {@hide} */
515 public static final int TETHER_ERROR_UNTETHER_IFACE_ERROR = 7;
516 /** {@hide} */
517 public static final int TETHER_ERROR_ENABLE_NAT_ERROR = 8;
518 /** {@hide} */
519 public static final int TETHER_ERROR_DISABLE_NAT_ERROR = 9;
520 /** {@hide} */
521 public static final int TETHER_ERROR_IFACE_CFG_ERROR = 10;
522
523 /**
524 * @param iface The name of the interface we're interested in
525 * @return error The error code of the last error tethering or untethering the named
526 * interface
527 * {@hide}
528 */
529 public int getLastTetherError(String iface) {
530 try {
531 return mService.getLastTetherError(iface);
532 } catch (RemoteException e) {
533 return TETHER_ERROR_SERVICE_UNAVAIL;
534 }
Robert Greenwaltd7085fc2010-09-08 15:24:47 -0700535 }
536
537 /**
538 * @param networkType The type of network you want to report on
539 * @param percentage The quality of the connection 0 is bad, 100 is good
540 * {@hide}
541 */
542 public void reportInetCondition(int networkType, int percentage) {
543 try {
544 mService.reportInetCondition(networkType, percentage);
545 } catch (RemoteException e) {
546 }
547 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800548}