blob: 2b5afa725de6a1aa1c70e6ba1bd03235b8d29c20 [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;
Jeremy Klein36c7aa02016-01-22 14:11:45 -080019
Felipe Leme1b103232016-01-22 09:44:57 -080020import android.annotation.IntDef;
Robin Lee244ce8e2016-01-05 18:03:46 +000021import android.annotation.Nullable;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080022import android.annotation.SdkConstant;
23import android.annotation.SdkConstant.SdkConstantType;
Udam Sainib7c24872016-01-04 12:16:14 -080024import android.annotation.SystemApi;
Robert Greenwalt9258c642014-03-26 16:47:06 -070025import android.app.PendingIntent;
Jeff Sharkey8fc27e82012-04-04 20:40:58 -070026import android.content.Context;
Robert Greenwaltd19c41c2014-05-18 23:07:25 -070027import android.content.Intent;
Lorenzo Colittiffc42b02015-07-29 11:41:21 +090028import android.content.pm.PackageManager;
Robert Greenwalt42acef32009-08-12 16:08:25 -070029import android.os.Binder;
Jeff Sharkey3a844fc2011-08-16 14:37:57 -070030import android.os.Build.VERSION_CODES;
Jeremy Klein36c7aa02016-01-22 14:11:45 -080031import android.os.Bundle;
Robert Greenwalt9258c642014-03-26 16:47:06 -070032import android.os.Handler;
33import android.os.HandlerThread;
Dianne Hackborn77b987f2014-02-26 16:20:52 -080034import android.os.IBinder;
35import android.os.INetworkActivityListener;
36import android.os.INetworkManagementService;
Robert Greenwalt9258c642014-03-26 16:47:06 -070037import android.os.Looper;
38import android.os.Message;
Robert Greenwalt665e1ae2012-08-21 19:27:00 -070039import android.os.Messenger;
Lorenzo Colittiffc42b02015-07-29 11:41:21 +090040import android.os.Process;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080041import android.os.RemoteException;
Jeremy Klein36c7aa02016-01-22 14:11:45 -080042import android.os.ResultReceiver;
Dianne Hackborn77b987f2014-02-26 16:20:52 -080043import android.os.ServiceManager;
Jeff Sharkey961e3042011-08-29 16:02:57 -070044import android.provider.Settings;
Wink Saville36ffb042014-12-05 11:10:30 -080045import android.telephony.SubscriptionManager;
Dianne Hackborn77b987f2014-02-26 16:20:52 -080046import android.util.ArrayMap;
Robert Greenwalt9258c642014-03-26 16:47:06 -070047import android.util.Log;
Lorenzo Colittifcfa7d92016-03-01 22:56:37 +090048import android.util.SparseArray;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080049
Robert Greenwaltafa05c02014-05-21 20:04:36 -070050import com.android.internal.telephony.ITelephony;
Robert Greenwalt562cc542014-05-15 18:07:26 -070051import com.android.internal.telephony.PhoneConstants;
Robert Greenwaltafa05c02014-05-21 20:04:36 -070052import com.android.internal.util.Protocol;
Lorenzo Colittifcfa7d92016-03-01 22:56:37 +090053import com.android.internal.util.MessageUtils;
Robert Greenwaltafa05c02014-05-21 20:04:36 -070054
Paul Jensenc91b5342014-08-27 12:38:45 -040055import libcore.net.event.NetworkEventDispatcher;
56
Felipe Leme1b103232016-01-22 09:44:57 -080057import java.lang.annotation.Retention;
58import java.lang.annotation.RetentionPolicy;
Jeremy Kleind42209d2015-12-28 15:11:58 -080059import java.net.InetAddress;
60import java.util.HashMap;
61import java.util.concurrent.atomic.AtomicInteger;
62
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080063/**
64 * Class that answers queries about the state of network connectivity. It also
65 * notifies applications when network connectivity changes. Get an instance
66 * of this class by calling
67 * {@link android.content.Context#getSystemService(String) Context.getSystemService(Context.CONNECTIVITY_SERVICE)}.
68 * <p>
69 * The primary responsibilities of this class are to:
70 * <ol>
71 * <li>Monitor network connections (Wi-Fi, GPRS, UMTS, etc.)</li>
72 * <li>Send broadcast intents when network connectivity changes</li>
73 * <li>Attempt to "fail over" to another network when connectivity to a network
74 * is lost</li>
75 * <li>Provide an API that allows applications to query the coarse-grained or fine-grained
76 * state of the available networks</li>
Robert Greenwaltd19c41c2014-05-18 23:07:25 -070077 * <li>Provide an API that allows applications to request and select networks for their data
78 * traffic</li>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080079 * </ol>
80 */
Jeff Sharkeyc006f1a2011-05-19 17:12:49 -070081public class ConnectivityManager {
82 private static final String TAG = "ConnectivityManager";
83
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080084 /**
Robert Greenwaltd19c41c2014-05-18 23:07:25 -070085 * A change in network connectivity has occurred. A default connection has either
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080086 * been established or lost. The NetworkInfo for the affected network is
87 * sent as an extra; it should be consulted to see what kind of
88 * connectivity event occurred.
89 * <p/>
Mark Lu33ec1062016-12-05 10:57:55 -080090 * Apps targeting Android 7.0 (API level 24) and higher do not receive this
91 * broadcast if they declare the broadcast receiver in their manifest. Apps
92 * will still receive broadcasts if they register their
93 * {@link android.content.BroadcastReceiver} with
94 * {@link android.content.Context#registerReceiver Context.registerReceiver()}
95 * and that context is still valid.
96 * <p/>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080097 * If this is a connection that was the result of failing over from a
98 * disconnected network, then the FAILOVER_CONNECTION boolean extra is
99 * set to true.
100 * <p/>
101 * For a loss of connectivity, if the connectivity manager is attempting
102 * to connect (or has already connected) to another network, the
103 * NetworkInfo for the new network is also passed as an extra. This lets
104 * any receivers of the broadcast know that they should not necessarily
105 * tell the user that no data traffic will be possible. Instead, the
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800106 * receiver should expect another broadcast soon, indicating either that
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800107 * the failover attempt succeeded (and so there is still overall data
108 * connectivity), or that the failover attempt failed, meaning that all
109 * connectivity has been lost.
110 * <p/>
111 * For a disconnect event, the boolean extra EXTRA_NO_CONNECTIVITY
112 * is set to {@code true} if there are no connected networks at all.
113 */
Jeff Sharkey4fa63b22013-02-20 18:21:19 -0800114 @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800115 public static final String CONNECTIVITY_ACTION = "android.net.conn.CONNECTIVITY_CHANGE";
Jeff Sharkeyf0ceede2011-08-02 17:22:34 -0700116
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800117 /**
Robert Greenwalte94a6ff2015-09-01 08:23:04 -0700118 * A temporary hack until SUPL system can get off the legacy APIS.
119 * They do too many network requests and the long list of apps listening
120 * and waking due to the CONNECTIVITY_ACTION bcast makes it expensive.
121 * Use this bcast intent instead for SUPL requests.
122 * @hide
123 */
124 public static final String CONNECTIVITY_ACTION_SUPL =
125 "android.net.conn.CONNECTIVITY_CHANGE_SUPL";
126
127 /**
Paul Jensen25a217c2015-02-27 22:55:47 -0500128 * The device has connected to a network that has presented a captive
129 * portal, which is blocking Internet connectivity. The user was presented
130 * with a notification that network sign in is required,
131 * and the user invoked the notification's action indicating they
Paul Jensen49e3edf2015-05-22 10:50:39 -0400132 * desire to sign in to the network. Apps handling this activity should
Paul Jensen25a217c2015-02-27 22:55:47 -0500133 * facilitate signing in to the network. This action includes a
134 * {@link Network} typed extra called {@link #EXTRA_NETWORK} that represents
135 * the network presenting the captive portal; all communication with the
136 * captive portal must be done using this {@code Network} object.
137 * <p/>
Paul Jensen49e3edf2015-05-22 10:50:39 -0400138 * This activity includes a {@link CaptivePortal} extra named
139 * {@link #EXTRA_CAPTIVE_PORTAL} that can be used to indicate different
140 * outcomes of the captive portal sign in to the system:
141 * <ul>
142 * <li> When the app handling this action believes the user has signed in to
143 * the network and the captive portal has been dismissed, the app should
144 * call {@link CaptivePortal#reportCaptivePortalDismissed} so the system can
145 * reevaluate the network. If reevaluation finds the network no longer
146 * subject to a captive portal, the network may become the default active
147 * data network. </li>
148 * <li> When the app handling this action believes the user explicitly wants
Paul Jensen25a217c2015-02-27 22:55:47 -0500149 * to ignore the captive portal and the network, the app should call
Paul Jensen49e3edf2015-05-22 10:50:39 -0400150 * {@link CaptivePortal#ignoreNetwork}. </li>
151 * </ul>
Paul Jensen25a217c2015-02-27 22:55:47 -0500152 */
153 @SdkConstant(SdkConstantType.ACTIVITY_INTENT_ACTION)
154 public static final String ACTION_CAPTIVE_PORTAL_SIGN_IN = "android.net.conn.CAPTIVE_PORTAL";
155
156 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800157 * The lookup key for a {@link NetworkInfo} object. Retrieve with
158 * {@link android.content.Intent#getParcelableExtra(String)}.
Jeff Sharkeyf0ceede2011-08-02 17:22:34 -0700159 *
160 * @deprecated Since {@link NetworkInfo} can vary based on UID, applications
161 * should always obtain network information through
Paul Jensen3541e9f2015-03-18 12:23:02 -0400162 * {@link #getActiveNetworkInfo()}.
Jeff Sharkey75fbb4b2012-08-06 11:41:50 -0700163 * @see #EXTRA_NETWORK_TYPE
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800164 */
Jeff Sharkeyf0ceede2011-08-02 17:22:34 -0700165 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800166 public static final String EXTRA_NETWORK_INFO = "networkInfo";
Jeff Sharkeyf0ceede2011-08-02 17:22:34 -0700167
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800168 /**
Jeff Sharkey75fbb4b2012-08-06 11:41:50 -0700169 * Network type which triggered a {@link #CONNECTIVITY_ACTION} broadcast.
Jeff Sharkey75fbb4b2012-08-06 11:41:50 -0700170 *
171 * @see android.content.Intent#getIntExtra(String, int)
172 */
173 public static final String EXTRA_NETWORK_TYPE = "networkType";
174
175 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800176 * The lookup key for a boolean that indicates whether a connect event
177 * is for a network to which the connectivity manager was failing over
178 * following a disconnect on another network.
179 * Retrieve it with {@link android.content.Intent#getBooleanExtra(String,boolean)}.
180 */
181 public static final String EXTRA_IS_FAILOVER = "isFailover";
182 /**
183 * The lookup key for a {@link NetworkInfo} object. This is supplied when
184 * there is another network that it may be possible to connect to. Retrieve with
185 * {@link android.content.Intent#getParcelableExtra(String)}.
186 */
187 public static final String EXTRA_OTHER_NETWORK_INFO = "otherNetwork";
188 /**
189 * The lookup key for a boolean that indicates whether there is a
190 * complete lack of connectivity, i.e., no network is available.
191 * Retrieve it with {@link android.content.Intent#getBooleanExtra(String,boolean)}.
192 */
193 public static final String EXTRA_NO_CONNECTIVITY = "noConnectivity";
194 /**
195 * The lookup key for a string that indicates why an attempt to connect
196 * to a network failed. The string has no particular structure. It is
197 * intended to be used in notifications presented to users. Retrieve
198 * it with {@link android.content.Intent#getStringExtra(String)}.
199 */
200 public static final String EXTRA_REASON = "reason";
201 /**
202 * The lookup key for a string that provides optionally supplied
203 * extra information about the network state. The information
204 * may be passed up from the lower networking layers, and its
205 * meaning may be specific to a particular network type. Retrieve
206 * it with {@link android.content.Intent#getStringExtra(String)}.
207 */
208 public static final String EXTRA_EXTRA_INFO = "extraInfo";
Robert Greenwaltd7085fc2010-09-08 15:24:47 -0700209 /**
210 * The lookup key for an int that provides information about
211 * our connection to the internet at large. 0 indicates no connection,
212 * 100 indicates a great connection. Retrieve it with
Jeff Sharkeyc006f1a2011-05-19 17:12:49 -0700213 * {@link android.content.Intent#getIntExtra(String, int)}.
Robert Greenwaltd7085fc2010-09-08 15:24:47 -0700214 * {@hide}
215 */
216 public static final String EXTRA_INET_CONDITION = "inetCondition";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800217 /**
Paul Jensen49e3edf2015-05-22 10:50:39 -0400218 * The lookup key for a {@link CaptivePortal} object included with the
219 * {@link #ACTION_CAPTIVE_PORTAL_SIGN_IN} intent. The {@code CaptivePortal}
220 * object can be used to either indicate to the system that the captive
221 * portal has been dismissed or that the user does not want to pursue
222 * signing in to captive portal. Retrieve it with
223 * {@link android.content.Intent#getParcelableExtra(String)}.
Paul Jensen25a217c2015-02-27 22:55:47 -0500224 */
Paul Jensen49e3edf2015-05-22 10:50:39 -0400225 public static final String EXTRA_CAPTIVE_PORTAL = "android.net.extra.CAPTIVE_PORTAL";
Jan Nordqvist52eb29f2015-09-22 15:54:32 -0700226
227 /**
228 * Key for passing a URL to the captive portal login activity.
229 */
230 public static final String EXTRA_CAPTIVE_PORTAL_URL = "android.net.extra.CAPTIVE_PORTAL_URL";
231
Paul Jensen25a217c2015-02-27 22:55:47 -0500232 /**
Hugo Benichicdf3ba42016-12-14 08:23:40 +0900233 * Key for passing a user agent string to the captive portal login activity.
234 * {@hide}
235 */
236 public static final String EXTRA_CAPTIVE_PORTAL_USER_AGENT =
237 "android.net.extra.CAPTIVE_PORTAL_USER_AGENT";
238
239 /**
Haoyu Baidb3c8672012-06-20 14:29:57 -0700240 * Broadcast action to indicate the change of data activity status
241 * (idle or active) on a network in a recent period.
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800242 * The network becomes active when data transmission is started, or
243 * idle if there is no data transmission for a period of time.
Haoyu Baidb3c8672012-06-20 14:29:57 -0700244 * {@hide}
245 */
246 @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION)
247 public static final String ACTION_DATA_ACTIVITY_CHANGE = "android.net.conn.DATA_ACTIVITY_CHANGE";
248 /**
249 * The lookup key for an enum that indicates the network device type on which this data activity
250 * change happens.
251 * {@hide}
252 */
253 public static final String EXTRA_DEVICE_TYPE = "deviceType";
254 /**
255 * The lookup key for a boolean that indicates the device is active or not. {@code true} means
256 * it is actively sending or receiving data and {@code false} means it is idle.
257 * {@hide}
258 */
259 public static final String EXTRA_IS_ACTIVE = "isActive";
Ashish Sharma0535a9f2014-03-12 18:42:23 -0700260 /**
261 * The lookup key for a long that contains the timestamp (nanos) of the radio state change.
262 * {@hide}
263 */
264 public static final String EXTRA_REALTIME_NS = "tsNanos";
Haoyu Baidb3c8672012-06-20 14:29:57 -0700265
266 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800267 * Broadcast Action: The setting for background data usage has changed
268 * values. Use {@link #getBackgroundDataSetting()} to get the current value.
269 * <p>
270 * If an application uses the network in the background, it should listen
271 * for this broadcast and stop using the background data if the value is
Jeff Sharkeyc006f1a2011-05-19 17:12:49 -0700272 * {@code false}.
Jeff Sharkey54ee2ad2012-01-30 16:29:24 -0800273 * <p>
274 *
275 * @deprecated As of {@link VERSION_CODES#ICE_CREAM_SANDWICH}, availability
276 * of background data depends on several combined factors, and
277 * this broadcast is no longer sent. Instead, when background
278 * data is unavailable, {@link #getActiveNetworkInfo()} will now
279 * appear disconnected. During first boot after a platform
280 * upgrade, this broadcast will be sent once if
281 * {@link #getBackgroundDataSetting()} was {@code false} before
282 * the upgrade.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800283 */
284 @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION)
Jeff Sharkey54ee2ad2012-01-30 16:29:24 -0800285 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800286 public static final String ACTION_BACKGROUND_DATA_SETTING_CHANGED =
287 "android.net.conn.BACKGROUND_DATA_SETTING_CHANGED";
288
Robert Greenwalt1e9aac22010-09-15 17:36:33 -0700289 /**
290 * Broadcast Action: The network connection may not be good
291 * uses {@code ConnectivityManager.EXTRA_INET_CONDITION} and
292 * {@code ConnectivityManager.EXTRA_NETWORK_INFO} to specify
293 * the network and it's condition.
294 * @hide
295 */
Jeff Sharkey4fa63b22013-02-20 18:21:19 -0800296 @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION)
Robert Greenwalt1e9aac22010-09-15 17:36:33 -0700297 public static final String INET_CONDITION_ACTION =
298 "android.net.conn.INET_CONDITION_ACTION";
299
Robert Greenwalt42acef32009-08-12 16:08:25 -0700300 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800301 * Broadcast Action: A tetherable connection has come or gone.
302 * Uses {@code ConnectivityManager.EXTRA_AVAILABLE_TETHER},
303 * {@code ConnectivityManager.EXTRA_ACTIVE_TETHER} and
304 * {@code ConnectivityManager.EXTRA_ERRORED_TETHER} to indicate
305 * the current state of tethering. Each include a list of
306 * interface names in that state (may be empty).
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -0800307 * @hide
308 */
Jeff Sharkey4fa63b22013-02-20 18:21:19 -0800309 @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION)
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -0800310 public static final String ACTION_TETHER_STATE_CHANGED =
311 "android.net.conn.TETHER_STATE_CHANGED";
312
313 /**
314 * @hide
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800315 * gives a String[] listing all the interfaces configured for
316 * tethering and currently available for tethering.
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -0800317 */
Robert Greenwalt2a091d72010-02-11 18:18:40 -0800318 public static final String EXTRA_AVAILABLE_TETHER = "availableArray";
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -0800319
320 /**
321 * @hide
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800322 * gives a String[] listing all the interfaces currently tethered
323 * (ie, has dhcp support and packets potentially forwarded/NATed)
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -0800324 */
Robert Greenwalt2a091d72010-02-11 18:18:40 -0800325 public static final String EXTRA_ACTIVE_TETHER = "activeArray";
326
327 /**
328 * @hide
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800329 * gives a String[] listing all the interfaces we tried to tether and
330 * failed. Use {@link #getLastTetherError} to find the error code
331 * for any interfaces listed here.
Robert Greenwalt2a091d72010-02-11 18:18:40 -0800332 */
333 public static final String EXTRA_ERRORED_TETHER = "erroredArray";
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -0800334
335 /**
Russell Brenner108da0c2013-02-12 10:03:14 -0800336 * Broadcast Action: The captive portal tracker has finished its test.
337 * Sent only while running Setup Wizard, in lieu of showing a user
338 * notification.
339 * @hide
340 */
Jeff Sharkey4fa63b22013-02-20 18:21:19 -0800341 @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION)
Russell Brenner108da0c2013-02-12 10:03:14 -0800342 public static final String ACTION_CAPTIVE_PORTAL_TEST_COMPLETED =
343 "android.net.conn.CAPTIVE_PORTAL_TEST_COMPLETED";
344 /**
345 * The lookup key for a boolean that indicates whether a captive portal was detected.
346 * Retrieve it with {@link android.content.Intent#getBooleanExtra(String,boolean)}.
347 * @hide
348 */
349 public static final String EXTRA_IS_CAPTIVE_PORTAL = "captivePortal";
350
351 /**
Lorenzo Colittie03c3c72015-04-03 16:38:52 +0900352 * Action used to display a dialog that asks the user whether to connect to a network that is
353 * not validated. This intent is used to start the dialog in settings via startActivity.
354 *
355 * @hide
356 */
357 public static final String ACTION_PROMPT_UNVALIDATED = "android.net.conn.PROMPT_UNVALIDATED";
358
359 /**
Lorenzo Colitti9be58c52016-09-15 14:02:29 +0900360 * Action used to display a dialog that asks the user whether to avoid a network that is no
361 * longer validated. This intent is used to start the dialog in settings via startActivity.
362 *
363 * @hide
364 */
365 public static final String ACTION_PROMPT_LOST_VALIDATION =
366 "android.net.conn.PROMPT_LOST_VALIDATION";
367
368 /**
Jeremy Klein36c7aa02016-01-22 14:11:45 -0800369 * Invalid tethering type.
370 * @see #startTethering(int, OnStartTetheringCallback, boolean)
371 * @hide
372 */
373 public static final int TETHERING_INVALID = -1;
374
375 /**
376 * Wifi tethering type.
377 * @see #startTethering(int, OnStartTetheringCallback, boolean)
378 * @hide
379 */
380 @SystemApi
381 public static final int TETHERING_WIFI = 0;
382
383 /**
384 * USB tethering type.
385 * @see #startTethering(int, OnStartTetheringCallback, boolean)
386 * @hide
387 */
388 @SystemApi
389 public static final int TETHERING_USB = 1;
390
391 /**
392 * Bluetooth tethering type.
393 * @see #startTethering(int, OnStartTetheringCallback, boolean)
394 * @hide
395 */
396 @SystemApi
397 public static final int TETHERING_BLUETOOTH = 2;
398
399 /**
400 * Extra used for communicating with the TetherService. Includes the type of tethering to
401 * enable if any.
402 * @hide
403 */
404 public static final String EXTRA_ADD_TETHER_TYPE = "extraAddTetherType";
405
406 /**
407 * Extra used for communicating with the TetherService. Includes the type of tethering for
408 * which to cancel provisioning.
409 * @hide
410 */
411 public static final String EXTRA_REM_TETHER_TYPE = "extraRemTetherType";
412
413 /**
414 * Extra used for communicating with the TetherService. True to schedule a recheck of tether
415 * provisioning.
416 * @hide
417 */
418 public static final String EXTRA_SET_ALARM = "extraSetAlarm";
419
420 /**
421 * Tells the TetherService to run a provision check now.
422 * @hide
423 */
424 public static final String EXTRA_RUN_PROVISION = "extraRunProvision";
425
426 /**
427 * Extra used for communicating with the TetherService. Contains the {@link ResultReceiver}
428 * which will receive provisioning results. Can be left empty.
429 * @hide
430 */
431 public static final String EXTRA_PROVISION_CALLBACK = "extraProvisionCallback";
432
433 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800434 * The absence of a connection type.
Robert Greenwaltccf83af12011-06-02 17:30:47 -0700435 * @hide
436 */
437 public static final int TYPE_NONE = -1;
438
439 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800440 * The Mobile data connection. When active, all data traffic
441 * will use this network type's interface by default
442 * (it has a default route)
Robert Greenwalt42acef32009-08-12 16:08:25 -0700443 */
444 public static final int TYPE_MOBILE = 0;
445 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800446 * The WIFI data connection. When active, all data traffic
447 * will use this network type's interface by default
448 * (it has a default route).
Robert Greenwalt42acef32009-08-12 16:08:25 -0700449 */
450 public static final int TYPE_WIFI = 1;
451 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800452 * An MMS-specific Mobile data connection. This network type may use the
453 * same network interface as {@link #TYPE_MOBILE} or it may use a different
454 * one. This is used by applications needing to talk to the carrier's
455 * Multimedia Messaging Service servers.
Lorenzo Colittie285b432015-04-23 15:32:42 +0900456 *
Lorenzo Colitti2ea89e52015-04-24 17:03:31 +0900457 * @deprecated Applications should instead use
458 * {@link #requestNetwork(NetworkRequest, NetworkCallback)} to request a network that
Lorenzo Colittie285b432015-04-23 15:32:42 +0900459 * provides the {@link NetworkCapabilities#NET_CAPABILITY_MMS} capability.
Robert Greenwalt42acef32009-08-12 16:08:25 -0700460 */
461 public static final int TYPE_MOBILE_MMS = 2;
462 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800463 * A SUPL-specific Mobile data connection. This network type may use the
464 * same network interface as {@link #TYPE_MOBILE} or it may use a different
465 * one. This is used by applications needing to talk to the carrier's
466 * Secure User Plane Location servers for help locating the device.
Lorenzo Colittie285b432015-04-23 15:32:42 +0900467 *
Lorenzo Colitti2ea89e52015-04-24 17:03:31 +0900468 * @deprecated Applications should instead use
469 * {@link #requestNetwork(NetworkRequest, NetworkCallback)} to request a network that
Lorenzo Colittie285b432015-04-23 15:32:42 +0900470 * provides the {@link NetworkCapabilities#NET_CAPABILITY_SUPL} capability.
Robert Greenwalt42acef32009-08-12 16:08:25 -0700471 */
472 public static final int TYPE_MOBILE_SUPL = 3;
473 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800474 * A DUN-specific Mobile data connection. This network type may use the
475 * same network interface as {@link #TYPE_MOBILE} or it may use a different
476 * one. This is sometimes by the system when setting up an upstream connection
477 * for tethering so that the carrier is aware of DUN traffic.
Robert Greenwalt42acef32009-08-12 16:08:25 -0700478 */
479 public static final int TYPE_MOBILE_DUN = 4;
480 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800481 * A High Priority Mobile data connection. This network type uses the
482 * same network interface as {@link #TYPE_MOBILE} but the routing setup
Lorenzo Colittie285b432015-04-23 15:32:42 +0900483 * is different.
484 *
Lorenzo Colitti2ea89e52015-04-24 17:03:31 +0900485 * @deprecated Applications should instead use
486 * {@link #requestNetwork(NetworkRequest, NetworkCallback)} to request a network that
Lorenzo Colittie285b432015-04-23 15:32:42 +0900487 * uses the {@link NetworkCapabilities#TRANSPORT_CELLULAR} transport.
Robert Greenwalt42acef32009-08-12 16:08:25 -0700488 */
489 public static final int TYPE_MOBILE_HIPRI = 5;
jsh8214deb2010-03-11 15:04:43 -0800490 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800491 * The WiMAX data connection. When active, all data traffic
492 * will use this network type's interface by default
493 * (it has a default route).
jsh8214deb2010-03-11 15:04:43 -0800494 */
495 public static final int TYPE_WIMAX = 6;
Robert Greenwaltda3d5e62010-12-06 13:56:24 -0800496
Jaikumar Ganesh15c74392010-12-21 22:31:44 -0800497 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800498 * The Bluetooth data connection. When active, all data traffic
499 * will use this network type's interface by default
500 * (it has a default route).
Jaikumar Ganesh15c74392010-12-21 22:31:44 -0800501 */
502 public static final int TYPE_BLUETOOTH = 7;
503
Robert Greenwalt60810842011-04-22 15:28:18 -0700504 /**
505 * Dummy data connection. This should not be used on shipping devices.
506 */
Jaikumar Ganesh15c74392010-12-21 22:31:44 -0800507 public static final int TYPE_DUMMY = 8;
Wink Saville9d7d6282011-03-12 14:52:01 -0800508
Robert Greenwalt60810842011-04-22 15:28:18 -0700509 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800510 * The Ethernet data connection. When active, all data traffic
511 * will use this network type's interface by default
512 * (it has a default route).
Robert Greenwalt60810842011-04-22 15:28:18 -0700513 */
Robert Greenwalte12aec92011-01-28 14:48:37 -0800514 public static final int TYPE_ETHERNET = 9;
Robert Greenwalt60810842011-04-22 15:28:18 -0700515
Wink Saville9d7d6282011-03-12 14:52:01 -0800516 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800517 * Over the air Administration.
Wink Saville9d7d6282011-03-12 14:52:01 -0800518 * {@hide}
519 */
520 public static final int TYPE_MOBILE_FOTA = 10;
521
522 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800523 * IP Multimedia Subsystem.
Wink Saville9d7d6282011-03-12 14:52:01 -0800524 * {@hide}
525 */
526 public static final int TYPE_MOBILE_IMS = 11;
527
528 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800529 * Carrier Branded Services.
Wink Saville9d7d6282011-03-12 14:52:01 -0800530 * {@hide}
531 */
532 public static final int TYPE_MOBILE_CBS = 12;
533
repo syncaea743a2011-07-29 23:55:49 -0700534 /**
535 * A Wi-Fi p2p connection. Only requesting processes will have access to
536 * the peers connected.
537 * {@hide}
538 */
539 public static final int TYPE_WIFI_P2P = 13;
Wink Saville9d7d6282011-03-12 14:52:01 -0800540
Wink Saville5e56bc52013-07-29 15:00:57 -0700541 /**
542 * The network to use for initially attaching to the network
543 * {@hide}
544 */
545 public static final int TYPE_MOBILE_IA = 14;
repo syncaea743a2011-07-29 23:55:49 -0700546
Lorenzo Colittie285b432015-04-23 15:32:42 +0900547 /**
Robert Greenwalt4bd43892015-07-09 14:49:35 -0700548 * Emergency PDN connection for emergency services. This
549 * may include IMS and MMS in emergency situations.
Ram3e0e3bc2014-06-26 11:03:44 -0700550 * {@hide}
551 */
552 public static final int TYPE_MOBILE_EMERGENCY = 15;
553
Hui Lu1c5624a2014-01-15 11:05:36 -0500554 /**
555 * The network that uses proxy to achieve connectivity.
556 * {@hide}
557 */
558 public static final int TYPE_PROXY = 16;
Wink Saville5e56bc52013-07-29 15:00:57 -0700559
Robert Greenwalt8283f882014-07-07 17:09:01 -0700560 /**
561 * A virtual network using one or more native bearers.
562 * It may or may not be providing security services.
563 */
564 public static final int TYPE_VPN = 17;
Hui Lu1c5624a2014-01-15 11:05:36 -0500565
566 /** {@hide} */
Robert Greenwalt8283f882014-07-07 17:09:01 -0700567 public static final int MAX_RADIO_TYPE = TYPE_VPN;
568
569 /** {@hide} */
570 public static final int MAX_NETWORK_TYPE = TYPE_VPN;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800571
Jianzheng Zhoudcf03f32012-11-16 13:45:20 +0800572 /**
573 * If you want to set the default network preference,you can directly
574 * change the networkAttributes array in framework's config.xml.
575 *
576 * @deprecated Since we support so many more networks now, the single
577 * network default network preference can't really express
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800578 * the hierarchy. Instead, the default is defined by the
Jianzheng Zhoudcf03f32012-11-16 13:45:20 +0800579 * networkAttributes in config.xml. You can determine
Robert Greenwalt4c8b7482012-12-07 09:56:50 -0800580 * the current value by calling {@link #getNetworkPreference()}
Jianzheng Zhoudcf03f32012-11-16 13:45:20 +0800581 * from an App.
582 */
583 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800584 public static final int DEFAULT_NETWORK_PREFERENCE = TYPE_WIFI;
585
Jeff Sharkey625239a2012-09-26 22:03:49 -0700586 /**
Robert Greenwalt9ba9c582014-03-19 17:56:12 -0700587 * @hide
588 */
Robert Greenwalt7569f182014-06-08 16:42:59 -0700589 public final static int REQUEST_ID_UNSET = 0;
590
Paul Jensen5d59e782014-07-11 12:28:19 -0400591 /**
592 * A NetID indicating no Network is selected.
593 * Keep in sync with bionic/libc/dns/include/resolv_netid.h
594 * @hide
595 */
596 public static final int NETID_UNSET = 0;
597
Jeff Sharkeyc006f1a2011-05-19 17:12:49 -0700598 private final IConnectivityManager mService;
Paul Jensene0bef712014-12-10 15:12:18 -0500599 /**
600 * A kludge to facilitate static access where a Context pointer isn't available, like in the
601 * case of the static set/getProcessDefaultNetwork methods and from the Network class.
602 * TODO: Remove this after deprecating the static methods in favor of non-static methods or
603 * methods that take a Context argument.
604 */
605 private static ConnectivityManager sInstance;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800606
Lorenzo Colittiffc42b02015-07-29 11:41:21 +0900607 private final Context mContext;
608
Dianne Hackborn77b987f2014-02-26 16:20:52 -0800609 private INetworkManagementService mNMService;
Felipe Leme1b103232016-01-22 09:44:57 -0800610 private INetworkPolicyManager mNPManager;
Dianne Hackborn77b987f2014-02-26 16:20:52 -0800611
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800612 /**
613 * Tests if a given integer represents a valid network type.
614 * @param networkType the type to be tested
615 * @return a boolean. {@code true} if the type is valid, else {@code false}
Paul Jensen9e59e122015-05-06 10:42:25 -0400616 * @deprecated All APIs accepting a network type are deprecated. There should be no need to
617 * validate a network type.
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800618 */
Jeff Sharkeyd2a45872011-05-28 20:56:34 -0700619 public static boolean isNetworkTypeValid(int networkType) {
Robert Greenwalt42acef32009-08-12 16:08:25 -0700620 return networkType >= 0 && networkType <= MAX_NETWORK_TYPE;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800621 }
622
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800623 /**
624 * Returns a non-localized string representing a given network type.
625 * ONLY used for debugging output.
626 * @param type the type needing naming
627 * @return a String for the given type, or a string version of the type ("87")
628 * if no name is known.
629 * {@hide}
630 */
Jeff Sharkeyd2a45872011-05-28 20:56:34 -0700631 public static String getNetworkTypeName(int type) {
632 switch (type) {
633 case TYPE_MOBILE:
634 return "MOBILE";
635 case TYPE_WIFI:
636 return "WIFI";
637 case TYPE_MOBILE_MMS:
638 return "MOBILE_MMS";
639 case TYPE_MOBILE_SUPL:
640 return "MOBILE_SUPL";
641 case TYPE_MOBILE_DUN:
642 return "MOBILE_DUN";
643 case TYPE_MOBILE_HIPRI:
644 return "MOBILE_HIPRI";
645 case TYPE_WIMAX:
646 return "WIMAX";
647 case TYPE_BLUETOOTH:
648 return "BLUETOOTH";
649 case TYPE_DUMMY:
650 return "DUMMY";
651 case TYPE_ETHERNET:
652 return "ETHERNET";
653 case TYPE_MOBILE_FOTA:
654 return "MOBILE_FOTA";
655 case TYPE_MOBILE_IMS:
656 return "MOBILE_IMS";
657 case TYPE_MOBILE_CBS:
658 return "MOBILE_CBS";
repo syncaea743a2011-07-29 23:55:49 -0700659 case TYPE_WIFI_P2P:
660 return "WIFI_P2P";
Wink Saville5e56bc52013-07-29 15:00:57 -0700661 case TYPE_MOBILE_IA:
662 return "MOBILE_IA";
Ram3e0e3bc2014-06-26 11:03:44 -0700663 case TYPE_MOBILE_EMERGENCY:
664 return "MOBILE_EMERGENCY";
Hui Lu1c5624a2014-01-15 11:05:36 -0500665 case TYPE_PROXY:
666 return "PROXY";
Erik Kline37fbfa12014-11-19 17:23:41 +0900667 case TYPE_VPN:
668 return "VPN";
Jeff Sharkeyd2a45872011-05-28 20:56:34 -0700669 default:
670 return Integer.toString(type);
671 }
672 }
673
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800674 /**
675 * Checks if a given type uses the cellular data connection.
676 * This should be replaced in the future by a network property.
677 * @param networkType the type to check
678 * @return a boolean - {@code true} if uses cellular network, else {@code false}
679 * {@hide}
680 */
Jeff Sharkeyd2a45872011-05-28 20:56:34 -0700681 public static boolean isNetworkTypeMobile(int networkType) {
682 switch (networkType) {
683 case TYPE_MOBILE:
684 case TYPE_MOBILE_MMS:
685 case TYPE_MOBILE_SUPL:
686 case TYPE_MOBILE_DUN:
687 case TYPE_MOBILE_HIPRI:
688 case TYPE_MOBILE_FOTA:
689 case TYPE_MOBILE_IMS:
690 case TYPE_MOBILE_CBS:
Wink Saville5e56bc52013-07-29 15:00:57 -0700691 case TYPE_MOBILE_IA:
Ram3e0e3bc2014-06-26 11:03:44 -0700692 case TYPE_MOBILE_EMERGENCY:
Jeff Sharkeyd2a45872011-05-28 20:56:34 -0700693 return true;
694 default:
695 return false;
696 }
697 }
698
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800699 /**
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -0700700 * Checks if the given network type is backed by a Wi-Fi radio.
701 *
702 * @hide
703 */
704 public static boolean isNetworkTypeWifi(int networkType) {
705 switch (networkType) {
706 case TYPE_WIFI:
707 case TYPE_WIFI_P2P:
708 return true;
709 default:
710 return false;
711 }
712 }
713
714 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800715 * Specifies the preferred network type. When the device has more
716 * than one type available the preferred network type will be used.
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800717 *
718 * @param preference the network type to prefer over all others. It is
719 * unspecified what happens to the old preferred network in the
720 * overall ordering.
Robert Greenwaltd19c41c2014-05-18 23:07:25 -0700721 * @deprecated Functionality has been removed as it no longer makes sense,
722 * with many more than two networks - we'd need an array to express
723 * preference. Instead we use dynamic network properties of
724 * the networks to describe their precedence.
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800725 */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800726 public void setNetworkPreference(int preference) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800727 }
728
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800729 /**
730 * Retrieves the current preferred network type.
Paul Jensenb2748922015-05-06 11:10:18 -0400731 * <p>This method requires the caller to hold the permission
732 * {@link android.Manifest.permission#ACCESS_NETWORK_STATE}.
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800733 *
734 * @return an integer representing the preferred network type
735 *
Robert Greenwaltd19c41c2014-05-18 23:07:25 -0700736 * @deprecated Functionality has been removed as it no longer makes sense,
737 * with many more than two networks - we'd need an array to express
738 * preference. Instead we use dynamic network properties of
739 * the networks to describe their precedence.
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800740 */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800741 public int getNetworkPreference() {
Robert Greenwaltd19c41c2014-05-18 23:07:25 -0700742 return TYPE_NONE;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800743 }
744
Scott Main671644c2011-10-06 19:02:28 -0700745 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800746 * Returns details about the currently active default data network. When
747 * connected, this network is the default route for outgoing connections.
748 * You should always check {@link NetworkInfo#isConnected()} before initiating
749 * network traffic. This may return {@code null} when there is no default
750 * network.
Paul Jensenb2748922015-05-06 11:10:18 -0400751 * <p>This method requires the caller to hold the permission
752 * {@link android.Manifest.permission#ACCESS_NETWORK_STATE}.
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800753 *
754 * @return a {@link NetworkInfo} object for the current default network
Paul Jensen0d719ca2015-02-13 14:18:39 -0500755 * or {@code null} if no default network is currently active
Jeff Sharkey9f7cbf02012-04-12 18:34:54 -0700756 */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800757 public NetworkInfo getActiveNetworkInfo() {
758 try {
759 return mService.getActiveNetworkInfo();
760 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -0700761 throw e.rethrowFromSystemServer();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800762 }
763 }
764
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800765 /**
Paul Jensen31a94f42015-02-13 14:18:39 -0500766 * Returns a {@link Network} object corresponding to the currently active
767 * default data network. In the event that the current active default data
768 * network disconnects, the returned {@code Network} object will no longer
769 * be usable. This will return {@code null} when there is no default
770 * network.
Paul Jensenb2748922015-05-06 11:10:18 -0400771 * <p>This method requires the caller to hold the permission
772 * {@link android.Manifest.permission#ACCESS_NETWORK_STATE}.
Paul Jensen31a94f42015-02-13 14:18:39 -0500773 *
774 * @return a {@link Network} object for the current default network or
775 * {@code null} if no default network is currently active
Paul Jensen31a94f42015-02-13 14:18:39 -0500776 */
777 public Network getActiveNetwork() {
778 try {
779 return mService.getActiveNetwork();
780 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -0700781 throw e.rethrowFromSystemServer();
Paul Jensen31a94f42015-02-13 14:18:39 -0500782 }
783 }
784
785 /**
Robin Leed2baf792016-03-24 12:07:00 +0000786 * Returns a {@link Network} object corresponding to the currently active
787 * default data network for a specific UID. In the event that the default data
788 * network disconnects, the returned {@code Network} object will no longer
789 * be usable. This will return {@code null} when there is no default
790 * network for the UID.
791 * <p>This method requires the caller to hold the permission
792 * {@link android.Manifest.permission#CONNECTIVITY_INTERNAL}.
793 *
794 * @return a {@link Network} object for the current default network for the
795 * given UID or {@code null} if no default network is currently active
796 *
797 * @hide
798 */
799 public Network getActiveNetworkForUid(int uid) {
Jeff Sharkey1b6519b2016-04-28 15:33:18 -0600800 return getActiveNetworkForUid(uid, false);
801 }
802
803 /** {@hide} */
804 public Network getActiveNetworkForUid(int uid, boolean ignoreBlocked) {
Robin Leed2baf792016-03-24 12:07:00 +0000805 try {
Jeff Sharkey1b6519b2016-04-28 15:33:18 -0600806 return mService.getActiveNetworkForUid(uid, ignoreBlocked);
Robin Leed2baf792016-03-24 12:07:00 +0000807 } catch (RemoteException e) {
808 throw e.rethrowFromSystemServer();
809 }
810 }
811
812 /**
Robin Lee244ce8e2016-01-05 18:03:46 +0000813 * Configures an always-on VPN connection through a specific application.
814 * This connection is automatically granted and persisted after a reboot.
815 *
816 * <p>The designated package should declare a {@link VpnService} in its
817 * manifest guarded by {@link android.Manifest.permission.BIND_VPN_SERVICE},
818 * otherwise the call will fail.
819 *
820 * @param userId The identifier of the user to set an always-on VPN for.
821 * @param vpnPackage The package name for an installed VPN app on the device, or {@code null}
822 * to remove an existing always-on VPN configuration.
Robin Leedc679712016-05-03 13:23:03 +0100823 * @param lockdownEnabled {@code true} to disallow networking when the VPN is not connected or
824 * {@code false} otherwise.
Robin Lee244ce8e2016-01-05 18:03:46 +0000825 * @return {@code true} if the package is set as always-on VPN controller;
826 * {@code false} otherwise.
827 * @hide
828 */
Robin Leedc679712016-05-03 13:23:03 +0100829 public boolean setAlwaysOnVpnPackageForUser(int userId, @Nullable String vpnPackage,
830 boolean lockdownEnabled) {
Robin Lee244ce8e2016-01-05 18:03:46 +0000831 try {
Robin Leedc679712016-05-03 13:23:03 +0100832 return mService.setAlwaysOnVpnPackage(userId, vpnPackage, lockdownEnabled);
Robin Lee244ce8e2016-01-05 18:03:46 +0000833 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -0700834 throw e.rethrowFromSystemServer();
Robin Lee244ce8e2016-01-05 18:03:46 +0000835 }
836 }
837
838 /**
839 * Returns the package name of the currently set always-on VPN application.
840 * If there is no always-on VPN set, or the VPN is provided by the system instead
841 * of by an app, {@code null} will be returned.
842 *
843 * @return Package name of VPN controller responsible for always-on VPN,
844 * or {@code null} if none is set.
845 * @hide
846 */
847 public String getAlwaysOnVpnPackageForUser(int userId) {
848 try {
849 return mService.getAlwaysOnVpnPackage(userId);
850 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -0700851 throw e.rethrowFromSystemServer();
Robin Lee244ce8e2016-01-05 18:03:46 +0000852 }
853 }
854
855 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800856 * Returns details about the currently active default data network
857 * for a given uid. This is for internal use only to avoid spying
858 * other apps.
Paul Jensenb2748922015-05-06 11:10:18 -0400859 * <p>This method requires the caller to hold the permission
860 * {@link android.Manifest.permission#CONNECTIVITY_INTERNAL}
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800861 *
862 * @return a {@link NetworkInfo} object for the current default network
863 * for the given uid or {@code null} if no default network is
864 * available for the specified uid.
865 *
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800866 * {@hide}
867 */
Jeff Sharkeyc006f1a2011-05-19 17:12:49 -0700868 public NetworkInfo getActiveNetworkInfoForUid(int uid) {
Jeff Sharkey1b6519b2016-04-28 15:33:18 -0600869 return getActiveNetworkInfoForUid(uid, false);
870 }
871
872 /** {@hide} */
873 public NetworkInfo getActiveNetworkInfoForUid(int uid, boolean ignoreBlocked) {
Jeff Sharkeyc006f1a2011-05-19 17:12:49 -0700874 try {
Jeff Sharkey1b6519b2016-04-28 15:33:18 -0600875 return mService.getActiveNetworkInfoForUid(uid, ignoreBlocked);
Jeff Sharkeyc006f1a2011-05-19 17:12:49 -0700876 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -0700877 throw e.rethrowFromSystemServer();
Jeff Sharkeyc006f1a2011-05-19 17:12:49 -0700878 }
879 }
880
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800881 /**
882 * Returns connection status information about a particular
883 * network type.
Paul Jensenb2748922015-05-06 11:10:18 -0400884 * <p>This method requires the caller to hold the permission
885 * {@link android.Manifest.permission#ACCESS_NETWORK_STATE}.
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800886 *
887 * @param networkType integer specifying which networkType in
888 * which you're interested.
889 * @return a {@link NetworkInfo} object for the requested
890 * network type or {@code null} if the type is not
891 * supported by the device.
892 *
Paul Jensen3541e9f2015-03-18 12:23:02 -0400893 * @deprecated This method does not support multiple connected networks
894 * of the same type. Use {@link #getAllNetworks} and
895 * {@link #getNetworkInfo(android.net.Network)} instead.
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800896 */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800897 public NetworkInfo getNetworkInfo(int networkType) {
898 try {
899 return mService.getNetworkInfo(networkType);
900 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -0700901 throw e.rethrowFromSystemServer();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800902 }
903 }
904
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800905 /**
Robert Greenwalt73b6cbae2014-06-23 11:40:00 -0700906 * Returns connection status information about a particular
907 * Network.
Paul Jensenb2748922015-05-06 11:10:18 -0400908 * <p>This method requires the caller to hold the permission
909 * {@link android.Manifest.permission#ACCESS_NETWORK_STATE}.
Robert Greenwalt73b6cbae2014-06-23 11:40:00 -0700910 *
911 * @param network {@link Network} specifying which network
912 * in which you're interested.
913 * @return a {@link NetworkInfo} object for the requested
914 * network or {@code null} if the {@code Network}
915 * is not valid.
Robert Greenwalt73b6cbae2014-06-23 11:40:00 -0700916 */
917 public NetworkInfo getNetworkInfo(Network network) {
Jeff Sharkey1b6519b2016-04-28 15:33:18 -0600918 return getNetworkInfoForUid(network, Process.myUid(), false);
919 }
920
921 /** {@hide} */
922 public NetworkInfo getNetworkInfoForUid(Network network, int uid, boolean ignoreBlocked) {
Robert Greenwalt73b6cbae2014-06-23 11:40:00 -0700923 try {
Jeff Sharkey1b6519b2016-04-28 15:33:18 -0600924 return mService.getNetworkInfoForUid(network, uid, ignoreBlocked);
Robert Greenwalt73b6cbae2014-06-23 11:40:00 -0700925 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -0700926 throw e.rethrowFromSystemServer();
Robert Greenwalt73b6cbae2014-06-23 11:40:00 -0700927 }
928 }
929
930 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800931 * Returns connection status information about all network
932 * types supported by the device.
Paul Jensenb2748922015-05-06 11:10:18 -0400933 * <p>This method requires the caller to hold the permission
934 * {@link android.Manifest.permission#ACCESS_NETWORK_STATE}.
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800935 *
936 * @return an array of {@link NetworkInfo} objects. Check each
937 * {@link NetworkInfo#getType} for which type each applies.
938 *
Paul Jensen3541e9f2015-03-18 12:23:02 -0400939 * @deprecated This method does not support multiple connected networks
940 * of the same type. Use {@link #getAllNetworks} and
941 * {@link #getNetworkInfo(android.net.Network)} instead.
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800942 */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800943 public NetworkInfo[] getAllNetworkInfo() {
944 try {
945 return mService.getAllNetworkInfo();
946 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -0700947 throw e.rethrowFromSystemServer();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800948 }
949 }
950
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800951 /**
Lorenzo Colittib57edc52014-08-22 17:10:50 -0700952 * Returns the {@link Network} object currently serving a given type, or
953 * null if the given type is not connected.
954 *
955 * <p>This method requires the caller to hold the permission
956 * {@link android.Manifest.permission#ACCESS_NETWORK_STATE}.
957 *
958 * @hide
Paul Jensen3541e9f2015-03-18 12:23:02 -0400959 * @deprecated This method does not support multiple connected networks
960 * of the same type. Use {@link #getAllNetworks} and
961 * {@link #getNetworkInfo(android.net.Network)} instead.
Lorenzo Colittib57edc52014-08-22 17:10:50 -0700962 */
963 public Network getNetworkForType(int networkType) {
964 try {
965 return mService.getNetworkForType(networkType);
966 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -0700967 throw e.rethrowFromSystemServer();
Lorenzo Colittib57edc52014-08-22 17:10:50 -0700968 }
969 }
970
971 /**
Robert Greenwalt73b6cbae2014-06-23 11:40:00 -0700972 * Returns an array of all {@link Network} currently tracked by the
973 * framework.
Robert Greenwalt73b6cbae2014-06-23 11:40:00 -0700974 * <p>This method requires the caller to hold the permission
975 * {@link android.Manifest.permission#ACCESS_NETWORK_STATE}.
Paul Jensenb2748922015-05-06 11:10:18 -0400976 *
977 * @return an array of {@link Network} objects.
Robert Greenwalt73b6cbae2014-06-23 11:40:00 -0700978 */
979 public Network[] getAllNetworks() {
980 try {
981 return mService.getAllNetworks();
982 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -0700983 throw e.rethrowFromSystemServer();
Robert Greenwalt73b6cbae2014-06-23 11:40:00 -0700984 }
985 }
986
987 /**
Lorenzo Colittie285b432015-04-23 15:32:42 +0900988 * Returns an array of {@link android.net.NetworkCapabilities} objects, representing
Lorenzo Colitti403aa262014-11-28 11:21:30 +0900989 * the Networks that applications run by the given user will use by default.
990 * @hide
991 */
992 public NetworkCapabilities[] getDefaultNetworkCapabilitiesForUser(int userId) {
993 try {
994 return mService.getDefaultNetworkCapabilitiesForUser(userId);
995 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -0700996 throw e.rethrowFromSystemServer();
Lorenzo Colitti403aa262014-11-28 11:21:30 +0900997 }
998 }
999
1000 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08001001 * Returns the IP information for the current default network.
Paul Jensenb2748922015-05-06 11:10:18 -04001002 * <p>This method requires the caller to hold the permission
1003 * {@link android.Manifest.permission#ACCESS_NETWORK_STATE}.
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08001004 *
1005 * @return a {@link LinkProperties} object describing the IP info
1006 * for the current default network, or {@code null} if there
1007 * is no current default network.
1008 *
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08001009 * {@hide}
1010 */
Robert Greenwaltd192dad2010-09-14 09:18:02 -07001011 public LinkProperties getActiveLinkProperties() {
1012 try {
1013 return mService.getActiveLinkProperties();
1014 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07001015 throw e.rethrowFromSystemServer();
Robert Greenwaltd192dad2010-09-14 09:18:02 -07001016 }
1017 }
1018
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08001019 /**
1020 * Returns the IP information for a given network type.
Paul Jensenb2748922015-05-06 11:10:18 -04001021 * <p>This method requires the caller to hold the permission
1022 * {@link android.Manifest.permission#ACCESS_NETWORK_STATE}.
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08001023 *
1024 * @param networkType the network type of interest.
1025 * @return a {@link LinkProperties} object describing the IP info
1026 * for the given networkType, or {@code null} if there is
1027 * no current default network.
1028 *
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08001029 * {@hide}
Paul Jensen3541e9f2015-03-18 12:23:02 -04001030 * @deprecated This method does not support multiple connected networks
1031 * of the same type. Use {@link #getAllNetworks},
1032 * {@link #getNetworkInfo(android.net.Network)}, and
1033 * {@link #getLinkProperties(android.net.Network)} instead.
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08001034 */
Robert Greenwaltd192dad2010-09-14 09:18:02 -07001035 public LinkProperties getLinkProperties(int networkType) {
1036 try {
Robert Greenwalt9258c642014-03-26 16:47:06 -07001037 return mService.getLinkPropertiesForType(networkType);
1038 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07001039 throw e.rethrowFromSystemServer();
Robert Greenwalt9258c642014-03-26 16:47:06 -07001040 }
1041 }
1042
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07001043 /**
1044 * Get the {@link LinkProperties} for the given {@link Network}. This
1045 * will return {@code null} if the network is unknown.
Paul Jensenb2748922015-05-06 11:10:18 -04001046 * <p>This method requires the caller to hold the permission
1047 * {@link android.Manifest.permission#ACCESS_NETWORK_STATE}.
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07001048 *
1049 * @param network The {@link Network} object identifying the network in question.
1050 * @return The {@link LinkProperties} for the network, or {@code null}.
Paul Jensenb2748922015-05-06 11:10:18 -04001051 */
Robert Greenwalt9258c642014-03-26 16:47:06 -07001052 public LinkProperties getLinkProperties(Network network) {
1053 try {
1054 return mService.getLinkProperties(network);
1055 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07001056 throw e.rethrowFromSystemServer();
Robert Greenwalt9258c642014-03-26 16:47:06 -07001057 }
1058 }
1059
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07001060 /**
Erik Klineacdd6392016-07-07 16:50:58 +09001061 * Request that this callback be invoked at ConnectivityService's earliest
1062 * convenience with the current satisfying network's LinkProperties.
1063 * If no such network exists no callback invocation is performed.
1064 *
1065 * The callback must have been registered with #requestNetwork() or
1066 * #registerDefaultNetworkCallback(); callbacks registered with
1067 * registerNetworkCallback() are not specific to any particular Network so
1068 * do not cause any updates.
1069 *
1070 * @hide
1071 */
1072 public void requestLinkProperties(NetworkCallback networkCallback) {
1073 try {
1074 mService.requestLinkProperties(networkCallback.networkRequest);
1075 } catch (RemoteException e) {
1076 throw e.rethrowFromSystemServer();
1077 }
1078 }
1079
1080 /**
Lorenzo Colittie285b432015-04-23 15:32:42 +09001081 * Get the {@link android.net.NetworkCapabilities} for the given {@link Network}. This
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07001082 * will return {@code null} if the network is unknown.
Paul Jensenb2748922015-05-06 11:10:18 -04001083 * <p>This method requires the caller to hold the permission
1084 * {@link android.Manifest.permission#ACCESS_NETWORK_STATE}.
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07001085 *
1086 * @param network The {@link Network} object identifying the network in question.
Lorenzo Colittie285b432015-04-23 15:32:42 +09001087 * @return The {@link android.net.NetworkCapabilities} for the network, or {@code null}.
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07001088 */
Robert Greenwalt9258c642014-03-26 16:47:06 -07001089 public NetworkCapabilities getNetworkCapabilities(Network network) {
1090 try {
1091 return mService.getNetworkCapabilities(network);
Robert Greenwaltd192dad2010-09-14 09:18:02 -07001092 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07001093 throw e.rethrowFromSystemServer();
Robert Greenwaltd192dad2010-09-14 09:18:02 -07001094 }
1095 }
1096
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08001097 /**
Erik Klineacdd6392016-07-07 16:50:58 +09001098 * Request that this callback be invoked at ConnectivityService's earliest
1099 * convenience with the current satisfying network's NetworkCapabilities.
1100 * If no such network exists no callback invocation is performed.
1101 *
1102 * The callback must have been registered with #requestNetwork() or
1103 * #registerDefaultNetworkCallback(); callbacks registered with
1104 * registerNetworkCallback() are not specific to any particular Network so
1105 * do not cause any updates.
1106 *
1107 * @hide
1108 */
1109 public void requestNetworkCapabilities(NetworkCallback networkCallback) {
1110 try {
1111 mService.requestNetworkCapabilities(networkCallback.networkRequest);
1112 } catch (RemoteException e) {
1113 throw e.rethrowFromSystemServer();
1114 }
1115 }
1116
1117 /**
Udam Sainib7c24872016-01-04 12:16:14 -08001118 * Gets the URL that should be used for resolving whether a captive portal is present.
1119 * 1. This URL should respond with a 204 response to a GET request to indicate no captive
1120 * portal is present.
1121 * 2. This URL must be HTTP as redirect responses are used to find captive portal
1122 * sign-in pages. Captive portals cannot respond to HTTPS requests with redirects.
1123 *
1124 * @hide
1125 */
1126 @SystemApi
1127 public String getCaptivePortalServerUrl() {
1128 try {
1129 return mService.getCaptivePortalServerUrl();
1130 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07001131 throw e.rethrowFromSystemServer();
Udam Sainib7c24872016-01-04 12:16:14 -08001132 }
1133 }
1134
1135 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001136 * Tells the underlying networking system that the caller wants to
1137 * begin using the named feature. The interpretation of {@code feature}
1138 * is completely up to each networking implementation.
Lorenzo Colittid5427052015-10-15 16:29:00 +09001139 *
1140 * <p>This method requires the caller to hold either the
1141 * {@link android.Manifest.permission#CHANGE_NETWORK_STATE} permission
1142 * or the ability to modify system settings as determined by
1143 * {@link android.provider.Settings.System#canWrite}.</p>
1144 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001145 * @param networkType specifies which network the request pertains to
1146 * @param feature the name of the feature to be used
1147 * @return an integer value representing the outcome of the request.
1148 * The interpretation of this value is specific to each networking
1149 * implementation+feature combination, except that the value {@code -1}
1150 * always indicates failure.
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07001151 *
Lorenzo Colitti2ea89e52015-04-24 17:03:31 +09001152 * @deprecated Deprecated in favor of the cleaner
1153 * {@link #requestNetwork(NetworkRequest, NetworkCallback)} API.
Dianne Hackborn692a2442015-07-31 10:35:34 -07001154 * In {@link VERSION_CODES#M}, and above, this method is unsupported and will
Lorenzo Colittiffc42b02015-07-29 11:41:21 +09001155 * throw {@code UnsupportedOperationException} if called.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001156 */
1157 public int startUsingNetworkFeature(int networkType, String feature) {
Lorenzo Colittiffc42b02015-07-29 11:41:21 +09001158 checkLegacyRoutingApiAccess();
Robert Greenwalt562cc542014-05-15 18:07:26 -07001159 NetworkCapabilities netCap = networkCapabilitiesForFeature(networkType, feature);
1160 if (netCap == null) {
1161 Log.d(TAG, "Can't satisfy startUsingNetworkFeature for " + networkType + ", " +
1162 feature);
1163 return PhoneConstants.APN_REQUEST_FAILED;
1164 }
1165
1166 NetworkRequest request = null;
1167 synchronized (sLegacyRequests) {
1168 LegacyRequest l = sLegacyRequests.get(netCap);
1169 if (l != null) {
1170 Log.d(TAG, "renewing startUsingNetworkFeature request " + l.networkRequest);
1171 renewRequestLocked(l);
1172 if (l.currentNetwork != null) {
1173 return PhoneConstants.APN_ALREADY_ACTIVE;
1174 } else {
1175 return PhoneConstants.APN_REQUEST_STARTED;
1176 }
1177 }
1178
1179 request = requestNetworkForFeatureLocked(netCap);
1180 }
1181 if (request != null) {
Robert Greenwalt257ee5f2014-06-20 10:58:45 -07001182 Log.d(TAG, "starting startUsingNetworkFeature for request " + request);
Robert Greenwalt562cc542014-05-15 18:07:26 -07001183 return PhoneConstants.APN_REQUEST_STARTED;
1184 } else {
1185 Log.d(TAG, " request Failed");
1186 return PhoneConstants.APN_REQUEST_FAILED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001187 }
1188 }
1189
1190 /**
1191 * Tells the underlying networking system that the caller is finished
1192 * using the named feature. The interpretation of {@code feature}
1193 * is completely up to each networking implementation.
Lorenzo Colittid5427052015-10-15 16:29:00 +09001194 *
1195 * <p>This method requires the caller to hold either the
1196 * {@link android.Manifest.permission#CHANGE_NETWORK_STATE} permission
1197 * or the ability to modify system settings as determined by
1198 * {@link android.provider.Settings.System#canWrite}.</p>
1199 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001200 * @param networkType specifies which network the request pertains to
1201 * @param feature the name of the feature that is no longer needed
1202 * @return an integer value representing the outcome of the request.
1203 * The interpretation of this value is specific to each networking
1204 * implementation+feature combination, except that the value {@code -1}
1205 * always indicates failure.
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07001206 *
Lorenzo Colitti88bc0bb2016-04-13 22:00:02 +09001207 * @deprecated Deprecated in favor of the cleaner
1208 * {@link #unregisterNetworkCallback(NetworkCallback)} API.
Dianne Hackborn692a2442015-07-31 10:35:34 -07001209 * In {@link VERSION_CODES#M}, and above, this method is unsupported and will
Lorenzo Colittiffc42b02015-07-29 11:41:21 +09001210 * throw {@code UnsupportedOperationException} if called.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001211 */
1212 public int stopUsingNetworkFeature(int networkType, String feature) {
Lorenzo Colittiffc42b02015-07-29 11:41:21 +09001213 checkLegacyRoutingApiAccess();
Robert Greenwalt562cc542014-05-15 18:07:26 -07001214 NetworkCapabilities netCap = networkCapabilitiesForFeature(networkType, feature);
1215 if (netCap == null) {
1216 Log.d(TAG, "Can't satisfy stopUsingNetworkFeature for " + networkType + ", " +
1217 feature);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001218 return -1;
1219 }
Robert Greenwalt562cc542014-05-15 18:07:26 -07001220
Paul Jensen9ffb53c2014-12-17 10:39:34 -05001221 if (removeRequestForFeature(netCap)) {
Robert Greenwalt562cc542014-05-15 18:07:26 -07001222 Log.d(TAG, "stopUsingNetworkFeature for " + networkType + ", " + feature);
Robert Greenwalt562cc542014-05-15 18:07:26 -07001223 }
1224 return 1;
1225 }
1226
1227 private NetworkCapabilities networkCapabilitiesForFeature(int networkType, String feature) {
1228 if (networkType == TYPE_MOBILE) {
1229 int cap = -1;
1230 if ("enableMMS".equals(feature)) {
1231 cap = NetworkCapabilities.NET_CAPABILITY_MMS;
1232 } else if ("enableSUPL".equals(feature)) {
1233 cap = NetworkCapabilities.NET_CAPABILITY_SUPL;
1234 } else if ("enableDUN".equals(feature) || "enableDUNAlways".equals(feature)) {
1235 cap = NetworkCapabilities.NET_CAPABILITY_DUN;
1236 } else if ("enableHIPRI".equals(feature)) {
1237 cap = NetworkCapabilities.NET_CAPABILITY_INTERNET;
1238 } else if ("enableFOTA".equals(feature)) {
1239 cap = NetworkCapabilities.NET_CAPABILITY_FOTA;
1240 } else if ("enableIMS".equals(feature)) {
1241 cap = NetworkCapabilities.NET_CAPABILITY_IMS;
1242 } else if ("enableCBS".equals(feature)) {
1243 cap = NetworkCapabilities.NET_CAPABILITY_CBS;
1244 } else {
1245 return null;
1246 }
1247 NetworkCapabilities netCap = new NetworkCapabilities();
Robert Greenwalt7569f182014-06-08 16:42:59 -07001248 netCap.addTransportType(NetworkCapabilities.TRANSPORT_CELLULAR).addCapability(cap);
Paul Jensen487ffe72015-07-24 15:57:11 -04001249 netCap.maybeMarkCapabilitiesRestricted();
Robert Greenwalt562cc542014-05-15 18:07:26 -07001250 return netCap;
1251 } else if (networkType == TYPE_WIFI) {
1252 if ("p2p".equals(feature)) {
1253 NetworkCapabilities netCap = new NetworkCapabilities();
1254 netCap.addTransportType(NetworkCapabilities.TRANSPORT_WIFI);
Robert Greenwalt7569f182014-06-08 16:42:59 -07001255 netCap.addCapability(NetworkCapabilities.NET_CAPABILITY_WIFI_P2P);
Paul Jensen487ffe72015-07-24 15:57:11 -04001256 netCap.maybeMarkCapabilitiesRestricted();
Robert Greenwalt562cc542014-05-15 18:07:26 -07001257 return netCap;
1258 }
1259 }
1260 return null;
1261 }
1262
Robert Greenwalt06314e42014-10-29 14:04:06 -07001263 /**
1264 * Guess what the network request was trying to say so that the resulting
1265 * network is accessible via the legacy (deprecated) API such as
1266 * requestRouteToHost.
Lorenzo Colittidef4cb02015-11-25 20:28:50 +09001267 *
1268 * This means we should try to be fairly precise about transport and
Robert Greenwalt06314e42014-10-29 14:04:06 -07001269 * capability but ignore things such as networkSpecifier.
1270 * If the request has more than one transport or capability it doesn't
1271 * match the old legacy requests (they selected only single transport/capability)
1272 * so this function cannot map the request to a single legacy type and
1273 * the resulting network will not be available to the legacy APIs.
1274 *
Lorenzo Colittidef4cb02015-11-25 20:28:50 +09001275 * This code is only called from the requestNetwork API (L and above).
1276 *
1277 * Setting a legacy type causes CONNECTIVITY_ACTION broadcasts, which are expensive
1278 * because they wake up lots of apps - see http://b/23350688 . So we currently only
1279 * do this for SUPL requests, which are the only ones that we know need it. If
1280 * omitting these broadcasts causes unacceptable app breakage, then for backwards
1281 * compatibility we can send them:
1282 *
1283 * if (targetSdkVersion < Build.VERSION_CODES.M) && // legacy API unsupported >= M
1284 * targetSdkVersion >= Build.VERSION_CODES.LOLLIPOP)) // requestNetwork not present < L
1285 *
Robert Greenwalt06314e42014-10-29 14:04:06 -07001286 * TODO - This should be removed when the legacy APIs are removed.
1287 */
Ye Wenb87875e2014-07-21 14:19:01 -07001288 private int inferLegacyTypeForNetworkCapabilities(NetworkCapabilities netCap) {
1289 if (netCap == null) {
1290 return TYPE_NONE;
1291 }
Robert Greenwalt06314e42014-10-29 14:04:06 -07001292
Ye Wenb87875e2014-07-21 14:19:01 -07001293 if (!netCap.hasTransport(NetworkCapabilities.TRANSPORT_CELLULAR)) {
1294 return TYPE_NONE;
1295 }
Robert Greenwalt06314e42014-10-29 14:04:06 -07001296
Lifu Tang30f95a72016-01-07 23:20:38 -08001297 // Do this only for SUPL, until GnssLocationProvider is fixed. http://b/25876485 .
Lorenzo Colittidef4cb02015-11-25 20:28:50 +09001298 if (!netCap.hasCapability(NetworkCapabilities.NET_CAPABILITY_SUPL)) {
1299 // NOTE: if this causes app breakage, we should not just comment out this early return;
1300 // instead, we should make this early return conditional on the requesting app's target
1301 // SDK version, as described in the comment above.
1302 return TYPE_NONE;
1303 }
1304
Robert Greenwalt06314e42014-10-29 14:04:06 -07001305 String type = null;
1306 int result = TYPE_NONE;
1307
Ye Wenb87875e2014-07-21 14:19:01 -07001308 if (netCap.hasCapability(NetworkCapabilities.NET_CAPABILITY_CBS)) {
Robert Greenwalt06314e42014-10-29 14:04:06 -07001309 type = "enableCBS";
1310 result = TYPE_MOBILE_CBS;
1311 } else if (netCap.hasCapability(NetworkCapabilities.NET_CAPABILITY_IMS)) {
1312 type = "enableIMS";
1313 result = TYPE_MOBILE_IMS;
1314 } else if (netCap.hasCapability(NetworkCapabilities.NET_CAPABILITY_FOTA)) {
1315 type = "enableFOTA";
1316 result = TYPE_MOBILE_FOTA;
1317 } else if (netCap.hasCapability(NetworkCapabilities.NET_CAPABILITY_DUN)) {
1318 type = "enableDUN";
1319 result = TYPE_MOBILE_DUN;
1320 } else if (netCap.hasCapability(NetworkCapabilities.NET_CAPABILITY_SUPL)) {
Lorenzo Colittidef4cb02015-11-25 20:28:50 +09001321 type = "enableSUPL";
Robert Greenwalt06314e42014-10-29 14:04:06 -07001322 result = TYPE_MOBILE_SUPL;
Robert Greenwalt74ab4fa2015-08-28 12:37:54 -07001323 // back out this hack for mms as they no longer need this and it's causing
1324 // device slowdowns - b/23350688 (note, supl still needs this)
1325 //} else if (netCap.hasCapability(NetworkCapabilities.NET_CAPABILITY_MMS)) {
1326 // type = "enableMMS";
1327 // result = TYPE_MOBILE_MMS;
Robert Greenwalt06314e42014-10-29 14:04:06 -07001328 } else if (netCap.hasCapability(NetworkCapabilities.NET_CAPABILITY_INTERNET)) {
1329 type = "enableHIPRI";
1330 result = TYPE_MOBILE_HIPRI;
Ye Wenb87875e2014-07-21 14:19:01 -07001331 }
Robert Greenwalt06314e42014-10-29 14:04:06 -07001332 if (type != null) {
1333 NetworkCapabilities testCap = networkCapabilitiesForFeature(TYPE_MOBILE, type);
1334 if (testCap.equalsNetCapabilities(netCap) && testCap.equalsTransportTypes(netCap)) {
1335 return result;
Ye Wenb87875e2014-07-21 14:19:01 -07001336 }
1337 }
1338 return TYPE_NONE;
1339 }
1340
Robert Greenwalt32aa65a2014-06-02 15:32:02 -07001341 private int legacyTypeForNetworkCapabilities(NetworkCapabilities netCap) {
Robert Greenwalt562cc542014-05-15 18:07:26 -07001342 if (netCap == null) return TYPE_NONE;
1343 if (netCap.hasCapability(NetworkCapabilities.NET_CAPABILITY_CBS)) {
1344 return TYPE_MOBILE_CBS;
1345 }
1346 if (netCap.hasCapability(NetworkCapabilities.NET_CAPABILITY_IMS)) {
1347 return TYPE_MOBILE_IMS;
1348 }
1349 if (netCap.hasCapability(NetworkCapabilities.NET_CAPABILITY_FOTA)) {
1350 return TYPE_MOBILE_FOTA;
1351 }
1352 if (netCap.hasCapability(NetworkCapabilities.NET_CAPABILITY_DUN)) {
1353 return TYPE_MOBILE_DUN;
1354 }
1355 if (netCap.hasCapability(NetworkCapabilities.NET_CAPABILITY_SUPL)) {
1356 return TYPE_MOBILE_SUPL;
1357 }
1358 if (netCap.hasCapability(NetworkCapabilities.NET_CAPABILITY_MMS)) {
1359 return TYPE_MOBILE_MMS;
1360 }
1361 if (netCap.hasCapability(NetworkCapabilities.NET_CAPABILITY_INTERNET)) {
1362 return TYPE_MOBILE_HIPRI;
1363 }
Robert Greenwalt32aa65a2014-06-02 15:32:02 -07001364 if (netCap.hasCapability(NetworkCapabilities.NET_CAPABILITY_WIFI_P2P)) {
1365 return TYPE_WIFI_P2P;
1366 }
Robert Greenwalt562cc542014-05-15 18:07:26 -07001367 return TYPE_NONE;
1368 }
1369
1370 private static class LegacyRequest {
1371 NetworkCapabilities networkCapabilities;
1372 NetworkRequest networkRequest;
1373 int expireSequenceNumber;
1374 Network currentNetwork;
1375 int delay = -1;
Paul Jensen9ffb53c2014-12-17 10:39:34 -05001376
1377 private void clearDnsBinding() {
1378 if (currentNetwork != null) {
1379 currentNetwork = null;
1380 setProcessDefaultNetworkForHostResolution(null);
1381 }
1382 }
1383
Robert Greenwalt6078b502014-06-11 16:05:07 -07001384 NetworkCallback networkCallback = new NetworkCallback() {
Robert Greenwalt562cc542014-05-15 18:07:26 -07001385 @Override
Robert Greenwalt6078b502014-06-11 16:05:07 -07001386 public void onAvailable(Network network) {
Robert Greenwalt562cc542014-05-15 18:07:26 -07001387 currentNetwork = network;
1388 Log.d(TAG, "startUsingNetworkFeature got Network:" + network);
Paul Jensen6d3ff9e2014-05-29 10:12:39 -04001389 setProcessDefaultNetworkForHostResolution(network);
Robert Greenwalt562cc542014-05-15 18:07:26 -07001390 }
1391 @Override
Robert Greenwalt6078b502014-06-11 16:05:07 -07001392 public void onLost(Network network) {
Paul Jensen9ffb53c2014-12-17 10:39:34 -05001393 if (network.equals(currentNetwork)) clearDnsBinding();
Robert Greenwalt562cc542014-05-15 18:07:26 -07001394 Log.d(TAG, "startUsingNetworkFeature lost Network:" + network);
1395 }
1396 };
1397 }
1398
Robert Greenwaltfab501672014-07-23 11:44:01 -07001399 private static HashMap<NetworkCapabilities, LegacyRequest> sLegacyRequests =
Robert Greenwalt562cc542014-05-15 18:07:26 -07001400 new HashMap<NetworkCapabilities, LegacyRequest>();
1401
1402 private NetworkRequest findRequestForFeature(NetworkCapabilities netCap) {
1403 synchronized (sLegacyRequests) {
1404 LegacyRequest l = sLegacyRequests.get(netCap);
1405 if (l != null) return l.networkRequest;
1406 }
1407 return null;
1408 }
1409
1410 private void renewRequestLocked(LegacyRequest l) {
1411 l.expireSequenceNumber++;
1412 Log.d(TAG, "renewing request to seqNum " + l.expireSequenceNumber);
1413 sendExpireMsgForFeature(l.networkCapabilities, l.expireSequenceNumber, l.delay);
1414 }
1415
1416 private void expireRequest(NetworkCapabilities netCap, int sequenceNum) {
1417 int ourSeqNum = -1;
1418 synchronized (sLegacyRequests) {
1419 LegacyRequest l = sLegacyRequests.get(netCap);
1420 if (l == null) return;
1421 ourSeqNum = l.expireSequenceNumber;
Paul Jensen9ffb53c2014-12-17 10:39:34 -05001422 if (l.expireSequenceNumber == sequenceNum) removeRequestForFeature(netCap);
Robert Greenwalt562cc542014-05-15 18:07:26 -07001423 }
1424 Log.d(TAG, "expireRequest with " + ourSeqNum + ", " + sequenceNum);
1425 }
1426
1427 private NetworkRequest requestNetworkForFeatureLocked(NetworkCapabilities netCap) {
1428 int delay = -1;
Robert Greenwalt32aa65a2014-06-02 15:32:02 -07001429 int type = legacyTypeForNetworkCapabilities(netCap);
Robert Greenwalt562cc542014-05-15 18:07:26 -07001430 try {
1431 delay = mService.getRestoreDefaultNetworkDelay(type);
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07001432 } catch (RemoteException e) {
1433 throw e.rethrowFromSystemServer();
1434 }
Robert Greenwalt562cc542014-05-15 18:07:26 -07001435 LegacyRequest l = new LegacyRequest();
1436 l.networkCapabilities = netCap;
1437 l.delay = delay;
1438 l.expireSequenceNumber = 0;
Robert Greenwalt6078b502014-06-11 16:05:07 -07001439 l.networkRequest = sendRequestForNetwork(netCap, l.networkCallback, 0,
Robert Greenwalt32aa65a2014-06-02 15:32:02 -07001440 REQUEST, type);
Robert Greenwalt562cc542014-05-15 18:07:26 -07001441 if (l.networkRequest == null) return null;
1442 sLegacyRequests.put(netCap, l);
1443 sendExpireMsgForFeature(netCap, l.expireSequenceNumber, delay);
1444 return l.networkRequest;
1445 }
1446
1447 private void sendExpireMsgForFeature(NetworkCapabilities netCap, int seqNum, int delay) {
1448 if (delay >= 0) {
1449 Log.d(TAG, "sending expire msg with seqNum " + seqNum + " and delay " + delay);
1450 Message msg = sCallbackHandler.obtainMessage(EXPIRE_LEGACY_REQUEST, seqNum, 0, netCap);
1451 sCallbackHandler.sendMessageDelayed(msg, delay);
1452 }
1453 }
1454
Paul Jensen9ffb53c2014-12-17 10:39:34 -05001455 private boolean removeRequestForFeature(NetworkCapabilities netCap) {
1456 final LegacyRequest l;
Robert Greenwalt562cc542014-05-15 18:07:26 -07001457 synchronized (sLegacyRequests) {
Paul Jensen9ffb53c2014-12-17 10:39:34 -05001458 l = sLegacyRequests.remove(netCap);
Robert Greenwalt562cc542014-05-15 18:07:26 -07001459 }
Paul Jensen9ffb53c2014-12-17 10:39:34 -05001460 if (l == null) return false;
1461 unregisterNetworkCallback(l.networkCallback);
1462 l.clearDnsBinding();
1463 return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001464 }
1465
Lorenzo Colitti8bf977d2015-06-15 14:29:22 +09001466 /** @hide */
1467 public static class PacketKeepaliveCallback {
1468 /** The requested keepalive was successfully started. */
1469 public void onStarted() {}
1470 /** The keepalive was successfully stopped. */
1471 public void onStopped() {}
1472 /** An error occurred. */
1473 public void onError(int error) {}
1474 }
1475
1476 /**
1477 * Allows applications to request that the system periodically send specific packets on their
1478 * behalf, using hardware offload to save battery power.
1479 *
1480 * To request that the system send keepalives, call one of the methods that return a
1481 * {@link ConnectivityManager.PacketKeepalive} object, such as {@link #startNattKeepalive},
1482 * passing in a non-null callback. If the callback is successfully started, the callback's
1483 * {@code onStarted} method will be called. If an error occurs, {@code onError} will be called,
1484 * specifying one of the {@code ERROR_*} constants in this class.
1485 *
1486 * To stop an existing keepalive, call {@link stop}. The system will call {@code onStopped} if
1487 * the operation was successfull or {@code onError} if an error occurred.
1488 *
1489 * @hide
1490 */
1491 public class PacketKeepalive {
1492
1493 private static final String TAG = "PacketKeepalive";
1494
1495 /** @hide */
1496 public static final int SUCCESS = 0;
1497
1498 /** @hide */
1499 public static final int NO_KEEPALIVE = -1;
1500
1501 /** @hide */
1502 public static final int BINDER_DIED = -10;
1503
1504 /** The specified {@code Network} is not connected. */
1505 public static final int ERROR_INVALID_NETWORK = -20;
1506 /** The specified IP addresses are invalid. For example, the specified source IP address is
1507 * not configured on the specified {@code Network}. */
1508 public static final int ERROR_INVALID_IP_ADDRESS = -21;
1509 /** The requested port is invalid. */
1510 public static final int ERROR_INVALID_PORT = -22;
1511 /** The packet length is invalid (e.g., too long). */
1512 public static final int ERROR_INVALID_LENGTH = -23;
1513 /** The packet transmission interval is invalid (e.g., too short). */
1514 public static final int ERROR_INVALID_INTERVAL = -24;
1515
1516 /** The hardware does not support this request. */
1517 public static final int ERROR_HARDWARE_UNSUPPORTED = -30;
Lorenzo Colitti9d1284e2015-09-08 16:46:36 +09001518 /** The hardware returned an error. */
1519 public static final int ERROR_HARDWARE_ERROR = -31;
Lorenzo Colitti8bf977d2015-06-15 14:29:22 +09001520
1521 public static final int NATT_PORT = 4500;
1522
1523 private final Network mNetwork;
1524 private final PacketKeepaliveCallback mCallback;
1525 private final Looper mLooper;
1526 private final Messenger mMessenger;
1527
1528 private volatile Integer mSlot;
1529
1530 void stopLooper() {
1531 mLooper.quit();
1532 }
1533
1534 public void stop() {
1535 try {
1536 mService.stopKeepalive(mNetwork, mSlot);
1537 } catch (RemoteException e) {
1538 Log.e(TAG, "Error stopping packet keepalive: ", e);
1539 stopLooper();
1540 }
1541 }
1542
1543 private PacketKeepalive(Network network, PacketKeepaliveCallback callback) {
1544 checkNotNull(network, "network cannot be null");
1545 checkNotNull(callback, "callback cannot be null");
1546 mNetwork = network;
1547 mCallback = callback;
1548 HandlerThread thread = new HandlerThread(TAG);
1549 thread.start();
1550 mLooper = thread.getLooper();
1551 mMessenger = new Messenger(new Handler(mLooper) {
1552 @Override
1553 public void handleMessage(Message message) {
1554 switch (message.what) {
1555 case NetworkAgent.EVENT_PACKET_KEEPALIVE:
1556 int error = message.arg2;
1557 try {
1558 if (error == SUCCESS) {
1559 if (mSlot == null) {
1560 mSlot = message.arg1;
1561 mCallback.onStarted();
1562 } else {
1563 mSlot = null;
1564 stopLooper();
1565 mCallback.onStopped();
1566 }
1567 } else {
1568 stopLooper();
1569 mCallback.onError(error);
1570 }
1571 } catch (Exception e) {
1572 Log.e(TAG, "Exception in keepalive callback(" + error + ")", e);
1573 }
1574 break;
1575 default:
1576 Log.e(TAG, "Unhandled message " + Integer.toHexString(message.what));
1577 break;
1578 }
1579 }
1580 });
1581 }
1582 }
1583
1584 /**
1585 * Starts an IPsec NAT-T keepalive packet with the specified parameters.
1586 *
1587 * @hide
1588 */
1589 public PacketKeepalive startNattKeepalive(
1590 Network network, int intervalSeconds, PacketKeepaliveCallback callback,
1591 InetAddress srcAddr, int srcPort, InetAddress dstAddr) {
1592 final PacketKeepalive k = new PacketKeepalive(network, callback);
1593 try {
1594 mService.startNattKeepalive(network, intervalSeconds, k.mMessenger, new Binder(),
1595 srcAddr.getHostAddress(), srcPort, dstAddr.getHostAddress());
1596 } catch (RemoteException e) {
1597 Log.e(TAG, "Error starting packet keepalive: ", e);
1598 k.stopLooper();
1599 return null;
1600 }
1601 return k;
1602 }
1603
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001604 /**
1605 * Ensure that a network route exists to deliver traffic to the specified
1606 * host via the specified network interface. An attempt to add a route that
1607 * already exists is ignored, but treated as successful.
Lorenzo Colittid5427052015-10-15 16:29:00 +09001608 *
1609 * <p>This method requires the caller to hold either the
1610 * {@link android.Manifest.permission#CHANGE_NETWORK_STATE} permission
1611 * or the ability to modify system settings as determined by
1612 * {@link android.provider.Settings.System#canWrite}.</p>
1613 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001614 * @param networkType the type of the network over which traffic to the specified
1615 * host is to be routed
1616 * @param hostAddress the IP address of the host to which the route is desired
1617 * @return {@code true} on success, {@code false} on failure
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07001618 *
Lorenzo Colitti2ea89e52015-04-24 17:03:31 +09001619 * @deprecated Deprecated in favor of the
1620 * {@link #requestNetwork(NetworkRequest, NetworkCallback)},
1621 * {@link #bindProcessToNetwork} and {@link Network#getSocketFactory} API.
Dianne Hackborn692a2442015-07-31 10:35:34 -07001622 * In {@link VERSION_CODES#M}, and above, this method is unsupported and will
Lorenzo Colittiffc42b02015-07-29 11:41:21 +09001623 * throw {@code UnsupportedOperationException} if called.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001624 */
1625 public boolean requestRouteToHost(int networkType, int hostAddress) {
Sreeram Ramachandran03666c72014-07-19 23:21:46 -07001626 return requestRouteToHostAddress(networkType, NetworkUtils.intToInetAddress(hostAddress));
Robert Greenwalt585ac0f2010-08-27 09:24:29 -07001627 }
1628
1629 /**
1630 * Ensure that a network route exists to deliver traffic to the specified
1631 * host via the specified network interface. An attempt to add a route that
1632 * already exists is ignored, but treated as successful.
Lorenzo Colittid5427052015-10-15 16:29:00 +09001633 *
1634 * <p>This method requires the caller to hold either the
1635 * {@link android.Manifest.permission#CHANGE_NETWORK_STATE} permission
1636 * or the ability to modify system settings as determined by
1637 * {@link android.provider.Settings.System#canWrite}.</p>
1638 *
Robert Greenwalt585ac0f2010-08-27 09:24:29 -07001639 * @param networkType the type of the network over which traffic to the specified
1640 * host is to be routed
1641 * @param hostAddress the IP address of the host to which the route is desired
1642 * @return {@code true} on success, {@code false} on failure
1643 * @hide
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07001644 * @deprecated Deprecated in favor of the {@link #requestNetwork} and
Lorenzo Colitti2ea89e52015-04-24 17:03:31 +09001645 * {@link #bindProcessToNetwork} API.
Robert Greenwalt585ac0f2010-08-27 09:24:29 -07001646 */
1647 public boolean requestRouteToHostAddress(int networkType, InetAddress hostAddress) {
Lorenzo Colittiffc42b02015-07-29 11:41:21 +09001648 checkLegacyRoutingApiAccess();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001649 try {
Sreeram Ramachandran03666c72014-07-19 23:21:46 -07001650 return mService.requestRouteToHostAddress(networkType, hostAddress.getAddress());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001651 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07001652 throw e.rethrowFromSystemServer();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001653 }
1654 }
1655
1656 /**
1657 * Returns the value of the setting for background data usage. If false,
1658 * applications should not use the network if the application is not in the
1659 * foreground. Developers should respect this setting, and check the value
1660 * of this before performing any background data operations.
1661 * <p>
1662 * All applications that have background services that use the network
1663 * should listen to {@link #ACTION_BACKGROUND_DATA_SETTING_CHANGED}.
Jeff Sharkey3a844fc2011-08-16 14:37:57 -07001664 * <p>
Scott Main4cc53332011-10-06 18:32:43 -07001665 * @deprecated As of {@link VERSION_CODES#ICE_CREAM_SANDWICH}, availability of
Jeff Sharkey3a844fc2011-08-16 14:37:57 -07001666 * background data depends on several combined factors, and this method will
1667 * always return {@code true}. Instead, when background data is unavailable,
1668 * {@link #getActiveNetworkInfo()} will now appear disconnected.
Danica Chang6fdd0c62010-08-11 14:54:43 -07001669 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001670 * @return Whether background data usage is allowed.
1671 */
Jeff Sharkey3a844fc2011-08-16 14:37:57 -07001672 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001673 public boolean getBackgroundDataSetting() {
Jeff Sharkey3a844fc2011-08-16 14:37:57 -07001674 // assume that background data is allowed; final authority is
1675 // NetworkInfo which may be blocked.
1676 return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001677 }
1678
1679 /**
1680 * Sets the value of the setting for background data usage.
Robert Greenwaltc03fa502010-02-23 18:58:05 -08001681 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001682 * @param allowBackgroundData Whether an application should use data while
1683 * it is in the background.
Robert Greenwaltc03fa502010-02-23 18:58:05 -08001684 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001685 * @attr ref android.Manifest.permission#CHANGE_BACKGROUND_DATA_SETTING
1686 * @see #getBackgroundDataSetting()
1687 * @hide
1688 */
Jeff Sharkey3a844fc2011-08-16 14:37:57 -07001689 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001690 public void setBackgroundDataSetting(boolean allowBackgroundData) {
Jeff Sharkey3a844fc2011-08-16 14:37:57 -07001691 // ignored
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001692 }
Robert Greenwaltc03fa502010-02-23 18:58:05 -08001693
1694 /**
Jeff Sharkeyf0ceede2011-08-02 17:22:34 -07001695 * Return quota status for the current active network, or {@code null} if no
1696 * network is active. Quota status can change rapidly, so these values
1697 * shouldn't be cached.
Jeff Sharkey44a3e0d2011-10-06 10:50:09 -07001698 *
Paul Jensen0d719ca2015-02-13 14:18:39 -05001699 * <p>This method requires the caller to hold the permission
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08001700 * {@link android.Manifest.permission#ACCESS_NETWORK_STATE}.
1701 *
Jeff Sharkey44a3e0d2011-10-06 10:50:09 -07001702 * @hide
Jeff Sharkeyf0ceede2011-08-02 17:22:34 -07001703 */
1704 public NetworkQuotaInfo getActiveNetworkQuotaInfo() {
1705 try {
1706 return mService.getActiveNetworkQuotaInfo();
1707 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07001708 throw e.rethrowFromSystemServer();
Jeff Sharkeyf0ceede2011-08-02 17:22:34 -07001709 }
1710 }
1711
1712 /**
Robert Greenwaltc03fa502010-02-23 18:58:05 -08001713 * @hide
Robert Greenwaltafa05c02014-05-21 20:04:36 -07001714 * @deprecated Talk to TelephonyManager directly
Robert Greenwaltc03fa502010-02-23 18:58:05 -08001715 */
1716 public boolean getMobileDataEnabled() {
Robert Greenwaltafa05c02014-05-21 20:04:36 -07001717 IBinder b = ServiceManager.getService(Context.TELEPHONY_SERVICE);
1718 if (b != null) {
1719 try {
1720 ITelephony it = ITelephony.Stub.asInterface(b);
Shishir Agrawal7ea3e8b2016-01-25 13:03:07 -08001721 int subId = SubscriptionManager.getDefaultDataSubscriptionId();
Wink Saville36ffb042014-12-05 11:10:30 -08001722 Log.d("ConnectivityManager", "getMobileDataEnabled()+ subId=" + subId);
1723 boolean retVal = it.getDataEnabled(subId);
1724 Log.d("ConnectivityManager", "getMobileDataEnabled()- subId=" + subId
1725 + " retVal=" + retVal);
1726 return retVal;
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07001727 } catch (RemoteException e) {
1728 throw e.rethrowFromSystemServer();
1729 }
Robert Greenwaltc03fa502010-02-23 18:58:05 -08001730 }
Wink Saville36ffb042014-12-05 11:10:30 -08001731 Log.d("ConnectivityManager", "getMobileDataEnabled()- remote exception retVal=false");
Robert Greenwaltafa05c02014-05-21 20:04:36 -07001732 return false;
Robert Greenwaltc03fa502010-02-23 18:58:05 -08001733 }
1734
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001735 /**
Robert Greenwaltb2489872014-09-04 16:44:35 -07001736 * Callback for use with {@link ConnectivityManager#addDefaultNetworkActiveListener}
Robert Greenwalt6078b502014-06-11 16:05:07 -07001737 * to find out when the system default network has gone in to a high power state.
Dianne Hackborn77b987f2014-02-26 16:20:52 -08001738 */
1739 public interface OnNetworkActiveListener {
1740 /**
1741 * Called on the main thread of the process to report that the current data network
1742 * has become active, and it is now a good time to perform any pending network
1743 * operations. Note that this listener only tells you when the network becomes
1744 * active; if at any other time you want to know whether it is active (and thus okay
1745 * to initiate network traffic), you can retrieve its instantaneous state with
Robert Greenwalt6078b502014-06-11 16:05:07 -07001746 * {@link ConnectivityManager#isDefaultNetworkActive}.
Dianne Hackborn77b987f2014-02-26 16:20:52 -08001747 */
1748 public void onNetworkActive();
1749 }
1750
1751 private INetworkManagementService getNetworkManagementService() {
1752 synchronized (this) {
1753 if (mNMService != null) {
1754 return mNMService;
1755 }
1756 IBinder b = ServiceManager.getService(Context.NETWORKMANAGEMENT_SERVICE);
1757 mNMService = INetworkManagementService.Stub.asInterface(b);
1758 return mNMService;
1759 }
1760 }
1761
1762 private final ArrayMap<OnNetworkActiveListener, INetworkActivityListener>
1763 mNetworkActivityListeners
1764 = new ArrayMap<OnNetworkActiveListener, INetworkActivityListener>();
1765
1766 /**
Robert Greenwalt6078b502014-06-11 16:05:07 -07001767 * Start listening to reports when the system's default data network is active, meaning it is
1768 * a good time to perform network traffic. Use {@link #isDefaultNetworkActive()}
1769 * to determine the current state of the system's default network after registering the
1770 * listener.
1771 * <p>
1772 * If the process default network has been set with
Paul Jensen72db88e2015-03-10 10:54:12 -04001773 * {@link ConnectivityManager#bindProcessToNetwork} this function will not
Robert Greenwalt6078b502014-06-11 16:05:07 -07001774 * reflect the process's default, but the system default.
Dianne Hackborn77b987f2014-02-26 16:20:52 -08001775 *
1776 * @param l The listener to be told when the network is active.
1777 */
Robert Greenwaltb2489872014-09-04 16:44:35 -07001778 public void addDefaultNetworkActiveListener(final OnNetworkActiveListener l) {
Dianne Hackborn77b987f2014-02-26 16:20:52 -08001779 INetworkActivityListener rl = new INetworkActivityListener.Stub() {
1780 @Override
1781 public void onNetworkActive() throws RemoteException {
1782 l.onNetworkActive();
1783 }
1784 };
1785
1786 try {
1787 getNetworkManagementService().registerNetworkActivityListener(rl);
1788 mNetworkActivityListeners.put(l, rl);
1789 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07001790 throw e.rethrowFromSystemServer();
Dianne Hackborn77b987f2014-02-26 16:20:52 -08001791 }
1792 }
1793
1794 /**
1795 * Remove network active listener previously registered with
Robert Greenwaltb2489872014-09-04 16:44:35 -07001796 * {@link #addDefaultNetworkActiveListener}.
Dianne Hackborn77b987f2014-02-26 16:20:52 -08001797 *
1798 * @param l Previously registered listener.
1799 */
Robert Greenwaltb2489872014-09-04 16:44:35 -07001800 public void removeDefaultNetworkActiveListener(OnNetworkActiveListener l) {
Dianne Hackborn77b987f2014-02-26 16:20:52 -08001801 INetworkActivityListener rl = mNetworkActivityListeners.get(l);
1802 if (rl == null) {
1803 throw new IllegalArgumentException("Listener not registered: " + l);
1804 }
1805 try {
1806 getNetworkManagementService().unregisterNetworkActivityListener(rl);
1807 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07001808 throw e.rethrowFromSystemServer();
Dianne Hackborn77b987f2014-02-26 16:20:52 -08001809 }
1810 }
1811
1812 /**
1813 * Return whether the data network is currently active. An active network means that
1814 * it is currently in a high power state for performing data transmission. On some
1815 * types of networks, it may be expensive to move and stay in such a state, so it is
1816 * more power efficient to batch network traffic together when the radio is already in
1817 * this state. This method tells you whether right now is currently a good time to
1818 * initiate network traffic, as the network is already active.
1819 */
Robert Greenwalt6078b502014-06-11 16:05:07 -07001820 public boolean isDefaultNetworkActive() {
Dianne Hackborn77b987f2014-02-26 16:20:52 -08001821 try {
1822 return getNetworkManagementService().isNetworkActive();
1823 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07001824 throw e.rethrowFromSystemServer();
Dianne Hackborn77b987f2014-02-26 16:20:52 -08001825 }
Dianne Hackborn77b987f2014-02-26 16:20:52 -08001826 }
1827
1828 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001829 * {@hide}
1830 */
Lorenzo Colittiffc42b02015-07-29 11:41:21 +09001831 public ConnectivityManager(Context context, IConnectivityManager service) {
1832 mContext = checkNotNull(context, "missing context");
Jeff Sharkeyf0ceede2011-08-02 17:22:34 -07001833 mService = checkNotNull(service, "missing IConnectivityManager");
Paul Jensene0bef712014-12-10 15:12:18 -05001834 sInstance = this;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001835 }
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -08001836
Jeff Sharkey8fc27e82012-04-04 20:40:58 -07001837 /** {@hide} */
1838 public static ConnectivityManager from(Context context) {
1839 return (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
1840 }
1841
Lorenzo Colittifbe9b1a2016-07-28 17:14:11 +09001842 /* TODO: These permissions checks don't belong in client-side code. Move them to
1843 * services.jar, possibly in com.android.server.net. */
1844
1845 /** {@hide} */
1846 public static final boolean checkChangePermission(Context context) {
1847 int uid = Binder.getCallingUid();
1848 return Settings.checkAndNoteChangeNetworkStateOperation(context, uid, Settings
1849 .getPackageNameForUid(context, uid), false /* throwException */);
1850 }
1851
Lorenzo Colittid5427052015-10-15 16:29:00 +09001852 /** {@hide} */
1853 public static final void enforceChangePermission(Context context) {
1854 int uid = Binder.getCallingUid();
1855 Settings.checkAndNoteChangeNetworkStateOperation(context, uid, Settings
1856 .getPackageNameForUid(context, uid), true /* throwException */);
1857 }
1858
Robert Greenwaltedb47662014-09-16 17:54:19 -07001859 /** {@hide */
1860 public static final void enforceTetherChangePermission(Context context) {
1861 if (context.getResources().getStringArray(
1862 com.android.internal.R.array.config_mobile_hotspot_provision_app).length == 2) {
1863 // Have a provisioning app - must only let system apps (which check this app)
1864 // turn on tethering
1865 context.enforceCallingOrSelfPermission(
Jeremy Kleind42209d2015-12-28 15:11:58 -08001866 android.Manifest.permission.TETHER_PRIVILEGED, "ConnectivityService");
Robert Greenwaltedb47662014-09-16 17:54:19 -07001867 } else {
Billy Laua7238a32015-08-01 12:45:02 +01001868 int uid = Binder.getCallingUid();
Lorenzo Colittid5427052015-10-15 16:29:00 +09001869 Settings.checkAndNoteWriteSettingsOperation(context, uid, Settings
1870 .getPackageNameForUid(context, uid), true /* throwException */);
Robert Greenwaltedb47662014-09-16 17:54:19 -07001871 }
1872 }
1873
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -08001874 /**
Paul Jensene0bef712014-12-10 15:12:18 -05001875 * @deprecated - use getSystemService. This is a kludge to support static access in certain
1876 * situations where a Context pointer is unavailable.
1877 * @hide
1878 */
Paul Jensen72db88e2015-03-10 10:54:12 -04001879 static ConnectivityManager getInstanceOrNull() {
1880 return sInstance;
1881 }
1882
1883 /**
1884 * @deprecated - use getSystemService. This is a kludge to support static access in certain
1885 * situations where a Context pointer is unavailable.
1886 * @hide
1887 */
1888 private static ConnectivityManager getInstance() {
1889 if (getInstanceOrNull() == null) {
Paul Jensene0bef712014-12-10 15:12:18 -05001890 throw new IllegalStateException("No ConnectivityManager yet constructed");
1891 }
Paul Jensen72db88e2015-03-10 10:54:12 -04001892 return getInstanceOrNull();
Paul Jensene0bef712014-12-10 15:12:18 -05001893 }
1894
1895 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08001896 * Get the set of tetherable, available interfaces. This list is limited by
1897 * device configuration and current interface existence.
Paul Jensenb2748922015-05-06 11:10:18 -04001898 * <p>This method requires the caller to hold the permission
1899 * {@link android.Manifest.permission#ACCESS_NETWORK_STATE}.
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08001900 *
1901 * @return an array of 0 or more Strings of tetherable interface names.
1902 *
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -08001903 * {@hide}
1904 */
1905 public String[] getTetherableIfaces() {
1906 try {
1907 return mService.getTetherableIfaces();
1908 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07001909 throw e.rethrowFromSystemServer();
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -08001910 }
1911 }
1912
1913 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08001914 * Get the set of tethered interfaces.
Paul Jensenb2748922015-05-06 11:10:18 -04001915 * <p>This method requires the caller to hold the permission
1916 * {@link android.Manifest.permission#ACCESS_NETWORK_STATE}.
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08001917 *
1918 * @return an array of 0 or more String of currently tethered interface names.
1919 *
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -08001920 * {@hide}
1921 */
1922 public String[] getTetheredIfaces() {
1923 try {
1924 return mService.getTetheredIfaces();
1925 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07001926 throw e.rethrowFromSystemServer();
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -08001927 }
1928 }
1929
1930 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08001931 * Get the set of interface names which attempted to tether but
1932 * failed. Re-attempting to tether may cause them to reset to the Tethered
1933 * state. Alternatively, causing the interface to be destroyed and recreated
1934 * may cause them to reset to the available state.
1935 * {@link ConnectivityManager#getLastTetherError} can be used to get more
1936 * information on the cause of the errors.
Paul Jensenb2748922015-05-06 11:10:18 -04001937 * <p>This method requires the caller to hold the permission
1938 * {@link android.Manifest.permission#ACCESS_NETWORK_STATE}.
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08001939 *
1940 * @return an array of 0 or more String indicating the interface names
1941 * which failed to tether.
1942 *
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -08001943 * {@hide}
1944 */
Robert Greenwalt5a735062010-03-02 17:25:02 -08001945 public String[] getTetheringErroredIfaces() {
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -08001946 try {
Robert Greenwalt5a735062010-03-02 17:25:02 -08001947 return mService.getTetheringErroredIfaces();
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -08001948 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07001949 throw e.rethrowFromSystemServer();
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -08001950 }
1951 }
1952
1953 /**
Robert Greenwalt9c7e2c22014-06-23 14:53:42 -07001954 * Get the set of tethered dhcp ranges.
1955 *
1956 * @return an array of 0 or more {@code String} of tethered dhcp ranges.
1957 * {@hide}
1958 */
1959 public String[] getTetheredDhcpRanges() {
1960 try {
1961 return mService.getTetheredDhcpRanges();
1962 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07001963 throw e.rethrowFromSystemServer();
Robert Greenwalt9c7e2c22014-06-23 14:53:42 -07001964 }
1965 }
1966
1967 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08001968 * Attempt to tether the named interface. This will setup a dhcp server
1969 * on the interface, forward and NAT IP packets and forward DNS requests
1970 * to the best active upstream network interface. Note that if no upstream
1971 * IP network interface is available, dhcp will still run and traffic will be
1972 * allowed between the tethered devices and this device, though upstream net
1973 * access will of course fail until an upstream network interface becomes
1974 * active.
Lorenzo Colittid5427052015-10-15 16:29:00 +09001975 *
1976 * <p>This method requires the caller to hold either the
1977 * {@link android.Manifest.permission#CHANGE_NETWORK_STATE} permission
1978 * or the ability to modify system settings as determined by
1979 * {@link android.provider.Settings.System#canWrite}.</p>
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08001980 *
Jeremy Klein36c7aa02016-01-22 14:11:45 -08001981 * <p>WARNING: New clients should not use this function. The only usages should be in PanService
1982 * and WifiStateMachine which need direct access. All other clients should use
1983 * {@link #startTethering} and {@link #stopTethering} which encapsulate proper provisioning
1984 * logic.</p>
1985 *
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08001986 * @param iface the interface name to tether.
1987 * @return error a {@code TETHER_ERROR} value indicating success or failure type
1988 *
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -08001989 * {@hide}
1990 */
Robert Greenwalt5a735062010-03-02 17:25:02 -08001991 public int tether(String iface) {
1992 try {
1993 return mService.tether(iface);
1994 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07001995 throw e.rethrowFromSystemServer();
Robert Greenwalt5a735062010-03-02 17:25:02 -08001996 }
1997 }
1998
1999 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08002000 * Stop tethering the named interface.
Lorenzo Colittid5427052015-10-15 16:29:00 +09002001 *
2002 * <p>This method requires the caller to hold either the
2003 * {@link android.Manifest.permission#CHANGE_NETWORK_STATE} permission
2004 * or the ability to modify system settings as determined by
2005 * {@link android.provider.Settings.System#canWrite}.</p>
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08002006 *
Jeremy Klein36c7aa02016-01-22 14:11:45 -08002007 * <p>WARNING: New clients should not use this function. The only usages should be in PanService
2008 * and WifiStateMachine which need direct access. All other clients should use
2009 * {@link #startTethering} and {@link #stopTethering} which encapsulate proper provisioning
2010 * logic.</p>
2011 *
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08002012 * @param iface the interface name to untether.
2013 * @return error a {@code TETHER_ERROR} value indicating success or failure type
2014 *
Robert Greenwalt5a735062010-03-02 17:25:02 -08002015 * {@hide}
2016 */
2017 public int untether(String iface) {
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -08002018 try {
2019 return mService.untether(iface);
2020 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07002021 throw e.rethrowFromSystemServer();
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -08002022 }
2023 }
Robert Greenwalt2a091d72010-02-11 18:18:40 -08002024
2025 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08002026 * Check if the device allows for tethering. It may be disabled via
Dianne Hackborn77b987f2014-02-26 16:20:52 -08002027 * {@code ro.tether.denied} system property, Settings.TETHER_SUPPORTED or
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08002028 * due to device configuration.
Paul Jensenb2748922015-05-06 11:10:18 -04002029 * <p>This method requires the caller to hold the permission
2030 * {@link android.Manifest.permission#ACCESS_NETWORK_STATE}.
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08002031 *
2032 * @return a boolean - {@code true} indicating Tethering is supported.
2033 *
Robert Greenwalt2a091d72010-02-11 18:18:40 -08002034 * {@hide}
2035 */
Jeremy Klein36c7aa02016-01-22 14:11:45 -08002036 @SystemApi
Robert Greenwalt2a091d72010-02-11 18:18:40 -08002037 public boolean isTetheringSupported() {
2038 try {
2039 return mService.isTetheringSupported();
2040 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07002041 throw e.rethrowFromSystemServer();
Robert Greenwalt2a091d72010-02-11 18:18:40 -08002042 }
2043 }
2044
2045 /**
Jeremy Klein36c7aa02016-01-22 14:11:45 -08002046 * Callback for use with {@link #startTethering} to find out whether tethering succeeded.
2047 * @hide
2048 */
2049 @SystemApi
2050 public static abstract class OnStartTetheringCallback {
2051 /**
2052 * Called when tethering has been successfully started.
2053 */
2054 public void onTetheringStarted() {};
2055
2056 /**
2057 * Called when starting tethering failed.
2058 */
2059 public void onTetheringFailed() {};
2060 }
2061
2062 /**
2063 * Convenient overload for
2064 * {@link #startTethering(int, boolean, OnStartTetheringCallback, Handler)} which passes a null
2065 * handler to run on the current thread's {@link Looper}.
2066 * @hide
2067 */
2068 @SystemApi
2069 public void startTethering(int type, boolean showProvisioningUi,
2070 final OnStartTetheringCallback callback) {
2071 startTethering(type, showProvisioningUi, callback, null);
2072 }
2073
2074 /**
2075 * Runs tether provisioning for the given type if needed and then starts tethering if
2076 * the check succeeds. If no carrier provisioning is required for tethering, tethering is
2077 * enabled immediately. If provisioning fails, tethering will not be enabled. It also
2078 * schedules tether provisioning re-checks if appropriate.
2079 *
2080 * @param type The type of tethering to start. Must be one of
2081 * {@link ConnectivityManager.TETHERING_WIFI},
2082 * {@link ConnectivityManager.TETHERING_USB}, or
2083 * {@link ConnectivityManager.TETHERING_BLUETOOTH}.
2084 * @param showProvisioningUi a boolean indicating to show the provisioning app UI if there
2085 * is one. This should be true the first time this function is called and also any time
2086 * the user can see this UI. It gives users information from their carrier about the
2087 * check failing and how they can sign up for tethering if possible.
2088 * @param callback an {@link OnStartTetheringCallback} which will be called to notify the caller
2089 * of the result of trying to tether.
2090 * @param handler {@link Handler} to specify the thread upon which the callback will be invoked.
2091 * @hide
2092 */
2093 @SystemApi
2094 public void startTethering(int type, boolean showProvisioningUi,
2095 final OnStartTetheringCallback callback, Handler handler) {
2096 ResultReceiver wrappedCallback = new ResultReceiver(handler) {
2097 @Override
2098 protected void onReceiveResult(int resultCode, Bundle resultData) {
2099 if (resultCode == TETHER_ERROR_NO_ERROR) {
2100 callback.onTetheringStarted();
2101 } else {
2102 callback.onTetheringFailed();
2103 }
2104 }
2105 };
2106 try {
2107 mService.startTethering(type, wrappedCallback, showProvisioningUi);
2108 } catch (RemoteException e) {
2109 Log.e(TAG, "Exception trying to start tethering.", e);
2110 wrappedCallback.send(TETHER_ERROR_SERVICE_UNAVAIL, null);
2111 }
2112 }
2113
2114 /**
2115 * Stops tethering for the given type. Also cancels any provisioning rechecks for that type if
2116 * applicable.
2117 *
2118 * @param type The type of tethering to stop. Must be one of
2119 * {@link ConnectivityManager.TETHERING_WIFI},
2120 * {@link ConnectivityManager.TETHERING_USB}, or
2121 * {@link ConnectivityManager.TETHERING_BLUETOOTH}.
2122 * @hide
2123 */
2124 @SystemApi
2125 public void stopTethering(int type) {
2126 try {
2127 mService.stopTethering(type);
2128 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07002129 throw e.rethrowFromSystemServer();
Jeremy Klein36c7aa02016-01-22 14:11:45 -08002130 }
2131 }
2132
2133 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08002134 * Get the list of regular expressions that define any tetherable
2135 * USB network interfaces. If USB tethering is not supported by the
2136 * device, this list should be empty.
Paul Jensenb2748922015-05-06 11:10:18 -04002137 * <p>This method requires the caller to hold the permission
2138 * {@link android.Manifest.permission#ACCESS_NETWORK_STATE}.
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08002139 *
2140 * @return an array of 0 or more regular expression Strings defining
2141 * what interfaces are considered tetherable usb interfaces.
2142 *
Robert Greenwalt2a091d72010-02-11 18:18:40 -08002143 * {@hide}
2144 */
2145 public String[] getTetherableUsbRegexs() {
2146 try {
2147 return mService.getTetherableUsbRegexs();
2148 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07002149 throw e.rethrowFromSystemServer();
Robert Greenwalt2a091d72010-02-11 18:18:40 -08002150 }
2151 }
2152
2153 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08002154 * Get the list of regular expressions that define any tetherable
2155 * Wifi network interfaces. If Wifi tethering is not supported by the
2156 * device, this list should be empty.
Paul Jensenb2748922015-05-06 11:10:18 -04002157 * <p>This method requires the caller to hold the permission
2158 * {@link android.Manifest.permission#ACCESS_NETWORK_STATE}.
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08002159 *
2160 * @return an array of 0 or more regular expression Strings defining
2161 * what interfaces are considered tetherable wifi interfaces.
2162 *
Robert Greenwalt2a091d72010-02-11 18:18:40 -08002163 * {@hide}
2164 */
2165 public String[] getTetherableWifiRegexs() {
2166 try {
2167 return mService.getTetherableWifiRegexs();
2168 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07002169 throw e.rethrowFromSystemServer();
Robert Greenwalt2a091d72010-02-11 18:18:40 -08002170 }
2171 }
Robert Greenwalt5a735062010-03-02 17:25:02 -08002172
Danica Chang6fdd0c62010-08-11 14:54:43 -07002173 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08002174 * Get the list of regular expressions that define any tetherable
2175 * Bluetooth network interfaces. If Bluetooth tethering is not supported by the
2176 * device, this list should be empty.
Paul Jensenb2748922015-05-06 11:10:18 -04002177 * <p>This method requires the caller to hold the permission
2178 * {@link android.Manifest.permission#ACCESS_NETWORK_STATE}.
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08002179 *
2180 * @return an array of 0 or more regular expression Strings defining
2181 * what interfaces are considered tetherable bluetooth interfaces.
2182 *
Danica Chang6fdd0c62010-08-11 14:54:43 -07002183 * {@hide}
2184 */
2185 public String[] getTetherableBluetoothRegexs() {
2186 try {
2187 return mService.getTetherableBluetoothRegexs();
2188 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07002189 throw e.rethrowFromSystemServer();
Danica Chang6fdd0c62010-08-11 14:54:43 -07002190 }
2191 }
2192
Mike Lockwood6c2260b2011-07-19 13:04:47 -07002193 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08002194 * Attempt to both alter the mode of USB and Tethering of USB. A
2195 * utility method to deal with some of the complexity of USB - will
2196 * attempt to switch to Rndis and subsequently tether the resulting
2197 * interface on {@code true} or turn off tethering and switch off
2198 * Rndis on {@code false}.
Lorenzo Colittid5427052015-10-15 16:29:00 +09002199 *
2200 * <p>This method requires the caller to hold either the
2201 * {@link android.Manifest.permission#CHANGE_NETWORK_STATE} permission
2202 * or the ability to modify system settings as determined by
2203 * {@link android.provider.Settings.System#canWrite}.</p>
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08002204 *
2205 * @param enable a boolean - {@code true} to enable tethering
2206 * @return error a {@code TETHER_ERROR} value indicating success or failure type
2207 *
Mike Lockwood6c2260b2011-07-19 13:04:47 -07002208 * {@hide}
2209 */
2210 public int setUsbTethering(boolean enable) {
2211 try {
2212 return mService.setUsbTethering(enable);
2213 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07002214 throw e.rethrowFromSystemServer();
Mike Lockwood6c2260b2011-07-19 13:04:47 -07002215 }
2216 }
2217
Robert Greenwalt5a735062010-03-02 17:25:02 -08002218 /** {@hide} */
2219 public static final int TETHER_ERROR_NO_ERROR = 0;
2220 /** {@hide} */
2221 public static final int TETHER_ERROR_UNKNOWN_IFACE = 1;
2222 /** {@hide} */
2223 public static final int TETHER_ERROR_SERVICE_UNAVAIL = 2;
2224 /** {@hide} */
2225 public static final int TETHER_ERROR_UNSUPPORTED = 3;
2226 /** {@hide} */
2227 public static final int TETHER_ERROR_UNAVAIL_IFACE = 4;
2228 /** {@hide} */
2229 public static final int TETHER_ERROR_MASTER_ERROR = 5;
2230 /** {@hide} */
2231 public static final int TETHER_ERROR_TETHER_IFACE_ERROR = 6;
2232 /** {@hide} */
2233 public static final int TETHER_ERROR_UNTETHER_IFACE_ERROR = 7;
2234 /** {@hide} */
2235 public static final int TETHER_ERROR_ENABLE_NAT_ERROR = 8;
2236 /** {@hide} */
2237 public static final int TETHER_ERROR_DISABLE_NAT_ERROR = 9;
2238 /** {@hide} */
2239 public static final int TETHER_ERROR_IFACE_CFG_ERROR = 10;
Jeremy Klein36c7aa02016-01-22 14:11:45 -08002240 /** {@hide} */
2241 public static final int TETHER_ERROR_PROVISION_FAILED = 11;
Robert Greenwalt5a735062010-03-02 17:25:02 -08002242
2243 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08002244 * Get a more detailed error code after a Tethering or Untethering
2245 * request asynchronously failed.
Paul Jensenb2748922015-05-06 11:10:18 -04002246 * <p>This method requires the caller to hold the permission
2247 * {@link android.Manifest.permission#ACCESS_NETWORK_STATE}.
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08002248 *
2249 * @param iface The name of the interface of interest
Robert Greenwalt5a735062010-03-02 17:25:02 -08002250 * @return error The error code of the last error tethering or untethering the named
2251 * interface
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08002252 *
Robert Greenwalt5a735062010-03-02 17:25:02 -08002253 * {@hide}
2254 */
2255 public int getLastTetherError(String iface) {
2256 try {
2257 return mService.getLastTetherError(iface);
2258 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07002259 throw e.rethrowFromSystemServer();
Robert Greenwalt5a735062010-03-02 17:25:02 -08002260 }
Robert Greenwalt14f2ef42010-06-15 12:19:37 -07002261 }
2262
2263 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08002264 * Report network connectivity status. This is currently used only
2265 * to alter status bar UI.
Paul Jensenb2748922015-05-06 11:10:18 -04002266 * <p>This method requires the caller to hold the permission
2267 * {@link android.Manifest.permission#STATUS_BAR}.
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08002268 *
Robert Greenwaltd7085fc2010-09-08 15:24:47 -07002269 * @param networkType The type of network you want to report on
2270 * @param percentage The quality of the connection 0 is bad, 100 is good
2271 * {@hide}
2272 */
2273 public void reportInetCondition(int networkType, int percentage) {
2274 try {
2275 mService.reportInetCondition(networkType, percentage);
2276 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07002277 throw e.rethrowFromSystemServer();
Robert Greenwaltd7085fc2010-09-08 15:24:47 -07002278 }
2279 }
Robert Greenwalt434203a2010-10-11 16:00:27 -07002280
2281 /**
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07002282 * Report a problem network to the framework. This provides a hint to the system
Ye Wenb87875e2014-07-21 14:19:01 -07002283 * that there might be connectivity problems on this network and may cause
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07002284 * the framework to re-evaluate network connectivity and/or switch to another
2285 * network.
Robert Greenwalt9258c642014-03-26 16:47:06 -07002286 *
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07002287 * @param network The {@link Network} the application was attempting to use
2288 * or {@code null} to indicate the current default network.
Paul Jensenbfd17b72015-04-07 12:43:13 -04002289 * @deprecated Use {@link #reportNetworkConnectivity} which allows reporting both
2290 * working and non-working connectivity.
Robert Greenwalt9258c642014-03-26 16:47:06 -07002291 */
2292 public void reportBadNetwork(Network network) {
2293 try {
Paul Jensenbfd17b72015-04-07 12:43:13 -04002294 // One of these will be ignored because it matches system's current state.
2295 // The other will trigger the necessary reevaluation.
2296 mService.reportNetworkConnectivity(network, true);
2297 mService.reportNetworkConnectivity(network, false);
2298 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07002299 throw e.rethrowFromSystemServer();
Paul Jensenbfd17b72015-04-07 12:43:13 -04002300 }
2301 }
2302
2303 /**
2304 * Report to the framework whether a network has working connectivity.
2305 * This provides a hint to the system that a particular network is providing
2306 * working connectivity or not. In response the framework may re-evaluate
2307 * the network's connectivity and might take further action thereafter.
2308 *
2309 * @param network The {@link Network} the application was attempting to use
2310 * or {@code null} to indicate the current default network.
2311 * @param hasConnectivity {@code true} if the application was able to successfully access the
2312 * Internet using {@code network} or {@code false} if not.
2313 */
2314 public void reportNetworkConnectivity(Network network, boolean hasConnectivity) {
2315 try {
2316 mService.reportNetworkConnectivity(network, hasConnectivity);
Robert Greenwalt9258c642014-03-26 16:47:06 -07002317 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07002318 throw e.rethrowFromSystemServer();
Robert Greenwalt9258c642014-03-26 16:47:06 -07002319 }
2320 }
2321
2322 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08002323 * Set a network-independent global http proxy. This is not normally what you want
2324 * for typical HTTP proxies - they are general network dependent. However if you're
2325 * doing something unusual like general internal filtering this may be useful. On
2326 * a private network where the proxy is not accessible, you may break HTTP using this.
Paul Jensen0d719ca2015-02-13 14:18:39 -05002327 * <p>This method requires the caller to hold the permission
Jason Monkfaf3fd52014-05-07 18:41:13 -04002328 * android.Manifest.permission#CONNECTIVITY_INTERNAL.
Paul Jensenb2748922015-05-06 11:10:18 -04002329 *
2330 * @param p A {@link ProxyInfo} object defining the new global
2331 * HTTP proxy. A {@code null} value will clear the global HTTP proxy.
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07002332 * @hide
Robert Greenwalt434203a2010-10-11 16:00:27 -07002333 */
Jason Monk207900c2014-04-25 15:00:09 -04002334 public void setGlobalProxy(ProxyInfo p) {
Robert Greenwalt434203a2010-10-11 16:00:27 -07002335 try {
2336 mService.setGlobalProxy(p);
2337 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07002338 throw e.rethrowFromSystemServer();
Robert Greenwalt434203a2010-10-11 16:00:27 -07002339 }
2340 }
2341
2342 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08002343 * Retrieve any network-independent global HTTP proxy.
2344 *
Jason Monk207900c2014-04-25 15:00:09 -04002345 * @return {@link ProxyInfo} for the current global HTTP proxy or {@code null}
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08002346 * if no global HTTP proxy is set.
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07002347 * @hide
Robert Greenwalt434203a2010-10-11 16:00:27 -07002348 */
Jason Monk207900c2014-04-25 15:00:09 -04002349 public ProxyInfo getGlobalProxy() {
Robert Greenwalt434203a2010-10-11 16:00:27 -07002350 try {
2351 return mService.getGlobalProxy();
2352 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07002353 throw e.rethrowFromSystemServer();
Robert Greenwalt434203a2010-10-11 16:00:27 -07002354 }
2355 }
2356
2357 /**
Paul Jensencee9b512015-05-06 07:32:40 -04002358 * Retrieve the global HTTP proxy, or if no global HTTP proxy is set, a
2359 * network-specific HTTP proxy. If {@code network} is null, the
2360 * network-specific proxy returned is the proxy of the default active
2361 * network.
2362 *
2363 * @return {@link ProxyInfo} for the current global HTTP proxy, or if no
2364 * global HTTP proxy is set, {@code ProxyInfo} for {@code network},
2365 * or when {@code network} is {@code null},
2366 * the {@code ProxyInfo} for the default active network. Returns
2367 * {@code null} when no proxy applies or the caller doesn't have
2368 * permission to use {@code network}.
2369 * @hide
2370 */
2371 public ProxyInfo getProxyForNetwork(Network network) {
2372 try {
2373 return mService.getProxyForNetwork(network);
2374 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07002375 throw e.rethrowFromSystemServer();
Paul Jensencee9b512015-05-06 07:32:40 -04002376 }
2377 }
2378
2379 /**
Paul Jensene0bef712014-12-10 15:12:18 -05002380 * Get the current default HTTP proxy settings. If a global proxy is set it will be returned,
2381 * otherwise if this process is bound to a {@link Network} using
Paul Jensen72db88e2015-03-10 10:54:12 -04002382 * {@link #bindProcessToNetwork} then that {@code Network}'s proxy is returned, otherwise
Paul Jensene0bef712014-12-10 15:12:18 -05002383 * the default network's proxy is returned.
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08002384 *
Jason Monk207900c2014-04-25 15:00:09 -04002385 * @return the {@link ProxyInfo} for the current HTTP proxy, or {@code null} if no
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08002386 * HTTP proxy is active.
Robert Greenwalt434203a2010-10-11 16:00:27 -07002387 */
Paul Jensene0bef712014-12-10 15:12:18 -05002388 public ProxyInfo getDefaultProxy() {
Paul Jensencee9b512015-05-06 07:32:40 -04002389 return getProxyForNetwork(getBoundNetworkForProcess());
Robert Greenwalt434203a2010-10-11 16:00:27 -07002390 }
Robert Greenwaltd55a6b42011-03-25 13:09:25 -07002391
2392 /**
Robert Greenwalt9b2886e2011-08-31 11:46:42 -07002393 * Returns true if the hardware supports the given network type
2394 * else it returns false. This doesn't indicate we have coverage
2395 * or are authorized onto a network, just whether or not the
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08002396 * hardware supports it. For example a GSM phone without a SIM
2397 * should still return {@code true} for mobile data, but a wifi only
2398 * tablet would return {@code false}.
Paul Jensenb2748922015-05-06 11:10:18 -04002399 * <p>This method requires the caller to hold the permission
2400 * {@link android.Manifest.permission#ACCESS_NETWORK_STATE}.
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08002401 *
2402 * @param networkType The network type we'd like to check
2403 * @return {@code true} if supported, else {@code false}
2404 *
Robert Greenwalt9b2886e2011-08-31 11:46:42 -07002405 * @hide
2406 */
2407 public boolean isNetworkSupported(int networkType) {
2408 try {
2409 return mService.isNetworkSupported(networkType);
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07002410 } catch (RemoteException e) {
2411 throw e.rethrowFromSystemServer();
2412 }
Robert Greenwalt9b2886e2011-08-31 11:46:42 -07002413 }
Jeff Sharkey9f7cbf02012-04-12 18:34:54 -07002414
2415 /**
2416 * Returns if the currently active data network is metered. A network is
2417 * classified as metered when the user is sensitive to heavy data usage on
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08002418 * that connection due to monetary costs, data limitations or
2419 * battery/performance issues. You should check this before doing large
2420 * data transfers, and warn the user or delay the operation until another
2421 * network is available.
Paul Jensenb2748922015-05-06 11:10:18 -04002422 * <p>This method requires the caller to hold the permission
2423 * {@link android.Manifest.permission#ACCESS_NETWORK_STATE}.
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08002424 *
2425 * @return {@code true} if large transfers should be avoided, otherwise
2426 * {@code false}.
Jeff Sharkey9f7cbf02012-04-12 18:34:54 -07002427 */
2428 public boolean isActiveNetworkMetered() {
2429 try {
2430 return mService.isActiveNetworkMetered();
2431 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07002432 throw e.rethrowFromSystemServer();
Jeff Sharkey9f7cbf02012-04-12 18:34:54 -07002433 }
2434 }
Jeff Sharkey69ddab42012-08-25 00:05:46 -07002435
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08002436 /**
2437 * If the LockdownVpn mechanism is enabled, updates the vpn
2438 * with a reload of its profile.
2439 *
2440 * @return a boolean with {@code} indicating success
2441 *
2442 * <p>This method can only be called by the system UID
2443 * {@hide}
2444 */
Jeff Sharkey69ddab42012-08-25 00:05:46 -07002445 public boolean updateLockdownVpn() {
2446 try {
2447 return mService.updateLockdownVpn();
2448 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07002449 throw e.rethrowFromSystemServer();
Jeff Sharkey69ddab42012-08-25 00:05:46 -07002450 }
2451 }
Irfan Sheriffda6da092012-08-16 12:49:23 -07002452
2453 /**
Wink Saville948282b2013-08-29 08:55:16 -07002454 * Check mobile provisioning.
Wink Savilleab9321d2013-06-29 21:10:57 -07002455 *
Wink Savilleab9321d2013-06-29 21:10:57 -07002456 * @param suggestedTimeOutMs, timeout in milliseconds
Wink Savilleab9321d2013-06-29 21:10:57 -07002457 *
2458 * @return time out that will be used, maybe less that suggestedTimeOutMs
2459 * -1 if an error.
2460 *
2461 * {@hide}
2462 */
Wink Saville948282b2013-08-29 08:55:16 -07002463 public int checkMobileProvisioning(int suggestedTimeOutMs) {
Wink Savilleab9321d2013-06-29 21:10:57 -07002464 int timeOutMs = -1;
2465 try {
Wink Saville948282b2013-08-29 08:55:16 -07002466 timeOutMs = mService.checkMobileProvisioning(suggestedTimeOutMs);
Wink Savilleab9321d2013-06-29 21:10:57 -07002467 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07002468 throw e.rethrowFromSystemServer();
Wink Savilleab9321d2013-06-29 21:10:57 -07002469 }
2470 return timeOutMs;
2471 }
Robert Greenwalte182bfe2013-07-16 12:06:09 -07002472
2473 /**
Wink Saville42d4f082013-07-20 20:31:59 -07002474 * Get the mobile provisioning url.
Robert Greenwalte182bfe2013-07-16 12:06:09 -07002475 * {@hide}
2476 */
2477 public String getMobileProvisioningUrl() {
2478 try {
2479 return mService.getMobileProvisioningUrl();
2480 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07002481 throw e.rethrowFromSystemServer();
Robert Greenwalte182bfe2013-07-16 12:06:09 -07002482 }
Robert Greenwalte182bfe2013-07-16 12:06:09 -07002483 }
Wink Saville42d4f082013-07-20 20:31:59 -07002484
2485 /**
Wink Saville948282b2013-08-29 08:55:16 -07002486 * Set sign in error notification to visible or in visible
2487 *
2488 * @param visible
2489 * @param networkType
2490 *
2491 * {@hide}
Paul Jensen3541e9f2015-03-18 12:23:02 -04002492 * @deprecated Doesn't properly deal with multiple connected networks of the same type.
Wink Saville948282b2013-08-29 08:55:16 -07002493 */
2494 public void setProvisioningNotificationVisible(boolean visible, int networkType,
Paul Jensen89e0f092014-09-15 15:59:36 -04002495 String action) {
Wink Saville948282b2013-08-29 08:55:16 -07002496 try {
Paul Jensen89e0f092014-09-15 15:59:36 -04002497 mService.setProvisioningNotificationVisible(visible, networkType, action);
Wink Saville948282b2013-08-29 08:55:16 -07002498 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07002499 throw e.rethrowFromSystemServer();
Wink Saville948282b2013-08-29 08:55:16 -07002500 }
2501 }
Yuhao Zheng5cd1a0e2013-09-09 17:00:04 -07002502
2503 /**
2504 * Set the value for enabling/disabling airplane mode
Paul Jensenb2748922015-05-06 11:10:18 -04002505 * <p>This method requires the caller to hold the permission
2506 * {@link android.Manifest.permission#CONNECTIVITY_INTERNAL}.
Yuhao Zheng5cd1a0e2013-09-09 17:00:04 -07002507 *
2508 * @param enable whether to enable airplane mode or not
2509 *
Yuhao Zheng5cd1a0e2013-09-09 17:00:04 -07002510 * @hide
2511 */
2512 public void setAirplaneMode(boolean enable) {
2513 try {
2514 mService.setAirplaneMode(enable);
2515 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07002516 throw e.rethrowFromSystemServer();
Yuhao Zheng5cd1a0e2013-09-09 17:00:04 -07002517 }
2518 }
Robert Greenwalte049c232014-04-11 15:53:27 -07002519
2520 /** {@hide} */
Robert Greenwalta67be032014-05-16 15:49:14 -07002521 public void registerNetworkFactory(Messenger messenger, String name) {
Robert Greenwalte049c232014-04-11 15:53:27 -07002522 try {
Robert Greenwalta67be032014-05-16 15:49:14 -07002523 mService.registerNetworkFactory(messenger, name);
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07002524 } catch (RemoteException e) {
2525 throw e.rethrowFromSystemServer();
2526 }
Robert Greenwalta67be032014-05-16 15:49:14 -07002527 }
2528
2529 /** {@hide} */
2530 public void unregisterNetworkFactory(Messenger messenger) {
2531 try {
2532 mService.unregisterNetworkFactory(messenger);
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07002533 } catch (RemoteException e) {
2534 throw e.rethrowFromSystemServer();
2535 }
Robert Greenwalte049c232014-04-11 15:53:27 -07002536 }
Robert Greenwalt7b816022014-04-18 15:25:25 -07002537
Paul Jensen31a94f42015-02-13 14:18:39 -05002538 /**
2539 * @hide
2540 * Register a NetworkAgent with ConnectivityService.
2541 * @return NetID corresponding to NetworkAgent.
2542 */
2543 public int registerNetworkAgent(Messenger messenger, NetworkInfo ni, LinkProperties lp,
Sreeram Ramachandran8cd33ed2014-07-23 15:23:15 -07002544 NetworkCapabilities nc, int score, NetworkMisc misc) {
Robert Greenwalt7b816022014-04-18 15:25:25 -07002545 try {
Paul Jensen31a94f42015-02-13 14:18:39 -05002546 return mService.registerNetworkAgent(messenger, ni, lp, nc, score, misc);
2547 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07002548 throw e.rethrowFromSystemServer();
Paul Jensen31a94f42015-02-13 14:18:39 -05002549 }
Robert Greenwalt7b816022014-04-18 15:25:25 -07002550 }
2551
Robert Greenwalt9258c642014-03-26 16:47:06 -07002552 /**
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07002553 * Base class for NetworkRequest callbacks. Used for notifications about network
2554 * changes. Should be extended by applications wanting notifications.
Robert Greenwalt9258c642014-03-26 16:47:06 -07002555 */
Robert Greenwalt6078b502014-06-11 16:05:07 -07002556 public static class NetworkCallback {
Robert Greenwalt7b816022014-04-18 15:25:25 -07002557 /**
Lorenzo Colitti07086932015-04-24 12:23:24 +09002558 * Called when the framework connects to a new network to evaluate whether it satisfies this
2559 * request. If evaluation succeeds, this callback may be followed by an {@link #onAvailable}
2560 * callback. There is no guarantee that this new network will satisfy any requests, or that
2561 * the network will stay connected for longer than the time necessary to evaluate it.
2562 * <p>
2563 * Most applications <b>should not</b> act on this callback, and should instead use
2564 * {@link #onAvailable}. This callback is intended for use by applications that can assist
2565 * the framework in properly evaluating the network &mdash; for example, an application that
2566 * can automatically log in to a captive portal without user intervention.
2567 *
2568 * @param network The {@link Network} of the network that is being evaluated.
Lorenzo Colitti66276122015-06-11 14:27:17 +09002569 *
2570 * @hide
Robert Greenwalt7b816022014-04-18 15:25:25 -07002571 */
Robert Greenwalt6078b502014-06-11 16:05:07 -07002572 public void onPreCheck(Network network) {}
Robert Greenwalt7b816022014-04-18 15:25:25 -07002573
2574 /**
Lorenzo Colitti07086932015-04-24 12:23:24 +09002575 * Called when the framework connects and has declared a new network ready for use.
Robert Greenwalt6078b502014-06-11 16:05:07 -07002576 * This callback may be called more than once if the {@link Network} that is
2577 * satisfying the request changes.
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07002578 *
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07002579 * @param network The {@link Network} of the satisfying network.
Robert Greenwalt7b816022014-04-18 15:25:25 -07002580 */
Robert Greenwalt6078b502014-06-11 16:05:07 -07002581 public void onAvailable(Network network) {}
Robert Greenwalt7b816022014-04-18 15:25:25 -07002582
2583 /**
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07002584 * Called when the network is about to be disconnected. Often paired with an
Robert Greenwalt6078b502014-06-11 16:05:07 -07002585 * {@link NetworkCallback#onAvailable} call with the new replacement network
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07002586 * for graceful handover. This may not be called if we have a hard loss
2587 * (loss without warning). This may be followed by either a
Robert Greenwalt6078b502014-06-11 16:05:07 -07002588 * {@link NetworkCallback#onLost} call or a
2589 * {@link NetworkCallback#onAvailable} call for this network depending
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07002590 * on whether we lose or regain it.
2591 *
Robert Greenwalt6078b502014-06-11 16:05:07 -07002592 * @param network The {@link Network} that is about to be disconnected.
2593 * @param maxMsToLive The time in ms the framework will attempt to keep the
2594 * network connected. Note that the network may suffer a
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07002595 * hard loss at any time.
Robert Greenwalt7b816022014-04-18 15:25:25 -07002596 */
Robert Greenwalt6078b502014-06-11 16:05:07 -07002597 public void onLosing(Network network, int maxMsToLive) {}
Robert Greenwalt7b816022014-04-18 15:25:25 -07002598
2599 /**
2600 * Called when the framework has a hard loss of the network or when the
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07002601 * graceful failure ends.
2602 *
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07002603 * @param network The {@link Network} lost.
Robert Greenwalt7b816022014-04-18 15:25:25 -07002604 */
Robert Greenwalt6078b502014-06-11 16:05:07 -07002605 public void onLost(Network network) {}
Robert Greenwalt7b816022014-04-18 15:25:25 -07002606
2607 /**
2608 * Called if no network is found in the given timeout time. If no timeout is given,
2609 * this will not be called.
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07002610 * @hide
Robert Greenwalt7b816022014-04-18 15:25:25 -07002611 */
Robert Greenwalt6078b502014-06-11 16:05:07 -07002612 public void onUnavailable() {}
Robert Greenwalt7b816022014-04-18 15:25:25 -07002613
2614 /**
2615 * Called when the network the framework connected to for this request
2616 * changes capabilities but still satisfies the stated need.
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07002617 *
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07002618 * @param network The {@link Network} whose capabilities have changed.
Lorenzo Colittie285b432015-04-23 15:32:42 +09002619 * @param networkCapabilities The new {@link android.net.NetworkCapabilities} for this network.
Robert Greenwalt7b816022014-04-18 15:25:25 -07002620 */
Robert Greenwalt6078b502014-06-11 16:05:07 -07002621 public void onCapabilitiesChanged(Network network,
Robert Greenwalt7b816022014-04-18 15:25:25 -07002622 NetworkCapabilities networkCapabilities) {}
2623
2624 /**
2625 * Called when the network the framework connected to for this request
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07002626 * changes {@link LinkProperties}.
2627 *
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07002628 * @param network The {@link Network} whose link properties have changed.
2629 * @param linkProperties The new {@link LinkProperties} for this network.
Robert Greenwalt7b816022014-04-18 15:25:25 -07002630 */
Robert Greenwalt6078b502014-06-11 16:05:07 -07002631 public void onLinkPropertiesChanged(Network network, LinkProperties linkProperties) {}
Robert Greenwalt7b816022014-04-18 15:25:25 -07002632
Robert Greenwalt8d482522015-06-24 13:23:42 -07002633 /**
2634 * Called when the network the framework connected to for this request
2635 * goes into {@link NetworkInfo.DetailedState.SUSPENDED}.
2636 * This generally means that while the TCP connections are still live,
2637 * temporarily network data fails to transfer. Specifically this is used
2638 * on cellular networks to mask temporary outages when driving through
2639 * a tunnel, etc.
2640 * @hide
2641 */
2642 public void onNetworkSuspended(Network network) {}
2643
2644 /**
2645 * Called when the network the framework connected to for this request
2646 * returns from a {@link NetworkInfo.DetailedState.SUSPENDED} state.
2647 * This should always be preceeded by a matching {@code onNetworkSuspended}
2648 * call.
2649 * @hide
2650 */
2651 public void onNetworkResumed(Network network) {}
2652
Robert Greenwalt6078b502014-06-11 16:05:07 -07002653 private NetworkRequest networkRequest;
Robert Greenwalt7b816022014-04-18 15:25:25 -07002654 }
2655
Robert Greenwalt9258c642014-03-26 16:47:06 -07002656 private static final int BASE = Protocol.BASE_CONNECTIVITY_MANAGER;
Robert Greenwalt9258c642014-03-26 16:47:06 -07002657 /** @hide */
Robert Greenwalt8d482522015-06-24 13:23:42 -07002658 public static final int CALLBACK_PRECHECK = BASE + 1;
2659 /** @hide */
2660 public static final int CALLBACK_AVAILABLE = BASE + 2;
2661 /** @hide arg1 = TTL */
2662 public static final int CALLBACK_LOSING = BASE + 3;
2663 /** @hide */
2664 public static final int CALLBACK_LOST = BASE + 4;
2665 /** @hide */
2666 public static final int CALLBACK_UNAVAIL = BASE + 5;
2667 /** @hide */
2668 public static final int CALLBACK_CAP_CHANGED = BASE + 6;
2669 /** @hide */
2670 public static final int CALLBACK_IP_CHANGED = BASE + 7;
2671 /** @hide */
2672 public static final int CALLBACK_RELEASED = BASE + 8;
2673 /** @hide */
2674 public static final int CALLBACK_EXIT = BASE + 9;
Robert Greenwalt562cc542014-05-15 18:07:26 -07002675 /** @hide obj = NetworkCapabilities, arg1 = seq number */
Robert Greenwalt8d482522015-06-24 13:23:42 -07002676 private static final int EXPIRE_LEGACY_REQUEST = BASE + 10;
2677 /** @hide */
2678 public static final int CALLBACK_SUSPENDED = BASE + 11;
2679 /** @hide */
2680 public static final int CALLBACK_RESUMED = BASE + 12;
Robert Greenwalt9258c642014-03-26 16:47:06 -07002681
Robert Greenwalt562cc542014-05-15 18:07:26 -07002682 private class CallbackHandler extends Handler {
Robert Greenwalt6078b502014-06-11 16:05:07 -07002683 private final HashMap<NetworkRequest, NetworkCallback>mCallbackMap;
Robert Greenwalt9258c642014-03-26 16:47:06 -07002684 private final AtomicInteger mRefCount;
2685 private static final String TAG = "ConnectivityManager.CallbackHandler";
2686 private final ConnectivityManager mCm;
Robert Greenwalta9ebeef2015-09-03 16:41:45 -07002687 private static final boolean DBG = false;
Robert Greenwalt9258c642014-03-26 16:47:06 -07002688
Robert Greenwalt6078b502014-06-11 16:05:07 -07002689 CallbackHandler(Looper looper, HashMap<NetworkRequest, NetworkCallback>callbackMap,
Robert Greenwalt9258c642014-03-26 16:47:06 -07002690 AtomicInteger refCount, ConnectivityManager cm) {
2691 super(looper);
2692 mCallbackMap = callbackMap;
2693 mRefCount = refCount;
2694 mCm = cm;
2695 }
2696
2697 @Override
2698 public void handleMessage(Message message) {
Lorenzo Colittidb95a602015-04-24 15:21:21 +09002699 NetworkRequest request = (NetworkRequest) getObject(message, NetworkRequest.class);
2700 Network network = (Network) getObject(message, Network.class);
Lorenzo Colittifcfa7d92016-03-01 22:56:37 +09002701 if (DBG) {
2702 Log.d(TAG, whatToString(message.what) + " for network " + network);
2703 }
Robert Greenwalt9258c642014-03-26 16:47:06 -07002704 switch (message.what) {
2705 case CALLBACK_PRECHECK: {
Lorenzo Colittidb95a602015-04-24 15:21:21 +09002706 NetworkCallback callback = getCallback(request, "PRECHECK");
2707 if (callback != null) {
2708 callback.onPreCheck(network);
Robert Greenwalt9258c642014-03-26 16:47:06 -07002709 }
2710 break;
2711 }
2712 case CALLBACK_AVAILABLE: {
Lorenzo Colittidb95a602015-04-24 15:21:21 +09002713 NetworkCallback callback = getCallback(request, "AVAILABLE");
2714 if (callback != null) {
2715 callback.onAvailable(network);
Robert Greenwalt9258c642014-03-26 16:47:06 -07002716 }
2717 break;
2718 }
2719 case CALLBACK_LOSING: {
Lorenzo Colittidb95a602015-04-24 15:21:21 +09002720 NetworkCallback callback = getCallback(request, "LOSING");
2721 if (callback != null) {
2722 callback.onLosing(network, message.arg1);
Robert Greenwalt9258c642014-03-26 16:47:06 -07002723 }
2724 break;
2725 }
2726 case CALLBACK_LOST: {
Lorenzo Colittidb95a602015-04-24 15:21:21 +09002727 NetworkCallback callback = getCallback(request, "LOST");
2728 if (callback != null) {
2729 callback.onLost(network);
Robert Greenwalt9258c642014-03-26 16:47:06 -07002730 }
2731 break;
2732 }
2733 case CALLBACK_UNAVAIL: {
Lorenzo Colittidb95a602015-04-24 15:21:21 +09002734 NetworkCallback callback = getCallback(request, "UNAVAIL");
2735 if (callback != null) {
2736 callback.onUnavailable();
Robert Greenwalt9258c642014-03-26 16:47:06 -07002737 }
2738 break;
2739 }
2740 case CALLBACK_CAP_CHANGED: {
Lorenzo Colittidb95a602015-04-24 15:21:21 +09002741 NetworkCallback callback = getCallback(request, "CAP_CHANGED");
2742 if (callback != null) {
Robert Greenwalta848c1c2014-09-30 16:50:07 -07002743 NetworkCapabilities cap = (NetworkCapabilities)getObject(message,
2744 NetworkCapabilities.class);
Robert Greenwalt9258c642014-03-26 16:47:06 -07002745
Lorenzo Colittidb95a602015-04-24 15:21:21 +09002746 callback.onCapabilitiesChanged(network, cap);
Robert Greenwalt9258c642014-03-26 16:47:06 -07002747 }
2748 break;
2749 }
2750 case CALLBACK_IP_CHANGED: {
Lorenzo Colittidb95a602015-04-24 15:21:21 +09002751 NetworkCallback callback = getCallback(request, "IP_CHANGED");
2752 if (callback != null) {
Robert Greenwalta848c1c2014-09-30 16:50:07 -07002753 LinkProperties lp = (LinkProperties)getObject(message,
2754 LinkProperties.class);
Robert Greenwalt9258c642014-03-26 16:47:06 -07002755
Lorenzo Colittidb95a602015-04-24 15:21:21 +09002756 callback.onLinkPropertiesChanged(network, lp);
Robert Greenwalt9258c642014-03-26 16:47:06 -07002757 }
2758 break;
2759 }
Robert Greenwalt8d482522015-06-24 13:23:42 -07002760 case CALLBACK_SUSPENDED: {
2761 NetworkCallback callback = getCallback(request, "SUSPENDED");
2762 if (callback != null) {
2763 callback.onNetworkSuspended(network);
2764 }
2765 break;
2766 }
2767 case CALLBACK_RESUMED: {
2768 NetworkCallback callback = getCallback(request, "RESUMED");
2769 if (callback != null) {
2770 callback.onNetworkResumed(network);
2771 }
2772 break;
2773 }
Robert Greenwalt9258c642014-03-26 16:47:06 -07002774 case CALLBACK_RELEASED: {
Lorenzo Colittidb95a602015-04-24 15:21:21 +09002775 NetworkCallback callback = null;
Robert Greenwalt9258c642014-03-26 16:47:06 -07002776 synchronized(mCallbackMap) {
Lorenzo Colittidb95a602015-04-24 15:21:21 +09002777 callback = mCallbackMap.remove(request);
Robert Greenwalt9258c642014-03-26 16:47:06 -07002778 }
Lorenzo Colittidb95a602015-04-24 15:21:21 +09002779 if (callback != null) {
Robert Greenwalt6078b502014-06-11 16:05:07 -07002780 synchronized(mRefCount) {
2781 if (mRefCount.decrementAndGet() == 0) {
2782 getLooper().quit();
2783 }
2784 }
Robert Greenwalt9258c642014-03-26 16:47:06 -07002785 } else {
Lorenzo Colittidb95a602015-04-24 15:21:21 +09002786 Log.e(TAG, "callback not found for RELEASED message");
Robert Greenwalt9258c642014-03-26 16:47:06 -07002787 }
Robert Greenwalt9258c642014-03-26 16:47:06 -07002788 break;
2789 }
2790 case CALLBACK_EXIT: {
Lorenzo Colittidb95a602015-04-24 15:21:21 +09002791 Log.d(TAG, "Listener quitting");
Robert Greenwalt9258c642014-03-26 16:47:06 -07002792 getLooper().quit();
2793 break;
2794 }
Robert Greenwalt562cc542014-05-15 18:07:26 -07002795 case EXPIRE_LEGACY_REQUEST: {
2796 expireRequest((NetworkCapabilities)message.obj, message.arg1);
2797 break;
2798 }
Robert Greenwalt9258c642014-03-26 16:47:06 -07002799 }
2800 }
2801
Robert Greenwalta848c1c2014-09-30 16:50:07 -07002802 private Object getObject(Message msg, Class c) {
2803 return msg.getData().getParcelable(c.getSimpleName());
Robert Greenwalt9258c642014-03-26 16:47:06 -07002804 }
Lorenzo Colittidb95a602015-04-24 15:21:21 +09002805
2806 private NetworkCallback getCallback(NetworkRequest req, String name) {
2807 NetworkCallback callback;
Robert Greenwalt9258c642014-03-26 16:47:06 -07002808 synchronized(mCallbackMap) {
Lorenzo Colittidb95a602015-04-24 15:21:21 +09002809 callback = mCallbackMap.get(req);
Robert Greenwalt9258c642014-03-26 16:47:06 -07002810 }
Lorenzo Colittidb95a602015-04-24 15:21:21 +09002811 if (callback == null) {
2812 Log.e(TAG, "callback not found for " + name + " message");
2813 }
2814 return callback;
Robert Greenwalt9258c642014-03-26 16:47:06 -07002815 }
Robert Greenwalt9258c642014-03-26 16:47:06 -07002816 }
2817
Robert Greenwalt6078b502014-06-11 16:05:07 -07002818 private void incCallbackHandlerRefCount() {
Robert Greenwalt9258c642014-03-26 16:47:06 -07002819 synchronized(sCallbackRefCount) {
2820 if (sCallbackRefCount.incrementAndGet() == 1) {
Pierre Imaib290c0a2016-02-19 16:26:01 +09002821 // TODO: switch this to ConnectivityThread
Robert Greenwalt9258c642014-03-26 16:47:06 -07002822 HandlerThread callbackThread = new HandlerThread("ConnectivityManager");
2823 callbackThread.start();
2824 sCallbackHandler = new CallbackHandler(callbackThread.getLooper(),
Robert Greenwalt6078b502014-06-11 16:05:07 -07002825 sNetworkCallback, sCallbackRefCount, this);
Robert Greenwalt9258c642014-03-26 16:47:06 -07002826 }
2827 }
2828 }
2829
Robert Greenwalt6078b502014-06-11 16:05:07 -07002830 private void decCallbackHandlerRefCount() {
Robert Greenwalt9258c642014-03-26 16:47:06 -07002831 synchronized(sCallbackRefCount) {
2832 if (sCallbackRefCount.decrementAndGet() == 0) {
2833 sCallbackHandler.obtainMessage(CALLBACK_EXIT).sendToTarget();
2834 sCallbackHandler = null;
2835 }
2836 }
2837 }
2838
Robert Greenwalt6078b502014-06-11 16:05:07 -07002839 static final HashMap<NetworkRequest, NetworkCallback> sNetworkCallback =
2840 new HashMap<NetworkRequest, NetworkCallback>();
Robert Greenwalt9258c642014-03-26 16:47:06 -07002841 static final AtomicInteger sCallbackRefCount = new AtomicInteger(0);
2842 static CallbackHandler sCallbackHandler = null;
2843
2844 private final static int LISTEN = 1;
2845 private final static int REQUEST = 2;
2846
Robert Greenwalt562cc542014-05-15 18:07:26 -07002847 private NetworkRequest sendRequestForNetwork(NetworkCapabilities need,
Robert Greenwalt6078b502014-06-11 16:05:07 -07002848 NetworkCallback networkCallback, int timeoutSec, int action,
Robert Greenwalt32aa65a2014-06-02 15:32:02 -07002849 int legacyType) {
Robert Greenwalt6078b502014-06-11 16:05:07 -07002850 if (networkCallback == null) {
2851 throw new IllegalArgumentException("null NetworkCallback");
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07002852 }
Erik Klinea2d29402016-03-16 15:31:39 +09002853 if (need == null && action != REQUEST) {
2854 throw new IllegalArgumentException("null NetworkCapabilities");
2855 }
Robert Greenwalt9258c642014-03-26 16:47:06 -07002856 try {
Robert Greenwalt6078b502014-06-11 16:05:07 -07002857 incCallbackHandlerRefCount();
Paul Jensen7221cc32014-06-27 11:05:32 -04002858 synchronized(sNetworkCallback) {
2859 if (action == LISTEN) {
2860 networkCallback.networkRequest = mService.listenForNetwork(need,
2861 new Messenger(sCallbackHandler), new Binder());
2862 } else {
2863 networkCallback.networkRequest = mService.requestNetwork(need,
2864 new Messenger(sCallbackHandler), timeoutSec, new Binder(), legacyType);
2865 }
2866 if (networkCallback.networkRequest != null) {
Robert Greenwalt6078b502014-06-11 16:05:07 -07002867 sNetworkCallback.put(networkCallback.networkRequest, networkCallback);
Robert Greenwalt9258c642014-03-26 16:47:06 -07002868 }
2869 }
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07002870 } catch (RemoteException e) {
2871 throw e.rethrowFromSystemServer();
2872 }
Robert Greenwalt6078b502014-06-11 16:05:07 -07002873 if (networkCallback.networkRequest == null) decCallbackHandlerRefCount();
2874 return networkCallback.networkRequest;
Robert Greenwalt9258c642014-03-26 16:47:06 -07002875 }
2876
2877 /**
Erik Klinea2d29402016-03-16 15:31:39 +09002878 * Helper function to request a network with a particular legacy type.
Lorenzo Colitti7de289f2015-11-25 12:00:52 +09002879 *
2880 * This is temporarily public @hide so it can be called by system code that uses the
2881 * NetworkRequest API to request networks but relies on CONNECTIVITY_ACTION broadcasts for
2882 * instead network notifications.
2883 *
2884 * TODO: update said system code to rely on NetworkCallbacks and make this method private.
2885 *
2886 * @hide
2887 */
Lorenzo Colittid1179462015-11-25 15:47:14 +09002888 public void requestNetwork(NetworkRequest request, NetworkCallback networkCallback,
Lorenzo Colitti7de289f2015-11-25 12:00:52 +09002889 int timeoutMs, int legacyType) {
Lorenzo Colittid1179462015-11-25 15:47:14 +09002890 sendRequestForNetwork(request.networkCapabilities, networkCallback, timeoutMs, REQUEST,
2891 legacyType);
Lorenzo Colitti7de289f2015-11-25 12:00:52 +09002892 }
2893
2894 /**
Lorenzo Colittie285b432015-04-23 15:32:42 +09002895 * Request a network to satisfy a set of {@link android.net.NetworkCapabilities}.
Robert Greenwalt9258c642014-03-26 16:47:06 -07002896 *
2897 * This {@link NetworkRequest} will live until released via
Lorenzo Colitti88bc0bb2016-04-13 22:00:02 +09002898 * {@link #unregisterNetworkCallback(NetworkCallback)} or the calling application exits.
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07002899 * Status of the request can be followed by listening to the various
Robert Greenwalt6078b502014-06-11 16:05:07 -07002900 * callbacks described in {@link NetworkCallback}. The {@link Network}
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07002901 * can be used to direct traffic to the network.
Paul Jensenbb2e0e92015-06-16 15:11:58 -04002902 * <p>It is presently unsupported to request a network with mutable
2903 * {@link NetworkCapabilities} such as
2904 * {@link NetworkCapabilities#NET_CAPABILITY_VALIDATED} or
2905 * {@link NetworkCapabilities#NET_CAPABILITY_CAPTIVE_PORTAL}
2906 * as these {@code NetworkCapabilities} represent states that a particular
2907 * network may never attain, and whether a network will attain these states
2908 * is unknown prior to bringing up the network so the framework does not
2909 * know how to go about satisfing a request with these capabilities.
Lorenzo Colittid5427052015-10-15 16:29:00 +09002910 *
2911 * <p>This method requires the caller to hold either the
2912 * {@link android.Manifest.permission#CHANGE_NETWORK_STATE} permission
2913 * or the ability to modify system settings as determined by
2914 * {@link android.provider.Settings.System#canWrite}.</p>
Robert Greenwalt9258c642014-03-26 16:47:06 -07002915 *
Robert Greenwalt6078b502014-06-11 16:05:07 -07002916 * @param request {@link NetworkRequest} describing this request.
2917 * @param networkCallback The {@link NetworkCallback} to be utilized for this
2918 * request. Note the callback must not be shared - they
2919 * uniquely specify this request.
Paul Jensenbb2e0e92015-06-16 15:11:58 -04002920 * @throws IllegalArgumentException if {@code request} specifies any mutable
2921 * {@code NetworkCapabilities}.
Robert Greenwalt9258c642014-03-26 16:47:06 -07002922 */
Robert Greenwalt6078b502014-06-11 16:05:07 -07002923 public void requestNetwork(NetworkRequest request, NetworkCallback networkCallback) {
Lorenzo Colittid1179462015-11-25 15:47:14 +09002924 requestNetwork(request, networkCallback, 0,
Lorenzo Colitti7de289f2015-11-25 12:00:52 +09002925 inferLegacyTypeForNetworkCapabilities(request.networkCapabilities));
Robert Greenwalt9258c642014-03-26 16:47:06 -07002926 }
2927
2928 /**
Lorenzo Colittie285b432015-04-23 15:32:42 +09002929 * Request a network to satisfy a set of {@link android.net.NetworkCapabilities}, limited
Robert Greenwalt9258c642014-03-26 16:47:06 -07002930 * by a timeout.
2931 *
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07002932 * This function behaves identically to the non-timedout version, but if a suitable
Robert Greenwalt6078b502014-06-11 16:05:07 -07002933 * network is not found within the given time (in milliseconds) the
2934 * {@link NetworkCallback#unavailable} callback is called. The request must
Lorenzo Colitti88bc0bb2016-04-13 22:00:02 +09002935 * still be released normally by calling {@link unregisterNetworkCallback(NetworkCallback)}.
Lorenzo Colittid5427052015-10-15 16:29:00 +09002936 *
2937 * <p>This method requires the caller to hold either the
2938 * {@link android.Manifest.permission#CHANGE_NETWORK_STATE} permission
2939 * or the ability to modify system settings as determined by
2940 * {@link android.provider.Settings.System#canWrite}.</p>
2941 *
Robert Greenwalt6078b502014-06-11 16:05:07 -07002942 * @param request {@link NetworkRequest} describing this request.
2943 * @param networkCallback The callbacks to be utilized for this request. Note
2944 * the callbacks must not be shared - they uniquely specify
2945 * this request.
2946 * @param timeoutMs The time in milliseconds to attempt looking for a suitable network
2947 * before {@link NetworkCallback#unavailable} is called.
Lorenzo Colitti7de289f2015-11-25 12:00:52 +09002948 *
2949 * TODO: Make timeouts work and then unhide this method.
2950 *
Robert Greenwalt9258c642014-03-26 16:47:06 -07002951 * @hide
2952 */
Robert Greenwalt6078b502014-06-11 16:05:07 -07002953 public void requestNetwork(NetworkRequest request, NetworkCallback networkCallback,
2954 int timeoutMs) {
Lorenzo Colittid1179462015-11-25 15:47:14 +09002955 requestNetwork(request, networkCallback, timeoutMs,
Lorenzo Colitti7de289f2015-11-25 12:00:52 +09002956 inferLegacyTypeForNetworkCapabilities(request.networkCapabilities));
Robert Greenwalt9258c642014-03-26 16:47:06 -07002957 }
2958
2959 /**
Robert Greenwalt6078b502014-06-11 16:05:07 -07002960 * The maximum number of milliseconds the framework will look for a suitable network
Robert Greenwalt9258c642014-03-26 16:47:06 -07002961 * during a timeout-equiped call to {@link requestNetwork}.
2962 * {@hide}
2963 */
Robert Greenwalt6078b502014-06-11 16:05:07 -07002964 public final static int MAX_NETWORK_REQUEST_TIMEOUT_MS = 100 * 60 * 1000;
Robert Greenwalt9258c642014-03-26 16:47:06 -07002965
2966 /**
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07002967 * The lookup key for a {@link Network} object included with the intent after
Jeremy Joslin46e3ac82014-11-05 10:32:09 -08002968 * successfully finding a network for the applications request. Retrieve it with
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07002969 * {@link android.content.Intent#getParcelableExtra(String)}.
Jeremy Joslinfcde58f2015-02-11 16:51:13 -08002970 * <p>
Paul Jensen72db88e2015-03-10 10:54:12 -04002971 * Note that if you intend to invoke {@link Network#openConnection(java.net.URL)}
2972 * then you must get a ConnectivityManager instance before doing so.
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07002973 */
Erik Kline90e93072014-11-19 12:12:24 +09002974 public static final String EXTRA_NETWORK = "android.net.extra.NETWORK";
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07002975
2976 /**
Robert Greenwalt6078b502014-06-11 16:05:07 -07002977 * The lookup key for a {@link NetworkRequest} object included with the intent after
Jeremy Joslin46e3ac82014-11-05 10:32:09 -08002978 * successfully finding a network for the applications request. Retrieve it with
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07002979 * {@link android.content.Intent#getParcelableExtra(String)}.
2980 */
Erik Kline90e93072014-11-19 12:12:24 +09002981 public static final String EXTRA_NETWORK_REQUEST = "android.net.extra.NETWORK_REQUEST";
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07002982
2983
2984 /**
Lorenzo Colittie285b432015-04-23 15:32:42 +09002985 * Request a network to satisfy a set of {@link android.net.NetworkCapabilities}.
Robert Greenwalt9258c642014-03-26 16:47:06 -07002986 *
Jeremy Joslin46e3ac82014-11-05 10:32:09 -08002987 * This function behaves identically to the version that takes a NetworkCallback, but instead
Robert Greenwalt6078b502014-06-11 16:05:07 -07002988 * of {@link NetworkCallback} a {@link PendingIntent} is used. This means
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07002989 * the request may outlive the calling application and get called back when a suitable
2990 * network is found.
Robert Greenwalt9258c642014-03-26 16:47:06 -07002991 * <p>
2992 * The operation is an Intent broadcast that goes to a broadcast receiver that
2993 * you registered with {@link Context#registerReceiver} or through the
2994 * &lt;receiver&gt; tag in an AndroidManifest.xml file
2995 * <p>
2996 * The operation Intent is delivered with two extras, a {@link Network} typed
Erik Kline90e93072014-11-19 12:12:24 +09002997 * extra called {@link #EXTRA_NETWORK} and a {@link NetworkRequest}
2998 * typed extra called {@link #EXTRA_NETWORK_REQUEST} containing
Robert Greenwalt9258c642014-03-26 16:47:06 -07002999 * the original requests parameters. It is important to create a new,
Robert Greenwalt6078b502014-06-11 16:05:07 -07003000 * {@link NetworkCallback} based request before completing the processing of the
Robert Greenwalt9258c642014-03-26 16:47:06 -07003001 * Intent to reserve the network or it will be released shortly after the Intent
3002 * is processed.
3003 * <p>
Paul Jensen694f2b82015-06-17 14:15:39 -04003004 * If there is already a request for this Intent registered (with the equality of
Robert Greenwalt9258c642014-03-26 16:47:06 -07003005 * two Intents defined by {@link Intent#filterEquals}), then it will be removed and
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07003006 * replaced by this one, effectively releasing the previous {@link NetworkRequest}.
Robert Greenwalt9258c642014-03-26 16:47:06 -07003007 * <p>
Jeremy Joslin46e3ac82014-11-05 10:32:09 -08003008 * The request may be released normally by calling
3009 * {@link #releaseNetworkRequest(android.app.PendingIntent)}.
Paul Jensenbb2e0e92015-06-16 15:11:58 -04003010 * <p>It is presently unsupported to request a network with either
3011 * {@link NetworkCapabilities#NET_CAPABILITY_VALIDATED} or
3012 * {@link NetworkCapabilities#NET_CAPABILITY_CAPTIVE_PORTAL}
3013 * as these {@code NetworkCapabilities} represent states that a particular
3014 * network may never attain, and whether a network will attain these states
3015 * is unknown prior to bringing up the network so the framework does not
3016 * know how to go about satisfing a request with these capabilities.
Lorenzo Colittid5427052015-10-15 16:29:00 +09003017 *
3018 * <p>This method requires the caller to hold either the
3019 * {@link android.Manifest.permission#CHANGE_NETWORK_STATE} permission
3020 * or the ability to modify system settings as determined by
3021 * {@link android.provider.Settings.System#canWrite}.</p>
3022 *
Robert Greenwalt6078b502014-06-11 16:05:07 -07003023 * @param request {@link NetworkRequest} describing this request.
Robert Greenwalt9258c642014-03-26 16:47:06 -07003024 * @param operation Action to perform when the network is available (corresponds
Robert Greenwalt6078b502014-06-11 16:05:07 -07003025 * to the {@link NetworkCallback#onAvailable} call. Typically
Jeremy Joslin46e3ac82014-11-05 10:32:09 -08003026 * comes from {@link PendingIntent#getBroadcast}. Cannot be null.
Paul Jensenbb2e0e92015-06-16 15:11:58 -04003027 * @throws IllegalArgumentException if {@code request} contains either
3028 * {@link NetworkCapabilities#NET_CAPABILITY_VALIDATED} or
3029 * {@link NetworkCapabilities#NET_CAPABILITY_CAPTIVE_PORTAL}.
Robert Greenwalt9258c642014-03-26 16:47:06 -07003030 */
Robert Greenwalt6078b502014-06-11 16:05:07 -07003031 public void requestNetwork(NetworkRequest request, PendingIntent operation) {
Jeremy Joslin46e3ac82014-11-05 10:32:09 -08003032 checkPendingIntent(operation);
Robert Greenwalt9258c642014-03-26 16:47:06 -07003033 try {
Robert Greenwalt6078b502014-06-11 16:05:07 -07003034 mService.pendingRequestForNetwork(request.networkCapabilities, operation);
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07003035 } catch (RemoteException e) {
3036 throw e.rethrowFromSystemServer();
3037 }
Robert Greenwalt9258c642014-03-26 16:47:06 -07003038 }
3039
3040 /**
Jeremy Joslin46e3ac82014-11-05 10:32:09 -08003041 * Removes a request made via {@link #requestNetwork(NetworkRequest, android.app.PendingIntent)}
3042 * <p>
Lorenzo Colitti88bc0bb2016-04-13 22:00:02 +09003043 * This method has the same behavior as
3044 * {@link #unregisterNetworkCallback(android.app.PendingIntent)} with respect to
Jeremy Joslin46e3ac82014-11-05 10:32:09 -08003045 * releasing network resources and disconnecting.
3046 *
3047 * @param operation A PendingIntent equal (as defined by {@link Intent#filterEquals}) to the
3048 * PendingIntent passed to
3049 * {@link #requestNetwork(NetworkRequest, android.app.PendingIntent)} with the
3050 * corresponding NetworkRequest you'd like to remove. Cannot be null.
3051 */
3052 public void releaseNetworkRequest(PendingIntent operation) {
3053 checkPendingIntent(operation);
3054 try {
3055 mService.releasePendingNetworkRequest(operation);
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07003056 } catch (RemoteException e) {
3057 throw e.rethrowFromSystemServer();
3058 }
Jeremy Joslin46e3ac82014-11-05 10:32:09 -08003059 }
3060
3061 private void checkPendingIntent(PendingIntent intent) {
3062 if (intent == null) {
3063 throw new IllegalArgumentException("PendingIntent cannot be null.");
3064 }
3065 }
3066
3067 /**
Robert Greenwalt9258c642014-03-26 16:47:06 -07003068 * Registers to receive notifications about all networks which satisfy the given
Robert Greenwalt6078b502014-06-11 16:05:07 -07003069 * {@link NetworkRequest}. The callbacks will continue to be called until
Lorenzo Colitti88bc0bb2016-04-13 22:00:02 +09003070 * either the application exits or link #unregisterNetworkCallback(NetworkCallback)} is called.
Paul Jensenb2748922015-05-06 11:10:18 -04003071 * <p>This method requires the caller to hold the permission
3072 * {@link android.Manifest.permission#ACCESS_NETWORK_STATE}.
Robert Greenwalt9258c642014-03-26 16:47:06 -07003073 *
Robert Greenwalt6078b502014-06-11 16:05:07 -07003074 * @param request {@link NetworkRequest} describing this request.
3075 * @param networkCallback The {@link NetworkCallback} that the system will call as suitable
3076 * networks change state.
Robert Greenwalt9258c642014-03-26 16:47:06 -07003077 */
Robert Greenwalt6078b502014-06-11 16:05:07 -07003078 public void registerNetworkCallback(NetworkRequest request, NetworkCallback networkCallback) {
3079 sendRequestForNetwork(request.networkCapabilities, networkCallback, 0, LISTEN, TYPE_NONE);
Robert Greenwalt9258c642014-03-26 16:47:06 -07003080 }
3081
3082 /**
Paul Jensen694f2b82015-06-17 14:15:39 -04003083 * Registers a PendingIntent to be sent when a network is available which satisfies the given
3084 * {@link NetworkRequest}.
3085 *
3086 * This function behaves identically to the version that takes a NetworkCallback, but instead
3087 * of {@link NetworkCallback} a {@link PendingIntent} is used. This means
3088 * the request may outlive the calling application and get called back when a suitable
3089 * network is found.
3090 * <p>
3091 * The operation is an Intent broadcast that goes to a broadcast receiver that
3092 * you registered with {@link Context#registerReceiver} or through the
3093 * &lt;receiver&gt; tag in an AndroidManifest.xml file
3094 * <p>
3095 * The operation Intent is delivered with two extras, a {@link Network} typed
3096 * extra called {@link #EXTRA_NETWORK} and a {@link NetworkRequest}
3097 * typed extra called {@link #EXTRA_NETWORK_REQUEST} containing
3098 * the original requests parameters.
3099 * <p>
3100 * If there is already a request for this Intent registered (with the equality of
3101 * two Intents defined by {@link Intent#filterEquals}), then it will be removed and
3102 * replaced by this one, effectively releasing the previous {@link NetworkRequest}.
3103 * <p>
3104 * The request may be released normally by calling
Paul Jensenf2c1cfe2015-06-30 14:29:18 -04003105 * {@link #unregisterNetworkCallback(android.app.PendingIntent)}.
Paul Jensen694f2b82015-06-17 14:15:39 -04003106 * <p>This method requires the caller to hold the permission
3107 * {@link android.Manifest.permission#ACCESS_NETWORK_STATE}.
3108 * @param request {@link NetworkRequest} describing this request.
3109 * @param operation Action to perform when the network is available (corresponds
3110 * to the {@link NetworkCallback#onAvailable} call. Typically
3111 * comes from {@link PendingIntent#getBroadcast}. Cannot be null.
3112 */
3113 public void registerNetworkCallback(NetworkRequest request, PendingIntent operation) {
3114 checkPendingIntent(operation);
3115 try {
3116 mService.pendingListenForNetwork(request.networkCapabilities, operation);
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07003117 } catch (RemoteException e) {
3118 throw e.rethrowFromSystemServer();
3119 }
Paul Jensen694f2b82015-06-17 14:15:39 -04003120 }
3121
3122 /**
Lorenzo Colitti88bc0bb2016-04-13 22:00:02 +09003123 * Registers to receive notifications about changes in the system default network. The callbacks
3124 * will continue to be called until either the application exits or
3125 * {@link #unregisterNetworkCallback(NetworkCallback)} is called.
Erik Klinea2d29402016-03-16 15:31:39 +09003126 * <p>This method requires the caller to hold the permission
3127 * {@link android.Manifest.permission#ACCESS_NETWORK_STATE}.
3128 *
3129 * @param networkCallback The {@link NetworkCallback} that the system will call as the
3130 * system default network changes.
Erik Klinea2d29402016-03-16 15:31:39 +09003131 */
3132 public void registerDefaultNetworkCallback(NetworkCallback networkCallback) {
3133 // This works because if the NetworkCapabilities are null,
3134 // ConnectivityService takes them from the default request.
3135 //
3136 // Since the capabilities are exactly the same as the default request's
3137 // capabilities, this request is guaranteed, at all times, to be
3138 // satisfied by the same network, if any, that satisfies the default
3139 // request, i.e., the system default network.
3140 sendRequestForNetwork(null, networkCallback, 0, REQUEST, TYPE_NONE);
3141 }
3142
3143 /**
fengludb571472015-04-21 17:12:05 -07003144 * Requests bandwidth update for a given {@link Network} and returns whether the update request
3145 * is accepted by ConnectivityService. Once accepted, ConnectivityService will poll underlying
3146 * network connection for updated bandwidth information. The caller will be notified via
3147 * {@link ConnectivityManager.NetworkCallback} if there is an update. Notice that this
Lorenzo Colitti88bc0bb2016-04-13 22:00:02 +09003148 * method assumes that the caller has previously called
3149 * {@link #registerNetworkCallback(NetworkRequest, NetworkCallback)} to listen for network
3150 * changes.
fenglub15e72b2015-03-20 11:29:56 -07003151 *
fengluae519192015-04-27 14:28:04 -07003152 * @param network {@link Network} specifying which network you're interested.
fengludb571472015-04-21 17:12:05 -07003153 * @return {@code true} on success, {@code false} if the {@link Network} is no longer valid.
fenglub15e72b2015-03-20 11:29:56 -07003154 */
fengludb571472015-04-21 17:12:05 -07003155 public boolean requestBandwidthUpdate(Network network) {
fenglub15e72b2015-03-20 11:29:56 -07003156 try {
fengludb571472015-04-21 17:12:05 -07003157 return mService.requestBandwidthUpdate(network);
fenglub15e72b2015-03-20 11:29:56 -07003158 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07003159 throw e.rethrowFromSystemServer();
fenglub15e72b2015-03-20 11:29:56 -07003160 }
3161 }
3162
3163 /**
Robert Greenwalt6078b502014-06-11 16:05:07 -07003164 * Unregisters callbacks about and possibly releases networks originating from
Lorenzo Colitti88bc0bb2016-04-13 22:00:02 +09003165 * {@link #requestNetwork(NetworkRequest, NetworkCallback)} and
3166 * {@link #registerNetworkCallback(NetworkRequest, NetworkCallback)} calls.
3167 * If the given {@code NetworkCallback} had previously been used with
Lorenzo Colitti2ea89e52015-04-24 17:03:31 +09003168 * {@code #requestNetwork}, any networks that had been connected to only to satisfy that request
3169 * will be disconnected.
Robert Greenwalt9258c642014-03-26 16:47:06 -07003170 *
Robert Greenwalt6078b502014-06-11 16:05:07 -07003171 * @param networkCallback The {@link NetworkCallback} used when making the request.
Robert Greenwalt9258c642014-03-26 16:47:06 -07003172 */
Robert Greenwalt6078b502014-06-11 16:05:07 -07003173 public void unregisterNetworkCallback(NetworkCallback networkCallback) {
3174 if (networkCallback == null || networkCallback.networkRequest == null ||
3175 networkCallback.networkRequest.requestId == REQUEST_ID_UNSET) {
3176 throw new IllegalArgumentException("Invalid NetworkCallback");
3177 }
Robert Greenwalt9258c642014-03-26 16:47:06 -07003178 try {
Hugo Benichi39e10e82016-07-07 09:36:12 +09003179 // CallbackHandler will release callback when receiving CALLBACK_RELEASED.
Robert Greenwalt6078b502014-06-11 16:05:07 -07003180 mService.releaseNetworkRequest(networkCallback.networkRequest);
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07003181 } catch (RemoteException e) {
3182 throw e.rethrowFromSystemServer();
3183 }
Robert Greenwalt9258c642014-03-26 16:47:06 -07003184 }
Paul Jensen6d3ff9e2014-05-29 10:12:39 -04003185
3186 /**
Paul Jensenf2c1cfe2015-06-30 14:29:18 -04003187 * Unregisters a callback previously registered via
3188 * {@link #registerNetworkCallback(NetworkRequest, android.app.PendingIntent)}.
3189 *
3190 * @param operation A PendingIntent equal (as defined by {@link Intent#filterEquals}) to the
3191 * PendingIntent passed to
3192 * {@link #registerNetworkCallback(NetworkRequest, android.app.PendingIntent)}.
3193 * Cannot be null.
3194 */
3195 public void unregisterNetworkCallback(PendingIntent operation) {
3196 releaseNetworkRequest(operation);
3197 }
3198
3199 /**
Lorenzo Colittie03c3c72015-04-03 16:38:52 +09003200 * Informs the system whether it should switch to {@code network} regardless of whether it is
3201 * validated or not. If {@code accept} is true, and the network was explicitly selected by the
3202 * user (e.g., by selecting a Wi-Fi network in the Settings app), then the network will become
3203 * the system default network regardless of any other network that's currently connected. If
3204 * {@code always} is true, then the choice is remembered, so that the next time the user
3205 * connects to this network, the system will switch to it.
3206 *
3207 * <p>This method requires the caller to hold the permission
3208 * {@link android.Manifest.permission#CONNECTIVITY_INTERNAL}
3209 *
3210 * @param network The network to accept.
3211 * @param accept Whether to accept the network even if unvalidated.
3212 * @param always Whether to remember this choice in the future.
3213 *
3214 * @hide
3215 */
3216 public void setAcceptUnvalidated(Network network, boolean accept, boolean always) {
3217 try {
3218 mService.setAcceptUnvalidated(network, accept, always);
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07003219 } catch (RemoteException e) {
3220 throw e.rethrowFromSystemServer();
3221 }
Lorenzo Colittie03c3c72015-04-03 16:38:52 +09003222 }
3223
3224 /**
Lorenzo Colitti165c51c2016-09-19 01:00:19 +09003225 * Informs the system to penalize {@code network}'s score when it becomes unvalidated. This is
3226 * only meaningful if the system is configured not to penalize such networks, e.g., if the
3227 * {@code config_networkAvoidBadWifi} configuration variable is set to 0 and the {@code
3228 * NETWORK_AVOID_BAD_WIFI setting is unset}.
3229 *
3230 * <p>This method requires the caller to hold the permission
3231 * {@link android.Manifest.permission#CONNECTIVITY_INTERNAL}
3232 *
3233 * @param network The network to accept.
3234 *
3235 * @hide
3236 */
3237 public void setAvoidUnvalidated(Network network) {
3238 try {
3239 mService.setAvoidUnvalidated(network);
3240 } catch (RemoteException e) {
3241 throw e.rethrowFromSystemServer();
3242 }
3243 }
3244
3245 /**
Stuart Scott984dc852015-03-30 13:17:11 -07003246 * Resets all connectivity manager settings back to factory defaults.
3247 * @hide
3248 */
3249 public void factoryReset() {
Stuart Scott984dc852015-03-30 13:17:11 -07003250 try {
Stuart Scottf1fb3972015-04-02 18:00:02 -07003251 mService.factoryReset();
Stuart Scott984dc852015-03-30 13:17:11 -07003252 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07003253 throw e.rethrowFromSystemServer();
Stuart Scott984dc852015-03-30 13:17:11 -07003254 }
3255 }
3256
3257 /**
Paul Jensen6d3ff9e2014-05-29 10:12:39 -04003258 * Binds the current process to {@code network}. All Sockets created in the future
3259 * (and not explicitly bound via a bound SocketFactory from
3260 * {@link Network#getSocketFactory() Network.getSocketFactory()}) will be bound to
3261 * {@code network}. All host name resolutions will be limited to {@code network} as well.
3262 * Note that if {@code network} ever disconnects, all Sockets created in this way will cease to
3263 * work and all host name resolutions will fail. This is by design so an application doesn't
3264 * accidentally use Sockets it thinks are still bound to a particular {@link Network}.
3265 * To clear binding pass {@code null} for {@code network}. Using individually bound
3266 * Sockets created by Network.getSocketFactory().createSocket() and
3267 * performing network-specific host name resolutions via
3268 * {@link Network#getAllByName Network.getAllByName} is preferred to calling
Paul Jensen72db88e2015-03-10 10:54:12 -04003269 * {@code bindProcessToNetwork}.
Paul Jensen6d3ff9e2014-05-29 10:12:39 -04003270 *
3271 * @param network The {@link Network} to bind the current process to, or {@code null} to clear
3272 * the current binding.
3273 * @return {@code true} on success, {@code false} if the {@link Network} is no longer valid.
3274 */
Paul Jensen72db88e2015-03-10 10:54:12 -04003275 public boolean bindProcessToNetwork(Network network) {
3276 // Forcing callers to call thru non-static function ensures ConnectivityManager
3277 // instantiated.
3278 return setProcessDefaultNetwork(network);
3279 }
3280
3281 /**
3282 * Binds the current process to {@code network}. All Sockets created in the future
3283 * (and not explicitly bound via a bound SocketFactory from
3284 * {@link Network#getSocketFactory() Network.getSocketFactory()}) will be bound to
3285 * {@code network}. All host name resolutions will be limited to {@code network} as well.
3286 * Note that if {@code network} ever disconnects, all Sockets created in this way will cease to
3287 * work and all host name resolutions will fail. This is by design so an application doesn't
3288 * accidentally use Sockets it thinks are still bound to a particular {@link Network}.
3289 * To clear binding pass {@code null} for {@code network}. Using individually bound
3290 * Sockets created by Network.getSocketFactory().createSocket() and
3291 * performing network-specific host name resolutions via
3292 * {@link Network#getAllByName Network.getAllByName} is preferred to calling
3293 * {@code setProcessDefaultNetwork}.
3294 *
3295 * @param network The {@link Network} to bind the current process to, or {@code null} to clear
3296 * the current binding.
3297 * @return {@code true} on success, {@code false} if the {@link Network} is no longer valid.
3298 * @deprecated This function can throw {@link IllegalStateException}. Use
3299 * {@link #bindProcessToNetwork} instead. {@code bindProcessToNetwork}
3300 * is a direct replacement.
3301 */
Paul Jensen6d3ff9e2014-05-29 10:12:39 -04003302 public static boolean setProcessDefaultNetwork(Network network) {
Paul Jensenc91b5342014-08-27 12:38:45 -04003303 int netId = (network == null) ? NETID_UNSET : network.netId;
Paul Jensen72db88e2015-03-10 10:54:12 -04003304 if (netId == NetworkUtils.getBoundNetworkForProcess()) {
Paul Jensenc91b5342014-08-27 12:38:45 -04003305 return true;
3306 }
3307 if (NetworkUtils.bindProcessToNetwork(netId)) {
Paul Jensene0bef712014-12-10 15:12:18 -05003308 // Set HTTP proxy system properties to match network.
3309 // TODO: Deprecate this static method and replace it with a non-static version.
Lorenzo Colittiec4c5552015-04-22 11:52:48 +09003310 try {
3311 Proxy.setHttpProxySystemProperty(getInstance().getDefaultProxy());
3312 } catch (SecurityException e) {
3313 // The process doesn't have ACCESS_NETWORK_STATE, so we can't fetch the proxy.
3314 Log.e(TAG, "Can't set proxy properties", e);
3315 }
Paul Jensenc91b5342014-08-27 12:38:45 -04003316 // Must flush DNS cache as new network may have different DNS resolutions.
3317 InetAddress.clearDnsCache();
3318 // Must flush socket pool as idle sockets will be bound to previous network and may
3319 // cause subsequent fetches to be performed on old network.
3320 NetworkEventDispatcher.getInstance().onNetworkConfigurationChanged();
3321 return true;
3322 } else {
3323 return false;
3324 }
Paul Jensen6d3ff9e2014-05-29 10:12:39 -04003325 }
3326
3327 /**
3328 * Returns the {@link Network} currently bound to this process via
Paul Jensen72db88e2015-03-10 10:54:12 -04003329 * {@link #bindProcessToNetwork}, or {@code null} if no {@link Network} is explicitly bound.
Paul Jensen6d3ff9e2014-05-29 10:12:39 -04003330 *
3331 * @return {@code Network} to which this process is bound, or {@code null}.
3332 */
Paul Jensen72db88e2015-03-10 10:54:12 -04003333 public Network getBoundNetworkForProcess() {
3334 // Forcing callers to call thru non-static function ensures ConnectivityManager
3335 // instantiated.
3336 return getProcessDefaultNetwork();
3337 }
3338
3339 /**
3340 * Returns the {@link Network} currently bound to this process via
3341 * {@link #bindProcessToNetwork}, or {@code null} if no {@link Network} is explicitly bound.
3342 *
3343 * @return {@code Network} to which this process is bound, or {@code null}.
3344 * @deprecated Using this function can lead to other functions throwing
3345 * {@link IllegalStateException}. Use {@link #getBoundNetworkForProcess} instead.
3346 * {@code getBoundNetworkForProcess} is a direct replacement.
3347 */
Paul Jensen6d3ff9e2014-05-29 10:12:39 -04003348 public static Network getProcessDefaultNetwork() {
Paul Jensen72db88e2015-03-10 10:54:12 -04003349 int netId = NetworkUtils.getBoundNetworkForProcess();
Paul Jensenbcc76d32014-07-11 08:17:29 -04003350 if (netId == NETID_UNSET) return null;
Paul Jensen6d3ff9e2014-05-29 10:12:39 -04003351 return new Network(netId);
3352 }
3353
Lorenzo Colittiffc42b02015-07-29 11:41:21 +09003354 private void unsupportedStartingFrom(int version) {
3355 if (Process.myUid() == Process.SYSTEM_UID) {
3356 // The getApplicationInfo() call we make below is not supported in system context, and
3357 // we want to allow the system to use these APIs anyway.
3358 return;
3359 }
3360
3361 if (mContext.getApplicationInfo().targetSdkVersion >= version) {
3362 throw new UnsupportedOperationException(
3363 "This method is not supported in target SDK version " + version + " and above");
3364 }
3365 }
3366
3367 // Checks whether the calling app can use the legacy routing API (startUsingNetworkFeature,
3368 // stopUsingNetworkFeature, requestRouteToHost), and if not throw UnsupportedOperationException.
Lifu Tang30f95a72016-01-07 23:20:38 -08003369 // TODO: convert the existing system users (Tethering, GnssLocationProvider) to the new APIs and
Lorenzo Colittiffc42b02015-07-29 11:41:21 +09003370 // remove these exemptions. Note that this check is not secure, and apps can still access these
3371 // functions by accessing ConnectivityService directly. However, it should be clear that doing
3372 // so is unsupported and may break in the future. http://b/22728205
3373 private void checkLegacyRoutingApiAccess() {
3374 if (mContext.checkCallingOrSelfPermission("com.android.permission.INJECT_OMADM_SETTINGS")
3375 == PackageManager.PERMISSION_GRANTED) {
3376 return;
3377 }
3378
Dianne Hackborn692a2442015-07-31 10:35:34 -07003379 unsupportedStartingFrom(VERSION_CODES.M);
Lorenzo Colittiffc42b02015-07-29 11:41:21 +09003380 }
3381
Paul Jensen6d3ff9e2014-05-29 10:12:39 -04003382 /**
3383 * Binds host resolutions performed by this process to {@code network}.
Paul Jensen72db88e2015-03-10 10:54:12 -04003384 * {@link #bindProcessToNetwork} takes precedence over this setting.
Paul Jensen6d3ff9e2014-05-29 10:12:39 -04003385 *
3386 * @param network The {@link Network} to bind host resolutions from the current process to, or
3387 * {@code null} to clear the current binding.
3388 * @return {@code true} on success, {@code false} if the {@link Network} is no longer valid.
3389 * @hide
3390 * @deprecated This is strictly for legacy usage to support {@link #startUsingNetworkFeature}.
3391 */
3392 public static boolean setProcessDefaultNetworkForHostResolution(Network network) {
Paul Jensenbcc76d32014-07-11 08:17:29 -04003393 return NetworkUtils.bindProcessToNetworkForHostResolution(
3394 network == null ? NETID_UNSET : network.netId);
Paul Jensen6d3ff9e2014-05-29 10:12:39 -04003395 }
Felipe Leme1b103232016-01-22 09:44:57 -08003396
3397 /**
3398 * Device is not restricting metered network activity while application is running on
3399 * background.
3400 */
3401 public static final int RESTRICT_BACKGROUND_STATUS_DISABLED = 1;
3402
3403 /**
3404 * Device is restricting metered network activity while application is running on background,
3405 * but application is allowed to bypass it.
3406 * <p>
3407 * In this state, application should take action to mitigate metered network access.
3408 * For example, a music streaming application should switch to a low-bandwidth bitrate.
3409 */
3410 public static final int RESTRICT_BACKGROUND_STATUS_WHITELISTED = 2;
3411
3412 /**
3413 * Device is restricting metered network activity while application is running on background.
Felipe Leme9778f762016-01-27 14:46:39 -08003414 * <p>
Felipe Leme1b103232016-01-22 09:44:57 -08003415 * In this state, application should not try to use the network while running on background,
3416 * because it would be denied.
3417 */
3418 public static final int RESTRICT_BACKGROUND_STATUS_ENABLED = 3;
3419
Felipe Leme9778f762016-01-27 14:46:39 -08003420 /**
3421 * A change in the background metered network activity restriction has occurred.
3422 * <p>
3423 * Applications should call {@link #getRestrictBackgroundStatus()} to check if the restriction
3424 * applies to them.
3425 * <p>
3426 * This is only sent to registered receivers, not manifest receivers.
3427 */
3428 @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION)
3429 public static final String ACTION_RESTRICT_BACKGROUND_CHANGED =
3430 "android.net.conn.RESTRICT_BACKGROUND_CHANGED";
3431
Felipe Lemeecfccea2016-01-25 11:48:04 -08003432 /** @hide */
3433 @Retention(RetentionPolicy.SOURCE)
Felipe Leme1b103232016-01-22 09:44:57 -08003434 @IntDef(flag = false, value = {
3435 RESTRICT_BACKGROUND_STATUS_DISABLED,
3436 RESTRICT_BACKGROUND_STATUS_WHITELISTED,
3437 RESTRICT_BACKGROUND_STATUS_ENABLED,
3438 })
Felipe Leme1b103232016-01-22 09:44:57 -08003439 public @interface RestrictBackgroundStatus {
3440 }
3441
3442 private INetworkPolicyManager getNetworkPolicyManager() {
3443 synchronized (this) {
3444 if (mNPManager != null) {
3445 return mNPManager;
3446 }
3447 mNPManager = INetworkPolicyManager.Stub.asInterface(ServiceManager
3448 .getService(Context.NETWORK_POLICY_SERVICE));
3449 return mNPManager;
3450 }
3451 }
3452
3453 /**
3454 * Determines if the calling application is subject to metered network restrictions while
3455 * running on background.
Felipe Lemec9c7be52016-05-16 13:57:19 -07003456 *
3457 * @return {@link #RESTRICT_BACKGROUND_STATUS_DISABLED},
3458 * {@link #RESTRICT_BACKGROUND_STATUS_ENABLED},
3459 * or {@link #RESTRICT_BACKGROUND_STATUS_WHITELISTED}
Felipe Leme1b103232016-01-22 09:44:57 -08003460 */
3461 public @RestrictBackgroundStatus int getRestrictBackgroundStatus() {
3462 try {
3463 return getNetworkPolicyManager().getRestrictBackgroundByCaller();
3464 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07003465 throw e.rethrowFromSystemServer();
Felipe Leme1b103232016-01-22 09:44:57 -08003466 }
3467 }
Andreas Gampe34802132016-04-20 14:33:51 -07003468
3469 /**
3470 * A holder class for debug info (mapping CALLBACK values to field names). This is stored
3471 * in a holder for two reasons:
3472 * 1) The reflection necessary to establish the map can't be run at compile-time. Thus, this
3473 * code will make the enclosing class not compile-time initializeable, deferring its
3474 * initialization to zygote startup. This leads to dirty (but shared) memory.
3475 * As this is debug info, use a holder that isn't initialized by default. This way the map
3476 * will be created on demand, while ConnectivityManager can be compile-time initialized.
3477 * 2) Static initialization is still preferred for its strong thread safety guarantees without
3478 * requiring a lock.
3479 */
3480 private static class NoPreloadHolder {
3481 public static final SparseArray<String> sMagicDecoderRing = MessageUtils.findMessageNames(
3482 new Class[]{ConnectivityManager.class}, new String[]{"CALLBACK_"});
3483 }
3484
3485 static {
3486 // When debug is enabled, aggressively initialize the holder by touching the field (which
3487 // will guarantee static initialization).
3488 if (CallbackHandler.DBG) {
3489 Object dummy = NoPreloadHolder.sMagicDecoderRing;
3490 }
3491 }
3492
3493 private static final String whatToString(int what) {
3494 return NoPreloadHolder.sMagicDecoderRing.get(what, Integer.toString(what));
3495 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003496}