blob: d30ef04628b9c2ca2187c34c68be8d820c0015c9 [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
Jeff Sharkeyf0ceede2011-08-02 17:22:34 -070019import static com.android.internal.util.Preconditions.checkNotNull;
20
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080021import android.annotation.SdkConstant;
22import android.annotation.SdkConstant.SdkConstantType;
Jeff Sharkey8fc27e82012-04-04 20:40:58 -070023import android.content.Context;
Robert Greenwalt42acef32009-08-12 16:08:25 -070024import android.os.Binder;
Jeff Sharkey3a844fc2011-08-16 14:37:57 -070025import android.os.Build.VERSION_CODES;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080026import android.os.RemoteException;
Jeff Sharkey961e3042011-08-29 16:02:57 -070027import android.provider.Settings;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080028
Robert Greenwalt585ac0f2010-08-27 09:24:29 -070029import java.net.InetAddress;
Robert Greenwalt585ac0f2010-08-27 09:24:29 -070030
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080031/**
32 * Class that answers queries about the state of network connectivity. It also
33 * notifies applications when network connectivity changes. Get an instance
34 * of this class by calling
35 * {@link android.content.Context#getSystemService(String) Context.getSystemService(Context.CONNECTIVITY_SERVICE)}.
36 * <p>
37 * The primary responsibilities of this class are to:
38 * <ol>
39 * <li>Monitor network connections (Wi-Fi, GPRS, UMTS, etc.)</li>
40 * <li>Send broadcast intents when network connectivity changes</li>
41 * <li>Attempt to "fail over" to another network when connectivity to a network
42 * is lost</li>
43 * <li>Provide an API that allows applications to query the coarse-grained or fine-grained
44 * state of the available networks</li>
45 * </ol>
46 */
Jeff Sharkeyc006f1a2011-05-19 17:12:49 -070047public class ConnectivityManager {
48 private static final String TAG = "ConnectivityManager";
49
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080050 /**
51 * A change in network connectivity has occurred. A connection has either
52 * been established or lost. The NetworkInfo for the affected network is
53 * sent as an extra; it should be consulted to see what kind of
54 * connectivity event occurred.
55 * <p/>
56 * If this is a connection that was the result of failing over from a
57 * disconnected network, then the FAILOVER_CONNECTION boolean extra is
58 * set to true.
59 * <p/>
60 * For a loss of connectivity, if the connectivity manager is attempting
61 * to connect (or has already connected) to another network, the
62 * NetworkInfo for the new network is also passed as an extra. This lets
63 * any receivers of the broadcast know that they should not necessarily
64 * tell the user that no data traffic will be possible. Instead, the
65 * reciever should expect another broadcast soon, indicating either that
66 * the failover attempt succeeded (and so there is still overall data
67 * connectivity), or that the failover attempt failed, meaning that all
68 * connectivity has been lost.
69 * <p/>
70 * For a disconnect event, the boolean extra EXTRA_NO_CONNECTIVITY
71 * is set to {@code true} if there are no connected networks at all.
72 */
73 public static final String CONNECTIVITY_ACTION = "android.net.conn.CONNECTIVITY_CHANGE";
Jeff Sharkeyf0ceede2011-08-02 17:22:34 -070074
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080075 /**
Jeff Sharkey961e3042011-08-29 16:02:57 -070076 * Identical to {@link #CONNECTIVITY_ACTION} broadcast, but sent without any
77 * applicable {@link Settings.Secure#CONNECTIVITY_CHANGE_DELAY}.
78 *
79 * @hide
80 */
81 public static final String CONNECTIVITY_ACTION_IMMEDIATE =
82 "android.net.conn.CONNECTIVITY_CHANGE_IMMEDIATE";
83
84 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080085 * The lookup key for a {@link NetworkInfo} object. Retrieve with
86 * {@link android.content.Intent#getParcelableExtra(String)}.
Jeff Sharkeyf0ceede2011-08-02 17:22:34 -070087 *
88 * @deprecated Since {@link NetworkInfo} can vary based on UID, applications
89 * should always obtain network information through
90 * {@link #getActiveNetworkInfo()} or
91 * {@link #getAllNetworkInfo()}.
Jeff Sharkey75fbb4b2012-08-06 11:41:50 -070092 * @see #EXTRA_NETWORK_TYPE
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080093 */
Jeff Sharkeyf0ceede2011-08-02 17:22:34 -070094 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080095 public static final String EXTRA_NETWORK_INFO = "networkInfo";
Jeff Sharkeyf0ceede2011-08-02 17:22:34 -070096
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080097 /**
Jeff Sharkey75fbb4b2012-08-06 11:41:50 -070098 * Network type which triggered a {@link #CONNECTIVITY_ACTION} broadcast.
99 * Can be used with {@link #getNetworkInfo(int)} to get {@link NetworkInfo}
100 * state based on the calling application.
101 *
102 * @see android.content.Intent#getIntExtra(String, int)
103 */
104 public static final String EXTRA_NETWORK_TYPE = "networkType";
105
106 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800107 * The lookup key for a boolean that indicates whether a connect event
108 * is for a network to which the connectivity manager was failing over
109 * following a disconnect on another network.
110 * Retrieve it with {@link android.content.Intent#getBooleanExtra(String,boolean)}.
111 */
112 public static final String EXTRA_IS_FAILOVER = "isFailover";
113 /**
114 * The lookup key for a {@link NetworkInfo} object. This is supplied when
115 * there is another network that it may be possible to connect to. Retrieve with
116 * {@link android.content.Intent#getParcelableExtra(String)}.
117 */
118 public static final String EXTRA_OTHER_NETWORK_INFO = "otherNetwork";
119 /**
120 * The lookup key for a boolean that indicates whether there is a
121 * complete lack of connectivity, i.e., no network is available.
122 * Retrieve it with {@link android.content.Intent#getBooleanExtra(String,boolean)}.
123 */
124 public static final String EXTRA_NO_CONNECTIVITY = "noConnectivity";
125 /**
126 * The lookup key for a string that indicates why an attempt to connect
127 * to a network failed. The string has no particular structure. It is
128 * intended to be used in notifications presented to users. Retrieve
129 * it with {@link android.content.Intent#getStringExtra(String)}.
130 */
131 public static final String EXTRA_REASON = "reason";
132 /**
133 * The lookup key for a string that provides optionally supplied
134 * extra information about the network state. The information
135 * may be passed up from the lower networking layers, and its
136 * meaning may be specific to a particular network type. Retrieve
137 * it with {@link android.content.Intent#getStringExtra(String)}.
138 */
139 public static final String EXTRA_EXTRA_INFO = "extraInfo";
Robert Greenwaltd7085fc2010-09-08 15:24:47 -0700140 /**
141 * The lookup key for an int that provides information about
142 * our connection to the internet at large. 0 indicates no connection,
143 * 100 indicates a great connection. Retrieve it with
Jeff Sharkeyc006f1a2011-05-19 17:12:49 -0700144 * {@link android.content.Intent#getIntExtra(String, int)}.
Robert Greenwaltd7085fc2010-09-08 15:24:47 -0700145 * {@hide}
146 */
147 public static final String EXTRA_INET_CONDITION = "inetCondition";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800148
149 /**
Haoyu Baidb3c8672012-06-20 14:29:57 -0700150 * Broadcast action to indicate the change of data activity status
151 * (idle or active) on a network in a recent period.
152 * The network becomes active when data transimission is started, or
153 * idle if there is no data transimition for a period of time.
154 * {@hide}
155 */
156 @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION)
157 public static final String ACTION_DATA_ACTIVITY_CHANGE = "android.net.conn.DATA_ACTIVITY_CHANGE";
158 /**
159 * The lookup key for an enum that indicates the network device type on which this data activity
160 * change happens.
161 * {@hide}
162 */
163 public static final String EXTRA_DEVICE_TYPE = "deviceType";
164 /**
165 * The lookup key for a boolean that indicates the device is active or not. {@code true} means
166 * it is actively sending or receiving data and {@code false} means it is idle.
167 * {@hide}
168 */
169 public static final String EXTRA_IS_ACTIVE = "isActive";
170
171 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800172 * Broadcast Action: The setting for background data usage has changed
173 * values. Use {@link #getBackgroundDataSetting()} to get the current value.
174 * <p>
175 * If an application uses the network in the background, it should listen
176 * for this broadcast and stop using the background data if the value is
Jeff Sharkeyc006f1a2011-05-19 17:12:49 -0700177 * {@code false}.
Jeff Sharkey54ee2ad2012-01-30 16:29:24 -0800178 * <p>
179 *
180 * @deprecated As of {@link VERSION_CODES#ICE_CREAM_SANDWICH}, availability
181 * of background data depends on several combined factors, and
182 * this broadcast is no longer sent. Instead, when background
183 * data is unavailable, {@link #getActiveNetworkInfo()} will now
184 * appear disconnected. During first boot after a platform
185 * upgrade, this broadcast will be sent once if
186 * {@link #getBackgroundDataSetting()} was {@code false} before
187 * the upgrade.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800188 */
189 @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION)
Jeff Sharkey54ee2ad2012-01-30 16:29:24 -0800190 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800191 public static final String ACTION_BACKGROUND_DATA_SETTING_CHANGED =
192 "android.net.conn.BACKGROUND_DATA_SETTING_CHANGED";
193
Robert Greenwalt1e9aac22010-09-15 17:36:33 -0700194 /**
195 * Broadcast Action: The network connection may not be good
196 * uses {@code ConnectivityManager.EXTRA_INET_CONDITION} and
197 * {@code ConnectivityManager.EXTRA_NETWORK_INFO} to specify
198 * the network and it's condition.
199 * @hide
200 */
201 public static final String INET_CONDITION_ACTION =
202 "android.net.conn.INET_CONDITION_ACTION";
203
Robert Greenwalt42acef32009-08-12 16:08:25 -0700204 /**
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -0800205 * Broadcast Action: A tetherable connection has come or gone
206 * TODO - finish the doc
207 * @hide
208 */
209 public static final String ACTION_TETHER_STATE_CHANGED =
210 "android.net.conn.TETHER_STATE_CHANGED";
211
212 /**
213 * @hide
Robert Greenwalt2a091d72010-02-11 18:18:40 -0800214 * gives a String[]
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -0800215 */
Robert Greenwalt2a091d72010-02-11 18:18:40 -0800216 public static final String EXTRA_AVAILABLE_TETHER = "availableArray";
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -0800217
218 /**
219 * @hide
Robert Greenwalt2a091d72010-02-11 18:18:40 -0800220 * gives a String[]
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -0800221 */
Robert Greenwalt2a091d72010-02-11 18:18:40 -0800222 public static final String EXTRA_ACTIVE_TETHER = "activeArray";
223
224 /**
225 * @hide
226 * gives a String[]
227 */
228 public static final String EXTRA_ERRORED_TETHER = "erroredArray";
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -0800229
230 /**
Robert Greenwaltccf83af12011-06-02 17:30:47 -0700231 * The absence of APN..
232 * @hide
233 */
234 public static final int TYPE_NONE = -1;
235
236 /**
Robert Greenwalt42acef32009-08-12 16:08:25 -0700237 * The Default Mobile data connection. When active, all data traffic
Robert Greenwalt97ab2d42011-05-27 11:29:13 -0700238 * will use this connection by default.
Robert Greenwalt42acef32009-08-12 16:08:25 -0700239 */
240 public static final int TYPE_MOBILE = 0;
241 /**
242 * The Default WIFI data connection. When active, all data traffic
Robert Greenwalt97ab2d42011-05-27 11:29:13 -0700243 * will use this connection by default.
Robert Greenwalt42acef32009-08-12 16:08:25 -0700244 */
245 public static final int TYPE_WIFI = 1;
246 /**
247 * An MMS-specific Mobile data connection. This connection may be the
Robert Greenwalt1bc3c372010-01-13 11:29:03 -0800248 * same as {@link #TYPE_MOBILE} but it may be different. This is used
Robert Greenwalt42acef32009-08-12 16:08:25 -0700249 * by applications needing to talk to the carrier's Multimedia Messaging
250 * Service servers. It may coexist with default data connections.
Robert Greenwalt42acef32009-08-12 16:08:25 -0700251 */
252 public static final int TYPE_MOBILE_MMS = 2;
253 /**
254 * A SUPL-specific Mobile data connection. This connection may be the
Robert Greenwalt1bc3c372010-01-13 11:29:03 -0800255 * same as {@link #TYPE_MOBILE} but it may be different. This is used
Robert Greenwalt42acef32009-08-12 16:08:25 -0700256 * by applications needing to talk to the carrier's Secure User Plane
257 * Location servers for help locating the device. It may coexist with
258 * default data connections.
Robert Greenwalt42acef32009-08-12 16:08:25 -0700259 */
260 public static final int TYPE_MOBILE_SUPL = 3;
261 /**
262 * A DUN-specific Mobile data connection. This connection may be the
Robert Greenwalt1bc3c372010-01-13 11:29:03 -0800263 * same as {@link #TYPE_MOBILE} but it may be different. This is used
Robert Greenwalt42acef32009-08-12 16:08:25 -0700264 * by applicaitons performing a Dial Up Networking bridge so that
265 * the carrier is aware of DUN traffic. It may coexist with default data
266 * connections.
Robert Greenwalt42acef32009-08-12 16:08:25 -0700267 */
268 public static final int TYPE_MOBILE_DUN = 4;
269 /**
270 * A High Priority Mobile data connection. This connection is typically
Robert Greenwalt1bc3c372010-01-13 11:29:03 -0800271 * the same as {@link #TYPE_MOBILE} but the routing setup is different.
Robert Greenwalt42acef32009-08-12 16:08:25 -0700272 * Only requesting processes will have access to the Mobile DNS servers
273 * and only IP's explicitly requested via {@link #requestRouteToHost}
Robert Greenwaltc849cdf2010-01-08 11:47:27 -0800274 * will route over this interface if a default route exists.
Robert Greenwalt42acef32009-08-12 16:08:25 -0700275 */
276 public static final int TYPE_MOBILE_HIPRI = 5;
jsh8214deb2010-03-11 15:04:43 -0800277 /**
278 * The Default WiMAX data connection. When active, all data traffic
Robert Greenwalt97ab2d42011-05-27 11:29:13 -0700279 * will use this connection by default.
jsh8214deb2010-03-11 15:04:43 -0800280 */
281 public static final int TYPE_WIMAX = 6;
Robert Greenwaltda3d5e62010-12-06 13:56:24 -0800282
Jaikumar Ganesh15c74392010-12-21 22:31:44 -0800283 /**
Robert Greenwalt97ab2d42011-05-27 11:29:13 -0700284 * The Default Bluetooth data connection. When active, all data traffic
285 * will use this connection by default.
Jaikumar Ganesh15c74392010-12-21 22:31:44 -0800286 */
287 public static final int TYPE_BLUETOOTH = 7;
288
Robert Greenwalt60810842011-04-22 15:28:18 -0700289 /**
290 * Dummy data connection. This should not be used on shipping devices.
291 */
Jaikumar Ganesh15c74392010-12-21 22:31:44 -0800292 public static final int TYPE_DUMMY = 8;
Wink Saville9d7d6282011-03-12 14:52:01 -0800293
Robert Greenwalt60810842011-04-22 15:28:18 -0700294 /**
Robert Greenwalt97ab2d42011-05-27 11:29:13 -0700295 * The Default Ethernet data connection. When active, all data traffic
296 * will use this connection by default.
Robert Greenwalt60810842011-04-22 15:28:18 -0700297 */
Robert Greenwalte12aec92011-01-28 14:48:37 -0800298 public static final int TYPE_ETHERNET = 9;
Robert Greenwalt60810842011-04-22 15:28:18 -0700299
Wink Saville9d7d6282011-03-12 14:52:01 -0800300 /**
301 * Over the air Adminstration.
302 * {@hide}
303 */
304 public static final int TYPE_MOBILE_FOTA = 10;
305
306 /**
307 * IP Multimedia Subsystem
308 * {@hide}
309 */
310 public static final int TYPE_MOBILE_IMS = 11;
311
312 /**
313 * Carrier Branded Services
314 * {@hide}
315 */
316 public static final int TYPE_MOBILE_CBS = 12;
317
repo syncaea743a2011-07-29 23:55:49 -0700318 /**
319 * A Wi-Fi p2p connection. Only requesting processes will have access to
320 * the peers connected.
321 * {@hide}
322 */
323 public static final int TYPE_WIFI_P2P = 13;
Wink Saville9d7d6282011-03-12 14:52:01 -0800324
325 /** {@hide} */
repo syncaea743a2011-07-29 23:55:49 -0700326 public static final int MAX_RADIO_TYPE = TYPE_WIFI_P2P;
327
328 /** {@hide} */
329 public static final int MAX_NETWORK_TYPE = TYPE_WIFI_P2P;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800330
331 public static final int DEFAULT_NETWORK_PREFERENCE = TYPE_WIFI;
332
Jeff Sharkeyc006f1a2011-05-19 17:12:49 -0700333 private final IConnectivityManager mService;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800334
Jeff Sharkeyd2a45872011-05-28 20:56:34 -0700335 public static boolean isNetworkTypeValid(int networkType) {
Robert Greenwalt42acef32009-08-12 16:08:25 -0700336 return networkType >= 0 && networkType <= MAX_NETWORK_TYPE;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800337 }
338
Jeff Sharkeyd2a45872011-05-28 20:56:34 -0700339 /** {@hide} */
340 public static String getNetworkTypeName(int type) {
341 switch (type) {
342 case TYPE_MOBILE:
343 return "MOBILE";
344 case TYPE_WIFI:
345 return "WIFI";
346 case TYPE_MOBILE_MMS:
347 return "MOBILE_MMS";
348 case TYPE_MOBILE_SUPL:
349 return "MOBILE_SUPL";
350 case TYPE_MOBILE_DUN:
351 return "MOBILE_DUN";
352 case TYPE_MOBILE_HIPRI:
353 return "MOBILE_HIPRI";
354 case TYPE_WIMAX:
355 return "WIMAX";
356 case TYPE_BLUETOOTH:
357 return "BLUETOOTH";
358 case TYPE_DUMMY:
359 return "DUMMY";
360 case TYPE_ETHERNET:
361 return "ETHERNET";
362 case TYPE_MOBILE_FOTA:
363 return "MOBILE_FOTA";
364 case TYPE_MOBILE_IMS:
365 return "MOBILE_IMS";
366 case TYPE_MOBILE_CBS:
367 return "MOBILE_CBS";
repo syncaea743a2011-07-29 23:55:49 -0700368 case TYPE_WIFI_P2P:
369 return "WIFI_P2P";
Jeff Sharkeyd2a45872011-05-28 20:56:34 -0700370 default:
371 return Integer.toString(type);
372 }
373 }
374
375 /** {@hide} */
376 public static boolean isNetworkTypeMobile(int networkType) {
377 switch (networkType) {
378 case TYPE_MOBILE:
379 case TYPE_MOBILE_MMS:
380 case TYPE_MOBILE_SUPL:
381 case TYPE_MOBILE_DUN:
382 case TYPE_MOBILE_HIPRI:
383 case TYPE_MOBILE_FOTA:
384 case TYPE_MOBILE_IMS:
385 case TYPE_MOBILE_CBS:
386 return true;
387 default:
388 return false;
389 }
390 }
391
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800392 public void setNetworkPreference(int preference) {
393 try {
394 mService.setNetworkPreference(preference);
395 } catch (RemoteException e) {
396 }
397 }
398
399 public int getNetworkPreference() {
400 try {
401 return mService.getNetworkPreference();
402 } catch (RemoteException e) {
403 return -1;
404 }
405 }
406
Scott Main671644c2011-10-06 19:02:28 -0700407 /**
Jeff Sharkey9f7cbf02012-04-12 18:34:54 -0700408 * Returns details about the currently active data network. When connected,
409 * this network is the default route for outgoing connections. You should
410 * always check {@link NetworkInfo#isConnected()} before initiating network
411 * traffic. This may return {@code null} when no networks are available.
Nicolas Falliere9530e3a2012-06-18 17:21:06 -0700412 * <p>This method requires the caller to hold the permission
413 * {@link android.Manifest.permission#ACCESS_NETWORK_STATE}.
Jeff Sharkey9f7cbf02012-04-12 18:34:54 -0700414 */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800415 public NetworkInfo getActiveNetworkInfo() {
416 try {
417 return mService.getActiveNetworkInfo();
418 } catch (RemoteException e) {
419 return null;
420 }
421 }
422
Jeff Sharkeyc006f1a2011-05-19 17:12:49 -0700423 /** {@hide} */
424 public NetworkInfo getActiveNetworkInfoForUid(int uid) {
425 try {
426 return mService.getActiveNetworkInfoForUid(uid);
427 } catch (RemoteException e) {
428 return null;
429 }
430 }
431
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800432 public NetworkInfo getNetworkInfo(int networkType) {
433 try {
434 return mService.getNetworkInfo(networkType);
435 } catch (RemoteException e) {
436 return null;
437 }
438 }
439
440 public NetworkInfo[] getAllNetworkInfo() {
441 try {
442 return mService.getAllNetworkInfo();
443 } catch (RemoteException e) {
444 return null;
445 }
446 }
447
Jeff Sharkeyc006f1a2011-05-19 17:12:49 -0700448 /** {@hide} */
Robert Greenwaltd192dad2010-09-14 09:18:02 -0700449 public LinkProperties getActiveLinkProperties() {
450 try {
451 return mService.getActiveLinkProperties();
452 } catch (RemoteException e) {
453 return null;
454 }
455 }
456
Jeff Sharkeyc006f1a2011-05-19 17:12:49 -0700457 /** {@hide} */
Robert Greenwaltd192dad2010-09-14 09:18:02 -0700458 public LinkProperties getLinkProperties(int networkType) {
459 try {
460 return mService.getLinkProperties(networkType);
461 } catch (RemoteException e) {
462 return null;
463 }
464 }
465
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800466 /** {@hide} */
467 public boolean setRadios(boolean turnOn) {
468 try {
469 return mService.setRadios(turnOn);
470 } catch (RemoteException e) {
471 return false;
472 }
473 }
474
475 /** {@hide} */
476 public boolean setRadio(int networkType, boolean turnOn) {
477 try {
478 return mService.setRadio(networkType, turnOn);
479 } catch (RemoteException e) {
480 return false;
481 }
482 }
483
484 /**
485 * Tells the underlying networking system that the caller wants to
486 * begin using the named feature. The interpretation of {@code feature}
487 * is completely up to each networking implementation.
Nicolas Falliere9530e3a2012-06-18 17:21:06 -0700488 * <p>This method requires the caller to hold the permission
489 * {@link android.Manifest.permission#CHANGE_NETWORK_STATE}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800490 * @param networkType specifies which network the request pertains to
491 * @param feature the name of the feature to be used
492 * @return an integer value representing the outcome of the request.
493 * The interpretation of this value is specific to each networking
494 * implementation+feature combination, except that the value {@code -1}
495 * always indicates failure.
496 */
497 public int startUsingNetworkFeature(int networkType, String feature) {
498 try {
Robert Greenwalt42acef32009-08-12 16:08:25 -0700499 return mService.startUsingNetworkFeature(networkType, feature,
500 new Binder());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800501 } catch (RemoteException e) {
502 return -1;
503 }
504 }
505
506 /**
507 * Tells the underlying networking system that the caller is finished
508 * using the named feature. The interpretation of {@code feature}
509 * is completely up to each networking implementation.
Nicolas Falliere9530e3a2012-06-18 17:21:06 -0700510 * <p>This method requires the caller to hold the permission
511 * {@link android.Manifest.permission#CHANGE_NETWORK_STATE}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800512 * @param networkType specifies which network the request pertains to
513 * @param feature the name of the feature that is no longer needed
514 * @return an integer value representing the outcome of the request.
515 * The interpretation of this value is specific to each networking
516 * implementation+feature combination, except that the value {@code -1}
517 * always indicates failure.
518 */
519 public int stopUsingNetworkFeature(int networkType, String feature) {
520 try {
521 return mService.stopUsingNetworkFeature(networkType, feature);
522 } catch (RemoteException e) {
523 return -1;
524 }
525 }
526
527 /**
528 * Ensure that a network route exists to deliver traffic to the specified
529 * host via the specified network interface. An attempt to add a route that
530 * already exists is ignored, but treated as successful.
Nicolas Falliere9530e3a2012-06-18 17:21:06 -0700531 * <p>This method requires the caller to hold the permission
532 * {@link android.Manifest.permission#CHANGE_NETWORK_STATE}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800533 * @param networkType the type of the network over which traffic to the specified
534 * host is to be routed
535 * @param hostAddress the IP address of the host to which the route is desired
536 * @return {@code true} on success, {@code false} on failure
537 */
538 public boolean requestRouteToHost(int networkType, int hostAddress) {
Robert Greenwalt585ac0f2010-08-27 09:24:29 -0700539 InetAddress inetAddress = NetworkUtils.intToInetAddress(hostAddress);
540
541 if (inetAddress == null) {
542 return false;
543 }
544
545 return requestRouteToHostAddress(networkType, inetAddress);
546 }
547
548 /**
549 * Ensure that a network route exists to deliver traffic to the specified
550 * host via the specified network interface. An attempt to add a route that
551 * already exists is ignored, but treated as successful.
552 * @param networkType the type of the network over which traffic to the specified
553 * host is to be routed
554 * @param hostAddress the IP address of the host to which the route is desired
555 * @return {@code true} on success, {@code false} on failure
556 * @hide
557 */
558 public boolean requestRouteToHostAddress(int networkType, InetAddress hostAddress) {
559 byte[] address = hostAddress.getAddress();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800560 try {
Robert Greenwalt585ac0f2010-08-27 09:24:29 -0700561 return mService.requestRouteToHostAddress(networkType, address);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800562 } catch (RemoteException e) {
563 return false;
564 }
565 }
566
567 /**
568 * Returns the value of the setting for background data usage. If false,
569 * applications should not use the network if the application is not in the
570 * foreground. Developers should respect this setting, and check the value
571 * of this before performing any background data operations.
572 * <p>
573 * All applications that have background services that use the network
574 * should listen to {@link #ACTION_BACKGROUND_DATA_SETTING_CHANGED}.
Jeff Sharkey3a844fc2011-08-16 14:37:57 -0700575 * <p>
Scott Main4cc53332011-10-06 18:32:43 -0700576 * @deprecated As of {@link VERSION_CODES#ICE_CREAM_SANDWICH}, availability of
Jeff Sharkey3a844fc2011-08-16 14:37:57 -0700577 * background data depends on several combined factors, and this method will
578 * always return {@code true}. Instead, when background data is unavailable,
579 * {@link #getActiveNetworkInfo()} will now appear disconnected.
Danica Chang6fdd0c62010-08-11 14:54:43 -0700580 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800581 * @return Whether background data usage is allowed.
582 */
Jeff Sharkey3a844fc2011-08-16 14:37:57 -0700583 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800584 public boolean getBackgroundDataSetting() {
Jeff Sharkey3a844fc2011-08-16 14:37:57 -0700585 // assume that background data is allowed; final authority is
586 // NetworkInfo which may be blocked.
587 return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800588 }
589
590 /**
591 * Sets the value of the setting for background data usage.
Robert Greenwaltc03fa502010-02-23 18:58:05 -0800592 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800593 * @param allowBackgroundData Whether an application should use data while
594 * it is in the background.
Robert Greenwaltc03fa502010-02-23 18:58:05 -0800595 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800596 * @attr ref android.Manifest.permission#CHANGE_BACKGROUND_DATA_SETTING
597 * @see #getBackgroundDataSetting()
598 * @hide
599 */
Jeff Sharkey3a844fc2011-08-16 14:37:57 -0700600 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800601 public void setBackgroundDataSetting(boolean allowBackgroundData) {
Jeff Sharkey3a844fc2011-08-16 14:37:57 -0700602 // ignored
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800603 }
Robert Greenwaltc03fa502010-02-23 18:58:05 -0800604
605 /**
Jeff Sharkeyf0ceede2011-08-02 17:22:34 -0700606 * Return quota status for the current active network, or {@code null} if no
607 * network is active. Quota status can change rapidly, so these values
608 * shouldn't be cached.
Jeff Sharkey44a3e0d2011-10-06 10:50:09 -0700609 *
610 * @hide
Jeff Sharkeyf0ceede2011-08-02 17:22:34 -0700611 */
612 public NetworkQuotaInfo getActiveNetworkQuotaInfo() {
613 try {
614 return mService.getActiveNetworkQuotaInfo();
615 } catch (RemoteException e) {
616 return null;
617 }
618 }
619
620 /**
Robert Greenwaltc03fa502010-02-23 18:58:05 -0800621 * Gets the value of the setting for enabling Mobile data.
622 *
623 * @return Whether mobile data is enabled.
624 * @hide
625 */
626 public boolean getMobileDataEnabled() {
627 try {
628 return mService.getMobileDataEnabled();
629 } catch (RemoteException e) {
630 return true;
631 }
632 }
633
634 /**
635 * Sets the persisted value for enabling/disabling Mobile data.
636 *
Robert Greenwalt5a735062010-03-02 17:25:02 -0800637 * @param enabled Whether the mobile data connection should be
Robert Greenwaltc03fa502010-02-23 18:58:05 -0800638 * used or not.
639 * @hide
640 */
641 public void setMobileDataEnabled(boolean enabled) {
642 try {
643 mService.setMobileDataEnabled(enabled);
644 } catch (RemoteException e) {
645 }
646 }
647
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800648 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800649 * {@hide}
650 */
651 public ConnectivityManager(IConnectivityManager service) {
Jeff Sharkeyf0ceede2011-08-02 17:22:34 -0700652 mService = checkNotNull(service, "missing IConnectivityManager");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800653 }
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -0800654
Jeff Sharkey8fc27e82012-04-04 20:40:58 -0700655 /** {@hide} */
656 public static ConnectivityManager from(Context context) {
657 return (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
658 }
659
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -0800660 /**
661 * {@hide}
662 */
663 public String[] getTetherableIfaces() {
664 try {
665 return mService.getTetherableIfaces();
666 } catch (RemoteException e) {
667 return new String[0];
668 }
669 }
670
671 /**
672 * {@hide}
673 */
674 public String[] getTetheredIfaces() {
675 try {
676 return mService.getTetheredIfaces();
677 } catch (RemoteException e) {
678 return new String[0];
679 }
680 }
681
682 /**
683 * {@hide}
684 */
Robert Greenwalt5a735062010-03-02 17:25:02 -0800685 public String[] getTetheringErroredIfaces() {
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -0800686 try {
Robert Greenwalt5a735062010-03-02 17:25:02 -0800687 return mService.getTetheringErroredIfaces();
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -0800688 } catch (RemoteException e) {
Robert Greenwalt5a735062010-03-02 17:25:02 -0800689 return new String[0];
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -0800690 }
691 }
692
693 /**
Robert Greenwalt5a735062010-03-02 17:25:02 -0800694 * @return error A TETHER_ERROR value indicating success or failure type
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -0800695 * {@hide}
696 */
Robert Greenwalt5a735062010-03-02 17:25:02 -0800697 public int tether(String iface) {
698 try {
699 return mService.tether(iface);
700 } catch (RemoteException e) {
701 return TETHER_ERROR_SERVICE_UNAVAIL;
702 }
703 }
704
705 /**
706 * @return error A TETHER_ERROR value indicating success or failure type
707 * {@hide}
708 */
709 public int untether(String iface) {
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -0800710 try {
711 return mService.untether(iface);
712 } catch (RemoteException e) {
Robert Greenwalt5a735062010-03-02 17:25:02 -0800713 return TETHER_ERROR_SERVICE_UNAVAIL;
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -0800714 }
715 }
Robert Greenwalt2a091d72010-02-11 18:18:40 -0800716
717 /**
718 * {@hide}
719 */
720 public boolean isTetheringSupported() {
721 try {
722 return mService.isTetheringSupported();
723 } catch (RemoteException e) {
724 return false;
725 }
726 }
727
728 /**
729 * {@hide}
730 */
731 public String[] getTetherableUsbRegexs() {
732 try {
733 return mService.getTetherableUsbRegexs();
734 } catch (RemoteException e) {
735 return new String[0];
736 }
737 }
738
739 /**
740 * {@hide}
741 */
742 public String[] getTetherableWifiRegexs() {
743 try {
744 return mService.getTetherableWifiRegexs();
745 } catch (RemoteException e) {
746 return new String[0];
747 }
748 }
Robert Greenwalt5a735062010-03-02 17:25:02 -0800749
Danica Chang6fdd0c62010-08-11 14:54:43 -0700750 /**
751 * {@hide}
752 */
753 public String[] getTetherableBluetoothRegexs() {
754 try {
755 return mService.getTetherableBluetoothRegexs();
756 } catch (RemoteException e) {
757 return new String[0];
758 }
759 }
760
Mike Lockwood6c2260b2011-07-19 13:04:47 -0700761 /**
762 * {@hide}
763 */
764 public int setUsbTethering(boolean enable) {
765 try {
766 return mService.setUsbTethering(enable);
767 } catch (RemoteException e) {
768 return TETHER_ERROR_SERVICE_UNAVAIL;
769 }
770 }
771
Robert Greenwalt5a735062010-03-02 17:25:02 -0800772 /** {@hide} */
773 public static final int TETHER_ERROR_NO_ERROR = 0;
774 /** {@hide} */
775 public static final int TETHER_ERROR_UNKNOWN_IFACE = 1;
776 /** {@hide} */
777 public static final int TETHER_ERROR_SERVICE_UNAVAIL = 2;
778 /** {@hide} */
779 public static final int TETHER_ERROR_UNSUPPORTED = 3;
780 /** {@hide} */
781 public static final int TETHER_ERROR_UNAVAIL_IFACE = 4;
782 /** {@hide} */
783 public static final int TETHER_ERROR_MASTER_ERROR = 5;
784 /** {@hide} */
785 public static final int TETHER_ERROR_TETHER_IFACE_ERROR = 6;
786 /** {@hide} */
787 public static final int TETHER_ERROR_UNTETHER_IFACE_ERROR = 7;
788 /** {@hide} */
789 public static final int TETHER_ERROR_ENABLE_NAT_ERROR = 8;
790 /** {@hide} */
791 public static final int TETHER_ERROR_DISABLE_NAT_ERROR = 9;
792 /** {@hide} */
793 public static final int TETHER_ERROR_IFACE_CFG_ERROR = 10;
794
795 /**
796 * @param iface The name of the interface we're interested in
797 * @return error The error code of the last error tethering or untethering the named
798 * interface
799 * {@hide}
800 */
801 public int getLastTetherError(String iface) {
802 try {
803 return mService.getLastTetherError(iface);
804 } catch (RemoteException e) {
805 return TETHER_ERROR_SERVICE_UNAVAIL;
806 }
Robert Greenwalt14f2ef42010-06-15 12:19:37 -0700807 }
808
809 /**
810 * Ensure the device stays awake until we connect with the next network
811 * @param forWhome The name of the network going down for logging purposes
812 * @return {@code true} on success, {@code false} on failure
813 * {@hide}
814 */
815 public boolean requestNetworkTransitionWakelock(String forWhom) {
816 try {
817 mService.requestNetworkTransitionWakelock(forWhom);
818 return true;
819 } catch (RemoteException e) {
820 return false;
821 }
822 }
Robert Greenwaltca4306c2010-09-09 13:15:32 -0700823
Robert Greenwalt67fd6c92010-09-09 14:22:59 -0700824 /**
Robert Greenwaltd7085fc2010-09-08 15:24:47 -0700825 * @param networkType The type of network you want to report on
826 * @param percentage The quality of the connection 0 is bad, 100 is good
827 * {@hide}
828 */
829 public void reportInetCondition(int networkType, int percentage) {
830 try {
831 mService.reportInetCondition(networkType, percentage);
832 } catch (RemoteException e) {
833 }
834 }
Robert Greenwalt434203a2010-10-11 16:00:27 -0700835
836 /**
837 * @param proxyProperties The definition for the new global http proxy
838 * {@hide}
839 */
840 public void setGlobalProxy(ProxyProperties p) {
841 try {
842 mService.setGlobalProxy(p);
843 } catch (RemoteException e) {
844 }
845 }
846
847 /**
848 * @return proxyProperties for the current global proxy
849 * {@hide}
850 */
851 public ProxyProperties getGlobalProxy() {
852 try {
853 return mService.getGlobalProxy();
854 } catch (RemoteException e) {
855 return null;
856 }
857 }
858
859 /**
860 * @return proxyProperties for the current proxy (global if set, network specific if not)
861 * {@hide}
862 */
863 public ProxyProperties getProxy() {
864 try {
865 return mService.getProxy();
866 } catch (RemoteException e) {
867 return null;
868 }
869 }
Robert Greenwaltd55a6b42011-03-25 13:09:25 -0700870
871 /**
872 * @param networkType The network who's dependence has changed
873 * @param met Boolean - true if network use is ok, false if not
874 * {@hide}
875 */
876 public void setDataDependency(int networkType, boolean met) {
877 try {
878 mService.setDataDependency(networkType, met);
879 } catch (RemoteException e) {
880 }
881 }
Robert Greenwalt9b2886e2011-08-31 11:46:42 -0700882
883 /**
884 * Returns true if the hardware supports the given network type
885 * else it returns false. This doesn't indicate we have coverage
886 * or are authorized onto a network, just whether or not the
887 * hardware supports it. For example a gsm phone without a sim
888 * should still return true for mobile data, but a wifi only tablet
889 * would return false.
890 * @param networkType The nework type we'd like to check
891 * @return true if supported, else false
892 * @hide
893 */
894 public boolean isNetworkSupported(int networkType) {
895 try {
896 return mService.isNetworkSupported(networkType);
897 } catch (RemoteException e) {}
898 return false;
899 }
Jeff Sharkey9f7cbf02012-04-12 18:34:54 -0700900
901 /**
902 * Returns if the currently active data network is metered. A network is
903 * classified as metered when the user is sensitive to heavy data usage on
904 * that connection. You should check this before doing large data transfers,
905 * and warn the user or delay the operation until another network is
906 * available.
907 */
908 public boolean isActiveNetworkMetered() {
909 try {
910 return mService.isActiveNetworkMetered();
911 } catch (RemoteException e) {
912 return false;
913 }
914 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800915}