blob: dc37a149a884766f275ef9ccaf0ffb98d32d63a1 [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) {
Jeremy Klein27089aad2016-03-12 16:29:54 -08002096 checkNotNull(callback, "OnStartTetheringCallback cannot be null.");
2097
Jeremy Klein36c7aa02016-01-22 14:11:45 -08002098 ResultReceiver wrappedCallback = new ResultReceiver(handler) {
2099 @Override
2100 protected void onReceiveResult(int resultCode, Bundle resultData) {
2101 if (resultCode == TETHER_ERROR_NO_ERROR) {
2102 callback.onTetheringStarted();
2103 } else {
2104 callback.onTetheringFailed();
2105 }
2106 }
2107 };
Jeremy Klein27089aad2016-03-12 16:29:54 -08002108
Jeremy Klein36c7aa02016-01-22 14:11:45 -08002109 try {
2110 mService.startTethering(type, wrappedCallback, showProvisioningUi);
2111 } catch (RemoteException e) {
2112 Log.e(TAG, "Exception trying to start tethering.", e);
2113 wrappedCallback.send(TETHER_ERROR_SERVICE_UNAVAIL, null);
2114 }
2115 }
2116
2117 /**
2118 * Stops tethering for the given type. Also cancels any provisioning rechecks for that type if
2119 * applicable.
2120 *
2121 * @param type The type of tethering to stop. Must be one of
2122 * {@link ConnectivityManager.TETHERING_WIFI},
2123 * {@link ConnectivityManager.TETHERING_USB}, or
2124 * {@link ConnectivityManager.TETHERING_BLUETOOTH}.
2125 * @hide
2126 */
2127 @SystemApi
2128 public void stopTethering(int type) {
2129 try {
2130 mService.stopTethering(type);
2131 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07002132 throw e.rethrowFromSystemServer();
Jeremy Klein36c7aa02016-01-22 14:11:45 -08002133 }
2134 }
2135
2136 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08002137 * Get the list of regular expressions that define any tetherable
2138 * USB network interfaces. If USB tethering is not supported by the
2139 * device, this list should be empty.
Paul Jensenb2748922015-05-06 11:10:18 -04002140 * <p>This method requires the caller to hold the permission
2141 * {@link android.Manifest.permission#ACCESS_NETWORK_STATE}.
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08002142 *
2143 * @return an array of 0 or more regular expression Strings defining
2144 * what interfaces are considered tetherable usb interfaces.
2145 *
Robert Greenwalt2a091d72010-02-11 18:18:40 -08002146 * {@hide}
2147 */
2148 public String[] getTetherableUsbRegexs() {
2149 try {
2150 return mService.getTetherableUsbRegexs();
2151 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07002152 throw e.rethrowFromSystemServer();
Robert Greenwalt2a091d72010-02-11 18:18:40 -08002153 }
2154 }
2155
2156 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08002157 * Get the list of regular expressions that define any tetherable
2158 * Wifi network interfaces. If Wifi tethering is not supported by the
2159 * device, this list should be empty.
Paul Jensenb2748922015-05-06 11:10:18 -04002160 * <p>This method requires the caller to hold the permission
2161 * {@link android.Manifest.permission#ACCESS_NETWORK_STATE}.
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08002162 *
2163 * @return an array of 0 or more regular expression Strings defining
2164 * what interfaces are considered tetherable wifi interfaces.
2165 *
Robert Greenwalt2a091d72010-02-11 18:18:40 -08002166 * {@hide}
2167 */
2168 public String[] getTetherableWifiRegexs() {
2169 try {
2170 return mService.getTetherableWifiRegexs();
2171 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07002172 throw e.rethrowFromSystemServer();
Robert Greenwalt2a091d72010-02-11 18:18:40 -08002173 }
2174 }
Robert Greenwalt5a735062010-03-02 17:25:02 -08002175
Danica Chang6fdd0c62010-08-11 14:54:43 -07002176 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08002177 * Get the list of regular expressions that define any tetherable
2178 * Bluetooth network interfaces. If Bluetooth tethering is not supported by the
2179 * device, this list should be empty.
Paul Jensenb2748922015-05-06 11:10:18 -04002180 * <p>This method requires the caller to hold the permission
2181 * {@link android.Manifest.permission#ACCESS_NETWORK_STATE}.
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08002182 *
2183 * @return an array of 0 or more regular expression Strings defining
2184 * what interfaces are considered tetherable bluetooth interfaces.
2185 *
Danica Chang6fdd0c62010-08-11 14:54:43 -07002186 * {@hide}
2187 */
2188 public String[] getTetherableBluetoothRegexs() {
2189 try {
2190 return mService.getTetherableBluetoothRegexs();
2191 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07002192 throw e.rethrowFromSystemServer();
Danica Chang6fdd0c62010-08-11 14:54:43 -07002193 }
2194 }
2195
Mike Lockwood6c2260b2011-07-19 13:04:47 -07002196 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08002197 * Attempt to both alter the mode of USB and Tethering of USB. A
2198 * utility method to deal with some of the complexity of USB - will
2199 * attempt to switch to Rndis and subsequently tether the resulting
2200 * interface on {@code true} or turn off tethering and switch off
2201 * Rndis on {@code false}.
Lorenzo Colittid5427052015-10-15 16:29:00 +09002202 *
2203 * <p>This method requires the caller to hold either the
2204 * {@link android.Manifest.permission#CHANGE_NETWORK_STATE} permission
2205 * or the ability to modify system settings as determined by
2206 * {@link android.provider.Settings.System#canWrite}.</p>
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08002207 *
2208 * @param enable a boolean - {@code true} to enable tethering
2209 * @return error a {@code TETHER_ERROR} value indicating success or failure type
2210 *
Mike Lockwood6c2260b2011-07-19 13:04:47 -07002211 * {@hide}
2212 */
2213 public int setUsbTethering(boolean enable) {
2214 try {
2215 return mService.setUsbTethering(enable);
2216 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07002217 throw e.rethrowFromSystemServer();
Mike Lockwood6c2260b2011-07-19 13:04:47 -07002218 }
2219 }
2220
Robert Greenwalt5a735062010-03-02 17:25:02 -08002221 /** {@hide} */
2222 public static final int TETHER_ERROR_NO_ERROR = 0;
2223 /** {@hide} */
2224 public static final int TETHER_ERROR_UNKNOWN_IFACE = 1;
2225 /** {@hide} */
2226 public static final int TETHER_ERROR_SERVICE_UNAVAIL = 2;
2227 /** {@hide} */
2228 public static final int TETHER_ERROR_UNSUPPORTED = 3;
2229 /** {@hide} */
2230 public static final int TETHER_ERROR_UNAVAIL_IFACE = 4;
2231 /** {@hide} */
2232 public static final int TETHER_ERROR_MASTER_ERROR = 5;
2233 /** {@hide} */
2234 public static final int TETHER_ERROR_TETHER_IFACE_ERROR = 6;
2235 /** {@hide} */
2236 public static final int TETHER_ERROR_UNTETHER_IFACE_ERROR = 7;
2237 /** {@hide} */
2238 public static final int TETHER_ERROR_ENABLE_NAT_ERROR = 8;
2239 /** {@hide} */
2240 public static final int TETHER_ERROR_DISABLE_NAT_ERROR = 9;
2241 /** {@hide} */
2242 public static final int TETHER_ERROR_IFACE_CFG_ERROR = 10;
Jeremy Klein36c7aa02016-01-22 14:11:45 -08002243 /** {@hide} */
2244 public static final int TETHER_ERROR_PROVISION_FAILED = 11;
Robert Greenwalt5a735062010-03-02 17:25:02 -08002245
2246 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08002247 * Get a more detailed error code after a Tethering or Untethering
2248 * request asynchronously failed.
Paul Jensenb2748922015-05-06 11:10:18 -04002249 * <p>This method requires the caller to hold the permission
2250 * {@link android.Manifest.permission#ACCESS_NETWORK_STATE}.
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08002251 *
2252 * @param iface The name of the interface of interest
Robert Greenwalt5a735062010-03-02 17:25:02 -08002253 * @return error The error code of the last error tethering or untethering the named
2254 * interface
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08002255 *
Robert Greenwalt5a735062010-03-02 17:25:02 -08002256 * {@hide}
2257 */
2258 public int getLastTetherError(String iface) {
2259 try {
2260 return mService.getLastTetherError(iface);
2261 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07002262 throw e.rethrowFromSystemServer();
Robert Greenwalt5a735062010-03-02 17:25:02 -08002263 }
Robert Greenwalt14f2ef42010-06-15 12:19:37 -07002264 }
2265
2266 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08002267 * Report network connectivity status. This is currently used only
2268 * to alter status bar UI.
Paul Jensenb2748922015-05-06 11:10:18 -04002269 * <p>This method requires the caller to hold the permission
2270 * {@link android.Manifest.permission#STATUS_BAR}.
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08002271 *
Robert Greenwaltd7085fc2010-09-08 15:24:47 -07002272 * @param networkType The type of network you want to report on
2273 * @param percentage The quality of the connection 0 is bad, 100 is good
2274 * {@hide}
2275 */
2276 public void reportInetCondition(int networkType, int percentage) {
2277 try {
2278 mService.reportInetCondition(networkType, percentage);
2279 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07002280 throw e.rethrowFromSystemServer();
Robert Greenwaltd7085fc2010-09-08 15:24:47 -07002281 }
2282 }
Robert Greenwalt434203a2010-10-11 16:00:27 -07002283
2284 /**
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07002285 * Report a problem network to the framework. This provides a hint to the system
Ye Wenb87875e2014-07-21 14:19:01 -07002286 * that there might be connectivity problems on this network and may cause
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07002287 * the framework to re-evaluate network connectivity and/or switch to another
2288 * network.
Robert Greenwalt9258c642014-03-26 16:47:06 -07002289 *
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07002290 * @param network The {@link Network} the application was attempting to use
2291 * or {@code null} to indicate the current default network.
Paul Jensenbfd17b72015-04-07 12:43:13 -04002292 * @deprecated Use {@link #reportNetworkConnectivity} which allows reporting both
2293 * working and non-working connectivity.
Robert Greenwalt9258c642014-03-26 16:47:06 -07002294 */
2295 public void reportBadNetwork(Network network) {
2296 try {
Paul Jensenbfd17b72015-04-07 12:43:13 -04002297 // One of these will be ignored because it matches system's current state.
2298 // The other will trigger the necessary reevaluation.
2299 mService.reportNetworkConnectivity(network, true);
2300 mService.reportNetworkConnectivity(network, false);
2301 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07002302 throw e.rethrowFromSystemServer();
Paul Jensenbfd17b72015-04-07 12:43:13 -04002303 }
2304 }
2305
2306 /**
2307 * Report to the framework whether a network has working connectivity.
2308 * This provides a hint to the system that a particular network is providing
2309 * working connectivity or not. In response the framework may re-evaluate
2310 * the network's connectivity and might take further action thereafter.
2311 *
2312 * @param network The {@link Network} the application was attempting to use
2313 * or {@code null} to indicate the current default network.
2314 * @param hasConnectivity {@code true} if the application was able to successfully access the
2315 * Internet using {@code network} or {@code false} if not.
2316 */
2317 public void reportNetworkConnectivity(Network network, boolean hasConnectivity) {
2318 try {
2319 mService.reportNetworkConnectivity(network, hasConnectivity);
Robert Greenwalt9258c642014-03-26 16:47:06 -07002320 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07002321 throw e.rethrowFromSystemServer();
Robert Greenwalt9258c642014-03-26 16:47:06 -07002322 }
2323 }
2324
2325 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08002326 * Set a network-independent global http proxy. This is not normally what you want
2327 * for typical HTTP proxies - they are general network dependent. However if you're
2328 * doing something unusual like general internal filtering this may be useful. On
2329 * a private network where the proxy is not accessible, you may break HTTP using this.
Paul Jensen0d719ca2015-02-13 14:18:39 -05002330 * <p>This method requires the caller to hold the permission
Jason Monkfaf3fd52014-05-07 18:41:13 -04002331 * android.Manifest.permission#CONNECTIVITY_INTERNAL.
Paul Jensenb2748922015-05-06 11:10:18 -04002332 *
2333 * @param p A {@link ProxyInfo} object defining the new global
2334 * HTTP proxy. A {@code null} value will clear the global HTTP proxy.
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07002335 * @hide
Robert Greenwalt434203a2010-10-11 16:00:27 -07002336 */
Jason Monk207900c2014-04-25 15:00:09 -04002337 public void setGlobalProxy(ProxyInfo p) {
Robert Greenwalt434203a2010-10-11 16:00:27 -07002338 try {
2339 mService.setGlobalProxy(p);
2340 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07002341 throw e.rethrowFromSystemServer();
Robert Greenwalt434203a2010-10-11 16:00:27 -07002342 }
2343 }
2344
2345 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08002346 * Retrieve any network-independent global HTTP proxy.
2347 *
Jason Monk207900c2014-04-25 15:00:09 -04002348 * @return {@link ProxyInfo} for the current global HTTP proxy or {@code null}
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08002349 * if no global HTTP proxy is set.
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07002350 * @hide
Robert Greenwalt434203a2010-10-11 16:00:27 -07002351 */
Jason Monk207900c2014-04-25 15:00:09 -04002352 public ProxyInfo getGlobalProxy() {
Robert Greenwalt434203a2010-10-11 16:00:27 -07002353 try {
2354 return mService.getGlobalProxy();
2355 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07002356 throw e.rethrowFromSystemServer();
Robert Greenwalt434203a2010-10-11 16:00:27 -07002357 }
2358 }
2359
2360 /**
Paul Jensencee9b512015-05-06 07:32:40 -04002361 * Retrieve the global HTTP proxy, or if no global HTTP proxy is set, a
2362 * network-specific HTTP proxy. If {@code network} is null, the
2363 * network-specific proxy returned is the proxy of the default active
2364 * network.
2365 *
2366 * @return {@link ProxyInfo} for the current global HTTP proxy, or if no
2367 * global HTTP proxy is set, {@code ProxyInfo} for {@code network},
2368 * or when {@code network} is {@code null},
2369 * the {@code ProxyInfo} for the default active network. Returns
2370 * {@code null} when no proxy applies or the caller doesn't have
2371 * permission to use {@code network}.
2372 * @hide
2373 */
2374 public ProxyInfo getProxyForNetwork(Network network) {
2375 try {
2376 return mService.getProxyForNetwork(network);
2377 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07002378 throw e.rethrowFromSystemServer();
Paul Jensencee9b512015-05-06 07:32:40 -04002379 }
2380 }
2381
2382 /**
Paul Jensene0bef712014-12-10 15:12:18 -05002383 * Get the current default HTTP proxy settings. If a global proxy is set it will be returned,
2384 * otherwise if this process is bound to a {@link Network} using
Paul Jensen72db88e2015-03-10 10:54:12 -04002385 * {@link #bindProcessToNetwork} then that {@code Network}'s proxy is returned, otherwise
Paul Jensene0bef712014-12-10 15:12:18 -05002386 * the default network's proxy is returned.
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08002387 *
Jason Monk207900c2014-04-25 15:00:09 -04002388 * @return the {@link ProxyInfo} for the current HTTP proxy, or {@code null} if no
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08002389 * HTTP proxy is active.
Robert Greenwalt434203a2010-10-11 16:00:27 -07002390 */
Paul Jensene0bef712014-12-10 15:12:18 -05002391 public ProxyInfo getDefaultProxy() {
Paul Jensencee9b512015-05-06 07:32:40 -04002392 return getProxyForNetwork(getBoundNetworkForProcess());
Robert Greenwalt434203a2010-10-11 16:00:27 -07002393 }
Robert Greenwaltd55a6b42011-03-25 13:09:25 -07002394
2395 /**
Robert Greenwalt9b2886e2011-08-31 11:46:42 -07002396 * Returns true if the hardware supports the given network type
2397 * else it returns false. This doesn't indicate we have coverage
2398 * or are authorized onto a network, just whether or not the
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08002399 * hardware supports it. For example a GSM phone without a SIM
2400 * should still return {@code true} for mobile data, but a wifi only
2401 * tablet would return {@code false}.
Paul Jensenb2748922015-05-06 11:10:18 -04002402 * <p>This method requires the caller to hold the permission
2403 * {@link android.Manifest.permission#ACCESS_NETWORK_STATE}.
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08002404 *
2405 * @param networkType The network type we'd like to check
2406 * @return {@code true} if supported, else {@code false}
2407 *
Robert Greenwalt9b2886e2011-08-31 11:46:42 -07002408 * @hide
2409 */
2410 public boolean isNetworkSupported(int networkType) {
2411 try {
2412 return mService.isNetworkSupported(networkType);
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07002413 } catch (RemoteException e) {
2414 throw e.rethrowFromSystemServer();
2415 }
Robert Greenwalt9b2886e2011-08-31 11:46:42 -07002416 }
Jeff Sharkey9f7cbf02012-04-12 18:34:54 -07002417
2418 /**
2419 * Returns if the currently active data network is metered. A network is
2420 * classified as metered when the user is sensitive to heavy data usage on
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08002421 * that connection due to monetary costs, data limitations or
2422 * battery/performance issues. You should check this before doing large
2423 * data transfers, and warn the user or delay the operation until another
2424 * network is available.
Paul Jensenb2748922015-05-06 11:10:18 -04002425 * <p>This method requires the caller to hold the permission
2426 * {@link android.Manifest.permission#ACCESS_NETWORK_STATE}.
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08002427 *
2428 * @return {@code true} if large transfers should be avoided, otherwise
2429 * {@code false}.
Jeff Sharkey9f7cbf02012-04-12 18:34:54 -07002430 */
2431 public boolean isActiveNetworkMetered() {
2432 try {
2433 return mService.isActiveNetworkMetered();
2434 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07002435 throw e.rethrowFromSystemServer();
Jeff Sharkey9f7cbf02012-04-12 18:34:54 -07002436 }
2437 }
Jeff Sharkey69ddab42012-08-25 00:05:46 -07002438
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08002439 /**
2440 * If the LockdownVpn mechanism is enabled, updates the vpn
2441 * with a reload of its profile.
2442 *
2443 * @return a boolean with {@code} indicating success
2444 *
2445 * <p>This method can only be called by the system UID
2446 * {@hide}
2447 */
Jeff Sharkey69ddab42012-08-25 00:05:46 -07002448 public boolean updateLockdownVpn() {
2449 try {
2450 return mService.updateLockdownVpn();
2451 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07002452 throw e.rethrowFromSystemServer();
Jeff Sharkey69ddab42012-08-25 00:05:46 -07002453 }
2454 }
Irfan Sheriffda6da092012-08-16 12:49:23 -07002455
2456 /**
Wink Saville948282b2013-08-29 08:55:16 -07002457 * Check mobile provisioning.
Wink Savilleab9321d2013-06-29 21:10:57 -07002458 *
Wink Savilleab9321d2013-06-29 21:10:57 -07002459 * @param suggestedTimeOutMs, timeout in milliseconds
Wink Savilleab9321d2013-06-29 21:10:57 -07002460 *
2461 * @return time out that will be used, maybe less that suggestedTimeOutMs
2462 * -1 if an error.
2463 *
2464 * {@hide}
2465 */
Wink Saville948282b2013-08-29 08:55:16 -07002466 public int checkMobileProvisioning(int suggestedTimeOutMs) {
Wink Savilleab9321d2013-06-29 21:10:57 -07002467 int timeOutMs = -1;
2468 try {
Wink Saville948282b2013-08-29 08:55:16 -07002469 timeOutMs = mService.checkMobileProvisioning(suggestedTimeOutMs);
Wink Savilleab9321d2013-06-29 21:10:57 -07002470 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07002471 throw e.rethrowFromSystemServer();
Wink Savilleab9321d2013-06-29 21:10:57 -07002472 }
2473 return timeOutMs;
2474 }
Robert Greenwalte182bfe2013-07-16 12:06:09 -07002475
2476 /**
Wink Saville42d4f082013-07-20 20:31:59 -07002477 * Get the mobile provisioning url.
Robert Greenwalte182bfe2013-07-16 12:06:09 -07002478 * {@hide}
2479 */
2480 public String getMobileProvisioningUrl() {
2481 try {
2482 return mService.getMobileProvisioningUrl();
2483 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07002484 throw e.rethrowFromSystemServer();
Robert Greenwalte182bfe2013-07-16 12:06:09 -07002485 }
Robert Greenwalte182bfe2013-07-16 12:06:09 -07002486 }
Wink Saville42d4f082013-07-20 20:31:59 -07002487
2488 /**
Wink Saville948282b2013-08-29 08:55:16 -07002489 * Set sign in error notification to visible or in visible
2490 *
2491 * @param visible
2492 * @param networkType
2493 *
2494 * {@hide}
Paul Jensen3541e9f2015-03-18 12:23:02 -04002495 * @deprecated Doesn't properly deal with multiple connected networks of the same type.
Wink Saville948282b2013-08-29 08:55:16 -07002496 */
2497 public void setProvisioningNotificationVisible(boolean visible, int networkType,
Paul Jensen89e0f092014-09-15 15:59:36 -04002498 String action) {
Wink Saville948282b2013-08-29 08:55:16 -07002499 try {
Paul Jensen89e0f092014-09-15 15:59:36 -04002500 mService.setProvisioningNotificationVisible(visible, networkType, action);
Wink Saville948282b2013-08-29 08:55:16 -07002501 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07002502 throw e.rethrowFromSystemServer();
Wink Saville948282b2013-08-29 08:55:16 -07002503 }
2504 }
Yuhao Zheng5cd1a0e2013-09-09 17:00:04 -07002505
2506 /**
2507 * Set the value for enabling/disabling airplane mode
Paul Jensenb2748922015-05-06 11:10:18 -04002508 * <p>This method requires the caller to hold the permission
2509 * {@link android.Manifest.permission#CONNECTIVITY_INTERNAL}.
Yuhao Zheng5cd1a0e2013-09-09 17:00:04 -07002510 *
2511 * @param enable whether to enable airplane mode or not
2512 *
Yuhao Zheng5cd1a0e2013-09-09 17:00:04 -07002513 * @hide
2514 */
2515 public void setAirplaneMode(boolean enable) {
2516 try {
2517 mService.setAirplaneMode(enable);
2518 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07002519 throw e.rethrowFromSystemServer();
Yuhao Zheng5cd1a0e2013-09-09 17:00:04 -07002520 }
2521 }
Robert Greenwalte049c232014-04-11 15:53:27 -07002522
2523 /** {@hide} */
Robert Greenwalta67be032014-05-16 15:49:14 -07002524 public void registerNetworkFactory(Messenger messenger, String name) {
Robert Greenwalte049c232014-04-11 15:53:27 -07002525 try {
Robert Greenwalta67be032014-05-16 15:49:14 -07002526 mService.registerNetworkFactory(messenger, name);
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07002527 } catch (RemoteException e) {
2528 throw e.rethrowFromSystemServer();
2529 }
Robert Greenwalta67be032014-05-16 15:49:14 -07002530 }
2531
2532 /** {@hide} */
2533 public void unregisterNetworkFactory(Messenger messenger) {
2534 try {
2535 mService.unregisterNetworkFactory(messenger);
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07002536 } catch (RemoteException e) {
2537 throw e.rethrowFromSystemServer();
2538 }
Robert Greenwalte049c232014-04-11 15:53:27 -07002539 }
Robert Greenwalt7b816022014-04-18 15:25:25 -07002540
Paul Jensen31a94f42015-02-13 14:18:39 -05002541 /**
2542 * @hide
2543 * Register a NetworkAgent with ConnectivityService.
2544 * @return NetID corresponding to NetworkAgent.
2545 */
2546 public int registerNetworkAgent(Messenger messenger, NetworkInfo ni, LinkProperties lp,
Sreeram Ramachandran8cd33ed2014-07-23 15:23:15 -07002547 NetworkCapabilities nc, int score, NetworkMisc misc) {
Robert Greenwalt7b816022014-04-18 15:25:25 -07002548 try {
Paul Jensen31a94f42015-02-13 14:18:39 -05002549 return mService.registerNetworkAgent(messenger, ni, lp, nc, score, misc);
2550 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07002551 throw e.rethrowFromSystemServer();
Paul Jensen31a94f42015-02-13 14:18:39 -05002552 }
Robert Greenwalt7b816022014-04-18 15:25:25 -07002553 }
2554
Robert Greenwalt9258c642014-03-26 16:47:06 -07002555 /**
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07002556 * Base class for NetworkRequest callbacks. Used for notifications about network
2557 * changes. Should be extended by applications wanting notifications.
Robert Greenwalt9258c642014-03-26 16:47:06 -07002558 */
Robert Greenwalt6078b502014-06-11 16:05:07 -07002559 public static class NetworkCallback {
Robert Greenwalt7b816022014-04-18 15:25:25 -07002560 /**
Lorenzo Colitti07086932015-04-24 12:23:24 +09002561 * Called when the framework connects to a new network to evaluate whether it satisfies this
2562 * request. If evaluation succeeds, this callback may be followed by an {@link #onAvailable}
2563 * callback. There is no guarantee that this new network will satisfy any requests, or that
2564 * the network will stay connected for longer than the time necessary to evaluate it.
2565 * <p>
2566 * Most applications <b>should not</b> act on this callback, and should instead use
2567 * {@link #onAvailable}. This callback is intended for use by applications that can assist
2568 * the framework in properly evaluating the network &mdash; for example, an application that
2569 * can automatically log in to a captive portal without user intervention.
2570 *
2571 * @param network The {@link Network} of the network that is being evaluated.
Lorenzo Colitti66276122015-06-11 14:27:17 +09002572 *
2573 * @hide
Robert Greenwalt7b816022014-04-18 15:25:25 -07002574 */
Robert Greenwalt6078b502014-06-11 16:05:07 -07002575 public void onPreCheck(Network network) {}
Robert Greenwalt7b816022014-04-18 15:25:25 -07002576
2577 /**
Lorenzo Colitti07086932015-04-24 12:23:24 +09002578 * Called when the framework connects and has declared a new network ready for use.
Robert Greenwalt6078b502014-06-11 16:05:07 -07002579 * This callback may be called more than once if the {@link Network} that is
2580 * satisfying the request changes.
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07002581 *
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07002582 * @param network The {@link Network} of the satisfying network.
Robert Greenwalt7b816022014-04-18 15:25:25 -07002583 */
Robert Greenwalt6078b502014-06-11 16:05:07 -07002584 public void onAvailable(Network network) {}
Robert Greenwalt7b816022014-04-18 15:25:25 -07002585
2586 /**
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07002587 * Called when the network is about to be disconnected. Often paired with an
Robert Greenwalt6078b502014-06-11 16:05:07 -07002588 * {@link NetworkCallback#onAvailable} call with the new replacement network
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07002589 * for graceful handover. This may not be called if we have a hard loss
2590 * (loss without warning). This may be followed by either a
Robert Greenwalt6078b502014-06-11 16:05:07 -07002591 * {@link NetworkCallback#onLost} call or a
2592 * {@link NetworkCallback#onAvailable} call for this network depending
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07002593 * on whether we lose or regain it.
2594 *
Robert Greenwalt6078b502014-06-11 16:05:07 -07002595 * @param network The {@link Network} that is about to be disconnected.
2596 * @param maxMsToLive The time in ms the framework will attempt to keep the
2597 * network connected. Note that the network may suffer a
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07002598 * hard loss at any time.
Robert Greenwalt7b816022014-04-18 15:25:25 -07002599 */
Robert Greenwalt6078b502014-06-11 16:05:07 -07002600 public void onLosing(Network network, int maxMsToLive) {}
Robert Greenwalt7b816022014-04-18 15:25:25 -07002601
2602 /**
2603 * Called when the framework has a hard loss of the network or when the
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07002604 * graceful failure ends.
2605 *
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07002606 * @param network The {@link Network} lost.
Robert Greenwalt7b816022014-04-18 15:25:25 -07002607 */
Robert Greenwalt6078b502014-06-11 16:05:07 -07002608 public void onLost(Network network) {}
Robert Greenwalt7b816022014-04-18 15:25:25 -07002609
2610 /**
2611 * Called if no network is found in the given timeout time. If no timeout is given,
Erik Klinee9e251f2015-11-25 12:49:38 +09002612 * this will not be called. The associated {@link NetworkRequest} will have already
2613 * been removed and released, as if {@link #unregisterNetworkCallback} had been called.
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07002614 * @hide
Robert Greenwalt7b816022014-04-18 15:25:25 -07002615 */
Robert Greenwalt6078b502014-06-11 16:05:07 -07002616 public void onUnavailable() {}
Robert Greenwalt7b816022014-04-18 15:25:25 -07002617
2618 /**
2619 * Called when the network the framework connected to for this request
2620 * changes capabilities but still satisfies the stated need.
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07002621 *
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07002622 * @param network The {@link Network} whose capabilities have changed.
Lorenzo Colittie285b432015-04-23 15:32:42 +09002623 * @param networkCapabilities The new {@link android.net.NetworkCapabilities} for this network.
Robert Greenwalt7b816022014-04-18 15:25:25 -07002624 */
Robert Greenwalt6078b502014-06-11 16:05:07 -07002625 public void onCapabilitiesChanged(Network network,
Robert Greenwalt7b816022014-04-18 15:25:25 -07002626 NetworkCapabilities networkCapabilities) {}
2627
2628 /**
2629 * Called when the network the framework connected to for this request
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07002630 * changes {@link LinkProperties}.
2631 *
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07002632 * @param network The {@link Network} whose link properties have changed.
2633 * @param linkProperties The new {@link LinkProperties} for this network.
Robert Greenwalt7b816022014-04-18 15:25:25 -07002634 */
Robert Greenwalt6078b502014-06-11 16:05:07 -07002635 public void onLinkPropertiesChanged(Network network, LinkProperties linkProperties) {}
Robert Greenwalt7b816022014-04-18 15:25:25 -07002636
Robert Greenwalt8d482522015-06-24 13:23:42 -07002637 /**
2638 * Called when the network the framework connected to for this request
2639 * goes into {@link NetworkInfo.DetailedState.SUSPENDED}.
2640 * This generally means that while the TCP connections are still live,
2641 * temporarily network data fails to transfer. Specifically this is used
2642 * on cellular networks to mask temporary outages when driving through
2643 * a tunnel, etc.
2644 * @hide
2645 */
2646 public void onNetworkSuspended(Network network) {}
2647
2648 /**
2649 * Called when the network the framework connected to for this request
2650 * returns from a {@link NetworkInfo.DetailedState.SUSPENDED} state.
2651 * This should always be preceeded by a matching {@code onNetworkSuspended}
2652 * call.
2653 * @hide
2654 */
2655 public void onNetworkResumed(Network network) {}
2656
Robert Greenwalt6078b502014-06-11 16:05:07 -07002657 private NetworkRequest networkRequest;
Robert Greenwalt7b816022014-04-18 15:25:25 -07002658 }
2659
Robert Greenwalt9258c642014-03-26 16:47:06 -07002660 private static final int BASE = Protocol.BASE_CONNECTIVITY_MANAGER;
Robert Greenwalt9258c642014-03-26 16:47:06 -07002661 /** @hide */
Robert Greenwalt8d482522015-06-24 13:23:42 -07002662 public static final int CALLBACK_PRECHECK = BASE + 1;
2663 /** @hide */
2664 public static final int CALLBACK_AVAILABLE = BASE + 2;
2665 /** @hide arg1 = TTL */
2666 public static final int CALLBACK_LOSING = BASE + 3;
2667 /** @hide */
2668 public static final int CALLBACK_LOST = BASE + 4;
2669 /** @hide */
2670 public static final int CALLBACK_UNAVAIL = BASE + 5;
2671 /** @hide */
2672 public static final int CALLBACK_CAP_CHANGED = BASE + 6;
2673 /** @hide */
2674 public static final int CALLBACK_IP_CHANGED = BASE + 7;
2675 /** @hide */
2676 public static final int CALLBACK_RELEASED = BASE + 8;
Hugo Benichie804d372016-07-07 10:15:56 +09002677 // TODO: consider deleting CALLBACK_EXIT and shifting following enum codes down by 1.
Robert Greenwalt8d482522015-06-24 13:23:42 -07002678 /** @hide */
2679 public static final int CALLBACK_EXIT = BASE + 9;
Robert Greenwalt562cc542014-05-15 18:07:26 -07002680 /** @hide obj = NetworkCapabilities, arg1 = seq number */
Robert Greenwalt8d482522015-06-24 13:23:42 -07002681 private static final int EXPIRE_LEGACY_REQUEST = BASE + 10;
2682 /** @hide */
2683 public static final int CALLBACK_SUSPENDED = BASE + 11;
2684 /** @hide */
2685 public static final int CALLBACK_RESUMED = BASE + 12;
Robert Greenwalt9258c642014-03-26 16:47:06 -07002686
Erik Klinee9e251f2015-11-25 12:49:38 +09002687 /** @hide */
2688 public static String getCallbackName(int whichCallback) {
2689 switch (whichCallback) {
2690 case CALLBACK_PRECHECK: return "CALLBACK_PRECHECK";
2691 case CALLBACK_AVAILABLE: return "CALLBACK_AVAILABLE";
2692 case CALLBACK_LOSING: return "CALLBACK_LOSING";
2693 case CALLBACK_LOST: return "CALLBACK_LOST";
2694 case CALLBACK_UNAVAIL: return "CALLBACK_UNAVAIL";
2695 case CALLBACK_CAP_CHANGED: return "CALLBACK_CAP_CHANGED";
2696 case CALLBACK_IP_CHANGED: return "CALLBACK_IP_CHANGED";
2697 case CALLBACK_RELEASED: return "CALLBACK_RELEASED";
2698 case CALLBACK_EXIT: return "CALLBACK_EXIT";
2699 case EXPIRE_LEGACY_REQUEST: return "EXPIRE_LEGACY_REQUEST";
2700 case CALLBACK_SUSPENDED: return "CALLBACK_SUSPENDED";
2701 case CALLBACK_RESUMED: return "CALLBACK_RESUMED";
2702 default:
2703 return Integer.toString(whichCallback);
2704 }
2705 }
2706
Robert Greenwalt562cc542014-05-15 18:07:26 -07002707 private class CallbackHandler extends Handler {
Robert Greenwalt9258c642014-03-26 16:47:06 -07002708 private static final String TAG = "ConnectivityManager.CallbackHandler";
Robert Greenwalta9ebeef2015-09-03 16:41:45 -07002709 private static final boolean DBG = false;
Robert Greenwalt9258c642014-03-26 16:47:06 -07002710
Hugo Benichi791ee322016-07-06 22:53:17 +09002711 CallbackHandler(Looper looper) {
Robert Greenwalt9258c642014-03-26 16:47:06 -07002712 super(looper);
Robert Greenwalt9258c642014-03-26 16:47:06 -07002713 }
2714
2715 @Override
2716 public void handleMessage(Message message) {
Hugo Benichi791ee322016-07-06 22:53:17 +09002717 NetworkRequest request = getObject(message, NetworkRequest.class);
2718 Network network = getObject(message, Network.class);
Lorenzo Colittifcfa7d92016-03-01 22:56:37 +09002719 if (DBG) {
2720 Log.d(TAG, whatToString(message.what) + " for network " + network);
2721 }
Robert Greenwalt9258c642014-03-26 16:47:06 -07002722 switch (message.what) {
2723 case CALLBACK_PRECHECK: {
Lorenzo Colittidb95a602015-04-24 15:21:21 +09002724 NetworkCallback callback = getCallback(request, "PRECHECK");
2725 if (callback != null) {
2726 callback.onPreCheck(network);
Robert Greenwalt9258c642014-03-26 16:47:06 -07002727 }
2728 break;
2729 }
2730 case CALLBACK_AVAILABLE: {
Lorenzo Colittidb95a602015-04-24 15:21:21 +09002731 NetworkCallback callback = getCallback(request, "AVAILABLE");
2732 if (callback != null) {
2733 callback.onAvailable(network);
Robert Greenwalt9258c642014-03-26 16:47:06 -07002734 }
2735 break;
2736 }
2737 case CALLBACK_LOSING: {
Lorenzo Colittidb95a602015-04-24 15:21:21 +09002738 NetworkCallback callback = getCallback(request, "LOSING");
2739 if (callback != null) {
2740 callback.onLosing(network, message.arg1);
Robert Greenwalt9258c642014-03-26 16:47:06 -07002741 }
2742 break;
2743 }
2744 case CALLBACK_LOST: {
Lorenzo Colittidb95a602015-04-24 15:21:21 +09002745 NetworkCallback callback = getCallback(request, "LOST");
2746 if (callback != null) {
2747 callback.onLost(network);
Robert Greenwalt9258c642014-03-26 16:47:06 -07002748 }
2749 break;
2750 }
2751 case CALLBACK_UNAVAIL: {
Lorenzo Colittidb95a602015-04-24 15:21:21 +09002752 NetworkCallback callback = getCallback(request, "UNAVAIL");
2753 if (callback != null) {
2754 callback.onUnavailable();
Robert Greenwalt9258c642014-03-26 16:47:06 -07002755 }
2756 break;
2757 }
2758 case CALLBACK_CAP_CHANGED: {
Lorenzo Colittidb95a602015-04-24 15:21:21 +09002759 NetworkCallback callback = getCallback(request, "CAP_CHANGED");
2760 if (callback != null) {
Hugo Benichi791ee322016-07-06 22:53:17 +09002761 NetworkCapabilities cap = getObject(message, NetworkCapabilities.class);
Lorenzo Colittidb95a602015-04-24 15:21:21 +09002762 callback.onCapabilitiesChanged(network, cap);
Robert Greenwalt9258c642014-03-26 16:47:06 -07002763 }
2764 break;
2765 }
2766 case CALLBACK_IP_CHANGED: {
Lorenzo Colittidb95a602015-04-24 15:21:21 +09002767 NetworkCallback callback = getCallback(request, "IP_CHANGED");
2768 if (callback != null) {
Hugo Benichi791ee322016-07-06 22:53:17 +09002769 LinkProperties lp = getObject(message, LinkProperties.class);
Lorenzo Colittidb95a602015-04-24 15:21:21 +09002770 callback.onLinkPropertiesChanged(network, lp);
Robert Greenwalt9258c642014-03-26 16:47:06 -07002771 }
2772 break;
2773 }
Robert Greenwalt8d482522015-06-24 13:23:42 -07002774 case CALLBACK_SUSPENDED: {
2775 NetworkCallback callback = getCallback(request, "SUSPENDED");
2776 if (callback != null) {
2777 callback.onNetworkSuspended(network);
2778 }
2779 break;
2780 }
2781 case CALLBACK_RESUMED: {
2782 NetworkCallback callback = getCallback(request, "RESUMED");
2783 if (callback != null) {
2784 callback.onNetworkResumed(network);
2785 }
2786 break;
2787 }
Robert Greenwalt9258c642014-03-26 16:47:06 -07002788 case CALLBACK_RELEASED: {
Hugo Benichie804d372016-07-07 10:15:56 +09002789 final NetworkCallback callback;
Hugo Benichi791ee322016-07-06 22:53:17 +09002790 synchronized(sCallbacks) {
2791 callback = sCallbacks.remove(request);
Robert Greenwalt9258c642014-03-26 16:47:06 -07002792 }
Hugo Benichie804d372016-07-07 10:15:56 +09002793 if (callback == null) {
Lorenzo Colittidb95a602015-04-24 15:21:21 +09002794 Log.e(TAG, "callback not found for RELEASED message");
Robert Greenwalt9258c642014-03-26 16:47:06 -07002795 }
Robert Greenwalt9258c642014-03-26 16:47:06 -07002796 break;
2797 }
2798 case CALLBACK_EXIT: {
Robert Greenwalt9258c642014-03-26 16:47:06 -07002799 break;
2800 }
Robert Greenwalt562cc542014-05-15 18:07:26 -07002801 case EXPIRE_LEGACY_REQUEST: {
2802 expireRequest((NetworkCapabilities)message.obj, message.arg1);
2803 break;
2804 }
Robert Greenwalt9258c642014-03-26 16:47:06 -07002805 }
2806 }
2807
Hugo Benichi791ee322016-07-06 22:53:17 +09002808 private <T> T getObject(Message msg, Class<T> c) {
2809 return (T) msg.getData().getParcelable(c.getSimpleName());
Robert Greenwalt9258c642014-03-26 16:47:06 -07002810 }
Lorenzo Colittidb95a602015-04-24 15:21:21 +09002811
2812 private NetworkCallback getCallback(NetworkRequest req, String name) {
2813 NetworkCallback callback;
Hugo Benichi791ee322016-07-06 22:53:17 +09002814 synchronized(sCallbacks) {
2815 callback = sCallbacks.get(req);
Robert Greenwalt9258c642014-03-26 16:47:06 -07002816 }
Lorenzo Colittidb95a602015-04-24 15:21:21 +09002817 if (callback == null) {
2818 Log.e(TAG, "callback not found for " + name + " message");
2819 }
2820 return callback;
Robert Greenwalt9258c642014-03-26 16:47:06 -07002821 }
Robert Greenwalt9258c642014-03-26 16:47:06 -07002822 }
2823
Hugo Benichie804d372016-07-07 10:15:56 +09002824 private CallbackHandler getHandler() {
2825 synchronized (sCallbacks) {
2826 if (sCallbackHandler == null) {
2827 sCallbackHandler = new CallbackHandler(ConnectivityThread.getInstanceLooper());
Robert Greenwalt9258c642014-03-26 16:47:06 -07002828 }
Hugo Benichie804d372016-07-07 10:15:56 +09002829 return sCallbackHandler;
Robert Greenwalt9258c642014-03-26 16:47:06 -07002830 }
2831 }
2832
Hugo Benichi791ee322016-07-06 22:53:17 +09002833 static final HashMap<NetworkRequest, NetworkCallback> sCallbacks = new HashMap<>();
Hugo Benichie804d372016-07-07 10:15:56 +09002834 static CallbackHandler sCallbackHandler;
Robert Greenwalt9258c642014-03-26 16:47:06 -07002835
2836 private final static int LISTEN = 1;
2837 private final static int REQUEST = 2;
2838
Robert Greenwalt562cc542014-05-15 18:07:26 -07002839 private NetworkRequest sendRequestForNetwork(NetworkCapabilities need,
Hugo Benichie804d372016-07-07 10:15:56 +09002840 NetworkCallback callback, int timeoutMs, int action, int legacyType) {
2841 return sendRequestForNetwork(need, callback, getHandler(), timeoutMs, action, legacyType);
2842 }
2843
2844 private NetworkRequest sendRequestForNetwork(NetworkCapabilities need,
2845 NetworkCallback callback, Handler handler, int timeoutMs, int action, int legacyType) {
2846 if (callback == null) {
Robert Greenwalt6078b502014-06-11 16:05:07 -07002847 throw new IllegalArgumentException("null NetworkCallback");
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07002848 }
Erik Klinea2d29402016-03-16 15:31:39 +09002849 if (need == null && action != REQUEST) {
2850 throw new IllegalArgumentException("null NetworkCapabilities");
2851 }
Hugo Benichie804d372016-07-07 10:15:56 +09002852 // TODO: throw an exception if callback.networkRequest is not null.
Hugo Benichi791ee322016-07-06 22:53:17 +09002853 // http://b/20701525
2854 final NetworkRequest request;
Robert Greenwalt9258c642014-03-26 16:47:06 -07002855 try {
Hugo Benichi791ee322016-07-06 22:53:17 +09002856 synchronized(sCallbacks) {
Hugo Benichie804d372016-07-07 10:15:56 +09002857 Messenger messenger = new Messenger(handler);
Hugo Benichi791ee322016-07-06 22:53:17 +09002858 Binder binder = new Binder();
Paul Jensen7221cc32014-06-27 11:05:32 -04002859 if (action == LISTEN) {
Hugo Benichi791ee322016-07-06 22:53:17 +09002860 request = mService.listenForNetwork(need, messenger, binder);
Paul Jensen7221cc32014-06-27 11:05:32 -04002861 } else {
Hugo Benichi791ee322016-07-06 22:53:17 +09002862 request = mService.requestNetwork(
2863 need, messenger, timeoutMs, binder, legacyType);
Paul Jensen7221cc32014-06-27 11:05:32 -04002864 }
Hugo Benichi791ee322016-07-06 22:53:17 +09002865 if (request != null) {
Hugo Benichie804d372016-07-07 10:15:56 +09002866 sCallbacks.put(request, callback);
Robert Greenwalt9258c642014-03-26 16:47:06 -07002867 }
Hugo Benichie804d372016-07-07 10:15:56 +09002868 callback.networkRequest = request;
Robert Greenwalt9258c642014-03-26 16:47:06 -07002869 }
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07002870 } catch (RemoteException e) {
2871 throw e.rethrowFromSystemServer();
2872 }
Hugo Benichi791ee322016-07-06 22:53:17 +09002873 return request;
Robert Greenwalt9258c642014-03-26 16:47:06 -07002874 }
2875
2876 /**
Erik Klinea2d29402016-03-16 15:31:39 +09002877 * Helper function to request a network with a particular legacy type.
Lorenzo Colitti7de289f2015-11-25 12:00:52 +09002878 *
2879 * This is temporarily public @hide so it can be called by system code that uses the
2880 * NetworkRequest API to request networks but relies on CONNECTIVITY_ACTION broadcasts for
2881 * instead network notifications.
2882 *
2883 * TODO: update said system code to rely on NetworkCallbacks and make this method private.
2884 *
2885 * @hide
2886 */
Lorenzo Colittid1179462015-11-25 15:47:14 +09002887 public void requestNetwork(NetworkRequest request, NetworkCallback networkCallback,
Lorenzo Colitti7de289f2015-11-25 12:00:52 +09002888 int timeoutMs, int legacyType) {
Lorenzo Colittid1179462015-11-25 15:47:14 +09002889 sendRequestForNetwork(request.networkCapabilities, networkCallback, timeoutMs, REQUEST,
2890 legacyType);
Lorenzo Colitti7de289f2015-11-25 12:00:52 +09002891 }
2892
2893 /**
Lorenzo Colittie285b432015-04-23 15:32:42 +09002894 * Request a network to satisfy a set of {@link android.net.NetworkCapabilities}.
Robert Greenwalt9258c642014-03-26 16:47:06 -07002895 *
2896 * This {@link NetworkRequest} will live until released via
Lorenzo Colitti88bc0bb2016-04-13 22:00:02 +09002897 * {@link #unregisterNetworkCallback(NetworkCallback)} or the calling application exits.
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07002898 * Status of the request can be followed by listening to the various
Robert Greenwalt6078b502014-06-11 16:05:07 -07002899 * callbacks described in {@link NetworkCallback}. The {@link Network}
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07002900 * can be used to direct traffic to the network.
Paul Jensenbb2e0e92015-06-16 15:11:58 -04002901 * <p>It is presently unsupported to request a network with mutable
2902 * {@link NetworkCapabilities} such as
2903 * {@link NetworkCapabilities#NET_CAPABILITY_VALIDATED} or
2904 * {@link NetworkCapabilities#NET_CAPABILITY_CAPTIVE_PORTAL}
2905 * as these {@code NetworkCapabilities} represent states that a particular
2906 * network may never attain, and whether a network will attain these states
2907 * is unknown prior to bringing up the network so the framework does not
2908 * know how to go about satisfing a request with these capabilities.
Lorenzo Colittid5427052015-10-15 16:29:00 +09002909 *
2910 * <p>This method requires the caller to hold either the
2911 * {@link android.Manifest.permission#CHANGE_NETWORK_STATE} permission
2912 * or the ability to modify system settings as determined by
2913 * {@link android.provider.Settings.System#canWrite}.</p>
Robert Greenwalt9258c642014-03-26 16:47:06 -07002914 *
Robert Greenwalt6078b502014-06-11 16:05:07 -07002915 * @param request {@link NetworkRequest} describing this request.
2916 * @param networkCallback The {@link NetworkCallback} to be utilized for this
2917 * request. Note the callback must not be shared - they
2918 * uniquely specify this request.
Paul Jensenbb2e0e92015-06-16 15:11:58 -04002919 * @throws IllegalArgumentException if {@code request} specifies any mutable
2920 * {@code NetworkCapabilities}.
Robert Greenwalt9258c642014-03-26 16:47:06 -07002921 */
Robert Greenwalt6078b502014-06-11 16:05:07 -07002922 public void requestNetwork(NetworkRequest request, NetworkCallback networkCallback) {
Lorenzo Colittid1179462015-11-25 15:47:14 +09002923 requestNetwork(request, networkCallback, 0,
Lorenzo Colitti7de289f2015-11-25 12:00:52 +09002924 inferLegacyTypeForNetworkCapabilities(request.networkCapabilities));
Robert Greenwalt9258c642014-03-26 16:47:06 -07002925 }
2926
2927 /**
Lorenzo Colittie285b432015-04-23 15:32:42 +09002928 * Request a network to satisfy a set of {@link android.net.NetworkCapabilities}, limited
Robert Greenwalt9258c642014-03-26 16:47:06 -07002929 * by a timeout.
2930 *
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07002931 * This function behaves identically to the non-timedout version, but if a suitable
Robert Greenwalt6078b502014-06-11 16:05:07 -07002932 * network is not found within the given time (in milliseconds) the
2933 * {@link NetworkCallback#unavailable} callback is called. The request must
Lorenzo Colitti88bc0bb2016-04-13 22:00:02 +09002934 * still be released normally by calling {@link unregisterNetworkCallback(NetworkCallback)}.
Lorenzo Colittid5427052015-10-15 16:29:00 +09002935 *
2936 * <p>This method requires the caller to hold either the
2937 * {@link android.Manifest.permission#CHANGE_NETWORK_STATE} permission
2938 * or the ability to modify system settings as determined by
2939 * {@link android.provider.Settings.System#canWrite}.</p>
2940 *
Robert Greenwalt6078b502014-06-11 16:05:07 -07002941 * @param request {@link NetworkRequest} describing this request.
2942 * @param networkCallback The callbacks to be utilized for this request. Note
2943 * the callbacks must not be shared - they uniquely specify
2944 * this request.
2945 * @param timeoutMs The time in milliseconds to attempt looking for a suitable network
2946 * before {@link NetworkCallback#unavailable} is called.
Lorenzo Colitti7de289f2015-11-25 12:00:52 +09002947 *
2948 * TODO: Make timeouts work and then unhide this method.
2949 *
Robert Greenwalt9258c642014-03-26 16:47:06 -07002950 * @hide
2951 */
Robert Greenwalt6078b502014-06-11 16:05:07 -07002952 public void requestNetwork(NetworkRequest request, NetworkCallback networkCallback,
2953 int timeoutMs) {
Lorenzo Colittid1179462015-11-25 15:47:14 +09002954 requestNetwork(request, networkCallback, timeoutMs,
Lorenzo Colitti7de289f2015-11-25 12:00:52 +09002955 inferLegacyTypeForNetworkCapabilities(request.networkCapabilities));
Robert Greenwalt9258c642014-03-26 16:47:06 -07002956 }
2957
2958 /**
Robert Greenwalt6078b502014-06-11 16:05:07 -07002959 * The maximum number of milliseconds the framework will look for a suitable network
Robert Greenwalt9258c642014-03-26 16:47:06 -07002960 * during a timeout-equiped call to {@link requestNetwork}.
2961 * {@hide}
2962 */
Robert Greenwalt6078b502014-06-11 16:05:07 -07002963 public final static int MAX_NETWORK_REQUEST_TIMEOUT_MS = 100 * 60 * 1000;
Robert Greenwalt9258c642014-03-26 16:47:06 -07002964
2965 /**
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07002966 * The lookup key for a {@link Network} object included with the intent after
Jeremy Joslin46e3ac82014-11-05 10:32:09 -08002967 * successfully finding a network for the applications request. Retrieve it with
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07002968 * {@link android.content.Intent#getParcelableExtra(String)}.
Jeremy Joslinfcde58f2015-02-11 16:51:13 -08002969 * <p>
Paul Jensen72db88e2015-03-10 10:54:12 -04002970 * Note that if you intend to invoke {@link Network#openConnection(java.net.URL)}
2971 * then you must get a ConnectivityManager instance before doing so.
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07002972 */
Erik Kline90e93072014-11-19 12:12:24 +09002973 public static final String EXTRA_NETWORK = "android.net.extra.NETWORK";
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07002974
2975 /**
Robert Greenwalt6078b502014-06-11 16:05:07 -07002976 * The lookup key for a {@link NetworkRequest} object included with the intent after
Jeremy Joslin46e3ac82014-11-05 10:32:09 -08002977 * successfully finding a network for the applications request. Retrieve it with
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07002978 * {@link android.content.Intent#getParcelableExtra(String)}.
2979 */
Erik Kline90e93072014-11-19 12:12:24 +09002980 public static final String EXTRA_NETWORK_REQUEST = "android.net.extra.NETWORK_REQUEST";
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07002981
2982
2983 /**
Lorenzo Colittie285b432015-04-23 15:32:42 +09002984 * Request a network to satisfy a set of {@link android.net.NetworkCapabilities}.
Robert Greenwalt9258c642014-03-26 16:47:06 -07002985 *
Jeremy Joslin46e3ac82014-11-05 10:32:09 -08002986 * This function behaves identically to the version that takes a NetworkCallback, but instead
Robert Greenwalt6078b502014-06-11 16:05:07 -07002987 * of {@link NetworkCallback} a {@link PendingIntent} is used. This means
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07002988 * the request may outlive the calling application and get called back when a suitable
2989 * network is found.
Robert Greenwalt9258c642014-03-26 16:47:06 -07002990 * <p>
2991 * The operation is an Intent broadcast that goes to a broadcast receiver that
2992 * you registered with {@link Context#registerReceiver} or through the
2993 * &lt;receiver&gt; tag in an AndroidManifest.xml file
2994 * <p>
2995 * The operation Intent is delivered with two extras, a {@link Network} typed
Erik Kline90e93072014-11-19 12:12:24 +09002996 * extra called {@link #EXTRA_NETWORK} and a {@link NetworkRequest}
2997 * typed extra called {@link #EXTRA_NETWORK_REQUEST} containing
Robert Greenwalt9258c642014-03-26 16:47:06 -07002998 * the original requests parameters. It is important to create a new,
Robert Greenwalt6078b502014-06-11 16:05:07 -07002999 * {@link NetworkCallback} based request before completing the processing of the
Robert Greenwalt9258c642014-03-26 16:47:06 -07003000 * Intent to reserve the network or it will be released shortly after the Intent
3001 * is processed.
3002 * <p>
Paul Jensen694f2b82015-06-17 14:15:39 -04003003 * If there is already a request for this Intent registered (with the equality of
Robert Greenwalt9258c642014-03-26 16:47:06 -07003004 * two Intents defined by {@link Intent#filterEquals}), then it will be removed and
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07003005 * replaced by this one, effectively releasing the previous {@link NetworkRequest}.
Robert Greenwalt9258c642014-03-26 16:47:06 -07003006 * <p>
Jeremy Joslin46e3ac82014-11-05 10:32:09 -08003007 * The request may be released normally by calling
3008 * {@link #releaseNetworkRequest(android.app.PendingIntent)}.
Paul Jensenbb2e0e92015-06-16 15:11:58 -04003009 * <p>It is presently unsupported to request a network with either
3010 * {@link NetworkCapabilities#NET_CAPABILITY_VALIDATED} or
3011 * {@link NetworkCapabilities#NET_CAPABILITY_CAPTIVE_PORTAL}
3012 * as these {@code NetworkCapabilities} represent states that a particular
3013 * network may never attain, and whether a network will attain these states
3014 * is unknown prior to bringing up the network so the framework does not
3015 * know how to go about satisfing a request with these capabilities.
Lorenzo Colittid5427052015-10-15 16:29:00 +09003016 *
3017 * <p>This method requires the caller to hold either the
3018 * {@link android.Manifest.permission#CHANGE_NETWORK_STATE} permission
3019 * or the ability to modify system settings as determined by
3020 * {@link android.provider.Settings.System#canWrite}.</p>
3021 *
Robert Greenwalt6078b502014-06-11 16:05:07 -07003022 * @param request {@link NetworkRequest} describing this request.
Robert Greenwalt9258c642014-03-26 16:47:06 -07003023 * @param operation Action to perform when the network is available (corresponds
Robert Greenwalt6078b502014-06-11 16:05:07 -07003024 * to the {@link NetworkCallback#onAvailable} call. Typically
Jeremy Joslin46e3ac82014-11-05 10:32:09 -08003025 * comes from {@link PendingIntent#getBroadcast}. Cannot be null.
Paul Jensenbb2e0e92015-06-16 15:11:58 -04003026 * @throws IllegalArgumentException if {@code request} contains either
3027 * {@link NetworkCapabilities#NET_CAPABILITY_VALIDATED} or
3028 * {@link NetworkCapabilities#NET_CAPABILITY_CAPTIVE_PORTAL}.
Robert Greenwalt9258c642014-03-26 16:47:06 -07003029 */
Robert Greenwalt6078b502014-06-11 16:05:07 -07003030 public void requestNetwork(NetworkRequest request, PendingIntent operation) {
Jeremy Joslin46e3ac82014-11-05 10:32:09 -08003031 checkPendingIntent(operation);
Robert Greenwalt9258c642014-03-26 16:47:06 -07003032 try {
Robert Greenwalt6078b502014-06-11 16:05:07 -07003033 mService.pendingRequestForNetwork(request.networkCapabilities, operation);
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07003034 } catch (RemoteException e) {
3035 throw e.rethrowFromSystemServer();
3036 }
Robert Greenwalt9258c642014-03-26 16:47:06 -07003037 }
3038
3039 /**
Jeremy Joslin46e3ac82014-11-05 10:32:09 -08003040 * Removes a request made via {@link #requestNetwork(NetworkRequest, android.app.PendingIntent)}
3041 * <p>
Lorenzo Colitti88bc0bb2016-04-13 22:00:02 +09003042 * This method has the same behavior as
3043 * {@link #unregisterNetworkCallback(android.app.PendingIntent)} with respect to
Jeremy Joslin46e3ac82014-11-05 10:32:09 -08003044 * releasing network resources and disconnecting.
3045 *
3046 * @param operation A PendingIntent equal (as defined by {@link Intent#filterEquals}) to the
3047 * PendingIntent passed to
3048 * {@link #requestNetwork(NetworkRequest, android.app.PendingIntent)} with the
3049 * corresponding NetworkRequest you'd like to remove. Cannot be null.
3050 */
3051 public void releaseNetworkRequest(PendingIntent operation) {
3052 checkPendingIntent(operation);
3053 try {
3054 mService.releasePendingNetworkRequest(operation);
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07003055 } catch (RemoteException e) {
3056 throw e.rethrowFromSystemServer();
3057 }
Jeremy Joslin46e3ac82014-11-05 10:32:09 -08003058 }
3059
3060 private void checkPendingIntent(PendingIntent intent) {
3061 if (intent == null) {
3062 throw new IllegalArgumentException("PendingIntent cannot be null.");
3063 }
3064 }
3065
3066 /**
Robert Greenwalt9258c642014-03-26 16:47:06 -07003067 * Registers to receive notifications about all networks which satisfy the given
Robert Greenwalt6078b502014-06-11 16:05:07 -07003068 * {@link NetworkRequest}. The callbacks will continue to be called until
Lorenzo Colitti88bc0bb2016-04-13 22:00:02 +09003069 * either the application exits or link #unregisterNetworkCallback(NetworkCallback)} is called.
Paul Jensenb2748922015-05-06 11:10:18 -04003070 * <p>This method requires the caller to hold the permission
3071 * {@link android.Manifest.permission#ACCESS_NETWORK_STATE}.
Robert Greenwalt9258c642014-03-26 16:47:06 -07003072 *
Robert Greenwalt6078b502014-06-11 16:05:07 -07003073 * @param request {@link NetworkRequest} describing this request.
3074 * @param networkCallback The {@link NetworkCallback} that the system will call as suitable
3075 * networks change state.
Robert Greenwalt9258c642014-03-26 16:47:06 -07003076 */
Robert Greenwalt6078b502014-06-11 16:05:07 -07003077 public void registerNetworkCallback(NetworkRequest request, NetworkCallback networkCallback) {
3078 sendRequestForNetwork(request.networkCapabilities, networkCallback, 0, LISTEN, TYPE_NONE);
Robert Greenwalt9258c642014-03-26 16:47:06 -07003079 }
3080
3081 /**
Paul Jensen694f2b82015-06-17 14:15:39 -04003082 * Registers a PendingIntent to be sent when a network is available which satisfies the given
3083 * {@link NetworkRequest}.
3084 *
3085 * This function behaves identically to the version that takes a NetworkCallback, but instead
3086 * of {@link NetworkCallback} a {@link PendingIntent} is used. This means
3087 * the request may outlive the calling application and get called back when a suitable
3088 * network is found.
3089 * <p>
3090 * The operation is an Intent broadcast that goes to a broadcast receiver that
3091 * you registered with {@link Context#registerReceiver} or through the
3092 * &lt;receiver&gt; tag in an AndroidManifest.xml file
3093 * <p>
3094 * The operation Intent is delivered with two extras, a {@link Network} typed
3095 * extra called {@link #EXTRA_NETWORK} and a {@link NetworkRequest}
3096 * typed extra called {@link #EXTRA_NETWORK_REQUEST} containing
3097 * the original requests parameters.
3098 * <p>
3099 * If there is already a request for this Intent registered (with the equality of
3100 * two Intents defined by {@link Intent#filterEquals}), then it will be removed and
3101 * replaced by this one, effectively releasing the previous {@link NetworkRequest}.
3102 * <p>
3103 * The request may be released normally by calling
Paul Jensenf2c1cfe2015-06-30 14:29:18 -04003104 * {@link #unregisterNetworkCallback(android.app.PendingIntent)}.
Paul Jensen694f2b82015-06-17 14:15:39 -04003105 * <p>This method requires the caller to hold the permission
3106 * {@link android.Manifest.permission#ACCESS_NETWORK_STATE}.
3107 * @param request {@link NetworkRequest} describing this request.
3108 * @param operation Action to perform when the network is available (corresponds
3109 * to the {@link NetworkCallback#onAvailable} call. Typically
3110 * comes from {@link PendingIntent#getBroadcast}. Cannot be null.
3111 */
3112 public void registerNetworkCallback(NetworkRequest request, PendingIntent operation) {
3113 checkPendingIntent(operation);
3114 try {
3115 mService.pendingListenForNetwork(request.networkCapabilities, operation);
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07003116 } catch (RemoteException e) {
3117 throw e.rethrowFromSystemServer();
3118 }
Paul Jensen694f2b82015-06-17 14:15:39 -04003119 }
3120
3121 /**
Lorenzo Colitti88bc0bb2016-04-13 22:00:02 +09003122 * Registers to receive notifications about changes in the system default network. The callbacks
3123 * will continue to be called until either the application exits or
3124 * {@link #unregisterNetworkCallback(NetworkCallback)} is called.
Erik Klinea2d29402016-03-16 15:31:39 +09003125 * <p>This method requires the caller to hold the permission
3126 * {@link android.Manifest.permission#ACCESS_NETWORK_STATE}.
3127 *
3128 * @param networkCallback The {@link NetworkCallback} that the system will call as the
3129 * system default network changes.
Erik Klinea2d29402016-03-16 15:31:39 +09003130 */
3131 public void registerDefaultNetworkCallback(NetworkCallback networkCallback) {
3132 // This works because if the NetworkCapabilities are null,
3133 // ConnectivityService takes them from the default request.
3134 //
3135 // Since the capabilities are exactly the same as the default request's
3136 // capabilities, this request is guaranteed, at all times, to be
3137 // satisfied by the same network, if any, that satisfies the default
3138 // request, i.e., the system default network.
3139 sendRequestForNetwork(null, networkCallback, 0, REQUEST, TYPE_NONE);
3140 }
3141
3142 /**
fengludb571472015-04-21 17:12:05 -07003143 * Requests bandwidth update for a given {@link Network} and returns whether the update request
3144 * is accepted by ConnectivityService. Once accepted, ConnectivityService will poll underlying
3145 * network connection for updated bandwidth information. The caller will be notified via
3146 * {@link ConnectivityManager.NetworkCallback} if there is an update. Notice that this
Lorenzo Colitti88bc0bb2016-04-13 22:00:02 +09003147 * method assumes that the caller has previously called
3148 * {@link #registerNetworkCallback(NetworkRequest, NetworkCallback)} to listen for network
3149 * changes.
fenglub15e72b2015-03-20 11:29:56 -07003150 *
fengluae519192015-04-27 14:28:04 -07003151 * @param network {@link Network} specifying which network you're interested.
fengludb571472015-04-21 17:12:05 -07003152 * @return {@code true} on success, {@code false} if the {@link Network} is no longer valid.
fenglub15e72b2015-03-20 11:29:56 -07003153 */
fengludb571472015-04-21 17:12:05 -07003154 public boolean requestBandwidthUpdate(Network network) {
fenglub15e72b2015-03-20 11:29:56 -07003155 try {
fengludb571472015-04-21 17:12:05 -07003156 return mService.requestBandwidthUpdate(network);
fenglub15e72b2015-03-20 11:29:56 -07003157 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07003158 throw e.rethrowFromSystemServer();
fenglub15e72b2015-03-20 11:29:56 -07003159 }
3160 }
3161
3162 /**
Robert Greenwalt6078b502014-06-11 16:05:07 -07003163 * Unregisters callbacks about and possibly releases networks originating from
Lorenzo Colitti88bc0bb2016-04-13 22:00:02 +09003164 * {@link #requestNetwork(NetworkRequest, NetworkCallback)} and
3165 * {@link #registerNetworkCallback(NetworkRequest, NetworkCallback)} calls.
3166 * If the given {@code NetworkCallback} had previously been used with
Lorenzo Colitti2ea89e52015-04-24 17:03:31 +09003167 * {@code #requestNetwork}, any networks that had been connected to only to satisfy that request
3168 * will be disconnected.
Robert Greenwalt9258c642014-03-26 16:47:06 -07003169 *
Robert Greenwalt6078b502014-06-11 16:05:07 -07003170 * @param networkCallback The {@link NetworkCallback} used when making the request.
Robert Greenwalt9258c642014-03-26 16:47:06 -07003171 */
Robert Greenwalt6078b502014-06-11 16:05:07 -07003172 public void unregisterNetworkCallback(NetworkCallback networkCallback) {
3173 if (networkCallback == null || networkCallback.networkRequest == null ||
3174 networkCallback.networkRequest.requestId == REQUEST_ID_UNSET) {
3175 throw new IllegalArgumentException("Invalid NetworkCallback");
3176 }
Robert Greenwalt9258c642014-03-26 16:47:06 -07003177 try {
Hugo Benichi39e10e82016-07-07 09:36:12 +09003178 // CallbackHandler will release callback when receiving CALLBACK_RELEASED.
Robert Greenwalt6078b502014-06-11 16:05:07 -07003179 mService.releaseNetworkRequest(networkCallback.networkRequest);
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07003180 } catch (RemoteException e) {
3181 throw e.rethrowFromSystemServer();
3182 }
Robert Greenwalt9258c642014-03-26 16:47:06 -07003183 }
Paul Jensen6d3ff9e2014-05-29 10:12:39 -04003184
3185 /**
Paul Jensenf2c1cfe2015-06-30 14:29:18 -04003186 * Unregisters a callback previously registered via
3187 * {@link #registerNetworkCallback(NetworkRequest, android.app.PendingIntent)}.
3188 *
3189 * @param operation A PendingIntent equal (as defined by {@link Intent#filterEquals}) to the
3190 * PendingIntent passed to
3191 * {@link #registerNetworkCallback(NetworkRequest, android.app.PendingIntent)}.
3192 * Cannot be null.
3193 */
3194 public void unregisterNetworkCallback(PendingIntent operation) {
3195 releaseNetworkRequest(operation);
3196 }
3197
3198 /**
Lorenzo Colittie03c3c72015-04-03 16:38:52 +09003199 * Informs the system whether it should switch to {@code network} regardless of whether it is
3200 * validated or not. If {@code accept} is true, and the network was explicitly selected by the
3201 * user (e.g., by selecting a Wi-Fi network in the Settings app), then the network will become
3202 * the system default network regardless of any other network that's currently connected. If
3203 * {@code always} is true, then the choice is remembered, so that the next time the user
3204 * connects to this network, the system will switch to it.
3205 *
3206 * <p>This method requires the caller to hold the permission
3207 * {@link android.Manifest.permission#CONNECTIVITY_INTERNAL}
3208 *
3209 * @param network The network to accept.
3210 * @param accept Whether to accept the network even if unvalidated.
3211 * @param always Whether to remember this choice in the future.
3212 *
3213 * @hide
3214 */
3215 public void setAcceptUnvalidated(Network network, boolean accept, boolean always) {
3216 try {
3217 mService.setAcceptUnvalidated(network, accept, always);
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07003218 } catch (RemoteException e) {
3219 throw e.rethrowFromSystemServer();
3220 }
Lorenzo Colittie03c3c72015-04-03 16:38:52 +09003221 }
3222
3223 /**
Lorenzo Colitti165c51c2016-09-19 01:00:19 +09003224 * Informs the system to penalize {@code network}'s score when it becomes unvalidated. This is
3225 * only meaningful if the system is configured not to penalize such networks, e.g., if the
3226 * {@code config_networkAvoidBadWifi} configuration variable is set to 0 and the {@code
3227 * NETWORK_AVOID_BAD_WIFI setting is unset}.
3228 *
3229 * <p>This method requires the caller to hold the permission
3230 * {@link android.Manifest.permission#CONNECTIVITY_INTERNAL}
3231 *
3232 * @param network The network to accept.
3233 *
3234 * @hide
3235 */
3236 public void setAvoidUnvalidated(Network network) {
3237 try {
3238 mService.setAvoidUnvalidated(network);
3239 } catch (RemoteException e) {
3240 throw e.rethrowFromSystemServer();
3241 }
3242 }
3243
3244 /**
Stuart Scott984dc852015-03-30 13:17:11 -07003245 * Resets all connectivity manager settings back to factory defaults.
3246 * @hide
3247 */
3248 public void factoryReset() {
Stuart Scott984dc852015-03-30 13:17:11 -07003249 try {
Stuart Scottf1fb3972015-04-02 18:00:02 -07003250 mService.factoryReset();
Stuart Scott984dc852015-03-30 13:17:11 -07003251 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07003252 throw e.rethrowFromSystemServer();
Stuart Scott984dc852015-03-30 13:17:11 -07003253 }
3254 }
3255
3256 /**
Paul Jensen6d3ff9e2014-05-29 10:12:39 -04003257 * Binds the current process to {@code network}. All Sockets created in the future
3258 * (and not explicitly bound via a bound SocketFactory from
3259 * {@link Network#getSocketFactory() Network.getSocketFactory()}) will be bound to
3260 * {@code network}. All host name resolutions will be limited to {@code network} as well.
3261 * Note that if {@code network} ever disconnects, all Sockets created in this way will cease to
3262 * work and all host name resolutions will fail. This is by design so an application doesn't
3263 * accidentally use Sockets it thinks are still bound to a particular {@link Network}.
3264 * To clear binding pass {@code null} for {@code network}. Using individually bound
3265 * Sockets created by Network.getSocketFactory().createSocket() and
3266 * performing network-specific host name resolutions via
3267 * {@link Network#getAllByName Network.getAllByName} is preferred to calling
Paul Jensen72db88e2015-03-10 10:54:12 -04003268 * {@code bindProcessToNetwork}.
Paul Jensen6d3ff9e2014-05-29 10:12:39 -04003269 *
3270 * @param network The {@link Network} to bind the current process to, or {@code null} to clear
3271 * the current binding.
3272 * @return {@code true} on success, {@code false} if the {@link Network} is no longer valid.
3273 */
Paul Jensen72db88e2015-03-10 10:54:12 -04003274 public boolean bindProcessToNetwork(Network network) {
3275 // Forcing callers to call thru non-static function ensures ConnectivityManager
3276 // instantiated.
3277 return setProcessDefaultNetwork(network);
3278 }
3279
3280 /**
3281 * Binds the current process to {@code network}. All Sockets created in the future
3282 * (and not explicitly bound via a bound SocketFactory from
3283 * {@link Network#getSocketFactory() Network.getSocketFactory()}) will be bound to
3284 * {@code network}. All host name resolutions will be limited to {@code network} as well.
3285 * Note that if {@code network} ever disconnects, all Sockets created in this way will cease to
3286 * work and all host name resolutions will fail. This is by design so an application doesn't
3287 * accidentally use Sockets it thinks are still bound to a particular {@link Network}.
3288 * To clear binding pass {@code null} for {@code network}. Using individually bound
3289 * Sockets created by Network.getSocketFactory().createSocket() and
3290 * performing network-specific host name resolutions via
3291 * {@link Network#getAllByName Network.getAllByName} is preferred to calling
3292 * {@code setProcessDefaultNetwork}.
3293 *
3294 * @param network The {@link Network} to bind the current process to, or {@code null} to clear
3295 * the current binding.
3296 * @return {@code true} on success, {@code false} if the {@link Network} is no longer valid.
3297 * @deprecated This function can throw {@link IllegalStateException}. Use
3298 * {@link #bindProcessToNetwork} instead. {@code bindProcessToNetwork}
3299 * is a direct replacement.
3300 */
Paul Jensen6d3ff9e2014-05-29 10:12:39 -04003301 public static boolean setProcessDefaultNetwork(Network network) {
Paul Jensenc91b5342014-08-27 12:38:45 -04003302 int netId = (network == null) ? NETID_UNSET : network.netId;
Paul Jensen72db88e2015-03-10 10:54:12 -04003303 if (netId == NetworkUtils.getBoundNetworkForProcess()) {
Paul Jensenc91b5342014-08-27 12:38:45 -04003304 return true;
3305 }
3306 if (NetworkUtils.bindProcessToNetwork(netId)) {
Paul Jensene0bef712014-12-10 15:12:18 -05003307 // Set HTTP proxy system properties to match network.
3308 // TODO: Deprecate this static method and replace it with a non-static version.
Lorenzo Colittiec4c5552015-04-22 11:52:48 +09003309 try {
3310 Proxy.setHttpProxySystemProperty(getInstance().getDefaultProxy());
3311 } catch (SecurityException e) {
3312 // The process doesn't have ACCESS_NETWORK_STATE, so we can't fetch the proxy.
3313 Log.e(TAG, "Can't set proxy properties", e);
3314 }
Paul Jensenc91b5342014-08-27 12:38:45 -04003315 // Must flush DNS cache as new network may have different DNS resolutions.
3316 InetAddress.clearDnsCache();
3317 // Must flush socket pool as idle sockets will be bound to previous network and may
3318 // cause subsequent fetches to be performed on old network.
3319 NetworkEventDispatcher.getInstance().onNetworkConfigurationChanged();
3320 return true;
3321 } else {
3322 return false;
3323 }
Paul Jensen6d3ff9e2014-05-29 10:12:39 -04003324 }
3325
3326 /**
3327 * Returns the {@link Network} currently bound to this process via
Paul Jensen72db88e2015-03-10 10:54:12 -04003328 * {@link #bindProcessToNetwork}, or {@code null} if no {@link Network} is explicitly bound.
Paul Jensen6d3ff9e2014-05-29 10:12:39 -04003329 *
3330 * @return {@code Network} to which this process is bound, or {@code null}.
3331 */
Paul Jensen72db88e2015-03-10 10:54:12 -04003332 public Network getBoundNetworkForProcess() {
3333 // Forcing callers to call thru non-static function ensures ConnectivityManager
3334 // instantiated.
3335 return getProcessDefaultNetwork();
3336 }
3337
3338 /**
3339 * Returns the {@link Network} currently bound to this process via
3340 * {@link #bindProcessToNetwork}, or {@code null} if no {@link Network} is explicitly bound.
3341 *
3342 * @return {@code Network} to which this process is bound, or {@code null}.
3343 * @deprecated Using this function can lead to other functions throwing
3344 * {@link IllegalStateException}. Use {@link #getBoundNetworkForProcess} instead.
3345 * {@code getBoundNetworkForProcess} is a direct replacement.
3346 */
Paul Jensen6d3ff9e2014-05-29 10:12:39 -04003347 public static Network getProcessDefaultNetwork() {
Paul Jensen72db88e2015-03-10 10:54:12 -04003348 int netId = NetworkUtils.getBoundNetworkForProcess();
Paul Jensenbcc76d32014-07-11 08:17:29 -04003349 if (netId == NETID_UNSET) return null;
Paul Jensen6d3ff9e2014-05-29 10:12:39 -04003350 return new Network(netId);
3351 }
3352
Lorenzo Colittiffc42b02015-07-29 11:41:21 +09003353 private void unsupportedStartingFrom(int version) {
3354 if (Process.myUid() == Process.SYSTEM_UID) {
3355 // The getApplicationInfo() call we make below is not supported in system context, and
3356 // we want to allow the system to use these APIs anyway.
3357 return;
3358 }
3359
3360 if (mContext.getApplicationInfo().targetSdkVersion >= version) {
3361 throw new UnsupportedOperationException(
3362 "This method is not supported in target SDK version " + version + " and above");
3363 }
3364 }
3365
3366 // Checks whether the calling app can use the legacy routing API (startUsingNetworkFeature,
3367 // stopUsingNetworkFeature, requestRouteToHost), and if not throw UnsupportedOperationException.
Lifu Tang30f95a72016-01-07 23:20:38 -08003368 // TODO: convert the existing system users (Tethering, GnssLocationProvider) to the new APIs and
Lorenzo Colittiffc42b02015-07-29 11:41:21 +09003369 // remove these exemptions. Note that this check is not secure, and apps can still access these
3370 // functions by accessing ConnectivityService directly. However, it should be clear that doing
3371 // so is unsupported and may break in the future. http://b/22728205
3372 private void checkLegacyRoutingApiAccess() {
3373 if (mContext.checkCallingOrSelfPermission("com.android.permission.INJECT_OMADM_SETTINGS")
3374 == PackageManager.PERMISSION_GRANTED) {
3375 return;
3376 }
3377
Dianne Hackborn692a2442015-07-31 10:35:34 -07003378 unsupportedStartingFrom(VERSION_CODES.M);
Lorenzo Colittiffc42b02015-07-29 11:41:21 +09003379 }
3380
Paul Jensen6d3ff9e2014-05-29 10:12:39 -04003381 /**
3382 * Binds host resolutions performed by this process to {@code network}.
Paul Jensen72db88e2015-03-10 10:54:12 -04003383 * {@link #bindProcessToNetwork} takes precedence over this setting.
Paul Jensen6d3ff9e2014-05-29 10:12:39 -04003384 *
3385 * @param network The {@link Network} to bind host resolutions from the current process to, or
3386 * {@code null} to clear the current binding.
3387 * @return {@code true} on success, {@code false} if the {@link Network} is no longer valid.
3388 * @hide
3389 * @deprecated This is strictly for legacy usage to support {@link #startUsingNetworkFeature}.
3390 */
3391 public static boolean setProcessDefaultNetworkForHostResolution(Network network) {
Paul Jensenbcc76d32014-07-11 08:17:29 -04003392 return NetworkUtils.bindProcessToNetworkForHostResolution(
3393 network == null ? NETID_UNSET : network.netId);
Paul Jensen6d3ff9e2014-05-29 10:12:39 -04003394 }
Felipe Leme1b103232016-01-22 09:44:57 -08003395
3396 /**
3397 * Device is not restricting metered network activity while application is running on
3398 * background.
3399 */
3400 public static final int RESTRICT_BACKGROUND_STATUS_DISABLED = 1;
3401
3402 /**
3403 * Device is restricting metered network activity while application is running on background,
3404 * but application is allowed to bypass it.
3405 * <p>
3406 * In this state, application should take action to mitigate metered network access.
3407 * For example, a music streaming application should switch to a low-bandwidth bitrate.
3408 */
3409 public static final int RESTRICT_BACKGROUND_STATUS_WHITELISTED = 2;
3410
3411 /**
3412 * Device is restricting metered network activity while application is running on background.
Felipe Leme9778f762016-01-27 14:46:39 -08003413 * <p>
Felipe Leme1b103232016-01-22 09:44:57 -08003414 * In this state, application should not try to use the network while running on background,
3415 * because it would be denied.
3416 */
3417 public static final int RESTRICT_BACKGROUND_STATUS_ENABLED = 3;
3418
Felipe Leme9778f762016-01-27 14:46:39 -08003419 /**
3420 * A change in the background metered network activity restriction has occurred.
3421 * <p>
3422 * Applications should call {@link #getRestrictBackgroundStatus()} to check if the restriction
3423 * applies to them.
3424 * <p>
3425 * This is only sent to registered receivers, not manifest receivers.
3426 */
3427 @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION)
3428 public static final String ACTION_RESTRICT_BACKGROUND_CHANGED =
3429 "android.net.conn.RESTRICT_BACKGROUND_CHANGED";
3430
Felipe Lemeecfccea2016-01-25 11:48:04 -08003431 /** @hide */
3432 @Retention(RetentionPolicy.SOURCE)
Felipe Leme1b103232016-01-22 09:44:57 -08003433 @IntDef(flag = false, value = {
3434 RESTRICT_BACKGROUND_STATUS_DISABLED,
3435 RESTRICT_BACKGROUND_STATUS_WHITELISTED,
3436 RESTRICT_BACKGROUND_STATUS_ENABLED,
3437 })
Felipe Leme1b103232016-01-22 09:44:57 -08003438 public @interface RestrictBackgroundStatus {
3439 }
3440
3441 private INetworkPolicyManager getNetworkPolicyManager() {
3442 synchronized (this) {
3443 if (mNPManager != null) {
3444 return mNPManager;
3445 }
3446 mNPManager = INetworkPolicyManager.Stub.asInterface(ServiceManager
3447 .getService(Context.NETWORK_POLICY_SERVICE));
3448 return mNPManager;
3449 }
3450 }
3451
3452 /**
3453 * Determines if the calling application is subject to metered network restrictions while
3454 * running on background.
Felipe Lemec9c7be52016-05-16 13:57:19 -07003455 *
3456 * @return {@link #RESTRICT_BACKGROUND_STATUS_DISABLED},
3457 * {@link #RESTRICT_BACKGROUND_STATUS_ENABLED},
3458 * or {@link #RESTRICT_BACKGROUND_STATUS_WHITELISTED}
Felipe Leme1b103232016-01-22 09:44:57 -08003459 */
3460 public @RestrictBackgroundStatus int getRestrictBackgroundStatus() {
3461 try {
3462 return getNetworkPolicyManager().getRestrictBackgroundByCaller();
3463 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07003464 throw e.rethrowFromSystemServer();
Felipe Leme1b103232016-01-22 09:44:57 -08003465 }
3466 }
Andreas Gampe34802132016-04-20 14:33:51 -07003467
3468 /**
3469 * A holder class for debug info (mapping CALLBACK values to field names). This is stored
3470 * in a holder for two reasons:
3471 * 1) The reflection necessary to establish the map can't be run at compile-time. Thus, this
3472 * code will make the enclosing class not compile-time initializeable, deferring its
3473 * initialization to zygote startup. This leads to dirty (but shared) memory.
3474 * As this is debug info, use a holder that isn't initialized by default. This way the map
3475 * will be created on demand, while ConnectivityManager can be compile-time initialized.
3476 * 2) Static initialization is still preferred for its strong thread safety guarantees without
3477 * requiring a lock.
3478 */
3479 private static class NoPreloadHolder {
3480 public static final SparseArray<String> sMagicDecoderRing = MessageUtils.findMessageNames(
3481 new Class[]{ConnectivityManager.class}, new String[]{"CALLBACK_"});
3482 }
3483
3484 static {
3485 // When debug is enabled, aggressively initialize the holder by touching the field (which
3486 // will guarantee static initialization).
3487 if (CallbackHandler.DBG) {
3488 Object dummy = NoPreloadHolder.sMagicDecoderRing;
3489 }
3490 }
3491
3492 private static final String whatToString(int what) {
3493 return NoPreloadHolder.sMagicDecoderRing.get(what, Integer.toString(what));
3494 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003495}