blob: d1d5f40739dba9411544a29b601921579abb1e2e [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 */
Aurimas Liutikas514c5ef2016-05-24 15:22:55 -0700461 @Deprecated
Robert Greenwalt42acef32009-08-12 16:08:25 -0700462 public static final int TYPE_MOBILE_MMS = 2;
463 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800464 * A SUPL-specific Mobile data connection. This network type may use the
465 * same network interface as {@link #TYPE_MOBILE} or it may use a different
466 * one. This is used by applications needing to talk to the carrier's
467 * Secure User Plane Location servers for help locating the device.
Lorenzo Colittie285b432015-04-23 15:32:42 +0900468 *
Lorenzo Colitti2ea89e52015-04-24 17:03:31 +0900469 * @deprecated Applications should instead use
470 * {@link #requestNetwork(NetworkRequest, NetworkCallback)} to request a network that
Lorenzo Colittie285b432015-04-23 15:32:42 +0900471 * provides the {@link NetworkCapabilities#NET_CAPABILITY_SUPL} capability.
Robert Greenwalt42acef32009-08-12 16:08:25 -0700472 */
Aurimas Liutikas514c5ef2016-05-24 15:22:55 -0700473 @Deprecated
Robert Greenwalt42acef32009-08-12 16:08:25 -0700474 public static final int TYPE_MOBILE_SUPL = 3;
475 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800476 * A DUN-specific Mobile data connection. This network type may use the
477 * same network interface as {@link #TYPE_MOBILE} or it may use a different
478 * one. This is sometimes by the system when setting up an upstream connection
479 * for tethering so that the carrier is aware of DUN traffic.
Robert Greenwalt42acef32009-08-12 16:08:25 -0700480 */
481 public static final int TYPE_MOBILE_DUN = 4;
482 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800483 * A High Priority Mobile data connection. This network type uses the
484 * same network interface as {@link #TYPE_MOBILE} but the routing setup
Lorenzo Colittie285b432015-04-23 15:32:42 +0900485 * is different.
486 *
Lorenzo Colitti2ea89e52015-04-24 17:03:31 +0900487 * @deprecated Applications should instead use
488 * {@link #requestNetwork(NetworkRequest, NetworkCallback)} to request a network that
Lorenzo Colittie285b432015-04-23 15:32:42 +0900489 * uses the {@link NetworkCapabilities#TRANSPORT_CELLULAR} transport.
Robert Greenwalt42acef32009-08-12 16:08:25 -0700490 */
Aurimas Liutikas514c5ef2016-05-24 15:22:55 -0700491 @Deprecated
Robert Greenwalt42acef32009-08-12 16:08:25 -0700492 public static final int TYPE_MOBILE_HIPRI = 5;
jsh8214deb2010-03-11 15:04:43 -0800493 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800494 * The WiMAX data connection. When active, all data traffic
495 * will use this network type's interface by default
496 * (it has a default route).
jsh8214deb2010-03-11 15:04:43 -0800497 */
498 public static final int TYPE_WIMAX = 6;
Robert Greenwaltda3d5e62010-12-06 13:56:24 -0800499
Jaikumar Ganesh15c74392010-12-21 22:31:44 -0800500 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800501 * The Bluetooth data connection. When active, all data traffic
502 * will use this network type's interface by default
503 * (it has a default route).
Jaikumar Ganesh15c74392010-12-21 22:31:44 -0800504 */
505 public static final int TYPE_BLUETOOTH = 7;
506
Robert Greenwalt60810842011-04-22 15:28:18 -0700507 /**
508 * Dummy data connection. This should not be used on shipping devices.
509 */
Jaikumar Ganesh15c74392010-12-21 22:31:44 -0800510 public static final int TYPE_DUMMY = 8;
Wink Saville9d7d6282011-03-12 14:52:01 -0800511
Robert Greenwalt60810842011-04-22 15:28:18 -0700512 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800513 * The Ethernet data connection. When active, all data traffic
514 * will use this network type's interface by default
515 * (it has a default route).
Robert Greenwalt60810842011-04-22 15:28:18 -0700516 */
Robert Greenwalte12aec92011-01-28 14:48:37 -0800517 public static final int TYPE_ETHERNET = 9;
Robert Greenwalt60810842011-04-22 15:28:18 -0700518
Wink Saville9d7d6282011-03-12 14:52:01 -0800519 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800520 * Over the air Administration.
Wink Saville9d7d6282011-03-12 14:52:01 -0800521 * {@hide}
522 */
523 public static final int TYPE_MOBILE_FOTA = 10;
524
525 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800526 * IP Multimedia Subsystem.
Wink Saville9d7d6282011-03-12 14:52:01 -0800527 * {@hide}
528 */
529 public static final int TYPE_MOBILE_IMS = 11;
530
531 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800532 * Carrier Branded Services.
Wink Saville9d7d6282011-03-12 14:52:01 -0800533 * {@hide}
534 */
535 public static final int TYPE_MOBILE_CBS = 12;
536
repo syncaea743a2011-07-29 23:55:49 -0700537 /**
538 * A Wi-Fi p2p connection. Only requesting processes will have access to
539 * the peers connected.
540 * {@hide}
541 */
542 public static final int TYPE_WIFI_P2P = 13;
Wink Saville9d7d6282011-03-12 14:52:01 -0800543
Wink Saville5e56bc52013-07-29 15:00:57 -0700544 /**
545 * The network to use for initially attaching to the network
546 * {@hide}
547 */
548 public static final int TYPE_MOBILE_IA = 14;
repo syncaea743a2011-07-29 23:55:49 -0700549
Lorenzo Colittie285b432015-04-23 15:32:42 +0900550 /**
Robert Greenwalt4bd43892015-07-09 14:49:35 -0700551 * Emergency PDN connection for emergency services. This
552 * may include IMS and MMS in emergency situations.
Ram3e0e3bc2014-06-26 11:03:44 -0700553 * {@hide}
554 */
555 public static final int TYPE_MOBILE_EMERGENCY = 15;
556
Hui Lu1c5624a2014-01-15 11:05:36 -0500557 /**
558 * The network that uses proxy to achieve connectivity.
559 * {@hide}
560 */
561 public static final int TYPE_PROXY = 16;
Wink Saville5e56bc52013-07-29 15:00:57 -0700562
Robert Greenwalt8283f882014-07-07 17:09:01 -0700563 /**
564 * A virtual network using one or more native bearers.
565 * It may or may not be providing security services.
566 */
567 public static final int TYPE_VPN = 17;
Hui Lu1c5624a2014-01-15 11:05:36 -0500568
569 /** {@hide} */
Robert Greenwalt8283f882014-07-07 17:09:01 -0700570 public static final int MAX_RADIO_TYPE = TYPE_VPN;
571
572 /** {@hide} */
573 public static final int MAX_NETWORK_TYPE = TYPE_VPN;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800574
Jianzheng Zhoudcf03f32012-11-16 13:45:20 +0800575 /**
576 * If you want to set the default network preference,you can directly
577 * change the networkAttributes array in framework's config.xml.
578 *
579 * @deprecated Since we support so many more networks now, the single
580 * network default network preference can't really express
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800581 * the hierarchy. Instead, the default is defined by the
Jianzheng Zhoudcf03f32012-11-16 13:45:20 +0800582 * networkAttributes in config.xml. You can determine
Robert Greenwalt4c8b7482012-12-07 09:56:50 -0800583 * the current value by calling {@link #getNetworkPreference()}
Jianzheng Zhoudcf03f32012-11-16 13:45:20 +0800584 * from an App.
585 */
586 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800587 public static final int DEFAULT_NETWORK_PREFERENCE = TYPE_WIFI;
588
Jeff Sharkey625239a2012-09-26 22:03:49 -0700589 /**
Robert Greenwalt9ba9c582014-03-19 17:56:12 -0700590 * @hide
591 */
Robert Greenwalt7569f182014-06-08 16:42:59 -0700592 public final static int REQUEST_ID_UNSET = 0;
593
Paul Jensen5d59e782014-07-11 12:28:19 -0400594 /**
595 * A NetID indicating no Network is selected.
596 * Keep in sync with bionic/libc/dns/include/resolv_netid.h
597 * @hide
598 */
599 public static final int NETID_UNSET = 0;
600
Jeff Sharkeyc006f1a2011-05-19 17:12:49 -0700601 private final IConnectivityManager mService;
Paul Jensene0bef712014-12-10 15:12:18 -0500602 /**
603 * A kludge to facilitate static access where a Context pointer isn't available, like in the
604 * case of the static set/getProcessDefaultNetwork methods and from the Network class.
605 * TODO: Remove this after deprecating the static methods in favor of non-static methods or
606 * methods that take a Context argument.
607 */
608 private static ConnectivityManager sInstance;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800609
Lorenzo Colittiffc42b02015-07-29 11:41:21 +0900610 private final Context mContext;
611
Dianne Hackborn77b987f2014-02-26 16:20:52 -0800612 private INetworkManagementService mNMService;
Felipe Leme1b103232016-01-22 09:44:57 -0800613 private INetworkPolicyManager mNPManager;
Dianne Hackborn77b987f2014-02-26 16:20:52 -0800614
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800615 /**
616 * Tests if a given integer represents a valid network type.
617 * @param networkType the type to be tested
618 * @return a boolean. {@code true} if the type is valid, else {@code false}
Paul Jensen9e59e122015-05-06 10:42:25 -0400619 * @deprecated All APIs accepting a network type are deprecated. There should be no need to
620 * validate a network type.
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800621 */
Aurimas Liutikas514c5ef2016-05-24 15:22:55 -0700622 @Deprecated
Jeff Sharkeyd2a45872011-05-28 20:56:34 -0700623 public static boolean isNetworkTypeValid(int networkType) {
Robert Greenwalt42acef32009-08-12 16:08:25 -0700624 return networkType >= 0 && networkType <= MAX_NETWORK_TYPE;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800625 }
626
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800627 /**
628 * Returns a non-localized string representing a given network type.
629 * ONLY used for debugging output.
630 * @param type the type needing naming
631 * @return a String for the given type, or a string version of the type ("87")
632 * if no name is known.
633 * {@hide}
634 */
Jeff Sharkeyd2a45872011-05-28 20:56:34 -0700635 public static String getNetworkTypeName(int type) {
636 switch (type) {
637 case TYPE_MOBILE:
638 return "MOBILE";
639 case TYPE_WIFI:
640 return "WIFI";
641 case TYPE_MOBILE_MMS:
642 return "MOBILE_MMS";
643 case TYPE_MOBILE_SUPL:
644 return "MOBILE_SUPL";
645 case TYPE_MOBILE_DUN:
646 return "MOBILE_DUN";
647 case TYPE_MOBILE_HIPRI:
648 return "MOBILE_HIPRI";
649 case TYPE_WIMAX:
650 return "WIMAX";
651 case TYPE_BLUETOOTH:
652 return "BLUETOOTH";
653 case TYPE_DUMMY:
654 return "DUMMY";
655 case TYPE_ETHERNET:
656 return "ETHERNET";
657 case TYPE_MOBILE_FOTA:
658 return "MOBILE_FOTA";
659 case TYPE_MOBILE_IMS:
660 return "MOBILE_IMS";
661 case TYPE_MOBILE_CBS:
662 return "MOBILE_CBS";
repo syncaea743a2011-07-29 23:55:49 -0700663 case TYPE_WIFI_P2P:
664 return "WIFI_P2P";
Wink Saville5e56bc52013-07-29 15:00:57 -0700665 case TYPE_MOBILE_IA:
666 return "MOBILE_IA";
Ram3e0e3bc2014-06-26 11:03:44 -0700667 case TYPE_MOBILE_EMERGENCY:
668 return "MOBILE_EMERGENCY";
Hui Lu1c5624a2014-01-15 11:05:36 -0500669 case TYPE_PROXY:
670 return "PROXY";
Erik Kline37fbfa12014-11-19 17:23:41 +0900671 case TYPE_VPN:
672 return "VPN";
Jeff Sharkeyd2a45872011-05-28 20:56:34 -0700673 default:
674 return Integer.toString(type);
675 }
676 }
677
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800678 /**
679 * Checks if a given type uses the cellular data connection.
680 * This should be replaced in the future by a network property.
681 * @param networkType the type to check
682 * @return a boolean - {@code true} if uses cellular network, else {@code false}
683 * {@hide}
684 */
Jeff Sharkeyd2a45872011-05-28 20:56:34 -0700685 public static boolean isNetworkTypeMobile(int networkType) {
686 switch (networkType) {
687 case TYPE_MOBILE:
688 case TYPE_MOBILE_MMS:
689 case TYPE_MOBILE_SUPL:
690 case TYPE_MOBILE_DUN:
691 case TYPE_MOBILE_HIPRI:
692 case TYPE_MOBILE_FOTA:
693 case TYPE_MOBILE_IMS:
694 case TYPE_MOBILE_CBS:
Wink Saville5e56bc52013-07-29 15:00:57 -0700695 case TYPE_MOBILE_IA:
Ram3e0e3bc2014-06-26 11:03:44 -0700696 case TYPE_MOBILE_EMERGENCY:
Jeff Sharkeyd2a45872011-05-28 20:56:34 -0700697 return true;
698 default:
699 return false;
700 }
701 }
702
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800703 /**
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -0700704 * Checks if the given network type is backed by a Wi-Fi radio.
705 *
706 * @hide
707 */
708 public static boolean isNetworkTypeWifi(int networkType) {
709 switch (networkType) {
710 case TYPE_WIFI:
711 case TYPE_WIFI_P2P:
712 return true;
713 default:
714 return false;
715 }
716 }
717
718 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800719 * Specifies the preferred network type. When the device has more
720 * than one type available the preferred network type will be used.
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800721 *
722 * @param preference the network type to prefer over all others. It is
723 * unspecified what happens to the old preferred network in the
724 * overall ordering.
Robert Greenwaltd19c41c2014-05-18 23:07:25 -0700725 * @deprecated Functionality has been removed as it no longer makes sense,
726 * with many more than two networks - we'd need an array to express
727 * preference. Instead we use dynamic network properties of
728 * the networks to describe their precedence.
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800729 */
Aurimas Liutikas514c5ef2016-05-24 15:22:55 -0700730 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800731 public void setNetworkPreference(int preference) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800732 }
733
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800734 /**
735 * Retrieves the current preferred network type.
Paul Jensenb2748922015-05-06 11:10:18 -0400736 * <p>This method requires the caller to hold the permission
737 * {@link android.Manifest.permission#ACCESS_NETWORK_STATE}.
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800738 *
739 * @return an integer representing the preferred network type
740 *
Robert Greenwaltd19c41c2014-05-18 23:07:25 -0700741 * @deprecated Functionality has been removed as it no longer makes sense,
742 * with many more than two networks - we'd need an array to express
743 * preference. Instead we use dynamic network properties of
744 * the networks to describe their precedence.
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800745 */
Aurimas Liutikas514c5ef2016-05-24 15:22:55 -0700746 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800747 public int getNetworkPreference() {
Robert Greenwaltd19c41c2014-05-18 23:07:25 -0700748 return TYPE_NONE;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800749 }
750
Scott Main671644c2011-10-06 19:02:28 -0700751 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800752 * Returns details about the currently active default data network. When
753 * connected, this network is the default route for outgoing connections.
754 * You should always check {@link NetworkInfo#isConnected()} before initiating
755 * network traffic. This may return {@code null} when there is no default
756 * network.
Paul Jensenb2748922015-05-06 11:10:18 -0400757 * <p>This method requires the caller to hold the permission
758 * {@link android.Manifest.permission#ACCESS_NETWORK_STATE}.
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800759 *
760 * @return a {@link NetworkInfo} object for the current default network
Paul Jensen0d719ca2015-02-13 14:18:39 -0500761 * or {@code null} if no default network is currently active
Jeff Sharkey9f7cbf02012-04-12 18:34:54 -0700762 */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800763 public NetworkInfo getActiveNetworkInfo() {
764 try {
765 return mService.getActiveNetworkInfo();
766 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -0700767 throw e.rethrowFromSystemServer();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800768 }
769 }
770
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800771 /**
Paul Jensen31a94f42015-02-13 14:18:39 -0500772 * Returns a {@link Network} object corresponding to the currently active
773 * default data network. In the event that the current active default data
774 * network disconnects, the returned {@code Network} object will no longer
775 * be usable. This will return {@code null} when there is no default
776 * network.
Paul Jensenb2748922015-05-06 11:10:18 -0400777 * <p>This method requires the caller to hold the permission
778 * {@link android.Manifest.permission#ACCESS_NETWORK_STATE}.
Paul Jensen31a94f42015-02-13 14:18:39 -0500779 *
780 * @return a {@link Network} object for the current default network or
781 * {@code null} if no default network is currently active
Paul Jensen31a94f42015-02-13 14:18:39 -0500782 */
783 public Network getActiveNetwork() {
784 try {
785 return mService.getActiveNetwork();
786 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -0700787 throw e.rethrowFromSystemServer();
Paul Jensen31a94f42015-02-13 14:18:39 -0500788 }
789 }
790
791 /**
Robin Leed2baf792016-03-24 12:07:00 +0000792 * Returns a {@link Network} object corresponding to the currently active
793 * default data network for a specific UID. In the event that the default data
794 * network disconnects, the returned {@code Network} object will no longer
795 * be usable. This will return {@code null} when there is no default
796 * network for the UID.
797 * <p>This method requires the caller to hold the permission
798 * {@link android.Manifest.permission#CONNECTIVITY_INTERNAL}.
799 *
800 * @return a {@link Network} object for the current default network for the
801 * given UID or {@code null} if no default network is currently active
802 *
803 * @hide
804 */
805 public Network getActiveNetworkForUid(int uid) {
Jeff Sharkey1b6519b2016-04-28 15:33:18 -0600806 return getActiveNetworkForUid(uid, false);
807 }
808
809 /** {@hide} */
810 public Network getActiveNetworkForUid(int uid, boolean ignoreBlocked) {
Robin Leed2baf792016-03-24 12:07:00 +0000811 try {
Jeff Sharkey1b6519b2016-04-28 15:33:18 -0600812 return mService.getActiveNetworkForUid(uid, ignoreBlocked);
Robin Leed2baf792016-03-24 12:07:00 +0000813 } catch (RemoteException e) {
814 throw e.rethrowFromSystemServer();
815 }
816 }
817
818 /**
Robin Lee244ce8e2016-01-05 18:03:46 +0000819 * Configures an always-on VPN connection through a specific application.
820 * This connection is automatically granted and persisted after a reboot.
821 *
822 * <p>The designated package should declare a {@link VpnService} in its
823 * manifest guarded by {@link android.Manifest.permission.BIND_VPN_SERVICE},
824 * otherwise the call will fail.
825 *
826 * @param userId The identifier of the user to set an always-on VPN for.
827 * @param vpnPackage The package name for an installed VPN app on the device, or {@code null}
828 * to remove an existing always-on VPN configuration.
Robin Leedc679712016-05-03 13:23:03 +0100829 * @param lockdownEnabled {@code true} to disallow networking when the VPN is not connected or
830 * {@code false} otherwise.
Robin Lee244ce8e2016-01-05 18:03:46 +0000831 * @return {@code true} if the package is set as always-on VPN controller;
832 * {@code false} otherwise.
833 * @hide
834 */
Robin Leedc679712016-05-03 13:23:03 +0100835 public boolean setAlwaysOnVpnPackageForUser(int userId, @Nullable String vpnPackage,
836 boolean lockdownEnabled) {
Robin Lee244ce8e2016-01-05 18:03:46 +0000837 try {
Robin Leedc679712016-05-03 13:23:03 +0100838 return mService.setAlwaysOnVpnPackage(userId, vpnPackage, lockdownEnabled);
Robin Lee244ce8e2016-01-05 18:03:46 +0000839 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -0700840 throw e.rethrowFromSystemServer();
Robin Lee244ce8e2016-01-05 18:03:46 +0000841 }
842 }
843
844 /**
845 * Returns the package name of the currently set always-on VPN application.
846 * If there is no always-on VPN set, or the VPN is provided by the system instead
847 * of by an app, {@code null} will be returned.
848 *
849 * @return Package name of VPN controller responsible for always-on VPN,
850 * or {@code null} if none is set.
851 * @hide
852 */
853 public String getAlwaysOnVpnPackageForUser(int userId) {
854 try {
855 return mService.getAlwaysOnVpnPackage(userId);
856 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -0700857 throw e.rethrowFromSystemServer();
Robin Lee244ce8e2016-01-05 18:03:46 +0000858 }
859 }
860
861 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800862 * Returns details about the currently active default data network
863 * for a given uid. This is for internal use only to avoid spying
864 * other apps.
Paul Jensenb2748922015-05-06 11:10:18 -0400865 * <p>This method requires the caller to hold the permission
866 * {@link android.Manifest.permission#CONNECTIVITY_INTERNAL}
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800867 *
868 * @return a {@link NetworkInfo} object for the current default network
869 * for the given uid or {@code null} if no default network is
870 * available for the specified uid.
871 *
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800872 * {@hide}
873 */
Jeff Sharkeyc006f1a2011-05-19 17:12:49 -0700874 public NetworkInfo getActiveNetworkInfoForUid(int uid) {
Jeff Sharkey1b6519b2016-04-28 15:33:18 -0600875 return getActiveNetworkInfoForUid(uid, false);
876 }
877
878 /** {@hide} */
879 public NetworkInfo getActiveNetworkInfoForUid(int uid, boolean ignoreBlocked) {
Jeff Sharkeyc006f1a2011-05-19 17:12:49 -0700880 try {
Jeff Sharkey1b6519b2016-04-28 15:33:18 -0600881 return mService.getActiveNetworkInfoForUid(uid, ignoreBlocked);
Jeff Sharkeyc006f1a2011-05-19 17:12:49 -0700882 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -0700883 throw e.rethrowFromSystemServer();
Jeff Sharkeyc006f1a2011-05-19 17:12:49 -0700884 }
885 }
886
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800887 /**
888 * Returns connection status information about a particular
889 * network type.
Paul Jensenb2748922015-05-06 11:10:18 -0400890 * <p>This method requires the caller to hold the permission
891 * {@link android.Manifest.permission#ACCESS_NETWORK_STATE}.
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800892 *
893 * @param networkType integer specifying which networkType in
894 * which you're interested.
895 * @return a {@link NetworkInfo} object for the requested
896 * network type or {@code null} if the type is not
897 * supported by the device.
898 *
Paul Jensen3541e9f2015-03-18 12:23:02 -0400899 * @deprecated This method does not support multiple connected networks
900 * of the same type. Use {@link #getAllNetworks} and
901 * {@link #getNetworkInfo(android.net.Network)} instead.
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800902 */
Aurimas Liutikas514c5ef2016-05-24 15:22:55 -0700903 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800904 public NetworkInfo getNetworkInfo(int networkType) {
905 try {
906 return mService.getNetworkInfo(networkType);
907 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -0700908 throw e.rethrowFromSystemServer();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800909 }
910 }
911
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800912 /**
Robert Greenwalt73b6cbae2014-06-23 11:40:00 -0700913 * Returns connection status information about a particular
914 * Network.
Paul Jensenb2748922015-05-06 11:10:18 -0400915 * <p>This method requires the caller to hold the permission
916 * {@link android.Manifest.permission#ACCESS_NETWORK_STATE}.
Robert Greenwalt73b6cbae2014-06-23 11:40:00 -0700917 *
918 * @param network {@link Network} specifying which network
919 * in which you're interested.
920 * @return a {@link NetworkInfo} object for the requested
921 * network or {@code null} if the {@code Network}
922 * is not valid.
Robert Greenwalt73b6cbae2014-06-23 11:40:00 -0700923 */
924 public NetworkInfo getNetworkInfo(Network network) {
Jeff Sharkey1b6519b2016-04-28 15:33:18 -0600925 return getNetworkInfoForUid(network, Process.myUid(), false);
926 }
927
928 /** {@hide} */
929 public NetworkInfo getNetworkInfoForUid(Network network, int uid, boolean ignoreBlocked) {
Robert Greenwalt73b6cbae2014-06-23 11:40:00 -0700930 try {
Jeff Sharkey1b6519b2016-04-28 15:33:18 -0600931 return mService.getNetworkInfoForUid(network, uid, ignoreBlocked);
Robert Greenwalt73b6cbae2014-06-23 11:40:00 -0700932 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -0700933 throw e.rethrowFromSystemServer();
Robert Greenwalt73b6cbae2014-06-23 11:40:00 -0700934 }
935 }
936
937 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800938 * Returns connection status information about all network
939 * types supported by the device.
Paul Jensenb2748922015-05-06 11:10:18 -0400940 * <p>This method requires the caller to hold the permission
941 * {@link android.Manifest.permission#ACCESS_NETWORK_STATE}.
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800942 *
943 * @return an array of {@link NetworkInfo} objects. Check each
944 * {@link NetworkInfo#getType} for which type each applies.
945 *
Paul Jensen3541e9f2015-03-18 12:23:02 -0400946 * @deprecated This method does not support multiple connected networks
947 * of the same type. Use {@link #getAllNetworks} and
948 * {@link #getNetworkInfo(android.net.Network)} instead.
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800949 */
Aurimas Liutikas514c5ef2016-05-24 15:22:55 -0700950 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800951 public NetworkInfo[] getAllNetworkInfo() {
952 try {
953 return mService.getAllNetworkInfo();
954 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -0700955 throw e.rethrowFromSystemServer();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800956 }
957 }
958
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800959 /**
Lorenzo Colittib57edc52014-08-22 17:10:50 -0700960 * Returns the {@link Network} object currently serving a given type, or
961 * null if the given type is not connected.
962 *
963 * <p>This method requires the caller to hold the permission
964 * {@link android.Manifest.permission#ACCESS_NETWORK_STATE}.
965 *
966 * @hide
Paul Jensen3541e9f2015-03-18 12:23:02 -0400967 * @deprecated This method does not support multiple connected networks
968 * of the same type. Use {@link #getAllNetworks} and
969 * {@link #getNetworkInfo(android.net.Network)} instead.
Lorenzo Colittib57edc52014-08-22 17:10:50 -0700970 */
Aurimas Liutikas514c5ef2016-05-24 15:22:55 -0700971 @Deprecated
Lorenzo Colittib57edc52014-08-22 17:10:50 -0700972 public Network getNetworkForType(int networkType) {
973 try {
974 return mService.getNetworkForType(networkType);
975 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -0700976 throw e.rethrowFromSystemServer();
Lorenzo Colittib57edc52014-08-22 17:10:50 -0700977 }
978 }
979
980 /**
Robert Greenwalt73b6cbae2014-06-23 11:40:00 -0700981 * Returns an array of all {@link Network} currently tracked by the
982 * framework.
Robert Greenwalt73b6cbae2014-06-23 11:40:00 -0700983 * <p>This method requires the caller to hold the permission
984 * {@link android.Manifest.permission#ACCESS_NETWORK_STATE}.
Paul Jensenb2748922015-05-06 11:10:18 -0400985 *
986 * @return an array of {@link Network} objects.
Robert Greenwalt73b6cbae2014-06-23 11:40:00 -0700987 */
988 public Network[] getAllNetworks() {
989 try {
990 return mService.getAllNetworks();
991 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -0700992 throw e.rethrowFromSystemServer();
Robert Greenwalt73b6cbae2014-06-23 11:40:00 -0700993 }
994 }
995
996 /**
Lorenzo Colittie285b432015-04-23 15:32:42 +0900997 * Returns an array of {@link android.net.NetworkCapabilities} objects, representing
Lorenzo Colitti403aa262014-11-28 11:21:30 +0900998 * the Networks that applications run by the given user will use by default.
999 * @hide
1000 */
1001 public NetworkCapabilities[] getDefaultNetworkCapabilitiesForUser(int userId) {
1002 try {
1003 return mService.getDefaultNetworkCapabilitiesForUser(userId);
1004 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07001005 throw e.rethrowFromSystemServer();
Lorenzo Colitti403aa262014-11-28 11:21:30 +09001006 }
1007 }
1008
1009 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08001010 * Returns the IP information for the current default network.
Paul Jensenb2748922015-05-06 11:10:18 -04001011 * <p>This method requires the caller to hold the permission
1012 * {@link android.Manifest.permission#ACCESS_NETWORK_STATE}.
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08001013 *
1014 * @return a {@link LinkProperties} object describing the IP info
1015 * for the current default network, or {@code null} if there
1016 * is no current default network.
1017 *
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08001018 * {@hide}
1019 */
Robert Greenwaltd192dad2010-09-14 09:18:02 -07001020 public LinkProperties getActiveLinkProperties() {
1021 try {
1022 return mService.getActiveLinkProperties();
1023 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07001024 throw e.rethrowFromSystemServer();
Robert Greenwaltd192dad2010-09-14 09:18:02 -07001025 }
1026 }
1027
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08001028 /**
1029 * Returns the IP information for a given network type.
Paul Jensenb2748922015-05-06 11:10:18 -04001030 * <p>This method requires the caller to hold the permission
1031 * {@link android.Manifest.permission#ACCESS_NETWORK_STATE}.
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08001032 *
1033 * @param networkType the network type of interest.
1034 * @return a {@link LinkProperties} object describing the IP info
1035 * for the given networkType, or {@code null} if there is
1036 * no current default network.
1037 *
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08001038 * {@hide}
Paul Jensen3541e9f2015-03-18 12:23:02 -04001039 * @deprecated This method does not support multiple connected networks
1040 * of the same type. Use {@link #getAllNetworks},
1041 * {@link #getNetworkInfo(android.net.Network)}, and
1042 * {@link #getLinkProperties(android.net.Network)} instead.
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08001043 */
Aurimas Liutikas514c5ef2016-05-24 15:22:55 -07001044 @Deprecated
Robert Greenwaltd192dad2010-09-14 09:18:02 -07001045 public LinkProperties getLinkProperties(int networkType) {
1046 try {
Robert Greenwalt9258c642014-03-26 16:47:06 -07001047 return mService.getLinkPropertiesForType(networkType);
1048 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07001049 throw e.rethrowFromSystemServer();
Robert Greenwalt9258c642014-03-26 16:47:06 -07001050 }
1051 }
1052
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07001053 /**
1054 * Get the {@link LinkProperties} for the given {@link Network}. This
1055 * will return {@code null} if the network is unknown.
Paul Jensenb2748922015-05-06 11:10:18 -04001056 * <p>This method requires the caller to hold the permission
1057 * {@link android.Manifest.permission#ACCESS_NETWORK_STATE}.
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07001058 *
1059 * @param network The {@link Network} object identifying the network in question.
1060 * @return The {@link LinkProperties} for the network, or {@code null}.
Paul Jensenb2748922015-05-06 11:10:18 -04001061 */
Robert Greenwalt9258c642014-03-26 16:47:06 -07001062 public LinkProperties getLinkProperties(Network network) {
1063 try {
1064 return mService.getLinkProperties(network);
1065 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07001066 throw e.rethrowFromSystemServer();
Robert Greenwalt9258c642014-03-26 16:47:06 -07001067 }
1068 }
1069
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07001070 /**
Erik Klineacdd6392016-07-07 16:50:58 +09001071 * Request that this callback be invoked at ConnectivityService's earliest
1072 * convenience with the current satisfying network's LinkProperties.
1073 * If no such network exists no callback invocation is performed.
1074 *
1075 * The callback must have been registered with #requestNetwork() or
1076 * #registerDefaultNetworkCallback(); callbacks registered with
1077 * registerNetworkCallback() are not specific to any particular Network so
1078 * do not cause any updates.
1079 *
1080 * @hide
1081 */
1082 public void requestLinkProperties(NetworkCallback networkCallback) {
1083 try {
1084 mService.requestLinkProperties(networkCallback.networkRequest);
1085 } catch (RemoteException e) {
1086 throw e.rethrowFromSystemServer();
1087 }
1088 }
1089
1090 /**
Lorenzo Colittie285b432015-04-23 15:32:42 +09001091 * Get the {@link android.net.NetworkCapabilities} for the given {@link Network}. This
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07001092 * will return {@code null} if the network is unknown.
Paul Jensenb2748922015-05-06 11:10:18 -04001093 * <p>This method requires the caller to hold the permission
1094 * {@link android.Manifest.permission#ACCESS_NETWORK_STATE}.
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07001095 *
1096 * @param network The {@link Network} object identifying the network in question.
Lorenzo Colittie285b432015-04-23 15:32:42 +09001097 * @return The {@link android.net.NetworkCapabilities} for the network, or {@code null}.
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07001098 */
Robert Greenwalt9258c642014-03-26 16:47:06 -07001099 public NetworkCapabilities getNetworkCapabilities(Network network) {
1100 try {
1101 return mService.getNetworkCapabilities(network);
Robert Greenwaltd192dad2010-09-14 09:18:02 -07001102 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07001103 throw e.rethrowFromSystemServer();
Robert Greenwaltd192dad2010-09-14 09:18:02 -07001104 }
1105 }
1106
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08001107 /**
Erik Klineacdd6392016-07-07 16:50:58 +09001108 * Request that this callback be invoked at ConnectivityService's earliest
1109 * convenience with the current satisfying network's NetworkCapabilities.
1110 * If no such network exists no callback invocation is performed.
1111 *
1112 * The callback must have been registered with #requestNetwork() or
1113 * #registerDefaultNetworkCallback(); callbacks registered with
1114 * registerNetworkCallback() are not specific to any particular Network so
1115 * do not cause any updates.
1116 *
1117 * @hide
1118 */
1119 public void requestNetworkCapabilities(NetworkCallback networkCallback) {
1120 try {
1121 mService.requestNetworkCapabilities(networkCallback.networkRequest);
1122 } catch (RemoteException e) {
1123 throw e.rethrowFromSystemServer();
1124 }
1125 }
1126
1127 /**
Udam Sainib7c24872016-01-04 12:16:14 -08001128 * Gets the URL that should be used for resolving whether a captive portal is present.
1129 * 1. This URL should respond with a 204 response to a GET request to indicate no captive
1130 * portal is present.
1131 * 2. This URL must be HTTP as redirect responses are used to find captive portal
1132 * sign-in pages. Captive portals cannot respond to HTTPS requests with redirects.
1133 *
1134 * @hide
1135 */
1136 @SystemApi
1137 public String getCaptivePortalServerUrl() {
1138 try {
1139 return mService.getCaptivePortalServerUrl();
1140 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07001141 throw e.rethrowFromSystemServer();
Udam Sainib7c24872016-01-04 12:16:14 -08001142 }
1143 }
1144
1145 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001146 * Tells the underlying networking system that the caller wants to
1147 * begin using the named feature. The interpretation of {@code feature}
1148 * is completely up to each networking implementation.
Lorenzo Colittid5427052015-10-15 16:29:00 +09001149 *
1150 * <p>This method requires the caller to hold either the
1151 * {@link android.Manifest.permission#CHANGE_NETWORK_STATE} permission
1152 * or the ability to modify system settings as determined by
1153 * {@link android.provider.Settings.System#canWrite}.</p>
1154 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001155 * @param networkType specifies which network the request pertains to
1156 * @param feature the name of the feature to be used
1157 * @return an integer value representing the outcome of the request.
1158 * The interpretation of this value is specific to each networking
1159 * implementation+feature combination, except that the value {@code -1}
1160 * always indicates failure.
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07001161 *
Lorenzo Colitti2ea89e52015-04-24 17:03:31 +09001162 * @deprecated Deprecated in favor of the cleaner
1163 * {@link #requestNetwork(NetworkRequest, NetworkCallback)} API.
Dianne Hackborn692a2442015-07-31 10:35:34 -07001164 * In {@link VERSION_CODES#M}, and above, this method is unsupported and will
Lorenzo Colittiffc42b02015-07-29 11:41:21 +09001165 * throw {@code UnsupportedOperationException} if called.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001166 */
Aurimas Liutikas514c5ef2016-05-24 15:22:55 -07001167 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001168 public int startUsingNetworkFeature(int networkType, String feature) {
Lorenzo Colittiffc42b02015-07-29 11:41:21 +09001169 checkLegacyRoutingApiAccess();
Robert Greenwalt562cc542014-05-15 18:07:26 -07001170 NetworkCapabilities netCap = networkCapabilitiesForFeature(networkType, feature);
1171 if (netCap == null) {
1172 Log.d(TAG, "Can't satisfy startUsingNetworkFeature for " + networkType + ", " +
1173 feature);
1174 return PhoneConstants.APN_REQUEST_FAILED;
1175 }
1176
1177 NetworkRequest request = null;
1178 synchronized (sLegacyRequests) {
1179 LegacyRequest l = sLegacyRequests.get(netCap);
1180 if (l != null) {
1181 Log.d(TAG, "renewing startUsingNetworkFeature request " + l.networkRequest);
1182 renewRequestLocked(l);
1183 if (l.currentNetwork != null) {
1184 return PhoneConstants.APN_ALREADY_ACTIVE;
1185 } else {
1186 return PhoneConstants.APN_REQUEST_STARTED;
1187 }
1188 }
1189
1190 request = requestNetworkForFeatureLocked(netCap);
1191 }
1192 if (request != null) {
Robert Greenwalt257ee5f2014-06-20 10:58:45 -07001193 Log.d(TAG, "starting startUsingNetworkFeature for request " + request);
Robert Greenwalt562cc542014-05-15 18:07:26 -07001194 return PhoneConstants.APN_REQUEST_STARTED;
1195 } else {
1196 Log.d(TAG, " request Failed");
1197 return PhoneConstants.APN_REQUEST_FAILED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001198 }
1199 }
1200
1201 /**
1202 * Tells the underlying networking system that the caller is finished
1203 * using the named feature. The interpretation of {@code feature}
1204 * is completely up to each networking implementation.
Lorenzo Colittid5427052015-10-15 16:29:00 +09001205 *
1206 * <p>This method requires the caller to hold either the
1207 * {@link android.Manifest.permission#CHANGE_NETWORK_STATE} permission
1208 * or the ability to modify system settings as determined by
1209 * {@link android.provider.Settings.System#canWrite}.</p>
1210 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001211 * @param networkType specifies which network the request pertains to
1212 * @param feature the name of the feature that is no longer needed
1213 * @return an integer value representing the outcome of the request.
1214 * The interpretation of this value is specific to each networking
1215 * implementation+feature combination, except that the value {@code -1}
1216 * always indicates failure.
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07001217 *
Lorenzo Colitti88bc0bb2016-04-13 22:00:02 +09001218 * @deprecated Deprecated in favor of the cleaner
1219 * {@link #unregisterNetworkCallback(NetworkCallback)} API.
Dianne Hackborn692a2442015-07-31 10:35:34 -07001220 * In {@link VERSION_CODES#M}, and above, this method is unsupported and will
Lorenzo Colittiffc42b02015-07-29 11:41:21 +09001221 * throw {@code UnsupportedOperationException} if called.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001222 */
Aurimas Liutikas514c5ef2016-05-24 15:22:55 -07001223 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001224 public int stopUsingNetworkFeature(int networkType, String feature) {
Lorenzo Colittiffc42b02015-07-29 11:41:21 +09001225 checkLegacyRoutingApiAccess();
Robert Greenwalt562cc542014-05-15 18:07:26 -07001226 NetworkCapabilities netCap = networkCapabilitiesForFeature(networkType, feature);
1227 if (netCap == null) {
1228 Log.d(TAG, "Can't satisfy stopUsingNetworkFeature for " + networkType + ", " +
1229 feature);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001230 return -1;
1231 }
Robert Greenwalt562cc542014-05-15 18:07:26 -07001232
Paul Jensen9ffb53c2014-12-17 10:39:34 -05001233 if (removeRequestForFeature(netCap)) {
Robert Greenwalt562cc542014-05-15 18:07:26 -07001234 Log.d(TAG, "stopUsingNetworkFeature for " + networkType + ", " + feature);
Robert Greenwalt562cc542014-05-15 18:07:26 -07001235 }
1236 return 1;
1237 }
1238
1239 private NetworkCapabilities networkCapabilitiesForFeature(int networkType, String feature) {
1240 if (networkType == TYPE_MOBILE) {
1241 int cap = -1;
1242 if ("enableMMS".equals(feature)) {
1243 cap = NetworkCapabilities.NET_CAPABILITY_MMS;
1244 } else if ("enableSUPL".equals(feature)) {
1245 cap = NetworkCapabilities.NET_CAPABILITY_SUPL;
1246 } else if ("enableDUN".equals(feature) || "enableDUNAlways".equals(feature)) {
1247 cap = NetworkCapabilities.NET_CAPABILITY_DUN;
1248 } else if ("enableHIPRI".equals(feature)) {
1249 cap = NetworkCapabilities.NET_CAPABILITY_INTERNET;
1250 } else if ("enableFOTA".equals(feature)) {
1251 cap = NetworkCapabilities.NET_CAPABILITY_FOTA;
1252 } else if ("enableIMS".equals(feature)) {
1253 cap = NetworkCapabilities.NET_CAPABILITY_IMS;
1254 } else if ("enableCBS".equals(feature)) {
1255 cap = NetworkCapabilities.NET_CAPABILITY_CBS;
1256 } else {
1257 return null;
1258 }
1259 NetworkCapabilities netCap = new NetworkCapabilities();
Robert Greenwalt7569f182014-06-08 16:42:59 -07001260 netCap.addTransportType(NetworkCapabilities.TRANSPORT_CELLULAR).addCapability(cap);
Paul Jensen487ffe72015-07-24 15:57:11 -04001261 netCap.maybeMarkCapabilitiesRestricted();
Robert Greenwalt562cc542014-05-15 18:07:26 -07001262 return netCap;
1263 } else if (networkType == TYPE_WIFI) {
1264 if ("p2p".equals(feature)) {
1265 NetworkCapabilities netCap = new NetworkCapabilities();
1266 netCap.addTransportType(NetworkCapabilities.TRANSPORT_WIFI);
Robert Greenwalt7569f182014-06-08 16:42:59 -07001267 netCap.addCapability(NetworkCapabilities.NET_CAPABILITY_WIFI_P2P);
Paul Jensen487ffe72015-07-24 15:57:11 -04001268 netCap.maybeMarkCapabilitiesRestricted();
Robert Greenwalt562cc542014-05-15 18:07:26 -07001269 return netCap;
1270 }
1271 }
1272 return null;
1273 }
1274
Robert Greenwalt06314e42014-10-29 14:04:06 -07001275 /**
1276 * Guess what the network request was trying to say so that the resulting
1277 * network is accessible via the legacy (deprecated) API such as
1278 * requestRouteToHost.
Lorenzo Colittidef4cb02015-11-25 20:28:50 +09001279 *
1280 * This means we should try to be fairly precise about transport and
Robert Greenwalt06314e42014-10-29 14:04:06 -07001281 * capability but ignore things such as networkSpecifier.
1282 * If the request has more than one transport or capability it doesn't
1283 * match the old legacy requests (they selected only single transport/capability)
1284 * so this function cannot map the request to a single legacy type and
1285 * the resulting network will not be available to the legacy APIs.
1286 *
Lorenzo Colittidef4cb02015-11-25 20:28:50 +09001287 * This code is only called from the requestNetwork API (L and above).
1288 *
1289 * Setting a legacy type causes CONNECTIVITY_ACTION broadcasts, which are expensive
1290 * because they wake up lots of apps - see http://b/23350688 . So we currently only
1291 * do this for SUPL requests, which are the only ones that we know need it. If
1292 * omitting these broadcasts causes unacceptable app breakage, then for backwards
1293 * compatibility we can send them:
1294 *
1295 * if (targetSdkVersion < Build.VERSION_CODES.M) && // legacy API unsupported >= M
1296 * targetSdkVersion >= Build.VERSION_CODES.LOLLIPOP)) // requestNetwork not present < L
1297 *
Robert Greenwalt06314e42014-10-29 14:04:06 -07001298 * TODO - This should be removed when the legacy APIs are removed.
1299 */
Ye Wenb87875e2014-07-21 14:19:01 -07001300 private int inferLegacyTypeForNetworkCapabilities(NetworkCapabilities netCap) {
1301 if (netCap == null) {
1302 return TYPE_NONE;
1303 }
Robert Greenwalt06314e42014-10-29 14:04:06 -07001304
Ye Wenb87875e2014-07-21 14:19:01 -07001305 if (!netCap.hasTransport(NetworkCapabilities.TRANSPORT_CELLULAR)) {
1306 return TYPE_NONE;
1307 }
Robert Greenwalt06314e42014-10-29 14:04:06 -07001308
Lifu Tang30f95a72016-01-07 23:20:38 -08001309 // Do this only for SUPL, until GnssLocationProvider is fixed. http://b/25876485 .
Lorenzo Colittidef4cb02015-11-25 20:28:50 +09001310 if (!netCap.hasCapability(NetworkCapabilities.NET_CAPABILITY_SUPL)) {
1311 // NOTE: if this causes app breakage, we should not just comment out this early return;
1312 // instead, we should make this early return conditional on the requesting app's target
1313 // SDK version, as described in the comment above.
1314 return TYPE_NONE;
1315 }
1316
Robert Greenwalt06314e42014-10-29 14:04:06 -07001317 String type = null;
1318 int result = TYPE_NONE;
1319
Ye Wenb87875e2014-07-21 14:19:01 -07001320 if (netCap.hasCapability(NetworkCapabilities.NET_CAPABILITY_CBS)) {
Robert Greenwalt06314e42014-10-29 14:04:06 -07001321 type = "enableCBS";
1322 result = TYPE_MOBILE_CBS;
1323 } else if (netCap.hasCapability(NetworkCapabilities.NET_CAPABILITY_IMS)) {
1324 type = "enableIMS";
1325 result = TYPE_MOBILE_IMS;
1326 } else if (netCap.hasCapability(NetworkCapabilities.NET_CAPABILITY_FOTA)) {
1327 type = "enableFOTA";
1328 result = TYPE_MOBILE_FOTA;
1329 } else if (netCap.hasCapability(NetworkCapabilities.NET_CAPABILITY_DUN)) {
1330 type = "enableDUN";
1331 result = TYPE_MOBILE_DUN;
1332 } else if (netCap.hasCapability(NetworkCapabilities.NET_CAPABILITY_SUPL)) {
Lorenzo Colittidef4cb02015-11-25 20:28:50 +09001333 type = "enableSUPL";
Robert Greenwalt06314e42014-10-29 14:04:06 -07001334 result = TYPE_MOBILE_SUPL;
Robert Greenwalt74ab4fa2015-08-28 12:37:54 -07001335 // back out this hack for mms as they no longer need this and it's causing
1336 // device slowdowns - b/23350688 (note, supl still needs this)
1337 //} else if (netCap.hasCapability(NetworkCapabilities.NET_CAPABILITY_MMS)) {
1338 // type = "enableMMS";
1339 // result = TYPE_MOBILE_MMS;
Robert Greenwalt06314e42014-10-29 14:04:06 -07001340 } else if (netCap.hasCapability(NetworkCapabilities.NET_CAPABILITY_INTERNET)) {
1341 type = "enableHIPRI";
1342 result = TYPE_MOBILE_HIPRI;
Ye Wenb87875e2014-07-21 14:19:01 -07001343 }
Robert Greenwalt06314e42014-10-29 14:04:06 -07001344 if (type != null) {
1345 NetworkCapabilities testCap = networkCapabilitiesForFeature(TYPE_MOBILE, type);
1346 if (testCap.equalsNetCapabilities(netCap) && testCap.equalsTransportTypes(netCap)) {
1347 return result;
Ye Wenb87875e2014-07-21 14:19:01 -07001348 }
1349 }
1350 return TYPE_NONE;
1351 }
1352
Robert Greenwalt32aa65a2014-06-02 15:32:02 -07001353 private int legacyTypeForNetworkCapabilities(NetworkCapabilities netCap) {
Robert Greenwalt562cc542014-05-15 18:07:26 -07001354 if (netCap == null) return TYPE_NONE;
1355 if (netCap.hasCapability(NetworkCapabilities.NET_CAPABILITY_CBS)) {
1356 return TYPE_MOBILE_CBS;
1357 }
1358 if (netCap.hasCapability(NetworkCapabilities.NET_CAPABILITY_IMS)) {
1359 return TYPE_MOBILE_IMS;
1360 }
1361 if (netCap.hasCapability(NetworkCapabilities.NET_CAPABILITY_FOTA)) {
1362 return TYPE_MOBILE_FOTA;
1363 }
1364 if (netCap.hasCapability(NetworkCapabilities.NET_CAPABILITY_DUN)) {
1365 return TYPE_MOBILE_DUN;
1366 }
1367 if (netCap.hasCapability(NetworkCapabilities.NET_CAPABILITY_SUPL)) {
1368 return TYPE_MOBILE_SUPL;
1369 }
1370 if (netCap.hasCapability(NetworkCapabilities.NET_CAPABILITY_MMS)) {
1371 return TYPE_MOBILE_MMS;
1372 }
1373 if (netCap.hasCapability(NetworkCapabilities.NET_CAPABILITY_INTERNET)) {
1374 return TYPE_MOBILE_HIPRI;
1375 }
Robert Greenwalt32aa65a2014-06-02 15:32:02 -07001376 if (netCap.hasCapability(NetworkCapabilities.NET_CAPABILITY_WIFI_P2P)) {
1377 return TYPE_WIFI_P2P;
1378 }
Robert Greenwalt562cc542014-05-15 18:07:26 -07001379 return TYPE_NONE;
1380 }
1381
1382 private static class LegacyRequest {
1383 NetworkCapabilities networkCapabilities;
1384 NetworkRequest networkRequest;
1385 int expireSequenceNumber;
1386 Network currentNetwork;
1387 int delay = -1;
Paul Jensen9ffb53c2014-12-17 10:39:34 -05001388
1389 private void clearDnsBinding() {
1390 if (currentNetwork != null) {
1391 currentNetwork = null;
1392 setProcessDefaultNetworkForHostResolution(null);
1393 }
1394 }
1395
Robert Greenwalt6078b502014-06-11 16:05:07 -07001396 NetworkCallback networkCallback = new NetworkCallback() {
Robert Greenwalt562cc542014-05-15 18:07:26 -07001397 @Override
Robert Greenwalt6078b502014-06-11 16:05:07 -07001398 public void onAvailable(Network network) {
Robert Greenwalt562cc542014-05-15 18:07:26 -07001399 currentNetwork = network;
1400 Log.d(TAG, "startUsingNetworkFeature got Network:" + network);
Paul Jensen6d3ff9e2014-05-29 10:12:39 -04001401 setProcessDefaultNetworkForHostResolution(network);
Robert Greenwalt562cc542014-05-15 18:07:26 -07001402 }
1403 @Override
Robert Greenwalt6078b502014-06-11 16:05:07 -07001404 public void onLost(Network network) {
Paul Jensen9ffb53c2014-12-17 10:39:34 -05001405 if (network.equals(currentNetwork)) clearDnsBinding();
Robert Greenwalt562cc542014-05-15 18:07:26 -07001406 Log.d(TAG, "startUsingNetworkFeature lost Network:" + network);
1407 }
1408 };
1409 }
1410
Robert Greenwaltfab501672014-07-23 11:44:01 -07001411 private static HashMap<NetworkCapabilities, LegacyRequest> sLegacyRequests =
Robert Greenwalt562cc542014-05-15 18:07:26 -07001412 new HashMap<NetworkCapabilities, LegacyRequest>();
1413
1414 private NetworkRequest findRequestForFeature(NetworkCapabilities netCap) {
1415 synchronized (sLegacyRequests) {
1416 LegacyRequest l = sLegacyRequests.get(netCap);
1417 if (l != null) return l.networkRequest;
1418 }
1419 return null;
1420 }
1421
1422 private void renewRequestLocked(LegacyRequest l) {
1423 l.expireSequenceNumber++;
1424 Log.d(TAG, "renewing request to seqNum " + l.expireSequenceNumber);
1425 sendExpireMsgForFeature(l.networkCapabilities, l.expireSequenceNumber, l.delay);
1426 }
1427
1428 private void expireRequest(NetworkCapabilities netCap, int sequenceNum) {
1429 int ourSeqNum = -1;
1430 synchronized (sLegacyRequests) {
1431 LegacyRequest l = sLegacyRequests.get(netCap);
1432 if (l == null) return;
1433 ourSeqNum = l.expireSequenceNumber;
Paul Jensen9ffb53c2014-12-17 10:39:34 -05001434 if (l.expireSequenceNumber == sequenceNum) removeRequestForFeature(netCap);
Robert Greenwalt562cc542014-05-15 18:07:26 -07001435 }
1436 Log.d(TAG, "expireRequest with " + ourSeqNum + ", " + sequenceNum);
1437 }
1438
1439 private NetworkRequest requestNetworkForFeatureLocked(NetworkCapabilities netCap) {
1440 int delay = -1;
Robert Greenwalt32aa65a2014-06-02 15:32:02 -07001441 int type = legacyTypeForNetworkCapabilities(netCap);
Robert Greenwalt562cc542014-05-15 18:07:26 -07001442 try {
1443 delay = mService.getRestoreDefaultNetworkDelay(type);
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07001444 } catch (RemoteException e) {
1445 throw e.rethrowFromSystemServer();
1446 }
Robert Greenwalt562cc542014-05-15 18:07:26 -07001447 LegacyRequest l = new LegacyRequest();
1448 l.networkCapabilities = netCap;
1449 l.delay = delay;
1450 l.expireSequenceNumber = 0;
Robert Greenwalt6078b502014-06-11 16:05:07 -07001451 l.networkRequest = sendRequestForNetwork(netCap, l.networkCallback, 0,
Robert Greenwalt32aa65a2014-06-02 15:32:02 -07001452 REQUEST, type);
Robert Greenwalt562cc542014-05-15 18:07:26 -07001453 if (l.networkRequest == null) return null;
1454 sLegacyRequests.put(netCap, l);
1455 sendExpireMsgForFeature(netCap, l.expireSequenceNumber, delay);
1456 return l.networkRequest;
1457 }
1458
1459 private void sendExpireMsgForFeature(NetworkCapabilities netCap, int seqNum, int delay) {
1460 if (delay >= 0) {
1461 Log.d(TAG, "sending expire msg with seqNum " + seqNum + " and delay " + delay);
1462 Message msg = sCallbackHandler.obtainMessage(EXPIRE_LEGACY_REQUEST, seqNum, 0, netCap);
1463 sCallbackHandler.sendMessageDelayed(msg, delay);
1464 }
1465 }
1466
Paul Jensen9ffb53c2014-12-17 10:39:34 -05001467 private boolean removeRequestForFeature(NetworkCapabilities netCap) {
1468 final LegacyRequest l;
Robert Greenwalt562cc542014-05-15 18:07:26 -07001469 synchronized (sLegacyRequests) {
Paul Jensen9ffb53c2014-12-17 10:39:34 -05001470 l = sLegacyRequests.remove(netCap);
Robert Greenwalt562cc542014-05-15 18:07:26 -07001471 }
Paul Jensen9ffb53c2014-12-17 10:39:34 -05001472 if (l == null) return false;
1473 unregisterNetworkCallback(l.networkCallback);
1474 l.clearDnsBinding();
1475 return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001476 }
1477
Lorenzo Colitti8bf977d2015-06-15 14:29:22 +09001478 /** @hide */
1479 public static class PacketKeepaliveCallback {
1480 /** The requested keepalive was successfully started. */
1481 public void onStarted() {}
1482 /** The keepalive was successfully stopped. */
1483 public void onStopped() {}
1484 /** An error occurred. */
1485 public void onError(int error) {}
1486 }
1487
1488 /**
1489 * Allows applications to request that the system periodically send specific packets on their
1490 * behalf, using hardware offload to save battery power.
1491 *
1492 * To request that the system send keepalives, call one of the methods that return a
1493 * {@link ConnectivityManager.PacketKeepalive} object, such as {@link #startNattKeepalive},
1494 * passing in a non-null callback. If the callback is successfully started, the callback's
1495 * {@code onStarted} method will be called. If an error occurs, {@code onError} will be called,
1496 * specifying one of the {@code ERROR_*} constants in this class.
1497 *
1498 * To stop an existing keepalive, call {@link stop}. The system will call {@code onStopped} if
1499 * the operation was successfull or {@code onError} if an error occurred.
1500 *
1501 * @hide
1502 */
1503 public class PacketKeepalive {
1504
1505 private static final String TAG = "PacketKeepalive";
1506
1507 /** @hide */
1508 public static final int SUCCESS = 0;
1509
1510 /** @hide */
1511 public static final int NO_KEEPALIVE = -1;
1512
1513 /** @hide */
1514 public static final int BINDER_DIED = -10;
1515
1516 /** The specified {@code Network} is not connected. */
1517 public static final int ERROR_INVALID_NETWORK = -20;
1518 /** The specified IP addresses are invalid. For example, the specified source IP address is
1519 * not configured on the specified {@code Network}. */
1520 public static final int ERROR_INVALID_IP_ADDRESS = -21;
1521 /** The requested port is invalid. */
1522 public static final int ERROR_INVALID_PORT = -22;
1523 /** The packet length is invalid (e.g., too long). */
1524 public static final int ERROR_INVALID_LENGTH = -23;
1525 /** The packet transmission interval is invalid (e.g., too short). */
1526 public static final int ERROR_INVALID_INTERVAL = -24;
1527
1528 /** The hardware does not support this request. */
1529 public static final int ERROR_HARDWARE_UNSUPPORTED = -30;
Lorenzo Colitti9d1284e2015-09-08 16:46:36 +09001530 /** The hardware returned an error. */
1531 public static final int ERROR_HARDWARE_ERROR = -31;
Lorenzo Colitti8bf977d2015-06-15 14:29:22 +09001532
1533 public static final int NATT_PORT = 4500;
1534
1535 private final Network mNetwork;
1536 private final PacketKeepaliveCallback mCallback;
1537 private final Looper mLooper;
1538 private final Messenger mMessenger;
1539
1540 private volatile Integer mSlot;
1541
1542 void stopLooper() {
1543 mLooper.quit();
1544 }
1545
1546 public void stop() {
1547 try {
1548 mService.stopKeepalive(mNetwork, mSlot);
1549 } catch (RemoteException e) {
1550 Log.e(TAG, "Error stopping packet keepalive: ", e);
1551 stopLooper();
1552 }
1553 }
1554
1555 private PacketKeepalive(Network network, PacketKeepaliveCallback callback) {
1556 checkNotNull(network, "network cannot be null");
1557 checkNotNull(callback, "callback cannot be null");
1558 mNetwork = network;
1559 mCallback = callback;
1560 HandlerThread thread = new HandlerThread(TAG);
1561 thread.start();
1562 mLooper = thread.getLooper();
1563 mMessenger = new Messenger(new Handler(mLooper) {
1564 @Override
1565 public void handleMessage(Message message) {
1566 switch (message.what) {
1567 case NetworkAgent.EVENT_PACKET_KEEPALIVE:
1568 int error = message.arg2;
1569 try {
1570 if (error == SUCCESS) {
1571 if (mSlot == null) {
1572 mSlot = message.arg1;
1573 mCallback.onStarted();
1574 } else {
1575 mSlot = null;
1576 stopLooper();
1577 mCallback.onStopped();
1578 }
1579 } else {
1580 stopLooper();
1581 mCallback.onError(error);
1582 }
1583 } catch (Exception e) {
1584 Log.e(TAG, "Exception in keepalive callback(" + error + ")", e);
1585 }
1586 break;
1587 default:
1588 Log.e(TAG, "Unhandled message " + Integer.toHexString(message.what));
1589 break;
1590 }
1591 }
1592 });
1593 }
1594 }
1595
1596 /**
1597 * Starts an IPsec NAT-T keepalive packet with the specified parameters.
1598 *
1599 * @hide
1600 */
1601 public PacketKeepalive startNattKeepalive(
1602 Network network, int intervalSeconds, PacketKeepaliveCallback callback,
1603 InetAddress srcAddr, int srcPort, InetAddress dstAddr) {
1604 final PacketKeepalive k = new PacketKeepalive(network, callback);
1605 try {
1606 mService.startNattKeepalive(network, intervalSeconds, k.mMessenger, new Binder(),
1607 srcAddr.getHostAddress(), srcPort, dstAddr.getHostAddress());
1608 } catch (RemoteException e) {
1609 Log.e(TAG, "Error starting packet keepalive: ", e);
1610 k.stopLooper();
1611 return null;
1612 }
1613 return k;
1614 }
1615
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001616 /**
1617 * Ensure that a network route exists to deliver traffic to the specified
1618 * host via the specified network interface. An attempt to add a route that
1619 * already exists is ignored, but treated as successful.
Lorenzo Colittid5427052015-10-15 16:29:00 +09001620 *
1621 * <p>This method requires the caller to hold either the
1622 * {@link android.Manifest.permission#CHANGE_NETWORK_STATE} permission
1623 * or the ability to modify system settings as determined by
1624 * {@link android.provider.Settings.System#canWrite}.</p>
1625 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001626 * @param networkType the type of the network over which traffic to the specified
1627 * host is to be routed
1628 * @param hostAddress the IP address of the host to which the route is desired
1629 * @return {@code true} on success, {@code false} on failure
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07001630 *
Lorenzo Colitti2ea89e52015-04-24 17:03:31 +09001631 * @deprecated Deprecated in favor of the
1632 * {@link #requestNetwork(NetworkRequest, NetworkCallback)},
1633 * {@link #bindProcessToNetwork} and {@link Network#getSocketFactory} API.
Dianne Hackborn692a2442015-07-31 10:35:34 -07001634 * In {@link VERSION_CODES#M}, and above, this method is unsupported and will
Lorenzo Colittiffc42b02015-07-29 11:41:21 +09001635 * throw {@code UnsupportedOperationException} if called.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001636 */
Aurimas Liutikas514c5ef2016-05-24 15:22:55 -07001637 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001638 public boolean requestRouteToHost(int networkType, int hostAddress) {
Sreeram Ramachandran03666c72014-07-19 23:21:46 -07001639 return requestRouteToHostAddress(networkType, NetworkUtils.intToInetAddress(hostAddress));
Robert Greenwalt585ac0f2010-08-27 09:24:29 -07001640 }
1641
1642 /**
1643 * Ensure that a network route exists to deliver traffic to the specified
1644 * host via the specified network interface. An attempt to add a route that
1645 * already exists is ignored, but treated as successful.
Lorenzo Colittid5427052015-10-15 16:29:00 +09001646 *
1647 * <p>This method requires the caller to hold either the
1648 * {@link android.Manifest.permission#CHANGE_NETWORK_STATE} permission
1649 * or the ability to modify system settings as determined by
1650 * {@link android.provider.Settings.System#canWrite}.</p>
1651 *
Robert Greenwalt585ac0f2010-08-27 09:24:29 -07001652 * @param networkType the type of the network over which traffic to the specified
1653 * host is to be routed
1654 * @param hostAddress the IP address of the host to which the route is desired
1655 * @return {@code true} on success, {@code false} on failure
1656 * @hide
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07001657 * @deprecated Deprecated in favor of the {@link #requestNetwork} and
Lorenzo Colitti2ea89e52015-04-24 17:03:31 +09001658 * {@link #bindProcessToNetwork} API.
Robert Greenwalt585ac0f2010-08-27 09:24:29 -07001659 */
Aurimas Liutikas514c5ef2016-05-24 15:22:55 -07001660 @Deprecated
Robert Greenwalt585ac0f2010-08-27 09:24:29 -07001661 public boolean requestRouteToHostAddress(int networkType, InetAddress hostAddress) {
Lorenzo Colittiffc42b02015-07-29 11:41:21 +09001662 checkLegacyRoutingApiAccess();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001663 try {
Sreeram Ramachandran03666c72014-07-19 23:21:46 -07001664 return mService.requestRouteToHostAddress(networkType, hostAddress.getAddress());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001665 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07001666 throw e.rethrowFromSystemServer();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001667 }
1668 }
1669
1670 /**
1671 * Returns the value of the setting for background data usage. If false,
1672 * applications should not use the network if the application is not in the
1673 * foreground. Developers should respect this setting, and check the value
1674 * of this before performing any background data operations.
1675 * <p>
1676 * All applications that have background services that use the network
1677 * should listen to {@link #ACTION_BACKGROUND_DATA_SETTING_CHANGED}.
Jeff Sharkey3a844fc2011-08-16 14:37:57 -07001678 * <p>
Scott Main4cc53332011-10-06 18:32:43 -07001679 * @deprecated As of {@link VERSION_CODES#ICE_CREAM_SANDWICH}, availability of
Jeff Sharkey3a844fc2011-08-16 14:37:57 -07001680 * background data depends on several combined factors, and this method will
1681 * always return {@code true}. Instead, when background data is unavailable,
1682 * {@link #getActiveNetworkInfo()} will now appear disconnected.
Danica Chang6fdd0c62010-08-11 14:54:43 -07001683 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001684 * @return Whether background data usage is allowed.
1685 */
Jeff Sharkey3a844fc2011-08-16 14:37:57 -07001686 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001687 public boolean getBackgroundDataSetting() {
Jeff Sharkey3a844fc2011-08-16 14:37:57 -07001688 // assume that background data is allowed; final authority is
1689 // NetworkInfo which may be blocked.
1690 return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001691 }
1692
1693 /**
1694 * Sets the value of the setting for background data usage.
Robert Greenwaltc03fa502010-02-23 18:58:05 -08001695 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001696 * @param allowBackgroundData Whether an application should use data while
1697 * it is in the background.
Robert Greenwaltc03fa502010-02-23 18:58:05 -08001698 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001699 * @attr ref android.Manifest.permission#CHANGE_BACKGROUND_DATA_SETTING
1700 * @see #getBackgroundDataSetting()
1701 * @hide
1702 */
Jeff Sharkey3a844fc2011-08-16 14:37:57 -07001703 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001704 public void setBackgroundDataSetting(boolean allowBackgroundData) {
Jeff Sharkey3a844fc2011-08-16 14:37:57 -07001705 // ignored
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001706 }
Robert Greenwaltc03fa502010-02-23 18:58:05 -08001707
1708 /**
Jeff Sharkeyf0ceede2011-08-02 17:22:34 -07001709 * Return quota status for the current active network, or {@code null} if no
1710 * network is active. Quota status can change rapidly, so these values
1711 * shouldn't be cached.
Jeff Sharkey44a3e0d2011-10-06 10:50:09 -07001712 *
Paul Jensen0d719ca2015-02-13 14:18:39 -05001713 * <p>This method requires the caller to hold the permission
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08001714 * {@link android.Manifest.permission#ACCESS_NETWORK_STATE}.
1715 *
Jeff Sharkey44a3e0d2011-10-06 10:50:09 -07001716 * @hide
Jeff Sharkeyf0ceede2011-08-02 17:22:34 -07001717 */
1718 public NetworkQuotaInfo getActiveNetworkQuotaInfo() {
1719 try {
1720 return mService.getActiveNetworkQuotaInfo();
1721 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07001722 throw e.rethrowFromSystemServer();
Jeff Sharkeyf0ceede2011-08-02 17:22:34 -07001723 }
1724 }
1725
1726 /**
Robert Greenwaltc03fa502010-02-23 18:58:05 -08001727 * @hide
Robert Greenwaltafa05c02014-05-21 20:04:36 -07001728 * @deprecated Talk to TelephonyManager directly
Robert Greenwaltc03fa502010-02-23 18:58:05 -08001729 */
Aurimas Liutikas514c5ef2016-05-24 15:22:55 -07001730 @Deprecated
Robert Greenwaltc03fa502010-02-23 18:58:05 -08001731 public boolean getMobileDataEnabled() {
Robert Greenwaltafa05c02014-05-21 20:04:36 -07001732 IBinder b = ServiceManager.getService(Context.TELEPHONY_SERVICE);
1733 if (b != null) {
1734 try {
1735 ITelephony it = ITelephony.Stub.asInterface(b);
Shishir Agrawal7ea3e8b2016-01-25 13:03:07 -08001736 int subId = SubscriptionManager.getDefaultDataSubscriptionId();
Wink Saville36ffb042014-12-05 11:10:30 -08001737 Log.d("ConnectivityManager", "getMobileDataEnabled()+ subId=" + subId);
1738 boolean retVal = it.getDataEnabled(subId);
1739 Log.d("ConnectivityManager", "getMobileDataEnabled()- subId=" + subId
1740 + " retVal=" + retVal);
1741 return retVal;
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07001742 } catch (RemoteException e) {
1743 throw e.rethrowFromSystemServer();
1744 }
Robert Greenwaltc03fa502010-02-23 18:58:05 -08001745 }
Wink Saville36ffb042014-12-05 11:10:30 -08001746 Log.d("ConnectivityManager", "getMobileDataEnabled()- remote exception retVal=false");
Robert Greenwaltafa05c02014-05-21 20:04:36 -07001747 return false;
Robert Greenwaltc03fa502010-02-23 18:58:05 -08001748 }
1749
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001750 /**
Robert Greenwaltb2489872014-09-04 16:44:35 -07001751 * Callback for use with {@link ConnectivityManager#addDefaultNetworkActiveListener}
Robert Greenwalt6078b502014-06-11 16:05:07 -07001752 * to find out when the system default network has gone in to a high power state.
Dianne Hackborn77b987f2014-02-26 16:20:52 -08001753 */
1754 public interface OnNetworkActiveListener {
1755 /**
1756 * Called on the main thread of the process to report that the current data network
1757 * has become active, and it is now a good time to perform any pending network
1758 * operations. Note that this listener only tells you when the network becomes
1759 * active; if at any other time you want to know whether it is active (and thus okay
1760 * to initiate network traffic), you can retrieve its instantaneous state with
Robert Greenwalt6078b502014-06-11 16:05:07 -07001761 * {@link ConnectivityManager#isDefaultNetworkActive}.
Dianne Hackborn77b987f2014-02-26 16:20:52 -08001762 */
1763 public void onNetworkActive();
1764 }
1765
1766 private INetworkManagementService getNetworkManagementService() {
1767 synchronized (this) {
1768 if (mNMService != null) {
1769 return mNMService;
1770 }
1771 IBinder b = ServiceManager.getService(Context.NETWORKMANAGEMENT_SERVICE);
1772 mNMService = INetworkManagementService.Stub.asInterface(b);
1773 return mNMService;
1774 }
1775 }
1776
1777 private final ArrayMap<OnNetworkActiveListener, INetworkActivityListener>
1778 mNetworkActivityListeners
1779 = new ArrayMap<OnNetworkActiveListener, INetworkActivityListener>();
1780
1781 /**
Robert Greenwalt6078b502014-06-11 16:05:07 -07001782 * Start listening to reports when the system's default data network is active, meaning it is
1783 * a good time to perform network traffic. Use {@link #isDefaultNetworkActive()}
1784 * to determine the current state of the system's default network after registering the
1785 * listener.
1786 * <p>
1787 * If the process default network has been set with
Paul Jensen72db88e2015-03-10 10:54:12 -04001788 * {@link ConnectivityManager#bindProcessToNetwork} this function will not
Robert Greenwalt6078b502014-06-11 16:05:07 -07001789 * reflect the process's default, but the system default.
Dianne Hackborn77b987f2014-02-26 16:20:52 -08001790 *
1791 * @param l The listener to be told when the network is active.
1792 */
Robert Greenwaltb2489872014-09-04 16:44:35 -07001793 public void addDefaultNetworkActiveListener(final OnNetworkActiveListener l) {
Dianne Hackborn77b987f2014-02-26 16:20:52 -08001794 INetworkActivityListener rl = new INetworkActivityListener.Stub() {
1795 @Override
1796 public void onNetworkActive() throws RemoteException {
1797 l.onNetworkActive();
1798 }
1799 };
1800
1801 try {
1802 getNetworkManagementService().registerNetworkActivityListener(rl);
1803 mNetworkActivityListeners.put(l, rl);
1804 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07001805 throw e.rethrowFromSystemServer();
Dianne Hackborn77b987f2014-02-26 16:20:52 -08001806 }
1807 }
1808
1809 /**
1810 * Remove network active listener previously registered with
Robert Greenwaltb2489872014-09-04 16:44:35 -07001811 * {@link #addDefaultNetworkActiveListener}.
Dianne Hackborn77b987f2014-02-26 16:20:52 -08001812 *
1813 * @param l Previously registered listener.
1814 */
Robert Greenwaltb2489872014-09-04 16:44:35 -07001815 public void removeDefaultNetworkActiveListener(OnNetworkActiveListener l) {
Dianne Hackborn77b987f2014-02-26 16:20:52 -08001816 INetworkActivityListener rl = mNetworkActivityListeners.get(l);
1817 if (rl == null) {
1818 throw new IllegalArgumentException("Listener not registered: " + l);
1819 }
1820 try {
1821 getNetworkManagementService().unregisterNetworkActivityListener(rl);
1822 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07001823 throw e.rethrowFromSystemServer();
Dianne Hackborn77b987f2014-02-26 16:20:52 -08001824 }
1825 }
1826
1827 /**
1828 * Return whether the data network is currently active. An active network means that
1829 * it is currently in a high power state for performing data transmission. On some
1830 * types of networks, it may be expensive to move and stay in such a state, so it is
1831 * more power efficient to batch network traffic together when the radio is already in
1832 * this state. This method tells you whether right now is currently a good time to
1833 * initiate network traffic, as the network is already active.
1834 */
Robert Greenwalt6078b502014-06-11 16:05:07 -07001835 public boolean isDefaultNetworkActive() {
Dianne Hackborn77b987f2014-02-26 16:20:52 -08001836 try {
1837 return getNetworkManagementService().isNetworkActive();
1838 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07001839 throw e.rethrowFromSystemServer();
Dianne Hackborn77b987f2014-02-26 16:20:52 -08001840 }
Dianne Hackborn77b987f2014-02-26 16:20:52 -08001841 }
1842
1843 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001844 * {@hide}
1845 */
Lorenzo Colittiffc42b02015-07-29 11:41:21 +09001846 public ConnectivityManager(Context context, IConnectivityManager service) {
1847 mContext = checkNotNull(context, "missing context");
Jeff Sharkeyf0ceede2011-08-02 17:22:34 -07001848 mService = checkNotNull(service, "missing IConnectivityManager");
Paul Jensene0bef712014-12-10 15:12:18 -05001849 sInstance = this;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001850 }
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -08001851
Jeff Sharkey8fc27e82012-04-04 20:40:58 -07001852 /** {@hide} */
1853 public static ConnectivityManager from(Context context) {
1854 return (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
1855 }
1856
Lorenzo Colittifbe9b1a2016-07-28 17:14:11 +09001857 /* TODO: These permissions checks don't belong in client-side code. Move them to
1858 * services.jar, possibly in com.android.server.net. */
1859
1860 /** {@hide} */
1861 public static final boolean checkChangePermission(Context context) {
1862 int uid = Binder.getCallingUid();
1863 return Settings.checkAndNoteChangeNetworkStateOperation(context, uid, Settings
1864 .getPackageNameForUid(context, uid), false /* throwException */);
1865 }
1866
Lorenzo Colittid5427052015-10-15 16:29:00 +09001867 /** {@hide} */
1868 public static final void enforceChangePermission(Context context) {
1869 int uid = Binder.getCallingUid();
1870 Settings.checkAndNoteChangeNetworkStateOperation(context, uid, Settings
1871 .getPackageNameForUid(context, uid), true /* throwException */);
1872 }
1873
Robert Greenwaltedb47662014-09-16 17:54:19 -07001874 /** {@hide */
1875 public static final void enforceTetherChangePermission(Context context) {
1876 if (context.getResources().getStringArray(
1877 com.android.internal.R.array.config_mobile_hotspot_provision_app).length == 2) {
1878 // Have a provisioning app - must only let system apps (which check this app)
1879 // turn on tethering
1880 context.enforceCallingOrSelfPermission(
Jeremy Kleind42209d2015-12-28 15:11:58 -08001881 android.Manifest.permission.TETHER_PRIVILEGED, "ConnectivityService");
Robert Greenwaltedb47662014-09-16 17:54:19 -07001882 } else {
Billy Laua7238a32015-08-01 12:45:02 +01001883 int uid = Binder.getCallingUid();
Lorenzo Colittid5427052015-10-15 16:29:00 +09001884 Settings.checkAndNoteWriteSettingsOperation(context, uid, Settings
1885 .getPackageNameForUid(context, uid), true /* throwException */);
Robert Greenwaltedb47662014-09-16 17:54:19 -07001886 }
1887 }
1888
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -08001889 /**
Paul Jensene0bef712014-12-10 15:12:18 -05001890 * @deprecated - use getSystemService. This is a kludge to support static access in certain
1891 * situations where a Context pointer is unavailable.
1892 * @hide
1893 */
Aurimas Liutikas514c5ef2016-05-24 15:22:55 -07001894 @Deprecated
Paul Jensen72db88e2015-03-10 10:54:12 -04001895 static ConnectivityManager getInstanceOrNull() {
1896 return sInstance;
1897 }
1898
1899 /**
1900 * @deprecated - use getSystemService. This is a kludge to support static access in certain
1901 * situations where a Context pointer is unavailable.
1902 * @hide
1903 */
Aurimas Liutikas514c5ef2016-05-24 15:22:55 -07001904 @Deprecated
Paul Jensen72db88e2015-03-10 10:54:12 -04001905 private static ConnectivityManager getInstance() {
1906 if (getInstanceOrNull() == null) {
Paul Jensene0bef712014-12-10 15:12:18 -05001907 throw new IllegalStateException("No ConnectivityManager yet constructed");
1908 }
Paul Jensen72db88e2015-03-10 10:54:12 -04001909 return getInstanceOrNull();
Paul Jensene0bef712014-12-10 15:12:18 -05001910 }
1911
1912 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08001913 * Get the set of tetherable, available interfaces. This list is limited by
1914 * device configuration and current interface existence.
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 Strings of tetherable interface names.
1919 *
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -08001920 * {@hide}
1921 */
1922 public String[] getTetherableIfaces() {
1923 try {
1924 return mService.getTetherableIfaces();
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 tethered interfaces.
Paul Jensenb2748922015-05-06 11:10:18 -04001932 * <p>This method requires the caller to hold the permission
1933 * {@link android.Manifest.permission#ACCESS_NETWORK_STATE}.
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08001934 *
1935 * @return an array of 0 or more String of currently tethered interface names.
1936 *
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -08001937 * {@hide}
1938 */
1939 public String[] getTetheredIfaces() {
1940 try {
1941 return mService.getTetheredIfaces();
1942 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07001943 throw e.rethrowFromSystemServer();
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -08001944 }
1945 }
1946
1947 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08001948 * Get the set of interface names which attempted to tether but
1949 * failed. Re-attempting to tether may cause them to reset to the Tethered
1950 * state. Alternatively, causing the interface to be destroyed and recreated
1951 * may cause them to reset to the available state.
1952 * {@link ConnectivityManager#getLastTetherError} can be used to get more
1953 * information on the cause of the errors.
Paul Jensenb2748922015-05-06 11:10:18 -04001954 * <p>This method requires the caller to hold the permission
1955 * {@link android.Manifest.permission#ACCESS_NETWORK_STATE}.
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08001956 *
1957 * @return an array of 0 or more String indicating the interface names
1958 * which failed to tether.
1959 *
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -08001960 * {@hide}
1961 */
Robert Greenwalt5a735062010-03-02 17:25:02 -08001962 public String[] getTetheringErroredIfaces() {
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -08001963 try {
Robert Greenwalt5a735062010-03-02 17:25:02 -08001964 return mService.getTetheringErroredIfaces();
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -08001965 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07001966 throw e.rethrowFromSystemServer();
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -08001967 }
1968 }
1969
1970 /**
Robert Greenwalt9c7e2c22014-06-23 14:53:42 -07001971 * Get the set of tethered dhcp ranges.
1972 *
1973 * @return an array of 0 or more {@code String} of tethered dhcp ranges.
1974 * {@hide}
1975 */
1976 public String[] getTetheredDhcpRanges() {
1977 try {
1978 return mService.getTetheredDhcpRanges();
1979 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07001980 throw e.rethrowFromSystemServer();
Robert Greenwalt9c7e2c22014-06-23 14:53:42 -07001981 }
1982 }
1983
1984 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08001985 * Attempt to tether the named interface. This will setup a dhcp server
1986 * on the interface, forward and NAT IP packets and forward DNS requests
1987 * to the best active upstream network interface. Note that if no upstream
1988 * IP network interface is available, dhcp will still run and traffic will be
1989 * allowed between the tethered devices and this device, though upstream net
1990 * access will of course fail until an upstream network interface becomes
1991 * active.
Lorenzo Colittid5427052015-10-15 16:29:00 +09001992 *
1993 * <p>This method requires the caller to hold either the
1994 * {@link android.Manifest.permission#CHANGE_NETWORK_STATE} permission
1995 * or the ability to modify system settings as determined by
1996 * {@link android.provider.Settings.System#canWrite}.</p>
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08001997 *
Jeremy Klein36c7aa02016-01-22 14:11:45 -08001998 * <p>WARNING: New clients should not use this function. The only usages should be in PanService
1999 * and WifiStateMachine which need direct access. All other clients should use
2000 * {@link #startTethering} and {@link #stopTethering} which encapsulate proper provisioning
2001 * logic.</p>
2002 *
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08002003 * @param iface the interface name to tether.
2004 * @return error a {@code TETHER_ERROR} value indicating success or failure type
2005 *
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -08002006 * {@hide}
2007 */
Robert Greenwalt5a735062010-03-02 17:25:02 -08002008 public int tether(String iface) {
2009 try {
2010 return mService.tether(iface);
2011 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07002012 throw e.rethrowFromSystemServer();
Robert Greenwalt5a735062010-03-02 17:25:02 -08002013 }
2014 }
2015
2016 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08002017 * Stop tethering the named interface.
Lorenzo Colittid5427052015-10-15 16:29:00 +09002018 *
2019 * <p>This method requires the caller to hold either the
2020 * {@link android.Manifest.permission#CHANGE_NETWORK_STATE} permission
2021 * or the ability to modify system settings as determined by
2022 * {@link android.provider.Settings.System#canWrite}.</p>
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08002023 *
Jeremy Klein36c7aa02016-01-22 14:11:45 -08002024 * <p>WARNING: New clients should not use this function. The only usages should be in PanService
2025 * and WifiStateMachine which need direct access. All other clients should use
2026 * {@link #startTethering} and {@link #stopTethering} which encapsulate proper provisioning
2027 * logic.</p>
2028 *
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08002029 * @param iface the interface name to untether.
2030 * @return error a {@code TETHER_ERROR} value indicating success or failure type
2031 *
Robert Greenwalt5a735062010-03-02 17:25:02 -08002032 * {@hide}
2033 */
2034 public int untether(String iface) {
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -08002035 try {
2036 return mService.untether(iface);
2037 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07002038 throw e.rethrowFromSystemServer();
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -08002039 }
2040 }
Robert Greenwalt2a091d72010-02-11 18:18:40 -08002041
2042 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08002043 * Check if the device allows for tethering. It may be disabled via
Dianne Hackborn77b987f2014-02-26 16:20:52 -08002044 * {@code ro.tether.denied} system property, Settings.TETHER_SUPPORTED or
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08002045 * due to device configuration.
Paul Jensenb2748922015-05-06 11:10:18 -04002046 * <p>This method requires the caller to hold the permission
2047 * {@link android.Manifest.permission#ACCESS_NETWORK_STATE}.
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08002048 *
2049 * @return a boolean - {@code true} indicating Tethering is supported.
2050 *
Robert Greenwalt2a091d72010-02-11 18:18:40 -08002051 * {@hide}
2052 */
Jeremy Klein36c7aa02016-01-22 14:11:45 -08002053 @SystemApi
Robert Greenwalt2a091d72010-02-11 18:18:40 -08002054 public boolean isTetheringSupported() {
2055 try {
2056 return mService.isTetheringSupported();
2057 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07002058 throw e.rethrowFromSystemServer();
Robert Greenwalt2a091d72010-02-11 18:18:40 -08002059 }
2060 }
2061
2062 /**
Jeremy Klein36c7aa02016-01-22 14:11:45 -08002063 * Callback for use with {@link #startTethering} to find out whether tethering succeeded.
2064 * @hide
2065 */
2066 @SystemApi
2067 public static abstract class OnStartTetheringCallback {
2068 /**
2069 * Called when tethering has been successfully started.
2070 */
2071 public void onTetheringStarted() {};
2072
2073 /**
2074 * Called when starting tethering failed.
2075 */
2076 public void onTetheringFailed() {};
2077 }
2078
2079 /**
2080 * Convenient overload for
2081 * {@link #startTethering(int, boolean, OnStartTetheringCallback, Handler)} which passes a null
2082 * handler to run on the current thread's {@link Looper}.
2083 * @hide
2084 */
2085 @SystemApi
2086 public void startTethering(int type, boolean showProvisioningUi,
2087 final OnStartTetheringCallback callback) {
2088 startTethering(type, showProvisioningUi, callback, null);
2089 }
2090
2091 /**
2092 * Runs tether provisioning for the given type if needed and then starts tethering if
2093 * the check succeeds. If no carrier provisioning is required for tethering, tethering is
2094 * enabled immediately. If provisioning fails, tethering will not be enabled. It also
2095 * schedules tether provisioning re-checks if appropriate.
2096 *
2097 * @param type The type of tethering to start. Must be one of
2098 * {@link ConnectivityManager.TETHERING_WIFI},
2099 * {@link ConnectivityManager.TETHERING_USB}, or
2100 * {@link ConnectivityManager.TETHERING_BLUETOOTH}.
2101 * @param showProvisioningUi a boolean indicating to show the provisioning app UI if there
2102 * is one. This should be true the first time this function is called and also any time
2103 * the user can see this UI. It gives users information from their carrier about the
2104 * check failing and how they can sign up for tethering if possible.
2105 * @param callback an {@link OnStartTetheringCallback} which will be called to notify the caller
2106 * of the result of trying to tether.
2107 * @param handler {@link Handler} to specify the thread upon which the callback will be invoked.
2108 * @hide
2109 */
2110 @SystemApi
2111 public void startTethering(int type, boolean showProvisioningUi,
2112 final OnStartTetheringCallback callback, Handler handler) {
Jeremy Klein5f277e12016-03-12 16:29:54 -08002113 checkNotNull(callback, "OnStartTetheringCallback cannot be null.");
2114
Jeremy Klein36c7aa02016-01-22 14:11:45 -08002115 ResultReceiver wrappedCallback = new ResultReceiver(handler) {
2116 @Override
2117 protected void onReceiveResult(int resultCode, Bundle resultData) {
2118 if (resultCode == TETHER_ERROR_NO_ERROR) {
2119 callback.onTetheringStarted();
2120 } else {
2121 callback.onTetheringFailed();
2122 }
2123 }
2124 };
Jeremy Klein5f277e12016-03-12 16:29:54 -08002125
Jeremy Klein36c7aa02016-01-22 14:11:45 -08002126 try {
2127 mService.startTethering(type, wrappedCallback, showProvisioningUi);
2128 } catch (RemoteException e) {
2129 Log.e(TAG, "Exception trying to start tethering.", e);
2130 wrappedCallback.send(TETHER_ERROR_SERVICE_UNAVAIL, null);
2131 }
2132 }
2133
2134 /**
2135 * Stops tethering for the given type. Also cancels any provisioning rechecks for that type if
2136 * applicable.
2137 *
2138 * @param type The type of tethering to stop. Must be one of
2139 * {@link ConnectivityManager.TETHERING_WIFI},
2140 * {@link ConnectivityManager.TETHERING_USB}, or
2141 * {@link ConnectivityManager.TETHERING_BLUETOOTH}.
2142 * @hide
2143 */
2144 @SystemApi
2145 public void stopTethering(int type) {
2146 try {
2147 mService.stopTethering(type);
2148 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07002149 throw e.rethrowFromSystemServer();
Jeremy Klein36c7aa02016-01-22 14:11:45 -08002150 }
2151 }
2152
2153 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08002154 * Get the list of regular expressions that define any tetherable
2155 * USB network interfaces. If USB tethering is not supported by the
2156 * device, this list should be empty.
Paul Jensenb2748922015-05-06 11:10:18 -04002157 * <p>This method requires the caller to hold the permission
2158 * {@link android.Manifest.permission#ACCESS_NETWORK_STATE}.
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08002159 *
2160 * @return an array of 0 or more regular expression Strings defining
2161 * what interfaces are considered tetherable usb interfaces.
2162 *
Robert Greenwalt2a091d72010-02-11 18:18:40 -08002163 * {@hide}
2164 */
2165 public String[] getTetherableUsbRegexs() {
2166 try {
2167 return mService.getTetherableUsbRegexs();
2168 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07002169 throw e.rethrowFromSystemServer();
Robert Greenwalt2a091d72010-02-11 18:18:40 -08002170 }
2171 }
2172
2173 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08002174 * Get the list of regular expressions that define any tetherable
2175 * Wifi network interfaces. If Wifi tethering is not supported by the
2176 * device, this list should be empty.
Paul Jensenb2748922015-05-06 11:10:18 -04002177 * <p>This method requires the caller to hold the permission
2178 * {@link android.Manifest.permission#ACCESS_NETWORK_STATE}.
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08002179 *
2180 * @return an array of 0 or more regular expression Strings defining
2181 * what interfaces are considered tetherable wifi interfaces.
2182 *
Robert Greenwalt2a091d72010-02-11 18:18:40 -08002183 * {@hide}
2184 */
2185 public String[] getTetherableWifiRegexs() {
2186 try {
2187 return mService.getTetherableWifiRegexs();
2188 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07002189 throw e.rethrowFromSystemServer();
Robert Greenwalt2a091d72010-02-11 18:18:40 -08002190 }
2191 }
Robert Greenwalt5a735062010-03-02 17:25:02 -08002192
Danica Chang6fdd0c62010-08-11 14:54:43 -07002193 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08002194 * Get the list of regular expressions that define any tetherable
2195 * Bluetooth network interfaces. If Bluetooth tethering is not supported by the
2196 * device, this list should be empty.
Paul Jensenb2748922015-05-06 11:10:18 -04002197 * <p>This method requires the caller to hold the permission
2198 * {@link android.Manifest.permission#ACCESS_NETWORK_STATE}.
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08002199 *
2200 * @return an array of 0 or more regular expression Strings defining
2201 * what interfaces are considered tetherable bluetooth interfaces.
2202 *
Danica Chang6fdd0c62010-08-11 14:54:43 -07002203 * {@hide}
2204 */
2205 public String[] getTetherableBluetoothRegexs() {
2206 try {
2207 return mService.getTetherableBluetoothRegexs();
2208 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07002209 throw e.rethrowFromSystemServer();
Danica Chang6fdd0c62010-08-11 14:54:43 -07002210 }
2211 }
2212
Mike Lockwood6c2260b2011-07-19 13:04:47 -07002213 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08002214 * Attempt to both alter the mode of USB and Tethering of USB. A
2215 * utility method to deal with some of the complexity of USB - will
2216 * attempt to switch to Rndis and subsequently tether the resulting
2217 * interface on {@code true} or turn off tethering and switch off
2218 * Rndis on {@code false}.
Lorenzo Colittid5427052015-10-15 16:29:00 +09002219 *
2220 * <p>This method requires the caller to hold either the
2221 * {@link android.Manifest.permission#CHANGE_NETWORK_STATE} permission
2222 * or the ability to modify system settings as determined by
2223 * {@link android.provider.Settings.System#canWrite}.</p>
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08002224 *
2225 * @param enable a boolean - {@code true} to enable tethering
2226 * @return error a {@code TETHER_ERROR} value indicating success or failure type
2227 *
Mike Lockwood6c2260b2011-07-19 13:04:47 -07002228 * {@hide}
2229 */
2230 public int setUsbTethering(boolean enable) {
2231 try {
2232 return mService.setUsbTethering(enable);
2233 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07002234 throw e.rethrowFromSystemServer();
Mike Lockwood6c2260b2011-07-19 13:04:47 -07002235 }
2236 }
2237
Robert Greenwalt5a735062010-03-02 17:25:02 -08002238 /** {@hide} */
2239 public static final int TETHER_ERROR_NO_ERROR = 0;
2240 /** {@hide} */
2241 public static final int TETHER_ERROR_UNKNOWN_IFACE = 1;
2242 /** {@hide} */
2243 public static final int TETHER_ERROR_SERVICE_UNAVAIL = 2;
2244 /** {@hide} */
2245 public static final int TETHER_ERROR_UNSUPPORTED = 3;
2246 /** {@hide} */
2247 public static final int TETHER_ERROR_UNAVAIL_IFACE = 4;
2248 /** {@hide} */
2249 public static final int TETHER_ERROR_MASTER_ERROR = 5;
2250 /** {@hide} */
2251 public static final int TETHER_ERROR_TETHER_IFACE_ERROR = 6;
2252 /** {@hide} */
2253 public static final int TETHER_ERROR_UNTETHER_IFACE_ERROR = 7;
2254 /** {@hide} */
2255 public static final int TETHER_ERROR_ENABLE_NAT_ERROR = 8;
2256 /** {@hide} */
2257 public static final int TETHER_ERROR_DISABLE_NAT_ERROR = 9;
2258 /** {@hide} */
2259 public static final int TETHER_ERROR_IFACE_CFG_ERROR = 10;
Jeremy Klein36c7aa02016-01-22 14:11:45 -08002260 /** {@hide} */
2261 public static final int TETHER_ERROR_PROVISION_FAILED = 11;
Robert Greenwalt5a735062010-03-02 17:25:02 -08002262
2263 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08002264 * Get a more detailed error code after a Tethering or Untethering
2265 * request asynchronously failed.
Paul Jensenb2748922015-05-06 11:10:18 -04002266 * <p>This method requires the caller to hold the permission
2267 * {@link android.Manifest.permission#ACCESS_NETWORK_STATE}.
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08002268 *
2269 * @param iface The name of the interface of interest
Robert Greenwalt5a735062010-03-02 17:25:02 -08002270 * @return error The error code of the last error tethering or untethering the named
2271 * interface
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08002272 *
Robert Greenwalt5a735062010-03-02 17:25:02 -08002273 * {@hide}
2274 */
2275 public int getLastTetherError(String iface) {
2276 try {
2277 return mService.getLastTetherError(iface);
2278 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07002279 throw e.rethrowFromSystemServer();
Robert Greenwalt5a735062010-03-02 17:25:02 -08002280 }
Robert Greenwalt14f2ef42010-06-15 12:19:37 -07002281 }
2282
2283 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08002284 * Report network connectivity status. This is currently used only
2285 * to alter status bar UI.
Paul Jensenb2748922015-05-06 11:10:18 -04002286 * <p>This method requires the caller to hold the permission
2287 * {@link android.Manifest.permission#STATUS_BAR}.
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08002288 *
Robert Greenwaltd7085fc2010-09-08 15:24:47 -07002289 * @param networkType The type of network you want to report on
2290 * @param percentage The quality of the connection 0 is bad, 100 is good
2291 * {@hide}
2292 */
2293 public void reportInetCondition(int networkType, int percentage) {
2294 try {
2295 mService.reportInetCondition(networkType, percentage);
2296 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07002297 throw e.rethrowFromSystemServer();
Robert Greenwaltd7085fc2010-09-08 15:24:47 -07002298 }
2299 }
Robert Greenwalt434203a2010-10-11 16:00:27 -07002300
2301 /**
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07002302 * Report a problem network to the framework. This provides a hint to the system
Ye Wenb87875e2014-07-21 14:19:01 -07002303 * that there might be connectivity problems on this network and may cause
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07002304 * the framework to re-evaluate network connectivity and/or switch to another
2305 * network.
Robert Greenwalt9258c642014-03-26 16:47:06 -07002306 *
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07002307 * @param network The {@link Network} the application was attempting to use
2308 * or {@code null} to indicate the current default network.
Paul Jensenbfd17b72015-04-07 12:43:13 -04002309 * @deprecated Use {@link #reportNetworkConnectivity} which allows reporting both
2310 * working and non-working connectivity.
Robert Greenwalt9258c642014-03-26 16:47:06 -07002311 */
Aurimas Liutikas514c5ef2016-05-24 15:22:55 -07002312 @Deprecated
Robert Greenwalt9258c642014-03-26 16:47:06 -07002313 public void reportBadNetwork(Network network) {
2314 try {
Paul Jensenbfd17b72015-04-07 12:43:13 -04002315 // One of these will be ignored because it matches system's current state.
2316 // The other will trigger the necessary reevaluation.
2317 mService.reportNetworkConnectivity(network, true);
2318 mService.reportNetworkConnectivity(network, false);
2319 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07002320 throw e.rethrowFromSystemServer();
Paul Jensenbfd17b72015-04-07 12:43:13 -04002321 }
2322 }
2323
2324 /**
2325 * Report to the framework whether a network has working connectivity.
2326 * This provides a hint to the system that a particular network is providing
2327 * working connectivity or not. In response the framework may re-evaluate
2328 * the network's connectivity and might take further action thereafter.
2329 *
2330 * @param network The {@link Network} the application was attempting to use
2331 * or {@code null} to indicate the current default network.
2332 * @param hasConnectivity {@code true} if the application was able to successfully access the
2333 * Internet using {@code network} or {@code false} if not.
2334 */
2335 public void reportNetworkConnectivity(Network network, boolean hasConnectivity) {
2336 try {
2337 mService.reportNetworkConnectivity(network, hasConnectivity);
Robert Greenwalt9258c642014-03-26 16:47:06 -07002338 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07002339 throw e.rethrowFromSystemServer();
Robert Greenwalt9258c642014-03-26 16:47:06 -07002340 }
2341 }
2342
2343 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08002344 * Set a network-independent global http proxy. This is not normally what you want
2345 * for typical HTTP proxies - they are general network dependent. However if you're
2346 * doing something unusual like general internal filtering this may be useful. On
2347 * a private network where the proxy is not accessible, you may break HTTP using this.
Paul Jensen0d719ca2015-02-13 14:18:39 -05002348 * <p>This method requires the caller to hold the permission
Jason Monkfaf3fd52014-05-07 18:41:13 -04002349 * android.Manifest.permission#CONNECTIVITY_INTERNAL.
Paul Jensenb2748922015-05-06 11:10:18 -04002350 *
2351 * @param p A {@link ProxyInfo} object defining the new global
2352 * HTTP proxy. A {@code null} value will clear the global HTTP proxy.
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07002353 * @hide
Robert Greenwalt434203a2010-10-11 16:00:27 -07002354 */
Jason Monk207900c2014-04-25 15:00:09 -04002355 public void setGlobalProxy(ProxyInfo p) {
Robert Greenwalt434203a2010-10-11 16:00:27 -07002356 try {
2357 mService.setGlobalProxy(p);
2358 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07002359 throw e.rethrowFromSystemServer();
Robert Greenwalt434203a2010-10-11 16:00:27 -07002360 }
2361 }
2362
2363 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08002364 * Retrieve any network-independent global HTTP proxy.
2365 *
Jason Monk207900c2014-04-25 15:00:09 -04002366 * @return {@link ProxyInfo} for the current global HTTP proxy or {@code null}
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08002367 * if no global HTTP proxy is set.
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07002368 * @hide
Robert Greenwalt434203a2010-10-11 16:00:27 -07002369 */
Jason Monk207900c2014-04-25 15:00:09 -04002370 public ProxyInfo getGlobalProxy() {
Robert Greenwalt434203a2010-10-11 16:00:27 -07002371 try {
2372 return mService.getGlobalProxy();
2373 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07002374 throw e.rethrowFromSystemServer();
Robert Greenwalt434203a2010-10-11 16:00:27 -07002375 }
2376 }
2377
2378 /**
Paul Jensencee9b512015-05-06 07:32:40 -04002379 * Retrieve the global HTTP proxy, or if no global HTTP proxy is set, a
2380 * network-specific HTTP proxy. If {@code network} is null, the
2381 * network-specific proxy returned is the proxy of the default active
2382 * network.
2383 *
2384 * @return {@link ProxyInfo} for the current global HTTP proxy, or if no
2385 * global HTTP proxy is set, {@code ProxyInfo} for {@code network},
2386 * or when {@code network} is {@code null},
2387 * the {@code ProxyInfo} for the default active network. Returns
2388 * {@code null} when no proxy applies or the caller doesn't have
2389 * permission to use {@code network}.
2390 * @hide
2391 */
2392 public ProxyInfo getProxyForNetwork(Network network) {
2393 try {
2394 return mService.getProxyForNetwork(network);
2395 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07002396 throw e.rethrowFromSystemServer();
Paul Jensencee9b512015-05-06 07:32:40 -04002397 }
2398 }
2399
2400 /**
Paul Jensene0bef712014-12-10 15:12:18 -05002401 * Get the current default HTTP proxy settings. If a global proxy is set it will be returned,
2402 * otherwise if this process is bound to a {@link Network} using
Paul Jensen72db88e2015-03-10 10:54:12 -04002403 * {@link #bindProcessToNetwork} then that {@code Network}'s proxy is returned, otherwise
Paul Jensene0bef712014-12-10 15:12:18 -05002404 * the default network's proxy is returned.
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08002405 *
Jason Monk207900c2014-04-25 15:00:09 -04002406 * @return the {@link ProxyInfo} for the current HTTP proxy, or {@code null} if no
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08002407 * HTTP proxy is active.
Robert Greenwalt434203a2010-10-11 16:00:27 -07002408 */
Paul Jensene0bef712014-12-10 15:12:18 -05002409 public ProxyInfo getDefaultProxy() {
Paul Jensencee9b512015-05-06 07:32:40 -04002410 return getProxyForNetwork(getBoundNetworkForProcess());
Robert Greenwalt434203a2010-10-11 16:00:27 -07002411 }
Robert Greenwaltd55a6b42011-03-25 13:09:25 -07002412
2413 /**
Robert Greenwalt9b2886e2011-08-31 11:46:42 -07002414 * Returns true if the hardware supports the given network type
2415 * else it returns false. This doesn't indicate we have coverage
2416 * or are authorized onto a network, just whether or not the
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08002417 * hardware supports it. For example a GSM phone without a SIM
2418 * should still return {@code true} for mobile data, but a wifi only
2419 * tablet would return {@code false}.
Paul Jensenb2748922015-05-06 11:10:18 -04002420 * <p>This method requires the caller to hold the permission
2421 * {@link android.Manifest.permission#ACCESS_NETWORK_STATE}.
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08002422 *
2423 * @param networkType The network type we'd like to check
2424 * @return {@code true} if supported, else {@code false}
2425 *
Robert Greenwalt9b2886e2011-08-31 11:46:42 -07002426 * @hide
2427 */
2428 public boolean isNetworkSupported(int networkType) {
2429 try {
2430 return mService.isNetworkSupported(networkType);
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07002431 } catch (RemoteException e) {
2432 throw e.rethrowFromSystemServer();
2433 }
Robert Greenwalt9b2886e2011-08-31 11:46:42 -07002434 }
Jeff Sharkey9f7cbf02012-04-12 18:34:54 -07002435
2436 /**
2437 * Returns if the currently active data network is metered. A network is
2438 * classified as metered when the user is sensitive to heavy data usage on
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08002439 * that connection due to monetary costs, data limitations or
2440 * battery/performance issues. You should check this before doing large
2441 * data transfers, and warn the user or delay the operation until another
2442 * network is available.
Paul Jensenb2748922015-05-06 11:10:18 -04002443 * <p>This method requires the caller to hold the permission
2444 * {@link android.Manifest.permission#ACCESS_NETWORK_STATE}.
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08002445 *
2446 * @return {@code true} if large transfers should be avoided, otherwise
2447 * {@code false}.
Jeff Sharkey9f7cbf02012-04-12 18:34:54 -07002448 */
2449 public boolean isActiveNetworkMetered() {
2450 try {
2451 return mService.isActiveNetworkMetered();
2452 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07002453 throw e.rethrowFromSystemServer();
Jeff Sharkey9f7cbf02012-04-12 18:34:54 -07002454 }
2455 }
Jeff Sharkey69ddab42012-08-25 00:05:46 -07002456
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08002457 /**
2458 * If the LockdownVpn mechanism is enabled, updates the vpn
2459 * with a reload of its profile.
2460 *
2461 * @return a boolean with {@code} indicating success
2462 *
2463 * <p>This method can only be called by the system UID
2464 * {@hide}
2465 */
Jeff Sharkey69ddab42012-08-25 00:05:46 -07002466 public boolean updateLockdownVpn() {
2467 try {
2468 return mService.updateLockdownVpn();
2469 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07002470 throw e.rethrowFromSystemServer();
Jeff Sharkey69ddab42012-08-25 00:05:46 -07002471 }
2472 }
Irfan Sheriffda6da092012-08-16 12:49:23 -07002473
2474 /**
Wink Saville948282b2013-08-29 08:55:16 -07002475 * Check mobile provisioning.
Wink Savilleab9321d2013-06-29 21:10:57 -07002476 *
Wink Savilleab9321d2013-06-29 21:10:57 -07002477 * @param suggestedTimeOutMs, timeout in milliseconds
Wink Savilleab9321d2013-06-29 21:10:57 -07002478 *
2479 * @return time out that will be used, maybe less that suggestedTimeOutMs
2480 * -1 if an error.
2481 *
2482 * {@hide}
2483 */
Wink Saville948282b2013-08-29 08:55:16 -07002484 public int checkMobileProvisioning(int suggestedTimeOutMs) {
Wink Savilleab9321d2013-06-29 21:10:57 -07002485 int timeOutMs = -1;
2486 try {
Wink Saville948282b2013-08-29 08:55:16 -07002487 timeOutMs = mService.checkMobileProvisioning(suggestedTimeOutMs);
Wink Savilleab9321d2013-06-29 21:10:57 -07002488 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07002489 throw e.rethrowFromSystemServer();
Wink Savilleab9321d2013-06-29 21:10:57 -07002490 }
2491 return timeOutMs;
2492 }
Robert Greenwalte182bfe2013-07-16 12:06:09 -07002493
2494 /**
Wink Saville42d4f082013-07-20 20:31:59 -07002495 * Get the mobile provisioning url.
Robert Greenwalte182bfe2013-07-16 12:06:09 -07002496 * {@hide}
2497 */
2498 public String getMobileProvisioningUrl() {
2499 try {
2500 return mService.getMobileProvisioningUrl();
2501 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07002502 throw e.rethrowFromSystemServer();
Robert Greenwalte182bfe2013-07-16 12:06:09 -07002503 }
Robert Greenwalte182bfe2013-07-16 12:06:09 -07002504 }
Wink Saville42d4f082013-07-20 20:31:59 -07002505
2506 /**
Wink Saville948282b2013-08-29 08:55:16 -07002507 * Set sign in error notification to visible or in visible
2508 *
2509 * @param visible
2510 * @param networkType
2511 *
2512 * {@hide}
Paul Jensen3541e9f2015-03-18 12:23:02 -04002513 * @deprecated Doesn't properly deal with multiple connected networks of the same type.
Wink Saville948282b2013-08-29 08:55:16 -07002514 */
Aurimas Liutikas514c5ef2016-05-24 15:22:55 -07002515 @Deprecated
Wink Saville948282b2013-08-29 08:55:16 -07002516 public void setProvisioningNotificationVisible(boolean visible, int networkType,
Paul Jensen89e0f092014-09-15 15:59:36 -04002517 String action) {
Wink Saville948282b2013-08-29 08:55:16 -07002518 try {
Paul Jensen89e0f092014-09-15 15:59:36 -04002519 mService.setProvisioningNotificationVisible(visible, networkType, action);
Wink Saville948282b2013-08-29 08:55:16 -07002520 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07002521 throw e.rethrowFromSystemServer();
Wink Saville948282b2013-08-29 08:55:16 -07002522 }
2523 }
Yuhao Zheng5cd1a0e2013-09-09 17:00:04 -07002524
2525 /**
2526 * Set the value for enabling/disabling airplane mode
Paul Jensenb2748922015-05-06 11:10:18 -04002527 * <p>This method requires the caller to hold the permission
2528 * {@link android.Manifest.permission#CONNECTIVITY_INTERNAL}.
Yuhao Zheng5cd1a0e2013-09-09 17:00:04 -07002529 *
2530 * @param enable whether to enable airplane mode or not
2531 *
Yuhao Zheng5cd1a0e2013-09-09 17:00:04 -07002532 * @hide
2533 */
2534 public void setAirplaneMode(boolean enable) {
2535 try {
2536 mService.setAirplaneMode(enable);
2537 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07002538 throw e.rethrowFromSystemServer();
Yuhao Zheng5cd1a0e2013-09-09 17:00:04 -07002539 }
2540 }
Robert Greenwalte049c232014-04-11 15:53:27 -07002541
2542 /** {@hide} */
Robert Greenwalta67be032014-05-16 15:49:14 -07002543 public void registerNetworkFactory(Messenger messenger, String name) {
Robert Greenwalte049c232014-04-11 15:53:27 -07002544 try {
Robert Greenwalta67be032014-05-16 15:49:14 -07002545 mService.registerNetworkFactory(messenger, name);
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07002546 } catch (RemoteException e) {
2547 throw e.rethrowFromSystemServer();
2548 }
Robert Greenwalta67be032014-05-16 15:49:14 -07002549 }
2550
2551 /** {@hide} */
2552 public void unregisterNetworkFactory(Messenger messenger) {
2553 try {
2554 mService.unregisterNetworkFactory(messenger);
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07002555 } catch (RemoteException e) {
2556 throw e.rethrowFromSystemServer();
2557 }
Robert Greenwalte049c232014-04-11 15:53:27 -07002558 }
Robert Greenwalt7b816022014-04-18 15:25:25 -07002559
Paul Jensen31a94f42015-02-13 14:18:39 -05002560 /**
2561 * @hide
2562 * Register a NetworkAgent with ConnectivityService.
2563 * @return NetID corresponding to NetworkAgent.
2564 */
2565 public int registerNetworkAgent(Messenger messenger, NetworkInfo ni, LinkProperties lp,
Sreeram Ramachandran8cd33ed2014-07-23 15:23:15 -07002566 NetworkCapabilities nc, int score, NetworkMisc misc) {
Robert Greenwalt7b816022014-04-18 15:25:25 -07002567 try {
Paul Jensen31a94f42015-02-13 14:18:39 -05002568 return mService.registerNetworkAgent(messenger, ni, lp, nc, score, misc);
2569 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07002570 throw e.rethrowFromSystemServer();
Paul Jensen31a94f42015-02-13 14:18:39 -05002571 }
Robert Greenwalt7b816022014-04-18 15:25:25 -07002572 }
2573
Robert Greenwalt9258c642014-03-26 16:47:06 -07002574 /**
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07002575 * Base class for NetworkRequest callbacks. Used for notifications about network
2576 * changes. Should be extended by applications wanting notifications.
Robert Greenwalt9258c642014-03-26 16:47:06 -07002577 */
Robert Greenwalt6078b502014-06-11 16:05:07 -07002578 public static class NetworkCallback {
Robert Greenwalt7b816022014-04-18 15:25:25 -07002579 /**
Lorenzo Colitti07086932015-04-24 12:23:24 +09002580 * Called when the framework connects to a new network to evaluate whether it satisfies this
2581 * request. If evaluation succeeds, this callback may be followed by an {@link #onAvailable}
2582 * callback. There is no guarantee that this new network will satisfy any requests, or that
2583 * the network will stay connected for longer than the time necessary to evaluate it.
2584 * <p>
2585 * Most applications <b>should not</b> act on this callback, and should instead use
2586 * {@link #onAvailable}. This callback is intended for use by applications that can assist
2587 * the framework in properly evaluating the network &mdash; for example, an application that
2588 * can automatically log in to a captive portal without user intervention.
2589 *
2590 * @param network The {@link Network} of the network that is being evaluated.
Lorenzo Colitti66276122015-06-11 14:27:17 +09002591 *
2592 * @hide
Robert Greenwalt7b816022014-04-18 15:25:25 -07002593 */
Robert Greenwalt6078b502014-06-11 16:05:07 -07002594 public void onPreCheck(Network network) {}
Robert Greenwalt7b816022014-04-18 15:25:25 -07002595
2596 /**
Lorenzo Colitti07086932015-04-24 12:23:24 +09002597 * Called when the framework connects and has declared a new network ready for use.
Robert Greenwalt6078b502014-06-11 16:05:07 -07002598 * This callback may be called more than once if the {@link Network} that is
2599 * satisfying the request changes.
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07002600 *
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07002601 * @param network The {@link Network} of the satisfying network.
Robert Greenwalt7b816022014-04-18 15:25:25 -07002602 */
Robert Greenwalt6078b502014-06-11 16:05:07 -07002603 public void onAvailable(Network network) {}
Robert Greenwalt7b816022014-04-18 15:25:25 -07002604
2605 /**
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07002606 * Called when the network is about to be disconnected. Often paired with an
Robert Greenwalt6078b502014-06-11 16:05:07 -07002607 * {@link NetworkCallback#onAvailable} call with the new replacement network
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07002608 * for graceful handover. This may not be called if we have a hard loss
2609 * (loss without warning). This may be followed by either a
Robert Greenwalt6078b502014-06-11 16:05:07 -07002610 * {@link NetworkCallback#onLost} call or a
2611 * {@link NetworkCallback#onAvailable} call for this network depending
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07002612 * on whether we lose or regain it.
2613 *
Robert Greenwalt6078b502014-06-11 16:05:07 -07002614 * @param network The {@link Network} that is about to be disconnected.
2615 * @param maxMsToLive The time in ms the framework will attempt to keep the
2616 * network connected. Note that the network may suffer a
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07002617 * hard loss at any time.
Robert Greenwalt7b816022014-04-18 15:25:25 -07002618 */
Robert Greenwalt6078b502014-06-11 16:05:07 -07002619 public void onLosing(Network network, int maxMsToLive) {}
Robert Greenwalt7b816022014-04-18 15:25:25 -07002620
2621 /**
2622 * Called when the framework has a hard loss of the network or when the
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07002623 * graceful failure ends.
2624 *
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07002625 * @param network The {@link Network} lost.
Robert Greenwalt7b816022014-04-18 15:25:25 -07002626 */
Robert Greenwalt6078b502014-06-11 16:05:07 -07002627 public void onLost(Network network) {}
Robert Greenwalt7b816022014-04-18 15:25:25 -07002628
2629 /**
2630 * Called if no network is found in the given timeout time. If no timeout is given,
Erik Kline57faba92015-11-25 12:49:38 +09002631 * this will not be called. The associated {@link NetworkRequest} will have already
2632 * been removed and released, as if {@link #unregisterNetworkCallback} had been called.
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07002633 * @hide
Robert Greenwalt7b816022014-04-18 15:25:25 -07002634 */
Robert Greenwalt6078b502014-06-11 16:05:07 -07002635 public void onUnavailable() {}
Robert Greenwalt7b816022014-04-18 15:25:25 -07002636
2637 /**
2638 * Called when the network the framework connected to for this request
2639 * changes capabilities but still satisfies the stated need.
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07002640 *
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07002641 * @param network The {@link Network} whose capabilities have changed.
Lorenzo Colittie285b432015-04-23 15:32:42 +09002642 * @param networkCapabilities The new {@link android.net.NetworkCapabilities} for this network.
Robert Greenwalt7b816022014-04-18 15:25:25 -07002643 */
Robert Greenwalt6078b502014-06-11 16:05:07 -07002644 public void onCapabilitiesChanged(Network network,
Robert Greenwalt7b816022014-04-18 15:25:25 -07002645 NetworkCapabilities networkCapabilities) {}
2646
2647 /**
2648 * Called when the network the framework connected to for this request
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07002649 * changes {@link LinkProperties}.
2650 *
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07002651 * @param network The {@link Network} whose link properties have changed.
2652 * @param linkProperties The new {@link LinkProperties} for this network.
Robert Greenwalt7b816022014-04-18 15:25:25 -07002653 */
Robert Greenwalt6078b502014-06-11 16:05:07 -07002654 public void onLinkPropertiesChanged(Network network, LinkProperties linkProperties) {}
Robert Greenwalt7b816022014-04-18 15:25:25 -07002655
Robert Greenwalt8d482522015-06-24 13:23:42 -07002656 /**
2657 * Called when the network the framework connected to for this request
2658 * goes into {@link NetworkInfo.DetailedState.SUSPENDED}.
2659 * This generally means that while the TCP connections are still live,
2660 * temporarily network data fails to transfer. Specifically this is used
2661 * on cellular networks to mask temporary outages when driving through
2662 * a tunnel, etc.
2663 * @hide
2664 */
2665 public void onNetworkSuspended(Network network) {}
2666
2667 /**
2668 * Called when the network the framework connected to for this request
2669 * returns from a {@link NetworkInfo.DetailedState.SUSPENDED} state.
2670 * This should always be preceeded by a matching {@code onNetworkSuspended}
2671 * call.
2672 * @hide
2673 */
2674 public void onNetworkResumed(Network network) {}
2675
Robert Greenwalt6078b502014-06-11 16:05:07 -07002676 private NetworkRequest networkRequest;
Robert Greenwalt7b816022014-04-18 15:25:25 -07002677 }
2678
Robert Greenwalt9258c642014-03-26 16:47:06 -07002679 private static final int BASE = Protocol.BASE_CONNECTIVITY_MANAGER;
Robert Greenwalt9258c642014-03-26 16:47:06 -07002680 /** @hide */
Robert Greenwalt8d482522015-06-24 13:23:42 -07002681 public static final int CALLBACK_PRECHECK = BASE + 1;
2682 /** @hide */
2683 public static final int CALLBACK_AVAILABLE = BASE + 2;
2684 /** @hide arg1 = TTL */
2685 public static final int CALLBACK_LOSING = BASE + 3;
2686 /** @hide */
2687 public static final int CALLBACK_LOST = BASE + 4;
2688 /** @hide */
2689 public static final int CALLBACK_UNAVAIL = BASE + 5;
2690 /** @hide */
2691 public static final int CALLBACK_CAP_CHANGED = BASE + 6;
2692 /** @hide */
2693 public static final int CALLBACK_IP_CHANGED = BASE + 7;
2694 /** @hide */
2695 public static final int CALLBACK_RELEASED = BASE + 8;
Hugo Benichi7724cdd2016-07-07 10:15:56 +09002696 // TODO: consider deleting CALLBACK_EXIT and shifting following enum codes down by 1.
Robert Greenwalt8d482522015-06-24 13:23:42 -07002697 /** @hide */
2698 public static final int CALLBACK_EXIT = BASE + 9;
Robert Greenwalt562cc542014-05-15 18:07:26 -07002699 /** @hide obj = NetworkCapabilities, arg1 = seq number */
Robert Greenwalt8d482522015-06-24 13:23:42 -07002700 private static final int EXPIRE_LEGACY_REQUEST = BASE + 10;
2701 /** @hide */
2702 public static final int CALLBACK_SUSPENDED = BASE + 11;
2703 /** @hide */
2704 public static final int CALLBACK_RESUMED = BASE + 12;
Robert Greenwalt9258c642014-03-26 16:47:06 -07002705
Erik Kline57faba92015-11-25 12:49:38 +09002706 /** @hide */
2707 public static String getCallbackName(int whichCallback) {
2708 switch (whichCallback) {
2709 case CALLBACK_PRECHECK: return "CALLBACK_PRECHECK";
2710 case CALLBACK_AVAILABLE: return "CALLBACK_AVAILABLE";
2711 case CALLBACK_LOSING: return "CALLBACK_LOSING";
2712 case CALLBACK_LOST: return "CALLBACK_LOST";
2713 case CALLBACK_UNAVAIL: return "CALLBACK_UNAVAIL";
2714 case CALLBACK_CAP_CHANGED: return "CALLBACK_CAP_CHANGED";
2715 case CALLBACK_IP_CHANGED: return "CALLBACK_IP_CHANGED";
2716 case CALLBACK_RELEASED: return "CALLBACK_RELEASED";
2717 case CALLBACK_EXIT: return "CALLBACK_EXIT";
2718 case EXPIRE_LEGACY_REQUEST: return "EXPIRE_LEGACY_REQUEST";
2719 case CALLBACK_SUSPENDED: return "CALLBACK_SUSPENDED";
2720 case CALLBACK_RESUMED: return "CALLBACK_RESUMED";
2721 default:
2722 return Integer.toString(whichCallback);
2723 }
2724 }
2725
Robert Greenwalt562cc542014-05-15 18:07:26 -07002726 private class CallbackHandler extends Handler {
Robert Greenwalt9258c642014-03-26 16:47:06 -07002727 private static final String TAG = "ConnectivityManager.CallbackHandler";
Robert Greenwalta9ebeef2015-09-03 16:41:45 -07002728 private static final boolean DBG = false;
Robert Greenwalt9258c642014-03-26 16:47:06 -07002729
Hugo Benichid42650f2016-07-06 22:53:17 +09002730 CallbackHandler(Looper looper) {
Robert Greenwalt9258c642014-03-26 16:47:06 -07002731 super(looper);
Robert Greenwalt9258c642014-03-26 16:47:06 -07002732 }
2733
2734 @Override
2735 public void handleMessage(Message message) {
Hugo Benichid42650f2016-07-06 22:53:17 +09002736 NetworkRequest request = getObject(message, NetworkRequest.class);
2737 Network network = getObject(message, Network.class);
Lorenzo Colittifcfa7d92016-03-01 22:56:37 +09002738 if (DBG) {
2739 Log.d(TAG, whatToString(message.what) + " for network " + network);
2740 }
Robert Greenwalt9258c642014-03-26 16:47:06 -07002741 switch (message.what) {
2742 case CALLBACK_PRECHECK: {
Lorenzo Colittidb95a602015-04-24 15:21:21 +09002743 NetworkCallback callback = getCallback(request, "PRECHECK");
2744 if (callback != null) {
2745 callback.onPreCheck(network);
Robert Greenwalt9258c642014-03-26 16:47:06 -07002746 }
2747 break;
2748 }
2749 case CALLBACK_AVAILABLE: {
Lorenzo Colittidb95a602015-04-24 15:21:21 +09002750 NetworkCallback callback = getCallback(request, "AVAILABLE");
2751 if (callback != null) {
2752 callback.onAvailable(network);
Robert Greenwalt9258c642014-03-26 16:47:06 -07002753 }
2754 break;
2755 }
2756 case CALLBACK_LOSING: {
Lorenzo Colittidb95a602015-04-24 15:21:21 +09002757 NetworkCallback callback = getCallback(request, "LOSING");
2758 if (callback != null) {
2759 callback.onLosing(network, message.arg1);
Robert Greenwalt9258c642014-03-26 16:47:06 -07002760 }
2761 break;
2762 }
2763 case CALLBACK_LOST: {
Lorenzo Colittidb95a602015-04-24 15:21:21 +09002764 NetworkCallback callback = getCallback(request, "LOST");
2765 if (callback != null) {
2766 callback.onLost(network);
Robert Greenwalt9258c642014-03-26 16:47:06 -07002767 }
2768 break;
2769 }
2770 case CALLBACK_UNAVAIL: {
Lorenzo Colittidb95a602015-04-24 15:21:21 +09002771 NetworkCallback callback = getCallback(request, "UNAVAIL");
2772 if (callback != null) {
2773 callback.onUnavailable();
Robert Greenwalt9258c642014-03-26 16:47:06 -07002774 }
2775 break;
2776 }
2777 case CALLBACK_CAP_CHANGED: {
Lorenzo Colittidb95a602015-04-24 15:21:21 +09002778 NetworkCallback callback = getCallback(request, "CAP_CHANGED");
2779 if (callback != null) {
Hugo Benichid42650f2016-07-06 22:53:17 +09002780 NetworkCapabilities cap = getObject(message, NetworkCapabilities.class);
Lorenzo Colittidb95a602015-04-24 15:21:21 +09002781 callback.onCapabilitiesChanged(network, cap);
Robert Greenwalt9258c642014-03-26 16:47:06 -07002782 }
2783 break;
2784 }
2785 case CALLBACK_IP_CHANGED: {
Lorenzo Colittidb95a602015-04-24 15:21:21 +09002786 NetworkCallback callback = getCallback(request, "IP_CHANGED");
2787 if (callback != null) {
Hugo Benichid42650f2016-07-06 22:53:17 +09002788 LinkProperties lp = getObject(message, LinkProperties.class);
Lorenzo Colittidb95a602015-04-24 15:21:21 +09002789 callback.onLinkPropertiesChanged(network, lp);
Robert Greenwalt9258c642014-03-26 16:47:06 -07002790 }
2791 break;
2792 }
Robert Greenwalt8d482522015-06-24 13:23:42 -07002793 case CALLBACK_SUSPENDED: {
2794 NetworkCallback callback = getCallback(request, "SUSPENDED");
2795 if (callback != null) {
2796 callback.onNetworkSuspended(network);
2797 }
2798 break;
2799 }
2800 case CALLBACK_RESUMED: {
2801 NetworkCallback callback = getCallback(request, "RESUMED");
2802 if (callback != null) {
2803 callback.onNetworkResumed(network);
2804 }
2805 break;
2806 }
Robert Greenwalt9258c642014-03-26 16:47:06 -07002807 case CALLBACK_RELEASED: {
Hugo Benichi7724cdd2016-07-07 10:15:56 +09002808 final NetworkCallback callback;
Hugo Benichid42650f2016-07-06 22:53:17 +09002809 synchronized(sCallbacks) {
2810 callback = sCallbacks.remove(request);
Robert Greenwalt9258c642014-03-26 16:47:06 -07002811 }
Hugo Benichi7724cdd2016-07-07 10:15:56 +09002812 if (callback == null) {
Lorenzo Colittidb95a602015-04-24 15:21:21 +09002813 Log.e(TAG, "callback not found for RELEASED message");
Robert Greenwalt9258c642014-03-26 16:47:06 -07002814 }
Robert Greenwalt9258c642014-03-26 16:47:06 -07002815 break;
2816 }
2817 case CALLBACK_EXIT: {
Robert Greenwalt9258c642014-03-26 16:47:06 -07002818 break;
2819 }
Robert Greenwalt562cc542014-05-15 18:07:26 -07002820 case EXPIRE_LEGACY_REQUEST: {
2821 expireRequest((NetworkCapabilities)message.obj, message.arg1);
2822 break;
2823 }
Robert Greenwalt9258c642014-03-26 16:47:06 -07002824 }
2825 }
2826
Hugo Benichid42650f2016-07-06 22:53:17 +09002827 private <T> T getObject(Message msg, Class<T> c) {
2828 return (T) msg.getData().getParcelable(c.getSimpleName());
Robert Greenwalt9258c642014-03-26 16:47:06 -07002829 }
Lorenzo Colittidb95a602015-04-24 15:21:21 +09002830
2831 private NetworkCallback getCallback(NetworkRequest req, String name) {
2832 NetworkCallback callback;
Hugo Benichid42650f2016-07-06 22:53:17 +09002833 synchronized(sCallbacks) {
2834 callback = sCallbacks.get(req);
Robert Greenwalt9258c642014-03-26 16:47:06 -07002835 }
Lorenzo Colittidb95a602015-04-24 15:21:21 +09002836 if (callback == null) {
2837 Log.e(TAG, "callback not found for " + name + " message");
2838 }
2839 return callback;
Robert Greenwalt9258c642014-03-26 16:47:06 -07002840 }
Robert Greenwalt9258c642014-03-26 16:47:06 -07002841 }
2842
Hugo Benichi7724cdd2016-07-07 10:15:56 +09002843 private CallbackHandler getHandler() {
2844 synchronized (sCallbacks) {
2845 if (sCallbackHandler == null) {
2846 sCallbackHandler = new CallbackHandler(ConnectivityThread.getInstanceLooper());
Robert Greenwalt9258c642014-03-26 16:47:06 -07002847 }
Hugo Benichi7724cdd2016-07-07 10:15:56 +09002848 return sCallbackHandler;
Robert Greenwalt9258c642014-03-26 16:47:06 -07002849 }
2850 }
2851
Hugo Benichid42650f2016-07-06 22:53:17 +09002852 static final HashMap<NetworkRequest, NetworkCallback> sCallbacks = new HashMap<>();
Hugo Benichi7724cdd2016-07-07 10:15:56 +09002853 static CallbackHandler sCallbackHandler;
Robert Greenwalt9258c642014-03-26 16:47:06 -07002854
2855 private final static int LISTEN = 1;
2856 private final static int REQUEST = 2;
2857
Robert Greenwalt562cc542014-05-15 18:07:26 -07002858 private NetworkRequest sendRequestForNetwork(NetworkCapabilities need,
Hugo Benichi7724cdd2016-07-07 10:15:56 +09002859 NetworkCallback callback, int timeoutMs, int action, int legacyType) {
2860 return sendRequestForNetwork(need, callback, getHandler(), timeoutMs, action, legacyType);
2861 }
2862
2863 private NetworkRequest sendRequestForNetwork(NetworkCapabilities need,
2864 NetworkCallback callback, Handler handler, int timeoutMs, int action, int legacyType) {
2865 if (callback == null) {
Robert Greenwalt6078b502014-06-11 16:05:07 -07002866 throw new IllegalArgumentException("null NetworkCallback");
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07002867 }
Erik Klinea2d29402016-03-16 15:31:39 +09002868 if (need == null && action != REQUEST) {
2869 throw new IllegalArgumentException("null NetworkCapabilities");
2870 }
Hugo Benichi7724cdd2016-07-07 10:15:56 +09002871 // TODO: throw an exception if callback.networkRequest is not null.
Hugo Benichid42650f2016-07-06 22:53:17 +09002872 // http://b/20701525
2873 final NetworkRequest request;
Robert Greenwalt9258c642014-03-26 16:47:06 -07002874 try {
Hugo Benichid42650f2016-07-06 22:53:17 +09002875 synchronized(sCallbacks) {
Hugo Benichi7724cdd2016-07-07 10:15:56 +09002876 Messenger messenger = new Messenger(handler);
Hugo Benichid42650f2016-07-06 22:53:17 +09002877 Binder binder = new Binder();
Paul Jensen7221cc32014-06-27 11:05:32 -04002878 if (action == LISTEN) {
Hugo Benichid42650f2016-07-06 22:53:17 +09002879 request = mService.listenForNetwork(need, messenger, binder);
Paul Jensen7221cc32014-06-27 11:05:32 -04002880 } else {
Hugo Benichid42650f2016-07-06 22:53:17 +09002881 request = mService.requestNetwork(
2882 need, messenger, timeoutMs, binder, legacyType);
Paul Jensen7221cc32014-06-27 11:05:32 -04002883 }
Hugo Benichid42650f2016-07-06 22:53:17 +09002884 if (request != null) {
Hugo Benichi7724cdd2016-07-07 10:15:56 +09002885 sCallbacks.put(request, callback);
Robert Greenwalt9258c642014-03-26 16:47:06 -07002886 }
Hugo Benichi7724cdd2016-07-07 10:15:56 +09002887 callback.networkRequest = request;
Robert Greenwalt9258c642014-03-26 16:47:06 -07002888 }
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07002889 } catch (RemoteException e) {
2890 throw e.rethrowFromSystemServer();
2891 }
Hugo Benichid42650f2016-07-06 22:53:17 +09002892 return request;
Robert Greenwalt9258c642014-03-26 16:47:06 -07002893 }
2894
2895 /**
Erik Klinea2d29402016-03-16 15:31:39 +09002896 * Helper function to request a network with a particular legacy type.
Lorenzo Colitti7de289f2015-11-25 12:00:52 +09002897 *
2898 * This is temporarily public @hide so it can be called by system code that uses the
2899 * NetworkRequest API to request networks but relies on CONNECTIVITY_ACTION broadcasts for
2900 * instead network notifications.
2901 *
2902 * TODO: update said system code to rely on NetworkCallbacks and make this method private.
2903 *
2904 * @hide
2905 */
Lorenzo Colittid1179462015-11-25 15:47:14 +09002906 public void requestNetwork(NetworkRequest request, NetworkCallback networkCallback,
Lorenzo Colitti7de289f2015-11-25 12:00:52 +09002907 int timeoutMs, int legacyType) {
Lorenzo Colittid1179462015-11-25 15:47:14 +09002908 sendRequestForNetwork(request.networkCapabilities, networkCallback, timeoutMs, REQUEST,
2909 legacyType);
Lorenzo Colitti7de289f2015-11-25 12:00:52 +09002910 }
2911
2912 /**
Lorenzo Colittie285b432015-04-23 15:32:42 +09002913 * Request a network to satisfy a set of {@link android.net.NetworkCapabilities}.
Robert Greenwalt9258c642014-03-26 16:47:06 -07002914 *
2915 * This {@link NetworkRequest} will live until released via
Lorenzo Colitti88bc0bb2016-04-13 22:00:02 +09002916 * {@link #unregisterNetworkCallback(NetworkCallback)} or the calling application exits.
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07002917 * Status of the request can be followed by listening to the various
Robert Greenwalt6078b502014-06-11 16:05:07 -07002918 * callbacks described in {@link NetworkCallback}. The {@link Network}
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07002919 * can be used to direct traffic to the network.
Paul Jensenbb2e0e92015-06-16 15:11:58 -04002920 * <p>It is presently unsupported to request a network with mutable
2921 * {@link NetworkCapabilities} such as
2922 * {@link NetworkCapabilities#NET_CAPABILITY_VALIDATED} or
2923 * {@link NetworkCapabilities#NET_CAPABILITY_CAPTIVE_PORTAL}
2924 * as these {@code NetworkCapabilities} represent states that a particular
2925 * network may never attain, and whether a network will attain these states
2926 * is unknown prior to bringing up the network so the framework does not
2927 * know how to go about satisfing a request with these capabilities.
Lorenzo Colittid5427052015-10-15 16:29:00 +09002928 *
2929 * <p>This method requires the caller to hold either the
2930 * {@link android.Manifest.permission#CHANGE_NETWORK_STATE} permission
2931 * or the ability to modify system settings as determined by
2932 * {@link android.provider.Settings.System#canWrite}.</p>
Robert Greenwalt9258c642014-03-26 16:47:06 -07002933 *
Robert Greenwalt6078b502014-06-11 16:05:07 -07002934 * @param request {@link NetworkRequest} describing this request.
2935 * @param networkCallback The {@link NetworkCallback} to be utilized for this
2936 * request. Note the callback must not be shared - they
2937 * uniquely specify this request.
Paul Jensenbb2e0e92015-06-16 15:11:58 -04002938 * @throws IllegalArgumentException if {@code request} specifies any mutable
2939 * {@code NetworkCapabilities}.
Robert Greenwalt9258c642014-03-26 16:47:06 -07002940 */
Robert Greenwalt6078b502014-06-11 16:05:07 -07002941 public void requestNetwork(NetworkRequest request, NetworkCallback networkCallback) {
Lorenzo Colittid1179462015-11-25 15:47:14 +09002942 requestNetwork(request, networkCallback, 0,
Lorenzo Colitti7de289f2015-11-25 12:00:52 +09002943 inferLegacyTypeForNetworkCapabilities(request.networkCapabilities));
Robert Greenwalt9258c642014-03-26 16:47:06 -07002944 }
2945
2946 /**
Lorenzo Colittie285b432015-04-23 15:32:42 +09002947 * Request a network to satisfy a set of {@link android.net.NetworkCapabilities}, limited
Robert Greenwalt9258c642014-03-26 16:47:06 -07002948 * by a timeout.
2949 *
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07002950 * This function behaves identically to the non-timedout version, but if a suitable
Robert Greenwalt6078b502014-06-11 16:05:07 -07002951 * network is not found within the given time (in milliseconds) the
2952 * {@link NetworkCallback#unavailable} callback is called. The request must
Lorenzo Colitti88bc0bb2016-04-13 22:00:02 +09002953 * still be released normally by calling {@link unregisterNetworkCallback(NetworkCallback)}.
Lorenzo Colittid5427052015-10-15 16:29:00 +09002954 *
2955 * <p>This method requires the caller to hold either the
2956 * {@link android.Manifest.permission#CHANGE_NETWORK_STATE} permission
2957 * or the ability to modify system settings as determined by
2958 * {@link android.provider.Settings.System#canWrite}.</p>
2959 *
Robert Greenwalt6078b502014-06-11 16:05:07 -07002960 * @param request {@link NetworkRequest} describing this request.
2961 * @param networkCallback The callbacks to be utilized for this request. Note
2962 * the callbacks must not be shared - they uniquely specify
2963 * this request.
2964 * @param timeoutMs The time in milliseconds to attempt looking for a suitable network
2965 * before {@link NetworkCallback#unavailable} is called.
Lorenzo Colitti7de289f2015-11-25 12:00:52 +09002966 *
2967 * TODO: Make timeouts work and then unhide this method.
2968 *
Robert Greenwalt9258c642014-03-26 16:47:06 -07002969 * @hide
2970 */
Robert Greenwalt6078b502014-06-11 16:05:07 -07002971 public void requestNetwork(NetworkRequest request, NetworkCallback networkCallback,
2972 int timeoutMs) {
Lorenzo Colittid1179462015-11-25 15:47:14 +09002973 requestNetwork(request, networkCallback, timeoutMs,
Lorenzo Colitti7de289f2015-11-25 12:00:52 +09002974 inferLegacyTypeForNetworkCapabilities(request.networkCapabilities));
Robert Greenwalt9258c642014-03-26 16:47:06 -07002975 }
2976
2977 /**
Robert Greenwalt6078b502014-06-11 16:05:07 -07002978 * The maximum number of milliseconds the framework will look for a suitable network
Robert Greenwalt9258c642014-03-26 16:47:06 -07002979 * during a timeout-equiped call to {@link requestNetwork}.
2980 * {@hide}
2981 */
Robert Greenwalt6078b502014-06-11 16:05:07 -07002982 public final static int MAX_NETWORK_REQUEST_TIMEOUT_MS = 100 * 60 * 1000;
Robert Greenwalt9258c642014-03-26 16:47:06 -07002983
2984 /**
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07002985 * The lookup key for a {@link Network} object included with the intent after
Jeremy Joslin46e3ac82014-11-05 10:32:09 -08002986 * successfully finding a network for the applications request. Retrieve it with
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07002987 * {@link android.content.Intent#getParcelableExtra(String)}.
Jeremy Joslinfcde58f2015-02-11 16:51:13 -08002988 * <p>
Paul Jensen72db88e2015-03-10 10:54:12 -04002989 * Note that if you intend to invoke {@link Network#openConnection(java.net.URL)}
2990 * then you must get a ConnectivityManager instance before doing so.
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07002991 */
Erik Kline90e93072014-11-19 12:12:24 +09002992 public static final String EXTRA_NETWORK = "android.net.extra.NETWORK";
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07002993
2994 /**
Robert Greenwalt6078b502014-06-11 16:05:07 -07002995 * The lookup key for a {@link NetworkRequest} object included with the intent after
Jeremy Joslin46e3ac82014-11-05 10:32:09 -08002996 * successfully finding a network for the applications request. Retrieve it with
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07002997 * {@link android.content.Intent#getParcelableExtra(String)}.
2998 */
Erik Kline90e93072014-11-19 12:12:24 +09002999 public static final String EXTRA_NETWORK_REQUEST = "android.net.extra.NETWORK_REQUEST";
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07003000
3001
3002 /**
Lorenzo Colittie285b432015-04-23 15:32:42 +09003003 * Request a network to satisfy a set of {@link android.net.NetworkCapabilities}.
Robert Greenwalt9258c642014-03-26 16:47:06 -07003004 *
Jeremy Joslin46e3ac82014-11-05 10:32:09 -08003005 * This function behaves identically to the version that takes a NetworkCallback, but instead
Robert Greenwalt6078b502014-06-11 16:05:07 -07003006 * of {@link NetworkCallback} a {@link PendingIntent} is used. This means
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07003007 * the request may outlive the calling application and get called back when a suitable
3008 * network is found.
Robert Greenwalt9258c642014-03-26 16:47:06 -07003009 * <p>
3010 * The operation is an Intent broadcast that goes to a broadcast receiver that
3011 * you registered with {@link Context#registerReceiver} or through the
3012 * &lt;receiver&gt; tag in an AndroidManifest.xml file
3013 * <p>
3014 * The operation Intent is delivered with two extras, a {@link Network} typed
Erik Kline90e93072014-11-19 12:12:24 +09003015 * extra called {@link #EXTRA_NETWORK} and a {@link NetworkRequest}
3016 * typed extra called {@link #EXTRA_NETWORK_REQUEST} containing
Robert Greenwalt9258c642014-03-26 16:47:06 -07003017 * the original requests parameters. It is important to create a new,
Robert Greenwalt6078b502014-06-11 16:05:07 -07003018 * {@link NetworkCallback} based request before completing the processing of the
Robert Greenwalt9258c642014-03-26 16:47:06 -07003019 * Intent to reserve the network or it will be released shortly after the Intent
3020 * is processed.
3021 * <p>
Paul Jensen694f2b82015-06-17 14:15:39 -04003022 * If there is already a request for this Intent registered (with the equality of
Robert Greenwalt9258c642014-03-26 16:47:06 -07003023 * two Intents defined by {@link Intent#filterEquals}), then it will be removed and
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07003024 * replaced by this one, effectively releasing the previous {@link NetworkRequest}.
Robert Greenwalt9258c642014-03-26 16:47:06 -07003025 * <p>
Jeremy Joslin46e3ac82014-11-05 10:32:09 -08003026 * The request may be released normally by calling
3027 * {@link #releaseNetworkRequest(android.app.PendingIntent)}.
Paul Jensenbb2e0e92015-06-16 15:11:58 -04003028 * <p>It is presently unsupported to request a network with either
3029 * {@link NetworkCapabilities#NET_CAPABILITY_VALIDATED} or
3030 * {@link NetworkCapabilities#NET_CAPABILITY_CAPTIVE_PORTAL}
3031 * as these {@code NetworkCapabilities} represent states that a particular
3032 * network may never attain, and whether a network will attain these states
3033 * is unknown prior to bringing up the network so the framework does not
3034 * know how to go about satisfing a request with these capabilities.
Lorenzo Colittid5427052015-10-15 16:29:00 +09003035 *
3036 * <p>This method requires the caller to hold either the
3037 * {@link android.Manifest.permission#CHANGE_NETWORK_STATE} permission
3038 * or the ability to modify system settings as determined by
3039 * {@link android.provider.Settings.System#canWrite}.</p>
3040 *
Robert Greenwalt6078b502014-06-11 16:05:07 -07003041 * @param request {@link NetworkRequest} describing this request.
Robert Greenwalt9258c642014-03-26 16:47:06 -07003042 * @param operation Action to perform when the network is available (corresponds
Robert Greenwalt6078b502014-06-11 16:05:07 -07003043 * to the {@link NetworkCallback#onAvailable} call. Typically
Jeremy Joslin46e3ac82014-11-05 10:32:09 -08003044 * comes from {@link PendingIntent#getBroadcast}. Cannot be null.
Paul Jensenbb2e0e92015-06-16 15:11:58 -04003045 * @throws IllegalArgumentException if {@code request} contains either
3046 * {@link NetworkCapabilities#NET_CAPABILITY_VALIDATED} or
3047 * {@link NetworkCapabilities#NET_CAPABILITY_CAPTIVE_PORTAL}.
Robert Greenwalt9258c642014-03-26 16:47:06 -07003048 */
Robert Greenwalt6078b502014-06-11 16:05:07 -07003049 public void requestNetwork(NetworkRequest request, PendingIntent operation) {
Jeremy Joslin46e3ac82014-11-05 10:32:09 -08003050 checkPendingIntent(operation);
Robert Greenwalt9258c642014-03-26 16:47:06 -07003051 try {
Robert Greenwalt6078b502014-06-11 16:05:07 -07003052 mService.pendingRequestForNetwork(request.networkCapabilities, operation);
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07003053 } catch (RemoteException e) {
3054 throw e.rethrowFromSystemServer();
3055 }
Robert Greenwalt9258c642014-03-26 16:47:06 -07003056 }
3057
3058 /**
Jeremy Joslin46e3ac82014-11-05 10:32:09 -08003059 * Removes a request made via {@link #requestNetwork(NetworkRequest, android.app.PendingIntent)}
3060 * <p>
Lorenzo Colitti88bc0bb2016-04-13 22:00:02 +09003061 * This method has the same behavior as
3062 * {@link #unregisterNetworkCallback(android.app.PendingIntent)} with respect to
Jeremy Joslin46e3ac82014-11-05 10:32:09 -08003063 * releasing network resources and disconnecting.
3064 *
3065 * @param operation A PendingIntent equal (as defined by {@link Intent#filterEquals}) to the
3066 * PendingIntent passed to
3067 * {@link #requestNetwork(NetworkRequest, android.app.PendingIntent)} with the
3068 * corresponding NetworkRequest you'd like to remove. Cannot be null.
3069 */
3070 public void releaseNetworkRequest(PendingIntent operation) {
3071 checkPendingIntent(operation);
3072 try {
3073 mService.releasePendingNetworkRequest(operation);
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07003074 } catch (RemoteException e) {
3075 throw e.rethrowFromSystemServer();
3076 }
Jeremy Joslin46e3ac82014-11-05 10:32:09 -08003077 }
3078
3079 private void checkPendingIntent(PendingIntent intent) {
3080 if (intent == null) {
3081 throw new IllegalArgumentException("PendingIntent cannot be null.");
3082 }
3083 }
3084
3085 /**
Robert Greenwalt9258c642014-03-26 16:47:06 -07003086 * Registers to receive notifications about all networks which satisfy the given
Robert Greenwalt6078b502014-06-11 16:05:07 -07003087 * {@link NetworkRequest}. The callbacks will continue to be called until
Lorenzo Colitti88bc0bb2016-04-13 22:00:02 +09003088 * either the application exits or link #unregisterNetworkCallback(NetworkCallback)} is called.
Paul Jensenb2748922015-05-06 11:10:18 -04003089 * <p>This method requires the caller to hold the permission
3090 * {@link android.Manifest.permission#ACCESS_NETWORK_STATE}.
Robert Greenwalt9258c642014-03-26 16:47:06 -07003091 *
Robert Greenwalt6078b502014-06-11 16:05:07 -07003092 * @param request {@link NetworkRequest} describing this request.
3093 * @param networkCallback The {@link NetworkCallback} that the system will call as suitable
3094 * networks change state.
Robert Greenwalt9258c642014-03-26 16:47:06 -07003095 */
Robert Greenwalt6078b502014-06-11 16:05:07 -07003096 public void registerNetworkCallback(NetworkRequest request, NetworkCallback networkCallback) {
3097 sendRequestForNetwork(request.networkCapabilities, networkCallback, 0, LISTEN, TYPE_NONE);
Robert Greenwalt9258c642014-03-26 16:47:06 -07003098 }
3099
3100 /**
Paul Jensen694f2b82015-06-17 14:15:39 -04003101 * Registers a PendingIntent to be sent when a network is available which satisfies the given
3102 * {@link NetworkRequest}.
3103 *
3104 * This function behaves identically to the version that takes a NetworkCallback, but instead
3105 * of {@link NetworkCallback} a {@link PendingIntent} is used. This means
3106 * the request may outlive the calling application and get called back when a suitable
3107 * network is found.
3108 * <p>
3109 * The operation is an Intent broadcast that goes to a broadcast receiver that
3110 * you registered with {@link Context#registerReceiver} or through the
3111 * &lt;receiver&gt; tag in an AndroidManifest.xml file
3112 * <p>
3113 * The operation Intent is delivered with two extras, a {@link Network} typed
3114 * extra called {@link #EXTRA_NETWORK} and a {@link NetworkRequest}
3115 * typed extra called {@link #EXTRA_NETWORK_REQUEST} containing
3116 * the original requests parameters.
3117 * <p>
3118 * If there is already a request for this Intent registered (with the equality of
3119 * two Intents defined by {@link Intent#filterEquals}), then it will be removed and
3120 * replaced by this one, effectively releasing the previous {@link NetworkRequest}.
3121 * <p>
3122 * The request may be released normally by calling
Paul Jensenf2c1cfe2015-06-30 14:29:18 -04003123 * {@link #unregisterNetworkCallback(android.app.PendingIntent)}.
Paul Jensen694f2b82015-06-17 14:15:39 -04003124 * <p>This method requires the caller to hold the permission
3125 * {@link android.Manifest.permission#ACCESS_NETWORK_STATE}.
3126 * @param request {@link NetworkRequest} describing this request.
3127 * @param operation Action to perform when the network is available (corresponds
3128 * to the {@link NetworkCallback#onAvailable} call. Typically
3129 * comes from {@link PendingIntent#getBroadcast}. Cannot be null.
3130 */
3131 public void registerNetworkCallback(NetworkRequest request, PendingIntent operation) {
3132 checkPendingIntent(operation);
3133 try {
3134 mService.pendingListenForNetwork(request.networkCapabilities, operation);
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07003135 } catch (RemoteException e) {
3136 throw e.rethrowFromSystemServer();
3137 }
Paul Jensen694f2b82015-06-17 14:15:39 -04003138 }
3139
3140 /**
Lorenzo Colitti88bc0bb2016-04-13 22:00:02 +09003141 * Registers to receive notifications about changes in the system default network. The callbacks
3142 * will continue to be called until either the application exits or
3143 * {@link #unregisterNetworkCallback(NetworkCallback)} is called.
Erik Klinea2d29402016-03-16 15:31:39 +09003144 * <p>This method requires the caller to hold the permission
3145 * {@link android.Manifest.permission#ACCESS_NETWORK_STATE}.
3146 *
3147 * @param networkCallback The {@link NetworkCallback} that the system will call as the
3148 * system default network changes.
Erik Klinea2d29402016-03-16 15:31:39 +09003149 */
3150 public void registerDefaultNetworkCallback(NetworkCallback networkCallback) {
3151 // This works because if the NetworkCapabilities are null,
3152 // ConnectivityService takes them from the default request.
3153 //
3154 // Since the capabilities are exactly the same as the default request's
3155 // capabilities, this request is guaranteed, at all times, to be
3156 // satisfied by the same network, if any, that satisfies the default
3157 // request, i.e., the system default network.
3158 sendRequestForNetwork(null, networkCallback, 0, REQUEST, TYPE_NONE);
3159 }
3160
3161 /**
fengludb571472015-04-21 17:12:05 -07003162 * Requests bandwidth update for a given {@link Network} and returns whether the update request
3163 * is accepted by ConnectivityService. Once accepted, ConnectivityService will poll underlying
3164 * network connection for updated bandwidth information. The caller will be notified via
3165 * {@link ConnectivityManager.NetworkCallback} if there is an update. Notice that this
Lorenzo Colitti88bc0bb2016-04-13 22:00:02 +09003166 * method assumes that the caller has previously called
3167 * {@link #registerNetworkCallback(NetworkRequest, NetworkCallback)} to listen for network
3168 * changes.
fenglub15e72b2015-03-20 11:29:56 -07003169 *
fengluae519192015-04-27 14:28:04 -07003170 * @param network {@link Network} specifying which network you're interested.
fengludb571472015-04-21 17:12:05 -07003171 * @return {@code true} on success, {@code false} if the {@link Network} is no longer valid.
fenglub15e72b2015-03-20 11:29:56 -07003172 */
fengludb571472015-04-21 17:12:05 -07003173 public boolean requestBandwidthUpdate(Network network) {
fenglub15e72b2015-03-20 11:29:56 -07003174 try {
fengludb571472015-04-21 17:12:05 -07003175 return mService.requestBandwidthUpdate(network);
fenglub15e72b2015-03-20 11:29:56 -07003176 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07003177 throw e.rethrowFromSystemServer();
fenglub15e72b2015-03-20 11:29:56 -07003178 }
3179 }
3180
3181 /**
Robert Greenwalt6078b502014-06-11 16:05:07 -07003182 * Unregisters callbacks about and possibly releases networks originating from
Lorenzo Colitti88bc0bb2016-04-13 22:00:02 +09003183 * {@link #requestNetwork(NetworkRequest, NetworkCallback)} and
3184 * {@link #registerNetworkCallback(NetworkRequest, NetworkCallback)} calls.
3185 * If the given {@code NetworkCallback} had previously been used with
Lorenzo Colitti2ea89e52015-04-24 17:03:31 +09003186 * {@code #requestNetwork}, any networks that had been connected to only to satisfy that request
3187 * will be disconnected.
Robert Greenwalt9258c642014-03-26 16:47:06 -07003188 *
Robert Greenwalt6078b502014-06-11 16:05:07 -07003189 * @param networkCallback The {@link NetworkCallback} used when making the request.
Robert Greenwalt9258c642014-03-26 16:47:06 -07003190 */
Robert Greenwalt6078b502014-06-11 16:05:07 -07003191 public void unregisterNetworkCallback(NetworkCallback networkCallback) {
3192 if (networkCallback == null || networkCallback.networkRequest == null ||
3193 networkCallback.networkRequest.requestId == REQUEST_ID_UNSET) {
3194 throw new IllegalArgumentException("Invalid NetworkCallback");
3195 }
Robert Greenwalt9258c642014-03-26 16:47:06 -07003196 try {
Hugo Benichi39e10e82016-07-07 09:36:12 +09003197 // CallbackHandler will release callback when receiving CALLBACK_RELEASED.
Robert Greenwalt6078b502014-06-11 16:05:07 -07003198 mService.releaseNetworkRequest(networkCallback.networkRequest);
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07003199 } catch (RemoteException e) {
3200 throw e.rethrowFromSystemServer();
3201 }
Robert Greenwalt9258c642014-03-26 16:47:06 -07003202 }
Paul Jensen6d3ff9e2014-05-29 10:12:39 -04003203
3204 /**
Paul Jensenf2c1cfe2015-06-30 14:29:18 -04003205 * Unregisters a callback previously registered via
3206 * {@link #registerNetworkCallback(NetworkRequest, android.app.PendingIntent)}.
3207 *
3208 * @param operation A PendingIntent equal (as defined by {@link Intent#filterEquals}) to the
3209 * PendingIntent passed to
3210 * {@link #registerNetworkCallback(NetworkRequest, android.app.PendingIntent)}.
3211 * Cannot be null.
3212 */
3213 public void unregisterNetworkCallback(PendingIntent operation) {
3214 releaseNetworkRequest(operation);
3215 }
3216
3217 /**
Lorenzo Colittie03c3c72015-04-03 16:38:52 +09003218 * Informs the system whether it should switch to {@code network} regardless of whether it is
3219 * validated or not. If {@code accept} is true, and the network was explicitly selected by the
3220 * user (e.g., by selecting a Wi-Fi network in the Settings app), then the network will become
3221 * the system default network regardless of any other network that's currently connected. If
3222 * {@code always} is true, then the choice is remembered, so that the next time the user
3223 * connects to this network, the system will switch to it.
3224 *
3225 * <p>This method requires the caller to hold the permission
3226 * {@link android.Manifest.permission#CONNECTIVITY_INTERNAL}
3227 *
3228 * @param network The network to accept.
3229 * @param accept Whether to accept the network even if unvalidated.
3230 * @param always Whether to remember this choice in the future.
3231 *
3232 * @hide
3233 */
3234 public void setAcceptUnvalidated(Network network, boolean accept, boolean always) {
3235 try {
3236 mService.setAcceptUnvalidated(network, accept, always);
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07003237 } catch (RemoteException e) {
3238 throw e.rethrowFromSystemServer();
3239 }
Lorenzo Colittie03c3c72015-04-03 16:38:52 +09003240 }
3241
3242 /**
Lorenzo Colitti165c51c2016-09-19 01:00:19 +09003243 * Informs the system to penalize {@code network}'s score when it becomes unvalidated. This is
3244 * only meaningful if the system is configured not to penalize such networks, e.g., if the
3245 * {@code config_networkAvoidBadWifi} configuration variable is set to 0 and the {@code
3246 * NETWORK_AVOID_BAD_WIFI setting is unset}.
3247 *
3248 * <p>This method requires the caller to hold the permission
3249 * {@link android.Manifest.permission#CONNECTIVITY_INTERNAL}
3250 *
3251 * @param network The network to accept.
3252 *
3253 * @hide
3254 */
3255 public void setAvoidUnvalidated(Network network) {
3256 try {
3257 mService.setAvoidUnvalidated(network);
3258 } catch (RemoteException e) {
3259 throw e.rethrowFromSystemServer();
3260 }
3261 }
3262
3263 /**
Stuart Scott984dc852015-03-30 13:17:11 -07003264 * Resets all connectivity manager settings back to factory defaults.
3265 * @hide
3266 */
3267 public void factoryReset() {
Stuart Scott984dc852015-03-30 13:17:11 -07003268 try {
Stuart Scottf1fb3972015-04-02 18:00:02 -07003269 mService.factoryReset();
Stuart Scott984dc852015-03-30 13:17:11 -07003270 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07003271 throw e.rethrowFromSystemServer();
Stuart Scott984dc852015-03-30 13:17:11 -07003272 }
3273 }
3274
3275 /**
Paul Jensen6d3ff9e2014-05-29 10:12:39 -04003276 * Binds the current process to {@code network}. All Sockets created in the future
3277 * (and not explicitly bound via a bound SocketFactory from
3278 * {@link Network#getSocketFactory() Network.getSocketFactory()}) will be bound to
3279 * {@code network}. All host name resolutions will be limited to {@code network} as well.
3280 * Note that if {@code network} ever disconnects, all Sockets created in this way will cease to
3281 * work and all host name resolutions will fail. This is by design so an application doesn't
3282 * accidentally use Sockets it thinks are still bound to a particular {@link Network}.
3283 * To clear binding pass {@code null} for {@code network}. Using individually bound
3284 * Sockets created by Network.getSocketFactory().createSocket() and
3285 * performing network-specific host name resolutions via
3286 * {@link Network#getAllByName Network.getAllByName} is preferred to calling
Paul Jensen72db88e2015-03-10 10:54:12 -04003287 * {@code bindProcessToNetwork}.
Paul Jensen6d3ff9e2014-05-29 10:12:39 -04003288 *
3289 * @param network The {@link Network} to bind the current process to, or {@code null} to clear
3290 * the current binding.
3291 * @return {@code true} on success, {@code false} if the {@link Network} is no longer valid.
3292 */
Paul Jensen72db88e2015-03-10 10:54:12 -04003293 public boolean bindProcessToNetwork(Network network) {
3294 // Forcing callers to call thru non-static function ensures ConnectivityManager
3295 // instantiated.
3296 return setProcessDefaultNetwork(network);
3297 }
3298
3299 /**
3300 * Binds the current process to {@code network}. All Sockets created in the future
3301 * (and not explicitly bound via a bound SocketFactory from
3302 * {@link Network#getSocketFactory() Network.getSocketFactory()}) will be bound to
3303 * {@code network}. All host name resolutions will be limited to {@code network} as well.
3304 * Note that if {@code network} ever disconnects, all Sockets created in this way will cease to
3305 * work and all host name resolutions will fail. This is by design so an application doesn't
3306 * accidentally use Sockets it thinks are still bound to a particular {@link Network}.
3307 * To clear binding pass {@code null} for {@code network}. Using individually bound
3308 * Sockets created by Network.getSocketFactory().createSocket() and
3309 * performing network-specific host name resolutions via
3310 * {@link Network#getAllByName Network.getAllByName} is preferred to calling
3311 * {@code setProcessDefaultNetwork}.
3312 *
3313 * @param network The {@link Network} to bind the current process to, or {@code null} to clear
3314 * the current binding.
3315 * @return {@code true} on success, {@code false} if the {@link Network} is no longer valid.
3316 * @deprecated This function can throw {@link IllegalStateException}. Use
3317 * {@link #bindProcessToNetwork} instead. {@code bindProcessToNetwork}
3318 * is a direct replacement.
3319 */
Aurimas Liutikas514c5ef2016-05-24 15:22:55 -07003320 @Deprecated
Paul Jensen6d3ff9e2014-05-29 10:12:39 -04003321 public static boolean setProcessDefaultNetwork(Network network) {
Paul Jensenc91b5342014-08-27 12:38:45 -04003322 int netId = (network == null) ? NETID_UNSET : network.netId;
Paul Jensen72db88e2015-03-10 10:54:12 -04003323 if (netId == NetworkUtils.getBoundNetworkForProcess()) {
Paul Jensenc91b5342014-08-27 12:38:45 -04003324 return true;
3325 }
3326 if (NetworkUtils.bindProcessToNetwork(netId)) {
Paul Jensene0bef712014-12-10 15:12:18 -05003327 // Set HTTP proxy system properties to match network.
3328 // TODO: Deprecate this static method and replace it with a non-static version.
Lorenzo Colittiec4c5552015-04-22 11:52:48 +09003329 try {
3330 Proxy.setHttpProxySystemProperty(getInstance().getDefaultProxy());
3331 } catch (SecurityException e) {
3332 // The process doesn't have ACCESS_NETWORK_STATE, so we can't fetch the proxy.
3333 Log.e(TAG, "Can't set proxy properties", e);
3334 }
Paul Jensenc91b5342014-08-27 12:38:45 -04003335 // Must flush DNS cache as new network may have different DNS resolutions.
3336 InetAddress.clearDnsCache();
3337 // Must flush socket pool as idle sockets will be bound to previous network and may
3338 // cause subsequent fetches to be performed on old network.
3339 NetworkEventDispatcher.getInstance().onNetworkConfigurationChanged();
3340 return true;
3341 } else {
3342 return false;
3343 }
Paul Jensen6d3ff9e2014-05-29 10:12:39 -04003344 }
3345
3346 /**
3347 * Returns the {@link Network} currently bound to this process via
Paul Jensen72db88e2015-03-10 10:54:12 -04003348 * {@link #bindProcessToNetwork}, or {@code null} if no {@link Network} is explicitly bound.
Paul Jensen6d3ff9e2014-05-29 10:12:39 -04003349 *
3350 * @return {@code Network} to which this process is bound, or {@code null}.
3351 */
Paul Jensen72db88e2015-03-10 10:54:12 -04003352 public Network getBoundNetworkForProcess() {
3353 // Forcing callers to call thru non-static function ensures ConnectivityManager
3354 // instantiated.
3355 return getProcessDefaultNetwork();
3356 }
3357
3358 /**
3359 * Returns the {@link Network} currently bound to this process via
3360 * {@link #bindProcessToNetwork}, or {@code null} if no {@link Network} is explicitly bound.
3361 *
3362 * @return {@code Network} to which this process is bound, or {@code null}.
3363 * @deprecated Using this function can lead to other functions throwing
3364 * {@link IllegalStateException}. Use {@link #getBoundNetworkForProcess} instead.
3365 * {@code getBoundNetworkForProcess} is a direct replacement.
3366 */
Aurimas Liutikas514c5ef2016-05-24 15:22:55 -07003367 @Deprecated
Paul Jensen6d3ff9e2014-05-29 10:12:39 -04003368 public static Network getProcessDefaultNetwork() {
Paul Jensen72db88e2015-03-10 10:54:12 -04003369 int netId = NetworkUtils.getBoundNetworkForProcess();
Paul Jensenbcc76d32014-07-11 08:17:29 -04003370 if (netId == NETID_UNSET) return null;
Paul Jensen6d3ff9e2014-05-29 10:12:39 -04003371 return new Network(netId);
3372 }
3373
Lorenzo Colittiffc42b02015-07-29 11:41:21 +09003374 private void unsupportedStartingFrom(int version) {
3375 if (Process.myUid() == Process.SYSTEM_UID) {
3376 // The getApplicationInfo() call we make below is not supported in system context, and
3377 // we want to allow the system to use these APIs anyway.
3378 return;
3379 }
3380
3381 if (mContext.getApplicationInfo().targetSdkVersion >= version) {
3382 throw new UnsupportedOperationException(
3383 "This method is not supported in target SDK version " + version + " and above");
3384 }
3385 }
3386
3387 // Checks whether the calling app can use the legacy routing API (startUsingNetworkFeature,
3388 // stopUsingNetworkFeature, requestRouteToHost), and if not throw UnsupportedOperationException.
Lifu Tang30f95a72016-01-07 23:20:38 -08003389 // TODO: convert the existing system users (Tethering, GnssLocationProvider) to the new APIs and
Lorenzo Colittiffc42b02015-07-29 11:41:21 +09003390 // remove these exemptions. Note that this check is not secure, and apps can still access these
3391 // functions by accessing ConnectivityService directly. However, it should be clear that doing
3392 // so is unsupported and may break in the future. http://b/22728205
3393 private void checkLegacyRoutingApiAccess() {
3394 if (mContext.checkCallingOrSelfPermission("com.android.permission.INJECT_OMADM_SETTINGS")
3395 == PackageManager.PERMISSION_GRANTED) {
3396 return;
3397 }
3398
Dianne Hackborn692a2442015-07-31 10:35:34 -07003399 unsupportedStartingFrom(VERSION_CODES.M);
Lorenzo Colittiffc42b02015-07-29 11:41:21 +09003400 }
3401
Paul Jensen6d3ff9e2014-05-29 10:12:39 -04003402 /**
3403 * Binds host resolutions performed by this process to {@code network}.
Paul Jensen72db88e2015-03-10 10:54:12 -04003404 * {@link #bindProcessToNetwork} takes precedence over this setting.
Paul Jensen6d3ff9e2014-05-29 10:12:39 -04003405 *
3406 * @param network The {@link Network} to bind host resolutions from the current process to, or
3407 * {@code null} to clear the current binding.
3408 * @return {@code true} on success, {@code false} if the {@link Network} is no longer valid.
3409 * @hide
3410 * @deprecated This is strictly for legacy usage to support {@link #startUsingNetworkFeature}.
3411 */
Aurimas Liutikas514c5ef2016-05-24 15:22:55 -07003412 @Deprecated
Paul Jensen6d3ff9e2014-05-29 10:12:39 -04003413 public static boolean setProcessDefaultNetworkForHostResolution(Network network) {
Paul Jensenbcc76d32014-07-11 08:17:29 -04003414 return NetworkUtils.bindProcessToNetworkForHostResolution(
3415 network == null ? NETID_UNSET : network.netId);
Paul Jensen6d3ff9e2014-05-29 10:12:39 -04003416 }
Felipe Leme1b103232016-01-22 09:44:57 -08003417
3418 /**
3419 * Device is not restricting metered network activity while application is running on
3420 * background.
3421 */
3422 public static final int RESTRICT_BACKGROUND_STATUS_DISABLED = 1;
3423
3424 /**
3425 * Device is restricting metered network activity while application is running on background,
3426 * but application is allowed to bypass it.
3427 * <p>
3428 * In this state, application should take action to mitigate metered network access.
3429 * For example, a music streaming application should switch to a low-bandwidth bitrate.
3430 */
3431 public static final int RESTRICT_BACKGROUND_STATUS_WHITELISTED = 2;
3432
3433 /**
3434 * Device is restricting metered network activity while application is running on background.
Felipe Leme9778f762016-01-27 14:46:39 -08003435 * <p>
Felipe Leme1b103232016-01-22 09:44:57 -08003436 * In this state, application should not try to use the network while running on background,
3437 * because it would be denied.
3438 */
3439 public static final int RESTRICT_BACKGROUND_STATUS_ENABLED = 3;
3440
Felipe Leme9778f762016-01-27 14:46:39 -08003441 /**
3442 * A change in the background metered network activity restriction has occurred.
3443 * <p>
3444 * Applications should call {@link #getRestrictBackgroundStatus()} to check if the restriction
3445 * applies to them.
3446 * <p>
3447 * This is only sent to registered receivers, not manifest receivers.
3448 */
3449 @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION)
3450 public static final String ACTION_RESTRICT_BACKGROUND_CHANGED =
3451 "android.net.conn.RESTRICT_BACKGROUND_CHANGED";
3452
Felipe Lemeecfccea2016-01-25 11:48:04 -08003453 /** @hide */
3454 @Retention(RetentionPolicy.SOURCE)
Felipe Leme1b103232016-01-22 09:44:57 -08003455 @IntDef(flag = false, value = {
3456 RESTRICT_BACKGROUND_STATUS_DISABLED,
3457 RESTRICT_BACKGROUND_STATUS_WHITELISTED,
3458 RESTRICT_BACKGROUND_STATUS_ENABLED,
3459 })
Felipe Leme1b103232016-01-22 09:44:57 -08003460 public @interface RestrictBackgroundStatus {
3461 }
3462
3463 private INetworkPolicyManager getNetworkPolicyManager() {
3464 synchronized (this) {
3465 if (mNPManager != null) {
3466 return mNPManager;
3467 }
3468 mNPManager = INetworkPolicyManager.Stub.asInterface(ServiceManager
3469 .getService(Context.NETWORK_POLICY_SERVICE));
3470 return mNPManager;
3471 }
3472 }
3473
3474 /**
3475 * Determines if the calling application is subject to metered network restrictions while
3476 * running on background.
Felipe Lemec9c7be52016-05-16 13:57:19 -07003477 *
3478 * @return {@link #RESTRICT_BACKGROUND_STATUS_DISABLED},
3479 * {@link #RESTRICT_BACKGROUND_STATUS_ENABLED},
3480 * or {@link #RESTRICT_BACKGROUND_STATUS_WHITELISTED}
Felipe Leme1b103232016-01-22 09:44:57 -08003481 */
3482 public @RestrictBackgroundStatus int getRestrictBackgroundStatus() {
3483 try {
3484 return getNetworkPolicyManager().getRestrictBackgroundByCaller();
3485 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07003486 throw e.rethrowFromSystemServer();
Felipe Leme1b103232016-01-22 09:44:57 -08003487 }
3488 }
Andreas Gampe34802132016-04-20 14:33:51 -07003489
3490 /**
3491 * A holder class for debug info (mapping CALLBACK values to field names). This is stored
3492 * in a holder for two reasons:
3493 * 1) The reflection necessary to establish the map can't be run at compile-time. Thus, this
3494 * code will make the enclosing class not compile-time initializeable, deferring its
3495 * initialization to zygote startup. This leads to dirty (but shared) memory.
3496 * As this is debug info, use a holder that isn't initialized by default. This way the map
3497 * will be created on demand, while ConnectivityManager can be compile-time initialized.
3498 * 2) Static initialization is still preferred for its strong thread safety guarantees without
3499 * requiring a lock.
3500 */
3501 private static class NoPreloadHolder {
3502 public static final SparseArray<String> sMagicDecoderRing = MessageUtils.findMessageNames(
3503 new Class[]{ConnectivityManager.class}, new String[]{"CALLBACK_"});
3504 }
3505
3506 static {
3507 // When debug is enabled, aggressively initialize the holder by touching the field (which
3508 // will guarantee static initialization).
3509 if (CallbackHandler.DBG) {
3510 Object dummy = NoPreloadHolder.sMagicDecoderRing;
3511 }
3512 }
3513
3514 private static final String whatToString(int what) {
3515 return NoPreloadHolder.sMagicDecoderRing.get(what, Integer.toString(what));
3516 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003517}