blob: c4f0847e694bce39a58fbe07f9ca8a815ea5ae17 [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
Robin Lee244ce8e2016-01-05 18:03:46 +000020import android.annotation.Nullable;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080021import android.annotation.SdkConstant;
22import android.annotation.SdkConstant.SdkConstantType;
Udam Sainib7c24872016-01-04 12:16:14 -080023import android.annotation.SystemApi;
Robert Greenwalt9258c642014-03-26 16:47:06 -070024import android.app.PendingIntent;
Jeff Sharkey8fc27e82012-04-04 20:40:58 -070025import android.content.Context;
Robert Greenwaltd19c41c2014-05-18 23:07:25 -070026import android.content.Intent;
Lorenzo Colittiffc42b02015-07-29 11:41:21 +090027import android.content.pm.PackageManager;
Robert Greenwalt42acef32009-08-12 16:08:25 -070028import android.os.Binder;
Jeff Sharkey3a844fc2011-08-16 14:37:57 -070029import android.os.Build.VERSION_CODES;
Robert Greenwalt9258c642014-03-26 16:47:06 -070030import android.os.Handler;
31import android.os.HandlerThread;
Dianne Hackborn77b987f2014-02-26 16:20:52 -080032import android.os.IBinder;
33import android.os.INetworkActivityListener;
34import android.os.INetworkManagementService;
Robert Greenwalt9258c642014-03-26 16:47:06 -070035import android.os.Looper;
36import android.os.Message;
Robert Greenwalt665e1ae2012-08-21 19:27:00 -070037import android.os.Messenger;
Lorenzo Colittiffc42b02015-07-29 11:41:21 +090038import android.os.Process;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080039import android.os.RemoteException;
Dianne Hackborn77b987f2014-02-26 16:20:52 -080040import android.os.ServiceManager;
Jeff Sharkey961e3042011-08-29 16:02:57 -070041import android.provider.Settings;
Wink Saville36ffb042014-12-05 11:10:30 -080042import android.telephony.SubscriptionManager;
Dianne Hackborn77b987f2014-02-26 16:20:52 -080043import android.util.ArrayMap;
Robert Greenwalt9258c642014-03-26 16:47:06 -070044import android.util.Log;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080045
Robert Greenwaltafa05c02014-05-21 20:04:36 -070046import com.android.internal.telephony.ITelephony;
Robert Greenwalt562cc542014-05-15 18:07:26 -070047import com.android.internal.telephony.PhoneConstants;
Robert Greenwaltafa05c02014-05-21 20:04:36 -070048import com.android.internal.util.Protocol;
49
Paul Jensenc91b5342014-08-27 12:38:45 -040050import libcore.net.event.NetworkEventDispatcher;
51
Jeremy Kleind42209d2015-12-28 15:11:58 -080052import java.net.InetAddress;
53import java.util.HashMap;
54import java.util.concurrent.atomic.AtomicInteger;
55
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080056/**
57 * Class that answers queries about the state of network connectivity. It also
58 * notifies applications when network connectivity changes. Get an instance
59 * of this class by calling
60 * {@link android.content.Context#getSystemService(String) Context.getSystemService(Context.CONNECTIVITY_SERVICE)}.
61 * <p>
62 * The primary responsibilities of this class are to:
63 * <ol>
64 * <li>Monitor network connections (Wi-Fi, GPRS, UMTS, etc.)</li>
65 * <li>Send broadcast intents when network connectivity changes</li>
66 * <li>Attempt to "fail over" to another network when connectivity to a network
67 * is lost</li>
68 * <li>Provide an API that allows applications to query the coarse-grained or fine-grained
69 * state of the available networks</li>
Robert Greenwaltd19c41c2014-05-18 23:07:25 -070070 * <li>Provide an API that allows applications to request and select networks for their data
71 * traffic</li>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080072 * </ol>
73 */
Jeff Sharkeyc006f1a2011-05-19 17:12:49 -070074public class ConnectivityManager {
75 private static final String TAG = "ConnectivityManager";
76
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080077 /**
Robert Greenwaltd19c41c2014-05-18 23:07:25 -070078 * A change in network connectivity has occurred. A default connection has either
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080079 * been established or lost. The NetworkInfo for the affected network is
80 * sent as an extra; it should be consulted to see what kind of
81 * connectivity event occurred.
82 * <p/>
83 * If this is a connection that was the result of failing over from a
84 * disconnected network, then the FAILOVER_CONNECTION boolean extra is
85 * set to true.
86 * <p/>
87 * For a loss of connectivity, if the connectivity manager is attempting
88 * to connect (or has already connected) to another network, the
89 * NetworkInfo for the new network is also passed as an extra. This lets
90 * any receivers of the broadcast know that they should not necessarily
91 * tell the user that no data traffic will be possible. Instead, the
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -080092 * receiver should expect another broadcast soon, indicating either that
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080093 * the failover attempt succeeded (and so there is still overall data
94 * connectivity), or that the failover attempt failed, meaning that all
95 * connectivity has been lost.
96 * <p/>
97 * For a disconnect event, the boolean extra EXTRA_NO_CONNECTIVITY
98 * is set to {@code true} if there are no connected networks at all.
99 */
Jeff Sharkey4fa63b22013-02-20 18:21:19 -0800100 @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800101 public static final String CONNECTIVITY_ACTION = "android.net.conn.CONNECTIVITY_CHANGE";
Jeff Sharkeyf0ceede2011-08-02 17:22:34 -0700102
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800103 /**
Robert Greenwalte94a6ff2015-09-01 08:23:04 -0700104 * A temporary hack until SUPL system can get off the legacy APIS.
105 * They do too many network requests and the long list of apps listening
106 * and waking due to the CONNECTIVITY_ACTION bcast makes it expensive.
107 * Use this bcast intent instead for SUPL requests.
108 * @hide
109 */
110 public static final String CONNECTIVITY_ACTION_SUPL =
111 "android.net.conn.CONNECTIVITY_CHANGE_SUPL";
112
113 /**
Paul Jensen25a217c2015-02-27 22:55:47 -0500114 * The device has connected to a network that has presented a captive
115 * portal, which is blocking Internet connectivity. The user was presented
116 * with a notification that network sign in is required,
117 * and the user invoked the notification's action indicating they
Paul Jensen49e3edf2015-05-22 10:50:39 -0400118 * desire to sign in to the network. Apps handling this activity should
Paul Jensen25a217c2015-02-27 22:55:47 -0500119 * facilitate signing in to the network. This action includes a
120 * {@link Network} typed extra called {@link #EXTRA_NETWORK} that represents
121 * the network presenting the captive portal; all communication with the
122 * captive portal must be done using this {@code Network} object.
123 * <p/>
Paul Jensen49e3edf2015-05-22 10:50:39 -0400124 * This activity includes a {@link CaptivePortal} extra named
125 * {@link #EXTRA_CAPTIVE_PORTAL} that can be used to indicate different
126 * outcomes of the captive portal sign in to the system:
127 * <ul>
128 * <li> When the app handling this action believes the user has signed in to
129 * the network and the captive portal has been dismissed, the app should
130 * call {@link CaptivePortal#reportCaptivePortalDismissed} so the system can
131 * reevaluate the network. If reevaluation finds the network no longer
132 * subject to a captive portal, the network may become the default active
133 * data network. </li>
134 * <li> When the app handling this action believes the user explicitly wants
Paul Jensen25a217c2015-02-27 22:55:47 -0500135 * to ignore the captive portal and the network, the app should call
Paul Jensen49e3edf2015-05-22 10:50:39 -0400136 * {@link CaptivePortal#ignoreNetwork}. </li>
137 * </ul>
Paul Jensen25a217c2015-02-27 22:55:47 -0500138 */
139 @SdkConstant(SdkConstantType.ACTIVITY_INTENT_ACTION)
140 public static final String ACTION_CAPTIVE_PORTAL_SIGN_IN = "android.net.conn.CAPTIVE_PORTAL";
141
142 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800143 * The lookup key for a {@link NetworkInfo} object. Retrieve with
144 * {@link android.content.Intent#getParcelableExtra(String)}.
Jeff Sharkeyf0ceede2011-08-02 17:22:34 -0700145 *
146 * @deprecated Since {@link NetworkInfo} can vary based on UID, applications
147 * should always obtain network information through
Paul Jensen3541e9f2015-03-18 12:23:02 -0400148 * {@link #getActiveNetworkInfo()}.
Jeff Sharkey75fbb4b2012-08-06 11:41:50 -0700149 * @see #EXTRA_NETWORK_TYPE
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800150 */
Jeff Sharkeyf0ceede2011-08-02 17:22:34 -0700151 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800152 public static final String EXTRA_NETWORK_INFO = "networkInfo";
Jeff Sharkeyf0ceede2011-08-02 17:22:34 -0700153
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800154 /**
Jeff Sharkey75fbb4b2012-08-06 11:41:50 -0700155 * Network type which triggered a {@link #CONNECTIVITY_ACTION} broadcast.
Jeff Sharkey75fbb4b2012-08-06 11:41:50 -0700156 *
157 * @see android.content.Intent#getIntExtra(String, int)
158 */
159 public static final String EXTRA_NETWORK_TYPE = "networkType";
160
161 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800162 * The lookup key for a boolean that indicates whether a connect event
163 * is for a network to which the connectivity manager was failing over
164 * following a disconnect on another network.
165 * Retrieve it with {@link android.content.Intent#getBooleanExtra(String,boolean)}.
166 */
167 public static final String EXTRA_IS_FAILOVER = "isFailover";
168 /**
169 * The lookup key for a {@link NetworkInfo} object. This is supplied when
170 * there is another network that it may be possible to connect to. Retrieve with
171 * {@link android.content.Intent#getParcelableExtra(String)}.
172 */
173 public static final String EXTRA_OTHER_NETWORK_INFO = "otherNetwork";
174 /**
175 * The lookup key for a boolean that indicates whether there is a
176 * complete lack of connectivity, i.e., no network is available.
177 * Retrieve it with {@link android.content.Intent#getBooleanExtra(String,boolean)}.
178 */
179 public static final String EXTRA_NO_CONNECTIVITY = "noConnectivity";
180 /**
181 * The lookup key for a string that indicates why an attempt to connect
182 * to a network failed. The string has no particular structure. It is
183 * intended to be used in notifications presented to users. Retrieve
184 * it with {@link android.content.Intent#getStringExtra(String)}.
185 */
186 public static final String EXTRA_REASON = "reason";
187 /**
188 * The lookup key for a string that provides optionally supplied
189 * extra information about the network state. The information
190 * may be passed up from the lower networking layers, and its
191 * meaning may be specific to a particular network type. Retrieve
192 * it with {@link android.content.Intent#getStringExtra(String)}.
193 */
194 public static final String EXTRA_EXTRA_INFO = "extraInfo";
Robert Greenwaltd7085fc2010-09-08 15:24:47 -0700195 /**
196 * The lookup key for an int that provides information about
197 * our connection to the internet at large. 0 indicates no connection,
198 * 100 indicates a great connection. Retrieve it with
Jeff Sharkeyc006f1a2011-05-19 17:12:49 -0700199 * {@link android.content.Intent#getIntExtra(String, int)}.
Robert Greenwaltd7085fc2010-09-08 15:24:47 -0700200 * {@hide}
201 */
202 public static final String EXTRA_INET_CONDITION = "inetCondition";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800203 /**
Paul Jensen49e3edf2015-05-22 10:50:39 -0400204 * The lookup key for a {@link CaptivePortal} object included with the
205 * {@link #ACTION_CAPTIVE_PORTAL_SIGN_IN} intent. The {@code CaptivePortal}
206 * object can be used to either indicate to the system that the captive
207 * portal has been dismissed or that the user does not want to pursue
208 * signing in to captive portal. Retrieve it with
209 * {@link android.content.Intent#getParcelableExtra(String)}.
Paul Jensen25a217c2015-02-27 22:55:47 -0500210 */
Paul Jensen49e3edf2015-05-22 10:50:39 -0400211 public static final String EXTRA_CAPTIVE_PORTAL = "android.net.extra.CAPTIVE_PORTAL";
Jan Nordqvist52eb29f2015-09-22 15:54:32 -0700212
213 /**
214 * Key for passing a URL to the captive portal login activity.
215 */
216 public static final String EXTRA_CAPTIVE_PORTAL_URL = "android.net.extra.CAPTIVE_PORTAL_URL";
217
Paul Jensen25a217c2015-02-27 22:55:47 -0500218 /**
Haoyu Baidb3c8672012-06-20 14:29:57 -0700219 * Broadcast action to indicate the change of data activity status
220 * (idle or active) on a network in a recent period.
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800221 * The network becomes active when data transmission is started, or
222 * idle if there is no data transmission for a period of time.
Haoyu Baidb3c8672012-06-20 14:29:57 -0700223 * {@hide}
224 */
225 @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION)
226 public static final String ACTION_DATA_ACTIVITY_CHANGE = "android.net.conn.DATA_ACTIVITY_CHANGE";
227 /**
228 * The lookup key for an enum that indicates the network device type on which this data activity
229 * change happens.
230 * {@hide}
231 */
232 public static final String EXTRA_DEVICE_TYPE = "deviceType";
233 /**
234 * The lookup key for a boolean that indicates the device is active or not. {@code true} means
235 * it is actively sending or receiving data and {@code false} means it is idle.
236 * {@hide}
237 */
238 public static final String EXTRA_IS_ACTIVE = "isActive";
Ashish Sharma0535a9f2014-03-12 18:42:23 -0700239 /**
240 * The lookup key for a long that contains the timestamp (nanos) of the radio state change.
241 * {@hide}
242 */
243 public static final String EXTRA_REALTIME_NS = "tsNanos";
Haoyu Baidb3c8672012-06-20 14:29:57 -0700244
245 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800246 * Broadcast Action: The setting for background data usage has changed
247 * values. Use {@link #getBackgroundDataSetting()} to get the current value.
248 * <p>
249 * If an application uses the network in the background, it should listen
250 * for this broadcast and stop using the background data if the value is
Jeff Sharkeyc006f1a2011-05-19 17:12:49 -0700251 * {@code false}.
Jeff Sharkey54ee2ad2012-01-30 16:29:24 -0800252 * <p>
253 *
254 * @deprecated As of {@link VERSION_CODES#ICE_CREAM_SANDWICH}, availability
255 * of background data depends on several combined factors, and
256 * this broadcast is no longer sent. Instead, when background
257 * data is unavailable, {@link #getActiveNetworkInfo()} will now
258 * appear disconnected. During first boot after a platform
259 * upgrade, this broadcast will be sent once if
260 * {@link #getBackgroundDataSetting()} was {@code false} before
261 * the upgrade.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800262 */
263 @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION)
Jeff Sharkey54ee2ad2012-01-30 16:29:24 -0800264 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800265 public static final String ACTION_BACKGROUND_DATA_SETTING_CHANGED =
266 "android.net.conn.BACKGROUND_DATA_SETTING_CHANGED";
267
Robert Greenwalt1e9aac22010-09-15 17:36:33 -0700268 /**
269 * Broadcast Action: The network connection may not be good
270 * uses {@code ConnectivityManager.EXTRA_INET_CONDITION} and
271 * {@code ConnectivityManager.EXTRA_NETWORK_INFO} to specify
272 * the network and it's condition.
273 * @hide
274 */
Jeff Sharkey4fa63b22013-02-20 18:21:19 -0800275 @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION)
Robert Greenwalt1e9aac22010-09-15 17:36:33 -0700276 public static final String INET_CONDITION_ACTION =
277 "android.net.conn.INET_CONDITION_ACTION";
278
Robert Greenwalt42acef32009-08-12 16:08:25 -0700279 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800280 * Broadcast Action: A tetherable connection has come or gone.
281 * Uses {@code ConnectivityManager.EXTRA_AVAILABLE_TETHER},
282 * {@code ConnectivityManager.EXTRA_ACTIVE_TETHER} and
283 * {@code ConnectivityManager.EXTRA_ERRORED_TETHER} to indicate
284 * the current state of tethering. Each include a list of
285 * interface names in that state (may be empty).
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -0800286 * @hide
287 */
Jeff Sharkey4fa63b22013-02-20 18:21:19 -0800288 @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION)
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -0800289 public static final String ACTION_TETHER_STATE_CHANGED =
290 "android.net.conn.TETHER_STATE_CHANGED";
291
292 /**
293 * @hide
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800294 * gives a String[] listing all the interfaces configured for
295 * tethering and currently available for tethering.
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -0800296 */
Robert Greenwalt2a091d72010-02-11 18:18:40 -0800297 public static final String EXTRA_AVAILABLE_TETHER = "availableArray";
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -0800298
299 /**
300 * @hide
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800301 * gives a String[] listing all the interfaces currently tethered
302 * (ie, has dhcp support and packets potentially forwarded/NATed)
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -0800303 */
Robert Greenwalt2a091d72010-02-11 18:18:40 -0800304 public static final String EXTRA_ACTIVE_TETHER = "activeArray";
305
306 /**
307 * @hide
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800308 * gives a String[] listing all the interfaces we tried to tether and
309 * failed. Use {@link #getLastTetherError} to find the error code
310 * for any interfaces listed here.
Robert Greenwalt2a091d72010-02-11 18:18:40 -0800311 */
312 public static final String EXTRA_ERRORED_TETHER = "erroredArray";
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -0800313
314 /**
Russell Brenner108da0c2013-02-12 10:03:14 -0800315 * Broadcast Action: The captive portal tracker has finished its test.
316 * Sent only while running Setup Wizard, in lieu of showing a user
317 * notification.
318 * @hide
319 */
Jeff Sharkey4fa63b22013-02-20 18:21:19 -0800320 @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION)
Russell Brenner108da0c2013-02-12 10:03:14 -0800321 public static final String ACTION_CAPTIVE_PORTAL_TEST_COMPLETED =
322 "android.net.conn.CAPTIVE_PORTAL_TEST_COMPLETED";
323 /**
324 * The lookup key for a boolean that indicates whether a captive portal was detected.
325 * Retrieve it with {@link android.content.Intent#getBooleanExtra(String,boolean)}.
326 * @hide
327 */
328 public static final String EXTRA_IS_CAPTIVE_PORTAL = "captivePortal";
329
330 /**
Lorenzo Colittie03c3c72015-04-03 16:38:52 +0900331 * Action used to display a dialog that asks the user whether to connect to a network that is
332 * not validated. This intent is used to start the dialog in settings via startActivity.
333 *
334 * @hide
335 */
336 public static final String ACTION_PROMPT_UNVALIDATED = "android.net.conn.PROMPT_UNVALIDATED";
337
338 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800339 * The absence of a connection type.
Robert Greenwaltccf83af12011-06-02 17:30:47 -0700340 * @hide
341 */
342 public static final int TYPE_NONE = -1;
343
344 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800345 * The Mobile data connection. When active, all data traffic
346 * will use this network type's interface by default
347 * (it has a default route)
Robert Greenwalt42acef32009-08-12 16:08:25 -0700348 */
349 public static final int TYPE_MOBILE = 0;
350 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800351 * The WIFI data connection. When active, all data traffic
352 * will use this network type's interface by default
353 * (it has a default route).
Robert Greenwalt42acef32009-08-12 16:08:25 -0700354 */
355 public static final int TYPE_WIFI = 1;
356 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800357 * An MMS-specific Mobile data connection. This network type may use the
358 * same network interface as {@link #TYPE_MOBILE} or it may use a different
359 * one. This is used by applications needing to talk to the carrier's
360 * Multimedia Messaging Service servers.
Lorenzo Colittie285b432015-04-23 15:32:42 +0900361 *
Lorenzo Colitti2ea89e52015-04-24 17:03:31 +0900362 * @deprecated Applications should instead use
363 * {@link #requestNetwork(NetworkRequest, NetworkCallback)} to request a network that
Lorenzo Colittie285b432015-04-23 15:32:42 +0900364 * provides the {@link NetworkCapabilities#NET_CAPABILITY_MMS} capability.
Robert Greenwalt42acef32009-08-12 16:08:25 -0700365 */
366 public static final int TYPE_MOBILE_MMS = 2;
367 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800368 * A SUPL-specific Mobile data connection. This network type may use the
369 * same network interface as {@link #TYPE_MOBILE} or it may use a different
370 * one. This is used by applications needing to talk to the carrier's
371 * Secure User Plane Location servers for help locating the device.
Lorenzo Colittie285b432015-04-23 15:32:42 +0900372 *
Lorenzo Colitti2ea89e52015-04-24 17:03:31 +0900373 * @deprecated Applications should instead use
374 * {@link #requestNetwork(NetworkRequest, NetworkCallback)} to request a network that
Lorenzo Colittie285b432015-04-23 15:32:42 +0900375 * provides the {@link NetworkCapabilities#NET_CAPABILITY_SUPL} capability.
Robert Greenwalt42acef32009-08-12 16:08:25 -0700376 */
377 public static final int TYPE_MOBILE_SUPL = 3;
378 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800379 * A DUN-specific Mobile data connection. This network type may use the
380 * same network interface as {@link #TYPE_MOBILE} or it may use a different
381 * one. This is sometimes by the system when setting up an upstream connection
382 * for tethering so that the carrier is aware of DUN traffic.
Robert Greenwalt42acef32009-08-12 16:08:25 -0700383 */
384 public static final int TYPE_MOBILE_DUN = 4;
385 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800386 * A High Priority Mobile data connection. This network type uses the
387 * same network interface as {@link #TYPE_MOBILE} but the routing setup
Lorenzo Colittie285b432015-04-23 15:32:42 +0900388 * is different.
389 *
Lorenzo Colitti2ea89e52015-04-24 17:03:31 +0900390 * @deprecated Applications should instead use
391 * {@link #requestNetwork(NetworkRequest, NetworkCallback)} to request a network that
Lorenzo Colittie285b432015-04-23 15:32:42 +0900392 * uses the {@link NetworkCapabilities#TRANSPORT_CELLULAR} transport.
Robert Greenwalt42acef32009-08-12 16:08:25 -0700393 */
394 public static final int TYPE_MOBILE_HIPRI = 5;
jsh8214deb2010-03-11 15:04:43 -0800395 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800396 * The WiMAX data connection. When active, all data traffic
397 * will use this network type's interface by default
398 * (it has a default route).
jsh8214deb2010-03-11 15:04:43 -0800399 */
400 public static final int TYPE_WIMAX = 6;
Robert Greenwaltda3d5e62010-12-06 13:56:24 -0800401
Jaikumar Ganesh15c74392010-12-21 22:31:44 -0800402 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800403 * The Bluetooth data connection. When active, all data traffic
404 * will use this network type's interface by default
405 * (it has a default route).
Jaikumar Ganesh15c74392010-12-21 22:31:44 -0800406 */
407 public static final int TYPE_BLUETOOTH = 7;
408
Robert Greenwalt60810842011-04-22 15:28:18 -0700409 /**
410 * Dummy data connection. This should not be used on shipping devices.
411 */
Jaikumar Ganesh15c74392010-12-21 22:31:44 -0800412 public static final int TYPE_DUMMY = 8;
Wink Saville9d7d6282011-03-12 14:52:01 -0800413
Robert Greenwalt60810842011-04-22 15:28:18 -0700414 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800415 * The Ethernet data connection. When active, all data traffic
416 * will use this network type's interface by default
417 * (it has a default route).
Robert Greenwalt60810842011-04-22 15:28:18 -0700418 */
Robert Greenwalte12aec92011-01-28 14:48:37 -0800419 public static final int TYPE_ETHERNET = 9;
Robert Greenwalt60810842011-04-22 15:28:18 -0700420
Wink Saville9d7d6282011-03-12 14:52:01 -0800421 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800422 * Over the air Administration.
Wink Saville9d7d6282011-03-12 14:52:01 -0800423 * {@hide}
424 */
425 public static final int TYPE_MOBILE_FOTA = 10;
426
427 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800428 * IP Multimedia Subsystem.
Wink Saville9d7d6282011-03-12 14:52:01 -0800429 * {@hide}
430 */
431 public static final int TYPE_MOBILE_IMS = 11;
432
433 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800434 * Carrier Branded Services.
Wink Saville9d7d6282011-03-12 14:52:01 -0800435 * {@hide}
436 */
437 public static final int TYPE_MOBILE_CBS = 12;
438
repo syncaea743a2011-07-29 23:55:49 -0700439 /**
440 * A Wi-Fi p2p connection. Only requesting processes will have access to
441 * the peers connected.
442 * {@hide}
443 */
444 public static final int TYPE_WIFI_P2P = 13;
Wink Saville9d7d6282011-03-12 14:52:01 -0800445
Wink Saville5e56bc52013-07-29 15:00:57 -0700446 /**
447 * The network to use for initially attaching to the network
448 * {@hide}
449 */
450 public static final int TYPE_MOBILE_IA = 14;
repo syncaea743a2011-07-29 23:55:49 -0700451
Lorenzo Colittie285b432015-04-23 15:32:42 +0900452 /**
Robert Greenwalt4bd43892015-07-09 14:49:35 -0700453 * Emergency PDN connection for emergency services. This
454 * may include IMS and MMS in emergency situations.
Ram3e0e3bc2014-06-26 11:03:44 -0700455 * {@hide}
456 */
457 public static final int TYPE_MOBILE_EMERGENCY = 15;
458
Hui Lu1c5624a2014-01-15 11:05:36 -0500459 /**
460 * The network that uses proxy to achieve connectivity.
461 * {@hide}
462 */
463 public static final int TYPE_PROXY = 16;
Wink Saville5e56bc52013-07-29 15:00:57 -0700464
Robert Greenwalt8283f882014-07-07 17:09:01 -0700465 /**
466 * A virtual network using one or more native bearers.
467 * It may or may not be providing security services.
468 */
469 public static final int TYPE_VPN = 17;
Hui Lu1c5624a2014-01-15 11:05:36 -0500470
471 /** {@hide} */
Robert Greenwalt8283f882014-07-07 17:09:01 -0700472 public static final int MAX_RADIO_TYPE = TYPE_VPN;
473
474 /** {@hide} */
475 public static final int MAX_NETWORK_TYPE = TYPE_VPN;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800476
Jianzheng Zhoudcf03f32012-11-16 13:45:20 +0800477 /**
478 * If you want to set the default network preference,you can directly
479 * change the networkAttributes array in framework's config.xml.
480 *
481 * @deprecated Since we support so many more networks now, the single
482 * network default network preference can't really express
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800483 * the hierarchy. Instead, the default is defined by the
Jianzheng Zhoudcf03f32012-11-16 13:45:20 +0800484 * networkAttributes in config.xml. You can determine
Robert Greenwalt4c8b7482012-12-07 09:56:50 -0800485 * the current value by calling {@link #getNetworkPreference()}
Jianzheng Zhoudcf03f32012-11-16 13:45:20 +0800486 * from an App.
487 */
488 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800489 public static final int DEFAULT_NETWORK_PREFERENCE = TYPE_WIFI;
490
Jeff Sharkey625239a2012-09-26 22:03:49 -0700491 /**
Robert Greenwalt9ba9c582014-03-19 17:56:12 -0700492 * @hide
493 */
Robert Greenwalt7569f182014-06-08 16:42:59 -0700494 public final static int REQUEST_ID_UNSET = 0;
495
Paul Jensen5d59e782014-07-11 12:28:19 -0400496 /**
497 * A NetID indicating no Network is selected.
498 * Keep in sync with bionic/libc/dns/include/resolv_netid.h
499 * @hide
500 */
501 public static final int NETID_UNSET = 0;
502
Jeff Sharkeyc006f1a2011-05-19 17:12:49 -0700503 private final IConnectivityManager mService;
Paul Jensene0bef712014-12-10 15:12:18 -0500504 /**
505 * A kludge to facilitate static access where a Context pointer isn't available, like in the
506 * case of the static set/getProcessDefaultNetwork methods and from the Network class.
507 * TODO: Remove this after deprecating the static methods in favor of non-static methods or
508 * methods that take a Context argument.
509 */
510 private static ConnectivityManager sInstance;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800511
Lorenzo Colittiffc42b02015-07-29 11:41:21 +0900512 private final Context mContext;
513
Dianne Hackborn77b987f2014-02-26 16:20:52 -0800514 private INetworkManagementService mNMService;
515
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800516 /**
517 * Tests if a given integer represents a valid network type.
518 * @param networkType the type to be tested
519 * @return a boolean. {@code true} if the type is valid, else {@code false}
Paul Jensen9e59e122015-05-06 10:42:25 -0400520 * @deprecated All APIs accepting a network type are deprecated. There should be no need to
521 * validate a network type.
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800522 */
Jeff Sharkeyd2a45872011-05-28 20:56:34 -0700523 public static boolean isNetworkTypeValid(int networkType) {
Robert Greenwalt42acef32009-08-12 16:08:25 -0700524 return networkType >= 0 && networkType <= MAX_NETWORK_TYPE;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800525 }
526
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800527 /**
528 * Returns a non-localized string representing a given network type.
529 * ONLY used for debugging output.
530 * @param type the type needing naming
531 * @return a String for the given type, or a string version of the type ("87")
532 * if no name is known.
533 * {@hide}
534 */
Jeff Sharkeyd2a45872011-05-28 20:56:34 -0700535 public static String getNetworkTypeName(int type) {
536 switch (type) {
537 case TYPE_MOBILE:
538 return "MOBILE";
539 case TYPE_WIFI:
540 return "WIFI";
541 case TYPE_MOBILE_MMS:
542 return "MOBILE_MMS";
543 case TYPE_MOBILE_SUPL:
544 return "MOBILE_SUPL";
545 case TYPE_MOBILE_DUN:
546 return "MOBILE_DUN";
547 case TYPE_MOBILE_HIPRI:
548 return "MOBILE_HIPRI";
549 case TYPE_WIMAX:
550 return "WIMAX";
551 case TYPE_BLUETOOTH:
552 return "BLUETOOTH";
553 case TYPE_DUMMY:
554 return "DUMMY";
555 case TYPE_ETHERNET:
556 return "ETHERNET";
557 case TYPE_MOBILE_FOTA:
558 return "MOBILE_FOTA";
559 case TYPE_MOBILE_IMS:
560 return "MOBILE_IMS";
561 case TYPE_MOBILE_CBS:
562 return "MOBILE_CBS";
repo syncaea743a2011-07-29 23:55:49 -0700563 case TYPE_WIFI_P2P:
564 return "WIFI_P2P";
Wink Saville5e56bc52013-07-29 15:00:57 -0700565 case TYPE_MOBILE_IA:
566 return "MOBILE_IA";
Ram3e0e3bc2014-06-26 11:03:44 -0700567 case TYPE_MOBILE_EMERGENCY:
568 return "MOBILE_EMERGENCY";
Hui Lu1c5624a2014-01-15 11:05:36 -0500569 case TYPE_PROXY:
570 return "PROXY";
Erik Kline37fbfa12014-11-19 17:23:41 +0900571 case TYPE_VPN:
572 return "VPN";
Jeff Sharkeyd2a45872011-05-28 20:56:34 -0700573 default:
574 return Integer.toString(type);
575 }
576 }
577
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800578 /**
579 * Checks if a given type uses the cellular data connection.
580 * This should be replaced in the future by a network property.
581 * @param networkType the type to check
582 * @return a boolean - {@code true} if uses cellular network, else {@code false}
583 * {@hide}
584 */
Jeff Sharkeyd2a45872011-05-28 20:56:34 -0700585 public static boolean isNetworkTypeMobile(int networkType) {
586 switch (networkType) {
587 case TYPE_MOBILE:
588 case TYPE_MOBILE_MMS:
589 case TYPE_MOBILE_SUPL:
590 case TYPE_MOBILE_DUN:
591 case TYPE_MOBILE_HIPRI:
592 case TYPE_MOBILE_FOTA:
593 case TYPE_MOBILE_IMS:
594 case TYPE_MOBILE_CBS:
Wink Saville5e56bc52013-07-29 15:00:57 -0700595 case TYPE_MOBILE_IA:
Ram3e0e3bc2014-06-26 11:03:44 -0700596 case TYPE_MOBILE_EMERGENCY:
Jeff Sharkeyd2a45872011-05-28 20:56:34 -0700597 return true;
598 default:
599 return false;
600 }
601 }
602
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800603 /**
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -0700604 * Checks if the given network type is backed by a Wi-Fi radio.
605 *
606 * @hide
607 */
608 public static boolean isNetworkTypeWifi(int networkType) {
609 switch (networkType) {
610 case TYPE_WIFI:
611 case TYPE_WIFI_P2P:
612 return true;
613 default:
614 return false;
615 }
616 }
617
618 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800619 * Specifies the preferred network type. When the device has more
620 * than one type available the preferred network type will be used.
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800621 *
622 * @param preference the network type to prefer over all others. It is
623 * unspecified what happens to the old preferred network in the
624 * overall ordering.
Robert Greenwaltd19c41c2014-05-18 23:07:25 -0700625 * @deprecated Functionality has been removed as it no longer makes sense,
626 * with many more than two networks - we'd need an array to express
627 * preference. Instead we use dynamic network properties of
628 * the networks to describe their precedence.
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800629 */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800630 public void setNetworkPreference(int preference) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800631 }
632
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800633 /**
634 * Retrieves the current preferred network type.
Paul Jensenb2748922015-05-06 11:10:18 -0400635 * <p>This method requires the caller to hold the permission
636 * {@link android.Manifest.permission#ACCESS_NETWORK_STATE}.
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800637 *
638 * @return an integer representing the preferred network type
639 *
Robert Greenwaltd19c41c2014-05-18 23:07:25 -0700640 * @deprecated Functionality has been removed as it no longer makes sense,
641 * with many more than two networks - we'd need an array to express
642 * preference. Instead we use dynamic network properties of
643 * the networks to describe their precedence.
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800644 */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800645 public int getNetworkPreference() {
Robert Greenwaltd19c41c2014-05-18 23:07:25 -0700646 return TYPE_NONE;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800647 }
648
Scott Main671644c2011-10-06 19:02:28 -0700649 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800650 * Returns details about the currently active default data network. When
651 * connected, this network is the default route for outgoing connections.
652 * You should always check {@link NetworkInfo#isConnected()} before initiating
653 * network traffic. This may return {@code null} when there is no default
654 * network.
Paul Jensenb2748922015-05-06 11:10:18 -0400655 * <p>This method requires the caller to hold the permission
656 * {@link android.Manifest.permission#ACCESS_NETWORK_STATE}.
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800657 *
658 * @return a {@link NetworkInfo} object for the current default network
Paul Jensen0d719ca2015-02-13 14:18:39 -0500659 * or {@code null} if no default network is currently active
Jeff Sharkey9f7cbf02012-04-12 18:34:54 -0700660 */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800661 public NetworkInfo getActiveNetworkInfo() {
662 try {
663 return mService.getActiveNetworkInfo();
664 } catch (RemoteException e) {
665 return null;
666 }
667 }
668
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800669 /**
Paul Jensen31a94f42015-02-13 14:18:39 -0500670 * Returns a {@link Network} object corresponding to the currently active
671 * default data network. In the event that the current active default data
672 * network disconnects, the returned {@code Network} object will no longer
673 * be usable. This will return {@code null} when there is no default
674 * network.
Paul Jensenb2748922015-05-06 11:10:18 -0400675 * <p>This method requires the caller to hold the permission
676 * {@link android.Manifest.permission#ACCESS_NETWORK_STATE}.
Paul Jensen31a94f42015-02-13 14:18:39 -0500677 *
678 * @return a {@link Network} object for the current default network or
679 * {@code null} if no default network is currently active
Paul Jensen31a94f42015-02-13 14:18:39 -0500680 */
681 public Network getActiveNetwork() {
682 try {
683 return mService.getActiveNetwork();
684 } catch (RemoteException e) {
685 return null;
686 }
687 }
688
689 /**
Robin Lee244ce8e2016-01-05 18:03:46 +0000690 * Configures an always-on VPN connection through a specific application.
691 * This connection is automatically granted and persisted after a reboot.
692 *
693 * <p>The designated package should declare a {@link VpnService} in its
694 * manifest guarded by {@link android.Manifest.permission.BIND_VPN_SERVICE},
695 * otherwise the call will fail.
696 *
697 * @param userId The identifier of the user to set an always-on VPN for.
698 * @param vpnPackage The package name for an installed VPN app on the device, or {@code null}
699 * to remove an existing always-on VPN configuration.
700
701 * @return {@code true} if the package is set as always-on VPN controller;
702 * {@code false} otherwise.
703 * @hide
704 */
705 public boolean setAlwaysOnVpnPackageForUser(int userId, @Nullable String vpnPackage) {
706 try {
707 return mService.setAlwaysOnVpnPackage(userId, vpnPackage);
708 } catch (RemoteException e) {
709 return false;
710 }
711 }
712
713 /**
714 * Returns the package name of the currently set always-on VPN application.
715 * If there is no always-on VPN set, or the VPN is provided by the system instead
716 * of by an app, {@code null} will be returned.
717 *
718 * @return Package name of VPN controller responsible for always-on VPN,
719 * or {@code null} if none is set.
720 * @hide
721 */
722 public String getAlwaysOnVpnPackageForUser(int userId) {
723 try {
724 return mService.getAlwaysOnVpnPackage(userId);
725 } catch (RemoteException e) {
726 return null;
727 }
728 }
729
730 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800731 * Returns details about the currently active default data network
732 * for a given uid. This is for internal use only to avoid spying
733 * other apps.
Paul Jensenb2748922015-05-06 11:10:18 -0400734 * <p>This method requires the caller to hold the permission
735 * {@link android.Manifest.permission#CONNECTIVITY_INTERNAL}
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800736 *
737 * @return a {@link NetworkInfo} object for the current default network
738 * for the given uid or {@code null} if no default network is
739 * available for the specified uid.
740 *
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800741 * {@hide}
742 */
Jeff Sharkeyc006f1a2011-05-19 17:12:49 -0700743 public NetworkInfo getActiveNetworkInfoForUid(int uid) {
744 try {
745 return mService.getActiveNetworkInfoForUid(uid);
746 } catch (RemoteException e) {
747 return null;
748 }
749 }
750
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800751 /**
752 * Returns connection status information about a particular
753 * network type.
Paul Jensenb2748922015-05-06 11:10:18 -0400754 * <p>This method requires the caller to hold the permission
755 * {@link android.Manifest.permission#ACCESS_NETWORK_STATE}.
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800756 *
757 * @param networkType integer specifying which networkType in
758 * which you're interested.
759 * @return a {@link NetworkInfo} object for the requested
760 * network type or {@code null} if the type is not
761 * supported by the device.
762 *
Paul Jensen3541e9f2015-03-18 12:23:02 -0400763 * @deprecated This method does not support multiple connected networks
764 * of the same type. Use {@link #getAllNetworks} and
765 * {@link #getNetworkInfo(android.net.Network)} instead.
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800766 */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800767 public NetworkInfo getNetworkInfo(int networkType) {
768 try {
769 return mService.getNetworkInfo(networkType);
770 } catch (RemoteException e) {
771 return null;
772 }
773 }
774
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800775 /**
Robert Greenwalt73b6cbae2014-06-23 11:40:00 -0700776 * Returns connection status information about a particular
777 * Network.
Paul Jensenb2748922015-05-06 11:10:18 -0400778 * <p>This method requires the caller to hold the permission
779 * {@link android.Manifest.permission#ACCESS_NETWORK_STATE}.
Robert Greenwalt73b6cbae2014-06-23 11:40:00 -0700780 *
781 * @param network {@link Network} specifying which network
782 * in which you're interested.
783 * @return a {@link NetworkInfo} object for the requested
784 * network or {@code null} if the {@code Network}
785 * is not valid.
Robert Greenwalt73b6cbae2014-06-23 11:40:00 -0700786 */
787 public NetworkInfo getNetworkInfo(Network network) {
788 try {
789 return mService.getNetworkInfoForNetwork(network);
790 } catch (RemoteException e) {
791 return null;
792 }
793 }
794
795 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800796 * Returns connection status information about all network
797 * types supported by the device.
Paul Jensenb2748922015-05-06 11:10:18 -0400798 * <p>This method requires the caller to hold the permission
799 * {@link android.Manifest.permission#ACCESS_NETWORK_STATE}.
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800800 *
801 * @return an array of {@link NetworkInfo} objects. Check each
802 * {@link NetworkInfo#getType} for which type each applies.
803 *
Paul Jensen3541e9f2015-03-18 12:23:02 -0400804 * @deprecated This method does not support multiple connected networks
805 * of the same type. Use {@link #getAllNetworks} and
806 * {@link #getNetworkInfo(android.net.Network)} instead.
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800807 */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800808 public NetworkInfo[] getAllNetworkInfo() {
809 try {
810 return mService.getAllNetworkInfo();
811 } catch (RemoteException e) {
812 return null;
813 }
814 }
815
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800816 /**
Lorenzo Colittib57edc52014-08-22 17:10:50 -0700817 * Returns the {@link Network} object currently serving a given type, or
818 * null if the given type is not connected.
819 *
820 * <p>This method requires the caller to hold the permission
821 * {@link android.Manifest.permission#ACCESS_NETWORK_STATE}.
822 *
823 * @hide
Paul Jensen3541e9f2015-03-18 12:23:02 -0400824 * @deprecated This method does not support multiple connected networks
825 * of the same type. Use {@link #getAllNetworks} and
826 * {@link #getNetworkInfo(android.net.Network)} instead.
Lorenzo Colittib57edc52014-08-22 17:10:50 -0700827 */
828 public Network getNetworkForType(int networkType) {
829 try {
830 return mService.getNetworkForType(networkType);
831 } catch (RemoteException e) {
832 return null;
833 }
834 }
835
836 /**
Robert Greenwalt73b6cbae2014-06-23 11:40:00 -0700837 * Returns an array of all {@link Network} currently tracked by the
838 * framework.
Robert Greenwalt73b6cbae2014-06-23 11:40:00 -0700839 * <p>This method requires the caller to hold the permission
840 * {@link android.Manifest.permission#ACCESS_NETWORK_STATE}.
Paul Jensenb2748922015-05-06 11:10:18 -0400841 *
842 * @return an array of {@link Network} objects.
Robert Greenwalt73b6cbae2014-06-23 11:40:00 -0700843 */
844 public Network[] getAllNetworks() {
845 try {
846 return mService.getAllNetworks();
847 } catch (RemoteException e) {
848 return null;
849 }
850 }
851
852 /**
Lorenzo Colittie285b432015-04-23 15:32:42 +0900853 * Returns an array of {@link android.net.NetworkCapabilities} objects, representing
Lorenzo Colitti403aa262014-11-28 11:21:30 +0900854 * the Networks that applications run by the given user will use by default.
855 * @hide
856 */
857 public NetworkCapabilities[] getDefaultNetworkCapabilitiesForUser(int userId) {
858 try {
859 return mService.getDefaultNetworkCapabilitiesForUser(userId);
860 } catch (RemoteException e) {
861 return null;
862 }
863 }
864
865 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800866 * Returns the IP information for the current default network.
Paul Jensenb2748922015-05-06 11:10:18 -0400867 * <p>This method requires the caller to hold the permission
868 * {@link android.Manifest.permission#ACCESS_NETWORK_STATE}.
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800869 *
870 * @return a {@link LinkProperties} object describing the IP info
871 * for the current default network, or {@code null} if there
872 * is no current default network.
873 *
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800874 * {@hide}
875 */
Robert Greenwaltd192dad2010-09-14 09:18:02 -0700876 public LinkProperties getActiveLinkProperties() {
877 try {
878 return mService.getActiveLinkProperties();
879 } catch (RemoteException e) {
880 return null;
881 }
882 }
883
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800884 /**
885 * Returns the IP information for a given network type.
Paul Jensenb2748922015-05-06 11:10:18 -0400886 * <p>This method requires the caller to hold the permission
887 * {@link android.Manifest.permission#ACCESS_NETWORK_STATE}.
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800888 *
889 * @param networkType the network type of interest.
890 * @return a {@link LinkProperties} object describing the IP info
891 * for the given networkType, or {@code null} if there is
892 * no current default network.
893 *
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800894 * {@hide}
Paul Jensen3541e9f2015-03-18 12:23:02 -0400895 * @deprecated This method does not support multiple connected networks
896 * of the same type. Use {@link #getAllNetworks},
897 * {@link #getNetworkInfo(android.net.Network)}, and
898 * {@link #getLinkProperties(android.net.Network)} instead.
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800899 */
Robert Greenwaltd192dad2010-09-14 09:18:02 -0700900 public LinkProperties getLinkProperties(int networkType) {
901 try {
Robert Greenwalt9258c642014-03-26 16:47:06 -0700902 return mService.getLinkPropertiesForType(networkType);
903 } catch (RemoteException e) {
904 return null;
905 }
906 }
907
Robert Greenwaltd19c41c2014-05-18 23:07:25 -0700908 /**
909 * Get the {@link LinkProperties} for the given {@link Network}. This
910 * will return {@code null} if the network is unknown.
Paul Jensenb2748922015-05-06 11:10:18 -0400911 * <p>This method requires the caller to hold the permission
912 * {@link android.Manifest.permission#ACCESS_NETWORK_STATE}.
Robert Greenwaltd19c41c2014-05-18 23:07:25 -0700913 *
914 * @param network The {@link Network} object identifying the network in question.
915 * @return The {@link LinkProperties} for the network, or {@code null}.
Paul Jensenb2748922015-05-06 11:10:18 -0400916 */
Robert Greenwalt9258c642014-03-26 16:47:06 -0700917 public LinkProperties getLinkProperties(Network network) {
918 try {
919 return mService.getLinkProperties(network);
920 } catch (RemoteException e) {
921 return null;
922 }
923 }
924
Robert Greenwaltd19c41c2014-05-18 23:07:25 -0700925 /**
Lorenzo Colittie285b432015-04-23 15:32:42 +0900926 * Get the {@link android.net.NetworkCapabilities} for the given {@link Network}. This
Robert Greenwaltd19c41c2014-05-18 23:07:25 -0700927 * will return {@code null} if the network is unknown.
Paul Jensenb2748922015-05-06 11:10:18 -0400928 * <p>This method requires the caller to hold the permission
929 * {@link android.Manifest.permission#ACCESS_NETWORK_STATE}.
Robert Greenwaltd19c41c2014-05-18 23:07:25 -0700930 *
931 * @param network The {@link Network} object identifying the network in question.
Lorenzo Colittie285b432015-04-23 15:32:42 +0900932 * @return The {@link android.net.NetworkCapabilities} for the network, or {@code null}.
Robert Greenwaltd19c41c2014-05-18 23:07:25 -0700933 */
Robert Greenwalt9258c642014-03-26 16:47:06 -0700934 public NetworkCapabilities getNetworkCapabilities(Network network) {
935 try {
936 return mService.getNetworkCapabilities(network);
Robert Greenwaltd192dad2010-09-14 09:18:02 -0700937 } catch (RemoteException e) {
938 return null;
939 }
940 }
941
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800942 /**
Udam Sainib7c24872016-01-04 12:16:14 -0800943 * Gets the URL that should be used for resolving whether a captive portal is present.
944 * 1. This URL should respond with a 204 response to a GET request to indicate no captive
945 * portal is present.
946 * 2. This URL must be HTTP as redirect responses are used to find captive portal
947 * sign-in pages. Captive portals cannot respond to HTTPS requests with redirects.
948 *
949 * @hide
950 */
951 @SystemApi
952 public String getCaptivePortalServerUrl() {
953 try {
954 return mService.getCaptivePortalServerUrl();
955 } catch (RemoteException e) {
956 return null;
957 }
958 }
959
960 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800961 * Tells the underlying networking system that the caller wants to
962 * begin using the named feature. The interpretation of {@code feature}
963 * is completely up to each networking implementation.
Lorenzo Colittid5427052015-10-15 16:29:00 +0900964 *
965 * <p>This method requires the caller to hold either the
966 * {@link android.Manifest.permission#CHANGE_NETWORK_STATE} permission
967 * or the ability to modify system settings as determined by
968 * {@link android.provider.Settings.System#canWrite}.</p>
969 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800970 * @param networkType specifies which network the request pertains to
971 * @param feature the name of the feature to be used
972 * @return an integer value representing the outcome of the request.
973 * The interpretation of this value is specific to each networking
974 * implementation+feature combination, except that the value {@code -1}
975 * always indicates failure.
Robert Greenwaltd19c41c2014-05-18 23:07:25 -0700976 *
Lorenzo Colitti2ea89e52015-04-24 17:03:31 +0900977 * @deprecated Deprecated in favor of the cleaner
978 * {@link #requestNetwork(NetworkRequest, NetworkCallback)} API.
Dianne Hackborn692a2442015-07-31 10:35:34 -0700979 * In {@link VERSION_CODES#M}, and above, this method is unsupported and will
Lorenzo Colittiffc42b02015-07-29 11:41:21 +0900980 * throw {@code UnsupportedOperationException} if called.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800981 */
982 public int startUsingNetworkFeature(int networkType, String feature) {
Lorenzo Colittiffc42b02015-07-29 11:41:21 +0900983 checkLegacyRoutingApiAccess();
Robert Greenwalt562cc542014-05-15 18:07:26 -0700984 NetworkCapabilities netCap = networkCapabilitiesForFeature(networkType, feature);
985 if (netCap == null) {
986 Log.d(TAG, "Can't satisfy startUsingNetworkFeature for " + networkType + ", " +
987 feature);
988 return PhoneConstants.APN_REQUEST_FAILED;
989 }
990
991 NetworkRequest request = null;
992 synchronized (sLegacyRequests) {
993 LegacyRequest l = sLegacyRequests.get(netCap);
994 if (l != null) {
995 Log.d(TAG, "renewing startUsingNetworkFeature request " + l.networkRequest);
996 renewRequestLocked(l);
997 if (l.currentNetwork != null) {
998 return PhoneConstants.APN_ALREADY_ACTIVE;
999 } else {
1000 return PhoneConstants.APN_REQUEST_STARTED;
1001 }
1002 }
1003
1004 request = requestNetworkForFeatureLocked(netCap);
1005 }
1006 if (request != null) {
Robert Greenwalt257ee5f2014-06-20 10:58:45 -07001007 Log.d(TAG, "starting startUsingNetworkFeature for request " + request);
Robert Greenwalt562cc542014-05-15 18:07:26 -07001008 return PhoneConstants.APN_REQUEST_STARTED;
1009 } else {
1010 Log.d(TAG, " request Failed");
1011 return PhoneConstants.APN_REQUEST_FAILED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001012 }
1013 }
1014
1015 /**
1016 * Tells the underlying networking system that the caller is finished
1017 * using the named feature. The interpretation of {@code feature}
1018 * is completely up to each networking implementation.
Lorenzo Colittid5427052015-10-15 16:29:00 +09001019 *
1020 * <p>This method requires the caller to hold either the
1021 * {@link android.Manifest.permission#CHANGE_NETWORK_STATE} permission
1022 * or the ability to modify system settings as determined by
1023 * {@link android.provider.Settings.System#canWrite}.</p>
1024 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001025 * @param networkType specifies which network the request pertains to
1026 * @param feature the name of the feature that is no longer needed
1027 * @return an integer value representing the outcome of the request.
1028 * The interpretation of this value is specific to each networking
1029 * implementation+feature combination, except that the value {@code -1}
1030 * always indicates failure.
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07001031 *
Robert Greenwalta36c0742015-07-28 11:41:31 -07001032 * @deprecated Deprecated in favor of the cleaner {@link #unregisterNetworkCallback} API.
Dianne Hackborn692a2442015-07-31 10:35:34 -07001033 * In {@link VERSION_CODES#M}, and above, this method is unsupported and will
Lorenzo Colittiffc42b02015-07-29 11:41:21 +09001034 * throw {@code UnsupportedOperationException} if called.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001035 */
1036 public int stopUsingNetworkFeature(int networkType, String feature) {
Lorenzo Colittiffc42b02015-07-29 11:41:21 +09001037 checkLegacyRoutingApiAccess();
Robert Greenwalt562cc542014-05-15 18:07:26 -07001038 NetworkCapabilities netCap = networkCapabilitiesForFeature(networkType, feature);
1039 if (netCap == null) {
1040 Log.d(TAG, "Can't satisfy stopUsingNetworkFeature for " + networkType + ", " +
1041 feature);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001042 return -1;
1043 }
Robert Greenwalt562cc542014-05-15 18:07:26 -07001044
Paul Jensen9ffb53c2014-12-17 10:39:34 -05001045 if (removeRequestForFeature(netCap)) {
Robert Greenwalt562cc542014-05-15 18:07:26 -07001046 Log.d(TAG, "stopUsingNetworkFeature for " + networkType + ", " + feature);
Robert Greenwalt562cc542014-05-15 18:07:26 -07001047 }
1048 return 1;
1049 }
1050
1051 private NetworkCapabilities networkCapabilitiesForFeature(int networkType, String feature) {
1052 if (networkType == TYPE_MOBILE) {
1053 int cap = -1;
1054 if ("enableMMS".equals(feature)) {
1055 cap = NetworkCapabilities.NET_CAPABILITY_MMS;
1056 } else if ("enableSUPL".equals(feature)) {
1057 cap = NetworkCapabilities.NET_CAPABILITY_SUPL;
1058 } else if ("enableDUN".equals(feature) || "enableDUNAlways".equals(feature)) {
1059 cap = NetworkCapabilities.NET_CAPABILITY_DUN;
1060 } else if ("enableHIPRI".equals(feature)) {
1061 cap = NetworkCapabilities.NET_CAPABILITY_INTERNET;
1062 } else if ("enableFOTA".equals(feature)) {
1063 cap = NetworkCapabilities.NET_CAPABILITY_FOTA;
1064 } else if ("enableIMS".equals(feature)) {
1065 cap = NetworkCapabilities.NET_CAPABILITY_IMS;
1066 } else if ("enableCBS".equals(feature)) {
1067 cap = NetworkCapabilities.NET_CAPABILITY_CBS;
1068 } else {
1069 return null;
1070 }
1071 NetworkCapabilities netCap = new NetworkCapabilities();
Robert Greenwalt7569f182014-06-08 16:42:59 -07001072 netCap.addTransportType(NetworkCapabilities.TRANSPORT_CELLULAR).addCapability(cap);
Paul Jensen487ffe72015-07-24 15:57:11 -04001073 netCap.maybeMarkCapabilitiesRestricted();
Robert Greenwalt562cc542014-05-15 18:07:26 -07001074 return netCap;
1075 } else if (networkType == TYPE_WIFI) {
1076 if ("p2p".equals(feature)) {
1077 NetworkCapabilities netCap = new NetworkCapabilities();
1078 netCap.addTransportType(NetworkCapabilities.TRANSPORT_WIFI);
Robert Greenwalt7569f182014-06-08 16:42:59 -07001079 netCap.addCapability(NetworkCapabilities.NET_CAPABILITY_WIFI_P2P);
Paul Jensen487ffe72015-07-24 15:57:11 -04001080 netCap.maybeMarkCapabilitiesRestricted();
Robert Greenwalt562cc542014-05-15 18:07:26 -07001081 return netCap;
1082 }
1083 }
1084 return null;
1085 }
1086
Robert Greenwalt06314e42014-10-29 14:04:06 -07001087 /**
1088 * Guess what the network request was trying to say so that the resulting
1089 * network is accessible via the legacy (deprecated) API such as
1090 * requestRouteToHost.
Lorenzo Colittidef4cb02015-11-25 20:28:50 +09001091 *
1092 * This means we should try to be fairly precise about transport and
Robert Greenwalt06314e42014-10-29 14:04:06 -07001093 * capability but ignore things such as networkSpecifier.
1094 * If the request has more than one transport or capability it doesn't
1095 * match the old legacy requests (they selected only single transport/capability)
1096 * so this function cannot map the request to a single legacy type and
1097 * the resulting network will not be available to the legacy APIs.
1098 *
Lorenzo Colittidef4cb02015-11-25 20:28:50 +09001099 * This code is only called from the requestNetwork API (L and above).
1100 *
1101 * Setting a legacy type causes CONNECTIVITY_ACTION broadcasts, which are expensive
1102 * because they wake up lots of apps - see http://b/23350688 . So we currently only
1103 * do this for SUPL requests, which are the only ones that we know need it. If
1104 * omitting these broadcasts causes unacceptable app breakage, then for backwards
1105 * compatibility we can send them:
1106 *
1107 * if (targetSdkVersion < Build.VERSION_CODES.M) && // legacy API unsupported >= M
1108 * targetSdkVersion >= Build.VERSION_CODES.LOLLIPOP)) // requestNetwork not present < L
1109 *
Robert Greenwalt06314e42014-10-29 14:04:06 -07001110 * TODO - This should be removed when the legacy APIs are removed.
1111 */
Ye Wenb87875e2014-07-21 14:19:01 -07001112 private int inferLegacyTypeForNetworkCapabilities(NetworkCapabilities netCap) {
1113 if (netCap == null) {
1114 return TYPE_NONE;
1115 }
Robert Greenwalt06314e42014-10-29 14:04:06 -07001116
Ye Wenb87875e2014-07-21 14:19:01 -07001117 if (!netCap.hasTransport(NetworkCapabilities.TRANSPORT_CELLULAR)) {
1118 return TYPE_NONE;
1119 }
Robert Greenwalt06314e42014-10-29 14:04:06 -07001120
Lorenzo Colittidef4cb02015-11-25 20:28:50 +09001121 // Do this only for SUPL, until GpsLocationProvider is fixed. http://b/25876485 .
1122 if (!netCap.hasCapability(NetworkCapabilities.NET_CAPABILITY_SUPL)) {
1123 // NOTE: if this causes app breakage, we should not just comment out this early return;
1124 // instead, we should make this early return conditional on the requesting app's target
1125 // SDK version, as described in the comment above.
1126 return TYPE_NONE;
1127 }
1128
Robert Greenwalt06314e42014-10-29 14:04:06 -07001129 String type = null;
1130 int result = TYPE_NONE;
1131
Ye Wenb87875e2014-07-21 14:19:01 -07001132 if (netCap.hasCapability(NetworkCapabilities.NET_CAPABILITY_CBS)) {
Robert Greenwalt06314e42014-10-29 14:04:06 -07001133 type = "enableCBS";
1134 result = TYPE_MOBILE_CBS;
1135 } else if (netCap.hasCapability(NetworkCapabilities.NET_CAPABILITY_IMS)) {
1136 type = "enableIMS";
1137 result = TYPE_MOBILE_IMS;
1138 } else if (netCap.hasCapability(NetworkCapabilities.NET_CAPABILITY_FOTA)) {
1139 type = "enableFOTA";
1140 result = TYPE_MOBILE_FOTA;
1141 } else if (netCap.hasCapability(NetworkCapabilities.NET_CAPABILITY_DUN)) {
1142 type = "enableDUN";
1143 result = TYPE_MOBILE_DUN;
1144 } else if (netCap.hasCapability(NetworkCapabilities.NET_CAPABILITY_SUPL)) {
Lorenzo Colittidef4cb02015-11-25 20:28:50 +09001145 type = "enableSUPL";
Robert Greenwalt06314e42014-10-29 14:04:06 -07001146 result = TYPE_MOBILE_SUPL;
Robert Greenwalt74ab4fa2015-08-28 12:37:54 -07001147 // back out this hack for mms as they no longer need this and it's causing
1148 // device slowdowns - b/23350688 (note, supl still needs this)
1149 //} else if (netCap.hasCapability(NetworkCapabilities.NET_CAPABILITY_MMS)) {
1150 // type = "enableMMS";
1151 // result = TYPE_MOBILE_MMS;
Robert Greenwalt06314e42014-10-29 14:04:06 -07001152 } else if (netCap.hasCapability(NetworkCapabilities.NET_CAPABILITY_INTERNET)) {
1153 type = "enableHIPRI";
1154 result = TYPE_MOBILE_HIPRI;
Ye Wenb87875e2014-07-21 14:19:01 -07001155 }
Robert Greenwalt06314e42014-10-29 14:04:06 -07001156 if (type != null) {
1157 NetworkCapabilities testCap = networkCapabilitiesForFeature(TYPE_MOBILE, type);
1158 if (testCap.equalsNetCapabilities(netCap) && testCap.equalsTransportTypes(netCap)) {
1159 return result;
Ye Wenb87875e2014-07-21 14:19:01 -07001160 }
1161 }
1162 return TYPE_NONE;
1163 }
1164
Robert Greenwalt32aa65a2014-06-02 15:32:02 -07001165 private int legacyTypeForNetworkCapabilities(NetworkCapabilities netCap) {
Robert Greenwalt562cc542014-05-15 18:07:26 -07001166 if (netCap == null) return TYPE_NONE;
1167 if (netCap.hasCapability(NetworkCapabilities.NET_CAPABILITY_CBS)) {
1168 return TYPE_MOBILE_CBS;
1169 }
1170 if (netCap.hasCapability(NetworkCapabilities.NET_CAPABILITY_IMS)) {
1171 return TYPE_MOBILE_IMS;
1172 }
1173 if (netCap.hasCapability(NetworkCapabilities.NET_CAPABILITY_FOTA)) {
1174 return TYPE_MOBILE_FOTA;
1175 }
1176 if (netCap.hasCapability(NetworkCapabilities.NET_CAPABILITY_DUN)) {
1177 return TYPE_MOBILE_DUN;
1178 }
1179 if (netCap.hasCapability(NetworkCapabilities.NET_CAPABILITY_SUPL)) {
1180 return TYPE_MOBILE_SUPL;
1181 }
1182 if (netCap.hasCapability(NetworkCapabilities.NET_CAPABILITY_MMS)) {
1183 return TYPE_MOBILE_MMS;
1184 }
1185 if (netCap.hasCapability(NetworkCapabilities.NET_CAPABILITY_INTERNET)) {
1186 return TYPE_MOBILE_HIPRI;
1187 }
Robert Greenwalt32aa65a2014-06-02 15:32:02 -07001188 if (netCap.hasCapability(NetworkCapabilities.NET_CAPABILITY_WIFI_P2P)) {
1189 return TYPE_WIFI_P2P;
1190 }
Robert Greenwalt562cc542014-05-15 18:07:26 -07001191 return TYPE_NONE;
1192 }
1193
1194 private static class LegacyRequest {
1195 NetworkCapabilities networkCapabilities;
1196 NetworkRequest networkRequest;
1197 int expireSequenceNumber;
1198 Network currentNetwork;
1199 int delay = -1;
Paul Jensen9ffb53c2014-12-17 10:39:34 -05001200
1201 private void clearDnsBinding() {
1202 if (currentNetwork != null) {
1203 currentNetwork = null;
1204 setProcessDefaultNetworkForHostResolution(null);
1205 }
1206 }
1207
Robert Greenwalt6078b502014-06-11 16:05:07 -07001208 NetworkCallback networkCallback = new NetworkCallback() {
Robert Greenwalt562cc542014-05-15 18:07:26 -07001209 @Override
Robert Greenwalt6078b502014-06-11 16:05:07 -07001210 public void onAvailable(Network network) {
Robert Greenwalt562cc542014-05-15 18:07:26 -07001211 currentNetwork = network;
1212 Log.d(TAG, "startUsingNetworkFeature got Network:" + network);
Paul Jensen6d3ff9e2014-05-29 10:12:39 -04001213 setProcessDefaultNetworkForHostResolution(network);
Robert Greenwalt562cc542014-05-15 18:07:26 -07001214 }
1215 @Override
Robert Greenwalt6078b502014-06-11 16:05:07 -07001216 public void onLost(Network network) {
Paul Jensen9ffb53c2014-12-17 10:39:34 -05001217 if (network.equals(currentNetwork)) clearDnsBinding();
Robert Greenwalt562cc542014-05-15 18:07:26 -07001218 Log.d(TAG, "startUsingNetworkFeature lost Network:" + network);
1219 }
1220 };
1221 }
1222
Robert Greenwaltfab501672014-07-23 11:44:01 -07001223 private static HashMap<NetworkCapabilities, LegacyRequest> sLegacyRequests =
Robert Greenwalt562cc542014-05-15 18:07:26 -07001224 new HashMap<NetworkCapabilities, LegacyRequest>();
1225
1226 private NetworkRequest findRequestForFeature(NetworkCapabilities netCap) {
1227 synchronized (sLegacyRequests) {
1228 LegacyRequest l = sLegacyRequests.get(netCap);
1229 if (l != null) return l.networkRequest;
1230 }
1231 return null;
1232 }
1233
1234 private void renewRequestLocked(LegacyRequest l) {
1235 l.expireSequenceNumber++;
1236 Log.d(TAG, "renewing request to seqNum " + l.expireSequenceNumber);
1237 sendExpireMsgForFeature(l.networkCapabilities, l.expireSequenceNumber, l.delay);
1238 }
1239
1240 private void expireRequest(NetworkCapabilities netCap, int sequenceNum) {
1241 int ourSeqNum = -1;
1242 synchronized (sLegacyRequests) {
1243 LegacyRequest l = sLegacyRequests.get(netCap);
1244 if (l == null) return;
1245 ourSeqNum = l.expireSequenceNumber;
Paul Jensen9ffb53c2014-12-17 10:39:34 -05001246 if (l.expireSequenceNumber == sequenceNum) removeRequestForFeature(netCap);
Robert Greenwalt562cc542014-05-15 18:07:26 -07001247 }
1248 Log.d(TAG, "expireRequest with " + ourSeqNum + ", " + sequenceNum);
1249 }
1250
1251 private NetworkRequest requestNetworkForFeatureLocked(NetworkCapabilities netCap) {
1252 int delay = -1;
Robert Greenwalt32aa65a2014-06-02 15:32:02 -07001253 int type = legacyTypeForNetworkCapabilities(netCap);
Robert Greenwalt562cc542014-05-15 18:07:26 -07001254 try {
1255 delay = mService.getRestoreDefaultNetworkDelay(type);
1256 } catch (RemoteException e) {}
1257 LegacyRequest l = new LegacyRequest();
1258 l.networkCapabilities = netCap;
1259 l.delay = delay;
1260 l.expireSequenceNumber = 0;
Robert Greenwalt6078b502014-06-11 16:05:07 -07001261 l.networkRequest = sendRequestForNetwork(netCap, l.networkCallback, 0,
Robert Greenwalt32aa65a2014-06-02 15:32:02 -07001262 REQUEST, type);
Robert Greenwalt562cc542014-05-15 18:07:26 -07001263 if (l.networkRequest == null) return null;
1264 sLegacyRequests.put(netCap, l);
1265 sendExpireMsgForFeature(netCap, l.expireSequenceNumber, delay);
1266 return l.networkRequest;
1267 }
1268
1269 private void sendExpireMsgForFeature(NetworkCapabilities netCap, int seqNum, int delay) {
1270 if (delay >= 0) {
1271 Log.d(TAG, "sending expire msg with seqNum " + seqNum + " and delay " + delay);
1272 Message msg = sCallbackHandler.obtainMessage(EXPIRE_LEGACY_REQUEST, seqNum, 0, netCap);
1273 sCallbackHandler.sendMessageDelayed(msg, delay);
1274 }
1275 }
1276
Paul Jensen9ffb53c2014-12-17 10:39:34 -05001277 private boolean removeRequestForFeature(NetworkCapabilities netCap) {
1278 final LegacyRequest l;
Robert Greenwalt562cc542014-05-15 18:07:26 -07001279 synchronized (sLegacyRequests) {
Paul Jensen9ffb53c2014-12-17 10:39:34 -05001280 l = sLegacyRequests.remove(netCap);
Robert Greenwalt562cc542014-05-15 18:07:26 -07001281 }
Paul Jensen9ffb53c2014-12-17 10:39:34 -05001282 if (l == null) return false;
1283 unregisterNetworkCallback(l.networkCallback);
1284 l.clearDnsBinding();
1285 return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001286 }
1287
Lorenzo Colitti8bf977d2015-06-15 14:29:22 +09001288 /** @hide */
1289 public static class PacketKeepaliveCallback {
1290 /** The requested keepalive was successfully started. */
1291 public void onStarted() {}
1292 /** The keepalive was successfully stopped. */
1293 public void onStopped() {}
1294 /** An error occurred. */
1295 public void onError(int error) {}
1296 }
1297
1298 /**
1299 * Allows applications to request that the system periodically send specific packets on their
1300 * behalf, using hardware offload to save battery power.
1301 *
1302 * To request that the system send keepalives, call one of the methods that return a
1303 * {@link ConnectivityManager.PacketKeepalive} object, such as {@link #startNattKeepalive},
1304 * passing in a non-null callback. If the callback is successfully started, the callback's
1305 * {@code onStarted} method will be called. If an error occurs, {@code onError} will be called,
1306 * specifying one of the {@code ERROR_*} constants in this class.
1307 *
1308 * To stop an existing keepalive, call {@link stop}. The system will call {@code onStopped} if
1309 * the operation was successfull or {@code onError} if an error occurred.
1310 *
1311 * @hide
1312 */
1313 public class PacketKeepalive {
1314
1315 private static final String TAG = "PacketKeepalive";
1316
1317 /** @hide */
1318 public static final int SUCCESS = 0;
1319
1320 /** @hide */
1321 public static final int NO_KEEPALIVE = -1;
1322
1323 /** @hide */
1324 public static final int BINDER_DIED = -10;
1325
1326 /** The specified {@code Network} is not connected. */
1327 public static final int ERROR_INVALID_NETWORK = -20;
1328 /** The specified IP addresses are invalid. For example, the specified source IP address is
1329 * not configured on the specified {@code Network}. */
1330 public static final int ERROR_INVALID_IP_ADDRESS = -21;
1331 /** The requested port is invalid. */
1332 public static final int ERROR_INVALID_PORT = -22;
1333 /** The packet length is invalid (e.g., too long). */
1334 public static final int ERROR_INVALID_LENGTH = -23;
1335 /** The packet transmission interval is invalid (e.g., too short). */
1336 public static final int ERROR_INVALID_INTERVAL = -24;
1337
1338 /** The hardware does not support this request. */
1339 public static final int ERROR_HARDWARE_UNSUPPORTED = -30;
Lorenzo Colitti9d1284e2015-09-08 16:46:36 +09001340 /** The hardware returned an error. */
1341 public static final int ERROR_HARDWARE_ERROR = -31;
Lorenzo Colitti8bf977d2015-06-15 14:29:22 +09001342
1343 public static final int NATT_PORT = 4500;
1344
1345 private final Network mNetwork;
1346 private final PacketKeepaliveCallback mCallback;
1347 private final Looper mLooper;
1348 private final Messenger mMessenger;
1349
1350 private volatile Integer mSlot;
1351
1352 void stopLooper() {
1353 mLooper.quit();
1354 }
1355
1356 public void stop() {
1357 try {
1358 mService.stopKeepalive(mNetwork, mSlot);
1359 } catch (RemoteException e) {
1360 Log.e(TAG, "Error stopping packet keepalive: ", e);
1361 stopLooper();
1362 }
1363 }
1364
1365 private PacketKeepalive(Network network, PacketKeepaliveCallback callback) {
1366 checkNotNull(network, "network cannot be null");
1367 checkNotNull(callback, "callback cannot be null");
1368 mNetwork = network;
1369 mCallback = callback;
1370 HandlerThread thread = new HandlerThread(TAG);
1371 thread.start();
1372 mLooper = thread.getLooper();
1373 mMessenger = new Messenger(new Handler(mLooper) {
1374 @Override
1375 public void handleMessage(Message message) {
1376 switch (message.what) {
1377 case NetworkAgent.EVENT_PACKET_KEEPALIVE:
1378 int error = message.arg2;
1379 try {
1380 if (error == SUCCESS) {
1381 if (mSlot == null) {
1382 mSlot = message.arg1;
1383 mCallback.onStarted();
1384 } else {
1385 mSlot = null;
1386 stopLooper();
1387 mCallback.onStopped();
1388 }
1389 } else {
1390 stopLooper();
1391 mCallback.onError(error);
1392 }
1393 } catch (Exception e) {
1394 Log.e(TAG, "Exception in keepalive callback(" + error + ")", e);
1395 }
1396 break;
1397 default:
1398 Log.e(TAG, "Unhandled message " + Integer.toHexString(message.what));
1399 break;
1400 }
1401 }
1402 });
1403 }
1404 }
1405
1406 /**
1407 * Starts an IPsec NAT-T keepalive packet with the specified parameters.
1408 *
1409 * @hide
1410 */
1411 public PacketKeepalive startNattKeepalive(
1412 Network network, int intervalSeconds, PacketKeepaliveCallback callback,
1413 InetAddress srcAddr, int srcPort, InetAddress dstAddr) {
1414 final PacketKeepalive k = new PacketKeepalive(network, callback);
1415 try {
1416 mService.startNattKeepalive(network, intervalSeconds, k.mMessenger, new Binder(),
1417 srcAddr.getHostAddress(), srcPort, dstAddr.getHostAddress());
1418 } catch (RemoteException e) {
1419 Log.e(TAG, "Error starting packet keepalive: ", e);
1420 k.stopLooper();
1421 return null;
1422 }
1423 return k;
1424 }
1425
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001426 /**
1427 * Ensure that a network route exists to deliver traffic to the specified
1428 * host via the specified network interface. An attempt to add a route that
1429 * already exists is ignored, but treated as successful.
Lorenzo Colittid5427052015-10-15 16:29:00 +09001430 *
1431 * <p>This method requires the caller to hold either the
1432 * {@link android.Manifest.permission#CHANGE_NETWORK_STATE} permission
1433 * or the ability to modify system settings as determined by
1434 * {@link android.provider.Settings.System#canWrite}.</p>
1435 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001436 * @param networkType the type of the network over which traffic to the specified
1437 * host is to be routed
1438 * @param hostAddress the IP address of the host to which the route is desired
1439 * @return {@code true} on success, {@code false} on failure
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07001440 *
Lorenzo Colitti2ea89e52015-04-24 17:03:31 +09001441 * @deprecated Deprecated in favor of the
1442 * {@link #requestNetwork(NetworkRequest, NetworkCallback)},
1443 * {@link #bindProcessToNetwork} and {@link Network#getSocketFactory} API.
Dianne Hackborn692a2442015-07-31 10:35:34 -07001444 * In {@link VERSION_CODES#M}, and above, this method is unsupported and will
Lorenzo Colittiffc42b02015-07-29 11:41:21 +09001445 * throw {@code UnsupportedOperationException} if called.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001446 */
1447 public boolean requestRouteToHost(int networkType, int hostAddress) {
Sreeram Ramachandran03666c72014-07-19 23:21:46 -07001448 return requestRouteToHostAddress(networkType, NetworkUtils.intToInetAddress(hostAddress));
Robert Greenwalt585ac0f2010-08-27 09:24:29 -07001449 }
1450
1451 /**
1452 * Ensure that a network route exists to deliver traffic to the specified
1453 * host via the specified network interface. An attempt to add a route that
1454 * already exists is ignored, but treated as successful.
Lorenzo Colittid5427052015-10-15 16:29:00 +09001455 *
1456 * <p>This method requires the caller to hold either the
1457 * {@link android.Manifest.permission#CHANGE_NETWORK_STATE} permission
1458 * or the ability to modify system settings as determined by
1459 * {@link android.provider.Settings.System#canWrite}.</p>
1460 *
Robert Greenwalt585ac0f2010-08-27 09:24:29 -07001461 * @param networkType the type of the network over which traffic to the specified
1462 * host is to be routed
1463 * @param hostAddress the IP address of the host to which the route is desired
1464 * @return {@code true} on success, {@code false} on failure
1465 * @hide
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07001466 * @deprecated Deprecated in favor of the {@link #requestNetwork} and
Lorenzo Colitti2ea89e52015-04-24 17:03:31 +09001467 * {@link #bindProcessToNetwork} API.
Robert Greenwalt585ac0f2010-08-27 09:24:29 -07001468 */
1469 public boolean requestRouteToHostAddress(int networkType, InetAddress hostAddress) {
Lorenzo Colittiffc42b02015-07-29 11:41:21 +09001470 checkLegacyRoutingApiAccess();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001471 try {
Sreeram Ramachandran03666c72014-07-19 23:21:46 -07001472 return mService.requestRouteToHostAddress(networkType, hostAddress.getAddress());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001473 } catch (RemoteException e) {
1474 return false;
1475 }
1476 }
1477
1478 /**
1479 * Returns the value of the setting for background data usage. If false,
1480 * applications should not use the network if the application is not in the
1481 * foreground. Developers should respect this setting, and check the value
1482 * of this before performing any background data operations.
1483 * <p>
1484 * All applications that have background services that use the network
1485 * should listen to {@link #ACTION_BACKGROUND_DATA_SETTING_CHANGED}.
Jeff Sharkey3a844fc2011-08-16 14:37:57 -07001486 * <p>
Scott Main4cc53332011-10-06 18:32:43 -07001487 * @deprecated As of {@link VERSION_CODES#ICE_CREAM_SANDWICH}, availability of
Jeff Sharkey3a844fc2011-08-16 14:37:57 -07001488 * background data depends on several combined factors, and this method will
1489 * always return {@code true}. Instead, when background data is unavailable,
1490 * {@link #getActiveNetworkInfo()} will now appear disconnected.
Danica Chang6fdd0c62010-08-11 14:54:43 -07001491 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001492 * @return Whether background data usage is allowed.
1493 */
Jeff Sharkey3a844fc2011-08-16 14:37:57 -07001494 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001495 public boolean getBackgroundDataSetting() {
Jeff Sharkey3a844fc2011-08-16 14:37:57 -07001496 // assume that background data is allowed; final authority is
1497 // NetworkInfo which may be blocked.
1498 return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001499 }
1500
1501 /**
1502 * Sets the value of the setting for background data usage.
Robert Greenwaltc03fa502010-02-23 18:58:05 -08001503 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001504 * @param allowBackgroundData Whether an application should use data while
1505 * it is in the background.
Robert Greenwaltc03fa502010-02-23 18:58:05 -08001506 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001507 * @attr ref android.Manifest.permission#CHANGE_BACKGROUND_DATA_SETTING
1508 * @see #getBackgroundDataSetting()
1509 * @hide
1510 */
Jeff Sharkey3a844fc2011-08-16 14:37:57 -07001511 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001512 public void setBackgroundDataSetting(boolean allowBackgroundData) {
Jeff Sharkey3a844fc2011-08-16 14:37:57 -07001513 // ignored
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001514 }
Robert Greenwaltc03fa502010-02-23 18:58:05 -08001515
1516 /**
Jeff Sharkeyf0ceede2011-08-02 17:22:34 -07001517 * Return quota status for the current active network, or {@code null} if no
1518 * network is active. Quota status can change rapidly, so these values
1519 * shouldn't be cached.
Jeff Sharkey44a3e0d2011-10-06 10:50:09 -07001520 *
Paul Jensen0d719ca2015-02-13 14:18:39 -05001521 * <p>This method requires the caller to hold the permission
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08001522 * {@link android.Manifest.permission#ACCESS_NETWORK_STATE}.
1523 *
Jeff Sharkey44a3e0d2011-10-06 10:50:09 -07001524 * @hide
Jeff Sharkeyf0ceede2011-08-02 17:22:34 -07001525 */
1526 public NetworkQuotaInfo getActiveNetworkQuotaInfo() {
1527 try {
1528 return mService.getActiveNetworkQuotaInfo();
1529 } catch (RemoteException e) {
1530 return null;
1531 }
1532 }
1533
1534 /**
Robert Greenwaltc03fa502010-02-23 18:58:05 -08001535 * @hide
Robert Greenwaltafa05c02014-05-21 20:04:36 -07001536 * @deprecated Talk to TelephonyManager directly
Robert Greenwaltc03fa502010-02-23 18:58:05 -08001537 */
1538 public boolean getMobileDataEnabled() {
Robert Greenwaltafa05c02014-05-21 20:04:36 -07001539 IBinder b = ServiceManager.getService(Context.TELEPHONY_SERVICE);
1540 if (b != null) {
1541 try {
1542 ITelephony it = ITelephony.Stub.asInterface(b);
Wink Saville36ffb042014-12-05 11:10:30 -08001543 int subId = SubscriptionManager.getDefaultDataSubId();
1544 Log.d("ConnectivityManager", "getMobileDataEnabled()+ subId=" + subId);
1545 boolean retVal = it.getDataEnabled(subId);
1546 Log.d("ConnectivityManager", "getMobileDataEnabled()- subId=" + subId
1547 + " retVal=" + retVal);
1548 return retVal;
Robert Greenwaltafa05c02014-05-21 20:04:36 -07001549 } catch (RemoteException e) { }
Robert Greenwaltc03fa502010-02-23 18:58:05 -08001550 }
Wink Saville36ffb042014-12-05 11:10:30 -08001551 Log.d("ConnectivityManager", "getMobileDataEnabled()- remote exception retVal=false");
Robert Greenwaltafa05c02014-05-21 20:04:36 -07001552 return false;
Robert Greenwaltc03fa502010-02-23 18:58:05 -08001553 }
1554
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001555 /**
Robert Greenwaltb2489872014-09-04 16:44:35 -07001556 * Callback for use with {@link ConnectivityManager#addDefaultNetworkActiveListener}
Robert Greenwalt6078b502014-06-11 16:05:07 -07001557 * to find out when the system default network has gone in to a high power state.
Dianne Hackborn77b987f2014-02-26 16:20:52 -08001558 */
1559 public interface OnNetworkActiveListener {
1560 /**
1561 * Called on the main thread of the process to report that the current data network
1562 * has become active, and it is now a good time to perform any pending network
1563 * operations. Note that this listener only tells you when the network becomes
1564 * active; if at any other time you want to know whether it is active (and thus okay
1565 * to initiate network traffic), you can retrieve its instantaneous state with
Robert Greenwalt6078b502014-06-11 16:05:07 -07001566 * {@link ConnectivityManager#isDefaultNetworkActive}.
Dianne Hackborn77b987f2014-02-26 16:20:52 -08001567 */
1568 public void onNetworkActive();
1569 }
1570
1571 private INetworkManagementService getNetworkManagementService() {
1572 synchronized (this) {
1573 if (mNMService != null) {
1574 return mNMService;
1575 }
1576 IBinder b = ServiceManager.getService(Context.NETWORKMANAGEMENT_SERVICE);
1577 mNMService = INetworkManagementService.Stub.asInterface(b);
1578 return mNMService;
1579 }
1580 }
1581
1582 private final ArrayMap<OnNetworkActiveListener, INetworkActivityListener>
1583 mNetworkActivityListeners
1584 = new ArrayMap<OnNetworkActiveListener, INetworkActivityListener>();
1585
1586 /**
Robert Greenwalt6078b502014-06-11 16:05:07 -07001587 * Start listening to reports when the system's default data network is active, meaning it is
1588 * a good time to perform network traffic. Use {@link #isDefaultNetworkActive()}
1589 * to determine the current state of the system's default network after registering the
1590 * listener.
1591 * <p>
1592 * If the process default network has been set with
Paul Jensen72db88e2015-03-10 10:54:12 -04001593 * {@link ConnectivityManager#bindProcessToNetwork} this function will not
Robert Greenwalt6078b502014-06-11 16:05:07 -07001594 * reflect the process's default, but the system default.
Dianne Hackborn77b987f2014-02-26 16:20:52 -08001595 *
1596 * @param l The listener to be told when the network is active.
1597 */
Robert Greenwaltb2489872014-09-04 16:44:35 -07001598 public void addDefaultNetworkActiveListener(final OnNetworkActiveListener l) {
Dianne Hackborn77b987f2014-02-26 16:20:52 -08001599 INetworkActivityListener rl = new INetworkActivityListener.Stub() {
1600 @Override
1601 public void onNetworkActive() throws RemoteException {
1602 l.onNetworkActive();
1603 }
1604 };
1605
1606 try {
1607 getNetworkManagementService().registerNetworkActivityListener(rl);
1608 mNetworkActivityListeners.put(l, rl);
1609 } catch (RemoteException e) {
1610 }
1611 }
1612
1613 /**
1614 * Remove network active listener previously registered with
Robert Greenwaltb2489872014-09-04 16:44:35 -07001615 * {@link #addDefaultNetworkActiveListener}.
Dianne Hackborn77b987f2014-02-26 16:20:52 -08001616 *
1617 * @param l Previously registered listener.
1618 */
Robert Greenwaltb2489872014-09-04 16:44:35 -07001619 public void removeDefaultNetworkActiveListener(OnNetworkActiveListener l) {
Dianne Hackborn77b987f2014-02-26 16:20:52 -08001620 INetworkActivityListener rl = mNetworkActivityListeners.get(l);
1621 if (rl == null) {
1622 throw new IllegalArgumentException("Listener not registered: " + l);
1623 }
1624 try {
1625 getNetworkManagementService().unregisterNetworkActivityListener(rl);
1626 } catch (RemoteException e) {
1627 }
1628 }
1629
1630 /**
1631 * Return whether the data network is currently active. An active network means that
1632 * it is currently in a high power state for performing data transmission. On some
1633 * types of networks, it may be expensive to move and stay in such a state, so it is
1634 * more power efficient to batch network traffic together when the radio is already in
1635 * this state. This method tells you whether right now is currently a good time to
1636 * initiate network traffic, as the network is already active.
1637 */
Robert Greenwalt6078b502014-06-11 16:05:07 -07001638 public boolean isDefaultNetworkActive() {
Dianne Hackborn77b987f2014-02-26 16:20:52 -08001639 try {
1640 return getNetworkManagementService().isNetworkActive();
1641 } catch (RemoteException e) {
1642 }
1643 return false;
1644 }
1645
1646 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001647 * {@hide}
1648 */
Lorenzo Colittiffc42b02015-07-29 11:41:21 +09001649 public ConnectivityManager(Context context, IConnectivityManager service) {
1650 mContext = checkNotNull(context, "missing context");
Jeff Sharkeyf0ceede2011-08-02 17:22:34 -07001651 mService = checkNotNull(service, "missing IConnectivityManager");
Paul Jensene0bef712014-12-10 15:12:18 -05001652 sInstance = this;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001653 }
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -08001654
Jeff Sharkey8fc27e82012-04-04 20:40:58 -07001655 /** {@hide} */
1656 public static ConnectivityManager from(Context context) {
1657 return (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
1658 }
1659
Lorenzo Colittid5427052015-10-15 16:29:00 +09001660 /** {@hide} */
1661 public static final void enforceChangePermission(Context context) {
1662 int uid = Binder.getCallingUid();
1663 Settings.checkAndNoteChangeNetworkStateOperation(context, uid, Settings
1664 .getPackageNameForUid(context, uid), true /* throwException */);
1665 }
1666
Robert Greenwaltedb47662014-09-16 17:54:19 -07001667 /** {@hide */
1668 public static final void enforceTetherChangePermission(Context context) {
1669 if (context.getResources().getStringArray(
1670 com.android.internal.R.array.config_mobile_hotspot_provision_app).length == 2) {
1671 // Have a provisioning app - must only let system apps (which check this app)
1672 // turn on tethering
1673 context.enforceCallingOrSelfPermission(
Jeremy Kleind42209d2015-12-28 15:11:58 -08001674 android.Manifest.permission.TETHER_PRIVILEGED, "ConnectivityService");
Robert Greenwaltedb47662014-09-16 17:54:19 -07001675 } else {
Billy Laua7238a32015-08-01 12:45:02 +01001676 int uid = Binder.getCallingUid();
Lorenzo Colittid5427052015-10-15 16:29:00 +09001677 Settings.checkAndNoteWriteSettingsOperation(context, uid, Settings
1678 .getPackageNameForUid(context, uid), true /* throwException */);
Robert Greenwaltedb47662014-09-16 17:54:19 -07001679 }
1680 }
1681
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -08001682 /**
Paul Jensene0bef712014-12-10 15:12:18 -05001683 * @deprecated - use getSystemService. This is a kludge to support static access in certain
1684 * situations where a Context pointer is unavailable.
1685 * @hide
1686 */
Paul Jensen72db88e2015-03-10 10:54:12 -04001687 static ConnectivityManager getInstanceOrNull() {
1688 return sInstance;
1689 }
1690
1691 /**
1692 * @deprecated - use getSystemService. This is a kludge to support static access in certain
1693 * situations where a Context pointer is unavailable.
1694 * @hide
1695 */
1696 private static ConnectivityManager getInstance() {
1697 if (getInstanceOrNull() == null) {
Paul Jensene0bef712014-12-10 15:12:18 -05001698 throw new IllegalStateException("No ConnectivityManager yet constructed");
1699 }
Paul Jensen72db88e2015-03-10 10:54:12 -04001700 return getInstanceOrNull();
Paul Jensene0bef712014-12-10 15:12:18 -05001701 }
1702
1703 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08001704 * Get the set of tetherable, available interfaces. This list is limited by
1705 * device configuration and current interface existence.
Paul Jensenb2748922015-05-06 11:10:18 -04001706 * <p>This method requires the caller to hold the permission
1707 * {@link android.Manifest.permission#ACCESS_NETWORK_STATE}.
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08001708 *
1709 * @return an array of 0 or more Strings of tetherable interface names.
1710 *
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -08001711 * {@hide}
1712 */
1713 public String[] getTetherableIfaces() {
1714 try {
1715 return mService.getTetherableIfaces();
1716 } catch (RemoteException e) {
1717 return new String[0];
1718 }
1719 }
1720
1721 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08001722 * Get the set of tethered interfaces.
Paul Jensenb2748922015-05-06 11:10:18 -04001723 * <p>This method requires the caller to hold the permission
1724 * {@link android.Manifest.permission#ACCESS_NETWORK_STATE}.
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08001725 *
1726 * @return an array of 0 or more String of currently tethered interface names.
1727 *
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -08001728 * {@hide}
1729 */
1730 public String[] getTetheredIfaces() {
1731 try {
1732 return mService.getTetheredIfaces();
1733 } catch (RemoteException e) {
1734 return new String[0];
1735 }
1736 }
1737
1738 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08001739 * Get the set of interface names which attempted to tether but
1740 * failed. Re-attempting to tether may cause them to reset to the Tethered
1741 * state. Alternatively, causing the interface to be destroyed and recreated
1742 * may cause them to reset to the available state.
1743 * {@link ConnectivityManager#getLastTetherError} can be used to get more
1744 * information on the cause of the errors.
Paul Jensenb2748922015-05-06 11:10:18 -04001745 * <p>This method requires the caller to hold the permission
1746 * {@link android.Manifest.permission#ACCESS_NETWORK_STATE}.
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08001747 *
1748 * @return an array of 0 or more String indicating the interface names
1749 * which failed to tether.
1750 *
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -08001751 * {@hide}
1752 */
Robert Greenwalt5a735062010-03-02 17:25:02 -08001753 public String[] getTetheringErroredIfaces() {
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -08001754 try {
Robert Greenwalt5a735062010-03-02 17:25:02 -08001755 return mService.getTetheringErroredIfaces();
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -08001756 } catch (RemoteException e) {
Robert Greenwalt5a735062010-03-02 17:25:02 -08001757 return new String[0];
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -08001758 }
1759 }
1760
1761 /**
Robert Greenwalt9c7e2c22014-06-23 14:53:42 -07001762 * Get the set of tethered dhcp ranges.
1763 *
1764 * @return an array of 0 or more {@code String} of tethered dhcp ranges.
1765 * {@hide}
1766 */
1767 public String[] getTetheredDhcpRanges() {
1768 try {
1769 return mService.getTetheredDhcpRanges();
1770 } catch (RemoteException e) {
1771 return new String[0];
1772 }
1773 }
1774
1775 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08001776 * Attempt to tether the named interface. This will setup a dhcp server
1777 * on the interface, forward and NAT IP packets and forward DNS requests
1778 * to the best active upstream network interface. Note that if no upstream
1779 * IP network interface is available, dhcp will still run and traffic will be
1780 * allowed between the tethered devices and this device, though upstream net
1781 * access will of course fail until an upstream network interface becomes
1782 * active.
Lorenzo Colittid5427052015-10-15 16:29:00 +09001783 *
1784 * <p>This method requires the caller to hold either the
1785 * {@link android.Manifest.permission#CHANGE_NETWORK_STATE} permission
1786 * or the ability to modify system settings as determined by
1787 * {@link android.provider.Settings.System#canWrite}.</p>
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08001788 *
1789 * @param iface the interface name to tether.
1790 * @return error a {@code TETHER_ERROR} value indicating success or failure type
1791 *
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -08001792 * {@hide}
1793 */
Robert Greenwalt5a735062010-03-02 17:25:02 -08001794 public int tether(String iface) {
1795 try {
1796 return mService.tether(iface);
1797 } catch (RemoteException e) {
1798 return TETHER_ERROR_SERVICE_UNAVAIL;
1799 }
1800 }
1801
1802 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08001803 * Stop tethering the named interface.
Lorenzo Colittid5427052015-10-15 16:29:00 +09001804 *
1805 * <p>This method requires the caller to hold either the
1806 * {@link android.Manifest.permission#CHANGE_NETWORK_STATE} permission
1807 * or the ability to modify system settings as determined by
1808 * {@link android.provider.Settings.System#canWrite}.</p>
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08001809 *
1810 * @param iface the interface name to untether.
1811 * @return error a {@code TETHER_ERROR} value indicating success or failure type
1812 *
Robert Greenwalt5a735062010-03-02 17:25:02 -08001813 * {@hide}
1814 */
1815 public int untether(String iface) {
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -08001816 try {
1817 return mService.untether(iface);
1818 } catch (RemoteException e) {
Robert Greenwalt5a735062010-03-02 17:25:02 -08001819 return TETHER_ERROR_SERVICE_UNAVAIL;
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -08001820 }
1821 }
Robert Greenwalt2a091d72010-02-11 18:18:40 -08001822
1823 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08001824 * Check if the device allows for tethering. It may be disabled via
Dianne Hackborn77b987f2014-02-26 16:20:52 -08001825 * {@code ro.tether.denied} system property, Settings.TETHER_SUPPORTED or
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08001826 * due to device configuration.
Paul Jensenb2748922015-05-06 11:10:18 -04001827 * <p>This method requires the caller to hold the permission
1828 * {@link android.Manifest.permission#ACCESS_NETWORK_STATE}.
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08001829 *
1830 * @return a boolean - {@code true} indicating Tethering is supported.
1831 *
Robert Greenwalt2a091d72010-02-11 18:18:40 -08001832 * {@hide}
1833 */
1834 public boolean isTetheringSupported() {
1835 try {
1836 return mService.isTetheringSupported();
1837 } catch (RemoteException e) {
1838 return false;
1839 }
1840 }
1841
1842 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08001843 * Get the list of regular expressions that define any tetherable
1844 * USB network interfaces. If USB tethering is not supported by the
1845 * device, this list should be empty.
Paul Jensenb2748922015-05-06 11:10:18 -04001846 * <p>This method requires the caller to hold the permission
1847 * {@link android.Manifest.permission#ACCESS_NETWORK_STATE}.
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08001848 *
1849 * @return an array of 0 or more regular expression Strings defining
1850 * what interfaces are considered tetherable usb interfaces.
1851 *
Robert Greenwalt2a091d72010-02-11 18:18:40 -08001852 * {@hide}
1853 */
1854 public String[] getTetherableUsbRegexs() {
1855 try {
1856 return mService.getTetherableUsbRegexs();
1857 } catch (RemoteException e) {
1858 return new String[0];
1859 }
1860 }
1861
1862 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08001863 * Get the list of regular expressions that define any tetherable
1864 * Wifi network interfaces. If Wifi tethering is not supported by the
1865 * device, this list should be empty.
Paul Jensenb2748922015-05-06 11:10:18 -04001866 * <p>This method requires the caller to hold the permission
1867 * {@link android.Manifest.permission#ACCESS_NETWORK_STATE}.
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08001868 *
1869 * @return an array of 0 or more regular expression Strings defining
1870 * what interfaces are considered tetherable wifi interfaces.
1871 *
Robert Greenwalt2a091d72010-02-11 18:18:40 -08001872 * {@hide}
1873 */
1874 public String[] getTetherableWifiRegexs() {
1875 try {
1876 return mService.getTetherableWifiRegexs();
1877 } catch (RemoteException e) {
1878 return new String[0];
1879 }
1880 }
Robert Greenwalt5a735062010-03-02 17:25:02 -08001881
Danica Chang6fdd0c62010-08-11 14:54:43 -07001882 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08001883 * Get the list of regular expressions that define any tetherable
1884 * Bluetooth network interfaces. If Bluetooth tethering is not supported by the
1885 * device, this list should be empty.
Paul Jensenb2748922015-05-06 11:10:18 -04001886 * <p>This method requires the caller to hold the permission
1887 * {@link android.Manifest.permission#ACCESS_NETWORK_STATE}.
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08001888 *
1889 * @return an array of 0 or more regular expression Strings defining
1890 * what interfaces are considered tetherable bluetooth interfaces.
1891 *
Danica Chang6fdd0c62010-08-11 14:54:43 -07001892 * {@hide}
1893 */
1894 public String[] getTetherableBluetoothRegexs() {
1895 try {
1896 return mService.getTetherableBluetoothRegexs();
1897 } catch (RemoteException e) {
1898 return new String[0];
1899 }
1900 }
1901
Mike Lockwood6c2260b2011-07-19 13:04:47 -07001902 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08001903 * Attempt to both alter the mode of USB and Tethering of USB. A
1904 * utility method to deal with some of the complexity of USB - will
1905 * attempt to switch to Rndis and subsequently tether the resulting
1906 * interface on {@code true} or turn off tethering and switch off
1907 * Rndis on {@code false}.
Lorenzo Colittid5427052015-10-15 16:29:00 +09001908 *
1909 * <p>This method requires the caller to hold either the
1910 * {@link android.Manifest.permission#CHANGE_NETWORK_STATE} permission
1911 * or the ability to modify system settings as determined by
1912 * {@link android.provider.Settings.System#canWrite}.</p>
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08001913 *
1914 * @param enable a boolean - {@code true} to enable tethering
1915 * @return error a {@code TETHER_ERROR} value indicating success or failure type
1916 *
Mike Lockwood6c2260b2011-07-19 13:04:47 -07001917 * {@hide}
1918 */
1919 public int setUsbTethering(boolean enable) {
1920 try {
1921 return mService.setUsbTethering(enable);
1922 } catch (RemoteException e) {
1923 return TETHER_ERROR_SERVICE_UNAVAIL;
1924 }
1925 }
1926
Robert Greenwalt5a735062010-03-02 17:25:02 -08001927 /** {@hide} */
1928 public static final int TETHER_ERROR_NO_ERROR = 0;
1929 /** {@hide} */
1930 public static final int TETHER_ERROR_UNKNOWN_IFACE = 1;
1931 /** {@hide} */
1932 public static final int TETHER_ERROR_SERVICE_UNAVAIL = 2;
1933 /** {@hide} */
1934 public static final int TETHER_ERROR_UNSUPPORTED = 3;
1935 /** {@hide} */
1936 public static final int TETHER_ERROR_UNAVAIL_IFACE = 4;
1937 /** {@hide} */
1938 public static final int TETHER_ERROR_MASTER_ERROR = 5;
1939 /** {@hide} */
1940 public static final int TETHER_ERROR_TETHER_IFACE_ERROR = 6;
1941 /** {@hide} */
1942 public static final int TETHER_ERROR_UNTETHER_IFACE_ERROR = 7;
1943 /** {@hide} */
1944 public static final int TETHER_ERROR_ENABLE_NAT_ERROR = 8;
1945 /** {@hide} */
1946 public static final int TETHER_ERROR_DISABLE_NAT_ERROR = 9;
1947 /** {@hide} */
1948 public static final int TETHER_ERROR_IFACE_CFG_ERROR = 10;
1949
1950 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08001951 * Get a more detailed error code after a Tethering or Untethering
1952 * request asynchronously failed.
Paul Jensenb2748922015-05-06 11:10:18 -04001953 * <p>This method requires the caller to hold the permission
1954 * {@link android.Manifest.permission#ACCESS_NETWORK_STATE}.
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08001955 *
1956 * @param iface The name of the interface of interest
Robert Greenwalt5a735062010-03-02 17:25:02 -08001957 * @return error The error code of the last error tethering or untethering the named
1958 * interface
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08001959 *
Robert Greenwalt5a735062010-03-02 17:25:02 -08001960 * {@hide}
1961 */
1962 public int getLastTetherError(String iface) {
1963 try {
1964 return mService.getLastTetherError(iface);
1965 } catch (RemoteException e) {
1966 return TETHER_ERROR_SERVICE_UNAVAIL;
1967 }
Robert Greenwalt14f2ef42010-06-15 12:19:37 -07001968 }
1969
1970 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08001971 * Report network connectivity status. This is currently used only
1972 * to alter status bar UI.
Paul Jensenb2748922015-05-06 11:10:18 -04001973 * <p>This method requires the caller to hold the permission
1974 * {@link android.Manifest.permission#STATUS_BAR}.
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08001975 *
Robert Greenwaltd7085fc2010-09-08 15:24:47 -07001976 * @param networkType The type of network you want to report on
1977 * @param percentage The quality of the connection 0 is bad, 100 is good
1978 * {@hide}
1979 */
1980 public void reportInetCondition(int networkType, int percentage) {
1981 try {
1982 mService.reportInetCondition(networkType, percentage);
1983 } catch (RemoteException e) {
1984 }
1985 }
Robert Greenwalt434203a2010-10-11 16:00:27 -07001986
1987 /**
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07001988 * Report a problem network to the framework. This provides a hint to the system
Ye Wenb87875e2014-07-21 14:19:01 -07001989 * that there might be connectivity problems on this network and may cause
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07001990 * the framework to re-evaluate network connectivity and/or switch to another
1991 * network.
Robert Greenwalt9258c642014-03-26 16:47:06 -07001992 *
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07001993 * @param network The {@link Network} the application was attempting to use
1994 * or {@code null} to indicate the current default network.
Paul Jensenbfd17b72015-04-07 12:43:13 -04001995 * @deprecated Use {@link #reportNetworkConnectivity} which allows reporting both
1996 * working and non-working connectivity.
Robert Greenwalt9258c642014-03-26 16:47:06 -07001997 */
1998 public void reportBadNetwork(Network network) {
1999 try {
Paul Jensenbfd17b72015-04-07 12:43:13 -04002000 // One of these will be ignored because it matches system's current state.
2001 // The other will trigger the necessary reevaluation.
2002 mService.reportNetworkConnectivity(network, true);
2003 mService.reportNetworkConnectivity(network, false);
2004 } catch (RemoteException e) {
2005 }
2006 }
2007
2008 /**
2009 * Report to the framework whether a network has working connectivity.
2010 * This provides a hint to the system that a particular network is providing
2011 * working connectivity or not. In response the framework may re-evaluate
2012 * the network's connectivity and might take further action thereafter.
2013 *
2014 * @param network The {@link Network} the application was attempting to use
2015 * or {@code null} to indicate the current default network.
2016 * @param hasConnectivity {@code true} if the application was able to successfully access the
2017 * Internet using {@code network} or {@code false} if not.
2018 */
2019 public void reportNetworkConnectivity(Network network, boolean hasConnectivity) {
2020 try {
2021 mService.reportNetworkConnectivity(network, hasConnectivity);
Robert Greenwalt9258c642014-03-26 16:47:06 -07002022 } catch (RemoteException e) {
2023 }
2024 }
2025
2026 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08002027 * Set a network-independent global http proxy. This is not normally what you want
2028 * for typical HTTP proxies - they are general network dependent. However if you're
2029 * doing something unusual like general internal filtering this may be useful. On
2030 * a private network where the proxy is not accessible, you may break HTTP using this.
Paul Jensen0d719ca2015-02-13 14:18:39 -05002031 * <p>This method requires the caller to hold the permission
Jason Monkfaf3fd52014-05-07 18:41:13 -04002032 * android.Manifest.permission#CONNECTIVITY_INTERNAL.
Paul Jensenb2748922015-05-06 11:10:18 -04002033 *
2034 * @param p A {@link ProxyInfo} object defining the new global
2035 * HTTP proxy. A {@code null} value will clear the global HTTP proxy.
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07002036 * @hide
Robert Greenwalt434203a2010-10-11 16:00:27 -07002037 */
Jason Monk207900c2014-04-25 15:00:09 -04002038 public void setGlobalProxy(ProxyInfo p) {
Robert Greenwalt434203a2010-10-11 16:00:27 -07002039 try {
2040 mService.setGlobalProxy(p);
2041 } catch (RemoteException e) {
2042 }
2043 }
2044
2045 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08002046 * Retrieve any network-independent global HTTP proxy.
2047 *
Jason Monk207900c2014-04-25 15:00:09 -04002048 * @return {@link ProxyInfo} for the current global HTTP proxy or {@code null}
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08002049 * if no global HTTP proxy is set.
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07002050 * @hide
Robert Greenwalt434203a2010-10-11 16:00:27 -07002051 */
Jason Monk207900c2014-04-25 15:00:09 -04002052 public ProxyInfo getGlobalProxy() {
Robert Greenwalt434203a2010-10-11 16:00:27 -07002053 try {
2054 return mService.getGlobalProxy();
2055 } catch (RemoteException e) {
2056 return null;
2057 }
2058 }
2059
2060 /**
Paul Jensencee9b512015-05-06 07:32:40 -04002061 * Retrieve the global HTTP proxy, or if no global HTTP proxy is set, a
2062 * network-specific HTTP proxy. If {@code network} is null, the
2063 * network-specific proxy returned is the proxy of the default active
2064 * network.
2065 *
2066 * @return {@link ProxyInfo} for the current global HTTP proxy, or if no
2067 * global HTTP proxy is set, {@code ProxyInfo} for {@code network},
2068 * or when {@code network} is {@code null},
2069 * the {@code ProxyInfo} for the default active network. Returns
2070 * {@code null} when no proxy applies or the caller doesn't have
2071 * permission to use {@code network}.
2072 * @hide
2073 */
2074 public ProxyInfo getProxyForNetwork(Network network) {
2075 try {
2076 return mService.getProxyForNetwork(network);
2077 } catch (RemoteException e) {
2078 return null;
2079 }
2080 }
2081
2082 /**
Paul Jensene0bef712014-12-10 15:12:18 -05002083 * Get the current default HTTP proxy settings. If a global proxy is set it will be returned,
2084 * otherwise if this process is bound to a {@link Network} using
Paul Jensen72db88e2015-03-10 10:54:12 -04002085 * {@link #bindProcessToNetwork} then that {@code Network}'s proxy is returned, otherwise
Paul Jensene0bef712014-12-10 15:12:18 -05002086 * the default network's proxy is returned.
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08002087 *
Jason Monk207900c2014-04-25 15:00:09 -04002088 * @return the {@link ProxyInfo} for the current HTTP proxy, or {@code null} if no
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08002089 * HTTP proxy is active.
Robert Greenwalt434203a2010-10-11 16:00:27 -07002090 */
Paul Jensene0bef712014-12-10 15:12:18 -05002091 public ProxyInfo getDefaultProxy() {
Paul Jensencee9b512015-05-06 07:32:40 -04002092 return getProxyForNetwork(getBoundNetworkForProcess());
Robert Greenwalt434203a2010-10-11 16:00:27 -07002093 }
Robert Greenwaltd55a6b42011-03-25 13:09:25 -07002094
2095 /**
Robert Greenwalt9b2886e2011-08-31 11:46:42 -07002096 * Returns true if the hardware supports the given network type
2097 * else it returns false. This doesn't indicate we have coverage
2098 * or are authorized onto a network, just whether or not the
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08002099 * hardware supports it. For example a GSM phone without a SIM
2100 * should still return {@code true} for mobile data, but a wifi only
2101 * tablet would return {@code false}.
Paul Jensenb2748922015-05-06 11:10:18 -04002102 * <p>This method requires the caller to hold the permission
2103 * {@link android.Manifest.permission#ACCESS_NETWORK_STATE}.
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08002104 *
2105 * @param networkType The network type we'd like to check
2106 * @return {@code true} if supported, else {@code false}
2107 *
Robert Greenwalt9b2886e2011-08-31 11:46:42 -07002108 * @hide
2109 */
2110 public boolean isNetworkSupported(int networkType) {
2111 try {
2112 return mService.isNetworkSupported(networkType);
2113 } catch (RemoteException e) {}
2114 return false;
2115 }
Jeff Sharkey9f7cbf02012-04-12 18:34:54 -07002116
2117 /**
2118 * Returns if the currently active data network is metered. A network is
2119 * classified as metered when the user is sensitive to heavy data usage on
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08002120 * that connection due to monetary costs, data limitations or
2121 * battery/performance issues. You should check this before doing large
2122 * data transfers, and warn the user or delay the operation until another
2123 * network is available.
Paul Jensenb2748922015-05-06 11:10:18 -04002124 * <p>This method requires the caller to hold the permission
2125 * {@link android.Manifest.permission#ACCESS_NETWORK_STATE}.
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08002126 *
2127 * @return {@code true} if large transfers should be avoided, otherwise
2128 * {@code false}.
Jeff Sharkey9f7cbf02012-04-12 18:34:54 -07002129 */
2130 public boolean isActiveNetworkMetered() {
2131 try {
2132 return mService.isActiveNetworkMetered();
2133 } catch (RemoteException e) {
2134 return false;
2135 }
2136 }
Jeff Sharkey69ddab42012-08-25 00:05:46 -07002137
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08002138 /**
2139 * If the LockdownVpn mechanism is enabled, updates the vpn
2140 * with a reload of its profile.
2141 *
2142 * @return a boolean with {@code} indicating success
2143 *
2144 * <p>This method can only be called by the system UID
2145 * {@hide}
2146 */
Jeff Sharkey69ddab42012-08-25 00:05:46 -07002147 public boolean updateLockdownVpn() {
2148 try {
2149 return mService.updateLockdownVpn();
2150 } catch (RemoteException e) {
2151 return false;
2152 }
2153 }
Irfan Sheriffda6da092012-08-16 12:49:23 -07002154
2155 /**
Wink Saville948282b2013-08-29 08:55:16 -07002156 * Check mobile provisioning.
Wink Savilleab9321d2013-06-29 21:10:57 -07002157 *
Wink Savilleab9321d2013-06-29 21:10:57 -07002158 * @param suggestedTimeOutMs, timeout in milliseconds
Wink Savilleab9321d2013-06-29 21:10:57 -07002159 *
2160 * @return time out that will be used, maybe less that suggestedTimeOutMs
2161 * -1 if an error.
2162 *
2163 * {@hide}
2164 */
Wink Saville948282b2013-08-29 08:55:16 -07002165 public int checkMobileProvisioning(int suggestedTimeOutMs) {
Wink Savilleab9321d2013-06-29 21:10:57 -07002166 int timeOutMs = -1;
2167 try {
Wink Saville948282b2013-08-29 08:55:16 -07002168 timeOutMs = mService.checkMobileProvisioning(suggestedTimeOutMs);
Wink Savilleab9321d2013-06-29 21:10:57 -07002169 } catch (RemoteException e) {
2170 }
2171 return timeOutMs;
2172 }
Robert Greenwalte182bfe2013-07-16 12:06:09 -07002173
2174 /**
Wink Saville42d4f082013-07-20 20:31:59 -07002175 * Get the mobile provisioning url.
Robert Greenwalte182bfe2013-07-16 12:06:09 -07002176 * {@hide}
2177 */
2178 public String getMobileProvisioningUrl() {
2179 try {
2180 return mService.getMobileProvisioningUrl();
2181 } catch (RemoteException e) {
2182 }
2183 return null;
2184 }
Wink Saville42d4f082013-07-20 20:31:59 -07002185
2186 /**
Wink Saville948282b2013-08-29 08:55:16 -07002187 * Set sign in error notification to visible or in visible
2188 *
2189 * @param visible
2190 * @param networkType
2191 *
2192 * {@hide}
Paul Jensen3541e9f2015-03-18 12:23:02 -04002193 * @deprecated Doesn't properly deal with multiple connected networks of the same type.
Wink Saville948282b2013-08-29 08:55:16 -07002194 */
2195 public void setProvisioningNotificationVisible(boolean visible, int networkType,
Paul Jensen89e0f092014-09-15 15:59:36 -04002196 String action) {
Wink Saville948282b2013-08-29 08:55:16 -07002197 try {
Paul Jensen89e0f092014-09-15 15:59:36 -04002198 mService.setProvisioningNotificationVisible(visible, networkType, action);
Wink Saville948282b2013-08-29 08:55:16 -07002199 } catch (RemoteException e) {
2200 }
2201 }
Yuhao Zheng5cd1a0e2013-09-09 17:00:04 -07002202
2203 /**
2204 * Set the value for enabling/disabling airplane mode
Paul Jensenb2748922015-05-06 11:10:18 -04002205 * <p>This method requires the caller to hold the permission
2206 * {@link android.Manifest.permission#CONNECTIVITY_INTERNAL}.
Yuhao Zheng5cd1a0e2013-09-09 17:00:04 -07002207 *
2208 * @param enable whether to enable airplane mode or not
2209 *
Yuhao Zheng5cd1a0e2013-09-09 17:00:04 -07002210 * @hide
2211 */
2212 public void setAirplaneMode(boolean enable) {
2213 try {
2214 mService.setAirplaneMode(enable);
2215 } catch (RemoteException e) {
2216 }
2217 }
Robert Greenwalte049c232014-04-11 15:53:27 -07002218
2219 /** {@hide} */
Robert Greenwalta67be032014-05-16 15:49:14 -07002220 public void registerNetworkFactory(Messenger messenger, String name) {
Robert Greenwalte049c232014-04-11 15:53:27 -07002221 try {
Robert Greenwalta67be032014-05-16 15:49:14 -07002222 mService.registerNetworkFactory(messenger, name);
2223 } catch (RemoteException e) { }
2224 }
2225
2226 /** {@hide} */
2227 public void unregisterNetworkFactory(Messenger messenger) {
2228 try {
2229 mService.unregisterNetworkFactory(messenger);
Robert Greenwalte049c232014-04-11 15:53:27 -07002230 } catch (RemoteException e) { }
2231 }
Robert Greenwalt7b816022014-04-18 15:25:25 -07002232
Paul Jensen31a94f42015-02-13 14:18:39 -05002233 /**
2234 * @hide
2235 * Register a NetworkAgent with ConnectivityService.
2236 * @return NetID corresponding to NetworkAgent.
2237 */
2238 public int registerNetworkAgent(Messenger messenger, NetworkInfo ni, LinkProperties lp,
Sreeram Ramachandran8cd33ed2014-07-23 15:23:15 -07002239 NetworkCapabilities nc, int score, NetworkMisc misc) {
Robert Greenwalt7b816022014-04-18 15:25:25 -07002240 try {
Paul Jensen31a94f42015-02-13 14:18:39 -05002241 return mService.registerNetworkAgent(messenger, ni, lp, nc, score, misc);
2242 } catch (RemoteException e) {
2243 return NETID_UNSET;
2244 }
Robert Greenwalt7b816022014-04-18 15:25:25 -07002245 }
2246
Robert Greenwalt9258c642014-03-26 16:47:06 -07002247 /**
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07002248 * Base class for NetworkRequest callbacks. Used for notifications about network
2249 * changes. Should be extended by applications wanting notifications.
Robert Greenwalt9258c642014-03-26 16:47:06 -07002250 */
Robert Greenwalt6078b502014-06-11 16:05:07 -07002251 public static class NetworkCallback {
Robert Greenwalt7b816022014-04-18 15:25:25 -07002252 /**
Lorenzo Colitti07086932015-04-24 12:23:24 +09002253 * Called when the framework connects to a new network to evaluate whether it satisfies this
2254 * request. If evaluation succeeds, this callback may be followed by an {@link #onAvailable}
2255 * callback. There is no guarantee that this new network will satisfy any requests, or that
2256 * the network will stay connected for longer than the time necessary to evaluate it.
2257 * <p>
2258 * Most applications <b>should not</b> act on this callback, and should instead use
2259 * {@link #onAvailable}. This callback is intended for use by applications that can assist
2260 * the framework in properly evaluating the network &mdash; for example, an application that
2261 * can automatically log in to a captive portal without user intervention.
2262 *
2263 * @param network The {@link Network} of the network that is being evaluated.
Lorenzo Colitti66276122015-06-11 14:27:17 +09002264 *
2265 * @hide
Robert Greenwalt7b816022014-04-18 15:25:25 -07002266 */
Robert Greenwalt6078b502014-06-11 16:05:07 -07002267 public void onPreCheck(Network network) {}
Robert Greenwalt7b816022014-04-18 15:25:25 -07002268
2269 /**
Lorenzo Colitti07086932015-04-24 12:23:24 +09002270 * Called when the framework connects and has declared a new network ready for use.
Robert Greenwalt6078b502014-06-11 16:05:07 -07002271 * This callback may be called more than once if the {@link Network} that is
2272 * satisfying the request changes.
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07002273 *
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07002274 * @param network The {@link Network} of the satisfying network.
Robert Greenwalt7b816022014-04-18 15:25:25 -07002275 */
Robert Greenwalt6078b502014-06-11 16:05:07 -07002276 public void onAvailable(Network network) {}
Robert Greenwalt7b816022014-04-18 15:25:25 -07002277
2278 /**
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07002279 * Called when the network is about to be disconnected. Often paired with an
Robert Greenwalt6078b502014-06-11 16:05:07 -07002280 * {@link NetworkCallback#onAvailable} call with the new replacement network
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07002281 * for graceful handover. This may not be called if we have a hard loss
2282 * (loss without warning). This may be followed by either a
Robert Greenwalt6078b502014-06-11 16:05:07 -07002283 * {@link NetworkCallback#onLost} call or a
2284 * {@link NetworkCallback#onAvailable} call for this network depending
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07002285 * on whether we lose or regain it.
2286 *
Robert Greenwalt6078b502014-06-11 16:05:07 -07002287 * @param network The {@link Network} that is about to be disconnected.
2288 * @param maxMsToLive The time in ms the framework will attempt to keep the
2289 * network connected. Note that the network may suffer a
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07002290 * hard loss at any time.
Robert Greenwalt7b816022014-04-18 15:25:25 -07002291 */
Robert Greenwalt6078b502014-06-11 16:05:07 -07002292 public void onLosing(Network network, int maxMsToLive) {}
Robert Greenwalt7b816022014-04-18 15:25:25 -07002293
2294 /**
2295 * Called when the framework has a hard loss of the network or when the
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07002296 * graceful failure ends.
2297 *
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07002298 * @param network The {@link Network} lost.
Robert Greenwalt7b816022014-04-18 15:25:25 -07002299 */
Robert Greenwalt6078b502014-06-11 16:05:07 -07002300 public void onLost(Network network) {}
Robert Greenwalt7b816022014-04-18 15:25:25 -07002301
2302 /**
2303 * Called if no network is found in the given timeout time. If no timeout is given,
2304 * this will not be called.
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07002305 * @hide
Robert Greenwalt7b816022014-04-18 15:25:25 -07002306 */
Robert Greenwalt6078b502014-06-11 16:05:07 -07002307 public void onUnavailable() {}
Robert Greenwalt7b816022014-04-18 15:25:25 -07002308
2309 /**
2310 * Called when the network the framework connected to for this request
2311 * changes capabilities but still satisfies the stated need.
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07002312 *
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07002313 * @param network The {@link Network} whose capabilities have changed.
Lorenzo Colittie285b432015-04-23 15:32:42 +09002314 * @param networkCapabilities The new {@link android.net.NetworkCapabilities} for this network.
Robert Greenwalt7b816022014-04-18 15:25:25 -07002315 */
Robert Greenwalt6078b502014-06-11 16:05:07 -07002316 public void onCapabilitiesChanged(Network network,
Robert Greenwalt7b816022014-04-18 15:25:25 -07002317 NetworkCapabilities networkCapabilities) {}
2318
2319 /**
2320 * Called when the network the framework connected to for this request
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07002321 * changes {@link LinkProperties}.
2322 *
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07002323 * @param network The {@link Network} whose link properties have changed.
2324 * @param linkProperties The new {@link LinkProperties} for this network.
Robert Greenwalt7b816022014-04-18 15:25:25 -07002325 */
Robert Greenwalt6078b502014-06-11 16:05:07 -07002326 public void onLinkPropertiesChanged(Network network, LinkProperties linkProperties) {}
Robert Greenwalt7b816022014-04-18 15:25:25 -07002327
Robert Greenwalt8d482522015-06-24 13:23:42 -07002328 /**
2329 * Called when the network the framework connected to for this request
2330 * goes into {@link NetworkInfo.DetailedState.SUSPENDED}.
2331 * This generally means that while the TCP connections are still live,
2332 * temporarily network data fails to transfer. Specifically this is used
2333 * on cellular networks to mask temporary outages when driving through
2334 * a tunnel, etc.
2335 * @hide
2336 */
2337 public void onNetworkSuspended(Network network) {}
2338
2339 /**
2340 * Called when the network the framework connected to for this request
2341 * returns from a {@link NetworkInfo.DetailedState.SUSPENDED} state.
2342 * This should always be preceeded by a matching {@code onNetworkSuspended}
2343 * call.
2344 * @hide
2345 */
2346 public void onNetworkResumed(Network network) {}
2347
Robert Greenwalt6078b502014-06-11 16:05:07 -07002348 private NetworkRequest networkRequest;
Robert Greenwalt7b816022014-04-18 15:25:25 -07002349 }
2350
Robert Greenwalt9258c642014-03-26 16:47:06 -07002351 private static final int BASE = Protocol.BASE_CONNECTIVITY_MANAGER;
Robert Greenwalt9258c642014-03-26 16:47:06 -07002352 /** @hide */
Robert Greenwalt8d482522015-06-24 13:23:42 -07002353 public static final int CALLBACK_PRECHECK = BASE + 1;
2354 /** @hide */
2355 public static final int CALLBACK_AVAILABLE = BASE + 2;
2356 /** @hide arg1 = TTL */
2357 public static final int CALLBACK_LOSING = BASE + 3;
2358 /** @hide */
2359 public static final int CALLBACK_LOST = BASE + 4;
2360 /** @hide */
2361 public static final int CALLBACK_UNAVAIL = BASE + 5;
2362 /** @hide */
2363 public static final int CALLBACK_CAP_CHANGED = BASE + 6;
2364 /** @hide */
2365 public static final int CALLBACK_IP_CHANGED = BASE + 7;
2366 /** @hide */
2367 public static final int CALLBACK_RELEASED = BASE + 8;
2368 /** @hide */
2369 public static final int CALLBACK_EXIT = BASE + 9;
Robert Greenwalt562cc542014-05-15 18:07:26 -07002370 /** @hide obj = NetworkCapabilities, arg1 = seq number */
Robert Greenwalt8d482522015-06-24 13:23:42 -07002371 private static final int EXPIRE_LEGACY_REQUEST = BASE + 10;
2372 /** @hide */
2373 public static final int CALLBACK_SUSPENDED = BASE + 11;
2374 /** @hide */
2375 public static final int CALLBACK_RESUMED = BASE + 12;
Robert Greenwalt9258c642014-03-26 16:47:06 -07002376
Robert Greenwalt562cc542014-05-15 18:07:26 -07002377 private class CallbackHandler extends Handler {
Robert Greenwalt6078b502014-06-11 16:05:07 -07002378 private final HashMap<NetworkRequest, NetworkCallback>mCallbackMap;
Robert Greenwalt9258c642014-03-26 16:47:06 -07002379 private final AtomicInteger mRefCount;
2380 private static final String TAG = "ConnectivityManager.CallbackHandler";
2381 private final ConnectivityManager mCm;
Robert Greenwalta9ebeef2015-09-03 16:41:45 -07002382 private static final boolean DBG = false;
Robert Greenwalt9258c642014-03-26 16:47:06 -07002383
Robert Greenwalt6078b502014-06-11 16:05:07 -07002384 CallbackHandler(Looper looper, HashMap<NetworkRequest, NetworkCallback>callbackMap,
Robert Greenwalt9258c642014-03-26 16:47:06 -07002385 AtomicInteger refCount, ConnectivityManager cm) {
2386 super(looper);
2387 mCallbackMap = callbackMap;
2388 mRefCount = refCount;
2389 mCm = cm;
2390 }
2391
2392 @Override
2393 public void handleMessage(Message message) {
Robert Greenwalta9ebeef2015-09-03 16:41:45 -07002394 if (DBG) Log.d(TAG, "CM callback handler got msg " + message.what);
Lorenzo Colittidb95a602015-04-24 15:21:21 +09002395 NetworkRequest request = (NetworkRequest) getObject(message, NetworkRequest.class);
2396 Network network = (Network) getObject(message, Network.class);
Robert Greenwalt9258c642014-03-26 16:47:06 -07002397 switch (message.what) {
2398 case CALLBACK_PRECHECK: {
Lorenzo Colittidb95a602015-04-24 15:21:21 +09002399 NetworkCallback callback = getCallback(request, "PRECHECK");
2400 if (callback != null) {
2401 callback.onPreCheck(network);
Robert Greenwalt9258c642014-03-26 16:47:06 -07002402 }
2403 break;
2404 }
2405 case CALLBACK_AVAILABLE: {
Lorenzo Colittidb95a602015-04-24 15:21:21 +09002406 NetworkCallback callback = getCallback(request, "AVAILABLE");
2407 if (callback != null) {
2408 callback.onAvailable(network);
Robert Greenwalt9258c642014-03-26 16:47:06 -07002409 }
2410 break;
2411 }
2412 case CALLBACK_LOSING: {
Lorenzo Colittidb95a602015-04-24 15:21:21 +09002413 NetworkCallback callback = getCallback(request, "LOSING");
2414 if (callback != null) {
2415 callback.onLosing(network, message.arg1);
Robert Greenwalt9258c642014-03-26 16:47:06 -07002416 }
2417 break;
2418 }
2419 case CALLBACK_LOST: {
Lorenzo Colittidb95a602015-04-24 15:21:21 +09002420 NetworkCallback callback = getCallback(request, "LOST");
2421 if (callback != null) {
2422 callback.onLost(network);
Robert Greenwalt9258c642014-03-26 16:47:06 -07002423 }
2424 break;
2425 }
2426 case CALLBACK_UNAVAIL: {
Lorenzo Colittidb95a602015-04-24 15:21:21 +09002427 NetworkCallback callback = getCallback(request, "UNAVAIL");
2428 if (callback != null) {
2429 callback.onUnavailable();
Robert Greenwalt9258c642014-03-26 16:47:06 -07002430 }
2431 break;
2432 }
2433 case CALLBACK_CAP_CHANGED: {
Lorenzo Colittidb95a602015-04-24 15:21:21 +09002434 NetworkCallback callback = getCallback(request, "CAP_CHANGED");
2435 if (callback != null) {
Robert Greenwalta848c1c2014-09-30 16:50:07 -07002436 NetworkCapabilities cap = (NetworkCapabilities)getObject(message,
2437 NetworkCapabilities.class);
Robert Greenwalt9258c642014-03-26 16:47:06 -07002438
Lorenzo Colittidb95a602015-04-24 15:21:21 +09002439 callback.onCapabilitiesChanged(network, cap);
Robert Greenwalt9258c642014-03-26 16:47:06 -07002440 }
2441 break;
2442 }
2443 case CALLBACK_IP_CHANGED: {
Lorenzo Colittidb95a602015-04-24 15:21:21 +09002444 NetworkCallback callback = getCallback(request, "IP_CHANGED");
2445 if (callback != null) {
Robert Greenwalta848c1c2014-09-30 16:50:07 -07002446 LinkProperties lp = (LinkProperties)getObject(message,
2447 LinkProperties.class);
Robert Greenwalt9258c642014-03-26 16:47:06 -07002448
Lorenzo Colittidb95a602015-04-24 15:21:21 +09002449 callback.onLinkPropertiesChanged(network, lp);
Robert Greenwalt9258c642014-03-26 16:47:06 -07002450 }
2451 break;
2452 }
Robert Greenwalt8d482522015-06-24 13:23:42 -07002453 case CALLBACK_SUSPENDED: {
2454 NetworkCallback callback = getCallback(request, "SUSPENDED");
2455 if (callback != null) {
2456 callback.onNetworkSuspended(network);
2457 }
2458 break;
2459 }
2460 case CALLBACK_RESUMED: {
2461 NetworkCallback callback = getCallback(request, "RESUMED");
2462 if (callback != null) {
2463 callback.onNetworkResumed(network);
2464 }
2465 break;
2466 }
Robert Greenwalt9258c642014-03-26 16:47:06 -07002467 case CALLBACK_RELEASED: {
Lorenzo Colittidb95a602015-04-24 15:21:21 +09002468 NetworkCallback callback = null;
Robert Greenwalt9258c642014-03-26 16:47:06 -07002469 synchronized(mCallbackMap) {
Lorenzo Colittidb95a602015-04-24 15:21:21 +09002470 callback = mCallbackMap.remove(request);
Robert Greenwalt9258c642014-03-26 16:47:06 -07002471 }
Lorenzo Colittidb95a602015-04-24 15:21:21 +09002472 if (callback != null) {
Robert Greenwalt6078b502014-06-11 16:05:07 -07002473 synchronized(mRefCount) {
2474 if (mRefCount.decrementAndGet() == 0) {
2475 getLooper().quit();
2476 }
2477 }
Robert Greenwalt9258c642014-03-26 16:47:06 -07002478 } else {
Lorenzo Colittidb95a602015-04-24 15:21:21 +09002479 Log.e(TAG, "callback not found for RELEASED message");
Robert Greenwalt9258c642014-03-26 16:47:06 -07002480 }
Robert Greenwalt9258c642014-03-26 16:47:06 -07002481 break;
2482 }
2483 case CALLBACK_EXIT: {
Lorenzo Colittidb95a602015-04-24 15:21:21 +09002484 Log.d(TAG, "Listener quitting");
Robert Greenwalt9258c642014-03-26 16:47:06 -07002485 getLooper().quit();
2486 break;
2487 }
Robert Greenwalt562cc542014-05-15 18:07:26 -07002488 case EXPIRE_LEGACY_REQUEST: {
2489 expireRequest((NetworkCapabilities)message.obj, message.arg1);
2490 break;
2491 }
Robert Greenwalt9258c642014-03-26 16:47:06 -07002492 }
2493 }
2494
Robert Greenwalta848c1c2014-09-30 16:50:07 -07002495 private Object getObject(Message msg, Class c) {
2496 return msg.getData().getParcelable(c.getSimpleName());
Robert Greenwalt9258c642014-03-26 16:47:06 -07002497 }
Lorenzo Colittidb95a602015-04-24 15:21:21 +09002498
2499 private NetworkCallback getCallback(NetworkRequest req, String name) {
2500 NetworkCallback callback;
Robert Greenwalt9258c642014-03-26 16:47:06 -07002501 synchronized(mCallbackMap) {
Lorenzo Colittidb95a602015-04-24 15:21:21 +09002502 callback = mCallbackMap.get(req);
Robert Greenwalt9258c642014-03-26 16:47:06 -07002503 }
Lorenzo Colittidb95a602015-04-24 15:21:21 +09002504 if (callback == null) {
2505 Log.e(TAG, "callback not found for " + name + " message");
2506 }
2507 return callback;
Robert Greenwalt9258c642014-03-26 16:47:06 -07002508 }
Robert Greenwalt9258c642014-03-26 16:47:06 -07002509 }
2510
Robert Greenwalt6078b502014-06-11 16:05:07 -07002511 private void incCallbackHandlerRefCount() {
Robert Greenwalt9258c642014-03-26 16:47:06 -07002512 synchronized(sCallbackRefCount) {
2513 if (sCallbackRefCount.incrementAndGet() == 1) {
2514 // TODO - switch this over to a ManagerThread or expire it when done
2515 HandlerThread callbackThread = new HandlerThread("ConnectivityManager");
2516 callbackThread.start();
2517 sCallbackHandler = new CallbackHandler(callbackThread.getLooper(),
Robert Greenwalt6078b502014-06-11 16:05:07 -07002518 sNetworkCallback, sCallbackRefCount, this);
Robert Greenwalt9258c642014-03-26 16:47:06 -07002519 }
2520 }
2521 }
2522
Robert Greenwalt6078b502014-06-11 16:05:07 -07002523 private void decCallbackHandlerRefCount() {
Robert Greenwalt9258c642014-03-26 16:47:06 -07002524 synchronized(sCallbackRefCount) {
2525 if (sCallbackRefCount.decrementAndGet() == 0) {
2526 sCallbackHandler.obtainMessage(CALLBACK_EXIT).sendToTarget();
2527 sCallbackHandler = null;
2528 }
2529 }
2530 }
2531
Robert Greenwalt6078b502014-06-11 16:05:07 -07002532 static final HashMap<NetworkRequest, NetworkCallback> sNetworkCallback =
2533 new HashMap<NetworkRequest, NetworkCallback>();
Robert Greenwalt9258c642014-03-26 16:47:06 -07002534 static final AtomicInteger sCallbackRefCount = new AtomicInteger(0);
2535 static CallbackHandler sCallbackHandler = null;
2536
2537 private final static int LISTEN = 1;
2538 private final static int REQUEST = 2;
2539
Robert Greenwalt562cc542014-05-15 18:07:26 -07002540 private NetworkRequest sendRequestForNetwork(NetworkCapabilities need,
Robert Greenwalt6078b502014-06-11 16:05:07 -07002541 NetworkCallback networkCallback, int timeoutSec, int action,
Robert Greenwalt32aa65a2014-06-02 15:32:02 -07002542 int legacyType) {
Robert Greenwalt6078b502014-06-11 16:05:07 -07002543 if (networkCallback == null) {
2544 throw new IllegalArgumentException("null NetworkCallback");
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07002545 }
Robert Greenwalt9258c642014-03-26 16:47:06 -07002546 if (need == null) throw new IllegalArgumentException("null NetworkCapabilities");
2547 try {
Robert Greenwalt6078b502014-06-11 16:05:07 -07002548 incCallbackHandlerRefCount();
Paul Jensen7221cc32014-06-27 11:05:32 -04002549 synchronized(sNetworkCallback) {
2550 if (action == LISTEN) {
2551 networkCallback.networkRequest = mService.listenForNetwork(need,
2552 new Messenger(sCallbackHandler), new Binder());
2553 } else {
2554 networkCallback.networkRequest = mService.requestNetwork(need,
2555 new Messenger(sCallbackHandler), timeoutSec, new Binder(), legacyType);
2556 }
2557 if (networkCallback.networkRequest != null) {
Robert Greenwalt6078b502014-06-11 16:05:07 -07002558 sNetworkCallback.put(networkCallback.networkRequest, networkCallback);
Robert Greenwalt9258c642014-03-26 16:47:06 -07002559 }
2560 }
2561 } catch (RemoteException e) {}
Robert Greenwalt6078b502014-06-11 16:05:07 -07002562 if (networkCallback.networkRequest == null) decCallbackHandlerRefCount();
2563 return networkCallback.networkRequest;
Robert Greenwalt9258c642014-03-26 16:47:06 -07002564 }
2565
2566 /**
Lorenzo Colitti7de289f2015-11-25 12:00:52 +09002567 * Helper function to requests a network with a particular legacy type.
2568 *
2569 * This is temporarily public @hide so it can be called by system code that uses the
2570 * NetworkRequest API to request networks but relies on CONNECTIVITY_ACTION broadcasts for
2571 * instead network notifications.
2572 *
2573 * TODO: update said system code to rely on NetworkCallbacks and make this method private.
2574 *
2575 * @hide
2576 */
Lorenzo Colittid1179462015-11-25 15:47:14 +09002577 public void requestNetwork(NetworkRequest request, NetworkCallback networkCallback,
Lorenzo Colitti7de289f2015-11-25 12:00:52 +09002578 int timeoutMs, int legacyType) {
Lorenzo Colittid1179462015-11-25 15:47:14 +09002579 sendRequestForNetwork(request.networkCapabilities, networkCallback, timeoutMs, REQUEST,
2580 legacyType);
Lorenzo Colitti7de289f2015-11-25 12:00:52 +09002581 }
2582
2583 /**
Lorenzo Colittie285b432015-04-23 15:32:42 +09002584 * Request a network to satisfy a set of {@link android.net.NetworkCapabilities}.
Robert Greenwalt9258c642014-03-26 16:47:06 -07002585 *
2586 * This {@link NetworkRequest} will live until released via
Robert Greenwalt6078b502014-06-11 16:05:07 -07002587 * {@link #unregisterNetworkCallback} or the calling application exits.
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07002588 * Status of the request can be followed by listening to the various
Robert Greenwalt6078b502014-06-11 16:05:07 -07002589 * callbacks described in {@link NetworkCallback}. The {@link Network}
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07002590 * can be used to direct traffic to the network.
Paul Jensenbb2e0e92015-06-16 15:11:58 -04002591 * <p>It is presently unsupported to request a network with mutable
2592 * {@link NetworkCapabilities} such as
2593 * {@link NetworkCapabilities#NET_CAPABILITY_VALIDATED} or
2594 * {@link NetworkCapabilities#NET_CAPABILITY_CAPTIVE_PORTAL}
2595 * as these {@code NetworkCapabilities} represent states that a particular
2596 * network may never attain, and whether a network will attain these states
2597 * is unknown prior to bringing up the network so the framework does not
2598 * know how to go about satisfing a request with these capabilities.
Lorenzo Colittid5427052015-10-15 16:29:00 +09002599 *
2600 * <p>This method requires the caller to hold either the
2601 * {@link android.Manifest.permission#CHANGE_NETWORK_STATE} permission
2602 * or the ability to modify system settings as determined by
2603 * {@link android.provider.Settings.System#canWrite}.</p>
Robert Greenwalt9258c642014-03-26 16:47:06 -07002604 *
Robert Greenwalt6078b502014-06-11 16:05:07 -07002605 * @param request {@link NetworkRequest} describing this request.
2606 * @param networkCallback The {@link NetworkCallback} to be utilized for this
2607 * request. Note the callback must not be shared - they
2608 * uniquely specify this request.
Paul Jensenbb2e0e92015-06-16 15:11:58 -04002609 * @throws IllegalArgumentException if {@code request} specifies any mutable
2610 * {@code NetworkCapabilities}.
Robert Greenwalt9258c642014-03-26 16:47:06 -07002611 */
Robert Greenwalt6078b502014-06-11 16:05:07 -07002612 public void requestNetwork(NetworkRequest request, NetworkCallback networkCallback) {
Lorenzo Colittid1179462015-11-25 15:47:14 +09002613 requestNetwork(request, networkCallback, 0,
Lorenzo Colitti7de289f2015-11-25 12:00:52 +09002614 inferLegacyTypeForNetworkCapabilities(request.networkCapabilities));
Robert Greenwalt9258c642014-03-26 16:47:06 -07002615 }
2616
2617 /**
Lorenzo Colittie285b432015-04-23 15:32:42 +09002618 * Request a network to satisfy a set of {@link android.net.NetworkCapabilities}, limited
Robert Greenwalt9258c642014-03-26 16:47:06 -07002619 * by a timeout.
2620 *
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07002621 * This function behaves identically to the non-timedout version, but if a suitable
Robert Greenwalt6078b502014-06-11 16:05:07 -07002622 * network is not found within the given time (in milliseconds) the
2623 * {@link NetworkCallback#unavailable} callback is called. The request must
Lorenzo Colitti36728a92015-11-26 11:03:17 +09002624 * still be released normally by calling {@link unregisterNetworkCallback}.
Lorenzo Colittid5427052015-10-15 16:29:00 +09002625 *
2626 * <p>This method requires the caller to hold either the
2627 * {@link android.Manifest.permission#CHANGE_NETWORK_STATE} permission
2628 * or the ability to modify system settings as determined by
2629 * {@link android.provider.Settings.System#canWrite}.</p>
2630 *
Robert Greenwalt6078b502014-06-11 16:05:07 -07002631 * @param request {@link NetworkRequest} describing this request.
2632 * @param networkCallback The callbacks to be utilized for this request. Note
2633 * the callbacks must not be shared - they uniquely specify
2634 * this request.
2635 * @param timeoutMs The time in milliseconds to attempt looking for a suitable network
2636 * before {@link NetworkCallback#unavailable} is called.
Lorenzo Colitti7de289f2015-11-25 12:00:52 +09002637 *
2638 * TODO: Make timeouts work and then unhide this method.
2639 *
Robert Greenwalt9258c642014-03-26 16:47:06 -07002640 * @hide
2641 */
Robert Greenwalt6078b502014-06-11 16:05:07 -07002642 public void requestNetwork(NetworkRequest request, NetworkCallback networkCallback,
2643 int timeoutMs) {
Lorenzo Colittid1179462015-11-25 15:47:14 +09002644 requestNetwork(request, networkCallback, timeoutMs,
Lorenzo Colitti7de289f2015-11-25 12:00:52 +09002645 inferLegacyTypeForNetworkCapabilities(request.networkCapabilities));
Robert Greenwalt9258c642014-03-26 16:47:06 -07002646 }
2647
2648 /**
Robert Greenwalt6078b502014-06-11 16:05:07 -07002649 * The maximum number of milliseconds the framework will look for a suitable network
Robert Greenwalt9258c642014-03-26 16:47:06 -07002650 * during a timeout-equiped call to {@link requestNetwork}.
2651 * {@hide}
2652 */
Robert Greenwalt6078b502014-06-11 16:05:07 -07002653 public final static int MAX_NETWORK_REQUEST_TIMEOUT_MS = 100 * 60 * 1000;
Robert Greenwalt9258c642014-03-26 16:47:06 -07002654
2655 /**
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07002656 * The lookup key for a {@link Network} object included with the intent after
Jeremy Joslin46e3ac82014-11-05 10:32:09 -08002657 * successfully finding a network for the applications request. Retrieve it with
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07002658 * {@link android.content.Intent#getParcelableExtra(String)}.
Jeremy Joslinfcde58f2015-02-11 16:51:13 -08002659 * <p>
Paul Jensen72db88e2015-03-10 10:54:12 -04002660 * Note that if you intend to invoke {@link Network#openConnection(java.net.URL)}
2661 * then you must get a ConnectivityManager instance before doing so.
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07002662 */
Erik Kline90e93072014-11-19 12:12:24 +09002663 public static final String EXTRA_NETWORK = "android.net.extra.NETWORK";
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07002664
2665 /**
Robert Greenwalt6078b502014-06-11 16:05:07 -07002666 * The lookup key for a {@link NetworkRequest} object included with the intent after
Jeremy Joslin46e3ac82014-11-05 10:32:09 -08002667 * successfully finding a network for the applications request. Retrieve it with
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07002668 * {@link android.content.Intent#getParcelableExtra(String)}.
2669 */
Erik Kline90e93072014-11-19 12:12:24 +09002670 public static final String EXTRA_NETWORK_REQUEST = "android.net.extra.NETWORK_REQUEST";
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07002671
2672
2673 /**
Lorenzo Colittie285b432015-04-23 15:32:42 +09002674 * Request a network to satisfy a set of {@link android.net.NetworkCapabilities}.
Robert Greenwalt9258c642014-03-26 16:47:06 -07002675 *
Jeremy Joslin46e3ac82014-11-05 10:32:09 -08002676 * This function behaves identically to the version that takes a NetworkCallback, but instead
Robert Greenwalt6078b502014-06-11 16:05:07 -07002677 * of {@link NetworkCallback} a {@link PendingIntent} is used. This means
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07002678 * the request may outlive the calling application and get called back when a suitable
2679 * network is found.
Robert Greenwalt9258c642014-03-26 16:47:06 -07002680 * <p>
2681 * The operation is an Intent broadcast that goes to a broadcast receiver that
2682 * you registered with {@link Context#registerReceiver} or through the
2683 * &lt;receiver&gt; tag in an AndroidManifest.xml file
2684 * <p>
2685 * The operation Intent is delivered with two extras, a {@link Network} typed
Erik Kline90e93072014-11-19 12:12:24 +09002686 * extra called {@link #EXTRA_NETWORK} and a {@link NetworkRequest}
2687 * typed extra called {@link #EXTRA_NETWORK_REQUEST} containing
Robert Greenwalt9258c642014-03-26 16:47:06 -07002688 * the original requests parameters. It is important to create a new,
Robert Greenwalt6078b502014-06-11 16:05:07 -07002689 * {@link NetworkCallback} based request before completing the processing of the
Robert Greenwalt9258c642014-03-26 16:47:06 -07002690 * Intent to reserve the network or it will be released shortly after the Intent
2691 * is processed.
2692 * <p>
Paul Jensen694f2b82015-06-17 14:15:39 -04002693 * If there is already a request for this Intent registered (with the equality of
Robert Greenwalt9258c642014-03-26 16:47:06 -07002694 * two Intents defined by {@link Intent#filterEquals}), then it will be removed and
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07002695 * replaced by this one, effectively releasing the previous {@link NetworkRequest}.
Robert Greenwalt9258c642014-03-26 16:47:06 -07002696 * <p>
Jeremy Joslin46e3ac82014-11-05 10:32:09 -08002697 * The request may be released normally by calling
2698 * {@link #releaseNetworkRequest(android.app.PendingIntent)}.
Paul Jensenbb2e0e92015-06-16 15:11:58 -04002699 * <p>It is presently unsupported to request a network with either
2700 * {@link NetworkCapabilities#NET_CAPABILITY_VALIDATED} or
2701 * {@link NetworkCapabilities#NET_CAPABILITY_CAPTIVE_PORTAL}
2702 * as these {@code NetworkCapabilities} represent states that a particular
2703 * network may never attain, and whether a network will attain these states
2704 * is unknown prior to bringing up the network so the framework does not
2705 * know how to go about satisfing a request with these capabilities.
Lorenzo Colittid5427052015-10-15 16:29:00 +09002706 *
2707 * <p>This method requires the caller to hold either the
2708 * {@link android.Manifest.permission#CHANGE_NETWORK_STATE} permission
2709 * or the ability to modify system settings as determined by
2710 * {@link android.provider.Settings.System#canWrite}.</p>
2711 *
Robert Greenwalt6078b502014-06-11 16:05:07 -07002712 * @param request {@link NetworkRequest} describing this request.
Robert Greenwalt9258c642014-03-26 16:47:06 -07002713 * @param operation Action to perform when the network is available (corresponds
Robert Greenwalt6078b502014-06-11 16:05:07 -07002714 * to the {@link NetworkCallback#onAvailable} call. Typically
Jeremy Joslin46e3ac82014-11-05 10:32:09 -08002715 * comes from {@link PendingIntent#getBroadcast}. Cannot be null.
Paul Jensenbb2e0e92015-06-16 15:11:58 -04002716 * @throws IllegalArgumentException if {@code request} contains either
2717 * {@link NetworkCapabilities#NET_CAPABILITY_VALIDATED} or
2718 * {@link NetworkCapabilities#NET_CAPABILITY_CAPTIVE_PORTAL}.
Robert Greenwalt9258c642014-03-26 16:47:06 -07002719 */
Robert Greenwalt6078b502014-06-11 16:05:07 -07002720 public void requestNetwork(NetworkRequest request, PendingIntent operation) {
Jeremy Joslin46e3ac82014-11-05 10:32:09 -08002721 checkPendingIntent(operation);
Robert Greenwalt9258c642014-03-26 16:47:06 -07002722 try {
Robert Greenwalt6078b502014-06-11 16:05:07 -07002723 mService.pendingRequestForNetwork(request.networkCapabilities, operation);
Robert Greenwalt9258c642014-03-26 16:47:06 -07002724 } catch (RemoteException e) {}
Robert Greenwalt9258c642014-03-26 16:47:06 -07002725 }
2726
2727 /**
Jeremy Joslin46e3ac82014-11-05 10:32:09 -08002728 * Removes a request made via {@link #requestNetwork(NetworkRequest, android.app.PendingIntent)}
2729 * <p>
2730 * This method has the same behavior as {@link #unregisterNetworkCallback} with respect to
2731 * releasing network resources and disconnecting.
2732 *
2733 * @param operation A PendingIntent equal (as defined by {@link Intent#filterEquals}) to the
2734 * PendingIntent passed to
2735 * {@link #requestNetwork(NetworkRequest, android.app.PendingIntent)} with the
2736 * corresponding NetworkRequest you'd like to remove. Cannot be null.
2737 */
2738 public void releaseNetworkRequest(PendingIntent operation) {
2739 checkPendingIntent(operation);
2740 try {
2741 mService.releasePendingNetworkRequest(operation);
2742 } catch (RemoteException e) {}
2743 }
2744
2745 private void checkPendingIntent(PendingIntent intent) {
2746 if (intent == null) {
2747 throw new IllegalArgumentException("PendingIntent cannot be null.");
2748 }
2749 }
2750
2751 /**
Robert Greenwalt9258c642014-03-26 16:47:06 -07002752 * Registers to receive notifications about all networks which satisfy the given
Robert Greenwalt6078b502014-06-11 16:05:07 -07002753 * {@link NetworkRequest}. The callbacks will continue to be called until
2754 * either the application exits or {@link #unregisterNetworkCallback} is called
Paul Jensenb2748922015-05-06 11:10:18 -04002755 * <p>This method requires the caller to hold the permission
2756 * {@link android.Manifest.permission#ACCESS_NETWORK_STATE}.
Robert Greenwalt9258c642014-03-26 16:47:06 -07002757 *
Robert Greenwalt6078b502014-06-11 16:05:07 -07002758 * @param request {@link NetworkRequest} describing this request.
2759 * @param networkCallback The {@link NetworkCallback} that the system will call as suitable
2760 * networks change state.
Robert Greenwalt9258c642014-03-26 16:47:06 -07002761 */
Robert Greenwalt6078b502014-06-11 16:05:07 -07002762 public void registerNetworkCallback(NetworkRequest request, NetworkCallback networkCallback) {
2763 sendRequestForNetwork(request.networkCapabilities, networkCallback, 0, LISTEN, TYPE_NONE);
Robert Greenwalt9258c642014-03-26 16:47:06 -07002764 }
2765
2766 /**
Paul Jensen694f2b82015-06-17 14:15:39 -04002767 * Registers a PendingIntent to be sent when a network is available which satisfies the given
2768 * {@link NetworkRequest}.
2769 *
2770 * This function behaves identically to the version that takes a NetworkCallback, but instead
2771 * of {@link NetworkCallback} a {@link PendingIntent} is used. This means
2772 * the request may outlive the calling application and get called back when a suitable
2773 * network is found.
2774 * <p>
2775 * The operation is an Intent broadcast that goes to a broadcast receiver that
2776 * you registered with {@link Context#registerReceiver} or through the
2777 * &lt;receiver&gt; tag in an AndroidManifest.xml file
2778 * <p>
2779 * The operation Intent is delivered with two extras, a {@link Network} typed
2780 * extra called {@link #EXTRA_NETWORK} and a {@link NetworkRequest}
2781 * typed extra called {@link #EXTRA_NETWORK_REQUEST} containing
2782 * the original requests parameters.
2783 * <p>
2784 * If there is already a request for this Intent registered (with the equality of
2785 * two Intents defined by {@link Intent#filterEquals}), then it will be removed and
2786 * replaced by this one, effectively releasing the previous {@link NetworkRequest}.
2787 * <p>
2788 * The request may be released normally by calling
Paul Jensenf2c1cfe2015-06-30 14:29:18 -04002789 * {@link #unregisterNetworkCallback(android.app.PendingIntent)}.
Paul Jensen694f2b82015-06-17 14:15:39 -04002790 * <p>This method requires the caller to hold the permission
2791 * {@link android.Manifest.permission#ACCESS_NETWORK_STATE}.
2792 * @param request {@link NetworkRequest} describing this request.
2793 * @param operation Action to perform when the network is available (corresponds
2794 * to the {@link NetworkCallback#onAvailable} call. Typically
2795 * comes from {@link PendingIntent#getBroadcast}. Cannot be null.
2796 */
2797 public void registerNetworkCallback(NetworkRequest request, PendingIntent operation) {
2798 checkPendingIntent(operation);
2799 try {
2800 mService.pendingListenForNetwork(request.networkCapabilities, operation);
2801 } catch (RemoteException e) {}
2802 }
2803
2804 /**
fengludb571472015-04-21 17:12:05 -07002805 * Requests bandwidth update for a given {@link Network} and returns whether the update request
2806 * is accepted by ConnectivityService. Once accepted, ConnectivityService will poll underlying
2807 * network connection for updated bandwidth information. The caller will be notified via
2808 * {@link ConnectivityManager.NetworkCallback} if there is an update. Notice that this
2809 * method assumes that the caller has previously called {@link #registerNetworkCallback} to
2810 * listen for network changes.
fenglub15e72b2015-03-20 11:29:56 -07002811 *
fengluae519192015-04-27 14:28:04 -07002812 * @param network {@link Network} specifying which network you're interested.
fengludb571472015-04-21 17:12:05 -07002813 * @return {@code true} on success, {@code false} if the {@link Network} is no longer valid.
fenglub15e72b2015-03-20 11:29:56 -07002814 */
fengludb571472015-04-21 17:12:05 -07002815 public boolean requestBandwidthUpdate(Network network) {
fenglub15e72b2015-03-20 11:29:56 -07002816 try {
fengludb571472015-04-21 17:12:05 -07002817 return mService.requestBandwidthUpdate(network);
fenglub15e72b2015-03-20 11:29:56 -07002818 } catch (RemoteException e) {
2819 return false;
2820 }
2821 }
2822
2823 /**
Robert Greenwalt6078b502014-06-11 16:05:07 -07002824 * Unregisters callbacks about and possibly releases networks originating from
Lorenzo Colitti2ea89e52015-04-24 17:03:31 +09002825 * {@link #requestNetwork(NetworkRequest, NetworkCallback)} and {@link #registerNetworkCallback}
2826 * calls. If the given {@code NetworkCallback} had previously been used with
2827 * {@code #requestNetwork}, any networks that had been connected to only to satisfy that request
2828 * will be disconnected.
Robert Greenwalt9258c642014-03-26 16:47:06 -07002829 *
Robert Greenwalt6078b502014-06-11 16:05:07 -07002830 * @param networkCallback The {@link NetworkCallback} used when making the request.
Robert Greenwalt9258c642014-03-26 16:47:06 -07002831 */
Robert Greenwalt6078b502014-06-11 16:05:07 -07002832 public void unregisterNetworkCallback(NetworkCallback networkCallback) {
2833 if (networkCallback == null || networkCallback.networkRequest == null ||
2834 networkCallback.networkRequest.requestId == REQUEST_ID_UNSET) {
2835 throw new IllegalArgumentException("Invalid NetworkCallback");
2836 }
Robert Greenwalt9258c642014-03-26 16:47:06 -07002837 try {
Robert Greenwalt6078b502014-06-11 16:05:07 -07002838 mService.releaseNetworkRequest(networkCallback.networkRequest);
Robert Greenwalt9258c642014-03-26 16:47:06 -07002839 } catch (RemoteException e) {}
2840 }
Paul Jensen6d3ff9e2014-05-29 10:12:39 -04002841
2842 /**
Paul Jensenf2c1cfe2015-06-30 14:29:18 -04002843 * Unregisters a callback previously registered via
2844 * {@link #registerNetworkCallback(NetworkRequest, android.app.PendingIntent)}.
2845 *
2846 * @param operation A PendingIntent equal (as defined by {@link Intent#filterEquals}) to the
2847 * PendingIntent passed to
2848 * {@link #registerNetworkCallback(NetworkRequest, android.app.PendingIntent)}.
2849 * Cannot be null.
2850 */
2851 public void unregisterNetworkCallback(PendingIntent operation) {
2852 releaseNetworkRequest(operation);
2853 }
2854
2855 /**
Lorenzo Colittie03c3c72015-04-03 16:38:52 +09002856 * Informs the system whether it should switch to {@code network} regardless of whether it is
2857 * validated or not. If {@code accept} is true, and the network was explicitly selected by the
2858 * user (e.g., by selecting a Wi-Fi network in the Settings app), then the network will become
2859 * the system default network regardless of any other network that's currently connected. If
2860 * {@code always} is true, then the choice is remembered, so that the next time the user
2861 * connects to this network, the system will switch to it.
2862 *
2863 * <p>This method requires the caller to hold the permission
2864 * {@link android.Manifest.permission#CONNECTIVITY_INTERNAL}
2865 *
2866 * @param network The network to accept.
2867 * @param accept Whether to accept the network even if unvalidated.
2868 * @param always Whether to remember this choice in the future.
2869 *
2870 * @hide
2871 */
2872 public void setAcceptUnvalidated(Network network, boolean accept, boolean always) {
2873 try {
2874 mService.setAcceptUnvalidated(network, accept, always);
2875 } catch (RemoteException e) {}
2876 }
2877
2878 /**
Stuart Scott984dc852015-03-30 13:17:11 -07002879 * Resets all connectivity manager settings back to factory defaults.
2880 * @hide
2881 */
2882 public void factoryReset() {
Stuart Scott984dc852015-03-30 13:17:11 -07002883 try {
Stuart Scottf1fb3972015-04-02 18:00:02 -07002884 mService.factoryReset();
Stuart Scott984dc852015-03-30 13:17:11 -07002885 } catch (RemoteException e) {
Stuart Scott984dc852015-03-30 13:17:11 -07002886 }
2887 }
2888
2889 /**
Paul Jensen6d3ff9e2014-05-29 10:12:39 -04002890 * Binds the current process to {@code network}. All Sockets created in the future
2891 * (and not explicitly bound via a bound SocketFactory from
2892 * {@link Network#getSocketFactory() Network.getSocketFactory()}) will be bound to
2893 * {@code network}. All host name resolutions will be limited to {@code network} as well.
2894 * Note that if {@code network} ever disconnects, all Sockets created in this way will cease to
2895 * work and all host name resolutions will fail. This is by design so an application doesn't
2896 * accidentally use Sockets it thinks are still bound to a particular {@link Network}.
2897 * To clear binding pass {@code null} for {@code network}. Using individually bound
2898 * Sockets created by Network.getSocketFactory().createSocket() and
2899 * performing network-specific host name resolutions via
2900 * {@link Network#getAllByName Network.getAllByName} is preferred to calling
Paul Jensen72db88e2015-03-10 10:54:12 -04002901 * {@code bindProcessToNetwork}.
Paul Jensen6d3ff9e2014-05-29 10:12:39 -04002902 *
2903 * @param network The {@link Network} to bind the current process to, or {@code null} to clear
2904 * the current binding.
2905 * @return {@code true} on success, {@code false} if the {@link Network} is no longer valid.
2906 */
Paul Jensen72db88e2015-03-10 10:54:12 -04002907 public boolean bindProcessToNetwork(Network network) {
2908 // Forcing callers to call thru non-static function ensures ConnectivityManager
2909 // instantiated.
2910 return setProcessDefaultNetwork(network);
2911 }
2912
2913 /**
2914 * Binds the current process to {@code network}. All Sockets created in the future
2915 * (and not explicitly bound via a bound SocketFactory from
2916 * {@link Network#getSocketFactory() Network.getSocketFactory()}) will be bound to
2917 * {@code network}. All host name resolutions will be limited to {@code network} as well.
2918 * Note that if {@code network} ever disconnects, all Sockets created in this way will cease to
2919 * work and all host name resolutions will fail. This is by design so an application doesn't
2920 * accidentally use Sockets it thinks are still bound to a particular {@link Network}.
2921 * To clear binding pass {@code null} for {@code network}. Using individually bound
2922 * Sockets created by Network.getSocketFactory().createSocket() and
2923 * performing network-specific host name resolutions via
2924 * {@link Network#getAllByName Network.getAllByName} is preferred to calling
2925 * {@code setProcessDefaultNetwork}.
2926 *
2927 * @param network The {@link Network} to bind the current process to, or {@code null} to clear
2928 * the current binding.
2929 * @return {@code true} on success, {@code false} if the {@link Network} is no longer valid.
2930 * @deprecated This function can throw {@link IllegalStateException}. Use
2931 * {@link #bindProcessToNetwork} instead. {@code bindProcessToNetwork}
2932 * is a direct replacement.
2933 */
Paul Jensen6d3ff9e2014-05-29 10:12:39 -04002934 public static boolean setProcessDefaultNetwork(Network network) {
Paul Jensenc91b5342014-08-27 12:38:45 -04002935 int netId = (network == null) ? NETID_UNSET : network.netId;
Paul Jensen72db88e2015-03-10 10:54:12 -04002936 if (netId == NetworkUtils.getBoundNetworkForProcess()) {
Paul Jensenc91b5342014-08-27 12:38:45 -04002937 return true;
2938 }
2939 if (NetworkUtils.bindProcessToNetwork(netId)) {
Paul Jensene0bef712014-12-10 15:12:18 -05002940 // Set HTTP proxy system properties to match network.
2941 // TODO: Deprecate this static method and replace it with a non-static version.
Lorenzo Colittiec4c5552015-04-22 11:52:48 +09002942 try {
2943 Proxy.setHttpProxySystemProperty(getInstance().getDefaultProxy());
2944 } catch (SecurityException e) {
2945 // The process doesn't have ACCESS_NETWORK_STATE, so we can't fetch the proxy.
2946 Log.e(TAG, "Can't set proxy properties", e);
2947 }
Paul Jensenc91b5342014-08-27 12:38:45 -04002948 // Must flush DNS cache as new network may have different DNS resolutions.
2949 InetAddress.clearDnsCache();
2950 // Must flush socket pool as idle sockets will be bound to previous network and may
2951 // cause subsequent fetches to be performed on old network.
2952 NetworkEventDispatcher.getInstance().onNetworkConfigurationChanged();
2953 return true;
2954 } else {
2955 return false;
2956 }
Paul Jensen6d3ff9e2014-05-29 10:12:39 -04002957 }
2958
2959 /**
2960 * Returns the {@link Network} currently bound to this process via
Paul Jensen72db88e2015-03-10 10:54:12 -04002961 * {@link #bindProcessToNetwork}, or {@code null} if no {@link Network} is explicitly bound.
Paul Jensen6d3ff9e2014-05-29 10:12:39 -04002962 *
2963 * @return {@code Network} to which this process is bound, or {@code null}.
2964 */
Paul Jensen72db88e2015-03-10 10:54:12 -04002965 public Network getBoundNetworkForProcess() {
2966 // Forcing callers to call thru non-static function ensures ConnectivityManager
2967 // instantiated.
2968 return getProcessDefaultNetwork();
2969 }
2970
2971 /**
2972 * Returns the {@link Network} currently bound to this process via
2973 * {@link #bindProcessToNetwork}, or {@code null} if no {@link Network} is explicitly bound.
2974 *
2975 * @return {@code Network} to which this process is bound, or {@code null}.
2976 * @deprecated Using this function can lead to other functions throwing
2977 * {@link IllegalStateException}. Use {@link #getBoundNetworkForProcess} instead.
2978 * {@code getBoundNetworkForProcess} is a direct replacement.
2979 */
Paul Jensen6d3ff9e2014-05-29 10:12:39 -04002980 public static Network getProcessDefaultNetwork() {
Paul Jensen72db88e2015-03-10 10:54:12 -04002981 int netId = NetworkUtils.getBoundNetworkForProcess();
Paul Jensenbcc76d32014-07-11 08:17:29 -04002982 if (netId == NETID_UNSET) return null;
Paul Jensen6d3ff9e2014-05-29 10:12:39 -04002983 return new Network(netId);
2984 }
2985
Lorenzo Colittiffc42b02015-07-29 11:41:21 +09002986 private void unsupportedStartingFrom(int version) {
2987 if (Process.myUid() == Process.SYSTEM_UID) {
2988 // The getApplicationInfo() call we make below is not supported in system context, and
2989 // we want to allow the system to use these APIs anyway.
2990 return;
2991 }
2992
2993 if (mContext.getApplicationInfo().targetSdkVersion >= version) {
2994 throw new UnsupportedOperationException(
2995 "This method is not supported in target SDK version " + version + " and above");
2996 }
2997 }
2998
2999 // Checks whether the calling app can use the legacy routing API (startUsingNetworkFeature,
3000 // stopUsingNetworkFeature, requestRouteToHost), and if not throw UnsupportedOperationException.
3001 // TODO: convert the existing system users (Tethering, GpsLocationProvider) to the new APIs and
3002 // remove these exemptions. Note that this check is not secure, and apps can still access these
3003 // functions by accessing ConnectivityService directly. However, it should be clear that doing
3004 // so is unsupported and may break in the future. http://b/22728205
3005 private void checkLegacyRoutingApiAccess() {
3006 if (mContext.checkCallingOrSelfPermission("com.android.permission.INJECT_OMADM_SETTINGS")
3007 == PackageManager.PERMISSION_GRANTED) {
3008 return;
3009 }
3010
Dianne Hackborn692a2442015-07-31 10:35:34 -07003011 unsupportedStartingFrom(VERSION_CODES.M);
Lorenzo Colittiffc42b02015-07-29 11:41:21 +09003012 }
3013
Paul Jensen6d3ff9e2014-05-29 10:12:39 -04003014 /**
3015 * Binds host resolutions performed by this process to {@code network}.
Paul Jensen72db88e2015-03-10 10:54:12 -04003016 * {@link #bindProcessToNetwork} takes precedence over this setting.
Paul Jensen6d3ff9e2014-05-29 10:12:39 -04003017 *
3018 * @param network The {@link Network} to bind host resolutions from the current process to, or
3019 * {@code null} to clear the current binding.
3020 * @return {@code true} on success, {@code false} if the {@link Network} is no longer valid.
3021 * @hide
3022 * @deprecated This is strictly for legacy usage to support {@link #startUsingNetworkFeature}.
3023 */
3024 public static boolean setProcessDefaultNetworkForHostResolution(Network network) {
Paul Jensenbcc76d32014-07-11 08:17:29 -04003025 return NetworkUtils.bindProcessToNetworkForHostResolution(
3026 network == null ? NETID_UNSET : network.netId);
Paul Jensen6d3ff9e2014-05-29 10:12:39 -04003027 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003028}