blob: 9a2a24128b9235b242618f544ea3306b89c03ebc [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 */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016package android.net;
17
Jeff Sharkeyf0ceede2011-08-02 17:22:34 -070018import static com.android.internal.util.Preconditions.checkNotNull;
19
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080020import android.annotation.SdkConstant;
21import android.annotation.SdkConstant.SdkConstantType;
Robert Greenwalt9258c642014-03-26 16:47:06 -070022import android.app.PendingIntent;
Jeff Sharkey8fc27e82012-04-04 20:40:58 -070023import android.content.Context;
Robert Greenwaltd19c41c2014-05-18 23:07:25 -070024import android.content.Intent;
Lorenzo Colittiffc42b02015-07-29 11:41:21 +090025import android.content.pm.PackageManager;
Paul Jensen6d3ff9e2014-05-29 10:12:39 -040026import android.net.NetworkUtils;
Robert Greenwalt42acef32009-08-12 16:08:25 -070027import android.os.Binder;
Jeff Sharkey3a844fc2011-08-16 14:37:57 -070028import android.os.Build.VERSION_CODES;
Robert Greenwalt9258c642014-03-26 16:47:06 -070029import android.os.Handler;
30import android.os.HandlerThread;
Dianne Hackborn77b987f2014-02-26 16:20:52 -080031import android.os.IBinder;
32import android.os.INetworkActivityListener;
33import android.os.INetworkManagementService;
Robert Greenwalt9258c642014-03-26 16:47:06 -070034import android.os.Looper;
35import android.os.Message;
Robert Greenwalt665e1ae2012-08-21 19:27:00 -070036import android.os.Messenger;
Lorenzo Colittiffc42b02015-07-29 11:41:21 +090037import android.os.Process;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080038import android.os.RemoteException;
Dianne Hackborn77b987f2014-02-26 16:20:52 -080039import android.os.ServiceManager;
Jeff Sharkey961e3042011-08-29 16:02:57 -070040import android.provider.Settings;
Wink Saville36ffb042014-12-05 11:10:30 -080041import android.telephony.SubscriptionManager;
Dianne Hackborn77b987f2014-02-26 16:20:52 -080042import android.util.ArrayMap;
Robert Greenwalt9258c642014-03-26 16:47:06 -070043import android.util.Log;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080044
Robert Greenwaltafa05c02014-05-21 20:04:36 -070045import com.android.internal.telephony.ITelephony;
Robert Greenwalt562cc542014-05-15 18:07:26 -070046import com.android.internal.telephony.PhoneConstants;
Robert Greenwaltafa05c02014-05-21 20:04:36 -070047import com.android.internal.util.Protocol;
48
Robert Greenwalt585ac0f2010-08-27 09:24:29 -070049import java.net.InetAddress;
Robert Greenwalt9258c642014-03-26 16:47:06 -070050import java.util.concurrent.atomic.AtomicInteger;
51import java.util.HashMap;
52
Paul Jensenc91b5342014-08-27 12:38:45 -040053import libcore.net.event.NetworkEventDispatcher;
54
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080055/**
56 * Class that answers queries about the state of network connectivity. It also
57 * notifies applications when network connectivity changes. Get an instance
58 * of this class by calling
59 * {@link android.content.Context#getSystemService(String) Context.getSystemService(Context.CONNECTIVITY_SERVICE)}.
60 * <p>
61 * The primary responsibilities of this class are to:
62 * <ol>
63 * <li>Monitor network connections (Wi-Fi, GPRS, UMTS, etc.)</li>
64 * <li>Send broadcast intents when network connectivity changes</li>
65 * <li>Attempt to "fail over" to another network when connectivity to a network
66 * is lost</li>
67 * <li>Provide an API that allows applications to query the coarse-grained or fine-grained
68 * state of the available networks</li>
Robert Greenwaltd19c41c2014-05-18 23:07:25 -070069 * <li>Provide an API that allows applications to request and select networks for their data
70 * traffic</li>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080071 * </ol>
72 */
Jeff Sharkeyc006f1a2011-05-19 17:12:49 -070073public class ConnectivityManager {
74 private static final String TAG = "ConnectivityManager";
75
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080076 /**
Robert Greenwaltd19c41c2014-05-18 23:07:25 -070077 * A change in network connectivity has occurred. A default connection has either
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080078 * been established or lost. The NetworkInfo for the affected network is
79 * sent as an extra; it should be consulted to see what kind of
80 * connectivity event occurred.
81 * <p/>
82 * If this is a connection that was the result of failing over from a
83 * disconnected network, then the FAILOVER_CONNECTION boolean extra is
84 * set to true.
85 * <p/>
86 * For a loss of connectivity, if the connectivity manager is attempting
87 * to connect (or has already connected) to another network, the
88 * NetworkInfo for the new network is also passed as an extra. This lets
89 * any receivers of the broadcast know that they should not necessarily
90 * tell the user that no data traffic will be possible. Instead, the
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -080091 * receiver should expect another broadcast soon, indicating either that
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080092 * the failover attempt succeeded (and so there is still overall data
93 * connectivity), or that the failover attempt failed, meaning that all
94 * connectivity has been lost.
95 * <p/>
96 * For a disconnect event, the boolean extra EXTRA_NO_CONNECTIVITY
97 * is set to {@code true} if there are no connected networks at all.
98 */
Jeff Sharkey4fa63b22013-02-20 18:21:19 -080099 @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800100 public static final String CONNECTIVITY_ACTION = "android.net.conn.CONNECTIVITY_CHANGE";
Jeff Sharkeyf0ceede2011-08-02 17:22:34 -0700101
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800102 /**
Robert Greenwalte94a6ff2015-09-01 08:23:04 -0700103 * A temporary hack until SUPL system can get off the legacy APIS.
104 * They do too many network requests and the long list of apps listening
105 * and waking due to the CONNECTIVITY_ACTION bcast makes it expensive.
106 * Use this bcast intent instead for SUPL requests.
107 * @hide
108 */
109 public static final String CONNECTIVITY_ACTION_SUPL =
110 "android.net.conn.CONNECTIVITY_CHANGE_SUPL";
111
112 /**
Paul Jensen25a217c2015-02-27 22:55:47 -0500113 * The device has connected to a network that has presented a captive
114 * portal, which is blocking Internet connectivity. The user was presented
115 * with a notification that network sign in is required,
116 * and the user invoked the notification's action indicating they
Paul Jensen49e3edf2015-05-22 10:50:39 -0400117 * desire to sign in to the network. Apps handling this activity should
Paul Jensen25a217c2015-02-27 22:55:47 -0500118 * facilitate signing in to the network. This action includes a
119 * {@link Network} typed extra called {@link #EXTRA_NETWORK} that represents
120 * the network presenting the captive portal; all communication with the
121 * captive portal must be done using this {@code Network} object.
122 * <p/>
Paul Jensen49e3edf2015-05-22 10:50:39 -0400123 * This activity includes a {@link CaptivePortal} extra named
124 * {@link #EXTRA_CAPTIVE_PORTAL} that can be used to indicate different
125 * outcomes of the captive portal sign in to the system:
126 * <ul>
127 * <li> When the app handling this action believes the user has signed in to
128 * the network and the captive portal has been dismissed, the app should
129 * call {@link CaptivePortal#reportCaptivePortalDismissed} so the system can
130 * reevaluate the network. If reevaluation finds the network no longer
131 * subject to a captive portal, the network may become the default active
132 * data network. </li>
133 * <li> When the app handling this action believes the user explicitly wants
Paul Jensen25a217c2015-02-27 22:55:47 -0500134 * to ignore the captive portal and the network, the app should call
Paul Jensen49e3edf2015-05-22 10:50:39 -0400135 * {@link CaptivePortal#ignoreNetwork}. </li>
136 * </ul>
Paul Jensen25a217c2015-02-27 22:55:47 -0500137 */
138 @SdkConstant(SdkConstantType.ACTIVITY_INTENT_ACTION)
139 public static final String ACTION_CAPTIVE_PORTAL_SIGN_IN = "android.net.conn.CAPTIVE_PORTAL";
140
141 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800142 * The lookup key for a {@link NetworkInfo} object. Retrieve with
143 * {@link android.content.Intent#getParcelableExtra(String)}.
Jeff Sharkeyf0ceede2011-08-02 17:22:34 -0700144 *
145 * @deprecated Since {@link NetworkInfo} can vary based on UID, applications
146 * should always obtain network information through
Paul Jensen3541e9f2015-03-18 12:23:02 -0400147 * {@link #getActiveNetworkInfo()}.
Jeff Sharkey75fbb4b2012-08-06 11:41:50 -0700148 * @see #EXTRA_NETWORK_TYPE
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800149 */
Jeff Sharkeyf0ceede2011-08-02 17:22:34 -0700150 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800151 public static final String EXTRA_NETWORK_INFO = "networkInfo";
Jeff Sharkeyf0ceede2011-08-02 17:22:34 -0700152
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800153 /**
Jeff Sharkey75fbb4b2012-08-06 11:41:50 -0700154 * Network type which triggered a {@link #CONNECTIVITY_ACTION} broadcast.
Jeff Sharkey75fbb4b2012-08-06 11:41:50 -0700155 *
156 * @see android.content.Intent#getIntExtra(String, int)
157 */
158 public static final String EXTRA_NETWORK_TYPE = "networkType";
159
160 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800161 * The lookup key for a boolean that indicates whether a connect event
162 * is for a network to which the connectivity manager was failing over
163 * following a disconnect on another network.
164 * Retrieve it with {@link android.content.Intent#getBooleanExtra(String,boolean)}.
165 */
166 public static final String EXTRA_IS_FAILOVER = "isFailover";
167 /**
168 * The lookup key for a {@link NetworkInfo} object. This is supplied when
169 * there is another network that it may be possible to connect to. Retrieve with
170 * {@link android.content.Intent#getParcelableExtra(String)}.
171 */
172 public static final String EXTRA_OTHER_NETWORK_INFO = "otherNetwork";
173 /**
174 * The lookup key for a boolean that indicates whether there is a
175 * complete lack of connectivity, i.e., no network is available.
176 * Retrieve it with {@link android.content.Intent#getBooleanExtra(String,boolean)}.
177 */
178 public static final String EXTRA_NO_CONNECTIVITY = "noConnectivity";
179 /**
180 * The lookup key for a string that indicates why an attempt to connect
181 * to a network failed. The string has no particular structure. It is
182 * intended to be used in notifications presented to users. Retrieve
183 * it with {@link android.content.Intent#getStringExtra(String)}.
184 */
185 public static final String EXTRA_REASON = "reason";
186 /**
187 * The lookup key for a string that provides optionally supplied
188 * extra information about the network state. The information
189 * may be passed up from the lower networking layers, and its
190 * meaning may be specific to a particular network type. Retrieve
191 * it with {@link android.content.Intent#getStringExtra(String)}.
192 */
193 public static final String EXTRA_EXTRA_INFO = "extraInfo";
Robert Greenwaltd7085fc2010-09-08 15:24:47 -0700194 /**
195 * The lookup key for an int that provides information about
196 * our connection to the internet at large. 0 indicates no connection,
197 * 100 indicates a great connection. Retrieve it with
Jeff Sharkeyc006f1a2011-05-19 17:12:49 -0700198 * {@link android.content.Intent#getIntExtra(String, int)}.
Robert Greenwaltd7085fc2010-09-08 15:24:47 -0700199 * {@hide}
200 */
201 public static final String EXTRA_INET_CONDITION = "inetCondition";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800202 /**
Paul Jensen49e3edf2015-05-22 10:50:39 -0400203 * The lookup key for a {@link CaptivePortal} object included with the
204 * {@link #ACTION_CAPTIVE_PORTAL_SIGN_IN} intent. The {@code CaptivePortal}
205 * object can be used to either indicate to the system that the captive
206 * portal has been dismissed or that the user does not want to pursue
207 * signing in to captive portal. Retrieve it with
208 * {@link android.content.Intent#getParcelableExtra(String)}.
Paul Jensen25a217c2015-02-27 22:55:47 -0500209 */
Paul Jensen49e3edf2015-05-22 10:50:39 -0400210 public static final String EXTRA_CAPTIVE_PORTAL = "android.net.extra.CAPTIVE_PORTAL";
Paul Jensen25a217c2015-02-27 22:55:47 -0500211 /**
Haoyu Baidb3c8672012-06-20 14:29:57 -0700212 * Broadcast action to indicate the change of data activity status
213 * (idle or active) on a network in a recent period.
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800214 * The network becomes active when data transmission is started, or
215 * idle if there is no data transmission for a period of time.
Haoyu Baidb3c8672012-06-20 14:29:57 -0700216 * {@hide}
217 */
218 @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION)
219 public static final String ACTION_DATA_ACTIVITY_CHANGE = "android.net.conn.DATA_ACTIVITY_CHANGE";
220 /**
221 * The lookup key for an enum that indicates the network device type on which this data activity
222 * change happens.
223 * {@hide}
224 */
225 public static final String EXTRA_DEVICE_TYPE = "deviceType";
226 /**
227 * The lookup key for a boolean that indicates the device is active or not. {@code true} means
228 * it is actively sending or receiving data and {@code false} means it is idle.
229 * {@hide}
230 */
231 public static final String EXTRA_IS_ACTIVE = "isActive";
Ashish Sharma0535a9f2014-03-12 18:42:23 -0700232 /**
233 * The lookup key for a long that contains the timestamp (nanos) of the radio state change.
234 * {@hide}
235 */
236 public static final String EXTRA_REALTIME_NS = "tsNanos";
Haoyu Baidb3c8672012-06-20 14:29:57 -0700237
238 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800239 * Broadcast Action: The setting for background data usage has changed
240 * values. Use {@link #getBackgroundDataSetting()} to get the current value.
241 * <p>
242 * If an application uses the network in the background, it should listen
243 * for this broadcast and stop using the background data if the value is
Jeff Sharkeyc006f1a2011-05-19 17:12:49 -0700244 * {@code false}.
Jeff Sharkey54ee2ad2012-01-30 16:29:24 -0800245 * <p>
246 *
247 * @deprecated As of {@link VERSION_CODES#ICE_CREAM_SANDWICH}, availability
248 * of background data depends on several combined factors, and
249 * this broadcast is no longer sent. Instead, when background
250 * data is unavailable, {@link #getActiveNetworkInfo()} will now
251 * appear disconnected. During first boot after a platform
252 * upgrade, this broadcast will be sent once if
253 * {@link #getBackgroundDataSetting()} was {@code false} before
254 * the upgrade.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800255 */
256 @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION)
Jeff Sharkey54ee2ad2012-01-30 16:29:24 -0800257 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800258 public static final String ACTION_BACKGROUND_DATA_SETTING_CHANGED =
259 "android.net.conn.BACKGROUND_DATA_SETTING_CHANGED";
260
Robert Greenwalt1e9aac22010-09-15 17:36:33 -0700261 /**
262 * Broadcast Action: The network connection may not be good
263 * uses {@code ConnectivityManager.EXTRA_INET_CONDITION} and
264 * {@code ConnectivityManager.EXTRA_NETWORK_INFO} to specify
265 * the network and it's condition.
266 * @hide
267 */
Jeff Sharkey4fa63b22013-02-20 18:21:19 -0800268 @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION)
Robert Greenwalt1e9aac22010-09-15 17:36:33 -0700269 public static final String INET_CONDITION_ACTION =
270 "android.net.conn.INET_CONDITION_ACTION";
271
Robert Greenwalt42acef32009-08-12 16:08:25 -0700272 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800273 * Broadcast Action: A tetherable connection has come or gone.
274 * Uses {@code ConnectivityManager.EXTRA_AVAILABLE_TETHER},
275 * {@code ConnectivityManager.EXTRA_ACTIVE_TETHER} and
276 * {@code ConnectivityManager.EXTRA_ERRORED_TETHER} to indicate
277 * the current state of tethering. Each include a list of
278 * interface names in that state (may be empty).
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -0800279 * @hide
280 */
Jeff Sharkey4fa63b22013-02-20 18:21:19 -0800281 @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION)
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -0800282 public static final String ACTION_TETHER_STATE_CHANGED =
283 "android.net.conn.TETHER_STATE_CHANGED";
284
285 /**
286 * @hide
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800287 * gives a String[] listing all the interfaces configured for
288 * tethering and currently available for tethering.
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -0800289 */
Robert Greenwalt2a091d72010-02-11 18:18:40 -0800290 public static final String EXTRA_AVAILABLE_TETHER = "availableArray";
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -0800291
292 /**
293 * @hide
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800294 * gives a String[] listing all the interfaces currently tethered
295 * (ie, has dhcp support and packets potentially forwarded/NATed)
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -0800296 */
Robert Greenwalt2a091d72010-02-11 18:18:40 -0800297 public static final String EXTRA_ACTIVE_TETHER = "activeArray";
298
299 /**
300 * @hide
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800301 * gives a String[] listing all the interfaces we tried to tether and
302 * failed. Use {@link #getLastTetherError} to find the error code
303 * for any interfaces listed here.
Robert Greenwalt2a091d72010-02-11 18:18:40 -0800304 */
305 public static final String EXTRA_ERRORED_TETHER = "erroredArray";
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -0800306
307 /**
Russell Brenner108da0c2013-02-12 10:03:14 -0800308 * Broadcast Action: The captive portal tracker has finished its test.
309 * Sent only while running Setup Wizard, in lieu of showing a user
310 * notification.
311 * @hide
312 */
Jeff Sharkey4fa63b22013-02-20 18:21:19 -0800313 @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION)
Russell Brenner108da0c2013-02-12 10:03:14 -0800314 public static final String ACTION_CAPTIVE_PORTAL_TEST_COMPLETED =
315 "android.net.conn.CAPTIVE_PORTAL_TEST_COMPLETED";
316 /**
317 * The lookup key for a boolean that indicates whether a captive portal was detected.
318 * Retrieve it with {@link android.content.Intent#getBooleanExtra(String,boolean)}.
319 * @hide
320 */
321 public static final String EXTRA_IS_CAPTIVE_PORTAL = "captivePortal";
322
323 /**
Lorenzo Colittie03c3c72015-04-03 16:38:52 +0900324 * Action used to display a dialog that asks the user whether to connect to a network that is
325 * not validated. This intent is used to start the dialog in settings via startActivity.
326 *
327 * @hide
328 */
329 public static final String ACTION_PROMPT_UNVALIDATED = "android.net.conn.PROMPT_UNVALIDATED";
330
331 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800332 * The absence of a connection type.
Robert Greenwaltccf83af12011-06-02 17:30:47 -0700333 * @hide
334 */
335 public static final int TYPE_NONE = -1;
336
337 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800338 * The Mobile data connection. When active, all data traffic
339 * will use this network type's interface by default
340 * (it has a default route)
Robert Greenwalt42acef32009-08-12 16:08:25 -0700341 */
342 public static final int TYPE_MOBILE = 0;
343 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800344 * The WIFI data connection. When active, all data traffic
345 * will use this network type's interface by default
346 * (it has a default route).
Robert Greenwalt42acef32009-08-12 16:08:25 -0700347 */
348 public static final int TYPE_WIFI = 1;
349 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800350 * An MMS-specific Mobile data connection. This network type may use the
351 * same network interface as {@link #TYPE_MOBILE} or it may use a different
352 * one. This is used by applications needing to talk to the carrier's
353 * Multimedia Messaging Service servers.
Lorenzo Colittie285b432015-04-23 15:32:42 +0900354 *
Lorenzo Colitti2ea89e52015-04-24 17:03:31 +0900355 * @deprecated Applications should instead use
356 * {@link #requestNetwork(NetworkRequest, NetworkCallback)} to request a network that
Lorenzo Colittie285b432015-04-23 15:32:42 +0900357 * provides the {@link NetworkCapabilities#NET_CAPABILITY_MMS} capability.
Robert Greenwalt42acef32009-08-12 16:08:25 -0700358 */
359 public static final int TYPE_MOBILE_MMS = 2;
360 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800361 * A SUPL-specific Mobile data connection. This network type may use the
362 * same network interface as {@link #TYPE_MOBILE} or it may use a different
363 * one. This is used by applications needing to talk to the carrier's
364 * Secure User Plane Location servers for help locating the device.
Lorenzo Colittie285b432015-04-23 15:32:42 +0900365 *
Lorenzo Colitti2ea89e52015-04-24 17:03:31 +0900366 * @deprecated Applications should instead use
367 * {@link #requestNetwork(NetworkRequest, NetworkCallback)} to request a network that
Lorenzo Colittie285b432015-04-23 15:32:42 +0900368 * provides the {@link NetworkCapabilities#NET_CAPABILITY_SUPL} capability.
Robert Greenwalt42acef32009-08-12 16:08:25 -0700369 */
370 public static final int TYPE_MOBILE_SUPL = 3;
371 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800372 * A DUN-specific Mobile data connection. This network type may use the
373 * same network interface as {@link #TYPE_MOBILE} or it may use a different
374 * one. This is sometimes by the system when setting up an upstream connection
375 * for tethering so that the carrier is aware of DUN traffic.
Robert Greenwalt42acef32009-08-12 16:08:25 -0700376 */
377 public static final int TYPE_MOBILE_DUN = 4;
378 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800379 * A High Priority Mobile data connection. This network type uses the
380 * same network interface as {@link #TYPE_MOBILE} but the routing setup
Lorenzo Colittie285b432015-04-23 15:32:42 +0900381 * is different.
382 *
Lorenzo Colitti2ea89e52015-04-24 17:03:31 +0900383 * @deprecated Applications should instead use
384 * {@link #requestNetwork(NetworkRequest, NetworkCallback)} to request a network that
Lorenzo Colittie285b432015-04-23 15:32:42 +0900385 * uses the {@link NetworkCapabilities#TRANSPORT_CELLULAR} transport.
Robert Greenwalt42acef32009-08-12 16:08:25 -0700386 */
387 public static final int TYPE_MOBILE_HIPRI = 5;
jsh8214deb2010-03-11 15:04:43 -0800388 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800389 * The WiMAX data connection. When active, all data traffic
390 * will use this network type's interface by default
391 * (it has a default route).
jsh8214deb2010-03-11 15:04:43 -0800392 */
393 public static final int TYPE_WIMAX = 6;
Robert Greenwaltda3d5e62010-12-06 13:56:24 -0800394
Jaikumar Ganesh15c74392010-12-21 22:31:44 -0800395 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800396 * The Bluetooth data connection. When active, all data traffic
397 * will use this network type's interface by default
398 * (it has a default route).
Jaikumar Ganesh15c74392010-12-21 22:31:44 -0800399 */
400 public static final int TYPE_BLUETOOTH = 7;
401
Robert Greenwalt60810842011-04-22 15:28:18 -0700402 /**
403 * Dummy data connection. This should not be used on shipping devices.
404 */
Jaikumar Ganesh15c74392010-12-21 22:31:44 -0800405 public static final int TYPE_DUMMY = 8;
Wink Saville9d7d6282011-03-12 14:52:01 -0800406
Robert Greenwalt60810842011-04-22 15:28:18 -0700407 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800408 * The Ethernet data connection. When active, all data traffic
409 * will use this network type's interface by default
410 * (it has a default route).
Robert Greenwalt60810842011-04-22 15:28:18 -0700411 */
Robert Greenwalte12aec92011-01-28 14:48:37 -0800412 public static final int TYPE_ETHERNET = 9;
Robert Greenwalt60810842011-04-22 15:28:18 -0700413
Wink Saville9d7d6282011-03-12 14:52:01 -0800414 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800415 * Over the air Administration.
Wink Saville9d7d6282011-03-12 14:52:01 -0800416 * {@hide}
417 */
418 public static final int TYPE_MOBILE_FOTA = 10;
419
420 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800421 * IP Multimedia Subsystem.
Wink Saville9d7d6282011-03-12 14:52:01 -0800422 * {@hide}
423 */
424 public static final int TYPE_MOBILE_IMS = 11;
425
426 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800427 * Carrier Branded Services.
Wink Saville9d7d6282011-03-12 14:52:01 -0800428 * {@hide}
429 */
430 public static final int TYPE_MOBILE_CBS = 12;
431
repo syncaea743a2011-07-29 23:55:49 -0700432 /**
433 * A Wi-Fi p2p connection. Only requesting processes will have access to
434 * the peers connected.
435 * {@hide}
436 */
437 public static final int TYPE_WIFI_P2P = 13;
Wink Saville9d7d6282011-03-12 14:52:01 -0800438
Wink Saville5e56bc52013-07-29 15:00:57 -0700439 /**
440 * The network to use for initially attaching to the network
441 * {@hide}
442 */
443 public static final int TYPE_MOBILE_IA = 14;
repo syncaea743a2011-07-29 23:55:49 -0700444
Lorenzo Colittie285b432015-04-23 15:32:42 +0900445 /**
Robert Greenwalt4bd43892015-07-09 14:49:35 -0700446 * Emergency PDN connection for emergency services. This
447 * may include IMS and MMS in emergency situations.
Ram3e0e3bc2014-06-26 11:03:44 -0700448 * {@hide}
449 */
450 public static final int TYPE_MOBILE_EMERGENCY = 15;
451
Hui Lu1c5624a2014-01-15 11:05:36 -0500452 /**
453 * The network that uses proxy to achieve connectivity.
454 * {@hide}
455 */
456 public static final int TYPE_PROXY = 16;
Wink Saville5e56bc52013-07-29 15:00:57 -0700457
Robert Greenwalt8283f882014-07-07 17:09:01 -0700458 /**
459 * A virtual network using one or more native bearers.
460 * It may or may not be providing security services.
461 */
462 public static final int TYPE_VPN = 17;
Hui Lu1c5624a2014-01-15 11:05:36 -0500463
464 /** {@hide} */
Robert Greenwalt8283f882014-07-07 17:09:01 -0700465 public static final int MAX_RADIO_TYPE = TYPE_VPN;
466
467 /** {@hide} */
468 public static final int MAX_NETWORK_TYPE = TYPE_VPN;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800469
Jianzheng Zhoudcf03f32012-11-16 13:45:20 +0800470 /**
471 * If you want to set the default network preference,you can directly
472 * change the networkAttributes array in framework's config.xml.
473 *
474 * @deprecated Since we support so many more networks now, the single
475 * network default network preference can't really express
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800476 * the hierarchy. Instead, the default is defined by the
Jianzheng Zhoudcf03f32012-11-16 13:45:20 +0800477 * networkAttributes in config.xml. You can determine
Robert Greenwalt4c8b7482012-12-07 09:56:50 -0800478 * the current value by calling {@link #getNetworkPreference()}
Jianzheng Zhoudcf03f32012-11-16 13:45:20 +0800479 * from an App.
480 */
481 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800482 public static final int DEFAULT_NETWORK_PREFERENCE = TYPE_WIFI;
483
Jeff Sharkey625239a2012-09-26 22:03:49 -0700484 /**
Robert Greenwalt9ba9c582014-03-19 17:56:12 -0700485 * @hide
486 */
Robert Greenwalt7569f182014-06-08 16:42:59 -0700487 public final static int REQUEST_ID_UNSET = 0;
488
Paul Jensen5d59e782014-07-11 12:28:19 -0400489 /**
490 * A NetID indicating no Network is selected.
491 * Keep in sync with bionic/libc/dns/include/resolv_netid.h
492 * @hide
493 */
494 public static final int NETID_UNSET = 0;
495
Jeff Sharkeyc006f1a2011-05-19 17:12:49 -0700496 private final IConnectivityManager mService;
Paul Jensene0bef712014-12-10 15:12:18 -0500497 /**
498 * A kludge to facilitate static access where a Context pointer isn't available, like in the
499 * case of the static set/getProcessDefaultNetwork methods and from the Network class.
500 * TODO: Remove this after deprecating the static methods in favor of non-static methods or
501 * methods that take a Context argument.
502 */
503 private static ConnectivityManager sInstance;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800504
Lorenzo Colittiffc42b02015-07-29 11:41:21 +0900505 private final Context mContext;
506
Dianne Hackborn77b987f2014-02-26 16:20:52 -0800507 private INetworkManagementService mNMService;
508
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800509 /**
510 * Tests if a given integer represents a valid network type.
511 * @param networkType the type to be tested
512 * @return a boolean. {@code true} if the type is valid, else {@code false}
Paul Jensen9e59e122015-05-06 10:42:25 -0400513 * @deprecated All APIs accepting a network type are deprecated. There should be no need to
514 * validate a network type.
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800515 */
Jeff Sharkeyd2a45872011-05-28 20:56:34 -0700516 public static boolean isNetworkTypeValid(int networkType) {
Robert Greenwalt42acef32009-08-12 16:08:25 -0700517 return networkType >= 0 && networkType <= MAX_NETWORK_TYPE;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800518 }
519
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800520 /**
521 * Returns a non-localized string representing a given network type.
522 * ONLY used for debugging output.
523 * @param type the type needing naming
524 * @return a String for the given type, or a string version of the type ("87")
525 * if no name is known.
526 * {@hide}
527 */
Jeff Sharkeyd2a45872011-05-28 20:56:34 -0700528 public static String getNetworkTypeName(int type) {
529 switch (type) {
530 case TYPE_MOBILE:
531 return "MOBILE";
532 case TYPE_WIFI:
533 return "WIFI";
534 case TYPE_MOBILE_MMS:
535 return "MOBILE_MMS";
536 case TYPE_MOBILE_SUPL:
537 return "MOBILE_SUPL";
538 case TYPE_MOBILE_DUN:
539 return "MOBILE_DUN";
540 case TYPE_MOBILE_HIPRI:
541 return "MOBILE_HIPRI";
542 case TYPE_WIMAX:
543 return "WIMAX";
544 case TYPE_BLUETOOTH:
545 return "BLUETOOTH";
546 case TYPE_DUMMY:
547 return "DUMMY";
548 case TYPE_ETHERNET:
549 return "ETHERNET";
550 case TYPE_MOBILE_FOTA:
551 return "MOBILE_FOTA";
552 case TYPE_MOBILE_IMS:
553 return "MOBILE_IMS";
554 case TYPE_MOBILE_CBS:
555 return "MOBILE_CBS";
repo syncaea743a2011-07-29 23:55:49 -0700556 case TYPE_WIFI_P2P:
557 return "WIFI_P2P";
Wink Saville5e56bc52013-07-29 15:00:57 -0700558 case TYPE_MOBILE_IA:
559 return "MOBILE_IA";
Ram3e0e3bc2014-06-26 11:03:44 -0700560 case TYPE_MOBILE_EMERGENCY:
561 return "MOBILE_EMERGENCY";
Hui Lu1c5624a2014-01-15 11:05:36 -0500562 case TYPE_PROXY:
563 return "PROXY";
Erik Kline37fbfa12014-11-19 17:23:41 +0900564 case TYPE_VPN:
565 return "VPN";
Jeff Sharkeyd2a45872011-05-28 20:56:34 -0700566 default:
567 return Integer.toString(type);
568 }
569 }
570
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800571 /**
572 * Checks if a given type uses the cellular data connection.
573 * This should be replaced in the future by a network property.
574 * @param networkType the type to check
575 * @return a boolean - {@code true} if uses cellular network, else {@code false}
576 * {@hide}
577 */
Jeff Sharkeyd2a45872011-05-28 20:56:34 -0700578 public static boolean isNetworkTypeMobile(int networkType) {
579 switch (networkType) {
580 case TYPE_MOBILE:
581 case TYPE_MOBILE_MMS:
582 case TYPE_MOBILE_SUPL:
583 case TYPE_MOBILE_DUN:
584 case TYPE_MOBILE_HIPRI:
585 case TYPE_MOBILE_FOTA:
586 case TYPE_MOBILE_IMS:
587 case TYPE_MOBILE_CBS:
Wink Saville5e56bc52013-07-29 15:00:57 -0700588 case TYPE_MOBILE_IA:
Ram3e0e3bc2014-06-26 11:03:44 -0700589 case TYPE_MOBILE_EMERGENCY:
Jeff Sharkeyd2a45872011-05-28 20:56:34 -0700590 return true;
591 default:
592 return false;
593 }
594 }
595
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800596 /**
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -0700597 * Checks if the given network type is backed by a Wi-Fi radio.
598 *
599 * @hide
600 */
601 public static boolean isNetworkTypeWifi(int networkType) {
602 switch (networkType) {
603 case TYPE_WIFI:
604 case TYPE_WIFI_P2P:
605 return true;
606 default:
607 return false;
608 }
609 }
610
611 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800612 * Specifies the preferred network type. When the device has more
613 * than one type available the preferred network type will be used.
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800614 *
615 * @param preference the network type to prefer over all others. It is
616 * unspecified what happens to the old preferred network in the
617 * overall ordering.
Robert Greenwaltd19c41c2014-05-18 23:07:25 -0700618 * @deprecated Functionality has been removed as it no longer makes sense,
619 * with many more than two networks - we'd need an array to express
620 * preference. Instead we use dynamic network properties of
621 * the networks to describe their precedence.
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800622 */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800623 public void setNetworkPreference(int preference) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800624 }
625
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800626 /**
627 * Retrieves the current preferred network type.
Paul Jensenb2748922015-05-06 11:10:18 -0400628 * <p>This method requires the caller to hold the permission
629 * {@link android.Manifest.permission#ACCESS_NETWORK_STATE}.
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800630 *
631 * @return an integer representing the preferred network type
632 *
Robert Greenwaltd19c41c2014-05-18 23:07:25 -0700633 * @deprecated Functionality has been removed as it no longer makes sense,
634 * with many more than two networks - we'd need an array to express
635 * preference. Instead we use dynamic network properties of
636 * the networks to describe their precedence.
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800637 */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800638 public int getNetworkPreference() {
Robert Greenwaltd19c41c2014-05-18 23:07:25 -0700639 return TYPE_NONE;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800640 }
641
Scott Main671644c2011-10-06 19:02:28 -0700642 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800643 * Returns details about the currently active default data network. When
644 * connected, this network is the default route for outgoing connections.
645 * You should always check {@link NetworkInfo#isConnected()} before initiating
646 * network traffic. This may return {@code null} when there is no default
647 * network.
Paul Jensenb2748922015-05-06 11:10:18 -0400648 * <p>This method requires the caller to hold the permission
649 * {@link android.Manifest.permission#ACCESS_NETWORK_STATE}.
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800650 *
651 * @return a {@link NetworkInfo} object for the current default network
Paul Jensen0d719ca2015-02-13 14:18:39 -0500652 * or {@code null} if no default network is currently active
Jeff Sharkey9f7cbf02012-04-12 18:34:54 -0700653 */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800654 public NetworkInfo getActiveNetworkInfo() {
655 try {
656 return mService.getActiveNetworkInfo();
657 } catch (RemoteException e) {
658 return null;
659 }
660 }
661
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800662 /**
Paul Jensen31a94f42015-02-13 14:18:39 -0500663 * Returns a {@link Network} object corresponding to the currently active
664 * default data network. In the event that the current active default data
665 * network disconnects, the returned {@code Network} object will no longer
666 * be usable. This will return {@code null} when there is no default
667 * network.
Paul Jensenb2748922015-05-06 11:10:18 -0400668 * <p>This method requires the caller to hold the permission
669 * {@link android.Manifest.permission#ACCESS_NETWORK_STATE}.
Paul Jensen31a94f42015-02-13 14:18:39 -0500670 *
671 * @return a {@link Network} object for the current default network or
672 * {@code null} if no default network is currently active
Paul Jensen31a94f42015-02-13 14:18:39 -0500673 */
674 public Network getActiveNetwork() {
675 try {
676 return mService.getActiveNetwork();
677 } catch (RemoteException e) {
678 return null;
679 }
680 }
681
682 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800683 * Returns details about the currently active default data network
684 * for a given uid. This is for internal use only to avoid spying
685 * other apps.
Paul Jensenb2748922015-05-06 11:10:18 -0400686 * <p>This method requires the caller to hold the permission
687 * {@link android.Manifest.permission#CONNECTIVITY_INTERNAL}
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800688 *
689 * @return a {@link NetworkInfo} object for the current default network
690 * for the given uid or {@code null} if no default network is
691 * available for the specified uid.
692 *
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800693 * {@hide}
694 */
Jeff Sharkeyc006f1a2011-05-19 17:12:49 -0700695 public NetworkInfo getActiveNetworkInfoForUid(int uid) {
696 try {
697 return mService.getActiveNetworkInfoForUid(uid);
698 } catch (RemoteException e) {
699 return null;
700 }
701 }
702
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800703 /**
704 * Returns connection status information about a particular
705 * network type.
Paul Jensenb2748922015-05-06 11:10:18 -0400706 * <p>This method requires the caller to hold the permission
707 * {@link android.Manifest.permission#ACCESS_NETWORK_STATE}.
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800708 *
709 * @param networkType integer specifying which networkType in
710 * which you're interested.
711 * @return a {@link NetworkInfo} object for the requested
712 * network type or {@code null} if the type is not
713 * supported by the device.
714 *
Paul Jensen3541e9f2015-03-18 12:23:02 -0400715 * @deprecated This method does not support multiple connected networks
716 * of the same type. Use {@link #getAllNetworks} and
717 * {@link #getNetworkInfo(android.net.Network)} instead.
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800718 */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800719 public NetworkInfo getNetworkInfo(int networkType) {
720 try {
721 return mService.getNetworkInfo(networkType);
722 } catch (RemoteException e) {
723 return null;
724 }
725 }
726
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800727 /**
Robert Greenwalt73b6cbae2014-06-23 11:40:00 -0700728 * Returns connection status information about a particular
729 * Network.
Paul Jensenb2748922015-05-06 11:10:18 -0400730 * <p>This method requires the caller to hold the permission
731 * {@link android.Manifest.permission#ACCESS_NETWORK_STATE}.
Robert Greenwalt73b6cbae2014-06-23 11:40:00 -0700732 *
733 * @param network {@link Network} specifying which network
734 * in which you're interested.
735 * @return a {@link NetworkInfo} object for the requested
736 * network or {@code null} if the {@code Network}
737 * is not valid.
Robert Greenwalt73b6cbae2014-06-23 11:40:00 -0700738 */
739 public NetworkInfo getNetworkInfo(Network network) {
740 try {
741 return mService.getNetworkInfoForNetwork(network);
742 } catch (RemoteException e) {
743 return null;
744 }
745 }
746
747 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800748 * Returns connection status information about all network
749 * types supported by the device.
Paul Jensenb2748922015-05-06 11:10:18 -0400750 * <p>This method requires the caller to hold the permission
751 * {@link android.Manifest.permission#ACCESS_NETWORK_STATE}.
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800752 *
753 * @return an array of {@link NetworkInfo} objects. Check each
754 * {@link NetworkInfo#getType} for which type each applies.
755 *
Paul Jensen3541e9f2015-03-18 12:23:02 -0400756 * @deprecated This method does not support multiple connected networks
757 * of the same type. Use {@link #getAllNetworks} and
758 * {@link #getNetworkInfo(android.net.Network)} instead.
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800759 */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800760 public NetworkInfo[] getAllNetworkInfo() {
761 try {
762 return mService.getAllNetworkInfo();
763 } catch (RemoteException e) {
764 return null;
765 }
766 }
767
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800768 /**
Lorenzo Colittib57edc52014-08-22 17:10:50 -0700769 * Returns the {@link Network} object currently serving a given type, or
770 * null if the given type is not connected.
771 *
772 * <p>This method requires the caller to hold the permission
773 * {@link android.Manifest.permission#ACCESS_NETWORK_STATE}.
774 *
775 * @hide
Paul Jensen3541e9f2015-03-18 12:23:02 -0400776 * @deprecated This method does not support multiple connected networks
777 * of the same type. Use {@link #getAllNetworks} and
778 * {@link #getNetworkInfo(android.net.Network)} instead.
Lorenzo Colittib57edc52014-08-22 17:10:50 -0700779 */
780 public Network getNetworkForType(int networkType) {
781 try {
782 return mService.getNetworkForType(networkType);
783 } catch (RemoteException e) {
784 return null;
785 }
786 }
787
788 /**
Robert Greenwalt73b6cbae2014-06-23 11:40:00 -0700789 * Returns an array of all {@link Network} currently tracked by the
790 * framework.
Robert Greenwalt73b6cbae2014-06-23 11:40:00 -0700791 * <p>This method requires the caller to hold the permission
792 * {@link android.Manifest.permission#ACCESS_NETWORK_STATE}.
Paul Jensenb2748922015-05-06 11:10:18 -0400793 *
794 * @return an array of {@link Network} objects.
Robert Greenwalt73b6cbae2014-06-23 11:40:00 -0700795 */
796 public Network[] getAllNetworks() {
797 try {
798 return mService.getAllNetworks();
799 } catch (RemoteException e) {
800 return null;
801 }
802 }
803
804 /**
Lorenzo Colittie285b432015-04-23 15:32:42 +0900805 * Returns an array of {@link android.net.NetworkCapabilities} objects, representing
Lorenzo Colitti403aa262014-11-28 11:21:30 +0900806 * the Networks that applications run by the given user will use by default.
807 * @hide
808 */
809 public NetworkCapabilities[] getDefaultNetworkCapabilitiesForUser(int userId) {
810 try {
811 return mService.getDefaultNetworkCapabilitiesForUser(userId);
812 } catch (RemoteException e) {
813 return null;
814 }
815 }
816
817 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800818 * Returns the IP information for the current default network.
Paul Jensenb2748922015-05-06 11:10:18 -0400819 * <p>This method requires the caller to hold the permission
820 * {@link android.Manifest.permission#ACCESS_NETWORK_STATE}.
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800821 *
822 * @return a {@link LinkProperties} object describing the IP info
823 * for the current default network, or {@code null} if there
824 * is no current default network.
825 *
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800826 * {@hide}
827 */
Robert Greenwaltd192dad2010-09-14 09:18:02 -0700828 public LinkProperties getActiveLinkProperties() {
829 try {
830 return mService.getActiveLinkProperties();
831 } catch (RemoteException e) {
832 return null;
833 }
834 }
835
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800836 /**
837 * Returns the IP information for a given network type.
Paul Jensenb2748922015-05-06 11:10:18 -0400838 * <p>This method requires the caller to hold the permission
839 * {@link android.Manifest.permission#ACCESS_NETWORK_STATE}.
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800840 *
841 * @param networkType the network type of interest.
842 * @return a {@link LinkProperties} object describing the IP info
843 * for the given networkType, or {@code null} if there is
844 * no current default network.
845 *
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800846 * {@hide}
Paul Jensen3541e9f2015-03-18 12:23:02 -0400847 * @deprecated This method does not support multiple connected networks
848 * of the same type. Use {@link #getAllNetworks},
849 * {@link #getNetworkInfo(android.net.Network)}, and
850 * {@link #getLinkProperties(android.net.Network)} instead.
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800851 */
Robert Greenwaltd192dad2010-09-14 09:18:02 -0700852 public LinkProperties getLinkProperties(int networkType) {
853 try {
Robert Greenwalt9258c642014-03-26 16:47:06 -0700854 return mService.getLinkPropertiesForType(networkType);
855 } catch (RemoteException e) {
856 return null;
857 }
858 }
859
Robert Greenwaltd19c41c2014-05-18 23:07:25 -0700860 /**
861 * Get the {@link LinkProperties} for the given {@link Network}. This
862 * will return {@code null} if the network is unknown.
Paul Jensenb2748922015-05-06 11:10:18 -0400863 * <p>This method requires the caller to hold the permission
864 * {@link android.Manifest.permission#ACCESS_NETWORK_STATE}.
Robert Greenwaltd19c41c2014-05-18 23:07:25 -0700865 *
866 * @param network The {@link Network} object identifying the network in question.
867 * @return The {@link LinkProperties} for the network, or {@code null}.
Paul Jensenb2748922015-05-06 11:10:18 -0400868 */
Robert Greenwalt9258c642014-03-26 16:47:06 -0700869 public LinkProperties getLinkProperties(Network network) {
870 try {
871 return mService.getLinkProperties(network);
872 } catch (RemoteException e) {
873 return null;
874 }
875 }
876
Robert Greenwaltd19c41c2014-05-18 23:07:25 -0700877 /**
Lorenzo Colittie285b432015-04-23 15:32:42 +0900878 * Get the {@link android.net.NetworkCapabilities} for the given {@link Network}. This
Robert Greenwaltd19c41c2014-05-18 23:07:25 -0700879 * will return {@code null} if the network is unknown.
Paul Jensenb2748922015-05-06 11:10:18 -0400880 * <p>This method requires the caller to hold the permission
881 * {@link android.Manifest.permission#ACCESS_NETWORK_STATE}.
Robert Greenwaltd19c41c2014-05-18 23:07:25 -0700882 *
883 * @param network The {@link Network} object identifying the network in question.
Lorenzo Colittie285b432015-04-23 15:32:42 +0900884 * @return The {@link android.net.NetworkCapabilities} for the network, or {@code null}.
Robert Greenwaltd19c41c2014-05-18 23:07:25 -0700885 */
Robert Greenwalt9258c642014-03-26 16:47:06 -0700886 public NetworkCapabilities getNetworkCapabilities(Network network) {
887 try {
888 return mService.getNetworkCapabilities(network);
Robert Greenwaltd192dad2010-09-14 09:18:02 -0700889 } catch (RemoteException e) {
890 return null;
891 }
892 }
893
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800894 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800895 * Tells the underlying networking system that the caller wants to
896 * begin using the named feature. The interpretation of {@code feature}
897 * is completely up to each networking implementation.
Nicolas Falliere9530e3a2012-06-18 17:21:06 -0700898 * <p>This method requires the caller to hold the permission
899 * {@link android.Manifest.permission#CHANGE_NETWORK_STATE}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800900 * @param networkType specifies which network the request pertains to
901 * @param feature the name of the feature to be used
902 * @return an integer value representing the outcome of the request.
903 * The interpretation of this value is specific to each networking
904 * implementation+feature combination, except that the value {@code -1}
905 * always indicates failure.
Robert Greenwaltd19c41c2014-05-18 23:07:25 -0700906 *
Lorenzo Colitti2ea89e52015-04-24 17:03:31 +0900907 * @deprecated Deprecated in favor of the cleaner
908 * {@link #requestNetwork(NetworkRequest, NetworkCallback)} API.
Dianne Hackborn692a2442015-07-31 10:35:34 -0700909 * In {@link VERSION_CODES#M}, and above, this method is unsupported and will
Lorenzo Colittiffc42b02015-07-29 11:41:21 +0900910 * throw {@code UnsupportedOperationException} if called.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800911 */
912 public int startUsingNetworkFeature(int networkType, String feature) {
Lorenzo Colittiffc42b02015-07-29 11:41:21 +0900913 checkLegacyRoutingApiAccess();
Robert Greenwalt562cc542014-05-15 18:07:26 -0700914 NetworkCapabilities netCap = networkCapabilitiesForFeature(networkType, feature);
915 if (netCap == null) {
916 Log.d(TAG, "Can't satisfy startUsingNetworkFeature for " + networkType + ", " +
917 feature);
918 return PhoneConstants.APN_REQUEST_FAILED;
919 }
920
921 NetworkRequest request = null;
922 synchronized (sLegacyRequests) {
923 LegacyRequest l = sLegacyRequests.get(netCap);
924 if (l != null) {
925 Log.d(TAG, "renewing startUsingNetworkFeature request " + l.networkRequest);
926 renewRequestLocked(l);
927 if (l.currentNetwork != null) {
928 return PhoneConstants.APN_ALREADY_ACTIVE;
929 } else {
930 return PhoneConstants.APN_REQUEST_STARTED;
931 }
932 }
933
934 request = requestNetworkForFeatureLocked(netCap);
935 }
936 if (request != null) {
Robert Greenwalt257ee5f2014-06-20 10:58:45 -0700937 Log.d(TAG, "starting startUsingNetworkFeature for request " + request);
Robert Greenwalt562cc542014-05-15 18:07:26 -0700938 return PhoneConstants.APN_REQUEST_STARTED;
939 } else {
940 Log.d(TAG, " request Failed");
941 return PhoneConstants.APN_REQUEST_FAILED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800942 }
943 }
944
945 /**
946 * Tells the underlying networking system that the caller is finished
947 * using the named feature. The interpretation of {@code feature}
948 * is completely up to each networking implementation.
Nicolas Falliere9530e3a2012-06-18 17:21:06 -0700949 * <p>This method requires the caller to hold the permission
950 * {@link android.Manifest.permission#CHANGE_NETWORK_STATE}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800951 * @param networkType specifies which network the request pertains to
952 * @param feature the name of the feature that is no longer needed
953 * @return an integer value representing the outcome of the request.
954 * The interpretation of this value is specific to each networking
955 * implementation+feature combination, except that the value {@code -1}
956 * always indicates failure.
Robert Greenwaltd19c41c2014-05-18 23:07:25 -0700957 *
Robert Greenwalta36c0742015-07-28 11:41:31 -0700958 * @deprecated Deprecated in favor of the cleaner {@link #unregisterNetworkCallback} API.
Dianne Hackborn692a2442015-07-31 10:35:34 -0700959 * In {@link VERSION_CODES#M}, and above, this method is unsupported and will
Lorenzo Colittiffc42b02015-07-29 11:41:21 +0900960 * throw {@code UnsupportedOperationException} if called.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800961 */
962 public int stopUsingNetworkFeature(int networkType, String feature) {
Lorenzo Colittiffc42b02015-07-29 11:41:21 +0900963 checkLegacyRoutingApiAccess();
Robert Greenwalt562cc542014-05-15 18:07:26 -0700964 NetworkCapabilities netCap = networkCapabilitiesForFeature(networkType, feature);
965 if (netCap == null) {
966 Log.d(TAG, "Can't satisfy stopUsingNetworkFeature for " + networkType + ", " +
967 feature);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800968 return -1;
969 }
Robert Greenwalt562cc542014-05-15 18:07:26 -0700970
Paul Jensen9ffb53c2014-12-17 10:39:34 -0500971 if (removeRequestForFeature(netCap)) {
Robert Greenwalt562cc542014-05-15 18:07:26 -0700972 Log.d(TAG, "stopUsingNetworkFeature for " + networkType + ", " + feature);
Robert Greenwalt562cc542014-05-15 18:07:26 -0700973 }
974 return 1;
975 }
976
977 private NetworkCapabilities networkCapabilitiesForFeature(int networkType, String feature) {
978 if (networkType == TYPE_MOBILE) {
979 int cap = -1;
980 if ("enableMMS".equals(feature)) {
981 cap = NetworkCapabilities.NET_CAPABILITY_MMS;
982 } else if ("enableSUPL".equals(feature)) {
983 cap = NetworkCapabilities.NET_CAPABILITY_SUPL;
984 } else if ("enableDUN".equals(feature) || "enableDUNAlways".equals(feature)) {
985 cap = NetworkCapabilities.NET_CAPABILITY_DUN;
986 } else if ("enableHIPRI".equals(feature)) {
987 cap = NetworkCapabilities.NET_CAPABILITY_INTERNET;
988 } else if ("enableFOTA".equals(feature)) {
989 cap = NetworkCapabilities.NET_CAPABILITY_FOTA;
990 } else if ("enableIMS".equals(feature)) {
991 cap = NetworkCapabilities.NET_CAPABILITY_IMS;
992 } else if ("enableCBS".equals(feature)) {
993 cap = NetworkCapabilities.NET_CAPABILITY_CBS;
994 } else {
995 return null;
996 }
997 NetworkCapabilities netCap = new NetworkCapabilities();
Robert Greenwalt7569f182014-06-08 16:42:59 -0700998 netCap.addTransportType(NetworkCapabilities.TRANSPORT_CELLULAR).addCapability(cap);
Paul Jensen487ffe72015-07-24 15:57:11 -0400999 netCap.maybeMarkCapabilitiesRestricted();
Robert Greenwalt562cc542014-05-15 18:07:26 -07001000 return netCap;
1001 } else if (networkType == TYPE_WIFI) {
1002 if ("p2p".equals(feature)) {
1003 NetworkCapabilities netCap = new NetworkCapabilities();
1004 netCap.addTransportType(NetworkCapabilities.TRANSPORT_WIFI);
Robert Greenwalt7569f182014-06-08 16:42:59 -07001005 netCap.addCapability(NetworkCapabilities.NET_CAPABILITY_WIFI_P2P);
Paul Jensen487ffe72015-07-24 15:57:11 -04001006 netCap.maybeMarkCapabilitiesRestricted();
Robert Greenwalt562cc542014-05-15 18:07:26 -07001007 return netCap;
1008 }
1009 }
1010 return null;
1011 }
1012
Robert Greenwalt06314e42014-10-29 14:04:06 -07001013 /**
1014 * Guess what the network request was trying to say so that the resulting
1015 * network is accessible via the legacy (deprecated) API such as
1016 * requestRouteToHost.
1017 * This means we should try to be fairly preceise about transport and
1018 * capability but ignore things such as networkSpecifier.
1019 * If the request has more than one transport or capability it doesn't
1020 * match the old legacy requests (they selected only single transport/capability)
1021 * so this function cannot map the request to a single legacy type and
1022 * the resulting network will not be available to the legacy APIs.
1023 *
1024 * TODO - This should be removed when the legacy APIs are removed.
1025 */
Ye Wenb87875e2014-07-21 14:19:01 -07001026 private int inferLegacyTypeForNetworkCapabilities(NetworkCapabilities netCap) {
1027 if (netCap == null) {
1028 return TYPE_NONE;
1029 }
Robert Greenwalt06314e42014-10-29 14:04:06 -07001030
Ye Wenb87875e2014-07-21 14:19:01 -07001031 if (!netCap.hasTransport(NetworkCapabilities.TRANSPORT_CELLULAR)) {
1032 return TYPE_NONE;
1033 }
Robert Greenwalt06314e42014-10-29 14:04:06 -07001034
1035 String type = null;
1036 int result = TYPE_NONE;
1037
Ye Wenb87875e2014-07-21 14:19:01 -07001038 if (netCap.hasCapability(NetworkCapabilities.NET_CAPABILITY_CBS)) {
Robert Greenwalt06314e42014-10-29 14:04:06 -07001039 type = "enableCBS";
1040 result = TYPE_MOBILE_CBS;
1041 } else if (netCap.hasCapability(NetworkCapabilities.NET_CAPABILITY_IMS)) {
1042 type = "enableIMS";
1043 result = TYPE_MOBILE_IMS;
1044 } else if (netCap.hasCapability(NetworkCapabilities.NET_CAPABILITY_FOTA)) {
1045 type = "enableFOTA";
1046 result = TYPE_MOBILE_FOTA;
1047 } else if (netCap.hasCapability(NetworkCapabilities.NET_CAPABILITY_DUN)) {
1048 type = "enableDUN";
1049 result = TYPE_MOBILE_DUN;
1050 } else if (netCap.hasCapability(NetworkCapabilities.NET_CAPABILITY_SUPL)) {
Robert Greenwalt74ab4fa2015-08-28 12:37:54 -07001051 type = "enableSUPL";
Robert Greenwalt06314e42014-10-29 14:04:06 -07001052 result = TYPE_MOBILE_SUPL;
Robert Greenwalt74ab4fa2015-08-28 12:37:54 -07001053 // back out this hack for mms as they no longer need this and it's causing
1054 // device slowdowns - b/23350688 (note, supl still needs this)
1055 //} else if (netCap.hasCapability(NetworkCapabilities.NET_CAPABILITY_MMS)) {
1056 // type = "enableMMS";
1057 // result = TYPE_MOBILE_MMS;
Robert Greenwalt06314e42014-10-29 14:04:06 -07001058 } else if (netCap.hasCapability(NetworkCapabilities.NET_CAPABILITY_INTERNET)) {
1059 type = "enableHIPRI";
1060 result = TYPE_MOBILE_HIPRI;
Ye Wenb87875e2014-07-21 14:19:01 -07001061 }
Robert Greenwalt06314e42014-10-29 14:04:06 -07001062 if (type != null) {
1063 NetworkCapabilities testCap = networkCapabilitiesForFeature(TYPE_MOBILE, type);
1064 if (testCap.equalsNetCapabilities(netCap) && testCap.equalsTransportTypes(netCap)) {
1065 return result;
Ye Wenb87875e2014-07-21 14:19:01 -07001066 }
1067 }
1068 return TYPE_NONE;
1069 }
1070
Robert Greenwalt32aa65a2014-06-02 15:32:02 -07001071 private int legacyTypeForNetworkCapabilities(NetworkCapabilities netCap) {
Robert Greenwalt562cc542014-05-15 18:07:26 -07001072 if (netCap == null) return TYPE_NONE;
1073 if (netCap.hasCapability(NetworkCapabilities.NET_CAPABILITY_CBS)) {
1074 return TYPE_MOBILE_CBS;
1075 }
1076 if (netCap.hasCapability(NetworkCapabilities.NET_CAPABILITY_IMS)) {
1077 return TYPE_MOBILE_IMS;
1078 }
1079 if (netCap.hasCapability(NetworkCapabilities.NET_CAPABILITY_FOTA)) {
1080 return TYPE_MOBILE_FOTA;
1081 }
1082 if (netCap.hasCapability(NetworkCapabilities.NET_CAPABILITY_DUN)) {
1083 return TYPE_MOBILE_DUN;
1084 }
1085 if (netCap.hasCapability(NetworkCapabilities.NET_CAPABILITY_SUPL)) {
1086 return TYPE_MOBILE_SUPL;
1087 }
1088 if (netCap.hasCapability(NetworkCapabilities.NET_CAPABILITY_MMS)) {
1089 return TYPE_MOBILE_MMS;
1090 }
1091 if (netCap.hasCapability(NetworkCapabilities.NET_CAPABILITY_INTERNET)) {
1092 return TYPE_MOBILE_HIPRI;
1093 }
Robert Greenwalt32aa65a2014-06-02 15:32:02 -07001094 if (netCap.hasCapability(NetworkCapabilities.NET_CAPABILITY_WIFI_P2P)) {
1095 return TYPE_WIFI_P2P;
1096 }
Robert Greenwalt562cc542014-05-15 18:07:26 -07001097 return TYPE_NONE;
1098 }
1099
1100 private static class LegacyRequest {
1101 NetworkCapabilities networkCapabilities;
1102 NetworkRequest networkRequest;
1103 int expireSequenceNumber;
1104 Network currentNetwork;
1105 int delay = -1;
Paul Jensen9ffb53c2014-12-17 10:39:34 -05001106
1107 private void clearDnsBinding() {
1108 if (currentNetwork != null) {
1109 currentNetwork = null;
1110 setProcessDefaultNetworkForHostResolution(null);
1111 }
1112 }
1113
Robert Greenwalt6078b502014-06-11 16:05:07 -07001114 NetworkCallback networkCallback = new NetworkCallback() {
Robert Greenwalt562cc542014-05-15 18:07:26 -07001115 @Override
Robert Greenwalt6078b502014-06-11 16:05:07 -07001116 public void onAvailable(Network network) {
Robert Greenwalt562cc542014-05-15 18:07:26 -07001117 currentNetwork = network;
1118 Log.d(TAG, "startUsingNetworkFeature got Network:" + network);
Paul Jensen6d3ff9e2014-05-29 10:12:39 -04001119 setProcessDefaultNetworkForHostResolution(network);
Robert Greenwalt562cc542014-05-15 18:07:26 -07001120 }
1121 @Override
Robert Greenwalt6078b502014-06-11 16:05:07 -07001122 public void onLost(Network network) {
Paul Jensen9ffb53c2014-12-17 10:39:34 -05001123 if (network.equals(currentNetwork)) clearDnsBinding();
Robert Greenwalt562cc542014-05-15 18:07:26 -07001124 Log.d(TAG, "startUsingNetworkFeature lost Network:" + network);
1125 }
1126 };
1127 }
1128
Robert Greenwaltfab501672014-07-23 11:44:01 -07001129 private static HashMap<NetworkCapabilities, LegacyRequest> sLegacyRequests =
Robert Greenwalt562cc542014-05-15 18:07:26 -07001130 new HashMap<NetworkCapabilities, LegacyRequest>();
1131
1132 private NetworkRequest findRequestForFeature(NetworkCapabilities netCap) {
1133 synchronized (sLegacyRequests) {
1134 LegacyRequest l = sLegacyRequests.get(netCap);
1135 if (l != null) return l.networkRequest;
1136 }
1137 return null;
1138 }
1139
1140 private void renewRequestLocked(LegacyRequest l) {
1141 l.expireSequenceNumber++;
1142 Log.d(TAG, "renewing request to seqNum " + l.expireSequenceNumber);
1143 sendExpireMsgForFeature(l.networkCapabilities, l.expireSequenceNumber, l.delay);
1144 }
1145
1146 private void expireRequest(NetworkCapabilities netCap, int sequenceNum) {
1147 int ourSeqNum = -1;
1148 synchronized (sLegacyRequests) {
1149 LegacyRequest l = sLegacyRequests.get(netCap);
1150 if (l == null) return;
1151 ourSeqNum = l.expireSequenceNumber;
Paul Jensen9ffb53c2014-12-17 10:39:34 -05001152 if (l.expireSequenceNumber == sequenceNum) removeRequestForFeature(netCap);
Robert Greenwalt562cc542014-05-15 18:07:26 -07001153 }
1154 Log.d(TAG, "expireRequest with " + ourSeqNum + ", " + sequenceNum);
1155 }
1156
1157 private NetworkRequest requestNetworkForFeatureLocked(NetworkCapabilities netCap) {
1158 int delay = -1;
Robert Greenwalt32aa65a2014-06-02 15:32:02 -07001159 int type = legacyTypeForNetworkCapabilities(netCap);
Robert Greenwalt562cc542014-05-15 18:07:26 -07001160 try {
1161 delay = mService.getRestoreDefaultNetworkDelay(type);
1162 } catch (RemoteException e) {}
1163 LegacyRequest l = new LegacyRequest();
1164 l.networkCapabilities = netCap;
1165 l.delay = delay;
1166 l.expireSequenceNumber = 0;
Robert Greenwalt6078b502014-06-11 16:05:07 -07001167 l.networkRequest = sendRequestForNetwork(netCap, l.networkCallback, 0,
Robert Greenwalt32aa65a2014-06-02 15:32:02 -07001168 REQUEST, type);
Robert Greenwalt562cc542014-05-15 18:07:26 -07001169 if (l.networkRequest == null) return null;
1170 sLegacyRequests.put(netCap, l);
1171 sendExpireMsgForFeature(netCap, l.expireSequenceNumber, delay);
1172 return l.networkRequest;
1173 }
1174
1175 private void sendExpireMsgForFeature(NetworkCapabilities netCap, int seqNum, int delay) {
1176 if (delay >= 0) {
1177 Log.d(TAG, "sending expire msg with seqNum " + seqNum + " and delay " + delay);
1178 Message msg = sCallbackHandler.obtainMessage(EXPIRE_LEGACY_REQUEST, seqNum, 0, netCap);
1179 sCallbackHandler.sendMessageDelayed(msg, delay);
1180 }
1181 }
1182
Paul Jensen9ffb53c2014-12-17 10:39:34 -05001183 private boolean removeRequestForFeature(NetworkCapabilities netCap) {
1184 final LegacyRequest l;
Robert Greenwalt562cc542014-05-15 18:07:26 -07001185 synchronized (sLegacyRequests) {
Paul Jensen9ffb53c2014-12-17 10:39:34 -05001186 l = sLegacyRequests.remove(netCap);
Robert Greenwalt562cc542014-05-15 18:07:26 -07001187 }
Paul Jensen9ffb53c2014-12-17 10:39:34 -05001188 if (l == null) return false;
1189 unregisterNetworkCallback(l.networkCallback);
1190 l.clearDnsBinding();
1191 return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001192 }
1193
Lorenzo Colitti8bf977d2015-06-15 14:29:22 +09001194 /** @hide */
1195 public static class PacketKeepaliveCallback {
1196 /** The requested keepalive was successfully started. */
1197 public void onStarted() {}
1198 /** The keepalive was successfully stopped. */
1199 public void onStopped() {}
1200 /** An error occurred. */
1201 public void onError(int error) {}
1202 }
1203
1204 /**
1205 * Allows applications to request that the system periodically send specific packets on their
1206 * behalf, using hardware offload to save battery power.
1207 *
1208 * To request that the system send keepalives, call one of the methods that return a
1209 * {@link ConnectivityManager.PacketKeepalive} object, such as {@link #startNattKeepalive},
1210 * passing in a non-null callback. If the callback is successfully started, the callback's
1211 * {@code onStarted} method will be called. If an error occurs, {@code onError} will be called,
1212 * specifying one of the {@code ERROR_*} constants in this class.
1213 *
1214 * To stop an existing keepalive, call {@link stop}. The system will call {@code onStopped} if
1215 * the operation was successfull or {@code onError} if an error occurred.
1216 *
1217 * @hide
1218 */
1219 public class PacketKeepalive {
1220
1221 private static final String TAG = "PacketKeepalive";
1222
1223 /** @hide */
1224 public static final int SUCCESS = 0;
1225
1226 /** @hide */
1227 public static final int NO_KEEPALIVE = -1;
1228
1229 /** @hide */
1230 public static final int BINDER_DIED = -10;
1231
1232 /** The specified {@code Network} is not connected. */
1233 public static final int ERROR_INVALID_NETWORK = -20;
1234 /** The specified IP addresses are invalid. For example, the specified source IP address is
1235 * not configured on the specified {@code Network}. */
1236 public static final int ERROR_INVALID_IP_ADDRESS = -21;
1237 /** The requested port is invalid. */
1238 public static final int ERROR_INVALID_PORT = -22;
1239 /** The packet length is invalid (e.g., too long). */
1240 public static final int ERROR_INVALID_LENGTH = -23;
1241 /** The packet transmission interval is invalid (e.g., too short). */
1242 public static final int ERROR_INVALID_INTERVAL = -24;
1243
1244 /** The hardware does not support this request. */
1245 public static final int ERROR_HARDWARE_UNSUPPORTED = -30;
Lorenzo Colitti9d1284e2015-09-08 16:46:36 +09001246 /** The hardware returned an error. */
1247 public static final int ERROR_HARDWARE_ERROR = -31;
Lorenzo Colitti8bf977d2015-06-15 14:29:22 +09001248
1249 public static final int NATT_PORT = 4500;
1250
1251 private final Network mNetwork;
1252 private final PacketKeepaliveCallback mCallback;
1253 private final Looper mLooper;
1254 private final Messenger mMessenger;
1255
1256 private volatile Integer mSlot;
1257
1258 void stopLooper() {
1259 mLooper.quit();
1260 }
1261
1262 public void stop() {
1263 try {
1264 mService.stopKeepalive(mNetwork, mSlot);
1265 } catch (RemoteException e) {
1266 Log.e(TAG, "Error stopping packet keepalive: ", e);
1267 stopLooper();
1268 }
1269 }
1270
1271 private PacketKeepalive(Network network, PacketKeepaliveCallback callback) {
1272 checkNotNull(network, "network cannot be null");
1273 checkNotNull(callback, "callback cannot be null");
1274 mNetwork = network;
1275 mCallback = callback;
1276 HandlerThread thread = new HandlerThread(TAG);
1277 thread.start();
1278 mLooper = thread.getLooper();
1279 mMessenger = new Messenger(new Handler(mLooper) {
1280 @Override
1281 public void handleMessage(Message message) {
1282 switch (message.what) {
1283 case NetworkAgent.EVENT_PACKET_KEEPALIVE:
1284 int error = message.arg2;
1285 try {
1286 if (error == SUCCESS) {
1287 if (mSlot == null) {
1288 mSlot = message.arg1;
1289 mCallback.onStarted();
1290 } else {
1291 mSlot = null;
1292 stopLooper();
1293 mCallback.onStopped();
1294 }
1295 } else {
1296 stopLooper();
1297 mCallback.onError(error);
1298 }
1299 } catch (Exception e) {
1300 Log.e(TAG, "Exception in keepalive callback(" + error + ")", e);
1301 }
1302 break;
1303 default:
1304 Log.e(TAG, "Unhandled message " + Integer.toHexString(message.what));
1305 break;
1306 }
1307 }
1308 });
1309 }
1310 }
1311
1312 /**
1313 * Starts an IPsec NAT-T keepalive packet with the specified parameters.
1314 *
1315 * @hide
1316 */
1317 public PacketKeepalive startNattKeepalive(
1318 Network network, int intervalSeconds, PacketKeepaliveCallback callback,
1319 InetAddress srcAddr, int srcPort, InetAddress dstAddr) {
1320 final PacketKeepalive k = new PacketKeepalive(network, callback);
1321 try {
1322 mService.startNattKeepalive(network, intervalSeconds, k.mMessenger, new Binder(),
1323 srcAddr.getHostAddress(), srcPort, dstAddr.getHostAddress());
1324 } catch (RemoteException e) {
1325 Log.e(TAG, "Error starting packet keepalive: ", e);
1326 k.stopLooper();
1327 return null;
1328 }
1329 return k;
1330 }
1331
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001332 /**
1333 * Ensure that a network route exists to deliver traffic to the specified
1334 * host via the specified network interface. An attempt to add a route that
1335 * already exists is ignored, but treated as successful.
Nicolas Falliere9530e3a2012-06-18 17:21:06 -07001336 * <p>This method requires the caller to hold the permission
1337 * {@link android.Manifest.permission#CHANGE_NETWORK_STATE}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001338 * @param networkType the type of the network over which traffic to the specified
1339 * host is to be routed
1340 * @param hostAddress the IP address of the host to which the route is desired
1341 * @return {@code true} on success, {@code false} on failure
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07001342 *
Lorenzo Colitti2ea89e52015-04-24 17:03:31 +09001343 * @deprecated Deprecated in favor of the
1344 * {@link #requestNetwork(NetworkRequest, NetworkCallback)},
1345 * {@link #bindProcessToNetwork} and {@link Network#getSocketFactory} API.
Dianne Hackborn692a2442015-07-31 10:35:34 -07001346 * In {@link VERSION_CODES#M}, and above, this method is unsupported and will
Lorenzo Colittiffc42b02015-07-29 11:41:21 +09001347 * throw {@code UnsupportedOperationException} if called.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001348 */
1349 public boolean requestRouteToHost(int networkType, int hostAddress) {
Sreeram Ramachandran03666c72014-07-19 23:21:46 -07001350 return requestRouteToHostAddress(networkType, NetworkUtils.intToInetAddress(hostAddress));
Robert Greenwalt585ac0f2010-08-27 09:24:29 -07001351 }
1352
1353 /**
1354 * Ensure that a network route exists to deliver traffic to the specified
1355 * host via the specified network interface. An attempt to add a route that
1356 * already exists is ignored, but treated as successful.
Jake Hamby8f9b33e2014-01-15 13:08:03 -08001357 * <p>This method requires the caller to hold the permission
1358 * {@link android.Manifest.permission#CHANGE_NETWORK_STATE}.
Robert Greenwalt585ac0f2010-08-27 09:24:29 -07001359 * @param networkType the type of the network over which traffic to the specified
1360 * host is to be routed
1361 * @param hostAddress the IP address of the host to which the route is desired
1362 * @return {@code true} on success, {@code false} on failure
1363 * @hide
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07001364 * @deprecated Deprecated in favor of the {@link #requestNetwork} and
Lorenzo Colitti2ea89e52015-04-24 17:03:31 +09001365 * {@link #bindProcessToNetwork} API.
Robert Greenwalt585ac0f2010-08-27 09:24:29 -07001366 */
1367 public boolean requestRouteToHostAddress(int networkType, InetAddress hostAddress) {
Lorenzo Colittiffc42b02015-07-29 11:41:21 +09001368 checkLegacyRoutingApiAccess();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001369 try {
Sreeram Ramachandran03666c72014-07-19 23:21:46 -07001370 return mService.requestRouteToHostAddress(networkType, hostAddress.getAddress());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001371 } catch (RemoteException e) {
1372 return false;
1373 }
1374 }
1375
1376 /**
1377 * Returns the value of the setting for background data usage. If false,
1378 * applications should not use the network if the application is not in the
1379 * foreground. Developers should respect this setting, and check the value
1380 * of this before performing any background data operations.
1381 * <p>
1382 * All applications that have background services that use the network
1383 * should listen to {@link #ACTION_BACKGROUND_DATA_SETTING_CHANGED}.
Jeff Sharkey3a844fc2011-08-16 14:37:57 -07001384 * <p>
Scott Main4cc53332011-10-06 18:32:43 -07001385 * @deprecated As of {@link VERSION_CODES#ICE_CREAM_SANDWICH}, availability of
Jeff Sharkey3a844fc2011-08-16 14:37:57 -07001386 * background data depends on several combined factors, and this method will
1387 * always return {@code true}. Instead, when background data is unavailable,
1388 * {@link #getActiveNetworkInfo()} will now appear disconnected.
Danica Chang6fdd0c62010-08-11 14:54:43 -07001389 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001390 * @return Whether background data usage is allowed.
1391 */
Jeff Sharkey3a844fc2011-08-16 14:37:57 -07001392 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001393 public boolean getBackgroundDataSetting() {
Jeff Sharkey3a844fc2011-08-16 14:37:57 -07001394 // assume that background data is allowed; final authority is
1395 // NetworkInfo which may be blocked.
1396 return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001397 }
1398
1399 /**
1400 * Sets the value of the setting for background data usage.
Robert Greenwaltc03fa502010-02-23 18:58:05 -08001401 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001402 * @param allowBackgroundData Whether an application should use data while
1403 * it is in the background.
Robert Greenwaltc03fa502010-02-23 18:58:05 -08001404 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001405 * @attr ref android.Manifest.permission#CHANGE_BACKGROUND_DATA_SETTING
1406 * @see #getBackgroundDataSetting()
1407 * @hide
1408 */
Jeff Sharkey3a844fc2011-08-16 14:37:57 -07001409 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001410 public void setBackgroundDataSetting(boolean allowBackgroundData) {
Jeff Sharkey3a844fc2011-08-16 14:37:57 -07001411 // ignored
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001412 }
Robert Greenwaltc03fa502010-02-23 18:58:05 -08001413
1414 /**
Jeff Sharkeyf0ceede2011-08-02 17:22:34 -07001415 * Return quota status for the current active network, or {@code null} if no
1416 * network is active. Quota status can change rapidly, so these values
1417 * shouldn't be cached.
Jeff Sharkey44a3e0d2011-10-06 10:50:09 -07001418 *
Paul Jensen0d719ca2015-02-13 14:18:39 -05001419 * <p>This method requires the caller to hold the permission
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08001420 * {@link android.Manifest.permission#ACCESS_NETWORK_STATE}.
1421 *
Jeff Sharkey44a3e0d2011-10-06 10:50:09 -07001422 * @hide
Jeff Sharkeyf0ceede2011-08-02 17:22:34 -07001423 */
1424 public NetworkQuotaInfo getActiveNetworkQuotaInfo() {
1425 try {
1426 return mService.getActiveNetworkQuotaInfo();
1427 } catch (RemoteException e) {
1428 return null;
1429 }
1430 }
1431
1432 /**
Robert Greenwaltc03fa502010-02-23 18:58:05 -08001433 * @hide
Robert Greenwaltafa05c02014-05-21 20:04:36 -07001434 * @deprecated Talk to TelephonyManager directly
Robert Greenwaltc03fa502010-02-23 18:58:05 -08001435 */
1436 public boolean getMobileDataEnabled() {
Robert Greenwaltafa05c02014-05-21 20:04:36 -07001437 IBinder b = ServiceManager.getService(Context.TELEPHONY_SERVICE);
1438 if (b != null) {
1439 try {
1440 ITelephony it = ITelephony.Stub.asInterface(b);
Wink Saville36ffb042014-12-05 11:10:30 -08001441 int subId = SubscriptionManager.getDefaultDataSubId();
1442 Log.d("ConnectivityManager", "getMobileDataEnabled()+ subId=" + subId);
1443 boolean retVal = it.getDataEnabled(subId);
1444 Log.d("ConnectivityManager", "getMobileDataEnabled()- subId=" + subId
1445 + " retVal=" + retVal);
1446 return retVal;
Robert Greenwaltafa05c02014-05-21 20:04:36 -07001447 } catch (RemoteException e) { }
Robert Greenwaltc03fa502010-02-23 18:58:05 -08001448 }
Wink Saville36ffb042014-12-05 11:10:30 -08001449 Log.d("ConnectivityManager", "getMobileDataEnabled()- remote exception retVal=false");
Robert Greenwaltafa05c02014-05-21 20:04:36 -07001450 return false;
Robert Greenwaltc03fa502010-02-23 18:58:05 -08001451 }
1452
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001453 /**
Robert Greenwaltb2489872014-09-04 16:44:35 -07001454 * Callback for use with {@link ConnectivityManager#addDefaultNetworkActiveListener}
Robert Greenwalt6078b502014-06-11 16:05:07 -07001455 * to find out when the system default network has gone in to a high power state.
Dianne Hackborn77b987f2014-02-26 16:20:52 -08001456 */
1457 public interface OnNetworkActiveListener {
1458 /**
1459 * Called on the main thread of the process to report that the current data network
1460 * has become active, and it is now a good time to perform any pending network
1461 * operations. Note that this listener only tells you when the network becomes
1462 * active; if at any other time you want to know whether it is active (and thus okay
1463 * to initiate network traffic), you can retrieve its instantaneous state with
Robert Greenwalt6078b502014-06-11 16:05:07 -07001464 * {@link ConnectivityManager#isDefaultNetworkActive}.
Dianne Hackborn77b987f2014-02-26 16:20:52 -08001465 */
1466 public void onNetworkActive();
1467 }
1468
1469 private INetworkManagementService getNetworkManagementService() {
1470 synchronized (this) {
1471 if (mNMService != null) {
1472 return mNMService;
1473 }
1474 IBinder b = ServiceManager.getService(Context.NETWORKMANAGEMENT_SERVICE);
1475 mNMService = INetworkManagementService.Stub.asInterface(b);
1476 return mNMService;
1477 }
1478 }
1479
1480 private final ArrayMap<OnNetworkActiveListener, INetworkActivityListener>
1481 mNetworkActivityListeners
1482 = new ArrayMap<OnNetworkActiveListener, INetworkActivityListener>();
1483
1484 /**
Robert Greenwalt6078b502014-06-11 16:05:07 -07001485 * Start listening to reports when the system's default data network is active, meaning it is
1486 * a good time to perform network traffic. Use {@link #isDefaultNetworkActive()}
1487 * to determine the current state of the system's default network after registering the
1488 * listener.
1489 * <p>
1490 * If the process default network has been set with
Paul Jensen72db88e2015-03-10 10:54:12 -04001491 * {@link ConnectivityManager#bindProcessToNetwork} this function will not
Robert Greenwalt6078b502014-06-11 16:05:07 -07001492 * reflect the process's default, but the system default.
Dianne Hackborn77b987f2014-02-26 16:20:52 -08001493 *
1494 * @param l The listener to be told when the network is active.
1495 */
Robert Greenwaltb2489872014-09-04 16:44:35 -07001496 public void addDefaultNetworkActiveListener(final OnNetworkActiveListener l) {
Dianne Hackborn77b987f2014-02-26 16:20:52 -08001497 INetworkActivityListener rl = new INetworkActivityListener.Stub() {
1498 @Override
1499 public void onNetworkActive() throws RemoteException {
1500 l.onNetworkActive();
1501 }
1502 };
1503
1504 try {
1505 getNetworkManagementService().registerNetworkActivityListener(rl);
1506 mNetworkActivityListeners.put(l, rl);
1507 } catch (RemoteException e) {
1508 }
1509 }
1510
1511 /**
1512 * Remove network active listener previously registered with
Robert Greenwaltb2489872014-09-04 16:44:35 -07001513 * {@link #addDefaultNetworkActiveListener}.
Dianne Hackborn77b987f2014-02-26 16:20:52 -08001514 *
1515 * @param l Previously registered listener.
1516 */
Robert Greenwaltb2489872014-09-04 16:44:35 -07001517 public void removeDefaultNetworkActiveListener(OnNetworkActiveListener l) {
Dianne Hackborn77b987f2014-02-26 16:20:52 -08001518 INetworkActivityListener rl = mNetworkActivityListeners.get(l);
1519 if (rl == null) {
1520 throw new IllegalArgumentException("Listener not registered: " + l);
1521 }
1522 try {
1523 getNetworkManagementService().unregisterNetworkActivityListener(rl);
1524 } catch (RemoteException e) {
1525 }
1526 }
1527
1528 /**
1529 * Return whether the data network is currently active. An active network means that
1530 * it is currently in a high power state for performing data transmission. On some
1531 * types of networks, it may be expensive to move and stay in such a state, so it is
1532 * more power efficient to batch network traffic together when the radio is already in
1533 * this state. This method tells you whether right now is currently a good time to
1534 * initiate network traffic, as the network is already active.
1535 */
Robert Greenwalt6078b502014-06-11 16:05:07 -07001536 public boolean isDefaultNetworkActive() {
Dianne Hackborn77b987f2014-02-26 16:20:52 -08001537 try {
1538 return getNetworkManagementService().isNetworkActive();
1539 } catch (RemoteException e) {
1540 }
1541 return false;
1542 }
1543
1544 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001545 * {@hide}
1546 */
Lorenzo Colittiffc42b02015-07-29 11:41:21 +09001547 public ConnectivityManager(Context context, IConnectivityManager service) {
1548 mContext = checkNotNull(context, "missing context");
Jeff Sharkeyf0ceede2011-08-02 17:22:34 -07001549 mService = checkNotNull(service, "missing IConnectivityManager");
Paul Jensene0bef712014-12-10 15:12:18 -05001550 sInstance = this;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001551 }
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -08001552
Jeff Sharkey8fc27e82012-04-04 20:40:58 -07001553 /** {@hide} */
1554 public static ConnectivityManager from(Context context) {
1555 return (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
1556 }
1557
Robert Greenwaltedb47662014-09-16 17:54:19 -07001558 /** {@hide */
1559 public static final void enforceTetherChangePermission(Context context) {
1560 if (context.getResources().getStringArray(
1561 com.android.internal.R.array.config_mobile_hotspot_provision_app).length == 2) {
1562 // Have a provisioning app - must only let system apps (which check this app)
1563 // turn on tethering
1564 context.enforceCallingOrSelfPermission(
1565 android.Manifest.permission.CONNECTIVITY_INTERNAL, "ConnectivityService");
1566 } else {
Billy Laua7238a32015-08-01 12:45:02 +01001567 int uid = Binder.getCallingUid();
1568 Settings.checkAndNoteChangeNetworkStateOperation(context, uid, Settings
1569 .getPackageNameForUid(context, uid), true);
Robert Greenwaltedb47662014-09-16 17:54:19 -07001570 }
1571 }
1572
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -08001573 /**
Paul Jensene0bef712014-12-10 15:12:18 -05001574 * @deprecated - use getSystemService. This is a kludge to support static access in certain
1575 * situations where a Context pointer is unavailable.
1576 * @hide
1577 */
Paul Jensen72db88e2015-03-10 10:54:12 -04001578 static ConnectivityManager getInstanceOrNull() {
1579 return sInstance;
1580 }
1581
1582 /**
1583 * @deprecated - use getSystemService. This is a kludge to support static access in certain
1584 * situations where a Context pointer is unavailable.
1585 * @hide
1586 */
1587 private static ConnectivityManager getInstance() {
1588 if (getInstanceOrNull() == null) {
Paul Jensene0bef712014-12-10 15:12:18 -05001589 throw new IllegalStateException("No ConnectivityManager yet constructed");
1590 }
Paul Jensen72db88e2015-03-10 10:54:12 -04001591 return getInstanceOrNull();
Paul Jensene0bef712014-12-10 15:12:18 -05001592 }
1593
1594 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08001595 * Get the set of tetherable, available interfaces. This list is limited by
1596 * device configuration and current interface existence.
Paul Jensenb2748922015-05-06 11:10:18 -04001597 * <p>This method requires the caller to hold the permission
1598 * {@link android.Manifest.permission#ACCESS_NETWORK_STATE}.
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08001599 *
1600 * @return an array of 0 or more Strings of tetherable interface names.
1601 *
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -08001602 * {@hide}
1603 */
1604 public String[] getTetherableIfaces() {
1605 try {
1606 return mService.getTetherableIfaces();
1607 } catch (RemoteException e) {
1608 return new String[0];
1609 }
1610 }
1611
1612 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08001613 * Get the set of tethered interfaces.
Paul Jensenb2748922015-05-06 11:10:18 -04001614 * <p>This method requires the caller to hold the permission
1615 * {@link android.Manifest.permission#ACCESS_NETWORK_STATE}.
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08001616 *
1617 * @return an array of 0 or more String of currently tethered interface names.
1618 *
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -08001619 * {@hide}
1620 */
1621 public String[] getTetheredIfaces() {
1622 try {
1623 return mService.getTetheredIfaces();
1624 } catch (RemoteException e) {
1625 return new String[0];
1626 }
1627 }
1628
1629 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08001630 * Get the set of interface names which attempted to tether but
1631 * failed. Re-attempting to tether may cause them to reset to the Tethered
1632 * state. Alternatively, causing the interface to be destroyed and recreated
1633 * may cause them to reset to the available state.
1634 * {@link ConnectivityManager#getLastTetherError} can be used to get more
1635 * information on the cause of the errors.
Paul Jensenb2748922015-05-06 11:10:18 -04001636 * <p>This method requires the caller to hold the permission
1637 * {@link android.Manifest.permission#ACCESS_NETWORK_STATE}.
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08001638 *
1639 * @return an array of 0 or more String indicating the interface names
1640 * which failed to tether.
1641 *
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -08001642 * {@hide}
1643 */
Robert Greenwalt5a735062010-03-02 17:25:02 -08001644 public String[] getTetheringErroredIfaces() {
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -08001645 try {
Robert Greenwalt5a735062010-03-02 17:25:02 -08001646 return mService.getTetheringErroredIfaces();
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -08001647 } catch (RemoteException e) {
Robert Greenwalt5a735062010-03-02 17:25:02 -08001648 return new String[0];
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -08001649 }
1650 }
1651
1652 /**
Robert Greenwalt9c7e2c22014-06-23 14:53:42 -07001653 * Get the set of tethered dhcp ranges.
1654 *
1655 * @return an array of 0 or more {@code String} of tethered dhcp ranges.
1656 * {@hide}
1657 */
1658 public String[] getTetheredDhcpRanges() {
1659 try {
1660 return mService.getTetheredDhcpRanges();
1661 } catch (RemoteException e) {
1662 return new String[0];
1663 }
1664 }
1665
1666 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08001667 * Attempt to tether the named interface. This will setup a dhcp server
1668 * on the interface, forward and NAT IP packets and forward DNS requests
1669 * to the best active upstream network interface. Note that if no upstream
1670 * IP network interface is available, dhcp will still run and traffic will be
1671 * allowed between the tethered devices and this device, though upstream net
1672 * access will of course fail until an upstream network interface becomes
1673 * active.
Paul Jensenb2748922015-05-06 11:10:18 -04001674 * <p>This method requires the caller to hold the permission
1675 * {@link android.Manifest.permission#CHANGE_NETWORK_STATE}.
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08001676 *
1677 * @param iface the interface name to tether.
1678 * @return error a {@code TETHER_ERROR} value indicating success or failure type
1679 *
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -08001680 * {@hide}
1681 */
Robert Greenwalt5a735062010-03-02 17:25:02 -08001682 public int tether(String iface) {
1683 try {
1684 return mService.tether(iface);
1685 } catch (RemoteException e) {
1686 return TETHER_ERROR_SERVICE_UNAVAIL;
1687 }
1688 }
1689
1690 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08001691 * Stop tethering the named interface.
Paul Jensenb2748922015-05-06 11:10:18 -04001692 * <p>This method requires the caller to hold the permission
1693 * {@link android.Manifest.permission#CHANGE_NETWORK_STATE}.
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08001694 *
1695 * @param iface the interface name to untether.
1696 * @return error a {@code TETHER_ERROR} value indicating success or failure type
1697 *
Robert Greenwalt5a735062010-03-02 17:25:02 -08001698 * {@hide}
1699 */
1700 public int untether(String iface) {
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -08001701 try {
1702 return mService.untether(iface);
1703 } catch (RemoteException e) {
Robert Greenwalt5a735062010-03-02 17:25:02 -08001704 return TETHER_ERROR_SERVICE_UNAVAIL;
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -08001705 }
1706 }
Robert Greenwalt2a091d72010-02-11 18:18:40 -08001707
1708 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08001709 * Check if the device allows for tethering. It may be disabled via
Dianne Hackborn77b987f2014-02-26 16:20:52 -08001710 * {@code ro.tether.denied} system property, Settings.TETHER_SUPPORTED or
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08001711 * due to device configuration.
Paul Jensenb2748922015-05-06 11:10:18 -04001712 * <p>This method requires the caller to hold the permission
1713 * {@link android.Manifest.permission#ACCESS_NETWORK_STATE}.
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08001714 *
1715 * @return a boolean - {@code true} indicating Tethering is supported.
1716 *
Robert Greenwalt2a091d72010-02-11 18:18:40 -08001717 * {@hide}
1718 */
1719 public boolean isTetheringSupported() {
1720 try {
1721 return mService.isTetheringSupported();
1722 } catch (RemoteException e) {
1723 return false;
1724 }
1725 }
1726
1727 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08001728 * Get the list of regular expressions that define any tetherable
1729 * USB network interfaces. If USB tethering is not supported by the
1730 * device, this list should be empty.
Paul Jensenb2748922015-05-06 11:10:18 -04001731 * <p>This method requires the caller to hold the permission
1732 * {@link android.Manifest.permission#ACCESS_NETWORK_STATE}.
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08001733 *
1734 * @return an array of 0 or more regular expression Strings defining
1735 * what interfaces are considered tetherable usb interfaces.
1736 *
Robert Greenwalt2a091d72010-02-11 18:18:40 -08001737 * {@hide}
1738 */
1739 public String[] getTetherableUsbRegexs() {
1740 try {
1741 return mService.getTetherableUsbRegexs();
1742 } catch (RemoteException e) {
1743 return new String[0];
1744 }
1745 }
1746
1747 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08001748 * Get the list of regular expressions that define any tetherable
1749 * Wifi network interfaces. If Wifi tethering is not supported by the
1750 * device, this list should be empty.
Paul Jensenb2748922015-05-06 11:10:18 -04001751 * <p>This method requires the caller to hold the permission
1752 * {@link android.Manifest.permission#ACCESS_NETWORK_STATE}.
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08001753 *
1754 * @return an array of 0 or more regular expression Strings defining
1755 * what interfaces are considered tetherable wifi interfaces.
1756 *
Robert Greenwalt2a091d72010-02-11 18:18:40 -08001757 * {@hide}
1758 */
1759 public String[] getTetherableWifiRegexs() {
1760 try {
1761 return mService.getTetherableWifiRegexs();
1762 } catch (RemoteException e) {
1763 return new String[0];
1764 }
1765 }
Robert Greenwalt5a735062010-03-02 17:25:02 -08001766
Danica Chang6fdd0c62010-08-11 14:54:43 -07001767 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08001768 * Get the list of regular expressions that define any tetherable
1769 * Bluetooth network interfaces. If Bluetooth tethering is not supported by the
1770 * device, this list should be empty.
Paul Jensenb2748922015-05-06 11:10:18 -04001771 * <p>This method requires the caller to hold the permission
1772 * {@link android.Manifest.permission#ACCESS_NETWORK_STATE}.
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08001773 *
1774 * @return an array of 0 or more regular expression Strings defining
1775 * what interfaces are considered tetherable bluetooth interfaces.
1776 *
Danica Chang6fdd0c62010-08-11 14:54:43 -07001777 * {@hide}
1778 */
1779 public String[] getTetherableBluetoothRegexs() {
1780 try {
1781 return mService.getTetherableBluetoothRegexs();
1782 } catch (RemoteException e) {
1783 return new String[0];
1784 }
1785 }
1786
Mike Lockwood6c2260b2011-07-19 13:04:47 -07001787 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08001788 * Attempt to both alter the mode of USB and Tethering of USB. A
1789 * utility method to deal with some of the complexity of USB - will
1790 * attempt to switch to Rndis and subsequently tether the resulting
1791 * interface on {@code true} or turn off tethering and switch off
1792 * Rndis on {@code false}.
Paul Jensenb2748922015-05-06 11:10:18 -04001793 * <p>This method requires the caller to hold the permission
1794 * {@link android.Manifest.permission#CHANGE_NETWORK_STATE}.
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08001795 *
1796 * @param enable a boolean - {@code true} to enable tethering
1797 * @return error a {@code TETHER_ERROR} value indicating success or failure type
1798 *
Mike Lockwood6c2260b2011-07-19 13:04:47 -07001799 * {@hide}
1800 */
1801 public int setUsbTethering(boolean enable) {
1802 try {
1803 return mService.setUsbTethering(enable);
1804 } catch (RemoteException e) {
1805 return TETHER_ERROR_SERVICE_UNAVAIL;
1806 }
1807 }
1808
Robert Greenwalt5a735062010-03-02 17:25:02 -08001809 /** {@hide} */
1810 public static final int TETHER_ERROR_NO_ERROR = 0;
1811 /** {@hide} */
1812 public static final int TETHER_ERROR_UNKNOWN_IFACE = 1;
1813 /** {@hide} */
1814 public static final int TETHER_ERROR_SERVICE_UNAVAIL = 2;
1815 /** {@hide} */
1816 public static final int TETHER_ERROR_UNSUPPORTED = 3;
1817 /** {@hide} */
1818 public static final int TETHER_ERROR_UNAVAIL_IFACE = 4;
1819 /** {@hide} */
1820 public static final int TETHER_ERROR_MASTER_ERROR = 5;
1821 /** {@hide} */
1822 public static final int TETHER_ERROR_TETHER_IFACE_ERROR = 6;
1823 /** {@hide} */
1824 public static final int TETHER_ERROR_UNTETHER_IFACE_ERROR = 7;
1825 /** {@hide} */
1826 public static final int TETHER_ERROR_ENABLE_NAT_ERROR = 8;
1827 /** {@hide} */
1828 public static final int TETHER_ERROR_DISABLE_NAT_ERROR = 9;
1829 /** {@hide} */
1830 public static final int TETHER_ERROR_IFACE_CFG_ERROR = 10;
1831
1832 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08001833 * Get a more detailed error code after a Tethering or Untethering
1834 * request asynchronously failed.
Paul Jensenb2748922015-05-06 11:10:18 -04001835 * <p>This method requires the caller to hold the permission
1836 * {@link android.Manifest.permission#ACCESS_NETWORK_STATE}.
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08001837 *
1838 * @param iface The name of the interface of interest
Robert Greenwalt5a735062010-03-02 17:25:02 -08001839 * @return error The error code of the last error tethering or untethering the named
1840 * interface
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08001841 *
Robert Greenwalt5a735062010-03-02 17:25:02 -08001842 * {@hide}
1843 */
1844 public int getLastTetherError(String iface) {
1845 try {
1846 return mService.getLastTetherError(iface);
1847 } catch (RemoteException e) {
1848 return TETHER_ERROR_SERVICE_UNAVAIL;
1849 }
Robert Greenwalt14f2ef42010-06-15 12:19:37 -07001850 }
1851
1852 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08001853 * Report network connectivity status. This is currently used only
1854 * to alter status bar UI.
Paul Jensenb2748922015-05-06 11:10:18 -04001855 * <p>This method requires the caller to hold the permission
1856 * {@link android.Manifest.permission#STATUS_BAR}.
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08001857 *
Robert Greenwaltd7085fc2010-09-08 15:24:47 -07001858 * @param networkType The type of network you want to report on
1859 * @param percentage The quality of the connection 0 is bad, 100 is good
1860 * {@hide}
1861 */
1862 public void reportInetCondition(int networkType, int percentage) {
1863 try {
1864 mService.reportInetCondition(networkType, percentage);
1865 } catch (RemoteException e) {
1866 }
1867 }
Robert Greenwalt434203a2010-10-11 16:00:27 -07001868
1869 /**
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07001870 * Report a problem network to the framework. This provides a hint to the system
Ye Wenb87875e2014-07-21 14:19:01 -07001871 * that there might be connectivity problems on this network and may cause
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07001872 * the framework to re-evaluate network connectivity and/or switch to another
1873 * network.
Robert Greenwalt9258c642014-03-26 16:47:06 -07001874 *
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07001875 * @param network The {@link Network} the application was attempting to use
1876 * or {@code null} to indicate the current default network.
Paul Jensenbfd17b72015-04-07 12:43:13 -04001877 * @deprecated Use {@link #reportNetworkConnectivity} which allows reporting both
1878 * working and non-working connectivity.
Robert Greenwalt9258c642014-03-26 16:47:06 -07001879 */
1880 public void reportBadNetwork(Network network) {
1881 try {
Paul Jensenbfd17b72015-04-07 12:43:13 -04001882 // One of these will be ignored because it matches system's current state.
1883 // The other will trigger the necessary reevaluation.
1884 mService.reportNetworkConnectivity(network, true);
1885 mService.reportNetworkConnectivity(network, false);
1886 } catch (RemoteException e) {
1887 }
1888 }
1889
1890 /**
1891 * Report to the framework whether a network has working connectivity.
1892 * This provides a hint to the system that a particular network is providing
1893 * working connectivity or not. In response the framework may re-evaluate
1894 * the network's connectivity and might take further action thereafter.
1895 *
1896 * @param network The {@link Network} the application was attempting to use
1897 * or {@code null} to indicate the current default network.
1898 * @param hasConnectivity {@code true} if the application was able to successfully access the
1899 * Internet using {@code network} or {@code false} if not.
1900 */
1901 public void reportNetworkConnectivity(Network network, boolean hasConnectivity) {
1902 try {
1903 mService.reportNetworkConnectivity(network, hasConnectivity);
Robert Greenwalt9258c642014-03-26 16:47:06 -07001904 } catch (RemoteException e) {
1905 }
1906 }
1907
1908 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08001909 * Set a network-independent global http proxy. This is not normally what you want
1910 * for typical HTTP proxies - they are general network dependent. However if you're
1911 * doing something unusual like general internal filtering this may be useful. On
1912 * a private network where the proxy is not accessible, you may break HTTP using this.
Paul Jensen0d719ca2015-02-13 14:18:39 -05001913 * <p>This method requires the caller to hold the permission
Jason Monkfaf3fd52014-05-07 18:41:13 -04001914 * android.Manifest.permission#CONNECTIVITY_INTERNAL.
Paul Jensenb2748922015-05-06 11:10:18 -04001915 *
1916 * @param p A {@link ProxyInfo} object defining the new global
1917 * HTTP proxy. A {@code null} value will clear the global HTTP proxy.
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07001918 * @hide
Robert Greenwalt434203a2010-10-11 16:00:27 -07001919 */
Jason Monk207900c2014-04-25 15:00:09 -04001920 public void setGlobalProxy(ProxyInfo p) {
Robert Greenwalt434203a2010-10-11 16:00:27 -07001921 try {
1922 mService.setGlobalProxy(p);
1923 } catch (RemoteException e) {
1924 }
1925 }
1926
1927 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08001928 * Retrieve any network-independent global HTTP proxy.
1929 *
Jason Monk207900c2014-04-25 15:00:09 -04001930 * @return {@link ProxyInfo} for the current global HTTP proxy or {@code null}
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08001931 * if no global HTTP proxy is set.
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07001932 * @hide
Robert Greenwalt434203a2010-10-11 16:00:27 -07001933 */
Jason Monk207900c2014-04-25 15:00:09 -04001934 public ProxyInfo getGlobalProxy() {
Robert Greenwalt434203a2010-10-11 16:00:27 -07001935 try {
1936 return mService.getGlobalProxy();
1937 } catch (RemoteException e) {
1938 return null;
1939 }
1940 }
1941
1942 /**
Paul Jensencee9b512015-05-06 07:32:40 -04001943 * Retrieve the global HTTP proxy, or if no global HTTP proxy is set, a
1944 * network-specific HTTP proxy. If {@code network} is null, the
1945 * network-specific proxy returned is the proxy of the default active
1946 * network.
1947 *
1948 * @return {@link ProxyInfo} for the current global HTTP proxy, or if no
1949 * global HTTP proxy is set, {@code ProxyInfo} for {@code network},
1950 * or when {@code network} is {@code null},
1951 * the {@code ProxyInfo} for the default active network. Returns
1952 * {@code null} when no proxy applies or the caller doesn't have
1953 * permission to use {@code network}.
1954 * @hide
1955 */
1956 public ProxyInfo getProxyForNetwork(Network network) {
1957 try {
1958 return mService.getProxyForNetwork(network);
1959 } catch (RemoteException e) {
1960 return null;
1961 }
1962 }
1963
1964 /**
Paul Jensene0bef712014-12-10 15:12:18 -05001965 * Get the current default HTTP proxy settings. If a global proxy is set it will be returned,
1966 * otherwise if this process is bound to a {@link Network} using
Paul Jensen72db88e2015-03-10 10:54:12 -04001967 * {@link #bindProcessToNetwork} then that {@code Network}'s proxy is returned, otherwise
Paul Jensene0bef712014-12-10 15:12:18 -05001968 * the default network's proxy is returned.
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08001969 *
Jason Monk207900c2014-04-25 15:00:09 -04001970 * @return the {@link ProxyInfo} for the current HTTP proxy, or {@code null} if no
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08001971 * HTTP proxy is active.
Robert Greenwalt434203a2010-10-11 16:00:27 -07001972 */
Paul Jensene0bef712014-12-10 15:12:18 -05001973 public ProxyInfo getDefaultProxy() {
Paul Jensencee9b512015-05-06 07:32:40 -04001974 return getProxyForNetwork(getBoundNetworkForProcess());
Robert Greenwalt434203a2010-10-11 16:00:27 -07001975 }
Robert Greenwaltd55a6b42011-03-25 13:09:25 -07001976
1977 /**
Robert Greenwalt9b2886e2011-08-31 11:46:42 -07001978 * Returns true if the hardware supports the given network type
1979 * else it returns false. This doesn't indicate we have coverage
1980 * or are authorized onto a network, just whether or not the
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08001981 * hardware supports it. For example a GSM phone without a SIM
1982 * should still return {@code true} for mobile data, but a wifi only
1983 * tablet would return {@code false}.
Paul Jensenb2748922015-05-06 11:10:18 -04001984 * <p>This method requires the caller to hold the permission
1985 * {@link android.Manifest.permission#ACCESS_NETWORK_STATE}.
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08001986 *
1987 * @param networkType The network type we'd like to check
1988 * @return {@code true} if supported, else {@code false}
1989 *
Robert Greenwalt9b2886e2011-08-31 11:46:42 -07001990 * @hide
1991 */
1992 public boolean isNetworkSupported(int networkType) {
1993 try {
1994 return mService.isNetworkSupported(networkType);
1995 } catch (RemoteException e) {}
1996 return false;
1997 }
Jeff Sharkey9f7cbf02012-04-12 18:34:54 -07001998
1999 /**
2000 * Returns if the currently active data network is metered. A network is
2001 * classified as metered when the user is sensitive to heavy data usage on
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08002002 * that connection due to monetary costs, data limitations or
2003 * battery/performance issues. You should check this before doing large
2004 * data transfers, and warn the user or delay the operation until another
2005 * network is available.
Paul Jensenb2748922015-05-06 11:10:18 -04002006 * <p>This method requires the caller to hold the permission
2007 * {@link android.Manifest.permission#ACCESS_NETWORK_STATE}.
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08002008 *
2009 * @return {@code true} if large transfers should be avoided, otherwise
2010 * {@code false}.
Jeff Sharkey9f7cbf02012-04-12 18:34:54 -07002011 */
2012 public boolean isActiveNetworkMetered() {
2013 try {
2014 return mService.isActiveNetworkMetered();
2015 } catch (RemoteException e) {
2016 return false;
2017 }
2018 }
Jeff Sharkey69ddab42012-08-25 00:05:46 -07002019
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08002020 /**
2021 * If the LockdownVpn mechanism is enabled, updates the vpn
2022 * with a reload of its profile.
2023 *
2024 * @return a boolean with {@code} indicating success
2025 *
2026 * <p>This method can only be called by the system UID
2027 * {@hide}
2028 */
Jeff Sharkey69ddab42012-08-25 00:05:46 -07002029 public boolean updateLockdownVpn() {
2030 try {
2031 return mService.updateLockdownVpn();
2032 } catch (RemoteException e) {
2033 return false;
2034 }
2035 }
Irfan Sheriffda6da092012-08-16 12:49:23 -07002036
2037 /**
Wink Saville948282b2013-08-29 08:55:16 -07002038 * Check mobile provisioning.
Wink Savilleab9321d2013-06-29 21:10:57 -07002039 *
Wink Savilleab9321d2013-06-29 21:10:57 -07002040 * @param suggestedTimeOutMs, timeout in milliseconds
Wink Savilleab9321d2013-06-29 21:10:57 -07002041 *
2042 * @return time out that will be used, maybe less that suggestedTimeOutMs
2043 * -1 if an error.
2044 *
2045 * {@hide}
2046 */
Wink Saville948282b2013-08-29 08:55:16 -07002047 public int checkMobileProvisioning(int suggestedTimeOutMs) {
Wink Savilleab9321d2013-06-29 21:10:57 -07002048 int timeOutMs = -1;
2049 try {
Wink Saville948282b2013-08-29 08:55:16 -07002050 timeOutMs = mService.checkMobileProvisioning(suggestedTimeOutMs);
Wink Savilleab9321d2013-06-29 21:10:57 -07002051 } catch (RemoteException e) {
2052 }
2053 return timeOutMs;
2054 }
Robert Greenwalte182bfe2013-07-16 12:06:09 -07002055
2056 /**
Wink Saville42d4f082013-07-20 20:31:59 -07002057 * Get the mobile provisioning url.
Robert Greenwalte182bfe2013-07-16 12:06:09 -07002058 * {@hide}
2059 */
2060 public String getMobileProvisioningUrl() {
2061 try {
2062 return mService.getMobileProvisioningUrl();
2063 } catch (RemoteException e) {
2064 }
2065 return null;
2066 }
Wink Saville42d4f082013-07-20 20:31:59 -07002067
2068 /**
Wink Saville948282b2013-08-29 08:55:16 -07002069 * Set sign in error notification to visible or in visible
2070 *
2071 * @param visible
2072 * @param networkType
2073 *
2074 * {@hide}
Paul Jensen3541e9f2015-03-18 12:23:02 -04002075 * @deprecated Doesn't properly deal with multiple connected networks of the same type.
Wink Saville948282b2013-08-29 08:55:16 -07002076 */
2077 public void setProvisioningNotificationVisible(boolean visible, int networkType,
Paul Jensen89e0f092014-09-15 15:59:36 -04002078 String action) {
Wink Saville948282b2013-08-29 08:55:16 -07002079 try {
Paul Jensen89e0f092014-09-15 15:59:36 -04002080 mService.setProvisioningNotificationVisible(visible, networkType, action);
Wink Saville948282b2013-08-29 08:55:16 -07002081 } catch (RemoteException e) {
2082 }
2083 }
Yuhao Zheng5cd1a0e2013-09-09 17:00:04 -07002084
2085 /**
2086 * Set the value for enabling/disabling airplane mode
Paul Jensenb2748922015-05-06 11:10:18 -04002087 * <p>This method requires the caller to hold the permission
2088 * {@link android.Manifest.permission#CONNECTIVITY_INTERNAL}.
Yuhao Zheng5cd1a0e2013-09-09 17:00:04 -07002089 *
2090 * @param enable whether to enable airplane mode or not
2091 *
Yuhao Zheng5cd1a0e2013-09-09 17:00:04 -07002092 * @hide
2093 */
2094 public void setAirplaneMode(boolean enable) {
2095 try {
2096 mService.setAirplaneMode(enable);
2097 } catch (RemoteException e) {
2098 }
2099 }
Robert Greenwalte049c232014-04-11 15:53:27 -07002100
2101 /** {@hide} */
Robert Greenwalta67be032014-05-16 15:49:14 -07002102 public void registerNetworkFactory(Messenger messenger, String name) {
Robert Greenwalte049c232014-04-11 15:53:27 -07002103 try {
Robert Greenwalta67be032014-05-16 15:49:14 -07002104 mService.registerNetworkFactory(messenger, name);
2105 } catch (RemoteException e) { }
2106 }
2107
2108 /** {@hide} */
2109 public void unregisterNetworkFactory(Messenger messenger) {
2110 try {
2111 mService.unregisterNetworkFactory(messenger);
Robert Greenwalte049c232014-04-11 15:53:27 -07002112 } catch (RemoteException e) { }
2113 }
Robert Greenwalt7b816022014-04-18 15:25:25 -07002114
Paul Jensen31a94f42015-02-13 14:18:39 -05002115 /**
2116 * @hide
2117 * Register a NetworkAgent with ConnectivityService.
2118 * @return NetID corresponding to NetworkAgent.
2119 */
2120 public int registerNetworkAgent(Messenger messenger, NetworkInfo ni, LinkProperties lp,
Sreeram Ramachandran8cd33ed2014-07-23 15:23:15 -07002121 NetworkCapabilities nc, int score, NetworkMisc misc) {
Robert Greenwalt7b816022014-04-18 15:25:25 -07002122 try {
Paul Jensen31a94f42015-02-13 14:18:39 -05002123 return mService.registerNetworkAgent(messenger, ni, lp, nc, score, misc);
2124 } catch (RemoteException e) {
2125 return NETID_UNSET;
2126 }
Robert Greenwalt7b816022014-04-18 15:25:25 -07002127 }
2128
Robert Greenwalt9258c642014-03-26 16:47:06 -07002129 /**
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07002130 * Base class for NetworkRequest callbacks. Used for notifications about network
2131 * changes. Should be extended by applications wanting notifications.
Robert Greenwalt9258c642014-03-26 16:47:06 -07002132 */
Robert Greenwalt6078b502014-06-11 16:05:07 -07002133 public static class NetworkCallback {
Robert Greenwalt7b816022014-04-18 15:25:25 -07002134 /**
Lorenzo Colitti07086932015-04-24 12:23:24 +09002135 * Called when the framework connects to a new network to evaluate whether it satisfies this
2136 * request. If evaluation succeeds, this callback may be followed by an {@link #onAvailable}
2137 * callback. There is no guarantee that this new network will satisfy any requests, or that
2138 * the network will stay connected for longer than the time necessary to evaluate it.
2139 * <p>
2140 * Most applications <b>should not</b> act on this callback, and should instead use
2141 * {@link #onAvailable}. This callback is intended for use by applications that can assist
2142 * the framework in properly evaluating the network &mdash; for example, an application that
2143 * can automatically log in to a captive portal without user intervention.
2144 *
2145 * @param network The {@link Network} of the network that is being evaluated.
Lorenzo Colitti66276122015-06-11 14:27:17 +09002146 *
2147 * @hide
Robert Greenwalt7b816022014-04-18 15:25:25 -07002148 */
Robert Greenwalt6078b502014-06-11 16:05:07 -07002149 public void onPreCheck(Network network) {}
Robert Greenwalt7b816022014-04-18 15:25:25 -07002150
2151 /**
Lorenzo Colitti07086932015-04-24 12:23:24 +09002152 * Called when the framework connects and has declared a new network ready for use.
Robert Greenwalt6078b502014-06-11 16:05:07 -07002153 * This callback may be called more than once if the {@link Network} that is
2154 * satisfying the request changes.
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07002155 *
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07002156 * @param network The {@link Network} of the satisfying network.
Robert Greenwalt7b816022014-04-18 15:25:25 -07002157 */
Robert Greenwalt6078b502014-06-11 16:05:07 -07002158 public void onAvailable(Network network) {}
Robert Greenwalt7b816022014-04-18 15:25:25 -07002159
2160 /**
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07002161 * Called when the network is about to be disconnected. Often paired with an
Robert Greenwalt6078b502014-06-11 16:05:07 -07002162 * {@link NetworkCallback#onAvailable} call with the new replacement network
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07002163 * for graceful handover. This may not be called if we have a hard loss
2164 * (loss without warning). This may be followed by either a
Robert Greenwalt6078b502014-06-11 16:05:07 -07002165 * {@link NetworkCallback#onLost} call or a
2166 * {@link NetworkCallback#onAvailable} call for this network depending
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07002167 * on whether we lose or regain it.
2168 *
Robert Greenwalt6078b502014-06-11 16:05:07 -07002169 * @param network The {@link Network} that is about to be disconnected.
2170 * @param maxMsToLive The time in ms the framework will attempt to keep the
2171 * network connected. Note that the network may suffer a
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07002172 * hard loss at any time.
Robert Greenwalt7b816022014-04-18 15:25:25 -07002173 */
Robert Greenwalt6078b502014-06-11 16:05:07 -07002174 public void onLosing(Network network, int maxMsToLive) {}
Robert Greenwalt7b816022014-04-18 15:25:25 -07002175
2176 /**
2177 * Called when the framework has a hard loss of the network or when the
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07002178 * graceful failure ends.
2179 *
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07002180 * @param network The {@link Network} lost.
Robert Greenwalt7b816022014-04-18 15:25:25 -07002181 */
Robert Greenwalt6078b502014-06-11 16:05:07 -07002182 public void onLost(Network network) {}
Robert Greenwalt7b816022014-04-18 15:25:25 -07002183
2184 /**
2185 * Called if no network is found in the given timeout time. If no timeout is given,
2186 * this will not be called.
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07002187 * @hide
Robert Greenwalt7b816022014-04-18 15:25:25 -07002188 */
Robert Greenwalt6078b502014-06-11 16:05:07 -07002189 public void onUnavailable() {}
Robert Greenwalt7b816022014-04-18 15:25:25 -07002190
2191 /**
2192 * Called when the network the framework connected to for this request
2193 * changes capabilities but still satisfies the stated need.
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07002194 *
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07002195 * @param network The {@link Network} whose capabilities have changed.
Lorenzo Colittie285b432015-04-23 15:32:42 +09002196 * @param networkCapabilities The new {@link android.net.NetworkCapabilities} for this network.
Robert Greenwalt7b816022014-04-18 15:25:25 -07002197 */
Robert Greenwalt6078b502014-06-11 16:05:07 -07002198 public void onCapabilitiesChanged(Network network,
Robert Greenwalt7b816022014-04-18 15:25:25 -07002199 NetworkCapabilities networkCapabilities) {}
2200
2201 /**
2202 * Called when the network the framework connected to for this request
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07002203 * changes {@link LinkProperties}.
2204 *
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07002205 * @param network The {@link Network} whose link properties have changed.
2206 * @param linkProperties The new {@link LinkProperties} for this network.
Robert Greenwalt7b816022014-04-18 15:25:25 -07002207 */
Robert Greenwalt6078b502014-06-11 16:05:07 -07002208 public void onLinkPropertiesChanged(Network network, LinkProperties linkProperties) {}
Robert Greenwalt7b816022014-04-18 15:25:25 -07002209
Robert Greenwalt8d482522015-06-24 13:23:42 -07002210 /**
2211 * Called when the network the framework connected to for this request
2212 * goes into {@link NetworkInfo.DetailedState.SUSPENDED}.
2213 * This generally means that while the TCP connections are still live,
2214 * temporarily network data fails to transfer. Specifically this is used
2215 * on cellular networks to mask temporary outages when driving through
2216 * a tunnel, etc.
2217 * @hide
2218 */
2219 public void onNetworkSuspended(Network network) {}
2220
2221 /**
2222 * Called when the network the framework connected to for this request
2223 * returns from a {@link NetworkInfo.DetailedState.SUSPENDED} state.
2224 * This should always be preceeded by a matching {@code onNetworkSuspended}
2225 * call.
2226 * @hide
2227 */
2228 public void onNetworkResumed(Network network) {}
2229
Robert Greenwalt6078b502014-06-11 16:05:07 -07002230 private NetworkRequest networkRequest;
Robert Greenwalt7b816022014-04-18 15:25:25 -07002231 }
2232
Robert Greenwalt9258c642014-03-26 16:47:06 -07002233 private static final int BASE = Protocol.BASE_CONNECTIVITY_MANAGER;
Robert Greenwalt9258c642014-03-26 16:47:06 -07002234 /** @hide */
Robert Greenwalt8d482522015-06-24 13:23:42 -07002235 public static final int CALLBACK_PRECHECK = BASE + 1;
2236 /** @hide */
2237 public static final int CALLBACK_AVAILABLE = BASE + 2;
2238 /** @hide arg1 = TTL */
2239 public static final int CALLBACK_LOSING = BASE + 3;
2240 /** @hide */
2241 public static final int CALLBACK_LOST = BASE + 4;
2242 /** @hide */
2243 public static final int CALLBACK_UNAVAIL = BASE + 5;
2244 /** @hide */
2245 public static final int CALLBACK_CAP_CHANGED = BASE + 6;
2246 /** @hide */
2247 public static final int CALLBACK_IP_CHANGED = BASE + 7;
2248 /** @hide */
2249 public static final int CALLBACK_RELEASED = BASE + 8;
2250 /** @hide */
2251 public static final int CALLBACK_EXIT = BASE + 9;
Robert Greenwalt562cc542014-05-15 18:07:26 -07002252 /** @hide obj = NetworkCapabilities, arg1 = seq number */
Robert Greenwalt8d482522015-06-24 13:23:42 -07002253 private static final int EXPIRE_LEGACY_REQUEST = BASE + 10;
2254 /** @hide */
2255 public static final int CALLBACK_SUSPENDED = BASE + 11;
2256 /** @hide */
2257 public static final int CALLBACK_RESUMED = BASE + 12;
Robert Greenwalt9258c642014-03-26 16:47:06 -07002258
Robert Greenwalt562cc542014-05-15 18:07:26 -07002259 private class CallbackHandler extends Handler {
Robert Greenwalt6078b502014-06-11 16:05:07 -07002260 private final HashMap<NetworkRequest, NetworkCallback>mCallbackMap;
Robert Greenwalt9258c642014-03-26 16:47:06 -07002261 private final AtomicInteger mRefCount;
2262 private static final String TAG = "ConnectivityManager.CallbackHandler";
2263 private final ConnectivityManager mCm;
Robert Greenwalta9ebeef2015-09-03 16:41:45 -07002264 private static final boolean DBG = false;
Robert Greenwalt9258c642014-03-26 16:47:06 -07002265
Robert Greenwalt6078b502014-06-11 16:05:07 -07002266 CallbackHandler(Looper looper, HashMap<NetworkRequest, NetworkCallback>callbackMap,
Robert Greenwalt9258c642014-03-26 16:47:06 -07002267 AtomicInteger refCount, ConnectivityManager cm) {
2268 super(looper);
2269 mCallbackMap = callbackMap;
2270 mRefCount = refCount;
2271 mCm = cm;
2272 }
2273
2274 @Override
2275 public void handleMessage(Message message) {
Robert Greenwalta9ebeef2015-09-03 16:41:45 -07002276 if (DBG) Log.d(TAG, "CM callback handler got msg " + message.what);
Lorenzo Colittidb95a602015-04-24 15:21:21 +09002277 NetworkRequest request = (NetworkRequest) getObject(message, NetworkRequest.class);
2278 Network network = (Network) getObject(message, Network.class);
Robert Greenwalt9258c642014-03-26 16:47:06 -07002279 switch (message.what) {
2280 case CALLBACK_PRECHECK: {
Lorenzo Colittidb95a602015-04-24 15:21:21 +09002281 NetworkCallback callback = getCallback(request, "PRECHECK");
2282 if (callback != null) {
2283 callback.onPreCheck(network);
Robert Greenwalt9258c642014-03-26 16:47:06 -07002284 }
2285 break;
2286 }
2287 case CALLBACK_AVAILABLE: {
Lorenzo Colittidb95a602015-04-24 15:21:21 +09002288 NetworkCallback callback = getCallback(request, "AVAILABLE");
2289 if (callback != null) {
2290 callback.onAvailable(network);
Robert Greenwalt9258c642014-03-26 16:47:06 -07002291 }
2292 break;
2293 }
2294 case CALLBACK_LOSING: {
Lorenzo Colittidb95a602015-04-24 15:21:21 +09002295 NetworkCallback callback = getCallback(request, "LOSING");
2296 if (callback != null) {
2297 callback.onLosing(network, message.arg1);
Robert Greenwalt9258c642014-03-26 16:47:06 -07002298 }
2299 break;
2300 }
2301 case CALLBACK_LOST: {
Lorenzo Colittidb95a602015-04-24 15:21:21 +09002302 NetworkCallback callback = getCallback(request, "LOST");
2303 if (callback != null) {
2304 callback.onLost(network);
Robert Greenwalt9258c642014-03-26 16:47:06 -07002305 }
2306 break;
2307 }
2308 case CALLBACK_UNAVAIL: {
Lorenzo Colittidb95a602015-04-24 15:21:21 +09002309 NetworkCallback callback = getCallback(request, "UNAVAIL");
2310 if (callback != null) {
2311 callback.onUnavailable();
Robert Greenwalt9258c642014-03-26 16:47:06 -07002312 }
2313 break;
2314 }
2315 case CALLBACK_CAP_CHANGED: {
Lorenzo Colittidb95a602015-04-24 15:21:21 +09002316 NetworkCallback callback = getCallback(request, "CAP_CHANGED");
2317 if (callback != null) {
Robert Greenwalta848c1c2014-09-30 16:50:07 -07002318 NetworkCapabilities cap = (NetworkCapabilities)getObject(message,
2319 NetworkCapabilities.class);
Robert Greenwalt9258c642014-03-26 16:47:06 -07002320
Lorenzo Colittidb95a602015-04-24 15:21:21 +09002321 callback.onCapabilitiesChanged(network, cap);
Robert Greenwalt9258c642014-03-26 16:47:06 -07002322 }
2323 break;
2324 }
2325 case CALLBACK_IP_CHANGED: {
Lorenzo Colittidb95a602015-04-24 15:21:21 +09002326 NetworkCallback callback = getCallback(request, "IP_CHANGED");
2327 if (callback != null) {
Robert Greenwalta848c1c2014-09-30 16:50:07 -07002328 LinkProperties lp = (LinkProperties)getObject(message,
2329 LinkProperties.class);
Robert Greenwalt9258c642014-03-26 16:47:06 -07002330
Lorenzo Colittidb95a602015-04-24 15:21:21 +09002331 callback.onLinkPropertiesChanged(network, lp);
Robert Greenwalt9258c642014-03-26 16:47:06 -07002332 }
2333 break;
2334 }
Robert Greenwalt8d482522015-06-24 13:23:42 -07002335 case CALLBACK_SUSPENDED: {
2336 NetworkCallback callback = getCallback(request, "SUSPENDED");
2337 if (callback != null) {
2338 callback.onNetworkSuspended(network);
2339 }
2340 break;
2341 }
2342 case CALLBACK_RESUMED: {
2343 NetworkCallback callback = getCallback(request, "RESUMED");
2344 if (callback != null) {
2345 callback.onNetworkResumed(network);
2346 }
2347 break;
2348 }
Robert Greenwalt9258c642014-03-26 16:47:06 -07002349 case CALLBACK_RELEASED: {
Lorenzo Colittidb95a602015-04-24 15:21:21 +09002350 NetworkCallback callback = null;
Robert Greenwalt9258c642014-03-26 16:47:06 -07002351 synchronized(mCallbackMap) {
Lorenzo Colittidb95a602015-04-24 15:21:21 +09002352 callback = mCallbackMap.remove(request);
Robert Greenwalt9258c642014-03-26 16:47:06 -07002353 }
Lorenzo Colittidb95a602015-04-24 15:21:21 +09002354 if (callback != null) {
Robert Greenwalt6078b502014-06-11 16:05:07 -07002355 synchronized(mRefCount) {
2356 if (mRefCount.decrementAndGet() == 0) {
2357 getLooper().quit();
2358 }
2359 }
Robert Greenwalt9258c642014-03-26 16:47:06 -07002360 } else {
Lorenzo Colittidb95a602015-04-24 15:21:21 +09002361 Log.e(TAG, "callback not found for RELEASED message");
Robert Greenwalt9258c642014-03-26 16:47:06 -07002362 }
Robert Greenwalt9258c642014-03-26 16:47:06 -07002363 break;
2364 }
2365 case CALLBACK_EXIT: {
Lorenzo Colittidb95a602015-04-24 15:21:21 +09002366 Log.d(TAG, "Listener quitting");
Robert Greenwalt9258c642014-03-26 16:47:06 -07002367 getLooper().quit();
2368 break;
2369 }
Robert Greenwalt562cc542014-05-15 18:07:26 -07002370 case EXPIRE_LEGACY_REQUEST: {
2371 expireRequest((NetworkCapabilities)message.obj, message.arg1);
2372 break;
2373 }
Robert Greenwalt9258c642014-03-26 16:47:06 -07002374 }
2375 }
2376
Robert Greenwalta848c1c2014-09-30 16:50:07 -07002377 private Object getObject(Message msg, Class c) {
2378 return msg.getData().getParcelable(c.getSimpleName());
Robert Greenwalt9258c642014-03-26 16:47:06 -07002379 }
Lorenzo Colittidb95a602015-04-24 15:21:21 +09002380
2381 private NetworkCallback getCallback(NetworkRequest req, String name) {
2382 NetworkCallback callback;
Robert Greenwalt9258c642014-03-26 16:47:06 -07002383 synchronized(mCallbackMap) {
Lorenzo Colittidb95a602015-04-24 15:21:21 +09002384 callback = mCallbackMap.get(req);
Robert Greenwalt9258c642014-03-26 16:47:06 -07002385 }
Lorenzo Colittidb95a602015-04-24 15:21:21 +09002386 if (callback == null) {
2387 Log.e(TAG, "callback not found for " + name + " message");
2388 }
2389 return callback;
Robert Greenwalt9258c642014-03-26 16:47:06 -07002390 }
Robert Greenwalt9258c642014-03-26 16:47:06 -07002391 }
2392
Robert Greenwalt6078b502014-06-11 16:05:07 -07002393 private void incCallbackHandlerRefCount() {
Robert Greenwalt9258c642014-03-26 16:47:06 -07002394 synchronized(sCallbackRefCount) {
2395 if (sCallbackRefCount.incrementAndGet() == 1) {
2396 // TODO - switch this over to a ManagerThread or expire it when done
2397 HandlerThread callbackThread = new HandlerThread("ConnectivityManager");
2398 callbackThread.start();
2399 sCallbackHandler = new CallbackHandler(callbackThread.getLooper(),
Robert Greenwalt6078b502014-06-11 16:05:07 -07002400 sNetworkCallback, sCallbackRefCount, this);
Robert Greenwalt9258c642014-03-26 16:47:06 -07002401 }
2402 }
2403 }
2404
Robert Greenwalt6078b502014-06-11 16:05:07 -07002405 private void decCallbackHandlerRefCount() {
Robert Greenwalt9258c642014-03-26 16:47:06 -07002406 synchronized(sCallbackRefCount) {
2407 if (sCallbackRefCount.decrementAndGet() == 0) {
2408 sCallbackHandler.obtainMessage(CALLBACK_EXIT).sendToTarget();
2409 sCallbackHandler = null;
2410 }
2411 }
2412 }
2413
Robert Greenwalt6078b502014-06-11 16:05:07 -07002414 static final HashMap<NetworkRequest, NetworkCallback> sNetworkCallback =
2415 new HashMap<NetworkRequest, NetworkCallback>();
Robert Greenwalt9258c642014-03-26 16:47:06 -07002416 static final AtomicInteger sCallbackRefCount = new AtomicInteger(0);
2417 static CallbackHandler sCallbackHandler = null;
2418
2419 private final static int LISTEN = 1;
2420 private final static int REQUEST = 2;
2421
Robert Greenwalt562cc542014-05-15 18:07:26 -07002422 private NetworkRequest sendRequestForNetwork(NetworkCapabilities need,
Robert Greenwalt6078b502014-06-11 16:05:07 -07002423 NetworkCallback networkCallback, int timeoutSec, int action,
Robert Greenwalt32aa65a2014-06-02 15:32:02 -07002424 int legacyType) {
Robert Greenwalt6078b502014-06-11 16:05:07 -07002425 if (networkCallback == null) {
2426 throw new IllegalArgumentException("null NetworkCallback");
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07002427 }
Robert Greenwalt9258c642014-03-26 16:47:06 -07002428 if (need == null) throw new IllegalArgumentException("null NetworkCapabilities");
2429 try {
Robert Greenwalt6078b502014-06-11 16:05:07 -07002430 incCallbackHandlerRefCount();
Paul Jensen7221cc32014-06-27 11:05:32 -04002431 synchronized(sNetworkCallback) {
2432 if (action == LISTEN) {
2433 networkCallback.networkRequest = mService.listenForNetwork(need,
2434 new Messenger(sCallbackHandler), new Binder());
2435 } else {
2436 networkCallback.networkRequest = mService.requestNetwork(need,
2437 new Messenger(sCallbackHandler), timeoutSec, new Binder(), legacyType);
2438 }
2439 if (networkCallback.networkRequest != null) {
Robert Greenwalt6078b502014-06-11 16:05:07 -07002440 sNetworkCallback.put(networkCallback.networkRequest, networkCallback);
Robert Greenwalt9258c642014-03-26 16:47:06 -07002441 }
2442 }
2443 } catch (RemoteException e) {}
Robert Greenwalt6078b502014-06-11 16:05:07 -07002444 if (networkCallback.networkRequest == null) decCallbackHandlerRefCount();
2445 return networkCallback.networkRequest;
Robert Greenwalt9258c642014-03-26 16:47:06 -07002446 }
2447
2448 /**
Lorenzo Colittie285b432015-04-23 15:32:42 +09002449 * Request a network to satisfy a set of {@link android.net.NetworkCapabilities}.
Robert Greenwalt9258c642014-03-26 16:47:06 -07002450 *
2451 * This {@link NetworkRequest} will live until released via
Robert Greenwalt6078b502014-06-11 16:05:07 -07002452 * {@link #unregisterNetworkCallback} or the calling application exits.
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07002453 * Status of the request can be followed by listening to the various
Robert Greenwalt6078b502014-06-11 16:05:07 -07002454 * callbacks described in {@link NetworkCallback}. The {@link Network}
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07002455 * can be used to direct traffic to the network.
Paul Jensenbb2e0e92015-06-16 15:11:58 -04002456 * <p>It is presently unsupported to request a network with mutable
2457 * {@link NetworkCapabilities} such as
2458 * {@link NetworkCapabilities#NET_CAPABILITY_VALIDATED} or
2459 * {@link NetworkCapabilities#NET_CAPABILITY_CAPTIVE_PORTAL}
2460 * as these {@code NetworkCapabilities} represent states that a particular
2461 * network may never attain, and whether a network will attain these states
2462 * is unknown prior to bringing up the network so the framework does not
2463 * know how to go about satisfing a request with these capabilities.
Paul Jensenb2748922015-05-06 11:10:18 -04002464 * <p>This method requires the caller to hold the permission
2465 * {@link android.Manifest.permission#CHANGE_NETWORK_STATE}.
Robert Greenwalt9258c642014-03-26 16:47:06 -07002466 *
Robert Greenwalt6078b502014-06-11 16:05:07 -07002467 * @param request {@link NetworkRequest} describing this request.
2468 * @param networkCallback The {@link NetworkCallback} to be utilized for this
2469 * request. Note the callback must not be shared - they
2470 * uniquely specify this request.
Paul Jensenbb2e0e92015-06-16 15:11:58 -04002471 * @throws IllegalArgumentException if {@code request} specifies any mutable
2472 * {@code NetworkCapabilities}.
Robert Greenwalt9258c642014-03-26 16:47:06 -07002473 */
Robert Greenwalt6078b502014-06-11 16:05:07 -07002474 public void requestNetwork(NetworkRequest request, NetworkCallback networkCallback) {
2475 sendRequestForNetwork(request.networkCapabilities, networkCallback, 0,
Ye Wenb87875e2014-07-21 14:19:01 -07002476 REQUEST, inferLegacyTypeForNetworkCapabilities(request.networkCapabilities));
Robert Greenwalt9258c642014-03-26 16:47:06 -07002477 }
2478
2479 /**
Lorenzo Colittie285b432015-04-23 15:32:42 +09002480 * Request a network to satisfy a set of {@link android.net.NetworkCapabilities}, limited
Robert Greenwalt9258c642014-03-26 16:47:06 -07002481 * by a timeout.
2482 *
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07002483 * This function behaves identically to the non-timedout version, but if a suitable
Robert Greenwalt6078b502014-06-11 16:05:07 -07002484 * network is not found within the given time (in milliseconds) the
2485 * {@link NetworkCallback#unavailable} callback is called. The request must
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07002486 * still be released normally by calling {@link releaseNetworkRequest}.
Paul Jensenb2748922015-05-06 11:10:18 -04002487 * <p>This method requires the caller to hold the permission
2488 * {@link android.Manifest.permission#CHANGE_NETWORK_STATE}.
Robert Greenwalt6078b502014-06-11 16:05:07 -07002489 * @param request {@link NetworkRequest} describing this request.
2490 * @param networkCallback The callbacks to be utilized for this request. Note
2491 * the callbacks must not be shared - they uniquely specify
2492 * this request.
2493 * @param timeoutMs The time in milliseconds to attempt looking for a suitable network
2494 * before {@link NetworkCallback#unavailable} is called.
Robert Greenwalt9258c642014-03-26 16:47:06 -07002495 * @hide
2496 */
Robert Greenwalt6078b502014-06-11 16:05:07 -07002497 public void requestNetwork(NetworkRequest request, NetworkCallback networkCallback,
2498 int timeoutMs) {
2499 sendRequestForNetwork(request.networkCapabilities, networkCallback, timeoutMs,
Ye Wenb87875e2014-07-21 14:19:01 -07002500 REQUEST, inferLegacyTypeForNetworkCapabilities(request.networkCapabilities));
Robert Greenwalt9258c642014-03-26 16:47:06 -07002501 }
2502
2503 /**
Robert Greenwalt6078b502014-06-11 16:05:07 -07002504 * The maximum number of milliseconds the framework will look for a suitable network
Robert Greenwalt9258c642014-03-26 16:47:06 -07002505 * during a timeout-equiped call to {@link requestNetwork}.
2506 * {@hide}
2507 */
Robert Greenwalt6078b502014-06-11 16:05:07 -07002508 public final static int MAX_NETWORK_REQUEST_TIMEOUT_MS = 100 * 60 * 1000;
Robert Greenwalt9258c642014-03-26 16:47:06 -07002509
2510 /**
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07002511 * The lookup key for a {@link Network} object included with the intent after
Jeremy Joslin46e3ac82014-11-05 10:32:09 -08002512 * successfully finding a network for the applications request. Retrieve it with
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07002513 * {@link android.content.Intent#getParcelableExtra(String)}.
Jeremy Joslinfcde58f2015-02-11 16:51:13 -08002514 * <p>
Paul Jensen72db88e2015-03-10 10:54:12 -04002515 * Note that if you intend to invoke {@link Network#openConnection(java.net.URL)}
2516 * then you must get a ConnectivityManager instance before doing so.
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07002517 */
Erik Kline90e93072014-11-19 12:12:24 +09002518 public static final String EXTRA_NETWORK = "android.net.extra.NETWORK";
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07002519
2520 /**
Robert Greenwalt6078b502014-06-11 16:05:07 -07002521 * The lookup key for a {@link NetworkRequest} object included with the intent after
Jeremy Joslin46e3ac82014-11-05 10:32:09 -08002522 * successfully finding a network for the applications request. Retrieve it with
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07002523 * {@link android.content.Intent#getParcelableExtra(String)}.
2524 */
Erik Kline90e93072014-11-19 12:12:24 +09002525 public static final String EXTRA_NETWORK_REQUEST = "android.net.extra.NETWORK_REQUEST";
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07002526
2527
2528 /**
Lorenzo Colittie285b432015-04-23 15:32:42 +09002529 * Request a network to satisfy a set of {@link android.net.NetworkCapabilities}.
Robert Greenwalt9258c642014-03-26 16:47:06 -07002530 *
Jeremy Joslin46e3ac82014-11-05 10:32:09 -08002531 * This function behaves identically to the version that takes a NetworkCallback, but instead
Robert Greenwalt6078b502014-06-11 16:05:07 -07002532 * of {@link NetworkCallback} a {@link PendingIntent} is used. This means
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07002533 * the request may outlive the calling application and get called back when a suitable
2534 * network is found.
Robert Greenwalt9258c642014-03-26 16:47:06 -07002535 * <p>
2536 * The operation is an Intent broadcast that goes to a broadcast receiver that
2537 * you registered with {@link Context#registerReceiver} or through the
2538 * &lt;receiver&gt; tag in an AndroidManifest.xml file
2539 * <p>
2540 * The operation Intent is delivered with two extras, a {@link Network} typed
Erik Kline90e93072014-11-19 12:12:24 +09002541 * extra called {@link #EXTRA_NETWORK} and a {@link NetworkRequest}
2542 * typed extra called {@link #EXTRA_NETWORK_REQUEST} containing
Robert Greenwalt9258c642014-03-26 16:47:06 -07002543 * the original requests parameters. It is important to create a new,
Robert Greenwalt6078b502014-06-11 16:05:07 -07002544 * {@link NetworkCallback} based request before completing the processing of the
Robert Greenwalt9258c642014-03-26 16:47:06 -07002545 * Intent to reserve the network or it will be released shortly after the Intent
2546 * is processed.
2547 * <p>
Paul Jensen694f2b82015-06-17 14:15:39 -04002548 * If there is already a request for this Intent registered (with the equality of
Robert Greenwalt9258c642014-03-26 16:47:06 -07002549 * two Intents defined by {@link Intent#filterEquals}), then it will be removed and
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07002550 * replaced by this one, effectively releasing the previous {@link NetworkRequest}.
Robert Greenwalt9258c642014-03-26 16:47:06 -07002551 * <p>
Jeremy Joslin46e3ac82014-11-05 10:32:09 -08002552 * The request may be released normally by calling
2553 * {@link #releaseNetworkRequest(android.app.PendingIntent)}.
Paul Jensenbb2e0e92015-06-16 15:11:58 -04002554 * <p>It is presently unsupported to request a network with either
2555 * {@link NetworkCapabilities#NET_CAPABILITY_VALIDATED} or
2556 * {@link NetworkCapabilities#NET_CAPABILITY_CAPTIVE_PORTAL}
2557 * as these {@code NetworkCapabilities} represent states that a particular
2558 * network may never attain, and whether a network will attain these states
2559 * is unknown prior to bringing up the network so the framework does not
2560 * know how to go about satisfing a request with these capabilities.
Paul Jensenb2748922015-05-06 11:10:18 -04002561 * <p>This method requires the caller to hold the permission
2562 * {@link android.Manifest.permission#CHANGE_NETWORK_STATE}.
Robert Greenwalt6078b502014-06-11 16:05:07 -07002563 * @param request {@link NetworkRequest} describing this request.
Robert Greenwalt9258c642014-03-26 16:47:06 -07002564 * @param operation Action to perform when the network is available (corresponds
Robert Greenwalt6078b502014-06-11 16:05:07 -07002565 * to the {@link NetworkCallback#onAvailable} call. Typically
Jeremy Joslin46e3ac82014-11-05 10:32:09 -08002566 * comes from {@link PendingIntent#getBroadcast}. Cannot be null.
Paul Jensenbb2e0e92015-06-16 15:11:58 -04002567 * @throws IllegalArgumentException if {@code request} contains either
2568 * {@link NetworkCapabilities#NET_CAPABILITY_VALIDATED} or
2569 * {@link NetworkCapabilities#NET_CAPABILITY_CAPTIVE_PORTAL}.
Robert Greenwalt9258c642014-03-26 16:47:06 -07002570 */
Robert Greenwalt6078b502014-06-11 16:05:07 -07002571 public void requestNetwork(NetworkRequest request, PendingIntent operation) {
Jeremy Joslin46e3ac82014-11-05 10:32:09 -08002572 checkPendingIntent(operation);
Robert Greenwalt9258c642014-03-26 16:47:06 -07002573 try {
Robert Greenwalt6078b502014-06-11 16:05:07 -07002574 mService.pendingRequestForNetwork(request.networkCapabilities, operation);
Robert Greenwalt9258c642014-03-26 16:47:06 -07002575 } catch (RemoteException e) {}
Robert Greenwalt9258c642014-03-26 16:47:06 -07002576 }
2577
2578 /**
Jeremy Joslin46e3ac82014-11-05 10:32:09 -08002579 * Removes a request made via {@link #requestNetwork(NetworkRequest, android.app.PendingIntent)}
2580 * <p>
2581 * This method has the same behavior as {@link #unregisterNetworkCallback} with respect to
2582 * releasing network resources and disconnecting.
2583 *
2584 * @param operation A PendingIntent equal (as defined by {@link Intent#filterEquals}) to the
2585 * PendingIntent passed to
2586 * {@link #requestNetwork(NetworkRequest, android.app.PendingIntent)} with the
2587 * corresponding NetworkRequest you'd like to remove. Cannot be null.
2588 */
2589 public void releaseNetworkRequest(PendingIntent operation) {
2590 checkPendingIntent(operation);
2591 try {
2592 mService.releasePendingNetworkRequest(operation);
2593 } catch (RemoteException e) {}
2594 }
2595
2596 private void checkPendingIntent(PendingIntent intent) {
2597 if (intent == null) {
2598 throw new IllegalArgumentException("PendingIntent cannot be null.");
2599 }
2600 }
2601
2602 /**
Robert Greenwalt9258c642014-03-26 16:47:06 -07002603 * Registers to receive notifications about all networks which satisfy the given
Robert Greenwalt6078b502014-06-11 16:05:07 -07002604 * {@link NetworkRequest}. The callbacks will continue to be called until
2605 * either the application exits or {@link #unregisterNetworkCallback} is called
Paul Jensenb2748922015-05-06 11:10:18 -04002606 * <p>This method requires the caller to hold the permission
2607 * {@link android.Manifest.permission#ACCESS_NETWORK_STATE}.
Robert Greenwalt9258c642014-03-26 16:47:06 -07002608 *
Robert Greenwalt6078b502014-06-11 16:05:07 -07002609 * @param request {@link NetworkRequest} describing this request.
2610 * @param networkCallback The {@link NetworkCallback} that the system will call as suitable
2611 * networks change state.
Robert Greenwalt9258c642014-03-26 16:47:06 -07002612 */
Robert Greenwalt6078b502014-06-11 16:05:07 -07002613 public void registerNetworkCallback(NetworkRequest request, NetworkCallback networkCallback) {
2614 sendRequestForNetwork(request.networkCapabilities, networkCallback, 0, LISTEN, TYPE_NONE);
Robert Greenwalt9258c642014-03-26 16:47:06 -07002615 }
2616
2617 /**
Paul Jensen694f2b82015-06-17 14:15:39 -04002618 * Registers a PendingIntent to be sent when a network is available which satisfies the given
2619 * {@link NetworkRequest}.
2620 *
2621 * This function behaves identically to the version that takes a NetworkCallback, but instead
2622 * of {@link NetworkCallback} a {@link PendingIntent} is used. This means
2623 * the request may outlive the calling application and get called back when a suitable
2624 * network is found.
2625 * <p>
2626 * The operation is an Intent broadcast that goes to a broadcast receiver that
2627 * you registered with {@link Context#registerReceiver} or through the
2628 * &lt;receiver&gt; tag in an AndroidManifest.xml file
2629 * <p>
2630 * The operation Intent is delivered with two extras, a {@link Network} typed
2631 * extra called {@link #EXTRA_NETWORK} and a {@link NetworkRequest}
2632 * typed extra called {@link #EXTRA_NETWORK_REQUEST} containing
2633 * the original requests parameters.
2634 * <p>
2635 * If there is already a request for this Intent registered (with the equality of
2636 * two Intents defined by {@link Intent#filterEquals}), then it will be removed and
2637 * replaced by this one, effectively releasing the previous {@link NetworkRequest}.
2638 * <p>
2639 * The request may be released normally by calling
Paul Jensenf2c1cfe2015-06-30 14:29:18 -04002640 * {@link #unregisterNetworkCallback(android.app.PendingIntent)}.
Paul Jensen694f2b82015-06-17 14:15:39 -04002641 * <p>This method requires the caller to hold the permission
2642 * {@link android.Manifest.permission#ACCESS_NETWORK_STATE}.
2643 * @param request {@link NetworkRequest} describing this request.
2644 * @param operation Action to perform when the network is available (corresponds
2645 * to the {@link NetworkCallback#onAvailable} call. Typically
2646 * comes from {@link PendingIntent#getBroadcast}. Cannot be null.
2647 */
2648 public void registerNetworkCallback(NetworkRequest request, PendingIntent operation) {
2649 checkPendingIntent(operation);
2650 try {
2651 mService.pendingListenForNetwork(request.networkCapabilities, operation);
2652 } catch (RemoteException e) {}
2653 }
2654
2655 /**
fengludb571472015-04-21 17:12:05 -07002656 * Requests bandwidth update for a given {@link Network} and returns whether the update request
2657 * is accepted by ConnectivityService. Once accepted, ConnectivityService will poll underlying
2658 * network connection for updated bandwidth information. The caller will be notified via
2659 * {@link ConnectivityManager.NetworkCallback} if there is an update. Notice that this
2660 * method assumes that the caller has previously called {@link #registerNetworkCallback} to
2661 * listen for network changes.
fenglub15e72b2015-03-20 11:29:56 -07002662 *
fengluae519192015-04-27 14:28:04 -07002663 * @param network {@link Network} specifying which network you're interested.
fengludb571472015-04-21 17:12:05 -07002664 * @return {@code true} on success, {@code false} if the {@link Network} is no longer valid.
fenglub15e72b2015-03-20 11:29:56 -07002665 */
fengludb571472015-04-21 17:12:05 -07002666 public boolean requestBandwidthUpdate(Network network) {
fenglub15e72b2015-03-20 11:29:56 -07002667 try {
fengludb571472015-04-21 17:12:05 -07002668 return mService.requestBandwidthUpdate(network);
fenglub15e72b2015-03-20 11:29:56 -07002669 } catch (RemoteException e) {
2670 return false;
2671 }
2672 }
2673
2674 /**
Robert Greenwalt6078b502014-06-11 16:05:07 -07002675 * Unregisters callbacks about and possibly releases networks originating from
Lorenzo Colitti2ea89e52015-04-24 17:03:31 +09002676 * {@link #requestNetwork(NetworkRequest, NetworkCallback)} and {@link #registerNetworkCallback}
2677 * calls. If the given {@code NetworkCallback} had previously been used with
2678 * {@code #requestNetwork}, any networks that had been connected to only to satisfy that request
2679 * will be disconnected.
Robert Greenwalt9258c642014-03-26 16:47:06 -07002680 *
Robert Greenwalt6078b502014-06-11 16:05:07 -07002681 * @param networkCallback The {@link NetworkCallback} used when making the request.
Robert Greenwalt9258c642014-03-26 16:47:06 -07002682 */
Robert Greenwalt6078b502014-06-11 16:05:07 -07002683 public void unregisterNetworkCallback(NetworkCallback networkCallback) {
2684 if (networkCallback == null || networkCallback.networkRequest == null ||
2685 networkCallback.networkRequest.requestId == REQUEST_ID_UNSET) {
2686 throw new IllegalArgumentException("Invalid NetworkCallback");
2687 }
Robert Greenwalt9258c642014-03-26 16:47:06 -07002688 try {
Robert Greenwalt6078b502014-06-11 16:05:07 -07002689 mService.releaseNetworkRequest(networkCallback.networkRequest);
Robert Greenwalt9258c642014-03-26 16:47:06 -07002690 } catch (RemoteException e) {}
2691 }
Paul Jensen6d3ff9e2014-05-29 10:12:39 -04002692
2693 /**
Paul Jensenf2c1cfe2015-06-30 14:29:18 -04002694 * Unregisters a callback previously registered via
2695 * {@link #registerNetworkCallback(NetworkRequest, android.app.PendingIntent)}.
2696 *
2697 * @param operation A PendingIntent equal (as defined by {@link Intent#filterEquals}) to the
2698 * PendingIntent passed to
2699 * {@link #registerNetworkCallback(NetworkRequest, android.app.PendingIntent)}.
2700 * Cannot be null.
2701 */
2702 public void unregisterNetworkCallback(PendingIntent operation) {
2703 releaseNetworkRequest(operation);
2704 }
2705
2706 /**
Lorenzo Colittie03c3c72015-04-03 16:38:52 +09002707 * Informs the system whether it should switch to {@code network} regardless of whether it is
2708 * validated or not. If {@code accept} is true, and the network was explicitly selected by the
2709 * user (e.g., by selecting a Wi-Fi network in the Settings app), then the network will become
2710 * the system default network regardless of any other network that's currently connected. If
2711 * {@code always} is true, then the choice is remembered, so that the next time the user
2712 * connects to this network, the system will switch to it.
2713 *
2714 * <p>This method requires the caller to hold the permission
2715 * {@link android.Manifest.permission#CONNECTIVITY_INTERNAL}
2716 *
2717 * @param network The network to accept.
2718 * @param accept Whether to accept the network even if unvalidated.
2719 * @param always Whether to remember this choice in the future.
2720 *
2721 * @hide
2722 */
2723 public void setAcceptUnvalidated(Network network, boolean accept, boolean always) {
2724 try {
2725 mService.setAcceptUnvalidated(network, accept, always);
2726 } catch (RemoteException e) {}
2727 }
2728
2729 /**
Stuart Scott984dc852015-03-30 13:17:11 -07002730 * Resets all connectivity manager settings back to factory defaults.
2731 * @hide
2732 */
2733 public void factoryReset() {
Stuart Scott984dc852015-03-30 13:17:11 -07002734 try {
Stuart Scottf1fb3972015-04-02 18:00:02 -07002735 mService.factoryReset();
Stuart Scott984dc852015-03-30 13:17:11 -07002736 } catch (RemoteException e) {
Stuart Scott984dc852015-03-30 13:17:11 -07002737 }
2738 }
2739
2740 /**
Paul Jensen6d3ff9e2014-05-29 10:12:39 -04002741 * Binds the current process to {@code network}. All Sockets created in the future
2742 * (and not explicitly bound via a bound SocketFactory from
2743 * {@link Network#getSocketFactory() Network.getSocketFactory()}) will be bound to
2744 * {@code network}. All host name resolutions will be limited to {@code network} as well.
2745 * Note that if {@code network} ever disconnects, all Sockets created in this way will cease to
2746 * work and all host name resolutions will fail. This is by design so an application doesn't
2747 * accidentally use Sockets it thinks are still bound to a particular {@link Network}.
2748 * To clear binding pass {@code null} for {@code network}. Using individually bound
2749 * Sockets created by Network.getSocketFactory().createSocket() and
2750 * performing network-specific host name resolutions via
2751 * {@link Network#getAllByName Network.getAllByName} is preferred to calling
Paul Jensen72db88e2015-03-10 10:54:12 -04002752 * {@code bindProcessToNetwork}.
Paul Jensen6d3ff9e2014-05-29 10:12:39 -04002753 *
2754 * @param network The {@link Network} to bind the current process to, or {@code null} to clear
2755 * the current binding.
2756 * @return {@code true} on success, {@code false} if the {@link Network} is no longer valid.
2757 */
Paul Jensen72db88e2015-03-10 10:54:12 -04002758 public boolean bindProcessToNetwork(Network network) {
2759 // Forcing callers to call thru non-static function ensures ConnectivityManager
2760 // instantiated.
2761 return setProcessDefaultNetwork(network);
2762 }
2763
2764 /**
2765 * Binds the current process to {@code network}. All Sockets created in the future
2766 * (and not explicitly bound via a bound SocketFactory from
2767 * {@link Network#getSocketFactory() Network.getSocketFactory()}) will be bound to
2768 * {@code network}. All host name resolutions will be limited to {@code network} as well.
2769 * Note that if {@code network} ever disconnects, all Sockets created in this way will cease to
2770 * work and all host name resolutions will fail. This is by design so an application doesn't
2771 * accidentally use Sockets it thinks are still bound to a particular {@link Network}.
2772 * To clear binding pass {@code null} for {@code network}. Using individually bound
2773 * Sockets created by Network.getSocketFactory().createSocket() and
2774 * performing network-specific host name resolutions via
2775 * {@link Network#getAllByName Network.getAllByName} is preferred to calling
2776 * {@code setProcessDefaultNetwork}.
2777 *
2778 * @param network The {@link Network} to bind the current process to, or {@code null} to clear
2779 * the current binding.
2780 * @return {@code true} on success, {@code false} if the {@link Network} is no longer valid.
2781 * @deprecated This function can throw {@link IllegalStateException}. Use
2782 * {@link #bindProcessToNetwork} instead. {@code bindProcessToNetwork}
2783 * is a direct replacement.
2784 */
Paul Jensen6d3ff9e2014-05-29 10:12:39 -04002785 public static boolean setProcessDefaultNetwork(Network network) {
Paul Jensenc91b5342014-08-27 12:38:45 -04002786 int netId = (network == null) ? NETID_UNSET : network.netId;
Paul Jensen72db88e2015-03-10 10:54:12 -04002787 if (netId == NetworkUtils.getBoundNetworkForProcess()) {
Paul Jensenc91b5342014-08-27 12:38:45 -04002788 return true;
2789 }
2790 if (NetworkUtils.bindProcessToNetwork(netId)) {
Paul Jensene0bef712014-12-10 15:12:18 -05002791 // Set HTTP proxy system properties to match network.
2792 // TODO: Deprecate this static method and replace it with a non-static version.
Lorenzo Colittiec4c5552015-04-22 11:52:48 +09002793 try {
2794 Proxy.setHttpProxySystemProperty(getInstance().getDefaultProxy());
2795 } catch (SecurityException e) {
2796 // The process doesn't have ACCESS_NETWORK_STATE, so we can't fetch the proxy.
2797 Log.e(TAG, "Can't set proxy properties", e);
2798 }
Paul Jensenc91b5342014-08-27 12:38:45 -04002799 // Must flush DNS cache as new network may have different DNS resolutions.
2800 InetAddress.clearDnsCache();
2801 // Must flush socket pool as idle sockets will be bound to previous network and may
2802 // cause subsequent fetches to be performed on old network.
2803 NetworkEventDispatcher.getInstance().onNetworkConfigurationChanged();
2804 return true;
2805 } else {
2806 return false;
2807 }
Paul Jensen6d3ff9e2014-05-29 10:12:39 -04002808 }
2809
2810 /**
2811 * Returns the {@link Network} currently bound to this process via
Paul Jensen72db88e2015-03-10 10:54:12 -04002812 * {@link #bindProcessToNetwork}, or {@code null} if no {@link Network} is explicitly bound.
Paul Jensen6d3ff9e2014-05-29 10:12:39 -04002813 *
2814 * @return {@code Network} to which this process is bound, or {@code null}.
2815 */
Paul Jensen72db88e2015-03-10 10:54:12 -04002816 public Network getBoundNetworkForProcess() {
2817 // Forcing callers to call thru non-static function ensures ConnectivityManager
2818 // instantiated.
2819 return getProcessDefaultNetwork();
2820 }
2821
2822 /**
2823 * Returns the {@link Network} currently bound to this process via
2824 * {@link #bindProcessToNetwork}, or {@code null} if no {@link Network} is explicitly bound.
2825 *
2826 * @return {@code Network} to which this process is bound, or {@code null}.
2827 * @deprecated Using this function can lead to other functions throwing
2828 * {@link IllegalStateException}. Use {@link #getBoundNetworkForProcess} instead.
2829 * {@code getBoundNetworkForProcess} is a direct replacement.
2830 */
Paul Jensen6d3ff9e2014-05-29 10:12:39 -04002831 public static Network getProcessDefaultNetwork() {
Paul Jensen72db88e2015-03-10 10:54:12 -04002832 int netId = NetworkUtils.getBoundNetworkForProcess();
Paul Jensenbcc76d32014-07-11 08:17:29 -04002833 if (netId == NETID_UNSET) return null;
Paul Jensen6d3ff9e2014-05-29 10:12:39 -04002834 return new Network(netId);
2835 }
2836
Lorenzo Colittiffc42b02015-07-29 11:41:21 +09002837 private void unsupportedStartingFrom(int version) {
2838 if (Process.myUid() == Process.SYSTEM_UID) {
2839 // The getApplicationInfo() call we make below is not supported in system context, and
2840 // we want to allow the system to use these APIs anyway.
2841 return;
2842 }
2843
2844 if (mContext.getApplicationInfo().targetSdkVersion >= version) {
2845 throw new UnsupportedOperationException(
2846 "This method is not supported in target SDK version " + version + " and above");
2847 }
2848 }
2849
2850 // Checks whether the calling app can use the legacy routing API (startUsingNetworkFeature,
2851 // stopUsingNetworkFeature, requestRouteToHost), and if not throw UnsupportedOperationException.
2852 // TODO: convert the existing system users (Tethering, GpsLocationProvider) to the new APIs and
2853 // remove these exemptions. Note that this check is not secure, and apps can still access these
2854 // functions by accessing ConnectivityService directly. However, it should be clear that doing
2855 // so is unsupported and may break in the future. http://b/22728205
2856 private void checkLegacyRoutingApiAccess() {
2857 if (mContext.checkCallingOrSelfPermission("com.android.permission.INJECT_OMADM_SETTINGS")
2858 == PackageManager.PERMISSION_GRANTED) {
2859 return;
2860 }
2861
Dianne Hackborn692a2442015-07-31 10:35:34 -07002862 unsupportedStartingFrom(VERSION_CODES.M);
Lorenzo Colittiffc42b02015-07-29 11:41:21 +09002863 }
2864
Paul Jensen6d3ff9e2014-05-29 10:12:39 -04002865 /**
2866 * Binds host resolutions performed by this process to {@code network}.
Paul Jensen72db88e2015-03-10 10:54:12 -04002867 * {@link #bindProcessToNetwork} takes precedence over this setting.
Paul Jensen6d3ff9e2014-05-29 10:12:39 -04002868 *
2869 * @param network The {@link Network} to bind host resolutions from the current process to, or
2870 * {@code null} to clear the current binding.
2871 * @return {@code true} on success, {@code false} if the {@link Network} is no longer valid.
2872 * @hide
2873 * @deprecated This is strictly for legacy usage to support {@link #startUsingNetworkFeature}.
2874 */
2875 public static boolean setProcessDefaultNetworkForHostResolution(Network network) {
Paul Jensenbcc76d32014-07-11 08:17:29 -04002876 return NetworkUtils.bindProcessToNetworkForHostResolution(
2877 network == null ? NETID_UNSET : network.netId);
Paul Jensen6d3ff9e2014-05-29 10:12:39 -04002878 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002879}