blob: 10307e4b3de300ad1a14cfd2e03ca5239bada564 [file] [log] [blame]
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001/*
2 * Copyright (C) 2008 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016package android.net;
17
Jeff Sharkeyf0ceede2011-08-02 17:22:34 -070018import static com.android.internal.util.Preconditions.checkNotNull;
19
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080020import android.annotation.SdkConstant;
21import android.annotation.SdkConstant.SdkConstantType;
Robert Greenwalt9258c642014-03-26 16:47:06 -070022import android.app.PendingIntent;
Jeff Sharkey8fc27e82012-04-04 20:40:58 -070023import android.content.Context;
Robert Greenwaltd19c41c2014-05-18 23:07:25 -070024import android.content.Intent;
Lorenzo Colittiffc42b02015-07-29 11:41:21 +090025import android.content.pm.PackageManager;
Paul Jensen6d3ff9e2014-05-29 10:12:39 -040026import android.net.NetworkUtils;
Robert Greenwalt42acef32009-08-12 16:08:25 -070027import android.os.Binder;
Jeff Sharkey3a844fc2011-08-16 14:37:57 -070028import android.os.Build.VERSION_CODES;
Robert Greenwalt9258c642014-03-26 16:47:06 -070029import android.os.Handler;
30import android.os.HandlerThread;
Dianne Hackborn77b987f2014-02-26 16:20:52 -080031import android.os.IBinder;
32import android.os.INetworkActivityListener;
33import android.os.INetworkManagementService;
Robert Greenwalt9258c642014-03-26 16:47:06 -070034import android.os.Looper;
35import android.os.Message;
Robert Greenwalt665e1ae2012-08-21 19:27:00 -070036import android.os.Messenger;
Lorenzo Colittiffc42b02015-07-29 11:41:21 +090037import android.os.Process;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080038import android.os.RemoteException;
Dianne Hackborn77b987f2014-02-26 16:20:52 -080039import android.os.ServiceManager;
Jeff Sharkey961e3042011-08-29 16:02:57 -070040import android.provider.Settings;
Wink Saville36ffb042014-12-05 11:10:30 -080041import android.telephony.SubscriptionManager;
Dianne Hackborn77b987f2014-02-26 16:20:52 -080042import android.util.ArrayMap;
Robert Greenwalt9258c642014-03-26 16:47:06 -070043import android.util.Log;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080044
Robert Greenwaltafa05c02014-05-21 20:04:36 -070045import com.android.internal.telephony.ITelephony;
Robert Greenwalt562cc542014-05-15 18:07:26 -070046import com.android.internal.telephony.PhoneConstants;
Robert Greenwaltafa05c02014-05-21 20:04:36 -070047import com.android.internal.util.Protocol;
48
Robert Greenwalt585ac0f2010-08-27 09:24:29 -070049import java.net.InetAddress;
Robert Greenwalt9258c642014-03-26 16:47:06 -070050import java.util.concurrent.atomic.AtomicInteger;
51import java.util.HashMap;
52
Paul Jensenc91b5342014-08-27 12:38:45 -040053import libcore.net.event.NetworkEventDispatcher;
54
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080055/**
56 * Class that answers queries about the state of network connectivity. It also
57 * notifies applications when network connectivity changes. Get an instance
58 * of this class by calling
59 * {@link android.content.Context#getSystemService(String) Context.getSystemService(Context.CONNECTIVITY_SERVICE)}.
60 * <p>
61 * The primary responsibilities of this class are to:
62 * <ol>
63 * <li>Monitor network connections (Wi-Fi, GPRS, UMTS, etc.)</li>
64 * <li>Send broadcast intents when network connectivity changes</li>
65 * <li>Attempt to "fail over" to another network when connectivity to a network
66 * is lost</li>
67 * <li>Provide an API that allows applications to query the coarse-grained or fine-grained
68 * state of the available networks</li>
Robert Greenwaltd19c41c2014-05-18 23:07:25 -070069 * <li>Provide an API that allows applications to request and select networks for their data
70 * traffic</li>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080071 * </ol>
72 */
Jeff Sharkeyc006f1a2011-05-19 17:12:49 -070073public class ConnectivityManager {
74 private static final String TAG = "ConnectivityManager";
75
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080076 /**
Robert Greenwaltd19c41c2014-05-18 23:07:25 -070077 * A change in network connectivity has occurred. A default connection has either
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080078 * been established or lost. The NetworkInfo for the affected network is
79 * sent as an extra; it should be consulted to see what kind of
80 * connectivity event occurred.
81 * <p/>
82 * If this is a connection that was the result of failing over from a
83 * disconnected network, then the FAILOVER_CONNECTION boolean extra is
84 * set to true.
85 * <p/>
86 * For a loss of connectivity, if the connectivity manager is attempting
87 * to connect (or has already connected) to another network, the
88 * NetworkInfo for the new network is also passed as an extra. This lets
89 * any receivers of the broadcast know that they should not necessarily
90 * tell the user that no data traffic will be possible. Instead, the
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -080091 * receiver should expect another broadcast soon, indicating either that
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080092 * the failover attempt succeeded (and so there is still overall data
93 * connectivity), or that the failover attempt failed, meaning that all
94 * connectivity has been lost.
95 * <p/>
96 * For a disconnect event, the boolean extra EXTRA_NO_CONNECTIVITY
97 * is set to {@code true} if there are no connected networks at all.
98 */
Jeff Sharkey4fa63b22013-02-20 18:21:19 -080099 @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800100 public static final String CONNECTIVITY_ACTION = "android.net.conn.CONNECTIVITY_CHANGE";
Jeff Sharkeyf0ceede2011-08-02 17:22:34 -0700101
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800102 /**
Robert Greenwalte94a6ff2015-09-01 08:23:04 -0700103 * A temporary hack until SUPL system can get off the legacy APIS.
104 * They do too many network requests and the long list of apps listening
105 * and waking due to the CONNECTIVITY_ACTION bcast makes it expensive.
106 * Use this bcast intent instead for SUPL requests.
107 * @hide
108 */
109 public static final String CONNECTIVITY_ACTION_SUPL =
110 "android.net.conn.CONNECTIVITY_CHANGE_SUPL";
111
112 /**
Paul Jensen25a217c2015-02-27 22:55:47 -0500113 * The device has connected to a network that has presented a captive
114 * portal, which is blocking Internet connectivity. The user was presented
115 * with a notification that network sign in is required,
116 * and the user invoked the notification's action indicating they
Paul Jensen49e3edf2015-05-22 10:50:39 -0400117 * desire to sign in to the network. Apps handling this activity should
Paul Jensen25a217c2015-02-27 22:55:47 -0500118 * facilitate signing in to the network. This action includes a
119 * {@link Network} typed extra called {@link #EXTRA_NETWORK} that represents
120 * the network presenting the captive portal; all communication with the
121 * captive portal must be done using this {@code Network} object.
122 * <p/>
Paul Jensen49e3edf2015-05-22 10:50:39 -0400123 * This activity includes a {@link CaptivePortal} extra named
124 * {@link #EXTRA_CAPTIVE_PORTAL} that can be used to indicate different
125 * outcomes of the captive portal sign in to the system:
126 * <ul>
127 * <li> When the app handling this action believes the user has signed in to
128 * the network and the captive portal has been dismissed, the app should
129 * call {@link CaptivePortal#reportCaptivePortalDismissed} so the system can
130 * reevaluate the network. If reevaluation finds the network no longer
131 * subject to a captive portal, the network may become the default active
132 * data network. </li>
133 * <li> When the app handling this action believes the user explicitly wants
Paul Jensen25a217c2015-02-27 22:55:47 -0500134 * to ignore the captive portal and the network, the app should call
Paul Jensen49e3edf2015-05-22 10:50:39 -0400135 * {@link CaptivePortal#ignoreNetwork}. </li>
136 * </ul>
Paul Jensen25a217c2015-02-27 22:55:47 -0500137 */
138 @SdkConstant(SdkConstantType.ACTIVITY_INTENT_ACTION)
139 public static final String ACTION_CAPTIVE_PORTAL_SIGN_IN = "android.net.conn.CAPTIVE_PORTAL";
140
141 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800142 * The lookup key for a {@link NetworkInfo} object. Retrieve with
143 * {@link android.content.Intent#getParcelableExtra(String)}.
Jeff Sharkeyf0ceede2011-08-02 17:22:34 -0700144 *
145 * @deprecated Since {@link NetworkInfo} can vary based on UID, applications
146 * should always obtain network information through
Paul Jensen3541e9f2015-03-18 12:23:02 -0400147 * {@link #getActiveNetworkInfo()}.
Jeff Sharkey75fbb4b2012-08-06 11:41:50 -0700148 * @see #EXTRA_NETWORK_TYPE
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800149 */
Jeff Sharkeyf0ceede2011-08-02 17:22:34 -0700150 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800151 public static final String EXTRA_NETWORK_INFO = "networkInfo";
Jeff Sharkeyf0ceede2011-08-02 17:22:34 -0700152
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800153 /**
Jeff Sharkey75fbb4b2012-08-06 11:41:50 -0700154 * Network type which triggered a {@link #CONNECTIVITY_ACTION} broadcast.
Jeff Sharkey75fbb4b2012-08-06 11:41:50 -0700155 *
156 * @see android.content.Intent#getIntExtra(String, int)
157 */
158 public static final String EXTRA_NETWORK_TYPE = "networkType";
159
160 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800161 * The lookup key for a boolean that indicates whether a connect event
162 * is for a network to which the connectivity manager was failing over
163 * following a disconnect on another network.
164 * Retrieve it with {@link android.content.Intent#getBooleanExtra(String,boolean)}.
165 */
166 public static final String EXTRA_IS_FAILOVER = "isFailover";
167 /**
168 * The lookup key for a {@link NetworkInfo} object. This is supplied when
169 * there is another network that it may be possible to connect to. Retrieve with
170 * {@link android.content.Intent#getParcelableExtra(String)}.
171 */
172 public static final String EXTRA_OTHER_NETWORK_INFO = "otherNetwork";
173 /**
174 * The lookup key for a boolean that indicates whether there is a
175 * complete lack of connectivity, i.e., no network is available.
176 * Retrieve it with {@link android.content.Intent#getBooleanExtra(String,boolean)}.
177 */
178 public static final String EXTRA_NO_CONNECTIVITY = "noConnectivity";
179 /**
180 * The lookup key for a string that indicates why an attempt to connect
181 * to a network failed. The string has no particular structure. It is
182 * intended to be used in notifications presented to users. Retrieve
183 * it with {@link android.content.Intent#getStringExtra(String)}.
184 */
185 public static final String EXTRA_REASON = "reason";
186 /**
187 * The lookup key for a string that provides optionally supplied
188 * extra information about the network state. The information
189 * may be passed up from the lower networking layers, and its
190 * meaning may be specific to a particular network type. Retrieve
191 * it with {@link android.content.Intent#getStringExtra(String)}.
192 */
193 public static final String EXTRA_EXTRA_INFO = "extraInfo";
Robert Greenwaltd7085fc2010-09-08 15:24:47 -0700194 /**
195 * The lookup key for an int that provides information about
196 * our connection to the internet at large. 0 indicates no connection,
197 * 100 indicates a great connection. Retrieve it with
Jeff Sharkeyc006f1a2011-05-19 17:12:49 -0700198 * {@link android.content.Intent#getIntExtra(String, int)}.
Robert Greenwaltd7085fc2010-09-08 15:24:47 -0700199 * {@hide}
200 */
201 public static final String EXTRA_INET_CONDITION = "inetCondition";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800202 /**
Paul Jensen49e3edf2015-05-22 10:50:39 -0400203 * The lookup key for a {@link CaptivePortal} object included with the
204 * {@link #ACTION_CAPTIVE_PORTAL_SIGN_IN} intent. The {@code CaptivePortal}
205 * object can be used to either indicate to the system that the captive
206 * portal has been dismissed or that the user does not want to pursue
207 * signing in to captive portal. Retrieve it with
208 * {@link android.content.Intent#getParcelableExtra(String)}.
Paul Jensen25a217c2015-02-27 22:55:47 -0500209 */
Paul Jensen49e3edf2015-05-22 10:50:39 -0400210 public static final String EXTRA_CAPTIVE_PORTAL = "android.net.extra.CAPTIVE_PORTAL";
Jan Nordqvist52eb29f2015-09-22 15:54:32 -0700211
212 /**
213 * Key for passing a URL to the captive portal login activity.
214 */
215 public static final String EXTRA_CAPTIVE_PORTAL_URL = "android.net.extra.CAPTIVE_PORTAL_URL";
216
Paul Jensen25a217c2015-02-27 22:55:47 -0500217 /**
Haoyu Baidb3c8672012-06-20 14:29:57 -0700218 * Broadcast action to indicate the change of data activity status
219 * (idle or active) on a network in a recent period.
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800220 * The network becomes active when data transmission is started, or
221 * idle if there is no data transmission for a period of time.
Haoyu Baidb3c8672012-06-20 14:29:57 -0700222 * {@hide}
223 */
224 @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION)
225 public static final String ACTION_DATA_ACTIVITY_CHANGE = "android.net.conn.DATA_ACTIVITY_CHANGE";
226 /**
227 * The lookup key for an enum that indicates the network device type on which this data activity
228 * change happens.
229 * {@hide}
230 */
231 public static final String EXTRA_DEVICE_TYPE = "deviceType";
232 /**
233 * The lookup key for a boolean that indicates the device is active or not. {@code true} means
234 * it is actively sending or receiving data and {@code false} means it is idle.
235 * {@hide}
236 */
237 public static final String EXTRA_IS_ACTIVE = "isActive";
Ashish Sharma0535a9f2014-03-12 18:42:23 -0700238 /**
239 * The lookup key for a long that contains the timestamp (nanos) of the radio state change.
240 * {@hide}
241 */
242 public static final String EXTRA_REALTIME_NS = "tsNanos";
Haoyu Baidb3c8672012-06-20 14:29:57 -0700243
244 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800245 * Broadcast Action: The setting for background data usage has changed
246 * values. Use {@link #getBackgroundDataSetting()} to get the current value.
247 * <p>
248 * If an application uses the network in the background, it should listen
249 * for this broadcast and stop using the background data if the value is
Jeff Sharkeyc006f1a2011-05-19 17:12:49 -0700250 * {@code false}.
Jeff Sharkey54ee2ad2012-01-30 16:29:24 -0800251 * <p>
252 *
253 * @deprecated As of {@link VERSION_CODES#ICE_CREAM_SANDWICH}, availability
254 * of background data depends on several combined factors, and
255 * this broadcast is no longer sent. Instead, when background
256 * data is unavailable, {@link #getActiveNetworkInfo()} will now
257 * appear disconnected. During first boot after a platform
258 * upgrade, this broadcast will be sent once if
259 * {@link #getBackgroundDataSetting()} was {@code false} before
260 * the upgrade.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800261 */
262 @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION)
Jeff Sharkey54ee2ad2012-01-30 16:29:24 -0800263 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800264 public static final String ACTION_BACKGROUND_DATA_SETTING_CHANGED =
265 "android.net.conn.BACKGROUND_DATA_SETTING_CHANGED";
266
Robert Greenwalt1e9aac22010-09-15 17:36:33 -0700267 /**
268 * Broadcast Action: The network connection may not be good
269 * uses {@code ConnectivityManager.EXTRA_INET_CONDITION} and
270 * {@code ConnectivityManager.EXTRA_NETWORK_INFO} to specify
271 * the network and it's condition.
272 * @hide
273 */
Jeff Sharkey4fa63b22013-02-20 18:21:19 -0800274 @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION)
Robert Greenwalt1e9aac22010-09-15 17:36:33 -0700275 public static final String INET_CONDITION_ACTION =
276 "android.net.conn.INET_CONDITION_ACTION";
277
Robert Greenwalt42acef32009-08-12 16:08:25 -0700278 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800279 * Broadcast Action: A tetherable connection has come or gone.
280 * Uses {@code ConnectivityManager.EXTRA_AVAILABLE_TETHER},
281 * {@code ConnectivityManager.EXTRA_ACTIVE_TETHER} and
282 * {@code ConnectivityManager.EXTRA_ERRORED_TETHER} to indicate
283 * the current state of tethering. Each include a list of
284 * interface names in that state (may be empty).
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -0800285 * @hide
286 */
Jeff Sharkey4fa63b22013-02-20 18:21:19 -0800287 @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION)
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -0800288 public static final String ACTION_TETHER_STATE_CHANGED =
289 "android.net.conn.TETHER_STATE_CHANGED";
290
291 /**
292 * @hide
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800293 * gives a String[] listing all the interfaces configured for
294 * tethering and currently available for tethering.
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -0800295 */
Robert Greenwalt2a091d72010-02-11 18:18:40 -0800296 public static final String EXTRA_AVAILABLE_TETHER = "availableArray";
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -0800297
298 /**
299 * @hide
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800300 * gives a String[] listing all the interfaces currently tethered
301 * (ie, has dhcp support and packets potentially forwarded/NATed)
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -0800302 */
Robert Greenwalt2a091d72010-02-11 18:18:40 -0800303 public static final String EXTRA_ACTIVE_TETHER = "activeArray";
304
305 /**
306 * @hide
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800307 * gives a String[] listing all the interfaces we tried to tether and
308 * failed. Use {@link #getLastTetherError} to find the error code
309 * for any interfaces listed here.
Robert Greenwalt2a091d72010-02-11 18:18:40 -0800310 */
311 public static final String EXTRA_ERRORED_TETHER = "erroredArray";
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -0800312
313 /**
Russell Brenner108da0c2013-02-12 10:03:14 -0800314 * Broadcast Action: The captive portal tracker has finished its test.
315 * Sent only while running Setup Wizard, in lieu of showing a user
316 * notification.
317 * @hide
318 */
Jeff Sharkey4fa63b22013-02-20 18:21:19 -0800319 @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION)
Russell Brenner108da0c2013-02-12 10:03:14 -0800320 public static final String ACTION_CAPTIVE_PORTAL_TEST_COMPLETED =
321 "android.net.conn.CAPTIVE_PORTAL_TEST_COMPLETED";
322 /**
323 * The lookup key for a boolean that indicates whether a captive portal was detected.
324 * Retrieve it with {@link android.content.Intent#getBooleanExtra(String,boolean)}.
325 * @hide
326 */
327 public static final String EXTRA_IS_CAPTIVE_PORTAL = "captivePortal";
328
329 /**
Lorenzo Colittie03c3c72015-04-03 16:38:52 +0900330 * Action used to display a dialog that asks the user whether to connect to a network that is
331 * not validated. This intent is used to start the dialog in settings via startActivity.
332 *
333 * @hide
334 */
335 public static final String ACTION_PROMPT_UNVALIDATED = "android.net.conn.PROMPT_UNVALIDATED";
336
337 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800338 * The absence of a connection type.
Robert Greenwaltccf83af12011-06-02 17:30:47 -0700339 * @hide
340 */
341 public static final int TYPE_NONE = -1;
342
343 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800344 * The Mobile data connection. When active, all data traffic
345 * will use this network type's interface by default
346 * (it has a default route)
Robert Greenwalt42acef32009-08-12 16:08:25 -0700347 */
348 public static final int TYPE_MOBILE = 0;
349 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800350 * The WIFI data connection. When active, all data traffic
351 * will use this network type's interface by default
352 * (it has a default route).
Robert Greenwalt42acef32009-08-12 16:08:25 -0700353 */
354 public static final int TYPE_WIFI = 1;
355 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800356 * An MMS-specific Mobile data connection. This network type may use the
357 * same network interface as {@link #TYPE_MOBILE} or it may use a different
358 * one. This is used by applications needing to talk to the carrier's
359 * Multimedia Messaging Service servers.
Lorenzo Colittie285b432015-04-23 15:32:42 +0900360 *
Lorenzo Colitti2ea89e52015-04-24 17:03:31 +0900361 * @deprecated Applications should instead use
362 * {@link #requestNetwork(NetworkRequest, NetworkCallback)} to request a network that
Lorenzo Colittie285b432015-04-23 15:32:42 +0900363 * provides the {@link NetworkCapabilities#NET_CAPABILITY_MMS} capability.
Robert Greenwalt42acef32009-08-12 16:08:25 -0700364 */
365 public static final int TYPE_MOBILE_MMS = 2;
366 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800367 * A SUPL-specific Mobile data connection. This network type may use the
368 * same network interface as {@link #TYPE_MOBILE} or it may use a different
369 * one. This is used by applications needing to talk to the carrier's
370 * Secure User Plane Location servers for help locating the device.
Lorenzo Colittie285b432015-04-23 15:32:42 +0900371 *
Lorenzo Colitti2ea89e52015-04-24 17:03:31 +0900372 * @deprecated Applications should instead use
373 * {@link #requestNetwork(NetworkRequest, NetworkCallback)} to request a network that
Lorenzo Colittie285b432015-04-23 15:32:42 +0900374 * provides the {@link NetworkCapabilities#NET_CAPABILITY_SUPL} capability.
Robert Greenwalt42acef32009-08-12 16:08:25 -0700375 */
376 public static final int TYPE_MOBILE_SUPL = 3;
377 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800378 * A DUN-specific Mobile data connection. This network type may use the
379 * same network interface as {@link #TYPE_MOBILE} or it may use a different
380 * one. This is sometimes by the system when setting up an upstream connection
381 * for tethering so that the carrier is aware of DUN traffic.
Robert Greenwalt42acef32009-08-12 16:08:25 -0700382 */
383 public static final int TYPE_MOBILE_DUN = 4;
384 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800385 * A High Priority Mobile data connection. This network type uses the
386 * same network interface as {@link #TYPE_MOBILE} but the routing setup
Lorenzo Colittie285b432015-04-23 15:32:42 +0900387 * is different.
388 *
Lorenzo Colitti2ea89e52015-04-24 17:03:31 +0900389 * @deprecated Applications should instead use
390 * {@link #requestNetwork(NetworkRequest, NetworkCallback)} to request a network that
Lorenzo Colittie285b432015-04-23 15:32:42 +0900391 * uses the {@link NetworkCapabilities#TRANSPORT_CELLULAR} transport.
Robert Greenwalt42acef32009-08-12 16:08:25 -0700392 */
393 public static final int TYPE_MOBILE_HIPRI = 5;
jsh8214deb2010-03-11 15:04:43 -0800394 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800395 * The WiMAX data connection. When active, all data traffic
396 * will use this network type's interface by default
397 * (it has a default route).
jsh8214deb2010-03-11 15:04:43 -0800398 */
399 public static final int TYPE_WIMAX = 6;
Robert Greenwaltda3d5e62010-12-06 13:56:24 -0800400
Jaikumar Ganesh15c74392010-12-21 22:31:44 -0800401 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800402 * The Bluetooth data connection. When active, all data traffic
403 * will use this network type's interface by default
404 * (it has a default route).
Jaikumar Ganesh15c74392010-12-21 22:31:44 -0800405 */
406 public static final int TYPE_BLUETOOTH = 7;
407
Robert Greenwalt60810842011-04-22 15:28:18 -0700408 /**
409 * Dummy data connection. This should not be used on shipping devices.
410 */
Jaikumar Ganesh15c74392010-12-21 22:31:44 -0800411 public static final int TYPE_DUMMY = 8;
Wink Saville9d7d6282011-03-12 14:52:01 -0800412
Robert Greenwalt60810842011-04-22 15:28:18 -0700413 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800414 * The Ethernet data connection. When active, all data traffic
415 * will use this network type's interface by default
416 * (it has a default route).
Robert Greenwalt60810842011-04-22 15:28:18 -0700417 */
Robert Greenwalte12aec92011-01-28 14:48:37 -0800418 public static final int TYPE_ETHERNET = 9;
Robert Greenwalt60810842011-04-22 15:28:18 -0700419
Wink Saville9d7d6282011-03-12 14:52:01 -0800420 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800421 * Over the air Administration.
Wink Saville9d7d6282011-03-12 14:52:01 -0800422 * {@hide}
423 */
424 public static final int TYPE_MOBILE_FOTA = 10;
425
426 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800427 * IP Multimedia Subsystem.
Wink Saville9d7d6282011-03-12 14:52:01 -0800428 * {@hide}
429 */
430 public static final int TYPE_MOBILE_IMS = 11;
431
432 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800433 * Carrier Branded Services.
Wink Saville9d7d6282011-03-12 14:52:01 -0800434 * {@hide}
435 */
436 public static final int TYPE_MOBILE_CBS = 12;
437
repo syncaea743a2011-07-29 23:55:49 -0700438 /**
439 * A Wi-Fi p2p connection. Only requesting processes will have access to
440 * the peers connected.
441 * {@hide}
442 */
443 public static final int TYPE_WIFI_P2P = 13;
Wink Saville9d7d6282011-03-12 14:52:01 -0800444
Wink Saville5e56bc52013-07-29 15:00:57 -0700445 /**
446 * The network to use for initially attaching to the network
447 * {@hide}
448 */
449 public static final int TYPE_MOBILE_IA = 14;
repo syncaea743a2011-07-29 23:55:49 -0700450
Lorenzo Colittie285b432015-04-23 15:32:42 +0900451 /**
Robert Greenwalt4bd43892015-07-09 14:49:35 -0700452 * Emergency PDN connection for emergency services. This
453 * may include IMS and MMS in emergency situations.
Ram3e0e3bc2014-06-26 11:03:44 -0700454 * {@hide}
455 */
456 public static final int TYPE_MOBILE_EMERGENCY = 15;
457
Hui Lu1c5624a2014-01-15 11:05:36 -0500458 /**
459 * The network that uses proxy to achieve connectivity.
460 * {@hide}
461 */
462 public static final int TYPE_PROXY = 16;
Wink Saville5e56bc52013-07-29 15:00:57 -0700463
Robert Greenwalt8283f882014-07-07 17:09:01 -0700464 /**
465 * A virtual network using one or more native bearers.
466 * It may or may not be providing security services.
467 */
468 public static final int TYPE_VPN = 17;
Hui Lu1c5624a2014-01-15 11:05:36 -0500469
470 /** {@hide} */
Robert Greenwalt8283f882014-07-07 17:09:01 -0700471 public static final int MAX_RADIO_TYPE = TYPE_VPN;
472
473 /** {@hide} */
474 public static final int MAX_NETWORK_TYPE = TYPE_VPN;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800475
Jianzheng Zhoudcf03f32012-11-16 13:45:20 +0800476 /**
477 * If you want to set the default network preference,you can directly
478 * change the networkAttributes array in framework's config.xml.
479 *
480 * @deprecated Since we support so many more networks now, the single
481 * network default network preference can't really express
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800482 * the hierarchy. Instead, the default is defined by the
Jianzheng Zhoudcf03f32012-11-16 13:45:20 +0800483 * networkAttributes in config.xml. You can determine
Robert Greenwalt4c8b7482012-12-07 09:56:50 -0800484 * the current value by calling {@link #getNetworkPreference()}
Jianzheng Zhoudcf03f32012-11-16 13:45:20 +0800485 * from an App.
486 */
487 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800488 public static final int DEFAULT_NETWORK_PREFERENCE = TYPE_WIFI;
489
Jeff Sharkey625239a2012-09-26 22:03:49 -0700490 /**
Robert Greenwalt9ba9c582014-03-19 17:56:12 -0700491 * @hide
492 */
Robert Greenwalt7569f182014-06-08 16:42:59 -0700493 public final static int REQUEST_ID_UNSET = 0;
494
Paul Jensen5d59e782014-07-11 12:28:19 -0400495 /**
496 * A NetID indicating no Network is selected.
497 * Keep in sync with bionic/libc/dns/include/resolv_netid.h
498 * @hide
499 */
500 public static final int NETID_UNSET = 0;
501
Jeff Sharkeyc006f1a2011-05-19 17:12:49 -0700502 private final IConnectivityManager mService;
Paul Jensene0bef712014-12-10 15:12:18 -0500503 /**
504 * A kludge to facilitate static access where a Context pointer isn't available, like in the
505 * case of the static set/getProcessDefaultNetwork methods and from the Network class.
506 * TODO: Remove this after deprecating the static methods in favor of non-static methods or
507 * methods that take a Context argument.
508 */
509 private static ConnectivityManager sInstance;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800510
Lorenzo Colittiffc42b02015-07-29 11:41:21 +0900511 private final Context mContext;
512
Dianne Hackborn77b987f2014-02-26 16:20:52 -0800513 private INetworkManagementService mNMService;
514
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800515 /**
516 * Tests if a given integer represents a valid network type.
517 * @param networkType the type to be tested
518 * @return a boolean. {@code true} if the type is valid, else {@code false}
Paul Jensen9e59e122015-05-06 10:42:25 -0400519 * @deprecated All APIs accepting a network type are deprecated. There should be no need to
520 * validate a network type.
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800521 */
Jeff Sharkeyd2a45872011-05-28 20:56:34 -0700522 public static boolean isNetworkTypeValid(int networkType) {
Robert Greenwalt42acef32009-08-12 16:08:25 -0700523 return networkType >= 0 && networkType <= MAX_NETWORK_TYPE;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800524 }
525
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800526 /**
527 * Returns a non-localized string representing a given network type.
528 * ONLY used for debugging output.
529 * @param type the type needing naming
530 * @return a String for the given type, or a string version of the type ("87")
531 * if no name is known.
532 * {@hide}
533 */
Jeff Sharkeyd2a45872011-05-28 20:56:34 -0700534 public static String getNetworkTypeName(int type) {
535 switch (type) {
536 case TYPE_MOBILE:
537 return "MOBILE";
538 case TYPE_WIFI:
539 return "WIFI";
540 case TYPE_MOBILE_MMS:
541 return "MOBILE_MMS";
542 case TYPE_MOBILE_SUPL:
543 return "MOBILE_SUPL";
544 case TYPE_MOBILE_DUN:
545 return "MOBILE_DUN";
546 case TYPE_MOBILE_HIPRI:
547 return "MOBILE_HIPRI";
548 case TYPE_WIMAX:
549 return "WIMAX";
550 case TYPE_BLUETOOTH:
551 return "BLUETOOTH";
552 case TYPE_DUMMY:
553 return "DUMMY";
554 case TYPE_ETHERNET:
555 return "ETHERNET";
556 case TYPE_MOBILE_FOTA:
557 return "MOBILE_FOTA";
558 case TYPE_MOBILE_IMS:
559 return "MOBILE_IMS";
560 case TYPE_MOBILE_CBS:
561 return "MOBILE_CBS";
repo syncaea743a2011-07-29 23:55:49 -0700562 case TYPE_WIFI_P2P:
563 return "WIFI_P2P";
Wink Saville5e56bc52013-07-29 15:00:57 -0700564 case TYPE_MOBILE_IA:
565 return "MOBILE_IA";
Ram3e0e3bc2014-06-26 11:03:44 -0700566 case TYPE_MOBILE_EMERGENCY:
567 return "MOBILE_EMERGENCY";
Hui Lu1c5624a2014-01-15 11:05:36 -0500568 case TYPE_PROXY:
569 return "PROXY";
Erik Kline37fbfa12014-11-19 17:23:41 +0900570 case TYPE_VPN:
571 return "VPN";
Jeff Sharkeyd2a45872011-05-28 20:56:34 -0700572 default:
573 return Integer.toString(type);
574 }
575 }
576
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800577 /**
578 * Checks if a given type uses the cellular data connection.
579 * This should be replaced in the future by a network property.
580 * @param networkType the type to check
581 * @return a boolean - {@code true} if uses cellular network, else {@code false}
582 * {@hide}
583 */
Jeff Sharkeyd2a45872011-05-28 20:56:34 -0700584 public static boolean isNetworkTypeMobile(int networkType) {
585 switch (networkType) {
586 case TYPE_MOBILE:
587 case TYPE_MOBILE_MMS:
588 case TYPE_MOBILE_SUPL:
589 case TYPE_MOBILE_DUN:
590 case TYPE_MOBILE_HIPRI:
591 case TYPE_MOBILE_FOTA:
592 case TYPE_MOBILE_IMS:
593 case TYPE_MOBILE_CBS:
Wink Saville5e56bc52013-07-29 15:00:57 -0700594 case TYPE_MOBILE_IA:
Ram3e0e3bc2014-06-26 11:03:44 -0700595 case TYPE_MOBILE_EMERGENCY:
Jeff Sharkeyd2a45872011-05-28 20:56:34 -0700596 return true;
597 default:
598 return false;
599 }
600 }
601
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800602 /**
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -0700603 * Checks if the given network type is backed by a Wi-Fi radio.
604 *
605 * @hide
606 */
607 public static boolean isNetworkTypeWifi(int networkType) {
608 switch (networkType) {
609 case TYPE_WIFI:
610 case TYPE_WIFI_P2P:
611 return true;
612 default:
613 return false;
614 }
615 }
616
617 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800618 * Specifies the preferred network type. When the device has more
619 * than one type available the preferred network type will be used.
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800620 *
621 * @param preference the network type to prefer over all others. It is
622 * unspecified what happens to the old preferred network in the
623 * overall ordering.
Robert Greenwaltd19c41c2014-05-18 23:07:25 -0700624 * @deprecated Functionality has been removed as it no longer makes sense,
625 * with many more than two networks - we'd need an array to express
626 * preference. Instead we use dynamic network properties of
627 * the networks to describe their precedence.
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800628 */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800629 public void setNetworkPreference(int preference) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800630 }
631
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800632 /**
633 * Retrieves the current preferred network type.
Paul Jensenb2748922015-05-06 11:10:18 -0400634 * <p>This method requires the caller to hold the permission
635 * {@link android.Manifest.permission#ACCESS_NETWORK_STATE}.
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800636 *
637 * @return an integer representing the preferred network type
638 *
Robert Greenwaltd19c41c2014-05-18 23:07:25 -0700639 * @deprecated Functionality has been removed as it no longer makes sense,
640 * with many more than two networks - we'd need an array to express
641 * preference. Instead we use dynamic network properties of
642 * the networks to describe their precedence.
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800643 */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800644 public int getNetworkPreference() {
Robert Greenwaltd19c41c2014-05-18 23:07:25 -0700645 return TYPE_NONE;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800646 }
647
Scott Main671644c2011-10-06 19:02:28 -0700648 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800649 * Returns details about the currently active default data network. When
650 * connected, this network is the default route for outgoing connections.
651 * You should always check {@link NetworkInfo#isConnected()} before initiating
652 * network traffic. This may return {@code null} when there is no default
653 * network.
Paul Jensenb2748922015-05-06 11:10:18 -0400654 * <p>This method requires the caller to hold the permission
655 * {@link android.Manifest.permission#ACCESS_NETWORK_STATE}.
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800656 *
657 * @return a {@link NetworkInfo} object for the current default network
Paul Jensen0d719ca2015-02-13 14:18:39 -0500658 * or {@code null} if no default network is currently active
Jeff Sharkey9f7cbf02012-04-12 18:34:54 -0700659 */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800660 public NetworkInfo getActiveNetworkInfo() {
661 try {
662 return mService.getActiveNetworkInfo();
663 } catch (RemoteException e) {
664 return null;
665 }
666 }
667
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800668 /**
Paul Jensen31a94f42015-02-13 14:18:39 -0500669 * Returns a {@link Network} object corresponding to the currently active
670 * default data network. In the event that the current active default data
671 * network disconnects, the returned {@code Network} object will no longer
672 * be usable. This will return {@code null} when there is no default
673 * network.
Paul Jensenb2748922015-05-06 11:10:18 -0400674 * <p>This method requires the caller to hold the permission
675 * {@link android.Manifest.permission#ACCESS_NETWORK_STATE}.
Paul Jensen31a94f42015-02-13 14:18:39 -0500676 *
677 * @return a {@link Network} object for the current default network or
678 * {@code null} if no default network is currently active
Paul Jensen31a94f42015-02-13 14:18:39 -0500679 */
680 public Network getActiveNetwork() {
681 try {
682 return mService.getActiveNetwork();
683 } catch (RemoteException e) {
684 return null;
685 }
686 }
687
688 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800689 * Returns details about the currently active default data network
690 * for a given uid. This is for internal use only to avoid spying
691 * other apps.
Paul Jensenb2748922015-05-06 11:10:18 -0400692 * <p>This method requires the caller to hold the permission
693 * {@link android.Manifest.permission#CONNECTIVITY_INTERNAL}
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800694 *
695 * @return a {@link NetworkInfo} object for the current default network
696 * for the given uid or {@code null} if no default network is
697 * available for the specified uid.
698 *
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800699 * {@hide}
700 */
Jeff Sharkeyc006f1a2011-05-19 17:12:49 -0700701 public NetworkInfo getActiveNetworkInfoForUid(int uid) {
702 try {
703 return mService.getActiveNetworkInfoForUid(uid);
704 } catch (RemoteException e) {
705 return null;
706 }
707 }
708
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800709 /**
710 * Returns connection status information about a particular
711 * network type.
Paul Jensenb2748922015-05-06 11:10:18 -0400712 * <p>This method requires the caller to hold the permission
713 * {@link android.Manifest.permission#ACCESS_NETWORK_STATE}.
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800714 *
715 * @param networkType integer specifying which networkType in
716 * which you're interested.
717 * @return a {@link NetworkInfo} object for the requested
718 * network type or {@code null} if the type is not
719 * supported by the device.
720 *
Paul Jensen3541e9f2015-03-18 12:23:02 -0400721 * @deprecated This method does not support multiple connected networks
722 * of the same type. Use {@link #getAllNetworks} and
723 * {@link #getNetworkInfo(android.net.Network)} instead.
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800724 */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800725 public NetworkInfo getNetworkInfo(int networkType) {
726 try {
727 return mService.getNetworkInfo(networkType);
728 } catch (RemoteException e) {
729 return null;
730 }
731 }
732
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800733 /**
Robert Greenwalt73b6cbae2014-06-23 11:40:00 -0700734 * Returns connection status information about a particular
735 * Network.
Paul Jensenb2748922015-05-06 11:10:18 -0400736 * <p>This method requires the caller to hold the permission
737 * {@link android.Manifest.permission#ACCESS_NETWORK_STATE}.
Robert Greenwalt73b6cbae2014-06-23 11:40:00 -0700738 *
739 * @param network {@link Network} specifying which network
740 * in which you're interested.
741 * @return a {@link NetworkInfo} object for the requested
742 * network or {@code null} if the {@code Network}
743 * is not valid.
Robert Greenwalt73b6cbae2014-06-23 11:40:00 -0700744 */
745 public NetworkInfo getNetworkInfo(Network network) {
746 try {
747 return mService.getNetworkInfoForNetwork(network);
748 } catch (RemoteException e) {
749 return null;
750 }
751 }
752
753 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800754 * Returns connection status information about all network
755 * types supported by the device.
Paul Jensenb2748922015-05-06 11:10:18 -0400756 * <p>This method requires the caller to hold the permission
757 * {@link android.Manifest.permission#ACCESS_NETWORK_STATE}.
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800758 *
759 * @return an array of {@link NetworkInfo} objects. Check each
760 * {@link NetworkInfo#getType} for which type each applies.
761 *
Paul Jensen3541e9f2015-03-18 12:23:02 -0400762 * @deprecated This method does not support multiple connected networks
763 * of the same type. Use {@link #getAllNetworks} and
764 * {@link #getNetworkInfo(android.net.Network)} instead.
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800765 */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800766 public NetworkInfo[] getAllNetworkInfo() {
767 try {
768 return mService.getAllNetworkInfo();
769 } catch (RemoteException e) {
770 return null;
771 }
772 }
773
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800774 /**
Lorenzo Colittib57edc52014-08-22 17:10:50 -0700775 * Returns the {@link Network} object currently serving a given type, or
776 * null if the given type is not connected.
777 *
778 * <p>This method requires the caller to hold the permission
779 * {@link android.Manifest.permission#ACCESS_NETWORK_STATE}.
780 *
781 * @hide
Paul Jensen3541e9f2015-03-18 12:23:02 -0400782 * @deprecated This method does not support multiple connected networks
783 * of the same type. Use {@link #getAllNetworks} and
784 * {@link #getNetworkInfo(android.net.Network)} instead.
Lorenzo Colittib57edc52014-08-22 17:10:50 -0700785 */
786 public Network getNetworkForType(int networkType) {
787 try {
788 return mService.getNetworkForType(networkType);
789 } catch (RemoteException e) {
790 return null;
791 }
792 }
793
794 /**
Robert Greenwalt73b6cbae2014-06-23 11:40:00 -0700795 * Returns an array of all {@link Network} currently tracked by the
796 * framework.
Robert Greenwalt73b6cbae2014-06-23 11:40:00 -0700797 * <p>This method requires the caller to hold the permission
798 * {@link android.Manifest.permission#ACCESS_NETWORK_STATE}.
Paul Jensenb2748922015-05-06 11:10:18 -0400799 *
800 * @return an array of {@link Network} objects.
Robert Greenwalt73b6cbae2014-06-23 11:40:00 -0700801 */
802 public Network[] getAllNetworks() {
803 try {
804 return mService.getAllNetworks();
805 } catch (RemoteException e) {
806 return null;
807 }
808 }
809
810 /**
Lorenzo Colittie285b432015-04-23 15:32:42 +0900811 * Returns an array of {@link android.net.NetworkCapabilities} objects, representing
Lorenzo Colitti403aa262014-11-28 11:21:30 +0900812 * the Networks that applications run by the given user will use by default.
813 * @hide
814 */
815 public NetworkCapabilities[] getDefaultNetworkCapabilitiesForUser(int userId) {
816 try {
817 return mService.getDefaultNetworkCapabilitiesForUser(userId);
818 } catch (RemoteException e) {
819 return null;
820 }
821 }
822
823 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800824 * Returns the IP information for the current default network.
Paul Jensenb2748922015-05-06 11:10:18 -0400825 * <p>This method requires the caller to hold the permission
826 * {@link android.Manifest.permission#ACCESS_NETWORK_STATE}.
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800827 *
828 * @return a {@link LinkProperties} object describing the IP info
829 * for the current default network, or {@code null} if there
830 * is no current default network.
831 *
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800832 * {@hide}
833 */
Robert Greenwaltd192dad2010-09-14 09:18:02 -0700834 public LinkProperties getActiveLinkProperties() {
835 try {
836 return mService.getActiveLinkProperties();
837 } catch (RemoteException e) {
838 return null;
839 }
840 }
841
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800842 /**
843 * Returns the IP information for a given network type.
Paul Jensenb2748922015-05-06 11:10:18 -0400844 * <p>This method requires the caller to hold the permission
845 * {@link android.Manifest.permission#ACCESS_NETWORK_STATE}.
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800846 *
847 * @param networkType the network type of interest.
848 * @return a {@link LinkProperties} object describing the IP info
849 * for the given networkType, or {@code null} if there is
850 * no current default network.
851 *
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800852 * {@hide}
Paul Jensen3541e9f2015-03-18 12:23:02 -0400853 * @deprecated This method does not support multiple connected networks
854 * of the same type. Use {@link #getAllNetworks},
855 * {@link #getNetworkInfo(android.net.Network)}, and
856 * {@link #getLinkProperties(android.net.Network)} instead.
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800857 */
Robert Greenwaltd192dad2010-09-14 09:18:02 -0700858 public LinkProperties getLinkProperties(int networkType) {
859 try {
Robert Greenwalt9258c642014-03-26 16:47:06 -0700860 return mService.getLinkPropertiesForType(networkType);
861 } catch (RemoteException e) {
862 return null;
863 }
864 }
865
Robert Greenwaltd19c41c2014-05-18 23:07:25 -0700866 /**
867 * Get the {@link LinkProperties} for the given {@link Network}. This
868 * will return {@code null} if the network is unknown.
Paul Jensenb2748922015-05-06 11:10:18 -0400869 * <p>This method requires the caller to hold the permission
870 * {@link android.Manifest.permission#ACCESS_NETWORK_STATE}.
Robert Greenwaltd19c41c2014-05-18 23:07:25 -0700871 *
872 * @param network The {@link Network} object identifying the network in question.
873 * @return The {@link LinkProperties} for the network, or {@code null}.
Paul Jensenb2748922015-05-06 11:10:18 -0400874 */
Robert Greenwalt9258c642014-03-26 16:47:06 -0700875 public LinkProperties getLinkProperties(Network network) {
876 try {
877 return mService.getLinkProperties(network);
878 } catch (RemoteException e) {
879 return null;
880 }
881 }
882
Robert Greenwaltd19c41c2014-05-18 23:07:25 -0700883 /**
Lorenzo Colittie285b432015-04-23 15:32:42 +0900884 * Get the {@link android.net.NetworkCapabilities} for the given {@link Network}. This
Robert Greenwaltd19c41c2014-05-18 23:07:25 -0700885 * will return {@code null} if the network is unknown.
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 Greenwaltd19c41c2014-05-18 23:07:25 -0700888 *
889 * @param network The {@link Network} object identifying the network in question.
Lorenzo Colittie285b432015-04-23 15:32:42 +0900890 * @return The {@link android.net.NetworkCapabilities} for the network, or {@code null}.
Robert Greenwaltd19c41c2014-05-18 23:07:25 -0700891 */
Robert Greenwalt9258c642014-03-26 16:47:06 -0700892 public NetworkCapabilities getNetworkCapabilities(Network network) {
893 try {
894 return mService.getNetworkCapabilities(network);
Robert Greenwaltd192dad2010-09-14 09:18:02 -0700895 } catch (RemoteException e) {
896 return null;
897 }
898 }
899
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800900 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800901 * Tells the underlying networking system that the caller wants to
902 * begin using the named feature. The interpretation of {@code feature}
903 * is completely up to each networking implementation.
Lorenzo Colittid5427052015-10-15 16:29:00 +0900904 *
905 * <p>This method requires the caller to hold either the
906 * {@link android.Manifest.permission#CHANGE_NETWORK_STATE} permission
907 * or the ability to modify system settings as determined by
908 * {@link android.provider.Settings.System#canWrite}.</p>
909 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800910 * @param networkType specifies which network the request pertains to
911 * @param feature the name of the feature to be used
912 * @return an integer value representing the outcome of the request.
913 * The interpretation of this value is specific to each networking
914 * implementation+feature combination, except that the value {@code -1}
915 * always indicates failure.
Robert Greenwaltd19c41c2014-05-18 23:07:25 -0700916 *
Lorenzo Colitti2ea89e52015-04-24 17:03:31 +0900917 * @deprecated Deprecated in favor of the cleaner
918 * {@link #requestNetwork(NetworkRequest, NetworkCallback)} API.
Dianne Hackborn692a2442015-07-31 10:35:34 -0700919 * In {@link VERSION_CODES#M}, and above, this method is unsupported and will
Lorenzo Colittiffc42b02015-07-29 11:41:21 +0900920 * throw {@code UnsupportedOperationException} if called.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800921 */
922 public int startUsingNetworkFeature(int networkType, String feature) {
Lorenzo Colittiffc42b02015-07-29 11:41:21 +0900923 checkLegacyRoutingApiAccess();
Robert Greenwalt562cc542014-05-15 18:07:26 -0700924 NetworkCapabilities netCap = networkCapabilitiesForFeature(networkType, feature);
925 if (netCap == null) {
926 Log.d(TAG, "Can't satisfy startUsingNetworkFeature for " + networkType + ", " +
927 feature);
928 return PhoneConstants.APN_REQUEST_FAILED;
929 }
930
931 NetworkRequest request = null;
932 synchronized (sLegacyRequests) {
933 LegacyRequest l = sLegacyRequests.get(netCap);
934 if (l != null) {
935 Log.d(TAG, "renewing startUsingNetworkFeature request " + l.networkRequest);
936 renewRequestLocked(l);
937 if (l.currentNetwork != null) {
938 return PhoneConstants.APN_ALREADY_ACTIVE;
939 } else {
940 return PhoneConstants.APN_REQUEST_STARTED;
941 }
942 }
943
944 request = requestNetworkForFeatureLocked(netCap);
945 }
946 if (request != null) {
Robert Greenwalt257ee5f2014-06-20 10:58:45 -0700947 Log.d(TAG, "starting startUsingNetworkFeature for request " + request);
Robert Greenwalt562cc542014-05-15 18:07:26 -0700948 return PhoneConstants.APN_REQUEST_STARTED;
949 } else {
950 Log.d(TAG, " request Failed");
951 return PhoneConstants.APN_REQUEST_FAILED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800952 }
953 }
954
955 /**
956 * Tells the underlying networking system that the caller is finished
957 * using the named feature. The interpretation of {@code feature}
958 * is completely up to each networking implementation.
Lorenzo Colittid5427052015-10-15 16:29:00 +0900959 *
960 * <p>This method requires the caller to hold either the
961 * {@link android.Manifest.permission#CHANGE_NETWORK_STATE} permission
962 * or the ability to modify system settings as determined by
963 * {@link android.provider.Settings.System#canWrite}.</p>
964 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800965 * @param networkType specifies which network the request pertains to
966 * @param feature the name of the feature that is no longer needed
967 * @return an integer value representing the outcome of the request.
968 * The interpretation of this value is specific to each networking
969 * implementation+feature combination, except that the value {@code -1}
970 * always indicates failure.
Robert Greenwaltd19c41c2014-05-18 23:07:25 -0700971 *
Robert Greenwalta36c0742015-07-28 11:41:31 -0700972 * @deprecated Deprecated in favor of the cleaner {@link #unregisterNetworkCallback} API.
Dianne Hackborn692a2442015-07-31 10:35:34 -0700973 * In {@link VERSION_CODES#M}, and above, this method is unsupported and will
Lorenzo Colittiffc42b02015-07-29 11:41:21 +0900974 * throw {@code UnsupportedOperationException} if called.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800975 */
976 public int stopUsingNetworkFeature(int networkType, String feature) {
Lorenzo Colittiffc42b02015-07-29 11:41:21 +0900977 checkLegacyRoutingApiAccess();
Robert Greenwalt562cc542014-05-15 18:07:26 -0700978 NetworkCapabilities netCap = networkCapabilitiesForFeature(networkType, feature);
979 if (netCap == null) {
980 Log.d(TAG, "Can't satisfy stopUsingNetworkFeature for " + networkType + ", " +
981 feature);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800982 return -1;
983 }
Robert Greenwalt562cc542014-05-15 18:07:26 -0700984
Paul Jensen9ffb53c2014-12-17 10:39:34 -0500985 if (removeRequestForFeature(netCap)) {
Robert Greenwalt562cc542014-05-15 18:07:26 -0700986 Log.d(TAG, "stopUsingNetworkFeature for " + networkType + ", " + feature);
Robert Greenwalt562cc542014-05-15 18:07:26 -0700987 }
988 return 1;
989 }
990
991 private NetworkCapabilities networkCapabilitiesForFeature(int networkType, String feature) {
992 if (networkType == TYPE_MOBILE) {
993 int cap = -1;
994 if ("enableMMS".equals(feature)) {
995 cap = NetworkCapabilities.NET_CAPABILITY_MMS;
996 } else if ("enableSUPL".equals(feature)) {
997 cap = NetworkCapabilities.NET_CAPABILITY_SUPL;
998 } else if ("enableDUN".equals(feature) || "enableDUNAlways".equals(feature)) {
999 cap = NetworkCapabilities.NET_CAPABILITY_DUN;
1000 } else if ("enableHIPRI".equals(feature)) {
1001 cap = NetworkCapabilities.NET_CAPABILITY_INTERNET;
1002 } else if ("enableFOTA".equals(feature)) {
1003 cap = NetworkCapabilities.NET_CAPABILITY_FOTA;
1004 } else if ("enableIMS".equals(feature)) {
1005 cap = NetworkCapabilities.NET_CAPABILITY_IMS;
1006 } else if ("enableCBS".equals(feature)) {
1007 cap = NetworkCapabilities.NET_CAPABILITY_CBS;
1008 } else {
1009 return null;
1010 }
1011 NetworkCapabilities netCap = new NetworkCapabilities();
Robert Greenwalt7569f182014-06-08 16:42:59 -07001012 netCap.addTransportType(NetworkCapabilities.TRANSPORT_CELLULAR).addCapability(cap);
Paul Jensen487ffe72015-07-24 15:57:11 -04001013 netCap.maybeMarkCapabilitiesRestricted();
Robert Greenwalt562cc542014-05-15 18:07:26 -07001014 return netCap;
1015 } else if (networkType == TYPE_WIFI) {
1016 if ("p2p".equals(feature)) {
1017 NetworkCapabilities netCap = new NetworkCapabilities();
1018 netCap.addTransportType(NetworkCapabilities.TRANSPORT_WIFI);
Robert Greenwalt7569f182014-06-08 16:42:59 -07001019 netCap.addCapability(NetworkCapabilities.NET_CAPABILITY_WIFI_P2P);
Paul Jensen487ffe72015-07-24 15:57:11 -04001020 netCap.maybeMarkCapabilitiesRestricted();
Robert Greenwalt562cc542014-05-15 18:07:26 -07001021 return netCap;
1022 }
1023 }
1024 return null;
1025 }
1026
Robert Greenwalt06314e42014-10-29 14:04:06 -07001027 /**
1028 * Guess what the network request was trying to say so that the resulting
1029 * network is accessible via the legacy (deprecated) API such as
1030 * requestRouteToHost.
1031 * This means we should try to be fairly preceise about transport and
1032 * capability but ignore things such as networkSpecifier.
1033 * If the request has more than one transport or capability it doesn't
1034 * match the old legacy requests (they selected only single transport/capability)
1035 * so this function cannot map the request to a single legacy type and
1036 * the resulting network will not be available to the legacy APIs.
1037 *
1038 * TODO - This should be removed when the legacy APIs are removed.
1039 */
Ye Wenb87875e2014-07-21 14:19:01 -07001040 private int inferLegacyTypeForNetworkCapabilities(NetworkCapabilities netCap) {
1041 if (netCap == null) {
1042 return TYPE_NONE;
1043 }
Robert Greenwalt06314e42014-10-29 14:04:06 -07001044
Ye Wenb87875e2014-07-21 14:19:01 -07001045 if (!netCap.hasTransport(NetworkCapabilities.TRANSPORT_CELLULAR)) {
1046 return TYPE_NONE;
1047 }
Robert Greenwalt06314e42014-10-29 14:04:06 -07001048
1049 String type = null;
1050 int result = TYPE_NONE;
1051
Ye Wenb87875e2014-07-21 14:19:01 -07001052 if (netCap.hasCapability(NetworkCapabilities.NET_CAPABILITY_CBS)) {
Robert Greenwalt06314e42014-10-29 14:04:06 -07001053 type = "enableCBS";
1054 result = TYPE_MOBILE_CBS;
1055 } else if (netCap.hasCapability(NetworkCapabilities.NET_CAPABILITY_IMS)) {
1056 type = "enableIMS";
1057 result = TYPE_MOBILE_IMS;
1058 } else if (netCap.hasCapability(NetworkCapabilities.NET_CAPABILITY_FOTA)) {
1059 type = "enableFOTA";
1060 result = TYPE_MOBILE_FOTA;
1061 } else if (netCap.hasCapability(NetworkCapabilities.NET_CAPABILITY_DUN)) {
1062 type = "enableDUN";
1063 result = TYPE_MOBILE_DUN;
1064 } else if (netCap.hasCapability(NetworkCapabilities.NET_CAPABILITY_SUPL)) {
Robert Greenwalt74ab4fa2015-08-28 12:37:54 -07001065 type = "enableSUPL";
Robert Greenwalt06314e42014-10-29 14:04:06 -07001066 result = TYPE_MOBILE_SUPL;
Robert Greenwalt74ab4fa2015-08-28 12:37:54 -07001067 // back out this hack for mms as they no longer need this and it's causing
1068 // device slowdowns - b/23350688 (note, supl still needs this)
1069 //} else if (netCap.hasCapability(NetworkCapabilities.NET_CAPABILITY_MMS)) {
1070 // type = "enableMMS";
1071 // result = TYPE_MOBILE_MMS;
Robert Greenwalt06314e42014-10-29 14:04:06 -07001072 } else if (netCap.hasCapability(NetworkCapabilities.NET_CAPABILITY_INTERNET)) {
1073 type = "enableHIPRI";
1074 result = TYPE_MOBILE_HIPRI;
Ye Wenb87875e2014-07-21 14:19:01 -07001075 }
Robert Greenwalt06314e42014-10-29 14:04:06 -07001076 if (type != null) {
1077 NetworkCapabilities testCap = networkCapabilitiesForFeature(TYPE_MOBILE, type);
1078 if (testCap.equalsNetCapabilities(netCap) && testCap.equalsTransportTypes(netCap)) {
1079 return result;
Ye Wenb87875e2014-07-21 14:19:01 -07001080 }
1081 }
1082 return TYPE_NONE;
1083 }
1084
Robert Greenwalt32aa65a2014-06-02 15:32:02 -07001085 private int legacyTypeForNetworkCapabilities(NetworkCapabilities netCap) {
Robert Greenwalt562cc542014-05-15 18:07:26 -07001086 if (netCap == null) return TYPE_NONE;
1087 if (netCap.hasCapability(NetworkCapabilities.NET_CAPABILITY_CBS)) {
1088 return TYPE_MOBILE_CBS;
1089 }
1090 if (netCap.hasCapability(NetworkCapabilities.NET_CAPABILITY_IMS)) {
1091 return TYPE_MOBILE_IMS;
1092 }
1093 if (netCap.hasCapability(NetworkCapabilities.NET_CAPABILITY_FOTA)) {
1094 return TYPE_MOBILE_FOTA;
1095 }
1096 if (netCap.hasCapability(NetworkCapabilities.NET_CAPABILITY_DUN)) {
1097 return TYPE_MOBILE_DUN;
1098 }
1099 if (netCap.hasCapability(NetworkCapabilities.NET_CAPABILITY_SUPL)) {
1100 return TYPE_MOBILE_SUPL;
1101 }
1102 if (netCap.hasCapability(NetworkCapabilities.NET_CAPABILITY_MMS)) {
1103 return TYPE_MOBILE_MMS;
1104 }
1105 if (netCap.hasCapability(NetworkCapabilities.NET_CAPABILITY_INTERNET)) {
1106 return TYPE_MOBILE_HIPRI;
1107 }
Robert Greenwalt32aa65a2014-06-02 15:32:02 -07001108 if (netCap.hasCapability(NetworkCapabilities.NET_CAPABILITY_WIFI_P2P)) {
1109 return TYPE_WIFI_P2P;
1110 }
Robert Greenwalt562cc542014-05-15 18:07:26 -07001111 return TYPE_NONE;
1112 }
1113
1114 private static class LegacyRequest {
1115 NetworkCapabilities networkCapabilities;
1116 NetworkRequest networkRequest;
1117 int expireSequenceNumber;
1118 Network currentNetwork;
1119 int delay = -1;
Paul Jensen9ffb53c2014-12-17 10:39:34 -05001120
1121 private void clearDnsBinding() {
1122 if (currentNetwork != null) {
1123 currentNetwork = null;
1124 setProcessDefaultNetworkForHostResolution(null);
1125 }
1126 }
1127
Robert Greenwalt6078b502014-06-11 16:05:07 -07001128 NetworkCallback networkCallback = new NetworkCallback() {
Robert Greenwalt562cc542014-05-15 18:07:26 -07001129 @Override
Robert Greenwalt6078b502014-06-11 16:05:07 -07001130 public void onAvailable(Network network) {
Robert Greenwalt562cc542014-05-15 18:07:26 -07001131 currentNetwork = network;
1132 Log.d(TAG, "startUsingNetworkFeature got Network:" + network);
Paul Jensen6d3ff9e2014-05-29 10:12:39 -04001133 setProcessDefaultNetworkForHostResolution(network);
Robert Greenwalt562cc542014-05-15 18:07:26 -07001134 }
1135 @Override
Robert Greenwalt6078b502014-06-11 16:05:07 -07001136 public void onLost(Network network) {
Paul Jensen9ffb53c2014-12-17 10:39:34 -05001137 if (network.equals(currentNetwork)) clearDnsBinding();
Robert Greenwalt562cc542014-05-15 18:07:26 -07001138 Log.d(TAG, "startUsingNetworkFeature lost Network:" + network);
1139 }
1140 };
1141 }
1142
Robert Greenwaltfab501672014-07-23 11:44:01 -07001143 private static HashMap<NetworkCapabilities, LegacyRequest> sLegacyRequests =
Robert Greenwalt562cc542014-05-15 18:07:26 -07001144 new HashMap<NetworkCapabilities, LegacyRequest>();
1145
1146 private NetworkRequest findRequestForFeature(NetworkCapabilities netCap) {
1147 synchronized (sLegacyRequests) {
1148 LegacyRequest l = sLegacyRequests.get(netCap);
1149 if (l != null) return l.networkRequest;
1150 }
1151 return null;
1152 }
1153
1154 private void renewRequestLocked(LegacyRequest l) {
1155 l.expireSequenceNumber++;
1156 Log.d(TAG, "renewing request to seqNum " + l.expireSequenceNumber);
1157 sendExpireMsgForFeature(l.networkCapabilities, l.expireSequenceNumber, l.delay);
1158 }
1159
1160 private void expireRequest(NetworkCapabilities netCap, int sequenceNum) {
1161 int ourSeqNum = -1;
1162 synchronized (sLegacyRequests) {
1163 LegacyRequest l = sLegacyRequests.get(netCap);
1164 if (l == null) return;
1165 ourSeqNum = l.expireSequenceNumber;
Paul Jensen9ffb53c2014-12-17 10:39:34 -05001166 if (l.expireSequenceNumber == sequenceNum) removeRequestForFeature(netCap);
Robert Greenwalt562cc542014-05-15 18:07:26 -07001167 }
1168 Log.d(TAG, "expireRequest with " + ourSeqNum + ", " + sequenceNum);
1169 }
1170
1171 private NetworkRequest requestNetworkForFeatureLocked(NetworkCapabilities netCap) {
1172 int delay = -1;
Robert Greenwalt32aa65a2014-06-02 15:32:02 -07001173 int type = legacyTypeForNetworkCapabilities(netCap);
Robert Greenwalt562cc542014-05-15 18:07:26 -07001174 try {
1175 delay = mService.getRestoreDefaultNetworkDelay(type);
1176 } catch (RemoteException e) {}
1177 LegacyRequest l = new LegacyRequest();
1178 l.networkCapabilities = netCap;
1179 l.delay = delay;
1180 l.expireSequenceNumber = 0;
Robert Greenwalt6078b502014-06-11 16:05:07 -07001181 l.networkRequest = sendRequestForNetwork(netCap, l.networkCallback, 0,
Robert Greenwalt32aa65a2014-06-02 15:32:02 -07001182 REQUEST, type);
Robert Greenwalt562cc542014-05-15 18:07:26 -07001183 if (l.networkRequest == null) return null;
1184 sLegacyRequests.put(netCap, l);
1185 sendExpireMsgForFeature(netCap, l.expireSequenceNumber, delay);
1186 return l.networkRequest;
1187 }
1188
1189 private void sendExpireMsgForFeature(NetworkCapabilities netCap, int seqNum, int delay) {
1190 if (delay >= 0) {
1191 Log.d(TAG, "sending expire msg with seqNum " + seqNum + " and delay " + delay);
1192 Message msg = sCallbackHandler.obtainMessage(EXPIRE_LEGACY_REQUEST, seqNum, 0, netCap);
1193 sCallbackHandler.sendMessageDelayed(msg, delay);
1194 }
1195 }
1196
Paul Jensen9ffb53c2014-12-17 10:39:34 -05001197 private boolean removeRequestForFeature(NetworkCapabilities netCap) {
1198 final LegacyRequest l;
Robert Greenwalt562cc542014-05-15 18:07:26 -07001199 synchronized (sLegacyRequests) {
Paul Jensen9ffb53c2014-12-17 10:39:34 -05001200 l = sLegacyRequests.remove(netCap);
Robert Greenwalt562cc542014-05-15 18:07:26 -07001201 }
Paul Jensen9ffb53c2014-12-17 10:39:34 -05001202 if (l == null) return false;
1203 unregisterNetworkCallback(l.networkCallback);
1204 l.clearDnsBinding();
1205 return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001206 }
1207
Lorenzo Colitti8bf977d2015-06-15 14:29:22 +09001208 /** @hide */
1209 public static class PacketKeepaliveCallback {
1210 /** The requested keepalive was successfully started. */
1211 public void onStarted() {}
1212 /** The keepalive was successfully stopped. */
1213 public void onStopped() {}
1214 /** An error occurred. */
1215 public void onError(int error) {}
1216 }
1217
1218 /**
1219 * Allows applications to request that the system periodically send specific packets on their
1220 * behalf, using hardware offload to save battery power.
1221 *
1222 * To request that the system send keepalives, call one of the methods that return a
1223 * {@link ConnectivityManager.PacketKeepalive} object, such as {@link #startNattKeepalive},
1224 * passing in a non-null callback. If the callback is successfully started, the callback's
1225 * {@code onStarted} method will be called. If an error occurs, {@code onError} will be called,
1226 * specifying one of the {@code ERROR_*} constants in this class.
1227 *
1228 * To stop an existing keepalive, call {@link stop}. The system will call {@code onStopped} if
1229 * the operation was successfull or {@code onError} if an error occurred.
1230 *
1231 * @hide
1232 */
1233 public class PacketKeepalive {
1234
1235 private static final String TAG = "PacketKeepalive";
1236
1237 /** @hide */
1238 public static final int SUCCESS = 0;
1239
1240 /** @hide */
1241 public static final int NO_KEEPALIVE = -1;
1242
1243 /** @hide */
1244 public static final int BINDER_DIED = -10;
1245
1246 /** The specified {@code Network} is not connected. */
1247 public static final int ERROR_INVALID_NETWORK = -20;
1248 /** The specified IP addresses are invalid. For example, the specified source IP address is
1249 * not configured on the specified {@code Network}. */
1250 public static final int ERROR_INVALID_IP_ADDRESS = -21;
1251 /** The requested port is invalid. */
1252 public static final int ERROR_INVALID_PORT = -22;
1253 /** The packet length is invalid (e.g., too long). */
1254 public static final int ERROR_INVALID_LENGTH = -23;
1255 /** The packet transmission interval is invalid (e.g., too short). */
1256 public static final int ERROR_INVALID_INTERVAL = -24;
1257
1258 /** The hardware does not support this request. */
1259 public static final int ERROR_HARDWARE_UNSUPPORTED = -30;
Lorenzo Colitti9d1284e2015-09-08 16:46:36 +09001260 /** The hardware returned an error. */
1261 public static final int ERROR_HARDWARE_ERROR = -31;
Lorenzo Colitti8bf977d2015-06-15 14:29:22 +09001262
1263 public static final int NATT_PORT = 4500;
1264
1265 private final Network mNetwork;
1266 private final PacketKeepaliveCallback mCallback;
1267 private final Looper mLooper;
1268 private final Messenger mMessenger;
1269
1270 private volatile Integer mSlot;
1271
1272 void stopLooper() {
1273 mLooper.quit();
1274 }
1275
1276 public void stop() {
1277 try {
1278 mService.stopKeepalive(mNetwork, mSlot);
1279 } catch (RemoteException e) {
1280 Log.e(TAG, "Error stopping packet keepalive: ", e);
1281 stopLooper();
1282 }
1283 }
1284
1285 private PacketKeepalive(Network network, PacketKeepaliveCallback callback) {
1286 checkNotNull(network, "network cannot be null");
1287 checkNotNull(callback, "callback cannot be null");
1288 mNetwork = network;
1289 mCallback = callback;
1290 HandlerThread thread = new HandlerThread(TAG);
1291 thread.start();
1292 mLooper = thread.getLooper();
1293 mMessenger = new Messenger(new Handler(mLooper) {
1294 @Override
1295 public void handleMessage(Message message) {
1296 switch (message.what) {
1297 case NetworkAgent.EVENT_PACKET_KEEPALIVE:
1298 int error = message.arg2;
1299 try {
1300 if (error == SUCCESS) {
1301 if (mSlot == null) {
1302 mSlot = message.arg1;
1303 mCallback.onStarted();
1304 } else {
1305 mSlot = null;
1306 stopLooper();
1307 mCallback.onStopped();
1308 }
1309 } else {
1310 stopLooper();
1311 mCallback.onError(error);
1312 }
1313 } catch (Exception e) {
1314 Log.e(TAG, "Exception in keepalive callback(" + error + ")", e);
1315 }
1316 break;
1317 default:
1318 Log.e(TAG, "Unhandled message " + Integer.toHexString(message.what));
1319 break;
1320 }
1321 }
1322 });
1323 }
1324 }
1325
1326 /**
1327 * Starts an IPsec NAT-T keepalive packet with the specified parameters.
1328 *
1329 * @hide
1330 */
1331 public PacketKeepalive startNattKeepalive(
1332 Network network, int intervalSeconds, PacketKeepaliveCallback callback,
1333 InetAddress srcAddr, int srcPort, InetAddress dstAddr) {
1334 final PacketKeepalive k = new PacketKeepalive(network, callback);
1335 try {
1336 mService.startNattKeepalive(network, intervalSeconds, k.mMessenger, new Binder(),
1337 srcAddr.getHostAddress(), srcPort, dstAddr.getHostAddress());
1338 } catch (RemoteException e) {
1339 Log.e(TAG, "Error starting packet keepalive: ", e);
1340 k.stopLooper();
1341 return null;
1342 }
1343 return k;
1344 }
1345
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001346 /**
1347 * Ensure that a network route exists to deliver traffic to the specified
1348 * host via the specified network interface. An attempt to add a route that
1349 * already exists is ignored, but treated as successful.
Lorenzo Colittid5427052015-10-15 16:29:00 +09001350 *
1351 * <p>This method requires the caller to hold either the
1352 * {@link android.Manifest.permission#CHANGE_NETWORK_STATE} permission
1353 * or the ability to modify system settings as determined by
1354 * {@link android.provider.Settings.System#canWrite}.</p>
1355 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001356 * @param networkType the type of the network over which traffic to the specified
1357 * host is to be routed
1358 * @param hostAddress the IP address of the host to which the route is desired
1359 * @return {@code true} on success, {@code false} on failure
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07001360 *
Lorenzo Colitti2ea89e52015-04-24 17:03:31 +09001361 * @deprecated Deprecated in favor of the
1362 * {@link #requestNetwork(NetworkRequest, NetworkCallback)},
1363 * {@link #bindProcessToNetwork} and {@link Network#getSocketFactory} API.
Dianne Hackborn692a2442015-07-31 10:35:34 -07001364 * In {@link VERSION_CODES#M}, and above, this method is unsupported and will
Lorenzo Colittiffc42b02015-07-29 11:41:21 +09001365 * throw {@code UnsupportedOperationException} if called.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001366 */
1367 public boolean requestRouteToHost(int networkType, int hostAddress) {
Sreeram Ramachandran03666c72014-07-19 23:21:46 -07001368 return requestRouteToHostAddress(networkType, NetworkUtils.intToInetAddress(hostAddress));
Robert Greenwalt585ac0f2010-08-27 09:24:29 -07001369 }
1370
1371 /**
1372 * Ensure that a network route exists to deliver traffic to the specified
1373 * host via the specified network interface. An attempt to add a route that
1374 * already exists is ignored, but treated as successful.
Lorenzo Colittid5427052015-10-15 16:29:00 +09001375 *
1376 * <p>This method requires the caller to hold either the
1377 * {@link android.Manifest.permission#CHANGE_NETWORK_STATE} permission
1378 * or the ability to modify system settings as determined by
1379 * {@link android.provider.Settings.System#canWrite}.</p>
1380 *
Robert Greenwalt585ac0f2010-08-27 09:24:29 -07001381 * @param networkType the type of the network over which traffic to the specified
1382 * host is to be routed
1383 * @param hostAddress the IP address of the host to which the route is desired
1384 * @return {@code true} on success, {@code false} on failure
1385 * @hide
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07001386 * @deprecated Deprecated in favor of the {@link #requestNetwork} and
Lorenzo Colitti2ea89e52015-04-24 17:03:31 +09001387 * {@link #bindProcessToNetwork} API.
Robert Greenwalt585ac0f2010-08-27 09:24:29 -07001388 */
1389 public boolean requestRouteToHostAddress(int networkType, InetAddress hostAddress) {
Lorenzo Colittiffc42b02015-07-29 11:41:21 +09001390 checkLegacyRoutingApiAccess();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001391 try {
Sreeram Ramachandran03666c72014-07-19 23:21:46 -07001392 return mService.requestRouteToHostAddress(networkType, hostAddress.getAddress());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001393 } catch (RemoteException e) {
1394 return false;
1395 }
1396 }
1397
1398 /**
1399 * Returns the value of the setting for background data usage. If false,
1400 * applications should not use the network if the application is not in the
1401 * foreground. Developers should respect this setting, and check the value
1402 * of this before performing any background data operations.
1403 * <p>
1404 * All applications that have background services that use the network
1405 * should listen to {@link #ACTION_BACKGROUND_DATA_SETTING_CHANGED}.
Jeff Sharkey3a844fc2011-08-16 14:37:57 -07001406 * <p>
Scott Main4cc53332011-10-06 18:32:43 -07001407 * @deprecated As of {@link VERSION_CODES#ICE_CREAM_SANDWICH}, availability of
Jeff Sharkey3a844fc2011-08-16 14:37:57 -07001408 * background data depends on several combined factors, and this method will
1409 * always return {@code true}. Instead, when background data is unavailable,
1410 * {@link #getActiveNetworkInfo()} will now appear disconnected.
Danica Chang6fdd0c62010-08-11 14:54:43 -07001411 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001412 * @return Whether background data usage is allowed.
1413 */
Jeff Sharkey3a844fc2011-08-16 14:37:57 -07001414 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001415 public boolean getBackgroundDataSetting() {
Jeff Sharkey3a844fc2011-08-16 14:37:57 -07001416 // assume that background data is allowed; final authority is
1417 // NetworkInfo which may be blocked.
1418 return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001419 }
1420
1421 /**
1422 * Sets the value of the setting for background data usage.
Robert Greenwaltc03fa502010-02-23 18:58:05 -08001423 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001424 * @param allowBackgroundData Whether an application should use data while
1425 * it is in the background.
Robert Greenwaltc03fa502010-02-23 18:58:05 -08001426 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001427 * @attr ref android.Manifest.permission#CHANGE_BACKGROUND_DATA_SETTING
1428 * @see #getBackgroundDataSetting()
1429 * @hide
1430 */
Jeff Sharkey3a844fc2011-08-16 14:37:57 -07001431 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001432 public void setBackgroundDataSetting(boolean allowBackgroundData) {
Jeff Sharkey3a844fc2011-08-16 14:37:57 -07001433 // ignored
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001434 }
Robert Greenwaltc03fa502010-02-23 18:58:05 -08001435
1436 /**
Jeff Sharkeyf0ceede2011-08-02 17:22:34 -07001437 * Return quota status for the current active network, or {@code null} if no
1438 * network is active. Quota status can change rapidly, so these values
1439 * shouldn't be cached.
Jeff Sharkey44a3e0d2011-10-06 10:50:09 -07001440 *
Paul Jensen0d719ca2015-02-13 14:18:39 -05001441 * <p>This method requires the caller to hold the permission
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08001442 * {@link android.Manifest.permission#ACCESS_NETWORK_STATE}.
1443 *
Jeff Sharkey44a3e0d2011-10-06 10:50:09 -07001444 * @hide
Jeff Sharkeyf0ceede2011-08-02 17:22:34 -07001445 */
1446 public NetworkQuotaInfo getActiveNetworkQuotaInfo() {
1447 try {
1448 return mService.getActiveNetworkQuotaInfo();
1449 } catch (RemoteException e) {
1450 return null;
1451 }
1452 }
1453
1454 /**
Robert Greenwaltc03fa502010-02-23 18:58:05 -08001455 * @hide
Robert Greenwaltafa05c02014-05-21 20:04:36 -07001456 * @deprecated Talk to TelephonyManager directly
Robert Greenwaltc03fa502010-02-23 18:58:05 -08001457 */
1458 public boolean getMobileDataEnabled() {
Robert Greenwaltafa05c02014-05-21 20:04:36 -07001459 IBinder b = ServiceManager.getService(Context.TELEPHONY_SERVICE);
1460 if (b != null) {
1461 try {
1462 ITelephony it = ITelephony.Stub.asInterface(b);
Wink Saville36ffb042014-12-05 11:10:30 -08001463 int subId = SubscriptionManager.getDefaultDataSubId();
1464 Log.d("ConnectivityManager", "getMobileDataEnabled()+ subId=" + subId);
1465 boolean retVal = it.getDataEnabled(subId);
1466 Log.d("ConnectivityManager", "getMobileDataEnabled()- subId=" + subId
1467 + " retVal=" + retVal);
1468 return retVal;
Robert Greenwaltafa05c02014-05-21 20:04:36 -07001469 } catch (RemoteException e) { }
Robert Greenwaltc03fa502010-02-23 18:58:05 -08001470 }
Wink Saville36ffb042014-12-05 11:10:30 -08001471 Log.d("ConnectivityManager", "getMobileDataEnabled()- remote exception retVal=false");
Robert Greenwaltafa05c02014-05-21 20:04:36 -07001472 return false;
Robert Greenwaltc03fa502010-02-23 18:58:05 -08001473 }
1474
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001475 /**
Robert Greenwaltb2489872014-09-04 16:44:35 -07001476 * Callback for use with {@link ConnectivityManager#addDefaultNetworkActiveListener}
Robert Greenwalt6078b502014-06-11 16:05:07 -07001477 * to find out when the system default network has gone in to a high power state.
Dianne Hackborn77b987f2014-02-26 16:20:52 -08001478 */
1479 public interface OnNetworkActiveListener {
1480 /**
1481 * Called on the main thread of the process to report that the current data network
1482 * has become active, and it is now a good time to perform any pending network
1483 * operations. Note that this listener only tells you when the network becomes
1484 * active; if at any other time you want to know whether it is active (and thus okay
1485 * to initiate network traffic), you can retrieve its instantaneous state with
Robert Greenwalt6078b502014-06-11 16:05:07 -07001486 * {@link ConnectivityManager#isDefaultNetworkActive}.
Dianne Hackborn77b987f2014-02-26 16:20:52 -08001487 */
1488 public void onNetworkActive();
1489 }
1490
1491 private INetworkManagementService getNetworkManagementService() {
1492 synchronized (this) {
1493 if (mNMService != null) {
1494 return mNMService;
1495 }
1496 IBinder b = ServiceManager.getService(Context.NETWORKMANAGEMENT_SERVICE);
1497 mNMService = INetworkManagementService.Stub.asInterface(b);
1498 return mNMService;
1499 }
1500 }
1501
1502 private final ArrayMap<OnNetworkActiveListener, INetworkActivityListener>
1503 mNetworkActivityListeners
1504 = new ArrayMap<OnNetworkActiveListener, INetworkActivityListener>();
1505
1506 /**
Robert Greenwalt6078b502014-06-11 16:05:07 -07001507 * Start listening to reports when the system's default data network is active, meaning it is
1508 * a good time to perform network traffic. Use {@link #isDefaultNetworkActive()}
1509 * to determine the current state of the system's default network after registering the
1510 * listener.
1511 * <p>
1512 * If the process default network has been set with
Paul Jensen72db88e2015-03-10 10:54:12 -04001513 * {@link ConnectivityManager#bindProcessToNetwork} this function will not
Robert Greenwalt6078b502014-06-11 16:05:07 -07001514 * reflect the process's default, but the system default.
Dianne Hackborn77b987f2014-02-26 16:20:52 -08001515 *
1516 * @param l The listener to be told when the network is active.
1517 */
Robert Greenwaltb2489872014-09-04 16:44:35 -07001518 public void addDefaultNetworkActiveListener(final OnNetworkActiveListener l) {
Dianne Hackborn77b987f2014-02-26 16:20:52 -08001519 INetworkActivityListener rl = new INetworkActivityListener.Stub() {
1520 @Override
1521 public void onNetworkActive() throws RemoteException {
1522 l.onNetworkActive();
1523 }
1524 };
1525
1526 try {
1527 getNetworkManagementService().registerNetworkActivityListener(rl);
1528 mNetworkActivityListeners.put(l, rl);
1529 } catch (RemoteException e) {
1530 }
1531 }
1532
1533 /**
1534 * Remove network active listener previously registered with
Robert Greenwaltb2489872014-09-04 16:44:35 -07001535 * {@link #addDefaultNetworkActiveListener}.
Dianne Hackborn77b987f2014-02-26 16:20:52 -08001536 *
1537 * @param l Previously registered listener.
1538 */
Robert Greenwaltb2489872014-09-04 16:44:35 -07001539 public void removeDefaultNetworkActiveListener(OnNetworkActiveListener l) {
Dianne Hackborn77b987f2014-02-26 16:20:52 -08001540 INetworkActivityListener rl = mNetworkActivityListeners.get(l);
1541 if (rl == null) {
1542 throw new IllegalArgumentException("Listener not registered: " + l);
1543 }
1544 try {
1545 getNetworkManagementService().unregisterNetworkActivityListener(rl);
1546 } catch (RemoteException e) {
1547 }
1548 }
1549
1550 /**
1551 * Return whether the data network is currently active. An active network means that
1552 * it is currently in a high power state for performing data transmission. On some
1553 * types of networks, it may be expensive to move and stay in such a state, so it is
1554 * more power efficient to batch network traffic together when the radio is already in
1555 * this state. This method tells you whether right now is currently a good time to
1556 * initiate network traffic, as the network is already active.
1557 */
Robert Greenwalt6078b502014-06-11 16:05:07 -07001558 public boolean isDefaultNetworkActive() {
Dianne Hackborn77b987f2014-02-26 16:20:52 -08001559 try {
1560 return getNetworkManagementService().isNetworkActive();
1561 } catch (RemoteException e) {
1562 }
1563 return false;
1564 }
1565
1566 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001567 * {@hide}
1568 */
Lorenzo Colittiffc42b02015-07-29 11:41:21 +09001569 public ConnectivityManager(Context context, IConnectivityManager service) {
1570 mContext = checkNotNull(context, "missing context");
Jeff Sharkeyf0ceede2011-08-02 17:22:34 -07001571 mService = checkNotNull(service, "missing IConnectivityManager");
Paul Jensene0bef712014-12-10 15:12:18 -05001572 sInstance = this;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001573 }
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -08001574
Jeff Sharkey8fc27e82012-04-04 20:40:58 -07001575 /** {@hide} */
1576 public static ConnectivityManager from(Context context) {
1577 return (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
1578 }
1579
Lorenzo Colittid5427052015-10-15 16:29:00 +09001580 /** {@hide} */
1581 public static final void enforceChangePermission(Context context) {
1582 int uid = Binder.getCallingUid();
1583 Settings.checkAndNoteChangeNetworkStateOperation(context, uid, Settings
1584 .getPackageNameForUid(context, uid), true /* throwException */);
1585 }
1586
Robert Greenwaltedb47662014-09-16 17:54:19 -07001587 /** {@hide */
1588 public static final void enforceTetherChangePermission(Context context) {
1589 if (context.getResources().getStringArray(
1590 com.android.internal.R.array.config_mobile_hotspot_provision_app).length == 2) {
1591 // Have a provisioning app - must only let system apps (which check this app)
1592 // turn on tethering
1593 context.enforceCallingOrSelfPermission(
1594 android.Manifest.permission.CONNECTIVITY_INTERNAL, "ConnectivityService");
1595 } else {
Billy Laua7238a32015-08-01 12:45:02 +01001596 int uid = Binder.getCallingUid();
Lorenzo Colittid5427052015-10-15 16:29:00 +09001597 Settings.checkAndNoteWriteSettingsOperation(context, uid, Settings
1598 .getPackageNameForUid(context, uid), true /* throwException */);
Robert Greenwaltedb47662014-09-16 17:54:19 -07001599 }
1600 }
1601
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -08001602 /**
Paul Jensene0bef712014-12-10 15:12:18 -05001603 * @deprecated - use getSystemService. This is a kludge to support static access in certain
1604 * situations where a Context pointer is unavailable.
1605 * @hide
1606 */
Paul Jensen72db88e2015-03-10 10:54:12 -04001607 static ConnectivityManager getInstanceOrNull() {
1608 return sInstance;
1609 }
1610
1611 /**
1612 * @deprecated - use getSystemService. This is a kludge to support static access in certain
1613 * situations where a Context pointer is unavailable.
1614 * @hide
1615 */
1616 private static ConnectivityManager getInstance() {
1617 if (getInstanceOrNull() == null) {
Paul Jensene0bef712014-12-10 15:12:18 -05001618 throw new IllegalStateException("No ConnectivityManager yet constructed");
1619 }
Paul Jensen72db88e2015-03-10 10:54:12 -04001620 return getInstanceOrNull();
Paul Jensene0bef712014-12-10 15:12:18 -05001621 }
1622
1623 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08001624 * Get the set of tetherable, available interfaces. This list is limited by
1625 * device configuration and current interface existence.
Paul Jensenb2748922015-05-06 11:10:18 -04001626 * <p>This method requires the caller to hold the permission
1627 * {@link android.Manifest.permission#ACCESS_NETWORK_STATE}.
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08001628 *
1629 * @return an array of 0 or more Strings of tetherable interface names.
1630 *
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -08001631 * {@hide}
1632 */
1633 public String[] getTetherableIfaces() {
1634 try {
1635 return mService.getTetherableIfaces();
1636 } catch (RemoteException e) {
1637 return new String[0];
1638 }
1639 }
1640
1641 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08001642 * Get the set of tethered interfaces.
Paul Jensenb2748922015-05-06 11:10:18 -04001643 * <p>This method requires the caller to hold the permission
1644 * {@link android.Manifest.permission#ACCESS_NETWORK_STATE}.
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08001645 *
1646 * @return an array of 0 or more String of currently tethered interface names.
1647 *
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -08001648 * {@hide}
1649 */
1650 public String[] getTetheredIfaces() {
1651 try {
1652 return mService.getTetheredIfaces();
1653 } catch (RemoteException e) {
1654 return new String[0];
1655 }
1656 }
1657
1658 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08001659 * Get the set of interface names which attempted to tether but
1660 * failed. Re-attempting to tether may cause them to reset to the Tethered
1661 * state. Alternatively, causing the interface to be destroyed and recreated
1662 * may cause them to reset to the available state.
1663 * {@link ConnectivityManager#getLastTetherError} can be used to get more
1664 * information on the cause of the errors.
Paul Jensenb2748922015-05-06 11:10:18 -04001665 * <p>This method requires the caller to hold the permission
1666 * {@link android.Manifest.permission#ACCESS_NETWORK_STATE}.
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08001667 *
1668 * @return an array of 0 or more String indicating the interface names
1669 * which failed to tether.
1670 *
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -08001671 * {@hide}
1672 */
Robert Greenwalt5a735062010-03-02 17:25:02 -08001673 public String[] getTetheringErroredIfaces() {
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -08001674 try {
Robert Greenwalt5a735062010-03-02 17:25:02 -08001675 return mService.getTetheringErroredIfaces();
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -08001676 } catch (RemoteException e) {
Robert Greenwalt5a735062010-03-02 17:25:02 -08001677 return new String[0];
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -08001678 }
1679 }
1680
1681 /**
Robert Greenwalt9c7e2c22014-06-23 14:53:42 -07001682 * Get the set of tethered dhcp ranges.
1683 *
1684 * @return an array of 0 or more {@code String} of tethered dhcp ranges.
1685 * {@hide}
1686 */
1687 public String[] getTetheredDhcpRanges() {
1688 try {
1689 return mService.getTetheredDhcpRanges();
1690 } catch (RemoteException e) {
1691 return new String[0];
1692 }
1693 }
1694
1695 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08001696 * Attempt to tether the named interface. This will setup a dhcp server
1697 * on the interface, forward and NAT IP packets and forward DNS requests
1698 * to the best active upstream network interface. Note that if no upstream
1699 * IP network interface is available, dhcp will still run and traffic will be
1700 * allowed between the tethered devices and this device, though upstream net
1701 * access will of course fail until an upstream network interface becomes
1702 * active.
Lorenzo Colittid5427052015-10-15 16:29:00 +09001703 *
1704 * <p>This method requires the caller to hold either the
1705 * {@link android.Manifest.permission#CHANGE_NETWORK_STATE} permission
1706 * or the ability to modify system settings as determined by
1707 * {@link android.provider.Settings.System#canWrite}.</p>
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08001708 *
1709 * @param iface the interface name to tether.
1710 * @return error a {@code TETHER_ERROR} value indicating success or failure type
1711 *
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -08001712 * {@hide}
1713 */
Robert Greenwalt5a735062010-03-02 17:25:02 -08001714 public int tether(String iface) {
1715 try {
1716 return mService.tether(iface);
1717 } catch (RemoteException e) {
1718 return TETHER_ERROR_SERVICE_UNAVAIL;
1719 }
1720 }
1721
1722 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08001723 * Stop tethering the named interface.
Lorenzo Colittid5427052015-10-15 16:29:00 +09001724 *
1725 * <p>This method requires the caller to hold either the
1726 * {@link android.Manifest.permission#CHANGE_NETWORK_STATE} permission
1727 * or the ability to modify system settings as determined by
1728 * {@link android.provider.Settings.System#canWrite}.</p>
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08001729 *
1730 * @param iface the interface name to untether.
1731 * @return error a {@code TETHER_ERROR} value indicating success or failure type
1732 *
Robert Greenwalt5a735062010-03-02 17:25:02 -08001733 * {@hide}
1734 */
1735 public int untether(String iface) {
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -08001736 try {
1737 return mService.untether(iface);
1738 } catch (RemoteException e) {
Robert Greenwalt5a735062010-03-02 17:25:02 -08001739 return TETHER_ERROR_SERVICE_UNAVAIL;
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -08001740 }
1741 }
Robert Greenwalt2a091d72010-02-11 18:18:40 -08001742
1743 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08001744 * Check if the device allows for tethering. It may be disabled via
Dianne Hackborn77b987f2014-02-26 16:20:52 -08001745 * {@code ro.tether.denied} system property, Settings.TETHER_SUPPORTED or
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08001746 * due to device configuration.
Paul Jensenb2748922015-05-06 11:10:18 -04001747 * <p>This method requires the caller to hold the permission
1748 * {@link android.Manifest.permission#ACCESS_NETWORK_STATE}.
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08001749 *
1750 * @return a boolean - {@code true} indicating Tethering is supported.
1751 *
Robert Greenwalt2a091d72010-02-11 18:18:40 -08001752 * {@hide}
1753 */
1754 public boolean isTetheringSupported() {
1755 try {
1756 return mService.isTetheringSupported();
1757 } catch (RemoteException e) {
1758 return false;
1759 }
1760 }
1761
1762 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08001763 * Get the list of regular expressions that define any tetherable
1764 * USB network interfaces. If USB tethering is not supported by the
1765 * device, this list should be empty.
Paul Jensenb2748922015-05-06 11:10:18 -04001766 * <p>This method requires the caller to hold the permission
1767 * {@link android.Manifest.permission#ACCESS_NETWORK_STATE}.
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08001768 *
1769 * @return an array of 0 or more regular expression Strings defining
1770 * what interfaces are considered tetherable usb interfaces.
1771 *
Robert Greenwalt2a091d72010-02-11 18:18:40 -08001772 * {@hide}
1773 */
1774 public String[] getTetherableUsbRegexs() {
1775 try {
1776 return mService.getTetherableUsbRegexs();
1777 } catch (RemoteException e) {
1778 return new String[0];
1779 }
1780 }
1781
1782 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08001783 * Get the list of regular expressions that define any tetherable
1784 * Wifi network interfaces. If Wifi tethering is not supported by the
1785 * device, this list should be empty.
Paul Jensenb2748922015-05-06 11:10:18 -04001786 * <p>This method requires the caller to hold the permission
1787 * {@link android.Manifest.permission#ACCESS_NETWORK_STATE}.
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08001788 *
1789 * @return an array of 0 or more regular expression Strings defining
1790 * what interfaces are considered tetherable wifi interfaces.
1791 *
Robert Greenwalt2a091d72010-02-11 18:18:40 -08001792 * {@hide}
1793 */
1794 public String[] getTetherableWifiRegexs() {
1795 try {
1796 return mService.getTetherableWifiRegexs();
1797 } catch (RemoteException e) {
1798 return new String[0];
1799 }
1800 }
Robert Greenwalt5a735062010-03-02 17:25:02 -08001801
Danica Chang6fdd0c62010-08-11 14:54:43 -07001802 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08001803 * Get the list of regular expressions that define any tetherable
1804 * Bluetooth network interfaces. If Bluetooth tethering is not supported by the
1805 * device, this list should be empty.
Paul Jensenb2748922015-05-06 11:10:18 -04001806 * <p>This method requires the caller to hold the permission
1807 * {@link android.Manifest.permission#ACCESS_NETWORK_STATE}.
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08001808 *
1809 * @return an array of 0 or more regular expression Strings defining
1810 * what interfaces are considered tetherable bluetooth interfaces.
1811 *
Danica Chang6fdd0c62010-08-11 14:54:43 -07001812 * {@hide}
1813 */
1814 public String[] getTetherableBluetoothRegexs() {
1815 try {
1816 return mService.getTetherableBluetoothRegexs();
1817 } catch (RemoteException e) {
1818 return new String[0];
1819 }
1820 }
1821
Mike Lockwood6c2260b2011-07-19 13:04:47 -07001822 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08001823 * Attempt to both alter the mode of USB and Tethering of USB. A
1824 * utility method to deal with some of the complexity of USB - will
1825 * attempt to switch to Rndis and subsequently tether the resulting
1826 * interface on {@code true} or turn off tethering and switch off
1827 * Rndis on {@code false}.
Lorenzo Colittid5427052015-10-15 16:29:00 +09001828 *
1829 * <p>This method requires the caller to hold either the
1830 * {@link android.Manifest.permission#CHANGE_NETWORK_STATE} permission
1831 * or the ability to modify system settings as determined by
1832 * {@link android.provider.Settings.System#canWrite}.</p>
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08001833 *
1834 * @param enable a boolean - {@code true} to enable tethering
1835 * @return error a {@code TETHER_ERROR} value indicating success or failure type
1836 *
Mike Lockwood6c2260b2011-07-19 13:04:47 -07001837 * {@hide}
1838 */
1839 public int setUsbTethering(boolean enable) {
1840 try {
1841 return mService.setUsbTethering(enable);
1842 } catch (RemoteException e) {
1843 return TETHER_ERROR_SERVICE_UNAVAIL;
1844 }
1845 }
1846
Robert Greenwalt5a735062010-03-02 17:25:02 -08001847 /** {@hide} */
1848 public static final int TETHER_ERROR_NO_ERROR = 0;
1849 /** {@hide} */
1850 public static final int TETHER_ERROR_UNKNOWN_IFACE = 1;
1851 /** {@hide} */
1852 public static final int TETHER_ERROR_SERVICE_UNAVAIL = 2;
1853 /** {@hide} */
1854 public static final int TETHER_ERROR_UNSUPPORTED = 3;
1855 /** {@hide} */
1856 public static final int TETHER_ERROR_UNAVAIL_IFACE = 4;
1857 /** {@hide} */
1858 public static final int TETHER_ERROR_MASTER_ERROR = 5;
1859 /** {@hide} */
1860 public static final int TETHER_ERROR_TETHER_IFACE_ERROR = 6;
1861 /** {@hide} */
1862 public static final int TETHER_ERROR_UNTETHER_IFACE_ERROR = 7;
1863 /** {@hide} */
1864 public static final int TETHER_ERROR_ENABLE_NAT_ERROR = 8;
1865 /** {@hide} */
1866 public static final int TETHER_ERROR_DISABLE_NAT_ERROR = 9;
1867 /** {@hide} */
1868 public static final int TETHER_ERROR_IFACE_CFG_ERROR = 10;
1869
1870 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08001871 * Get a more detailed error code after a Tethering or Untethering
1872 * request asynchronously failed.
Paul Jensenb2748922015-05-06 11:10:18 -04001873 * <p>This method requires the caller to hold the permission
1874 * {@link android.Manifest.permission#ACCESS_NETWORK_STATE}.
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08001875 *
1876 * @param iface The name of the interface of interest
Robert Greenwalt5a735062010-03-02 17:25:02 -08001877 * @return error The error code of the last error tethering or untethering the named
1878 * interface
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08001879 *
Robert Greenwalt5a735062010-03-02 17:25:02 -08001880 * {@hide}
1881 */
1882 public int getLastTetherError(String iface) {
1883 try {
1884 return mService.getLastTetherError(iface);
1885 } catch (RemoteException e) {
1886 return TETHER_ERROR_SERVICE_UNAVAIL;
1887 }
Robert Greenwalt14f2ef42010-06-15 12:19:37 -07001888 }
1889
1890 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08001891 * Report network connectivity status. This is currently used only
1892 * to alter status bar UI.
Paul Jensenb2748922015-05-06 11:10:18 -04001893 * <p>This method requires the caller to hold the permission
1894 * {@link android.Manifest.permission#STATUS_BAR}.
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08001895 *
Robert Greenwaltd7085fc2010-09-08 15:24:47 -07001896 * @param networkType The type of network you want to report on
1897 * @param percentage The quality of the connection 0 is bad, 100 is good
1898 * {@hide}
1899 */
1900 public void reportInetCondition(int networkType, int percentage) {
1901 try {
1902 mService.reportInetCondition(networkType, percentage);
1903 } catch (RemoteException e) {
1904 }
1905 }
Robert Greenwalt434203a2010-10-11 16:00:27 -07001906
1907 /**
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07001908 * Report a problem network to the framework. This provides a hint to the system
Ye Wenb87875e2014-07-21 14:19:01 -07001909 * that there might be connectivity problems on this network and may cause
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07001910 * the framework to re-evaluate network connectivity and/or switch to another
1911 * network.
Robert Greenwalt9258c642014-03-26 16:47:06 -07001912 *
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07001913 * @param network The {@link Network} the application was attempting to use
1914 * or {@code null} to indicate the current default network.
Paul Jensenbfd17b72015-04-07 12:43:13 -04001915 * @deprecated Use {@link #reportNetworkConnectivity} which allows reporting both
1916 * working and non-working connectivity.
Robert Greenwalt9258c642014-03-26 16:47:06 -07001917 */
1918 public void reportBadNetwork(Network network) {
1919 try {
Paul Jensenbfd17b72015-04-07 12:43:13 -04001920 // One of these will be ignored because it matches system's current state.
1921 // The other will trigger the necessary reevaluation.
1922 mService.reportNetworkConnectivity(network, true);
1923 mService.reportNetworkConnectivity(network, false);
1924 } catch (RemoteException e) {
1925 }
1926 }
1927
1928 /**
1929 * Report to the framework whether a network has working connectivity.
1930 * This provides a hint to the system that a particular network is providing
1931 * working connectivity or not. In response the framework may re-evaluate
1932 * the network's connectivity and might take further action thereafter.
1933 *
1934 * @param network The {@link Network} the application was attempting to use
1935 * or {@code null} to indicate the current default network.
1936 * @param hasConnectivity {@code true} if the application was able to successfully access the
1937 * Internet using {@code network} or {@code false} if not.
1938 */
1939 public void reportNetworkConnectivity(Network network, boolean hasConnectivity) {
1940 try {
1941 mService.reportNetworkConnectivity(network, hasConnectivity);
Robert Greenwalt9258c642014-03-26 16:47:06 -07001942 } catch (RemoteException e) {
1943 }
1944 }
1945
1946 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08001947 * Set a network-independent global http proxy. This is not normally what you want
1948 * for typical HTTP proxies - they are general network dependent. However if you're
1949 * doing something unusual like general internal filtering this may be useful. On
1950 * a private network where the proxy is not accessible, you may break HTTP using this.
Paul Jensen0d719ca2015-02-13 14:18:39 -05001951 * <p>This method requires the caller to hold the permission
Jason Monkfaf3fd52014-05-07 18:41:13 -04001952 * android.Manifest.permission#CONNECTIVITY_INTERNAL.
Paul Jensenb2748922015-05-06 11:10:18 -04001953 *
1954 * @param p A {@link ProxyInfo} object defining the new global
1955 * HTTP proxy. A {@code null} value will clear the global HTTP proxy.
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07001956 * @hide
Robert Greenwalt434203a2010-10-11 16:00:27 -07001957 */
Jason Monk207900c2014-04-25 15:00:09 -04001958 public void setGlobalProxy(ProxyInfo p) {
Robert Greenwalt434203a2010-10-11 16:00:27 -07001959 try {
1960 mService.setGlobalProxy(p);
1961 } catch (RemoteException e) {
1962 }
1963 }
1964
1965 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08001966 * Retrieve any network-independent global HTTP proxy.
1967 *
Jason Monk207900c2014-04-25 15:00:09 -04001968 * @return {@link ProxyInfo} for the current global HTTP proxy or {@code null}
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08001969 * if no global HTTP proxy is set.
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07001970 * @hide
Robert Greenwalt434203a2010-10-11 16:00:27 -07001971 */
Jason Monk207900c2014-04-25 15:00:09 -04001972 public ProxyInfo getGlobalProxy() {
Robert Greenwalt434203a2010-10-11 16:00:27 -07001973 try {
1974 return mService.getGlobalProxy();
1975 } catch (RemoteException e) {
1976 return null;
1977 }
1978 }
1979
1980 /**
Paul Jensencee9b512015-05-06 07:32:40 -04001981 * Retrieve the global HTTP proxy, or if no global HTTP proxy is set, a
1982 * network-specific HTTP proxy. If {@code network} is null, the
1983 * network-specific proxy returned is the proxy of the default active
1984 * network.
1985 *
1986 * @return {@link ProxyInfo} for the current global HTTP proxy, or if no
1987 * global HTTP proxy is set, {@code ProxyInfo} for {@code network},
1988 * or when {@code network} is {@code null},
1989 * the {@code ProxyInfo} for the default active network. Returns
1990 * {@code null} when no proxy applies or the caller doesn't have
1991 * permission to use {@code network}.
1992 * @hide
1993 */
1994 public ProxyInfo getProxyForNetwork(Network network) {
1995 try {
1996 return mService.getProxyForNetwork(network);
1997 } catch (RemoteException e) {
1998 return null;
1999 }
2000 }
2001
2002 /**
Paul Jensene0bef712014-12-10 15:12:18 -05002003 * Get the current default HTTP proxy settings. If a global proxy is set it will be returned,
2004 * otherwise if this process is bound to a {@link Network} using
Paul Jensen72db88e2015-03-10 10:54:12 -04002005 * {@link #bindProcessToNetwork} then that {@code Network}'s proxy is returned, otherwise
Paul Jensene0bef712014-12-10 15:12:18 -05002006 * the default network's proxy is returned.
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08002007 *
Jason Monk207900c2014-04-25 15:00:09 -04002008 * @return the {@link ProxyInfo} for the current HTTP proxy, or {@code null} if no
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08002009 * HTTP proxy is active.
Robert Greenwalt434203a2010-10-11 16:00:27 -07002010 */
Paul Jensene0bef712014-12-10 15:12:18 -05002011 public ProxyInfo getDefaultProxy() {
Paul Jensencee9b512015-05-06 07:32:40 -04002012 return getProxyForNetwork(getBoundNetworkForProcess());
Robert Greenwalt434203a2010-10-11 16:00:27 -07002013 }
Robert Greenwaltd55a6b42011-03-25 13:09:25 -07002014
2015 /**
Robert Greenwalt9b2886e2011-08-31 11:46:42 -07002016 * Returns true if the hardware supports the given network type
2017 * else it returns false. This doesn't indicate we have coverage
2018 * or are authorized onto a network, just whether or not the
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08002019 * hardware supports it. For example a GSM phone without a SIM
2020 * should still return {@code true} for mobile data, but a wifi only
2021 * tablet would return {@code false}.
Paul Jensenb2748922015-05-06 11:10:18 -04002022 * <p>This method requires the caller to hold the permission
2023 * {@link android.Manifest.permission#ACCESS_NETWORK_STATE}.
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08002024 *
2025 * @param networkType The network type we'd like to check
2026 * @return {@code true} if supported, else {@code false}
2027 *
Robert Greenwalt9b2886e2011-08-31 11:46:42 -07002028 * @hide
2029 */
2030 public boolean isNetworkSupported(int networkType) {
2031 try {
2032 return mService.isNetworkSupported(networkType);
2033 } catch (RemoteException e) {}
2034 return false;
2035 }
Jeff Sharkey9f7cbf02012-04-12 18:34:54 -07002036
2037 /**
2038 * Returns if the currently active data network is metered. A network is
2039 * classified as metered when the user is sensitive to heavy data usage on
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08002040 * that connection due to monetary costs, data limitations or
2041 * battery/performance issues. You should check this before doing large
2042 * data transfers, and warn the user or delay the operation until another
2043 * network is available.
Paul Jensenb2748922015-05-06 11:10:18 -04002044 * <p>This method requires the caller to hold the permission
2045 * {@link android.Manifest.permission#ACCESS_NETWORK_STATE}.
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08002046 *
2047 * @return {@code true} if large transfers should be avoided, otherwise
2048 * {@code false}.
Jeff Sharkey9f7cbf02012-04-12 18:34:54 -07002049 */
2050 public boolean isActiveNetworkMetered() {
2051 try {
2052 return mService.isActiveNetworkMetered();
2053 } catch (RemoteException e) {
2054 return false;
2055 }
2056 }
Jeff Sharkey69ddab42012-08-25 00:05:46 -07002057
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08002058 /**
2059 * If the LockdownVpn mechanism is enabled, updates the vpn
2060 * with a reload of its profile.
2061 *
2062 * @return a boolean with {@code} indicating success
2063 *
2064 * <p>This method can only be called by the system UID
2065 * {@hide}
2066 */
Jeff Sharkey69ddab42012-08-25 00:05:46 -07002067 public boolean updateLockdownVpn() {
2068 try {
2069 return mService.updateLockdownVpn();
2070 } catch (RemoteException e) {
2071 return false;
2072 }
2073 }
Irfan Sheriffda6da092012-08-16 12:49:23 -07002074
2075 /**
Wink Saville948282b2013-08-29 08:55:16 -07002076 * Check mobile provisioning.
Wink Savilleab9321d2013-06-29 21:10:57 -07002077 *
Wink Savilleab9321d2013-06-29 21:10:57 -07002078 * @param suggestedTimeOutMs, timeout in milliseconds
Wink Savilleab9321d2013-06-29 21:10:57 -07002079 *
2080 * @return time out that will be used, maybe less that suggestedTimeOutMs
2081 * -1 if an error.
2082 *
2083 * {@hide}
2084 */
Wink Saville948282b2013-08-29 08:55:16 -07002085 public int checkMobileProvisioning(int suggestedTimeOutMs) {
Wink Savilleab9321d2013-06-29 21:10:57 -07002086 int timeOutMs = -1;
2087 try {
Wink Saville948282b2013-08-29 08:55:16 -07002088 timeOutMs = mService.checkMobileProvisioning(suggestedTimeOutMs);
Wink Savilleab9321d2013-06-29 21:10:57 -07002089 } catch (RemoteException e) {
2090 }
2091 return timeOutMs;
2092 }
Robert Greenwalte182bfe2013-07-16 12:06:09 -07002093
2094 /**
Wink Saville42d4f082013-07-20 20:31:59 -07002095 * Get the mobile provisioning url.
Robert Greenwalte182bfe2013-07-16 12:06:09 -07002096 * {@hide}
2097 */
2098 public String getMobileProvisioningUrl() {
2099 try {
2100 return mService.getMobileProvisioningUrl();
2101 } catch (RemoteException e) {
2102 }
2103 return null;
2104 }
Wink Saville42d4f082013-07-20 20:31:59 -07002105
2106 /**
Wink Saville948282b2013-08-29 08:55:16 -07002107 * Set sign in error notification to visible or in visible
2108 *
2109 * @param visible
2110 * @param networkType
2111 *
2112 * {@hide}
Paul Jensen3541e9f2015-03-18 12:23:02 -04002113 * @deprecated Doesn't properly deal with multiple connected networks of the same type.
Wink Saville948282b2013-08-29 08:55:16 -07002114 */
2115 public void setProvisioningNotificationVisible(boolean visible, int networkType,
Paul Jensen89e0f092014-09-15 15:59:36 -04002116 String action) {
Wink Saville948282b2013-08-29 08:55:16 -07002117 try {
Paul Jensen89e0f092014-09-15 15:59:36 -04002118 mService.setProvisioningNotificationVisible(visible, networkType, action);
Wink Saville948282b2013-08-29 08:55:16 -07002119 } catch (RemoteException e) {
2120 }
2121 }
Yuhao Zheng5cd1a0e2013-09-09 17:00:04 -07002122
2123 /**
2124 * Set the value for enabling/disabling airplane mode
Paul Jensenb2748922015-05-06 11:10:18 -04002125 * <p>This method requires the caller to hold the permission
2126 * {@link android.Manifest.permission#CONNECTIVITY_INTERNAL}.
Yuhao Zheng5cd1a0e2013-09-09 17:00:04 -07002127 *
2128 * @param enable whether to enable airplane mode or not
2129 *
Yuhao Zheng5cd1a0e2013-09-09 17:00:04 -07002130 * @hide
2131 */
2132 public void setAirplaneMode(boolean enable) {
2133 try {
2134 mService.setAirplaneMode(enable);
2135 } catch (RemoteException e) {
2136 }
2137 }
Robert Greenwalte049c232014-04-11 15:53:27 -07002138
2139 /** {@hide} */
Robert Greenwalta67be032014-05-16 15:49:14 -07002140 public void registerNetworkFactory(Messenger messenger, String name) {
Robert Greenwalte049c232014-04-11 15:53:27 -07002141 try {
Robert Greenwalta67be032014-05-16 15:49:14 -07002142 mService.registerNetworkFactory(messenger, name);
2143 } catch (RemoteException e) { }
2144 }
2145
2146 /** {@hide} */
2147 public void unregisterNetworkFactory(Messenger messenger) {
2148 try {
2149 mService.unregisterNetworkFactory(messenger);
Robert Greenwalte049c232014-04-11 15:53:27 -07002150 } catch (RemoteException e) { }
2151 }
Robert Greenwalt7b816022014-04-18 15:25:25 -07002152
Paul Jensen31a94f42015-02-13 14:18:39 -05002153 /**
2154 * @hide
2155 * Register a NetworkAgent with ConnectivityService.
2156 * @return NetID corresponding to NetworkAgent.
2157 */
2158 public int registerNetworkAgent(Messenger messenger, NetworkInfo ni, LinkProperties lp,
Sreeram Ramachandran8cd33ed2014-07-23 15:23:15 -07002159 NetworkCapabilities nc, int score, NetworkMisc misc) {
Robert Greenwalt7b816022014-04-18 15:25:25 -07002160 try {
Paul Jensen31a94f42015-02-13 14:18:39 -05002161 return mService.registerNetworkAgent(messenger, ni, lp, nc, score, misc);
2162 } catch (RemoteException e) {
2163 return NETID_UNSET;
2164 }
Robert Greenwalt7b816022014-04-18 15:25:25 -07002165 }
2166
Robert Greenwalt9258c642014-03-26 16:47:06 -07002167 /**
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07002168 * Base class for NetworkRequest callbacks. Used for notifications about network
2169 * changes. Should be extended by applications wanting notifications.
Robert Greenwalt9258c642014-03-26 16:47:06 -07002170 */
Robert Greenwalt6078b502014-06-11 16:05:07 -07002171 public static class NetworkCallback {
Robert Greenwalt7b816022014-04-18 15:25:25 -07002172 /**
Lorenzo Colitti07086932015-04-24 12:23:24 +09002173 * Called when the framework connects to a new network to evaluate whether it satisfies this
2174 * request. If evaluation succeeds, this callback may be followed by an {@link #onAvailable}
2175 * callback. There is no guarantee that this new network will satisfy any requests, or that
2176 * the network will stay connected for longer than the time necessary to evaluate it.
2177 * <p>
2178 * Most applications <b>should not</b> act on this callback, and should instead use
2179 * {@link #onAvailable}. This callback is intended for use by applications that can assist
2180 * the framework in properly evaluating the network &mdash; for example, an application that
2181 * can automatically log in to a captive portal without user intervention.
2182 *
2183 * @param network The {@link Network} of the network that is being evaluated.
Lorenzo Colitti66276122015-06-11 14:27:17 +09002184 *
2185 * @hide
Robert Greenwalt7b816022014-04-18 15:25:25 -07002186 */
Robert Greenwalt6078b502014-06-11 16:05:07 -07002187 public void onPreCheck(Network network) {}
Robert Greenwalt7b816022014-04-18 15:25:25 -07002188
2189 /**
Lorenzo Colitti07086932015-04-24 12:23:24 +09002190 * Called when the framework connects and has declared a new network ready for use.
Robert Greenwalt6078b502014-06-11 16:05:07 -07002191 * This callback may be called more than once if the {@link Network} that is
2192 * satisfying the request changes.
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07002193 *
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07002194 * @param network The {@link Network} of the satisfying network.
Robert Greenwalt7b816022014-04-18 15:25:25 -07002195 */
Robert Greenwalt6078b502014-06-11 16:05:07 -07002196 public void onAvailable(Network network) {}
Robert Greenwalt7b816022014-04-18 15:25:25 -07002197
2198 /**
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07002199 * Called when the network is about to be disconnected. Often paired with an
Robert Greenwalt6078b502014-06-11 16:05:07 -07002200 * {@link NetworkCallback#onAvailable} call with the new replacement network
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07002201 * for graceful handover. This may not be called if we have a hard loss
2202 * (loss without warning). This may be followed by either a
Robert Greenwalt6078b502014-06-11 16:05:07 -07002203 * {@link NetworkCallback#onLost} call or a
2204 * {@link NetworkCallback#onAvailable} call for this network depending
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07002205 * on whether we lose or regain it.
2206 *
Robert Greenwalt6078b502014-06-11 16:05:07 -07002207 * @param network The {@link Network} that is about to be disconnected.
2208 * @param maxMsToLive The time in ms the framework will attempt to keep the
2209 * network connected. Note that the network may suffer a
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07002210 * hard loss at any time.
Robert Greenwalt7b816022014-04-18 15:25:25 -07002211 */
Robert Greenwalt6078b502014-06-11 16:05:07 -07002212 public void onLosing(Network network, int maxMsToLive) {}
Robert Greenwalt7b816022014-04-18 15:25:25 -07002213
2214 /**
2215 * Called when the framework has a hard loss of the network or when the
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07002216 * graceful failure ends.
2217 *
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07002218 * @param network The {@link Network} lost.
Robert Greenwalt7b816022014-04-18 15:25:25 -07002219 */
Robert Greenwalt6078b502014-06-11 16:05:07 -07002220 public void onLost(Network network) {}
Robert Greenwalt7b816022014-04-18 15:25:25 -07002221
2222 /**
2223 * Called if no network is found in the given timeout time. If no timeout is given,
2224 * this will not be called.
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07002225 * @hide
Robert Greenwalt7b816022014-04-18 15:25:25 -07002226 */
Robert Greenwalt6078b502014-06-11 16:05:07 -07002227 public void onUnavailable() {}
Robert Greenwalt7b816022014-04-18 15:25:25 -07002228
2229 /**
2230 * Called when the network the framework connected to for this request
2231 * changes capabilities but still satisfies the stated need.
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07002232 *
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07002233 * @param network The {@link Network} whose capabilities have changed.
Lorenzo Colittie285b432015-04-23 15:32:42 +09002234 * @param networkCapabilities The new {@link android.net.NetworkCapabilities} for this network.
Robert Greenwalt7b816022014-04-18 15:25:25 -07002235 */
Robert Greenwalt6078b502014-06-11 16:05:07 -07002236 public void onCapabilitiesChanged(Network network,
Robert Greenwalt7b816022014-04-18 15:25:25 -07002237 NetworkCapabilities networkCapabilities) {}
2238
2239 /**
2240 * Called when the network the framework connected to for this request
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07002241 * changes {@link LinkProperties}.
2242 *
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07002243 * @param network The {@link Network} whose link properties have changed.
2244 * @param linkProperties The new {@link LinkProperties} for this network.
Robert Greenwalt7b816022014-04-18 15:25:25 -07002245 */
Robert Greenwalt6078b502014-06-11 16:05:07 -07002246 public void onLinkPropertiesChanged(Network network, LinkProperties linkProperties) {}
Robert Greenwalt7b816022014-04-18 15:25:25 -07002247
Robert Greenwalt8d482522015-06-24 13:23:42 -07002248 /**
2249 * Called when the network the framework connected to for this request
2250 * goes into {@link NetworkInfo.DetailedState.SUSPENDED}.
2251 * This generally means that while the TCP connections are still live,
2252 * temporarily network data fails to transfer. Specifically this is used
2253 * on cellular networks to mask temporary outages when driving through
2254 * a tunnel, etc.
2255 * @hide
2256 */
2257 public void onNetworkSuspended(Network network) {}
2258
2259 /**
2260 * Called when the network the framework connected to for this request
2261 * returns from a {@link NetworkInfo.DetailedState.SUSPENDED} state.
2262 * This should always be preceeded by a matching {@code onNetworkSuspended}
2263 * call.
2264 * @hide
2265 */
2266 public void onNetworkResumed(Network network) {}
2267
Robert Greenwalt6078b502014-06-11 16:05:07 -07002268 private NetworkRequest networkRequest;
Robert Greenwalt7b816022014-04-18 15:25:25 -07002269 }
2270
Robert Greenwalt9258c642014-03-26 16:47:06 -07002271 private static final int BASE = Protocol.BASE_CONNECTIVITY_MANAGER;
Robert Greenwalt9258c642014-03-26 16:47:06 -07002272 /** @hide */
Robert Greenwalt8d482522015-06-24 13:23:42 -07002273 public static final int CALLBACK_PRECHECK = BASE + 1;
2274 /** @hide */
2275 public static final int CALLBACK_AVAILABLE = BASE + 2;
2276 /** @hide arg1 = TTL */
2277 public static final int CALLBACK_LOSING = BASE + 3;
2278 /** @hide */
2279 public static final int CALLBACK_LOST = BASE + 4;
2280 /** @hide */
2281 public static final int CALLBACK_UNAVAIL = BASE + 5;
2282 /** @hide */
2283 public static final int CALLBACK_CAP_CHANGED = BASE + 6;
2284 /** @hide */
2285 public static final int CALLBACK_IP_CHANGED = BASE + 7;
2286 /** @hide */
2287 public static final int CALLBACK_RELEASED = BASE + 8;
2288 /** @hide */
2289 public static final int CALLBACK_EXIT = BASE + 9;
Robert Greenwalt562cc542014-05-15 18:07:26 -07002290 /** @hide obj = NetworkCapabilities, arg1 = seq number */
Robert Greenwalt8d482522015-06-24 13:23:42 -07002291 private static final int EXPIRE_LEGACY_REQUEST = BASE + 10;
2292 /** @hide */
2293 public static final int CALLBACK_SUSPENDED = BASE + 11;
2294 /** @hide */
2295 public static final int CALLBACK_RESUMED = BASE + 12;
Robert Greenwalt9258c642014-03-26 16:47:06 -07002296
Robert Greenwalt562cc542014-05-15 18:07:26 -07002297 private class CallbackHandler extends Handler {
Robert Greenwalt6078b502014-06-11 16:05:07 -07002298 private final HashMap<NetworkRequest, NetworkCallback>mCallbackMap;
Robert Greenwalt9258c642014-03-26 16:47:06 -07002299 private final AtomicInteger mRefCount;
2300 private static final String TAG = "ConnectivityManager.CallbackHandler";
2301 private final ConnectivityManager mCm;
Robert Greenwalta9ebeef2015-09-03 16:41:45 -07002302 private static final boolean DBG = false;
Robert Greenwalt9258c642014-03-26 16:47:06 -07002303
Robert Greenwalt6078b502014-06-11 16:05:07 -07002304 CallbackHandler(Looper looper, HashMap<NetworkRequest, NetworkCallback>callbackMap,
Robert Greenwalt9258c642014-03-26 16:47:06 -07002305 AtomicInteger refCount, ConnectivityManager cm) {
2306 super(looper);
2307 mCallbackMap = callbackMap;
2308 mRefCount = refCount;
2309 mCm = cm;
2310 }
2311
2312 @Override
2313 public void handleMessage(Message message) {
Robert Greenwalta9ebeef2015-09-03 16:41:45 -07002314 if (DBG) Log.d(TAG, "CM callback handler got msg " + message.what);
Lorenzo Colittidb95a602015-04-24 15:21:21 +09002315 NetworkRequest request = (NetworkRequest) getObject(message, NetworkRequest.class);
2316 Network network = (Network) getObject(message, Network.class);
Robert Greenwalt9258c642014-03-26 16:47:06 -07002317 switch (message.what) {
2318 case CALLBACK_PRECHECK: {
Lorenzo Colittidb95a602015-04-24 15:21:21 +09002319 NetworkCallback callback = getCallback(request, "PRECHECK");
2320 if (callback != null) {
2321 callback.onPreCheck(network);
Robert Greenwalt9258c642014-03-26 16:47:06 -07002322 }
2323 break;
2324 }
2325 case CALLBACK_AVAILABLE: {
Lorenzo Colittidb95a602015-04-24 15:21:21 +09002326 NetworkCallback callback = getCallback(request, "AVAILABLE");
2327 if (callback != null) {
2328 callback.onAvailable(network);
Robert Greenwalt9258c642014-03-26 16:47:06 -07002329 }
2330 break;
2331 }
2332 case CALLBACK_LOSING: {
Lorenzo Colittidb95a602015-04-24 15:21:21 +09002333 NetworkCallback callback = getCallback(request, "LOSING");
2334 if (callback != null) {
2335 callback.onLosing(network, message.arg1);
Robert Greenwalt9258c642014-03-26 16:47:06 -07002336 }
2337 break;
2338 }
2339 case CALLBACK_LOST: {
Lorenzo Colittidb95a602015-04-24 15:21:21 +09002340 NetworkCallback callback = getCallback(request, "LOST");
2341 if (callback != null) {
2342 callback.onLost(network);
Robert Greenwalt9258c642014-03-26 16:47:06 -07002343 }
2344 break;
2345 }
2346 case CALLBACK_UNAVAIL: {
Lorenzo Colittidb95a602015-04-24 15:21:21 +09002347 NetworkCallback callback = getCallback(request, "UNAVAIL");
2348 if (callback != null) {
2349 callback.onUnavailable();
Robert Greenwalt9258c642014-03-26 16:47:06 -07002350 }
2351 break;
2352 }
2353 case CALLBACK_CAP_CHANGED: {
Lorenzo Colittidb95a602015-04-24 15:21:21 +09002354 NetworkCallback callback = getCallback(request, "CAP_CHANGED");
2355 if (callback != null) {
Robert Greenwalta848c1c2014-09-30 16:50:07 -07002356 NetworkCapabilities cap = (NetworkCapabilities)getObject(message,
2357 NetworkCapabilities.class);
Robert Greenwalt9258c642014-03-26 16:47:06 -07002358
Lorenzo Colittidb95a602015-04-24 15:21:21 +09002359 callback.onCapabilitiesChanged(network, cap);
Robert Greenwalt9258c642014-03-26 16:47:06 -07002360 }
2361 break;
2362 }
2363 case CALLBACK_IP_CHANGED: {
Lorenzo Colittidb95a602015-04-24 15:21:21 +09002364 NetworkCallback callback = getCallback(request, "IP_CHANGED");
2365 if (callback != null) {
Robert Greenwalta848c1c2014-09-30 16:50:07 -07002366 LinkProperties lp = (LinkProperties)getObject(message,
2367 LinkProperties.class);
Robert Greenwalt9258c642014-03-26 16:47:06 -07002368
Lorenzo Colittidb95a602015-04-24 15:21:21 +09002369 callback.onLinkPropertiesChanged(network, lp);
Robert Greenwalt9258c642014-03-26 16:47:06 -07002370 }
2371 break;
2372 }
Robert Greenwalt8d482522015-06-24 13:23:42 -07002373 case CALLBACK_SUSPENDED: {
2374 NetworkCallback callback = getCallback(request, "SUSPENDED");
2375 if (callback != null) {
2376 callback.onNetworkSuspended(network);
2377 }
2378 break;
2379 }
2380 case CALLBACK_RESUMED: {
2381 NetworkCallback callback = getCallback(request, "RESUMED");
2382 if (callback != null) {
2383 callback.onNetworkResumed(network);
2384 }
2385 break;
2386 }
Robert Greenwalt9258c642014-03-26 16:47:06 -07002387 case CALLBACK_RELEASED: {
Lorenzo Colittidb95a602015-04-24 15:21:21 +09002388 NetworkCallback callback = null;
Robert Greenwalt9258c642014-03-26 16:47:06 -07002389 synchronized(mCallbackMap) {
Lorenzo Colittidb95a602015-04-24 15:21:21 +09002390 callback = mCallbackMap.remove(request);
Robert Greenwalt9258c642014-03-26 16:47:06 -07002391 }
Lorenzo Colittidb95a602015-04-24 15:21:21 +09002392 if (callback != null) {
Robert Greenwalt6078b502014-06-11 16:05:07 -07002393 synchronized(mRefCount) {
2394 if (mRefCount.decrementAndGet() == 0) {
2395 getLooper().quit();
2396 }
2397 }
Robert Greenwalt9258c642014-03-26 16:47:06 -07002398 } else {
Lorenzo Colittidb95a602015-04-24 15:21:21 +09002399 Log.e(TAG, "callback not found for RELEASED message");
Robert Greenwalt9258c642014-03-26 16:47:06 -07002400 }
Robert Greenwalt9258c642014-03-26 16:47:06 -07002401 break;
2402 }
2403 case CALLBACK_EXIT: {
Lorenzo Colittidb95a602015-04-24 15:21:21 +09002404 Log.d(TAG, "Listener quitting");
Robert Greenwalt9258c642014-03-26 16:47:06 -07002405 getLooper().quit();
2406 break;
2407 }
Robert Greenwalt562cc542014-05-15 18:07:26 -07002408 case EXPIRE_LEGACY_REQUEST: {
2409 expireRequest((NetworkCapabilities)message.obj, message.arg1);
2410 break;
2411 }
Robert Greenwalt9258c642014-03-26 16:47:06 -07002412 }
2413 }
2414
Robert Greenwalta848c1c2014-09-30 16:50:07 -07002415 private Object getObject(Message msg, Class c) {
2416 return msg.getData().getParcelable(c.getSimpleName());
Robert Greenwalt9258c642014-03-26 16:47:06 -07002417 }
Lorenzo Colittidb95a602015-04-24 15:21:21 +09002418
2419 private NetworkCallback getCallback(NetworkRequest req, String name) {
2420 NetworkCallback callback;
Robert Greenwalt9258c642014-03-26 16:47:06 -07002421 synchronized(mCallbackMap) {
Lorenzo Colittidb95a602015-04-24 15:21:21 +09002422 callback = mCallbackMap.get(req);
Robert Greenwalt9258c642014-03-26 16:47:06 -07002423 }
Lorenzo Colittidb95a602015-04-24 15:21:21 +09002424 if (callback == null) {
2425 Log.e(TAG, "callback not found for " + name + " message");
2426 }
2427 return callback;
Robert Greenwalt9258c642014-03-26 16:47:06 -07002428 }
Robert Greenwalt9258c642014-03-26 16:47:06 -07002429 }
2430
Robert Greenwalt6078b502014-06-11 16:05:07 -07002431 private void incCallbackHandlerRefCount() {
Robert Greenwalt9258c642014-03-26 16:47:06 -07002432 synchronized(sCallbackRefCount) {
2433 if (sCallbackRefCount.incrementAndGet() == 1) {
2434 // TODO - switch this over to a ManagerThread or expire it when done
2435 HandlerThread callbackThread = new HandlerThread("ConnectivityManager");
2436 callbackThread.start();
2437 sCallbackHandler = new CallbackHandler(callbackThread.getLooper(),
Robert Greenwalt6078b502014-06-11 16:05:07 -07002438 sNetworkCallback, sCallbackRefCount, this);
Robert Greenwalt9258c642014-03-26 16:47:06 -07002439 }
2440 }
2441 }
2442
Robert Greenwalt6078b502014-06-11 16:05:07 -07002443 private void decCallbackHandlerRefCount() {
Robert Greenwalt9258c642014-03-26 16:47:06 -07002444 synchronized(sCallbackRefCount) {
2445 if (sCallbackRefCount.decrementAndGet() == 0) {
2446 sCallbackHandler.obtainMessage(CALLBACK_EXIT).sendToTarget();
2447 sCallbackHandler = null;
2448 }
2449 }
2450 }
2451
Robert Greenwalt6078b502014-06-11 16:05:07 -07002452 static final HashMap<NetworkRequest, NetworkCallback> sNetworkCallback =
2453 new HashMap<NetworkRequest, NetworkCallback>();
Robert Greenwalt9258c642014-03-26 16:47:06 -07002454 static final AtomicInteger sCallbackRefCount = new AtomicInteger(0);
2455 static CallbackHandler sCallbackHandler = null;
2456
2457 private final static int LISTEN = 1;
2458 private final static int REQUEST = 2;
2459
Robert Greenwalt562cc542014-05-15 18:07:26 -07002460 private NetworkRequest sendRequestForNetwork(NetworkCapabilities need,
Robert Greenwalt6078b502014-06-11 16:05:07 -07002461 NetworkCallback networkCallback, int timeoutSec, int action,
Robert Greenwalt32aa65a2014-06-02 15:32:02 -07002462 int legacyType) {
Robert Greenwalt6078b502014-06-11 16:05:07 -07002463 if (networkCallback == null) {
2464 throw new IllegalArgumentException("null NetworkCallback");
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07002465 }
Robert Greenwalt9258c642014-03-26 16:47:06 -07002466 if (need == null) throw new IllegalArgumentException("null NetworkCapabilities");
2467 try {
Robert Greenwalt6078b502014-06-11 16:05:07 -07002468 incCallbackHandlerRefCount();
Paul Jensen7221cc32014-06-27 11:05:32 -04002469 synchronized(sNetworkCallback) {
2470 if (action == LISTEN) {
2471 networkCallback.networkRequest = mService.listenForNetwork(need,
2472 new Messenger(sCallbackHandler), new Binder());
2473 } else {
2474 networkCallback.networkRequest = mService.requestNetwork(need,
2475 new Messenger(sCallbackHandler), timeoutSec, new Binder(), legacyType);
2476 }
2477 if (networkCallback.networkRequest != null) {
Robert Greenwalt6078b502014-06-11 16:05:07 -07002478 sNetworkCallback.put(networkCallback.networkRequest, networkCallback);
Robert Greenwalt9258c642014-03-26 16:47:06 -07002479 }
2480 }
2481 } catch (RemoteException e) {}
Robert Greenwalt6078b502014-06-11 16:05:07 -07002482 if (networkCallback.networkRequest == null) decCallbackHandlerRefCount();
2483 return networkCallback.networkRequest;
Robert Greenwalt9258c642014-03-26 16:47:06 -07002484 }
2485
2486 /**
Lorenzo Colitti7de289f2015-11-25 12:00:52 +09002487 * Helper function to requests a network with a particular legacy type.
2488 *
2489 * This is temporarily public @hide so it can be called by system code that uses the
2490 * NetworkRequest API to request networks but relies on CONNECTIVITY_ACTION broadcasts for
2491 * instead network notifications.
2492 *
2493 * TODO: update said system code to rely on NetworkCallbacks and make this method private.
2494 *
2495 * @hide
2496 */
Lorenzo Colittid1179462015-11-25 15:47:14 +09002497 public void requestNetwork(NetworkRequest request, NetworkCallback networkCallback,
Lorenzo Colitti7de289f2015-11-25 12:00:52 +09002498 int timeoutMs, int legacyType) {
Lorenzo Colittid1179462015-11-25 15:47:14 +09002499 sendRequestForNetwork(request.networkCapabilities, networkCallback, timeoutMs, REQUEST,
2500 legacyType);
Lorenzo Colitti7de289f2015-11-25 12:00:52 +09002501 }
2502
2503 /**
Lorenzo Colittie285b432015-04-23 15:32:42 +09002504 * Request a network to satisfy a set of {@link android.net.NetworkCapabilities}.
Robert Greenwalt9258c642014-03-26 16:47:06 -07002505 *
2506 * This {@link NetworkRequest} will live until released via
Robert Greenwalt6078b502014-06-11 16:05:07 -07002507 * {@link #unregisterNetworkCallback} or the calling application exits.
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07002508 * Status of the request can be followed by listening to the various
Robert Greenwalt6078b502014-06-11 16:05:07 -07002509 * callbacks described in {@link NetworkCallback}. The {@link Network}
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07002510 * can be used to direct traffic to the network.
Paul Jensenbb2e0e92015-06-16 15:11:58 -04002511 * <p>It is presently unsupported to request a network with mutable
2512 * {@link NetworkCapabilities} such as
2513 * {@link NetworkCapabilities#NET_CAPABILITY_VALIDATED} or
2514 * {@link NetworkCapabilities#NET_CAPABILITY_CAPTIVE_PORTAL}
2515 * as these {@code NetworkCapabilities} represent states that a particular
2516 * network may never attain, and whether a network will attain these states
2517 * is unknown prior to bringing up the network so the framework does not
2518 * know how to go about satisfing a request with these capabilities.
Lorenzo Colittid5427052015-10-15 16:29:00 +09002519 *
2520 * <p>This method requires the caller to hold either the
2521 * {@link android.Manifest.permission#CHANGE_NETWORK_STATE} permission
2522 * or the ability to modify system settings as determined by
2523 * {@link android.provider.Settings.System#canWrite}.</p>
Robert Greenwalt9258c642014-03-26 16:47:06 -07002524 *
Robert Greenwalt6078b502014-06-11 16:05:07 -07002525 * @param request {@link NetworkRequest} describing this request.
2526 * @param networkCallback The {@link NetworkCallback} to be utilized for this
2527 * request. Note the callback must not be shared - they
2528 * uniquely specify this request.
Paul Jensenbb2e0e92015-06-16 15:11:58 -04002529 * @throws IllegalArgumentException if {@code request} specifies any mutable
2530 * {@code NetworkCapabilities}.
Robert Greenwalt9258c642014-03-26 16:47:06 -07002531 */
Robert Greenwalt6078b502014-06-11 16:05:07 -07002532 public void requestNetwork(NetworkRequest request, NetworkCallback networkCallback) {
Lorenzo Colittid1179462015-11-25 15:47:14 +09002533 requestNetwork(request, networkCallback, 0,
Lorenzo Colitti7de289f2015-11-25 12:00:52 +09002534 inferLegacyTypeForNetworkCapabilities(request.networkCapabilities));
Robert Greenwalt9258c642014-03-26 16:47:06 -07002535 }
2536
2537 /**
Lorenzo Colittie285b432015-04-23 15:32:42 +09002538 * Request a network to satisfy a set of {@link android.net.NetworkCapabilities}, limited
Robert Greenwalt9258c642014-03-26 16:47:06 -07002539 * by a timeout.
2540 *
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07002541 * This function behaves identically to the non-timedout version, but if a suitable
Robert Greenwalt6078b502014-06-11 16:05:07 -07002542 * network is not found within the given time (in milliseconds) the
2543 * {@link NetworkCallback#unavailable} callback is called. The request must
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07002544 * still be released normally by calling {@link releaseNetworkRequest}.
Lorenzo Colittid5427052015-10-15 16:29:00 +09002545 *
2546 * <p>This method requires the caller to hold either the
2547 * {@link android.Manifest.permission#CHANGE_NETWORK_STATE} permission
2548 * or the ability to modify system settings as determined by
2549 * {@link android.provider.Settings.System#canWrite}.</p>
2550 *
Robert Greenwalt6078b502014-06-11 16:05:07 -07002551 * @param request {@link NetworkRequest} describing this request.
2552 * @param networkCallback The callbacks to be utilized for this request. Note
2553 * the callbacks must not be shared - they uniquely specify
2554 * this request.
2555 * @param timeoutMs The time in milliseconds to attempt looking for a suitable network
2556 * before {@link NetworkCallback#unavailable} is called.
Lorenzo Colitti7de289f2015-11-25 12:00:52 +09002557 *
2558 * TODO: Make timeouts work and then unhide this method.
2559 *
Robert Greenwalt9258c642014-03-26 16:47:06 -07002560 * @hide
2561 */
Robert Greenwalt6078b502014-06-11 16:05:07 -07002562 public void requestNetwork(NetworkRequest request, NetworkCallback networkCallback,
2563 int timeoutMs) {
Lorenzo Colittid1179462015-11-25 15:47:14 +09002564 requestNetwork(request, networkCallback, timeoutMs,
Lorenzo Colitti7de289f2015-11-25 12:00:52 +09002565 inferLegacyTypeForNetworkCapabilities(request.networkCapabilities));
Robert Greenwalt9258c642014-03-26 16:47:06 -07002566 }
2567
2568 /**
Robert Greenwalt6078b502014-06-11 16:05:07 -07002569 * The maximum number of milliseconds the framework will look for a suitable network
Robert Greenwalt9258c642014-03-26 16:47:06 -07002570 * during a timeout-equiped call to {@link requestNetwork}.
2571 * {@hide}
2572 */
Robert Greenwalt6078b502014-06-11 16:05:07 -07002573 public final static int MAX_NETWORK_REQUEST_TIMEOUT_MS = 100 * 60 * 1000;
Robert Greenwalt9258c642014-03-26 16:47:06 -07002574
2575 /**
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07002576 * The lookup key for a {@link Network} object included with the intent after
Jeremy Joslin46e3ac82014-11-05 10:32:09 -08002577 * successfully finding a network for the applications request. Retrieve it with
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07002578 * {@link android.content.Intent#getParcelableExtra(String)}.
Jeremy Joslinfcde58f2015-02-11 16:51:13 -08002579 * <p>
Paul Jensen72db88e2015-03-10 10:54:12 -04002580 * Note that if you intend to invoke {@link Network#openConnection(java.net.URL)}
2581 * then you must get a ConnectivityManager instance before doing so.
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07002582 */
Erik Kline90e93072014-11-19 12:12:24 +09002583 public static final String EXTRA_NETWORK = "android.net.extra.NETWORK";
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07002584
2585 /**
Robert Greenwalt6078b502014-06-11 16:05:07 -07002586 * The lookup key for a {@link NetworkRequest} object included with the intent after
Jeremy Joslin46e3ac82014-11-05 10:32:09 -08002587 * successfully finding a network for the applications request. Retrieve it with
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07002588 * {@link android.content.Intent#getParcelableExtra(String)}.
2589 */
Erik Kline90e93072014-11-19 12:12:24 +09002590 public static final String EXTRA_NETWORK_REQUEST = "android.net.extra.NETWORK_REQUEST";
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07002591
2592
2593 /**
Lorenzo Colittie285b432015-04-23 15:32:42 +09002594 * Request a network to satisfy a set of {@link android.net.NetworkCapabilities}.
Robert Greenwalt9258c642014-03-26 16:47:06 -07002595 *
Jeremy Joslin46e3ac82014-11-05 10:32:09 -08002596 * This function behaves identically to the version that takes a NetworkCallback, but instead
Robert Greenwalt6078b502014-06-11 16:05:07 -07002597 * of {@link NetworkCallback} a {@link PendingIntent} is used. This means
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07002598 * the request may outlive the calling application and get called back when a suitable
2599 * network is found.
Robert Greenwalt9258c642014-03-26 16:47:06 -07002600 * <p>
2601 * The operation is an Intent broadcast that goes to a broadcast receiver that
2602 * you registered with {@link Context#registerReceiver} or through the
2603 * &lt;receiver&gt; tag in an AndroidManifest.xml file
2604 * <p>
2605 * The operation Intent is delivered with two extras, a {@link Network} typed
Erik Kline90e93072014-11-19 12:12:24 +09002606 * extra called {@link #EXTRA_NETWORK} and a {@link NetworkRequest}
2607 * typed extra called {@link #EXTRA_NETWORK_REQUEST} containing
Robert Greenwalt9258c642014-03-26 16:47:06 -07002608 * the original requests parameters. It is important to create a new,
Robert Greenwalt6078b502014-06-11 16:05:07 -07002609 * {@link NetworkCallback} based request before completing the processing of the
Robert Greenwalt9258c642014-03-26 16:47:06 -07002610 * Intent to reserve the network or it will be released shortly after the Intent
2611 * is processed.
2612 * <p>
Paul Jensen694f2b82015-06-17 14:15:39 -04002613 * If there is already a request for this Intent registered (with the equality of
Robert Greenwalt9258c642014-03-26 16:47:06 -07002614 * two Intents defined by {@link Intent#filterEquals}), then it will be removed and
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07002615 * replaced by this one, effectively releasing the previous {@link NetworkRequest}.
Robert Greenwalt9258c642014-03-26 16:47:06 -07002616 * <p>
Jeremy Joslin46e3ac82014-11-05 10:32:09 -08002617 * The request may be released normally by calling
2618 * {@link #releaseNetworkRequest(android.app.PendingIntent)}.
Paul Jensenbb2e0e92015-06-16 15:11:58 -04002619 * <p>It is presently unsupported to request a network with either
2620 * {@link NetworkCapabilities#NET_CAPABILITY_VALIDATED} or
2621 * {@link NetworkCapabilities#NET_CAPABILITY_CAPTIVE_PORTAL}
2622 * as these {@code NetworkCapabilities} represent states that a particular
2623 * network may never attain, and whether a network will attain these states
2624 * is unknown prior to bringing up the network so the framework does not
2625 * know how to go about satisfing a request with these capabilities.
Lorenzo Colittid5427052015-10-15 16:29:00 +09002626 *
2627 * <p>This method requires the caller to hold either the
2628 * {@link android.Manifest.permission#CHANGE_NETWORK_STATE} permission
2629 * or the ability to modify system settings as determined by
2630 * {@link android.provider.Settings.System#canWrite}.</p>
2631 *
Robert Greenwalt6078b502014-06-11 16:05:07 -07002632 * @param request {@link NetworkRequest} describing this request.
Robert Greenwalt9258c642014-03-26 16:47:06 -07002633 * @param operation Action to perform when the network is available (corresponds
Robert Greenwalt6078b502014-06-11 16:05:07 -07002634 * to the {@link NetworkCallback#onAvailable} call. Typically
Jeremy Joslin46e3ac82014-11-05 10:32:09 -08002635 * comes from {@link PendingIntent#getBroadcast}. Cannot be null.
Paul Jensenbb2e0e92015-06-16 15:11:58 -04002636 * @throws IllegalArgumentException if {@code request} contains either
2637 * {@link NetworkCapabilities#NET_CAPABILITY_VALIDATED} or
2638 * {@link NetworkCapabilities#NET_CAPABILITY_CAPTIVE_PORTAL}.
Robert Greenwalt9258c642014-03-26 16:47:06 -07002639 */
Robert Greenwalt6078b502014-06-11 16:05:07 -07002640 public void requestNetwork(NetworkRequest request, PendingIntent operation) {
Jeremy Joslin46e3ac82014-11-05 10:32:09 -08002641 checkPendingIntent(operation);
Robert Greenwalt9258c642014-03-26 16:47:06 -07002642 try {
Robert Greenwalt6078b502014-06-11 16:05:07 -07002643 mService.pendingRequestForNetwork(request.networkCapabilities, operation);
Robert Greenwalt9258c642014-03-26 16:47:06 -07002644 } catch (RemoteException e) {}
Robert Greenwalt9258c642014-03-26 16:47:06 -07002645 }
2646
2647 /**
Jeremy Joslin46e3ac82014-11-05 10:32:09 -08002648 * Removes a request made via {@link #requestNetwork(NetworkRequest, android.app.PendingIntent)}
2649 * <p>
2650 * This method has the same behavior as {@link #unregisterNetworkCallback} with respect to
2651 * releasing network resources and disconnecting.
2652 *
2653 * @param operation A PendingIntent equal (as defined by {@link Intent#filterEquals}) to the
2654 * PendingIntent passed to
2655 * {@link #requestNetwork(NetworkRequest, android.app.PendingIntent)} with the
2656 * corresponding NetworkRequest you'd like to remove. Cannot be null.
2657 */
2658 public void releaseNetworkRequest(PendingIntent operation) {
2659 checkPendingIntent(operation);
2660 try {
2661 mService.releasePendingNetworkRequest(operation);
2662 } catch (RemoteException e) {}
2663 }
2664
2665 private void checkPendingIntent(PendingIntent intent) {
2666 if (intent == null) {
2667 throw new IllegalArgumentException("PendingIntent cannot be null.");
2668 }
2669 }
2670
2671 /**
Robert Greenwalt9258c642014-03-26 16:47:06 -07002672 * Registers to receive notifications about all networks which satisfy the given
Robert Greenwalt6078b502014-06-11 16:05:07 -07002673 * {@link NetworkRequest}. The callbacks will continue to be called until
2674 * either the application exits or {@link #unregisterNetworkCallback} is called
Paul Jensenb2748922015-05-06 11:10:18 -04002675 * <p>This method requires the caller to hold the permission
2676 * {@link android.Manifest.permission#ACCESS_NETWORK_STATE}.
Robert Greenwalt9258c642014-03-26 16:47:06 -07002677 *
Robert Greenwalt6078b502014-06-11 16:05:07 -07002678 * @param request {@link NetworkRequest} describing this request.
2679 * @param networkCallback The {@link NetworkCallback} that the system will call as suitable
2680 * networks change state.
Robert Greenwalt9258c642014-03-26 16:47:06 -07002681 */
Robert Greenwalt6078b502014-06-11 16:05:07 -07002682 public void registerNetworkCallback(NetworkRequest request, NetworkCallback networkCallback) {
2683 sendRequestForNetwork(request.networkCapabilities, networkCallback, 0, LISTEN, TYPE_NONE);
Robert Greenwalt9258c642014-03-26 16:47:06 -07002684 }
2685
2686 /**
Paul Jensen694f2b82015-06-17 14:15:39 -04002687 * Registers a PendingIntent to be sent when a network is available which satisfies the given
2688 * {@link NetworkRequest}.
2689 *
2690 * This function behaves identically to the version that takes a NetworkCallback, but instead
2691 * of {@link NetworkCallback} a {@link PendingIntent} is used. This means
2692 * the request may outlive the calling application and get called back when a suitable
2693 * network is found.
2694 * <p>
2695 * The operation is an Intent broadcast that goes to a broadcast receiver that
2696 * you registered with {@link Context#registerReceiver} or through the
2697 * &lt;receiver&gt; tag in an AndroidManifest.xml file
2698 * <p>
2699 * The operation Intent is delivered with two extras, a {@link Network} typed
2700 * extra called {@link #EXTRA_NETWORK} and a {@link NetworkRequest}
2701 * typed extra called {@link #EXTRA_NETWORK_REQUEST} containing
2702 * the original requests parameters.
2703 * <p>
2704 * If there is already a request for this Intent registered (with the equality of
2705 * two Intents defined by {@link Intent#filterEquals}), then it will be removed and
2706 * replaced by this one, effectively releasing the previous {@link NetworkRequest}.
2707 * <p>
2708 * The request may be released normally by calling
Paul Jensenf2c1cfe2015-06-30 14:29:18 -04002709 * {@link #unregisterNetworkCallback(android.app.PendingIntent)}.
Paul Jensen694f2b82015-06-17 14:15:39 -04002710 * <p>This method requires the caller to hold the permission
2711 * {@link android.Manifest.permission#ACCESS_NETWORK_STATE}.
2712 * @param request {@link NetworkRequest} describing this request.
2713 * @param operation Action to perform when the network is available (corresponds
2714 * to the {@link NetworkCallback#onAvailable} call. Typically
2715 * comes from {@link PendingIntent#getBroadcast}. Cannot be null.
2716 */
2717 public void registerNetworkCallback(NetworkRequest request, PendingIntent operation) {
2718 checkPendingIntent(operation);
2719 try {
2720 mService.pendingListenForNetwork(request.networkCapabilities, operation);
2721 } catch (RemoteException e) {}
2722 }
2723
2724 /**
fengludb571472015-04-21 17:12:05 -07002725 * Requests bandwidth update for a given {@link Network} and returns whether the update request
2726 * is accepted by ConnectivityService. Once accepted, ConnectivityService will poll underlying
2727 * network connection for updated bandwidth information. The caller will be notified via
2728 * {@link ConnectivityManager.NetworkCallback} if there is an update. Notice that this
2729 * method assumes that the caller has previously called {@link #registerNetworkCallback} to
2730 * listen for network changes.
fenglub15e72b2015-03-20 11:29:56 -07002731 *
fengluae519192015-04-27 14:28:04 -07002732 * @param network {@link Network} specifying which network you're interested.
fengludb571472015-04-21 17:12:05 -07002733 * @return {@code true} on success, {@code false} if the {@link Network} is no longer valid.
fenglub15e72b2015-03-20 11:29:56 -07002734 */
fengludb571472015-04-21 17:12:05 -07002735 public boolean requestBandwidthUpdate(Network network) {
fenglub15e72b2015-03-20 11:29:56 -07002736 try {
fengludb571472015-04-21 17:12:05 -07002737 return mService.requestBandwidthUpdate(network);
fenglub15e72b2015-03-20 11:29:56 -07002738 } catch (RemoteException e) {
2739 return false;
2740 }
2741 }
2742
2743 /**
Robert Greenwalt6078b502014-06-11 16:05:07 -07002744 * Unregisters callbacks about and possibly releases networks originating from
Lorenzo Colitti2ea89e52015-04-24 17:03:31 +09002745 * {@link #requestNetwork(NetworkRequest, NetworkCallback)} and {@link #registerNetworkCallback}
2746 * calls. If the given {@code NetworkCallback} had previously been used with
2747 * {@code #requestNetwork}, any networks that had been connected to only to satisfy that request
2748 * will be disconnected.
Robert Greenwalt9258c642014-03-26 16:47:06 -07002749 *
Robert Greenwalt6078b502014-06-11 16:05:07 -07002750 * @param networkCallback The {@link NetworkCallback} used when making the request.
Robert Greenwalt9258c642014-03-26 16:47:06 -07002751 */
Robert Greenwalt6078b502014-06-11 16:05:07 -07002752 public void unregisterNetworkCallback(NetworkCallback networkCallback) {
2753 if (networkCallback == null || networkCallback.networkRequest == null ||
2754 networkCallback.networkRequest.requestId == REQUEST_ID_UNSET) {
2755 throw new IllegalArgumentException("Invalid NetworkCallback");
2756 }
Robert Greenwalt9258c642014-03-26 16:47:06 -07002757 try {
Robert Greenwalt6078b502014-06-11 16:05:07 -07002758 mService.releaseNetworkRequest(networkCallback.networkRequest);
Robert Greenwalt9258c642014-03-26 16:47:06 -07002759 } catch (RemoteException e) {}
2760 }
Paul Jensen6d3ff9e2014-05-29 10:12:39 -04002761
2762 /**
Paul Jensenf2c1cfe2015-06-30 14:29:18 -04002763 * Unregisters a callback previously registered via
2764 * {@link #registerNetworkCallback(NetworkRequest, android.app.PendingIntent)}.
2765 *
2766 * @param operation A PendingIntent equal (as defined by {@link Intent#filterEquals}) to the
2767 * PendingIntent passed to
2768 * {@link #registerNetworkCallback(NetworkRequest, android.app.PendingIntent)}.
2769 * Cannot be null.
2770 */
2771 public void unregisterNetworkCallback(PendingIntent operation) {
2772 releaseNetworkRequest(operation);
2773 }
2774
2775 /**
Lorenzo Colittie03c3c72015-04-03 16:38:52 +09002776 * Informs the system whether it should switch to {@code network} regardless of whether it is
2777 * validated or not. If {@code accept} is true, and the network was explicitly selected by the
2778 * user (e.g., by selecting a Wi-Fi network in the Settings app), then the network will become
2779 * the system default network regardless of any other network that's currently connected. If
2780 * {@code always} is true, then the choice is remembered, so that the next time the user
2781 * connects to this network, the system will switch to it.
2782 *
2783 * <p>This method requires the caller to hold the permission
2784 * {@link android.Manifest.permission#CONNECTIVITY_INTERNAL}
2785 *
2786 * @param network The network to accept.
2787 * @param accept Whether to accept the network even if unvalidated.
2788 * @param always Whether to remember this choice in the future.
2789 *
2790 * @hide
2791 */
2792 public void setAcceptUnvalidated(Network network, boolean accept, boolean always) {
2793 try {
2794 mService.setAcceptUnvalidated(network, accept, always);
2795 } catch (RemoteException e) {}
2796 }
2797
2798 /**
Stuart Scott984dc852015-03-30 13:17:11 -07002799 * Resets all connectivity manager settings back to factory defaults.
2800 * @hide
2801 */
2802 public void factoryReset() {
Stuart Scott984dc852015-03-30 13:17:11 -07002803 try {
Stuart Scottf1fb3972015-04-02 18:00:02 -07002804 mService.factoryReset();
Stuart Scott984dc852015-03-30 13:17:11 -07002805 } catch (RemoteException e) {
Stuart Scott984dc852015-03-30 13:17:11 -07002806 }
2807 }
2808
2809 /**
Paul Jensen6d3ff9e2014-05-29 10:12:39 -04002810 * Binds the current process to {@code network}. All Sockets created in the future
2811 * (and not explicitly bound via a bound SocketFactory from
2812 * {@link Network#getSocketFactory() Network.getSocketFactory()}) will be bound to
2813 * {@code network}. All host name resolutions will be limited to {@code network} as well.
2814 * Note that if {@code network} ever disconnects, all Sockets created in this way will cease to
2815 * work and all host name resolutions will fail. This is by design so an application doesn't
2816 * accidentally use Sockets it thinks are still bound to a particular {@link Network}.
2817 * To clear binding pass {@code null} for {@code network}. Using individually bound
2818 * Sockets created by Network.getSocketFactory().createSocket() and
2819 * performing network-specific host name resolutions via
2820 * {@link Network#getAllByName Network.getAllByName} is preferred to calling
Paul Jensen72db88e2015-03-10 10:54:12 -04002821 * {@code bindProcessToNetwork}.
Paul Jensen6d3ff9e2014-05-29 10:12:39 -04002822 *
2823 * @param network The {@link Network} to bind the current process to, or {@code null} to clear
2824 * the current binding.
2825 * @return {@code true} on success, {@code false} if the {@link Network} is no longer valid.
2826 */
Paul Jensen72db88e2015-03-10 10:54:12 -04002827 public boolean bindProcessToNetwork(Network network) {
2828 // Forcing callers to call thru non-static function ensures ConnectivityManager
2829 // instantiated.
2830 return setProcessDefaultNetwork(network);
2831 }
2832
2833 /**
2834 * Binds the current process to {@code network}. All Sockets created in the future
2835 * (and not explicitly bound via a bound SocketFactory from
2836 * {@link Network#getSocketFactory() Network.getSocketFactory()}) will be bound to
2837 * {@code network}. All host name resolutions will be limited to {@code network} as well.
2838 * Note that if {@code network} ever disconnects, all Sockets created in this way will cease to
2839 * work and all host name resolutions will fail. This is by design so an application doesn't
2840 * accidentally use Sockets it thinks are still bound to a particular {@link Network}.
2841 * To clear binding pass {@code null} for {@code network}. Using individually bound
2842 * Sockets created by Network.getSocketFactory().createSocket() and
2843 * performing network-specific host name resolutions via
2844 * {@link Network#getAllByName Network.getAllByName} is preferred to calling
2845 * {@code setProcessDefaultNetwork}.
2846 *
2847 * @param network The {@link Network} to bind the current process to, or {@code null} to clear
2848 * the current binding.
2849 * @return {@code true} on success, {@code false} if the {@link Network} is no longer valid.
2850 * @deprecated This function can throw {@link IllegalStateException}. Use
2851 * {@link #bindProcessToNetwork} instead. {@code bindProcessToNetwork}
2852 * is a direct replacement.
2853 */
Paul Jensen6d3ff9e2014-05-29 10:12:39 -04002854 public static boolean setProcessDefaultNetwork(Network network) {
Paul Jensenc91b5342014-08-27 12:38:45 -04002855 int netId = (network == null) ? NETID_UNSET : network.netId;
Paul Jensen72db88e2015-03-10 10:54:12 -04002856 if (netId == NetworkUtils.getBoundNetworkForProcess()) {
Paul Jensenc91b5342014-08-27 12:38:45 -04002857 return true;
2858 }
2859 if (NetworkUtils.bindProcessToNetwork(netId)) {
Paul Jensene0bef712014-12-10 15:12:18 -05002860 // Set HTTP proxy system properties to match network.
2861 // TODO: Deprecate this static method and replace it with a non-static version.
Lorenzo Colittiec4c5552015-04-22 11:52:48 +09002862 try {
2863 Proxy.setHttpProxySystemProperty(getInstance().getDefaultProxy());
2864 } catch (SecurityException e) {
2865 // The process doesn't have ACCESS_NETWORK_STATE, so we can't fetch the proxy.
2866 Log.e(TAG, "Can't set proxy properties", e);
2867 }
Paul Jensenc91b5342014-08-27 12:38:45 -04002868 // Must flush DNS cache as new network may have different DNS resolutions.
2869 InetAddress.clearDnsCache();
2870 // Must flush socket pool as idle sockets will be bound to previous network and may
2871 // cause subsequent fetches to be performed on old network.
2872 NetworkEventDispatcher.getInstance().onNetworkConfigurationChanged();
2873 return true;
2874 } else {
2875 return false;
2876 }
Paul Jensen6d3ff9e2014-05-29 10:12:39 -04002877 }
2878
2879 /**
2880 * Returns the {@link Network} currently bound to this process via
Paul Jensen72db88e2015-03-10 10:54:12 -04002881 * {@link #bindProcessToNetwork}, or {@code null} if no {@link Network} is explicitly bound.
Paul Jensen6d3ff9e2014-05-29 10:12:39 -04002882 *
2883 * @return {@code Network} to which this process is bound, or {@code null}.
2884 */
Paul Jensen72db88e2015-03-10 10:54:12 -04002885 public Network getBoundNetworkForProcess() {
2886 // Forcing callers to call thru non-static function ensures ConnectivityManager
2887 // instantiated.
2888 return getProcessDefaultNetwork();
2889 }
2890
2891 /**
2892 * Returns the {@link Network} currently bound to this process via
2893 * {@link #bindProcessToNetwork}, or {@code null} if no {@link Network} is explicitly bound.
2894 *
2895 * @return {@code Network} to which this process is bound, or {@code null}.
2896 * @deprecated Using this function can lead to other functions throwing
2897 * {@link IllegalStateException}. Use {@link #getBoundNetworkForProcess} instead.
2898 * {@code getBoundNetworkForProcess} is a direct replacement.
2899 */
Paul Jensen6d3ff9e2014-05-29 10:12:39 -04002900 public static Network getProcessDefaultNetwork() {
Paul Jensen72db88e2015-03-10 10:54:12 -04002901 int netId = NetworkUtils.getBoundNetworkForProcess();
Paul Jensenbcc76d32014-07-11 08:17:29 -04002902 if (netId == NETID_UNSET) return null;
Paul Jensen6d3ff9e2014-05-29 10:12:39 -04002903 return new Network(netId);
2904 }
2905
Lorenzo Colittiffc42b02015-07-29 11:41:21 +09002906 private void unsupportedStartingFrom(int version) {
2907 if (Process.myUid() == Process.SYSTEM_UID) {
2908 // The getApplicationInfo() call we make below is not supported in system context, and
2909 // we want to allow the system to use these APIs anyway.
2910 return;
2911 }
2912
2913 if (mContext.getApplicationInfo().targetSdkVersion >= version) {
2914 throw new UnsupportedOperationException(
2915 "This method is not supported in target SDK version " + version + " and above");
2916 }
2917 }
2918
2919 // Checks whether the calling app can use the legacy routing API (startUsingNetworkFeature,
2920 // stopUsingNetworkFeature, requestRouteToHost), and if not throw UnsupportedOperationException.
2921 // TODO: convert the existing system users (Tethering, GpsLocationProvider) to the new APIs and
2922 // remove these exemptions. Note that this check is not secure, and apps can still access these
2923 // functions by accessing ConnectivityService directly. However, it should be clear that doing
2924 // so is unsupported and may break in the future. http://b/22728205
2925 private void checkLegacyRoutingApiAccess() {
2926 if (mContext.checkCallingOrSelfPermission("com.android.permission.INJECT_OMADM_SETTINGS")
2927 == PackageManager.PERMISSION_GRANTED) {
2928 return;
2929 }
2930
Dianne Hackborn692a2442015-07-31 10:35:34 -07002931 unsupportedStartingFrom(VERSION_CODES.M);
Lorenzo Colittiffc42b02015-07-29 11:41:21 +09002932 }
2933
Paul Jensen6d3ff9e2014-05-29 10:12:39 -04002934 /**
2935 * Binds host resolutions performed by this process to {@code network}.
Paul Jensen72db88e2015-03-10 10:54:12 -04002936 * {@link #bindProcessToNetwork} takes precedence over this setting.
Paul Jensen6d3ff9e2014-05-29 10:12:39 -04002937 *
2938 * @param network The {@link Network} to bind host resolutions from the current process to, or
2939 * {@code null} to clear the current binding.
2940 * @return {@code true} on success, {@code false} if the {@link Network} is no longer valid.
2941 * @hide
2942 * @deprecated This is strictly for legacy usage to support {@link #startUsingNetworkFeature}.
2943 */
2944 public static boolean setProcessDefaultNetworkForHostResolution(Network network) {
Paul Jensenbcc76d32014-07-11 08:17:29 -04002945 return NetworkUtils.bindProcessToNetworkForHostResolution(
2946 network == null ? NETID_UNSET : network.netId);
Paul Jensen6d3ff9e2014-05-29 10:12:39 -04002947 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002948}