blob: 111c50aefc7962b5aca97cdf293a27ed4fbb8715 [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;
Erik Kline35bf06c2017-01-26 18:08:28 +090049import android.util.SparseIntArray;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080050
Robert Greenwaltafa05c02014-05-21 20:04:36 -070051import com.android.internal.telephony.ITelephony;
Robert Greenwalt562cc542014-05-15 18:07:26 -070052import com.android.internal.telephony.PhoneConstants;
Robert Greenwaltafa05c02014-05-21 20:04:36 -070053import com.android.internal.util.Protocol;
Lorenzo Colittifcfa7d92016-03-01 22:56:37 +090054import com.android.internal.util.MessageUtils;
Robert Greenwaltafa05c02014-05-21 20:04:36 -070055
Paul Jensenc91b5342014-08-27 12:38:45 -040056import libcore.net.event.NetworkEventDispatcher;
57
Felipe Leme1b103232016-01-22 09:44:57 -080058import java.lang.annotation.Retention;
59import java.lang.annotation.RetentionPolicy;
Jeremy Kleind42209d2015-12-28 15:11:58 -080060import java.net.InetAddress;
61import java.util.HashMap;
62import java.util.concurrent.atomic.AtomicInteger;
63
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080064/**
65 * Class that answers queries about the state of network connectivity. It also
66 * notifies applications when network connectivity changes. Get an instance
67 * of this class by calling
68 * {@link android.content.Context#getSystemService(String) Context.getSystemService(Context.CONNECTIVITY_SERVICE)}.
69 * <p>
70 * The primary responsibilities of this class are to:
71 * <ol>
72 * <li>Monitor network connections (Wi-Fi, GPRS, UMTS, etc.)</li>
73 * <li>Send broadcast intents when network connectivity changes</li>
74 * <li>Attempt to "fail over" to another network when connectivity to a network
75 * is lost</li>
76 * <li>Provide an API that allows applications to query the coarse-grained or fine-grained
77 * state of the available networks</li>
Robert Greenwaltd19c41c2014-05-18 23:07:25 -070078 * <li>Provide an API that allows applications to request and select networks for their data
79 * traffic</li>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080080 * </ol>
81 */
Jeff Sharkeyc006f1a2011-05-19 17:12:49 -070082public class ConnectivityManager {
83 private static final String TAG = "ConnectivityManager";
84
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080085 /**
Robert Greenwaltd19c41c2014-05-18 23:07:25 -070086 * A change in network connectivity has occurred. A default connection has either
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080087 * been established or lost. The NetworkInfo for the affected network is
88 * sent as an extra; it should be consulted to see what kind of
89 * connectivity event occurred.
90 * <p/>
Mark Lu33ec1062016-12-05 10:57:55 -080091 * Apps targeting Android 7.0 (API level 24) and higher do not receive this
92 * broadcast if they declare the broadcast receiver in their manifest. Apps
93 * will still receive broadcasts if they register their
94 * {@link android.content.BroadcastReceiver} with
95 * {@link android.content.Context#registerReceiver Context.registerReceiver()}
96 * and that context is still valid.
97 * <p/>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080098 * If this is a connection that was the result of failing over from a
99 * disconnected network, then the FAILOVER_CONNECTION boolean extra is
100 * set to true.
101 * <p/>
102 * For a loss of connectivity, if the connectivity manager is attempting
103 * to connect (or has already connected) to another network, the
104 * NetworkInfo for the new network is also passed as an extra. This lets
105 * any receivers of the broadcast know that they should not necessarily
106 * tell the user that no data traffic will be possible. Instead, the
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800107 * receiver should expect another broadcast soon, indicating either that
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800108 * the failover attempt succeeded (and so there is still overall data
109 * connectivity), or that the failover attempt failed, meaning that all
110 * connectivity has been lost.
111 * <p/>
112 * For a disconnect event, the boolean extra EXTRA_NO_CONNECTIVITY
113 * is set to {@code true} if there are no connected networks at all.
114 */
Jeff Sharkey4fa63b22013-02-20 18:21:19 -0800115 @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800116 public static final String CONNECTIVITY_ACTION = "android.net.conn.CONNECTIVITY_CHANGE";
Jeff Sharkeyf0ceede2011-08-02 17:22:34 -0700117
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800118 /**
Robert Greenwalte94a6ff2015-09-01 08:23:04 -0700119 * A temporary hack until SUPL system can get off the legacy APIS.
120 * They do too many network requests and the long list of apps listening
121 * and waking due to the CONNECTIVITY_ACTION bcast makes it expensive.
122 * Use this bcast intent instead for SUPL requests.
123 * @hide
124 */
125 public static final String CONNECTIVITY_ACTION_SUPL =
126 "android.net.conn.CONNECTIVITY_CHANGE_SUPL";
127
128 /**
Paul Jensen25a217c2015-02-27 22:55:47 -0500129 * The device has connected to a network that has presented a captive
130 * portal, which is blocking Internet connectivity. The user was presented
131 * with a notification that network sign in is required,
132 * and the user invoked the notification's action indicating they
Paul Jensen49e3edf2015-05-22 10:50:39 -0400133 * desire to sign in to the network. Apps handling this activity should
Paul Jensen25a217c2015-02-27 22:55:47 -0500134 * facilitate signing in to the network. This action includes a
135 * {@link Network} typed extra called {@link #EXTRA_NETWORK} that represents
136 * the network presenting the captive portal; all communication with the
137 * captive portal must be done using this {@code Network} object.
138 * <p/>
Paul Jensen49e3edf2015-05-22 10:50:39 -0400139 * This activity includes a {@link CaptivePortal} extra named
140 * {@link #EXTRA_CAPTIVE_PORTAL} that can be used to indicate different
141 * outcomes of the captive portal sign in to the system:
142 * <ul>
143 * <li> When the app handling this action believes the user has signed in to
144 * the network and the captive portal has been dismissed, the app should
145 * call {@link CaptivePortal#reportCaptivePortalDismissed} so the system can
146 * reevaluate the network. If reevaluation finds the network no longer
147 * subject to a captive portal, the network may become the default active
148 * data network. </li>
149 * <li> When the app handling this action believes the user explicitly wants
Paul Jensen25a217c2015-02-27 22:55:47 -0500150 * to ignore the captive portal and the network, the app should call
Paul Jensen49e3edf2015-05-22 10:50:39 -0400151 * {@link CaptivePortal#ignoreNetwork}. </li>
152 * </ul>
Paul Jensen25a217c2015-02-27 22:55:47 -0500153 */
154 @SdkConstant(SdkConstantType.ACTIVITY_INTENT_ACTION)
155 public static final String ACTION_CAPTIVE_PORTAL_SIGN_IN = "android.net.conn.CAPTIVE_PORTAL";
156
157 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800158 * The lookup key for a {@link NetworkInfo} object. Retrieve with
159 * {@link android.content.Intent#getParcelableExtra(String)}.
Jeff Sharkeyf0ceede2011-08-02 17:22:34 -0700160 *
161 * @deprecated Since {@link NetworkInfo} can vary based on UID, applications
162 * should always obtain network information through
Paul Jensen3541e9f2015-03-18 12:23:02 -0400163 * {@link #getActiveNetworkInfo()}.
Jeff Sharkey75fbb4b2012-08-06 11:41:50 -0700164 * @see #EXTRA_NETWORK_TYPE
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800165 */
Jeff Sharkeyf0ceede2011-08-02 17:22:34 -0700166 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800167 public static final String EXTRA_NETWORK_INFO = "networkInfo";
Jeff Sharkeyf0ceede2011-08-02 17:22:34 -0700168
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800169 /**
Jeff Sharkey75fbb4b2012-08-06 11:41:50 -0700170 * Network type which triggered a {@link #CONNECTIVITY_ACTION} broadcast.
Jeff Sharkey75fbb4b2012-08-06 11:41:50 -0700171 *
172 * @see android.content.Intent#getIntExtra(String, int)
173 */
174 public static final String EXTRA_NETWORK_TYPE = "networkType";
175
176 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800177 * The lookup key for a boolean that indicates whether a connect event
178 * is for a network to which the connectivity manager was failing over
179 * following a disconnect on another network.
180 * Retrieve it with {@link android.content.Intent#getBooleanExtra(String,boolean)}.
181 */
182 public static final String EXTRA_IS_FAILOVER = "isFailover";
183 /**
184 * The lookup key for a {@link NetworkInfo} object. This is supplied when
185 * there is another network that it may be possible to connect to. Retrieve with
186 * {@link android.content.Intent#getParcelableExtra(String)}.
187 */
188 public static final String EXTRA_OTHER_NETWORK_INFO = "otherNetwork";
189 /**
190 * The lookup key for a boolean that indicates whether there is a
191 * complete lack of connectivity, i.e., no network is available.
192 * Retrieve it with {@link android.content.Intent#getBooleanExtra(String,boolean)}.
193 */
194 public static final String EXTRA_NO_CONNECTIVITY = "noConnectivity";
195 /**
196 * The lookup key for a string that indicates why an attempt to connect
197 * to a network failed. The string has no particular structure. It is
198 * intended to be used in notifications presented to users. Retrieve
199 * it with {@link android.content.Intent#getStringExtra(String)}.
200 */
201 public static final String EXTRA_REASON = "reason";
202 /**
203 * The lookup key for a string that provides optionally supplied
204 * extra information about the network state. The information
205 * may be passed up from the lower networking layers, and its
206 * meaning may be specific to a particular network type. Retrieve
207 * it with {@link android.content.Intent#getStringExtra(String)}.
208 */
209 public static final String EXTRA_EXTRA_INFO = "extraInfo";
Robert Greenwaltd7085fc2010-09-08 15:24:47 -0700210 /**
211 * The lookup key for an int that provides information about
212 * our connection to the internet at large. 0 indicates no connection,
213 * 100 indicates a great connection. Retrieve it with
Jeff Sharkeyc006f1a2011-05-19 17:12:49 -0700214 * {@link android.content.Intent#getIntExtra(String, int)}.
Robert Greenwaltd7085fc2010-09-08 15:24:47 -0700215 * {@hide}
216 */
217 public static final String EXTRA_INET_CONDITION = "inetCondition";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800218 /**
Paul Jensen49e3edf2015-05-22 10:50:39 -0400219 * The lookup key for a {@link CaptivePortal} object included with the
220 * {@link #ACTION_CAPTIVE_PORTAL_SIGN_IN} intent. The {@code CaptivePortal}
221 * object can be used to either indicate to the system that the captive
222 * portal has been dismissed or that the user does not want to pursue
223 * signing in to captive portal. Retrieve it with
224 * {@link android.content.Intent#getParcelableExtra(String)}.
Paul Jensen25a217c2015-02-27 22:55:47 -0500225 */
Paul Jensen49e3edf2015-05-22 10:50:39 -0400226 public static final String EXTRA_CAPTIVE_PORTAL = "android.net.extra.CAPTIVE_PORTAL";
Jan Nordqvist52eb29f2015-09-22 15:54:32 -0700227
228 /**
229 * Key for passing a URL to the captive portal login activity.
230 */
231 public static final String EXTRA_CAPTIVE_PORTAL_URL = "android.net.extra.CAPTIVE_PORTAL_URL";
232
Paul Jensen25a217c2015-02-27 22:55:47 -0500233 /**
Hugo Benichicdf3ba42016-12-14 08:23:40 +0900234 * Key for passing a user agent string to the captive portal login activity.
235 * {@hide}
236 */
237 public static final String EXTRA_CAPTIVE_PORTAL_USER_AGENT =
238 "android.net.extra.CAPTIVE_PORTAL_USER_AGENT";
239
240 /**
Haoyu Baidb3c8672012-06-20 14:29:57 -0700241 * Broadcast action to indicate the change of data activity status
242 * (idle or active) on a network in a recent period.
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800243 * The network becomes active when data transmission is started, or
244 * idle if there is no data transmission for a period of time.
Haoyu Baidb3c8672012-06-20 14:29:57 -0700245 * {@hide}
246 */
247 @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION)
248 public static final String ACTION_DATA_ACTIVITY_CHANGE = "android.net.conn.DATA_ACTIVITY_CHANGE";
249 /**
250 * The lookup key for an enum that indicates the network device type on which this data activity
251 * change happens.
252 * {@hide}
253 */
254 public static final String EXTRA_DEVICE_TYPE = "deviceType";
255 /**
256 * The lookup key for a boolean that indicates the device is active or not. {@code true} means
257 * it is actively sending or receiving data and {@code false} means it is idle.
258 * {@hide}
259 */
260 public static final String EXTRA_IS_ACTIVE = "isActive";
Ashish Sharma0535a9f2014-03-12 18:42:23 -0700261 /**
262 * The lookup key for a long that contains the timestamp (nanos) of the radio state change.
263 * {@hide}
264 */
265 public static final String EXTRA_REALTIME_NS = "tsNanos";
Haoyu Baidb3c8672012-06-20 14:29:57 -0700266
267 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800268 * Broadcast Action: The setting for background data usage has changed
269 * values. Use {@link #getBackgroundDataSetting()} to get the current value.
270 * <p>
271 * If an application uses the network in the background, it should listen
272 * for this broadcast and stop using the background data if the value is
Jeff Sharkeyc006f1a2011-05-19 17:12:49 -0700273 * {@code false}.
Jeff Sharkey54ee2ad2012-01-30 16:29:24 -0800274 * <p>
275 *
276 * @deprecated As of {@link VERSION_CODES#ICE_CREAM_SANDWICH}, availability
277 * of background data depends on several combined factors, and
278 * this broadcast is no longer sent. Instead, when background
279 * data is unavailable, {@link #getActiveNetworkInfo()} will now
280 * appear disconnected. During first boot after a platform
281 * upgrade, this broadcast will be sent once if
282 * {@link #getBackgroundDataSetting()} was {@code false} before
283 * the upgrade.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800284 */
285 @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION)
Jeff Sharkey54ee2ad2012-01-30 16:29:24 -0800286 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800287 public static final String ACTION_BACKGROUND_DATA_SETTING_CHANGED =
288 "android.net.conn.BACKGROUND_DATA_SETTING_CHANGED";
289
Robert Greenwalt1e9aac22010-09-15 17:36:33 -0700290 /**
291 * Broadcast Action: The network connection may not be good
292 * uses {@code ConnectivityManager.EXTRA_INET_CONDITION} and
293 * {@code ConnectivityManager.EXTRA_NETWORK_INFO} to specify
294 * the network and it's condition.
295 * @hide
296 */
Jeff Sharkey4fa63b22013-02-20 18:21:19 -0800297 @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION)
Robert Greenwalt1e9aac22010-09-15 17:36:33 -0700298 public static final String INET_CONDITION_ACTION =
299 "android.net.conn.INET_CONDITION_ACTION";
300
Robert Greenwalt42acef32009-08-12 16:08:25 -0700301 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800302 * Broadcast Action: A tetherable connection has come or gone.
303 * Uses {@code ConnectivityManager.EXTRA_AVAILABLE_TETHER},
304 * {@code ConnectivityManager.EXTRA_ACTIVE_TETHER} and
305 * {@code ConnectivityManager.EXTRA_ERRORED_TETHER} to indicate
306 * the current state of tethering. Each include a list of
307 * interface names in that state (may be empty).
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -0800308 * @hide
309 */
Jeff Sharkey4fa63b22013-02-20 18:21:19 -0800310 @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION)
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -0800311 public static final String ACTION_TETHER_STATE_CHANGED =
312 "android.net.conn.TETHER_STATE_CHANGED";
313
314 /**
315 * @hide
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800316 * gives a String[] listing all the interfaces configured for
317 * tethering and currently available for tethering.
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -0800318 */
Robert Greenwalt2a091d72010-02-11 18:18:40 -0800319 public static final String EXTRA_AVAILABLE_TETHER = "availableArray";
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -0800320
321 /**
322 * @hide
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800323 * gives a String[] listing all the interfaces currently tethered
324 * (ie, has dhcp support and packets potentially forwarded/NATed)
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -0800325 */
Robert Greenwalt2a091d72010-02-11 18:18:40 -0800326 public static final String EXTRA_ACTIVE_TETHER = "activeArray";
327
328 /**
329 * @hide
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800330 * gives a String[] listing all the interfaces we tried to tether and
331 * failed. Use {@link #getLastTetherError} to find the error code
332 * for any interfaces listed here.
Robert Greenwalt2a091d72010-02-11 18:18:40 -0800333 */
334 public static final String EXTRA_ERRORED_TETHER = "erroredArray";
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -0800335
336 /**
Russell Brenner108da0c2013-02-12 10:03:14 -0800337 * Broadcast Action: The captive portal tracker has finished its test.
338 * Sent only while running Setup Wizard, in lieu of showing a user
339 * notification.
340 * @hide
341 */
Jeff Sharkey4fa63b22013-02-20 18:21:19 -0800342 @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION)
Russell Brenner108da0c2013-02-12 10:03:14 -0800343 public static final String ACTION_CAPTIVE_PORTAL_TEST_COMPLETED =
344 "android.net.conn.CAPTIVE_PORTAL_TEST_COMPLETED";
345 /**
346 * The lookup key for a boolean that indicates whether a captive portal was detected.
347 * Retrieve it with {@link android.content.Intent#getBooleanExtra(String,boolean)}.
348 * @hide
349 */
350 public static final String EXTRA_IS_CAPTIVE_PORTAL = "captivePortal";
351
352 /**
Lorenzo Colittie03c3c72015-04-03 16:38:52 +0900353 * Action used to display a dialog that asks the user whether to connect to a network that is
354 * not validated. This intent is used to start the dialog in settings via startActivity.
355 *
356 * @hide
357 */
358 public static final String ACTION_PROMPT_UNVALIDATED = "android.net.conn.PROMPT_UNVALIDATED";
359
360 /**
Lorenzo Colitti9be58c52016-09-15 14:02:29 +0900361 * Action used to display a dialog that asks the user whether to avoid a network that is no
362 * longer validated. This intent is used to start the dialog in settings via startActivity.
363 *
364 * @hide
365 */
366 public static final String ACTION_PROMPT_LOST_VALIDATION =
367 "android.net.conn.PROMPT_LOST_VALIDATION";
368
369 /**
Jeremy Klein36c7aa02016-01-22 14:11:45 -0800370 * Invalid tethering type.
371 * @see #startTethering(int, OnStartTetheringCallback, boolean)
372 * @hide
373 */
374 public static final int TETHERING_INVALID = -1;
375
376 /**
377 * Wifi tethering type.
378 * @see #startTethering(int, OnStartTetheringCallback, boolean)
379 * @hide
380 */
381 @SystemApi
382 public static final int TETHERING_WIFI = 0;
383
384 /**
385 * USB tethering type.
386 * @see #startTethering(int, OnStartTetheringCallback, boolean)
387 * @hide
388 */
389 @SystemApi
390 public static final int TETHERING_USB = 1;
391
392 /**
393 * Bluetooth tethering type.
394 * @see #startTethering(int, OnStartTetheringCallback, boolean)
395 * @hide
396 */
397 @SystemApi
398 public static final int TETHERING_BLUETOOTH = 2;
399
400 /**
401 * Extra used for communicating with the TetherService. Includes the type of tethering to
402 * enable if any.
403 * @hide
404 */
405 public static final String EXTRA_ADD_TETHER_TYPE = "extraAddTetherType";
406
407 /**
408 * Extra used for communicating with the TetherService. Includes the type of tethering for
409 * which to cancel provisioning.
410 * @hide
411 */
412 public static final String EXTRA_REM_TETHER_TYPE = "extraRemTetherType";
413
414 /**
415 * Extra used for communicating with the TetherService. True to schedule a recheck of tether
416 * provisioning.
417 * @hide
418 */
419 public static final String EXTRA_SET_ALARM = "extraSetAlarm";
420
421 /**
422 * Tells the TetherService to run a provision check now.
423 * @hide
424 */
425 public static final String EXTRA_RUN_PROVISION = "extraRunProvision";
426
427 /**
428 * Extra used for communicating with the TetherService. Contains the {@link ResultReceiver}
429 * which will receive provisioning results. Can be left empty.
430 * @hide
431 */
432 public static final String EXTRA_PROVISION_CALLBACK = "extraProvisionCallback";
433
434 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800435 * The absence of a connection type.
Robert Greenwaltccf83af12011-06-02 17:30:47 -0700436 * @hide
437 */
438 public static final int TYPE_NONE = -1;
439
440 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800441 * The Mobile data connection. When active, all data traffic
442 * will use this network type's interface by default
443 * (it has a default route)
Robert Greenwalt42acef32009-08-12 16:08:25 -0700444 */
445 public static final int TYPE_MOBILE = 0;
446 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800447 * The WIFI data connection. When active, all data traffic
448 * will use this network type's interface by default
449 * (it has a default route).
Robert Greenwalt42acef32009-08-12 16:08:25 -0700450 */
451 public static final int TYPE_WIFI = 1;
452 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800453 * An MMS-specific Mobile data connection. This network type may use the
454 * same network interface as {@link #TYPE_MOBILE} or it may use a different
455 * one. This is used by applications needing to talk to the carrier's
456 * Multimedia Messaging Service servers.
Lorenzo Colittie285b432015-04-23 15:32:42 +0900457 *
Lorenzo Colitti2ea89e52015-04-24 17:03:31 +0900458 * @deprecated Applications should instead use
459 * {@link #requestNetwork(NetworkRequest, NetworkCallback)} to request a network that
Lorenzo Colittie285b432015-04-23 15:32:42 +0900460 * provides the {@link NetworkCapabilities#NET_CAPABILITY_MMS} capability.
Robert Greenwalt42acef32009-08-12 16:08:25 -0700461 */
Aurimas Liutikas514c5ef2016-05-24 15:22:55 -0700462 @Deprecated
Robert Greenwalt42acef32009-08-12 16:08:25 -0700463 public static final int TYPE_MOBILE_MMS = 2;
464 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800465 * A SUPL-specific Mobile data connection. This network type may use the
466 * same network interface as {@link #TYPE_MOBILE} or it may use a different
467 * one. This is used by applications needing to talk to the carrier's
468 * Secure User Plane Location servers for help locating the device.
Lorenzo Colittie285b432015-04-23 15:32:42 +0900469 *
Lorenzo Colitti2ea89e52015-04-24 17:03:31 +0900470 * @deprecated Applications should instead use
471 * {@link #requestNetwork(NetworkRequest, NetworkCallback)} to request a network that
Lorenzo Colittie285b432015-04-23 15:32:42 +0900472 * provides the {@link NetworkCapabilities#NET_CAPABILITY_SUPL} capability.
Robert Greenwalt42acef32009-08-12 16:08:25 -0700473 */
Aurimas Liutikas514c5ef2016-05-24 15:22:55 -0700474 @Deprecated
Robert Greenwalt42acef32009-08-12 16:08:25 -0700475 public static final int TYPE_MOBILE_SUPL = 3;
476 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800477 * A DUN-specific Mobile data connection. This network type may use the
478 * same network interface as {@link #TYPE_MOBILE} or it may use a different
479 * one. This is sometimes by the system when setting up an upstream connection
480 * for tethering so that the carrier is aware of DUN traffic.
Robert Greenwalt42acef32009-08-12 16:08:25 -0700481 */
482 public static final int TYPE_MOBILE_DUN = 4;
483 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800484 * A High Priority Mobile data connection. This network type uses the
485 * same network interface as {@link #TYPE_MOBILE} but the routing setup
Lorenzo Colittie285b432015-04-23 15:32:42 +0900486 * is different.
487 *
Lorenzo Colitti2ea89e52015-04-24 17:03:31 +0900488 * @deprecated Applications should instead use
489 * {@link #requestNetwork(NetworkRequest, NetworkCallback)} to request a network that
Lorenzo Colittie285b432015-04-23 15:32:42 +0900490 * uses the {@link NetworkCapabilities#TRANSPORT_CELLULAR} transport.
Robert Greenwalt42acef32009-08-12 16:08:25 -0700491 */
Aurimas Liutikas514c5ef2016-05-24 15:22:55 -0700492 @Deprecated
Robert Greenwalt42acef32009-08-12 16:08:25 -0700493 public static final int TYPE_MOBILE_HIPRI = 5;
jsh8214deb2010-03-11 15:04:43 -0800494 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800495 * The WiMAX data connection. When active, all data traffic
496 * will use this network type's interface by default
497 * (it has a default route).
jsh8214deb2010-03-11 15:04:43 -0800498 */
499 public static final int TYPE_WIMAX = 6;
Robert Greenwaltda3d5e62010-12-06 13:56:24 -0800500
Jaikumar Ganesh15c74392010-12-21 22:31:44 -0800501 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800502 * The Bluetooth data connection. When active, all data traffic
503 * will use this network type's interface by default
504 * (it has a default route).
Jaikumar Ganesh15c74392010-12-21 22:31:44 -0800505 */
506 public static final int TYPE_BLUETOOTH = 7;
507
Robert Greenwalt60810842011-04-22 15:28:18 -0700508 /**
509 * Dummy data connection. This should not be used on shipping devices.
510 */
Jaikumar Ganesh15c74392010-12-21 22:31:44 -0800511 public static final int TYPE_DUMMY = 8;
Wink Saville9d7d6282011-03-12 14:52:01 -0800512
Robert Greenwalt60810842011-04-22 15:28:18 -0700513 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800514 * The Ethernet data connection. When active, all data traffic
515 * will use this network type's interface by default
516 * (it has a default route).
Robert Greenwalt60810842011-04-22 15:28:18 -0700517 */
Robert Greenwalte12aec92011-01-28 14:48:37 -0800518 public static final int TYPE_ETHERNET = 9;
Robert Greenwalt60810842011-04-22 15:28:18 -0700519
Wink Saville9d7d6282011-03-12 14:52:01 -0800520 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800521 * Over the air Administration.
Wink Saville9d7d6282011-03-12 14:52:01 -0800522 * {@hide}
523 */
524 public static final int TYPE_MOBILE_FOTA = 10;
525
526 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800527 * IP Multimedia Subsystem.
Wink Saville9d7d6282011-03-12 14:52:01 -0800528 * {@hide}
529 */
530 public static final int TYPE_MOBILE_IMS = 11;
531
532 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800533 * Carrier Branded Services.
Wink Saville9d7d6282011-03-12 14:52:01 -0800534 * {@hide}
535 */
536 public static final int TYPE_MOBILE_CBS = 12;
537
repo syncaea743a2011-07-29 23:55:49 -0700538 /**
539 * A Wi-Fi p2p connection. Only requesting processes will have access to
540 * the peers connected.
541 * {@hide}
542 */
543 public static final int TYPE_WIFI_P2P = 13;
Wink Saville9d7d6282011-03-12 14:52:01 -0800544
Wink Saville5e56bc52013-07-29 15:00:57 -0700545 /**
546 * The network to use for initially attaching to the network
547 * {@hide}
548 */
549 public static final int TYPE_MOBILE_IA = 14;
repo syncaea743a2011-07-29 23:55:49 -0700550
Lorenzo Colittie285b432015-04-23 15:32:42 +0900551 /**
Robert Greenwalt4bd43892015-07-09 14:49:35 -0700552 * Emergency PDN connection for emergency services. This
553 * may include IMS and MMS in emergency situations.
Ram3e0e3bc2014-06-26 11:03:44 -0700554 * {@hide}
555 */
556 public static final int TYPE_MOBILE_EMERGENCY = 15;
557
Hui Lu1c5624a2014-01-15 11:05:36 -0500558 /**
559 * The network that uses proxy to achieve connectivity.
560 * {@hide}
561 */
562 public static final int TYPE_PROXY = 16;
Wink Saville5e56bc52013-07-29 15:00:57 -0700563
Robert Greenwalt8283f882014-07-07 17:09:01 -0700564 /**
565 * A virtual network using one or more native bearers.
566 * It may or may not be providing security services.
567 */
568 public static final int TYPE_VPN = 17;
Hui Lu1c5624a2014-01-15 11:05:36 -0500569
570 /** {@hide} */
Robert Greenwalt8283f882014-07-07 17:09:01 -0700571 public static final int MAX_RADIO_TYPE = TYPE_VPN;
572
573 /** {@hide} */
574 public static final int MAX_NETWORK_TYPE = TYPE_VPN;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800575
Jianzheng Zhoudcf03f32012-11-16 13:45:20 +0800576 /**
577 * If you want to set the default network preference,you can directly
578 * change the networkAttributes array in framework's config.xml.
579 *
580 * @deprecated Since we support so many more networks now, the single
581 * network default network preference can't really express
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800582 * the hierarchy. Instead, the default is defined by the
Jianzheng Zhoudcf03f32012-11-16 13:45:20 +0800583 * networkAttributes in config.xml. You can determine
Robert Greenwalt4c8b7482012-12-07 09:56:50 -0800584 * the current value by calling {@link #getNetworkPreference()}
Jianzheng Zhoudcf03f32012-11-16 13:45:20 +0800585 * from an App.
586 */
587 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800588 public static final int DEFAULT_NETWORK_PREFERENCE = TYPE_WIFI;
589
Jeff Sharkey625239a2012-09-26 22:03:49 -0700590 /**
Robert Greenwalt9ba9c582014-03-19 17:56:12 -0700591 * @hide
592 */
Robert Greenwalt7569f182014-06-08 16:42:59 -0700593 public final static int REQUEST_ID_UNSET = 0;
594
Paul Jensen5d59e782014-07-11 12:28:19 -0400595 /**
596 * A NetID indicating no Network is selected.
597 * Keep in sync with bionic/libc/dns/include/resolv_netid.h
598 * @hide
599 */
600 public static final int NETID_UNSET = 0;
601
Jeff Sharkeyc006f1a2011-05-19 17:12:49 -0700602 private final IConnectivityManager mService;
Paul Jensene0bef712014-12-10 15:12:18 -0500603 /**
604 * A kludge to facilitate static access where a Context pointer isn't available, like in the
605 * case of the static set/getProcessDefaultNetwork methods and from the Network class.
606 * TODO: Remove this after deprecating the static methods in favor of non-static methods or
607 * methods that take a Context argument.
608 */
609 private static ConnectivityManager sInstance;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800610
Lorenzo Colittiffc42b02015-07-29 11:41:21 +0900611 private final Context mContext;
612
Dianne Hackborn77b987f2014-02-26 16:20:52 -0800613 private INetworkManagementService mNMService;
Felipe Leme1b103232016-01-22 09:44:57 -0800614 private INetworkPolicyManager mNPManager;
Dianne Hackborn77b987f2014-02-26 16:20:52 -0800615
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800616 /**
617 * Tests if a given integer represents a valid network type.
618 * @param networkType the type to be tested
619 * @return a boolean. {@code true} if the type is valid, else {@code false}
Paul Jensen9e59e122015-05-06 10:42:25 -0400620 * @deprecated All APIs accepting a network type are deprecated. There should be no need to
621 * validate a network type.
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800622 */
Aurimas Liutikas514c5ef2016-05-24 15:22:55 -0700623 @Deprecated
Jeff Sharkeyd2a45872011-05-28 20:56:34 -0700624 public static boolean isNetworkTypeValid(int networkType) {
Robert Greenwalt42acef32009-08-12 16:08:25 -0700625 return networkType >= 0 && networkType <= MAX_NETWORK_TYPE;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800626 }
627
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800628 /**
629 * Returns a non-localized string representing a given network type.
630 * ONLY used for debugging output.
631 * @param type the type needing naming
632 * @return a String for the given type, or a string version of the type ("87")
633 * if no name is known.
634 * {@hide}
635 */
Jeff Sharkeyd2a45872011-05-28 20:56:34 -0700636 public static String getNetworkTypeName(int type) {
637 switch (type) {
638 case TYPE_MOBILE:
639 return "MOBILE";
640 case TYPE_WIFI:
641 return "WIFI";
642 case TYPE_MOBILE_MMS:
643 return "MOBILE_MMS";
644 case TYPE_MOBILE_SUPL:
645 return "MOBILE_SUPL";
646 case TYPE_MOBILE_DUN:
647 return "MOBILE_DUN";
648 case TYPE_MOBILE_HIPRI:
649 return "MOBILE_HIPRI";
650 case TYPE_WIMAX:
651 return "WIMAX";
652 case TYPE_BLUETOOTH:
653 return "BLUETOOTH";
654 case TYPE_DUMMY:
655 return "DUMMY";
656 case TYPE_ETHERNET:
657 return "ETHERNET";
658 case TYPE_MOBILE_FOTA:
659 return "MOBILE_FOTA";
660 case TYPE_MOBILE_IMS:
661 return "MOBILE_IMS";
662 case TYPE_MOBILE_CBS:
663 return "MOBILE_CBS";
repo syncaea743a2011-07-29 23:55:49 -0700664 case TYPE_WIFI_P2P:
665 return "WIFI_P2P";
Wink Saville5e56bc52013-07-29 15:00:57 -0700666 case TYPE_MOBILE_IA:
667 return "MOBILE_IA";
Ram3e0e3bc2014-06-26 11:03:44 -0700668 case TYPE_MOBILE_EMERGENCY:
669 return "MOBILE_EMERGENCY";
Hui Lu1c5624a2014-01-15 11:05:36 -0500670 case TYPE_PROXY:
671 return "PROXY";
Erik Kline37fbfa12014-11-19 17:23:41 +0900672 case TYPE_VPN:
673 return "VPN";
Jeff Sharkeyd2a45872011-05-28 20:56:34 -0700674 default:
675 return Integer.toString(type);
676 }
677 }
678
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800679 /**
680 * Checks if a given type uses the cellular data connection.
681 * This should be replaced in the future by a network property.
682 * @param networkType the type to check
683 * @return a boolean - {@code true} if uses cellular network, else {@code false}
684 * {@hide}
685 */
Jeff Sharkeyd2a45872011-05-28 20:56:34 -0700686 public static boolean isNetworkTypeMobile(int networkType) {
687 switch (networkType) {
688 case TYPE_MOBILE:
689 case TYPE_MOBILE_MMS:
690 case TYPE_MOBILE_SUPL:
691 case TYPE_MOBILE_DUN:
692 case TYPE_MOBILE_HIPRI:
693 case TYPE_MOBILE_FOTA:
694 case TYPE_MOBILE_IMS:
695 case TYPE_MOBILE_CBS:
Wink Saville5e56bc52013-07-29 15:00:57 -0700696 case TYPE_MOBILE_IA:
Ram3e0e3bc2014-06-26 11:03:44 -0700697 case TYPE_MOBILE_EMERGENCY:
Jeff Sharkeyd2a45872011-05-28 20:56:34 -0700698 return true;
699 default:
700 return false;
701 }
702 }
703
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800704 /**
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -0700705 * Checks if the given network type is backed by a Wi-Fi radio.
706 *
707 * @hide
708 */
709 public static boolean isNetworkTypeWifi(int networkType) {
710 switch (networkType) {
711 case TYPE_WIFI:
712 case TYPE_WIFI_P2P:
713 return true;
714 default:
715 return false;
716 }
717 }
718
719 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800720 * Specifies the preferred network type. When the device has more
721 * than one type available the preferred network type will be used.
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800722 *
723 * @param preference the network type to prefer over all others. It is
724 * unspecified what happens to the old preferred network in the
725 * overall ordering.
Robert Greenwaltd19c41c2014-05-18 23:07:25 -0700726 * @deprecated Functionality has been removed as it no longer makes sense,
727 * with many more than two networks - we'd need an array to express
728 * preference. Instead we use dynamic network properties of
729 * the networks to describe their precedence.
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800730 */
Aurimas Liutikas514c5ef2016-05-24 15:22:55 -0700731 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800732 public void setNetworkPreference(int preference) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800733 }
734
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800735 /**
736 * Retrieves the current preferred network type.
Paul Jensenb2748922015-05-06 11:10:18 -0400737 * <p>This method requires the caller to hold the permission
738 * {@link android.Manifest.permission#ACCESS_NETWORK_STATE}.
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800739 *
740 * @return an integer representing the preferred network type
741 *
Robert Greenwaltd19c41c2014-05-18 23:07:25 -0700742 * @deprecated Functionality has been removed as it no longer makes sense,
743 * with many more than two networks - we'd need an array to express
744 * preference. Instead we use dynamic network properties of
745 * the networks to describe their precedence.
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800746 */
Aurimas Liutikas514c5ef2016-05-24 15:22:55 -0700747 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800748 public int getNetworkPreference() {
Robert Greenwaltd19c41c2014-05-18 23:07:25 -0700749 return TYPE_NONE;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800750 }
751
Scott Main671644c2011-10-06 19:02:28 -0700752 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800753 * Returns details about the currently active default data network. When
754 * connected, this network is the default route for outgoing connections.
755 * You should always check {@link NetworkInfo#isConnected()} before initiating
756 * network traffic. This may return {@code null} when there is no default
757 * network.
Paul Jensenb2748922015-05-06 11:10:18 -0400758 * <p>This method requires the caller to hold the permission
759 * {@link android.Manifest.permission#ACCESS_NETWORK_STATE}.
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800760 *
761 * @return a {@link NetworkInfo} object for the current default network
Paul Jensen0d719ca2015-02-13 14:18:39 -0500762 * or {@code null} if no default network is currently active
Jeff Sharkey9f7cbf02012-04-12 18:34:54 -0700763 */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800764 public NetworkInfo getActiveNetworkInfo() {
765 try {
766 return mService.getActiveNetworkInfo();
767 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -0700768 throw e.rethrowFromSystemServer();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800769 }
770 }
771
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800772 /**
Paul Jensen31a94f42015-02-13 14:18:39 -0500773 * Returns a {@link Network} object corresponding to the currently active
774 * default data network. In the event that the current active default data
775 * network disconnects, the returned {@code Network} object will no longer
776 * be usable. This will return {@code null} when there is no default
777 * network.
Paul Jensenb2748922015-05-06 11:10:18 -0400778 * <p>This method requires the caller to hold the permission
779 * {@link android.Manifest.permission#ACCESS_NETWORK_STATE}.
Paul Jensen31a94f42015-02-13 14:18:39 -0500780 *
781 * @return a {@link Network} object for the current default network or
782 * {@code null} if no default network is currently active
Paul Jensen31a94f42015-02-13 14:18:39 -0500783 */
784 public Network getActiveNetwork() {
785 try {
786 return mService.getActiveNetwork();
787 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -0700788 throw e.rethrowFromSystemServer();
Paul Jensen31a94f42015-02-13 14:18:39 -0500789 }
790 }
791
792 /**
Robin Leed2baf792016-03-24 12:07:00 +0000793 * Returns a {@link Network} object corresponding to the currently active
794 * default data network for a specific UID. In the event that the default data
795 * network disconnects, the returned {@code Network} object will no longer
796 * be usable. This will return {@code null} when there is no default
797 * network for the UID.
798 * <p>This method requires the caller to hold the permission
799 * {@link android.Manifest.permission#CONNECTIVITY_INTERNAL}.
800 *
801 * @return a {@link Network} object for the current default network for the
802 * given UID or {@code null} if no default network is currently active
803 *
804 * @hide
805 */
806 public Network getActiveNetworkForUid(int uid) {
Jeff Sharkey1b6519b2016-04-28 15:33:18 -0600807 return getActiveNetworkForUid(uid, false);
808 }
809
810 /** {@hide} */
811 public Network getActiveNetworkForUid(int uid, boolean ignoreBlocked) {
Robin Leed2baf792016-03-24 12:07:00 +0000812 try {
Jeff Sharkey1b6519b2016-04-28 15:33:18 -0600813 return mService.getActiveNetworkForUid(uid, ignoreBlocked);
Robin Leed2baf792016-03-24 12:07:00 +0000814 } catch (RemoteException e) {
815 throw e.rethrowFromSystemServer();
816 }
817 }
818
819 /**
Robin Lee244ce8e2016-01-05 18:03:46 +0000820 * Configures an always-on VPN connection through a specific application.
821 * This connection is automatically granted and persisted after a reboot.
822 *
823 * <p>The designated package should declare a {@link VpnService} in its
824 * manifest guarded by {@link android.Manifest.permission.BIND_VPN_SERVICE},
825 * otherwise the call will fail.
826 *
827 * @param userId The identifier of the user to set an always-on VPN for.
828 * @param vpnPackage The package name for an installed VPN app on the device, or {@code null}
829 * to remove an existing always-on VPN configuration.
Robin Leedc679712016-05-03 13:23:03 +0100830 * @param lockdownEnabled {@code true} to disallow networking when the VPN is not connected or
831 * {@code false} otherwise.
Robin Lee244ce8e2016-01-05 18:03:46 +0000832 * @return {@code true} if the package is set as always-on VPN controller;
833 * {@code false} otherwise.
834 * @hide
835 */
Robin Leedc679712016-05-03 13:23:03 +0100836 public boolean setAlwaysOnVpnPackageForUser(int userId, @Nullable String vpnPackage,
837 boolean lockdownEnabled) {
Robin Lee244ce8e2016-01-05 18:03:46 +0000838 try {
Robin Leedc679712016-05-03 13:23:03 +0100839 return mService.setAlwaysOnVpnPackage(userId, vpnPackage, lockdownEnabled);
Robin Lee244ce8e2016-01-05 18:03:46 +0000840 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -0700841 throw e.rethrowFromSystemServer();
Robin Lee244ce8e2016-01-05 18:03:46 +0000842 }
843 }
844
845 /**
846 * Returns the package name of the currently set always-on VPN application.
847 * If there is no always-on VPN set, or the VPN is provided by the system instead
848 * of by an app, {@code null} will be returned.
849 *
850 * @return Package name of VPN controller responsible for always-on VPN,
851 * or {@code null} if none is set.
852 * @hide
853 */
854 public String getAlwaysOnVpnPackageForUser(int userId) {
855 try {
856 return mService.getAlwaysOnVpnPackage(userId);
857 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -0700858 throw e.rethrowFromSystemServer();
Robin Lee244ce8e2016-01-05 18:03:46 +0000859 }
860 }
861
862 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800863 * Returns details about the currently active default data network
864 * for a given uid. This is for internal use only to avoid spying
865 * other apps.
Paul Jensenb2748922015-05-06 11:10:18 -0400866 * <p>This method requires the caller to hold the permission
867 * {@link android.Manifest.permission#CONNECTIVITY_INTERNAL}
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800868 *
869 * @return a {@link NetworkInfo} object for the current default network
870 * for the given uid or {@code null} if no default network is
871 * available for the specified uid.
872 *
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800873 * {@hide}
874 */
Jeff Sharkeyc006f1a2011-05-19 17:12:49 -0700875 public NetworkInfo getActiveNetworkInfoForUid(int uid) {
Jeff Sharkey1b6519b2016-04-28 15:33:18 -0600876 return getActiveNetworkInfoForUid(uid, false);
877 }
878
879 /** {@hide} */
880 public NetworkInfo getActiveNetworkInfoForUid(int uid, boolean ignoreBlocked) {
Jeff Sharkeyc006f1a2011-05-19 17:12:49 -0700881 try {
Jeff Sharkey1b6519b2016-04-28 15:33:18 -0600882 return mService.getActiveNetworkInfoForUid(uid, ignoreBlocked);
Jeff Sharkeyc006f1a2011-05-19 17:12:49 -0700883 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -0700884 throw e.rethrowFromSystemServer();
Jeff Sharkeyc006f1a2011-05-19 17:12:49 -0700885 }
886 }
887
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800888 /**
889 * Returns connection status information about a particular
890 * network type.
Paul Jensenb2748922015-05-06 11:10:18 -0400891 * <p>This method requires the caller to hold the permission
892 * {@link android.Manifest.permission#ACCESS_NETWORK_STATE}.
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800893 *
894 * @param networkType integer specifying which networkType in
895 * which you're interested.
896 * @return a {@link NetworkInfo} object for the requested
897 * network type or {@code null} if the type is not
898 * supported by the device.
899 *
Paul Jensen3541e9f2015-03-18 12:23:02 -0400900 * @deprecated This method does not support multiple connected networks
901 * of the same type. Use {@link #getAllNetworks} and
902 * {@link #getNetworkInfo(android.net.Network)} instead.
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800903 */
Aurimas Liutikas514c5ef2016-05-24 15:22:55 -0700904 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800905 public NetworkInfo getNetworkInfo(int networkType) {
906 try {
907 return mService.getNetworkInfo(networkType);
908 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -0700909 throw e.rethrowFromSystemServer();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800910 }
911 }
912
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800913 /**
Robert Greenwalt73b6cbae2014-06-23 11:40:00 -0700914 * Returns connection status information about a particular
915 * Network.
Paul Jensenb2748922015-05-06 11:10:18 -0400916 * <p>This method requires the caller to hold the permission
917 * {@link android.Manifest.permission#ACCESS_NETWORK_STATE}.
Robert Greenwalt73b6cbae2014-06-23 11:40:00 -0700918 *
919 * @param network {@link Network} specifying which network
920 * in which you're interested.
921 * @return a {@link NetworkInfo} object for the requested
922 * network or {@code null} if the {@code Network}
923 * is not valid.
Robert Greenwalt73b6cbae2014-06-23 11:40:00 -0700924 */
925 public NetworkInfo getNetworkInfo(Network network) {
Jeff Sharkey1b6519b2016-04-28 15:33:18 -0600926 return getNetworkInfoForUid(network, Process.myUid(), false);
927 }
928
929 /** {@hide} */
930 public NetworkInfo getNetworkInfoForUid(Network network, int uid, boolean ignoreBlocked) {
Robert Greenwalt73b6cbae2014-06-23 11:40:00 -0700931 try {
Jeff Sharkey1b6519b2016-04-28 15:33:18 -0600932 return mService.getNetworkInfoForUid(network, uid, ignoreBlocked);
Robert Greenwalt73b6cbae2014-06-23 11:40:00 -0700933 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -0700934 throw e.rethrowFromSystemServer();
Robert Greenwalt73b6cbae2014-06-23 11:40:00 -0700935 }
936 }
937
938 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800939 * Returns connection status information about all network
940 * types supported by the device.
Paul Jensenb2748922015-05-06 11:10:18 -0400941 * <p>This method requires the caller to hold the permission
942 * {@link android.Manifest.permission#ACCESS_NETWORK_STATE}.
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800943 *
944 * @return an array of {@link NetworkInfo} objects. Check each
945 * {@link NetworkInfo#getType} for which type each applies.
946 *
Paul Jensen3541e9f2015-03-18 12:23:02 -0400947 * @deprecated This method does not support multiple connected networks
948 * of the same type. Use {@link #getAllNetworks} and
949 * {@link #getNetworkInfo(android.net.Network)} instead.
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800950 */
Aurimas Liutikas514c5ef2016-05-24 15:22:55 -0700951 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800952 public NetworkInfo[] getAllNetworkInfo() {
953 try {
954 return mService.getAllNetworkInfo();
955 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -0700956 throw e.rethrowFromSystemServer();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800957 }
958 }
959
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800960 /**
Lorenzo Colittib57edc52014-08-22 17:10:50 -0700961 * Returns the {@link Network} object currently serving a given type, or
962 * null if the given type is not connected.
963 *
964 * <p>This method requires the caller to hold the permission
965 * {@link android.Manifest.permission#ACCESS_NETWORK_STATE}.
966 *
967 * @hide
Paul Jensen3541e9f2015-03-18 12:23:02 -0400968 * @deprecated This method does not support multiple connected networks
969 * of the same type. Use {@link #getAllNetworks} and
970 * {@link #getNetworkInfo(android.net.Network)} instead.
Lorenzo Colittib57edc52014-08-22 17:10:50 -0700971 */
Aurimas Liutikas514c5ef2016-05-24 15:22:55 -0700972 @Deprecated
Lorenzo Colittib57edc52014-08-22 17:10:50 -0700973 public Network getNetworkForType(int networkType) {
974 try {
975 return mService.getNetworkForType(networkType);
976 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -0700977 throw e.rethrowFromSystemServer();
Lorenzo Colittib57edc52014-08-22 17:10:50 -0700978 }
979 }
980
981 /**
Robert Greenwalt73b6cbae2014-06-23 11:40:00 -0700982 * Returns an array of all {@link Network} currently tracked by the
983 * framework.
Robert Greenwalt73b6cbae2014-06-23 11:40:00 -0700984 * <p>This method requires the caller to hold the permission
985 * {@link android.Manifest.permission#ACCESS_NETWORK_STATE}.
Paul Jensenb2748922015-05-06 11:10:18 -0400986 *
987 * @return an array of {@link Network} objects.
Robert Greenwalt73b6cbae2014-06-23 11:40:00 -0700988 */
989 public Network[] getAllNetworks() {
990 try {
991 return mService.getAllNetworks();
992 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -0700993 throw e.rethrowFromSystemServer();
Robert Greenwalt73b6cbae2014-06-23 11:40:00 -0700994 }
995 }
996
997 /**
Lorenzo Colittie285b432015-04-23 15:32:42 +0900998 * Returns an array of {@link android.net.NetworkCapabilities} objects, representing
Lorenzo Colitti403aa262014-11-28 11:21:30 +0900999 * the Networks that applications run by the given user will use by default.
1000 * @hide
1001 */
1002 public NetworkCapabilities[] getDefaultNetworkCapabilitiesForUser(int userId) {
1003 try {
1004 return mService.getDefaultNetworkCapabilitiesForUser(userId);
1005 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07001006 throw e.rethrowFromSystemServer();
Lorenzo Colitti403aa262014-11-28 11:21:30 +09001007 }
1008 }
1009
1010 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08001011 * Returns the IP information for the current default network.
Paul Jensenb2748922015-05-06 11:10:18 -04001012 * <p>This method requires the caller to hold the permission
1013 * {@link android.Manifest.permission#ACCESS_NETWORK_STATE}.
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08001014 *
1015 * @return a {@link LinkProperties} object describing the IP info
1016 * for the current default network, or {@code null} if there
1017 * is no current default network.
1018 *
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08001019 * {@hide}
1020 */
Robert Greenwaltd192dad2010-09-14 09:18:02 -07001021 public LinkProperties getActiveLinkProperties() {
1022 try {
1023 return mService.getActiveLinkProperties();
1024 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07001025 throw e.rethrowFromSystemServer();
Robert Greenwaltd192dad2010-09-14 09:18:02 -07001026 }
1027 }
1028
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08001029 /**
1030 * Returns the IP information for a given network type.
Paul Jensenb2748922015-05-06 11:10:18 -04001031 * <p>This method requires the caller to hold the permission
1032 * {@link android.Manifest.permission#ACCESS_NETWORK_STATE}.
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08001033 *
1034 * @param networkType the network type of interest.
1035 * @return a {@link LinkProperties} object describing the IP info
1036 * for the given networkType, or {@code null} if there is
1037 * no current default network.
1038 *
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08001039 * {@hide}
Paul Jensen3541e9f2015-03-18 12:23:02 -04001040 * @deprecated This method does not support multiple connected networks
1041 * of the same type. Use {@link #getAllNetworks},
1042 * {@link #getNetworkInfo(android.net.Network)}, and
1043 * {@link #getLinkProperties(android.net.Network)} instead.
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08001044 */
Aurimas Liutikas514c5ef2016-05-24 15:22:55 -07001045 @Deprecated
Robert Greenwaltd192dad2010-09-14 09:18:02 -07001046 public LinkProperties getLinkProperties(int networkType) {
1047 try {
Robert Greenwalt9258c642014-03-26 16:47:06 -07001048 return mService.getLinkPropertiesForType(networkType);
1049 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07001050 throw e.rethrowFromSystemServer();
Robert Greenwalt9258c642014-03-26 16:47:06 -07001051 }
1052 }
1053
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07001054 /**
1055 * Get the {@link LinkProperties} for the given {@link Network}. This
1056 * will return {@code null} if the network is unknown.
Paul Jensenb2748922015-05-06 11:10:18 -04001057 * <p>This method requires the caller to hold the permission
1058 * {@link android.Manifest.permission#ACCESS_NETWORK_STATE}.
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07001059 *
1060 * @param network The {@link Network} object identifying the network in question.
1061 * @return The {@link LinkProperties} for the network, or {@code null}.
Paul Jensenb2748922015-05-06 11:10:18 -04001062 */
Robert Greenwalt9258c642014-03-26 16:47:06 -07001063 public LinkProperties getLinkProperties(Network network) {
1064 try {
1065 return mService.getLinkProperties(network);
1066 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07001067 throw e.rethrowFromSystemServer();
Robert Greenwalt9258c642014-03-26 16:47:06 -07001068 }
1069 }
1070
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07001071 /**
Lorenzo Colittie285b432015-04-23 15:32:42 +09001072 * Get the {@link android.net.NetworkCapabilities} for the given {@link Network}. This
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07001073 * will return {@code null} if the network is unknown.
Paul Jensenb2748922015-05-06 11:10:18 -04001074 * <p>This method requires the caller to hold the permission
1075 * {@link android.Manifest.permission#ACCESS_NETWORK_STATE}.
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07001076 *
1077 * @param network The {@link Network} object identifying the network in question.
Lorenzo Colittie285b432015-04-23 15:32:42 +09001078 * @return The {@link android.net.NetworkCapabilities} for the network, or {@code null}.
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07001079 */
Robert Greenwalt9258c642014-03-26 16:47:06 -07001080 public NetworkCapabilities getNetworkCapabilities(Network network) {
1081 try {
1082 return mService.getNetworkCapabilities(network);
Robert Greenwaltd192dad2010-09-14 09:18:02 -07001083 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07001084 throw e.rethrowFromSystemServer();
Robert Greenwaltd192dad2010-09-14 09:18:02 -07001085 }
1086 }
1087
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08001088 /**
Udam Sainib7c24872016-01-04 12:16:14 -08001089 * Gets the URL that should be used for resolving whether a captive portal is present.
1090 * 1. This URL should respond with a 204 response to a GET request to indicate no captive
1091 * portal is present.
1092 * 2. This URL must be HTTP as redirect responses are used to find captive portal
1093 * sign-in pages. Captive portals cannot respond to HTTPS requests with redirects.
1094 *
1095 * @hide
1096 */
1097 @SystemApi
1098 public String getCaptivePortalServerUrl() {
1099 try {
1100 return mService.getCaptivePortalServerUrl();
1101 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07001102 throw e.rethrowFromSystemServer();
Udam Sainib7c24872016-01-04 12:16:14 -08001103 }
1104 }
1105
1106 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001107 * Tells the underlying networking system that the caller wants to
1108 * begin using the named feature. The interpretation of {@code feature}
1109 * is completely up to each networking implementation.
Lorenzo Colittid5427052015-10-15 16:29:00 +09001110 *
1111 * <p>This method requires the caller to hold either the
1112 * {@link android.Manifest.permission#CHANGE_NETWORK_STATE} permission
1113 * or the ability to modify system settings as determined by
1114 * {@link android.provider.Settings.System#canWrite}.</p>
1115 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001116 * @param networkType specifies which network the request pertains to
1117 * @param feature the name of the feature to be used
1118 * @return an integer value representing the outcome of the request.
1119 * The interpretation of this value is specific to each networking
1120 * implementation+feature combination, except that the value {@code -1}
1121 * always indicates failure.
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07001122 *
Lorenzo Colitti2ea89e52015-04-24 17:03:31 +09001123 * @deprecated Deprecated in favor of the cleaner
1124 * {@link #requestNetwork(NetworkRequest, NetworkCallback)} API.
Dianne Hackborn692a2442015-07-31 10:35:34 -07001125 * In {@link VERSION_CODES#M}, and above, this method is unsupported and will
Lorenzo Colittiffc42b02015-07-29 11:41:21 +09001126 * throw {@code UnsupportedOperationException} if called.
Lorenzo Colitti2187df72016-12-09 18:39:30 +09001127 * @removed
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001128 */
Aurimas Liutikas514c5ef2016-05-24 15:22:55 -07001129 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001130 public int startUsingNetworkFeature(int networkType, String feature) {
Lorenzo Colittiffc42b02015-07-29 11:41:21 +09001131 checkLegacyRoutingApiAccess();
Robert Greenwalt562cc542014-05-15 18:07:26 -07001132 NetworkCapabilities netCap = networkCapabilitiesForFeature(networkType, feature);
1133 if (netCap == null) {
1134 Log.d(TAG, "Can't satisfy startUsingNetworkFeature for " + networkType + ", " +
1135 feature);
1136 return PhoneConstants.APN_REQUEST_FAILED;
1137 }
1138
1139 NetworkRequest request = null;
1140 synchronized (sLegacyRequests) {
1141 LegacyRequest l = sLegacyRequests.get(netCap);
1142 if (l != null) {
1143 Log.d(TAG, "renewing startUsingNetworkFeature request " + l.networkRequest);
1144 renewRequestLocked(l);
1145 if (l.currentNetwork != null) {
1146 return PhoneConstants.APN_ALREADY_ACTIVE;
1147 } else {
1148 return PhoneConstants.APN_REQUEST_STARTED;
1149 }
1150 }
1151
1152 request = requestNetworkForFeatureLocked(netCap);
1153 }
1154 if (request != null) {
Robert Greenwalt257ee5f2014-06-20 10:58:45 -07001155 Log.d(TAG, "starting startUsingNetworkFeature for request " + request);
Robert Greenwalt562cc542014-05-15 18:07:26 -07001156 return PhoneConstants.APN_REQUEST_STARTED;
1157 } else {
1158 Log.d(TAG, " request Failed");
1159 return PhoneConstants.APN_REQUEST_FAILED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001160 }
1161 }
1162
1163 /**
1164 * Tells the underlying networking system that the caller is finished
1165 * using the named feature. The interpretation of {@code feature}
1166 * is completely up to each networking implementation.
Lorenzo Colittid5427052015-10-15 16:29:00 +09001167 *
1168 * <p>This method requires the caller to hold either the
1169 * {@link android.Manifest.permission#CHANGE_NETWORK_STATE} permission
1170 * or the ability to modify system settings as determined by
1171 * {@link android.provider.Settings.System#canWrite}.</p>
1172 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001173 * @param networkType specifies which network the request pertains to
1174 * @param feature the name of the feature that is no longer needed
1175 * @return an integer value representing the outcome of the request.
1176 * The interpretation of this value is specific to each networking
1177 * implementation+feature combination, except that the value {@code -1}
1178 * always indicates failure.
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07001179 *
Lorenzo Colitti88bc0bb2016-04-13 22:00:02 +09001180 * @deprecated Deprecated in favor of the cleaner
1181 * {@link #unregisterNetworkCallback(NetworkCallback)} API.
Dianne Hackborn692a2442015-07-31 10:35:34 -07001182 * In {@link VERSION_CODES#M}, and above, this method is unsupported and will
Lorenzo Colittiffc42b02015-07-29 11:41:21 +09001183 * throw {@code UnsupportedOperationException} if called.
Lorenzo Colitti2187df72016-12-09 18:39:30 +09001184 * @removed
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001185 */
Aurimas Liutikas514c5ef2016-05-24 15:22:55 -07001186 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001187 public int stopUsingNetworkFeature(int networkType, String feature) {
Lorenzo Colittiffc42b02015-07-29 11:41:21 +09001188 checkLegacyRoutingApiAccess();
Robert Greenwalt562cc542014-05-15 18:07:26 -07001189 NetworkCapabilities netCap = networkCapabilitiesForFeature(networkType, feature);
1190 if (netCap == null) {
1191 Log.d(TAG, "Can't satisfy stopUsingNetworkFeature for " + networkType + ", " +
1192 feature);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001193 return -1;
1194 }
Robert Greenwalt562cc542014-05-15 18:07:26 -07001195
Paul Jensen9ffb53c2014-12-17 10:39:34 -05001196 if (removeRequestForFeature(netCap)) {
Robert Greenwalt562cc542014-05-15 18:07:26 -07001197 Log.d(TAG, "stopUsingNetworkFeature for " + networkType + ", " + feature);
Robert Greenwalt562cc542014-05-15 18:07:26 -07001198 }
1199 return 1;
1200 }
1201
1202 private NetworkCapabilities networkCapabilitiesForFeature(int networkType, String feature) {
1203 if (networkType == TYPE_MOBILE) {
Erik Kline35bf06c2017-01-26 18:08:28 +09001204 switch (feature) {
1205 case "enableCBS":
1206 return networkCapabilitiesForType(TYPE_MOBILE_CBS);
1207 case "enableDUN":
1208 case "enableDUNAlways":
1209 return networkCapabilitiesForType(TYPE_MOBILE_DUN);
1210 case "enableFOTA":
1211 return networkCapabilitiesForType(TYPE_MOBILE_FOTA);
1212 case "enableHIPRI":
1213 return networkCapabilitiesForType(TYPE_MOBILE_HIPRI);
1214 case "enableIMS":
1215 return networkCapabilitiesForType(TYPE_MOBILE_IMS);
1216 case "enableMMS":
1217 return networkCapabilitiesForType(TYPE_MOBILE_MMS);
1218 case "enableSUPL":
1219 return networkCapabilitiesForType(TYPE_MOBILE_SUPL);
1220 default:
1221 return null;
Robert Greenwalt562cc542014-05-15 18:07:26 -07001222 }
Erik Kline35bf06c2017-01-26 18:08:28 +09001223 } else if (networkType == TYPE_WIFI && "p2p".equals(feature)) {
1224 return networkCapabilitiesForType(TYPE_WIFI_P2P);
Robert Greenwalt562cc542014-05-15 18:07:26 -07001225 }
1226 return null;
1227 }
1228
Robert Greenwalt06314e42014-10-29 14:04:06 -07001229 /**
1230 * Guess what the network request was trying to say so that the resulting
1231 * network is accessible via the legacy (deprecated) API such as
1232 * requestRouteToHost.
Lorenzo Colittidef4cb02015-11-25 20:28:50 +09001233 *
1234 * This means we should try to be fairly precise about transport and
Robert Greenwalt06314e42014-10-29 14:04:06 -07001235 * capability but ignore things such as networkSpecifier.
1236 * If the request has more than one transport or capability it doesn't
1237 * match the old legacy requests (they selected only single transport/capability)
1238 * so this function cannot map the request to a single legacy type and
1239 * the resulting network will not be available to the legacy APIs.
1240 *
Lorenzo Colittidef4cb02015-11-25 20:28:50 +09001241 * This code is only called from the requestNetwork API (L and above).
1242 *
1243 * Setting a legacy type causes CONNECTIVITY_ACTION broadcasts, which are expensive
1244 * because they wake up lots of apps - see http://b/23350688 . So we currently only
1245 * do this for SUPL requests, which are the only ones that we know need it. If
1246 * omitting these broadcasts causes unacceptable app breakage, then for backwards
1247 * compatibility we can send them:
1248 *
1249 * if (targetSdkVersion < Build.VERSION_CODES.M) && // legacy API unsupported >= M
1250 * targetSdkVersion >= Build.VERSION_CODES.LOLLIPOP)) // requestNetwork not present < L
1251 *
Robert Greenwalt06314e42014-10-29 14:04:06 -07001252 * TODO - This should be removed when the legacy APIs are removed.
1253 */
Ye Wenb87875e2014-07-21 14:19:01 -07001254 private int inferLegacyTypeForNetworkCapabilities(NetworkCapabilities netCap) {
1255 if (netCap == null) {
1256 return TYPE_NONE;
1257 }
Robert Greenwalt06314e42014-10-29 14:04:06 -07001258
Ye Wenb87875e2014-07-21 14:19:01 -07001259 if (!netCap.hasTransport(NetworkCapabilities.TRANSPORT_CELLULAR)) {
1260 return TYPE_NONE;
1261 }
Robert Greenwalt06314e42014-10-29 14:04:06 -07001262
Lifu Tang30f95a72016-01-07 23:20:38 -08001263 // Do this only for SUPL, until GnssLocationProvider is fixed. http://b/25876485 .
Lorenzo Colittidef4cb02015-11-25 20:28:50 +09001264 if (!netCap.hasCapability(NetworkCapabilities.NET_CAPABILITY_SUPL)) {
1265 // NOTE: if this causes app breakage, we should not just comment out this early return;
1266 // instead, we should make this early return conditional on the requesting app's target
1267 // SDK version, as described in the comment above.
1268 return TYPE_NONE;
1269 }
1270
Robert Greenwalt06314e42014-10-29 14:04:06 -07001271 String type = null;
1272 int result = TYPE_NONE;
1273
Ye Wenb87875e2014-07-21 14:19:01 -07001274 if (netCap.hasCapability(NetworkCapabilities.NET_CAPABILITY_CBS)) {
Robert Greenwalt06314e42014-10-29 14:04:06 -07001275 type = "enableCBS";
1276 result = TYPE_MOBILE_CBS;
1277 } else if (netCap.hasCapability(NetworkCapabilities.NET_CAPABILITY_IMS)) {
1278 type = "enableIMS";
1279 result = TYPE_MOBILE_IMS;
1280 } else if (netCap.hasCapability(NetworkCapabilities.NET_CAPABILITY_FOTA)) {
1281 type = "enableFOTA";
1282 result = TYPE_MOBILE_FOTA;
1283 } else if (netCap.hasCapability(NetworkCapabilities.NET_CAPABILITY_DUN)) {
1284 type = "enableDUN";
1285 result = TYPE_MOBILE_DUN;
1286 } else if (netCap.hasCapability(NetworkCapabilities.NET_CAPABILITY_SUPL)) {
Lorenzo Colittidef4cb02015-11-25 20:28:50 +09001287 type = "enableSUPL";
Robert Greenwalt06314e42014-10-29 14:04:06 -07001288 result = TYPE_MOBILE_SUPL;
Robert Greenwalt74ab4fa2015-08-28 12:37:54 -07001289 // back out this hack for mms as they no longer need this and it's causing
1290 // device slowdowns - b/23350688 (note, supl still needs this)
1291 //} else if (netCap.hasCapability(NetworkCapabilities.NET_CAPABILITY_MMS)) {
1292 // type = "enableMMS";
1293 // result = TYPE_MOBILE_MMS;
Robert Greenwalt06314e42014-10-29 14:04:06 -07001294 } else if (netCap.hasCapability(NetworkCapabilities.NET_CAPABILITY_INTERNET)) {
1295 type = "enableHIPRI";
1296 result = TYPE_MOBILE_HIPRI;
Ye Wenb87875e2014-07-21 14:19:01 -07001297 }
Robert Greenwalt06314e42014-10-29 14:04:06 -07001298 if (type != null) {
1299 NetworkCapabilities testCap = networkCapabilitiesForFeature(TYPE_MOBILE, type);
1300 if (testCap.equalsNetCapabilities(netCap) && testCap.equalsTransportTypes(netCap)) {
1301 return result;
Ye Wenb87875e2014-07-21 14:19:01 -07001302 }
1303 }
1304 return TYPE_NONE;
1305 }
1306
Robert Greenwalt32aa65a2014-06-02 15:32:02 -07001307 private int legacyTypeForNetworkCapabilities(NetworkCapabilities netCap) {
Robert Greenwalt562cc542014-05-15 18:07:26 -07001308 if (netCap == null) return TYPE_NONE;
1309 if (netCap.hasCapability(NetworkCapabilities.NET_CAPABILITY_CBS)) {
1310 return TYPE_MOBILE_CBS;
1311 }
1312 if (netCap.hasCapability(NetworkCapabilities.NET_CAPABILITY_IMS)) {
1313 return TYPE_MOBILE_IMS;
1314 }
1315 if (netCap.hasCapability(NetworkCapabilities.NET_CAPABILITY_FOTA)) {
1316 return TYPE_MOBILE_FOTA;
1317 }
1318 if (netCap.hasCapability(NetworkCapabilities.NET_CAPABILITY_DUN)) {
1319 return TYPE_MOBILE_DUN;
1320 }
1321 if (netCap.hasCapability(NetworkCapabilities.NET_CAPABILITY_SUPL)) {
1322 return TYPE_MOBILE_SUPL;
1323 }
1324 if (netCap.hasCapability(NetworkCapabilities.NET_CAPABILITY_MMS)) {
1325 return TYPE_MOBILE_MMS;
1326 }
1327 if (netCap.hasCapability(NetworkCapabilities.NET_CAPABILITY_INTERNET)) {
1328 return TYPE_MOBILE_HIPRI;
1329 }
Robert Greenwalt32aa65a2014-06-02 15:32:02 -07001330 if (netCap.hasCapability(NetworkCapabilities.NET_CAPABILITY_WIFI_P2P)) {
1331 return TYPE_WIFI_P2P;
1332 }
Robert Greenwalt562cc542014-05-15 18:07:26 -07001333 return TYPE_NONE;
1334 }
1335
1336 private static class LegacyRequest {
1337 NetworkCapabilities networkCapabilities;
1338 NetworkRequest networkRequest;
1339 int expireSequenceNumber;
1340 Network currentNetwork;
1341 int delay = -1;
Paul Jensen9ffb53c2014-12-17 10:39:34 -05001342
1343 private void clearDnsBinding() {
1344 if (currentNetwork != null) {
1345 currentNetwork = null;
1346 setProcessDefaultNetworkForHostResolution(null);
1347 }
1348 }
1349
Robert Greenwalt6078b502014-06-11 16:05:07 -07001350 NetworkCallback networkCallback = new NetworkCallback() {
Robert Greenwalt562cc542014-05-15 18:07:26 -07001351 @Override
Robert Greenwalt6078b502014-06-11 16:05:07 -07001352 public void onAvailable(Network network) {
Robert Greenwalt562cc542014-05-15 18:07:26 -07001353 currentNetwork = network;
1354 Log.d(TAG, "startUsingNetworkFeature got Network:" + network);
Paul Jensen6d3ff9e2014-05-29 10:12:39 -04001355 setProcessDefaultNetworkForHostResolution(network);
Robert Greenwalt562cc542014-05-15 18:07:26 -07001356 }
1357 @Override
Robert Greenwalt6078b502014-06-11 16:05:07 -07001358 public void onLost(Network network) {
Paul Jensen9ffb53c2014-12-17 10:39:34 -05001359 if (network.equals(currentNetwork)) clearDnsBinding();
Robert Greenwalt562cc542014-05-15 18:07:26 -07001360 Log.d(TAG, "startUsingNetworkFeature lost Network:" + network);
1361 }
1362 };
1363 }
1364
Robert Greenwaltfab501672014-07-23 11:44:01 -07001365 private static HashMap<NetworkCapabilities, LegacyRequest> sLegacyRequests =
Robert Greenwalt562cc542014-05-15 18:07:26 -07001366 new HashMap<NetworkCapabilities, LegacyRequest>();
1367
1368 private NetworkRequest findRequestForFeature(NetworkCapabilities netCap) {
1369 synchronized (sLegacyRequests) {
1370 LegacyRequest l = sLegacyRequests.get(netCap);
1371 if (l != null) return l.networkRequest;
1372 }
1373 return null;
1374 }
1375
1376 private void renewRequestLocked(LegacyRequest l) {
1377 l.expireSequenceNumber++;
1378 Log.d(TAG, "renewing request to seqNum " + l.expireSequenceNumber);
1379 sendExpireMsgForFeature(l.networkCapabilities, l.expireSequenceNumber, l.delay);
1380 }
1381
1382 private void expireRequest(NetworkCapabilities netCap, int sequenceNum) {
1383 int ourSeqNum = -1;
1384 synchronized (sLegacyRequests) {
1385 LegacyRequest l = sLegacyRequests.get(netCap);
1386 if (l == null) return;
1387 ourSeqNum = l.expireSequenceNumber;
Paul Jensen9ffb53c2014-12-17 10:39:34 -05001388 if (l.expireSequenceNumber == sequenceNum) removeRequestForFeature(netCap);
Robert Greenwalt562cc542014-05-15 18:07:26 -07001389 }
1390 Log.d(TAG, "expireRequest with " + ourSeqNum + ", " + sequenceNum);
1391 }
1392
1393 private NetworkRequest requestNetworkForFeatureLocked(NetworkCapabilities netCap) {
1394 int delay = -1;
Robert Greenwalt32aa65a2014-06-02 15:32:02 -07001395 int type = legacyTypeForNetworkCapabilities(netCap);
Robert Greenwalt562cc542014-05-15 18:07:26 -07001396 try {
1397 delay = mService.getRestoreDefaultNetworkDelay(type);
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07001398 } catch (RemoteException e) {
1399 throw e.rethrowFromSystemServer();
1400 }
Robert Greenwalt562cc542014-05-15 18:07:26 -07001401 LegacyRequest l = new LegacyRequest();
1402 l.networkCapabilities = netCap;
1403 l.delay = delay;
1404 l.expireSequenceNumber = 0;
Hugo Benichi2583ef02017-02-02 17:02:36 +09001405 l.networkRequest = sendRequestForNetwork(
1406 netCap, l.networkCallback, 0, REQUEST, type, getDefaultHandler());
Robert Greenwalt562cc542014-05-15 18:07:26 -07001407 if (l.networkRequest == null) return null;
1408 sLegacyRequests.put(netCap, l);
1409 sendExpireMsgForFeature(netCap, l.expireSequenceNumber, delay);
1410 return l.networkRequest;
1411 }
1412
1413 private void sendExpireMsgForFeature(NetworkCapabilities netCap, int seqNum, int delay) {
1414 if (delay >= 0) {
1415 Log.d(TAG, "sending expire msg with seqNum " + seqNum + " and delay " + delay);
Hugo Benichi2583ef02017-02-02 17:02:36 +09001416 CallbackHandler handler = getDefaultHandler();
Hugo Benichi6f260f32017-02-03 14:18:44 +09001417 Message msg = handler.obtainMessage(EXPIRE_LEGACY_REQUEST, seqNum, 0, netCap);
1418 handler.sendMessageDelayed(msg, delay);
Robert Greenwalt562cc542014-05-15 18:07:26 -07001419 }
1420 }
1421
Paul Jensen9ffb53c2014-12-17 10:39:34 -05001422 private boolean removeRequestForFeature(NetworkCapabilities netCap) {
1423 final LegacyRequest l;
Robert Greenwalt562cc542014-05-15 18:07:26 -07001424 synchronized (sLegacyRequests) {
Paul Jensen9ffb53c2014-12-17 10:39:34 -05001425 l = sLegacyRequests.remove(netCap);
Robert Greenwalt562cc542014-05-15 18:07:26 -07001426 }
Paul Jensen9ffb53c2014-12-17 10:39:34 -05001427 if (l == null) return false;
1428 unregisterNetworkCallback(l.networkCallback);
1429 l.clearDnsBinding();
1430 return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001431 }
1432
Erik Kline35bf06c2017-01-26 18:08:28 +09001433 private static final SparseIntArray sLegacyTypeToTransport = new SparseIntArray();
1434 static {
1435 sLegacyTypeToTransport.put(TYPE_MOBILE, NetworkCapabilities.TRANSPORT_CELLULAR);
1436 sLegacyTypeToTransport.put(TYPE_MOBILE_CBS, NetworkCapabilities.TRANSPORT_CELLULAR);
1437 sLegacyTypeToTransport.put(TYPE_MOBILE_DUN, NetworkCapabilities.TRANSPORT_CELLULAR);
1438 sLegacyTypeToTransport.put(TYPE_MOBILE_FOTA, NetworkCapabilities.TRANSPORT_CELLULAR);
1439 sLegacyTypeToTransport.put(TYPE_MOBILE_HIPRI, NetworkCapabilities.TRANSPORT_CELLULAR);
1440 sLegacyTypeToTransport.put(TYPE_MOBILE_IMS, NetworkCapabilities.TRANSPORT_CELLULAR);
1441 sLegacyTypeToTransport.put(TYPE_MOBILE_MMS, NetworkCapabilities.TRANSPORT_CELLULAR);
1442 sLegacyTypeToTransport.put(TYPE_MOBILE_SUPL, NetworkCapabilities.TRANSPORT_CELLULAR);
1443 sLegacyTypeToTransport.put(TYPE_WIFI, NetworkCapabilities.TRANSPORT_WIFI);
1444 sLegacyTypeToTransport.put(TYPE_WIFI_P2P, NetworkCapabilities.TRANSPORT_WIFI);
1445 sLegacyTypeToTransport.put(TYPE_BLUETOOTH, NetworkCapabilities.TRANSPORT_BLUETOOTH);
1446 sLegacyTypeToTransport.put(TYPE_ETHERNET, NetworkCapabilities.TRANSPORT_ETHERNET);
1447 }
1448
1449 private static final SparseIntArray sLegacyTypeToCapability = new SparseIntArray();
1450 static {
1451 sLegacyTypeToCapability.put(TYPE_MOBILE_CBS, NetworkCapabilities.NET_CAPABILITY_CBS);
1452 sLegacyTypeToCapability.put(TYPE_MOBILE_DUN, NetworkCapabilities.NET_CAPABILITY_DUN);
1453 sLegacyTypeToCapability.put(TYPE_MOBILE_FOTA, NetworkCapabilities.NET_CAPABILITY_FOTA);
1454 sLegacyTypeToCapability.put(TYPE_MOBILE_IMS, NetworkCapabilities.NET_CAPABILITY_IMS);
1455 sLegacyTypeToCapability.put(TYPE_MOBILE_MMS, NetworkCapabilities.NET_CAPABILITY_MMS);
1456 sLegacyTypeToCapability.put(TYPE_MOBILE_SUPL, NetworkCapabilities.NET_CAPABILITY_SUPL);
1457 sLegacyTypeToCapability.put(TYPE_WIFI_P2P, NetworkCapabilities.NET_CAPABILITY_WIFI_P2P);
1458 }
1459
1460 /**
1461 * Given a legacy type (TYPE_WIFI, ...) returns a NetworkCapabilities
1462 * instance suitable for registering a request or callback. Throws an
1463 * IllegalArgumentException if no mapping from the legacy type to
1464 * NetworkCapabilities is known.
1465 *
1466 * @hide
1467 */
1468 public static NetworkCapabilities networkCapabilitiesForType(int type) {
1469 final NetworkCapabilities nc = new NetworkCapabilities();
1470
1471 // Map from type to transports.
1472 final int NOT_FOUND = -1;
1473 final int transport = sLegacyTypeToTransport.get(type, NOT_FOUND);
1474 if (transport == NOT_FOUND) {
1475 throw new IllegalArgumentException("unknown legacy type: " + type);
1476 }
1477 nc.addTransportType(transport);
1478
1479 // Map from type to capabilities.
1480 nc.addCapability(sLegacyTypeToCapability.get(
1481 type, NetworkCapabilities.NET_CAPABILITY_INTERNET));
1482 nc.maybeMarkCapabilitiesRestricted();
1483 return nc;
1484 }
1485
Lorenzo Colitti8bf977d2015-06-15 14:29:22 +09001486 /** @hide */
1487 public static class PacketKeepaliveCallback {
1488 /** The requested keepalive was successfully started. */
1489 public void onStarted() {}
1490 /** The keepalive was successfully stopped. */
1491 public void onStopped() {}
1492 /** An error occurred. */
1493 public void onError(int error) {}
1494 }
1495
1496 /**
1497 * Allows applications to request that the system periodically send specific packets on their
1498 * behalf, using hardware offload to save battery power.
1499 *
1500 * To request that the system send keepalives, call one of the methods that return a
1501 * {@link ConnectivityManager.PacketKeepalive} object, such as {@link #startNattKeepalive},
1502 * passing in a non-null callback. If the callback is successfully started, the callback's
1503 * {@code onStarted} method will be called. If an error occurs, {@code onError} will be called,
1504 * specifying one of the {@code ERROR_*} constants in this class.
1505 *
1506 * To stop an existing keepalive, call {@link stop}. The system will call {@code onStopped} if
1507 * the operation was successfull or {@code onError} if an error occurred.
1508 *
1509 * @hide
1510 */
1511 public class PacketKeepalive {
1512
1513 private static final String TAG = "PacketKeepalive";
1514
1515 /** @hide */
1516 public static final int SUCCESS = 0;
1517
1518 /** @hide */
1519 public static final int NO_KEEPALIVE = -1;
1520
1521 /** @hide */
1522 public static final int BINDER_DIED = -10;
1523
1524 /** The specified {@code Network} is not connected. */
1525 public static final int ERROR_INVALID_NETWORK = -20;
1526 /** The specified IP addresses are invalid. For example, the specified source IP address is
1527 * not configured on the specified {@code Network}. */
1528 public static final int ERROR_INVALID_IP_ADDRESS = -21;
1529 /** The requested port is invalid. */
1530 public static final int ERROR_INVALID_PORT = -22;
1531 /** The packet length is invalid (e.g., too long). */
1532 public static final int ERROR_INVALID_LENGTH = -23;
1533 /** The packet transmission interval is invalid (e.g., too short). */
1534 public static final int ERROR_INVALID_INTERVAL = -24;
1535
1536 /** The hardware does not support this request. */
1537 public static final int ERROR_HARDWARE_UNSUPPORTED = -30;
Lorenzo Colitti9d1284e2015-09-08 16:46:36 +09001538 /** The hardware returned an error. */
1539 public static final int ERROR_HARDWARE_ERROR = -31;
Lorenzo Colitti8bf977d2015-06-15 14:29:22 +09001540
1541 public static final int NATT_PORT = 4500;
1542
1543 private final Network mNetwork;
1544 private final PacketKeepaliveCallback mCallback;
1545 private final Looper mLooper;
1546 private final Messenger mMessenger;
1547
1548 private volatile Integer mSlot;
1549
1550 void stopLooper() {
1551 mLooper.quit();
1552 }
1553
1554 public void stop() {
1555 try {
1556 mService.stopKeepalive(mNetwork, mSlot);
1557 } catch (RemoteException e) {
1558 Log.e(TAG, "Error stopping packet keepalive: ", e);
1559 stopLooper();
1560 }
1561 }
1562
1563 private PacketKeepalive(Network network, PacketKeepaliveCallback callback) {
1564 checkNotNull(network, "network cannot be null");
1565 checkNotNull(callback, "callback cannot be null");
1566 mNetwork = network;
1567 mCallback = callback;
1568 HandlerThread thread = new HandlerThread(TAG);
1569 thread.start();
1570 mLooper = thread.getLooper();
1571 mMessenger = new Messenger(new Handler(mLooper) {
1572 @Override
1573 public void handleMessage(Message message) {
1574 switch (message.what) {
1575 case NetworkAgent.EVENT_PACKET_KEEPALIVE:
1576 int error = message.arg2;
1577 try {
1578 if (error == SUCCESS) {
1579 if (mSlot == null) {
1580 mSlot = message.arg1;
1581 mCallback.onStarted();
1582 } else {
1583 mSlot = null;
1584 stopLooper();
1585 mCallback.onStopped();
1586 }
1587 } else {
1588 stopLooper();
1589 mCallback.onError(error);
1590 }
1591 } catch (Exception e) {
1592 Log.e(TAG, "Exception in keepalive callback(" + error + ")", e);
1593 }
1594 break;
1595 default:
1596 Log.e(TAG, "Unhandled message " + Integer.toHexString(message.what));
1597 break;
1598 }
1599 }
1600 });
1601 }
1602 }
1603
1604 /**
1605 * Starts an IPsec NAT-T keepalive packet with the specified parameters.
1606 *
1607 * @hide
1608 */
1609 public PacketKeepalive startNattKeepalive(
1610 Network network, int intervalSeconds, PacketKeepaliveCallback callback,
1611 InetAddress srcAddr, int srcPort, InetAddress dstAddr) {
1612 final PacketKeepalive k = new PacketKeepalive(network, callback);
1613 try {
1614 mService.startNattKeepalive(network, intervalSeconds, k.mMessenger, new Binder(),
1615 srcAddr.getHostAddress(), srcPort, dstAddr.getHostAddress());
1616 } catch (RemoteException e) {
1617 Log.e(TAG, "Error starting packet keepalive: ", e);
1618 k.stopLooper();
1619 return null;
1620 }
1621 return k;
1622 }
1623
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001624 /**
1625 * Ensure that a network route exists to deliver traffic to the specified
1626 * host via the specified network interface. An attempt to add a route that
1627 * already exists is ignored, but treated as successful.
Lorenzo Colittid5427052015-10-15 16:29:00 +09001628 *
1629 * <p>This method requires the caller to hold either the
1630 * {@link android.Manifest.permission#CHANGE_NETWORK_STATE} permission
1631 * or the ability to modify system settings as determined by
1632 * {@link android.provider.Settings.System#canWrite}.</p>
1633 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001634 * @param networkType the type of the network over which traffic to the specified
1635 * host is to be routed
1636 * @param hostAddress the IP address of the host to which the route is desired
1637 * @return {@code true} on success, {@code false} on failure
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07001638 *
Lorenzo Colitti2ea89e52015-04-24 17:03:31 +09001639 * @deprecated Deprecated in favor of the
1640 * {@link #requestNetwork(NetworkRequest, NetworkCallback)},
1641 * {@link #bindProcessToNetwork} and {@link Network#getSocketFactory} API.
Dianne Hackborn692a2442015-07-31 10:35:34 -07001642 * In {@link VERSION_CODES#M}, and above, this method is unsupported and will
Lorenzo Colittiffc42b02015-07-29 11:41:21 +09001643 * throw {@code UnsupportedOperationException} if called.
Lorenzo Colitti2187df72016-12-09 18:39:30 +09001644 * @removed
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001645 */
Aurimas Liutikas514c5ef2016-05-24 15:22:55 -07001646 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001647 public boolean requestRouteToHost(int networkType, int hostAddress) {
Sreeram Ramachandran03666c72014-07-19 23:21:46 -07001648 return requestRouteToHostAddress(networkType, NetworkUtils.intToInetAddress(hostAddress));
Robert Greenwalt585ac0f2010-08-27 09:24:29 -07001649 }
1650
1651 /**
1652 * Ensure that a network route exists to deliver traffic to the specified
1653 * host via the specified network interface. An attempt to add a route that
1654 * already exists is ignored, but treated as successful.
Lorenzo Colittid5427052015-10-15 16:29:00 +09001655 *
1656 * <p>This method requires the caller to hold either the
1657 * {@link android.Manifest.permission#CHANGE_NETWORK_STATE} permission
1658 * or the ability to modify system settings as determined by
1659 * {@link android.provider.Settings.System#canWrite}.</p>
1660 *
Robert Greenwalt585ac0f2010-08-27 09:24:29 -07001661 * @param networkType the type of the network over which traffic to the specified
1662 * host is to be routed
1663 * @param hostAddress the IP address of the host to which the route is desired
1664 * @return {@code true} on success, {@code false} on failure
1665 * @hide
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07001666 * @deprecated Deprecated in favor of the {@link #requestNetwork} and
Lorenzo Colitti2ea89e52015-04-24 17:03:31 +09001667 * {@link #bindProcessToNetwork} API.
Robert Greenwalt585ac0f2010-08-27 09:24:29 -07001668 */
Aurimas Liutikas514c5ef2016-05-24 15:22:55 -07001669 @Deprecated
Robert Greenwalt585ac0f2010-08-27 09:24:29 -07001670 public boolean requestRouteToHostAddress(int networkType, InetAddress hostAddress) {
Lorenzo Colittiffc42b02015-07-29 11:41:21 +09001671 checkLegacyRoutingApiAccess();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001672 try {
Sreeram Ramachandran03666c72014-07-19 23:21:46 -07001673 return mService.requestRouteToHostAddress(networkType, hostAddress.getAddress());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001674 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07001675 throw e.rethrowFromSystemServer();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001676 }
1677 }
1678
1679 /**
1680 * Returns the value of the setting for background data usage. If false,
1681 * applications should not use the network if the application is not in the
1682 * foreground. Developers should respect this setting, and check the value
1683 * of this before performing any background data operations.
1684 * <p>
1685 * All applications that have background services that use the network
1686 * should listen to {@link #ACTION_BACKGROUND_DATA_SETTING_CHANGED}.
Jeff Sharkey3a844fc2011-08-16 14:37:57 -07001687 * <p>
Scott Main4cc53332011-10-06 18:32:43 -07001688 * @deprecated As of {@link VERSION_CODES#ICE_CREAM_SANDWICH}, availability of
Jeff Sharkey3a844fc2011-08-16 14:37:57 -07001689 * background data depends on several combined factors, and this method will
1690 * always return {@code true}. Instead, when background data is unavailable,
1691 * {@link #getActiveNetworkInfo()} will now appear disconnected.
Danica Chang6fdd0c62010-08-11 14:54:43 -07001692 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001693 * @return Whether background data usage is allowed.
1694 */
Jeff Sharkey3a844fc2011-08-16 14:37:57 -07001695 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001696 public boolean getBackgroundDataSetting() {
Jeff Sharkey3a844fc2011-08-16 14:37:57 -07001697 // assume that background data is allowed; final authority is
1698 // NetworkInfo which may be blocked.
1699 return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001700 }
1701
1702 /**
1703 * Sets the value of the setting for background data usage.
Robert Greenwaltc03fa502010-02-23 18:58:05 -08001704 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001705 * @param allowBackgroundData Whether an application should use data while
1706 * it is in the background.
Robert Greenwaltc03fa502010-02-23 18:58:05 -08001707 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001708 * @attr ref android.Manifest.permission#CHANGE_BACKGROUND_DATA_SETTING
1709 * @see #getBackgroundDataSetting()
1710 * @hide
1711 */
Jeff Sharkey3a844fc2011-08-16 14:37:57 -07001712 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001713 public void setBackgroundDataSetting(boolean allowBackgroundData) {
Jeff Sharkey3a844fc2011-08-16 14:37:57 -07001714 // ignored
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001715 }
Robert Greenwaltc03fa502010-02-23 18:58:05 -08001716
1717 /**
Jeff Sharkeyf0ceede2011-08-02 17:22:34 -07001718 * Return quota status for the current active network, or {@code null} if no
1719 * network is active. Quota status can change rapidly, so these values
1720 * shouldn't be cached.
Jeff Sharkey44a3e0d2011-10-06 10:50:09 -07001721 *
Paul Jensen0d719ca2015-02-13 14:18:39 -05001722 * <p>This method requires the caller to hold the permission
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08001723 * {@link android.Manifest.permission#ACCESS_NETWORK_STATE}.
1724 *
Jeff Sharkey44a3e0d2011-10-06 10:50:09 -07001725 * @hide
Jeff Sharkeyf0ceede2011-08-02 17:22:34 -07001726 */
1727 public NetworkQuotaInfo getActiveNetworkQuotaInfo() {
1728 try {
1729 return mService.getActiveNetworkQuotaInfo();
1730 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07001731 throw e.rethrowFromSystemServer();
Jeff Sharkeyf0ceede2011-08-02 17:22:34 -07001732 }
1733 }
1734
1735 /**
Robert Greenwaltc03fa502010-02-23 18:58:05 -08001736 * @hide
Robert Greenwaltafa05c02014-05-21 20:04:36 -07001737 * @deprecated Talk to TelephonyManager directly
Robert Greenwaltc03fa502010-02-23 18:58:05 -08001738 */
Aurimas Liutikas514c5ef2016-05-24 15:22:55 -07001739 @Deprecated
Robert Greenwaltc03fa502010-02-23 18:58:05 -08001740 public boolean getMobileDataEnabled() {
Robert Greenwaltafa05c02014-05-21 20:04:36 -07001741 IBinder b = ServiceManager.getService(Context.TELEPHONY_SERVICE);
1742 if (b != null) {
1743 try {
1744 ITelephony it = ITelephony.Stub.asInterface(b);
Shishir Agrawal7ea3e8b2016-01-25 13:03:07 -08001745 int subId = SubscriptionManager.getDefaultDataSubscriptionId();
Wink Saville36ffb042014-12-05 11:10:30 -08001746 Log.d("ConnectivityManager", "getMobileDataEnabled()+ subId=" + subId);
1747 boolean retVal = it.getDataEnabled(subId);
1748 Log.d("ConnectivityManager", "getMobileDataEnabled()- subId=" + subId
1749 + " retVal=" + retVal);
1750 return retVal;
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07001751 } catch (RemoteException e) {
1752 throw e.rethrowFromSystemServer();
1753 }
Robert Greenwaltc03fa502010-02-23 18:58:05 -08001754 }
Wink Saville36ffb042014-12-05 11:10:30 -08001755 Log.d("ConnectivityManager", "getMobileDataEnabled()- remote exception retVal=false");
Robert Greenwaltafa05c02014-05-21 20:04:36 -07001756 return false;
Robert Greenwaltc03fa502010-02-23 18:58:05 -08001757 }
1758
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001759 /**
Robert Greenwaltb2489872014-09-04 16:44:35 -07001760 * Callback for use with {@link ConnectivityManager#addDefaultNetworkActiveListener}
Robert Greenwalt6078b502014-06-11 16:05:07 -07001761 * to find out when the system default network has gone in to a high power state.
Dianne Hackborn77b987f2014-02-26 16:20:52 -08001762 */
1763 public interface OnNetworkActiveListener {
1764 /**
1765 * Called on the main thread of the process to report that the current data network
1766 * has become active, and it is now a good time to perform any pending network
1767 * operations. Note that this listener only tells you when the network becomes
1768 * active; if at any other time you want to know whether it is active (and thus okay
1769 * to initiate network traffic), you can retrieve its instantaneous state with
Robert Greenwalt6078b502014-06-11 16:05:07 -07001770 * {@link ConnectivityManager#isDefaultNetworkActive}.
Dianne Hackborn77b987f2014-02-26 16:20:52 -08001771 */
1772 public void onNetworkActive();
1773 }
1774
1775 private INetworkManagementService getNetworkManagementService() {
1776 synchronized (this) {
1777 if (mNMService != null) {
1778 return mNMService;
1779 }
1780 IBinder b = ServiceManager.getService(Context.NETWORKMANAGEMENT_SERVICE);
1781 mNMService = INetworkManagementService.Stub.asInterface(b);
1782 return mNMService;
1783 }
1784 }
1785
1786 private final ArrayMap<OnNetworkActiveListener, INetworkActivityListener>
1787 mNetworkActivityListeners
1788 = new ArrayMap<OnNetworkActiveListener, INetworkActivityListener>();
1789
1790 /**
Robert Greenwalt6078b502014-06-11 16:05:07 -07001791 * Start listening to reports when the system's default data network is active, meaning it is
1792 * a good time to perform network traffic. Use {@link #isDefaultNetworkActive()}
1793 * to determine the current state of the system's default network after registering the
1794 * listener.
1795 * <p>
1796 * If the process default network has been set with
Paul Jensen72db88e2015-03-10 10:54:12 -04001797 * {@link ConnectivityManager#bindProcessToNetwork} this function will not
Robert Greenwalt6078b502014-06-11 16:05:07 -07001798 * reflect the process's default, but the system default.
Dianne Hackborn77b987f2014-02-26 16:20:52 -08001799 *
1800 * @param l The listener to be told when the network is active.
1801 */
Robert Greenwaltb2489872014-09-04 16:44:35 -07001802 public void addDefaultNetworkActiveListener(final OnNetworkActiveListener l) {
Dianne Hackborn77b987f2014-02-26 16:20:52 -08001803 INetworkActivityListener rl = new INetworkActivityListener.Stub() {
1804 @Override
1805 public void onNetworkActive() throws RemoteException {
1806 l.onNetworkActive();
1807 }
1808 };
1809
1810 try {
1811 getNetworkManagementService().registerNetworkActivityListener(rl);
1812 mNetworkActivityListeners.put(l, rl);
1813 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07001814 throw e.rethrowFromSystemServer();
Dianne Hackborn77b987f2014-02-26 16:20:52 -08001815 }
1816 }
1817
1818 /**
1819 * Remove network active listener previously registered with
Robert Greenwaltb2489872014-09-04 16:44:35 -07001820 * {@link #addDefaultNetworkActiveListener}.
Dianne Hackborn77b987f2014-02-26 16:20:52 -08001821 *
1822 * @param l Previously registered listener.
1823 */
Robert Greenwaltb2489872014-09-04 16:44:35 -07001824 public void removeDefaultNetworkActiveListener(OnNetworkActiveListener l) {
Dianne Hackborn77b987f2014-02-26 16:20:52 -08001825 INetworkActivityListener rl = mNetworkActivityListeners.get(l);
1826 if (rl == null) {
1827 throw new IllegalArgumentException("Listener not registered: " + l);
1828 }
1829 try {
1830 getNetworkManagementService().unregisterNetworkActivityListener(rl);
1831 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07001832 throw e.rethrowFromSystemServer();
Dianne Hackborn77b987f2014-02-26 16:20:52 -08001833 }
1834 }
1835
1836 /**
1837 * Return whether the data network is currently active. An active network means that
1838 * it is currently in a high power state for performing data transmission. On some
1839 * types of networks, it may be expensive to move and stay in such a state, so it is
1840 * more power efficient to batch network traffic together when the radio is already in
1841 * this state. This method tells you whether right now is currently a good time to
1842 * initiate network traffic, as the network is already active.
1843 */
Robert Greenwalt6078b502014-06-11 16:05:07 -07001844 public boolean isDefaultNetworkActive() {
Dianne Hackborn77b987f2014-02-26 16:20:52 -08001845 try {
1846 return getNetworkManagementService().isNetworkActive();
1847 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07001848 throw e.rethrowFromSystemServer();
Dianne Hackborn77b987f2014-02-26 16:20:52 -08001849 }
Dianne Hackborn77b987f2014-02-26 16:20:52 -08001850 }
1851
1852 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001853 * {@hide}
1854 */
Lorenzo Colittiffc42b02015-07-29 11:41:21 +09001855 public ConnectivityManager(Context context, IConnectivityManager service) {
1856 mContext = checkNotNull(context, "missing context");
Jeff Sharkeyf0ceede2011-08-02 17:22:34 -07001857 mService = checkNotNull(service, "missing IConnectivityManager");
Paul Jensene0bef712014-12-10 15:12:18 -05001858 sInstance = this;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001859 }
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -08001860
Jeff Sharkey8fc27e82012-04-04 20:40:58 -07001861 /** {@hide} */
1862 public static ConnectivityManager from(Context context) {
1863 return (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
1864 }
1865
Lorenzo Colittifbe9b1a2016-07-28 17:14:11 +09001866 /* TODO: These permissions checks don't belong in client-side code. Move them to
1867 * services.jar, possibly in com.android.server.net. */
1868
1869 /** {@hide} */
1870 public static final boolean checkChangePermission(Context context) {
1871 int uid = Binder.getCallingUid();
1872 return Settings.checkAndNoteChangeNetworkStateOperation(context, uid, Settings
1873 .getPackageNameForUid(context, uid), false /* throwException */);
1874 }
1875
Lorenzo Colittid5427052015-10-15 16:29:00 +09001876 /** {@hide} */
1877 public static final void enforceChangePermission(Context context) {
1878 int uid = Binder.getCallingUid();
1879 Settings.checkAndNoteChangeNetworkStateOperation(context, uid, Settings
1880 .getPackageNameForUid(context, uid), true /* throwException */);
1881 }
1882
Robert Greenwaltedb47662014-09-16 17:54:19 -07001883 /** {@hide */
1884 public static final void enforceTetherChangePermission(Context context) {
1885 if (context.getResources().getStringArray(
1886 com.android.internal.R.array.config_mobile_hotspot_provision_app).length == 2) {
1887 // Have a provisioning app - must only let system apps (which check this app)
1888 // turn on tethering
1889 context.enforceCallingOrSelfPermission(
Jeremy Kleind42209d2015-12-28 15:11:58 -08001890 android.Manifest.permission.TETHER_PRIVILEGED, "ConnectivityService");
Robert Greenwaltedb47662014-09-16 17:54:19 -07001891 } else {
Billy Laua7238a32015-08-01 12:45:02 +01001892 int uid = Binder.getCallingUid();
Lorenzo Colittid5427052015-10-15 16:29:00 +09001893 Settings.checkAndNoteWriteSettingsOperation(context, uid, Settings
1894 .getPackageNameForUid(context, uid), true /* throwException */);
Robert Greenwaltedb47662014-09-16 17:54:19 -07001895 }
1896 }
1897
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -08001898 /**
Paul Jensene0bef712014-12-10 15:12:18 -05001899 * @deprecated - use getSystemService. This is a kludge to support static access in certain
1900 * situations where a Context pointer is unavailable.
1901 * @hide
1902 */
Aurimas Liutikas514c5ef2016-05-24 15:22:55 -07001903 @Deprecated
Paul Jensen72db88e2015-03-10 10:54:12 -04001904 static ConnectivityManager getInstanceOrNull() {
1905 return sInstance;
1906 }
1907
1908 /**
1909 * @deprecated - use getSystemService. This is a kludge to support static access in certain
1910 * situations where a Context pointer is unavailable.
1911 * @hide
1912 */
Aurimas Liutikas514c5ef2016-05-24 15:22:55 -07001913 @Deprecated
Paul Jensen72db88e2015-03-10 10:54:12 -04001914 private static ConnectivityManager getInstance() {
1915 if (getInstanceOrNull() == null) {
Paul Jensene0bef712014-12-10 15:12:18 -05001916 throw new IllegalStateException("No ConnectivityManager yet constructed");
1917 }
Paul Jensen72db88e2015-03-10 10:54:12 -04001918 return getInstanceOrNull();
Paul Jensene0bef712014-12-10 15:12:18 -05001919 }
1920
1921 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08001922 * Get the set of tetherable, available interfaces. This list is limited by
1923 * device configuration and current interface existence.
Paul Jensenb2748922015-05-06 11:10:18 -04001924 * <p>This method requires the caller to hold the permission
1925 * {@link android.Manifest.permission#ACCESS_NETWORK_STATE}.
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08001926 *
1927 * @return an array of 0 or more Strings of tetherable interface names.
1928 *
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -08001929 * {@hide}
1930 */
1931 public String[] getTetherableIfaces() {
1932 try {
1933 return mService.getTetherableIfaces();
1934 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07001935 throw e.rethrowFromSystemServer();
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -08001936 }
1937 }
1938
1939 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08001940 * Get the set of tethered interfaces.
Paul Jensenb2748922015-05-06 11:10:18 -04001941 * <p>This method requires the caller to hold the permission
1942 * {@link android.Manifest.permission#ACCESS_NETWORK_STATE}.
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08001943 *
1944 * @return an array of 0 or more String of currently tethered interface names.
1945 *
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -08001946 * {@hide}
1947 */
1948 public String[] getTetheredIfaces() {
1949 try {
1950 return mService.getTetheredIfaces();
1951 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07001952 throw e.rethrowFromSystemServer();
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -08001953 }
1954 }
1955
1956 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08001957 * Get the set of interface names which attempted to tether but
1958 * failed. Re-attempting to tether may cause them to reset to the Tethered
1959 * state. Alternatively, causing the interface to be destroyed and recreated
1960 * may cause them to reset to the available state.
1961 * {@link ConnectivityManager#getLastTetherError} can be used to get more
1962 * information on the cause of the errors.
Paul Jensenb2748922015-05-06 11:10:18 -04001963 * <p>This method requires the caller to hold the permission
1964 * {@link android.Manifest.permission#ACCESS_NETWORK_STATE}.
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08001965 *
1966 * @return an array of 0 or more String indicating the interface names
1967 * which failed to tether.
1968 *
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -08001969 * {@hide}
1970 */
Robert Greenwalt5a735062010-03-02 17:25:02 -08001971 public String[] getTetheringErroredIfaces() {
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -08001972 try {
Robert Greenwalt5a735062010-03-02 17:25:02 -08001973 return mService.getTetheringErroredIfaces();
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -08001974 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07001975 throw e.rethrowFromSystemServer();
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -08001976 }
1977 }
1978
1979 /**
Robert Greenwalt9c7e2c22014-06-23 14:53:42 -07001980 * Get the set of tethered dhcp ranges.
1981 *
1982 * @return an array of 0 or more {@code String} of tethered dhcp ranges.
1983 * {@hide}
1984 */
1985 public String[] getTetheredDhcpRanges() {
1986 try {
1987 return mService.getTetheredDhcpRanges();
1988 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07001989 throw e.rethrowFromSystemServer();
Robert Greenwalt9c7e2c22014-06-23 14:53:42 -07001990 }
1991 }
1992
1993 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08001994 * Attempt to tether the named interface. This will setup a dhcp server
1995 * on the interface, forward and NAT IP packets and forward DNS requests
1996 * to the best active upstream network interface. Note that if no upstream
1997 * IP network interface is available, dhcp will still run and traffic will be
1998 * allowed between the tethered devices and this device, though upstream net
1999 * access will of course fail until an upstream network interface becomes
2000 * active.
Lorenzo Colittid5427052015-10-15 16:29:00 +09002001 *
2002 * <p>This method requires the caller to hold either the
2003 * {@link android.Manifest.permission#CHANGE_NETWORK_STATE} permission
2004 * or the ability to modify system settings as determined by
2005 * {@link android.provider.Settings.System#canWrite}.</p>
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08002006 *
Jeremy Klein36c7aa02016-01-22 14:11:45 -08002007 * <p>WARNING: New clients should not use this function. The only usages should be in PanService
2008 * and WifiStateMachine which need direct access. All other clients should use
2009 * {@link #startTethering} and {@link #stopTethering} which encapsulate proper provisioning
2010 * logic.</p>
2011 *
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08002012 * @param iface the interface name to tether.
2013 * @return error a {@code TETHER_ERROR} value indicating success or failure type
2014 *
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -08002015 * {@hide}
2016 */
Robert Greenwalt5a735062010-03-02 17:25:02 -08002017 public int tether(String iface) {
2018 try {
2019 return mService.tether(iface);
2020 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07002021 throw e.rethrowFromSystemServer();
Robert Greenwalt5a735062010-03-02 17:25:02 -08002022 }
2023 }
2024
2025 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08002026 * Stop tethering the named interface.
Lorenzo Colittid5427052015-10-15 16:29:00 +09002027 *
2028 * <p>This method requires the caller to hold either the
2029 * {@link android.Manifest.permission#CHANGE_NETWORK_STATE} permission
2030 * or the ability to modify system settings as determined by
2031 * {@link android.provider.Settings.System#canWrite}.</p>
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08002032 *
Jeremy Klein36c7aa02016-01-22 14:11:45 -08002033 * <p>WARNING: New clients should not use this function. The only usages should be in PanService
2034 * and WifiStateMachine which need direct access. All other clients should use
2035 * {@link #startTethering} and {@link #stopTethering} which encapsulate proper provisioning
2036 * logic.</p>
2037 *
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08002038 * @param iface the interface name to untether.
2039 * @return error a {@code TETHER_ERROR} value indicating success or failure type
2040 *
Robert Greenwalt5a735062010-03-02 17:25:02 -08002041 * {@hide}
2042 */
2043 public int untether(String iface) {
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -08002044 try {
2045 return mService.untether(iface);
2046 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07002047 throw e.rethrowFromSystemServer();
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -08002048 }
2049 }
Robert Greenwalt2a091d72010-02-11 18:18:40 -08002050
2051 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08002052 * Check if the device allows for tethering. It may be disabled via
Dianne Hackborn77b987f2014-02-26 16:20:52 -08002053 * {@code ro.tether.denied} system property, Settings.TETHER_SUPPORTED or
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08002054 * due to device configuration.
Paul Jensenb2748922015-05-06 11:10:18 -04002055 * <p>This method requires the caller to hold the permission
2056 * {@link android.Manifest.permission#ACCESS_NETWORK_STATE}.
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08002057 *
2058 * @return a boolean - {@code true} indicating Tethering is supported.
2059 *
Robert Greenwalt2a091d72010-02-11 18:18:40 -08002060 * {@hide}
2061 */
Jeremy Klein36c7aa02016-01-22 14:11:45 -08002062 @SystemApi
Robert Greenwalt2a091d72010-02-11 18:18:40 -08002063 public boolean isTetheringSupported() {
2064 try {
2065 return mService.isTetheringSupported();
2066 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07002067 throw e.rethrowFromSystemServer();
Robert Greenwalt2a091d72010-02-11 18:18:40 -08002068 }
2069 }
2070
2071 /**
Jeremy Klein36c7aa02016-01-22 14:11:45 -08002072 * Callback for use with {@link #startTethering} to find out whether tethering succeeded.
2073 * @hide
2074 */
2075 @SystemApi
2076 public static abstract class OnStartTetheringCallback {
2077 /**
2078 * Called when tethering has been successfully started.
2079 */
2080 public void onTetheringStarted() {};
2081
2082 /**
2083 * Called when starting tethering failed.
2084 */
2085 public void onTetheringFailed() {};
2086 }
2087
2088 /**
2089 * Convenient overload for
2090 * {@link #startTethering(int, boolean, OnStartTetheringCallback, Handler)} which passes a null
2091 * handler to run on the current thread's {@link Looper}.
2092 * @hide
2093 */
2094 @SystemApi
2095 public void startTethering(int type, boolean showProvisioningUi,
2096 final OnStartTetheringCallback callback) {
2097 startTethering(type, showProvisioningUi, callback, null);
2098 }
2099
2100 /**
2101 * Runs tether provisioning for the given type if needed and then starts tethering if
2102 * the check succeeds. If no carrier provisioning is required for tethering, tethering is
2103 * enabled immediately. If provisioning fails, tethering will not be enabled. It also
2104 * schedules tether provisioning re-checks if appropriate.
2105 *
2106 * @param type The type of tethering to start. Must be one of
2107 * {@link ConnectivityManager.TETHERING_WIFI},
2108 * {@link ConnectivityManager.TETHERING_USB}, or
2109 * {@link ConnectivityManager.TETHERING_BLUETOOTH}.
2110 * @param showProvisioningUi a boolean indicating to show the provisioning app UI if there
2111 * is one. This should be true the first time this function is called and also any time
2112 * the user can see this UI. It gives users information from their carrier about the
2113 * check failing and how they can sign up for tethering if possible.
2114 * @param callback an {@link OnStartTetheringCallback} which will be called to notify the caller
2115 * of the result of trying to tether.
2116 * @param handler {@link Handler} to specify the thread upon which the callback will be invoked.
2117 * @hide
2118 */
2119 @SystemApi
2120 public void startTethering(int type, boolean showProvisioningUi,
2121 final OnStartTetheringCallback callback, Handler handler) {
Jeremy Klein5f277e12016-03-12 16:29:54 -08002122 checkNotNull(callback, "OnStartTetheringCallback cannot be null.");
2123
Jeremy Klein36c7aa02016-01-22 14:11:45 -08002124 ResultReceiver wrappedCallback = new ResultReceiver(handler) {
2125 @Override
2126 protected void onReceiveResult(int resultCode, Bundle resultData) {
2127 if (resultCode == TETHER_ERROR_NO_ERROR) {
2128 callback.onTetheringStarted();
2129 } else {
2130 callback.onTetheringFailed();
2131 }
2132 }
2133 };
Jeremy Klein5f277e12016-03-12 16:29:54 -08002134
Jeremy Klein36c7aa02016-01-22 14:11:45 -08002135 try {
2136 mService.startTethering(type, wrappedCallback, showProvisioningUi);
2137 } catch (RemoteException e) {
2138 Log.e(TAG, "Exception trying to start tethering.", e);
2139 wrappedCallback.send(TETHER_ERROR_SERVICE_UNAVAIL, null);
2140 }
2141 }
2142
2143 /**
2144 * Stops tethering for the given type. Also cancels any provisioning rechecks for that type if
2145 * applicable.
2146 *
2147 * @param type The type of tethering to stop. Must be one of
2148 * {@link ConnectivityManager.TETHERING_WIFI},
2149 * {@link ConnectivityManager.TETHERING_USB}, or
2150 * {@link ConnectivityManager.TETHERING_BLUETOOTH}.
2151 * @hide
2152 */
2153 @SystemApi
2154 public void stopTethering(int type) {
2155 try {
2156 mService.stopTethering(type);
2157 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07002158 throw e.rethrowFromSystemServer();
Jeremy Klein36c7aa02016-01-22 14:11:45 -08002159 }
2160 }
2161
2162 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08002163 * Get the list of regular expressions that define any tetherable
2164 * USB network interfaces. If USB tethering is not supported by the
2165 * device, this list should be empty.
Paul Jensenb2748922015-05-06 11:10:18 -04002166 * <p>This method requires the caller to hold the permission
2167 * {@link android.Manifest.permission#ACCESS_NETWORK_STATE}.
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08002168 *
2169 * @return an array of 0 or more regular expression Strings defining
2170 * what interfaces are considered tetherable usb interfaces.
2171 *
Robert Greenwalt2a091d72010-02-11 18:18:40 -08002172 * {@hide}
2173 */
2174 public String[] getTetherableUsbRegexs() {
2175 try {
2176 return mService.getTetherableUsbRegexs();
2177 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07002178 throw e.rethrowFromSystemServer();
Robert Greenwalt2a091d72010-02-11 18:18:40 -08002179 }
2180 }
2181
2182 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08002183 * Get the list of regular expressions that define any tetherable
2184 * Wifi network interfaces. If Wifi tethering is not supported by the
2185 * device, this list should be empty.
Paul Jensenb2748922015-05-06 11:10:18 -04002186 * <p>This method requires the caller to hold the permission
2187 * {@link android.Manifest.permission#ACCESS_NETWORK_STATE}.
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08002188 *
2189 * @return an array of 0 or more regular expression Strings defining
2190 * what interfaces are considered tetherable wifi interfaces.
2191 *
Robert Greenwalt2a091d72010-02-11 18:18:40 -08002192 * {@hide}
2193 */
2194 public String[] getTetherableWifiRegexs() {
2195 try {
2196 return mService.getTetherableWifiRegexs();
2197 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07002198 throw e.rethrowFromSystemServer();
Robert Greenwalt2a091d72010-02-11 18:18:40 -08002199 }
2200 }
Robert Greenwalt5a735062010-03-02 17:25:02 -08002201
Danica Chang6fdd0c62010-08-11 14:54:43 -07002202 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08002203 * Get the list of regular expressions that define any tetherable
2204 * Bluetooth network interfaces. If Bluetooth tethering is not supported by the
2205 * device, this list should be empty.
Paul Jensenb2748922015-05-06 11:10:18 -04002206 * <p>This method requires the caller to hold the permission
2207 * {@link android.Manifest.permission#ACCESS_NETWORK_STATE}.
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08002208 *
2209 * @return an array of 0 or more regular expression Strings defining
2210 * what interfaces are considered tetherable bluetooth interfaces.
2211 *
Danica Chang6fdd0c62010-08-11 14:54:43 -07002212 * {@hide}
2213 */
2214 public String[] getTetherableBluetoothRegexs() {
2215 try {
2216 return mService.getTetherableBluetoothRegexs();
2217 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07002218 throw e.rethrowFromSystemServer();
Danica Chang6fdd0c62010-08-11 14:54:43 -07002219 }
2220 }
2221
Mike Lockwood6c2260b2011-07-19 13:04:47 -07002222 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08002223 * Attempt to both alter the mode of USB and Tethering of USB. A
2224 * utility method to deal with some of the complexity of USB - will
2225 * attempt to switch to Rndis and subsequently tether the resulting
2226 * interface on {@code true} or turn off tethering and switch off
2227 * Rndis on {@code false}.
Lorenzo Colittid5427052015-10-15 16:29:00 +09002228 *
2229 * <p>This method requires the caller to hold either the
2230 * {@link android.Manifest.permission#CHANGE_NETWORK_STATE} permission
2231 * or the ability to modify system settings as determined by
2232 * {@link android.provider.Settings.System#canWrite}.</p>
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08002233 *
2234 * @param enable a boolean - {@code true} to enable tethering
2235 * @return error a {@code TETHER_ERROR} value indicating success or failure type
2236 *
Mike Lockwood6c2260b2011-07-19 13:04:47 -07002237 * {@hide}
2238 */
2239 public int setUsbTethering(boolean enable) {
2240 try {
2241 return mService.setUsbTethering(enable);
2242 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07002243 throw e.rethrowFromSystemServer();
Mike Lockwood6c2260b2011-07-19 13:04:47 -07002244 }
2245 }
2246
Robert Greenwalt5a735062010-03-02 17:25:02 -08002247 /** {@hide} */
2248 public static final int TETHER_ERROR_NO_ERROR = 0;
2249 /** {@hide} */
2250 public static final int TETHER_ERROR_UNKNOWN_IFACE = 1;
2251 /** {@hide} */
2252 public static final int TETHER_ERROR_SERVICE_UNAVAIL = 2;
2253 /** {@hide} */
2254 public static final int TETHER_ERROR_UNSUPPORTED = 3;
2255 /** {@hide} */
2256 public static final int TETHER_ERROR_UNAVAIL_IFACE = 4;
2257 /** {@hide} */
2258 public static final int TETHER_ERROR_MASTER_ERROR = 5;
2259 /** {@hide} */
2260 public static final int TETHER_ERROR_TETHER_IFACE_ERROR = 6;
2261 /** {@hide} */
2262 public static final int TETHER_ERROR_UNTETHER_IFACE_ERROR = 7;
2263 /** {@hide} */
2264 public static final int TETHER_ERROR_ENABLE_NAT_ERROR = 8;
2265 /** {@hide} */
2266 public static final int TETHER_ERROR_DISABLE_NAT_ERROR = 9;
2267 /** {@hide} */
2268 public static final int TETHER_ERROR_IFACE_CFG_ERROR = 10;
Jeremy Klein36c7aa02016-01-22 14:11:45 -08002269 /** {@hide} */
2270 public static final int TETHER_ERROR_PROVISION_FAILED = 11;
Robert Greenwalt5a735062010-03-02 17:25:02 -08002271
2272 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08002273 * Get a more detailed error code after a Tethering or Untethering
2274 * request asynchronously failed.
Paul Jensenb2748922015-05-06 11:10:18 -04002275 * <p>This method requires the caller to hold the permission
2276 * {@link android.Manifest.permission#ACCESS_NETWORK_STATE}.
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08002277 *
2278 * @param iface The name of the interface of interest
Robert Greenwalt5a735062010-03-02 17:25:02 -08002279 * @return error The error code of the last error tethering or untethering the named
2280 * interface
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08002281 *
Robert Greenwalt5a735062010-03-02 17:25:02 -08002282 * {@hide}
2283 */
2284 public int getLastTetherError(String iface) {
2285 try {
2286 return mService.getLastTetherError(iface);
2287 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07002288 throw e.rethrowFromSystemServer();
Robert Greenwalt5a735062010-03-02 17:25:02 -08002289 }
Robert Greenwalt14f2ef42010-06-15 12:19:37 -07002290 }
2291
2292 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08002293 * Report network connectivity status. This is currently used only
2294 * to alter status bar UI.
Paul Jensenb2748922015-05-06 11:10:18 -04002295 * <p>This method requires the caller to hold the permission
2296 * {@link android.Manifest.permission#STATUS_BAR}.
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08002297 *
Robert Greenwaltd7085fc2010-09-08 15:24:47 -07002298 * @param networkType The type of network you want to report on
2299 * @param percentage The quality of the connection 0 is bad, 100 is good
2300 * {@hide}
2301 */
2302 public void reportInetCondition(int networkType, int percentage) {
2303 try {
2304 mService.reportInetCondition(networkType, percentage);
2305 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07002306 throw e.rethrowFromSystemServer();
Robert Greenwaltd7085fc2010-09-08 15:24:47 -07002307 }
2308 }
Robert Greenwalt434203a2010-10-11 16:00:27 -07002309
2310 /**
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07002311 * Report a problem network to the framework. This provides a hint to the system
Ye Wenb87875e2014-07-21 14:19:01 -07002312 * that there might be connectivity problems on this network and may cause
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07002313 * the framework to re-evaluate network connectivity and/or switch to another
2314 * network.
Robert Greenwalt9258c642014-03-26 16:47:06 -07002315 *
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07002316 * @param network The {@link Network} the application was attempting to use
2317 * or {@code null} to indicate the current default network.
Paul Jensenbfd17b72015-04-07 12:43:13 -04002318 * @deprecated Use {@link #reportNetworkConnectivity} which allows reporting both
2319 * working and non-working connectivity.
Robert Greenwalt9258c642014-03-26 16:47:06 -07002320 */
Aurimas Liutikas514c5ef2016-05-24 15:22:55 -07002321 @Deprecated
Robert Greenwalt9258c642014-03-26 16:47:06 -07002322 public void reportBadNetwork(Network network) {
2323 try {
Paul Jensenbfd17b72015-04-07 12:43:13 -04002324 // One of these will be ignored because it matches system's current state.
2325 // The other will trigger the necessary reevaluation.
2326 mService.reportNetworkConnectivity(network, true);
2327 mService.reportNetworkConnectivity(network, false);
2328 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07002329 throw e.rethrowFromSystemServer();
Paul Jensenbfd17b72015-04-07 12:43:13 -04002330 }
2331 }
2332
2333 /**
2334 * Report to the framework whether a network has working connectivity.
2335 * This provides a hint to the system that a particular network is providing
2336 * working connectivity or not. In response the framework may re-evaluate
2337 * the network's connectivity and might take further action thereafter.
2338 *
2339 * @param network The {@link Network} the application was attempting to use
2340 * or {@code null} to indicate the current default network.
2341 * @param hasConnectivity {@code true} if the application was able to successfully access the
2342 * Internet using {@code network} or {@code false} if not.
2343 */
2344 public void reportNetworkConnectivity(Network network, boolean hasConnectivity) {
2345 try {
2346 mService.reportNetworkConnectivity(network, hasConnectivity);
Robert Greenwalt9258c642014-03-26 16:47:06 -07002347 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07002348 throw e.rethrowFromSystemServer();
Robert Greenwalt9258c642014-03-26 16:47:06 -07002349 }
2350 }
2351
2352 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08002353 * Set a network-independent global http proxy. This is not normally what you want
2354 * for typical HTTP proxies - they are general network dependent. However if you're
2355 * doing something unusual like general internal filtering this may be useful. On
2356 * a private network where the proxy is not accessible, you may break HTTP using this.
Paul Jensen0d719ca2015-02-13 14:18:39 -05002357 * <p>This method requires the caller to hold the permission
Jason Monkfaf3fd52014-05-07 18:41:13 -04002358 * android.Manifest.permission#CONNECTIVITY_INTERNAL.
Paul Jensenb2748922015-05-06 11:10:18 -04002359 *
2360 * @param p A {@link ProxyInfo} object defining the new global
2361 * HTTP proxy. A {@code null} value will clear the global HTTP proxy.
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07002362 * @hide
Robert Greenwalt434203a2010-10-11 16:00:27 -07002363 */
Jason Monk207900c2014-04-25 15:00:09 -04002364 public void setGlobalProxy(ProxyInfo p) {
Robert Greenwalt434203a2010-10-11 16:00:27 -07002365 try {
2366 mService.setGlobalProxy(p);
2367 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07002368 throw e.rethrowFromSystemServer();
Robert Greenwalt434203a2010-10-11 16:00:27 -07002369 }
2370 }
2371
2372 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08002373 * Retrieve any network-independent global HTTP proxy.
2374 *
Jason Monk207900c2014-04-25 15:00:09 -04002375 * @return {@link ProxyInfo} for the current global HTTP proxy or {@code null}
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08002376 * if no global HTTP proxy is set.
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07002377 * @hide
Robert Greenwalt434203a2010-10-11 16:00:27 -07002378 */
Jason Monk207900c2014-04-25 15:00:09 -04002379 public ProxyInfo getGlobalProxy() {
Robert Greenwalt434203a2010-10-11 16:00:27 -07002380 try {
2381 return mService.getGlobalProxy();
2382 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07002383 throw e.rethrowFromSystemServer();
Robert Greenwalt434203a2010-10-11 16:00:27 -07002384 }
2385 }
2386
2387 /**
Paul Jensencee9b512015-05-06 07:32:40 -04002388 * Retrieve the global HTTP proxy, or if no global HTTP proxy is set, a
2389 * network-specific HTTP proxy. If {@code network} is null, the
2390 * network-specific proxy returned is the proxy of the default active
2391 * network.
2392 *
2393 * @return {@link ProxyInfo} for the current global HTTP proxy, or if no
2394 * global HTTP proxy is set, {@code ProxyInfo} for {@code network},
2395 * or when {@code network} is {@code null},
2396 * the {@code ProxyInfo} for the default active network. Returns
2397 * {@code null} when no proxy applies or the caller doesn't have
2398 * permission to use {@code network}.
2399 * @hide
2400 */
2401 public ProxyInfo getProxyForNetwork(Network network) {
2402 try {
2403 return mService.getProxyForNetwork(network);
2404 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07002405 throw e.rethrowFromSystemServer();
Paul Jensencee9b512015-05-06 07:32:40 -04002406 }
2407 }
2408
2409 /**
Paul Jensene0bef712014-12-10 15:12:18 -05002410 * Get the current default HTTP proxy settings. If a global proxy is set it will be returned,
2411 * otherwise if this process is bound to a {@link Network} using
Paul Jensen72db88e2015-03-10 10:54:12 -04002412 * {@link #bindProcessToNetwork} then that {@code Network}'s proxy is returned, otherwise
Paul Jensene0bef712014-12-10 15:12:18 -05002413 * the default network's proxy is returned.
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08002414 *
Jason Monk207900c2014-04-25 15:00:09 -04002415 * @return the {@link ProxyInfo} for the current HTTP proxy, or {@code null} if no
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08002416 * HTTP proxy is active.
Robert Greenwalt434203a2010-10-11 16:00:27 -07002417 */
Paul Jensene0bef712014-12-10 15:12:18 -05002418 public ProxyInfo getDefaultProxy() {
Paul Jensencee9b512015-05-06 07:32:40 -04002419 return getProxyForNetwork(getBoundNetworkForProcess());
Robert Greenwalt434203a2010-10-11 16:00:27 -07002420 }
Robert Greenwaltd55a6b42011-03-25 13:09:25 -07002421
2422 /**
Robert Greenwalt9b2886e2011-08-31 11:46:42 -07002423 * Returns true if the hardware supports the given network type
2424 * else it returns false. This doesn't indicate we have coverage
2425 * or are authorized onto a network, just whether or not the
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08002426 * hardware supports it. For example a GSM phone without a SIM
2427 * should still return {@code true} for mobile data, but a wifi only
2428 * tablet would return {@code false}.
Paul Jensenb2748922015-05-06 11:10:18 -04002429 * <p>This method requires the caller to hold the permission
2430 * {@link android.Manifest.permission#ACCESS_NETWORK_STATE}.
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08002431 *
2432 * @param networkType The network type we'd like to check
2433 * @return {@code true} if supported, else {@code false}
2434 *
Robert Greenwalt9b2886e2011-08-31 11:46:42 -07002435 * @hide
2436 */
2437 public boolean isNetworkSupported(int networkType) {
2438 try {
2439 return mService.isNetworkSupported(networkType);
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07002440 } catch (RemoteException e) {
2441 throw e.rethrowFromSystemServer();
2442 }
Robert Greenwalt9b2886e2011-08-31 11:46:42 -07002443 }
Jeff Sharkey9f7cbf02012-04-12 18:34:54 -07002444
2445 /**
2446 * Returns if the currently active data network is metered. A network is
2447 * classified as metered when the user is sensitive to heavy data usage on
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08002448 * that connection due to monetary costs, data limitations or
2449 * battery/performance issues. You should check this before doing large
2450 * data transfers, and warn the user or delay the operation until another
2451 * network is available.
Paul Jensenb2748922015-05-06 11:10:18 -04002452 * <p>This method requires the caller to hold the permission
2453 * {@link android.Manifest.permission#ACCESS_NETWORK_STATE}.
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08002454 *
2455 * @return {@code true} if large transfers should be avoided, otherwise
2456 * {@code false}.
Jeff Sharkey9f7cbf02012-04-12 18:34:54 -07002457 */
2458 public boolean isActiveNetworkMetered() {
2459 try {
2460 return mService.isActiveNetworkMetered();
2461 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07002462 throw e.rethrowFromSystemServer();
Jeff Sharkey9f7cbf02012-04-12 18:34:54 -07002463 }
2464 }
Jeff Sharkey69ddab42012-08-25 00:05:46 -07002465
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08002466 /**
2467 * If the LockdownVpn mechanism is enabled, updates the vpn
2468 * with a reload of its profile.
2469 *
2470 * @return a boolean with {@code} indicating success
2471 *
2472 * <p>This method can only be called by the system UID
2473 * {@hide}
2474 */
Jeff Sharkey69ddab42012-08-25 00:05:46 -07002475 public boolean updateLockdownVpn() {
2476 try {
2477 return mService.updateLockdownVpn();
2478 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07002479 throw e.rethrowFromSystemServer();
Jeff Sharkey69ddab42012-08-25 00:05:46 -07002480 }
2481 }
Irfan Sheriffda6da092012-08-16 12:49:23 -07002482
2483 /**
Wink Saville948282b2013-08-29 08:55:16 -07002484 * Check mobile provisioning.
Wink Savilleab9321d2013-06-29 21:10:57 -07002485 *
Wink Savilleab9321d2013-06-29 21:10:57 -07002486 * @param suggestedTimeOutMs, timeout in milliseconds
Wink Savilleab9321d2013-06-29 21:10:57 -07002487 *
2488 * @return time out that will be used, maybe less that suggestedTimeOutMs
2489 * -1 if an error.
2490 *
2491 * {@hide}
2492 */
Wink Saville948282b2013-08-29 08:55:16 -07002493 public int checkMobileProvisioning(int suggestedTimeOutMs) {
Wink Savilleab9321d2013-06-29 21:10:57 -07002494 int timeOutMs = -1;
2495 try {
Wink Saville948282b2013-08-29 08:55:16 -07002496 timeOutMs = mService.checkMobileProvisioning(suggestedTimeOutMs);
Wink Savilleab9321d2013-06-29 21:10:57 -07002497 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07002498 throw e.rethrowFromSystemServer();
Wink Savilleab9321d2013-06-29 21:10:57 -07002499 }
2500 return timeOutMs;
2501 }
Robert Greenwalte182bfe2013-07-16 12:06:09 -07002502
2503 /**
Wink Saville42d4f082013-07-20 20:31:59 -07002504 * Get the mobile provisioning url.
Robert Greenwalte182bfe2013-07-16 12:06:09 -07002505 * {@hide}
2506 */
2507 public String getMobileProvisioningUrl() {
2508 try {
2509 return mService.getMobileProvisioningUrl();
2510 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07002511 throw e.rethrowFromSystemServer();
Robert Greenwalte182bfe2013-07-16 12:06:09 -07002512 }
Robert Greenwalte182bfe2013-07-16 12:06:09 -07002513 }
Wink Saville42d4f082013-07-20 20:31:59 -07002514
2515 /**
Wink Saville948282b2013-08-29 08:55:16 -07002516 * Set sign in error notification to visible or in visible
2517 *
2518 * @param visible
2519 * @param networkType
2520 *
2521 * {@hide}
Paul Jensen3541e9f2015-03-18 12:23:02 -04002522 * @deprecated Doesn't properly deal with multiple connected networks of the same type.
Wink Saville948282b2013-08-29 08:55:16 -07002523 */
Aurimas Liutikas514c5ef2016-05-24 15:22:55 -07002524 @Deprecated
Wink Saville948282b2013-08-29 08:55:16 -07002525 public void setProvisioningNotificationVisible(boolean visible, int networkType,
Paul Jensen89e0f092014-09-15 15:59:36 -04002526 String action) {
Wink Saville948282b2013-08-29 08:55:16 -07002527 try {
Paul Jensen89e0f092014-09-15 15:59:36 -04002528 mService.setProvisioningNotificationVisible(visible, networkType, action);
Wink Saville948282b2013-08-29 08:55:16 -07002529 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07002530 throw e.rethrowFromSystemServer();
Wink Saville948282b2013-08-29 08:55:16 -07002531 }
2532 }
Yuhao Zheng5cd1a0e2013-09-09 17:00:04 -07002533
2534 /**
2535 * Set the value for enabling/disabling airplane mode
Paul Jensenb2748922015-05-06 11:10:18 -04002536 * <p>This method requires the caller to hold the permission
2537 * {@link android.Manifest.permission#CONNECTIVITY_INTERNAL}.
Yuhao Zheng5cd1a0e2013-09-09 17:00:04 -07002538 *
2539 * @param enable whether to enable airplane mode or not
2540 *
Yuhao Zheng5cd1a0e2013-09-09 17:00:04 -07002541 * @hide
2542 */
2543 public void setAirplaneMode(boolean enable) {
2544 try {
2545 mService.setAirplaneMode(enable);
2546 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07002547 throw e.rethrowFromSystemServer();
Yuhao Zheng5cd1a0e2013-09-09 17:00:04 -07002548 }
2549 }
Robert Greenwalte049c232014-04-11 15:53:27 -07002550
2551 /** {@hide} */
Robert Greenwalta67be032014-05-16 15:49:14 -07002552 public void registerNetworkFactory(Messenger messenger, String name) {
Robert Greenwalte049c232014-04-11 15:53:27 -07002553 try {
Robert Greenwalta67be032014-05-16 15:49:14 -07002554 mService.registerNetworkFactory(messenger, name);
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07002555 } catch (RemoteException e) {
2556 throw e.rethrowFromSystemServer();
2557 }
Robert Greenwalta67be032014-05-16 15:49:14 -07002558 }
2559
2560 /** {@hide} */
2561 public void unregisterNetworkFactory(Messenger messenger) {
2562 try {
2563 mService.unregisterNetworkFactory(messenger);
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07002564 } catch (RemoteException e) {
2565 throw e.rethrowFromSystemServer();
2566 }
Robert Greenwalte049c232014-04-11 15:53:27 -07002567 }
Robert Greenwalt7b816022014-04-18 15:25:25 -07002568
Paul Jensen31a94f42015-02-13 14:18:39 -05002569 /**
2570 * @hide
2571 * Register a NetworkAgent with ConnectivityService.
2572 * @return NetID corresponding to NetworkAgent.
2573 */
2574 public int registerNetworkAgent(Messenger messenger, NetworkInfo ni, LinkProperties lp,
Sreeram Ramachandran8cd33ed2014-07-23 15:23:15 -07002575 NetworkCapabilities nc, int score, NetworkMisc misc) {
Robert Greenwalt7b816022014-04-18 15:25:25 -07002576 try {
Paul Jensen31a94f42015-02-13 14:18:39 -05002577 return mService.registerNetworkAgent(messenger, ni, lp, nc, score, misc);
2578 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07002579 throw e.rethrowFromSystemServer();
Paul Jensen31a94f42015-02-13 14:18:39 -05002580 }
Robert Greenwalt7b816022014-04-18 15:25:25 -07002581 }
2582
Robert Greenwalt9258c642014-03-26 16:47:06 -07002583 /**
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07002584 * Base class for NetworkRequest callbacks. Used for notifications about network
2585 * changes. Should be extended by applications wanting notifications.
Robert Greenwalt9258c642014-03-26 16:47:06 -07002586 */
Robert Greenwalt6078b502014-06-11 16:05:07 -07002587 public static class NetworkCallback {
Robert Greenwalt7b816022014-04-18 15:25:25 -07002588 /**
Lorenzo Colitti07086932015-04-24 12:23:24 +09002589 * Called when the framework connects to a new network to evaluate whether it satisfies this
2590 * request. If evaluation succeeds, this callback may be followed by an {@link #onAvailable}
2591 * callback. There is no guarantee that this new network will satisfy any requests, or that
2592 * the network will stay connected for longer than the time necessary to evaluate it.
2593 * <p>
2594 * Most applications <b>should not</b> act on this callback, and should instead use
2595 * {@link #onAvailable}. This callback is intended for use by applications that can assist
2596 * the framework in properly evaluating the network &mdash; for example, an application that
2597 * can automatically log in to a captive portal without user intervention.
2598 *
2599 * @param network The {@link Network} of the network that is being evaluated.
Lorenzo Colitti66276122015-06-11 14:27:17 +09002600 *
2601 * @hide
Robert Greenwalt7b816022014-04-18 15:25:25 -07002602 */
Robert Greenwalt6078b502014-06-11 16:05:07 -07002603 public void onPreCheck(Network network) {}
Robert Greenwalt7b816022014-04-18 15:25:25 -07002604
2605 /**
Lorenzo Colitti07086932015-04-24 12:23:24 +09002606 * Called when the framework connects and has declared a new network ready for use.
Robert Greenwalt6078b502014-06-11 16:05:07 -07002607 * This callback may be called more than once if the {@link Network} that is
2608 * satisfying the request changes.
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07002609 *
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07002610 * @param network The {@link Network} of the satisfying network.
Robert Greenwalt7b816022014-04-18 15:25:25 -07002611 */
Robert Greenwalt6078b502014-06-11 16:05:07 -07002612 public void onAvailable(Network network) {}
Robert Greenwalt7b816022014-04-18 15:25:25 -07002613
2614 /**
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07002615 * Called when the network is about to be disconnected. Often paired with an
Robert Greenwalt6078b502014-06-11 16:05:07 -07002616 * {@link NetworkCallback#onAvailable} call with the new replacement network
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07002617 * for graceful handover. This may not be called if we have a hard loss
2618 * (loss without warning). This may be followed by either a
Robert Greenwalt6078b502014-06-11 16:05:07 -07002619 * {@link NetworkCallback#onLost} call or a
2620 * {@link NetworkCallback#onAvailable} call for this network depending
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07002621 * on whether we lose or regain it.
2622 *
Robert Greenwalt6078b502014-06-11 16:05:07 -07002623 * @param network The {@link Network} that is about to be disconnected.
2624 * @param maxMsToLive The time in ms the framework will attempt to keep the
2625 * network connected. Note that the network may suffer a
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07002626 * hard loss at any time.
Robert Greenwalt7b816022014-04-18 15:25:25 -07002627 */
Robert Greenwalt6078b502014-06-11 16:05:07 -07002628 public void onLosing(Network network, int maxMsToLive) {}
Robert Greenwalt7b816022014-04-18 15:25:25 -07002629
2630 /**
2631 * Called when the framework has a hard loss of the network or when the
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07002632 * graceful failure ends.
2633 *
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07002634 * @param network The {@link Network} lost.
Robert Greenwalt7b816022014-04-18 15:25:25 -07002635 */
Robert Greenwalt6078b502014-06-11 16:05:07 -07002636 public void onLost(Network network) {}
Robert Greenwalt7b816022014-04-18 15:25:25 -07002637
2638 /**
Etan Cohenaebf17e2017-03-01 12:47:28 -08002639 * Called if no network is found in the timeout time specified in
2640 * {@link #requestNetwork(NetworkRequest, int, NetworkCallback)} call. This callback is not
2641 * called for the version of {@link #requestNetwork(NetworkRequest, NetworkCallback)}
2642 * without timeout. When this callback is invoked the associated
2643 * {@link NetworkRequest} will have already been removed and released, as if
2644 * {@link #unregisterNetworkCallback(NetworkCallback)} had been called.
Robert Greenwalt7b816022014-04-18 15:25:25 -07002645 */
Robert Greenwalt6078b502014-06-11 16:05:07 -07002646 public void onUnavailable() {}
Robert Greenwalt7b816022014-04-18 15:25:25 -07002647
2648 /**
2649 * Called when the network the framework connected to for this request
2650 * changes capabilities but still satisfies the stated need.
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07002651 *
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07002652 * @param network The {@link Network} whose capabilities have changed.
Lorenzo Colittie285b432015-04-23 15:32:42 +09002653 * @param networkCapabilities The new {@link android.net.NetworkCapabilities} for this network.
Robert Greenwalt7b816022014-04-18 15:25:25 -07002654 */
Robert Greenwalt6078b502014-06-11 16:05:07 -07002655 public void onCapabilitiesChanged(Network network,
Robert Greenwalt7b816022014-04-18 15:25:25 -07002656 NetworkCapabilities networkCapabilities) {}
2657
2658 /**
2659 * Called when the network the framework connected to for this request
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07002660 * changes {@link LinkProperties}.
2661 *
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07002662 * @param network The {@link Network} whose link properties have changed.
2663 * @param linkProperties The new {@link LinkProperties} for this network.
Robert Greenwalt7b816022014-04-18 15:25:25 -07002664 */
Robert Greenwalt6078b502014-06-11 16:05:07 -07002665 public void onLinkPropertiesChanged(Network network, LinkProperties linkProperties) {}
Robert Greenwalt7b816022014-04-18 15:25:25 -07002666
Robert Greenwalt8d482522015-06-24 13:23:42 -07002667 /**
2668 * Called when the network the framework connected to for this request
2669 * goes into {@link NetworkInfo.DetailedState.SUSPENDED}.
2670 * This generally means that while the TCP connections are still live,
2671 * temporarily network data fails to transfer. Specifically this is used
2672 * on cellular networks to mask temporary outages when driving through
2673 * a tunnel, etc.
2674 * @hide
2675 */
2676 public void onNetworkSuspended(Network network) {}
2677
2678 /**
2679 * Called when the network the framework connected to for this request
2680 * returns from a {@link NetworkInfo.DetailedState.SUSPENDED} state.
2681 * This should always be preceeded by a matching {@code onNetworkSuspended}
2682 * call.
2683 * @hide
2684 */
2685 public void onNetworkResumed(Network network) {}
2686
Robert Greenwalt6078b502014-06-11 16:05:07 -07002687 private NetworkRequest networkRequest;
Robert Greenwalt7b816022014-04-18 15:25:25 -07002688 }
2689
Robert Greenwalt9258c642014-03-26 16:47:06 -07002690 private static final int BASE = Protocol.BASE_CONNECTIVITY_MANAGER;
Robert Greenwalt9258c642014-03-26 16:47:06 -07002691 /** @hide */
Robert Greenwalt8d482522015-06-24 13:23:42 -07002692 public static final int CALLBACK_PRECHECK = BASE + 1;
2693 /** @hide */
2694 public static final int CALLBACK_AVAILABLE = BASE + 2;
2695 /** @hide arg1 = TTL */
2696 public static final int CALLBACK_LOSING = BASE + 3;
2697 /** @hide */
2698 public static final int CALLBACK_LOST = BASE + 4;
2699 /** @hide */
2700 public static final int CALLBACK_UNAVAIL = BASE + 5;
2701 /** @hide */
2702 public static final int CALLBACK_CAP_CHANGED = BASE + 6;
2703 /** @hide */
2704 public static final int CALLBACK_IP_CHANGED = BASE + 7;
2705 /** @hide */
2706 public static final int CALLBACK_RELEASED = BASE + 8;
Hugo Benichi7724cdd2016-07-07 10:15:56 +09002707 // TODO: consider deleting CALLBACK_EXIT and shifting following enum codes down by 1.
Robert Greenwalt8d482522015-06-24 13:23:42 -07002708 /** @hide */
2709 public static final int CALLBACK_EXIT = BASE + 9;
Robert Greenwalt562cc542014-05-15 18:07:26 -07002710 /** @hide obj = NetworkCapabilities, arg1 = seq number */
Robert Greenwalt8d482522015-06-24 13:23:42 -07002711 private static final int EXPIRE_LEGACY_REQUEST = BASE + 10;
2712 /** @hide */
2713 public static final int CALLBACK_SUSPENDED = BASE + 11;
2714 /** @hide */
2715 public static final int CALLBACK_RESUMED = BASE + 12;
Robert Greenwalt9258c642014-03-26 16:47:06 -07002716
Erik Kline57faba92015-11-25 12:49:38 +09002717 /** @hide */
2718 public static String getCallbackName(int whichCallback) {
2719 switch (whichCallback) {
2720 case CALLBACK_PRECHECK: return "CALLBACK_PRECHECK";
2721 case CALLBACK_AVAILABLE: return "CALLBACK_AVAILABLE";
2722 case CALLBACK_LOSING: return "CALLBACK_LOSING";
2723 case CALLBACK_LOST: return "CALLBACK_LOST";
2724 case CALLBACK_UNAVAIL: return "CALLBACK_UNAVAIL";
2725 case CALLBACK_CAP_CHANGED: return "CALLBACK_CAP_CHANGED";
2726 case CALLBACK_IP_CHANGED: return "CALLBACK_IP_CHANGED";
2727 case CALLBACK_RELEASED: return "CALLBACK_RELEASED";
2728 case CALLBACK_EXIT: return "CALLBACK_EXIT";
2729 case EXPIRE_LEGACY_REQUEST: return "EXPIRE_LEGACY_REQUEST";
2730 case CALLBACK_SUSPENDED: return "CALLBACK_SUSPENDED";
2731 case CALLBACK_RESUMED: return "CALLBACK_RESUMED";
2732 default:
2733 return Integer.toString(whichCallback);
2734 }
2735 }
2736
Robert Greenwalt562cc542014-05-15 18:07:26 -07002737 private class CallbackHandler extends Handler {
Robert Greenwalt9258c642014-03-26 16:47:06 -07002738 private static final String TAG = "ConnectivityManager.CallbackHandler";
Robert Greenwalta9ebeef2015-09-03 16:41:45 -07002739 private static final boolean DBG = false;
Robert Greenwalt9258c642014-03-26 16:47:06 -07002740
Hugo Benichid42650f2016-07-06 22:53:17 +09002741 CallbackHandler(Looper looper) {
Robert Greenwalt9258c642014-03-26 16:47:06 -07002742 super(looper);
Robert Greenwalt9258c642014-03-26 16:47:06 -07002743 }
2744
Hugo Benichi2583ef02017-02-02 17:02:36 +09002745 CallbackHandler(Handler handler) {
2746 this(handler.getLooper());
2747 }
2748
Robert Greenwalt9258c642014-03-26 16:47:06 -07002749 @Override
2750 public void handleMessage(Message message) {
Hugo Benichid42650f2016-07-06 22:53:17 +09002751 NetworkRequest request = getObject(message, NetworkRequest.class);
2752 Network network = getObject(message, Network.class);
Lorenzo Colittifcfa7d92016-03-01 22:56:37 +09002753 if (DBG) {
2754 Log.d(TAG, whatToString(message.what) + " for network " + network);
2755 }
Robert Greenwalt9258c642014-03-26 16:47:06 -07002756 switch (message.what) {
2757 case CALLBACK_PRECHECK: {
Lorenzo Colittidb95a602015-04-24 15:21:21 +09002758 NetworkCallback callback = getCallback(request, "PRECHECK");
2759 if (callback != null) {
2760 callback.onPreCheck(network);
Robert Greenwalt9258c642014-03-26 16:47:06 -07002761 }
2762 break;
2763 }
2764 case CALLBACK_AVAILABLE: {
Lorenzo Colittidb95a602015-04-24 15:21:21 +09002765 NetworkCallback callback = getCallback(request, "AVAILABLE");
2766 if (callback != null) {
2767 callback.onAvailable(network);
Robert Greenwalt9258c642014-03-26 16:47:06 -07002768 }
2769 break;
2770 }
2771 case CALLBACK_LOSING: {
Lorenzo Colittidb95a602015-04-24 15:21:21 +09002772 NetworkCallback callback = getCallback(request, "LOSING");
2773 if (callback != null) {
2774 callback.onLosing(network, message.arg1);
Robert Greenwalt9258c642014-03-26 16:47:06 -07002775 }
2776 break;
2777 }
2778 case CALLBACK_LOST: {
Lorenzo Colittidb95a602015-04-24 15:21:21 +09002779 NetworkCallback callback = getCallback(request, "LOST");
2780 if (callback != null) {
2781 callback.onLost(network);
Robert Greenwalt9258c642014-03-26 16:47:06 -07002782 }
2783 break;
2784 }
2785 case CALLBACK_UNAVAIL: {
Lorenzo Colittidb95a602015-04-24 15:21:21 +09002786 NetworkCallback callback = getCallback(request, "UNAVAIL");
2787 if (callback != null) {
2788 callback.onUnavailable();
Robert Greenwalt9258c642014-03-26 16:47:06 -07002789 }
2790 break;
2791 }
2792 case CALLBACK_CAP_CHANGED: {
Lorenzo Colittidb95a602015-04-24 15:21:21 +09002793 NetworkCallback callback = getCallback(request, "CAP_CHANGED");
2794 if (callback != null) {
Hugo Benichid42650f2016-07-06 22:53:17 +09002795 NetworkCapabilities cap = getObject(message, NetworkCapabilities.class);
Lorenzo Colittidb95a602015-04-24 15:21:21 +09002796 callback.onCapabilitiesChanged(network, cap);
Robert Greenwalt9258c642014-03-26 16:47:06 -07002797 }
2798 break;
2799 }
2800 case CALLBACK_IP_CHANGED: {
Lorenzo Colittidb95a602015-04-24 15:21:21 +09002801 NetworkCallback callback = getCallback(request, "IP_CHANGED");
2802 if (callback != null) {
Hugo Benichid42650f2016-07-06 22:53:17 +09002803 LinkProperties lp = getObject(message, LinkProperties.class);
Lorenzo Colittidb95a602015-04-24 15:21:21 +09002804 callback.onLinkPropertiesChanged(network, lp);
Robert Greenwalt9258c642014-03-26 16:47:06 -07002805 }
2806 break;
2807 }
Robert Greenwalt8d482522015-06-24 13:23:42 -07002808 case CALLBACK_SUSPENDED: {
2809 NetworkCallback callback = getCallback(request, "SUSPENDED");
2810 if (callback != null) {
2811 callback.onNetworkSuspended(network);
2812 }
2813 break;
2814 }
2815 case CALLBACK_RESUMED: {
2816 NetworkCallback callback = getCallback(request, "RESUMED");
2817 if (callback != null) {
2818 callback.onNetworkResumed(network);
2819 }
2820 break;
2821 }
Robert Greenwalt9258c642014-03-26 16:47:06 -07002822 case CALLBACK_RELEASED: {
Hugo Benichi7724cdd2016-07-07 10:15:56 +09002823 final NetworkCallback callback;
Hugo Benichid42650f2016-07-06 22:53:17 +09002824 synchronized(sCallbacks) {
2825 callback = sCallbacks.remove(request);
Robert Greenwalt9258c642014-03-26 16:47:06 -07002826 }
Hugo Benichi7724cdd2016-07-07 10:15:56 +09002827 if (callback == null) {
Lorenzo Colittidb95a602015-04-24 15:21:21 +09002828 Log.e(TAG, "callback not found for RELEASED message");
Robert Greenwalt9258c642014-03-26 16:47:06 -07002829 }
Robert Greenwalt9258c642014-03-26 16:47:06 -07002830 break;
2831 }
2832 case CALLBACK_EXIT: {
Robert Greenwalt9258c642014-03-26 16:47:06 -07002833 break;
2834 }
Robert Greenwalt562cc542014-05-15 18:07:26 -07002835 case EXPIRE_LEGACY_REQUEST: {
2836 expireRequest((NetworkCapabilities)message.obj, message.arg1);
2837 break;
2838 }
Robert Greenwalt9258c642014-03-26 16:47:06 -07002839 }
2840 }
2841
Hugo Benichid42650f2016-07-06 22:53:17 +09002842 private <T> T getObject(Message msg, Class<T> c) {
2843 return (T) msg.getData().getParcelable(c.getSimpleName());
Robert Greenwalt9258c642014-03-26 16:47:06 -07002844 }
Lorenzo Colittidb95a602015-04-24 15:21:21 +09002845
2846 private NetworkCallback getCallback(NetworkRequest req, String name) {
2847 NetworkCallback callback;
Hugo Benichid42650f2016-07-06 22:53:17 +09002848 synchronized(sCallbacks) {
2849 callback = sCallbacks.get(req);
Robert Greenwalt9258c642014-03-26 16:47:06 -07002850 }
Lorenzo Colittidb95a602015-04-24 15:21:21 +09002851 if (callback == null) {
2852 Log.e(TAG, "callback not found for " + name + " message");
2853 }
2854 return callback;
Robert Greenwalt9258c642014-03-26 16:47:06 -07002855 }
Robert Greenwalt9258c642014-03-26 16:47:06 -07002856 }
2857
Hugo Benichi2583ef02017-02-02 17:02:36 +09002858 private CallbackHandler getDefaultHandler() {
Hugo Benichi7724cdd2016-07-07 10:15:56 +09002859 synchronized (sCallbacks) {
2860 if (sCallbackHandler == null) {
2861 sCallbackHandler = new CallbackHandler(ConnectivityThread.getInstanceLooper());
Robert Greenwalt9258c642014-03-26 16:47:06 -07002862 }
Hugo Benichi7724cdd2016-07-07 10:15:56 +09002863 return sCallbackHandler;
Robert Greenwalt9258c642014-03-26 16:47:06 -07002864 }
2865 }
2866
Hugo Benichi6f260f32017-02-03 14:18:44 +09002867 private static final HashMap<NetworkRequest, NetworkCallback> sCallbacks = new HashMap<>();
2868 private static CallbackHandler sCallbackHandler;
Robert Greenwalt9258c642014-03-26 16:47:06 -07002869
Hugo Benichi6f260f32017-02-03 14:18:44 +09002870 private static final int LISTEN = 1;
2871 private static final int REQUEST = 2;
Robert Greenwalt9258c642014-03-26 16:47:06 -07002872
Hugo Benichi6f260f32017-02-03 14:18:44 +09002873 private NetworkRequest sendRequestForNetwork(NetworkCapabilities need, NetworkCallback callback,
2874 int timeoutMs, int action, int legacyType, CallbackHandler handler) {
Hugo Benichi7724cdd2016-07-07 10:15:56 +09002875 if (callback == null) {
Robert Greenwalt6078b502014-06-11 16:05:07 -07002876 throw new IllegalArgumentException("null NetworkCallback");
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07002877 }
Hugo Benichi649e3242017-03-06 06:16:51 +00002878 if (need == null && action != REQUEST) {
Erik Klinea2d29402016-03-16 15:31:39 +09002879 throw new IllegalArgumentException("null NetworkCapabilities");
2880 }
Hugo Benichi649e3242017-03-06 06:16:51 +00002881 // TODO: throw an exception if callback.networkRequest is not null.
2882 // http://b/20701525
Hugo Benichid42650f2016-07-06 22:53:17 +09002883 final NetworkRequest request;
Robert Greenwalt9258c642014-03-26 16:47:06 -07002884 try {
Hugo Benichid42650f2016-07-06 22:53:17 +09002885 synchronized(sCallbacks) {
Hugo Benichi7724cdd2016-07-07 10:15:56 +09002886 Messenger messenger = new Messenger(handler);
Hugo Benichid42650f2016-07-06 22:53:17 +09002887 Binder binder = new Binder();
Paul Jensen7221cc32014-06-27 11:05:32 -04002888 if (action == LISTEN) {
Hugo Benichid42650f2016-07-06 22:53:17 +09002889 request = mService.listenForNetwork(need, messenger, binder);
Paul Jensen7221cc32014-06-27 11:05:32 -04002890 } else {
Hugo Benichid42650f2016-07-06 22:53:17 +09002891 request = mService.requestNetwork(
2892 need, messenger, timeoutMs, binder, legacyType);
Paul Jensen7221cc32014-06-27 11:05:32 -04002893 }
Hugo Benichid42650f2016-07-06 22:53:17 +09002894 if (request != null) {
Hugo Benichi7724cdd2016-07-07 10:15:56 +09002895 sCallbacks.put(request, callback);
Robert Greenwalt9258c642014-03-26 16:47:06 -07002896 }
Hugo Benichi7724cdd2016-07-07 10:15:56 +09002897 callback.networkRequest = request;
Robert Greenwalt9258c642014-03-26 16:47:06 -07002898 }
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07002899 } catch (RemoteException e) {
2900 throw e.rethrowFromSystemServer();
2901 }
Hugo Benichid42650f2016-07-06 22:53:17 +09002902 return request;
Robert Greenwalt9258c642014-03-26 16:47:06 -07002903 }
2904
2905 /**
Erik Klinea2d29402016-03-16 15:31:39 +09002906 * Helper function to request a network with a particular legacy type.
Lorenzo Colitti7de289f2015-11-25 12:00:52 +09002907 *
2908 * This is temporarily public @hide so it can be called by system code that uses the
2909 * NetworkRequest API to request networks but relies on CONNECTIVITY_ACTION broadcasts for
2910 * instead network notifications.
2911 *
2912 * TODO: update said system code to rely on NetworkCallbacks and make this method private.
2913 *
2914 * @hide
2915 */
Lorenzo Colittid1179462015-11-25 15:47:14 +09002916 public void requestNetwork(NetworkRequest request, NetworkCallback networkCallback,
Hugo Benichi2583ef02017-02-02 17:02:36 +09002917 int timeoutMs, int legacyType, Handler handler) {
2918 CallbackHandler cbHandler = new CallbackHandler(handler);
2919 NetworkCapabilities nc = request.networkCapabilities;
2920 sendRequestForNetwork(nc, networkCallback, timeoutMs, REQUEST, legacyType, cbHandler);
Lorenzo Colitti7de289f2015-11-25 12:00:52 +09002921 }
2922
2923 /**
Lorenzo Colittie285b432015-04-23 15:32:42 +09002924 * Request a network to satisfy a set of {@link android.net.NetworkCapabilities}.
Robert Greenwalt9258c642014-03-26 16:47:06 -07002925 *
2926 * This {@link NetworkRequest} will live until released via
Etan Cohenaebf17e2017-03-01 12:47:28 -08002927 * {@link #unregisterNetworkCallback(NetworkCallback)} or the calling application exits. A
2928 * version of the method which takes a timeout is
2929 * {@link #requestNetwork(NetworkRequest, int, NetworkCallback)}.
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07002930 * Status of the request can be followed by listening to the various
Robert Greenwalt6078b502014-06-11 16:05:07 -07002931 * callbacks described in {@link NetworkCallback}. The {@link Network}
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07002932 * can be used to direct traffic to the network.
Paul Jensenbb2e0e92015-06-16 15:11:58 -04002933 * <p>It is presently unsupported to request a network with mutable
2934 * {@link NetworkCapabilities} such as
2935 * {@link NetworkCapabilities#NET_CAPABILITY_VALIDATED} or
2936 * {@link NetworkCapabilities#NET_CAPABILITY_CAPTIVE_PORTAL}
2937 * as these {@code NetworkCapabilities} represent states that a particular
2938 * network may never attain, and whether a network will attain these states
2939 * is unknown prior to bringing up the network so the framework does not
2940 * know how to go about satisfing a request with these capabilities.
Lorenzo Colittid5427052015-10-15 16:29:00 +09002941 *
2942 * <p>This method requires the caller to hold either the
2943 * {@link android.Manifest.permission#CHANGE_NETWORK_STATE} permission
2944 * or the ability to modify system settings as determined by
2945 * {@link android.provider.Settings.System#canWrite}.</p>
Robert Greenwalt9258c642014-03-26 16:47:06 -07002946 *
Robert Greenwalt6078b502014-06-11 16:05:07 -07002947 * @param request {@link NetworkRequest} describing this request.
Hugo Benichi2583ef02017-02-02 17:02:36 +09002948 * @param networkCallback The {@link NetworkCallback} to be utilized for this request. Note
2949 * the callback must not be shared - it uniquely specifies this request.
2950 * The callback is invoked on the default internal Handler.
Paul Jensenbb2e0e92015-06-16 15:11:58 -04002951 * @throws IllegalArgumentException if {@code request} specifies any mutable
2952 * {@code NetworkCapabilities}.
Robert Greenwalt9258c642014-03-26 16:47:06 -07002953 */
Robert Greenwalt6078b502014-06-11 16:05:07 -07002954 public void requestNetwork(NetworkRequest request, NetworkCallback networkCallback) {
Hugo Benichi2583ef02017-02-02 17:02:36 +09002955 requestNetwork(request, networkCallback, getDefaultHandler());
2956 }
2957
2958 /**
2959 * Request a network to satisfy a set of {@link android.net.NetworkCapabilities}.
2960 *
2961 * This {@link NetworkRequest} will live until released via
Etan Cohenaebf17e2017-03-01 12:47:28 -08002962 * {@link #unregisterNetworkCallback(NetworkCallback)} or the calling application exits. A
2963 * version of the method which takes a timeout is
2964 * {@link #requestNetwork(NetworkRequest, int, NetworkCallback)}.
Hugo Benichi2583ef02017-02-02 17:02:36 +09002965 * Status of the request can be followed by listening to the various
2966 * callbacks described in {@link NetworkCallback}. The {@link Network}
2967 * can be used to direct traffic to the network.
2968 * <p>It is presently unsupported to request a network with mutable
2969 * {@link NetworkCapabilities} such as
2970 * {@link NetworkCapabilities#NET_CAPABILITY_VALIDATED} or
2971 * {@link NetworkCapabilities#NET_CAPABILITY_CAPTIVE_PORTAL}
2972 * as these {@code NetworkCapabilities} represent states that a particular
2973 * network may never attain, and whether a network will attain these states
2974 * is unknown prior to bringing up the network so the framework does not
2975 * know how to go about satisfing a request with these capabilities.
2976 *
2977 * <p>This method requires the caller to hold either the
2978 * {@link android.Manifest.permission#CHANGE_NETWORK_STATE} permission
2979 * or the ability to modify system settings as determined by
2980 * {@link android.provider.Settings.System#canWrite}.</p>
2981 *
2982 * @param request {@link NetworkRequest} describing this request.
2983 * @param networkCallback The {@link NetworkCallback} to be utilized for this request. Note
2984 * the callback must not be shared - it uniquely specifies this request.
2985 * @param handler {@link Handler} to specify the thread upon which the callback will be invoked.
2986 * @throws IllegalArgumentException if {@code request} specifies any mutable
2987 * {@code NetworkCapabilities}.
Hugo Benichi2583ef02017-02-02 17:02:36 +09002988 */
2989 public void requestNetwork(
2990 NetworkRequest request, NetworkCallback networkCallback, Handler handler) {
2991 int legacyType = inferLegacyTypeForNetworkCapabilities(request.networkCapabilities);
2992 CallbackHandler cbHandler = new CallbackHandler(handler);
2993 requestNetwork(request, networkCallback, 0, legacyType, cbHandler);
Robert Greenwalt9258c642014-03-26 16:47:06 -07002994 }
2995
2996 /**
Etan Cohenaebf17e2017-03-01 12:47:28 -08002997 * Note: this is a deprecated version of
2998 * {@link #requestNetwork(NetworkRequest, int, NetworkCallback)} - please transition code to use
2999 * the unhidden version of the function.
3000 * TODO: replace all callers with the new version of the API
3001 *
Lorenzo Colittie285b432015-04-23 15:32:42 +09003002 * Request a network to satisfy a set of {@link android.net.NetworkCapabilities}, limited
Robert Greenwalt9258c642014-03-26 16:47:06 -07003003 * by a timeout.
3004 *
Etan Cohenaebf17e2017-03-01 12:47:28 -08003005 * This function behaves identically to the non-timed-out version
3006 * {@link #requestNetwork(NetworkRequest, NetworkCallback)}, but if a suitable network
3007 * is not found within the given time (in milliseconds) the
3008 * {@link NetworkCallback#onUnavailable()} callback is called. The request can still be
3009 * released normally by calling {@link #unregisterNetworkCallback(NetworkCallback)} but does
3010 * not have to be released if timed-out (it is automatically released). Unregistering a
3011 * request that timed out is not an error.
3012 *
3013 * <p>Do not use this method to poll for the existence of specific networks (e.g. with a small
3014 * timeout) - the {@link #registerNetworkCallback(NetworkRequest, NetworkCallback)} is provided
3015 * for that purpose. Calling this method will attempt to bring up the requested network.
Lorenzo Colittid5427052015-10-15 16:29:00 +09003016 *
3017 * <p>This method requires the caller to hold either the
3018 * {@link android.Manifest.permission#CHANGE_NETWORK_STATE} permission
3019 * or the ability to modify system settings as determined by
3020 * {@link android.provider.Settings.System#canWrite}.</p>
3021 *
Robert Greenwalt6078b502014-06-11 16:05:07 -07003022 * @param request {@link NetworkRequest} describing this request.
Etan Cohenaebf17e2017-03-01 12:47:28 -08003023 * @param networkCallback The callbacks to be utilized for this request. Note
3024 * the callbacks must not be shared - they uniquely specify
3025 * this request.
Robert Greenwalt6078b502014-06-11 16:05:07 -07003026 * @param timeoutMs The time in milliseconds to attempt looking for a suitable network
Etan Cohenaebf17e2017-03-01 12:47:28 -08003027 * before {@link NetworkCallback#onUnavailable()} is called. The timeout must
3028 * be a positive value (i.e. >0).
Robert Greenwalt9258c642014-03-26 16:47:06 -07003029 * @hide
3030 */
Robert Greenwalt6078b502014-06-11 16:05:07 -07003031 public void requestNetwork(NetworkRequest request, NetworkCallback networkCallback,
3032 int timeoutMs) {
Etan Cohenaebf17e2017-03-01 12:47:28 -08003033 if (timeoutMs <= 0) {
3034 throw new IllegalArgumentException("Non-positive timeoutMs: " + timeoutMs);
3035 }
3036 int legacyType = inferLegacyTypeForNetworkCapabilities(request.networkCapabilities);
3037 requestNetwork(request, networkCallback, timeoutMs, legacyType, getDefaultHandler());
3038 }
3039
3040 /**
3041 * Request a network to satisfy a set of {@link android.net.NetworkCapabilities}, limited
3042 * by a timeout.
3043 *
3044 * This function behaves identically to the non-timed-out version
3045 * {@link #requestNetwork(NetworkRequest, NetworkCallback)}, but if a suitable network
3046 * is not found within the given time (in milliseconds) the
3047 * {@link NetworkCallback#onUnavailable()} callback is called. The request can still be
3048 * released normally by calling {@link #unregisterNetworkCallback(NetworkCallback)} but does
3049 * not have to be released if timed-out (it is automatically released). Unregistering a
3050 * request that timed out is not an error.
3051 *
3052 * <p>Do not use this method to poll for the existence of specific networks (e.g. with a small
3053 * timeout) - {@link #registerNetworkCallback(NetworkRequest, NetworkCallback)} is provided
3054 * for that purpose. Calling this method will attempt to bring up the requested network.
3055 *
3056 * <p>This method requires the caller to hold either the
3057 * {@link android.Manifest.permission#CHANGE_NETWORK_STATE} permission
3058 * or the ability to modify system settings as determined by
3059 * {@link android.provider.Settings.System#canWrite}.</p>
3060 *
3061 * @param request {@link NetworkRequest} describing this request.
3062 * @param timeoutMs The time in milliseconds to attempt looking for a suitable network
3063 * before {@link NetworkCallback#onUnavailable()} is called. The timeout must
3064 * be a positive value (i.e. >0).
3065 * @param networkCallback The {@link NetworkCallback} to be utilized for this request. Note
3066 * the callback must not be shared - it uniquely specifies this request.
3067 */
3068 public void requestNetwork(NetworkRequest request, int timeoutMs,
3069 NetworkCallback networkCallback) {
3070 if (timeoutMs <= 0) {
3071 throw new IllegalArgumentException("Non-positive timeoutMs: " + timeoutMs);
3072 }
Hugo Benichi2583ef02017-02-02 17:02:36 +09003073 int legacyType = inferLegacyTypeForNetworkCapabilities(request.networkCapabilities);
Erik Klineb583b032017-02-22 12:58:24 +09003074 requestNetwork(request, networkCallback, timeoutMs, legacyType, getDefaultHandler());
Hugo Benichi2583ef02017-02-02 17:02:36 +09003075 }
3076
3077
3078 /**
3079 * Request a network to satisfy a set of {@link android.net.NetworkCapabilities}, limited
3080 * by a timeout.
3081 *
3082 * This function behaves identically to the non-timedout version, but if a suitable
3083 * network is not found within the given time (in milliseconds) the
Etan Cohenaebf17e2017-03-01 12:47:28 -08003084 * {@link NetworkCallback#onUnavailable} callback is called. The request can still be
3085 * released normally by calling {@link #unregisterNetworkCallback(NetworkCallback)} but does
3086 * not have to be released if timed-out (it is automatically released). Unregistering a
3087 * request that timed out is not an error.
3088 *
3089 * <p>Do not use this method to poll for the existence of specific networks (e.g. with a small
3090 * timeout) - {@link #registerNetworkCallback(NetworkRequest, NetworkCallback)} is provided
3091 * for that purpose. Calling this method will attempt to bring up the requested network.
Hugo Benichi2583ef02017-02-02 17:02:36 +09003092 *
3093 * <p>This method requires the caller to hold either the
3094 * {@link android.Manifest.permission#CHANGE_NETWORK_STATE} permission
3095 * or the ability to modify system settings as determined by
3096 * {@link android.provider.Settings.System#canWrite}.</p>
3097 *
3098 * @param request {@link NetworkRequest} describing this request.
Hugo Benichi2583ef02017-02-02 17:02:36 +09003099 * @param timeoutMs The time in milliseconds to attempt looking for a suitable network
3100 * before {@link NetworkCallback#onUnavailable} is called.
Etan Cohenaebf17e2017-03-01 12:47:28 -08003101 * @param networkCallback The {@link NetworkCallback} to be utilized for this request. Note
3102 * the callback must not be shared - it uniquely specifies this request.
Hugo Benichi2583ef02017-02-02 17:02:36 +09003103 * @param handler {@link Handler} to specify the thread upon which the callback will be invoked.
Hugo Benichi2583ef02017-02-02 17:02:36 +09003104 */
Etan Cohenaebf17e2017-03-01 12:47:28 -08003105 public void requestNetwork(NetworkRequest request, int timeoutMs,
3106 NetworkCallback networkCallback, Handler handler) {
3107 if (timeoutMs <= 0) {
3108 throw new IllegalArgumentException("Non-positive timeoutMs");
3109 }
Hugo Benichi2583ef02017-02-02 17:02:36 +09003110 int legacyType = inferLegacyTypeForNetworkCapabilities(request.networkCapabilities);
3111 CallbackHandler cbHandler = new CallbackHandler(handler);
3112 requestNetwork(request, networkCallback, timeoutMs, legacyType, cbHandler);
Robert Greenwalt9258c642014-03-26 16:47:06 -07003113 }
3114
3115 /**
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07003116 * The lookup key for a {@link Network} object included with the intent after
Jeremy Joslin46e3ac82014-11-05 10:32:09 -08003117 * successfully finding a network for the applications request. Retrieve it with
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07003118 * {@link android.content.Intent#getParcelableExtra(String)}.
Jeremy Joslinfcde58f2015-02-11 16:51:13 -08003119 * <p>
Paul Jensen72db88e2015-03-10 10:54:12 -04003120 * Note that if you intend to invoke {@link Network#openConnection(java.net.URL)}
3121 * then you must get a ConnectivityManager instance before doing so.
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07003122 */
Erik Kline90e93072014-11-19 12:12:24 +09003123 public static final String EXTRA_NETWORK = "android.net.extra.NETWORK";
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07003124
3125 /**
Robert Greenwalt6078b502014-06-11 16:05:07 -07003126 * The lookup key for a {@link NetworkRequest} object included with the intent after
Jeremy Joslin46e3ac82014-11-05 10:32:09 -08003127 * successfully finding a network for the applications request. Retrieve it with
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07003128 * {@link android.content.Intent#getParcelableExtra(String)}.
3129 */
Erik Kline90e93072014-11-19 12:12:24 +09003130 public static final String EXTRA_NETWORK_REQUEST = "android.net.extra.NETWORK_REQUEST";
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07003131
3132
3133 /**
Lorenzo Colittie285b432015-04-23 15:32:42 +09003134 * Request a network to satisfy a set of {@link android.net.NetworkCapabilities}.
Robert Greenwalt9258c642014-03-26 16:47:06 -07003135 *
Jeremy Joslin46e3ac82014-11-05 10:32:09 -08003136 * This function behaves identically to the version that takes a NetworkCallback, but instead
Robert Greenwalt6078b502014-06-11 16:05:07 -07003137 * of {@link NetworkCallback} a {@link PendingIntent} is used. This means
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07003138 * the request may outlive the calling application and get called back when a suitable
3139 * network is found.
Robert Greenwalt9258c642014-03-26 16:47:06 -07003140 * <p>
3141 * The operation is an Intent broadcast that goes to a broadcast receiver that
3142 * you registered with {@link Context#registerReceiver} or through the
3143 * &lt;receiver&gt; tag in an AndroidManifest.xml file
3144 * <p>
3145 * The operation Intent is delivered with two extras, a {@link Network} typed
Erik Kline90e93072014-11-19 12:12:24 +09003146 * extra called {@link #EXTRA_NETWORK} and a {@link NetworkRequest}
3147 * typed extra called {@link #EXTRA_NETWORK_REQUEST} containing
Robert Greenwalt9258c642014-03-26 16:47:06 -07003148 * the original requests parameters. It is important to create a new,
Robert Greenwalt6078b502014-06-11 16:05:07 -07003149 * {@link NetworkCallback} based request before completing the processing of the
Robert Greenwalt9258c642014-03-26 16:47:06 -07003150 * Intent to reserve the network or it will be released shortly after the Intent
3151 * is processed.
3152 * <p>
Paul Jensen694f2b82015-06-17 14:15:39 -04003153 * If there is already a request for this Intent registered (with the equality of
Robert Greenwalt9258c642014-03-26 16:47:06 -07003154 * two Intents defined by {@link Intent#filterEquals}), then it will be removed and
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07003155 * replaced by this one, effectively releasing the previous {@link NetworkRequest}.
Robert Greenwalt9258c642014-03-26 16:47:06 -07003156 * <p>
Jeremy Joslin46e3ac82014-11-05 10:32:09 -08003157 * The request may be released normally by calling
3158 * {@link #releaseNetworkRequest(android.app.PendingIntent)}.
Paul Jensenbb2e0e92015-06-16 15:11:58 -04003159 * <p>It is presently unsupported to request a network with either
3160 * {@link NetworkCapabilities#NET_CAPABILITY_VALIDATED} or
3161 * {@link NetworkCapabilities#NET_CAPABILITY_CAPTIVE_PORTAL}
3162 * as these {@code NetworkCapabilities} represent states that a particular
3163 * network may never attain, and whether a network will attain these states
3164 * is unknown prior to bringing up the network so the framework does not
3165 * know how to go about satisfing a request with these capabilities.
Lorenzo Colittid5427052015-10-15 16:29:00 +09003166 *
3167 * <p>This method requires the caller to hold either the
3168 * {@link android.Manifest.permission#CHANGE_NETWORK_STATE} permission
3169 * or the ability to modify system settings as determined by
3170 * {@link android.provider.Settings.System#canWrite}.</p>
3171 *
Robert Greenwalt6078b502014-06-11 16:05:07 -07003172 * @param request {@link NetworkRequest} describing this request.
Robert Greenwalt9258c642014-03-26 16:47:06 -07003173 * @param operation Action to perform when the network is available (corresponds
Robert Greenwalt6078b502014-06-11 16:05:07 -07003174 * to the {@link NetworkCallback#onAvailable} call. Typically
Jeremy Joslin46e3ac82014-11-05 10:32:09 -08003175 * comes from {@link PendingIntent#getBroadcast}. Cannot be null.
Paul Jensenbb2e0e92015-06-16 15:11:58 -04003176 * @throws IllegalArgumentException if {@code request} contains either
3177 * {@link NetworkCapabilities#NET_CAPABILITY_VALIDATED} or
3178 * {@link NetworkCapabilities#NET_CAPABILITY_CAPTIVE_PORTAL}.
Robert Greenwalt9258c642014-03-26 16:47:06 -07003179 */
Robert Greenwalt6078b502014-06-11 16:05:07 -07003180 public void requestNetwork(NetworkRequest request, PendingIntent operation) {
Jeremy Joslin46e3ac82014-11-05 10:32:09 -08003181 checkPendingIntent(operation);
Robert Greenwalt9258c642014-03-26 16:47:06 -07003182 try {
Robert Greenwalt6078b502014-06-11 16:05:07 -07003183 mService.pendingRequestForNetwork(request.networkCapabilities, operation);
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07003184 } catch (RemoteException e) {
3185 throw e.rethrowFromSystemServer();
3186 }
Robert Greenwalt9258c642014-03-26 16:47:06 -07003187 }
3188
3189 /**
Jeremy Joslin46e3ac82014-11-05 10:32:09 -08003190 * Removes a request made via {@link #requestNetwork(NetworkRequest, android.app.PendingIntent)}
3191 * <p>
Lorenzo Colitti88bc0bb2016-04-13 22:00:02 +09003192 * This method has the same behavior as
3193 * {@link #unregisterNetworkCallback(android.app.PendingIntent)} with respect to
Jeremy Joslin46e3ac82014-11-05 10:32:09 -08003194 * releasing network resources and disconnecting.
3195 *
3196 * @param operation A PendingIntent equal (as defined by {@link Intent#filterEquals}) to the
3197 * PendingIntent passed to
3198 * {@link #requestNetwork(NetworkRequest, android.app.PendingIntent)} with the
3199 * corresponding NetworkRequest you'd like to remove. Cannot be null.
3200 */
3201 public void releaseNetworkRequest(PendingIntent operation) {
3202 checkPendingIntent(operation);
3203 try {
3204 mService.releasePendingNetworkRequest(operation);
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07003205 } catch (RemoteException e) {
3206 throw e.rethrowFromSystemServer();
3207 }
Jeremy Joslin46e3ac82014-11-05 10:32:09 -08003208 }
3209
3210 private void checkPendingIntent(PendingIntent intent) {
3211 if (intent == null) {
3212 throw new IllegalArgumentException("PendingIntent cannot be null.");
3213 }
3214 }
3215
3216 /**
Robert Greenwalt9258c642014-03-26 16:47:06 -07003217 * Registers to receive notifications about all networks which satisfy the given
Robert Greenwalt6078b502014-06-11 16:05:07 -07003218 * {@link NetworkRequest}. The callbacks will continue to be called until
Lorenzo Colitti88bc0bb2016-04-13 22:00:02 +09003219 * either the application exits or link #unregisterNetworkCallback(NetworkCallback)} is called.
Paul Jensenb2748922015-05-06 11:10:18 -04003220 * <p>This method requires the caller to hold the permission
3221 * {@link android.Manifest.permission#ACCESS_NETWORK_STATE}.
Robert Greenwalt9258c642014-03-26 16:47:06 -07003222 *
Robert Greenwalt6078b502014-06-11 16:05:07 -07003223 * @param request {@link NetworkRequest} describing this request.
3224 * @param networkCallback The {@link NetworkCallback} that the system will call as suitable
3225 * networks change state.
Hugo Benichi2583ef02017-02-02 17:02:36 +09003226 * The callback is invoked on the default internal Handler.
Robert Greenwalt9258c642014-03-26 16:47:06 -07003227 */
Robert Greenwalt6078b502014-06-11 16:05:07 -07003228 public void registerNetworkCallback(NetworkRequest request, NetworkCallback networkCallback) {
Hugo Benichi2583ef02017-02-02 17:02:36 +09003229 registerNetworkCallback(request, networkCallback, getDefaultHandler());
3230 }
3231
3232 /**
3233 * Registers to receive notifications about all networks which satisfy the given
3234 * {@link NetworkRequest}. The callbacks will continue to be called until
3235 * either the application exits or link #unregisterNetworkCallback(NetworkCallback)} is called.
3236 * <p>This method requires the caller to hold the permission
3237 * {@link android.Manifest.permission#ACCESS_NETWORK_STATE}.
3238 *
3239 * @param request {@link NetworkRequest} describing this request.
3240 * @param networkCallback The {@link NetworkCallback} that the system will call as suitable
3241 * networks change state.
3242 * @param handler {@link Handler} to specify the thread upon which the callback will be invoked.
Hugo Benichi2583ef02017-02-02 17:02:36 +09003243 */
3244 public void registerNetworkCallback(
3245 NetworkRequest request, NetworkCallback networkCallback, Handler handler) {
3246 CallbackHandler cbHandler = new CallbackHandler(handler);
3247 NetworkCapabilities nc = request.networkCapabilities;
3248 sendRequestForNetwork(nc, networkCallback, 0, LISTEN, TYPE_NONE, cbHandler);
Robert Greenwalt9258c642014-03-26 16:47:06 -07003249 }
3250
3251 /**
Paul Jensen694f2b82015-06-17 14:15:39 -04003252 * Registers a PendingIntent to be sent when a network is available which satisfies the given
3253 * {@link NetworkRequest}.
3254 *
3255 * This function behaves identically to the version that takes a NetworkCallback, but instead
3256 * of {@link NetworkCallback} a {@link PendingIntent} is used. This means
3257 * the request may outlive the calling application and get called back when a suitable
3258 * network is found.
3259 * <p>
3260 * The operation is an Intent broadcast that goes to a broadcast receiver that
3261 * you registered with {@link Context#registerReceiver} or through the
3262 * &lt;receiver&gt; tag in an AndroidManifest.xml file
3263 * <p>
3264 * The operation Intent is delivered with two extras, a {@link Network} typed
3265 * extra called {@link #EXTRA_NETWORK} and a {@link NetworkRequest}
3266 * typed extra called {@link #EXTRA_NETWORK_REQUEST} containing
3267 * the original requests parameters.
3268 * <p>
3269 * If there is already a request for this Intent registered (with the equality of
3270 * two Intents defined by {@link Intent#filterEquals}), then it will be removed and
3271 * replaced by this one, effectively releasing the previous {@link NetworkRequest}.
3272 * <p>
3273 * The request may be released normally by calling
Paul Jensenf2c1cfe2015-06-30 14:29:18 -04003274 * {@link #unregisterNetworkCallback(android.app.PendingIntent)}.
Paul Jensen694f2b82015-06-17 14:15:39 -04003275 * <p>This method requires the caller to hold the permission
3276 * {@link android.Manifest.permission#ACCESS_NETWORK_STATE}.
3277 * @param request {@link NetworkRequest} describing this request.
3278 * @param operation Action to perform when the network is available (corresponds
3279 * to the {@link NetworkCallback#onAvailable} call. Typically
3280 * comes from {@link PendingIntent#getBroadcast}. Cannot be null.
3281 */
3282 public void registerNetworkCallback(NetworkRequest request, PendingIntent operation) {
3283 checkPendingIntent(operation);
3284 try {
3285 mService.pendingListenForNetwork(request.networkCapabilities, operation);
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07003286 } catch (RemoteException e) {
3287 throw e.rethrowFromSystemServer();
3288 }
Paul Jensen694f2b82015-06-17 14:15:39 -04003289 }
3290
3291 /**
Lorenzo Colitti88bc0bb2016-04-13 22:00:02 +09003292 * Registers to receive notifications about changes in the system default network. The callbacks
3293 * will continue to be called until either the application exits or
3294 * {@link #unregisterNetworkCallback(NetworkCallback)} is called.
Erik Klinea2d29402016-03-16 15:31:39 +09003295 * <p>This method requires the caller to hold the permission
3296 * {@link android.Manifest.permission#ACCESS_NETWORK_STATE}.
3297 *
3298 * @param networkCallback The {@link NetworkCallback} that the system will call as the
3299 * system default network changes.
Hugo Benichi2583ef02017-02-02 17:02:36 +09003300 * The callback is invoked on the default internal Handler.
Erik Klinea2d29402016-03-16 15:31:39 +09003301 */
3302 public void registerDefaultNetworkCallback(NetworkCallback networkCallback) {
Hugo Benichi2583ef02017-02-02 17:02:36 +09003303 registerDefaultNetworkCallback(networkCallback, getDefaultHandler());
3304 }
3305
3306 /**
3307 * Registers to receive notifications about changes in the system default network. The callbacks
3308 * will continue to be called until either the application exits or
3309 * {@link #unregisterNetworkCallback(NetworkCallback)} is called.
3310 * <p>This method requires the caller to hold the permission
3311 * {@link android.Manifest.permission#ACCESS_NETWORK_STATE}.
3312 *
3313 * @param networkCallback The {@link NetworkCallback} that the system will call as the
3314 * system default network changes.
3315 * @param handler {@link Handler} to specify the thread upon which the callback will be invoked.
Hugo Benichi2583ef02017-02-02 17:02:36 +09003316 */
3317 public void registerDefaultNetworkCallback(NetworkCallback networkCallback, Handler handler) {
Erik Klinea2d29402016-03-16 15:31:39 +09003318 // This works because if the NetworkCapabilities are null,
3319 // ConnectivityService takes them from the default request.
3320 //
3321 // Since the capabilities are exactly the same as the default request's
3322 // capabilities, this request is guaranteed, at all times, to be
3323 // satisfied by the same network, if any, that satisfies the default
3324 // request, i.e., the system default network.
Hugo Benichi2583ef02017-02-02 17:02:36 +09003325 CallbackHandler cbHandler = new CallbackHandler(handler);
3326 sendRequestForNetwork(null, networkCallback, 0, REQUEST, TYPE_NONE, cbHandler);
Erik Klinea2d29402016-03-16 15:31:39 +09003327 }
3328
3329 /**
fengludb571472015-04-21 17:12:05 -07003330 * Requests bandwidth update for a given {@link Network} and returns whether the update request
3331 * is accepted by ConnectivityService. Once accepted, ConnectivityService will poll underlying
3332 * network connection for updated bandwidth information. The caller will be notified via
3333 * {@link ConnectivityManager.NetworkCallback} if there is an update. Notice that this
Lorenzo Colitti88bc0bb2016-04-13 22:00:02 +09003334 * method assumes that the caller has previously called
3335 * {@link #registerNetworkCallback(NetworkRequest, NetworkCallback)} to listen for network
3336 * changes.
fenglub15e72b2015-03-20 11:29:56 -07003337 *
fengluae519192015-04-27 14:28:04 -07003338 * @param network {@link Network} specifying which network you're interested.
fengludb571472015-04-21 17:12:05 -07003339 * @return {@code true} on success, {@code false} if the {@link Network} is no longer valid.
fenglub15e72b2015-03-20 11:29:56 -07003340 */
fengludb571472015-04-21 17:12:05 -07003341 public boolean requestBandwidthUpdate(Network network) {
fenglub15e72b2015-03-20 11:29:56 -07003342 try {
fengludb571472015-04-21 17:12:05 -07003343 return mService.requestBandwidthUpdate(network);
fenglub15e72b2015-03-20 11:29:56 -07003344 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07003345 throw e.rethrowFromSystemServer();
fenglub15e72b2015-03-20 11:29:56 -07003346 }
3347 }
3348
3349 /**
Robert Greenwalt6078b502014-06-11 16:05:07 -07003350 * Unregisters callbacks about and possibly releases networks originating from
Lorenzo Colitti88bc0bb2016-04-13 22:00:02 +09003351 * {@link #requestNetwork(NetworkRequest, NetworkCallback)} and
3352 * {@link #registerNetworkCallback(NetworkRequest, NetworkCallback)} calls.
3353 * If the given {@code NetworkCallback} had previously been used with
Lorenzo Colitti2ea89e52015-04-24 17:03:31 +09003354 * {@code #requestNetwork}, any networks that had been connected to only to satisfy that request
3355 * will be disconnected.
Robert Greenwalt9258c642014-03-26 16:47:06 -07003356 *
Robert Greenwalt6078b502014-06-11 16:05:07 -07003357 * @param networkCallback The {@link NetworkCallback} used when making the request.
Robert Greenwalt9258c642014-03-26 16:47:06 -07003358 */
Robert Greenwalt6078b502014-06-11 16:05:07 -07003359 public void unregisterNetworkCallback(NetworkCallback networkCallback) {
3360 if (networkCallback == null || networkCallback.networkRequest == null ||
3361 networkCallback.networkRequest.requestId == REQUEST_ID_UNSET) {
3362 throw new IllegalArgumentException("Invalid NetworkCallback");
3363 }
Robert Greenwalt9258c642014-03-26 16:47:06 -07003364 try {
Hugo Benichi39e10e82016-07-07 09:36:12 +09003365 // CallbackHandler will release callback when receiving CALLBACK_RELEASED.
Robert Greenwalt6078b502014-06-11 16:05:07 -07003366 mService.releaseNetworkRequest(networkCallback.networkRequest);
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07003367 } catch (RemoteException e) {
3368 throw e.rethrowFromSystemServer();
3369 }
Robert Greenwalt9258c642014-03-26 16:47:06 -07003370 }
Paul Jensen6d3ff9e2014-05-29 10:12:39 -04003371
3372 /**
Paul Jensenf2c1cfe2015-06-30 14:29:18 -04003373 * Unregisters a callback previously registered via
3374 * {@link #registerNetworkCallback(NetworkRequest, android.app.PendingIntent)}.
3375 *
3376 * @param operation A PendingIntent equal (as defined by {@link Intent#filterEquals}) to the
3377 * PendingIntent passed to
3378 * {@link #registerNetworkCallback(NetworkRequest, android.app.PendingIntent)}.
3379 * Cannot be null.
3380 */
3381 public void unregisterNetworkCallback(PendingIntent operation) {
3382 releaseNetworkRequest(operation);
3383 }
3384
3385 /**
Lorenzo Colittie03c3c72015-04-03 16:38:52 +09003386 * Informs the system whether it should switch to {@code network} regardless of whether it is
3387 * validated or not. If {@code accept} is true, and the network was explicitly selected by the
3388 * user (e.g., by selecting a Wi-Fi network in the Settings app), then the network will become
3389 * the system default network regardless of any other network that's currently connected. If
3390 * {@code always} is true, then the choice is remembered, so that the next time the user
3391 * connects to this network, the system will switch to it.
3392 *
3393 * <p>This method requires the caller to hold the permission
3394 * {@link android.Manifest.permission#CONNECTIVITY_INTERNAL}
3395 *
3396 * @param network The network to accept.
3397 * @param accept Whether to accept the network even if unvalidated.
3398 * @param always Whether to remember this choice in the future.
3399 *
3400 * @hide
3401 */
3402 public void setAcceptUnvalidated(Network network, boolean accept, boolean always) {
3403 try {
3404 mService.setAcceptUnvalidated(network, accept, always);
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07003405 } catch (RemoteException e) {
3406 throw e.rethrowFromSystemServer();
3407 }
Lorenzo Colittie03c3c72015-04-03 16:38:52 +09003408 }
3409
3410 /**
Lorenzo Colitti165c51c2016-09-19 01:00:19 +09003411 * Informs the system to penalize {@code network}'s score when it becomes unvalidated. This is
3412 * only meaningful if the system is configured not to penalize such networks, e.g., if the
3413 * {@code config_networkAvoidBadWifi} configuration variable is set to 0 and the {@code
3414 * NETWORK_AVOID_BAD_WIFI setting is unset}.
3415 *
3416 * <p>This method requires the caller to hold the permission
3417 * {@link android.Manifest.permission#CONNECTIVITY_INTERNAL}
3418 *
3419 * @param network The network to accept.
3420 *
3421 * @hide
3422 */
3423 public void setAvoidUnvalidated(Network network) {
3424 try {
3425 mService.setAvoidUnvalidated(network);
3426 } catch (RemoteException e) {
3427 throw e.rethrowFromSystemServer();
3428 }
3429 }
3430
3431 /**
Lorenzo Colitti2de49252017-01-24 18:08:41 +09003432 * It is acceptable to briefly use multipath data to provide seamless connectivity for
3433 * time-sensitive user-facing operations when the system default network is temporarily
3434 * unresponsive. The amount of data should be limited (less than one megabyte), and the
3435 * operation should be infrequent to ensure that data usage is limited.
3436 *
3437 * An example of such an operation might be a time-sensitive foreground activity, such as a
3438 * voice command, that the user is performing while walking out of range of a Wi-Fi network.
3439 */
3440 public static final int MULTIPATH_PREFERENCE_HANDOVER = 1 << 0;
3441
3442 /**
3443 * It is acceptable to use small amounts of multipath data on an ongoing basis to provide
3444 * a backup channel for traffic that is primarily going over another network.
3445 *
3446 * An example might be maintaining backup connections to peers or servers for the purpose of
3447 * fast fallback if the default network is temporarily unresponsive or disconnects. The traffic
3448 * on backup paths should be negligible compared to the traffic on the main path.
3449 */
3450 public static final int MULTIPATH_PREFERENCE_RELIABILITY = 1 << 1;
3451
3452 /**
3453 * It is acceptable to use metered data to improve network latency and performance.
3454 */
3455 public static final int MULTIPATH_PREFERENCE_PERFORMANCE = 1 << 2;
3456
3457 /**
3458 * Return value to use for unmetered networks. On such networks we currently set all the flags
3459 * to true.
3460 * @hide
3461 */
3462 public static final int MULTIPATH_PREFERENCE_UNMETERED =
3463 MULTIPATH_PREFERENCE_HANDOVER |
3464 MULTIPATH_PREFERENCE_RELIABILITY |
3465 MULTIPATH_PREFERENCE_PERFORMANCE;
3466
3467 /** @hide */
3468 @Retention(RetentionPolicy.SOURCE)
3469 @IntDef(flag = true, value = {
3470 MULTIPATH_PREFERENCE_HANDOVER,
3471 MULTIPATH_PREFERENCE_RELIABILITY,
3472 MULTIPATH_PREFERENCE_PERFORMANCE,
3473 })
3474 public @interface MultipathPreference {
3475 }
3476
3477 /**
3478 * Provides a hint to the calling application on whether it is desirable to use the
3479 * multinetwork APIs (e.g., {@link Network#openConnection}, {@link Network#bindSocket}, etc.)
3480 * for multipath data transfer on this network when it is not the system default network.
3481 * Applications desiring to use multipath network protocols should call this method before
3482 * each such operation.
3483 * <p>
3484 * This method requires the caller to hold the permission
3485 * {@link android.Manifest.permission#ACCESS_NETWORK_STATE}.
3486 *
3487 * @param network The network on which the application desires to use multipath data.
3488 * If {@code null}, this method will return the a preference that will generally
3489 * apply to metered networks.
3490 * @return a bitwise OR of zero or more of the {@code MULTIPATH_PREFERENCE_*} constants.
3491 */
3492 public @MultipathPreference int getMultipathPreference(Network network) {
3493 try {
3494 return mService.getMultipathPreference(network);
3495 } catch (RemoteException e) {
3496 throw e.rethrowFromSystemServer();
3497 }
3498 }
3499
3500 /**
Stuart Scott984dc852015-03-30 13:17:11 -07003501 * Resets all connectivity manager settings back to factory defaults.
3502 * @hide
3503 */
3504 public void factoryReset() {
Stuart Scott984dc852015-03-30 13:17:11 -07003505 try {
Stuart Scottf1fb3972015-04-02 18:00:02 -07003506 mService.factoryReset();
Stuart Scott984dc852015-03-30 13:17:11 -07003507 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07003508 throw e.rethrowFromSystemServer();
Stuart Scott984dc852015-03-30 13:17:11 -07003509 }
3510 }
3511
3512 /**
Paul Jensen6d3ff9e2014-05-29 10:12:39 -04003513 * Binds the current process to {@code network}. All Sockets created in the future
3514 * (and not explicitly bound via a bound SocketFactory from
3515 * {@link Network#getSocketFactory() Network.getSocketFactory()}) will be bound to
3516 * {@code network}. All host name resolutions will be limited to {@code network} as well.
3517 * Note that if {@code network} ever disconnects, all Sockets created in this way will cease to
3518 * work and all host name resolutions will fail. This is by design so an application doesn't
3519 * accidentally use Sockets it thinks are still bound to a particular {@link Network}.
3520 * To clear binding pass {@code null} for {@code network}. Using individually bound
3521 * Sockets created by Network.getSocketFactory().createSocket() and
3522 * performing network-specific host name resolutions via
3523 * {@link Network#getAllByName Network.getAllByName} is preferred to calling
Paul Jensen72db88e2015-03-10 10:54:12 -04003524 * {@code bindProcessToNetwork}.
Paul Jensen6d3ff9e2014-05-29 10:12:39 -04003525 *
3526 * @param network The {@link Network} to bind the current process to, or {@code null} to clear
3527 * the current binding.
3528 * @return {@code true} on success, {@code false} if the {@link Network} is no longer valid.
3529 */
Paul Jensen72db88e2015-03-10 10:54:12 -04003530 public boolean bindProcessToNetwork(Network network) {
3531 // Forcing callers to call thru non-static function ensures ConnectivityManager
3532 // instantiated.
3533 return setProcessDefaultNetwork(network);
3534 }
3535
3536 /**
3537 * Binds the current process to {@code network}. All Sockets created in the future
3538 * (and not explicitly bound via a bound SocketFactory from
3539 * {@link Network#getSocketFactory() Network.getSocketFactory()}) will be bound to
3540 * {@code network}. All host name resolutions will be limited to {@code network} as well.
3541 * Note that if {@code network} ever disconnects, all Sockets created in this way will cease to
3542 * work and all host name resolutions will fail. This is by design so an application doesn't
3543 * accidentally use Sockets it thinks are still bound to a particular {@link Network}.
3544 * To clear binding pass {@code null} for {@code network}. Using individually bound
3545 * Sockets created by Network.getSocketFactory().createSocket() and
3546 * performing network-specific host name resolutions via
3547 * {@link Network#getAllByName Network.getAllByName} is preferred to calling
3548 * {@code setProcessDefaultNetwork}.
3549 *
3550 * @param network The {@link Network} to bind the current process to, or {@code null} to clear
3551 * the current binding.
3552 * @return {@code true} on success, {@code false} if the {@link Network} is no longer valid.
3553 * @deprecated This function can throw {@link IllegalStateException}. Use
3554 * {@link #bindProcessToNetwork} instead. {@code bindProcessToNetwork}
3555 * is a direct replacement.
3556 */
Aurimas Liutikas514c5ef2016-05-24 15:22:55 -07003557 @Deprecated
Paul Jensen6d3ff9e2014-05-29 10:12:39 -04003558 public static boolean setProcessDefaultNetwork(Network network) {
Paul Jensenc91b5342014-08-27 12:38:45 -04003559 int netId = (network == null) ? NETID_UNSET : network.netId;
Paul Jensen72db88e2015-03-10 10:54:12 -04003560 if (netId == NetworkUtils.getBoundNetworkForProcess()) {
Paul Jensenc91b5342014-08-27 12:38:45 -04003561 return true;
3562 }
3563 if (NetworkUtils.bindProcessToNetwork(netId)) {
Paul Jensene0bef712014-12-10 15:12:18 -05003564 // Set HTTP proxy system properties to match network.
3565 // TODO: Deprecate this static method and replace it with a non-static version.
Lorenzo Colittiec4c5552015-04-22 11:52:48 +09003566 try {
3567 Proxy.setHttpProxySystemProperty(getInstance().getDefaultProxy());
3568 } catch (SecurityException e) {
3569 // The process doesn't have ACCESS_NETWORK_STATE, so we can't fetch the proxy.
3570 Log.e(TAG, "Can't set proxy properties", e);
3571 }
Paul Jensenc91b5342014-08-27 12:38:45 -04003572 // Must flush DNS cache as new network may have different DNS resolutions.
3573 InetAddress.clearDnsCache();
3574 // Must flush socket pool as idle sockets will be bound to previous network and may
3575 // cause subsequent fetches to be performed on old network.
3576 NetworkEventDispatcher.getInstance().onNetworkConfigurationChanged();
3577 return true;
3578 } else {
3579 return false;
3580 }
Paul Jensen6d3ff9e2014-05-29 10:12:39 -04003581 }
3582
3583 /**
3584 * Returns the {@link Network} currently bound to this process via
Paul Jensen72db88e2015-03-10 10:54:12 -04003585 * {@link #bindProcessToNetwork}, or {@code null} if no {@link Network} is explicitly bound.
Paul Jensen6d3ff9e2014-05-29 10:12:39 -04003586 *
3587 * @return {@code Network} to which this process is bound, or {@code null}.
3588 */
Paul Jensen72db88e2015-03-10 10:54:12 -04003589 public Network getBoundNetworkForProcess() {
3590 // Forcing callers to call thru non-static function ensures ConnectivityManager
3591 // instantiated.
3592 return getProcessDefaultNetwork();
3593 }
3594
3595 /**
3596 * Returns the {@link Network} currently bound to this process via
3597 * {@link #bindProcessToNetwork}, or {@code null} if no {@link Network} is explicitly bound.
3598 *
3599 * @return {@code Network} to which this process is bound, or {@code null}.
3600 * @deprecated Using this function can lead to other functions throwing
3601 * {@link IllegalStateException}. Use {@link #getBoundNetworkForProcess} instead.
3602 * {@code getBoundNetworkForProcess} is a direct replacement.
3603 */
Aurimas Liutikas514c5ef2016-05-24 15:22:55 -07003604 @Deprecated
Paul Jensen6d3ff9e2014-05-29 10:12:39 -04003605 public static Network getProcessDefaultNetwork() {
Paul Jensen72db88e2015-03-10 10:54:12 -04003606 int netId = NetworkUtils.getBoundNetworkForProcess();
Paul Jensenbcc76d32014-07-11 08:17:29 -04003607 if (netId == NETID_UNSET) return null;
Paul Jensen6d3ff9e2014-05-29 10:12:39 -04003608 return new Network(netId);
3609 }
3610
Lorenzo Colittiffc42b02015-07-29 11:41:21 +09003611 private void unsupportedStartingFrom(int version) {
3612 if (Process.myUid() == Process.SYSTEM_UID) {
3613 // The getApplicationInfo() call we make below is not supported in system context, and
3614 // we want to allow the system to use these APIs anyway.
3615 return;
3616 }
3617
3618 if (mContext.getApplicationInfo().targetSdkVersion >= version) {
3619 throw new UnsupportedOperationException(
3620 "This method is not supported in target SDK version " + version + " and above");
3621 }
3622 }
3623
3624 // Checks whether the calling app can use the legacy routing API (startUsingNetworkFeature,
3625 // stopUsingNetworkFeature, requestRouteToHost), and if not throw UnsupportedOperationException.
Lifu Tang30f95a72016-01-07 23:20:38 -08003626 // TODO: convert the existing system users (Tethering, GnssLocationProvider) to the new APIs and
Lorenzo Colittiffc42b02015-07-29 11:41:21 +09003627 // remove these exemptions. Note that this check is not secure, and apps can still access these
3628 // functions by accessing ConnectivityService directly. However, it should be clear that doing
3629 // so is unsupported and may break in the future. http://b/22728205
3630 private void checkLegacyRoutingApiAccess() {
3631 if (mContext.checkCallingOrSelfPermission("com.android.permission.INJECT_OMADM_SETTINGS")
3632 == PackageManager.PERMISSION_GRANTED) {
3633 return;
3634 }
3635
Dianne Hackborn692a2442015-07-31 10:35:34 -07003636 unsupportedStartingFrom(VERSION_CODES.M);
Lorenzo Colittiffc42b02015-07-29 11:41:21 +09003637 }
3638
Paul Jensen6d3ff9e2014-05-29 10:12:39 -04003639 /**
3640 * Binds host resolutions performed by this process to {@code network}.
Paul Jensen72db88e2015-03-10 10:54:12 -04003641 * {@link #bindProcessToNetwork} takes precedence over this setting.
Paul Jensen6d3ff9e2014-05-29 10:12:39 -04003642 *
3643 * @param network The {@link Network} to bind host resolutions from the current process to, or
3644 * {@code null} to clear the current binding.
3645 * @return {@code true} on success, {@code false} if the {@link Network} is no longer valid.
3646 * @hide
3647 * @deprecated This is strictly for legacy usage to support {@link #startUsingNetworkFeature}.
3648 */
Aurimas Liutikas514c5ef2016-05-24 15:22:55 -07003649 @Deprecated
Paul Jensen6d3ff9e2014-05-29 10:12:39 -04003650 public static boolean setProcessDefaultNetworkForHostResolution(Network network) {
Paul Jensenbcc76d32014-07-11 08:17:29 -04003651 return NetworkUtils.bindProcessToNetworkForHostResolution(
3652 network == null ? NETID_UNSET : network.netId);
Paul Jensen6d3ff9e2014-05-29 10:12:39 -04003653 }
Felipe Leme1b103232016-01-22 09:44:57 -08003654
3655 /**
3656 * Device is not restricting metered network activity while application is running on
3657 * background.
3658 */
3659 public static final int RESTRICT_BACKGROUND_STATUS_DISABLED = 1;
3660
3661 /**
3662 * Device is restricting metered network activity while application is running on background,
3663 * but application is allowed to bypass it.
3664 * <p>
3665 * In this state, application should take action to mitigate metered network access.
3666 * For example, a music streaming application should switch to a low-bandwidth bitrate.
3667 */
3668 public static final int RESTRICT_BACKGROUND_STATUS_WHITELISTED = 2;
3669
3670 /**
3671 * Device is restricting metered network activity while application is running on background.
Felipe Leme9778f762016-01-27 14:46:39 -08003672 * <p>
Felipe Leme1b103232016-01-22 09:44:57 -08003673 * In this state, application should not try to use the network while running on background,
3674 * because it would be denied.
3675 */
3676 public static final int RESTRICT_BACKGROUND_STATUS_ENABLED = 3;
3677
Felipe Leme9778f762016-01-27 14:46:39 -08003678 /**
3679 * A change in the background metered network activity restriction has occurred.
3680 * <p>
3681 * Applications should call {@link #getRestrictBackgroundStatus()} to check if the restriction
3682 * applies to them.
3683 * <p>
3684 * This is only sent to registered receivers, not manifest receivers.
3685 */
3686 @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION)
3687 public static final String ACTION_RESTRICT_BACKGROUND_CHANGED =
3688 "android.net.conn.RESTRICT_BACKGROUND_CHANGED";
3689
Felipe Lemeecfccea2016-01-25 11:48:04 -08003690 /** @hide */
3691 @Retention(RetentionPolicy.SOURCE)
Felipe Leme1b103232016-01-22 09:44:57 -08003692 @IntDef(flag = false, value = {
3693 RESTRICT_BACKGROUND_STATUS_DISABLED,
3694 RESTRICT_BACKGROUND_STATUS_WHITELISTED,
3695 RESTRICT_BACKGROUND_STATUS_ENABLED,
3696 })
Felipe Leme1b103232016-01-22 09:44:57 -08003697 public @interface RestrictBackgroundStatus {
3698 }
3699
3700 private INetworkPolicyManager getNetworkPolicyManager() {
3701 synchronized (this) {
3702 if (mNPManager != null) {
3703 return mNPManager;
3704 }
3705 mNPManager = INetworkPolicyManager.Stub.asInterface(ServiceManager
3706 .getService(Context.NETWORK_POLICY_SERVICE));
3707 return mNPManager;
3708 }
3709 }
3710
3711 /**
3712 * Determines if the calling application is subject to metered network restrictions while
3713 * running on background.
Felipe Lemec9c7be52016-05-16 13:57:19 -07003714 *
3715 * @return {@link #RESTRICT_BACKGROUND_STATUS_DISABLED},
3716 * {@link #RESTRICT_BACKGROUND_STATUS_ENABLED},
3717 * or {@link #RESTRICT_BACKGROUND_STATUS_WHITELISTED}
Felipe Leme1b103232016-01-22 09:44:57 -08003718 */
3719 public @RestrictBackgroundStatus int getRestrictBackgroundStatus() {
3720 try {
3721 return getNetworkPolicyManager().getRestrictBackgroundByCaller();
3722 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07003723 throw e.rethrowFromSystemServer();
Felipe Leme1b103232016-01-22 09:44:57 -08003724 }
3725 }
Andreas Gampe34802132016-04-20 14:33:51 -07003726
3727 /**
3728 * A holder class for debug info (mapping CALLBACK values to field names). This is stored
3729 * in a holder for two reasons:
3730 * 1) The reflection necessary to establish the map can't be run at compile-time. Thus, this
3731 * code will make the enclosing class not compile-time initializeable, deferring its
3732 * initialization to zygote startup. This leads to dirty (but shared) memory.
3733 * As this is debug info, use a holder that isn't initialized by default. This way the map
3734 * will be created on demand, while ConnectivityManager can be compile-time initialized.
3735 * 2) Static initialization is still preferred for its strong thread safety guarantees without
3736 * requiring a lock.
3737 */
3738 private static class NoPreloadHolder {
3739 public static final SparseArray<String> sMagicDecoderRing = MessageUtils.findMessageNames(
3740 new Class[]{ConnectivityManager.class}, new String[]{"CALLBACK_"});
3741 }
3742
3743 static {
3744 // When debug is enabled, aggressively initialize the holder by touching the field (which
3745 // will guarantee static initialization).
3746 if (CallbackHandler.DBG) {
3747 Object dummy = NoPreloadHolder.sMagicDecoderRing;
3748 }
3749 }
3750
3751 private static final String whatToString(int what) {
3752 return NoPreloadHolder.sMagicDecoderRing.get(what, Integer.toString(what));
3753 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003754}