blob: f9208745062a5e331ab3205d5def5e1bcbdc2f1d [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;
Robert Greenwalt665e1ae2012-08-21 19:27:00 -070026import android.os.Messenger;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080027import android.os.RemoteException;
Wink Savilleab9321d2013-06-29 21:10:57 -070028import android.os.ResultReceiver;
Jeff Sharkey961e3042011-08-29 16:02:57 -070029import android.provider.Settings;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080030
Robert Greenwalt585ac0f2010-08-27 09:24:29 -070031import java.net.InetAddress;
Robert Greenwalt585ac0f2010-08-27 09:24:29 -070032
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080033/**
34 * Class that answers queries about the state of network connectivity. It also
35 * notifies applications when network connectivity changes. Get an instance
36 * of this class by calling
37 * {@link android.content.Context#getSystemService(String) Context.getSystemService(Context.CONNECTIVITY_SERVICE)}.
38 * <p>
39 * The primary responsibilities of this class are to:
40 * <ol>
41 * <li>Monitor network connections (Wi-Fi, GPRS, UMTS, etc.)</li>
42 * <li>Send broadcast intents when network connectivity changes</li>
43 * <li>Attempt to "fail over" to another network when connectivity to a network
44 * is lost</li>
45 * <li>Provide an API that allows applications to query the coarse-grained or fine-grained
46 * state of the available networks</li>
47 * </ol>
48 */
Jeff Sharkeyc006f1a2011-05-19 17:12:49 -070049public class ConnectivityManager {
50 private static final String TAG = "ConnectivityManager";
51
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080052 /**
53 * A change in network connectivity has occurred. A connection has either
54 * been established or lost. The NetworkInfo for the affected network is
55 * sent as an extra; it should be consulted to see what kind of
56 * connectivity event occurred.
57 * <p/>
58 * If this is a connection that was the result of failing over from a
59 * disconnected network, then the FAILOVER_CONNECTION boolean extra is
60 * set to true.
61 * <p/>
62 * For a loss of connectivity, if the connectivity manager is attempting
63 * to connect (or has already connected) to another network, the
64 * NetworkInfo for the new network is also passed as an extra. This lets
65 * any receivers of the broadcast know that they should not necessarily
66 * tell the user that no data traffic will be possible. Instead, the
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -080067 * receiver should expect another broadcast soon, indicating either that
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080068 * the failover attempt succeeded (and so there is still overall data
69 * connectivity), or that the failover attempt failed, meaning that all
70 * connectivity has been lost.
71 * <p/>
72 * For a disconnect event, the boolean extra EXTRA_NO_CONNECTIVITY
73 * is set to {@code true} if there are no connected networks at all.
74 */
Jeff Sharkey4fa63b22013-02-20 18:21:19 -080075 @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080076 public static final String CONNECTIVITY_ACTION = "android.net.conn.CONNECTIVITY_CHANGE";
Jeff Sharkeyf0ceede2011-08-02 17:22:34 -070077
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080078 /**
Jeff Sharkey961e3042011-08-29 16:02:57 -070079 * Identical to {@link #CONNECTIVITY_ACTION} broadcast, but sent without any
80 * applicable {@link Settings.Secure#CONNECTIVITY_CHANGE_DELAY}.
81 *
82 * @hide
83 */
Jeff Sharkey4fa63b22013-02-20 18:21:19 -080084 @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION)
Jeff Sharkey961e3042011-08-29 16:02:57 -070085 public static final String CONNECTIVITY_ACTION_IMMEDIATE =
86 "android.net.conn.CONNECTIVITY_CHANGE_IMMEDIATE";
87
88 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080089 * The lookup key for a {@link NetworkInfo} object. Retrieve with
90 * {@link android.content.Intent#getParcelableExtra(String)}.
Jeff Sharkeyf0ceede2011-08-02 17:22:34 -070091 *
92 * @deprecated Since {@link NetworkInfo} can vary based on UID, applications
93 * should always obtain network information through
94 * {@link #getActiveNetworkInfo()} or
95 * {@link #getAllNetworkInfo()}.
Jeff Sharkey75fbb4b2012-08-06 11:41:50 -070096 * @see #EXTRA_NETWORK_TYPE
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080097 */
Jeff Sharkeyf0ceede2011-08-02 17:22:34 -070098 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080099 public static final String EXTRA_NETWORK_INFO = "networkInfo";
Jeff Sharkeyf0ceede2011-08-02 17:22:34 -0700100
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800101 /**
Jeff Sharkey75fbb4b2012-08-06 11:41:50 -0700102 * Network type which triggered a {@link #CONNECTIVITY_ACTION} broadcast.
103 * Can be used with {@link #getNetworkInfo(int)} to get {@link NetworkInfo}
104 * state based on the calling application.
105 *
106 * @see android.content.Intent#getIntExtra(String, int)
107 */
108 public static final String EXTRA_NETWORK_TYPE = "networkType";
109
110 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800111 * The lookup key for a boolean that indicates whether a connect event
112 * is for a network to which the connectivity manager was failing over
113 * following a disconnect on another network.
114 * Retrieve it with {@link android.content.Intent#getBooleanExtra(String,boolean)}.
115 */
116 public static final String EXTRA_IS_FAILOVER = "isFailover";
117 /**
118 * The lookup key for a {@link NetworkInfo} object. This is supplied when
119 * there is another network that it may be possible to connect to. Retrieve with
120 * {@link android.content.Intent#getParcelableExtra(String)}.
121 */
122 public static final String EXTRA_OTHER_NETWORK_INFO = "otherNetwork";
123 /**
124 * The lookup key for a boolean that indicates whether there is a
125 * complete lack of connectivity, i.e., no network is available.
126 * Retrieve it with {@link android.content.Intent#getBooleanExtra(String,boolean)}.
127 */
128 public static final String EXTRA_NO_CONNECTIVITY = "noConnectivity";
129 /**
130 * The lookup key for a string that indicates why an attempt to connect
131 * to a network failed. The string has no particular structure. It is
132 * intended to be used in notifications presented to users. Retrieve
133 * it with {@link android.content.Intent#getStringExtra(String)}.
134 */
135 public static final String EXTRA_REASON = "reason";
136 /**
137 * The lookup key for a string that provides optionally supplied
138 * extra information about the network state. The information
139 * may be passed up from the lower networking layers, and its
140 * meaning may be specific to a particular network type. Retrieve
141 * it with {@link android.content.Intent#getStringExtra(String)}.
142 */
143 public static final String EXTRA_EXTRA_INFO = "extraInfo";
Robert Greenwaltd7085fc2010-09-08 15:24:47 -0700144 /**
145 * The lookup key for an int that provides information about
146 * our connection to the internet at large. 0 indicates no connection,
147 * 100 indicates a great connection. Retrieve it with
Jeff Sharkeyc006f1a2011-05-19 17:12:49 -0700148 * {@link android.content.Intent#getIntExtra(String, int)}.
Robert Greenwaltd7085fc2010-09-08 15:24:47 -0700149 * {@hide}
150 */
151 public static final String EXTRA_INET_CONDITION = "inetCondition";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800152
153 /**
Haoyu Baidb3c8672012-06-20 14:29:57 -0700154 * Broadcast action to indicate the change of data activity status
155 * (idle or active) on a network in a recent period.
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800156 * The network becomes active when data transmission is started, or
157 * idle if there is no data transmission for a period of time.
Haoyu Baidb3c8672012-06-20 14:29:57 -0700158 * {@hide}
159 */
160 @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION)
161 public static final String ACTION_DATA_ACTIVITY_CHANGE = "android.net.conn.DATA_ACTIVITY_CHANGE";
162 /**
163 * The lookup key for an enum that indicates the network device type on which this data activity
164 * change happens.
165 * {@hide}
166 */
167 public static final String EXTRA_DEVICE_TYPE = "deviceType";
168 /**
169 * The lookup key for a boolean that indicates the device is active or not. {@code true} means
170 * it is actively sending or receiving data and {@code false} means it is idle.
171 * {@hide}
172 */
173 public static final String EXTRA_IS_ACTIVE = "isActive";
174
175 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800176 * Broadcast Action: The setting for background data usage has changed
177 * values. Use {@link #getBackgroundDataSetting()} to get the current value.
178 * <p>
179 * If an application uses the network in the background, it should listen
180 * for this broadcast and stop using the background data if the value is
Jeff Sharkeyc006f1a2011-05-19 17:12:49 -0700181 * {@code false}.
Jeff Sharkey54ee2ad2012-01-30 16:29:24 -0800182 * <p>
183 *
184 * @deprecated As of {@link VERSION_CODES#ICE_CREAM_SANDWICH}, availability
185 * of background data depends on several combined factors, and
186 * this broadcast is no longer sent. Instead, when background
187 * data is unavailable, {@link #getActiveNetworkInfo()} will now
188 * appear disconnected. During first boot after a platform
189 * upgrade, this broadcast will be sent once if
190 * {@link #getBackgroundDataSetting()} was {@code false} before
191 * the upgrade.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800192 */
193 @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION)
Jeff Sharkey54ee2ad2012-01-30 16:29:24 -0800194 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800195 public static final String ACTION_BACKGROUND_DATA_SETTING_CHANGED =
196 "android.net.conn.BACKGROUND_DATA_SETTING_CHANGED";
197
Robert Greenwalt1e9aac22010-09-15 17:36:33 -0700198 /**
199 * Broadcast Action: The network connection may not be good
200 * uses {@code ConnectivityManager.EXTRA_INET_CONDITION} and
201 * {@code ConnectivityManager.EXTRA_NETWORK_INFO} to specify
202 * the network and it's condition.
203 * @hide
204 */
Jeff Sharkey4fa63b22013-02-20 18:21:19 -0800205 @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION)
Robert Greenwalt1e9aac22010-09-15 17:36:33 -0700206 public static final String INET_CONDITION_ACTION =
207 "android.net.conn.INET_CONDITION_ACTION";
208
Robert Greenwalt42acef32009-08-12 16:08:25 -0700209 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800210 * Broadcast Action: A tetherable connection has come or gone.
211 * Uses {@code ConnectivityManager.EXTRA_AVAILABLE_TETHER},
212 * {@code ConnectivityManager.EXTRA_ACTIVE_TETHER} and
213 * {@code ConnectivityManager.EXTRA_ERRORED_TETHER} to indicate
214 * the current state of tethering. Each include a list of
215 * interface names in that state (may be empty).
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -0800216 * @hide
217 */
Jeff Sharkey4fa63b22013-02-20 18:21:19 -0800218 @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION)
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -0800219 public static final String ACTION_TETHER_STATE_CHANGED =
220 "android.net.conn.TETHER_STATE_CHANGED";
221
222 /**
223 * @hide
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800224 * gives a String[] listing all the interfaces configured for
225 * tethering and currently available for tethering.
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -0800226 */
Robert Greenwalt2a091d72010-02-11 18:18:40 -0800227 public static final String EXTRA_AVAILABLE_TETHER = "availableArray";
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -0800228
229 /**
230 * @hide
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800231 * gives a String[] listing all the interfaces currently tethered
232 * (ie, has dhcp support and packets potentially forwarded/NATed)
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -0800233 */
Robert Greenwalt2a091d72010-02-11 18:18:40 -0800234 public static final String EXTRA_ACTIVE_TETHER = "activeArray";
235
236 /**
237 * @hide
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800238 * gives a String[] listing all the interfaces we tried to tether and
239 * failed. Use {@link #getLastTetherError} to find the error code
240 * for any interfaces listed here.
Robert Greenwalt2a091d72010-02-11 18:18:40 -0800241 */
242 public static final String EXTRA_ERRORED_TETHER = "erroredArray";
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -0800243
244 /**
Russell Brenner108da0c2013-02-12 10:03:14 -0800245 * Broadcast Action: The captive portal tracker has finished its test.
246 * Sent only while running Setup Wizard, in lieu of showing a user
247 * notification.
248 * @hide
249 */
Jeff Sharkey4fa63b22013-02-20 18:21:19 -0800250 @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION)
Russell Brenner108da0c2013-02-12 10:03:14 -0800251 public static final String ACTION_CAPTIVE_PORTAL_TEST_COMPLETED =
252 "android.net.conn.CAPTIVE_PORTAL_TEST_COMPLETED";
253 /**
254 * The lookup key for a boolean that indicates whether a captive portal was detected.
255 * Retrieve it with {@link android.content.Intent#getBooleanExtra(String,boolean)}.
256 * @hide
257 */
258 public static final String EXTRA_IS_CAPTIVE_PORTAL = "captivePortal";
259
260 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800261 * The absence of a connection type.
Robert Greenwaltccf83af12011-06-02 17:30:47 -0700262 * @hide
263 */
264 public static final int TYPE_NONE = -1;
265
266 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800267 * The Mobile data connection. When active, all data traffic
268 * will use this network type's interface by default
269 * (it has a default route)
Robert Greenwalt42acef32009-08-12 16:08:25 -0700270 */
271 public static final int TYPE_MOBILE = 0;
272 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800273 * The WIFI data connection. When active, all data traffic
274 * will use this network type's interface by default
275 * (it has a default route).
Robert Greenwalt42acef32009-08-12 16:08:25 -0700276 */
277 public static final int TYPE_WIFI = 1;
278 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800279 * An MMS-specific Mobile data connection. This network type may use the
280 * same network interface as {@link #TYPE_MOBILE} or it may use a different
281 * one. This is used by applications needing to talk to the carrier's
282 * Multimedia Messaging Service servers.
Robert Greenwalt42acef32009-08-12 16:08:25 -0700283 */
284 public static final int TYPE_MOBILE_MMS = 2;
285 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800286 * A SUPL-specific Mobile data connection. This network type may use the
287 * same network interface as {@link #TYPE_MOBILE} or it may use a different
288 * one. This is used by applications needing to talk to the carrier's
289 * Secure User Plane Location servers for help locating the device.
Robert Greenwalt42acef32009-08-12 16:08:25 -0700290 */
291 public static final int TYPE_MOBILE_SUPL = 3;
292 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800293 * A DUN-specific Mobile data connection. This network type may use the
294 * same network interface as {@link #TYPE_MOBILE} or it may use a different
295 * one. This is sometimes by the system when setting up an upstream connection
296 * for tethering so that the carrier is aware of DUN traffic.
Robert Greenwalt42acef32009-08-12 16:08:25 -0700297 */
298 public static final int TYPE_MOBILE_DUN = 4;
299 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800300 * A High Priority Mobile data connection. This network type uses the
301 * same network interface as {@link #TYPE_MOBILE} but the routing setup
302 * is different. Only requesting processes will have access to the
303 * Mobile DNS servers and only IP's explicitly requested via {@link #requestRouteToHost}
304 * will route over this interface if no default route exists.
Robert Greenwalt42acef32009-08-12 16:08:25 -0700305 */
306 public static final int TYPE_MOBILE_HIPRI = 5;
jsh8214deb2010-03-11 15:04:43 -0800307 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800308 * The WiMAX data connection. When active, all data traffic
309 * will use this network type's interface by default
310 * (it has a default route).
jsh8214deb2010-03-11 15:04:43 -0800311 */
312 public static final int TYPE_WIMAX = 6;
Robert Greenwaltda3d5e62010-12-06 13:56:24 -0800313
Jaikumar Ganesh15c74392010-12-21 22:31:44 -0800314 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800315 * The Bluetooth data connection. When active, all data traffic
316 * will use this network type's interface by default
317 * (it has a default route).
Jaikumar Ganesh15c74392010-12-21 22:31:44 -0800318 */
319 public static final int TYPE_BLUETOOTH = 7;
320
Robert Greenwalt60810842011-04-22 15:28:18 -0700321 /**
322 * Dummy data connection. This should not be used on shipping devices.
323 */
Jaikumar Ganesh15c74392010-12-21 22:31:44 -0800324 public static final int TYPE_DUMMY = 8;
Wink Saville9d7d6282011-03-12 14:52:01 -0800325
Robert Greenwalt60810842011-04-22 15:28:18 -0700326 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800327 * The Ethernet data connection. When active, all data traffic
328 * will use this network type's interface by default
329 * (it has a default route).
Robert Greenwalt60810842011-04-22 15:28:18 -0700330 */
Robert Greenwalte12aec92011-01-28 14:48:37 -0800331 public static final int TYPE_ETHERNET = 9;
Robert Greenwalt60810842011-04-22 15:28:18 -0700332
Wink Saville9d7d6282011-03-12 14:52:01 -0800333 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800334 * Over the air Administration.
Wink Saville9d7d6282011-03-12 14:52:01 -0800335 * {@hide}
336 */
337 public static final int TYPE_MOBILE_FOTA = 10;
338
339 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800340 * IP Multimedia Subsystem.
Wink Saville9d7d6282011-03-12 14:52:01 -0800341 * {@hide}
342 */
343 public static final int TYPE_MOBILE_IMS = 11;
344
345 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800346 * Carrier Branded Services.
Wink Saville9d7d6282011-03-12 14:52:01 -0800347 * {@hide}
348 */
349 public static final int TYPE_MOBILE_CBS = 12;
350
repo syncaea743a2011-07-29 23:55:49 -0700351 /**
352 * A Wi-Fi p2p connection. Only requesting processes will have access to
353 * the peers connected.
354 * {@hide}
355 */
356 public static final int TYPE_WIFI_P2P = 13;
Wink Saville9d7d6282011-03-12 14:52:01 -0800357
358 /** {@hide} */
repo syncaea743a2011-07-29 23:55:49 -0700359 public static final int MAX_RADIO_TYPE = TYPE_WIFI_P2P;
360
361 /** {@hide} */
362 public static final int MAX_NETWORK_TYPE = TYPE_WIFI_P2P;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800363
Jianzheng Zhoudcf03f32012-11-16 13:45:20 +0800364 /**
365 * If you want to set the default network preference,you can directly
366 * change the networkAttributes array in framework's config.xml.
367 *
368 * @deprecated Since we support so many more networks now, the single
369 * network default network preference can't really express
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800370 * the hierarchy. Instead, the default is defined by the
Jianzheng Zhoudcf03f32012-11-16 13:45:20 +0800371 * networkAttributes in config.xml. You can determine
Robert Greenwalt4c8b7482012-12-07 09:56:50 -0800372 * the current value by calling {@link #getNetworkPreference()}
Jianzheng Zhoudcf03f32012-11-16 13:45:20 +0800373 * from an App.
374 */
375 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800376 public static final int DEFAULT_NETWORK_PREFERENCE = TYPE_WIFI;
377
Jeff Sharkey625239a2012-09-26 22:03:49 -0700378 /**
379 * Default value for {@link Settings.Global#CONNECTIVITY_CHANGE_DELAY} in
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800380 * milliseconds. This was introduced because IPv6 routes seem to take a
381 * moment to settle - trying network activity before the routes are adjusted
382 * can lead to packets using the wrong interface or having the wrong IP address.
383 * This delay is a bit crude, but in the future hopefully we will have kernel
384 * notifications letting us know when it's safe to use the new network.
Jeff Sharkey625239a2012-09-26 22:03:49 -0700385 *
386 * @hide
387 */
388 public static final int CONNECTIVITY_CHANGE_DELAY_DEFAULT = 3000;
389
Jeff Sharkeyc006f1a2011-05-19 17:12:49 -0700390 private final IConnectivityManager mService;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800391
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800392 /**
393 * Tests if a given integer represents a valid network type.
394 * @param networkType the type to be tested
395 * @return a boolean. {@code true} if the type is valid, else {@code false}
396 */
Jeff Sharkeyd2a45872011-05-28 20:56:34 -0700397 public static boolean isNetworkTypeValid(int networkType) {
Robert Greenwalt42acef32009-08-12 16:08:25 -0700398 return networkType >= 0 && networkType <= MAX_NETWORK_TYPE;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800399 }
400
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800401 /**
402 * Returns a non-localized string representing a given network type.
403 * ONLY used for debugging output.
404 * @param type the type needing naming
405 * @return a String for the given type, or a string version of the type ("87")
406 * if no name is known.
407 * {@hide}
408 */
Jeff Sharkeyd2a45872011-05-28 20:56:34 -0700409 public static String getNetworkTypeName(int type) {
410 switch (type) {
411 case TYPE_MOBILE:
412 return "MOBILE";
413 case TYPE_WIFI:
414 return "WIFI";
415 case TYPE_MOBILE_MMS:
416 return "MOBILE_MMS";
417 case TYPE_MOBILE_SUPL:
418 return "MOBILE_SUPL";
419 case TYPE_MOBILE_DUN:
420 return "MOBILE_DUN";
421 case TYPE_MOBILE_HIPRI:
422 return "MOBILE_HIPRI";
423 case TYPE_WIMAX:
424 return "WIMAX";
425 case TYPE_BLUETOOTH:
426 return "BLUETOOTH";
427 case TYPE_DUMMY:
428 return "DUMMY";
429 case TYPE_ETHERNET:
430 return "ETHERNET";
431 case TYPE_MOBILE_FOTA:
432 return "MOBILE_FOTA";
433 case TYPE_MOBILE_IMS:
434 return "MOBILE_IMS";
435 case TYPE_MOBILE_CBS:
436 return "MOBILE_CBS";
repo syncaea743a2011-07-29 23:55:49 -0700437 case TYPE_WIFI_P2P:
438 return "WIFI_P2P";
Jeff Sharkeyd2a45872011-05-28 20:56:34 -0700439 default:
440 return Integer.toString(type);
441 }
442 }
443
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800444 /**
445 * Checks if a given type uses the cellular data connection.
446 * This should be replaced in the future by a network property.
447 * @param networkType the type to check
448 * @return a boolean - {@code true} if uses cellular network, else {@code false}
449 * {@hide}
450 */
Jeff Sharkeyd2a45872011-05-28 20:56:34 -0700451 public static boolean isNetworkTypeMobile(int networkType) {
452 switch (networkType) {
453 case TYPE_MOBILE:
454 case TYPE_MOBILE_MMS:
455 case TYPE_MOBILE_SUPL:
456 case TYPE_MOBILE_DUN:
457 case TYPE_MOBILE_HIPRI:
458 case TYPE_MOBILE_FOTA:
459 case TYPE_MOBILE_IMS:
460 case TYPE_MOBILE_CBS:
461 return true;
462 default:
463 return false;
464 }
465 }
466
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800467 /**
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -0700468 * Checks if the given network type is backed by a Wi-Fi radio.
469 *
470 * @hide
471 */
472 public static boolean isNetworkTypeWifi(int networkType) {
473 switch (networkType) {
474 case TYPE_WIFI:
475 case TYPE_WIFI_P2P:
476 return true;
477 default:
478 return false;
479 }
480 }
481
482 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800483 * Specifies the preferred network type. When the device has more
484 * than one type available the preferred network type will be used.
485 * Note that this made sense when we only had 2 network types,
486 * but with more and more default networks we need an array to list
487 * their ordering. This will be deprecated soon.
488 *
489 * @param preference the network type to prefer over all others. It is
490 * unspecified what happens to the old preferred network in the
491 * overall ordering.
492 */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800493 public void setNetworkPreference(int preference) {
494 try {
495 mService.setNetworkPreference(preference);
496 } catch (RemoteException e) {
497 }
498 }
499
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800500 /**
501 * Retrieves the current preferred network type.
502 * Note that this made sense when we only had 2 network types,
503 * but with more and more default networks we need an array to list
504 * their ordering. This will be deprecated soon.
505 *
506 * @return an integer representing the preferred network type
507 *
508 * <p>This method requires the caller to hold the permission
509 * {@link android.Manifest.permission#ACCESS_NETWORK_STATE}.
510 */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800511 public int getNetworkPreference() {
512 try {
513 return mService.getNetworkPreference();
514 } catch (RemoteException e) {
515 return -1;
516 }
517 }
518
Scott Main671644c2011-10-06 19:02:28 -0700519 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800520 * Returns details about the currently active default data network. When
521 * connected, this network is the default route for outgoing connections.
522 * You should always check {@link NetworkInfo#isConnected()} before initiating
523 * network traffic. This may return {@code null} when there is no default
524 * network.
525 *
526 * @return a {@link NetworkInfo} object for the current default network
527 * or {@code null} if no network default network is currently active
528 *
529 * <p>This method requires the call to hold the permission
Nicolas Falliere9530e3a2012-06-18 17:21:06 -0700530 * {@link android.Manifest.permission#ACCESS_NETWORK_STATE}.
Jeff Sharkey9f7cbf02012-04-12 18:34:54 -0700531 */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800532 public NetworkInfo getActiveNetworkInfo() {
533 try {
534 return mService.getActiveNetworkInfo();
535 } catch (RemoteException e) {
536 return null;
537 }
538 }
539
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800540 /**
541 * Returns details about the currently active default data network
542 * for a given uid. This is for internal use only to avoid spying
543 * other apps.
544 *
545 * @return a {@link NetworkInfo} object for the current default network
546 * for the given uid or {@code null} if no default network is
547 * available for the specified uid.
548 *
549 * <p>This method requires the caller to hold the permission
550 * {@link android.Manifest.permission#CONNECTIVITY_INTERNAL}
551 * {@hide}
552 */
Jeff Sharkeyc006f1a2011-05-19 17:12:49 -0700553 public NetworkInfo getActiveNetworkInfoForUid(int uid) {
554 try {
555 return mService.getActiveNetworkInfoForUid(uid);
556 } catch (RemoteException e) {
557 return null;
558 }
559 }
560
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800561 /**
562 * Returns connection status information about a particular
563 * network type.
564 *
565 * @param networkType integer specifying which networkType in
566 * which you're interested.
567 * @return a {@link NetworkInfo} object for the requested
568 * network type or {@code null} if the type is not
569 * supported by the device.
570 *
571 * <p>This method requires the call to hold the permission
572 * {@link android.Manifest.permission#ACCESS_NETWORK_STATE}.
573 */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800574 public NetworkInfo getNetworkInfo(int networkType) {
575 try {
576 return mService.getNetworkInfo(networkType);
577 } catch (RemoteException e) {
578 return null;
579 }
580 }
581
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800582 /**
583 * Returns connection status information about all network
584 * types supported by the device.
585 *
586 * @return an array of {@link NetworkInfo} objects. Check each
587 * {@link NetworkInfo#getType} for which type each applies.
588 *
589 * <p>This method requires the call to hold the permission
590 * {@link android.Manifest.permission#ACCESS_NETWORK_STATE}.
591 */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800592 public NetworkInfo[] getAllNetworkInfo() {
593 try {
594 return mService.getAllNetworkInfo();
595 } catch (RemoteException e) {
596 return null;
597 }
598 }
599
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800600 /**
601 * Returns the IP information for the current default network.
602 *
603 * @return a {@link LinkProperties} object describing the IP info
604 * for the current default network, or {@code null} if there
605 * is no current default network.
606 *
607 * <p>This method requires the call to hold the permission
608 * {@link android.Manifest.permission#ACCESS_NETWORK_STATE}.
609 * {@hide}
610 */
Robert Greenwaltd192dad2010-09-14 09:18:02 -0700611 public LinkProperties getActiveLinkProperties() {
612 try {
613 return mService.getActiveLinkProperties();
614 } catch (RemoteException e) {
615 return null;
616 }
617 }
618
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800619 /**
620 * Returns the IP information for a given network type.
621 *
622 * @param networkType the network type of interest.
623 * @return a {@link LinkProperties} object describing the IP info
624 * for the given networkType, or {@code null} if there is
625 * no current default network.
626 *
627 * <p>This method requires the call to hold the permission
628 * {@link android.Manifest.permission#ACCESS_NETWORK_STATE}.
629 * {@hide}
630 */
Robert Greenwaltd192dad2010-09-14 09:18:02 -0700631 public LinkProperties getLinkProperties(int networkType) {
632 try {
633 return mService.getLinkProperties(networkType);
634 } catch (RemoteException e) {
635 return null;
636 }
637 }
638
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800639 /**
640 * Tells each network type to set its radio power state as directed.
641 *
642 * @param turnOn a boolean, {@code true} to turn the radios on,
643 * {@code false} to turn them off.
644 * @return a boolean, {@code true} indicating success. All network types
645 * will be tried, even if some fail.
646 *
647 * <p>This method requires the call to hold the permission
648 * {@link android.Manifest.permission#CHANGE_NETWORK_STATE}.
649 * {@hide}
650 */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800651 public boolean setRadios(boolean turnOn) {
652 try {
653 return mService.setRadios(turnOn);
654 } catch (RemoteException e) {
655 return false;
656 }
657 }
658
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800659 /**
660 * Tells a given networkType to set its radio power state as directed.
661 *
662 * @param networkType the int networkType of interest.
663 * @param turnOn a boolean, {@code true} to turn the radio on,
664 * {@code} false to turn it off.
665 * @return a boolean, {@code true} indicating success.
666 *
667 * <p>This method requires the call to hold the permission
668 * {@link android.Manifest.permission#CHANGE_NETWORK_STATE}.
669 * {@hide}
670 */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800671 public boolean setRadio(int networkType, boolean turnOn) {
672 try {
673 return mService.setRadio(networkType, turnOn);
674 } catch (RemoteException e) {
675 return false;
676 }
677 }
678
679 /**
680 * Tells the underlying networking system that the caller wants to
681 * begin using the named feature. The interpretation of {@code feature}
682 * is completely up to each networking implementation.
Nicolas Falliere9530e3a2012-06-18 17:21:06 -0700683 * <p>This method requires the caller to hold the permission
684 * {@link android.Manifest.permission#CHANGE_NETWORK_STATE}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800685 * @param networkType specifies which network the request pertains to
686 * @param feature the name of the feature to be used
687 * @return an integer value representing the outcome of the request.
688 * The interpretation of this value is specific to each networking
689 * implementation+feature combination, except that the value {@code -1}
690 * always indicates failure.
691 */
692 public int startUsingNetworkFeature(int networkType, String feature) {
693 try {
Robert Greenwalt42acef32009-08-12 16:08:25 -0700694 return mService.startUsingNetworkFeature(networkType, feature,
695 new Binder());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800696 } catch (RemoteException e) {
697 return -1;
698 }
699 }
700
701 /**
702 * Tells the underlying networking system that the caller is finished
703 * using the named feature. The interpretation of {@code feature}
704 * is completely up to each networking implementation.
Nicolas Falliere9530e3a2012-06-18 17:21:06 -0700705 * <p>This method requires the caller to hold the permission
706 * {@link android.Manifest.permission#CHANGE_NETWORK_STATE}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800707 * @param networkType specifies which network the request pertains to
708 * @param feature the name of the feature that is no longer needed
709 * @return an integer value representing the outcome of the request.
710 * The interpretation of this value is specific to each networking
711 * implementation+feature combination, except that the value {@code -1}
712 * always indicates failure.
713 */
714 public int stopUsingNetworkFeature(int networkType, String feature) {
715 try {
716 return mService.stopUsingNetworkFeature(networkType, feature);
717 } catch (RemoteException e) {
718 return -1;
719 }
720 }
721
722 /**
723 * Ensure that a network route exists to deliver traffic to the specified
724 * host via the specified network interface. An attempt to add a route that
725 * already exists is ignored, but treated as successful.
Nicolas Falliere9530e3a2012-06-18 17:21:06 -0700726 * <p>This method requires the caller to hold the permission
727 * {@link android.Manifest.permission#CHANGE_NETWORK_STATE}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800728 * @param networkType the type of the network over which traffic to the specified
729 * host is to be routed
730 * @param hostAddress the IP address of the host to which the route is desired
731 * @return {@code true} on success, {@code false} on failure
732 */
733 public boolean requestRouteToHost(int networkType, int hostAddress) {
Robert Greenwalt585ac0f2010-08-27 09:24:29 -0700734 InetAddress inetAddress = NetworkUtils.intToInetAddress(hostAddress);
735
736 if (inetAddress == null) {
737 return false;
738 }
739
740 return requestRouteToHostAddress(networkType, inetAddress);
741 }
742
743 /**
744 * Ensure that a network route exists to deliver traffic to the specified
745 * host via the specified network interface. An attempt to add a route that
746 * already exists is ignored, but treated as successful.
747 * @param networkType the type of the network over which traffic to the specified
748 * host is to be routed
749 * @param hostAddress the IP address of the host to which the route is desired
750 * @return {@code true} on success, {@code false} on failure
751 * @hide
752 */
753 public boolean requestRouteToHostAddress(int networkType, InetAddress hostAddress) {
754 byte[] address = hostAddress.getAddress();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800755 try {
Robert Greenwalt585ac0f2010-08-27 09:24:29 -0700756 return mService.requestRouteToHostAddress(networkType, address);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800757 } catch (RemoteException e) {
758 return false;
759 }
760 }
761
762 /**
763 * Returns the value of the setting for background data usage. If false,
764 * applications should not use the network if the application is not in the
765 * foreground. Developers should respect this setting, and check the value
766 * of this before performing any background data operations.
767 * <p>
768 * All applications that have background services that use the network
769 * should listen to {@link #ACTION_BACKGROUND_DATA_SETTING_CHANGED}.
Jeff Sharkey3a844fc2011-08-16 14:37:57 -0700770 * <p>
Scott Main4cc53332011-10-06 18:32:43 -0700771 * @deprecated As of {@link VERSION_CODES#ICE_CREAM_SANDWICH}, availability of
Jeff Sharkey3a844fc2011-08-16 14:37:57 -0700772 * background data depends on several combined factors, and this method will
773 * always return {@code true}. Instead, when background data is unavailable,
774 * {@link #getActiveNetworkInfo()} will now appear disconnected.
Danica Chang6fdd0c62010-08-11 14:54:43 -0700775 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800776 * @return Whether background data usage is allowed.
777 */
Jeff Sharkey3a844fc2011-08-16 14:37:57 -0700778 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800779 public boolean getBackgroundDataSetting() {
Jeff Sharkey3a844fc2011-08-16 14:37:57 -0700780 // assume that background data is allowed; final authority is
781 // NetworkInfo which may be blocked.
782 return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800783 }
784
785 /**
786 * Sets the value of the setting for background data usage.
Robert Greenwaltc03fa502010-02-23 18:58:05 -0800787 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800788 * @param allowBackgroundData Whether an application should use data while
789 * it is in the background.
Robert Greenwaltc03fa502010-02-23 18:58:05 -0800790 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800791 * @attr ref android.Manifest.permission#CHANGE_BACKGROUND_DATA_SETTING
792 * @see #getBackgroundDataSetting()
793 * @hide
794 */
Jeff Sharkey3a844fc2011-08-16 14:37:57 -0700795 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800796 public void setBackgroundDataSetting(boolean allowBackgroundData) {
Jeff Sharkey3a844fc2011-08-16 14:37:57 -0700797 // ignored
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800798 }
Robert Greenwaltc03fa502010-02-23 18:58:05 -0800799
800 /**
Jeff Sharkeyf0ceede2011-08-02 17:22:34 -0700801 * Return quota status for the current active network, or {@code null} if no
802 * network is active. Quota status can change rapidly, so these values
803 * shouldn't be cached.
Jeff Sharkey44a3e0d2011-10-06 10:50:09 -0700804 *
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800805 * <p>This method requires the call to hold the permission
806 * {@link android.Manifest.permission#ACCESS_NETWORK_STATE}.
807 *
Jeff Sharkey44a3e0d2011-10-06 10:50:09 -0700808 * @hide
Jeff Sharkeyf0ceede2011-08-02 17:22:34 -0700809 */
810 public NetworkQuotaInfo getActiveNetworkQuotaInfo() {
811 try {
812 return mService.getActiveNetworkQuotaInfo();
813 } catch (RemoteException e) {
814 return null;
815 }
816 }
817
818 /**
Robert Greenwaltc03fa502010-02-23 18:58:05 -0800819 * Gets the value of the setting for enabling Mobile data.
820 *
821 * @return Whether mobile data is enabled.
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800822 *
823 * <p>This method requires the call to hold the permission
824 * {@link android.Manifest.permission#ACCESS_NETWORK_STATE}.
Robert Greenwaltc03fa502010-02-23 18:58:05 -0800825 * @hide
826 */
827 public boolean getMobileDataEnabled() {
828 try {
829 return mService.getMobileDataEnabled();
830 } catch (RemoteException e) {
831 return true;
832 }
833 }
834
835 /**
836 * Sets the persisted value for enabling/disabling Mobile data.
837 *
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800838 * @param enabled Whether the user wants the mobile data connection used
839 * or not.
Robert Greenwaltc03fa502010-02-23 18:58:05 -0800840 * @hide
841 */
842 public void setMobileDataEnabled(boolean enabled) {
843 try {
844 mService.setMobileDataEnabled(enabled);
845 } catch (RemoteException e) {
846 }
847 }
848
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800849 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800850 * {@hide}
851 */
852 public ConnectivityManager(IConnectivityManager service) {
Jeff Sharkeyf0ceede2011-08-02 17:22:34 -0700853 mService = checkNotNull(service, "missing IConnectivityManager");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800854 }
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -0800855
Jeff Sharkey8fc27e82012-04-04 20:40:58 -0700856 /** {@hide} */
857 public static ConnectivityManager from(Context context) {
858 return (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
859 }
860
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -0800861 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800862 * Get the set of tetherable, available interfaces. This list is limited by
863 * device configuration and current interface existence.
864 *
865 * @return an array of 0 or more Strings of tetherable interface names.
866 *
867 * <p>This method requires the call to hold the permission
868 * {@link android.Manifest.permission#ACCESS_NETWORK_STATE}.
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -0800869 * {@hide}
870 */
871 public String[] getTetherableIfaces() {
872 try {
873 return mService.getTetherableIfaces();
874 } catch (RemoteException e) {
875 return new String[0];
876 }
877 }
878
879 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800880 * Get the set of tethered interfaces.
881 *
882 * @return an array of 0 or more String of currently tethered interface names.
883 *
884 * <p>This method requires the call to hold the permission
885 * {@link android.Manifest.permission#ACCESS_NETWORK_STATE}.
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -0800886 * {@hide}
887 */
888 public String[] getTetheredIfaces() {
889 try {
890 return mService.getTetheredIfaces();
891 } catch (RemoteException e) {
892 return new String[0];
893 }
894 }
895
896 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800897 * Get the set of interface names which attempted to tether but
898 * failed. Re-attempting to tether may cause them to reset to the Tethered
899 * state. Alternatively, causing the interface to be destroyed and recreated
900 * may cause them to reset to the available state.
901 * {@link ConnectivityManager#getLastTetherError} can be used to get more
902 * information on the cause of the errors.
903 *
904 * @return an array of 0 or more String indicating the interface names
905 * which failed to tether.
906 *
907 * <p>This method requires the call to hold the permission
908 * {@link android.Manifest.permission#ACCESS_NETWORK_STATE}.
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -0800909 * {@hide}
910 */
Robert Greenwalt5a735062010-03-02 17:25:02 -0800911 public String[] getTetheringErroredIfaces() {
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -0800912 try {
Robert Greenwalt5a735062010-03-02 17:25:02 -0800913 return mService.getTetheringErroredIfaces();
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -0800914 } catch (RemoteException e) {
Robert Greenwalt5a735062010-03-02 17:25:02 -0800915 return new String[0];
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -0800916 }
917 }
918
919 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800920 * Attempt to tether the named interface. This will setup a dhcp server
921 * on the interface, forward and NAT IP packets and forward DNS requests
922 * to the best active upstream network interface. Note that if no upstream
923 * IP network interface is available, dhcp will still run and traffic will be
924 * allowed between the tethered devices and this device, though upstream net
925 * access will of course fail until an upstream network interface becomes
926 * active.
927 *
928 * @param iface the interface name to tether.
929 * @return error a {@code TETHER_ERROR} value indicating success or failure type
930 *
931 * <p>This method requires the call to hold the permission
932 * {@link android.Manifest.permission#CHANGE_NETWORK_STATE}.
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -0800933 * {@hide}
934 */
Robert Greenwalt5a735062010-03-02 17:25:02 -0800935 public int tether(String iface) {
936 try {
937 return mService.tether(iface);
938 } catch (RemoteException e) {
939 return TETHER_ERROR_SERVICE_UNAVAIL;
940 }
941 }
942
943 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800944 * Stop tethering the named interface.
945 *
946 * @param iface the interface name to untether.
947 * @return error a {@code TETHER_ERROR} value indicating success or failure type
948 *
949 * <p>This method requires the call to hold the permission
950 * {@link android.Manifest.permission#CHANGE_NETWORK_STATE}.
Robert Greenwalt5a735062010-03-02 17:25:02 -0800951 * {@hide}
952 */
953 public int untether(String iface) {
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -0800954 try {
955 return mService.untether(iface);
956 } catch (RemoteException e) {
Robert Greenwalt5a735062010-03-02 17:25:02 -0800957 return TETHER_ERROR_SERVICE_UNAVAIL;
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -0800958 }
959 }
Robert Greenwalt2a091d72010-02-11 18:18:40 -0800960
961 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800962 * Check if the device allows for tethering. It may be disabled via
963 * {@code ro.tether.denied} system property, {@link Settings#TETHER_SUPPORTED} or
964 * due to device configuration.
965 *
966 * @return a boolean - {@code true} indicating Tethering is supported.
967 *
968 * <p>This method requires the call to hold the permission
969 * {@link android.Manifest.permission#ACCESS_NETWORK_STATE}.
Robert Greenwalt2a091d72010-02-11 18:18:40 -0800970 * {@hide}
971 */
972 public boolean isTetheringSupported() {
973 try {
974 return mService.isTetheringSupported();
975 } catch (RemoteException e) {
976 return false;
977 }
978 }
979
980 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800981 * Get the list of regular expressions that define any tetherable
982 * USB network interfaces. If USB tethering is not supported by the
983 * device, this list should be empty.
984 *
985 * @return an array of 0 or more regular expression Strings defining
986 * what interfaces are considered tetherable usb interfaces.
987 *
988 * <p>This method requires the call to hold the permission
989 * {@link android.Manifest.permission#ACCESS_NETWORK_STATE}.
Robert Greenwalt2a091d72010-02-11 18:18:40 -0800990 * {@hide}
991 */
992 public String[] getTetherableUsbRegexs() {
993 try {
994 return mService.getTetherableUsbRegexs();
995 } catch (RemoteException e) {
996 return new String[0];
997 }
998 }
999
1000 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08001001 * Get the list of regular expressions that define any tetherable
1002 * Wifi network interfaces. If Wifi tethering is not supported by the
1003 * device, this list should be empty.
1004 *
1005 * @return an array of 0 or more regular expression Strings defining
1006 * what interfaces are considered tetherable wifi interfaces.
1007 *
1008 * <p>This method requires the call to hold the permission
1009 * {@link android.Manifest.permission#ACCESS_NETWORK_STATE}.
Robert Greenwalt2a091d72010-02-11 18:18:40 -08001010 * {@hide}
1011 */
1012 public String[] getTetherableWifiRegexs() {
1013 try {
1014 return mService.getTetherableWifiRegexs();
1015 } catch (RemoteException e) {
1016 return new String[0];
1017 }
1018 }
Robert Greenwalt5a735062010-03-02 17:25:02 -08001019
Danica Chang6fdd0c62010-08-11 14:54:43 -07001020 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08001021 * Get the list of regular expressions that define any tetherable
1022 * Bluetooth network interfaces. If Bluetooth tethering is not supported by the
1023 * device, this list should be empty.
1024 *
1025 * @return an array of 0 or more regular expression Strings defining
1026 * what interfaces are considered tetherable bluetooth interfaces.
1027 *
1028 * <p>This method requires the call to hold the permission
1029 * {@link android.Manifest.permission#ACCESS_NETWORK_STATE}.
Danica Chang6fdd0c62010-08-11 14:54:43 -07001030 * {@hide}
1031 */
1032 public String[] getTetherableBluetoothRegexs() {
1033 try {
1034 return mService.getTetherableBluetoothRegexs();
1035 } catch (RemoteException e) {
1036 return new String[0];
1037 }
1038 }
1039
Mike Lockwood6c2260b2011-07-19 13:04:47 -07001040 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08001041 * Attempt to both alter the mode of USB and Tethering of USB. A
1042 * utility method to deal with some of the complexity of USB - will
1043 * attempt to switch to Rndis and subsequently tether the resulting
1044 * interface on {@code true} or turn off tethering and switch off
1045 * Rndis on {@code false}.
1046 *
1047 * @param enable a boolean - {@code true} to enable tethering
1048 * @return error a {@code TETHER_ERROR} value indicating success or failure type
1049 *
1050 * <p>This method requires the call to hold the permission
1051 * {@link android.Manifest.permission#CHANGE_NETWORK_STATE}.
Mike Lockwood6c2260b2011-07-19 13:04:47 -07001052 * {@hide}
1053 */
1054 public int setUsbTethering(boolean enable) {
1055 try {
1056 return mService.setUsbTethering(enable);
1057 } catch (RemoteException e) {
1058 return TETHER_ERROR_SERVICE_UNAVAIL;
1059 }
1060 }
1061
Robert Greenwalt5a735062010-03-02 17:25:02 -08001062 /** {@hide} */
1063 public static final int TETHER_ERROR_NO_ERROR = 0;
1064 /** {@hide} */
1065 public static final int TETHER_ERROR_UNKNOWN_IFACE = 1;
1066 /** {@hide} */
1067 public static final int TETHER_ERROR_SERVICE_UNAVAIL = 2;
1068 /** {@hide} */
1069 public static final int TETHER_ERROR_UNSUPPORTED = 3;
1070 /** {@hide} */
1071 public static final int TETHER_ERROR_UNAVAIL_IFACE = 4;
1072 /** {@hide} */
1073 public static final int TETHER_ERROR_MASTER_ERROR = 5;
1074 /** {@hide} */
1075 public static final int TETHER_ERROR_TETHER_IFACE_ERROR = 6;
1076 /** {@hide} */
1077 public static final int TETHER_ERROR_UNTETHER_IFACE_ERROR = 7;
1078 /** {@hide} */
1079 public static final int TETHER_ERROR_ENABLE_NAT_ERROR = 8;
1080 /** {@hide} */
1081 public static final int TETHER_ERROR_DISABLE_NAT_ERROR = 9;
1082 /** {@hide} */
1083 public static final int TETHER_ERROR_IFACE_CFG_ERROR = 10;
1084
1085 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08001086 * Get a more detailed error code after a Tethering or Untethering
1087 * request asynchronously failed.
1088 *
1089 * @param iface The name of the interface of interest
Robert Greenwalt5a735062010-03-02 17:25:02 -08001090 * @return error The error code of the last error tethering or untethering the named
1091 * interface
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08001092 *
1093 * <p>This method requires the call to hold the permission
1094 * {@link android.Manifest.permission#ACCESS_NETWORK_STATE}.
Robert Greenwalt5a735062010-03-02 17:25:02 -08001095 * {@hide}
1096 */
1097 public int getLastTetherError(String iface) {
1098 try {
1099 return mService.getLastTetherError(iface);
1100 } catch (RemoteException e) {
1101 return TETHER_ERROR_SERVICE_UNAVAIL;
1102 }
Robert Greenwalt14f2ef42010-06-15 12:19:37 -07001103 }
1104
1105 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08001106 * Try to ensure the device stays awake until we connect with the next network.
1107 * Actually just holds a wakelock for a number of seconds while we try to connect
1108 * to any default networks. This will expire if the timeout passes or if we connect
1109 * to a default after this is called. For internal use only.
1110 *
1111 * @param forWhom the name of the network going down for logging purposes
Robert Greenwalt14f2ef42010-06-15 12:19:37 -07001112 * @return {@code true} on success, {@code false} on failure
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08001113 *
1114 * <p>This method requires the call to hold the permission
1115 * {@link android.Manifest.permission#CONNECTIVITY_INTERNAL}.
Robert Greenwalt14f2ef42010-06-15 12:19:37 -07001116 * {@hide}
1117 */
1118 public boolean requestNetworkTransitionWakelock(String forWhom) {
1119 try {
1120 mService.requestNetworkTransitionWakelock(forWhom);
1121 return true;
1122 } catch (RemoteException e) {
1123 return false;
1124 }
1125 }
Robert Greenwaltca4306c2010-09-09 13:15:32 -07001126
Robert Greenwalt67fd6c92010-09-09 14:22:59 -07001127 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08001128 * Report network connectivity status. This is currently used only
1129 * to alter status bar UI.
1130 *
Robert Greenwaltd7085fc2010-09-08 15:24:47 -07001131 * @param networkType The type of network you want to report on
1132 * @param percentage The quality of the connection 0 is bad, 100 is good
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08001133 *
1134 * <p>This method requires the call to hold the permission
1135 * {@link android.Manifest.permission#STATUS_BAR}.
Robert Greenwaltd7085fc2010-09-08 15:24:47 -07001136 * {@hide}
1137 */
1138 public void reportInetCondition(int networkType, int percentage) {
1139 try {
1140 mService.reportInetCondition(networkType, percentage);
1141 } catch (RemoteException e) {
1142 }
1143 }
Robert Greenwalt434203a2010-10-11 16:00:27 -07001144
1145 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08001146 * Set a network-independent global http proxy. This is not normally what you want
1147 * for typical HTTP proxies - they are general network dependent. However if you're
1148 * doing something unusual like general internal filtering this may be useful. On
1149 * a private network where the proxy is not accessible, you may break HTTP using this.
1150 *
1151 * @param proxyProperties The a {@link ProxyProperites} object defining the new global
1152 * HTTP proxy. A {@code null} value will clear the global HTTP proxy.
1153 *
1154 * <p>This method requires the call to hold the permission
Robert Greenwalta9bebc22013-04-10 15:32:18 -07001155 * {@link android.Manifest.permission#CONNECTIVITY_INTERNAL}.
Robert Greenwalt434203a2010-10-11 16:00:27 -07001156 * {@hide}
1157 */
1158 public void setGlobalProxy(ProxyProperties p) {
1159 try {
1160 mService.setGlobalProxy(p);
1161 } catch (RemoteException e) {
1162 }
1163 }
1164
1165 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08001166 * Retrieve any network-independent global HTTP proxy.
1167 *
1168 * @return {@link ProxyProperties} for the current global HTTP proxy or {@code null}
1169 * if no global HTTP proxy is set.
1170 *
1171 * <p>This method requires the call to hold the permission
1172 * {@link android.Manifest.permission#ACCESS_NETWORK_STATE}.
Robert Greenwalt434203a2010-10-11 16:00:27 -07001173 * {@hide}
1174 */
1175 public ProxyProperties getGlobalProxy() {
1176 try {
1177 return mService.getGlobalProxy();
1178 } catch (RemoteException e) {
1179 return null;
1180 }
1181 }
1182
1183 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08001184 * Get the HTTP proxy settings for the current default network. Note that
1185 * if a global proxy is set, it will override any per-network setting.
1186 *
1187 * @return the {@link ProxyProperties} for the current HTTP proxy, or {@code null} if no
1188 * HTTP proxy is active.
1189 *
1190 * <p>This method requires the call to hold the permission
1191 * {@link android.Manifest.permission#ACCESS_NETWORK_STATE}.
Robert Greenwalt434203a2010-10-11 16:00:27 -07001192 * {@hide}
1193 */
1194 public ProxyProperties getProxy() {
1195 try {
1196 return mService.getProxy();
1197 } catch (RemoteException e) {
1198 return null;
1199 }
1200 }
Robert Greenwaltd55a6b42011-03-25 13:09:25 -07001201
1202 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08001203 * Sets a secondary requirement bit for the given networkType.
1204 * This requirement bit is generally under the control of the carrier
1205 * or its agents and is not directly controlled by the user.
1206 *
Robert Greenwaltd55a6b42011-03-25 13:09:25 -07001207 * @param networkType The network who's dependence has changed
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08001208 * @param met Boolean - true if network use is OK, false if not
1209 *
1210 * <p>This method requires the call to hold the permission
1211 * {@link android.Manifest.permission#CONNECTIVITY_INTERNAL}.
Robert Greenwaltd55a6b42011-03-25 13:09:25 -07001212 * {@hide}
1213 */
1214 public void setDataDependency(int networkType, boolean met) {
1215 try {
1216 mService.setDataDependency(networkType, met);
1217 } catch (RemoteException e) {
1218 }
1219 }
Robert Greenwalt9b2886e2011-08-31 11:46:42 -07001220
1221 /**
1222 * Returns true if the hardware supports the given network type
1223 * else it returns false. This doesn't indicate we have coverage
1224 * or are authorized onto a network, just whether or not the
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08001225 * hardware supports it. For example a GSM phone without a SIM
1226 * should still return {@code true} for mobile data, but a wifi only
1227 * tablet would return {@code false}.
1228 *
1229 * @param networkType The network type we'd like to check
1230 * @return {@code true} if supported, else {@code false}
1231 *
1232 * <p>This method requires the call to hold the permission
1233 * {@link android.Manifest.permission#ACCESS_NETWORK_STATE}.
Robert Greenwalt9b2886e2011-08-31 11:46:42 -07001234 * @hide
1235 */
1236 public boolean isNetworkSupported(int networkType) {
1237 try {
1238 return mService.isNetworkSupported(networkType);
1239 } catch (RemoteException e) {}
1240 return false;
1241 }
Jeff Sharkey9f7cbf02012-04-12 18:34:54 -07001242
1243 /**
1244 * Returns if the currently active data network is metered. A network is
1245 * classified as metered when the user is sensitive to heavy data usage on
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08001246 * that connection due to monetary costs, data limitations or
1247 * battery/performance issues. You should check this before doing large
1248 * data transfers, and warn the user or delay the operation until another
1249 * network is available.
1250 *
1251 * @return {@code true} if large transfers should be avoided, otherwise
1252 * {@code false}.
1253 *
1254 * <p>This method requires the call to hold the permission
1255 * {@link android.Manifest.permission#ACCESS_NETWORK_STATE}.
Jeff Sharkey9f7cbf02012-04-12 18:34:54 -07001256 */
1257 public boolean isActiveNetworkMetered() {
1258 try {
1259 return mService.isActiveNetworkMetered();
1260 } catch (RemoteException e) {
1261 return false;
1262 }
1263 }
Jeff Sharkey69ddab42012-08-25 00:05:46 -07001264
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08001265 /**
1266 * If the LockdownVpn mechanism is enabled, updates the vpn
1267 * with a reload of its profile.
1268 *
1269 * @return a boolean with {@code} indicating success
1270 *
1271 * <p>This method can only be called by the system UID
1272 * {@hide}
1273 */
Jeff Sharkey69ddab42012-08-25 00:05:46 -07001274 public boolean updateLockdownVpn() {
1275 try {
1276 return mService.updateLockdownVpn();
1277 } catch (RemoteException e) {
1278 return false;
1279 }
1280 }
Irfan Sheriffda6da092012-08-16 12:49:23 -07001281
1282 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08001283 * Signal that the captive portal check on the indicated network
1284 * is complete and we can turn the network on for general use.
1285 *
1286 * @param info the {@link NetworkInfo} object for the networkType
1287 * in question.
1288 *
1289 * <p>This method requires the call to hold the permission
1290 * {@link android.Manifest.permission#CONNECTIVITY_INTERNAL}.
Irfan Sheriffda6da092012-08-16 12:49:23 -07001291 * {@hide}
1292 */
1293 public void captivePortalCheckComplete(NetworkInfo info) {
1294 try {
1295 mService.captivePortalCheckComplete(info);
1296 } catch (RemoteException e) {
1297 }
1298 }
1299
Robert Greenwalt665e1ae2012-08-21 19:27:00 -07001300 /**
1301 * Supply the backend messenger for a network tracker
1302 *
1303 * @param type NetworkType to set
1304 * @param messenger {@link Messenger}
1305 * {@hide}
1306 */
1307 public void supplyMessenger(int networkType, Messenger messenger) {
1308 try {
1309 mService.supplyMessenger(networkType, messenger);
1310 } catch (RemoteException e) {
1311 }
1312 }
Wink Savilleab9321d2013-06-29 21:10:57 -07001313
1314 /**
1315 * The ResultReceiver resultCode for checkMobileProvisioning (CMP_RESULT_CODE)
1316 */
1317
1318 /**
1319 * No connection was possible to the network.
1320 * {@hide}
1321 */
1322 public static final int CMP_RESULT_CODE_NO_CONNECTION = 0;
1323
1324 /**
1325 * A connection was made to the internet, all is well.
1326 * {@hide}
1327 */
1328 public static final int CMP_RESULT_CODE_CONNECTABLE = 1;
1329
1330 /**
1331 * A connection was made but there was a redirection, we appear to be in walled garden.
1332 * This is an indication of a warm sim on a mobile network.
1333 * {@hide}
1334 */
1335 public static final int CMP_RESULT_CODE_REDIRECTED = 2;
1336
1337 /**
1338 * A connection was made but no dns server was available to resolve a name to address.
1339 * This is an indication of a warm sim on a mobile network.
1340 *
1341 * {@hide}
1342 */
1343 public static final int CMP_RESULT_CODE_NO_DNS = 3;
1344
1345 /**
1346 * A connection was made but could not open a TCP connection.
1347 * This is an indication of a warm sim on a mobile network.
1348 * {@hide}
1349 */
1350 public static final int CMP_RESULT_CODE_NO_TCP_CONNECTION = 4;
1351
1352 /**
1353 * Check mobile provisioning. The resultCode passed to
1354 * onReceiveResult will be one of the CMP_RESULT_CODE_xxxx values above.
1355 * This may take a minute or more to complete.
1356 *
1357 * @param sendNotificaiton, when true a notification will be sent to user.
1358 * @param suggestedTimeOutMs, timeout in milliseconds
1359 * @param resultReceiver needs to be supplied to receive the result
1360 *
1361 * @return time out that will be used, maybe less that suggestedTimeOutMs
1362 * -1 if an error.
1363 *
1364 * {@hide}
1365 */
1366 public int checkMobileProvisioning(boolean sendNotification, int suggestedTimeOutMs,
1367 ResultReceiver resultReceiver) {
1368 int timeOutMs = -1;
1369 try {
1370 timeOutMs = mService.checkMobileProvisioning(sendNotification, suggestedTimeOutMs,
1371 resultReceiver);
1372 } catch (RemoteException e) {
1373 }
1374 return timeOutMs;
1375 }
Robert Greenwalte182bfe2013-07-16 12:06:09 -07001376
1377 /**
1378 * Get the carrier provisioning url.
1379 * {@hide}
1380 */
1381 public String getMobileProvisioningUrl() {
1382 try {
1383 return mService.getMobileProvisioningUrl();
1384 } catch (RemoteException e) {
1385 }
1386 return null;
1387 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001388}