blob: d7ecc81ffdba1f13381803c15e19b551d98a68db [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
Felipe Leme1b103232016-01-22 09:44:57 -080018import android.annotation.IntDef;
Robin Lee244ce8e2016-01-05 18:03:46 +000019import android.annotation.Nullable;
Jeff Sharkey30e06bb2017-04-24 11:18:03 -060020import android.annotation.RequiresPermission;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080021import android.annotation.SdkConstant;
22import android.annotation.SdkConstant.SdkConstantType;
Udam Sainib7c24872016-01-04 12:16:14 -080023import android.annotation.SystemApi;
Jeff Sharkeyd86b8fe2017-06-02 17:36:26 -060024import android.annotation.SystemService;
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;
Hugo Benichicb883232017-05-11 13:16:17 +090044import android.os.ServiceSpecificException;
Jeff Sharkey961e3042011-08-29 16:02:57 -070045import android.provider.Settings;
Wink Saville36ffb042014-12-05 11:10:30 -080046import android.telephony.SubscriptionManager;
Dianne Hackborn77b987f2014-02-26 16:20:52 -080047import android.util.ArrayMap;
Robert Greenwalt9258c642014-03-26 16:47:06 -070048import android.util.Log;
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;
Hugo Benichidafed3d2017-03-06 09:17:06 +090053import com.android.internal.util.Preconditions;
54import com.android.internal.util.Protocol;
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;
Hugo Benichidafed3d2017-03-06 09:17:06 +090061import java.util.ArrayList;
Jeremy Kleind42209d2015-12-28 15:11:58 -080062import java.util.HashMap;
Hugo Benichidafed3d2017-03-06 09:17:06 +090063import java.util.List;
64import java.util.Map;
Jeremy Kleind42209d2015-12-28 15:11:58 -080065
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080066/**
67 * Class that answers queries about the state of network connectivity. It also
Jeff Sharkeyd86b8fe2017-06-02 17:36:26 -060068 * notifies applications when network connectivity changes.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080069 * <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 Sharkeyd86b8fe2017-06-02 17:36:26 -060082@SystemService(Context.CONNECTIVITY_SERVICE)
Jeff Sharkeyc006f1a2011-05-19 17:12:49 -070083public class ConnectivityManager {
84 private static final String TAG = "ConnectivityManager";
85
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080086 /**
Robert Greenwaltd19c41c2014-05-18 23:07:25 -070087 * A change in network connectivity has occurred. A default connection has either
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080088 * been established or lost. The NetworkInfo for the affected network is
89 * sent as an extra; it should be consulted to see what kind of
90 * connectivity event occurred.
91 * <p/>
Mark Lu33ec1062016-12-05 10:57:55 -080092 * Apps targeting Android 7.0 (API level 24) and higher do not receive this
93 * broadcast if they declare the broadcast receiver in their manifest. Apps
94 * will still receive broadcasts if they register their
95 * {@link android.content.BroadcastReceiver} with
96 * {@link android.content.Context#registerReceiver Context.registerReceiver()}
97 * and that context is still valid.
98 * <p/>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080099 * If this is a connection that was the result of failing over from a
100 * disconnected network, then the FAILOVER_CONNECTION boolean extra is
101 * set to true.
102 * <p/>
103 * For a loss of connectivity, if the connectivity manager is attempting
104 * to connect (or has already connected) to another network, the
105 * NetworkInfo for the new network is also passed as an extra. This lets
106 * any receivers of the broadcast know that they should not necessarily
107 * tell the user that no data traffic will be possible. Instead, the
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800108 * receiver should expect another broadcast soon, indicating either that
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800109 * the failover attempt succeeded (and so there is still overall data
110 * connectivity), or that the failover attempt failed, meaning that all
111 * connectivity has been lost.
112 * <p/>
113 * For a disconnect event, the boolean extra EXTRA_NO_CONNECTIVITY
114 * is set to {@code true} if there are no connected networks at all.
115 */
Jeff Sharkey4fa63b22013-02-20 18:21:19 -0800116 @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800117 public static final String CONNECTIVITY_ACTION = "android.net.conn.CONNECTIVITY_CHANGE";
Jeff Sharkeyf0ceede2011-08-02 17:22:34 -0700118
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800119 /**
Robert Greenwalte94a6ff2015-09-01 08:23:04 -0700120 * A temporary hack until SUPL system can get off the legacy APIS.
121 * They do too many network requests and the long list of apps listening
122 * and waking due to the CONNECTIVITY_ACTION bcast makes it expensive.
123 * Use this bcast intent instead for SUPL requests.
124 * @hide
125 */
126 public static final String CONNECTIVITY_ACTION_SUPL =
127 "android.net.conn.CONNECTIVITY_CHANGE_SUPL";
128
129 /**
Paul Jensen25a217c2015-02-27 22:55:47 -0500130 * The device has connected to a network that has presented a captive
131 * portal, which is blocking Internet connectivity. The user was presented
132 * with a notification that network sign in is required,
133 * and the user invoked the notification's action indicating they
Paul Jensen49e3edf2015-05-22 10:50:39 -0400134 * desire to sign in to the network. Apps handling this activity should
Paul Jensen25a217c2015-02-27 22:55:47 -0500135 * facilitate signing in to the network. This action includes a
136 * {@link Network} typed extra called {@link #EXTRA_NETWORK} that represents
137 * the network presenting the captive portal; all communication with the
138 * captive portal must be done using this {@code Network} object.
139 * <p/>
Paul Jensen49e3edf2015-05-22 10:50:39 -0400140 * This activity includes a {@link CaptivePortal} extra named
141 * {@link #EXTRA_CAPTIVE_PORTAL} that can be used to indicate different
142 * outcomes of the captive portal sign in to the system:
143 * <ul>
144 * <li> When the app handling this action believes the user has signed in to
145 * the network and the captive portal has been dismissed, the app should
146 * call {@link CaptivePortal#reportCaptivePortalDismissed} so the system can
147 * reevaluate the network. If reevaluation finds the network no longer
148 * subject to a captive portal, the network may become the default active
149 * data network. </li>
150 * <li> When the app handling this action believes the user explicitly wants
Paul Jensen25a217c2015-02-27 22:55:47 -0500151 * to ignore the captive portal and the network, the app should call
Paul Jensen49e3edf2015-05-22 10:50:39 -0400152 * {@link CaptivePortal#ignoreNetwork}. </li>
153 * </ul>
Paul Jensen25a217c2015-02-27 22:55:47 -0500154 */
155 @SdkConstant(SdkConstantType.ACTIVITY_INTENT_ACTION)
156 public static final String ACTION_CAPTIVE_PORTAL_SIGN_IN = "android.net.conn.CAPTIVE_PORTAL";
157
158 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800159 * The lookup key for a {@link NetworkInfo} object. Retrieve with
160 * {@link android.content.Intent#getParcelableExtra(String)}.
Jeff Sharkeyf0ceede2011-08-02 17:22:34 -0700161 *
162 * @deprecated Since {@link NetworkInfo} can vary based on UID, applications
163 * should always obtain network information through
Paul Jensen3541e9f2015-03-18 12:23:02 -0400164 * {@link #getActiveNetworkInfo()}.
Jeff Sharkey75fbb4b2012-08-06 11:41:50 -0700165 * @see #EXTRA_NETWORK_TYPE
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800166 */
Jeff Sharkeyf0ceede2011-08-02 17:22:34 -0700167 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800168 public static final String EXTRA_NETWORK_INFO = "networkInfo";
Jeff Sharkeyf0ceede2011-08-02 17:22:34 -0700169
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800170 /**
Jeff Sharkey75fbb4b2012-08-06 11:41:50 -0700171 * Network type which triggered a {@link #CONNECTIVITY_ACTION} broadcast.
Jeff Sharkey75fbb4b2012-08-06 11:41:50 -0700172 *
173 * @see android.content.Intent#getIntExtra(String, int)
174 */
175 public static final String EXTRA_NETWORK_TYPE = "networkType";
176
177 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800178 * The lookup key for a boolean that indicates whether a connect event
179 * is for a network to which the connectivity manager was failing over
180 * following a disconnect on another network.
181 * Retrieve it with {@link android.content.Intent#getBooleanExtra(String,boolean)}.
182 */
183 public static final String EXTRA_IS_FAILOVER = "isFailover";
184 /**
185 * The lookup key for a {@link NetworkInfo} object. This is supplied when
186 * there is another network that it may be possible to connect to. Retrieve with
187 * {@link android.content.Intent#getParcelableExtra(String)}.
188 */
189 public static final String EXTRA_OTHER_NETWORK_INFO = "otherNetwork";
190 /**
191 * The lookup key for a boolean that indicates whether there is a
192 * complete lack of connectivity, i.e., no network is available.
193 * Retrieve it with {@link android.content.Intent#getBooleanExtra(String,boolean)}.
194 */
195 public static final String EXTRA_NO_CONNECTIVITY = "noConnectivity";
196 /**
197 * The lookup key for a string that indicates why an attempt to connect
198 * to a network failed. The string has no particular structure. It is
199 * intended to be used in notifications presented to users. Retrieve
200 * it with {@link android.content.Intent#getStringExtra(String)}.
201 */
202 public static final String EXTRA_REASON = "reason";
203 /**
204 * The lookup key for a string that provides optionally supplied
205 * extra information about the network state. The information
206 * may be passed up from the lower networking layers, and its
207 * meaning may be specific to a particular network type. Retrieve
208 * it with {@link android.content.Intent#getStringExtra(String)}.
209 */
210 public static final String EXTRA_EXTRA_INFO = "extraInfo";
Robert Greenwaltd7085fc2010-09-08 15:24:47 -0700211 /**
212 * The lookup key for an int that provides information about
213 * our connection to the internet at large. 0 indicates no connection,
214 * 100 indicates a great connection. Retrieve it with
Jeff Sharkeyc006f1a2011-05-19 17:12:49 -0700215 * {@link android.content.Intent#getIntExtra(String, int)}.
Robert Greenwaltd7085fc2010-09-08 15:24:47 -0700216 * {@hide}
217 */
218 public static final String EXTRA_INET_CONDITION = "inetCondition";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800219 /**
Paul Jensen49e3edf2015-05-22 10:50:39 -0400220 * The lookup key for a {@link CaptivePortal} object included with the
221 * {@link #ACTION_CAPTIVE_PORTAL_SIGN_IN} intent. The {@code CaptivePortal}
222 * object can be used to either indicate to the system that the captive
223 * portal has been dismissed or that the user does not want to pursue
224 * signing in to captive portal. Retrieve it with
225 * {@link android.content.Intent#getParcelableExtra(String)}.
Paul Jensen25a217c2015-02-27 22:55:47 -0500226 */
Paul Jensen49e3edf2015-05-22 10:50:39 -0400227 public static final String EXTRA_CAPTIVE_PORTAL = "android.net.extra.CAPTIVE_PORTAL";
Jan Nordqvist52eb29f2015-09-22 15:54:32 -0700228
229 /**
230 * Key for passing a URL to the captive portal login activity.
231 */
232 public static final String EXTRA_CAPTIVE_PORTAL_URL = "android.net.extra.CAPTIVE_PORTAL_URL";
233
Paul Jensen25a217c2015-02-27 22:55:47 -0500234 /**
Hugo Benichicdf3ba42016-12-14 08:23:40 +0900235 * Key for passing a user agent string to the captive portal login activity.
236 * {@hide}
237 */
238 public static final String EXTRA_CAPTIVE_PORTAL_USER_AGENT =
239 "android.net.extra.CAPTIVE_PORTAL_USER_AGENT";
240
241 /**
Haoyu Baidb3c8672012-06-20 14:29:57 -0700242 * Broadcast action to indicate the change of data activity status
243 * (idle or active) on a network in a recent period.
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800244 * The network becomes active when data transmission is started, or
245 * idle if there is no data transmission for a period of time.
Haoyu Baidb3c8672012-06-20 14:29:57 -0700246 * {@hide}
247 */
248 @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION)
249 public static final String ACTION_DATA_ACTIVITY_CHANGE = "android.net.conn.DATA_ACTIVITY_CHANGE";
250 /**
251 * The lookup key for an enum that indicates the network device type on which this data activity
252 * change happens.
253 * {@hide}
254 */
255 public static final String EXTRA_DEVICE_TYPE = "deviceType";
256 /**
257 * The lookup key for a boolean that indicates the device is active or not. {@code true} means
258 * it is actively sending or receiving data and {@code false} means it is idle.
259 * {@hide}
260 */
261 public static final String EXTRA_IS_ACTIVE = "isActive";
Ashish Sharma0535a9f2014-03-12 18:42:23 -0700262 /**
263 * The lookup key for a long that contains the timestamp (nanos) of the radio state change.
264 * {@hide}
265 */
266 public static final String EXTRA_REALTIME_NS = "tsNanos";
Haoyu Baidb3c8672012-06-20 14:29:57 -0700267
268 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800269 * Broadcast Action: The setting for background data usage has changed
270 * values. Use {@link #getBackgroundDataSetting()} to get the current value.
271 * <p>
272 * If an application uses the network in the background, it should listen
273 * for this broadcast and stop using the background data if the value is
Jeff Sharkeyc006f1a2011-05-19 17:12:49 -0700274 * {@code false}.
Jeff Sharkey54ee2ad2012-01-30 16:29:24 -0800275 * <p>
276 *
277 * @deprecated As of {@link VERSION_CODES#ICE_CREAM_SANDWICH}, availability
278 * of background data depends on several combined factors, and
279 * this broadcast is no longer sent. Instead, when background
280 * data is unavailable, {@link #getActiveNetworkInfo()} will now
281 * appear disconnected. During first boot after a platform
282 * upgrade, this broadcast will be sent once if
283 * {@link #getBackgroundDataSetting()} was {@code false} before
284 * the upgrade.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800285 */
286 @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION)
Jeff Sharkey54ee2ad2012-01-30 16:29:24 -0800287 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800288 public static final String ACTION_BACKGROUND_DATA_SETTING_CHANGED =
289 "android.net.conn.BACKGROUND_DATA_SETTING_CHANGED";
290
Robert Greenwalt1e9aac22010-09-15 17:36:33 -0700291 /**
292 * Broadcast Action: The network connection may not be good
293 * uses {@code ConnectivityManager.EXTRA_INET_CONDITION} and
294 * {@code ConnectivityManager.EXTRA_NETWORK_INFO} to specify
295 * the network and it's condition.
296 * @hide
297 */
Jeff Sharkey4fa63b22013-02-20 18:21:19 -0800298 @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION)
Robert Greenwalt1e9aac22010-09-15 17:36:33 -0700299 public static final String INET_CONDITION_ACTION =
300 "android.net.conn.INET_CONDITION_ACTION";
301
Robert Greenwalt42acef32009-08-12 16:08:25 -0700302 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800303 * Broadcast Action: A tetherable connection has come or gone.
304 * Uses {@code ConnectivityManager.EXTRA_AVAILABLE_TETHER},
Erik Kline8351faa2017-04-17 16:47:23 +0900305 * {@code ConnectivityManager.EXTRA_ACTIVE_LOCAL_ONLY},
306 * {@code ConnectivityManager.EXTRA_ACTIVE_TETHER}, and
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800307 * {@code ConnectivityManager.EXTRA_ERRORED_TETHER} to indicate
308 * the current state of tethering. Each include a list of
309 * interface names in that state (may be empty).
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -0800310 * @hide
311 */
Jeff Sharkey4fa63b22013-02-20 18:21:19 -0800312 @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION)
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -0800313 public static final String ACTION_TETHER_STATE_CHANGED =
314 "android.net.conn.TETHER_STATE_CHANGED";
315
316 /**
317 * @hide
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800318 * gives a String[] listing all the interfaces configured for
319 * tethering and currently available for tethering.
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -0800320 */
Robert Greenwalt2a091d72010-02-11 18:18:40 -0800321 public static final String EXTRA_AVAILABLE_TETHER = "availableArray";
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -0800322
323 /**
324 * @hide
Erik Kline8351faa2017-04-17 16:47:23 +0900325 * gives a String[] listing all the interfaces currently in local-only
326 * mode (ie, has DHCPv4+IPv6-ULA support and no packet forwarding)
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -0800327 */
Erik Kline8351faa2017-04-17 16:47:23 +0900328 public static final String EXTRA_ACTIVE_LOCAL_ONLY = "localOnlyArray";
329
330 /**
331 * @hide
332 * gives a String[] listing all the interfaces currently tethered
333 * (ie, has DHCPv4 support and packets potentially forwarded/NATed)
334 */
335 public static final String EXTRA_ACTIVE_TETHER = "tetherArray";
Robert Greenwalt2a091d72010-02-11 18:18:40 -0800336
337 /**
338 * @hide
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800339 * gives a String[] listing all the interfaces we tried to tether and
340 * failed. Use {@link #getLastTetherError} to find the error code
341 * for any interfaces listed here.
Robert Greenwalt2a091d72010-02-11 18:18:40 -0800342 */
343 public static final String EXTRA_ERRORED_TETHER = "erroredArray";
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -0800344
345 /**
Russell Brenner108da0c2013-02-12 10:03:14 -0800346 * Broadcast Action: The captive portal tracker has finished its test.
347 * Sent only while running Setup Wizard, in lieu of showing a user
348 * notification.
349 * @hide
350 */
Jeff Sharkey4fa63b22013-02-20 18:21:19 -0800351 @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION)
Russell Brenner108da0c2013-02-12 10:03:14 -0800352 public static final String ACTION_CAPTIVE_PORTAL_TEST_COMPLETED =
353 "android.net.conn.CAPTIVE_PORTAL_TEST_COMPLETED";
354 /**
355 * The lookup key for a boolean that indicates whether a captive portal was detected.
356 * Retrieve it with {@link android.content.Intent#getBooleanExtra(String,boolean)}.
357 * @hide
358 */
359 public static final String EXTRA_IS_CAPTIVE_PORTAL = "captivePortal";
360
361 /**
Lorenzo Colittie03c3c72015-04-03 16:38:52 +0900362 * Action used to display a dialog that asks the user whether to connect to a network that is
363 * not validated. This intent is used to start the dialog in settings via startActivity.
364 *
365 * @hide
366 */
367 public static final String ACTION_PROMPT_UNVALIDATED = "android.net.conn.PROMPT_UNVALIDATED";
368
369 /**
Lorenzo Colitti9be58c52016-09-15 14:02:29 +0900370 * Action used to display a dialog that asks the user whether to avoid a network that is no
371 * longer validated. This intent is used to start the dialog in settings via startActivity.
372 *
373 * @hide
374 */
375 public static final String ACTION_PROMPT_LOST_VALIDATION =
376 "android.net.conn.PROMPT_LOST_VALIDATION";
377
378 /**
Jeremy Klein36c7aa02016-01-22 14:11:45 -0800379 * Invalid tethering type.
380 * @see #startTethering(int, OnStartTetheringCallback, boolean)
381 * @hide
382 */
383 public static final int TETHERING_INVALID = -1;
384
385 /**
386 * Wifi tethering type.
387 * @see #startTethering(int, OnStartTetheringCallback, boolean)
388 * @hide
389 */
390 @SystemApi
391 public static final int TETHERING_WIFI = 0;
392
393 /**
394 * USB tethering type.
395 * @see #startTethering(int, OnStartTetheringCallback, boolean)
396 * @hide
397 */
398 @SystemApi
399 public static final int TETHERING_USB = 1;
400
401 /**
402 * Bluetooth tethering type.
403 * @see #startTethering(int, OnStartTetheringCallback, boolean)
404 * @hide
405 */
406 @SystemApi
407 public static final int TETHERING_BLUETOOTH = 2;
408
409 /**
410 * Extra used for communicating with the TetherService. Includes the type of tethering to
411 * enable if any.
412 * @hide
413 */
414 public static final String EXTRA_ADD_TETHER_TYPE = "extraAddTetherType";
415
416 /**
417 * Extra used for communicating with the TetherService. Includes the type of tethering for
418 * which to cancel provisioning.
419 * @hide
420 */
421 public static final String EXTRA_REM_TETHER_TYPE = "extraRemTetherType";
422
423 /**
424 * Extra used for communicating with the TetherService. True to schedule a recheck of tether
425 * provisioning.
426 * @hide
427 */
428 public static final String EXTRA_SET_ALARM = "extraSetAlarm";
429
430 /**
431 * Tells the TetherService to run a provision check now.
432 * @hide
433 */
434 public static final String EXTRA_RUN_PROVISION = "extraRunProvision";
435
436 /**
437 * Extra used for communicating with the TetherService. Contains the {@link ResultReceiver}
438 * which will receive provisioning results. Can be left empty.
439 * @hide
440 */
441 public static final String EXTRA_PROVISION_CALLBACK = "extraProvisionCallback";
442
443 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800444 * The absence of a connection type.
Robert Greenwaltccf83af12011-06-02 17:30:47 -0700445 * @hide
446 */
447 public static final int TYPE_NONE = -1;
448
449 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800450 * The Mobile data connection. When active, all data traffic
451 * will use this network type's interface by default
452 * (it has a default route)
Robert Greenwalt42acef32009-08-12 16:08:25 -0700453 */
454 public static final int TYPE_MOBILE = 0;
455 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800456 * The WIFI data connection. When active, all data traffic
457 * will use this network type's interface by default
458 * (it has a default route).
Robert Greenwalt42acef32009-08-12 16:08:25 -0700459 */
460 public static final int TYPE_WIFI = 1;
461 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800462 * An MMS-specific Mobile data connection. This network type may use the
463 * same network interface as {@link #TYPE_MOBILE} or it may use a different
464 * one. This is used by applications needing to talk to the carrier's
465 * Multimedia Messaging Service servers.
Lorenzo Colittie285b432015-04-23 15:32:42 +0900466 *
Lorenzo Colitti2ea89e52015-04-24 17:03:31 +0900467 * @deprecated Applications should instead use
468 * {@link #requestNetwork(NetworkRequest, NetworkCallback)} to request a network that
Lorenzo Colittie285b432015-04-23 15:32:42 +0900469 * provides the {@link NetworkCapabilities#NET_CAPABILITY_MMS} capability.
Robert Greenwalt42acef32009-08-12 16:08:25 -0700470 */
Aurimas Liutikas514c5ef2016-05-24 15:22:55 -0700471 @Deprecated
Robert Greenwalt42acef32009-08-12 16:08:25 -0700472 public static final int TYPE_MOBILE_MMS = 2;
473 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800474 * A SUPL-specific Mobile data connection. This network type may use the
475 * same network interface as {@link #TYPE_MOBILE} or it may use a different
476 * one. This is used by applications needing to talk to the carrier's
477 * Secure User Plane Location servers for help locating the device.
Lorenzo Colittie285b432015-04-23 15:32:42 +0900478 *
Lorenzo Colitti2ea89e52015-04-24 17:03:31 +0900479 * @deprecated Applications should instead use
480 * {@link #requestNetwork(NetworkRequest, NetworkCallback)} to request a network that
Lorenzo Colittie285b432015-04-23 15:32:42 +0900481 * provides the {@link NetworkCapabilities#NET_CAPABILITY_SUPL} capability.
Robert Greenwalt42acef32009-08-12 16:08:25 -0700482 */
Aurimas Liutikas514c5ef2016-05-24 15:22:55 -0700483 @Deprecated
Robert Greenwalt42acef32009-08-12 16:08:25 -0700484 public static final int TYPE_MOBILE_SUPL = 3;
485 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800486 * A DUN-specific Mobile data connection. This network type may use the
487 * same network interface as {@link #TYPE_MOBILE} or it may use a different
488 * one. This is sometimes by the system when setting up an upstream connection
489 * for tethering so that the carrier is aware of DUN traffic.
Robert Greenwalt42acef32009-08-12 16:08:25 -0700490 */
491 public static final int TYPE_MOBILE_DUN = 4;
492 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800493 * A High Priority Mobile data connection. This network type uses the
494 * same network interface as {@link #TYPE_MOBILE} but the routing setup
Lorenzo Colittie285b432015-04-23 15:32:42 +0900495 * is different.
496 *
Lorenzo Colitti2ea89e52015-04-24 17:03:31 +0900497 * @deprecated Applications should instead use
498 * {@link #requestNetwork(NetworkRequest, NetworkCallback)} to request a network that
Lorenzo Colittie285b432015-04-23 15:32:42 +0900499 * uses the {@link NetworkCapabilities#TRANSPORT_CELLULAR} transport.
Robert Greenwalt42acef32009-08-12 16:08:25 -0700500 */
Aurimas Liutikas514c5ef2016-05-24 15:22:55 -0700501 @Deprecated
Robert Greenwalt42acef32009-08-12 16:08:25 -0700502 public static final int TYPE_MOBILE_HIPRI = 5;
jsh8214deb2010-03-11 15:04:43 -0800503 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800504 * The WiMAX data connection. When active, all data traffic
505 * will use this network type's interface by default
506 * (it has a default route).
jsh8214deb2010-03-11 15:04:43 -0800507 */
508 public static final int TYPE_WIMAX = 6;
Robert Greenwaltda3d5e62010-12-06 13:56:24 -0800509
Jaikumar Ganesh15c74392010-12-21 22:31:44 -0800510 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800511 * The Bluetooth data connection. When active, all data traffic
512 * will use this network type's interface by default
513 * (it has a default route).
Jaikumar Ganesh15c74392010-12-21 22:31:44 -0800514 */
515 public static final int TYPE_BLUETOOTH = 7;
516
Robert Greenwalt60810842011-04-22 15:28:18 -0700517 /**
518 * Dummy data connection. This should not be used on shipping devices.
519 */
Jaikumar Ganesh15c74392010-12-21 22:31:44 -0800520 public static final int TYPE_DUMMY = 8;
Wink Saville9d7d6282011-03-12 14:52:01 -0800521
Robert Greenwalt60810842011-04-22 15:28:18 -0700522 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800523 * The Ethernet data connection. When active, all data traffic
524 * will use this network type's interface by default
525 * (it has a default route).
Robert Greenwalt60810842011-04-22 15:28:18 -0700526 */
Robert Greenwalte12aec92011-01-28 14:48:37 -0800527 public static final int TYPE_ETHERNET = 9;
Robert Greenwalt60810842011-04-22 15:28:18 -0700528
Wink Saville9d7d6282011-03-12 14:52:01 -0800529 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800530 * Over the air Administration.
Wink Saville9d7d6282011-03-12 14:52:01 -0800531 * {@hide}
532 */
533 public static final int TYPE_MOBILE_FOTA = 10;
534
535 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800536 * IP Multimedia Subsystem.
Wink Saville9d7d6282011-03-12 14:52:01 -0800537 * {@hide}
538 */
539 public static final int TYPE_MOBILE_IMS = 11;
540
541 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800542 * Carrier Branded Services.
Wink Saville9d7d6282011-03-12 14:52:01 -0800543 * {@hide}
544 */
545 public static final int TYPE_MOBILE_CBS = 12;
546
repo syncaea743a2011-07-29 23:55:49 -0700547 /**
548 * A Wi-Fi p2p connection. Only requesting processes will have access to
549 * the peers connected.
550 * {@hide}
551 */
552 public static final int TYPE_WIFI_P2P = 13;
Wink Saville9d7d6282011-03-12 14:52:01 -0800553
Wink Saville5e56bc52013-07-29 15:00:57 -0700554 /**
555 * The network to use for initially attaching to the network
556 * {@hide}
557 */
558 public static final int TYPE_MOBILE_IA = 14;
repo syncaea743a2011-07-29 23:55:49 -0700559
Lorenzo Colittie285b432015-04-23 15:32:42 +0900560 /**
Robert Greenwalt4bd43892015-07-09 14:49:35 -0700561 * Emergency PDN connection for emergency services. This
562 * may include IMS and MMS in emergency situations.
Ram3e0e3bc2014-06-26 11:03:44 -0700563 * {@hide}
564 */
565 public static final int TYPE_MOBILE_EMERGENCY = 15;
566
Hui Lu1c5624a2014-01-15 11:05:36 -0500567 /**
568 * The network that uses proxy to achieve connectivity.
569 * {@hide}
570 */
571 public static final int TYPE_PROXY = 16;
Wink Saville5e56bc52013-07-29 15:00:57 -0700572
Robert Greenwalt8283f882014-07-07 17:09:01 -0700573 /**
574 * A virtual network using one or more native bearers.
575 * It may or may not be providing security services.
576 */
577 public static final int TYPE_VPN = 17;
Hui Lu1c5624a2014-01-15 11:05:36 -0500578
579 /** {@hide} */
Robert Greenwalt8283f882014-07-07 17:09:01 -0700580 public static final int MAX_RADIO_TYPE = TYPE_VPN;
581
582 /** {@hide} */
583 public static final int MAX_NETWORK_TYPE = TYPE_VPN;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800584
Hugo Benichi16f0a942017-06-20 14:07:59 +0900585 private static final int MIN_NETWORK_TYPE = TYPE_MOBILE;
586
Jianzheng Zhoudcf03f32012-11-16 13:45:20 +0800587 /**
588 * If you want to set the default network preference,you can directly
589 * change the networkAttributes array in framework's config.xml.
590 *
591 * @deprecated Since we support so many more networks now, the single
592 * network default network preference can't really express
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800593 * the hierarchy. Instead, the default is defined by the
Jianzheng Zhoudcf03f32012-11-16 13:45:20 +0800594 * networkAttributes in config.xml. You can determine
Robert Greenwalt4c8b7482012-12-07 09:56:50 -0800595 * the current value by calling {@link #getNetworkPreference()}
Jianzheng Zhoudcf03f32012-11-16 13:45:20 +0800596 * from an App.
597 */
598 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800599 public static final int DEFAULT_NETWORK_PREFERENCE = TYPE_WIFI;
600
Jeff Sharkey625239a2012-09-26 22:03:49 -0700601 /**
Robert Greenwalt9ba9c582014-03-19 17:56:12 -0700602 * @hide
603 */
Hugo Benichia5c1f7f2017-06-20 14:10:14 +0900604 public static final int REQUEST_ID_UNSET = 0;
Robert Greenwalt7569f182014-06-08 16:42:59 -0700605
Paul Jensen5d59e782014-07-11 12:28:19 -0400606 /**
Hugo Benichi31c176d2017-06-17 13:14:12 +0900607 * Static unique request used as a tombstone for NetworkCallbacks that have been unregistered.
608 * This allows to distinguish when unregistering NetworkCallbacks those that were never
609 * registered and those that were already unregistered.
610 * @hide
611 */
Hugo Benichia5c1f7f2017-06-20 14:10:14 +0900612 private static final NetworkRequest ALREADY_UNREGISTERED =
Hugo Benichi31c176d2017-06-17 13:14:12 +0900613 new NetworkRequest.Builder().clearCapabilities().build();
614
615 /**
Paul Jensen5d59e782014-07-11 12:28:19 -0400616 * A NetID indicating no Network is selected.
617 * Keep in sync with bionic/libc/dns/include/resolv_netid.h
618 * @hide
619 */
620 public static final int NETID_UNSET = 0;
621
Jeff Sharkeyc006f1a2011-05-19 17:12:49 -0700622 private final IConnectivityManager mService;
Paul Jensene0bef712014-12-10 15:12:18 -0500623 /**
624 * A kludge to facilitate static access where a Context pointer isn't available, like in the
625 * case of the static set/getProcessDefaultNetwork methods and from the Network class.
626 * TODO: Remove this after deprecating the static methods in favor of non-static methods or
627 * methods that take a Context argument.
628 */
629 private static ConnectivityManager sInstance;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800630
Lorenzo Colittiffc42b02015-07-29 11:41:21 +0900631 private final Context mContext;
632
Dianne Hackborn77b987f2014-02-26 16:20:52 -0800633 private INetworkManagementService mNMService;
Felipe Leme1b103232016-01-22 09:44:57 -0800634 private INetworkPolicyManager mNPManager;
Dianne Hackborn77b987f2014-02-26 16:20:52 -0800635
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800636 /**
637 * Tests if a given integer represents a valid network type.
638 * @param networkType the type to be tested
639 * @return a boolean. {@code true} if the type is valid, else {@code false}
Paul Jensen9e59e122015-05-06 10:42:25 -0400640 * @deprecated All APIs accepting a network type are deprecated. There should be no need to
641 * validate a network type.
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800642 */
Aurimas Liutikas514c5ef2016-05-24 15:22:55 -0700643 @Deprecated
Jeff Sharkeyd2a45872011-05-28 20:56:34 -0700644 public static boolean isNetworkTypeValid(int networkType) {
Hugo Benichi16f0a942017-06-20 14:07:59 +0900645 return MIN_NETWORK_TYPE <= networkType && networkType <= MAX_NETWORK_TYPE;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800646 }
647
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800648 /**
649 * Returns a non-localized string representing a given network type.
650 * ONLY used for debugging output.
651 * @param type the type needing naming
652 * @return a String for the given type, or a string version of the type ("87")
653 * if no name is known.
654 * {@hide}
655 */
Jeff Sharkeyd2a45872011-05-28 20:56:34 -0700656 public static String getNetworkTypeName(int type) {
657 switch (type) {
Hugo Benichi16f0a942017-06-20 14:07:59 +0900658 case TYPE_NONE:
659 return "NONE";
Jeff Sharkeyd2a45872011-05-28 20:56:34 -0700660 case TYPE_MOBILE:
661 return "MOBILE";
662 case TYPE_WIFI:
663 return "WIFI";
664 case TYPE_MOBILE_MMS:
665 return "MOBILE_MMS";
666 case TYPE_MOBILE_SUPL:
667 return "MOBILE_SUPL";
668 case TYPE_MOBILE_DUN:
669 return "MOBILE_DUN";
670 case TYPE_MOBILE_HIPRI:
671 return "MOBILE_HIPRI";
672 case TYPE_WIMAX:
673 return "WIMAX";
674 case TYPE_BLUETOOTH:
675 return "BLUETOOTH";
676 case TYPE_DUMMY:
677 return "DUMMY";
678 case TYPE_ETHERNET:
679 return "ETHERNET";
680 case TYPE_MOBILE_FOTA:
681 return "MOBILE_FOTA";
682 case TYPE_MOBILE_IMS:
683 return "MOBILE_IMS";
684 case TYPE_MOBILE_CBS:
685 return "MOBILE_CBS";
repo syncaea743a2011-07-29 23:55:49 -0700686 case TYPE_WIFI_P2P:
687 return "WIFI_P2P";
Wink Saville5e56bc52013-07-29 15:00:57 -0700688 case TYPE_MOBILE_IA:
689 return "MOBILE_IA";
Ram3e0e3bc2014-06-26 11:03:44 -0700690 case TYPE_MOBILE_EMERGENCY:
691 return "MOBILE_EMERGENCY";
Hui Lu1c5624a2014-01-15 11:05:36 -0500692 case TYPE_PROXY:
693 return "PROXY";
Erik Kline37fbfa12014-11-19 17:23:41 +0900694 case TYPE_VPN:
695 return "VPN";
Jeff Sharkeyd2a45872011-05-28 20:56:34 -0700696 default:
697 return Integer.toString(type);
698 }
699 }
700
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800701 /**
702 * Checks if a given type uses the cellular data connection.
703 * This should be replaced in the future by a network property.
704 * @param networkType the type to check
705 * @return a boolean - {@code true} if uses cellular network, else {@code false}
706 * {@hide}
707 */
Jeff Sharkeyd2a45872011-05-28 20:56:34 -0700708 public static boolean isNetworkTypeMobile(int networkType) {
709 switch (networkType) {
710 case TYPE_MOBILE:
711 case TYPE_MOBILE_MMS:
712 case TYPE_MOBILE_SUPL:
713 case TYPE_MOBILE_DUN:
714 case TYPE_MOBILE_HIPRI:
715 case TYPE_MOBILE_FOTA:
716 case TYPE_MOBILE_IMS:
717 case TYPE_MOBILE_CBS:
Wink Saville5e56bc52013-07-29 15:00:57 -0700718 case TYPE_MOBILE_IA:
Ram3e0e3bc2014-06-26 11:03:44 -0700719 case TYPE_MOBILE_EMERGENCY:
Jeff Sharkeyd2a45872011-05-28 20:56:34 -0700720 return true;
721 default:
722 return false;
723 }
724 }
725
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800726 /**
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -0700727 * Checks if the given network type is backed by a Wi-Fi radio.
728 *
729 * @hide
730 */
731 public static boolean isNetworkTypeWifi(int networkType) {
732 switch (networkType) {
733 case TYPE_WIFI:
734 case TYPE_WIFI_P2P:
735 return true;
736 default:
737 return false;
738 }
739 }
740
741 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800742 * Specifies the preferred network type. When the device has more
743 * than one type available the preferred network type will be used.
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800744 *
745 * @param preference the network type to prefer over all others. It is
746 * unspecified what happens to the old preferred network in the
747 * overall ordering.
Robert Greenwaltd19c41c2014-05-18 23:07:25 -0700748 * @deprecated Functionality has been removed as it no longer makes sense,
749 * with many more than two networks - we'd need an array to express
750 * preference. Instead we use dynamic network properties of
751 * the networks to describe their precedence.
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800752 */
Aurimas Liutikas514c5ef2016-05-24 15:22:55 -0700753 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800754 public void setNetworkPreference(int preference) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800755 }
756
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800757 /**
758 * Retrieves the current preferred network type.
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800759 *
760 * @return an integer representing the preferred network type
761 *
Robert Greenwaltd19c41c2014-05-18 23:07:25 -0700762 * @deprecated Functionality has been removed as it no longer makes sense,
763 * with many more than two networks - we'd need an array to express
764 * preference. Instead we use dynamic network properties of
765 * the networks to describe their precedence.
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800766 */
Aurimas Liutikas514c5ef2016-05-24 15:22:55 -0700767 @Deprecated
Jeff Sharkey30e06bb2017-04-24 11:18:03 -0600768 @RequiresPermission(android.Manifest.permission.ACCESS_NETWORK_STATE)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800769 public int getNetworkPreference() {
Robert Greenwaltd19c41c2014-05-18 23:07:25 -0700770 return TYPE_NONE;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800771 }
772
Scott Main671644c2011-10-06 19:02:28 -0700773 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800774 * Returns details about the currently active default data network. When
775 * connected, this network is the default route for outgoing connections.
776 * You should always check {@link NetworkInfo#isConnected()} before initiating
777 * network traffic. This may return {@code null} when there is no default
778 * network.
779 *
780 * @return a {@link NetworkInfo} object for the current default network
Paul Jensen0d719ca2015-02-13 14:18:39 -0500781 * or {@code null} if no default network is currently active
Jeff Sharkey9f7cbf02012-04-12 18:34:54 -0700782 */
Jeff Sharkey30e06bb2017-04-24 11:18:03 -0600783 @RequiresPermission(android.Manifest.permission.ACCESS_NETWORK_STATE)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800784 public NetworkInfo getActiveNetworkInfo() {
785 try {
786 return mService.getActiveNetworkInfo();
787 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -0700788 throw e.rethrowFromSystemServer();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800789 }
790 }
791
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800792 /**
Paul Jensen31a94f42015-02-13 14:18:39 -0500793 * Returns a {@link Network} object corresponding to the currently active
794 * default data network. In the event that the current active 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.
798 *
799 * @return a {@link Network} object for the current default network or
800 * {@code null} if no default network is currently active
Paul Jensen31a94f42015-02-13 14:18:39 -0500801 */
Jeff Sharkey30e06bb2017-04-24 11:18:03 -0600802 @RequiresPermission(android.Manifest.permission.ACCESS_NETWORK_STATE)
Paul Jensen31a94f42015-02-13 14:18:39 -0500803 public Network getActiveNetwork() {
804 try {
805 return mService.getActiveNetwork();
806 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -0700807 throw e.rethrowFromSystemServer();
Paul Jensen31a94f42015-02-13 14:18:39 -0500808 }
809 }
810
811 /**
Robin Leed2baf792016-03-24 12:07:00 +0000812 * Returns a {@link Network} object corresponding to the currently active
813 * default data network for a specific UID. In the event that the default data
814 * network disconnects, the returned {@code Network} object will no longer
815 * be usable. This will return {@code null} when there is no default
816 * network for the UID.
Robin Leed2baf792016-03-24 12:07:00 +0000817 *
818 * @return a {@link Network} object for the current default network for the
819 * given UID or {@code null} if no default network is currently active
820 *
821 * @hide
822 */
Jeff Sharkey30e06bb2017-04-24 11:18:03 -0600823 @RequiresPermission(android.Manifest.permission.CONNECTIVITY_INTERNAL)
Robin Leed2baf792016-03-24 12:07:00 +0000824 public Network getActiveNetworkForUid(int uid) {
Jeff Sharkey1b6519b2016-04-28 15:33:18 -0600825 return getActiveNetworkForUid(uid, false);
826 }
827
828 /** {@hide} */
829 public Network getActiveNetworkForUid(int uid, boolean ignoreBlocked) {
Robin Leed2baf792016-03-24 12:07:00 +0000830 try {
Jeff Sharkey1b6519b2016-04-28 15:33:18 -0600831 return mService.getActiveNetworkForUid(uid, ignoreBlocked);
Robin Leed2baf792016-03-24 12:07:00 +0000832 } catch (RemoteException e) {
833 throw e.rethrowFromSystemServer();
834 }
835 }
836
837 /**
Charles He36738632017-05-15 17:07:18 +0100838 * Checks if a VPN app supports always-on mode.
839 *
840 * In order to support the always-on feature, an app has to
841 * <ul>
842 * <li>target {@link VERSION_CODES#N API 24} or above, and
Charles Hec57a01c2017-08-15 15:30:22 +0100843 * <li>not opt out through the {@link VpnService#SERVICE_META_DATA_SUPPORTS_ALWAYS_ON}
844 * meta-data field.
Charles He36738632017-05-15 17:07:18 +0100845 * </ul>
846 *
847 * @param userId The identifier of the user for whom the VPN app is installed.
848 * @param vpnPackage The canonical package name of the VPN app.
849 * @return {@code true} if and only if the VPN app exists and supports always-on mode.
850 * @hide
851 */
852 public boolean isAlwaysOnVpnPackageSupportedForUser(int userId, @Nullable String vpnPackage) {
853 try {
854 return mService.isAlwaysOnVpnPackageSupported(userId, vpnPackage);
855 } catch (RemoteException e) {
856 throw e.rethrowFromSystemServer();
857 }
858 }
859
860 /**
Robin Lee244ce8e2016-01-05 18:03:46 +0000861 * Configures an always-on VPN connection through a specific application.
862 * This connection is automatically granted and persisted after a reboot.
863 *
864 * <p>The designated package should declare a {@link VpnService} in its
865 * manifest guarded by {@link android.Manifest.permission.BIND_VPN_SERVICE},
866 * otherwise the call will fail.
867 *
868 * @param userId The identifier of the user to set an always-on VPN for.
869 * @param vpnPackage The package name for an installed VPN app on the device, or {@code null}
870 * to remove an existing always-on VPN configuration.
Robin Leedc679712016-05-03 13:23:03 +0100871 * @param lockdownEnabled {@code true} to disallow networking when the VPN is not connected or
872 * {@code false} otherwise.
Robin Lee244ce8e2016-01-05 18:03:46 +0000873 * @return {@code true} if the package is set as always-on VPN controller;
874 * {@code false} otherwise.
875 * @hide
876 */
Robin Leedc679712016-05-03 13:23:03 +0100877 public boolean setAlwaysOnVpnPackageForUser(int userId, @Nullable String vpnPackage,
878 boolean lockdownEnabled) {
Robin Lee244ce8e2016-01-05 18:03:46 +0000879 try {
Robin Leedc679712016-05-03 13:23:03 +0100880 return mService.setAlwaysOnVpnPackage(userId, vpnPackage, lockdownEnabled);
Robin Lee244ce8e2016-01-05 18:03:46 +0000881 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -0700882 throw e.rethrowFromSystemServer();
Robin Lee244ce8e2016-01-05 18:03:46 +0000883 }
884 }
885
886 /**
887 * Returns the package name of the currently set always-on VPN application.
888 * If there is no always-on VPN set, or the VPN is provided by the system instead
889 * of by an app, {@code null} will be returned.
890 *
891 * @return Package name of VPN controller responsible for always-on VPN,
892 * or {@code null} if none is set.
893 * @hide
894 */
895 public String getAlwaysOnVpnPackageForUser(int userId) {
896 try {
897 return mService.getAlwaysOnVpnPackage(userId);
898 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -0700899 throw e.rethrowFromSystemServer();
Robin Lee244ce8e2016-01-05 18:03:46 +0000900 }
901 }
902
903 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800904 * Returns details about the currently active default data network
905 * for a given uid. This is for internal use only to avoid spying
906 * other apps.
907 *
908 * @return a {@link NetworkInfo} object for the current default network
909 * for the given uid or {@code null} if no default network is
910 * available for the specified uid.
911 *
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800912 * {@hide}
913 */
Jeff Sharkey30e06bb2017-04-24 11:18:03 -0600914 @RequiresPermission(android.Manifest.permission.CONNECTIVITY_INTERNAL)
Jeff Sharkeyc006f1a2011-05-19 17:12:49 -0700915 public NetworkInfo getActiveNetworkInfoForUid(int uid) {
Jeff Sharkey1b6519b2016-04-28 15:33:18 -0600916 return getActiveNetworkInfoForUid(uid, false);
917 }
918
919 /** {@hide} */
920 public NetworkInfo getActiveNetworkInfoForUid(int uid, boolean ignoreBlocked) {
Jeff Sharkeyc006f1a2011-05-19 17:12:49 -0700921 try {
Jeff Sharkey1b6519b2016-04-28 15:33:18 -0600922 return mService.getActiveNetworkInfoForUid(uid, ignoreBlocked);
Jeff Sharkeyc006f1a2011-05-19 17:12:49 -0700923 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -0700924 throw e.rethrowFromSystemServer();
Jeff Sharkeyc006f1a2011-05-19 17:12:49 -0700925 }
926 }
927
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800928 /**
929 * Returns connection status information about a particular
930 * network type.
931 *
932 * @param networkType integer specifying which networkType in
933 * which you're interested.
934 * @return a {@link NetworkInfo} object for the requested
935 * network type or {@code null} if the type is not
936 * supported by the device.
937 *
Paul Jensen3541e9f2015-03-18 12:23:02 -0400938 * @deprecated This method does not support multiple connected networks
939 * of the same type. Use {@link #getAllNetworks} and
940 * {@link #getNetworkInfo(android.net.Network)} instead.
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800941 */
Aurimas Liutikas514c5ef2016-05-24 15:22:55 -0700942 @Deprecated
Jeff Sharkey30e06bb2017-04-24 11:18:03 -0600943 @RequiresPermission(android.Manifest.permission.ACCESS_NETWORK_STATE)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800944 public NetworkInfo getNetworkInfo(int networkType) {
945 try {
946 return mService.getNetworkInfo(networkType);
947 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -0700948 throw e.rethrowFromSystemServer();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800949 }
950 }
951
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800952 /**
Robert Greenwalt73b6cbae2014-06-23 11:40:00 -0700953 * Returns connection status information about a particular
954 * Network.
955 *
956 * @param network {@link Network} specifying which network
957 * in which you're interested.
958 * @return a {@link NetworkInfo} object for the requested
959 * network or {@code null} if the {@code Network}
960 * is not valid.
Robert Greenwalt73b6cbae2014-06-23 11:40:00 -0700961 */
Jeff Sharkey30e06bb2017-04-24 11:18:03 -0600962 @RequiresPermission(android.Manifest.permission.ACCESS_NETWORK_STATE)
Robert Greenwalt73b6cbae2014-06-23 11:40:00 -0700963 public NetworkInfo getNetworkInfo(Network network) {
Jeff Sharkey1b6519b2016-04-28 15:33:18 -0600964 return getNetworkInfoForUid(network, Process.myUid(), false);
965 }
966
967 /** {@hide} */
968 public NetworkInfo getNetworkInfoForUid(Network network, int uid, boolean ignoreBlocked) {
Robert Greenwalt73b6cbae2014-06-23 11:40:00 -0700969 try {
Jeff Sharkey1b6519b2016-04-28 15:33:18 -0600970 return mService.getNetworkInfoForUid(network, uid, ignoreBlocked);
Robert Greenwalt73b6cbae2014-06-23 11:40:00 -0700971 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -0700972 throw e.rethrowFromSystemServer();
Robert Greenwalt73b6cbae2014-06-23 11:40:00 -0700973 }
974 }
975
976 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800977 * Returns connection status information about all network
978 * types supported by the device.
979 *
980 * @return an array of {@link NetworkInfo} objects. Check each
981 * {@link NetworkInfo#getType} for which type each applies.
982 *
Paul Jensen3541e9f2015-03-18 12:23:02 -0400983 * @deprecated This method does not support multiple connected networks
984 * of the same type. Use {@link #getAllNetworks} and
985 * {@link #getNetworkInfo(android.net.Network)} instead.
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800986 */
Aurimas Liutikas514c5ef2016-05-24 15:22:55 -0700987 @Deprecated
Jeff Sharkey30e06bb2017-04-24 11:18:03 -0600988 @RequiresPermission(android.Manifest.permission.ACCESS_NETWORK_STATE)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800989 public NetworkInfo[] getAllNetworkInfo() {
990 try {
991 return mService.getAllNetworkInfo();
992 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -0700993 throw e.rethrowFromSystemServer();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800994 }
995 }
996
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800997 /**
Lorenzo Colittib57edc52014-08-22 17:10:50 -0700998 * Returns the {@link Network} object currently serving a given type, or
999 * null if the given type is not connected.
1000 *
Lorenzo Colittib57edc52014-08-22 17:10:50 -07001001 * @hide
Paul Jensen3541e9f2015-03-18 12:23:02 -04001002 * @deprecated This method does not support multiple connected networks
1003 * of the same type. Use {@link #getAllNetworks} and
1004 * {@link #getNetworkInfo(android.net.Network)} instead.
Lorenzo Colittib57edc52014-08-22 17:10:50 -07001005 */
Aurimas Liutikas514c5ef2016-05-24 15:22:55 -07001006 @Deprecated
Jeff Sharkey30e06bb2017-04-24 11:18:03 -06001007 @RequiresPermission(android.Manifest.permission.ACCESS_NETWORK_STATE)
Lorenzo Colittib57edc52014-08-22 17:10:50 -07001008 public Network getNetworkForType(int networkType) {
1009 try {
1010 return mService.getNetworkForType(networkType);
1011 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07001012 throw e.rethrowFromSystemServer();
Lorenzo Colittib57edc52014-08-22 17:10:50 -07001013 }
1014 }
1015
1016 /**
Robert Greenwalt73b6cbae2014-06-23 11:40:00 -07001017 * Returns an array of all {@link Network} currently tracked by the
1018 * framework.
Paul Jensenb2748922015-05-06 11:10:18 -04001019 *
1020 * @return an array of {@link Network} objects.
Robert Greenwalt73b6cbae2014-06-23 11:40:00 -07001021 */
Jeff Sharkey30e06bb2017-04-24 11:18:03 -06001022 @RequiresPermission(android.Manifest.permission.ACCESS_NETWORK_STATE)
Robert Greenwalt73b6cbae2014-06-23 11:40:00 -07001023 public Network[] getAllNetworks() {
1024 try {
1025 return mService.getAllNetworks();
1026 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07001027 throw e.rethrowFromSystemServer();
Robert Greenwalt73b6cbae2014-06-23 11:40:00 -07001028 }
1029 }
1030
1031 /**
Lorenzo Colittie285b432015-04-23 15:32:42 +09001032 * Returns an array of {@link android.net.NetworkCapabilities} objects, representing
Lorenzo Colitti403aa262014-11-28 11:21:30 +09001033 * the Networks that applications run by the given user will use by default.
1034 * @hide
1035 */
1036 public NetworkCapabilities[] getDefaultNetworkCapabilitiesForUser(int userId) {
1037 try {
1038 return mService.getDefaultNetworkCapabilitiesForUser(userId);
1039 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07001040 throw e.rethrowFromSystemServer();
Lorenzo Colitti403aa262014-11-28 11:21:30 +09001041 }
1042 }
1043
1044 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08001045 * Returns the IP information for the current default network.
1046 *
1047 * @return a {@link LinkProperties} object describing the IP info
1048 * for the current default network, or {@code null} if there
1049 * is no current default network.
1050 *
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08001051 * {@hide}
1052 */
Jeff Sharkey30e06bb2017-04-24 11:18:03 -06001053 @RequiresPermission(android.Manifest.permission.ACCESS_NETWORK_STATE)
Robert Greenwaltd192dad2010-09-14 09:18:02 -07001054 public LinkProperties getActiveLinkProperties() {
1055 try {
1056 return mService.getActiveLinkProperties();
1057 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07001058 throw e.rethrowFromSystemServer();
Robert Greenwaltd192dad2010-09-14 09:18:02 -07001059 }
1060 }
1061
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08001062 /**
1063 * Returns the IP information for a given network type.
1064 *
1065 * @param networkType the network type of interest.
1066 * @return a {@link LinkProperties} object describing the IP info
1067 * for the given networkType, or {@code null} if there is
1068 * no current default network.
1069 *
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08001070 * {@hide}
Paul Jensen3541e9f2015-03-18 12:23:02 -04001071 * @deprecated This method does not support multiple connected networks
1072 * of the same type. Use {@link #getAllNetworks},
1073 * {@link #getNetworkInfo(android.net.Network)}, and
1074 * {@link #getLinkProperties(android.net.Network)} instead.
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08001075 */
Aurimas Liutikas514c5ef2016-05-24 15:22:55 -07001076 @Deprecated
Jeff Sharkey30e06bb2017-04-24 11:18:03 -06001077 @RequiresPermission(android.Manifest.permission.ACCESS_NETWORK_STATE)
Robert Greenwaltd192dad2010-09-14 09:18:02 -07001078 public LinkProperties getLinkProperties(int networkType) {
1079 try {
Robert Greenwalt9258c642014-03-26 16:47:06 -07001080 return mService.getLinkPropertiesForType(networkType);
1081 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07001082 throw e.rethrowFromSystemServer();
Robert Greenwalt9258c642014-03-26 16:47:06 -07001083 }
1084 }
1085
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07001086 /**
1087 * Get the {@link LinkProperties} for the given {@link Network}. This
1088 * will return {@code null} if the network is unknown.
1089 *
1090 * @param network The {@link Network} object identifying the network in question.
1091 * @return The {@link LinkProperties} for the network, or {@code null}.
Paul Jensenb2748922015-05-06 11:10:18 -04001092 */
Jeff Sharkey30e06bb2017-04-24 11:18:03 -06001093 @RequiresPermission(android.Manifest.permission.ACCESS_NETWORK_STATE)
Robert Greenwalt9258c642014-03-26 16:47:06 -07001094 public LinkProperties getLinkProperties(Network network) {
1095 try {
1096 return mService.getLinkProperties(network);
1097 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07001098 throw e.rethrowFromSystemServer();
Robert Greenwalt9258c642014-03-26 16:47:06 -07001099 }
1100 }
1101
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07001102 /**
Lorenzo Colittie285b432015-04-23 15:32:42 +09001103 * Get the {@link android.net.NetworkCapabilities} for the given {@link Network}. This
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07001104 * will return {@code null} if the network is unknown.
1105 *
1106 * @param network The {@link Network} object identifying the network in question.
Lorenzo Colittie285b432015-04-23 15:32:42 +09001107 * @return The {@link android.net.NetworkCapabilities} for the network, or {@code null}.
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07001108 */
Jeff Sharkey30e06bb2017-04-24 11:18:03 -06001109 @RequiresPermission(android.Manifest.permission.ACCESS_NETWORK_STATE)
Robert Greenwalt9258c642014-03-26 16:47:06 -07001110 public NetworkCapabilities getNetworkCapabilities(Network network) {
1111 try {
1112 return mService.getNetworkCapabilities(network);
Robert Greenwaltd192dad2010-09-14 09:18:02 -07001113 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07001114 throw e.rethrowFromSystemServer();
Robert Greenwaltd192dad2010-09-14 09:18:02 -07001115 }
1116 }
1117
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08001118 /**
Udam Sainib7c24872016-01-04 12:16:14 -08001119 * Gets the URL that should be used for resolving whether a captive portal is present.
1120 * 1. This URL should respond with a 204 response to a GET request to indicate no captive
1121 * portal is present.
1122 * 2. This URL must be HTTP as redirect responses are used to find captive portal
1123 * sign-in pages. Captive portals cannot respond to HTTPS requests with redirects.
1124 *
1125 * @hide
1126 */
1127 @SystemApi
Udam Saini0e94c362017-06-07 12:06:28 -07001128 @RequiresPermission(android.Manifest.permission.LOCAL_MAC_ADDRESS)
Udam Sainib7c24872016-01-04 12:16:14 -08001129 public String getCaptivePortalServerUrl() {
1130 try {
1131 return mService.getCaptivePortalServerUrl();
1132 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07001133 throw e.rethrowFromSystemServer();
Udam Sainib7c24872016-01-04 12:16:14 -08001134 }
1135 }
1136
1137 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001138 * Tells the underlying networking system that the caller wants to
1139 * begin using the named feature. The interpretation of {@code feature}
1140 * is completely up to each networking implementation.
Lorenzo Colittid5427052015-10-15 16:29:00 +09001141 *
1142 * <p>This method requires the caller to hold either the
1143 * {@link android.Manifest.permission#CHANGE_NETWORK_STATE} permission
1144 * or the ability to modify system settings as determined by
1145 * {@link android.provider.Settings.System#canWrite}.</p>
1146 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001147 * @param networkType specifies which network the request pertains to
1148 * @param feature the name of the feature to be used
1149 * @return an integer value representing the outcome of the request.
1150 * The interpretation of this value is specific to each networking
1151 * implementation+feature combination, except that the value {@code -1}
1152 * always indicates failure.
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07001153 *
Lorenzo Colitti2ea89e52015-04-24 17:03:31 +09001154 * @deprecated Deprecated in favor of the cleaner
1155 * {@link #requestNetwork(NetworkRequest, NetworkCallback)} API.
Dianne Hackborn692a2442015-07-31 10:35:34 -07001156 * In {@link VERSION_CODES#M}, and above, this method is unsupported and will
Lorenzo Colittiffc42b02015-07-29 11:41:21 +09001157 * throw {@code UnsupportedOperationException} if called.
Lorenzo Colitti2187df72016-12-09 18:39:30 +09001158 * @removed
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001159 */
Aurimas Liutikas514c5ef2016-05-24 15:22:55 -07001160 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001161 public int startUsingNetworkFeature(int networkType, String feature) {
Lorenzo Colittiffc42b02015-07-29 11:41:21 +09001162 checkLegacyRoutingApiAccess();
Robert Greenwalt562cc542014-05-15 18:07:26 -07001163 NetworkCapabilities netCap = networkCapabilitiesForFeature(networkType, feature);
1164 if (netCap == null) {
1165 Log.d(TAG, "Can't satisfy startUsingNetworkFeature for " + networkType + ", " +
1166 feature);
1167 return PhoneConstants.APN_REQUEST_FAILED;
1168 }
1169
1170 NetworkRequest request = null;
1171 synchronized (sLegacyRequests) {
1172 LegacyRequest l = sLegacyRequests.get(netCap);
1173 if (l != null) {
1174 Log.d(TAG, "renewing startUsingNetworkFeature request " + l.networkRequest);
1175 renewRequestLocked(l);
1176 if (l.currentNetwork != null) {
1177 return PhoneConstants.APN_ALREADY_ACTIVE;
1178 } else {
1179 return PhoneConstants.APN_REQUEST_STARTED;
1180 }
1181 }
1182
1183 request = requestNetworkForFeatureLocked(netCap);
1184 }
1185 if (request != null) {
Robert Greenwalt257ee5f2014-06-20 10:58:45 -07001186 Log.d(TAG, "starting startUsingNetworkFeature for request " + request);
Robert Greenwalt562cc542014-05-15 18:07:26 -07001187 return PhoneConstants.APN_REQUEST_STARTED;
1188 } else {
1189 Log.d(TAG, " request Failed");
1190 return PhoneConstants.APN_REQUEST_FAILED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001191 }
1192 }
1193
1194 /**
1195 * Tells the underlying networking system that the caller is finished
1196 * using the named feature. The interpretation of {@code feature}
1197 * is completely up to each networking implementation.
Lorenzo Colittid5427052015-10-15 16:29:00 +09001198 *
1199 * <p>This method requires the caller to hold either the
1200 * {@link android.Manifest.permission#CHANGE_NETWORK_STATE} permission
1201 * or the ability to modify system settings as determined by
1202 * {@link android.provider.Settings.System#canWrite}.</p>
1203 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001204 * @param networkType specifies which network the request pertains to
1205 * @param feature the name of the feature that is no longer needed
1206 * @return an integer value representing the outcome of the request.
1207 * The interpretation of this value is specific to each networking
1208 * implementation+feature combination, except that the value {@code -1}
1209 * always indicates failure.
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07001210 *
Lorenzo Colitti88bc0bb2016-04-13 22:00:02 +09001211 * @deprecated Deprecated in favor of the cleaner
1212 * {@link #unregisterNetworkCallback(NetworkCallback)} API.
Dianne Hackborn692a2442015-07-31 10:35:34 -07001213 * In {@link VERSION_CODES#M}, and above, this method is unsupported and will
Lorenzo Colittiffc42b02015-07-29 11:41:21 +09001214 * throw {@code UnsupportedOperationException} if called.
Lorenzo Colitti2187df72016-12-09 18:39:30 +09001215 * @removed
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001216 */
Aurimas Liutikas514c5ef2016-05-24 15:22:55 -07001217 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001218 public int stopUsingNetworkFeature(int networkType, String feature) {
Lorenzo Colittiffc42b02015-07-29 11:41:21 +09001219 checkLegacyRoutingApiAccess();
Robert Greenwalt562cc542014-05-15 18:07:26 -07001220 NetworkCapabilities netCap = networkCapabilitiesForFeature(networkType, feature);
1221 if (netCap == null) {
1222 Log.d(TAG, "Can't satisfy stopUsingNetworkFeature for " + networkType + ", " +
1223 feature);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001224 return -1;
1225 }
Robert Greenwalt562cc542014-05-15 18:07:26 -07001226
Paul Jensen9ffb53c2014-12-17 10:39:34 -05001227 if (removeRequestForFeature(netCap)) {
Robert Greenwalt562cc542014-05-15 18:07:26 -07001228 Log.d(TAG, "stopUsingNetworkFeature for " + networkType + ", " + feature);
Robert Greenwalt562cc542014-05-15 18:07:26 -07001229 }
1230 return 1;
1231 }
1232
1233 private NetworkCapabilities networkCapabilitiesForFeature(int networkType, String feature) {
1234 if (networkType == TYPE_MOBILE) {
Erik Kline35bf06c2017-01-26 18:08:28 +09001235 switch (feature) {
1236 case "enableCBS":
1237 return networkCapabilitiesForType(TYPE_MOBILE_CBS);
1238 case "enableDUN":
1239 case "enableDUNAlways":
1240 return networkCapabilitiesForType(TYPE_MOBILE_DUN);
1241 case "enableFOTA":
1242 return networkCapabilitiesForType(TYPE_MOBILE_FOTA);
1243 case "enableHIPRI":
1244 return networkCapabilitiesForType(TYPE_MOBILE_HIPRI);
1245 case "enableIMS":
1246 return networkCapabilitiesForType(TYPE_MOBILE_IMS);
1247 case "enableMMS":
1248 return networkCapabilitiesForType(TYPE_MOBILE_MMS);
1249 case "enableSUPL":
1250 return networkCapabilitiesForType(TYPE_MOBILE_SUPL);
1251 default:
1252 return null;
Robert Greenwalt562cc542014-05-15 18:07:26 -07001253 }
Erik Kline35bf06c2017-01-26 18:08:28 +09001254 } else if (networkType == TYPE_WIFI && "p2p".equals(feature)) {
1255 return networkCapabilitiesForType(TYPE_WIFI_P2P);
Robert Greenwalt562cc542014-05-15 18:07:26 -07001256 }
1257 return null;
1258 }
1259
Robert Greenwalt06314e42014-10-29 14:04:06 -07001260 /**
1261 * Guess what the network request was trying to say so that the resulting
1262 * network is accessible via the legacy (deprecated) API such as
1263 * requestRouteToHost.
Lorenzo Colittidef4cb02015-11-25 20:28:50 +09001264 *
1265 * This means we should try to be fairly precise about transport and
Robert Greenwalt06314e42014-10-29 14:04:06 -07001266 * capability but ignore things such as networkSpecifier.
1267 * If the request has more than one transport or capability it doesn't
1268 * match the old legacy requests (they selected only single transport/capability)
1269 * so this function cannot map the request to a single legacy type and
1270 * the resulting network will not be available to the legacy APIs.
1271 *
Lorenzo Colittidef4cb02015-11-25 20:28:50 +09001272 * This code is only called from the requestNetwork API (L and above).
1273 *
1274 * Setting a legacy type causes CONNECTIVITY_ACTION broadcasts, which are expensive
1275 * because they wake up lots of apps - see http://b/23350688 . So we currently only
1276 * do this for SUPL requests, which are the only ones that we know need it. If
1277 * omitting these broadcasts causes unacceptable app breakage, then for backwards
1278 * compatibility we can send them:
1279 *
1280 * if (targetSdkVersion < Build.VERSION_CODES.M) && // legacy API unsupported >= M
1281 * targetSdkVersion >= Build.VERSION_CODES.LOLLIPOP)) // requestNetwork not present < L
1282 *
Robert Greenwalt06314e42014-10-29 14:04:06 -07001283 * TODO - This should be removed when the legacy APIs are removed.
1284 */
Ye Wenb87875e2014-07-21 14:19:01 -07001285 private int inferLegacyTypeForNetworkCapabilities(NetworkCapabilities netCap) {
1286 if (netCap == null) {
1287 return TYPE_NONE;
1288 }
Robert Greenwalt06314e42014-10-29 14:04:06 -07001289
Ye Wenb87875e2014-07-21 14:19:01 -07001290 if (!netCap.hasTransport(NetworkCapabilities.TRANSPORT_CELLULAR)) {
1291 return TYPE_NONE;
1292 }
Robert Greenwalt06314e42014-10-29 14:04:06 -07001293
Lifu Tang30f95a72016-01-07 23:20:38 -08001294 // Do this only for SUPL, until GnssLocationProvider is fixed. http://b/25876485 .
Lorenzo Colittidef4cb02015-11-25 20:28:50 +09001295 if (!netCap.hasCapability(NetworkCapabilities.NET_CAPABILITY_SUPL)) {
1296 // NOTE: if this causes app breakage, we should not just comment out this early return;
1297 // instead, we should make this early return conditional on the requesting app's target
1298 // SDK version, as described in the comment above.
1299 return TYPE_NONE;
1300 }
1301
Robert Greenwalt06314e42014-10-29 14:04:06 -07001302 String type = null;
1303 int result = TYPE_NONE;
1304
Ye Wenb87875e2014-07-21 14:19:01 -07001305 if (netCap.hasCapability(NetworkCapabilities.NET_CAPABILITY_CBS)) {
Robert Greenwalt06314e42014-10-29 14:04:06 -07001306 type = "enableCBS";
1307 result = TYPE_MOBILE_CBS;
1308 } else if (netCap.hasCapability(NetworkCapabilities.NET_CAPABILITY_IMS)) {
1309 type = "enableIMS";
1310 result = TYPE_MOBILE_IMS;
1311 } else if (netCap.hasCapability(NetworkCapabilities.NET_CAPABILITY_FOTA)) {
1312 type = "enableFOTA";
1313 result = TYPE_MOBILE_FOTA;
1314 } else if (netCap.hasCapability(NetworkCapabilities.NET_CAPABILITY_DUN)) {
1315 type = "enableDUN";
1316 result = TYPE_MOBILE_DUN;
1317 } else if (netCap.hasCapability(NetworkCapabilities.NET_CAPABILITY_SUPL)) {
Lorenzo Colittidef4cb02015-11-25 20:28:50 +09001318 type = "enableSUPL";
Robert Greenwalt06314e42014-10-29 14:04:06 -07001319 result = TYPE_MOBILE_SUPL;
Robert Greenwalt74ab4fa2015-08-28 12:37:54 -07001320 // back out this hack for mms as they no longer need this and it's causing
1321 // device slowdowns - b/23350688 (note, supl still needs this)
1322 //} else if (netCap.hasCapability(NetworkCapabilities.NET_CAPABILITY_MMS)) {
1323 // type = "enableMMS";
1324 // result = TYPE_MOBILE_MMS;
Robert Greenwalt06314e42014-10-29 14:04:06 -07001325 } else if (netCap.hasCapability(NetworkCapabilities.NET_CAPABILITY_INTERNET)) {
1326 type = "enableHIPRI";
1327 result = TYPE_MOBILE_HIPRI;
Ye Wenb87875e2014-07-21 14:19:01 -07001328 }
Robert Greenwalt06314e42014-10-29 14:04:06 -07001329 if (type != null) {
1330 NetworkCapabilities testCap = networkCapabilitiesForFeature(TYPE_MOBILE, type);
1331 if (testCap.equalsNetCapabilities(netCap) && testCap.equalsTransportTypes(netCap)) {
1332 return result;
Ye Wenb87875e2014-07-21 14:19:01 -07001333 }
1334 }
1335 return TYPE_NONE;
1336 }
1337
Robert Greenwalt32aa65a2014-06-02 15:32:02 -07001338 private int legacyTypeForNetworkCapabilities(NetworkCapabilities netCap) {
Robert Greenwalt562cc542014-05-15 18:07:26 -07001339 if (netCap == null) return TYPE_NONE;
1340 if (netCap.hasCapability(NetworkCapabilities.NET_CAPABILITY_CBS)) {
1341 return TYPE_MOBILE_CBS;
1342 }
1343 if (netCap.hasCapability(NetworkCapabilities.NET_CAPABILITY_IMS)) {
1344 return TYPE_MOBILE_IMS;
1345 }
1346 if (netCap.hasCapability(NetworkCapabilities.NET_CAPABILITY_FOTA)) {
1347 return TYPE_MOBILE_FOTA;
1348 }
1349 if (netCap.hasCapability(NetworkCapabilities.NET_CAPABILITY_DUN)) {
1350 return TYPE_MOBILE_DUN;
1351 }
1352 if (netCap.hasCapability(NetworkCapabilities.NET_CAPABILITY_SUPL)) {
1353 return TYPE_MOBILE_SUPL;
1354 }
1355 if (netCap.hasCapability(NetworkCapabilities.NET_CAPABILITY_MMS)) {
1356 return TYPE_MOBILE_MMS;
1357 }
1358 if (netCap.hasCapability(NetworkCapabilities.NET_CAPABILITY_INTERNET)) {
1359 return TYPE_MOBILE_HIPRI;
1360 }
Robert Greenwalt32aa65a2014-06-02 15:32:02 -07001361 if (netCap.hasCapability(NetworkCapabilities.NET_CAPABILITY_WIFI_P2P)) {
1362 return TYPE_WIFI_P2P;
1363 }
Robert Greenwalt562cc542014-05-15 18:07:26 -07001364 return TYPE_NONE;
1365 }
1366
1367 private static class LegacyRequest {
1368 NetworkCapabilities networkCapabilities;
1369 NetworkRequest networkRequest;
1370 int expireSequenceNumber;
1371 Network currentNetwork;
1372 int delay = -1;
Paul Jensen9ffb53c2014-12-17 10:39:34 -05001373
1374 private void clearDnsBinding() {
1375 if (currentNetwork != null) {
1376 currentNetwork = null;
1377 setProcessDefaultNetworkForHostResolution(null);
1378 }
1379 }
1380
Robert Greenwalt6078b502014-06-11 16:05:07 -07001381 NetworkCallback networkCallback = new NetworkCallback() {
Robert Greenwalt562cc542014-05-15 18:07:26 -07001382 @Override
Robert Greenwalt6078b502014-06-11 16:05:07 -07001383 public void onAvailable(Network network) {
Robert Greenwalt562cc542014-05-15 18:07:26 -07001384 currentNetwork = network;
1385 Log.d(TAG, "startUsingNetworkFeature got Network:" + network);
Paul Jensen6d3ff9e2014-05-29 10:12:39 -04001386 setProcessDefaultNetworkForHostResolution(network);
Robert Greenwalt562cc542014-05-15 18:07:26 -07001387 }
1388 @Override
Robert Greenwalt6078b502014-06-11 16:05:07 -07001389 public void onLost(Network network) {
Paul Jensen9ffb53c2014-12-17 10:39:34 -05001390 if (network.equals(currentNetwork)) clearDnsBinding();
Robert Greenwalt562cc542014-05-15 18:07:26 -07001391 Log.d(TAG, "startUsingNetworkFeature lost Network:" + network);
1392 }
1393 };
1394 }
1395
Robert Greenwaltfab501672014-07-23 11:44:01 -07001396 private static HashMap<NetworkCapabilities, LegacyRequest> sLegacyRequests =
Robert Greenwalt562cc542014-05-15 18:07:26 -07001397 new HashMap<NetworkCapabilities, LegacyRequest>();
1398
1399 private NetworkRequest findRequestForFeature(NetworkCapabilities netCap) {
1400 synchronized (sLegacyRequests) {
1401 LegacyRequest l = sLegacyRequests.get(netCap);
1402 if (l != null) return l.networkRequest;
1403 }
1404 return null;
1405 }
1406
1407 private void renewRequestLocked(LegacyRequest l) {
1408 l.expireSequenceNumber++;
1409 Log.d(TAG, "renewing request to seqNum " + l.expireSequenceNumber);
1410 sendExpireMsgForFeature(l.networkCapabilities, l.expireSequenceNumber, l.delay);
1411 }
1412
1413 private void expireRequest(NetworkCapabilities netCap, int sequenceNum) {
1414 int ourSeqNum = -1;
1415 synchronized (sLegacyRequests) {
1416 LegacyRequest l = sLegacyRequests.get(netCap);
1417 if (l == null) return;
1418 ourSeqNum = l.expireSequenceNumber;
Paul Jensen9ffb53c2014-12-17 10:39:34 -05001419 if (l.expireSequenceNumber == sequenceNum) removeRequestForFeature(netCap);
Robert Greenwalt562cc542014-05-15 18:07:26 -07001420 }
1421 Log.d(TAG, "expireRequest with " + ourSeqNum + ", " + sequenceNum);
1422 }
1423
1424 private NetworkRequest requestNetworkForFeatureLocked(NetworkCapabilities netCap) {
1425 int delay = -1;
Robert Greenwalt32aa65a2014-06-02 15:32:02 -07001426 int type = legacyTypeForNetworkCapabilities(netCap);
Robert Greenwalt562cc542014-05-15 18:07:26 -07001427 try {
1428 delay = mService.getRestoreDefaultNetworkDelay(type);
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07001429 } catch (RemoteException e) {
1430 throw e.rethrowFromSystemServer();
1431 }
Robert Greenwalt562cc542014-05-15 18:07:26 -07001432 LegacyRequest l = new LegacyRequest();
1433 l.networkCapabilities = netCap;
1434 l.delay = delay;
1435 l.expireSequenceNumber = 0;
Hugo Benichi2583ef02017-02-02 17:02:36 +09001436 l.networkRequest = sendRequestForNetwork(
1437 netCap, l.networkCallback, 0, REQUEST, type, getDefaultHandler());
Robert Greenwalt562cc542014-05-15 18:07:26 -07001438 if (l.networkRequest == null) return null;
1439 sLegacyRequests.put(netCap, l);
1440 sendExpireMsgForFeature(netCap, l.expireSequenceNumber, delay);
1441 return l.networkRequest;
1442 }
1443
1444 private void sendExpireMsgForFeature(NetworkCapabilities netCap, int seqNum, int delay) {
1445 if (delay >= 0) {
1446 Log.d(TAG, "sending expire msg with seqNum " + seqNum + " and delay " + delay);
Hugo Benichi2583ef02017-02-02 17:02:36 +09001447 CallbackHandler handler = getDefaultHandler();
Hugo Benichi6f260f32017-02-03 14:18:44 +09001448 Message msg = handler.obtainMessage(EXPIRE_LEGACY_REQUEST, seqNum, 0, netCap);
1449 handler.sendMessageDelayed(msg, delay);
Robert Greenwalt562cc542014-05-15 18:07:26 -07001450 }
1451 }
1452
Paul Jensen9ffb53c2014-12-17 10:39:34 -05001453 private boolean removeRequestForFeature(NetworkCapabilities netCap) {
1454 final LegacyRequest l;
Robert Greenwalt562cc542014-05-15 18:07:26 -07001455 synchronized (sLegacyRequests) {
Paul Jensen9ffb53c2014-12-17 10:39:34 -05001456 l = sLegacyRequests.remove(netCap);
Robert Greenwalt562cc542014-05-15 18:07:26 -07001457 }
Paul Jensen9ffb53c2014-12-17 10:39:34 -05001458 if (l == null) return false;
1459 unregisterNetworkCallback(l.networkCallback);
1460 l.clearDnsBinding();
1461 return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001462 }
1463
Erik Kline35bf06c2017-01-26 18:08:28 +09001464 private static final SparseIntArray sLegacyTypeToTransport = new SparseIntArray();
1465 static {
1466 sLegacyTypeToTransport.put(TYPE_MOBILE, NetworkCapabilities.TRANSPORT_CELLULAR);
1467 sLegacyTypeToTransport.put(TYPE_MOBILE_CBS, NetworkCapabilities.TRANSPORT_CELLULAR);
1468 sLegacyTypeToTransport.put(TYPE_MOBILE_DUN, NetworkCapabilities.TRANSPORT_CELLULAR);
1469 sLegacyTypeToTransport.put(TYPE_MOBILE_FOTA, NetworkCapabilities.TRANSPORT_CELLULAR);
1470 sLegacyTypeToTransport.put(TYPE_MOBILE_HIPRI, NetworkCapabilities.TRANSPORT_CELLULAR);
1471 sLegacyTypeToTransport.put(TYPE_MOBILE_IMS, NetworkCapabilities.TRANSPORT_CELLULAR);
1472 sLegacyTypeToTransport.put(TYPE_MOBILE_MMS, NetworkCapabilities.TRANSPORT_CELLULAR);
1473 sLegacyTypeToTransport.put(TYPE_MOBILE_SUPL, NetworkCapabilities.TRANSPORT_CELLULAR);
1474 sLegacyTypeToTransport.put(TYPE_WIFI, NetworkCapabilities.TRANSPORT_WIFI);
1475 sLegacyTypeToTransport.put(TYPE_WIFI_P2P, NetworkCapabilities.TRANSPORT_WIFI);
1476 sLegacyTypeToTransport.put(TYPE_BLUETOOTH, NetworkCapabilities.TRANSPORT_BLUETOOTH);
1477 sLegacyTypeToTransport.put(TYPE_ETHERNET, NetworkCapabilities.TRANSPORT_ETHERNET);
1478 }
1479
1480 private static final SparseIntArray sLegacyTypeToCapability = new SparseIntArray();
1481 static {
1482 sLegacyTypeToCapability.put(TYPE_MOBILE_CBS, NetworkCapabilities.NET_CAPABILITY_CBS);
1483 sLegacyTypeToCapability.put(TYPE_MOBILE_DUN, NetworkCapabilities.NET_CAPABILITY_DUN);
1484 sLegacyTypeToCapability.put(TYPE_MOBILE_FOTA, NetworkCapabilities.NET_CAPABILITY_FOTA);
1485 sLegacyTypeToCapability.put(TYPE_MOBILE_IMS, NetworkCapabilities.NET_CAPABILITY_IMS);
1486 sLegacyTypeToCapability.put(TYPE_MOBILE_MMS, NetworkCapabilities.NET_CAPABILITY_MMS);
1487 sLegacyTypeToCapability.put(TYPE_MOBILE_SUPL, NetworkCapabilities.NET_CAPABILITY_SUPL);
1488 sLegacyTypeToCapability.put(TYPE_WIFI_P2P, NetworkCapabilities.NET_CAPABILITY_WIFI_P2P);
1489 }
1490
1491 /**
1492 * Given a legacy type (TYPE_WIFI, ...) returns a NetworkCapabilities
1493 * instance suitable for registering a request or callback. Throws an
1494 * IllegalArgumentException if no mapping from the legacy type to
1495 * NetworkCapabilities is known.
1496 *
1497 * @hide
1498 */
1499 public static NetworkCapabilities networkCapabilitiesForType(int type) {
1500 final NetworkCapabilities nc = new NetworkCapabilities();
1501
1502 // Map from type to transports.
1503 final int NOT_FOUND = -1;
1504 final int transport = sLegacyTypeToTransport.get(type, NOT_FOUND);
Hugo Benichie7678512017-05-09 15:19:01 +09001505 Preconditions.checkArgument(transport != NOT_FOUND, "unknown legacy type: " + type);
Erik Kline35bf06c2017-01-26 18:08:28 +09001506 nc.addTransportType(transport);
1507
1508 // Map from type to capabilities.
1509 nc.addCapability(sLegacyTypeToCapability.get(
1510 type, NetworkCapabilities.NET_CAPABILITY_INTERNET));
1511 nc.maybeMarkCapabilitiesRestricted();
1512 return nc;
1513 }
1514
Lorenzo Colitti8bf977d2015-06-15 14:29:22 +09001515 /** @hide */
1516 public static class PacketKeepaliveCallback {
1517 /** The requested keepalive was successfully started. */
1518 public void onStarted() {}
1519 /** The keepalive was successfully stopped. */
1520 public void onStopped() {}
1521 /** An error occurred. */
1522 public void onError(int error) {}
1523 }
1524
1525 /**
1526 * Allows applications to request that the system periodically send specific packets on their
1527 * behalf, using hardware offload to save battery power.
1528 *
1529 * To request that the system send keepalives, call one of the methods that return a
1530 * {@link ConnectivityManager.PacketKeepalive} object, such as {@link #startNattKeepalive},
1531 * passing in a non-null callback. If the callback is successfully started, the callback's
1532 * {@code onStarted} method will be called. If an error occurs, {@code onError} will be called,
1533 * specifying one of the {@code ERROR_*} constants in this class.
1534 *
1535 * To stop an existing keepalive, call {@link stop}. The system will call {@code onStopped} if
1536 * the operation was successfull or {@code onError} if an error occurred.
1537 *
1538 * @hide
1539 */
1540 public class PacketKeepalive {
1541
1542 private static final String TAG = "PacketKeepalive";
1543
1544 /** @hide */
1545 public static final int SUCCESS = 0;
1546
1547 /** @hide */
1548 public static final int NO_KEEPALIVE = -1;
1549
1550 /** @hide */
1551 public static final int BINDER_DIED = -10;
1552
1553 /** The specified {@code Network} is not connected. */
1554 public static final int ERROR_INVALID_NETWORK = -20;
1555 /** The specified IP addresses are invalid. For example, the specified source IP address is
1556 * not configured on the specified {@code Network}. */
1557 public static final int ERROR_INVALID_IP_ADDRESS = -21;
1558 /** The requested port is invalid. */
1559 public static final int ERROR_INVALID_PORT = -22;
1560 /** The packet length is invalid (e.g., too long). */
1561 public static final int ERROR_INVALID_LENGTH = -23;
1562 /** The packet transmission interval is invalid (e.g., too short). */
1563 public static final int ERROR_INVALID_INTERVAL = -24;
1564
1565 /** The hardware does not support this request. */
1566 public static final int ERROR_HARDWARE_UNSUPPORTED = -30;
Lorenzo Colitti9d1284e2015-09-08 16:46:36 +09001567 /** The hardware returned an error. */
1568 public static final int ERROR_HARDWARE_ERROR = -31;
Lorenzo Colitti8bf977d2015-06-15 14:29:22 +09001569
1570 public static final int NATT_PORT = 4500;
1571
1572 private final Network mNetwork;
1573 private final PacketKeepaliveCallback mCallback;
1574 private final Looper mLooper;
1575 private final Messenger mMessenger;
1576
1577 private volatile Integer mSlot;
1578
1579 void stopLooper() {
1580 mLooper.quit();
1581 }
1582
1583 public void stop() {
1584 try {
1585 mService.stopKeepalive(mNetwork, mSlot);
1586 } catch (RemoteException e) {
1587 Log.e(TAG, "Error stopping packet keepalive: ", e);
1588 stopLooper();
1589 }
1590 }
1591
1592 private PacketKeepalive(Network network, PacketKeepaliveCallback callback) {
Hugo Benichidafed3d2017-03-06 09:17:06 +09001593 Preconditions.checkNotNull(network, "network cannot be null");
1594 Preconditions.checkNotNull(callback, "callback cannot be null");
Lorenzo Colitti8bf977d2015-06-15 14:29:22 +09001595 mNetwork = network;
1596 mCallback = callback;
1597 HandlerThread thread = new HandlerThread(TAG);
1598 thread.start();
1599 mLooper = thread.getLooper();
1600 mMessenger = new Messenger(new Handler(mLooper) {
1601 @Override
1602 public void handleMessage(Message message) {
1603 switch (message.what) {
1604 case NetworkAgent.EVENT_PACKET_KEEPALIVE:
1605 int error = message.arg2;
1606 try {
1607 if (error == SUCCESS) {
1608 if (mSlot == null) {
1609 mSlot = message.arg1;
1610 mCallback.onStarted();
1611 } else {
1612 mSlot = null;
1613 stopLooper();
1614 mCallback.onStopped();
1615 }
1616 } else {
1617 stopLooper();
1618 mCallback.onError(error);
1619 }
1620 } catch (Exception e) {
1621 Log.e(TAG, "Exception in keepalive callback(" + error + ")", e);
1622 }
1623 break;
1624 default:
1625 Log.e(TAG, "Unhandled message " + Integer.toHexString(message.what));
1626 break;
1627 }
1628 }
1629 });
1630 }
1631 }
1632
1633 /**
1634 * Starts an IPsec NAT-T keepalive packet with the specified parameters.
1635 *
1636 * @hide
1637 */
1638 public PacketKeepalive startNattKeepalive(
1639 Network network, int intervalSeconds, PacketKeepaliveCallback callback,
1640 InetAddress srcAddr, int srcPort, InetAddress dstAddr) {
1641 final PacketKeepalive k = new PacketKeepalive(network, callback);
1642 try {
1643 mService.startNattKeepalive(network, intervalSeconds, k.mMessenger, new Binder(),
1644 srcAddr.getHostAddress(), srcPort, dstAddr.getHostAddress());
1645 } catch (RemoteException e) {
1646 Log.e(TAG, "Error starting packet keepalive: ", e);
1647 k.stopLooper();
1648 return null;
1649 }
1650 return k;
1651 }
1652
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001653 /**
1654 * Ensure that a network route exists to deliver traffic to the specified
1655 * host via the specified network interface. An attempt to add a route that
1656 * already exists is ignored, but treated as successful.
Lorenzo Colittid5427052015-10-15 16:29:00 +09001657 *
1658 * <p>This method requires the caller to hold either the
1659 * {@link android.Manifest.permission#CHANGE_NETWORK_STATE} permission
1660 * or the ability to modify system settings as determined by
1661 * {@link android.provider.Settings.System#canWrite}.</p>
1662 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001663 * @param networkType the type of the network over which traffic to the specified
1664 * host is to be routed
1665 * @param hostAddress the IP address of the host to which the route is desired
1666 * @return {@code true} on success, {@code false} on failure
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07001667 *
Lorenzo Colitti2ea89e52015-04-24 17:03:31 +09001668 * @deprecated Deprecated in favor of the
1669 * {@link #requestNetwork(NetworkRequest, NetworkCallback)},
1670 * {@link #bindProcessToNetwork} and {@link Network#getSocketFactory} API.
Dianne Hackborn692a2442015-07-31 10:35:34 -07001671 * In {@link VERSION_CODES#M}, and above, this method is unsupported and will
Lorenzo Colittiffc42b02015-07-29 11:41:21 +09001672 * throw {@code UnsupportedOperationException} if called.
Lorenzo Colitti2187df72016-12-09 18:39:30 +09001673 * @removed
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001674 */
Aurimas Liutikas514c5ef2016-05-24 15:22:55 -07001675 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001676 public boolean requestRouteToHost(int networkType, int hostAddress) {
Sreeram Ramachandran03666c72014-07-19 23:21:46 -07001677 return requestRouteToHostAddress(networkType, NetworkUtils.intToInetAddress(hostAddress));
Robert Greenwalt585ac0f2010-08-27 09:24:29 -07001678 }
1679
1680 /**
1681 * Ensure that a network route exists to deliver traffic to the specified
1682 * host via the specified network interface. An attempt to add a route that
1683 * already exists is ignored, but treated as successful.
Lorenzo Colittid5427052015-10-15 16:29:00 +09001684 *
1685 * <p>This method requires the caller to hold either the
1686 * {@link android.Manifest.permission#CHANGE_NETWORK_STATE} permission
1687 * or the ability to modify system settings as determined by
1688 * {@link android.provider.Settings.System#canWrite}.</p>
1689 *
Robert Greenwalt585ac0f2010-08-27 09:24:29 -07001690 * @param networkType the type of the network over which traffic to the specified
1691 * host is to be routed
1692 * @param hostAddress the IP address of the host to which the route is desired
1693 * @return {@code true} on success, {@code false} on failure
1694 * @hide
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07001695 * @deprecated Deprecated in favor of the {@link #requestNetwork} and
Lorenzo Colitti2ea89e52015-04-24 17:03:31 +09001696 * {@link #bindProcessToNetwork} API.
Robert Greenwalt585ac0f2010-08-27 09:24:29 -07001697 */
Aurimas Liutikas514c5ef2016-05-24 15:22:55 -07001698 @Deprecated
Robert Greenwalt585ac0f2010-08-27 09:24:29 -07001699 public boolean requestRouteToHostAddress(int networkType, InetAddress hostAddress) {
Lorenzo Colittiffc42b02015-07-29 11:41:21 +09001700 checkLegacyRoutingApiAccess();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001701 try {
Sreeram Ramachandran03666c72014-07-19 23:21:46 -07001702 return mService.requestRouteToHostAddress(networkType, hostAddress.getAddress());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001703 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07001704 throw e.rethrowFromSystemServer();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001705 }
1706 }
1707
1708 /**
1709 * Returns the value of the setting for background data usage. If false,
1710 * applications should not use the network if the application is not in the
1711 * foreground. Developers should respect this setting, and check the value
1712 * of this before performing any background data operations.
1713 * <p>
1714 * All applications that have background services that use the network
1715 * should listen to {@link #ACTION_BACKGROUND_DATA_SETTING_CHANGED}.
Jeff Sharkey3a844fc2011-08-16 14:37:57 -07001716 * <p>
Scott Main4cc53332011-10-06 18:32:43 -07001717 * @deprecated As of {@link VERSION_CODES#ICE_CREAM_SANDWICH}, availability of
Jeff Sharkey3a844fc2011-08-16 14:37:57 -07001718 * background data depends on several combined factors, and this method will
1719 * always return {@code true}. Instead, when background data is unavailable,
1720 * {@link #getActiveNetworkInfo()} will now appear disconnected.
Danica Chang6fdd0c62010-08-11 14:54:43 -07001721 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001722 * @return Whether background data usage is allowed.
1723 */
Jeff Sharkey3a844fc2011-08-16 14:37:57 -07001724 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001725 public boolean getBackgroundDataSetting() {
Jeff Sharkey3a844fc2011-08-16 14:37:57 -07001726 // assume that background data is allowed; final authority is
1727 // NetworkInfo which may be blocked.
1728 return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001729 }
1730
1731 /**
1732 * Sets the value of the setting for background data usage.
Robert Greenwaltc03fa502010-02-23 18:58:05 -08001733 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001734 * @param allowBackgroundData Whether an application should use data while
1735 * it is in the background.
Robert Greenwaltc03fa502010-02-23 18:58:05 -08001736 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001737 * @attr ref android.Manifest.permission#CHANGE_BACKGROUND_DATA_SETTING
1738 * @see #getBackgroundDataSetting()
1739 * @hide
1740 */
Jeff Sharkey3a844fc2011-08-16 14:37:57 -07001741 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001742 public void setBackgroundDataSetting(boolean allowBackgroundData) {
Jeff Sharkey3a844fc2011-08-16 14:37:57 -07001743 // ignored
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001744 }
Robert Greenwaltc03fa502010-02-23 18:58:05 -08001745
Jeff Sharkey43d2a172017-07-12 10:50:42 -06001746 /** {@hide} */
1747 @Deprecated
Jeff Sharkeyf0ceede2011-08-02 17:22:34 -07001748 public NetworkQuotaInfo getActiveNetworkQuotaInfo() {
1749 try {
1750 return mService.getActiveNetworkQuotaInfo();
1751 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07001752 throw e.rethrowFromSystemServer();
Jeff Sharkeyf0ceede2011-08-02 17:22:34 -07001753 }
1754 }
1755
1756 /**
Robert Greenwaltc03fa502010-02-23 18:58:05 -08001757 * @hide
Robert Greenwaltafa05c02014-05-21 20:04:36 -07001758 * @deprecated Talk to TelephonyManager directly
Robert Greenwaltc03fa502010-02-23 18:58:05 -08001759 */
Aurimas Liutikas514c5ef2016-05-24 15:22:55 -07001760 @Deprecated
Robert Greenwaltc03fa502010-02-23 18:58:05 -08001761 public boolean getMobileDataEnabled() {
Robert Greenwaltafa05c02014-05-21 20:04:36 -07001762 IBinder b = ServiceManager.getService(Context.TELEPHONY_SERVICE);
1763 if (b != null) {
1764 try {
1765 ITelephony it = ITelephony.Stub.asInterface(b);
Shishir Agrawal7ea3e8b2016-01-25 13:03:07 -08001766 int subId = SubscriptionManager.getDefaultDataSubscriptionId();
Wink Saville36ffb042014-12-05 11:10:30 -08001767 Log.d("ConnectivityManager", "getMobileDataEnabled()+ subId=" + subId);
1768 boolean retVal = it.getDataEnabled(subId);
1769 Log.d("ConnectivityManager", "getMobileDataEnabled()- subId=" + subId
1770 + " retVal=" + retVal);
1771 return retVal;
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07001772 } catch (RemoteException e) {
1773 throw e.rethrowFromSystemServer();
1774 }
Robert Greenwaltc03fa502010-02-23 18:58:05 -08001775 }
Wink Saville36ffb042014-12-05 11:10:30 -08001776 Log.d("ConnectivityManager", "getMobileDataEnabled()- remote exception retVal=false");
Robert Greenwaltafa05c02014-05-21 20:04:36 -07001777 return false;
Robert Greenwaltc03fa502010-02-23 18:58:05 -08001778 }
1779
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001780 /**
Robert Greenwaltb2489872014-09-04 16:44:35 -07001781 * Callback for use with {@link ConnectivityManager#addDefaultNetworkActiveListener}
Robert Greenwalt6078b502014-06-11 16:05:07 -07001782 * to find out when the system default network has gone in to a high power state.
Dianne Hackborn77b987f2014-02-26 16:20:52 -08001783 */
1784 public interface OnNetworkActiveListener {
1785 /**
1786 * Called on the main thread of the process to report that the current data network
1787 * has become active, and it is now a good time to perform any pending network
1788 * operations. Note that this listener only tells you when the network becomes
1789 * active; if at any other time you want to know whether it is active (and thus okay
1790 * to initiate network traffic), you can retrieve its instantaneous state with
Robert Greenwalt6078b502014-06-11 16:05:07 -07001791 * {@link ConnectivityManager#isDefaultNetworkActive}.
Dianne Hackborn77b987f2014-02-26 16:20:52 -08001792 */
1793 public void onNetworkActive();
1794 }
1795
1796 private INetworkManagementService getNetworkManagementService() {
1797 synchronized (this) {
1798 if (mNMService != null) {
1799 return mNMService;
1800 }
1801 IBinder b = ServiceManager.getService(Context.NETWORKMANAGEMENT_SERVICE);
1802 mNMService = INetworkManagementService.Stub.asInterface(b);
1803 return mNMService;
1804 }
1805 }
1806
1807 private final ArrayMap<OnNetworkActiveListener, INetworkActivityListener>
1808 mNetworkActivityListeners
1809 = new ArrayMap<OnNetworkActiveListener, INetworkActivityListener>();
1810
1811 /**
Robert Greenwalt6078b502014-06-11 16:05:07 -07001812 * Start listening to reports when the system's default data network is active, meaning it is
1813 * a good time to perform network traffic. Use {@link #isDefaultNetworkActive()}
1814 * to determine the current state of the system's default network after registering the
1815 * listener.
1816 * <p>
1817 * If the process default network has been set with
Paul Jensen72db88e2015-03-10 10:54:12 -04001818 * {@link ConnectivityManager#bindProcessToNetwork} this function will not
Robert Greenwalt6078b502014-06-11 16:05:07 -07001819 * reflect the process's default, but the system default.
Dianne Hackborn77b987f2014-02-26 16:20:52 -08001820 *
1821 * @param l The listener to be told when the network is active.
1822 */
Robert Greenwaltb2489872014-09-04 16:44:35 -07001823 public void addDefaultNetworkActiveListener(final OnNetworkActiveListener l) {
Dianne Hackborn77b987f2014-02-26 16:20:52 -08001824 INetworkActivityListener rl = new INetworkActivityListener.Stub() {
1825 @Override
1826 public void onNetworkActive() throws RemoteException {
1827 l.onNetworkActive();
1828 }
1829 };
1830
1831 try {
1832 getNetworkManagementService().registerNetworkActivityListener(rl);
1833 mNetworkActivityListeners.put(l, rl);
1834 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07001835 throw e.rethrowFromSystemServer();
Dianne Hackborn77b987f2014-02-26 16:20:52 -08001836 }
1837 }
1838
1839 /**
1840 * Remove network active listener previously registered with
Robert Greenwaltb2489872014-09-04 16:44:35 -07001841 * {@link #addDefaultNetworkActiveListener}.
Dianne Hackborn77b987f2014-02-26 16:20:52 -08001842 *
1843 * @param l Previously registered listener.
1844 */
Robert Greenwaltb2489872014-09-04 16:44:35 -07001845 public void removeDefaultNetworkActiveListener(OnNetworkActiveListener l) {
Dianne Hackborn77b987f2014-02-26 16:20:52 -08001846 INetworkActivityListener rl = mNetworkActivityListeners.get(l);
Hugo Benichie7678512017-05-09 15:19:01 +09001847 Preconditions.checkArgument(rl != null, "Listener was not registered.");
Dianne Hackborn77b987f2014-02-26 16:20:52 -08001848 try {
1849 getNetworkManagementService().unregisterNetworkActivityListener(rl);
1850 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07001851 throw e.rethrowFromSystemServer();
Dianne Hackborn77b987f2014-02-26 16:20:52 -08001852 }
1853 }
1854
1855 /**
1856 * Return whether the data network is currently active. An active network means that
1857 * it is currently in a high power state for performing data transmission. On some
1858 * types of networks, it may be expensive to move and stay in such a state, so it is
1859 * more power efficient to batch network traffic together when the radio is already in
1860 * this state. This method tells you whether right now is currently a good time to
1861 * initiate network traffic, as the network is already active.
1862 */
Robert Greenwalt6078b502014-06-11 16:05:07 -07001863 public boolean isDefaultNetworkActive() {
Dianne Hackborn77b987f2014-02-26 16:20:52 -08001864 try {
1865 return getNetworkManagementService().isNetworkActive();
1866 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07001867 throw e.rethrowFromSystemServer();
Dianne Hackborn77b987f2014-02-26 16:20:52 -08001868 }
Dianne Hackborn77b987f2014-02-26 16:20:52 -08001869 }
1870
1871 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001872 * {@hide}
1873 */
Lorenzo Colittiffc42b02015-07-29 11:41:21 +09001874 public ConnectivityManager(Context context, IConnectivityManager service) {
Hugo Benichidafed3d2017-03-06 09:17:06 +09001875 mContext = Preconditions.checkNotNull(context, "missing context");
1876 mService = Preconditions.checkNotNull(service, "missing IConnectivityManager");
Paul Jensene0bef712014-12-10 15:12:18 -05001877 sInstance = this;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001878 }
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -08001879
Jeff Sharkey8fc27e82012-04-04 20:40:58 -07001880 /** {@hide} */
1881 public static ConnectivityManager from(Context context) {
1882 return (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
1883 }
1884
Lorenzo Colittifbe9b1a2016-07-28 17:14:11 +09001885 /* TODO: These permissions checks don't belong in client-side code. Move them to
1886 * services.jar, possibly in com.android.server.net. */
1887
1888 /** {@hide} */
1889 public static final boolean checkChangePermission(Context context) {
1890 int uid = Binder.getCallingUid();
1891 return Settings.checkAndNoteChangeNetworkStateOperation(context, uid, Settings
1892 .getPackageNameForUid(context, uid), false /* throwException */);
1893 }
1894
Lorenzo Colittid5427052015-10-15 16:29:00 +09001895 /** {@hide} */
1896 public static final void enforceChangePermission(Context context) {
1897 int uid = Binder.getCallingUid();
1898 Settings.checkAndNoteChangeNetworkStateOperation(context, uid, Settings
1899 .getPackageNameForUid(context, uid), true /* throwException */);
1900 }
1901
Tetsutoki Shiozawa335d2ed2016-03-16 23:30:57 +09001902 /** {@hide} */
1903 public static final void enforceTetherChangePermission(Context context, String callingPkg) {
Hugo Benichie7678512017-05-09 15:19:01 +09001904 Preconditions.checkNotNull(context, "Context cannot be null");
1905 Preconditions.checkNotNull(callingPkg, "callingPkg cannot be null");
Tetsutoki Shiozawa335d2ed2016-03-16 23:30:57 +09001906
Robert Greenwaltedb47662014-09-16 17:54:19 -07001907 if (context.getResources().getStringArray(
1908 com.android.internal.R.array.config_mobile_hotspot_provision_app).length == 2) {
1909 // Have a provisioning app - must only let system apps (which check this app)
1910 // turn on tethering
1911 context.enforceCallingOrSelfPermission(
Jeremy Kleind42209d2015-12-28 15:11:58 -08001912 android.Manifest.permission.TETHER_PRIVILEGED, "ConnectivityService");
Robert Greenwaltedb47662014-09-16 17:54:19 -07001913 } else {
Billy Laua7238a32015-08-01 12:45:02 +01001914 int uid = Binder.getCallingUid();
Tetsutoki Shiozawa335d2ed2016-03-16 23:30:57 +09001915 // If callingPkg's uid is not same as Binder.getCallingUid(),
1916 // AppOpsService throws SecurityException.
1917 Settings.checkAndNoteWriteSettingsOperation(context, uid, callingPkg,
1918 true /* throwException */);
Robert Greenwaltedb47662014-09-16 17:54:19 -07001919 }
1920 }
1921
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -08001922 /**
Paul Jensene0bef712014-12-10 15:12:18 -05001923 * @deprecated - use getSystemService. This is a kludge to support static access in certain
1924 * situations where a Context pointer is unavailable.
1925 * @hide
1926 */
Aurimas Liutikas514c5ef2016-05-24 15:22:55 -07001927 @Deprecated
Paul Jensen72db88e2015-03-10 10:54:12 -04001928 static ConnectivityManager getInstanceOrNull() {
1929 return sInstance;
1930 }
1931
1932 /**
1933 * @deprecated - use getSystemService. This is a kludge to support static access in certain
1934 * situations where a Context pointer is unavailable.
1935 * @hide
1936 */
Aurimas Liutikas514c5ef2016-05-24 15:22:55 -07001937 @Deprecated
Paul Jensen72db88e2015-03-10 10:54:12 -04001938 private static ConnectivityManager getInstance() {
1939 if (getInstanceOrNull() == null) {
Paul Jensene0bef712014-12-10 15:12:18 -05001940 throw new IllegalStateException("No ConnectivityManager yet constructed");
1941 }
Paul Jensen72db88e2015-03-10 10:54:12 -04001942 return getInstanceOrNull();
Paul Jensene0bef712014-12-10 15:12:18 -05001943 }
1944
1945 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08001946 * Get the set of tetherable, available interfaces. This list is limited by
1947 * device configuration and current interface existence.
1948 *
1949 * @return an array of 0 or more Strings of tetherable interface names.
1950 *
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -08001951 * {@hide}
1952 */
Jeff Sharkey30e06bb2017-04-24 11:18:03 -06001953 @RequiresPermission(android.Manifest.permission.ACCESS_NETWORK_STATE)
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -08001954 public String[] getTetherableIfaces() {
1955 try {
1956 return mService.getTetherableIfaces();
1957 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07001958 throw e.rethrowFromSystemServer();
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -08001959 }
1960 }
1961
1962 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08001963 * Get the set of tethered interfaces.
1964 *
1965 * @return an array of 0 or more String of currently tethered interface names.
1966 *
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -08001967 * {@hide}
1968 */
Jeff Sharkey30e06bb2017-04-24 11:18:03 -06001969 @RequiresPermission(android.Manifest.permission.ACCESS_NETWORK_STATE)
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -08001970 public String[] getTetheredIfaces() {
1971 try {
1972 return mService.getTetheredIfaces();
1973 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07001974 throw e.rethrowFromSystemServer();
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -08001975 }
1976 }
1977
1978 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08001979 * Get the set of interface names which attempted to tether but
1980 * failed. Re-attempting to tether may cause them to reset to the Tethered
1981 * state. Alternatively, causing the interface to be destroyed and recreated
1982 * may cause them to reset to the available state.
1983 * {@link ConnectivityManager#getLastTetherError} can be used to get more
1984 * information on the cause of the errors.
1985 *
1986 * @return an array of 0 or more String indicating the interface names
1987 * which failed to tether.
1988 *
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -08001989 * {@hide}
1990 */
Jeff Sharkey30e06bb2017-04-24 11:18:03 -06001991 @RequiresPermission(android.Manifest.permission.ACCESS_NETWORK_STATE)
Robert Greenwalt5a735062010-03-02 17:25:02 -08001992 public String[] getTetheringErroredIfaces() {
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -08001993 try {
Robert Greenwalt5a735062010-03-02 17:25:02 -08001994 return mService.getTetheringErroredIfaces();
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -08001995 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07001996 throw e.rethrowFromSystemServer();
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -08001997 }
1998 }
1999
2000 /**
Robert Greenwalt9c7e2c22014-06-23 14:53:42 -07002001 * Get the set of tethered dhcp ranges.
2002 *
2003 * @return an array of 0 or more {@code String} of tethered dhcp ranges.
2004 * {@hide}
2005 */
2006 public String[] getTetheredDhcpRanges() {
2007 try {
2008 return mService.getTetheredDhcpRanges();
2009 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07002010 throw e.rethrowFromSystemServer();
Robert Greenwalt9c7e2c22014-06-23 14:53:42 -07002011 }
2012 }
2013
2014 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08002015 * Attempt to tether the named interface. This will setup a dhcp server
2016 * on the interface, forward and NAT IP packets and forward DNS requests
2017 * to the best active upstream network interface. Note that if no upstream
2018 * IP network interface is available, dhcp will still run and traffic will be
2019 * allowed between the tethered devices and this device, though upstream net
2020 * access will of course fail until an upstream network interface becomes
2021 * active.
Lorenzo Colittid5427052015-10-15 16:29:00 +09002022 *
2023 * <p>This method requires the caller to hold either the
2024 * {@link android.Manifest.permission#CHANGE_NETWORK_STATE} permission
2025 * or the ability to modify system settings as determined by
2026 * {@link android.provider.Settings.System#canWrite}.</p>
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08002027 *
Jeremy Klein36c7aa02016-01-22 14:11:45 -08002028 * <p>WARNING: New clients should not use this function. The only usages should be in PanService
2029 * and WifiStateMachine which need direct access. All other clients should use
2030 * {@link #startTethering} and {@link #stopTethering} which encapsulate proper provisioning
2031 * logic.</p>
2032 *
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08002033 * @param iface the interface name to tether.
2034 * @return error a {@code TETHER_ERROR} value indicating success or failure type
2035 *
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -08002036 * {@hide}
2037 */
Robert Greenwalt5a735062010-03-02 17:25:02 -08002038 public int tether(String iface) {
2039 try {
Tetsutoki Shiozawa335d2ed2016-03-16 23:30:57 +09002040 String pkgName = mContext.getOpPackageName();
2041 Log.i(TAG, "tether caller:" + pkgName);
2042 return mService.tether(iface, pkgName);
Robert Greenwalt5a735062010-03-02 17:25:02 -08002043 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07002044 throw e.rethrowFromSystemServer();
Robert Greenwalt5a735062010-03-02 17:25:02 -08002045 }
2046 }
2047
2048 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08002049 * Stop tethering the named interface.
Lorenzo Colittid5427052015-10-15 16:29:00 +09002050 *
2051 * <p>This method requires the caller to hold either the
2052 * {@link android.Manifest.permission#CHANGE_NETWORK_STATE} permission
2053 * or the ability to modify system settings as determined by
2054 * {@link android.provider.Settings.System#canWrite}.</p>
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08002055 *
Jeremy Klein36c7aa02016-01-22 14:11:45 -08002056 * <p>WARNING: New clients should not use this function. The only usages should be in PanService
2057 * and WifiStateMachine which need direct access. All other clients should use
2058 * {@link #startTethering} and {@link #stopTethering} which encapsulate proper provisioning
2059 * logic.</p>
2060 *
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08002061 * @param iface the interface name to untether.
2062 * @return error a {@code TETHER_ERROR} value indicating success or failure type
2063 *
Robert Greenwalt5a735062010-03-02 17:25:02 -08002064 * {@hide}
2065 */
2066 public int untether(String iface) {
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -08002067 try {
Tetsutoki Shiozawa335d2ed2016-03-16 23:30:57 +09002068 String pkgName = mContext.getOpPackageName();
2069 Log.i(TAG, "untether caller:" + pkgName);
2070 return mService.untether(iface, pkgName);
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -08002071 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07002072 throw e.rethrowFromSystemServer();
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -08002073 }
2074 }
Robert Greenwalt2a091d72010-02-11 18:18:40 -08002075
2076 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08002077 * Check if the device allows for tethering. It may be disabled via
Dianne Hackborn77b987f2014-02-26 16:20:52 -08002078 * {@code ro.tether.denied} system property, Settings.TETHER_SUPPORTED or
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08002079 * due to device configuration.
2080 *
Chalard Jean8f76fc32017-09-26 15:45:18 +09002081 * <p>If this app does not have permission to use this API, it will always
2082 * return false rather than throw an exception.</p>
2083 *
2084 * <p>If the device has a hotspot provisioning app, the caller is required to hold the
2085 * {@link android.Manifest.permission.TETHER_PRIVILEGED} permission.</p>
2086 *
2087 * <p>Otherwise, this method requires the caller to hold the ability to modify system
2088 * settings as determined by {@link android.provider.Settings.System#canWrite}.</p>
2089 *
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08002090 * @return a boolean - {@code true} indicating Tethering is supported.
2091 *
Robert Greenwalt2a091d72010-02-11 18:18:40 -08002092 * {@hide}
2093 */
Jeremy Klein36c7aa02016-01-22 14:11:45 -08002094 @SystemApi
Chalard Jean8f76fc32017-09-26 15:45:18 +09002095 @RequiresPermission(anyOf = {android.Manifest.permission.TETHER_PRIVILEGED,
2096 android.Manifest.permission.WRITE_SETTINGS})
Robert Greenwalt2a091d72010-02-11 18:18:40 -08002097 public boolean isTetheringSupported() {
Chalard Jean8f76fc32017-09-26 15:45:18 +09002098 String pkgName = mContext.getOpPackageName();
Robert Greenwalt2a091d72010-02-11 18:18:40 -08002099 try {
Udam Saini0e94c362017-06-07 12:06:28 -07002100 return mService.isTetheringSupported(pkgName);
Chalard Jean8f76fc32017-09-26 15:45:18 +09002101 } catch (SecurityException e) {
2102 // This API is not available to this caller, but for backward-compatibility
2103 // this will just return false instead of throwing.
2104 return false;
Robert Greenwalt2a091d72010-02-11 18:18:40 -08002105 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07002106 throw e.rethrowFromSystemServer();
Robert Greenwalt2a091d72010-02-11 18:18:40 -08002107 }
2108 }
2109
2110 /**
Jeremy Klein36c7aa02016-01-22 14:11:45 -08002111 * Callback for use with {@link #startTethering} to find out whether tethering succeeded.
2112 * @hide
2113 */
2114 @SystemApi
2115 public static abstract class OnStartTetheringCallback {
2116 /**
2117 * Called when tethering has been successfully started.
2118 */
2119 public void onTetheringStarted() {};
2120
2121 /**
2122 * Called when starting tethering failed.
2123 */
2124 public void onTetheringFailed() {};
2125 }
2126
2127 /**
2128 * Convenient overload for
2129 * {@link #startTethering(int, boolean, OnStartTetheringCallback, Handler)} which passes a null
2130 * handler to run on the current thread's {@link Looper}.
2131 * @hide
2132 */
2133 @SystemApi
Udam Saini0e94c362017-06-07 12:06:28 -07002134 @RequiresPermission(android.Manifest.permission.TETHER_PRIVILEGED)
Jeremy Klein36c7aa02016-01-22 14:11:45 -08002135 public void startTethering(int type, boolean showProvisioningUi,
2136 final OnStartTetheringCallback callback) {
2137 startTethering(type, showProvisioningUi, callback, null);
2138 }
2139
2140 /**
2141 * Runs tether provisioning for the given type if needed and then starts tethering if
2142 * the check succeeds. If no carrier provisioning is required for tethering, tethering is
2143 * enabled immediately. If provisioning fails, tethering will not be enabled. It also
2144 * schedules tether provisioning re-checks if appropriate.
2145 *
2146 * @param type The type of tethering to start. Must be one of
2147 * {@link ConnectivityManager.TETHERING_WIFI},
2148 * {@link ConnectivityManager.TETHERING_USB}, or
2149 * {@link ConnectivityManager.TETHERING_BLUETOOTH}.
2150 * @param showProvisioningUi a boolean indicating to show the provisioning app UI if there
2151 * is one. This should be true the first time this function is called and also any time
2152 * the user can see this UI. It gives users information from their carrier about the
2153 * check failing and how they can sign up for tethering if possible.
2154 * @param callback an {@link OnStartTetheringCallback} which will be called to notify the caller
2155 * of the result of trying to tether.
2156 * @param handler {@link Handler} to specify the thread upon which the callback will be invoked.
2157 * @hide
2158 */
2159 @SystemApi
Jeff Sharkeyd86b8fe2017-06-02 17:36:26 -06002160 @RequiresPermission(android.Manifest.permission.TETHER_PRIVILEGED)
Jeremy Klein36c7aa02016-01-22 14:11:45 -08002161 public void startTethering(int type, boolean showProvisioningUi,
2162 final OnStartTetheringCallback callback, Handler handler) {
Hugo Benichidafed3d2017-03-06 09:17:06 +09002163 Preconditions.checkNotNull(callback, "OnStartTetheringCallback cannot be null.");
Jeremy Klein5f277e12016-03-12 16:29:54 -08002164
Jeremy Klein36c7aa02016-01-22 14:11:45 -08002165 ResultReceiver wrappedCallback = new ResultReceiver(handler) {
2166 @Override
2167 protected void onReceiveResult(int resultCode, Bundle resultData) {
2168 if (resultCode == TETHER_ERROR_NO_ERROR) {
2169 callback.onTetheringStarted();
2170 } else {
2171 callback.onTetheringFailed();
2172 }
2173 }
2174 };
Jeremy Klein5f277e12016-03-12 16:29:54 -08002175
Jeremy Klein36c7aa02016-01-22 14:11:45 -08002176 try {
Tetsutoki Shiozawa335d2ed2016-03-16 23:30:57 +09002177 String pkgName = mContext.getOpPackageName();
2178 Log.i(TAG, "startTethering caller:" + pkgName);
2179 mService.startTethering(type, wrappedCallback, showProvisioningUi, pkgName);
Jeremy Klein36c7aa02016-01-22 14:11:45 -08002180 } catch (RemoteException e) {
2181 Log.e(TAG, "Exception trying to start tethering.", e);
2182 wrappedCallback.send(TETHER_ERROR_SERVICE_UNAVAIL, null);
2183 }
2184 }
2185
2186 /**
2187 * Stops tethering for the given type. Also cancels any provisioning rechecks for that type if
2188 * applicable.
2189 *
2190 * @param type The type of tethering to stop. Must be one of
2191 * {@link ConnectivityManager.TETHERING_WIFI},
2192 * {@link ConnectivityManager.TETHERING_USB}, or
2193 * {@link ConnectivityManager.TETHERING_BLUETOOTH}.
2194 * @hide
2195 */
2196 @SystemApi
Jeff Sharkeyd86b8fe2017-06-02 17:36:26 -06002197 @RequiresPermission(android.Manifest.permission.TETHER_PRIVILEGED)
Jeremy Klein36c7aa02016-01-22 14:11:45 -08002198 public void stopTethering(int type) {
2199 try {
Tetsutoki Shiozawa335d2ed2016-03-16 23:30:57 +09002200 String pkgName = mContext.getOpPackageName();
2201 Log.i(TAG, "stopTethering caller:" + pkgName);
2202 mService.stopTethering(type, pkgName);
Jeremy Klein36c7aa02016-01-22 14:11:45 -08002203 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07002204 throw e.rethrowFromSystemServer();
Jeremy Klein36c7aa02016-01-22 14:11:45 -08002205 }
2206 }
2207
2208 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08002209 * Get the list of regular expressions that define any tetherable
2210 * USB network interfaces. If USB tethering is not supported by the
2211 * device, this list should be empty.
2212 *
2213 * @return an array of 0 or more regular expression Strings defining
2214 * what interfaces are considered tetherable usb interfaces.
2215 *
Robert Greenwalt2a091d72010-02-11 18:18:40 -08002216 * {@hide}
2217 */
Jeff Sharkey30e06bb2017-04-24 11:18:03 -06002218 @RequiresPermission(android.Manifest.permission.ACCESS_NETWORK_STATE)
Robert Greenwalt2a091d72010-02-11 18:18:40 -08002219 public String[] getTetherableUsbRegexs() {
2220 try {
2221 return mService.getTetherableUsbRegexs();
2222 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07002223 throw e.rethrowFromSystemServer();
Robert Greenwalt2a091d72010-02-11 18:18:40 -08002224 }
2225 }
2226
2227 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08002228 * Get the list of regular expressions that define any tetherable
2229 * Wifi network interfaces. If Wifi tethering is not supported by the
2230 * device, this list should be empty.
2231 *
2232 * @return an array of 0 or more regular expression Strings defining
2233 * what interfaces are considered tetherable wifi interfaces.
2234 *
Robert Greenwalt2a091d72010-02-11 18:18:40 -08002235 * {@hide}
2236 */
Jeff Sharkey30e06bb2017-04-24 11:18:03 -06002237 @RequiresPermission(android.Manifest.permission.ACCESS_NETWORK_STATE)
Robert Greenwalt2a091d72010-02-11 18:18:40 -08002238 public String[] getTetherableWifiRegexs() {
2239 try {
2240 return mService.getTetherableWifiRegexs();
2241 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07002242 throw e.rethrowFromSystemServer();
Robert Greenwalt2a091d72010-02-11 18:18:40 -08002243 }
2244 }
Robert Greenwalt5a735062010-03-02 17:25:02 -08002245
Danica Chang6fdd0c62010-08-11 14:54:43 -07002246 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08002247 * Get the list of regular expressions that define any tetherable
2248 * Bluetooth network interfaces. If Bluetooth tethering is not supported by the
2249 * device, this list should be empty.
2250 *
2251 * @return an array of 0 or more regular expression Strings defining
2252 * what interfaces are considered tetherable bluetooth interfaces.
2253 *
Danica Chang6fdd0c62010-08-11 14:54:43 -07002254 * {@hide}
2255 */
Jeff Sharkey30e06bb2017-04-24 11:18:03 -06002256 @RequiresPermission(android.Manifest.permission.ACCESS_NETWORK_STATE)
Danica Chang6fdd0c62010-08-11 14:54:43 -07002257 public String[] getTetherableBluetoothRegexs() {
2258 try {
2259 return mService.getTetherableBluetoothRegexs();
2260 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07002261 throw e.rethrowFromSystemServer();
Danica Chang6fdd0c62010-08-11 14:54:43 -07002262 }
2263 }
2264
Mike Lockwood6c2260b2011-07-19 13:04:47 -07002265 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08002266 * Attempt to both alter the mode of USB and Tethering of USB. A
2267 * utility method to deal with some of the complexity of USB - will
2268 * attempt to switch to Rndis and subsequently tether the resulting
2269 * interface on {@code true} or turn off tethering and switch off
2270 * Rndis on {@code false}.
Lorenzo Colittid5427052015-10-15 16:29:00 +09002271 *
2272 * <p>This method requires the caller to hold either the
2273 * {@link android.Manifest.permission#CHANGE_NETWORK_STATE} permission
2274 * or the ability to modify system settings as determined by
2275 * {@link android.provider.Settings.System#canWrite}.</p>
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08002276 *
2277 * @param enable a boolean - {@code true} to enable tethering
2278 * @return error a {@code TETHER_ERROR} value indicating success or failure type
2279 *
Mike Lockwood6c2260b2011-07-19 13:04:47 -07002280 * {@hide}
2281 */
2282 public int setUsbTethering(boolean enable) {
2283 try {
Tetsutoki Shiozawa335d2ed2016-03-16 23:30:57 +09002284 String pkgName = mContext.getOpPackageName();
2285 Log.i(TAG, "setUsbTethering caller:" + pkgName);
2286 return mService.setUsbTethering(enable, pkgName);
Mike Lockwood6c2260b2011-07-19 13:04:47 -07002287 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07002288 throw e.rethrowFromSystemServer();
Mike Lockwood6c2260b2011-07-19 13:04:47 -07002289 }
2290 }
2291
Robert Greenwalt5a735062010-03-02 17:25:02 -08002292 /** {@hide} */
2293 public static final int TETHER_ERROR_NO_ERROR = 0;
2294 /** {@hide} */
2295 public static final int TETHER_ERROR_UNKNOWN_IFACE = 1;
2296 /** {@hide} */
2297 public static final int TETHER_ERROR_SERVICE_UNAVAIL = 2;
2298 /** {@hide} */
2299 public static final int TETHER_ERROR_UNSUPPORTED = 3;
2300 /** {@hide} */
2301 public static final int TETHER_ERROR_UNAVAIL_IFACE = 4;
2302 /** {@hide} */
2303 public static final int TETHER_ERROR_MASTER_ERROR = 5;
2304 /** {@hide} */
2305 public static final int TETHER_ERROR_TETHER_IFACE_ERROR = 6;
2306 /** {@hide} */
2307 public static final int TETHER_ERROR_UNTETHER_IFACE_ERROR = 7;
2308 /** {@hide} */
2309 public static final int TETHER_ERROR_ENABLE_NAT_ERROR = 8;
2310 /** {@hide} */
2311 public static final int TETHER_ERROR_DISABLE_NAT_ERROR = 9;
2312 /** {@hide} */
2313 public static final int TETHER_ERROR_IFACE_CFG_ERROR = 10;
Jeremy Klein36c7aa02016-01-22 14:11:45 -08002314 /** {@hide} */
2315 public static final int TETHER_ERROR_PROVISION_FAILED = 11;
Robert Greenwalt5a735062010-03-02 17:25:02 -08002316
2317 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08002318 * Get a more detailed error code after a Tethering or Untethering
2319 * request asynchronously failed.
2320 *
2321 * @param iface The name of the interface of interest
Robert Greenwalt5a735062010-03-02 17:25:02 -08002322 * @return error The error code of the last error tethering or untethering the named
2323 * interface
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08002324 *
Robert Greenwalt5a735062010-03-02 17:25:02 -08002325 * {@hide}
2326 */
Jeff Sharkey30e06bb2017-04-24 11:18:03 -06002327 @RequiresPermission(android.Manifest.permission.ACCESS_NETWORK_STATE)
Robert Greenwalt5a735062010-03-02 17:25:02 -08002328 public int getLastTetherError(String iface) {
2329 try {
2330 return mService.getLastTetherError(iface);
2331 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07002332 throw e.rethrowFromSystemServer();
Robert Greenwalt5a735062010-03-02 17:25:02 -08002333 }
Robert Greenwalt14f2ef42010-06-15 12:19:37 -07002334 }
2335
2336 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08002337 * Report network connectivity status. This is currently used only
2338 * to alter status bar UI.
Paul Jensenb2748922015-05-06 11:10:18 -04002339 * <p>This method requires the caller to hold the permission
2340 * {@link android.Manifest.permission#STATUS_BAR}.
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08002341 *
Robert Greenwaltd7085fc2010-09-08 15:24:47 -07002342 * @param networkType The type of network you want to report on
2343 * @param percentage The quality of the connection 0 is bad, 100 is good
2344 * {@hide}
2345 */
2346 public void reportInetCondition(int networkType, int percentage) {
2347 try {
2348 mService.reportInetCondition(networkType, percentage);
2349 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07002350 throw e.rethrowFromSystemServer();
Robert Greenwaltd7085fc2010-09-08 15:24:47 -07002351 }
2352 }
Robert Greenwalt434203a2010-10-11 16:00:27 -07002353
2354 /**
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07002355 * Report a problem network to the framework. This provides a hint to the system
Ye Wenb87875e2014-07-21 14:19:01 -07002356 * that there might be connectivity problems on this network and may cause
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07002357 * the framework to re-evaluate network connectivity and/or switch to another
2358 * network.
Robert Greenwalt9258c642014-03-26 16:47:06 -07002359 *
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07002360 * @param network The {@link Network} the application was attempting to use
2361 * or {@code null} to indicate the current default network.
Paul Jensenbfd17b72015-04-07 12:43:13 -04002362 * @deprecated Use {@link #reportNetworkConnectivity} which allows reporting both
2363 * working and non-working connectivity.
Robert Greenwalt9258c642014-03-26 16:47:06 -07002364 */
Aurimas Liutikas514c5ef2016-05-24 15:22:55 -07002365 @Deprecated
Robert Greenwalt9258c642014-03-26 16:47:06 -07002366 public void reportBadNetwork(Network network) {
2367 try {
Paul Jensenbfd17b72015-04-07 12:43:13 -04002368 // One of these will be ignored because it matches system's current state.
2369 // The other will trigger the necessary reevaluation.
2370 mService.reportNetworkConnectivity(network, true);
2371 mService.reportNetworkConnectivity(network, false);
2372 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07002373 throw e.rethrowFromSystemServer();
Paul Jensenbfd17b72015-04-07 12:43:13 -04002374 }
2375 }
2376
2377 /**
2378 * Report to the framework whether a network has working connectivity.
2379 * This provides a hint to the system that a particular network is providing
2380 * working connectivity or not. In response the framework may re-evaluate
2381 * the network's connectivity and might take further action thereafter.
2382 *
2383 * @param network The {@link Network} the application was attempting to use
2384 * or {@code null} to indicate the current default network.
2385 * @param hasConnectivity {@code true} if the application was able to successfully access the
2386 * Internet using {@code network} or {@code false} if not.
2387 */
2388 public void reportNetworkConnectivity(Network network, boolean hasConnectivity) {
2389 try {
2390 mService.reportNetworkConnectivity(network, hasConnectivity);
Robert Greenwalt9258c642014-03-26 16:47:06 -07002391 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07002392 throw e.rethrowFromSystemServer();
Robert Greenwalt9258c642014-03-26 16:47:06 -07002393 }
2394 }
2395
2396 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08002397 * Set a network-independent global http proxy. This is not normally what you want
2398 * for typical HTTP proxies - they are general network dependent. However if you're
2399 * doing something unusual like general internal filtering this may be useful. On
2400 * a private network where the proxy is not accessible, you may break HTTP using this.
Paul Jensenb2748922015-05-06 11:10:18 -04002401 *
2402 * @param p A {@link ProxyInfo} object defining the new global
2403 * HTTP proxy. A {@code null} value will clear the global HTTP proxy.
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07002404 * @hide
Robert Greenwalt434203a2010-10-11 16:00:27 -07002405 */
Jeff Sharkey30e06bb2017-04-24 11:18:03 -06002406 @RequiresPermission(android.Manifest.permission.CONNECTIVITY_INTERNAL)
Jason Monk207900c2014-04-25 15:00:09 -04002407 public void setGlobalProxy(ProxyInfo p) {
Robert Greenwalt434203a2010-10-11 16:00:27 -07002408 try {
2409 mService.setGlobalProxy(p);
2410 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07002411 throw e.rethrowFromSystemServer();
Robert Greenwalt434203a2010-10-11 16:00:27 -07002412 }
2413 }
2414
2415 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08002416 * Retrieve any network-independent global HTTP proxy.
2417 *
Jason Monk207900c2014-04-25 15:00:09 -04002418 * @return {@link ProxyInfo} for the current global HTTP proxy or {@code null}
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08002419 * if no global HTTP proxy is set.
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07002420 * @hide
Robert Greenwalt434203a2010-10-11 16:00:27 -07002421 */
Jason Monk207900c2014-04-25 15:00:09 -04002422 public ProxyInfo getGlobalProxy() {
Robert Greenwalt434203a2010-10-11 16:00:27 -07002423 try {
2424 return mService.getGlobalProxy();
2425 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07002426 throw e.rethrowFromSystemServer();
Robert Greenwalt434203a2010-10-11 16:00:27 -07002427 }
2428 }
2429
2430 /**
Paul Jensencee9b512015-05-06 07:32:40 -04002431 * Retrieve the global HTTP proxy, or if no global HTTP proxy is set, a
2432 * network-specific HTTP proxy. If {@code network} is null, the
2433 * network-specific proxy returned is the proxy of the default active
2434 * network.
2435 *
2436 * @return {@link ProxyInfo} for the current global HTTP proxy, or if no
2437 * global HTTP proxy is set, {@code ProxyInfo} for {@code network},
2438 * or when {@code network} is {@code null},
2439 * the {@code ProxyInfo} for the default active network. Returns
2440 * {@code null} when no proxy applies or the caller doesn't have
2441 * permission to use {@code network}.
2442 * @hide
2443 */
2444 public ProxyInfo getProxyForNetwork(Network network) {
2445 try {
2446 return mService.getProxyForNetwork(network);
2447 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07002448 throw e.rethrowFromSystemServer();
Paul Jensencee9b512015-05-06 07:32:40 -04002449 }
2450 }
2451
2452 /**
Paul Jensene0bef712014-12-10 15:12:18 -05002453 * Get the current default HTTP proxy settings. If a global proxy is set it will be returned,
2454 * otherwise if this process is bound to a {@link Network} using
Paul Jensen72db88e2015-03-10 10:54:12 -04002455 * {@link #bindProcessToNetwork} then that {@code Network}'s proxy is returned, otherwise
Paul Jensene0bef712014-12-10 15:12:18 -05002456 * the default network's proxy is returned.
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08002457 *
Jason Monk207900c2014-04-25 15:00:09 -04002458 * @return the {@link ProxyInfo} for the current HTTP proxy, or {@code null} if no
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08002459 * HTTP proxy is active.
Robert Greenwalt434203a2010-10-11 16:00:27 -07002460 */
Paul Jensene0bef712014-12-10 15:12:18 -05002461 public ProxyInfo getDefaultProxy() {
Paul Jensencee9b512015-05-06 07:32:40 -04002462 return getProxyForNetwork(getBoundNetworkForProcess());
Robert Greenwalt434203a2010-10-11 16:00:27 -07002463 }
Robert Greenwaltd55a6b42011-03-25 13:09:25 -07002464
2465 /**
Robert Greenwalt9b2886e2011-08-31 11:46:42 -07002466 * Returns true if the hardware supports the given network type
2467 * else it returns false. This doesn't indicate we have coverage
2468 * or are authorized onto a network, just whether or not the
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08002469 * hardware supports it. For example a GSM phone without a SIM
2470 * should still return {@code true} for mobile data, but a wifi only
2471 * tablet would return {@code false}.
2472 *
2473 * @param networkType The network type we'd like to check
2474 * @return {@code true} if supported, else {@code false}
2475 *
Robert Greenwalt9b2886e2011-08-31 11:46:42 -07002476 * @hide
2477 */
Jeff Sharkey30e06bb2017-04-24 11:18:03 -06002478 @RequiresPermission(android.Manifest.permission.ACCESS_NETWORK_STATE)
Robert Greenwalt9b2886e2011-08-31 11:46:42 -07002479 public boolean isNetworkSupported(int networkType) {
2480 try {
2481 return mService.isNetworkSupported(networkType);
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07002482 } catch (RemoteException e) {
2483 throw e.rethrowFromSystemServer();
2484 }
Robert Greenwalt9b2886e2011-08-31 11:46:42 -07002485 }
Jeff Sharkey9f7cbf02012-04-12 18:34:54 -07002486
2487 /**
2488 * Returns if the currently active data network is metered. A network is
2489 * classified as metered when the user is sensitive to heavy data usage on
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08002490 * that connection due to monetary costs, data limitations or
2491 * battery/performance issues. You should check this before doing large
2492 * data transfers, and warn the user or delay the operation until another
2493 * network is available.
2494 *
2495 * @return {@code true} if large transfers should be avoided, otherwise
2496 * {@code false}.
Jeff Sharkey9f7cbf02012-04-12 18:34:54 -07002497 */
Jeff Sharkey30e06bb2017-04-24 11:18:03 -06002498 @RequiresPermission(android.Manifest.permission.ACCESS_NETWORK_STATE)
Jeff Sharkey9f7cbf02012-04-12 18:34:54 -07002499 public boolean isActiveNetworkMetered() {
2500 try {
2501 return mService.isActiveNetworkMetered();
2502 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07002503 throw e.rethrowFromSystemServer();
Jeff Sharkey9f7cbf02012-04-12 18:34:54 -07002504 }
2505 }
Jeff Sharkey69ddab42012-08-25 00:05:46 -07002506
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08002507 /**
2508 * If the LockdownVpn mechanism is enabled, updates the vpn
2509 * with a reload of its profile.
2510 *
2511 * @return a boolean with {@code} indicating success
2512 *
2513 * <p>This method can only be called by the system UID
2514 * {@hide}
2515 */
Jeff Sharkey69ddab42012-08-25 00:05:46 -07002516 public boolean updateLockdownVpn() {
2517 try {
2518 return mService.updateLockdownVpn();
2519 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07002520 throw e.rethrowFromSystemServer();
Jeff Sharkey69ddab42012-08-25 00:05:46 -07002521 }
2522 }
Irfan Sheriffda6da092012-08-16 12:49:23 -07002523
2524 /**
Wink Saville948282b2013-08-29 08:55:16 -07002525 * Check mobile provisioning.
Wink Savilleab9321d2013-06-29 21:10:57 -07002526 *
Wink Savilleab9321d2013-06-29 21:10:57 -07002527 * @param suggestedTimeOutMs, timeout in milliseconds
Wink Savilleab9321d2013-06-29 21:10:57 -07002528 *
2529 * @return time out that will be used, maybe less that suggestedTimeOutMs
2530 * -1 if an error.
2531 *
2532 * {@hide}
2533 */
Wink Saville948282b2013-08-29 08:55:16 -07002534 public int checkMobileProvisioning(int suggestedTimeOutMs) {
Wink Savilleab9321d2013-06-29 21:10:57 -07002535 int timeOutMs = -1;
2536 try {
Wink Saville948282b2013-08-29 08:55:16 -07002537 timeOutMs = mService.checkMobileProvisioning(suggestedTimeOutMs);
Wink Savilleab9321d2013-06-29 21:10:57 -07002538 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07002539 throw e.rethrowFromSystemServer();
Wink Savilleab9321d2013-06-29 21:10:57 -07002540 }
2541 return timeOutMs;
2542 }
Robert Greenwalte182bfe2013-07-16 12:06:09 -07002543
2544 /**
Wink Saville42d4f082013-07-20 20:31:59 -07002545 * Get the mobile provisioning url.
Robert Greenwalte182bfe2013-07-16 12:06:09 -07002546 * {@hide}
2547 */
2548 public String getMobileProvisioningUrl() {
2549 try {
2550 return mService.getMobileProvisioningUrl();
2551 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07002552 throw e.rethrowFromSystemServer();
Robert Greenwalte182bfe2013-07-16 12:06:09 -07002553 }
Robert Greenwalte182bfe2013-07-16 12:06:09 -07002554 }
Wink Saville42d4f082013-07-20 20:31:59 -07002555
2556 /**
Wink Saville948282b2013-08-29 08:55:16 -07002557 * Set sign in error notification to visible or in visible
2558 *
2559 * @param visible
2560 * @param networkType
2561 *
2562 * {@hide}
Paul Jensen3541e9f2015-03-18 12:23:02 -04002563 * @deprecated Doesn't properly deal with multiple connected networks of the same type.
Wink Saville948282b2013-08-29 08:55:16 -07002564 */
Aurimas Liutikas514c5ef2016-05-24 15:22:55 -07002565 @Deprecated
Wink Saville948282b2013-08-29 08:55:16 -07002566 public void setProvisioningNotificationVisible(boolean visible, int networkType,
Paul Jensen89e0f092014-09-15 15:59:36 -04002567 String action) {
Wink Saville948282b2013-08-29 08:55:16 -07002568 try {
Paul Jensen89e0f092014-09-15 15:59:36 -04002569 mService.setProvisioningNotificationVisible(visible, networkType, action);
Wink Saville948282b2013-08-29 08:55:16 -07002570 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07002571 throw e.rethrowFromSystemServer();
Wink Saville948282b2013-08-29 08:55:16 -07002572 }
2573 }
Yuhao Zheng5cd1a0e2013-09-09 17:00:04 -07002574
2575 /**
2576 * Set the value for enabling/disabling airplane mode
2577 *
2578 * @param enable whether to enable airplane mode or not
2579 *
Yuhao Zheng5cd1a0e2013-09-09 17:00:04 -07002580 * @hide
2581 */
Jeff Sharkey30e06bb2017-04-24 11:18:03 -06002582 @RequiresPermission(android.Manifest.permission.CONNECTIVITY_INTERNAL)
Yuhao Zheng5cd1a0e2013-09-09 17:00:04 -07002583 public void setAirplaneMode(boolean enable) {
2584 try {
2585 mService.setAirplaneMode(enable);
2586 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07002587 throw e.rethrowFromSystemServer();
Yuhao Zheng5cd1a0e2013-09-09 17:00:04 -07002588 }
2589 }
Robert Greenwalte049c232014-04-11 15:53:27 -07002590
2591 /** {@hide} */
Robert Greenwalta67be032014-05-16 15:49:14 -07002592 public void registerNetworkFactory(Messenger messenger, String name) {
Robert Greenwalte049c232014-04-11 15:53:27 -07002593 try {
Robert Greenwalta67be032014-05-16 15:49:14 -07002594 mService.registerNetworkFactory(messenger, name);
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07002595 } catch (RemoteException e) {
2596 throw e.rethrowFromSystemServer();
2597 }
Robert Greenwalta67be032014-05-16 15:49:14 -07002598 }
2599
2600 /** {@hide} */
2601 public void unregisterNetworkFactory(Messenger messenger) {
2602 try {
2603 mService.unregisterNetworkFactory(messenger);
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07002604 } catch (RemoteException e) {
2605 throw e.rethrowFromSystemServer();
2606 }
Robert Greenwalte049c232014-04-11 15:53:27 -07002607 }
Robert Greenwalt7b816022014-04-18 15:25:25 -07002608
Paul Jensen31a94f42015-02-13 14:18:39 -05002609 /**
2610 * @hide
2611 * Register a NetworkAgent with ConnectivityService.
2612 * @return NetID corresponding to NetworkAgent.
2613 */
2614 public int registerNetworkAgent(Messenger messenger, NetworkInfo ni, LinkProperties lp,
Sreeram Ramachandran8cd33ed2014-07-23 15:23:15 -07002615 NetworkCapabilities nc, int score, NetworkMisc misc) {
Robert Greenwalt7b816022014-04-18 15:25:25 -07002616 try {
Paul Jensen31a94f42015-02-13 14:18:39 -05002617 return mService.registerNetworkAgent(messenger, ni, lp, nc, score, misc);
2618 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07002619 throw e.rethrowFromSystemServer();
Paul Jensen31a94f42015-02-13 14:18:39 -05002620 }
Robert Greenwalt7b816022014-04-18 15:25:25 -07002621 }
2622
Robert Greenwalt9258c642014-03-26 16:47:06 -07002623 /**
Hugo Benichidafed3d2017-03-06 09:17:06 +09002624 * Base class for {@code NetworkRequest} callbacks. Used for notifications about network
2625 * changes. Should be extended by applications wanting notifications.
2626 *
2627 * A {@code NetworkCallback} is registered by calling
2628 * {@link #requestNetwork(NetworkRequest, NetworkCallback)},
2629 * {@link #registerNetworkCallback(NetworkRequest, NetworkCallback)},
2630 * or {@link #registerDefaultNetworkCallback(NetworkCallback). A {@code NetworkCallback} is
2631 * unregistered by calling {@link #unregisterNetworkCallback(NetworkCallback)}.
2632 * A {@code NetworkCallback} should be registered at most once at any time.
2633 * A {@code NetworkCallback} that has been unregistered can be registered again.
Robert Greenwalt9258c642014-03-26 16:47:06 -07002634 */
Robert Greenwalt6078b502014-06-11 16:05:07 -07002635 public static class NetworkCallback {
Robert Greenwalt7b816022014-04-18 15:25:25 -07002636 /**
Lorenzo Colitti07086932015-04-24 12:23:24 +09002637 * Called when the framework connects to a new network to evaluate whether it satisfies this
2638 * request. If evaluation succeeds, this callback may be followed by an {@link #onAvailable}
2639 * callback. There is no guarantee that this new network will satisfy any requests, or that
2640 * the network will stay connected for longer than the time necessary to evaluate it.
2641 * <p>
2642 * Most applications <b>should not</b> act on this callback, and should instead use
2643 * {@link #onAvailable}. This callback is intended for use by applications that can assist
2644 * the framework in properly evaluating the network &mdash; for example, an application that
2645 * can automatically log in to a captive portal without user intervention.
2646 *
2647 * @param network The {@link Network} of the network that is being evaluated.
Lorenzo Colitti66276122015-06-11 14:27:17 +09002648 *
2649 * @hide
Robert Greenwalt7b816022014-04-18 15:25:25 -07002650 */
Robert Greenwalt6078b502014-06-11 16:05:07 -07002651 public void onPreCheck(Network network) {}
Robert Greenwalt7b816022014-04-18 15:25:25 -07002652
2653 /**
Lorenzo Colitti07086932015-04-24 12:23:24 +09002654 * Called when the framework connects and has declared a new network ready for use.
Robert Greenwalt6078b502014-06-11 16:05:07 -07002655 * This callback may be called more than once if the {@link Network} that is
2656 * satisfying the request changes.
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07002657 *
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07002658 * @param network The {@link Network} of the satisfying network.
Robert Greenwalt7b816022014-04-18 15:25:25 -07002659 */
Robert Greenwalt6078b502014-06-11 16:05:07 -07002660 public void onAvailable(Network network) {}
Robert Greenwalt7b816022014-04-18 15:25:25 -07002661
2662 /**
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07002663 * Called when the network is about to be disconnected. Often paired with an
Robert Greenwalt6078b502014-06-11 16:05:07 -07002664 * {@link NetworkCallback#onAvailable} call with the new replacement network
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07002665 * for graceful handover. This may not be called if we have a hard loss
2666 * (loss without warning). This may be followed by either a
Robert Greenwalt6078b502014-06-11 16:05:07 -07002667 * {@link NetworkCallback#onLost} call or a
2668 * {@link NetworkCallback#onAvailable} call for this network depending
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07002669 * on whether we lose or regain it.
2670 *
Robert Greenwalt6078b502014-06-11 16:05:07 -07002671 * @param network The {@link Network} that is about to be disconnected.
2672 * @param maxMsToLive The time in ms the framework will attempt to keep the
2673 * network connected. Note that the network may suffer a
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07002674 * hard loss at any time.
Robert Greenwalt7b816022014-04-18 15:25:25 -07002675 */
Robert Greenwalt6078b502014-06-11 16:05:07 -07002676 public void onLosing(Network network, int maxMsToLive) {}
Robert Greenwalt7b816022014-04-18 15:25:25 -07002677
2678 /**
2679 * Called when the framework has a hard loss of the network or when the
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07002680 * graceful failure ends.
2681 *
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07002682 * @param network The {@link Network} lost.
Robert Greenwalt7b816022014-04-18 15:25:25 -07002683 */
Robert Greenwalt6078b502014-06-11 16:05:07 -07002684 public void onLost(Network network) {}
Robert Greenwalt7b816022014-04-18 15:25:25 -07002685
2686 /**
Etan Cohenaebf17e2017-03-01 12:47:28 -08002687 * Called if no network is found in the timeout time specified in
Hugo Benichi0eec03f2017-05-15 15:15:33 +09002688 * {@link #requestNetwork(NetworkRequest, NetworkCallback, int)} call. This callback is not
Etan Cohenaebf17e2017-03-01 12:47:28 -08002689 * called for the version of {@link #requestNetwork(NetworkRequest, NetworkCallback)}
2690 * without timeout. When this callback is invoked the associated
2691 * {@link NetworkRequest} will have already been removed and released, as if
2692 * {@link #unregisterNetworkCallback(NetworkCallback)} had been called.
Robert Greenwalt7b816022014-04-18 15:25:25 -07002693 */
Robert Greenwalt6078b502014-06-11 16:05:07 -07002694 public void onUnavailable() {}
Robert Greenwalt7b816022014-04-18 15:25:25 -07002695
2696 /**
2697 * Called when the network the framework connected to for this request
2698 * changes capabilities but still satisfies the stated need.
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07002699 *
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07002700 * @param network The {@link Network} whose capabilities have changed.
Lorenzo Colittie285b432015-04-23 15:32:42 +09002701 * @param networkCapabilities The new {@link android.net.NetworkCapabilities} for this network.
Robert Greenwalt7b816022014-04-18 15:25:25 -07002702 */
Robert Greenwalt6078b502014-06-11 16:05:07 -07002703 public void onCapabilitiesChanged(Network network,
Robert Greenwalt7b816022014-04-18 15:25:25 -07002704 NetworkCapabilities networkCapabilities) {}
2705
2706 /**
2707 * Called when the network the framework connected to for this request
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07002708 * changes {@link LinkProperties}.
2709 *
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07002710 * @param network The {@link Network} whose link properties have changed.
2711 * @param linkProperties The new {@link LinkProperties} for this network.
Robert Greenwalt7b816022014-04-18 15:25:25 -07002712 */
Robert Greenwalt6078b502014-06-11 16:05:07 -07002713 public void onLinkPropertiesChanged(Network network, LinkProperties linkProperties) {}
Robert Greenwalt7b816022014-04-18 15:25:25 -07002714
Robert Greenwalt8d482522015-06-24 13:23:42 -07002715 /**
2716 * Called when the network the framework connected to for this request
2717 * goes into {@link NetworkInfo.DetailedState.SUSPENDED}.
2718 * This generally means that while the TCP connections are still live,
2719 * temporarily network data fails to transfer. Specifically this is used
2720 * on cellular networks to mask temporary outages when driving through
2721 * a tunnel, etc.
2722 * @hide
2723 */
2724 public void onNetworkSuspended(Network network) {}
2725
2726 /**
2727 * Called when the network the framework connected to for this request
2728 * returns from a {@link NetworkInfo.DetailedState.SUSPENDED} state.
2729 * This should always be preceeded by a matching {@code onNetworkSuspended}
2730 * call.
2731 * @hide
2732 */
2733 public void onNetworkResumed(Network network) {}
2734
Robert Greenwalt6078b502014-06-11 16:05:07 -07002735 private NetworkRequest networkRequest;
Robert Greenwalt7b816022014-04-18 15:25:25 -07002736 }
2737
Hugo Benichicb883232017-05-11 13:16:17 +09002738 /**
2739 * Constant error codes used by ConnectivityService to communicate about failures and errors
2740 * across a Binder boundary.
2741 * @hide
2742 */
2743 public interface Errors {
2744 static int TOO_MANY_REQUESTS = 1;
2745 }
2746
2747 /** @hide */
2748 public static class TooManyRequestsException extends RuntimeException {}
2749
2750 private static RuntimeException convertServiceException(ServiceSpecificException e) {
2751 switch (e.errorCode) {
2752 case Errors.TOO_MANY_REQUESTS:
2753 return new TooManyRequestsException();
2754 default:
2755 Log.w(TAG, "Unknown service error code " + e.errorCode);
2756 return new RuntimeException(e);
2757 }
2758 }
2759
Robert Greenwalt9258c642014-03-26 16:47:06 -07002760 private static final int BASE = Protocol.BASE_CONNECTIVITY_MANAGER;
Robert Greenwalt9258c642014-03-26 16:47:06 -07002761 /** @hide */
Robert Greenwalt8d482522015-06-24 13:23:42 -07002762 public static final int CALLBACK_PRECHECK = BASE + 1;
2763 /** @hide */
2764 public static final int CALLBACK_AVAILABLE = BASE + 2;
2765 /** @hide arg1 = TTL */
2766 public static final int CALLBACK_LOSING = BASE + 3;
2767 /** @hide */
2768 public static final int CALLBACK_LOST = BASE + 4;
2769 /** @hide */
2770 public static final int CALLBACK_UNAVAIL = BASE + 5;
2771 /** @hide */
2772 public static final int CALLBACK_CAP_CHANGED = BASE + 6;
2773 /** @hide */
2774 public static final int CALLBACK_IP_CHANGED = BASE + 7;
Robert Greenwalt562cc542014-05-15 18:07:26 -07002775 /** @hide obj = NetworkCapabilities, arg1 = seq number */
Hugo Benichidba33db2017-03-23 22:40:44 +09002776 private static final int EXPIRE_LEGACY_REQUEST = BASE + 8;
Robert Greenwalt8d482522015-06-24 13:23:42 -07002777 /** @hide */
Hugo Benichidba33db2017-03-23 22:40:44 +09002778 public static final int CALLBACK_SUSPENDED = BASE + 9;
Robert Greenwalt8d482522015-06-24 13:23:42 -07002779 /** @hide */
Hugo Benichidba33db2017-03-23 22:40:44 +09002780 public static final int CALLBACK_RESUMED = BASE + 10;
Robert Greenwalt9258c642014-03-26 16:47:06 -07002781
Erik Kline57faba92015-11-25 12:49:38 +09002782 /** @hide */
2783 public static String getCallbackName(int whichCallback) {
2784 switch (whichCallback) {
2785 case CALLBACK_PRECHECK: return "CALLBACK_PRECHECK";
2786 case CALLBACK_AVAILABLE: return "CALLBACK_AVAILABLE";
2787 case CALLBACK_LOSING: return "CALLBACK_LOSING";
2788 case CALLBACK_LOST: return "CALLBACK_LOST";
2789 case CALLBACK_UNAVAIL: return "CALLBACK_UNAVAIL";
2790 case CALLBACK_CAP_CHANGED: return "CALLBACK_CAP_CHANGED";
2791 case CALLBACK_IP_CHANGED: return "CALLBACK_IP_CHANGED";
Erik Kline57faba92015-11-25 12:49:38 +09002792 case EXPIRE_LEGACY_REQUEST: return "EXPIRE_LEGACY_REQUEST";
2793 case CALLBACK_SUSPENDED: return "CALLBACK_SUSPENDED";
2794 case CALLBACK_RESUMED: return "CALLBACK_RESUMED";
2795 default:
2796 return Integer.toString(whichCallback);
2797 }
2798 }
2799
Robert Greenwalt562cc542014-05-15 18:07:26 -07002800 private class CallbackHandler extends Handler {
Robert Greenwalt9258c642014-03-26 16:47:06 -07002801 private static final String TAG = "ConnectivityManager.CallbackHandler";
Robert Greenwalta9ebeef2015-09-03 16:41:45 -07002802 private static final boolean DBG = false;
Robert Greenwalt9258c642014-03-26 16:47:06 -07002803
Hugo Benichid42650f2016-07-06 22:53:17 +09002804 CallbackHandler(Looper looper) {
Robert Greenwalt9258c642014-03-26 16:47:06 -07002805 super(looper);
Robert Greenwalt9258c642014-03-26 16:47:06 -07002806 }
2807
Hugo Benichi2583ef02017-02-02 17:02:36 +09002808 CallbackHandler(Handler handler) {
Hugo Benichie7678512017-05-09 15:19:01 +09002809 this(Preconditions.checkNotNull(handler, "Handler cannot be null.").getLooper());
Hugo Benichi2583ef02017-02-02 17:02:36 +09002810 }
2811
Robert Greenwalt9258c642014-03-26 16:47:06 -07002812 @Override
2813 public void handleMessage(Message message) {
Hugo Benichi2c684522017-05-09 14:36:02 +09002814 if (message.what == EXPIRE_LEGACY_REQUEST) {
2815 expireRequest((NetworkCapabilities) message.obj, message.arg1);
2816 return;
2817 }
2818
2819 final NetworkRequest request = getObject(message, NetworkRequest.class);
2820 final Network network = getObject(message, Network.class);
2821 final NetworkCallback callback;
2822 synchronized (sCallbacks) {
2823 callback = sCallbacks.get(request);
2824 }
Lorenzo Colittifcfa7d92016-03-01 22:56:37 +09002825 if (DBG) {
Hugo Benichia0385682017-03-22 17:07:57 +09002826 Log.d(TAG, getCallbackName(message.what) + " for network " + network);
Lorenzo Colittifcfa7d92016-03-01 22:56:37 +09002827 }
Hugo Benichi2c684522017-05-09 14:36:02 +09002828 if (callback == null) {
2829 Log.w(TAG, "callback not found for " + getCallbackName(message.what) + " message");
2830 return;
2831 }
2832
Robert Greenwalt9258c642014-03-26 16:47:06 -07002833 switch (message.what) {
2834 case CALLBACK_PRECHECK: {
Hugo Benichi2c684522017-05-09 14:36:02 +09002835 callback.onPreCheck(network);
Robert Greenwalt9258c642014-03-26 16:47:06 -07002836 break;
2837 }
2838 case CALLBACK_AVAILABLE: {
Hugo Benichi2c684522017-05-09 14:36:02 +09002839 callback.onAvailable(network);
Robert Greenwalt9258c642014-03-26 16:47:06 -07002840 break;
2841 }
2842 case CALLBACK_LOSING: {
Hugo Benichi2c684522017-05-09 14:36:02 +09002843 callback.onLosing(network, message.arg1);
Robert Greenwalt9258c642014-03-26 16:47:06 -07002844 break;
2845 }
2846 case CALLBACK_LOST: {
Hugo Benichi2c684522017-05-09 14:36:02 +09002847 callback.onLost(network);
Robert Greenwalt9258c642014-03-26 16:47:06 -07002848 break;
2849 }
2850 case CALLBACK_UNAVAIL: {
Hugo Benichi2c684522017-05-09 14:36:02 +09002851 callback.onUnavailable();
Robert Greenwalt9258c642014-03-26 16:47:06 -07002852 break;
2853 }
2854 case CALLBACK_CAP_CHANGED: {
Hugo Benichi2c684522017-05-09 14:36:02 +09002855 NetworkCapabilities cap = getObject(message, NetworkCapabilities.class);
2856 callback.onCapabilitiesChanged(network, cap);
Robert Greenwalt9258c642014-03-26 16:47:06 -07002857 break;
2858 }
2859 case CALLBACK_IP_CHANGED: {
Hugo Benichi2c684522017-05-09 14:36:02 +09002860 LinkProperties lp = getObject(message, LinkProperties.class);
2861 callback.onLinkPropertiesChanged(network, lp);
Robert Greenwalt9258c642014-03-26 16:47:06 -07002862 break;
2863 }
Robert Greenwalt8d482522015-06-24 13:23:42 -07002864 case CALLBACK_SUSPENDED: {
Hugo Benichi2c684522017-05-09 14:36:02 +09002865 callback.onNetworkSuspended(network);
Robert Greenwalt8d482522015-06-24 13:23:42 -07002866 break;
2867 }
2868 case CALLBACK_RESUMED: {
Hugo Benichi2c684522017-05-09 14:36:02 +09002869 callback.onNetworkResumed(network);
Robert Greenwalt562cc542014-05-15 18:07:26 -07002870 break;
2871 }
Robert Greenwalt9258c642014-03-26 16:47:06 -07002872 }
2873 }
2874
Hugo Benichid42650f2016-07-06 22:53:17 +09002875 private <T> T getObject(Message msg, Class<T> c) {
2876 return (T) msg.getData().getParcelable(c.getSimpleName());
Robert Greenwalt9258c642014-03-26 16:47:06 -07002877 }
Robert Greenwalt9258c642014-03-26 16:47:06 -07002878 }
2879
Hugo Benichi2583ef02017-02-02 17:02:36 +09002880 private CallbackHandler getDefaultHandler() {
Hugo Benichi7724cdd2016-07-07 10:15:56 +09002881 synchronized (sCallbacks) {
2882 if (sCallbackHandler == null) {
2883 sCallbackHandler = new CallbackHandler(ConnectivityThread.getInstanceLooper());
Robert Greenwalt9258c642014-03-26 16:47:06 -07002884 }
Hugo Benichi7724cdd2016-07-07 10:15:56 +09002885 return sCallbackHandler;
Robert Greenwalt9258c642014-03-26 16:47:06 -07002886 }
2887 }
2888
Hugo Benichi6f260f32017-02-03 14:18:44 +09002889 private static final HashMap<NetworkRequest, NetworkCallback> sCallbacks = new HashMap<>();
2890 private static CallbackHandler sCallbackHandler;
Robert Greenwalt9258c642014-03-26 16:47:06 -07002891
Hugo Benichi6f260f32017-02-03 14:18:44 +09002892 private static final int LISTEN = 1;
2893 private static final int REQUEST = 2;
Robert Greenwalt9258c642014-03-26 16:47:06 -07002894
Hugo Benichi6f260f32017-02-03 14:18:44 +09002895 private NetworkRequest sendRequestForNetwork(NetworkCapabilities need, NetworkCallback callback,
2896 int timeoutMs, int action, int legacyType, CallbackHandler handler) {
Hugo Benichie7678512017-05-09 15:19:01 +09002897 checkCallbackNotNull(callback);
Hugo Benichidafed3d2017-03-06 09:17:06 +09002898 Preconditions.checkArgument(action == REQUEST || need != null, "null NetworkCapabilities");
Hugo Benichid42650f2016-07-06 22:53:17 +09002899 final NetworkRequest request;
Robert Greenwalt9258c642014-03-26 16:47:06 -07002900 try {
Hugo Benichid42650f2016-07-06 22:53:17 +09002901 synchronized(sCallbacks) {
Hugo Benichi31c176d2017-06-17 13:14:12 +09002902 if (callback.networkRequest != null
2903 && callback.networkRequest != ALREADY_UNREGISTERED) {
Hugo Benichidafed3d2017-03-06 09:17:06 +09002904 // TODO: throw exception instead and enforce 1:1 mapping of callbacks
2905 // and requests (http://b/20701525).
2906 Log.e(TAG, "NetworkCallback was already registered");
2907 }
Hugo Benichi7724cdd2016-07-07 10:15:56 +09002908 Messenger messenger = new Messenger(handler);
Hugo Benichid42650f2016-07-06 22:53:17 +09002909 Binder binder = new Binder();
Paul Jensen7221cc32014-06-27 11:05:32 -04002910 if (action == LISTEN) {
Hugo Benichid42650f2016-07-06 22:53:17 +09002911 request = mService.listenForNetwork(need, messenger, binder);
Paul Jensen7221cc32014-06-27 11:05:32 -04002912 } else {
Hugo Benichid42650f2016-07-06 22:53:17 +09002913 request = mService.requestNetwork(
2914 need, messenger, timeoutMs, binder, legacyType);
Paul Jensen7221cc32014-06-27 11:05:32 -04002915 }
Hugo Benichid42650f2016-07-06 22:53:17 +09002916 if (request != null) {
Hugo Benichi7724cdd2016-07-07 10:15:56 +09002917 sCallbacks.put(request, callback);
Robert Greenwalt9258c642014-03-26 16:47:06 -07002918 }
Hugo Benichi7724cdd2016-07-07 10:15:56 +09002919 callback.networkRequest = request;
Robert Greenwalt9258c642014-03-26 16:47:06 -07002920 }
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07002921 } catch (RemoteException e) {
2922 throw e.rethrowFromSystemServer();
Hugo Benichicb883232017-05-11 13:16:17 +09002923 } catch (ServiceSpecificException e) {
2924 throw convertServiceException(e);
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07002925 }
Hugo Benichid42650f2016-07-06 22:53:17 +09002926 return request;
Robert Greenwalt9258c642014-03-26 16:47:06 -07002927 }
2928
2929 /**
Erik Klinea2d29402016-03-16 15:31:39 +09002930 * Helper function to request a network with a particular legacy type.
Lorenzo Colitti7de289f2015-11-25 12:00:52 +09002931 *
2932 * This is temporarily public @hide so it can be called by system code that uses the
2933 * NetworkRequest API to request networks but relies on CONNECTIVITY_ACTION broadcasts for
2934 * instead network notifications.
2935 *
2936 * TODO: update said system code to rely on NetworkCallbacks and make this method private.
2937 *
2938 * @hide
2939 */
Lorenzo Colittid1179462015-11-25 15:47:14 +09002940 public void requestNetwork(NetworkRequest request, NetworkCallback networkCallback,
Hugo Benichi2583ef02017-02-02 17:02:36 +09002941 int timeoutMs, int legacyType, Handler handler) {
2942 CallbackHandler cbHandler = new CallbackHandler(handler);
2943 NetworkCapabilities nc = request.networkCapabilities;
2944 sendRequestForNetwork(nc, networkCallback, timeoutMs, REQUEST, legacyType, cbHandler);
Lorenzo Colitti7de289f2015-11-25 12:00:52 +09002945 }
2946
2947 /**
Lorenzo Colittie285b432015-04-23 15:32:42 +09002948 * Request a network to satisfy a set of {@link android.net.NetworkCapabilities}.
Robert Greenwalt9258c642014-03-26 16:47:06 -07002949 *
2950 * This {@link NetworkRequest} will live until released via
Etan Cohenaebf17e2017-03-01 12:47:28 -08002951 * {@link #unregisterNetworkCallback(NetworkCallback)} or the calling application exits. A
2952 * version of the method which takes a timeout is
Hugo Benichi0eec03f2017-05-15 15:15:33 +09002953 * {@link #requestNetwork(NetworkRequest, NetworkCallback, int)}.
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07002954 * Status of the request can be followed by listening to the various
Robert Greenwalt6078b502014-06-11 16:05:07 -07002955 * callbacks described in {@link NetworkCallback}. The {@link Network}
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07002956 * can be used to direct traffic to the network.
Paul Jensenbb2e0e92015-06-16 15:11:58 -04002957 * <p>It is presently unsupported to request a network with mutable
2958 * {@link NetworkCapabilities} such as
2959 * {@link NetworkCapabilities#NET_CAPABILITY_VALIDATED} or
2960 * {@link NetworkCapabilities#NET_CAPABILITY_CAPTIVE_PORTAL}
2961 * as these {@code NetworkCapabilities} represent states that a particular
2962 * network may never attain, and whether a network will attain these states
2963 * is unknown prior to bringing up the network so the framework does not
2964 * know how to go about satisfing a request with these capabilities.
Lorenzo Colittid5427052015-10-15 16:29:00 +09002965 *
2966 * <p>This method requires the caller to hold either the
2967 * {@link android.Manifest.permission#CHANGE_NETWORK_STATE} permission
2968 * or the ability to modify system settings as determined by
2969 * {@link android.provider.Settings.System#canWrite}.</p>
Robert Greenwalt9258c642014-03-26 16:47:06 -07002970 *
Robert Greenwalt6078b502014-06-11 16:05:07 -07002971 * @param request {@link NetworkRequest} describing this request.
Hugo Benichi2583ef02017-02-02 17:02:36 +09002972 * @param networkCallback The {@link NetworkCallback} to be utilized for this request. Note
2973 * the callback must not be shared - it uniquely specifies this request.
2974 * The callback is invoked on the default internal Handler.
Paul Jensenbb2e0e92015-06-16 15:11:58 -04002975 * @throws IllegalArgumentException if {@code request} specifies any mutable
2976 * {@code NetworkCapabilities}.
Robert Greenwalt9258c642014-03-26 16:47:06 -07002977 */
Robert Greenwalt6078b502014-06-11 16:05:07 -07002978 public void requestNetwork(NetworkRequest request, NetworkCallback networkCallback) {
Hugo Benichi2583ef02017-02-02 17:02:36 +09002979 requestNetwork(request, networkCallback, getDefaultHandler());
2980 }
2981
2982 /**
2983 * Request a network to satisfy a set of {@link android.net.NetworkCapabilities}.
2984 *
2985 * This {@link NetworkRequest} will live until released via
Etan Cohenaebf17e2017-03-01 12:47:28 -08002986 * {@link #unregisterNetworkCallback(NetworkCallback)} or the calling application exits. A
2987 * version of the method which takes a timeout is
Hugo Benichi0eec03f2017-05-15 15:15:33 +09002988 * {@link #requestNetwork(NetworkRequest, NetworkCallback, int)}.
Hugo Benichi2583ef02017-02-02 17:02:36 +09002989 * Status of the request can be followed by listening to the various
2990 * callbacks described in {@link NetworkCallback}. The {@link Network}
2991 * can be used to direct traffic to the network.
2992 * <p>It is presently unsupported to request a network with mutable
2993 * {@link NetworkCapabilities} such as
2994 * {@link NetworkCapabilities#NET_CAPABILITY_VALIDATED} or
2995 * {@link NetworkCapabilities#NET_CAPABILITY_CAPTIVE_PORTAL}
2996 * as these {@code NetworkCapabilities} represent states that a particular
2997 * network may never attain, and whether a network will attain these states
2998 * is unknown prior to bringing up the network so the framework does not
2999 * know how to go about satisfing a request with these capabilities.
3000 *
3001 * <p>This method requires the caller to hold either the
3002 * {@link android.Manifest.permission#CHANGE_NETWORK_STATE} permission
3003 * or the ability to modify system settings as determined by
3004 * {@link android.provider.Settings.System#canWrite}.</p>
3005 *
3006 * @param request {@link NetworkRequest} describing this request.
3007 * @param networkCallback The {@link NetworkCallback} to be utilized for this request. Note
3008 * the callback must not be shared - it uniquely specifies this request.
3009 * @param handler {@link Handler} to specify the thread upon which the callback will be invoked.
3010 * @throws IllegalArgumentException if {@code request} specifies any mutable
3011 * {@code NetworkCapabilities}.
Hugo Benichi2583ef02017-02-02 17:02:36 +09003012 */
3013 public void requestNetwork(
3014 NetworkRequest request, NetworkCallback networkCallback, Handler handler) {
3015 int legacyType = inferLegacyTypeForNetworkCapabilities(request.networkCapabilities);
3016 CallbackHandler cbHandler = new CallbackHandler(handler);
3017 requestNetwork(request, networkCallback, 0, legacyType, cbHandler);
Robert Greenwalt9258c642014-03-26 16:47:06 -07003018 }
3019
3020 /**
Etan Cohenaebf17e2017-03-01 12:47:28 -08003021 * Request a network to satisfy a set of {@link android.net.NetworkCapabilities}, limited
3022 * by a timeout.
3023 *
3024 * This function behaves identically to the non-timed-out version
3025 * {@link #requestNetwork(NetworkRequest, NetworkCallback)}, but if a suitable network
3026 * is not found within the given time (in milliseconds) the
3027 * {@link NetworkCallback#onUnavailable()} callback is called. The request can still be
3028 * released normally by calling {@link #unregisterNetworkCallback(NetworkCallback)} but does
3029 * not have to be released if timed-out (it is automatically released). Unregistering a
3030 * request that timed out is not an error.
3031 *
3032 * <p>Do not use this method to poll for the existence of specific networks (e.g. with a small
3033 * timeout) - {@link #registerNetworkCallback(NetworkRequest, NetworkCallback)} is provided
3034 * for that purpose. Calling this method will attempt to bring up the requested network.
3035 *
3036 * <p>This method requires the caller to hold either the
3037 * {@link android.Manifest.permission#CHANGE_NETWORK_STATE} permission
3038 * or the ability to modify system settings as determined by
3039 * {@link android.provider.Settings.System#canWrite}.</p>
3040 *
3041 * @param request {@link NetworkRequest} describing this request.
Lorenzo Colitti15fd4392017-04-28 00:56:30 +09003042 * @param networkCallback The {@link NetworkCallback} to be utilized for this request. Note
3043 * the callback must not be shared - it uniquely specifies this request.
Etan Cohenaebf17e2017-03-01 12:47:28 -08003044 * @param timeoutMs The time in milliseconds to attempt looking for a suitable network
3045 * before {@link NetworkCallback#onUnavailable()} is called. The timeout must
3046 * be a positive value (i.e. >0).
Etan Cohenaebf17e2017-03-01 12:47:28 -08003047 */
Lorenzo Colitti15fd4392017-04-28 00:56:30 +09003048 public void requestNetwork(NetworkRequest request, NetworkCallback networkCallback,
3049 int timeoutMs) {
Hugo Benichie7678512017-05-09 15:19:01 +09003050 checkTimeout(timeoutMs);
Hugo Benichi2583ef02017-02-02 17:02:36 +09003051 int legacyType = inferLegacyTypeForNetworkCapabilities(request.networkCapabilities);
Erik Klineb583b032017-02-22 12:58:24 +09003052 requestNetwork(request, networkCallback, timeoutMs, legacyType, getDefaultHandler());
Hugo Benichi2583ef02017-02-02 17:02:36 +09003053 }
3054
Hugo Benichi2583ef02017-02-02 17:02:36 +09003055 /**
3056 * Request a network to satisfy a set of {@link android.net.NetworkCapabilities}, limited
3057 * by a timeout.
3058 *
3059 * This function behaves identically to the non-timedout version, but if a suitable
3060 * network is not found within the given time (in milliseconds) the
Etan Cohenaebf17e2017-03-01 12:47:28 -08003061 * {@link NetworkCallback#onUnavailable} callback is called. The request can still be
3062 * released normally by calling {@link #unregisterNetworkCallback(NetworkCallback)} but does
3063 * not have to be released if timed-out (it is automatically released). Unregistering a
3064 * request that timed out is not an error.
3065 *
3066 * <p>Do not use this method to poll for the existence of specific networks (e.g. with a small
3067 * timeout) - {@link #registerNetworkCallback(NetworkRequest, NetworkCallback)} is provided
3068 * for that purpose. Calling this method will attempt to bring up the requested network.
Hugo Benichi2583ef02017-02-02 17:02:36 +09003069 *
3070 * <p>This method requires the caller to hold either the
3071 * {@link android.Manifest.permission#CHANGE_NETWORK_STATE} permission
3072 * or the ability to modify system settings as determined by
3073 * {@link android.provider.Settings.System#canWrite}.</p>
3074 *
3075 * @param request {@link NetworkRequest} describing this request.
Etan Cohenaebf17e2017-03-01 12:47:28 -08003076 * @param networkCallback The {@link NetworkCallback} to be utilized for this request. Note
3077 * the callback must not be shared - it uniquely specifies this request.
Hugo Benichi2583ef02017-02-02 17:02:36 +09003078 * @param handler {@link Handler} to specify the thread upon which the callback will be invoked.
Lorenzo Colitti15fd4392017-04-28 00:56:30 +09003079 * @param timeoutMs The time in milliseconds to attempt looking for a suitable network
3080 * before {@link NetworkCallback#onUnavailable} is called.
Hugo Benichi2583ef02017-02-02 17:02:36 +09003081 */
Lorenzo Colitti15fd4392017-04-28 00:56:30 +09003082 public void requestNetwork(NetworkRequest request, NetworkCallback networkCallback,
3083 Handler handler, int timeoutMs) {
Hugo Benichie7678512017-05-09 15:19:01 +09003084 checkTimeout(timeoutMs);
Hugo Benichi2583ef02017-02-02 17:02:36 +09003085 int legacyType = inferLegacyTypeForNetworkCapabilities(request.networkCapabilities);
3086 CallbackHandler cbHandler = new CallbackHandler(handler);
3087 requestNetwork(request, networkCallback, timeoutMs, legacyType, cbHandler);
Robert Greenwalt9258c642014-03-26 16:47:06 -07003088 }
3089
3090 /**
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07003091 * The lookup key for a {@link Network} object included with the intent after
Jeremy Joslin46e3ac82014-11-05 10:32:09 -08003092 * successfully finding a network for the applications request. Retrieve it with
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07003093 * {@link android.content.Intent#getParcelableExtra(String)}.
Jeremy Joslinfcde58f2015-02-11 16:51:13 -08003094 * <p>
Paul Jensen72db88e2015-03-10 10:54:12 -04003095 * Note that if you intend to invoke {@link Network#openConnection(java.net.URL)}
3096 * then you must get a ConnectivityManager instance before doing so.
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07003097 */
Erik Kline90e93072014-11-19 12:12:24 +09003098 public static final String EXTRA_NETWORK = "android.net.extra.NETWORK";
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07003099
3100 /**
Robert Greenwalt6078b502014-06-11 16:05:07 -07003101 * The lookup key for a {@link NetworkRequest} object included with the intent after
Jeremy Joslin46e3ac82014-11-05 10:32:09 -08003102 * successfully finding a network for the applications request. Retrieve it with
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07003103 * {@link android.content.Intent#getParcelableExtra(String)}.
3104 */
Erik Kline90e93072014-11-19 12:12:24 +09003105 public static final String EXTRA_NETWORK_REQUEST = "android.net.extra.NETWORK_REQUEST";
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07003106
3107
3108 /**
Lorenzo Colittie285b432015-04-23 15:32:42 +09003109 * Request a network to satisfy a set of {@link android.net.NetworkCapabilities}.
Robert Greenwalt9258c642014-03-26 16:47:06 -07003110 *
Jeremy Joslin46e3ac82014-11-05 10:32:09 -08003111 * This function behaves identically to the version that takes a NetworkCallback, but instead
Robert Greenwalt6078b502014-06-11 16:05:07 -07003112 * of {@link NetworkCallback} a {@link PendingIntent} is used. This means
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07003113 * the request may outlive the calling application and get called back when a suitable
3114 * network is found.
Robert Greenwalt9258c642014-03-26 16:47:06 -07003115 * <p>
3116 * The operation is an Intent broadcast that goes to a broadcast receiver that
3117 * you registered with {@link Context#registerReceiver} or through the
3118 * &lt;receiver&gt; tag in an AndroidManifest.xml file
3119 * <p>
3120 * The operation Intent is delivered with two extras, a {@link Network} typed
Erik Kline90e93072014-11-19 12:12:24 +09003121 * extra called {@link #EXTRA_NETWORK} and a {@link NetworkRequest}
3122 * typed extra called {@link #EXTRA_NETWORK_REQUEST} containing
Robert Greenwalt9258c642014-03-26 16:47:06 -07003123 * the original requests parameters. It is important to create a new,
Robert Greenwalt6078b502014-06-11 16:05:07 -07003124 * {@link NetworkCallback} based request before completing the processing of the
Robert Greenwalt9258c642014-03-26 16:47:06 -07003125 * Intent to reserve the network or it will be released shortly after the Intent
3126 * is processed.
3127 * <p>
Paul Jensen694f2b82015-06-17 14:15:39 -04003128 * If there is already a request for this Intent registered (with the equality of
Robert Greenwalt9258c642014-03-26 16:47:06 -07003129 * two Intents defined by {@link Intent#filterEquals}), then it will be removed and
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07003130 * replaced by this one, effectively releasing the previous {@link NetworkRequest}.
Robert Greenwalt9258c642014-03-26 16:47:06 -07003131 * <p>
Jeremy Joslin46e3ac82014-11-05 10:32:09 -08003132 * The request may be released normally by calling
3133 * {@link #releaseNetworkRequest(android.app.PendingIntent)}.
Paul Jensenbb2e0e92015-06-16 15:11:58 -04003134 * <p>It is presently unsupported to request a network with either
3135 * {@link NetworkCapabilities#NET_CAPABILITY_VALIDATED} or
3136 * {@link NetworkCapabilities#NET_CAPABILITY_CAPTIVE_PORTAL}
3137 * as these {@code NetworkCapabilities} represent states that a particular
3138 * network may never attain, and whether a network will attain these states
3139 * is unknown prior to bringing up the network so the framework does not
3140 * know how to go about satisfing a request with these capabilities.
Lorenzo Colittid5427052015-10-15 16:29:00 +09003141 *
3142 * <p>This method requires the caller to hold either the
3143 * {@link android.Manifest.permission#CHANGE_NETWORK_STATE} permission
3144 * or the ability to modify system settings as determined by
3145 * {@link android.provider.Settings.System#canWrite}.</p>
3146 *
Robert Greenwalt6078b502014-06-11 16:05:07 -07003147 * @param request {@link NetworkRequest} describing this request.
Robert Greenwalt9258c642014-03-26 16:47:06 -07003148 * @param operation Action to perform when the network is available (corresponds
Robert Greenwalt6078b502014-06-11 16:05:07 -07003149 * to the {@link NetworkCallback#onAvailable} call. Typically
Jeremy Joslin46e3ac82014-11-05 10:32:09 -08003150 * comes from {@link PendingIntent#getBroadcast}. Cannot be null.
Paul Jensenbb2e0e92015-06-16 15:11:58 -04003151 * @throws IllegalArgumentException if {@code request} contains either
3152 * {@link NetworkCapabilities#NET_CAPABILITY_VALIDATED} or
3153 * {@link NetworkCapabilities#NET_CAPABILITY_CAPTIVE_PORTAL}.
Robert Greenwalt9258c642014-03-26 16:47:06 -07003154 */
Robert Greenwalt6078b502014-06-11 16:05:07 -07003155 public void requestNetwork(NetworkRequest request, PendingIntent operation) {
Hugo Benichie7678512017-05-09 15:19:01 +09003156 checkPendingIntentNotNull(operation);
Robert Greenwalt9258c642014-03-26 16:47:06 -07003157 try {
Robert Greenwalt6078b502014-06-11 16:05:07 -07003158 mService.pendingRequestForNetwork(request.networkCapabilities, operation);
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07003159 } catch (RemoteException e) {
3160 throw e.rethrowFromSystemServer();
Hugo Benichicb883232017-05-11 13:16:17 +09003161 } catch (ServiceSpecificException e) {
3162 throw convertServiceException(e);
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07003163 }
Robert Greenwalt9258c642014-03-26 16:47:06 -07003164 }
3165
3166 /**
Jeremy Joslin46e3ac82014-11-05 10:32:09 -08003167 * Removes a request made via {@link #requestNetwork(NetworkRequest, android.app.PendingIntent)}
3168 * <p>
Lorenzo Colitti88bc0bb2016-04-13 22:00:02 +09003169 * This method has the same behavior as
3170 * {@link #unregisterNetworkCallback(android.app.PendingIntent)} with respect to
Jeremy Joslin46e3ac82014-11-05 10:32:09 -08003171 * releasing network resources and disconnecting.
3172 *
3173 * @param operation A PendingIntent equal (as defined by {@link Intent#filterEquals}) to the
3174 * PendingIntent passed to
3175 * {@link #requestNetwork(NetworkRequest, android.app.PendingIntent)} with the
3176 * corresponding NetworkRequest you'd like to remove. Cannot be null.
3177 */
3178 public void releaseNetworkRequest(PendingIntent operation) {
Hugo Benichie7678512017-05-09 15:19:01 +09003179 checkPendingIntentNotNull(operation);
Jeremy Joslin46e3ac82014-11-05 10:32:09 -08003180 try {
3181 mService.releasePendingNetworkRequest(operation);
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07003182 } catch (RemoteException e) {
3183 throw e.rethrowFromSystemServer();
3184 }
Jeremy Joslin46e3ac82014-11-05 10:32:09 -08003185 }
3186
Hugo Benichie7678512017-05-09 15:19:01 +09003187 private static void checkPendingIntentNotNull(PendingIntent intent) {
3188 Preconditions.checkNotNull(intent, "PendingIntent cannot be null.");
3189 }
3190
3191 private static void checkCallbackNotNull(NetworkCallback callback) {
3192 Preconditions.checkNotNull(callback, "null NetworkCallback");
3193 }
3194
3195 private static void checkTimeout(int timeoutMs) {
3196 Preconditions.checkArgumentPositive(timeoutMs, "timeoutMs must be strictly positive.");
Jeremy Joslin46e3ac82014-11-05 10:32:09 -08003197 }
3198
3199 /**
Robert Greenwalt9258c642014-03-26 16:47:06 -07003200 * Registers to receive notifications about all networks which satisfy the given
Robert Greenwalt6078b502014-06-11 16:05:07 -07003201 * {@link NetworkRequest}. The callbacks will continue to be called until
Lorenzo Colitti88bc0bb2016-04-13 22:00:02 +09003202 * either the application exits or link #unregisterNetworkCallback(NetworkCallback)} is called.
Robert Greenwalt9258c642014-03-26 16:47:06 -07003203 *
Robert Greenwalt6078b502014-06-11 16:05:07 -07003204 * @param request {@link NetworkRequest} describing this request.
3205 * @param networkCallback The {@link NetworkCallback} that the system will call as suitable
3206 * networks change state.
Hugo Benichi2583ef02017-02-02 17:02:36 +09003207 * The callback is invoked on the default internal Handler.
Robert Greenwalt9258c642014-03-26 16:47:06 -07003208 */
Jeff Sharkey30e06bb2017-04-24 11:18:03 -06003209 @RequiresPermission(android.Manifest.permission.ACCESS_NETWORK_STATE)
Robert Greenwalt6078b502014-06-11 16:05:07 -07003210 public void registerNetworkCallback(NetworkRequest request, NetworkCallback networkCallback) {
Hugo Benichi2583ef02017-02-02 17:02:36 +09003211 registerNetworkCallback(request, networkCallback, getDefaultHandler());
3212 }
3213
3214 /**
3215 * Registers to receive notifications about all networks which satisfy the given
3216 * {@link NetworkRequest}. The callbacks will continue to be called until
3217 * either the application exits or link #unregisterNetworkCallback(NetworkCallback)} is called.
Hugo Benichi2583ef02017-02-02 17:02:36 +09003218 *
3219 * @param request {@link NetworkRequest} describing this request.
3220 * @param networkCallback The {@link NetworkCallback} that the system will call as suitable
3221 * networks change state.
3222 * @param handler {@link Handler} to specify the thread upon which the callback will be invoked.
Hugo Benichi2583ef02017-02-02 17:02:36 +09003223 */
Jeff Sharkey30e06bb2017-04-24 11:18:03 -06003224 @RequiresPermission(android.Manifest.permission.ACCESS_NETWORK_STATE)
Hugo Benichi2583ef02017-02-02 17:02:36 +09003225 public void registerNetworkCallback(
3226 NetworkRequest request, NetworkCallback networkCallback, Handler handler) {
3227 CallbackHandler cbHandler = new CallbackHandler(handler);
3228 NetworkCapabilities nc = request.networkCapabilities;
3229 sendRequestForNetwork(nc, networkCallback, 0, LISTEN, TYPE_NONE, cbHandler);
Robert Greenwalt9258c642014-03-26 16:47:06 -07003230 }
3231
3232 /**
Paul Jensen694f2b82015-06-17 14:15:39 -04003233 * Registers a PendingIntent to be sent when a network is available which satisfies the given
3234 * {@link NetworkRequest}.
3235 *
3236 * This function behaves identically to the version that takes a NetworkCallback, but instead
3237 * of {@link NetworkCallback} a {@link PendingIntent} is used. This means
3238 * the request may outlive the calling application and get called back when a suitable
3239 * network is found.
3240 * <p>
3241 * The operation is an Intent broadcast that goes to a broadcast receiver that
3242 * you registered with {@link Context#registerReceiver} or through the
3243 * &lt;receiver&gt; tag in an AndroidManifest.xml file
3244 * <p>
3245 * The operation Intent is delivered with two extras, a {@link Network} typed
3246 * extra called {@link #EXTRA_NETWORK} and a {@link NetworkRequest}
3247 * typed extra called {@link #EXTRA_NETWORK_REQUEST} containing
3248 * the original requests parameters.
3249 * <p>
3250 * If there is already a request for this Intent registered (with the equality of
3251 * two Intents defined by {@link Intent#filterEquals}), then it will be removed and
3252 * replaced by this one, effectively releasing the previous {@link NetworkRequest}.
3253 * <p>
3254 * The request may be released normally by calling
Paul Jensenf2c1cfe2015-06-30 14:29:18 -04003255 * {@link #unregisterNetworkCallback(android.app.PendingIntent)}.
Paul Jensen694f2b82015-06-17 14:15:39 -04003256 * @param request {@link NetworkRequest} describing this request.
3257 * @param operation Action to perform when the network is available (corresponds
3258 * to the {@link NetworkCallback#onAvailable} call. Typically
3259 * comes from {@link PendingIntent#getBroadcast}. Cannot be null.
3260 */
Jeff Sharkey30e06bb2017-04-24 11:18:03 -06003261 @RequiresPermission(android.Manifest.permission.ACCESS_NETWORK_STATE)
Paul Jensen694f2b82015-06-17 14:15:39 -04003262 public void registerNetworkCallback(NetworkRequest request, PendingIntent operation) {
Hugo Benichie7678512017-05-09 15:19:01 +09003263 checkPendingIntentNotNull(operation);
Paul Jensen694f2b82015-06-17 14:15:39 -04003264 try {
3265 mService.pendingListenForNetwork(request.networkCapabilities, operation);
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07003266 } catch (RemoteException e) {
3267 throw e.rethrowFromSystemServer();
Hugo Benichicb883232017-05-11 13:16:17 +09003268 } catch (ServiceSpecificException e) {
3269 throw convertServiceException(e);
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07003270 }
Paul Jensen694f2b82015-06-17 14:15:39 -04003271 }
3272
3273 /**
Lorenzo Colitti88bc0bb2016-04-13 22:00:02 +09003274 * Registers to receive notifications about changes in the system default network. The callbacks
3275 * will continue to be called until either the application exits or
3276 * {@link #unregisterNetworkCallback(NetworkCallback)} is called.
Erik Klinea2d29402016-03-16 15:31:39 +09003277 *
3278 * @param networkCallback The {@link NetworkCallback} that the system will call as the
3279 * system default network changes.
Hugo Benichi2583ef02017-02-02 17:02:36 +09003280 * The callback is invoked on the default internal Handler.
Erik Klinea2d29402016-03-16 15:31:39 +09003281 */
Jeff Sharkey30e06bb2017-04-24 11:18:03 -06003282 @RequiresPermission(android.Manifest.permission.ACCESS_NETWORK_STATE)
Erik Klinea2d29402016-03-16 15:31:39 +09003283 public void registerDefaultNetworkCallback(NetworkCallback networkCallback) {
Hugo Benichi2583ef02017-02-02 17:02:36 +09003284 registerDefaultNetworkCallback(networkCallback, getDefaultHandler());
3285 }
3286
3287 /**
3288 * Registers to receive notifications about changes in the system default network. The callbacks
3289 * will continue to be called until either the application exits or
3290 * {@link #unregisterNetworkCallback(NetworkCallback)} is called.
Hugo Benichi2583ef02017-02-02 17:02:36 +09003291 *
3292 * @param networkCallback The {@link NetworkCallback} that the system will call as the
3293 * system default network changes.
3294 * @param handler {@link Handler} to specify the thread upon which the callback will be invoked.
Hugo Benichi2583ef02017-02-02 17:02:36 +09003295 */
Jeff Sharkey30e06bb2017-04-24 11:18:03 -06003296 @RequiresPermission(android.Manifest.permission.ACCESS_NETWORK_STATE)
Hugo Benichi2583ef02017-02-02 17:02:36 +09003297 public void registerDefaultNetworkCallback(NetworkCallback networkCallback, Handler handler) {
Erik Klinea2d29402016-03-16 15:31:39 +09003298 // This works because if the NetworkCapabilities are null,
3299 // ConnectivityService takes them from the default request.
3300 //
3301 // Since the capabilities are exactly the same as the default request's
3302 // capabilities, this request is guaranteed, at all times, to be
3303 // satisfied by the same network, if any, that satisfies the default
3304 // request, i.e., the system default network.
Hugo Benichie7678512017-05-09 15:19:01 +09003305 NetworkCapabilities nullCapabilities = null;
Hugo Benichi2583ef02017-02-02 17:02:36 +09003306 CallbackHandler cbHandler = new CallbackHandler(handler);
Hugo Benichie7678512017-05-09 15:19:01 +09003307 sendRequestForNetwork(nullCapabilities, networkCallback, 0, REQUEST, TYPE_NONE, cbHandler);
Erik Klinea2d29402016-03-16 15:31:39 +09003308 }
3309
3310 /**
fengludb571472015-04-21 17:12:05 -07003311 * Requests bandwidth update for a given {@link Network} and returns whether the update request
3312 * is accepted by ConnectivityService. Once accepted, ConnectivityService will poll underlying
3313 * network connection for updated bandwidth information. The caller will be notified via
3314 * {@link ConnectivityManager.NetworkCallback} if there is an update. Notice that this
Lorenzo Colitti88bc0bb2016-04-13 22:00:02 +09003315 * method assumes that the caller has previously called
3316 * {@link #registerNetworkCallback(NetworkRequest, NetworkCallback)} to listen for network
3317 * changes.
fenglub15e72b2015-03-20 11:29:56 -07003318 *
fengluae519192015-04-27 14:28:04 -07003319 * @param network {@link Network} specifying which network you're interested.
fengludb571472015-04-21 17:12:05 -07003320 * @return {@code true} on success, {@code false} if the {@link Network} is no longer valid.
fenglub15e72b2015-03-20 11:29:56 -07003321 */
fengludb571472015-04-21 17:12:05 -07003322 public boolean requestBandwidthUpdate(Network network) {
fenglub15e72b2015-03-20 11:29:56 -07003323 try {
fengludb571472015-04-21 17:12:05 -07003324 return mService.requestBandwidthUpdate(network);
fenglub15e72b2015-03-20 11:29:56 -07003325 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07003326 throw e.rethrowFromSystemServer();
fenglub15e72b2015-03-20 11:29:56 -07003327 }
3328 }
3329
3330 /**
Hugo Benichidafed3d2017-03-06 09:17:06 +09003331 * Unregisters a {@code NetworkCallback} and possibly releases networks originating from
Lorenzo Colitti88bc0bb2016-04-13 22:00:02 +09003332 * {@link #requestNetwork(NetworkRequest, NetworkCallback)} and
3333 * {@link #registerNetworkCallback(NetworkRequest, NetworkCallback)} calls.
3334 * If the given {@code NetworkCallback} had previously been used with
Lorenzo Colitti2ea89e52015-04-24 17:03:31 +09003335 * {@code #requestNetwork}, any networks that had been connected to only to satisfy that request
3336 * will be disconnected.
Robert Greenwalt9258c642014-03-26 16:47:06 -07003337 *
Hugo Benichidafed3d2017-03-06 09:17:06 +09003338 * Notifications that would have triggered that {@code NetworkCallback} will immediately stop
3339 * triggering it as soon as this call returns.
3340 *
Robert Greenwalt6078b502014-06-11 16:05:07 -07003341 * @param networkCallback The {@link NetworkCallback} used when making the request.
Robert Greenwalt9258c642014-03-26 16:47:06 -07003342 */
Robert Greenwalt6078b502014-06-11 16:05:07 -07003343 public void unregisterNetworkCallback(NetworkCallback networkCallback) {
Hugo Benichie7678512017-05-09 15:19:01 +09003344 checkCallbackNotNull(networkCallback);
Hugo Benichidafed3d2017-03-06 09:17:06 +09003345 final List<NetworkRequest> reqs = new ArrayList<>();
3346 // Find all requests associated to this callback and stop callback triggers immediately.
3347 // Callback is reusable immediately. http://b/20701525, http://b/35921499.
3348 synchronized (sCallbacks) {
Hugo Benichi31c176d2017-06-17 13:14:12 +09003349 Preconditions.checkArgument(networkCallback.networkRequest != null,
3350 "NetworkCallback was not registered");
3351 Preconditions.checkArgument(networkCallback.networkRequest != ALREADY_UNREGISTERED,
3352 "NetworkCallback was already unregistered");
Hugo Benichidafed3d2017-03-06 09:17:06 +09003353 for (Map.Entry<NetworkRequest, NetworkCallback> e : sCallbacks.entrySet()) {
3354 if (e.getValue() == networkCallback) {
3355 reqs.add(e.getKey());
3356 }
3357 }
3358 // TODO: throw exception if callback was registered more than once (http://b/20701525).
3359 for (NetworkRequest r : reqs) {
3360 try {
3361 mService.releaseNetworkRequest(r);
3362 } catch (RemoteException e) {
3363 throw e.rethrowFromSystemServer();
3364 }
3365 // Only remove mapping if rpc was successful.
3366 sCallbacks.remove(r);
3367 }
Hugo Benichi31c176d2017-06-17 13:14:12 +09003368 networkCallback.networkRequest = ALREADY_UNREGISTERED;
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07003369 }
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) {
Hugo Benichie7678512017-05-09 15:19:01 +09003382 checkPendingIntentNotNull(operation);
Paul Jensenf2c1cfe2015-06-30 14:29:18 -04003383 releaseNetworkRequest(operation);
3384 }
3385
3386 /**
Lorenzo Colittie03c3c72015-04-03 16:38:52 +09003387 * Informs the system whether it should switch to {@code network} regardless of whether it is
3388 * validated or not. If {@code accept} is true, and the network was explicitly selected by the
3389 * user (e.g., by selecting a Wi-Fi network in the Settings app), then the network will become
3390 * the system default network regardless of any other network that's currently connected. If
3391 * {@code always} is true, then the choice is remembered, so that the next time the user
3392 * connects to this network, the system will switch to it.
3393 *
Lorenzo Colittie03c3c72015-04-03 16:38:52 +09003394 * @param network The network to accept.
3395 * @param accept Whether to accept the network even if unvalidated.
3396 * @param always Whether to remember this choice in the future.
3397 *
3398 * @hide
3399 */
Jeff Sharkey30e06bb2017-04-24 11:18:03 -06003400 @RequiresPermission(android.Manifest.permission.CONNECTIVITY_INTERNAL)
Lorenzo Colittie03c3c72015-04-03 16:38:52 +09003401 public void setAcceptUnvalidated(Network network, boolean accept, boolean always) {
3402 try {
3403 mService.setAcceptUnvalidated(network, accept, always);
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07003404 } catch (RemoteException e) {
3405 throw e.rethrowFromSystemServer();
3406 }
Lorenzo Colittie03c3c72015-04-03 16:38:52 +09003407 }
3408
3409 /**
Lorenzo Colitti165c51c2016-09-19 01:00:19 +09003410 * Informs the system to penalize {@code network}'s score when it becomes unvalidated. This is
3411 * only meaningful if the system is configured not to penalize such networks, e.g., if the
3412 * {@code config_networkAvoidBadWifi} configuration variable is set to 0 and the {@code
3413 * NETWORK_AVOID_BAD_WIFI setting is unset}.
3414 *
Lorenzo Colitti165c51c2016-09-19 01:00:19 +09003415 * @param network The network to accept.
3416 *
3417 * @hide
3418 */
Jeff Sharkey30e06bb2017-04-24 11:18:03 -06003419 @RequiresPermission(android.Manifest.permission.CONNECTIVITY_INTERNAL)
Lorenzo Colitti165c51c2016-09-19 01:00:19 +09003420 public void setAvoidUnvalidated(Network network) {
3421 try {
3422 mService.setAvoidUnvalidated(network);
3423 } catch (RemoteException e) {
3424 throw e.rethrowFromSystemServer();
3425 }
3426 }
3427
3428 /**
Lorenzo Colitti4734cdb2017-04-27 14:30:21 +09003429 * Requests that the system open the captive portal app on the specified network.
3430 *
3431 * @param network The network to log into.
3432 *
3433 * @hide
3434 */
3435 @RequiresPermission(android.Manifest.permission.CONNECTIVITY_INTERNAL)
3436 public void startCaptivePortalApp(Network network) {
3437 try {
3438 mService.startCaptivePortalApp(network);
3439 } catch (RemoteException e) {
3440 throw e.rethrowFromSystemServer();
3441 }
3442 }
3443
3444 /**
Lorenzo Colitti2de49252017-01-24 18:08:41 +09003445 * It is acceptable to briefly use multipath data to provide seamless connectivity for
3446 * time-sensitive user-facing operations when the system default network is temporarily
Lorenzo Colitti15fd4392017-04-28 00:56:30 +09003447 * unresponsive. The amount of data should be limited (less than one megabyte for every call to
3448 * this method), and the operation should be infrequent to ensure that data usage is limited.
Lorenzo Colitti2de49252017-01-24 18:08:41 +09003449 *
3450 * An example of such an operation might be a time-sensitive foreground activity, such as a
3451 * voice command, that the user is performing while walking out of range of a Wi-Fi network.
3452 */
3453 public static final int MULTIPATH_PREFERENCE_HANDOVER = 1 << 0;
3454
3455 /**
3456 * It is acceptable to use small amounts of multipath data on an ongoing basis to provide
3457 * a backup channel for traffic that is primarily going over another network.
3458 *
3459 * An example might be maintaining backup connections to peers or servers for the purpose of
3460 * fast fallback if the default network is temporarily unresponsive or disconnects. The traffic
3461 * on backup paths should be negligible compared to the traffic on the main path.
3462 */
3463 public static final int MULTIPATH_PREFERENCE_RELIABILITY = 1 << 1;
3464
3465 /**
3466 * It is acceptable to use metered data to improve network latency and performance.
3467 */
3468 public static final int MULTIPATH_PREFERENCE_PERFORMANCE = 1 << 2;
3469
3470 /**
3471 * Return value to use for unmetered networks. On such networks we currently set all the flags
3472 * to true.
3473 * @hide
3474 */
3475 public static final int MULTIPATH_PREFERENCE_UNMETERED =
3476 MULTIPATH_PREFERENCE_HANDOVER |
3477 MULTIPATH_PREFERENCE_RELIABILITY |
3478 MULTIPATH_PREFERENCE_PERFORMANCE;
3479
3480 /** @hide */
3481 @Retention(RetentionPolicy.SOURCE)
3482 @IntDef(flag = true, value = {
3483 MULTIPATH_PREFERENCE_HANDOVER,
3484 MULTIPATH_PREFERENCE_RELIABILITY,
3485 MULTIPATH_PREFERENCE_PERFORMANCE,
3486 })
3487 public @interface MultipathPreference {
3488 }
3489
3490 /**
3491 * Provides a hint to the calling application on whether it is desirable to use the
3492 * multinetwork APIs (e.g., {@link Network#openConnection}, {@link Network#bindSocket}, etc.)
3493 * for multipath data transfer on this network when it is not the system default network.
3494 * Applications desiring to use multipath network protocols should call this method before
3495 * each such operation.
Lorenzo Colitti2de49252017-01-24 18:08:41 +09003496 *
3497 * @param network The network on which the application desires to use multipath data.
3498 * If {@code null}, this method will return the a preference that will generally
3499 * apply to metered networks.
3500 * @return a bitwise OR of zero or more of the {@code MULTIPATH_PREFERENCE_*} constants.
3501 */
Jeff Sharkey30e06bb2017-04-24 11:18:03 -06003502 @RequiresPermission(android.Manifest.permission.ACCESS_NETWORK_STATE)
Lorenzo Colitti2de49252017-01-24 18:08:41 +09003503 public @MultipathPreference int getMultipathPreference(Network network) {
3504 try {
3505 return mService.getMultipathPreference(network);
3506 } catch (RemoteException e) {
3507 throw e.rethrowFromSystemServer();
3508 }
3509 }
3510
3511 /**
Stuart Scott984dc852015-03-30 13:17:11 -07003512 * Resets all connectivity manager settings back to factory defaults.
3513 * @hide
3514 */
3515 public void factoryReset() {
Stuart Scott984dc852015-03-30 13:17:11 -07003516 try {
Stuart Scottf1fb3972015-04-02 18:00:02 -07003517 mService.factoryReset();
Stuart Scott984dc852015-03-30 13:17:11 -07003518 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07003519 throw e.rethrowFromSystemServer();
Stuart Scott984dc852015-03-30 13:17:11 -07003520 }
3521 }
3522
3523 /**
Paul Jensen6d3ff9e2014-05-29 10:12:39 -04003524 * Binds the current process to {@code network}. All Sockets created in the future
3525 * (and not explicitly bound via a bound SocketFactory from
3526 * {@link Network#getSocketFactory() Network.getSocketFactory()}) will be bound to
3527 * {@code network}. All host name resolutions will be limited to {@code network} as well.
3528 * Note that if {@code network} ever disconnects, all Sockets created in this way will cease to
3529 * work and all host name resolutions will fail. This is by design so an application doesn't
3530 * accidentally use Sockets it thinks are still bound to a particular {@link Network}.
3531 * To clear binding pass {@code null} for {@code network}. Using individually bound
3532 * Sockets created by Network.getSocketFactory().createSocket() and
3533 * performing network-specific host name resolutions via
3534 * {@link Network#getAllByName Network.getAllByName} is preferred to calling
Paul Jensen72db88e2015-03-10 10:54:12 -04003535 * {@code bindProcessToNetwork}.
Paul Jensen6d3ff9e2014-05-29 10:12:39 -04003536 *
3537 * @param network The {@link Network} to bind the current process to, or {@code null} to clear
3538 * the current binding.
3539 * @return {@code true} on success, {@code false} if the {@link Network} is no longer valid.
3540 */
Paul Jensen72db88e2015-03-10 10:54:12 -04003541 public boolean bindProcessToNetwork(Network network) {
3542 // Forcing callers to call thru non-static function ensures ConnectivityManager
3543 // instantiated.
3544 return setProcessDefaultNetwork(network);
3545 }
3546
3547 /**
3548 * Binds the current process to {@code network}. All Sockets created in the future
3549 * (and not explicitly bound via a bound SocketFactory from
3550 * {@link Network#getSocketFactory() Network.getSocketFactory()}) will be bound to
3551 * {@code network}. All host name resolutions will be limited to {@code network} as well.
3552 * Note that if {@code network} ever disconnects, all Sockets created in this way will cease to
3553 * work and all host name resolutions will fail. This is by design so an application doesn't
3554 * accidentally use Sockets it thinks are still bound to a particular {@link Network}.
3555 * To clear binding pass {@code null} for {@code network}. Using individually bound
3556 * Sockets created by Network.getSocketFactory().createSocket() and
3557 * performing network-specific host name resolutions via
3558 * {@link Network#getAllByName Network.getAllByName} is preferred to calling
3559 * {@code setProcessDefaultNetwork}.
3560 *
3561 * @param network The {@link Network} to bind the current process to, or {@code null} to clear
3562 * the current binding.
3563 * @return {@code true} on success, {@code false} if the {@link Network} is no longer valid.
3564 * @deprecated This function can throw {@link IllegalStateException}. Use
3565 * {@link #bindProcessToNetwork} instead. {@code bindProcessToNetwork}
3566 * is a direct replacement.
3567 */
Aurimas Liutikas514c5ef2016-05-24 15:22:55 -07003568 @Deprecated
Paul Jensen6d3ff9e2014-05-29 10:12:39 -04003569 public static boolean setProcessDefaultNetwork(Network network) {
Paul Jensenc91b5342014-08-27 12:38:45 -04003570 int netId = (network == null) ? NETID_UNSET : network.netId;
Paul Jensen72db88e2015-03-10 10:54:12 -04003571 if (netId == NetworkUtils.getBoundNetworkForProcess()) {
Paul Jensenc91b5342014-08-27 12:38:45 -04003572 return true;
3573 }
3574 if (NetworkUtils.bindProcessToNetwork(netId)) {
Paul Jensene0bef712014-12-10 15:12:18 -05003575 // Set HTTP proxy system properties to match network.
3576 // TODO: Deprecate this static method and replace it with a non-static version.
Lorenzo Colittiec4c5552015-04-22 11:52:48 +09003577 try {
3578 Proxy.setHttpProxySystemProperty(getInstance().getDefaultProxy());
3579 } catch (SecurityException e) {
3580 // The process doesn't have ACCESS_NETWORK_STATE, so we can't fetch the proxy.
3581 Log.e(TAG, "Can't set proxy properties", e);
3582 }
Paul Jensenc91b5342014-08-27 12:38:45 -04003583 // Must flush DNS cache as new network may have different DNS resolutions.
3584 InetAddress.clearDnsCache();
3585 // Must flush socket pool as idle sockets will be bound to previous network and may
3586 // cause subsequent fetches to be performed on old network.
3587 NetworkEventDispatcher.getInstance().onNetworkConfigurationChanged();
3588 return true;
3589 } else {
3590 return false;
3591 }
Paul Jensen6d3ff9e2014-05-29 10:12:39 -04003592 }
3593
3594 /**
3595 * Returns the {@link Network} currently bound to this process via
Paul Jensen72db88e2015-03-10 10:54:12 -04003596 * {@link #bindProcessToNetwork}, or {@code null} if no {@link Network} is explicitly bound.
Paul Jensen6d3ff9e2014-05-29 10:12:39 -04003597 *
3598 * @return {@code Network} to which this process is bound, or {@code null}.
3599 */
Paul Jensen72db88e2015-03-10 10:54:12 -04003600 public Network getBoundNetworkForProcess() {
3601 // Forcing callers to call thru non-static function ensures ConnectivityManager
3602 // instantiated.
3603 return getProcessDefaultNetwork();
3604 }
3605
3606 /**
3607 * Returns the {@link Network} currently bound to this process via
3608 * {@link #bindProcessToNetwork}, or {@code null} if no {@link Network} is explicitly bound.
3609 *
3610 * @return {@code Network} to which this process is bound, or {@code null}.
3611 * @deprecated Using this function can lead to other functions throwing
3612 * {@link IllegalStateException}. Use {@link #getBoundNetworkForProcess} instead.
3613 * {@code getBoundNetworkForProcess} is a direct replacement.
3614 */
Aurimas Liutikas514c5ef2016-05-24 15:22:55 -07003615 @Deprecated
Paul Jensen6d3ff9e2014-05-29 10:12:39 -04003616 public static Network getProcessDefaultNetwork() {
Paul Jensen72db88e2015-03-10 10:54:12 -04003617 int netId = NetworkUtils.getBoundNetworkForProcess();
Paul Jensenbcc76d32014-07-11 08:17:29 -04003618 if (netId == NETID_UNSET) return null;
Paul Jensen6d3ff9e2014-05-29 10:12:39 -04003619 return new Network(netId);
3620 }
3621
Lorenzo Colittiffc42b02015-07-29 11:41:21 +09003622 private void unsupportedStartingFrom(int version) {
3623 if (Process.myUid() == Process.SYSTEM_UID) {
3624 // The getApplicationInfo() call we make below is not supported in system context, and
3625 // we want to allow the system to use these APIs anyway.
3626 return;
3627 }
3628
3629 if (mContext.getApplicationInfo().targetSdkVersion >= version) {
3630 throw new UnsupportedOperationException(
3631 "This method is not supported in target SDK version " + version + " and above");
3632 }
3633 }
3634
3635 // Checks whether the calling app can use the legacy routing API (startUsingNetworkFeature,
3636 // stopUsingNetworkFeature, requestRouteToHost), and if not throw UnsupportedOperationException.
Lifu Tang30f95a72016-01-07 23:20:38 -08003637 // TODO: convert the existing system users (Tethering, GnssLocationProvider) to the new APIs and
Lorenzo Colittiffc42b02015-07-29 11:41:21 +09003638 // remove these exemptions. Note that this check is not secure, and apps can still access these
3639 // functions by accessing ConnectivityService directly. However, it should be clear that doing
3640 // so is unsupported and may break in the future. http://b/22728205
3641 private void checkLegacyRoutingApiAccess() {
3642 if (mContext.checkCallingOrSelfPermission("com.android.permission.INJECT_OMADM_SETTINGS")
3643 == PackageManager.PERMISSION_GRANTED) {
3644 return;
3645 }
3646
Dianne Hackborn692a2442015-07-31 10:35:34 -07003647 unsupportedStartingFrom(VERSION_CODES.M);
Lorenzo Colittiffc42b02015-07-29 11:41:21 +09003648 }
3649
Paul Jensen6d3ff9e2014-05-29 10:12:39 -04003650 /**
3651 * Binds host resolutions performed by this process to {@code network}.
Paul Jensen72db88e2015-03-10 10:54:12 -04003652 * {@link #bindProcessToNetwork} takes precedence over this setting.
Paul Jensen6d3ff9e2014-05-29 10:12:39 -04003653 *
3654 * @param network The {@link Network} to bind host resolutions from the current process to, or
3655 * {@code null} to clear the current binding.
3656 * @return {@code true} on success, {@code false} if the {@link Network} is no longer valid.
3657 * @hide
3658 * @deprecated This is strictly for legacy usage to support {@link #startUsingNetworkFeature}.
3659 */
Aurimas Liutikas514c5ef2016-05-24 15:22:55 -07003660 @Deprecated
Paul Jensen6d3ff9e2014-05-29 10:12:39 -04003661 public static boolean setProcessDefaultNetworkForHostResolution(Network network) {
Paul Jensenbcc76d32014-07-11 08:17:29 -04003662 return NetworkUtils.bindProcessToNetworkForHostResolution(
3663 network == null ? NETID_UNSET : network.netId);
Paul Jensen6d3ff9e2014-05-29 10:12:39 -04003664 }
Felipe Leme1b103232016-01-22 09:44:57 -08003665
3666 /**
3667 * Device is not restricting metered network activity while application is running on
3668 * background.
3669 */
3670 public static final int RESTRICT_BACKGROUND_STATUS_DISABLED = 1;
3671
3672 /**
3673 * Device is restricting metered network activity while application is running on background,
3674 * but application is allowed to bypass it.
3675 * <p>
3676 * In this state, application should take action to mitigate metered network access.
3677 * For example, a music streaming application should switch to a low-bandwidth bitrate.
3678 */
3679 public static final int RESTRICT_BACKGROUND_STATUS_WHITELISTED = 2;
3680
3681 /**
3682 * Device is restricting metered network activity while application is running on background.
Felipe Leme9778f762016-01-27 14:46:39 -08003683 * <p>
Felipe Leme1b103232016-01-22 09:44:57 -08003684 * In this state, application should not try to use the network while running on background,
3685 * because it would be denied.
3686 */
3687 public static final int RESTRICT_BACKGROUND_STATUS_ENABLED = 3;
3688
Felipe Leme9778f762016-01-27 14:46:39 -08003689 /**
3690 * A change in the background metered network activity restriction has occurred.
3691 * <p>
3692 * Applications should call {@link #getRestrictBackgroundStatus()} to check if the restriction
3693 * applies to them.
3694 * <p>
3695 * This is only sent to registered receivers, not manifest receivers.
3696 */
3697 @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION)
3698 public static final String ACTION_RESTRICT_BACKGROUND_CHANGED =
3699 "android.net.conn.RESTRICT_BACKGROUND_CHANGED";
3700
Felipe Lemeecfccea2016-01-25 11:48:04 -08003701 /** @hide */
3702 @Retention(RetentionPolicy.SOURCE)
Felipe Leme1b103232016-01-22 09:44:57 -08003703 @IntDef(flag = false, value = {
3704 RESTRICT_BACKGROUND_STATUS_DISABLED,
3705 RESTRICT_BACKGROUND_STATUS_WHITELISTED,
3706 RESTRICT_BACKGROUND_STATUS_ENABLED,
3707 })
Felipe Leme1b103232016-01-22 09:44:57 -08003708 public @interface RestrictBackgroundStatus {
3709 }
3710
3711 private INetworkPolicyManager getNetworkPolicyManager() {
3712 synchronized (this) {
3713 if (mNPManager != null) {
3714 return mNPManager;
3715 }
3716 mNPManager = INetworkPolicyManager.Stub.asInterface(ServiceManager
3717 .getService(Context.NETWORK_POLICY_SERVICE));
3718 return mNPManager;
3719 }
3720 }
3721
3722 /**
3723 * Determines if the calling application is subject to metered network restrictions while
3724 * running on background.
Felipe Lemec9c7be52016-05-16 13:57:19 -07003725 *
3726 * @return {@link #RESTRICT_BACKGROUND_STATUS_DISABLED},
3727 * {@link #RESTRICT_BACKGROUND_STATUS_ENABLED},
3728 * or {@link #RESTRICT_BACKGROUND_STATUS_WHITELISTED}
Felipe Leme1b103232016-01-22 09:44:57 -08003729 */
3730 public @RestrictBackgroundStatus int getRestrictBackgroundStatus() {
3731 try {
3732 return getNetworkPolicyManager().getRestrictBackgroundByCaller();
3733 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07003734 throw e.rethrowFromSystemServer();
Felipe Leme1b103232016-01-22 09:44:57 -08003735 }
3736 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003737}