blob: d5924889431a000b3a3be5c5f66f6eb555c723ea [file] [log] [blame]
The Android Open Source Project28527d22009-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 Project28527d22009-03-03 19:31:44 -080016package android.net;
17
Junyu Laia62493f2021-01-19 11:10:56 +000018import static android.annotation.SystemApi.Client.MODULE_LIBRARIES;
Junyu Laia62493f2021-01-19 11:10:56 +000019import static android.net.NetworkRequest.Type.BACKGROUND_REQUEST;
junyulaiad010792021-01-11 16:53:38 +080020import static android.net.NetworkRequest.Type.LISTEN;
junyulai1b1c8742021-03-12 20:05:08 +080021import static android.net.NetworkRequest.Type.LISTEN_FOR_BEST;
junyulaiad010792021-01-11 16:53:38 +080022import static android.net.NetworkRequest.Type.REQUEST;
23import static android.net.NetworkRequest.Type.TRACK_DEFAULT;
Lorenzo Colitti76b639e2021-01-29 20:14:04 +090024import static android.net.NetworkRequest.Type.TRACK_SYSTEM_DEFAULT;
Daniel Brightf9e945b2020-06-15 16:10:01 -070025import static android.net.QosCallback.QosCallbackRegistrationException;
junyulaid05a1922019-01-15 11:32:44 +080026
junyulai4c95b082018-12-27 17:25:29 +080027import android.annotation.CallbackExecutor;
Felipe Leme30511352016-01-22 09:44:57 -080028import android.annotation.IntDef;
Chalard Jean158702d2019-01-07 19:26:34 +090029import android.annotation.NonNull;
Robin Leee5d5ed52016-01-05 18:03:46 +000030import android.annotation.Nullable;
Jeff Sharkey656584a2017-04-24 11:18:03 -060031import android.annotation.RequiresPermission;
The Android Open Source Project28527d22009-03-03 19:31:44 -080032import android.annotation.SdkConstant;
33import android.annotation.SdkConstant.SdkConstantType;
Junyu Laia62493f2021-01-19 11:10:56 +000034import android.annotation.SuppressLint;
Udam Sainicd645462016-01-04 12:16:14 -080035import android.annotation.SystemApi;
Jeff Sharkey9b39e9a2017-06-02 17:36:26 -060036import android.annotation.SystemService;
Robert Greenwaltf99b8392014-03-26 16:47:06 -070037import android.app.PendingIntent;
Lorenzo Colitti79c6f222021-03-18 00:54:57 +090038import android.app.admin.DevicePolicyManager;
Artur Satayev9c2add62019-12-10 17:47:52 +000039import android.compat.annotation.UnsupportedAppUsage;
Lorenzo Colitti79c6f222021-03-18 00:54:57 +090040import android.content.ComponentName;
Jeff Sharkey2d9e5a52012-04-04 20:40:58 -070041import android.content.Context;
Robert Greenwaltf3017f72014-05-18 23:07:25 -070042import android.content.Intent;
Remi NGUYEN VANd2d60c62021-04-08 16:26:20 +090043import android.net.ConnectivityDiagnosticsManager.DataStallReport.DetectionMethod;
junyulai4c95b082018-12-27 17:25:29 +080044import android.net.IpSecManager.UdpEncapsulationSocket;
45import android.net.SocketKeepalive.Callback;
markchien91c78e52020-01-20 19:31:56 +080046import android.net.TetheringManager.StartTetheringCallback;
markchien6ae63e52020-01-21 13:11:06 +080047import android.net.TetheringManager.TetheringEventCallback;
markchien91c78e52020-01-20 19:31:56 +080048import android.net.TetheringManager.TetheringRequest;
Roshan Pius951c0032020-12-22 15:10:42 -080049import android.net.wifi.WifiNetworkSuggestion;
Robert Greenwalt2034b912009-08-12 16:08:25 -070050import android.os.Binder;
Mathew Inwoodbdfc1fc2018-12-20 15:30:45 +000051import android.os.Build;
Jeff Sharkey39c01eb2011-08-16 14:37:57 -070052import android.os.Build.VERSION_CODES;
Jeremy Klein3dabcb92016-01-22 14:11:45 -080053import android.os.Bundle;
Robert Greenwaltf99b8392014-03-26 16:47:06 -070054import android.os.Handler;
Dianne Hackborn5ac88162014-02-26 16:20:52 -080055import android.os.IBinder;
Robert Greenwaltf99b8392014-03-26 16:47:06 -070056import android.os.Looper;
57import android.os.Message;
Robert Greenwalt15a41532012-08-21 19:27:00 -070058import android.os.Messenger;
junyulai7e06ad42019-03-04 22:45:36 +080059import android.os.ParcelFileDescriptor;
Cody Kestingf53a0752020-04-15 12:33:28 -070060import android.os.PersistableBundle;
Lorenzo Colitti2780e4b2015-07-29 11:41:21 +090061import android.os.Process;
The Android Open Source Project28527d22009-03-03 19:31:44 -080062import android.os.RemoteException;
Jeremy Klein3dabcb92016-01-22 14:11:45 -080063import android.os.ResultReceiver;
Hugo Benichia590ab82017-05-11 13:16:17 +090064import android.os.ServiceSpecificException;
Chalard Jeanfa45a682021-02-25 17:23:40 +090065import android.os.UserHandle;
Jeff Sharkey971cd162011-08-29 16:02:57 -070066import android.provider.Settings;
Wink Saville689f7042014-12-05 11:10:30 -080067import android.telephony.SubscriptionManager;
Meng Wanged6f4412019-11-18 17:10:00 -080068import android.telephony.TelephonyManager;
Dianne Hackborn5ac88162014-02-26 16:20:52 -080069import android.util.ArrayMap;
Robert Greenwaltf99b8392014-03-26 16:47:06 -070070import android.util.Log;
Lorenzo Colitti3f54f102020-12-12 00:51:11 +090071import android.util.Range;
Erik Klinece55eb12017-01-26 18:08:28 +090072import android.util.SparseIntArray;
The Android Open Source Project28527d22009-03-03 19:31:44 -080073
markchien6ae63e52020-01-21 13:11:06 +080074import com.android.internal.annotations.GuardedBy;
Robert Greenwalt0c150c02014-05-21 20:04:36 -070075
Paul Jensen3e2917c2014-08-27 12:38:45 -040076import libcore.net.event.NetworkEventDispatcher;
77
junyulai7e06ad42019-03-04 22:45:36 +080078import java.io.IOException;
79import java.io.UncheckedIOException;
Felipe Leme30511352016-01-22 09:44:57 -080080import java.lang.annotation.Retention;
81import java.lang.annotation.RetentionPolicy;
Lorenzo Colitti3f54f102020-12-12 00:51:11 +090082import java.net.DatagramSocket;
Jeremy Klein434835a2015-12-28 15:11:58 -080083import java.net.InetAddress;
Jeff Vander Stoep39a51e02018-07-23 10:57:53 -070084import java.net.InetSocketAddress;
junyulai0835a1e2019-01-08 20:04:33 +080085import java.net.Socket;
Hugo Benichi2aa65af2017-03-06 09:17:06 +090086import java.util.ArrayList;
Lorenzo Colitti3f54f102020-12-12 00:51:11 +090087import java.util.Collection;
Jeremy Klein434835a2015-12-28 15:11:58 -080088import java.util.HashMap;
Hugo Benichi2aa65af2017-03-06 09:17:06 +090089import java.util.List;
90import java.util.Map;
markchien6ae63e52020-01-21 13:11:06 +080091import java.util.Objects;
junyulai4c95b082018-12-27 17:25:29 +080092import java.util.concurrent.Executor;
junyulai070f9ff2019-01-16 20:23:34 +080093import java.util.concurrent.ExecutorService;
94import java.util.concurrent.Executors;
95import java.util.concurrent.RejectedExecutionException;
Jeremy Klein434835a2015-12-28 15:11:58 -080096
The Android Open Source Project28527d22009-03-03 19:31:44 -080097/**
98 * Class that answers queries about the state of network connectivity. It also
Jeff Sharkey9b39e9a2017-06-02 17:36:26 -060099 * notifies applications when network connectivity changes.
The Android Open Source Project28527d22009-03-03 19:31:44 -0800100 * <p>
101 * The primary responsibilities of this class are to:
102 * <ol>
103 * <li>Monitor network connections (Wi-Fi, GPRS, UMTS, etc.)</li>
104 * <li>Send broadcast intents when network connectivity changes</li>
105 * <li>Attempt to "fail over" to another network when connectivity to a network
106 * is lost</li>
107 * <li>Provide an API that allows applications to query the coarse-grained or fine-grained
108 * state of the available networks</li>
Robert Greenwaltf3017f72014-05-18 23:07:25 -0700109 * <li>Provide an API that allows applications to request and select networks for their data
110 * traffic</li>
The Android Open Source Project28527d22009-03-03 19:31:44 -0800111 * </ol>
112 */
Jeff Sharkey9b39e9a2017-06-02 17:36:26 -0600113@SystemService(Context.CONNECTIVITY_SERVICE)
Jeff Sharkey921ebf22011-05-19 17:12:49 -0700114public class ConnectivityManager {
115 private static final String TAG = "ConnectivityManager";
Soi, Yoshinarib8102ef2015-11-12 12:09:02 +0900116 private static final boolean DEBUG = Log.isLoggable(TAG, Log.DEBUG);
Jeff Sharkey921ebf22011-05-19 17:12:49 -0700117
The Android Open Source Project28527d22009-03-03 19:31:44 -0800118 /**
Robert Greenwaltf3017f72014-05-18 23:07:25 -0700119 * A change in network connectivity has occurred. A default connection has either
The Android Open Source Project28527d22009-03-03 19:31:44 -0800120 * been established or lost. The NetworkInfo for the affected network is
121 * sent as an extra; it should be consulted to see what kind of
122 * connectivity event occurred.
123 * <p/>
Mark Lu3e422ac2016-12-05 10:57:55 -0800124 * Apps targeting Android 7.0 (API level 24) and higher do not receive this
125 * broadcast if they declare the broadcast receiver in their manifest. Apps
126 * will still receive broadcasts if they register their
127 * {@link android.content.BroadcastReceiver} with
128 * {@link android.content.Context#registerReceiver Context.registerReceiver()}
129 * and that context is still valid.
130 * <p/>
The Android Open Source Project28527d22009-03-03 19:31:44 -0800131 * If this is a connection that was the result of failing over from a
132 * disconnected network, then the FAILOVER_CONNECTION boolean extra is
133 * set to true.
134 * <p/>
135 * For a loss of connectivity, if the connectivity manager is attempting
136 * to connect (or has already connected) to another network, the
137 * NetworkInfo for the new network is also passed as an extra. This lets
138 * any receivers of the broadcast know that they should not necessarily
139 * tell the user that no data traffic will be possible. Instead, the
Robert Greenwaltfaa4b402013-02-15 10:56:35 -0800140 * receiver should expect another broadcast soon, indicating either that
The Android Open Source Project28527d22009-03-03 19:31:44 -0800141 * the failover attempt succeeded (and so there is still overall data
142 * connectivity), or that the failover attempt failed, meaning that all
143 * connectivity has been lost.
144 * <p/>
145 * For a disconnect event, the boolean extra EXTRA_NO_CONNECTIVITY
146 * is set to {@code true} if there are no connected networks at all.
Chalard Jean52e23962018-02-10 05:33:50 +0900147 *
148 * @deprecated apps should use the more versatile {@link #requestNetwork},
149 * {@link #registerNetworkCallback} or {@link #registerDefaultNetworkCallback}
150 * functions instead for faster and more detailed updates about the network
151 * changes they care about.
The Android Open Source Project28527d22009-03-03 19:31:44 -0800152 */
Jeff Sharkeyee9275b2013-02-20 18:21:19 -0800153 @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION)
Chalard Jean52e23962018-02-10 05:33:50 +0900154 @Deprecated
The Android Open Source Project28527d22009-03-03 19:31:44 -0800155 public static final String CONNECTIVITY_ACTION = "android.net.conn.CONNECTIVITY_CHANGE";
Jeff Sharkey66fa9682011-08-02 17:22:34 -0700156
The Android Open Source Project28527d22009-03-03 19:31:44 -0800157 /**
Paul Jensen60df4aa2015-02-27 22:55:47 -0500158 * The device has connected to a network that has presented a captive
159 * portal, which is blocking Internet connectivity. The user was presented
160 * with a notification that network sign in is required,
161 * and the user invoked the notification's action indicating they
Paul Jensen75e0adb2015-05-22 10:50:39 -0400162 * desire to sign in to the network. Apps handling this activity should
Paul Jensen60df4aa2015-02-27 22:55:47 -0500163 * facilitate signing in to the network. This action includes a
164 * {@link Network} typed extra called {@link #EXTRA_NETWORK} that represents
165 * the network presenting the captive portal; all communication with the
166 * captive portal must be done using this {@code Network} object.
167 * <p/>
Paul Jensen75e0adb2015-05-22 10:50:39 -0400168 * This activity includes a {@link CaptivePortal} extra named
169 * {@link #EXTRA_CAPTIVE_PORTAL} that can be used to indicate different
170 * outcomes of the captive portal sign in to the system:
171 * <ul>
172 * <li> When the app handling this action believes the user has signed in to
173 * the network and the captive portal has been dismissed, the app should
174 * call {@link CaptivePortal#reportCaptivePortalDismissed} so the system can
175 * reevaluate the network. If reevaluation finds the network no longer
176 * subject to a captive portal, the network may become the default active
Chalard Jean9dd11612018-06-04 16:52:49 +0900177 * data network.</li>
Paul Jensen75e0adb2015-05-22 10:50:39 -0400178 * <li> When the app handling this action believes the user explicitly wants
Paul Jensen60df4aa2015-02-27 22:55:47 -0500179 * to ignore the captive portal and the network, the app should call
Paul Jensen75e0adb2015-05-22 10:50:39 -0400180 * {@link CaptivePortal#ignoreNetwork}. </li>
181 * </ul>
Paul Jensen60df4aa2015-02-27 22:55:47 -0500182 */
183 @SdkConstant(SdkConstantType.ACTIVITY_INTENT_ACTION)
184 public static final String ACTION_CAPTIVE_PORTAL_SIGN_IN = "android.net.conn.CAPTIVE_PORTAL";
185
186 /**
The Android Open Source Project28527d22009-03-03 19:31:44 -0800187 * The lookup key for a {@link NetworkInfo} object. Retrieve with
188 * {@link android.content.Intent#getParcelableExtra(String)}.
Jeff Sharkey66fa9682011-08-02 17:22:34 -0700189 *
Chalard Jean97021a12019-01-11 16:47:53 +0900190 * @deprecated The {@link NetworkInfo} object is deprecated, as many of its properties
191 * can't accurately represent modern network characteristics.
192 * Please obtain information about networks from the {@link NetworkCapabilities}
193 * or {@link LinkProperties} objects instead.
The Android Open Source Project28527d22009-03-03 19:31:44 -0800194 */
Jeff Sharkey66fa9682011-08-02 17:22:34 -0700195 @Deprecated
The Android Open Source Project28527d22009-03-03 19:31:44 -0800196 public static final String EXTRA_NETWORK_INFO = "networkInfo";
Jeff Sharkey66fa9682011-08-02 17:22:34 -0700197
The Android Open Source Project28527d22009-03-03 19:31:44 -0800198 /**
Jeff Sharkey47905d12012-08-06 11:41:50 -0700199 * Network type which triggered a {@link #CONNECTIVITY_ACTION} broadcast.
Jeff Sharkey47905d12012-08-06 11:41:50 -0700200 *
201 * @see android.content.Intent#getIntExtra(String, int)
Chalard Jean97021a12019-01-11 16:47:53 +0900202 * @deprecated The network type is not rich enough to represent the characteristics
203 * of modern networks. Please use {@link NetworkCapabilities} instead,
204 * in particular the transports.
Jeff Sharkey47905d12012-08-06 11:41:50 -0700205 */
Chalard Jean97021a12019-01-11 16:47:53 +0900206 @Deprecated
Jeff Sharkey47905d12012-08-06 11:41:50 -0700207 public static final String EXTRA_NETWORK_TYPE = "networkType";
208
209 /**
The Android Open Source Project28527d22009-03-03 19:31:44 -0800210 * The lookup key for a boolean that indicates whether a connect event
211 * is for a network to which the connectivity manager was failing over
212 * following a disconnect on another network.
213 * Retrieve it with {@link android.content.Intent#getBooleanExtra(String,boolean)}.
junyulai9826e7f2018-12-13 12:47:51 +0800214 *
215 * @deprecated See {@link NetworkInfo}.
The Android Open Source Project28527d22009-03-03 19:31:44 -0800216 */
junyulai9826e7f2018-12-13 12:47:51 +0800217 @Deprecated
The Android Open Source Project28527d22009-03-03 19:31:44 -0800218 public static final String EXTRA_IS_FAILOVER = "isFailover";
219 /**
220 * The lookup key for a {@link NetworkInfo} object. This is supplied when
221 * there is another network that it may be possible to connect to. Retrieve with
222 * {@link android.content.Intent#getParcelableExtra(String)}.
junyulai9826e7f2018-12-13 12:47:51 +0800223 *
224 * @deprecated See {@link NetworkInfo}.
The Android Open Source Project28527d22009-03-03 19:31:44 -0800225 */
junyulai9826e7f2018-12-13 12:47:51 +0800226 @Deprecated
The Android Open Source Project28527d22009-03-03 19:31:44 -0800227 public static final String EXTRA_OTHER_NETWORK_INFO = "otherNetwork";
228 /**
229 * The lookup key for a boolean that indicates whether there is a
230 * complete lack of connectivity, i.e., no network is available.
231 * Retrieve it with {@link android.content.Intent#getBooleanExtra(String,boolean)}.
232 */
233 public static final String EXTRA_NO_CONNECTIVITY = "noConnectivity";
234 /**
235 * The lookup key for a string that indicates why an attempt to connect
236 * to a network failed. The string has no particular structure. It is
237 * intended to be used in notifications presented to users. Retrieve
238 * it with {@link android.content.Intent#getStringExtra(String)}.
239 */
240 public static final String EXTRA_REASON = "reason";
241 /**
242 * The lookup key for a string that provides optionally supplied
243 * extra information about the network state. The information
244 * may be passed up from the lower networking layers, and its
245 * meaning may be specific to a particular network type. Retrieve
246 * it with {@link android.content.Intent#getStringExtra(String)}.
junyulai9826e7f2018-12-13 12:47:51 +0800247 *
248 * @deprecated See {@link NetworkInfo#getExtraInfo()}.
The Android Open Source Project28527d22009-03-03 19:31:44 -0800249 */
junyulai9826e7f2018-12-13 12:47:51 +0800250 @Deprecated
The Android Open Source Project28527d22009-03-03 19:31:44 -0800251 public static final String EXTRA_EXTRA_INFO = "extraInfo";
Robert Greenwalt986c7412010-09-08 15:24:47 -0700252 /**
253 * The lookup key for an int that provides information about
254 * our connection to the internet at large. 0 indicates no connection,
255 * 100 indicates a great connection. Retrieve it with
Jeff Sharkey921ebf22011-05-19 17:12:49 -0700256 * {@link android.content.Intent#getIntExtra(String, int)}.
Robert Greenwalt986c7412010-09-08 15:24:47 -0700257 * {@hide}
258 */
259 public static final String EXTRA_INET_CONDITION = "inetCondition";
The Android Open Source Project28527d22009-03-03 19:31:44 -0800260 /**
Paul Jensen75e0adb2015-05-22 10:50:39 -0400261 * The lookup key for a {@link CaptivePortal} object included with the
262 * {@link #ACTION_CAPTIVE_PORTAL_SIGN_IN} intent. The {@code CaptivePortal}
263 * object can be used to either indicate to the system that the captive
264 * portal has been dismissed or that the user does not want to pursue
265 * signing in to captive portal. Retrieve it with
266 * {@link android.content.Intent#getParcelableExtra(String)}.
Paul Jensen60df4aa2015-02-27 22:55:47 -0500267 */
Paul Jensen75e0adb2015-05-22 10:50:39 -0400268 public static final String EXTRA_CAPTIVE_PORTAL = "android.net.extra.CAPTIVE_PORTAL";
Jan Nordqvist032e2672015-09-22 15:54:32 -0700269
270 /**
271 * Key for passing a URL to the captive portal login activity.
272 */
273 public static final String EXTRA_CAPTIVE_PORTAL_URL = "android.net.extra.CAPTIVE_PORTAL_URL";
274
Paul Jensen60df4aa2015-02-27 22:55:47 -0500275 /**
Remi NGUYEN VANc2491572018-05-22 10:01:53 +0900276 * Key for passing a {@link android.net.captiveportal.CaptivePortalProbeSpec} to the captive
277 * portal login activity.
278 * {@hide}
279 */
Remi NGUYEN VAN70ab67f2019-01-17 14:38:31 +0900280 @SystemApi
Remi NGUYEN VANc2491572018-05-22 10:01:53 +0900281 public static final String EXTRA_CAPTIVE_PORTAL_PROBE_SPEC =
282 "android.net.extra.CAPTIVE_PORTAL_PROBE_SPEC";
283
284 /**
Hugo Benichi454e0662016-12-14 08:23:40 +0900285 * Key for passing a user agent string to the captive portal login activity.
286 * {@hide}
287 */
Remi NGUYEN VAN70ab67f2019-01-17 14:38:31 +0900288 @SystemApi
Hugo Benichi454e0662016-12-14 08:23:40 +0900289 public static final String EXTRA_CAPTIVE_PORTAL_USER_AGENT =
290 "android.net.extra.CAPTIVE_PORTAL_USER_AGENT";
291
292 /**
Haoyu Baib5da5752012-06-20 14:29:57 -0700293 * Broadcast action to indicate the change of data activity status
294 * (idle or active) on a network in a recent period.
Robert Greenwaltfaa4b402013-02-15 10:56:35 -0800295 * The network becomes active when data transmission is started, or
296 * idle if there is no data transmission for a period of time.
Haoyu Baib5da5752012-06-20 14:29:57 -0700297 * {@hide}
298 */
299 @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION)
Chalard Jean9dd11612018-06-04 16:52:49 +0900300 public static final String ACTION_DATA_ACTIVITY_CHANGE =
301 "android.net.conn.DATA_ACTIVITY_CHANGE";
Haoyu Baib5da5752012-06-20 14:29:57 -0700302 /**
303 * The lookup key for an enum that indicates the network device type on which this data activity
304 * change happens.
305 * {@hide}
306 */
307 public static final String EXTRA_DEVICE_TYPE = "deviceType";
308 /**
309 * The lookup key for a boolean that indicates the device is active or not. {@code true} means
310 * it is actively sending or receiving data and {@code false} means it is idle.
311 * {@hide}
312 */
313 public static final String EXTRA_IS_ACTIVE = "isActive";
Ashish Sharma29f7e0e2014-03-12 18:42:23 -0700314 /**
315 * The lookup key for a long that contains the timestamp (nanos) of the radio state change.
316 * {@hide}
317 */
318 public static final String EXTRA_REALTIME_NS = "tsNanos";
Haoyu Baib5da5752012-06-20 14:29:57 -0700319
320 /**
The Android Open Source Project28527d22009-03-03 19:31:44 -0800321 * Broadcast Action: The setting for background data usage has changed
322 * values. Use {@link #getBackgroundDataSetting()} to get the current value.
323 * <p>
324 * If an application uses the network in the background, it should listen
325 * for this broadcast and stop using the background data if the value is
Jeff Sharkey921ebf22011-05-19 17:12:49 -0700326 * {@code false}.
Jeff Sharkeyc958c772012-01-30 16:29:24 -0800327 * <p>
328 *
329 * @deprecated As of {@link VERSION_CODES#ICE_CREAM_SANDWICH}, availability
330 * of background data depends on several combined factors, and
331 * this broadcast is no longer sent. Instead, when background
332 * data is unavailable, {@link #getActiveNetworkInfo()} will now
333 * appear disconnected. During first boot after a platform
334 * upgrade, this broadcast will be sent once if
335 * {@link #getBackgroundDataSetting()} was {@code false} before
336 * the upgrade.
The Android Open Source Project28527d22009-03-03 19:31:44 -0800337 */
338 @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION)
Jeff Sharkeyc958c772012-01-30 16:29:24 -0800339 @Deprecated
The Android Open Source Project28527d22009-03-03 19:31:44 -0800340 public static final String ACTION_BACKGROUND_DATA_SETTING_CHANGED =
341 "android.net.conn.BACKGROUND_DATA_SETTING_CHANGED";
342
Robert Greenwaltd3401f92010-09-15 17:36:33 -0700343 /**
344 * Broadcast Action: The network connection may not be good
345 * uses {@code ConnectivityManager.EXTRA_INET_CONDITION} and
346 * {@code ConnectivityManager.EXTRA_NETWORK_INFO} to specify
347 * the network and it's condition.
348 * @hide
349 */
Jeff Sharkeyee9275b2013-02-20 18:21:19 -0800350 @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION)
Mathew Inwoodc5b9bec2018-08-08 14:52:47 +0100351 @UnsupportedAppUsage
Robert Greenwaltd3401f92010-09-15 17:36:33 -0700352 public static final String INET_CONDITION_ACTION =
353 "android.net.conn.INET_CONDITION_ACTION";
354
Robert Greenwalt2034b912009-08-12 16:08:25 -0700355 /**
Robert Greenwaltfaa4b402013-02-15 10:56:35 -0800356 * Broadcast Action: A tetherable connection has come or gone.
357 * Uses {@code ConnectivityManager.EXTRA_AVAILABLE_TETHER},
Erik Kline762fa8e2017-04-17 16:47:23 +0900358 * {@code ConnectivityManager.EXTRA_ACTIVE_LOCAL_ONLY},
359 * {@code ConnectivityManager.EXTRA_ACTIVE_TETHER}, and
Robert Greenwaltfaa4b402013-02-15 10:56:35 -0800360 * {@code ConnectivityManager.EXTRA_ERRORED_TETHER} to indicate
361 * the current state of tethering. Each include a list of
362 * interface names in that state (may be empty).
Robert Greenwalt0c4828c2010-01-26 11:40:34 -0800363 * @hide
364 */
Jeff Sharkeyee9275b2013-02-20 18:21:19 -0800365 @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION)
Mathew Inwoode1a17ba2020-11-04 09:29:36 +0000366 @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.R, trackingBug = 170729553)
Robert Greenwalt0c4828c2010-01-26 11:40:34 -0800367 public static final String ACTION_TETHER_STATE_CHANGED =
markchiena0f8fd92019-12-25 19:40:32 +0800368 TetheringManager.ACTION_TETHER_STATE_CHANGED;
Robert Greenwalt0c4828c2010-01-26 11:40:34 -0800369
370 /**
371 * @hide
Robert Greenwaltfaa4b402013-02-15 10:56:35 -0800372 * gives a String[] listing all the interfaces configured for
373 * tethering and currently available for tethering.
Robert Greenwalt0c4828c2010-01-26 11:40:34 -0800374 */
Mathew Inwoode1a17ba2020-11-04 09:29:36 +0000375 @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.R, trackingBug = 170729553)
markchiena0f8fd92019-12-25 19:40:32 +0800376 public static final String EXTRA_AVAILABLE_TETHER = TetheringManager.EXTRA_AVAILABLE_TETHER;
Robert Greenwalt0c4828c2010-01-26 11:40:34 -0800377
378 /**
379 * @hide
Erik Kline762fa8e2017-04-17 16:47:23 +0900380 * gives a String[] listing all the interfaces currently in local-only
381 * mode (ie, has DHCPv4+IPv6-ULA support and no packet forwarding)
Robert Greenwalt0c4828c2010-01-26 11:40:34 -0800382 */
markchiena0f8fd92019-12-25 19:40:32 +0800383 public static final String EXTRA_ACTIVE_LOCAL_ONLY = TetheringManager.EXTRA_ACTIVE_LOCAL_ONLY;
Erik Kline762fa8e2017-04-17 16:47:23 +0900384
385 /**
386 * @hide
387 * gives a String[] listing all the interfaces currently tethered
388 * (ie, has DHCPv4 support and packets potentially forwarded/NATed)
389 */
Mathew Inwoode1a17ba2020-11-04 09:29:36 +0000390 @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.R, trackingBug = 170729553)
markchiena0f8fd92019-12-25 19:40:32 +0800391 public static final String EXTRA_ACTIVE_TETHER = TetheringManager.EXTRA_ACTIVE_TETHER;
Robert Greenwalt8e87f122010-02-11 18:18:40 -0800392
393 /**
394 * @hide
Robert Greenwaltfaa4b402013-02-15 10:56:35 -0800395 * gives a String[] listing all the interfaces we tried to tether and
396 * failed. Use {@link #getLastTetherError} to find the error code
397 * for any interfaces listed here.
Robert Greenwalt8e87f122010-02-11 18:18:40 -0800398 */
Mathew Inwoode1a17ba2020-11-04 09:29:36 +0000399 @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.R, trackingBug = 170729553)
markchiena0f8fd92019-12-25 19:40:32 +0800400 public static final String EXTRA_ERRORED_TETHER = TetheringManager.EXTRA_ERRORED_TETHER;
Robert Greenwalt0c4828c2010-01-26 11:40:34 -0800401
402 /**
Russell Brenner30fe2642013-02-12 10:03:14 -0800403 * Broadcast Action: The captive portal tracker has finished its test.
404 * Sent only while running Setup Wizard, in lieu of showing a user
405 * notification.
406 * @hide
407 */
Jeff Sharkeyee9275b2013-02-20 18:21:19 -0800408 @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION)
Russell Brenner30fe2642013-02-12 10:03:14 -0800409 public static final String ACTION_CAPTIVE_PORTAL_TEST_COMPLETED =
410 "android.net.conn.CAPTIVE_PORTAL_TEST_COMPLETED";
411 /**
412 * The lookup key for a boolean that indicates whether a captive portal was detected.
413 * Retrieve it with {@link android.content.Intent#getBooleanExtra(String,boolean)}.
414 * @hide
415 */
416 public static final String EXTRA_IS_CAPTIVE_PORTAL = "captivePortal";
417
418 /**
Lorenzo Colitti6947c062015-04-03 16:38:52 +0900419 * Action used to display a dialog that asks the user whether to connect to a network that is
420 * not validated. This intent is used to start the dialog in settings via startActivity.
421 *
lucaslin311b6902021-04-21 10:43:15 +0800422 * This action includes a {@link Network} typed extra which is called
423 * {@link ConnectivityManager#EXTRA_NETWORK} that represents the network which is unvalidated.
424 *
Lorenzo Colitti6947c062015-04-03 16:38:52 +0900425 * @hide
426 */
lucasline8b3f9c2021-03-04 17:09:51 +0800427 @SystemApi(client = MODULE_LIBRARIES)
428 public static final String ACTION_PROMPT_UNVALIDATED = "android.net.action.PROMPT_UNVALIDATED";
Lorenzo Colitti6947c062015-04-03 16:38:52 +0900429
430 /**
Lorenzo Colitti199ecfc2016-09-15 14:02:29 +0900431 * Action used to display a dialog that asks the user whether to avoid a network that is no
432 * longer validated. This intent is used to start the dialog in settings via startActivity.
433 *
lucaslin311b6902021-04-21 10:43:15 +0800434 * This action includes a {@link Network} typed extra which is called
435 * {@link ConnectivityManager#EXTRA_NETWORK} that represents the network which is no longer
436 * validated.
437 *
Lorenzo Colitti199ecfc2016-09-15 14:02:29 +0900438 * @hide
439 */
lucasline8b3f9c2021-03-04 17:09:51 +0800440 @SystemApi(client = MODULE_LIBRARIES)
Lorenzo Colitti199ecfc2016-09-15 14:02:29 +0900441 public static final String ACTION_PROMPT_LOST_VALIDATION =
lucasline8b3f9c2021-03-04 17:09:51 +0800442 "android.net.action.PROMPT_LOST_VALIDATION";
Lorenzo Colitti199ecfc2016-09-15 14:02:29 +0900443
444 /**
lucaslin2240ef62019-03-12 13:08:03 +0800445 * Action used to display a dialog that asks the user whether to stay connected to a network
446 * that has not validated. This intent is used to start the dialog in settings via
447 * startActivity.
448 *
lucaslin311b6902021-04-21 10:43:15 +0800449 * This action includes a {@link Network} typed extra which is called
450 * {@link ConnectivityManager#EXTRA_NETWORK} that represents the network which has partial
451 * connectivity.
452 *
lucaslin2240ef62019-03-12 13:08:03 +0800453 * @hide
454 */
lucasline8b3f9c2021-03-04 17:09:51 +0800455 @SystemApi(client = MODULE_LIBRARIES)
lucaslin2240ef62019-03-12 13:08:03 +0800456 public static final String ACTION_PROMPT_PARTIAL_CONNECTIVITY =
lucasline8b3f9c2021-03-04 17:09:51 +0800457 "android.net.action.PROMPT_PARTIAL_CONNECTIVITY";
lucaslin2240ef62019-03-12 13:08:03 +0800458
459 /**
paulhue3fe4d02021-04-07 16:18:13 +0800460 * Clear DNS Cache Action: This is broadcast when networks have changed and old
461 * DNS entries should be cleared.
462 * @hide
463 */
464 @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION)
465 @SystemApi(client = SystemApi.Client.MODULE_LIBRARIES)
466 public static final String ACTION_CLEAR_DNS_CACHE = "android.net.action.CLEAR_DNS_CACHE";
467
468 /**
Jeremy Klein3dabcb92016-01-22 14:11:45 -0800469 * Invalid tethering type.
Chalard Jean9dd11612018-06-04 16:52:49 +0900470 * @see #startTethering(int, boolean, OnStartTetheringCallback)
Jeremy Klein3dabcb92016-01-22 14:11:45 -0800471 * @hide
472 */
markchiena0f8fd92019-12-25 19:40:32 +0800473 public static final int TETHERING_INVALID = TetheringManager.TETHERING_INVALID;
Jeremy Klein3dabcb92016-01-22 14:11:45 -0800474
475 /**
476 * Wifi tethering type.
Chalard Jean9dd11612018-06-04 16:52:49 +0900477 * @see #startTethering(int, boolean, OnStartTetheringCallback)
Jeremy Klein3dabcb92016-01-22 14:11:45 -0800478 * @hide
479 */
480 @SystemApi
Remi NGUYEN VAN09f8ed22021-02-15 18:52:06 +0900481 public static final int TETHERING_WIFI = 0;
Jeremy Klein3dabcb92016-01-22 14:11:45 -0800482
483 /**
484 * USB tethering type.
Chalard Jean9dd11612018-06-04 16:52:49 +0900485 * @see #startTethering(int, boolean, OnStartTetheringCallback)
Jeremy Klein3dabcb92016-01-22 14:11:45 -0800486 * @hide
487 */
488 @SystemApi
Remi NGUYEN VAN09f8ed22021-02-15 18:52:06 +0900489 public static final int TETHERING_USB = 1;
Jeremy Klein3dabcb92016-01-22 14:11:45 -0800490
491 /**
492 * Bluetooth tethering type.
Chalard Jean9dd11612018-06-04 16:52:49 +0900493 * @see #startTethering(int, boolean, OnStartTetheringCallback)
Jeremy Klein3dabcb92016-01-22 14:11:45 -0800494 * @hide
495 */
496 @SystemApi
Remi NGUYEN VAN09f8ed22021-02-15 18:52:06 +0900497 public static final int TETHERING_BLUETOOTH = 2;
Jeremy Klein3dabcb92016-01-22 14:11:45 -0800498
499 /**
Jimmy Chendd31bc42019-07-15 18:03:23 +0800500 * Wifi P2p tethering type.
501 * Wifi P2p tethering is set through events automatically, and don't
502 * need to start from #startTethering(int, boolean, OnStartTetheringCallback).
503 * @hide
504 */
markchiena0f8fd92019-12-25 19:40:32 +0800505 public static final int TETHERING_WIFI_P2P = TetheringManager.TETHERING_WIFI_P2P;
Jimmy Chendd31bc42019-07-15 18:03:23 +0800506
507 /**
Jeremy Klein3dabcb92016-01-22 14:11:45 -0800508 * Extra used for communicating with the TetherService. Includes the type of tethering to
509 * enable if any.
510 * @hide
511 */
markchien6ae63e52020-01-21 13:11:06 +0800512 public static final String EXTRA_ADD_TETHER_TYPE = TetheringConstants.EXTRA_ADD_TETHER_TYPE;
Jeremy Klein3dabcb92016-01-22 14:11:45 -0800513
514 /**
515 * Extra used for communicating with the TetherService. Includes the type of tethering for
516 * which to cancel provisioning.
517 * @hide
518 */
markchien6ae63e52020-01-21 13:11:06 +0800519 public static final String EXTRA_REM_TETHER_TYPE = TetheringConstants.EXTRA_REM_TETHER_TYPE;
Jeremy Klein3dabcb92016-01-22 14:11:45 -0800520
521 /**
522 * Extra used for communicating with the TetherService. True to schedule a recheck of tether
523 * provisioning.
524 * @hide
525 */
markchien6ae63e52020-01-21 13:11:06 +0800526 public static final String EXTRA_SET_ALARM = TetheringConstants.EXTRA_SET_ALARM;
Jeremy Klein3dabcb92016-01-22 14:11:45 -0800527
528 /**
529 * Tells the TetherService to run a provision check now.
530 * @hide
531 */
markchien6ae63e52020-01-21 13:11:06 +0800532 public static final String EXTRA_RUN_PROVISION = TetheringConstants.EXTRA_RUN_PROVISION;
Jeremy Klein3dabcb92016-01-22 14:11:45 -0800533
534 /**
535 * Extra used for communicating with the TetherService. Contains the {@link ResultReceiver}
536 * which will receive provisioning results. Can be left empty.
537 * @hide
538 */
markchien6ae63e52020-01-21 13:11:06 +0800539 public static final String EXTRA_PROVISION_CALLBACK =
540 TetheringConstants.EXTRA_PROVISION_CALLBACK;
Jeremy Klein3dabcb92016-01-22 14:11:45 -0800541
542 /**
Robert Greenwaltfaa4b402013-02-15 10:56:35 -0800543 * The absence of a connection type.
Robert Greenwalt33cdcdf2011-06-02 17:30:47 -0700544 * @hide
545 */
paulhu74357e72020-01-13 16:46:45 +0800546 @SystemApi
Robert Greenwalt33cdcdf2011-06-02 17:30:47 -0700547 public static final int TYPE_NONE = -1;
548
549 /**
Chalard Jean7eaf3b12018-03-08 13:54:53 +0900550 * A Mobile data connection. Devices may support more than one.
551 *
552 * @deprecated Applications should instead use {@link NetworkCapabilities#hasTransport} or
553 * {@link #requestNetwork(NetworkRequest, NetworkCallback)} to request an
554 * appropriate network. {@see NetworkCapabilities} for supported transports.
Robert Greenwalt2034b912009-08-12 16:08:25 -0700555 */
Chalard Jean7eaf3b12018-03-08 13:54:53 +0900556 @Deprecated
Robert Greenwalt2034b912009-08-12 16:08:25 -0700557 public static final int TYPE_MOBILE = 0;
Chalard Jean7eaf3b12018-03-08 13:54:53 +0900558
Robert Greenwalt2034b912009-08-12 16:08:25 -0700559 /**
Chalard Jean7eaf3b12018-03-08 13:54:53 +0900560 * A WIFI data connection. Devices may support more than one.
561 *
562 * @deprecated Applications should instead use {@link NetworkCapabilities#hasTransport} or
563 * {@link #requestNetwork(NetworkRequest, NetworkCallback)} to request an
564 * appropriate network. {@see NetworkCapabilities} for supported transports.
Robert Greenwalt2034b912009-08-12 16:08:25 -0700565 */
Chalard Jean7eaf3b12018-03-08 13:54:53 +0900566 @Deprecated
Robert Greenwalt2034b912009-08-12 16:08:25 -0700567 public static final int TYPE_WIFI = 1;
Chalard Jean7eaf3b12018-03-08 13:54:53 +0900568
Robert Greenwalt2034b912009-08-12 16:08:25 -0700569 /**
Robert Greenwaltfaa4b402013-02-15 10:56:35 -0800570 * An MMS-specific Mobile data connection. This network type may use the
571 * same network interface as {@link #TYPE_MOBILE} or it may use a different
572 * one. This is used by applications needing to talk to the carrier's
573 * Multimedia Messaging Service servers.
Lorenzo Colitti21e9a152015-04-23 15:32:42 +0900574 *
Chalard Jean7eaf3b12018-03-08 13:54:53 +0900575 * @deprecated Applications should instead use {@link NetworkCapabilities#hasCapability} or
Lorenzo Colitti7f6a2bf2015-04-24 17:03:31 +0900576 * {@link #requestNetwork(NetworkRequest, NetworkCallback)} to request a network that
Lorenzo Colitti21e9a152015-04-23 15:32:42 +0900577 * provides the {@link NetworkCapabilities#NET_CAPABILITY_MMS} capability.
Robert Greenwalt2034b912009-08-12 16:08:25 -0700578 */
Aurimas Liutikase80a0eb2016-05-24 15:22:55 -0700579 @Deprecated
Robert Greenwalt2034b912009-08-12 16:08:25 -0700580 public static final int TYPE_MOBILE_MMS = 2;
Chalard Jean7eaf3b12018-03-08 13:54:53 +0900581
Robert Greenwalt2034b912009-08-12 16:08:25 -0700582 /**
Robert Greenwaltfaa4b402013-02-15 10:56:35 -0800583 * A SUPL-specific Mobile data connection. This network type may use the
584 * same network interface as {@link #TYPE_MOBILE} or it may use a different
585 * one. This is used by applications needing to talk to the carrier's
586 * Secure User Plane Location servers for help locating the device.
Lorenzo Colitti21e9a152015-04-23 15:32:42 +0900587 *
Chalard Jean7eaf3b12018-03-08 13:54:53 +0900588 * @deprecated Applications should instead use {@link NetworkCapabilities#hasCapability} or
Lorenzo Colitti7f6a2bf2015-04-24 17:03:31 +0900589 * {@link #requestNetwork(NetworkRequest, NetworkCallback)} to request a network that
Lorenzo Colitti21e9a152015-04-23 15:32:42 +0900590 * provides the {@link NetworkCapabilities#NET_CAPABILITY_SUPL} capability.
Robert Greenwalt2034b912009-08-12 16:08:25 -0700591 */
Aurimas Liutikase80a0eb2016-05-24 15:22:55 -0700592 @Deprecated
Robert Greenwalt2034b912009-08-12 16:08:25 -0700593 public static final int TYPE_MOBILE_SUPL = 3;
Chalard Jean7eaf3b12018-03-08 13:54:53 +0900594
Robert Greenwalt2034b912009-08-12 16:08:25 -0700595 /**
Robert Greenwaltfaa4b402013-02-15 10:56:35 -0800596 * A DUN-specific Mobile data connection. This network type may use the
597 * same network interface as {@link #TYPE_MOBILE} or it may use a different
598 * one. This is sometimes by the system when setting up an upstream connection
599 * for tethering so that the carrier is aware of DUN traffic.
Chalard Jean7eaf3b12018-03-08 13:54:53 +0900600 *
601 * @deprecated Applications should instead use {@link NetworkCapabilities#hasCapability} or
602 * {@link #requestNetwork(NetworkRequest, NetworkCallback)} to request a network that
603 * provides the {@link NetworkCapabilities#NET_CAPABILITY_DUN} capability.
Robert Greenwalt2034b912009-08-12 16:08:25 -0700604 */
Chalard Jean7eaf3b12018-03-08 13:54:53 +0900605 @Deprecated
Robert Greenwalt2034b912009-08-12 16:08:25 -0700606 public static final int TYPE_MOBILE_DUN = 4;
Chalard Jean7eaf3b12018-03-08 13:54:53 +0900607
Robert Greenwalt2034b912009-08-12 16:08:25 -0700608 /**
Robert Greenwaltfaa4b402013-02-15 10:56:35 -0800609 * A High Priority Mobile data connection. This network type uses the
610 * same network interface as {@link #TYPE_MOBILE} but the routing setup
Lorenzo Colitti21e9a152015-04-23 15:32:42 +0900611 * is different.
612 *
Chalard Jean7eaf3b12018-03-08 13:54:53 +0900613 * @deprecated Applications should instead use {@link NetworkCapabilities#hasTransport} or
614 * {@link #requestNetwork(NetworkRequest, NetworkCallback)} to request an
615 * appropriate network. {@see NetworkCapabilities} for supported transports.
Robert Greenwalt2034b912009-08-12 16:08:25 -0700616 */
Aurimas Liutikase80a0eb2016-05-24 15:22:55 -0700617 @Deprecated
Robert Greenwalt2034b912009-08-12 16:08:25 -0700618 public static final int TYPE_MOBILE_HIPRI = 5;
Chalard Jean7eaf3b12018-03-08 13:54:53 +0900619
jshbfa81722010-03-11 15:04:43 -0800620 /**
Chalard Jean7eaf3b12018-03-08 13:54:53 +0900621 * A WiMAX data connection.
622 *
623 * @deprecated Applications should instead use {@link NetworkCapabilities#hasTransport} or
624 * {@link #requestNetwork(NetworkRequest, NetworkCallback)} to request an
625 * appropriate network. {@see NetworkCapabilities} for supported transports.
jshbfa81722010-03-11 15:04:43 -0800626 */
Chalard Jean7eaf3b12018-03-08 13:54:53 +0900627 @Deprecated
jshbfa81722010-03-11 15:04:43 -0800628 public static final int TYPE_WIMAX = 6;
Robert Greenwalteb123ac2010-12-06 13:56:24 -0800629
Jaikumar Ganesh0db51a02010-12-21 22:31:44 -0800630 /**
Chalard Jean7eaf3b12018-03-08 13:54:53 +0900631 * A Bluetooth data connection.
632 *
633 * @deprecated Applications should instead use {@link NetworkCapabilities#hasTransport} or
634 * {@link #requestNetwork(NetworkRequest, NetworkCallback)} to request an
635 * appropriate network. {@see NetworkCapabilities} for supported transports.
Jaikumar Ganesh0db51a02010-12-21 22:31:44 -0800636 */
Chalard Jean7eaf3b12018-03-08 13:54:53 +0900637 @Deprecated
Jaikumar Ganesh0db51a02010-12-21 22:31:44 -0800638 public static final int TYPE_BLUETOOTH = 7;
639
Robert Greenwaltf76c55d2011-04-22 15:28:18 -0700640 /**
Chiachang Wang3b9549f2020-07-28 13:53:09 +0800641 * Fake data connection. This should not be used on shipping devices.
Chalard Jean7eaf3b12018-03-08 13:54:53 +0900642 * @deprecated This is not used any more.
Robert Greenwaltf76c55d2011-04-22 15:28:18 -0700643 */
Chalard Jean7eaf3b12018-03-08 13:54:53 +0900644 @Deprecated
Jaikumar Ganesh0db51a02010-12-21 22:31:44 -0800645 public static final int TYPE_DUMMY = 8;
Wink Savilleb7c92c72011-03-12 14:52:01 -0800646
Robert Greenwaltf76c55d2011-04-22 15:28:18 -0700647 /**
Chalard Jean7eaf3b12018-03-08 13:54:53 +0900648 * An Ethernet data connection.
649 *
650 * @deprecated Applications should instead use {@link NetworkCapabilities#hasTransport} or
651 * {@link #requestNetwork(NetworkRequest, NetworkCallback)} to request an
652 * appropriate network. {@see NetworkCapabilities} for supported transports.
Robert Greenwaltf76c55d2011-04-22 15:28:18 -0700653 */
Chalard Jean7eaf3b12018-03-08 13:54:53 +0900654 @Deprecated
Robert Greenwalt9d077812011-01-28 14:48:37 -0800655 public static final int TYPE_ETHERNET = 9;
Robert Greenwaltf76c55d2011-04-22 15:28:18 -0700656
Wink Savilleb7c92c72011-03-12 14:52:01 -0800657 /**
Robert Greenwaltfaa4b402013-02-15 10:56:35 -0800658 * Over the air Administration.
Chalard Jean7eaf3b12018-03-08 13:54:53 +0900659 * @deprecated Use {@link NetworkCapabilities} instead.
Wink Savilleb7c92c72011-03-12 14:52:01 -0800660 * {@hide}
661 */
Chalard Jean7eaf3b12018-03-08 13:54:53 +0900662 @Deprecated
Chalard Jeana3b77512019-04-09 15:46:21 +0900663 @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P, trackingBug = 130143562)
Wink Savilleb7c92c72011-03-12 14:52:01 -0800664 public static final int TYPE_MOBILE_FOTA = 10;
665
666 /**
Robert Greenwaltfaa4b402013-02-15 10:56:35 -0800667 * IP Multimedia Subsystem.
Chalard Jean7eaf3b12018-03-08 13:54:53 +0900668 * @deprecated Use {@link NetworkCapabilities#NET_CAPABILITY_IMS} instead.
Wink Savilleb7c92c72011-03-12 14:52:01 -0800669 * {@hide}
670 */
Chalard Jean7eaf3b12018-03-08 13:54:53 +0900671 @Deprecated
Mathew Inwoodc5b9bec2018-08-08 14:52:47 +0100672 @UnsupportedAppUsage
Wink Savilleb7c92c72011-03-12 14:52:01 -0800673 public static final int TYPE_MOBILE_IMS = 11;
674
675 /**
Robert Greenwaltfaa4b402013-02-15 10:56:35 -0800676 * Carrier Branded Services.
Chalard Jean7eaf3b12018-03-08 13:54:53 +0900677 * @deprecated Use {@link NetworkCapabilities#NET_CAPABILITY_CBS} instead.
Wink Savilleb7c92c72011-03-12 14:52:01 -0800678 * {@hide}
679 */
Chalard Jean7eaf3b12018-03-08 13:54:53 +0900680 @Deprecated
Chalard Jeana3b77512019-04-09 15:46:21 +0900681 @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P, trackingBug = 130143562)
Wink Savilleb7c92c72011-03-12 14:52:01 -0800682 public static final int TYPE_MOBILE_CBS = 12;
683
repo syncf5de5572011-07-29 23:55:49 -0700684 /**
685 * A Wi-Fi p2p connection. Only requesting processes will have access to
686 * the peers connected.
Chalard Jean7eaf3b12018-03-08 13:54:53 +0900687 * @deprecated Use {@link NetworkCapabilities#NET_CAPABILITY_WIFI_P2P} instead.
repo syncf5de5572011-07-29 23:55:49 -0700688 * {@hide}
689 */
Chalard Jean7eaf3b12018-03-08 13:54:53 +0900690 @Deprecated
paulhue102b0b2020-01-15 15:38:23 +0800691 @SystemApi
repo syncf5de5572011-07-29 23:55:49 -0700692 public static final int TYPE_WIFI_P2P = 13;
Wink Savilleb7c92c72011-03-12 14:52:01 -0800693
Wink Saville512c2202013-07-29 15:00:57 -0700694 /**
695 * The network to use for initially attaching to the network
Chalard Jean7eaf3b12018-03-08 13:54:53 +0900696 * @deprecated Use {@link NetworkCapabilities#NET_CAPABILITY_IA} instead.
Wink Saville512c2202013-07-29 15:00:57 -0700697 * {@hide}
698 */
Chalard Jean7eaf3b12018-03-08 13:54:53 +0900699 @Deprecated
Mathew Inwoodc5b9bec2018-08-08 14:52:47 +0100700 @UnsupportedAppUsage
Wink Saville512c2202013-07-29 15:00:57 -0700701 public static final int TYPE_MOBILE_IA = 14;
repo syncf5de5572011-07-29 23:55:49 -0700702
Lorenzo Colitti21e9a152015-04-23 15:32:42 +0900703 /**
Robert Greenwaltb1f7f752015-07-09 14:49:35 -0700704 * Emergency PDN connection for emergency services. This
705 * may include IMS and MMS in emergency situations.
Chalard Jean7eaf3b12018-03-08 13:54:53 +0900706 * @deprecated Use {@link NetworkCapabilities#NET_CAPABILITY_EIMS} instead.
Ram693d07a2014-06-26 11:03:44 -0700707 * {@hide}
708 */
Chalard Jean7eaf3b12018-03-08 13:54:53 +0900709 @Deprecated
Chalard Jeana3b77512019-04-09 15:46:21 +0900710 @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P, trackingBug = 130143562)
Ram693d07a2014-06-26 11:03:44 -0700711 public static final int TYPE_MOBILE_EMERGENCY = 15;
712
Hui Lu865b70d2014-01-15 11:05:36 -0500713 /**
714 * The network that uses proxy to achieve connectivity.
Chalard Jean7eaf3b12018-03-08 13:54:53 +0900715 * @deprecated Use {@link NetworkCapabilities} instead.
Hui Lu865b70d2014-01-15 11:05:36 -0500716 * {@hide}
717 */
Chalard Jean7eaf3b12018-03-08 13:54:53 +0900718 @Deprecated
Remi NGUYEN VANee660cf2020-11-30 19:23:45 +0900719 @SystemApi
Hui Lu865b70d2014-01-15 11:05:36 -0500720 public static final int TYPE_PROXY = 16;
Wink Saville512c2202013-07-29 15:00:57 -0700721
Robert Greenwaltce7a1442014-07-07 17:09:01 -0700722 /**
723 * A virtual network using one or more native bearers.
724 * It may or may not be providing security services.
Chalard Jean7eaf3b12018-03-08 13:54:53 +0900725 * @deprecated Applications should use {@link NetworkCapabilities#TRANSPORT_VPN} instead.
Robert Greenwaltce7a1442014-07-07 17:09:01 -0700726 */
Chalard Jean7eaf3b12018-03-08 13:54:53 +0900727 @Deprecated
Robert Greenwaltce7a1442014-07-07 17:09:01 -0700728 public static final int TYPE_VPN = 17;
Hui Lu865b70d2014-01-15 11:05:36 -0500729
Benedict Wongbdfaa482018-11-14 17:40:55 -0800730 /**
731 * A network that is exclusively meant to be used for testing
732 *
733 * @deprecated Use {@link NetworkCapabilities} instead.
734 * @hide
735 */
736 @Deprecated
737 public static final int TYPE_TEST = 18; // TODO: Remove this once NetworkTypes are unused.
Robert Greenwaltce7a1442014-07-07 17:09:01 -0700738
Chalard Jeanaea539a2020-03-25 01:24:04 +0900739 /**
740 * @deprecated Use {@link NetworkCapabilities} instead.
741 * @hide
742 */
743 @Deprecated
744 @Retention(RetentionPolicy.SOURCE)
745 @IntDef(prefix = { "TYPE_" }, value = {
746 TYPE_NONE,
747 TYPE_MOBILE,
748 TYPE_WIFI,
749 TYPE_MOBILE_MMS,
750 TYPE_MOBILE_SUPL,
751 TYPE_MOBILE_DUN,
752 TYPE_MOBILE_HIPRI,
753 TYPE_WIMAX,
754 TYPE_BLUETOOTH,
755 TYPE_DUMMY,
756 TYPE_ETHERNET,
757 TYPE_MOBILE_FOTA,
758 TYPE_MOBILE_IMS,
759 TYPE_MOBILE_CBS,
760 TYPE_WIFI_P2P,
761 TYPE_MOBILE_IA,
762 TYPE_MOBILE_EMERGENCY,
763 TYPE_PROXY,
764 TYPE_VPN,
765 TYPE_TEST
766 })
767 public @interface LegacyNetworkType {}
768
Chalard Jeanafaed1a2019-11-21 14:48:00 +0900769 // Deprecated constants for return values of startUsingNetworkFeature. They used to live
770 // in com.android.internal.telephony.PhoneConstants until they were made inaccessible.
771 private static final int DEPRECATED_PHONE_CONSTANT_APN_ALREADY_ACTIVE = 0;
772 private static final int DEPRECATED_PHONE_CONSTANT_APN_REQUEST_STARTED = 1;
773 private static final int DEPRECATED_PHONE_CONSTANT_APN_REQUEST_FAILED = 3;
774
Robert Greenwaltce7a1442014-07-07 17:09:01 -0700775 /** {@hide} */
Benedict Wongbdfaa482018-11-14 17:40:55 -0800776 public static final int MAX_RADIO_TYPE = TYPE_TEST;
777
778 /** {@hide} */
779 public static final int MAX_NETWORK_TYPE = TYPE_TEST;
The Android Open Source Project28527d22009-03-03 19:31:44 -0800780
Hugo Benichiad353f42017-06-20 14:07:59 +0900781 private static final int MIN_NETWORK_TYPE = TYPE_MOBILE;
782
Jianzheng Zhou028d2032012-11-16 13:45:20 +0800783 /**
784 * If you want to set the default network preference,you can directly
785 * change the networkAttributes array in framework's config.xml.
786 *
787 * @deprecated Since we support so many more networks now, the single
788 * network default network preference can't really express
Robert Greenwaltfaa4b402013-02-15 10:56:35 -0800789 * the hierarchy. Instead, the default is defined by the
Jianzheng Zhou028d2032012-11-16 13:45:20 +0800790 * networkAttributes in config.xml. You can determine
Robert Greenwaltf909cb12012-12-07 09:56:50 -0800791 * the current value by calling {@link #getNetworkPreference()}
Jianzheng Zhou028d2032012-11-16 13:45:20 +0800792 * from an App.
793 */
794 @Deprecated
The Android Open Source Project28527d22009-03-03 19:31:44 -0800795 public static final int DEFAULT_NETWORK_PREFERENCE = TYPE_WIFI;
796
Jeff Sharkey8c870452012-09-26 22:03:49 -0700797 /**
Robert Greenwalt42a0e1e2014-03-19 17:56:12 -0700798 * @hide
799 */
Hugo Benichi7ab07a32017-06-20 14:10:14 +0900800 public static final int REQUEST_ID_UNSET = 0;
Robert Greenwaltbfd1f4c2014-06-08 16:42:59 -0700801
Paul Jensen5dea4352014-07-11 12:28:19 -0400802 /**
Hugo Benichibee30fe2017-06-17 13:14:12 +0900803 * Static unique request used as a tombstone for NetworkCallbacks that have been unregistered.
804 * This allows to distinguish when unregistering NetworkCallbacks those that were never
Chalard Jean9dd11612018-06-04 16:52:49 +0900805 * registered from those that were already unregistered.
Hugo Benichibee30fe2017-06-17 13:14:12 +0900806 * @hide
807 */
Hugo Benichi7ab07a32017-06-20 14:10:14 +0900808 private static final NetworkRequest ALREADY_UNREGISTERED =
Hugo Benichibee30fe2017-06-17 13:14:12 +0900809 new NetworkRequest.Builder().clearCapabilities().build();
810
811 /**
Paul Jensen5dea4352014-07-11 12:28:19 -0400812 * A NetID indicating no Network is selected.
813 * Keep in sync with bionic/libc/dns/include/resolv_netid.h
814 * @hide
815 */
816 public static final int NETID_UNSET = 0;
817
Erik Klineb0be3e62017-10-30 15:29:44 +0900818 /**
Sudheer Shanka98215562021-03-23 08:12:28 +0000819 * Flag to indicate that an app is not subject to any restrictions that could result in its
820 * network access blocked.
821 *
822 * @hide
823 */
824 @SystemApi(client = SystemApi.Client.MODULE_LIBRARIES)
825 public static final int BLOCKED_REASON_NONE = 0;
826
827 /**
828 * Flag to indicate that an app is subject to Battery saver restrictions that would
829 * result in its network access being blocked.
830 *
831 * @hide
832 */
833 @SystemApi(client = SystemApi.Client.MODULE_LIBRARIES)
834 public static final int BLOCKED_REASON_BATTERY_SAVER = 1 << 0;
835
836 /**
837 * Flag to indicate that an app is subject to Doze restrictions that would
838 * result in its network access being blocked.
839 *
840 * @hide
841 */
842 @SystemApi(client = SystemApi.Client.MODULE_LIBRARIES)
843 public static final int BLOCKED_REASON_DOZE = 1 << 1;
844
845 /**
846 * Flag to indicate that an app is subject to App Standby restrictions that would
847 * result in its network access being blocked.
848 *
849 * @hide
850 */
851 @SystemApi(client = SystemApi.Client.MODULE_LIBRARIES)
852 public static final int BLOCKED_REASON_APP_STANDBY = 1 << 2;
853
854 /**
855 * Flag to indicate that an app is subject to Restricted mode restrictions that would
856 * result in its network access being blocked.
857 *
858 * @hide
859 */
860 @SystemApi(client = SystemApi.Client.MODULE_LIBRARIES)
861 public static final int BLOCKED_REASON_RESTRICTED_MODE = 1 << 3;
862
863 /**
Lorenzo Colitti79c6f222021-03-18 00:54:57 +0900864 * Flag to indicate that an app is blocked because it is subject to an always-on VPN but the VPN
865 * is not currently connected.
866 *
867 * @see DevicePolicyManager#setAlwaysOnVpnPackage(ComponentName, String, boolean)
868 *
869 * @hide
870 */
871 @SystemApi(client = SystemApi.Client.MODULE_LIBRARIES)
872 public static final int BLOCKED_REASON_LOCKDOWN_VPN = 1 << 4;
873
874 /**
Sudheer Shanka98215562021-03-23 08:12:28 +0000875 * Flag to indicate that an app is subject to Data saver restrictions that would
876 * result in its metered network access being blocked.
877 *
878 * @hide
879 */
880 @SystemApi(client = SystemApi.Client.MODULE_LIBRARIES)
881 public static final int BLOCKED_METERED_REASON_DATA_SAVER = 1 << 16;
882
883 /**
884 * Flag to indicate that an app is subject to user restrictions that would
885 * result in its metered network access being blocked.
886 *
887 * @hide
888 */
889 @SystemApi(client = SystemApi.Client.MODULE_LIBRARIES)
890 public static final int BLOCKED_METERED_REASON_USER_RESTRICTED = 1 << 17;
891
892 /**
893 * Flag to indicate that an app is subject to Device admin restrictions that would
894 * result in its metered network access being blocked.
895 *
896 * @hide
897 */
898 @SystemApi(client = SystemApi.Client.MODULE_LIBRARIES)
899 public static final int BLOCKED_METERED_REASON_ADMIN_DISABLED = 1 << 18;
900
901 /**
902 * @hide
903 */
904 @Retention(RetentionPolicy.SOURCE)
905 @IntDef(flag = true, prefix = {"BLOCKED_"}, value = {
906 BLOCKED_REASON_NONE,
907 BLOCKED_REASON_BATTERY_SAVER,
908 BLOCKED_REASON_DOZE,
909 BLOCKED_REASON_APP_STANDBY,
910 BLOCKED_REASON_RESTRICTED_MODE,
Lorenzo Colitti42fe2232021-03-25 23:17:36 +0900911 BLOCKED_REASON_LOCKDOWN_VPN,
Sudheer Shanka98215562021-03-23 08:12:28 +0000912 BLOCKED_METERED_REASON_DATA_SAVER,
913 BLOCKED_METERED_REASON_USER_RESTRICTED,
914 BLOCKED_METERED_REASON_ADMIN_DISABLED,
915 })
916 public @interface BlockedReason {}
917
Lorenzo Colitti79c6f222021-03-18 00:54:57 +0900918 /**
919 * Set of blocked reasons that are only applicable on metered networks.
920 *
921 * @hide
922 */
923 @SystemApi(client = SystemApi.Client.MODULE_LIBRARIES)
924 public static final int BLOCKED_METERED_REASON_MASK = 0xffff0000;
925
Chalard Jeana3b77512019-04-09 15:46:21 +0900926 @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P, trackingBug = 130143562)
Jeff Sharkey921ebf22011-05-19 17:12:49 -0700927 private final IConnectivityManager mService;
Lorenzo Colitticd675292021-02-04 17:32:07 +0900928
Paul Jensenc0618a62014-12-10 15:12:18 -0500929 /**
930 * A kludge to facilitate static access where a Context pointer isn't available, like in the
931 * case of the static set/getProcessDefaultNetwork methods and from the Network class.
932 * TODO: Remove this after deprecating the static methods in favor of non-static methods or
933 * methods that take a Context argument.
934 */
935 private static ConnectivityManager sInstance;
The Android Open Source Project28527d22009-03-03 19:31:44 -0800936
Lorenzo Colitti2780e4b2015-07-29 11:41:21 +0900937 private final Context mContext;
938
Amos Bianchia9b415a2020-03-04 11:07:38 -0800939 private final TetheringManager mTetheringManager;
Dianne Hackborn5ac88162014-02-26 16:20:52 -0800940
Robert Greenwaltfaa4b402013-02-15 10:56:35 -0800941 /**
942 * Tests if a given integer represents a valid network type.
943 * @param networkType the type to be tested
944 * @return a boolean. {@code true} if the type is valid, else {@code false}
Paul Jensenbbb61092015-05-06 10:42:25 -0400945 * @deprecated All APIs accepting a network type are deprecated. There should be no need to
946 * validate a network type.
Robert Greenwaltfaa4b402013-02-15 10:56:35 -0800947 */
Aurimas Liutikase80a0eb2016-05-24 15:22:55 -0700948 @Deprecated
Jeff Sharkey21062e72011-05-28 20:56:34 -0700949 public static boolean isNetworkTypeValid(int networkType) {
Hugo Benichiad353f42017-06-20 14:07:59 +0900950 return MIN_NETWORK_TYPE <= networkType && networkType <= MAX_NETWORK_TYPE;
The Android Open Source Project28527d22009-03-03 19:31:44 -0800951 }
952
Robert Greenwaltfaa4b402013-02-15 10:56:35 -0800953 /**
954 * Returns a non-localized string representing a given network type.
955 * ONLY used for debugging output.
956 * @param type the type needing naming
957 * @return a String for the given type, or a string version of the type ("87")
958 * if no name is known.
Chalard Jean7eaf3b12018-03-08 13:54:53 +0900959 * @deprecated Types are deprecated. Use {@link NetworkCapabilities} instead.
Robert Greenwaltfaa4b402013-02-15 10:56:35 -0800960 * {@hide}
961 */
Chalard Jean7eaf3b12018-03-08 13:54:53 +0900962 @Deprecated
Mathew Inwoode1a17ba2020-11-04 09:29:36 +0000963 @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.R, trackingBug = 170729553)
Jeff Sharkey21062e72011-05-28 20:56:34 -0700964 public static String getNetworkTypeName(int type) {
965 switch (type) {
Hugo Benichiad353f42017-06-20 14:07:59 +0900966 case TYPE_NONE:
967 return "NONE";
Jeff Sharkey21062e72011-05-28 20:56:34 -0700968 case TYPE_MOBILE:
969 return "MOBILE";
970 case TYPE_WIFI:
971 return "WIFI";
972 case TYPE_MOBILE_MMS:
973 return "MOBILE_MMS";
974 case TYPE_MOBILE_SUPL:
975 return "MOBILE_SUPL";
976 case TYPE_MOBILE_DUN:
977 return "MOBILE_DUN";
978 case TYPE_MOBILE_HIPRI:
979 return "MOBILE_HIPRI";
980 case TYPE_WIMAX:
981 return "WIMAX";
982 case TYPE_BLUETOOTH:
983 return "BLUETOOTH";
984 case TYPE_DUMMY:
985 return "DUMMY";
986 case TYPE_ETHERNET:
987 return "ETHERNET";
988 case TYPE_MOBILE_FOTA:
989 return "MOBILE_FOTA";
990 case TYPE_MOBILE_IMS:
991 return "MOBILE_IMS";
992 case TYPE_MOBILE_CBS:
993 return "MOBILE_CBS";
repo syncf5de5572011-07-29 23:55:49 -0700994 case TYPE_WIFI_P2P:
995 return "WIFI_P2P";
Wink Saville512c2202013-07-29 15:00:57 -0700996 case TYPE_MOBILE_IA:
997 return "MOBILE_IA";
Ram693d07a2014-06-26 11:03:44 -0700998 case TYPE_MOBILE_EMERGENCY:
999 return "MOBILE_EMERGENCY";
Hui Lu865b70d2014-01-15 11:05:36 -05001000 case TYPE_PROXY:
1001 return "PROXY";
Erik Kline137fadc2014-11-19 17:23:41 +09001002 case TYPE_VPN:
1003 return "VPN";
Jeff Sharkey21062e72011-05-28 20:56:34 -07001004 default:
1005 return Integer.toString(type);
1006 }
1007 }
1008
Robert Greenwaltfaa4b402013-02-15 10:56:35 -08001009 /**
Aaron Huang96011892020-06-27 07:18:23 +08001010 * @hide
Aaron Huang96011892020-06-27 07:18:23 +08001011 */
lucaslin7fc930b2021-03-17 14:16:01 +08001012 @SystemApi(client = MODULE_LIBRARIES)
Aaron Huang96011892020-06-27 07:18:23 +08001013 public void systemReady() {
1014 try {
1015 mService.systemReady();
1016 } catch (RemoteException e) {
1017 throw e.rethrowFromSystemServer();
1018 }
1019 }
1020
1021 /**
Robert Greenwaltfaa4b402013-02-15 10:56:35 -08001022 * Checks if a given type uses the cellular data connection.
1023 * This should be replaced in the future by a network property.
1024 * @param networkType the type to check
1025 * @return a boolean - {@code true} if uses cellular network, else {@code false}
Chalard Jean7eaf3b12018-03-08 13:54:53 +09001026 * @deprecated Types are deprecated. Use {@link NetworkCapabilities} instead.
Robert Greenwaltfaa4b402013-02-15 10:56:35 -08001027 * {@hide}
1028 */
Chalard Jean7eaf3b12018-03-08 13:54:53 +09001029 @Deprecated
Chalard Jeana3b77512019-04-09 15:46:21 +09001030 @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P, trackingBug = 130143562)
Jeff Sharkey21062e72011-05-28 20:56:34 -07001031 public static boolean isNetworkTypeMobile(int networkType) {
1032 switch (networkType) {
1033 case TYPE_MOBILE:
1034 case TYPE_MOBILE_MMS:
1035 case TYPE_MOBILE_SUPL:
1036 case TYPE_MOBILE_DUN:
1037 case TYPE_MOBILE_HIPRI:
1038 case TYPE_MOBILE_FOTA:
1039 case TYPE_MOBILE_IMS:
1040 case TYPE_MOBILE_CBS:
Wink Saville512c2202013-07-29 15:00:57 -07001041 case TYPE_MOBILE_IA:
Ram693d07a2014-06-26 11:03:44 -07001042 case TYPE_MOBILE_EMERGENCY:
Jeff Sharkey21062e72011-05-28 20:56:34 -07001043 return true;
1044 default:
1045 return false;
1046 }
1047 }
1048
Robert Greenwaltfaa4b402013-02-15 10:56:35 -08001049 /**
Jeff Sharkey79f3e022013-06-04 12:29:00 -07001050 * Checks if the given network type is backed by a Wi-Fi radio.
1051 *
Chalard Jean7eaf3b12018-03-08 13:54:53 +09001052 * @deprecated Types are deprecated. Use {@link NetworkCapabilities} instead.
Jeff Sharkey79f3e022013-06-04 12:29:00 -07001053 * @hide
1054 */
Chalard Jean7eaf3b12018-03-08 13:54:53 +09001055 @Deprecated
Jeff Sharkey79f3e022013-06-04 12:29:00 -07001056 public static boolean isNetworkTypeWifi(int networkType) {
1057 switch (networkType) {
1058 case TYPE_WIFI:
1059 case TYPE_WIFI_P2P:
1060 return true;
1061 default:
1062 return false;
1063 }
1064 }
1065
1066 /**
Chalard Jeanfa45a682021-02-25 17:23:40 +09001067 * Preference for {@link #setNetworkPreferenceForUser(UserHandle, int, Executor, Runnable)}.
1068 * Specify that the traffic for this user should by follow the default rules.
1069 * @hide
1070 */
Chalard Jeanb43f3962021-03-17 14:33:24 +09001071 @SystemApi(client = MODULE_LIBRARIES)
Chalard Jeanfa45a682021-02-25 17:23:40 +09001072 public static final int PROFILE_NETWORK_PREFERENCE_DEFAULT = 0;
1073
1074 /**
1075 * Preference for {@link #setNetworkPreferenceForUser(UserHandle, int, Executor, Runnable)}.
1076 * Specify that the traffic for this user should by default go on a network with
1077 * {@link NetworkCapabilities#NET_CAPABILITY_ENTERPRISE}, and on the system default network
1078 * if no such network is available.
1079 * @hide
1080 */
Chalard Jeanb43f3962021-03-17 14:33:24 +09001081 @SystemApi(client = MODULE_LIBRARIES)
Chalard Jeanfa45a682021-02-25 17:23:40 +09001082 public static final int PROFILE_NETWORK_PREFERENCE_ENTERPRISE = 1;
1083
1084 /** @hide */
1085 @Retention(RetentionPolicy.SOURCE)
1086 @IntDef(value = {
1087 PROFILE_NETWORK_PREFERENCE_DEFAULT,
1088 PROFILE_NETWORK_PREFERENCE_ENTERPRISE
1089 })
1090 public @interface ProfileNetworkPreference {
1091 }
1092
1093 /**
Robert Greenwaltfaa4b402013-02-15 10:56:35 -08001094 * Specifies the preferred network type. When the device has more
1095 * than one type available the preferred network type will be used.
Robert Greenwaltfaa4b402013-02-15 10:56:35 -08001096 *
1097 * @param preference the network type to prefer over all others. It is
1098 * unspecified what happens to the old preferred network in the
1099 * overall ordering.
Robert Greenwaltf3017f72014-05-18 23:07:25 -07001100 * @deprecated Functionality has been removed as it no longer makes sense,
1101 * with many more than two networks - we'd need an array to express
1102 * preference. Instead we use dynamic network properties of
1103 * the networks to describe their precedence.
Robert Greenwaltfaa4b402013-02-15 10:56:35 -08001104 */
Aurimas Liutikase80a0eb2016-05-24 15:22:55 -07001105 @Deprecated
The Android Open Source Project28527d22009-03-03 19:31:44 -08001106 public void setNetworkPreference(int preference) {
The Android Open Source Project28527d22009-03-03 19:31:44 -08001107 }
1108
Robert Greenwaltfaa4b402013-02-15 10:56:35 -08001109 /**
1110 * Retrieves the current preferred network type.
Robert Greenwaltfaa4b402013-02-15 10:56:35 -08001111 *
1112 * @return an integer representing the preferred network type
1113 *
Robert Greenwaltf3017f72014-05-18 23:07:25 -07001114 * @deprecated Functionality has been removed as it no longer makes sense,
1115 * with many more than two networks - we'd need an array to express
1116 * preference. Instead we use dynamic network properties of
1117 * the networks to describe their precedence.
Robert Greenwaltfaa4b402013-02-15 10:56:35 -08001118 */
Aurimas Liutikase80a0eb2016-05-24 15:22:55 -07001119 @Deprecated
Jeff Sharkey656584a2017-04-24 11:18:03 -06001120 @RequiresPermission(android.Manifest.permission.ACCESS_NETWORK_STATE)
The Android Open Source Project28527d22009-03-03 19:31:44 -08001121 public int getNetworkPreference() {
Robert Greenwaltf3017f72014-05-18 23:07:25 -07001122 return TYPE_NONE;
The Android Open Source Project28527d22009-03-03 19:31:44 -08001123 }
1124
Scott Mainf58b7d82011-10-06 19:02:28 -07001125 /**
Robert Greenwaltfaa4b402013-02-15 10:56:35 -08001126 * Returns details about the currently active default data network. When
1127 * connected, this network is the default route for outgoing connections.
1128 * You should always check {@link NetworkInfo#isConnected()} before initiating
1129 * network traffic. This may return {@code null} when there is no default
1130 * network.
Chalard Jean96d10a72018-03-29 17:45:24 +09001131 * Note that if the default network is a VPN, this method will return the
1132 * NetworkInfo for one of its underlying networks instead, or null if the
1133 * VPN agent did not specify any. Apps interested in learning about VPNs
1134 * should use {@link #getNetworkInfo(android.net.Network)} instead.
Robert Greenwaltfaa4b402013-02-15 10:56:35 -08001135 *
1136 * @return a {@link NetworkInfo} object for the current default network
Paul Jensenbd2d3782015-02-13 14:18:39 -05001137 * or {@code null} if no default network is currently active
junyulai9826e7f2018-12-13 12:47:51 +08001138 * @deprecated See {@link NetworkInfo}.
Jeff Sharkeyd00b1302012-04-12 18:34:54 -07001139 */
junyulai9826e7f2018-12-13 12:47:51 +08001140 @Deprecated
Jeff Sharkey656584a2017-04-24 11:18:03 -06001141 @RequiresPermission(android.Manifest.permission.ACCESS_NETWORK_STATE)
Chalard Jean158702d2019-01-07 19:26:34 +09001142 @Nullable
The Android Open Source Project28527d22009-03-03 19:31:44 -08001143 public NetworkInfo getActiveNetworkInfo() {
1144 try {
1145 return mService.getActiveNetworkInfo();
1146 } catch (RemoteException e) {
Jeff Sharkeyc78f85c2016-03-01 19:27:23 -07001147 throw e.rethrowFromSystemServer();
The Android Open Source Project28527d22009-03-03 19:31:44 -08001148 }
1149 }
1150
Robert Greenwaltfaa4b402013-02-15 10:56:35 -08001151 /**
Paul Jensen1f567382015-02-13 14:18:39 -05001152 * Returns a {@link Network} object corresponding to the currently active
1153 * default data network. In the event that the current active default data
1154 * network disconnects, the returned {@code Network} object will no longer
1155 * be usable. This will return {@code null} when there is no default
1156 * network.
1157 *
1158 * @return a {@link Network} object for the current default network or
1159 * {@code null} if no default network is currently active
Paul Jensen1f567382015-02-13 14:18:39 -05001160 */
Jeff Sharkey656584a2017-04-24 11:18:03 -06001161 @RequiresPermission(android.Manifest.permission.ACCESS_NETWORK_STATE)
Chalard Jean158702d2019-01-07 19:26:34 +09001162 @Nullable
Paul Jensen1f567382015-02-13 14:18:39 -05001163 public Network getActiveNetwork() {
1164 try {
1165 return mService.getActiveNetwork();
1166 } catch (RemoteException e) {
Jeff Sharkeyc78f85c2016-03-01 19:27:23 -07001167 throw e.rethrowFromSystemServer();
Paul Jensen1f567382015-02-13 14:18:39 -05001168 }
1169 }
1170
1171 /**
Robin Lee5b52bef2016-03-24 12:07:00 +00001172 * Returns a {@link Network} object corresponding to the currently active
1173 * default data network for a specific UID. In the event that the default data
1174 * network disconnects, the returned {@code Network} object will no longer
1175 * be usable. This will return {@code null} when there is no default
1176 * network for the UID.
Robin Lee5b52bef2016-03-24 12:07:00 +00001177 *
1178 * @return a {@link Network} object for the current default network for the
1179 * given UID or {@code null} if no default network is currently active
lifr42a60c22021-03-11 20:11:09 +08001180 * TODO: b/183465229 Cleanup getActiveNetworkForUid once b/165835257 is fixed
Robin Lee5b52bef2016-03-24 12:07:00 +00001181 */
paulhu8e96a752019-08-12 16:25:11 +08001182 @RequiresPermission(android.Manifest.permission.NETWORK_STACK)
Chalard Jean158702d2019-01-07 19:26:34 +09001183 @Nullable
Robin Lee5b52bef2016-03-24 12:07:00 +00001184 public Network getActiveNetworkForUid(int uid) {
Jeff Sharkey7dbf83d2016-04-28 15:33:18 -06001185 return getActiveNetworkForUid(uid, false);
1186 }
1187
1188 /** {@hide} */
1189 public Network getActiveNetworkForUid(int uid, boolean ignoreBlocked) {
Robin Lee5b52bef2016-03-24 12:07:00 +00001190 try {
Jeff Sharkey7dbf83d2016-04-28 15:33:18 -06001191 return mService.getActiveNetworkForUid(uid, ignoreBlocked);
Robin Lee5b52bef2016-03-24 12:07:00 +00001192 } catch (RemoteException e) {
1193 throw e.rethrowFromSystemServer();
1194 }
1195 }
1196
1197 /**
Lorenzo Colitti3f54f102020-12-12 00:51:11 +09001198 * Adds or removes a requirement for given UID ranges to use the VPN.
1199 *
1200 * If set to {@code true}, informs the system that the UIDs in the specified ranges must not
1201 * have any connectivity except if a VPN is connected and applies to the UIDs, or if the UIDs
1202 * otherwise have permission to bypass the VPN (e.g., because they have the
1203 * {@link android.Manifest.permission.CONNECTIVITY_USE_RESTRICTED_NETWORKS} permission, or when
1204 * using a socket protected by a method such as {@link VpnService#protect(DatagramSocket)}. If
1205 * set to {@code false}, a previously-added restriction is removed.
1206 * <p>
1207 * Each of the UID ranges specified by this method is added and removed as is, and no processing
1208 * is performed on the ranges to de-duplicate, merge, split, or intersect them. In order to
1209 * remove a previously-added range, the exact range must be removed as is.
1210 * <p>
1211 * The changes are applied asynchronously and may not have been applied by the time the method
1212 * returns. Apps will be notified about any changes that apply to them via
1213 * {@link NetworkCallback#onBlockedStatusChanged} callbacks called after the changes take
1214 * effect.
1215 * <p>
1216 * This method should be called only by the VPN code.
1217 *
1218 * @param ranges the UID ranges to restrict
1219 * @param requireVpn whether the specified UID ranges must use a VPN
1220 *
Lorenzo Colitti3f54f102020-12-12 00:51:11 +09001221 * @hide
1222 */
1223 @RequiresPermission(anyOf = {
1224 NetworkStack.PERMISSION_MAINLINE_NETWORK_STACK,
lucasline257bce2021-03-22 11:51:27 +08001225 android.Manifest.permission.NETWORK_STACK,
1226 android.Manifest.permission.NETWORK_SETTINGS})
1227 @SystemApi(client = MODULE_LIBRARIES)
Lorenzo Colitti3f54f102020-12-12 00:51:11 +09001228 public void setRequireVpnForUids(boolean requireVpn,
1229 @NonNull Collection<Range<Integer>> ranges) {
1230 Objects.requireNonNull(ranges);
1231 // The Range class is not parcelable. Convert to UidRange, which is what is used internally.
1232 // This method is not necessarily expected to be used outside the system server, so
1233 // parceling may not be necessary, but it could be used out-of-process, e.g., by the network
1234 // stack process, or by tests.
1235 UidRange[] rangesArray = new UidRange[ranges.size()];
1236 int index = 0;
1237 for (Range<Integer> range : ranges) {
1238 rangesArray[index++] = new UidRange(range.getLower(), range.getUpper());
1239 }
1240 try {
1241 mService.setRequireVpnForUids(requireVpn, rangesArray);
1242 } catch (RemoteException e) {
1243 throw e.rethrowFromSystemServer();
1244 }
1245 }
1246
1247 /**
Lorenzo Colittibcd692f2021-01-15 01:29:01 +09001248 * Informs ConnectivityService of whether the legacy lockdown VPN, as implemented by
1249 * LockdownVpnTracker, is in use. This is deprecated for new devices starting from Android 12
1250 * but is still supported for backwards compatibility.
1251 * <p>
1252 * This type of VPN is assumed always to use the system default network, and must always declare
1253 * exactly one underlying network, which is the network that was the default when the VPN
1254 * connected.
1255 * <p>
1256 * Calling this method with {@code true} enables legacy behaviour, specifically:
1257 * <ul>
1258 * <li>Any VPN that applies to userId 0 behaves specially with respect to deprecated
1259 * {@link #CONNECTIVITY_ACTION} broadcasts. Any such broadcasts will have the state in the
1260 * {@link #EXTRA_NETWORK_INFO} replaced by state of the VPN network. Also, any time the VPN
1261 * connects, a {@link #CONNECTIVITY_ACTION} broadcast will be sent for the network
1262 * underlying the VPN.</li>
1263 * <li>Deprecated APIs that return {@link NetworkInfo} objects will have their state
1264 * similarly replaced by the VPN network state.</li>
1265 * <li>Information on current network interfaces passed to NetworkStatsService will not
1266 * include any VPN interfaces.</li>
1267 * </ul>
1268 *
1269 * @param enabled whether legacy lockdown VPN is enabled or disabled
1270 *
Lorenzo Colittibcd692f2021-01-15 01:29:01 +09001271 * @hide
1272 */
1273 @RequiresPermission(anyOf = {
1274 NetworkStack.PERMISSION_MAINLINE_NETWORK_STACK,
lucasline257bce2021-03-22 11:51:27 +08001275 android.Manifest.permission.NETWORK_STACK,
Lorenzo Colittibcd692f2021-01-15 01:29:01 +09001276 android.Manifest.permission.NETWORK_SETTINGS})
lucasline257bce2021-03-22 11:51:27 +08001277 @SystemApi(client = MODULE_LIBRARIES)
Lorenzo Colittibcd692f2021-01-15 01:29:01 +09001278 public void setLegacyLockdownVpnEnabled(boolean enabled) {
1279 try {
1280 mService.setLegacyLockdownVpnEnabled(enabled);
1281 } catch (RemoteException e) {
1282 throw e.rethrowFromSystemServer();
1283 }
1284 }
1285
1286 /**
Robert Greenwaltfaa4b402013-02-15 10:56:35 -08001287 * Returns details about the currently active default data network
1288 * for a given uid. This is for internal use only to avoid spying
1289 * other apps.
1290 *
1291 * @return a {@link NetworkInfo} object for the current default network
1292 * for the given uid or {@code null} if no default network is
1293 * available for the specified uid.
1294 *
Robert Greenwaltfaa4b402013-02-15 10:56:35 -08001295 * {@hide}
1296 */
paulhu8e96a752019-08-12 16:25:11 +08001297 @RequiresPermission(android.Manifest.permission.NETWORK_STACK)
Mathew Inwoode1a17ba2020-11-04 09:29:36 +00001298 @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.R, trackingBug = 170729553)
Jeff Sharkey921ebf22011-05-19 17:12:49 -07001299 public NetworkInfo getActiveNetworkInfoForUid(int uid) {
Jeff Sharkey7dbf83d2016-04-28 15:33:18 -06001300 return getActiveNetworkInfoForUid(uid, false);
1301 }
1302
1303 /** {@hide} */
1304 public NetworkInfo getActiveNetworkInfoForUid(int uid, boolean ignoreBlocked) {
Jeff Sharkey921ebf22011-05-19 17:12:49 -07001305 try {
Jeff Sharkey7dbf83d2016-04-28 15:33:18 -06001306 return mService.getActiveNetworkInfoForUid(uid, ignoreBlocked);
Jeff Sharkey921ebf22011-05-19 17:12:49 -07001307 } catch (RemoteException e) {
Jeff Sharkeyc78f85c2016-03-01 19:27:23 -07001308 throw e.rethrowFromSystemServer();
Jeff Sharkey921ebf22011-05-19 17:12:49 -07001309 }
1310 }
1311
Robert Greenwaltfaa4b402013-02-15 10:56:35 -08001312 /**
1313 * Returns connection status information about a particular
1314 * network type.
1315 *
1316 * @param networkType integer specifying which networkType in
1317 * which you're interested.
1318 * @return a {@link NetworkInfo} object for the requested
1319 * network type or {@code null} if the type is not
Chalard Jean96d10a72018-03-29 17:45:24 +09001320 * supported by the device. If {@code networkType} is
1321 * TYPE_VPN and a VPN is active for the calling app,
1322 * then this method will try to return one of the
1323 * underlying networks for the VPN or null if the
1324 * VPN agent didn't specify any.
Robert Greenwaltfaa4b402013-02-15 10:56:35 -08001325 *
Paul Jensendda8e592015-03-18 12:23:02 -04001326 * @deprecated This method does not support multiple connected networks
1327 * of the same type. Use {@link #getAllNetworks} and
1328 * {@link #getNetworkInfo(android.net.Network)} instead.
Robert Greenwaltfaa4b402013-02-15 10:56:35 -08001329 */
Aurimas Liutikase80a0eb2016-05-24 15:22:55 -07001330 @Deprecated
Jeff Sharkey656584a2017-04-24 11:18:03 -06001331 @RequiresPermission(android.Manifest.permission.ACCESS_NETWORK_STATE)
Chalard Jean158702d2019-01-07 19:26:34 +09001332 @Nullable
The Android Open Source Project28527d22009-03-03 19:31:44 -08001333 public NetworkInfo getNetworkInfo(int networkType) {
1334 try {
1335 return mService.getNetworkInfo(networkType);
1336 } catch (RemoteException e) {
Jeff Sharkeyc78f85c2016-03-01 19:27:23 -07001337 throw e.rethrowFromSystemServer();
The Android Open Source Project28527d22009-03-03 19:31:44 -08001338 }
1339 }
1340
Robert Greenwaltfaa4b402013-02-15 10:56:35 -08001341 /**
Robert Greenwalt02fe11e2014-06-23 11:40:00 -07001342 * Returns connection status information about a particular
1343 * Network.
1344 *
1345 * @param network {@link Network} specifying which network
1346 * in which you're interested.
1347 * @return a {@link NetworkInfo} object for the requested
1348 * network or {@code null} if the {@code Network}
1349 * is not valid.
junyulai9826e7f2018-12-13 12:47:51 +08001350 * @deprecated See {@link NetworkInfo}.
Robert Greenwalt02fe11e2014-06-23 11:40:00 -07001351 */
junyulai9826e7f2018-12-13 12:47:51 +08001352 @Deprecated
Jeff Sharkey656584a2017-04-24 11:18:03 -06001353 @RequiresPermission(android.Manifest.permission.ACCESS_NETWORK_STATE)
Chalard Jean158702d2019-01-07 19:26:34 +09001354 @Nullable
1355 public NetworkInfo getNetworkInfo(@Nullable Network network) {
Jeff Sharkey7dbf83d2016-04-28 15:33:18 -06001356 return getNetworkInfoForUid(network, Process.myUid(), false);
1357 }
1358
1359 /** {@hide} */
1360 public NetworkInfo getNetworkInfoForUid(Network network, int uid, boolean ignoreBlocked) {
Robert Greenwalt02fe11e2014-06-23 11:40:00 -07001361 try {
Jeff Sharkey7dbf83d2016-04-28 15:33:18 -06001362 return mService.getNetworkInfoForUid(network, uid, ignoreBlocked);
Robert Greenwalt02fe11e2014-06-23 11:40:00 -07001363 } catch (RemoteException e) {
Jeff Sharkeyc78f85c2016-03-01 19:27:23 -07001364 throw e.rethrowFromSystemServer();
Robert Greenwalt02fe11e2014-06-23 11:40:00 -07001365 }
1366 }
1367
1368 /**
Robert Greenwaltfaa4b402013-02-15 10:56:35 -08001369 * Returns connection status information about all network
1370 * types supported by the device.
1371 *
1372 * @return an array of {@link NetworkInfo} objects. Check each
1373 * {@link NetworkInfo#getType} for which type each applies.
1374 *
Paul Jensendda8e592015-03-18 12:23:02 -04001375 * @deprecated This method does not support multiple connected networks
1376 * of the same type. Use {@link #getAllNetworks} and
1377 * {@link #getNetworkInfo(android.net.Network)} instead.
Robert Greenwaltfaa4b402013-02-15 10:56:35 -08001378 */
Aurimas Liutikase80a0eb2016-05-24 15:22:55 -07001379 @Deprecated
Jeff Sharkey656584a2017-04-24 11:18:03 -06001380 @RequiresPermission(android.Manifest.permission.ACCESS_NETWORK_STATE)
Chalard Jean158702d2019-01-07 19:26:34 +09001381 @NonNull
The Android Open Source Project28527d22009-03-03 19:31:44 -08001382 public NetworkInfo[] getAllNetworkInfo() {
1383 try {
1384 return mService.getAllNetworkInfo();
1385 } catch (RemoteException e) {
Jeff Sharkeyc78f85c2016-03-01 19:27:23 -07001386 throw e.rethrowFromSystemServer();
The Android Open Source Project28527d22009-03-03 19:31:44 -08001387 }
1388 }
1389
Robert Greenwaltfaa4b402013-02-15 10:56:35 -08001390 /**
junyulaiebd15162021-03-03 12:09:05 +08001391 * Return a list of {@link NetworkStateSnapshot}s, one for each network that is currently
1392 * connected.
1393 * @hide
1394 */
1395 @SystemApi(client = MODULE_LIBRARIES)
1396 @RequiresPermission(anyOf = {
1397 NetworkStack.PERMISSION_MAINLINE_NETWORK_STACK,
1398 android.Manifest.permission.NETWORK_STACK,
1399 android.Manifest.permission.NETWORK_SETTINGS})
1400 @NonNull
Aaron Huangee78b1f2021-04-17 13:46:25 +08001401 public List<NetworkStateSnapshot> getAllNetworkStateSnapshots() {
junyulaiebd15162021-03-03 12:09:05 +08001402 try {
Aaron Huangee78b1f2021-04-17 13:46:25 +08001403 return mService.getAllNetworkStateSnapshots();
junyulaiebd15162021-03-03 12:09:05 +08001404 } catch (RemoteException e) {
1405 throw e.rethrowFromSystemServer();
1406 }
1407 }
1408
1409 /**
Lorenzo Colitti860a7aa2014-08-22 17:10:50 -07001410 * Returns the {@link Network} object currently serving a given type, or
1411 * null if the given type is not connected.
1412 *
Lorenzo Colitti860a7aa2014-08-22 17:10:50 -07001413 * @hide
Paul Jensendda8e592015-03-18 12:23:02 -04001414 * @deprecated This method does not support multiple connected networks
1415 * of the same type. Use {@link #getAllNetworks} and
1416 * {@link #getNetworkInfo(android.net.Network)} instead.
Lorenzo Colitti860a7aa2014-08-22 17:10:50 -07001417 */
Aurimas Liutikase80a0eb2016-05-24 15:22:55 -07001418 @Deprecated
Jeff Sharkey656584a2017-04-24 11:18:03 -06001419 @RequiresPermission(android.Manifest.permission.ACCESS_NETWORK_STATE)
Mathew Inwoodc5b9bec2018-08-08 14:52:47 +01001420 @UnsupportedAppUsage
Lorenzo Colitti860a7aa2014-08-22 17:10:50 -07001421 public Network getNetworkForType(int networkType) {
1422 try {
1423 return mService.getNetworkForType(networkType);
1424 } catch (RemoteException e) {
Jeff Sharkeyc78f85c2016-03-01 19:27:23 -07001425 throw e.rethrowFromSystemServer();
Lorenzo Colitti860a7aa2014-08-22 17:10:50 -07001426 }
1427 }
1428
1429 /**
Robert Greenwalt02fe11e2014-06-23 11:40:00 -07001430 * Returns an array of all {@link Network} currently tracked by the
1431 * framework.
Paul Jensen08f9dbb2015-05-06 11:10:18 -04001432 *
1433 * @return an array of {@link Network} objects.
Robert Greenwalt02fe11e2014-06-23 11:40:00 -07001434 */
Jeff Sharkey656584a2017-04-24 11:18:03 -06001435 @RequiresPermission(android.Manifest.permission.ACCESS_NETWORK_STATE)
Chalard Jean158702d2019-01-07 19:26:34 +09001436 @NonNull
Robert Greenwalt02fe11e2014-06-23 11:40:00 -07001437 public Network[] getAllNetworks() {
1438 try {
1439 return mService.getAllNetworks();
1440 } catch (RemoteException e) {
Jeff Sharkeyc78f85c2016-03-01 19:27:23 -07001441 throw e.rethrowFromSystemServer();
Robert Greenwalt02fe11e2014-06-23 11:40:00 -07001442 }
1443 }
1444
1445 /**
Roshan Pius951c0032020-12-22 15:10:42 -08001446 * Returns an array of {@link NetworkCapabilities} objects, representing
Lorenzo Colitti79bd2e22014-11-28 11:21:30 +09001447 * the Networks that applications run by the given user will use by default.
1448 * @hide
1449 */
Mathew Inwoodc5b9bec2018-08-08 14:52:47 +01001450 @UnsupportedAppUsage
Lorenzo Colitti79bd2e22014-11-28 11:21:30 +09001451 public NetworkCapabilities[] getDefaultNetworkCapabilitiesForUser(int userId) {
1452 try {
Qingxi Lib2748102020-01-08 12:51:49 -08001453 return mService.getDefaultNetworkCapabilitiesForUser(
Roshan Piusaa24fde2020-12-17 14:53:09 -08001454 userId, mContext.getOpPackageName(), getAttributionTag());
Lorenzo Colitti79bd2e22014-11-28 11:21:30 +09001455 } catch (RemoteException e) {
Jeff Sharkeyc78f85c2016-03-01 19:27:23 -07001456 throw e.rethrowFromSystemServer();
Lorenzo Colitti79bd2e22014-11-28 11:21:30 +09001457 }
1458 }
1459
1460 /**
Robert Greenwaltfaa4b402013-02-15 10:56:35 -08001461 * Returns the IP information for the current default network.
1462 *
1463 * @return a {@link LinkProperties} object describing the IP info
1464 * for the current default network, or {@code null} if there
1465 * is no current default network.
1466 *
Robert Greenwaltfaa4b402013-02-15 10:56:35 -08001467 * {@hide}
Chalard Jean97021a12019-01-11 16:47:53 +09001468 * @deprecated please use {@link #getLinkProperties(Network)} on the return
1469 * value of {@link #getActiveNetwork()} instead. In particular,
1470 * this method will return non-null LinkProperties even if the
1471 * app is blocked by policy from using this network.
Robert Greenwaltfaa4b402013-02-15 10:56:35 -08001472 */
Jeff Sharkey656584a2017-04-24 11:18:03 -06001473 @RequiresPermission(android.Manifest.permission.ACCESS_NETWORK_STATE)
Chalard Jean97021a12019-01-11 16:47:53 +09001474 @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P, trackingBug = 109783091)
Robert Greenwalt9f0ee4f2010-09-14 09:18:02 -07001475 public LinkProperties getActiveLinkProperties() {
1476 try {
1477 return mService.getActiveLinkProperties();
1478 } catch (RemoteException e) {
Jeff Sharkeyc78f85c2016-03-01 19:27:23 -07001479 throw e.rethrowFromSystemServer();
Robert Greenwalt9f0ee4f2010-09-14 09:18:02 -07001480 }
1481 }
1482
Robert Greenwaltfaa4b402013-02-15 10:56:35 -08001483 /**
1484 * Returns the IP information for a given network type.
1485 *
1486 * @param networkType the network type of interest.
1487 * @return a {@link LinkProperties} object describing the IP info
1488 * for the given networkType, or {@code null} if there is
1489 * no current default network.
1490 *
Robert Greenwaltfaa4b402013-02-15 10:56:35 -08001491 * {@hide}
Paul Jensendda8e592015-03-18 12:23:02 -04001492 * @deprecated This method does not support multiple connected networks
1493 * of the same type. Use {@link #getAllNetworks},
1494 * {@link #getNetworkInfo(android.net.Network)}, and
1495 * {@link #getLinkProperties(android.net.Network)} instead.
Robert Greenwaltfaa4b402013-02-15 10:56:35 -08001496 */
Aurimas Liutikase80a0eb2016-05-24 15:22:55 -07001497 @Deprecated
Jeff Sharkey656584a2017-04-24 11:18:03 -06001498 @RequiresPermission(android.Manifest.permission.ACCESS_NETWORK_STATE)
Chalard Jeana3b77512019-04-09 15:46:21 +09001499 @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P, trackingBug = 130143562)
Robert Greenwalt9f0ee4f2010-09-14 09:18:02 -07001500 public LinkProperties getLinkProperties(int networkType) {
1501 try {
Robert Greenwaltf99b8392014-03-26 16:47:06 -07001502 return mService.getLinkPropertiesForType(networkType);
1503 } catch (RemoteException e) {
Jeff Sharkeyc78f85c2016-03-01 19:27:23 -07001504 throw e.rethrowFromSystemServer();
Robert Greenwaltf99b8392014-03-26 16:47:06 -07001505 }
1506 }
1507
Robert Greenwaltf3017f72014-05-18 23:07:25 -07001508 /**
1509 * Get the {@link LinkProperties} for the given {@link Network}. This
1510 * will return {@code null} if the network is unknown.
1511 *
1512 * @param network The {@link Network} object identifying the network in question.
1513 * @return The {@link LinkProperties} for the network, or {@code null}.
Paul Jensen08f9dbb2015-05-06 11:10:18 -04001514 */
Jeff Sharkey656584a2017-04-24 11:18:03 -06001515 @RequiresPermission(android.Manifest.permission.ACCESS_NETWORK_STATE)
Chalard Jean158702d2019-01-07 19:26:34 +09001516 @Nullable
1517 public LinkProperties getLinkProperties(@Nullable Network network) {
Robert Greenwaltf99b8392014-03-26 16:47:06 -07001518 try {
1519 return mService.getLinkProperties(network);
1520 } catch (RemoteException e) {
Jeff Sharkeyc78f85c2016-03-01 19:27:23 -07001521 throw e.rethrowFromSystemServer();
Robert Greenwaltf99b8392014-03-26 16:47:06 -07001522 }
1523 }
1524
Robert Greenwaltf3017f72014-05-18 23:07:25 -07001525 /**
Roshan Pius951c0032020-12-22 15:10:42 -08001526 * Get the {@link NetworkCapabilities} for the given {@link Network}. This
Chalard Jean53a8bcc2021-04-30 20:22:10 +09001527 * will return {@code null} if the network is unknown or if the |network| argument is null.
Robert Greenwaltf3017f72014-05-18 23:07:25 -07001528 *
Roshan Pius951c0032020-12-22 15:10:42 -08001529 * This will remove any location sensitive data in {@link TransportInfo} embedded in
1530 * {@link NetworkCapabilities#getTransportInfo()}. Some transport info instances like
1531 * {@link android.net.wifi.WifiInfo} contain location sensitive information. Retrieving
1532 * this location sensitive information (subject to app's location permissions) will be
1533 * noted by system. To include any location sensitive data in {@link TransportInfo},
1534 * use a {@link NetworkCallback} with
1535 * {@link NetworkCallback#FLAG_INCLUDE_LOCATION_INFO} flag.
1536 *
Robert Greenwaltf3017f72014-05-18 23:07:25 -07001537 * @param network The {@link Network} object identifying the network in question.
Roshan Pius951c0032020-12-22 15:10:42 -08001538 * @return The {@link NetworkCapabilities} for the network, or {@code null}.
Robert Greenwaltf3017f72014-05-18 23:07:25 -07001539 */
Jeff Sharkey656584a2017-04-24 11:18:03 -06001540 @RequiresPermission(android.Manifest.permission.ACCESS_NETWORK_STATE)
Chalard Jean158702d2019-01-07 19:26:34 +09001541 @Nullable
1542 public NetworkCapabilities getNetworkCapabilities(@Nullable Network network) {
Robert Greenwaltf99b8392014-03-26 16:47:06 -07001543 try {
Roshan Piusaa24fde2020-12-17 14:53:09 -08001544 return mService.getNetworkCapabilities(
1545 network, mContext.getOpPackageName(), getAttributionTag());
Robert Greenwalt9f0ee4f2010-09-14 09:18:02 -07001546 } catch (RemoteException e) {
Jeff Sharkeyc78f85c2016-03-01 19:27:23 -07001547 throw e.rethrowFromSystemServer();
Robert Greenwalt9f0ee4f2010-09-14 09:18:02 -07001548 }
1549 }
1550
Robert Greenwaltfaa4b402013-02-15 10:56:35 -08001551 /**
Remi NGUYEN VAN035f6532019-03-20 14:22:49 +09001552 * Gets a URL that can be used for resolving whether a captive portal is present.
Udam Sainicd645462016-01-04 12:16:14 -08001553 * 1. This URL should respond with a 204 response to a GET request to indicate no captive
1554 * portal is present.
1555 * 2. This URL must be HTTP as redirect responses are used to find captive portal
1556 * sign-in pages. Captive portals cannot respond to HTTPS requests with redirects.
1557 *
Remi NGUYEN VAN035f6532019-03-20 14:22:49 +09001558 * The system network validation may be using different strategies to detect captive portals,
1559 * so this method does not necessarily return a URL used by the system. It only returns a URL
1560 * that may be relevant for other components trying to detect captive portals.
paulhu8e96a752019-08-12 16:25:11 +08001561 *
Udam Sainicd645462016-01-04 12:16:14 -08001562 * @hide
paulhu8e96a752019-08-12 16:25:11 +08001563 * @deprecated This API returns URL which is not guaranteed to be one of the URLs used by the
1564 * system.
Udam Sainicd645462016-01-04 12:16:14 -08001565 */
paulhu8e96a752019-08-12 16:25:11 +08001566 @Deprecated
Udam Sainicd645462016-01-04 12:16:14 -08001567 @SystemApi
paulhu8e96a752019-08-12 16:25:11 +08001568 @RequiresPermission(android.Manifest.permission.NETWORK_SETTINGS)
Udam Sainicd645462016-01-04 12:16:14 -08001569 public String getCaptivePortalServerUrl() {
1570 try {
1571 return mService.getCaptivePortalServerUrl();
1572 } catch (RemoteException e) {
Jeff Sharkeyc78f85c2016-03-01 19:27:23 -07001573 throw e.rethrowFromSystemServer();
Udam Sainicd645462016-01-04 12:16:14 -08001574 }
1575 }
1576
1577 /**
The Android Open Source Project28527d22009-03-03 19:31:44 -08001578 * Tells the underlying networking system that the caller wants to
1579 * begin using the named feature. The interpretation of {@code feature}
1580 * is completely up to each networking implementation.
Lorenzo Colitti0b40c872015-10-15 16:29:00 +09001581 *
1582 * <p>This method requires the caller to hold either the
1583 * {@link android.Manifest.permission#CHANGE_NETWORK_STATE} permission
1584 * or the ability to modify system settings as determined by
1585 * {@link android.provider.Settings.System#canWrite}.</p>
1586 *
The Android Open Source Project28527d22009-03-03 19:31:44 -08001587 * @param networkType specifies which network the request pertains to
1588 * @param feature the name of the feature to be used
1589 * @return an integer value representing the outcome of the request.
1590 * The interpretation of this value is specific to each networking
1591 * implementation+feature combination, except that the value {@code -1}
1592 * always indicates failure.
Robert Greenwaltf3017f72014-05-18 23:07:25 -07001593 *
Lorenzo Colitti7f6a2bf2015-04-24 17:03:31 +09001594 * @deprecated Deprecated in favor of the cleaner
1595 * {@link #requestNetwork(NetworkRequest, NetworkCallback)} API.
Dianne Hackborn18c1d832015-07-31 10:35:34 -07001596 * In {@link VERSION_CODES#M}, and above, this method is unsupported and will
Lorenzo Colitti2780e4b2015-07-29 11:41:21 +09001597 * throw {@code UnsupportedOperationException} if called.
Lorenzo Colitti84d7f072016-12-09 18:39:30 +09001598 * @removed
The Android Open Source Project28527d22009-03-03 19:31:44 -08001599 */
Aurimas Liutikase80a0eb2016-05-24 15:22:55 -07001600 @Deprecated
The Android Open Source Project28527d22009-03-03 19:31:44 -08001601 public int startUsingNetworkFeature(int networkType, String feature) {
Lorenzo Colitti2780e4b2015-07-29 11:41:21 +09001602 checkLegacyRoutingApiAccess();
Robert Greenwalt3dc73e52014-05-15 18:07:26 -07001603 NetworkCapabilities netCap = networkCapabilitiesForFeature(networkType, feature);
1604 if (netCap == null) {
1605 Log.d(TAG, "Can't satisfy startUsingNetworkFeature for " + networkType + ", " +
1606 feature);
Chalard Jeanafaed1a2019-11-21 14:48:00 +09001607 return DEPRECATED_PHONE_CONSTANT_APN_REQUEST_FAILED;
Robert Greenwalt3dc73e52014-05-15 18:07:26 -07001608 }
1609
1610 NetworkRequest request = null;
1611 synchronized (sLegacyRequests) {
1612 LegacyRequest l = sLegacyRequests.get(netCap);
1613 if (l != null) {
1614 Log.d(TAG, "renewing startUsingNetworkFeature request " + l.networkRequest);
1615 renewRequestLocked(l);
1616 if (l.currentNetwork != null) {
Chalard Jeanafaed1a2019-11-21 14:48:00 +09001617 return DEPRECATED_PHONE_CONSTANT_APN_ALREADY_ACTIVE;
Robert Greenwalt3dc73e52014-05-15 18:07:26 -07001618 } else {
Chalard Jeanafaed1a2019-11-21 14:48:00 +09001619 return DEPRECATED_PHONE_CONSTANT_APN_REQUEST_STARTED;
Robert Greenwalt3dc73e52014-05-15 18:07:26 -07001620 }
1621 }
1622
1623 request = requestNetworkForFeatureLocked(netCap);
1624 }
1625 if (request != null) {
Robert Greenwaltb8401732014-06-20 10:58:45 -07001626 Log.d(TAG, "starting startUsingNetworkFeature for request " + request);
Chalard Jeanafaed1a2019-11-21 14:48:00 +09001627 return DEPRECATED_PHONE_CONSTANT_APN_REQUEST_STARTED;
Robert Greenwalt3dc73e52014-05-15 18:07:26 -07001628 } else {
1629 Log.d(TAG, " request Failed");
Chalard Jeanafaed1a2019-11-21 14:48:00 +09001630 return DEPRECATED_PHONE_CONSTANT_APN_REQUEST_FAILED;
The Android Open Source Project28527d22009-03-03 19:31:44 -08001631 }
1632 }
1633
1634 /**
1635 * Tells the underlying networking system that the caller is finished
1636 * using the named feature. The interpretation of {@code feature}
1637 * is completely up to each networking implementation.
Lorenzo Colitti0b40c872015-10-15 16:29:00 +09001638 *
1639 * <p>This method requires the caller to hold either the
1640 * {@link android.Manifest.permission#CHANGE_NETWORK_STATE} permission
1641 * or the ability to modify system settings as determined by
1642 * {@link android.provider.Settings.System#canWrite}.</p>
1643 *
The Android Open Source Project28527d22009-03-03 19:31:44 -08001644 * @param networkType specifies which network the request pertains to
1645 * @param feature the name of the feature that is no longer needed
1646 * @return an integer value representing the outcome of the request.
1647 * The interpretation of this value is specific to each networking
1648 * implementation+feature combination, except that the value {@code -1}
1649 * always indicates failure.
Robert Greenwaltf3017f72014-05-18 23:07:25 -07001650 *
Lorenzo Colitti15874cd2016-04-13 22:00:02 +09001651 * @deprecated Deprecated in favor of the cleaner
1652 * {@link #unregisterNetworkCallback(NetworkCallback)} API.
Dianne Hackborn18c1d832015-07-31 10:35:34 -07001653 * In {@link VERSION_CODES#M}, and above, this method is unsupported and will
Lorenzo Colitti2780e4b2015-07-29 11:41:21 +09001654 * throw {@code UnsupportedOperationException} if called.
Lorenzo Colitti84d7f072016-12-09 18:39:30 +09001655 * @removed
The Android Open Source Project28527d22009-03-03 19:31:44 -08001656 */
Aurimas Liutikase80a0eb2016-05-24 15:22:55 -07001657 @Deprecated
The Android Open Source Project28527d22009-03-03 19:31:44 -08001658 public int stopUsingNetworkFeature(int networkType, String feature) {
Lorenzo Colitti2780e4b2015-07-29 11:41:21 +09001659 checkLegacyRoutingApiAccess();
Robert Greenwalt3dc73e52014-05-15 18:07:26 -07001660 NetworkCapabilities netCap = networkCapabilitiesForFeature(networkType, feature);
1661 if (netCap == null) {
1662 Log.d(TAG, "Can't satisfy stopUsingNetworkFeature for " + networkType + ", " +
1663 feature);
The Android Open Source Project28527d22009-03-03 19:31:44 -08001664 return -1;
1665 }
Robert Greenwalt3dc73e52014-05-15 18:07:26 -07001666
Paul Jensen49f74a32014-12-17 10:39:34 -05001667 if (removeRequestForFeature(netCap)) {
Robert Greenwalt3dc73e52014-05-15 18:07:26 -07001668 Log.d(TAG, "stopUsingNetworkFeature for " + networkType + ", " + feature);
Robert Greenwalt3dc73e52014-05-15 18:07:26 -07001669 }
1670 return 1;
1671 }
1672
Mathew Inwoode1a17ba2020-11-04 09:29:36 +00001673 @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.R, trackingBug = 170729553)
Robert Greenwalt3dc73e52014-05-15 18:07:26 -07001674 private NetworkCapabilities networkCapabilitiesForFeature(int networkType, String feature) {
1675 if (networkType == TYPE_MOBILE) {
Erik Klinece55eb12017-01-26 18:08:28 +09001676 switch (feature) {
1677 case "enableCBS":
1678 return networkCapabilitiesForType(TYPE_MOBILE_CBS);
1679 case "enableDUN":
1680 case "enableDUNAlways":
1681 return networkCapabilitiesForType(TYPE_MOBILE_DUN);
1682 case "enableFOTA":
1683 return networkCapabilitiesForType(TYPE_MOBILE_FOTA);
1684 case "enableHIPRI":
1685 return networkCapabilitiesForType(TYPE_MOBILE_HIPRI);
1686 case "enableIMS":
1687 return networkCapabilitiesForType(TYPE_MOBILE_IMS);
1688 case "enableMMS":
1689 return networkCapabilitiesForType(TYPE_MOBILE_MMS);
1690 case "enableSUPL":
1691 return networkCapabilitiesForType(TYPE_MOBILE_SUPL);
1692 default:
1693 return null;
Robert Greenwalt3dc73e52014-05-15 18:07:26 -07001694 }
Erik Klinece55eb12017-01-26 18:08:28 +09001695 } else if (networkType == TYPE_WIFI && "p2p".equals(feature)) {
1696 return networkCapabilitiesForType(TYPE_WIFI_P2P);
Robert Greenwalt3dc73e52014-05-15 18:07:26 -07001697 }
1698 return null;
1699 }
1700
Robert Greenwalt802c1102014-06-02 15:32:02 -07001701 private int legacyTypeForNetworkCapabilities(NetworkCapabilities netCap) {
Robert Greenwalt3dc73e52014-05-15 18:07:26 -07001702 if (netCap == null) return TYPE_NONE;
1703 if (netCap.hasCapability(NetworkCapabilities.NET_CAPABILITY_CBS)) {
1704 return TYPE_MOBILE_CBS;
1705 }
1706 if (netCap.hasCapability(NetworkCapabilities.NET_CAPABILITY_IMS)) {
1707 return TYPE_MOBILE_IMS;
1708 }
1709 if (netCap.hasCapability(NetworkCapabilities.NET_CAPABILITY_FOTA)) {
1710 return TYPE_MOBILE_FOTA;
1711 }
1712 if (netCap.hasCapability(NetworkCapabilities.NET_CAPABILITY_DUN)) {
1713 return TYPE_MOBILE_DUN;
1714 }
1715 if (netCap.hasCapability(NetworkCapabilities.NET_CAPABILITY_SUPL)) {
1716 return TYPE_MOBILE_SUPL;
1717 }
1718 if (netCap.hasCapability(NetworkCapabilities.NET_CAPABILITY_MMS)) {
1719 return TYPE_MOBILE_MMS;
1720 }
1721 if (netCap.hasCapability(NetworkCapabilities.NET_CAPABILITY_INTERNET)) {
1722 return TYPE_MOBILE_HIPRI;
1723 }
Robert Greenwalt802c1102014-06-02 15:32:02 -07001724 if (netCap.hasCapability(NetworkCapabilities.NET_CAPABILITY_WIFI_P2P)) {
1725 return TYPE_WIFI_P2P;
1726 }
Robert Greenwalt3dc73e52014-05-15 18:07:26 -07001727 return TYPE_NONE;
1728 }
1729
1730 private static class LegacyRequest {
1731 NetworkCapabilities networkCapabilities;
1732 NetworkRequest networkRequest;
1733 int expireSequenceNumber;
1734 Network currentNetwork;
1735 int delay = -1;
Paul Jensen49f74a32014-12-17 10:39:34 -05001736
1737 private void clearDnsBinding() {
1738 if (currentNetwork != null) {
1739 currentNetwork = null;
1740 setProcessDefaultNetworkForHostResolution(null);
1741 }
1742 }
1743
Robert Greenwaltf57c03c2014-06-11 16:05:07 -07001744 NetworkCallback networkCallback = new NetworkCallback() {
Robert Greenwalt3dc73e52014-05-15 18:07:26 -07001745 @Override
Robert Greenwaltf57c03c2014-06-11 16:05:07 -07001746 public void onAvailable(Network network) {
Robert Greenwalt3dc73e52014-05-15 18:07:26 -07001747 currentNetwork = network;
1748 Log.d(TAG, "startUsingNetworkFeature got Network:" + network);
Paul Jensen8cdda642014-05-29 10:12:39 -04001749 setProcessDefaultNetworkForHostResolution(network);
Robert Greenwalt3dc73e52014-05-15 18:07:26 -07001750 }
1751 @Override
Robert Greenwaltf57c03c2014-06-11 16:05:07 -07001752 public void onLost(Network network) {
Paul Jensen49f74a32014-12-17 10:39:34 -05001753 if (network.equals(currentNetwork)) clearDnsBinding();
Robert Greenwalt3dc73e52014-05-15 18:07:26 -07001754 Log.d(TAG, "startUsingNetworkFeature lost Network:" + network);
1755 }
1756 };
1757 }
1758
Mathew Inwoode1a17ba2020-11-04 09:29:36 +00001759 @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.R, trackingBug = 170729553)
Chalard Jean9dd11612018-06-04 16:52:49 +09001760 private static final HashMap<NetworkCapabilities, LegacyRequest> sLegacyRequests =
1761 new HashMap<>();
Robert Greenwalt3dc73e52014-05-15 18:07:26 -07001762
1763 private NetworkRequest findRequestForFeature(NetworkCapabilities netCap) {
1764 synchronized (sLegacyRequests) {
1765 LegacyRequest l = sLegacyRequests.get(netCap);
1766 if (l != null) return l.networkRequest;
1767 }
1768 return null;
1769 }
1770
1771 private void renewRequestLocked(LegacyRequest l) {
1772 l.expireSequenceNumber++;
1773 Log.d(TAG, "renewing request to seqNum " + l.expireSequenceNumber);
1774 sendExpireMsgForFeature(l.networkCapabilities, l.expireSequenceNumber, l.delay);
1775 }
1776
1777 private void expireRequest(NetworkCapabilities netCap, int sequenceNum) {
1778 int ourSeqNum = -1;
1779 synchronized (sLegacyRequests) {
1780 LegacyRequest l = sLegacyRequests.get(netCap);
1781 if (l == null) return;
1782 ourSeqNum = l.expireSequenceNumber;
Paul Jensen49f74a32014-12-17 10:39:34 -05001783 if (l.expireSequenceNumber == sequenceNum) removeRequestForFeature(netCap);
Robert Greenwalt3dc73e52014-05-15 18:07:26 -07001784 }
1785 Log.d(TAG, "expireRequest with " + ourSeqNum + ", " + sequenceNum);
1786 }
1787
Mathew Inwoode1a17ba2020-11-04 09:29:36 +00001788 @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.R, trackingBug = 170729553)
Robert Greenwalt3dc73e52014-05-15 18:07:26 -07001789 private NetworkRequest requestNetworkForFeatureLocked(NetworkCapabilities netCap) {
1790 int delay = -1;
Robert Greenwalt802c1102014-06-02 15:32:02 -07001791 int type = legacyTypeForNetworkCapabilities(netCap);
Robert Greenwalt3dc73e52014-05-15 18:07:26 -07001792 try {
1793 delay = mService.getRestoreDefaultNetworkDelay(type);
Jeff Sharkeyc78f85c2016-03-01 19:27:23 -07001794 } catch (RemoteException e) {
1795 throw e.rethrowFromSystemServer();
1796 }
Robert Greenwalt3dc73e52014-05-15 18:07:26 -07001797 LegacyRequest l = new LegacyRequest();
1798 l.networkCapabilities = netCap;
1799 l.delay = delay;
1800 l.expireSequenceNumber = 0;
Hugo Benichifd44e912017-02-02 17:02:36 +09001801 l.networkRequest = sendRequestForNetwork(
1802 netCap, l.networkCallback, 0, REQUEST, type, getDefaultHandler());
Robert Greenwalt3dc73e52014-05-15 18:07:26 -07001803 if (l.networkRequest == null) return null;
1804 sLegacyRequests.put(netCap, l);
1805 sendExpireMsgForFeature(netCap, l.expireSequenceNumber, delay);
1806 return l.networkRequest;
1807 }
1808
1809 private void sendExpireMsgForFeature(NetworkCapabilities netCap, int seqNum, int delay) {
1810 if (delay >= 0) {
1811 Log.d(TAG, "sending expire msg with seqNum " + seqNum + " and delay " + delay);
Hugo Benichifd44e912017-02-02 17:02:36 +09001812 CallbackHandler handler = getDefaultHandler();
Hugo Benichibc4ac972017-02-03 14:18:44 +09001813 Message msg = handler.obtainMessage(EXPIRE_LEGACY_REQUEST, seqNum, 0, netCap);
1814 handler.sendMessageDelayed(msg, delay);
Robert Greenwalt3dc73e52014-05-15 18:07:26 -07001815 }
1816 }
1817
Mathew Inwoode1a17ba2020-11-04 09:29:36 +00001818 @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.R, trackingBug = 170729553)
Paul Jensen49f74a32014-12-17 10:39:34 -05001819 private boolean removeRequestForFeature(NetworkCapabilities netCap) {
1820 final LegacyRequest l;
Robert Greenwalt3dc73e52014-05-15 18:07:26 -07001821 synchronized (sLegacyRequests) {
Paul Jensen49f74a32014-12-17 10:39:34 -05001822 l = sLegacyRequests.remove(netCap);
Robert Greenwalt3dc73e52014-05-15 18:07:26 -07001823 }
Paul Jensen49f74a32014-12-17 10:39:34 -05001824 if (l == null) return false;
1825 unregisterNetworkCallback(l.networkCallback);
1826 l.clearDnsBinding();
1827 return true;
The Android Open Source Project28527d22009-03-03 19:31:44 -08001828 }
1829
Erik Klinece55eb12017-01-26 18:08:28 +09001830 private static final SparseIntArray sLegacyTypeToTransport = new SparseIntArray();
1831 static {
1832 sLegacyTypeToTransport.put(TYPE_MOBILE, NetworkCapabilities.TRANSPORT_CELLULAR);
1833 sLegacyTypeToTransport.put(TYPE_MOBILE_CBS, NetworkCapabilities.TRANSPORT_CELLULAR);
1834 sLegacyTypeToTransport.put(TYPE_MOBILE_DUN, NetworkCapabilities.TRANSPORT_CELLULAR);
1835 sLegacyTypeToTransport.put(TYPE_MOBILE_FOTA, NetworkCapabilities.TRANSPORT_CELLULAR);
1836 sLegacyTypeToTransport.put(TYPE_MOBILE_HIPRI, NetworkCapabilities.TRANSPORT_CELLULAR);
1837 sLegacyTypeToTransport.put(TYPE_MOBILE_IMS, NetworkCapabilities.TRANSPORT_CELLULAR);
1838 sLegacyTypeToTransport.put(TYPE_MOBILE_MMS, NetworkCapabilities.TRANSPORT_CELLULAR);
1839 sLegacyTypeToTransport.put(TYPE_MOBILE_SUPL, NetworkCapabilities.TRANSPORT_CELLULAR);
1840 sLegacyTypeToTransport.put(TYPE_WIFI, NetworkCapabilities.TRANSPORT_WIFI);
1841 sLegacyTypeToTransport.put(TYPE_WIFI_P2P, NetworkCapabilities.TRANSPORT_WIFI);
1842 sLegacyTypeToTransport.put(TYPE_BLUETOOTH, NetworkCapabilities.TRANSPORT_BLUETOOTH);
1843 sLegacyTypeToTransport.put(TYPE_ETHERNET, NetworkCapabilities.TRANSPORT_ETHERNET);
1844 }
1845
1846 private static final SparseIntArray sLegacyTypeToCapability = new SparseIntArray();
1847 static {
1848 sLegacyTypeToCapability.put(TYPE_MOBILE_CBS, NetworkCapabilities.NET_CAPABILITY_CBS);
1849 sLegacyTypeToCapability.put(TYPE_MOBILE_DUN, NetworkCapabilities.NET_CAPABILITY_DUN);
1850 sLegacyTypeToCapability.put(TYPE_MOBILE_FOTA, NetworkCapabilities.NET_CAPABILITY_FOTA);
1851 sLegacyTypeToCapability.put(TYPE_MOBILE_IMS, NetworkCapabilities.NET_CAPABILITY_IMS);
1852 sLegacyTypeToCapability.put(TYPE_MOBILE_MMS, NetworkCapabilities.NET_CAPABILITY_MMS);
1853 sLegacyTypeToCapability.put(TYPE_MOBILE_SUPL, NetworkCapabilities.NET_CAPABILITY_SUPL);
1854 sLegacyTypeToCapability.put(TYPE_WIFI_P2P, NetworkCapabilities.NET_CAPABILITY_WIFI_P2P);
1855 }
1856
1857 /**
1858 * Given a legacy type (TYPE_WIFI, ...) returns a NetworkCapabilities
1859 * instance suitable for registering a request or callback. Throws an
1860 * IllegalArgumentException if no mapping from the legacy type to
1861 * NetworkCapabilities is known.
1862 *
Chalard Jean7eaf3b12018-03-08 13:54:53 +09001863 * @deprecated Types are deprecated. Use {@link NetworkCallback} or {@link NetworkRequest}
1864 * to find the network instead.
Erik Klinece55eb12017-01-26 18:08:28 +09001865 * @hide
1866 */
1867 public static NetworkCapabilities networkCapabilitiesForType(int type) {
1868 final NetworkCapabilities nc = new NetworkCapabilities();
1869
1870 // Map from type to transports.
1871 final int NOT_FOUND = -1;
1872 final int transport = sLegacyTypeToTransport.get(type, NOT_FOUND);
Remi NGUYEN VAN81f68422021-03-15 07:31:54 +00001873 if (transport == NOT_FOUND) {
1874 throw new IllegalArgumentException("unknown legacy type: " + type);
1875 }
Erik Klinece55eb12017-01-26 18:08:28 +09001876 nc.addTransportType(transport);
1877
1878 // Map from type to capabilities.
1879 nc.addCapability(sLegacyTypeToCapability.get(
1880 type, NetworkCapabilities.NET_CAPABILITY_INTERNET));
1881 nc.maybeMarkCapabilitiesRestricted();
1882 return nc;
1883 }
1884
Lorenzo Colitti0b798a82015-06-15 14:29:22 +09001885 /** @hide */
1886 public static class PacketKeepaliveCallback {
Mathew Inwoode1a17ba2020-11-04 09:29:36 +00001887 @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.R, trackingBug = 170729553)
Artur Satayev56cb6bb2019-11-04 17:50:59 +00001888 public PacketKeepaliveCallback() {
1889 }
Lorenzo Colitti0b798a82015-06-15 14:29:22 +09001890 /** The requested keepalive was successfully started. */
Mathew Inwoode1a17ba2020-11-04 09:29:36 +00001891 @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.R, trackingBug = 170729553)
Lorenzo Colitti0b798a82015-06-15 14:29:22 +09001892 public void onStarted() {}
1893 /** The keepalive was successfully stopped. */
Mathew Inwoode1a17ba2020-11-04 09:29:36 +00001894 @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.R, trackingBug = 170729553)
Lorenzo Colitti0b798a82015-06-15 14:29:22 +09001895 public void onStopped() {}
1896 /** An error occurred. */
Mathew Inwoode1a17ba2020-11-04 09:29:36 +00001897 @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.R, trackingBug = 170729553)
Lorenzo Colitti0b798a82015-06-15 14:29:22 +09001898 public void onError(int error) {}
1899 }
1900
1901 /**
1902 * Allows applications to request that the system periodically send specific packets on their
1903 * behalf, using hardware offload to save battery power.
1904 *
1905 * To request that the system send keepalives, call one of the methods that return a
1906 * {@link ConnectivityManager.PacketKeepalive} object, such as {@link #startNattKeepalive},
1907 * passing in a non-null callback. If the callback is successfully started, the callback's
1908 * {@code onStarted} method will be called. If an error occurs, {@code onError} will be called,
1909 * specifying one of the {@code ERROR_*} constants in this class.
1910 *
Chalard Jean9dd11612018-06-04 16:52:49 +09001911 * To stop an existing keepalive, call {@link PacketKeepalive#stop}. The system will call
1912 * {@link PacketKeepaliveCallback#onStopped} if the operation was successful or
1913 * {@link PacketKeepaliveCallback#onError} if an error occurred.
Lorenzo Colitti0b798a82015-06-15 14:29:22 +09001914 *
junyulai4c95b082018-12-27 17:25:29 +08001915 * @deprecated Use {@link SocketKeepalive} instead.
1916 *
Lorenzo Colitti0b798a82015-06-15 14:29:22 +09001917 * @hide
1918 */
1919 public class PacketKeepalive {
1920
1921 private static final String TAG = "PacketKeepalive";
1922
1923 /** @hide */
1924 public static final int SUCCESS = 0;
1925
1926 /** @hide */
1927 public static final int NO_KEEPALIVE = -1;
1928
1929 /** @hide */
1930 public static final int BINDER_DIED = -10;
1931
1932 /** The specified {@code Network} is not connected. */
1933 public static final int ERROR_INVALID_NETWORK = -20;
1934 /** The specified IP addresses are invalid. For example, the specified source IP address is
1935 * not configured on the specified {@code Network}. */
1936 public static final int ERROR_INVALID_IP_ADDRESS = -21;
1937 /** The requested port is invalid. */
1938 public static final int ERROR_INVALID_PORT = -22;
1939 /** The packet length is invalid (e.g., too long). */
1940 public static final int ERROR_INVALID_LENGTH = -23;
1941 /** The packet transmission interval is invalid (e.g., too short). */
1942 public static final int ERROR_INVALID_INTERVAL = -24;
1943
1944 /** The hardware does not support this request. */
1945 public static final int ERROR_HARDWARE_UNSUPPORTED = -30;
Lorenzo Colitti723f82f2015-09-08 16:46:36 +09001946 /** The hardware returned an error. */
1947 public static final int ERROR_HARDWARE_ERROR = -31;
Lorenzo Colitti0b798a82015-06-15 14:29:22 +09001948
Nathan Harold0990bc82018-02-14 13:09:45 -08001949 /** The NAT-T destination port for IPsec */
Lorenzo Colitti0b798a82015-06-15 14:29:22 +09001950 public static final int NATT_PORT = 4500;
1951
Nathan Harold0990bc82018-02-14 13:09:45 -08001952 /** The minimum interval in seconds between keepalive packet transmissions */
1953 public static final int MIN_INTERVAL = 10;
1954
Lorenzo Colitti0b798a82015-06-15 14:29:22 +09001955 private final Network mNetwork;
junyulai070f9ff2019-01-16 20:23:34 +08001956 private final ISocketKeepaliveCallback mCallback;
1957 private final ExecutorService mExecutor;
Lorenzo Colitti0b798a82015-06-15 14:29:22 +09001958
1959 private volatile Integer mSlot;
1960
Mathew Inwoode1a17ba2020-11-04 09:29:36 +00001961 @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.R, trackingBug = 170729553)
Lorenzo Colitti0b798a82015-06-15 14:29:22 +09001962 public void stop() {
1963 try {
junyulai070f9ff2019-01-16 20:23:34 +08001964 mExecutor.execute(() -> {
1965 try {
1966 if (mSlot != null) {
1967 mService.stopKeepalive(mNetwork, mSlot);
1968 }
1969 } catch (RemoteException e) {
1970 Log.e(TAG, "Error stopping packet keepalive: ", e);
1971 throw e.rethrowFromSystemServer();
1972 }
1973 });
1974 } catch (RejectedExecutionException e) {
1975 // The internal executor has already stopped due to previous event.
Lorenzo Colitti0b798a82015-06-15 14:29:22 +09001976 }
1977 }
1978
1979 private PacketKeepalive(Network network, PacketKeepaliveCallback callback) {
Remi NGUYEN VAN81f68422021-03-15 07:31:54 +00001980 Objects.requireNonNull(network, "network cannot be null");
1981 Objects.requireNonNull(callback, "callback cannot be null");
Lorenzo Colitti0b798a82015-06-15 14:29:22 +09001982 mNetwork = network;
junyulai070f9ff2019-01-16 20:23:34 +08001983 mExecutor = Executors.newSingleThreadExecutor();
1984 mCallback = new ISocketKeepaliveCallback.Stub() {
Lorenzo Colitti0b798a82015-06-15 14:29:22 +09001985 @Override
junyulai070f9ff2019-01-16 20:23:34 +08001986 public void onStarted(int slot) {
lucaslinbe801382020-12-30 11:54:55 +08001987 final long token = Binder.clearCallingIdentity();
1988 try {
1989 mExecutor.execute(() -> {
1990 mSlot = slot;
1991 callback.onStarted();
1992 });
1993 } finally {
1994 Binder.restoreCallingIdentity(token);
1995 }
Lorenzo Colitti0b798a82015-06-15 14:29:22 +09001996 }
junyulai070f9ff2019-01-16 20:23:34 +08001997
1998 @Override
1999 public void onStopped() {
lucaslinbe801382020-12-30 11:54:55 +08002000 final long token = Binder.clearCallingIdentity();
2001 try {
2002 mExecutor.execute(() -> {
2003 mSlot = null;
2004 callback.onStopped();
2005 });
2006 } finally {
2007 Binder.restoreCallingIdentity(token);
2008 }
junyulai070f9ff2019-01-16 20:23:34 +08002009 mExecutor.shutdown();
2010 }
2011
2012 @Override
2013 public void onError(int error) {
lucaslinbe801382020-12-30 11:54:55 +08002014 final long token = Binder.clearCallingIdentity();
2015 try {
2016 mExecutor.execute(() -> {
2017 mSlot = null;
2018 callback.onError(error);
2019 });
2020 } finally {
2021 Binder.restoreCallingIdentity(token);
2022 }
junyulai070f9ff2019-01-16 20:23:34 +08002023 mExecutor.shutdown();
2024 }
2025
2026 @Override
2027 public void onDataReceived() {
2028 // PacketKeepalive is only used for Nat-T keepalive and as such does not invoke
2029 // this callback when data is received.
2030 }
2031 };
Lorenzo Colitti0b798a82015-06-15 14:29:22 +09002032 }
2033 }
2034
2035 /**
2036 * Starts an IPsec NAT-T keepalive packet with the specified parameters.
2037 *
junyulai4c95b082018-12-27 17:25:29 +08002038 * @deprecated Use {@link #createSocketKeepalive} instead.
2039 *
Lorenzo Colitti0b798a82015-06-15 14:29:22 +09002040 * @hide
2041 */
Mathew Inwoode1a17ba2020-11-04 09:29:36 +00002042 @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.R, trackingBug = 170729553)
Lorenzo Colitti0b798a82015-06-15 14:29:22 +09002043 public PacketKeepalive startNattKeepalive(
2044 Network network, int intervalSeconds, PacketKeepaliveCallback callback,
2045 InetAddress srcAddr, int srcPort, InetAddress dstAddr) {
2046 final PacketKeepalive k = new PacketKeepalive(network, callback);
2047 try {
junyulai070f9ff2019-01-16 20:23:34 +08002048 mService.startNattKeepalive(network, intervalSeconds, k.mCallback,
Lorenzo Colitti0b798a82015-06-15 14:29:22 +09002049 srcAddr.getHostAddress(), srcPort, dstAddr.getHostAddress());
2050 } catch (RemoteException e) {
2051 Log.e(TAG, "Error starting packet keepalive: ", e);
junyulai070f9ff2019-01-16 20:23:34 +08002052 throw e.rethrowFromSystemServer();
Lorenzo Colitti0b798a82015-06-15 14:29:22 +09002053 }
2054 return k;
2055 }
2056
Chiachang Wang619319a2021-01-15 11:06:21 +08002057 // Construct an invalid fd.
2058 private ParcelFileDescriptor createInvalidFd() {
2059 final int invalidFd = -1;
2060 return ParcelFileDescriptor.adoptFd(invalidFd);
2061 }
2062
The Android Open Source Project28527d22009-03-03 19:31:44 -08002063 /**
junyulai4c95b082018-12-27 17:25:29 +08002064 * Request that keepalives be started on a IPsec NAT-T socket.
2065 *
2066 * @param network The {@link Network} the socket is on.
2067 * @param socket The socket that needs to be kept alive.
2068 * @param source The source address of the {@link UdpEncapsulationSocket}.
2069 * @param destination The destination address of the {@link UdpEncapsulationSocket}.
2070 * @param executor The executor on which callback will be invoked. The provided {@link Executor}
2071 * must run callback sequentially, otherwise the order of callbacks cannot be
2072 * guaranteed.
2073 * @param callback A {@link SocketKeepalive.Callback}. Used for notifications about keepalive
2074 * changes. Must be extended by applications that use this API.
2075 *
junyulai0835a1e2019-01-08 20:04:33 +08002076 * @return A {@link SocketKeepalive} object that can be used to control the keepalive on the
2077 * given socket.
junyulai4c95b082018-12-27 17:25:29 +08002078 **/
junyulai7e06ad42019-03-04 22:45:36 +08002079 public @NonNull SocketKeepalive createSocketKeepalive(@NonNull Network network,
junyulai4c95b082018-12-27 17:25:29 +08002080 @NonNull UdpEncapsulationSocket socket,
2081 @NonNull InetAddress source,
2082 @NonNull InetAddress destination,
2083 @NonNull @CallbackExecutor Executor executor,
2084 @NonNull Callback callback) {
junyulai7e06ad42019-03-04 22:45:36 +08002085 ParcelFileDescriptor dup;
2086 try {
junyulai828dad12019-03-27 11:00:37 +08002087 // Dup is needed here as the pfd inside the socket is owned by the IpSecService,
2088 // which cannot be obtained by the app process.
junyulai7e06ad42019-03-04 22:45:36 +08002089 dup = ParcelFileDescriptor.dup(socket.getFileDescriptor());
2090 } catch (IOException ignored) {
2091 // Construct an invalid fd, so that if the user later calls start(), it will fail with
2092 // ERROR_INVALID_SOCKET.
Chiachang Wang619319a2021-01-15 11:06:21 +08002093 dup = createInvalidFd();
junyulai7e06ad42019-03-04 22:45:36 +08002094 }
2095 return new NattSocketKeepalive(mService, network, dup, socket.getResourceId(), source,
2096 destination, executor, callback);
junyulaid05a1922019-01-15 11:32:44 +08002097 }
2098
2099 /**
2100 * Request that keepalives be started on a IPsec NAT-T socket file descriptor. Directly called
2101 * by system apps which don't use IpSecService to create {@link UdpEncapsulationSocket}.
2102 *
2103 * @param network The {@link Network} the socket is on.
junyulai7e06ad42019-03-04 22:45:36 +08002104 * @param pfd The {@link ParcelFileDescriptor} that needs to be kept alive. The provided
2105 * {@link ParcelFileDescriptor} must be bound to a port and the keepalives will be sent
2106 * from that port.
junyulaid05a1922019-01-15 11:32:44 +08002107 * @param source The source address of the {@link UdpEncapsulationSocket}.
2108 * @param destination The destination address of the {@link UdpEncapsulationSocket}. The
2109 * keepalive packets will always be sent to port 4500 of the given {@code destination}.
2110 * @param executor The executor on which callback will be invoked. The provided {@link Executor}
2111 * must run callback sequentially, otherwise the order of callbacks cannot be
2112 * guaranteed.
2113 * @param callback A {@link SocketKeepalive.Callback}. Used for notifications about keepalive
2114 * changes. Must be extended by applications that use this API.
2115 *
junyulai0835a1e2019-01-08 20:04:33 +08002116 * @return A {@link SocketKeepalive} object that can be used to control the keepalive on the
2117 * given socket.
junyulaid05a1922019-01-15 11:32:44 +08002118 * @hide
2119 */
2120 @SystemApi
2121 @RequiresPermission(android.Manifest.permission.PACKET_KEEPALIVE_OFFLOAD)
junyulai7e06ad42019-03-04 22:45:36 +08002122 public @NonNull SocketKeepalive createNattKeepalive(@NonNull Network network,
2123 @NonNull ParcelFileDescriptor pfd,
junyulaid05a1922019-01-15 11:32:44 +08002124 @NonNull InetAddress source,
2125 @NonNull InetAddress destination,
2126 @NonNull @CallbackExecutor Executor executor,
2127 @NonNull Callback callback) {
junyulai7e06ad42019-03-04 22:45:36 +08002128 ParcelFileDescriptor dup;
2129 try {
junyulai828dad12019-03-27 11:00:37 +08002130 // TODO: Consider remove unnecessary dup.
junyulai7e06ad42019-03-04 22:45:36 +08002131 dup = pfd.dup();
2132 } catch (IOException ignored) {
2133 // Construct an invalid fd, so that if the user later calls start(), it will fail with
2134 // ERROR_INVALID_SOCKET.
Chiachang Wang619319a2021-01-15 11:06:21 +08002135 dup = createInvalidFd();
junyulai7e06ad42019-03-04 22:45:36 +08002136 }
2137 return new NattSocketKeepalive(mService, network, dup,
Remi NGUYEN VANce355502021-03-11 10:56:49 +00002138 -1 /* Unused */, source, destination, executor, callback);
junyulai4c95b082018-12-27 17:25:29 +08002139 }
2140
2141 /**
junyulai0835a1e2019-01-08 20:04:33 +08002142 * Request that keepalives be started on a TCP socket.
2143 * The socket must be established.
2144 *
2145 * @param network The {@link Network} the socket is on.
2146 * @param socket The socket that needs to be kept alive.
2147 * @param executor The executor on which callback will be invoked. This implementation assumes
2148 * the provided {@link Executor} runs the callbacks in sequence with no
2149 * concurrency. Failing this, no guarantee of correctness can be made. It is
2150 * the responsibility of the caller to ensure the executor provides this
2151 * guarantee. A simple way of creating such an executor is with the standard
2152 * tool {@code Executors.newSingleThreadExecutor}.
2153 * @param callback A {@link SocketKeepalive.Callback}. Used for notifications about keepalive
2154 * changes. Must be extended by applications that use this API.
2155 *
2156 * @return A {@link SocketKeepalive} object that can be used to control the keepalive on the
2157 * given socket.
2158 * @hide
2159 */
2160 @SystemApi
2161 @RequiresPermission(android.Manifest.permission.PACKET_KEEPALIVE_OFFLOAD)
junyulai7e06ad42019-03-04 22:45:36 +08002162 public @NonNull SocketKeepalive createSocketKeepalive(@NonNull Network network,
junyulai0835a1e2019-01-08 20:04:33 +08002163 @NonNull Socket socket,
2164 @NonNull Executor executor,
2165 @NonNull Callback callback) {
junyulai7e06ad42019-03-04 22:45:36 +08002166 ParcelFileDescriptor dup;
2167 try {
2168 dup = ParcelFileDescriptor.fromSocket(socket);
2169 } catch (UncheckedIOException ignored) {
2170 // Construct an invalid fd, so that if the user later calls start(), it will fail with
2171 // ERROR_INVALID_SOCKET.
Chiachang Wang619319a2021-01-15 11:06:21 +08002172 dup = createInvalidFd();
junyulai7e06ad42019-03-04 22:45:36 +08002173 }
2174 return new TcpSocketKeepalive(mService, network, dup, executor, callback);
junyulai0835a1e2019-01-08 20:04:33 +08002175 }
2176
2177 /**
The Android Open Source Project28527d22009-03-03 19:31:44 -08002178 * Ensure that a network route exists to deliver traffic to the specified
2179 * host via the specified network interface. An attempt to add a route that
2180 * already exists is ignored, but treated as successful.
Lorenzo Colitti0b40c872015-10-15 16:29:00 +09002181 *
2182 * <p>This method requires the caller to hold either the
2183 * {@link android.Manifest.permission#CHANGE_NETWORK_STATE} permission
2184 * or the ability to modify system settings as determined by
2185 * {@link android.provider.Settings.System#canWrite}.</p>
2186 *
The Android Open Source Project28527d22009-03-03 19:31:44 -08002187 * @param networkType the type of the network over which traffic to the specified
2188 * host is to be routed
2189 * @param hostAddress the IP address of the host to which the route is desired
2190 * @return {@code true} on success, {@code false} on failure
Robert Greenwaltf3017f72014-05-18 23:07:25 -07002191 *
Lorenzo Colitti7f6a2bf2015-04-24 17:03:31 +09002192 * @deprecated Deprecated in favor of the
2193 * {@link #requestNetwork(NetworkRequest, NetworkCallback)},
2194 * {@link #bindProcessToNetwork} and {@link Network#getSocketFactory} API.
Dianne Hackborn18c1d832015-07-31 10:35:34 -07002195 * In {@link VERSION_CODES#M}, and above, this method is unsupported and will
Lorenzo Colitti2780e4b2015-07-29 11:41:21 +09002196 * throw {@code UnsupportedOperationException} if called.
Lorenzo Colitti84d7f072016-12-09 18:39:30 +09002197 * @removed
The Android Open Source Project28527d22009-03-03 19:31:44 -08002198 */
Aurimas Liutikase80a0eb2016-05-24 15:22:55 -07002199 @Deprecated
The Android Open Source Project28527d22009-03-03 19:31:44 -08002200 public boolean requestRouteToHost(int networkType, int hostAddress) {
Sreeram Ramachandranc06ec732014-07-19 23:21:46 -07002201 return requestRouteToHostAddress(networkType, NetworkUtils.intToInetAddress(hostAddress));
Robert Greenwalt7fe44cb2010-08-27 09:24:29 -07002202 }
2203
2204 /**
2205 * Ensure that a network route exists to deliver traffic to the specified
2206 * host via the specified network interface. An attempt to add a route that
2207 * already exists is ignored, but treated as successful.
Lorenzo Colitti0b40c872015-10-15 16:29:00 +09002208 *
2209 * <p>This method requires the caller to hold either the
2210 * {@link android.Manifest.permission#CHANGE_NETWORK_STATE} permission
2211 * or the ability to modify system settings as determined by
2212 * {@link android.provider.Settings.System#canWrite}.</p>
2213 *
Robert Greenwalt7fe44cb2010-08-27 09:24:29 -07002214 * @param networkType the type of the network over which traffic to the specified
2215 * host is to be routed
2216 * @param hostAddress the IP address of the host to which the route is desired
2217 * @return {@code true} on success, {@code false} on failure
2218 * @hide
Robert Greenwaltf3017f72014-05-18 23:07:25 -07002219 * @deprecated Deprecated in favor of the {@link #requestNetwork} and
Lorenzo Colitti7f6a2bf2015-04-24 17:03:31 +09002220 * {@link #bindProcessToNetwork} API.
Robert Greenwalt7fe44cb2010-08-27 09:24:29 -07002221 */
Aurimas Liutikase80a0eb2016-05-24 15:22:55 -07002222 @Deprecated
Mathew Inwoodc5b9bec2018-08-08 14:52:47 +01002223 @UnsupportedAppUsage
lucasline257bce2021-03-22 11:51:27 +08002224 @SystemApi(client = MODULE_LIBRARIES)
Robert Greenwalt7fe44cb2010-08-27 09:24:29 -07002225 public boolean requestRouteToHostAddress(int networkType, InetAddress hostAddress) {
Lorenzo Colitti2780e4b2015-07-29 11:41:21 +09002226 checkLegacyRoutingApiAccess();
The Android Open Source Project28527d22009-03-03 19:31:44 -08002227 try {
Philip P. Moltmann7bc33df2020-03-26 11:50:35 -07002228 return mService.requestRouteToHostAddress(networkType, hostAddress.getAddress(),
2229 mContext.getOpPackageName(), getAttributionTag());
The Android Open Source Project28527d22009-03-03 19:31:44 -08002230 } catch (RemoteException e) {
Jeff Sharkeyc78f85c2016-03-01 19:27:23 -07002231 throw e.rethrowFromSystemServer();
The Android Open Source Project28527d22009-03-03 19:31:44 -08002232 }
2233 }
2234
2235 /**
Philip P. Moltmann7bc33df2020-03-26 11:50:35 -07002236 * @return the context's attribution tag
2237 */
2238 // TODO: Remove method and replace with direct call once R code is pushed to AOSP
2239 private @Nullable String getAttributionTag() {
Roshan Piusaa24fde2020-12-17 14:53:09 -08002240 return mContext.getAttributionTag();
Philip P. Moltmann7bc33df2020-03-26 11:50:35 -07002241 }
2242
2243 /**
The Android Open Source Project28527d22009-03-03 19:31:44 -08002244 * Returns the value of the setting for background data usage. If false,
2245 * applications should not use the network if the application is not in the
2246 * foreground. Developers should respect this setting, and check the value
2247 * of this before performing any background data operations.
2248 * <p>
2249 * All applications that have background services that use the network
2250 * should listen to {@link #ACTION_BACKGROUND_DATA_SETTING_CHANGED}.
Jeff Sharkey39c01eb2011-08-16 14:37:57 -07002251 * <p>
Scott Main50589142011-10-06 18:32:43 -07002252 * @deprecated As of {@link VERSION_CODES#ICE_CREAM_SANDWICH}, availability of
Jeff Sharkey39c01eb2011-08-16 14:37:57 -07002253 * background data depends on several combined factors, and this method will
2254 * always return {@code true}. Instead, when background data is unavailable,
2255 * {@link #getActiveNetworkInfo()} will now appear disconnected.
Danica Chang96567052010-08-11 14:54:43 -07002256 *
The Android Open Source Project28527d22009-03-03 19:31:44 -08002257 * @return Whether background data usage is allowed.
2258 */
Jeff Sharkey39c01eb2011-08-16 14:37:57 -07002259 @Deprecated
The Android Open Source Project28527d22009-03-03 19:31:44 -08002260 public boolean getBackgroundDataSetting() {
Jeff Sharkey39c01eb2011-08-16 14:37:57 -07002261 // assume that background data is allowed; final authority is
2262 // NetworkInfo which may be blocked.
2263 return true;
The Android Open Source Project28527d22009-03-03 19:31:44 -08002264 }
2265
2266 /**
2267 * Sets the value of the setting for background data usage.
Robert Greenwalt1b21f6c2010-02-23 18:58:05 -08002268 *
The Android Open Source Project28527d22009-03-03 19:31:44 -08002269 * @param allowBackgroundData Whether an application should use data while
2270 * it is in the background.
Robert Greenwalt1b21f6c2010-02-23 18:58:05 -08002271 *
The Android Open Source Project28527d22009-03-03 19:31:44 -08002272 * @attr ref android.Manifest.permission#CHANGE_BACKGROUND_DATA_SETTING
2273 * @see #getBackgroundDataSetting()
2274 * @hide
2275 */
Jeff Sharkey39c01eb2011-08-16 14:37:57 -07002276 @Deprecated
Mathew Inwoodc5b9bec2018-08-08 14:52:47 +01002277 @UnsupportedAppUsage
The Android Open Source Project28527d22009-03-03 19:31:44 -08002278 public void setBackgroundDataSetting(boolean allowBackgroundData) {
Jeff Sharkey39c01eb2011-08-16 14:37:57 -07002279 // ignored
The Android Open Source Project28527d22009-03-03 19:31:44 -08002280 }
Robert Greenwalt1b21f6c2010-02-23 18:58:05 -08002281
Jeff Sharkey66fa9682011-08-02 17:22:34 -07002282 /**
Robert Greenwalt1b21f6c2010-02-23 18:58:05 -08002283 * @hide
Robert Greenwalt0c150c02014-05-21 20:04:36 -07002284 * @deprecated Talk to TelephonyManager directly
Robert Greenwalt1b21f6c2010-02-23 18:58:05 -08002285 */
Aurimas Liutikase80a0eb2016-05-24 15:22:55 -07002286 @Deprecated
Mathew Inwoodc5b9bec2018-08-08 14:52:47 +01002287 @UnsupportedAppUsage
Robert Greenwalt1b21f6c2010-02-23 18:58:05 -08002288 public boolean getMobileDataEnabled() {
Meng Wanged6f4412019-11-18 17:10:00 -08002289 TelephonyManager tm = mContext.getSystemService(TelephonyManager.class);
2290 if (tm != null) {
2291 int subId = SubscriptionManager.getDefaultDataSubscriptionId();
2292 Log.d("ConnectivityManager", "getMobileDataEnabled()+ subId=" + subId);
2293 boolean retVal = tm.createForSubscriptionId(subId).isDataEnabled();
2294 Log.d("ConnectivityManager", "getMobileDataEnabled()- subId=" + subId
2295 + " retVal=" + retVal);
2296 return retVal;
Robert Greenwalt1b21f6c2010-02-23 18:58:05 -08002297 }
Wink Saville689f7042014-12-05 11:10:30 -08002298 Log.d("ConnectivityManager", "getMobileDataEnabled()- remote exception retVal=false");
Robert Greenwalt0c150c02014-05-21 20:04:36 -07002299 return false;
Robert Greenwalt1b21f6c2010-02-23 18:58:05 -08002300 }
2301
The Android Open Source Project28527d22009-03-03 19:31:44 -08002302 /**
Robert Greenwaltad35b132014-09-04 16:44:35 -07002303 * Callback for use with {@link ConnectivityManager#addDefaultNetworkActiveListener}
Robert Greenwaltf57c03c2014-06-11 16:05:07 -07002304 * to find out when the system default network has gone in to a high power state.
Dianne Hackborn5ac88162014-02-26 16:20:52 -08002305 */
2306 public interface OnNetworkActiveListener {
2307 /**
2308 * Called on the main thread of the process to report that the current data network
2309 * has become active, and it is now a good time to perform any pending network
2310 * operations. Note that this listener only tells you when the network becomes
2311 * active; if at any other time you want to know whether it is active (and thus okay
2312 * to initiate network traffic), you can retrieve its instantaneous state with
Robert Greenwaltf57c03c2014-06-11 16:05:07 -07002313 * {@link ConnectivityManager#isDefaultNetworkActive}.
Dianne Hackborn5ac88162014-02-26 16:20:52 -08002314 */
Chalard Jean9dd11612018-06-04 16:52:49 +09002315 void onNetworkActive();
Dianne Hackborn5ac88162014-02-26 16:20:52 -08002316 }
2317
Dianne Hackborn5ac88162014-02-26 16:20:52 -08002318 private final ArrayMap<OnNetworkActiveListener, INetworkActivityListener>
Chalard Jean9dd11612018-06-04 16:52:49 +09002319 mNetworkActivityListeners = new ArrayMap<>();
Dianne Hackborn5ac88162014-02-26 16:20:52 -08002320
2321 /**
Robert Greenwaltf57c03c2014-06-11 16:05:07 -07002322 * Start listening to reports when the system's default data network is active, meaning it is
2323 * a good time to perform network traffic. Use {@link #isDefaultNetworkActive()}
2324 * to determine the current state of the system's default network after registering the
2325 * listener.
2326 * <p>
2327 * If the process default network has been set with
Paul Jensenee2f45d2015-03-10 10:54:12 -04002328 * {@link ConnectivityManager#bindProcessToNetwork} this function will not
Robert Greenwaltf57c03c2014-06-11 16:05:07 -07002329 * reflect the process's default, but the system default.
Dianne Hackborn5ac88162014-02-26 16:20:52 -08002330 *
2331 * @param l The listener to be told when the network is active.
2332 */
Robert Greenwaltad35b132014-09-04 16:44:35 -07002333 public void addDefaultNetworkActiveListener(final OnNetworkActiveListener l) {
Dianne Hackborn5ac88162014-02-26 16:20:52 -08002334 INetworkActivityListener rl = new INetworkActivityListener.Stub() {
2335 @Override
2336 public void onNetworkActive() throws RemoteException {
2337 l.onNetworkActive();
2338 }
2339 };
2340
2341 try {
lucaslin1193a5d2021-01-21 02:04:15 +08002342 mService.registerNetworkActivityListener(rl);
Dianne Hackborn5ac88162014-02-26 16:20:52 -08002343 mNetworkActivityListeners.put(l, rl);
2344 } catch (RemoteException e) {
Jeff Sharkeyc78f85c2016-03-01 19:27:23 -07002345 throw e.rethrowFromSystemServer();
Dianne Hackborn5ac88162014-02-26 16:20:52 -08002346 }
2347 }
2348
2349 /**
2350 * Remove network active listener previously registered with
Robert Greenwaltad35b132014-09-04 16:44:35 -07002351 * {@link #addDefaultNetworkActiveListener}.
Dianne Hackborn5ac88162014-02-26 16:20:52 -08002352 *
2353 * @param l Previously registered listener.
2354 */
Chalard Jean158702d2019-01-07 19:26:34 +09002355 public void removeDefaultNetworkActiveListener(@NonNull OnNetworkActiveListener l) {
Dianne Hackborn5ac88162014-02-26 16:20:52 -08002356 INetworkActivityListener rl = mNetworkActivityListeners.get(l);
Remi NGUYEN VAN81f68422021-03-15 07:31:54 +00002357 if (rl == null) {
2358 throw new IllegalArgumentException("Listener was not registered.");
2359 }
Dianne Hackborn5ac88162014-02-26 16:20:52 -08002360 try {
lucaslin1193a5d2021-01-21 02:04:15 +08002361 mService.registerNetworkActivityListener(rl);
Dianne Hackborn5ac88162014-02-26 16:20:52 -08002362 } catch (RemoteException e) {
Jeff Sharkeyc78f85c2016-03-01 19:27:23 -07002363 throw e.rethrowFromSystemServer();
Dianne Hackborn5ac88162014-02-26 16:20:52 -08002364 }
2365 }
2366
2367 /**
2368 * Return whether the data network is currently active. An active network means that
2369 * it is currently in a high power state for performing data transmission. On some
2370 * types of networks, it may be expensive to move and stay in such a state, so it is
2371 * more power efficient to batch network traffic together when the radio is already in
2372 * this state. This method tells you whether right now is currently a good time to
2373 * initiate network traffic, as the network is already active.
2374 */
Robert Greenwaltf57c03c2014-06-11 16:05:07 -07002375 public boolean isDefaultNetworkActive() {
Dianne Hackborn5ac88162014-02-26 16:20:52 -08002376 try {
lucaslin1193a5d2021-01-21 02:04:15 +08002377 return mService.isDefaultNetworkActive();
Dianne Hackborn5ac88162014-02-26 16:20:52 -08002378 } catch (RemoteException e) {
Jeff Sharkeyc78f85c2016-03-01 19:27:23 -07002379 throw e.rethrowFromSystemServer();
Dianne Hackborn5ac88162014-02-26 16:20:52 -08002380 }
Dianne Hackborn5ac88162014-02-26 16:20:52 -08002381 }
2382
2383 /**
The Android Open Source Project28527d22009-03-03 19:31:44 -08002384 * {@hide}
2385 */
Lorenzo Colitti2780e4b2015-07-29 11:41:21 +09002386 public ConnectivityManager(Context context, IConnectivityManager service) {
Remi NGUYEN VAN81f68422021-03-15 07:31:54 +00002387 mContext = Objects.requireNonNull(context, "missing context");
2388 mService = Objects.requireNonNull(service, "missing IConnectivityManager");
Amos Bianchia9b415a2020-03-04 11:07:38 -08002389 mTetheringManager = (TetheringManager) mContext.getSystemService(Context.TETHERING_SERVICE);
Paul Jensenc0618a62014-12-10 15:12:18 -05002390 sInstance = this;
The Android Open Source Project28527d22009-03-03 19:31:44 -08002391 }
Robert Greenwalt0c4828c2010-01-26 11:40:34 -08002392
Jeff Sharkey2d9e5a52012-04-04 20:40:58 -07002393 /** {@hide} */
Mathew Inwoodc5b9bec2018-08-08 14:52:47 +01002394 @UnsupportedAppUsage
Jeff Sharkey2d9e5a52012-04-04 20:40:58 -07002395 public static ConnectivityManager from(Context context) {
2396 return (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
2397 }
2398
Remi NGUYEN VAN85391292018-12-27 16:43:56 +09002399 /** @hide */
2400 public NetworkRequest getDefaultRequest() {
2401 try {
2402 // This is not racy as the default request is final in ConnectivityService.
2403 return mService.getDefaultRequest();
2404 } catch (RemoteException e) {
2405 throw e.rethrowFromSystemServer();
2406 }
2407 }
2408
Robert Greenwalt0c4828c2010-01-26 11:40:34 -08002409 /**
Philip P. Moltmann7bc33df2020-03-26 11:50:35 -07002410 * Check if the package is a allowed to write settings. This also accounts that such an access
2411 * happened.
2412 *
2413 * @return {@code true} iff the package is allowed to write settings.
2414 */
2415 // TODO: Remove method and replace with direct call once R code is pushed to AOSP
2416 private static boolean checkAndNoteWriteSettingsOperation(@NonNull Context context, int uid,
2417 @NonNull String callingPackage, @Nullable String callingAttributionTag,
2418 boolean throwException) {
2419 return Settings.checkAndNoteWriteSettingsOperation(context, uid, callingPackage,
2420 throwException);
2421 }
2422
2423 /**
Paul Jensenc0618a62014-12-10 15:12:18 -05002424 * @deprecated - use getSystemService. This is a kludge to support static access in certain
2425 * situations where a Context pointer is unavailable.
2426 * @hide
2427 */
Aurimas Liutikase80a0eb2016-05-24 15:22:55 -07002428 @Deprecated
Paul Jensenee2f45d2015-03-10 10:54:12 -04002429 static ConnectivityManager getInstanceOrNull() {
2430 return sInstance;
2431 }
2432
2433 /**
2434 * @deprecated - use getSystemService. This is a kludge to support static access in certain
2435 * situations where a Context pointer is unavailable.
2436 * @hide
2437 */
Aurimas Liutikase80a0eb2016-05-24 15:22:55 -07002438 @Deprecated
Mathew Inwoodc5b9bec2018-08-08 14:52:47 +01002439 @UnsupportedAppUsage
Paul Jensenee2f45d2015-03-10 10:54:12 -04002440 private static ConnectivityManager getInstance() {
2441 if (getInstanceOrNull() == null) {
Paul Jensenc0618a62014-12-10 15:12:18 -05002442 throw new IllegalStateException("No ConnectivityManager yet constructed");
2443 }
Paul Jensenee2f45d2015-03-10 10:54:12 -04002444 return getInstanceOrNull();
Paul Jensenc0618a62014-12-10 15:12:18 -05002445 }
2446
2447 /**
Robert Greenwaltfaa4b402013-02-15 10:56:35 -08002448 * Get the set of tetherable, available interfaces. This list is limited by
2449 * device configuration and current interface existence.
2450 *
2451 * @return an array of 0 or more Strings of tetherable interface names.
2452 *
markchien6ae63e52020-01-21 13:11:06 +08002453 * @deprecated Use {@link TetheringEventCallback#onTetherableInterfacesChanged(List)} instead.
Robert Greenwalt0c4828c2010-01-26 11:40:34 -08002454 * {@hide}
2455 */
Jeff Sharkey656584a2017-04-24 11:18:03 -06002456 @RequiresPermission(android.Manifest.permission.ACCESS_NETWORK_STATE)
Mathew Inwoodc5b9bec2018-08-08 14:52:47 +01002457 @UnsupportedAppUsage
markchien6ae63e52020-01-21 13:11:06 +08002458 @Deprecated
Robert Greenwalt0c4828c2010-01-26 11:40:34 -08002459 public String[] getTetherableIfaces() {
Amos Bianchia9b415a2020-03-04 11:07:38 -08002460 return mTetheringManager.getTetherableIfaces();
Robert Greenwalt0c4828c2010-01-26 11:40:34 -08002461 }
2462
2463 /**
Robert Greenwaltfaa4b402013-02-15 10:56:35 -08002464 * Get the set of tethered interfaces.
2465 *
2466 * @return an array of 0 or more String of currently tethered interface names.
2467 *
markchien6ae63e52020-01-21 13:11:06 +08002468 * @deprecated Use {@link TetheringEventCallback#onTetherableInterfacesChanged(List)} instead.
Robert Greenwalt0c4828c2010-01-26 11:40:34 -08002469 * {@hide}
2470 */
Jeff Sharkey656584a2017-04-24 11:18:03 -06002471 @RequiresPermission(android.Manifest.permission.ACCESS_NETWORK_STATE)
Mathew Inwoodc5b9bec2018-08-08 14:52:47 +01002472 @UnsupportedAppUsage
markchien6ae63e52020-01-21 13:11:06 +08002473 @Deprecated
Robert Greenwalt0c4828c2010-01-26 11:40:34 -08002474 public String[] getTetheredIfaces() {
Amos Bianchia9b415a2020-03-04 11:07:38 -08002475 return mTetheringManager.getTetheredIfaces();
Robert Greenwalt0c4828c2010-01-26 11:40:34 -08002476 }
2477
2478 /**
Robert Greenwaltfaa4b402013-02-15 10:56:35 -08002479 * Get the set of interface names which attempted to tether but
2480 * failed. Re-attempting to tether may cause them to reset to the Tethered
2481 * state. Alternatively, causing the interface to be destroyed and recreated
2482 * may cause them to reset to the available state.
2483 * {@link ConnectivityManager#getLastTetherError} can be used to get more
2484 * information on the cause of the errors.
2485 *
2486 * @return an array of 0 or more String indicating the interface names
2487 * which failed to tether.
2488 *
markchien6ae63e52020-01-21 13:11:06 +08002489 * @deprecated Use {@link TetheringEventCallback#onError(String, int)} instead.
Robert Greenwalt0c4828c2010-01-26 11:40:34 -08002490 * {@hide}
2491 */
Jeff Sharkey656584a2017-04-24 11:18:03 -06002492 @RequiresPermission(android.Manifest.permission.ACCESS_NETWORK_STATE)
Mathew Inwoodc5b9bec2018-08-08 14:52:47 +01002493 @UnsupportedAppUsage
markchien6ae63e52020-01-21 13:11:06 +08002494 @Deprecated
Robert Greenwalt4283ded2010-03-02 17:25:02 -08002495 public String[] getTetheringErroredIfaces() {
Amos Bianchia9b415a2020-03-04 11:07:38 -08002496 return mTetheringManager.getTetheringErroredIfaces();
Robert Greenwalt0c4828c2010-01-26 11:40:34 -08002497 }
2498
2499 /**
Robert Greenwalte594a762014-06-23 14:53:42 -07002500 * Get the set of tethered dhcp ranges.
2501 *
markchien2e6ba522020-02-14 11:55:48 +08002502 * @deprecated This method is not supported.
2503 * TODO: remove this function when all of clients are removed.
Robert Greenwalte594a762014-06-23 14:53:42 -07002504 * {@hide}
2505 */
paulhu8e96a752019-08-12 16:25:11 +08002506 @RequiresPermission(android.Manifest.permission.NETWORK_SETTINGS)
markchien6ae63e52020-01-21 13:11:06 +08002507 @Deprecated
Robert Greenwalte594a762014-06-23 14:53:42 -07002508 public String[] getTetheredDhcpRanges() {
markchien2e6ba522020-02-14 11:55:48 +08002509 throw new UnsupportedOperationException("getTetheredDhcpRanges is not supported");
Robert Greenwalte594a762014-06-23 14:53:42 -07002510 }
2511
2512 /**
Robert Greenwaltfaa4b402013-02-15 10:56:35 -08002513 * Attempt to tether the named interface. This will setup a dhcp server
2514 * on the interface, forward and NAT IP packets and forward DNS requests
2515 * to the best active upstream network interface. Note that if no upstream
2516 * IP network interface is available, dhcp will still run and traffic will be
2517 * allowed between the tethered devices and this device, though upstream net
2518 * access will of course fail until an upstream network interface becomes
2519 * active.
Lorenzo Colitti0b40c872015-10-15 16:29:00 +09002520 *
2521 * <p>This method requires the caller to hold either the
2522 * {@link android.Manifest.permission#CHANGE_NETWORK_STATE} permission
2523 * or the ability to modify system settings as determined by
2524 * {@link android.provider.Settings.System#canWrite}.</p>
Robert Greenwaltfaa4b402013-02-15 10:56:35 -08002525 *
Jeremy Klein3dabcb92016-01-22 14:11:45 -08002526 * <p>WARNING: New clients should not use this function. The only usages should be in PanService
2527 * and WifiStateMachine which need direct access. All other clients should use
2528 * {@link #startTethering} and {@link #stopTethering} which encapsulate proper provisioning
2529 * logic.</p>
2530 *
Robert Greenwaltfaa4b402013-02-15 10:56:35 -08002531 * @param iface the interface name to tether.
2532 * @return error a {@code TETHER_ERROR} value indicating success or failure type
markchien91c78e52020-01-20 19:31:56 +08002533 * @deprecated Use {@link TetheringManager#startTethering} instead
Robert Greenwaltfaa4b402013-02-15 10:56:35 -08002534 *
Robert Greenwalt0c4828c2010-01-26 11:40:34 -08002535 * {@hide}
2536 */
Mathew Inwoode1a17ba2020-11-04 09:29:36 +00002537 @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.R, trackingBug = 170729553)
markchien91c78e52020-01-20 19:31:56 +08002538 @Deprecated
Robert Greenwalt4283ded2010-03-02 17:25:02 -08002539 public int tether(String iface) {
Amos Bianchia9b415a2020-03-04 11:07:38 -08002540 return mTetheringManager.tether(iface);
Robert Greenwalt4283ded2010-03-02 17:25:02 -08002541 }
2542
2543 /**
Robert Greenwaltfaa4b402013-02-15 10:56:35 -08002544 * Stop tethering the named interface.
Lorenzo Colitti0b40c872015-10-15 16:29:00 +09002545 *
2546 * <p>This method requires the caller to hold either the
2547 * {@link android.Manifest.permission#CHANGE_NETWORK_STATE} permission
2548 * or the ability to modify system settings as determined by
2549 * {@link android.provider.Settings.System#canWrite}.</p>
Robert Greenwaltfaa4b402013-02-15 10:56:35 -08002550 *
Jeremy Klein3dabcb92016-01-22 14:11:45 -08002551 * <p>WARNING: New clients should not use this function. The only usages should be in PanService
2552 * and WifiStateMachine which need direct access. All other clients should use
2553 * {@link #startTethering} and {@link #stopTethering} which encapsulate proper provisioning
2554 * logic.</p>
2555 *
Robert Greenwaltfaa4b402013-02-15 10:56:35 -08002556 * @param iface the interface name to untether.
2557 * @return error a {@code TETHER_ERROR} value indicating success or failure type
2558 *
Robert Greenwalt4283ded2010-03-02 17:25:02 -08002559 * {@hide}
2560 */
Mathew Inwoodc5b9bec2018-08-08 14:52:47 +01002561 @UnsupportedAppUsage
markchien6ae63e52020-01-21 13:11:06 +08002562 @Deprecated
Robert Greenwalt4283ded2010-03-02 17:25:02 -08002563 public int untether(String iface) {
Amos Bianchia9b415a2020-03-04 11:07:38 -08002564 return mTetheringManager.untether(iface);
Robert Greenwalt0c4828c2010-01-26 11:40:34 -08002565 }
Robert Greenwalt8e87f122010-02-11 18:18:40 -08002566
2567 /**
Robert Greenwaltfaa4b402013-02-15 10:56:35 -08002568 * Check if the device allows for tethering. It may be disabled via
Dianne Hackborn5ac88162014-02-26 16:20:52 -08002569 * {@code ro.tether.denied} system property, Settings.TETHER_SUPPORTED or
Robert Greenwaltfaa4b402013-02-15 10:56:35 -08002570 * due to device configuration.
2571 *
Chalard Jeanffacaef2017-09-26 15:45:18 +09002572 * <p>If this app does not have permission to use this API, it will always
2573 * return false rather than throw an exception.</p>
2574 *
2575 * <p>If the device has a hotspot provisioning app, the caller is required to hold the
2576 * {@link android.Manifest.permission.TETHER_PRIVILEGED} permission.</p>
2577 *
2578 * <p>Otherwise, this method requires the caller to hold the ability to modify system
2579 * settings as determined by {@link android.provider.Settings.System#canWrite}.</p>
2580 *
Robert Greenwaltfaa4b402013-02-15 10:56:35 -08002581 * @return a boolean - {@code true} indicating Tethering is supported.
2582 *
markchien6ae63e52020-01-21 13:11:06 +08002583 * @deprecated Use {@link TetheringEventCallback#onTetheringSupported(boolean)} instead.
Robert Greenwalt8e87f122010-02-11 18:18:40 -08002584 * {@hide}
2585 */
Jeremy Klein3dabcb92016-01-22 14:11:45 -08002586 @SystemApi
Chalard Jeanffacaef2017-09-26 15:45:18 +09002587 @RequiresPermission(anyOf = {android.Manifest.permission.TETHER_PRIVILEGED,
2588 android.Manifest.permission.WRITE_SETTINGS})
Robert Greenwalt8e87f122010-02-11 18:18:40 -08002589 public boolean isTetheringSupported() {
Amos Bianchia9b415a2020-03-04 11:07:38 -08002590 return mTetheringManager.isTetheringSupported();
Robert Greenwalt8e87f122010-02-11 18:18:40 -08002591 }
2592
2593 /**
Jeremy Klein3dabcb92016-01-22 14:11:45 -08002594 * Callback for use with {@link #startTethering} to find out whether tethering succeeded.
markchien91c78e52020-01-20 19:31:56 +08002595 *
2596 * @deprecated Use {@link TetheringManager.StartTetheringCallback} instead.
Jeremy Klein3dabcb92016-01-22 14:11:45 -08002597 * @hide
2598 */
2599 @SystemApi
markchien91c78e52020-01-20 19:31:56 +08002600 @Deprecated
Jeremy Klein3dabcb92016-01-22 14:11:45 -08002601 public static abstract class OnStartTetheringCallback {
2602 /**
2603 * Called when tethering has been successfully started.
2604 */
Chalard Jean9dd11612018-06-04 16:52:49 +09002605 public void onTetheringStarted() {}
Jeremy Klein3dabcb92016-01-22 14:11:45 -08002606
2607 /**
2608 * Called when starting tethering failed.
2609 */
Chalard Jean9dd11612018-06-04 16:52:49 +09002610 public void onTetheringFailed() {}
Jeremy Klein3dabcb92016-01-22 14:11:45 -08002611 }
2612
2613 /**
2614 * Convenient overload for
2615 * {@link #startTethering(int, boolean, OnStartTetheringCallback, Handler)} which passes a null
2616 * handler to run on the current thread's {@link Looper}.
markchien91c78e52020-01-20 19:31:56 +08002617 *
2618 * @deprecated Use {@link TetheringManager#startTethering} instead.
Jeremy Klein3dabcb92016-01-22 14:11:45 -08002619 * @hide
2620 */
2621 @SystemApi
markchien91c78e52020-01-20 19:31:56 +08002622 @Deprecated
Udam Saini8f7d6a72017-06-07 12:06:28 -07002623 @RequiresPermission(android.Manifest.permission.TETHER_PRIVILEGED)
Jeremy Klein3dabcb92016-01-22 14:11:45 -08002624 public void startTethering(int type, boolean showProvisioningUi,
2625 final OnStartTetheringCallback callback) {
2626 startTethering(type, showProvisioningUi, callback, null);
2627 }
2628
2629 /**
2630 * Runs tether provisioning for the given type if needed and then starts tethering if
2631 * the check succeeds. If no carrier provisioning is required for tethering, tethering is
2632 * enabled immediately. If provisioning fails, tethering will not be enabled. It also
2633 * schedules tether provisioning re-checks if appropriate.
2634 *
2635 * @param type The type of tethering to start. Must be one of
2636 * {@link ConnectivityManager.TETHERING_WIFI},
2637 * {@link ConnectivityManager.TETHERING_USB}, or
2638 * {@link ConnectivityManager.TETHERING_BLUETOOTH}.
2639 * @param showProvisioningUi a boolean indicating to show the provisioning app UI if there
2640 * is one. This should be true the first time this function is called and also any time
2641 * the user can see this UI. It gives users information from their carrier about the
2642 * check failing and how they can sign up for tethering if possible.
2643 * @param callback an {@link OnStartTetheringCallback} which will be called to notify the caller
2644 * of the result of trying to tether.
2645 * @param handler {@link Handler} to specify the thread upon which the callback will be invoked.
markchien91c78e52020-01-20 19:31:56 +08002646 *
2647 * @deprecated Use {@link TetheringManager#startTethering} instead.
Jeremy Klein3dabcb92016-01-22 14:11:45 -08002648 * @hide
2649 */
2650 @SystemApi
markchien91c78e52020-01-20 19:31:56 +08002651 @Deprecated
Jeff Sharkey9b39e9a2017-06-02 17:36:26 -06002652 @RequiresPermission(android.Manifest.permission.TETHER_PRIVILEGED)
Jeremy Klein3dabcb92016-01-22 14:11:45 -08002653 public void startTethering(int type, boolean showProvisioningUi,
2654 final OnStartTetheringCallback callback, Handler handler) {
Remi NGUYEN VAN81f68422021-03-15 07:31:54 +00002655 Objects.requireNonNull(callback, "OnStartTetheringCallback cannot be null.");
Jeremy Klein35e99ea2016-03-12 16:29:54 -08002656
markchien91c78e52020-01-20 19:31:56 +08002657 final Executor executor = new Executor() {
Jeremy Klein3dabcb92016-01-22 14:11:45 -08002658 @Override
markchien91c78e52020-01-20 19:31:56 +08002659 public void execute(Runnable command) {
2660 if (handler == null) {
2661 command.run();
Jeremy Klein3dabcb92016-01-22 14:11:45 -08002662 } else {
markchien91c78e52020-01-20 19:31:56 +08002663 handler.post(command);
Jeremy Klein3dabcb92016-01-22 14:11:45 -08002664 }
2665 }
2666 };
Jeremy Klein35e99ea2016-03-12 16:29:54 -08002667
markchien91c78e52020-01-20 19:31:56 +08002668 final StartTetheringCallback tetheringCallback = new StartTetheringCallback() {
2669 @Override
2670 public void onTetheringStarted() {
2671 callback.onTetheringStarted();
2672 }
2673
2674 @Override
markchienf47d8342020-03-19 13:37:43 +08002675 public void onTetheringFailed(final int error) {
markchien91c78e52020-01-20 19:31:56 +08002676 callback.onTetheringFailed();
2677 }
2678 };
2679
2680 final TetheringRequest request = new TetheringRequest.Builder(type)
markchienf47d8342020-03-19 13:37:43 +08002681 .setShouldShowEntitlementUi(showProvisioningUi).build();
markchien91c78e52020-01-20 19:31:56 +08002682
Amos Bianchia9b415a2020-03-04 11:07:38 -08002683 mTetheringManager.startTethering(request, executor, tetheringCallback);
Jeremy Klein3dabcb92016-01-22 14:11:45 -08002684 }
2685
2686 /**
2687 * Stops tethering for the given type. Also cancels any provisioning rechecks for that type if
2688 * applicable.
2689 *
2690 * @param type The type of tethering to stop. Must be one of
2691 * {@link ConnectivityManager.TETHERING_WIFI},
2692 * {@link ConnectivityManager.TETHERING_USB}, or
2693 * {@link ConnectivityManager.TETHERING_BLUETOOTH}.
markchien6ae63e52020-01-21 13:11:06 +08002694 *
2695 * @deprecated Use {@link TetheringManager#stopTethering} instead.
Jeremy Klein3dabcb92016-01-22 14:11:45 -08002696 * @hide
2697 */
2698 @SystemApi
markchien6ae63e52020-01-21 13:11:06 +08002699 @Deprecated
Jeff Sharkey9b39e9a2017-06-02 17:36:26 -06002700 @RequiresPermission(android.Manifest.permission.TETHER_PRIVILEGED)
Jeremy Klein3dabcb92016-01-22 14:11:45 -08002701 public void stopTethering(int type) {
Amos Bianchia9b415a2020-03-04 11:07:38 -08002702 mTetheringManager.stopTethering(type);
Jeremy Klein3dabcb92016-01-22 14:11:45 -08002703 }
2704
2705 /**
markchien4ef53e82019-02-27 14:56:11 +08002706 * Callback for use with {@link registerTetheringEventCallback} to find out tethering
2707 * upstream status.
2708 *
Nathan Harold74f0fb82020-01-23 18:03:46 -08002709 * @deprecated Use {@link TetheringManager#OnTetheringEventCallback} instead.
markchien6ae63e52020-01-21 13:11:06 +08002710 * @hide
markchien4ef53e82019-02-27 14:56:11 +08002711 */
2712 @SystemApi
markchien6ae63e52020-01-21 13:11:06 +08002713 @Deprecated
markchien4ef53e82019-02-27 14:56:11 +08002714 public abstract static class OnTetheringEventCallback {
2715
2716 /**
2717 * Called when tethering upstream changed. This can be called multiple times and can be
2718 * called any time.
2719 *
2720 * @param network the {@link Network} of tethering upstream. Null means tethering doesn't
2721 * have any upstream.
2722 */
2723 public void onUpstreamChanged(@Nullable Network network) {}
2724 }
2725
markchien6ae63e52020-01-21 13:11:06 +08002726 @GuardedBy("mTetheringEventCallbacks")
2727 private final ArrayMap<OnTetheringEventCallback, TetheringEventCallback>
2728 mTetheringEventCallbacks = new ArrayMap<>();
2729
markchien4ef53e82019-02-27 14:56:11 +08002730 /**
2731 * Start listening to tethering change events. Any new added callback will receive the last
markchien42e22092019-04-03 10:43:09 +08002732 * tethering status right away. If callback is registered when tethering has no upstream or
markchien4ef53e82019-02-27 14:56:11 +08002733 * disabled, {@link OnTetheringEventCallback#onUpstreamChanged} will immediately be called
2734 * with a null argument. The same callback object cannot be registered twice.
2735 *
2736 * @param executor the executor on which callback will be invoked.
2737 * @param callback the callback to be called when tethering has change events.
markchien6ae63e52020-01-21 13:11:06 +08002738 *
Nathan Harold74f0fb82020-01-23 18:03:46 -08002739 * @deprecated Use {@link TetheringManager#registerTetheringEventCallback} instead.
markchien4ef53e82019-02-27 14:56:11 +08002740 * @hide
2741 */
2742 @SystemApi
markchien6ae63e52020-01-21 13:11:06 +08002743 @Deprecated
markchien4ef53e82019-02-27 14:56:11 +08002744 @RequiresPermission(android.Manifest.permission.TETHER_PRIVILEGED)
2745 public void registerTetheringEventCallback(
2746 @NonNull @CallbackExecutor Executor executor,
2747 @NonNull final OnTetheringEventCallback callback) {
Remi NGUYEN VAN81f68422021-03-15 07:31:54 +00002748 Objects.requireNonNull(callback, "OnTetheringEventCallback cannot be null.");
markchien4ef53e82019-02-27 14:56:11 +08002749
markchien6ae63e52020-01-21 13:11:06 +08002750 final TetheringEventCallback tetherCallback =
2751 new TetheringEventCallback() {
2752 @Override
2753 public void onUpstreamChanged(@Nullable Network network) {
2754 callback.onUpstreamChanged(network);
2755 }
2756 };
2757
2758 synchronized (mTetheringEventCallbacks) {
2759 mTetheringEventCallbacks.put(callback, tetherCallback);
Amos Bianchia9b415a2020-03-04 11:07:38 -08002760 mTetheringManager.registerTetheringEventCallback(executor, tetherCallback);
markchien6ae63e52020-01-21 13:11:06 +08002761 }
markchien4ef53e82019-02-27 14:56:11 +08002762 }
2763
2764 /**
2765 * Remove tethering event callback previously registered with
2766 * {@link #registerTetheringEventCallback}.
2767 *
2768 * @param callback previously registered callback.
markchien6ae63e52020-01-21 13:11:06 +08002769 *
2770 * @deprecated Use {@link TetheringManager#unregisterTetheringEventCallback} instead.
markchien4ef53e82019-02-27 14:56:11 +08002771 * @hide
2772 */
2773 @SystemApi
markchien6ae63e52020-01-21 13:11:06 +08002774 @Deprecated
markchien4ef53e82019-02-27 14:56:11 +08002775 @RequiresPermission(android.Manifest.permission.TETHER_PRIVILEGED)
2776 public void unregisterTetheringEventCallback(
2777 @NonNull final OnTetheringEventCallback callback) {
markchien6ae63e52020-01-21 13:11:06 +08002778 Objects.requireNonNull(callback, "The callback must be non-null");
2779 synchronized (mTetheringEventCallbacks) {
2780 final TetheringEventCallback tetherCallback =
2781 mTetheringEventCallbacks.remove(callback);
Amos Bianchia9b415a2020-03-04 11:07:38 -08002782 mTetheringManager.unregisterTetheringEventCallback(tetherCallback);
markchien6ae63e52020-01-21 13:11:06 +08002783 }
markchien4ef53e82019-02-27 14:56:11 +08002784 }
2785
2786
2787 /**
Robert Greenwaltfaa4b402013-02-15 10:56:35 -08002788 * Get the list of regular expressions that define any tetherable
2789 * USB network interfaces. If USB tethering is not supported by the
2790 * device, this list should be empty.
2791 *
2792 * @return an array of 0 or more regular expression Strings defining
2793 * what interfaces are considered tetherable usb interfaces.
2794 *
markchien6ae63e52020-01-21 13:11:06 +08002795 * @deprecated Use {@link TetheringEventCallback#onTetherableInterfaceRegexpsChanged} instead.
Robert Greenwalt8e87f122010-02-11 18:18:40 -08002796 * {@hide}
2797 */
Jeff Sharkey656584a2017-04-24 11:18:03 -06002798 @RequiresPermission(android.Manifest.permission.ACCESS_NETWORK_STATE)
Mathew Inwoodc5b9bec2018-08-08 14:52:47 +01002799 @UnsupportedAppUsage
markchien6ae63e52020-01-21 13:11:06 +08002800 @Deprecated
Robert Greenwalt8e87f122010-02-11 18:18:40 -08002801 public String[] getTetherableUsbRegexs() {
Amos Bianchia9b415a2020-03-04 11:07:38 -08002802 return mTetheringManager.getTetherableUsbRegexs();
Robert Greenwalt8e87f122010-02-11 18:18:40 -08002803 }
2804
2805 /**
Robert Greenwaltfaa4b402013-02-15 10:56:35 -08002806 * Get the list of regular expressions that define any tetherable
2807 * Wifi network interfaces. If Wifi tethering is not supported by the
2808 * device, this list should be empty.
2809 *
2810 * @return an array of 0 or more regular expression Strings defining
2811 * what interfaces are considered tetherable wifi interfaces.
2812 *
markchien6ae63e52020-01-21 13:11:06 +08002813 * @deprecated Use {@link TetheringEventCallback#onTetherableInterfaceRegexpsChanged} instead.
Robert Greenwalt8e87f122010-02-11 18:18:40 -08002814 * {@hide}
2815 */
Jeff Sharkey656584a2017-04-24 11:18:03 -06002816 @RequiresPermission(android.Manifest.permission.ACCESS_NETWORK_STATE)
Mathew Inwoodc5b9bec2018-08-08 14:52:47 +01002817 @UnsupportedAppUsage
markchien6ae63e52020-01-21 13:11:06 +08002818 @Deprecated
Robert Greenwalt8e87f122010-02-11 18:18:40 -08002819 public String[] getTetherableWifiRegexs() {
Amos Bianchia9b415a2020-03-04 11:07:38 -08002820 return mTetheringManager.getTetherableWifiRegexs();
Robert Greenwalt8e87f122010-02-11 18:18:40 -08002821 }
Robert Greenwalt4283ded2010-03-02 17:25:02 -08002822
Danica Chang96567052010-08-11 14:54:43 -07002823 /**
Robert Greenwaltfaa4b402013-02-15 10:56:35 -08002824 * Get the list of regular expressions that define any tetherable
2825 * Bluetooth network interfaces. If Bluetooth tethering is not supported by the
2826 * device, this list should be empty.
2827 *
2828 * @return an array of 0 or more regular expression Strings defining
2829 * what interfaces are considered tetherable bluetooth interfaces.
2830 *
markchien6ae63e52020-01-21 13:11:06 +08002831 * @deprecated Use {@link TetheringEventCallback#onTetherableInterfaceRegexpsChanged(
2832 *TetheringManager.TetheringInterfaceRegexps)} instead.
Danica Chang96567052010-08-11 14:54:43 -07002833 * {@hide}
2834 */
Jeff Sharkey656584a2017-04-24 11:18:03 -06002835 @RequiresPermission(android.Manifest.permission.ACCESS_NETWORK_STATE)
Mathew Inwoodc5b9bec2018-08-08 14:52:47 +01002836 @UnsupportedAppUsage
markchien6ae63e52020-01-21 13:11:06 +08002837 @Deprecated
Danica Chang96567052010-08-11 14:54:43 -07002838 public String[] getTetherableBluetoothRegexs() {
Amos Bianchia9b415a2020-03-04 11:07:38 -08002839 return mTetheringManager.getTetherableBluetoothRegexs();
Danica Chang96567052010-08-11 14:54:43 -07002840 }
2841
Mike Lockwooded4a1742011-07-19 13:04:47 -07002842 /**
Robert Greenwaltfaa4b402013-02-15 10:56:35 -08002843 * Attempt to both alter the mode of USB and Tethering of USB. A
2844 * utility method to deal with some of the complexity of USB - will
2845 * attempt to switch to Rndis and subsequently tether the resulting
2846 * interface on {@code true} or turn off tethering and switch off
2847 * Rndis on {@code false}.
Lorenzo Colitti0b40c872015-10-15 16:29:00 +09002848 *
2849 * <p>This method requires the caller to hold either the
2850 * {@link android.Manifest.permission#CHANGE_NETWORK_STATE} permission
2851 * or the ability to modify system settings as determined by
2852 * {@link android.provider.Settings.System#canWrite}.</p>
Robert Greenwaltfaa4b402013-02-15 10:56:35 -08002853 *
2854 * @param enable a boolean - {@code true} to enable tethering
2855 * @return error a {@code TETHER_ERROR} value indicating success or failure type
markchien91c78e52020-01-20 19:31:56 +08002856 * @deprecated Use {@link TetheringManager#startTethering} instead
Robert Greenwaltfaa4b402013-02-15 10:56:35 -08002857 *
Mike Lockwooded4a1742011-07-19 13:04:47 -07002858 * {@hide}
2859 */
Mathew Inwoodc5b9bec2018-08-08 14:52:47 +01002860 @UnsupportedAppUsage
markchien91c78e52020-01-20 19:31:56 +08002861 @Deprecated
Mike Lockwooded4a1742011-07-19 13:04:47 -07002862 public int setUsbTethering(boolean enable) {
Amos Bianchia9b415a2020-03-04 11:07:38 -08002863 return mTetheringManager.setUsbTethering(enable);
Mike Lockwooded4a1742011-07-19 13:04:47 -07002864 }
2865
markchien6ae63e52020-01-21 13:11:06 +08002866 /**
2867 * @deprecated Use {@link TetheringManager#TETHER_ERROR_NO_ERROR}.
2868 * {@hide}
2869 */
markchien0f45bb92019-01-16 17:44:13 +08002870 @SystemApi
markchien6ae63e52020-01-21 13:11:06 +08002871 @Deprecated
Remi NGUYEN VAN09f8ed22021-02-15 18:52:06 +09002872 public static final int TETHER_ERROR_NO_ERROR = 0;
markchien6ae63e52020-01-21 13:11:06 +08002873 /**
2874 * @deprecated Use {@link TetheringManager#TETHER_ERROR_UNKNOWN_IFACE}.
2875 * {@hide}
2876 */
2877 @Deprecated
2878 public static final int TETHER_ERROR_UNKNOWN_IFACE =
2879 TetheringManager.TETHER_ERROR_UNKNOWN_IFACE;
2880 /**
2881 * @deprecated Use {@link TetheringManager#TETHER_ERROR_SERVICE_UNAVAIL}.
2882 * {@hide}
2883 */
2884 @Deprecated
2885 public static final int TETHER_ERROR_SERVICE_UNAVAIL =
2886 TetheringManager.TETHER_ERROR_SERVICE_UNAVAIL;
2887 /**
2888 * @deprecated Use {@link TetheringManager#TETHER_ERROR_UNSUPPORTED}.
2889 * {@hide}
2890 */
2891 @Deprecated
2892 public static final int TETHER_ERROR_UNSUPPORTED = TetheringManager.TETHER_ERROR_UNSUPPORTED;
2893 /**
2894 * @deprecated Use {@link TetheringManager#TETHER_ERROR_UNAVAIL_IFACE}.
2895 * {@hide}
2896 */
2897 @Deprecated
2898 public static final int TETHER_ERROR_UNAVAIL_IFACE =
2899 TetheringManager.TETHER_ERROR_UNAVAIL_IFACE;
2900 /**
markchienf47d8342020-03-19 13:37:43 +08002901 * @deprecated Use {@link TetheringManager#TETHER_ERROR_INTERNAL_ERROR}.
markchien6ae63e52020-01-21 13:11:06 +08002902 * {@hide}
2903 */
2904 @Deprecated
markchienf47d8342020-03-19 13:37:43 +08002905 public static final int TETHER_ERROR_MASTER_ERROR =
2906 TetheringManager.TETHER_ERROR_INTERNAL_ERROR;
markchien6ae63e52020-01-21 13:11:06 +08002907 /**
2908 * @deprecated Use {@link TetheringManager#TETHER_ERROR_TETHER_IFACE_ERROR}.
2909 * {@hide}
2910 */
2911 @Deprecated
2912 public static final int TETHER_ERROR_TETHER_IFACE_ERROR =
2913 TetheringManager.TETHER_ERROR_TETHER_IFACE_ERROR;
2914 /**
2915 * @deprecated Use {@link TetheringManager#TETHER_ERROR_UNTETHER_IFACE_ERROR}.
2916 * {@hide}
2917 */
2918 @Deprecated
2919 public static final int TETHER_ERROR_UNTETHER_IFACE_ERROR =
2920 TetheringManager.TETHER_ERROR_UNTETHER_IFACE_ERROR;
2921 /**
markchienf47d8342020-03-19 13:37:43 +08002922 * @deprecated Use {@link TetheringManager#TETHER_ERROR_ENABLE_FORWARDING_ERROR}.
markchien6ae63e52020-01-21 13:11:06 +08002923 * {@hide}
2924 */
2925 @Deprecated
2926 public static final int TETHER_ERROR_ENABLE_NAT_ERROR =
markchienf47d8342020-03-19 13:37:43 +08002927 TetheringManager.TETHER_ERROR_ENABLE_FORWARDING_ERROR;
markchien6ae63e52020-01-21 13:11:06 +08002928 /**
markchienf47d8342020-03-19 13:37:43 +08002929 * @deprecated Use {@link TetheringManager#TETHER_ERROR_DISABLE_FORWARDING_ERROR}.
markchien6ae63e52020-01-21 13:11:06 +08002930 * {@hide}
2931 */
2932 @Deprecated
2933 public static final int TETHER_ERROR_DISABLE_NAT_ERROR =
markchienf47d8342020-03-19 13:37:43 +08002934 TetheringManager.TETHER_ERROR_DISABLE_FORWARDING_ERROR;
markchien6ae63e52020-01-21 13:11:06 +08002935 /**
2936 * @deprecated Use {@link TetheringManager#TETHER_ERROR_IFACE_CFG_ERROR}.
2937 * {@hide}
2938 */
2939 @Deprecated
2940 public static final int TETHER_ERROR_IFACE_CFG_ERROR =
2941 TetheringManager.TETHER_ERROR_IFACE_CFG_ERROR;
2942 /**
markchienf47d8342020-03-19 13:37:43 +08002943 * @deprecated Use {@link TetheringManager#TETHER_ERROR_PROVISIONING_FAILED}.
markchien6ae63e52020-01-21 13:11:06 +08002944 * {@hide}
2945 */
markchien0f45bb92019-01-16 17:44:13 +08002946 @SystemApi
markchien6ae63e52020-01-21 13:11:06 +08002947 @Deprecated
Remi NGUYEN VAN09f8ed22021-02-15 18:52:06 +09002948 public static final int TETHER_ERROR_PROVISION_FAILED = 11;
markchien6ae63e52020-01-21 13:11:06 +08002949 /**
2950 * @deprecated Use {@link TetheringManager#TETHER_ERROR_DHCPSERVER_ERROR}.
2951 * {@hide}
2952 */
2953 @Deprecated
2954 public static final int TETHER_ERROR_DHCPSERVER_ERROR =
2955 TetheringManager.TETHER_ERROR_DHCPSERVER_ERROR;
2956 /**
2957 * @deprecated Use {@link TetheringManager#TETHER_ERROR_ENTITLEMENT_UNKNOWN}.
2958 * {@hide}
2959 */
markchien0f45bb92019-01-16 17:44:13 +08002960 @SystemApi
markchien6ae63e52020-01-21 13:11:06 +08002961 @Deprecated
Remi NGUYEN VAN09f8ed22021-02-15 18:52:06 +09002962 public static final int TETHER_ERROR_ENTITLEMENT_UNKONWN = 13;
Robert Greenwalt4283ded2010-03-02 17:25:02 -08002963
2964 /**
Robert Greenwaltfaa4b402013-02-15 10:56:35 -08002965 * Get a more detailed error code after a Tethering or Untethering
2966 * request asynchronously failed.
2967 *
2968 * @param iface The name of the interface of interest
Robert Greenwalt4283ded2010-03-02 17:25:02 -08002969 * @return error The error code of the last error tethering or untethering the named
2970 * interface
Robert Greenwaltfaa4b402013-02-15 10:56:35 -08002971 *
markchien6ae63e52020-01-21 13:11:06 +08002972 * @deprecated Use {@link TetheringEventCallback#onError(String, int)} instead.
Robert Greenwalt4283ded2010-03-02 17:25:02 -08002973 * {@hide}
2974 */
Jeff Sharkey656584a2017-04-24 11:18:03 -06002975 @RequiresPermission(android.Manifest.permission.ACCESS_NETWORK_STATE)
Mathew Inwoode1a17ba2020-11-04 09:29:36 +00002976 @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.R, trackingBug = 170729553)
markchien6ae63e52020-01-21 13:11:06 +08002977 @Deprecated
Robert Greenwalt4283ded2010-03-02 17:25:02 -08002978 public int getLastTetherError(String iface) {
markchienf47d8342020-03-19 13:37:43 +08002979 int error = mTetheringManager.getLastTetherError(iface);
2980 if (error == TetheringManager.TETHER_ERROR_UNKNOWN_TYPE) {
2981 // TETHER_ERROR_UNKNOWN_TYPE was introduced with TetheringManager and has never been
2982 // returned by ConnectivityManager. Convert it to the legacy TETHER_ERROR_UNKNOWN_IFACE
2983 // instead.
2984 error = TetheringManager.TETHER_ERROR_UNKNOWN_IFACE;
2985 }
2986 return error;
Robert Greenwalt93dc1042010-06-15 12:19:37 -07002987 }
2988
markchienbf5ab012019-03-06 16:25:00 +08002989 /** @hide */
2990 @Retention(RetentionPolicy.SOURCE)
2991 @IntDef(value = {
2992 TETHER_ERROR_NO_ERROR,
2993 TETHER_ERROR_PROVISION_FAILED,
2994 TETHER_ERROR_ENTITLEMENT_UNKONWN,
2995 })
2996 public @interface EntitlementResultCode {
2997 }
2998
Robert Greenwalt93dc1042010-06-15 12:19:37 -07002999 /**
markchienbf5ab012019-03-06 16:25:00 +08003000 * Callback for use with {@link #getLatestTetheringEntitlementResult} to find out whether
markchien0f45bb92019-01-16 17:44:13 +08003001 * entitlement succeeded.
markchien6ae63e52020-01-21 13:11:06 +08003002 *
3003 * @deprecated Use {@link TetheringManager#OnTetheringEntitlementResultListener} instead.
markchien0f45bb92019-01-16 17:44:13 +08003004 * @hide
3005 */
3006 @SystemApi
markchien6ae63e52020-01-21 13:11:06 +08003007 @Deprecated
markchienbf5ab012019-03-06 16:25:00 +08003008 public interface OnTetheringEntitlementResultListener {
3009 /**
3010 * Called to notify entitlement result.
3011 *
3012 * @param resultCode an int value of entitlement result. It may be one of
3013 * {@link #TETHER_ERROR_NO_ERROR},
3014 * {@link #TETHER_ERROR_PROVISION_FAILED}, or
3015 * {@link #TETHER_ERROR_ENTITLEMENT_UNKONWN}.
3016 */
Jeremy Klein7e7c7422019-03-12 13:32:08 -07003017 void onTetheringEntitlementResult(@EntitlementResultCode int resultCode);
markchienbf5ab012019-03-06 16:25:00 +08003018 }
3019
3020 /**
markchien0f45bb92019-01-16 17:44:13 +08003021 * Get the last value of the entitlement check on this downstream. If the cached value is
3022 * {@link #TETHER_ERROR_NO_ERROR} or showEntitlementUi argument is false, it just return the
3023 * cached value. Otherwise, a UI-based entitlement check would be performed. It is not
3024 * guaranteed that the UI-based entitlement check will complete in any specific time period
3025 * and may in fact never complete. Any successful entitlement check the platform performs for
3026 * any reason will update the cached value.
3027 *
3028 * @param type the downstream type of tethering. Must be one of
3029 * {@link #TETHERING_WIFI},
3030 * {@link #TETHERING_USB}, or
3031 * {@link #TETHERING_BLUETOOTH}.
3032 * @param showEntitlementUi a boolean indicating whether to run UI-based entitlement check.
markchienbf5ab012019-03-06 16:25:00 +08003033 * @param executor the executor on which callback will be invoked.
3034 * @param listener an {@link OnTetheringEntitlementResultListener} which will be called to
3035 * notify the caller of the result of entitlement check. The listener may be called zero
3036 * or one time.
markchien6ae63e52020-01-21 13:11:06 +08003037 * @deprecated Use {@link TetheringManager#requestLatestTetheringEntitlementResult} instead.
markchien0f45bb92019-01-16 17:44:13 +08003038 * {@hide}
3039 */
3040 @SystemApi
markchien6ae63e52020-01-21 13:11:06 +08003041 @Deprecated
markchien0f45bb92019-01-16 17:44:13 +08003042 @RequiresPermission(android.Manifest.permission.TETHER_PRIVILEGED)
markchienbf5ab012019-03-06 16:25:00 +08003043 public void getLatestTetheringEntitlementResult(int type, boolean showEntitlementUi,
3044 @NonNull @CallbackExecutor Executor executor,
3045 @NonNull final OnTetheringEntitlementResultListener listener) {
Remi NGUYEN VAN81f68422021-03-15 07:31:54 +00003046 Objects.requireNonNull(listener, "TetheringEntitlementResultListener cannot be null.");
markchienbf5ab012019-03-06 16:25:00 +08003047 ResultReceiver wrappedListener = new ResultReceiver(null) {
3048 @Override
3049 protected void onReceiveResult(int resultCode, Bundle resultData) {
lucaslinc6170bb2021-03-04 09:38:21 +08003050 final long token = Binder.clearCallingIdentity();
3051 try {
3052 executor.execute(() -> {
3053 listener.onTetheringEntitlementResult(resultCode);
3054 });
3055 } finally {
3056 Binder.restoreCallingIdentity(token);
3057 }
markchienbf5ab012019-03-06 16:25:00 +08003058 }
3059 };
3060
Amos Bianchia9b415a2020-03-04 11:07:38 -08003061 mTetheringManager.requestLatestTetheringEntitlementResult(type, wrappedListener,
markchien5776f962019-12-16 20:15:20 +08003062 showEntitlementUi);
markchienbf5ab012019-03-06 16:25:00 +08003063 }
3064
3065 /**
Robert Greenwaltfaa4b402013-02-15 10:56:35 -08003066 * Report network connectivity status. This is currently used only
3067 * to alter status bar UI.
Paul Jensen08f9dbb2015-05-06 11:10:18 -04003068 * <p>This method requires the caller to hold the permission
3069 * {@link android.Manifest.permission#STATUS_BAR}.
Robert Greenwaltfaa4b402013-02-15 10:56:35 -08003070 *
Robert Greenwalt986c7412010-09-08 15:24:47 -07003071 * @param networkType The type of network you want to report on
3072 * @param percentage The quality of the connection 0 is bad, 100 is good
Chalard Jean7eaf3b12018-03-08 13:54:53 +09003073 * @deprecated Types are deprecated. Use {@link #reportNetworkConnectivity} instead.
Robert Greenwalt986c7412010-09-08 15:24:47 -07003074 * {@hide}
3075 */
3076 public void reportInetCondition(int networkType, int percentage) {
Soi, Yoshinarib8102ef2015-11-12 12:09:02 +09003077 printStackTrace();
Robert Greenwalt986c7412010-09-08 15:24:47 -07003078 try {
3079 mService.reportInetCondition(networkType, percentage);
3080 } catch (RemoteException e) {
Jeff Sharkeyc78f85c2016-03-01 19:27:23 -07003081 throw e.rethrowFromSystemServer();
Robert Greenwalt986c7412010-09-08 15:24:47 -07003082 }
3083 }
Robert Greenwaltc3c5f862010-10-11 16:00:27 -07003084
3085 /**
Robert Greenwaltf3017f72014-05-18 23:07:25 -07003086 * Report a problem network to the framework. This provides a hint to the system
Ye Wenca7abab2014-07-21 14:19:01 -07003087 * that there might be connectivity problems on this network and may cause
Robert Greenwaltf3017f72014-05-18 23:07:25 -07003088 * the framework to re-evaluate network connectivity and/or switch to another
3089 * network.
Robert Greenwaltf99b8392014-03-26 16:47:06 -07003090 *
Robert Greenwaltf3017f72014-05-18 23:07:25 -07003091 * @param network The {@link Network} the application was attempting to use
3092 * or {@code null} to indicate the current default network.
Paul Jensenb95d7952015-04-07 12:43:13 -04003093 * @deprecated Use {@link #reportNetworkConnectivity} which allows reporting both
3094 * working and non-working connectivity.
Robert Greenwaltf99b8392014-03-26 16:47:06 -07003095 */
Aurimas Liutikase80a0eb2016-05-24 15:22:55 -07003096 @Deprecated
Chalard Jean158702d2019-01-07 19:26:34 +09003097 public void reportBadNetwork(@Nullable Network network) {
Soi, Yoshinarib8102ef2015-11-12 12:09:02 +09003098 printStackTrace();
Robert Greenwaltf99b8392014-03-26 16:47:06 -07003099 try {
Paul Jensenb95d7952015-04-07 12:43:13 -04003100 // One of these will be ignored because it matches system's current state.
3101 // The other will trigger the necessary reevaluation.
3102 mService.reportNetworkConnectivity(network, true);
3103 mService.reportNetworkConnectivity(network, false);
3104 } catch (RemoteException e) {
Jeff Sharkeyc78f85c2016-03-01 19:27:23 -07003105 throw e.rethrowFromSystemServer();
Paul Jensenb95d7952015-04-07 12:43:13 -04003106 }
3107 }
3108
3109 /**
3110 * Report to the framework whether a network has working connectivity.
3111 * This provides a hint to the system that a particular network is providing
3112 * working connectivity or not. In response the framework may re-evaluate
3113 * the network's connectivity and might take further action thereafter.
3114 *
3115 * @param network The {@link Network} the application was attempting to use
3116 * or {@code null} to indicate the current default network.
3117 * @param hasConnectivity {@code true} if the application was able to successfully access the
3118 * Internet using {@code network} or {@code false} if not.
3119 */
Chalard Jean158702d2019-01-07 19:26:34 +09003120 public void reportNetworkConnectivity(@Nullable Network network, boolean hasConnectivity) {
Soi, Yoshinarib8102ef2015-11-12 12:09:02 +09003121 printStackTrace();
Paul Jensenb95d7952015-04-07 12:43:13 -04003122 try {
3123 mService.reportNetworkConnectivity(network, hasConnectivity);
Robert Greenwaltf99b8392014-03-26 16:47:06 -07003124 } catch (RemoteException e) {
Jeff Sharkeyc78f85c2016-03-01 19:27:23 -07003125 throw e.rethrowFromSystemServer();
Robert Greenwaltf99b8392014-03-26 16:47:06 -07003126 }
3127 }
3128
3129 /**
Chalard Jean48d60ea2021-03-17 17:03:34 +09003130 * Set a network-independent global HTTP proxy.
Paul Jensen08f9dbb2015-05-06 11:10:18 -04003131 *
Chalard Jean48d60ea2021-03-17 17:03:34 +09003132 * This sets an HTTP proxy that applies to all networks and overrides any network-specific
3133 * proxy. If set, HTTP libraries that are proxy-aware will use this global proxy when
3134 * accessing any network, regardless of what the settings for that network are.
3135 *
3136 * Note that HTTP proxies are by nature typically network-dependent, and setting a global
3137 * proxy is likely to break networking on multiple networks. This method is only meant
3138 * for device policy clients looking to do general internal filtering or similar use cases.
3139 *
3140 * {@see #getGlobalProxy}
3141 * {@see LinkProperties#getHttpProxy}
3142 *
3143 * @param p A {@link ProxyInfo} object defining the new global HTTP proxy. Calling this
3144 * method with a {@code null} value will clear the global HTTP proxy.
Robert Greenwaltf3017f72014-05-18 23:07:25 -07003145 * @hide
Robert Greenwaltc3c5f862010-10-11 16:00:27 -07003146 */
Chalard Jean48d60ea2021-03-17 17:03:34 +09003147 // Used by Device Policy Manager to set the global proxy.
Chiachang Wang4d513572021-03-18 09:44:34 +08003148 @SystemApi(client = MODULE_LIBRARIES)
paulhu8e96a752019-08-12 16:25:11 +08003149 @RequiresPermission(android.Manifest.permission.NETWORK_STACK)
Chalard Jean48d60ea2021-03-17 17:03:34 +09003150 public void setGlobalProxy(@Nullable final ProxyInfo p) {
Robert Greenwaltc3c5f862010-10-11 16:00:27 -07003151 try {
3152 mService.setGlobalProxy(p);
3153 } catch (RemoteException e) {
Jeff Sharkeyc78f85c2016-03-01 19:27:23 -07003154 throw e.rethrowFromSystemServer();
Robert Greenwaltc3c5f862010-10-11 16:00:27 -07003155 }
3156 }
3157
3158 /**
Robert Greenwaltfaa4b402013-02-15 10:56:35 -08003159 * Retrieve any network-independent global HTTP proxy.
3160 *
Jason Monk4d5e20f2014-04-25 15:00:09 -04003161 * @return {@link ProxyInfo} for the current global HTTP proxy or {@code null}
Robert Greenwaltfaa4b402013-02-15 10:56:35 -08003162 * if no global HTTP proxy is set.
Robert Greenwaltf3017f72014-05-18 23:07:25 -07003163 * @hide
Robert Greenwaltc3c5f862010-10-11 16:00:27 -07003164 */
Chiachang Wang4d513572021-03-18 09:44:34 +08003165 @SystemApi(client = MODULE_LIBRARIES)
3166 @Nullable
Jason Monk4d5e20f2014-04-25 15:00:09 -04003167 public ProxyInfo getGlobalProxy() {
Robert Greenwaltc3c5f862010-10-11 16:00:27 -07003168 try {
3169 return mService.getGlobalProxy();
3170 } catch (RemoteException e) {
Jeff Sharkeyc78f85c2016-03-01 19:27:23 -07003171 throw e.rethrowFromSystemServer();
Robert Greenwaltc3c5f862010-10-11 16:00:27 -07003172 }
3173 }
3174
3175 /**
Paul Jensendb93dd92015-05-06 07:32:40 -04003176 * Retrieve the global HTTP proxy, or if no global HTTP proxy is set, a
3177 * network-specific HTTP proxy. If {@code network} is null, the
3178 * network-specific proxy returned is the proxy of the default active
3179 * network.
3180 *
3181 * @return {@link ProxyInfo} for the current global HTTP proxy, or if no
3182 * global HTTP proxy is set, {@code ProxyInfo} for {@code network},
3183 * or when {@code network} is {@code null},
3184 * the {@code ProxyInfo} for the default active network. Returns
3185 * {@code null} when no proxy applies or the caller doesn't have
3186 * permission to use {@code network}.
3187 * @hide
3188 */
3189 public ProxyInfo getProxyForNetwork(Network network) {
3190 try {
3191 return mService.getProxyForNetwork(network);
3192 } catch (RemoteException e) {
Jeff Sharkeyc78f85c2016-03-01 19:27:23 -07003193 throw e.rethrowFromSystemServer();
Paul Jensendb93dd92015-05-06 07:32:40 -04003194 }
3195 }
3196
3197 /**
Paul Jensenc0618a62014-12-10 15:12:18 -05003198 * Get the current default HTTP proxy settings. If a global proxy is set it will be returned,
3199 * otherwise if this process is bound to a {@link Network} using
Paul Jensenee2f45d2015-03-10 10:54:12 -04003200 * {@link #bindProcessToNetwork} then that {@code Network}'s proxy is returned, otherwise
Paul Jensenc0618a62014-12-10 15:12:18 -05003201 * the default network's proxy is returned.
Robert Greenwaltfaa4b402013-02-15 10:56:35 -08003202 *
Jason Monk4d5e20f2014-04-25 15:00:09 -04003203 * @return the {@link ProxyInfo} for the current HTTP proxy, or {@code null} if no
Robert Greenwaltfaa4b402013-02-15 10:56:35 -08003204 * HTTP proxy is active.
Robert Greenwaltc3c5f862010-10-11 16:00:27 -07003205 */
Chalard Jean158702d2019-01-07 19:26:34 +09003206 @Nullable
Paul Jensenc0618a62014-12-10 15:12:18 -05003207 public ProxyInfo getDefaultProxy() {
Paul Jensendb93dd92015-05-06 07:32:40 -04003208 return getProxyForNetwork(getBoundNetworkForProcess());
Robert Greenwaltc3c5f862010-10-11 16:00:27 -07003209 }
Robert Greenwalt34848c02011-03-25 13:09:25 -07003210
3211 /**
Robert Greenwalt0114f6e2011-08-31 11:46:42 -07003212 * Returns true if the hardware supports the given network type
3213 * else it returns false. This doesn't indicate we have coverage
3214 * or are authorized onto a network, just whether or not the
Robert Greenwaltfaa4b402013-02-15 10:56:35 -08003215 * hardware supports it. For example a GSM phone without a SIM
3216 * should still return {@code true} for mobile data, but a wifi only
3217 * tablet would return {@code false}.
3218 *
3219 * @param networkType The network type we'd like to check
3220 * @return {@code true} if supported, else {@code false}
Chalard Jean7eaf3b12018-03-08 13:54:53 +09003221 * @deprecated Types are deprecated. Use {@link NetworkCapabilities} instead.
Robert Greenwalt0114f6e2011-08-31 11:46:42 -07003222 * @hide
3223 */
Chalard Jean7eaf3b12018-03-08 13:54:53 +09003224 @Deprecated
Jeff Sharkey656584a2017-04-24 11:18:03 -06003225 @RequiresPermission(android.Manifest.permission.ACCESS_NETWORK_STATE)
Chalard Jeana3b77512019-04-09 15:46:21 +09003226 @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P, trackingBug = 130143562)
Robert Greenwalt0114f6e2011-08-31 11:46:42 -07003227 public boolean isNetworkSupported(int networkType) {
3228 try {
3229 return mService.isNetworkSupported(networkType);
Jeff Sharkeyc78f85c2016-03-01 19:27:23 -07003230 } catch (RemoteException e) {
3231 throw e.rethrowFromSystemServer();
3232 }
Robert Greenwalt0114f6e2011-08-31 11:46:42 -07003233 }
Jeff Sharkeyd00b1302012-04-12 18:34:54 -07003234
3235 /**
3236 * Returns if the currently active data network is metered. A network is
3237 * classified as metered when the user is sensitive to heavy data usage on
Robert Greenwaltfaa4b402013-02-15 10:56:35 -08003238 * that connection due to monetary costs, data limitations or
3239 * battery/performance issues. You should check this before doing large
3240 * data transfers, and warn the user or delay the operation until another
3241 * network is available.
3242 *
3243 * @return {@code true} if large transfers should be avoided, otherwise
3244 * {@code false}.
Jeff Sharkeyd00b1302012-04-12 18:34:54 -07003245 */
Jeff Sharkey656584a2017-04-24 11:18:03 -06003246 @RequiresPermission(android.Manifest.permission.ACCESS_NETWORK_STATE)
Jeff Sharkeyd00b1302012-04-12 18:34:54 -07003247 public boolean isActiveNetworkMetered() {
3248 try {
3249 return mService.isActiveNetworkMetered();
3250 } catch (RemoteException e) {
Jeff Sharkeyc78f85c2016-03-01 19:27:23 -07003251 throw e.rethrowFromSystemServer();
Jeff Sharkeyd00b1302012-04-12 18:34:54 -07003252 }
3253 }
Jeff Sharkeyebcc7972012-08-25 00:05:46 -07003254
Robert Greenwaltfaa4b402013-02-15 10:56:35 -08003255 /**
Sarah Chincabcbff2020-11-25 12:15:14 -08003256 * Set sign in error notification to visible or invisible
Wink Saville9a1a7ef2013-08-29 08:55:16 -07003257 *
Sarah Chincabcbff2020-11-25 12:15:14 -08003258 * @hide
Paul Jensendda8e592015-03-18 12:23:02 -04003259 * @deprecated Doesn't properly deal with multiple connected networks of the same type.
Wink Saville9a1a7ef2013-08-29 08:55:16 -07003260 */
Aurimas Liutikase80a0eb2016-05-24 15:22:55 -07003261 @Deprecated
Wink Saville9a1a7ef2013-08-29 08:55:16 -07003262 public void setProvisioningNotificationVisible(boolean visible, int networkType,
Paul Jensenebeaecd2014-09-15 15:59:36 -04003263 String action) {
Wink Saville9a1a7ef2013-08-29 08:55:16 -07003264 try {
Paul Jensenebeaecd2014-09-15 15:59:36 -04003265 mService.setProvisioningNotificationVisible(visible, networkType, action);
Wink Saville9a1a7ef2013-08-29 08:55:16 -07003266 } catch (RemoteException e) {
Jeff Sharkeyc78f85c2016-03-01 19:27:23 -07003267 throw e.rethrowFromSystemServer();
Wink Saville9a1a7ef2013-08-29 08:55:16 -07003268 }
3269 }
Yuhao Zhengb77f15d2013-09-09 17:00:04 -07003270
3271 /**
3272 * Set the value for enabling/disabling airplane mode
3273 *
3274 * @param enable whether to enable airplane mode or not
3275 *
Yuhao Zhengb77f15d2013-09-09 17:00:04 -07003276 * @hide
3277 */
Lorenzo Colittic7da00d2018-10-09 18:55:11 +09003278 @RequiresPermission(anyOf = {
Edward Savage-Jonesd4723692019-11-26 13:18:08 +01003279 android.Manifest.permission.NETWORK_AIRPLANE_MODE,
Lorenzo Colittic7da00d2018-10-09 18:55:11 +09003280 android.Manifest.permission.NETWORK_SETTINGS,
3281 android.Manifest.permission.NETWORK_SETUP_WIZARD,
3282 android.Manifest.permission.NETWORK_STACK})
Lorenzo Colitti0bfef022018-10-09 18:50:32 +09003283 @SystemApi
Yuhao Zhengb77f15d2013-09-09 17:00:04 -07003284 public void setAirplaneMode(boolean enable) {
3285 try {
3286 mService.setAirplaneMode(enable);
3287 } catch (RemoteException e) {
Jeff Sharkeyc78f85c2016-03-01 19:27:23 -07003288 throw e.rethrowFromSystemServer();
Yuhao Zhengb77f15d2013-09-09 17:00:04 -07003289 }
3290 }
Robert Greenwalt7e45d112014-04-11 15:53:27 -07003291
Lorenzo Colitti6702d4b2020-01-08 00:04:09 +09003292 /**
3293 * Registers the specified {@link NetworkProvider}.
3294 * Each listener must only be registered once. The listener can be unregistered with
3295 * {@link #unregisterNetworkProvider}.
3296 *
3297 * @param provider the provider to register
3298 * @return the ID of the provider. This ID must be used by the provider when registering
3299 * {@link android.net.NetworkAgent}s.
3300 * @hide
3301 */
3302 @SystemApi
paulhub6ba8e82020-03-04 09:43:41 +08003303 @RequiresPermission(anyOf = {
3304 NetworkStack.PERMISSION_MAINLINE_NETWORK_STACK,
3305 android.Manifest.permission.NETWORK_FACTORY})
Lorenzo Colitti6702d4b2020-01-08 00:04:09 +09003306 public int registerNetworkProvider(@NonNull NetworkProvider provider) {
3307 if (provider.getProviderId() != NetworkProvider.ID_NONE) {
Lorenzo Colitti6702d4b2020-01-08 00:04:09 +09003308 throw new IllegalStateException("NetworkProviders can only be registered once");
3309 }
3310
3311 try {
3312 int providerId = mService.registerNetworkProvider(provider.getMessenger(),
3313 provider.getName());
3314 provider.setProviderId(providerId);
3315 } catch (RemoteException e) {
3316 throw e.rethrowFromSystemServer();
3317 }
3318 return provider.getProviderId();
3319 }
3320
3321 /**
3322 * Unregisters the specified NetworkProvider.
3323 *
3324 * @param provider the provider to unregister
3325 * @hide
3326 */
3327 @SystemApi
paulhub6ba8e82020-03-04 09:43:41 +08003328 @RequiresPermission(anyOf = {
3329 NetworkStack.PERMISSION_MAINLINE_NETWORK_STACK,
3330 android.Manifest.permission.NETWORK_FACTORY})
Lorenzo Colitti6702d4b2020-01-08 00:04:09 +09003331 public void unregisterNetworkProvider(@NonNull NetworkProvider provider) {
3332 try {
3333 mService.unregisterNetworkProvider(provider.getMessenger());
3334 } catch (RemoteException e) {
3335 throw e.rethrowFromSystemServer();
3336 }
3337 provider.setProviderId(NetworkProvider.ID_NONE);
3338 }
3339
Chalard Jeancdd68bc2021-01-05 08:40:09 +09003340 /**
3341 * Register or update a network offer with ConnectivityService.
3342 *
3343 * ConnectivityService keeps track of offers made by the various providers and matches
3344 * them to networking requests made by apps or the system. The provider supplies a score
3345 * and the capabilities of the network it might be able to bring up ; these act as filters
3346 * used by ConnectivityService to only send those requests that can be fulfilled by the
3347 * provider.
3348 *
3349 * The provider is under no obligation to be able to bring up the network it offers at any
3350 * given time. Instead, this mechanism is meant to limit requests received by providers
3351 * to those they actually have a chance to fulfill, as providers don't have a way to compare
3352 * the quality of the network satisfying a given request to their own offer.
3353 *
3354 * An offer can be updated by calling this again with the same callback object. This is
3355 * similar to calling unofferNetwork and offerNetwork again, but will only update the
3356 * provider with the changes caused by the changes in the offer.
3357 *
3358 * @param provider The provider making this offer.
3359 * @param score The prospective score of the network.
3360 * @param caps The prospective capabilities of the network.
3361 * @param callback The callback to call when this offer is needed or unneeded.
3362 * @hide
3363 */
3364 @RequiresPermission(anyOf = {
3365 NetworkStack.PERMISSION_MAINLINE_NETWORK_STACK,
3366 android.Manifest.permission.NETWORK_FACTORY})
Chalard Jean30689b82021-03-22 22:44:02 +09003367 public void offerNetwork(@NonNull final int providerId,
Chalard Jeancdd68bc2021-01-05 08:40:09 +09003368 @NonNull final NetworkScore score, @NonNull final NetworkCapabilities caps,
3369 @NonNull final INetworkOfferCallback callback) {
3370 try {
Chalard Jean30689b82021-03-22 22:44:02 +09003371 mService.offerNetwork(providerId,
Chalard Jeancdd68bc2021-01-05 08:40:09 +09003372 Objects.requireNonNull(score, "null score"),
3373 Objects.requireNonNull(caps, "null caps"),
3374 Objects.requireNonNull(callback, "null callback"));
3375 } catch (RemoteException e) {
3376 throw e.rethrowFromSystemServer();
3377 }
3378 }
Lorenzo Colitti6702d4b2020-01-08 00:04:09 +09003379
Chalard Jeancdd68bc2021-01-05 08:40:09 +09003380 /**
3381 * Withdraw a network offer made with {@link #offerNetwork}.
3382 *
3383 * @param callback The callback passed at registration time. This must be the same object
3384 * that was passed to {@link #offerNetwork}
3385 * @hide
3386 */
3387 public void unofferNetwork(@NonNull final INetworkOfferCallback callback) {
3388 try {
3389 mService.unofferNetwork(Objects.requireNonNull(callback));
3390 } catch (RemoteException e) {
3391 throw e.rethrowFromSystemServer();
3392 }
3393 }
Lorenzo Colitti6702d4b2020-01-08 00:04:09 +09003394 /** @hide exposed via the NetworkProvider class. */
paulhub6ba8e82020-03-04 09:43:41 +08003395 @RequiresPermission(anyOf = {
3396 NetworkStack.PERMISSION_MAINLINE_NETWORK_STACK,
3397 android.Manifest.permission.NETWORK_FACTORY})
Lorenzo Colitti6702d4b2020-01-08 00:04:09 +09003398 public void declareNetworkRequestUnfulfillable(@NonNull NetworkRequest request) {
3399 try {
3400 mService.declareNetworkRequestUnfulfillable(request);
3401 } catch (RemoteException e) {
3402 throw e.rethrowFromSystemServer();
3403 }
3404 }
3405
Paul Jensen1f567382015-02-13 14:18:39 -05003406 /**
3407 * @hide
3408 * Register a NetworkAgent with ConnectivityService.
Chalard Jeanf78c9642019-12-13 19:47:12 +09003409 * @return Network corresponding to NetworkAgent.
Paul Jensen1f567382015-02-13 14:18:39 -05003410 */
paulhub6ba8e82020-03-04 09:43:41 +08003411 @RequiresPermission(anyOf = {
3412 NetworkStack.PERMISSION_MAINLINE_NETWORK_STACK,
3413 android.Manifest.permission.NETWORK_FACTORY})
Remi NGUYEN VAN77b0c1c2020-12-23 12:45:08 +09003414 public Network registerNetworkAgent(INetworkAgent na, NetworkInfo ni, LinkProperties lp,
Chalard Jean28018572020-12-21 18:36:52 +09003415 NetworkCapabilities nc, @NonNull NetworkScore score, NetworkAgentConfig config,
3416 int providerId) {
Robert Greenwalte20f7a22014-04-18 15:25:25 -07003417 try {
Remi NGUYEN VAN77b0c1c2020-12-23 12:45:08 +09003418 return mService.registerNetworkAgent(na, ni, lp, nc, score, config, providerId);
Paul Jensen1f567382015-02-13 14:18:39 -05003419 } catch (RemoteException e) {
Jeff Sharkeyc78f85c2016-03-01 19:27:23 -07003420 throw e.rethrowFromSystemServer();
Paul Jensen1f567382015-02-13 14:18:39 -05003421 }
Robert Greenwalte20f7a22014-04-18 15:25:25 -07003422 }
3423
Robert Greenwaltf99b8392014-03-26 16:47:06 -07003424 /**
Hugo Benichi2aa65af2017-03-06 09:17:06 +09003425 * Base class for {@code NetworkRequest} callbacks. Used for notifications about network
3426 * changes. Should be extended by applications wanting notifications.
3427 *
3428 * A {@code NetworkCallback} is registered by calling
3429 * {@link #requestNetwork(NetworkRequest, NetworkCallback)},
3430 * {@link #registerNetworkCallback(NetworkRequest, NetworkCallback)},
Hugo Benichicbfbb372018-02-07 21:17:43 +09003431 * or {@link #registerDefaultNetworkCallback(NetworkCallback)}. A {@code NetworkCallback} is
Hugo Benichi2aa65af2017-03-06 09:17:06 +09003432 * unregistered by calling {@link #unregisterNetworkCallback(NetworkCallback)}.
3433 * A {@code NetworkCallback} should be registered at most once at any time.
3434 * A {@code NetworkCallback} that has been unregistered can be registered again.
Robert Greenwaltf99b8392014-03-26 16:47:06 -07003435 */
Robert Greenwaltf57c03c2014-06-11 16:05:07 -07003436 public static class NetworkCallback {
Robert Greenwalte20f7a22014-04-18 15:25:25 -07003437 /**
Roshan Pius951c0032020-12-22 15:10:42 -08003438 * No flags associated with this callback.
3439 * @hide
3440 */
3441 public static final int FLAG_NONE = 0;
3442 /**
3443 * Use this flag to include any location sensitive data in {@link NetworkCapabilities} sent
3444 * via {@link #onCapabilitiesChanged(Network, NetworkCapabilities)}.
3445 * <p>
3446 * These include:
3447 * <li> Some transport info instances (retrieved via
3448 * {@link NetworkCapabilities#getTransportInfo()}) like {@link android.net.wifi.WifiInfo}
3449 * contain location sensitive information.
3450 * <li> OwnerUid (retrieved via {@link NetworkCapabilities#getOwnerUid()} is location
3451 * sensitive for wifi suggestor apps (i.e using {@link WifiNetworkSuggestion}).</li>
3452 * </p>
3453 * <p>
3454 * Note:
3455 * <li> Retrieving this location sensitive information (subject to app's location
3456 * permissions) will be noted by system. </li>
3457 * <li> Without this flag any {@link NetworkCapabilities} provided via the callback does
3458 * not include location sensitive info.
3459 * </p>
3460 */
Roshan Piuse1220262021-03-11 21:16:44 -08003461 // Note: Some existing fields which are location sensitive may still be included without
3462 // this flag if the app targets SDK < S (to maintain backwards compatibility).
Roshan Pius951c0032020-12-22 15:10:42 -08003463 public static final int FLAG_INCLUDE_LOCATION_INFO = 1 << 0;
3464
3465 /** @hide */
3466 @Retention(RetentionPolicy.SOURCE)
3467 @IntDef(flag = true, prefix = "FLAG_", value = {
3468 FLAG_NONE,
3469 FLAG_INCLUDE_LOCATION_INFO
3470 })
3471 public @interface Flag { }
3472
3473 /**
3474 * All the valid flags for error checking.
3475 */
3476 private static final int VALID_FLAGS = FLAG_INCLUDE_LOCATION_INFO;
3477
3478 public NetworkCallback() {
3479 this(FLAG_NONE);
3480 }
3481
3482 public NetworkCallback(@Flag int flags) {
Remi NGUYEN VAN81f68422021-03-15 07:31:54 +00003483 if ((flags & VALID_FLAGS) != flags) {
3484 throw new IllegalArgumentException("Invalid flags");
3485 }
Roshan Pius951c0032020-12-22 15:10:42 -08003486 mFlags = flags;
3487 }
3488
3489 /**
Lorenzo Colitti14574592015-04-24 12:23:24 +09003490 * Called when the framework connects to a new network to evaluate whether it satisfies this
3491 * request. If evaluation succeeds, this callback may be followed by an {@link #onAvailable}
3492 * callback. There is no guarantee that this new network will satisfy any requests, or that
3493 * the network will stay connected for longer than the time necessary to evaluate it.
3494 * <p>
3495 * Most applications <b>should not</b> act on this callback, and should instead use
3496 * {@link #onAvailable}. This callback is intended for use by applications that can assist
3497 * the framework in properly evaluating the network &mdash; for example, an application that
3498 * can automatically log in to a captive portal without user intervention.
3499 *
3500 * @param network The {@link Network} of the network that is being evaluated.
Lorenzo Colitti3a9df1a2015-06-11 14:27:17 +09003501 *
3502 * @hide
Robert Greenwalte20f7a22014-04-18 15:25:25 -07003503 */
paulhu1a407652019-03-22 16:35:06 +08003504 public void onPreCheck(@NonNull Network network) {}
Robert Greenwalte20f7a22014-04-18 15:25:25 -07003505
3506 /**
Lorenzo Colitti14574592015-04-24 12:23:24 +09003507 * Called when the framework connects and has declared a new network ready for use.
Robert Greenwaltf57c03c2014-06-11 16:05:07 -07003508 * This callback may be called more than once if the {@link Network} that is
3509 * satisfying the request changes.
Robert Greenwaltf3017f72014-05-18 23:07:25 -07003510 *
Robert Greenwaltf3017f72014-05-18 23:07:25 -07003511 * @param network The {@link Network} of the satisfying network.
Chalard Jeana23bc9e2018-01-30 22:41:41 +09003512 * @param networkCapabilities The {@link NetworkCapabilities} of the satisfying network.
3513 * @param linkProperties The {@link LinkProperties} of the satisfying network.
junyulaif2c67e42018-08-07 19:50:45 +08003514 * @param blocked Whether access to the {@link Network} is blocked due to system policy.
Chalard Jeana23bc9e2018-01-30 22:41:41 +09003515 * @hide
3516 */
Lorenzo Colitti79c6f222021-03-18 00:54:57 +09003517 public final void onAvailable(@NonNull Network network,
paulhu1a407652019-03-22 16:35:06 +08003518 @NonNull NetworkCapabilities networkCapabilities,
Lorenzo Colitti79c6f222021-03-18 00:54:57 +09003519 @NonNull LinkProperties linkProperties, @BlockedReason int blocked) {
Chalard Jeana23bc9e2018-01-30 22:41:41 +09003520 // Internally only this method is called when a new network is available, and
3521 // it calls the callback in the same way and order that older versions used
3522 // to call so as not to change the behavior.
Lorenzo Colitti79c6f222021-03-18 00:54:57 +09003523 onAvailable(network, networkCapabilities, linkProperties, blocked != 0);
3524 onBlockedStatusChanged(network, blocked);
3525 }
3526
3527 /**
3528 * Legacy variant of onAvailable that takes a boolean blocked reason.
3529 *
3530 * This method has never been public API, but it's not final, so there may be apps that
3531 * implemented it and rely on it being called. Do our best not to break them.
3532 * Note: such apps will also get a second call to onBlockedStatusChanged immediately after
3533 * this method is called. There does not seem to be a way to avoid this.
3534 * TODO: add a compat check to move apps off this method, and eventually stop calling it.
3535 *
3536 * @hide
3537 */
3538 public void onAvailable(@NonNull Network network,
3539 @NonNull NetworkCapabilities networkCapabilities,
3540 @NonNull LinkProperties linkProperties, boolean blocked) {
Chalard Jeana23bc9e2018-01-30 22:41:41 +09003541 onAvailable(network);
3542 if (!networkCapabilities.hasCapability(
3543 NetworkCapabilities.NET_CAPABILITY_NOT_SUSPENDED)) {
3544 onNetworkSuspended(network);
3545 }
3546 onCapabilitiesChanged(network, networkCapabilities);
3547 onLinkPropertiesChanged(network, linkProperties);
Lorenzo Colitti79c6f222021-03-18 00:54:57 +09003548 // No call to onBlockedStatusChanged here. That is done by the caller.
Chalard Jeana23bc9e2018-01-30 22:41:41 +09003549 }
3550
3551 /**
3552 * Called when the framework connects and has declared a new network ready for use.
Chalard Jean01378b62019-08-30 16:27:28 +09003553 *
3554 * <p>For callbacks registered with {@link #registerNetworkCallback}, multiple networks may
3555 * be available at the same time, and onAvailable will be called for each of these as they
3556 * appear.
3557 *
3558 * <p>For callbacks registered with {@link #requestNetwork} and
3559 * {@link #registerDefaultNetworkCallback}, this means the network passed as an argument
3560 * is the new best network for this request and is now tracked by this callback ; this
3561 * callback will no longer receive method calls about other networks that may have been
3562 * passed to this method previously. The previously-best network may have disconnected, or
3563 * it may still be around and the newly-best network may simply be better.
3564 *
3565 * <p>Starting with {@link android.os.Build.VERSION_CODES#O}, this will always immediately
3566 * be followed by a call to {@link #onCapabilitiesChanged(Network, NetworkCapabilities)}
3567 * then by a call to {@link #onLinkPropertiesChanged(Network, LinkProperties)}, and a call
3568 * to {@link #onBlockedStatusChanged(Network, boolean)}.
3569 *
3570 * <p>Do NOT call {@link #getNetworkCapabilities(Network)} or
3571 * {@link #getLinkProperties(Network)} or other synchronous ConnectivityManager methods in
3572 * this callback as this is prone to race conditions (there is no guarantee the objects
3573 * returned by these methods will be current). Instead, wait for a call to
3574 * {@link #onCapabilitiesChanged(Network, NetworkCapabilities)} and
3575 * {@link #onLinkPropertiesChanged(Network, LinkProperties)} whose arguments are guaranteed
3576 * to be well-ordered with respect to other callbacks.
Chalard Jeana23bc9e2018-01-30 22:41:41 +09003577 *
3578 * @param network The {@link Network} of the satisfying network.
Robert Greenwalte20f7a22014-04-18 15:25:25 -07003579 */
paulhu1a407652019-03-22 16:35:06 +08003580 public void onAvailable(@NonNull Network network) {}
Robert Greenwalte20f7a22014-04-18 15:25:25 -07003581
3582 /**
Chalard Jean01378b62019-08-30 16:27:28 +09003583 * Called when the network is about to be lost, typically because there are no outstanding
3584 * requests left for it. This may be paired with a {@link NetworkCallback#onAvailable} call
3585 * with the new replacement network for graceful handover. This method is not guaranteed
3586 * to be called before {@link NetworkCallback#onLost} is called, for example in case a
3587 * network is suddenly disconnected.
Robert Greenwaltf3017f72014-05-18 23:07:25 -07003588 *
Chalard Jean01378b62019-08-30 16:27:28 +09003589 * <p>Do NOT call {@link #getNetworkCapabilities(Network)} or
3590 * {@link #getLinkProperties(Network)} or other synchronous ConnectivityManager methods in
3591 * this callback as this is prone to race conditions ; calling these methods while in a
3592 * callback may return an outdated or even a null object.
3593 *
3594 * @param network The {@link Network} that is about to be lost.
3595 * @param maxMsToLive The time in milliseconds the system intends to keep the network
3596 * connected for graceful handover; note that the network may still
3597 * suffer a hard loss at any time.
Robert Greenwalte20f7a22014-04-18 15:25:25 -07003598 */
paulhu1a407652019-03-22 16:35:06 +08003599 public void onLosing(@NonNull Network network, int maxMsToLive) {}
Robert Greenwalte20f7a22014-04-18 15:25:25 -07003600
3601 /**
Chalard Jean01378b62019-08-30 16:27:28 +09003602 * Called when a network disconnects or otherwise no longer satisfies this request or
3603 * callback.
3604 *
3605 * <p>If the callback was registered with requestNetwork() or
3606 * registerDefaultNetworkCallback(), it will only be invoked against the last network
3607 * returned by onAvailable() when that network is lost and no other network satisfies
3608 * the criteria of the request.
3609 *
3610 * <p>If the callback was registered with registerNetworkCallback() it will be called for
3611 * each network which no longer satisfies the criteria of the callback.
3612 *
3613 * <p>Do NOT call {@link #getNetworkCapabilities(Network)} or
3614 * {@link #getLinkProperties(Network)} or other synchronous ConnectivityManager methods in
3615 * this callback as this is prone to race conditions ; calling these methods while in a
3616 * callback may return an outdated or even a null object.
Robert Greenwaltf3017f72014-05-18 23:07:25 -07003617 *
Robert Greenwaltf3017f72014-05-18 23:07:25 -07003618 * @param network The {@link Network} lost.
Robert Greenwalte20f7a22014-04-18 15:25:25 -07003619 */
paulhu1a407652019-03-22 16:35:06 +08003620 public void onLost(@NonNull Network network) {}
Robert Greenwalte20f7a22014-04-18 15:25:25 -07003621
3622 /**
Chalard Jean01378b62019-08-30 16:27:28 +09003623 * Called if no network is found within the timeout time specified in
Etan Cohenfbfdd842019-01-08 12:09:18 -08003624 * {@link #requestNetwork(NetworkRequest, NetworkCallback, int)} call or if the
3625 * requested network request cannot be fulfilled (whether or not a timeout was
3626 * specified). When this callback is invoked the associated
Etan Cohenf67bde92017-03-01 12:47:28 -08003627 * {@link NetworkRequest} will have already been removed and released, as if
3628 * {@link #unregisterNetworkCallback(NetworkCallback)} had been called.
Robert Greenwalte20f7a22014-04-18 15:25:25 -07003629 */
Robert Greenwaltf57c03c2014-06-11 16:05:07 -07003630 public void onUnavailable() {}
Robert Greenwalte20f7a22014-04-18 15:25:25 -07003631
3632 /**
Chalard Jean01378b62019-08-30 16:27:28 +09003633 * Called when the network corresponding to this request changes capabilities but still
3634 * satisfies the requested criteria.
3635 *
3636 * <p>Starting with {@link android.os.Build.VERSION_CODES#O} this method is guaranteed
3637 * to be called immediately after {@link #onAvailable}.
3638 *
3639 * <p>Do NOT call {@link #getLinkProperties(Network)} or other synchronous
3640 * ConnectivityManager methods in this callback as this is prone to race conditions :
3641 * calling these methods while in a callback may return an outdated or even a null object.
Robert Greenwaltf3017f72014-05-18 23:07:25 -07003642 *
Robert Greenwaltf3017f72014-05-18 23:07:25 -07003643 * @param network The {@link Network} whose capabilities have changed.
Roshan Pius951c0032020-12-22 15:10:42 -08003644 * @param networkCapabilities The new {@link NetworkCapabilities} for this
Chalard Jeana23bc9e2018-01-30 22:41:41 +09003645 * network.
Robert Greenwalte20f7a22014-04-18 15:25:25 -07003646 */
paulhu1a407652019-03-22 16:35:06 +08003647 public void onCapabilitiesChanged(@NonNull Network network,
3648 @NonNull NetworkCapabilities networkCapabilities) {}
Robert Greenwalte20f7a22014-04-18 15:25:25 -07003649
3650 /**
Chalard Jean01378b62019-08-30 16:27:28 +09003651 * Called when the network corresponding to this request changes {@link LinkProperties}.
3652 *
3653 * <p>Starting with {@link android.os.Build.VERSION_CODES#O} this method is guaranteed
3654 * to be called immediately after {@link #onAvailable}.
3655 *
3656 * <p>Do NOT call {@link #getNetworkCapabilities(Network)} or other synchronous
3657 * ConnectivityManager methods in this callback as this is prone to race conditions :
3658 * calling these methods while in a callback may return an outdated or even a null object.
Robert Greenwaltf3017f72014-05-18 23:07:25 -07003659 *
Robert Greenwaltf3017f72014-05-18 23:07:25 -07003660 * @param network The {@link Network} whose link properties have changed.
3661 * @param linkProperties The new {@link LinkProperties} for this network.
Robert Greenwalte20f7a22014-04-18 15:25:25 -07003662 */
paulhu1a407652019-03-22 16:35:06 +08003663 public void onLinkPropertiesChanged(@NonNull Network network,
3664 @NonNull LinkProperties linkProperties) {}
Robert Greenwalte20f7a22014-04-18 15:25:25 -07003665
Robert Greenwalt7fb8adc2015-06-24 13:23:42 -07003666 /**
Chalard Jean01378b62019-08-30 16:27:28 +09003667 * Called when the network the framework connected to for this request suspends data
3668 * transmission temporarily.
3669 *
3670 * <p>This generally means that while the TCP connections are still live temporarily
3671 * network data fails to transfer. To give a specific example, this is used on cellular
3672 * networks to mask temporary outages when driving through a tunnel, etc. In general this
3673 * means read operations on sockets on this network will block once the buffers are
3674 * drained, and write operations will block once the buffers are full.
3675 *
3676 * <p>Do NOT call {@link #getNetworkCapabilities(Network)} or
3677 * {@link #getLinkProperties(Network)} or other synchronous ConnectivityManager methods in
3678 * this callback as this is prone to race conditions (there is no guarantee the objects
3679 * returned by these methods will be current).
3680 *
Robert Greenwalt7fb8adc2015-06-24 13:23:42 -07003681 * @hide
3682 */
paulhu1a407652019-03-22 16:35:06 +08003683 public void onNetworkSuspended(@NonNull Network network) {}
Robert Greenwalt7fb8adc2015-06-24 13:23:42 -07003684
3685 /**
3686 * Called when the network the framework connected to for this request
Chalard Jeana23bc9e2018-01-30 22:41:41 +09003687 * returns from a {@link NetworkInfo.State#SUSPENDED} state. This should always be
3688 * preceded by a matching {@link NetworkCallback#onNetworkSuspended} call.
Chalard Jean01378b62019-08-30 16:27:28 +09003689
3690 * <p>Do NOT call {@link #getNetworkCapabilities(Network)} or
3691 * {@link #getLinkProperties(Network)} or other synchronous ConnectivityManager methods in
3692 * this callback as this is prone to race conditions : calling these methods while in a
3693 * callback may return an outdated or even a null object.
3694 *
Robert Greenwalt7fb8adc2015-06-24 13:23:42 -07003695 * @hide
3696 */
paulhu1a407652019-03-22 16:35:06 +08003697 public void onNetworkResumed(@NonNull Network network) {}
Robert Greenwalt7fb8adc2015-06-24 13:23:42 -07003698
junyulaif2c67e42018-08-07 19:50:45 +08003699 /**
3700 * Called when access to the specified network is blocked or unblocked.
3701 *
Chalard Jean01378b62019-08-30 16:27:28 +09003702 * <p>Do NOT call {@link #getNetworkCapabilities(Network)} or
3703 * {@link #getLinkProperties(Network)} or other synchronous ConnectivityManager methods in
3704 * this callback as this is prone to race conditions : calling these methods while in a
3705 * callback may return an outdated or even a null object.
3706 *
junyulaif2c67e42018-08-07 19:50:45 +08003707 * @param network The {@link Network} whose blocked status has changed.
3708 * @param blocked The blocked status of this {@link Network}.
3709 */
junyulai7e06ad42019-03-04 22:45:36 +08003710 public void onBlockedStatusChanged(@NonNull Network network, boolean blocked) {}
junyulaif2c67e42018-08-07 19:50:45 +08003711
Lorenzo Colitti79c6f222021-03-18 00:54:57 +09003712 /**
Lorenzo Colitti42fe2232021-03-25 23:17:36 +09003713 * Called when access to the specified network is blocked or unblocked, or the reason for
3714 * access being blocked changes.
Lorenzo Colitti79c6f222021-03-18 00:54:57 +09003715 *
3716 * If a NetworkCallback object implements this method,
3717 * {@link #onBlockedStatusChanged(Network, boolean)} will not be called.
3718 *
3719 * <p>Do NOT call {@link #getNetworkCapabilities(Network)} or
3720 * {@link #getLinkProperties(Network)} or other synchronous ConnectivityManager methods in
3721 * this callback as this is prone to race conditions : calling these methods while in a
3722 * callback may return an outdated or even a null object.
3723 *
3724 * @param network The {@link Network} whose blocked status has changed.
3725 * @param blocked The blocked status of this {@link Network}.
3726 * @hide
3727 */
3728 @SystemApi(client = MODULE_LIBRARIES)
3729 public void onBlockedStatusChanged(@NonNull Network network, @BlockedReason int blocked) {
3730 onBlockedStatusChanged(network, blocked != 0);
3731 }
3732
Robert Greenwaltf57c03c2014-06-11 16:05:07 -07003733 private NetworkRequest networkRequest;
Roshan Pius951c0032020-12-22 15:10:42 -08003734 private final int mFlags;
Robert Greenwalte20f7a22014-04-18 15:25:25 -07003735 }
3736
Hugo Benichia590ab82017-05-11 13:16:17 +09003737 /**
3738 * Constant error codes used by ConnectivityService to communicate about failures and errors
3739 * across a Binder boundary.
3740 * @hide
3741 */
3742 public interface Errors {
Chalard Jean9dd11612018-06-04 16:52:49 +09003743 int TOO_MANY_REQUESTS = 1;
Hugo Benichia590ab82017-05-11 13:16:17 +09003744 }
3745
3746 /** @hide */
3747 public static class TooManyRequestsException extends RuntimeException {}
3748
3749 private static RuntimeException convertServiceException(ServiceSpecificException e) {
3750 switch (e.errorCode) {
3751 case Errors.TOO_MANY_REQUESTS:
3752 return new TooManyRequestsException();
3753 default:
3754 Log.w(TAG, "Unknown service error code " + e.errorCode);
3755 return new RuntimeException(e);
3756 }
3757 }
3758
Robert Greenwaltf99b8392014-03-26 16:47:06 -07003759 /** @hide */
Remi NGUYEN VAN1fb66ad2021-03-12 15:24:06 +09003760 public static final int CALLBACK_PRECHECK = 1;
Robert Greenwalt7fb8adc2015-06-24 13:23:42 -07003761 /** @hide */
Remi NGUYEN VAN1fb66ad2021-03-12 15:24:06 +09003762 public static final int CALLBACK_AVAILABLE = 2;
Robert Greenwalt7fb8adc2015-06-24 13:23:42 -07003763 /** @hide arg1 = TTL */
Remi NGUYEN VAN1fb66ad2021-03-12 15:24:06 +09003764 public static final int CALLBACK_LOSING = 3;
Robert Greenwalt7fb8adc2015-06-24 13:23:42 -07003765 /** @hide */
Remi NGUYEN VAN1fb66ad2021-03-12 15:24:06 +09003766 public static final int CALLBACK_LOST = 4;
Robert Greenwalt7fb8adc2015-06-24 13:23:42 -07003767 /** @hide */
Remi NGUYEN VAN1fb66ad2021-03-12 15:24:06 +09003768 public static final int CALLBACK_UNAVAIL = 5;
Robert Greenwalt7fb8adc2015-06-24 13:23:42 -07003769 /** @hide */
Remi NGUYEN VAN1fb66ad2021-03-12 15:24:06 +09003770 public static final int CALLBACK_CAP_CHANGED = 6;
Robert Greenwalt7fb8adc2015-06-24 13:23:42 -07003771 /** @hide */
Remi NGUYEN VAN1fb66ad2021-03-12 15:24:06 +09003772 public static final int CALLBACK_IP_CHANGED = 7;
Robert Greenwalt3dc73e52014-05-15 18:07:26 -07003773 /** @hide obj = NetworkCapabilities, arg1 = seq number */
Remi NGUYEN VAN1fb66ad2021-03-12 15:24:06 +09003774 private static final int EXPIRE_LEGACY_REQUEST = 8;
Robert Greenwalt7fb8adc2015-06-24 13:23:42 -07003775 /** @hide */
Remi NGUYEN VAN1fb66ad2021-03-12 15:24:06 +09003776 public static final int CALLBACK_SUSPENDED = 9;
Robert Greenwalt7fb8adc2015-06-24 13:23:42 -07003777 /** @hide */
Remi NGUYEN VAN1fb66ad2021-03-12 15:24:06 +09003778 public static final int CALLBACK_RESUMED = 10;
junyulaif2c67e42018-08-07 19:50:45 +08003779 /** @hide */
Remi NGUYEN VAN1fb66ad2021-03-12 15:24:06 +09003780 public static final int CALLBACK_BLK_CHANGED = 11;
Robert Greenwaltf99b8392014-03-26 16:47:06 -07003781
Erik Kline155a59a2015-11-25 12:49:38 +09003782 /** @hide */
3783 public static String getCallbackName(int whichCallback) {
3784 switch (whichCallback) {
3785 case CALLBACK_PRECHECK: return "CALLBACK_PRECHECK";
3786 case CALLBACK_AVAILABLE: return "CALLBACK_AVAILABLE";
3787 case CALLBACK_LOSING: return "CALLBACK_LOSING";
3788 case CALLBACK_LOST: return "CALLBACK_LOST";
3789 case CALLBACK_UNAVAIL: return "CALLBACK_UNAVAIL";
3790 case CALLBACK_CAP_CHANGED: return "CALLBACK_CAP_CHANGED";
3791 case CALLBACK_IP_CHANGED: return "CALLBACK_IP_CHANGED";
Erik Kline155a59a2015-11-25 12:49:38 +09003792 case EXPIRE_LEGACY_REQUEST: return "EXPIRE_LEGACY_REQUEST";
3793 case CALLBACK_SUSPENDED: return "CALLBACK_SUSPENDED";
3794 case CALLBACK_RESUMED: return "CALLBACK_RESUMED";
junyulaif2c67e42018-08-07 19:50:45 +08003795 case CALLBACK_BLK_CHANGED: return "CALLBACK_BLK_CHANGED";
Erik Kline155a59a2015-11-25 12:49:38 +09003796 default:
3797 return Integer.toString(whichCallback);
3798 }
3799 }
3800
Robert Greenwalt3dc73e52014-05-15 18:07:26 -07003801 private class CallbackHandler extends Handler {
Robert Greenwaltf99b8392014-03-26 16:47:06 -07003802 private static final String TAG = "ConnectivityManager.CallbackHandler";
Robert Greenwalt72877c22015-09-03 16:41:45 -07003803 private static final boolean DBG = false;
Robert Greenwaltf99b8392014-03-26 16:47:06 -07003804
Hugo Benichi0b42baf2016-07-06 22:53:17 +09003805 CallbackHandler(Looper looper) {
Robert Greenwaltf99b8392014-03-26 16:47:06 -07003806 super(looper);
Robert Greenwaltf99b8392014-03-26 16:47:06 -07003807 }
3808
Hugo Benichifd44e912017-02-02 17:02:36 +09003809 CallbackHandler(Handler handler) {
Remi NGUYEN VAN81f68422021-03-15 07:31:54 +00003810 this(Objects.requireNonNull(handler, "Handler cannot be null.").getLooper());
Hugo Benichifd44e912017-02-02 17:02:36 +09003811 }
3812
Robert Greenwaltf99b8392014-03-26 16:47:06 -07003813 @Override
3814 public void handleMessage(Message message) {
Hugo Benichib6c24062017-05-09 14:36:02 +09003815 if (message.what == EXPIRE_LEGACY_REQUEST) {
3816 expireRequest((NetworkCapabilities) message.obj, message.arg1);
3817 return;
3818 }
3819
3820 final NetworkRequest request = getObject(message, NetworkRequest.class);
3821 final Network network = getObject(message, Network.class);
3822 final NetworkCallback callback;
3823 synchronized (sCallbacks) {
3824 callback = sCallbacks.get(request);
Etan Cohenb58e3662019-05-21 12:06:04 -07003825 if (callback == null) {
3826 Log.w(TAG,
3827 "callback not found for " + getCallbackName(message.what) + " message");
3828 return;
3829 }
Etan Cohen6cb65992019-04-16 15:07:55 -07003830 if (message.what == CALLBACK_UNAVAIL) {
3831 sCallbacks.remove(request);
3832 callback.networkRequest = ALREADY_UNREGISTERED;
3833 }
Hugo Benichib6c24062017-05-09 14:36:02 +09003834 }
Lorenzo Colittib027e6e2016-03-01 22:56:37 +09003835 if (DBG) {
Hugo Benichi8d962922017-03-22 17:07:57 +09003836 Log.d(TAG, getCallbackName(message.what) + " for network " + network);
Lorenzo Colittib027e6e2016-03-01 22:56:37 +09003837 }
Hugo Benichib6c24062017-05-09 14:36:02 +09003838
Robert Greenwaltf99b8392014-03-26 16:47:06 -07003839 switch (message.what) {
3840 case CALLBACK_PRECHECK: {
Hugo Benichib6c24062017-05-09 14:36:02 +09003841 callback.onPreCheck(network);
Robert Greenwaltf99b8392014-03-26 16:47:06 -07003842 break;
3843 }
3844 case CALLBACK_AVAILABLE: {
Chalard Jeana23bc9e2018-01-30 22:41:41 +09003845 NetworkCapabilities cap = getObject(message, NetworkCapabilities.class);
3846 LinkProperties lp = getObject(message, LinkProperties.class);
Lorenzo Colitti79c6f222021-03-18 00:54:57 +09003847 callback.onAvailable(network, cap, lp, message.arg1);
Robert Greenwaltf99b8392014-03-26 16:47:06 -07003848 break;
3849 }
3850 case CALLBACK_LOSING: {
Hugo Benichib6c24062017-05-09 14:36:02 +09003851 callback.onLosing(network, message.arg1);
Robert Greenwaltf99b8392014-03-26 16:47:06 -07003852 break;
3853 }
3854 case CALLBACK_LOST: {
Hugo Benichib6c24062017-05-09 14:36:02 +09003855 callback.onLost(network);
Robert Greenwaltf99b8392014-03-26 16:47:06 -07003856 break;
3857 }
3858 case CALLBACK_UNAVAIL: {
Hugo Benichib6c24062017-05-09 14:36:02 +09003859 callback.onUnavailable();
Robert Greenwaltf99b8392014-03-26 16:47:06 -07003860 break;
3861 }
3862 case CALLBACK_CAP_CHANGED: {
Hugo Benichib6c24062017-05-09 14:36:02 +09003863 NetworkCapabilities cap = getObject(message, NetworkCapabilities.class);
3864 callback.onCapabilitiesChanged(network, cap);
Robert Greenwaltf99b8392014-03-26 16:47:06 -07003865 break;
3866 }
3867 case CALLBACK_IP_CHANGED: {
Hugo Benichib6c24062017-05-09 14:36:02 +09003868 LinkProperties lp = getObject(message, LinkProperties.class);
3869 callback.onLinkPropertiesChanged(network, lp);
Robert Greenwaltf99b8392014-03-26 16:47:06 -07003870 break;
3871 }
Robert Greenwalt7fb8adc2015-06-24 13:23:42 -07003872 case CALLBACK_SUSPENDED: {
Hugo Benichib6c24062017-05-09 14:36:02 +09003873 callback.onNetworkSuspended(network);
Robert Greenwalt7fb8adc2015-06-24 13:23:42 -07003874 break;
3875 }
3876 case CALLBACK_RESUMED: {
Hugo Benichib6c24062017-05-09 14:36:02 +09003877 callback.onNetworkResumed(network);
Robert Greenwalt3dc73e52014-05-15 18:07:26 -07003878 break;
3879 }
junyulaif2c67e42018-08-07 19:50:45 +08003880 case CALLBACK_BLK_CHANGED: {
Lorenzo Colitti79c6f222021-03-18 00:54:57 +09003881 callback.onBlockedStatusChanged(network, message.arg1);
junyulaif2c67e42018-08-07 19:50:45 +08003882 }
Robert Greenwaltf99b8392014-03-26 16:47:06 -07003883 }
3884 }
3885
Hugo Benichi0b42baf2016-07-06 22:53:17 +09003886 private <T> T getObject(Message msg, Class<T> c) {
3887 return (T) msg.getData().getParcelable(c.getSimpleName());
Robert Greenwaltf99b8392014-03-26 16:47:06 -07003888 }
Robert Greenwaltf99b8392014-03-26 16:47:06 -07003889 }
3890
Hugo Benichifd44e912017-02-02 17:02:36 +09003891 private CallbackHandler getDefaultHandler() {
Hugo Benichi3b41f052016-07-07 10:15:56 +09003892 synchronized (sCallbacks) {
3893 if (sCallbackHandler == null) {
3894 sCallbackHandler = new CallbackHandler(ConnectivityThread.getInstanceLooper());
Robert Greenwaltf99b8392014-03-26 16:47:06 -07003895 }
Hugo Benichi3b41f052016-07-07 10:15:56 +09003896 return sCallbackHandler;
Robert Greenwaltf99b8392014-03-26 16:47:06 -07003897 }
3898 }
3899
Hugo Benichibc4ac972017-02-03 14:18:44 +09003900 private static final HashMap<NetworkRequest, NetworkCallback> sCallbacks = new HashMap<>();
3901 private static CallbackHandler sCallbackHandler;
Robert Greenwaltf99b8392014-03-26 16:47:06 -07003902
Lorenzo Colitti2a49c5d2021-03-12 22:48:07 +09003903 private NetworkRequest sendRequestForNetwork(int asUid, NetworkCapabilities need,
3904 NetworkCallback callback, int timeoutMs, NetworkRequest.Type reqType, int legacyType,
3905 CallbackHandler handler) {
Soi, Yoshinarib8102ef2015-11-12 12:09:02 +09003906 printStackTrace();
Hugo Benichibc1104b2017-05-09 15:19:01 +09003907 checkCallbackNotNull(callback);
Remi NGUYEN VAN81f68422021-03-15 07:31:54 +00003908 if (reqType != TRACK_DEFAULT && reqType != TRACK_SYSTEM_DEFAULT && need == null) {
3909 throw new IllegalArgumentException("null NetworkCapabilities");
3910 }
Hugo Benichi0b42baf2016-07-06 22:53:17 +09003911 final NetworkRequest request;
Roshan Piusbc7e37d2020-01-16 12:17:17 -08003912 final String callingPackageName = mContext.getOpPackageName();
Robert Greenwaltf99b8392014-03-26 16:47:06 -07003913 try {
Hugo Benichi0b42baf2016-07-06 22:53:17 +09003914 synchronized(sCallbacks) {
Hugo Benichibee30fe2017-06-17 13:14:12 +09003915 if (callback.networkRequest != null
3916 && callback.networkRequest != ALREADY_UNREGISTERED) {
Hugo Benichi2aa65af2017-03-06 09:17:06 +09003917 // TODO: throw exception instead and enforce 1:1 mapping of callbacks
3918 // and requests (http://b/20701525).
3919 Log.e(TAG, "NetworkCallback was already registered");
3920 }
Hugo Benichi3b41f052016-07-07 10:15:56 +09003921 Messenger messenger = new Messenger(handler);
Hugo Benichi0b42baf2016-07-06 22:53:17 +09003922 Binder binder = new Binder();
Roshan Pius951c0032020-12-22 15:10:42 -08003923 final int callbackFlags = callback.mFlags;
junyulaiad010792021-01-11 16:53:38 +08003924 if (reqType == LISTEN) {
Roshan Piusbc7e37d2020-01-16 12:17:17 -08003925 request = mService.listenForNetwork(
Roshan Pius951c0032020-12-22 15:10:42 -08003926 need, messenger, binder, callbackFlags, callingPackageName,
Roshan Piusaa24fde2020-12-17 14:53:09 -08003927 getAttributionTag());
Paul Jensenbb427682014-06-27 11:05:32 -04003928 } else {
Hugo Benichi0b42baf2016-07-06 22:53:17 +09003929 request = mService.requestNetwork(
Lorenzo Colitti2a49c5d2021-03-12 22:48:07 +09003930 asUid, need, reqType.ordinal(), messenger, timeoutMs, binder,
3931 legacyType, callbackFlags, callingPackageName, getAttributionTag());
Paul Jensenbb427682014-06-27 11:05:32 -04003932 }
Hugo Benichi0b42baf2016-07-06 22:53:17 +09003933 if (request != null) {
Hugo Benichi3b41f052016-07-07 10:15:56 +09003934 sCallbacks.put(request, callback);
Robert Greenwaltf99b8392014-03-26 16:47:06 -07003935 }
Hugo Benichi3b41f052016-07-07 10:15:56 +09003936 callback.networkRequest = request;
Robert Greenwaltf99b8392014-03-26 16:47:06 -07003937 }
Jeff Sharkeyc78f85c2016-03-01 19:27:23 -07003938 } catch (RemoteException e) {
3939 throw e.rethrowFromSystemServer();
Hugo Benichia590ab82017-05-11 13:16:17 +09003940 } catch (ServiceSpecificException e) {
3941 throw convertServiceException(e);
Jeff Sharkeyc78f85c2016-03-01 19:27:23 -07003942 }
Hugo Benichi0b42baf2016-07-06 22:53:17 +09003943 return request;
Robert Greenwaltf99b8392014-03-26 16:47:06 -07003944 }
3945
Lorenzo Colitti2a49c5d2021-03-12 22:48:07 +09003946 private NetworkRequest sendRequestForNetwork(NetworkCapabilities need, NetworkCallback callback,
3947 int timeoutMs, NetworkRequest.Type reqType, int legacyType, CallbackHandler handler) {
3948 return sendRequestForNetwork(Process.INVALID_UID, need, callback, timeoutMs, reqType,
3949 legacyType, handler);
3950 }
3951
Robert Greenwaltf99b8392014-03-26 16:47:06 -07003952 /**
Erik Kline23bf99c2016-03-16 15:31:39 +09003953 * Helper function to request a network with a particular legacy type.
Lorenzo Colitti6653c4d2015-11-25 12:00:52 +09003954 *
markchienfac84a22020-03-18 21:16:15 +08003955 * This API is only for use in internal system code that requests networks with legacy type and
3956 * relies on CONNECTIVITY_ACTION broadcasts instead of NetworkCallbacks. New caller should use
markchiend6eeffd2020-01-14 00:55:21 +08003957 * {@link #requestNetwork(NetworkRequest, NetworkCallback, Handler)} instead.
Lorenzo Colitti6653c4d2015-11-25 12:00:52 +09003958 *
markchiend6eeffd2020-01-14 00:55:21 +08003959 * @param request {@link NetworkRequest} describing this request.
markchiend6eeffd2020-01-14 00:55:21 +08003960 * @param timeoutMs The time in milliseconds to attempt looking for a suitable network
3961 * before {@link NetworkCallback#onUnavailable()} is called. The timeout must
3962 * be a positive value (i.e. >0).
3963 * @param legacyType to specify the network type(#TYPE_*).
3964 * @param handler {@link Handler} to specify the thread upon which the callback will be invoked.
markchienfac84a22020-03-18 21:16:15 +08003965 * @param networkCallback The {@link NetworkCallback} to be utilized for this request. Note
3966 * the callback must not be shared - it uniquely specifies this request.
Lorenzo Colitti6653c4d2015-11-25 12:00:52 +09003967 *
3968 * @hide
3969 */
markchiend6eeffd2020-01-14 00:55:21 +08003970 @SystemApi
markchienfac84a22020-03-18 21:16:15 +08003971 @RequiresPermission(NetworkStack.PERMISSION_MAINLINE_NETWORK_STACK)
Chalard Jean158702d2019-01-07 19:26:34 +09003972 public void requestNetwork(@NonNull NetworkRequest request,
markchienfac84a22020-03-18 21:16:15 +08003973 int timeoutMs, int legacyType, @NonNull Handler handler,
3974 @NonNull NetworkCallback networkCallback) {
3975 if (legacyType == TYPE_NONE) {
3976 throw new IllegalArgumentException("TYPE_NONE is meaningless legacy type");
3977 }
Hugo Benichifd44e912017-02-02 17:02:36 +09003978 CallbackHandler cbHandler = new CallbackHandler(handler);
3979 NetworkCapabilities nc = request.networkCapabilities;
3980 sendRequestForNetwork(nc, networkCallback, timeoutMs, REQUEST, legacyType, cbHandler);
Lorenzo Colitti6653c4d2015-11-25 12:00:52 +09003981 }
3982
3983 /**
Roshan Pius951c0032020-12-22 15:10:42 -08003984 * Request a network to satisfy a set of {@link NetworkCapabilities}.
Robert Greenwaltf99b8392014-03-26 16:47:06 -07003985 *
Chalard Jean01378b62019-08-30 16:27:28 +09003986 * <p>This method will attempt to find the best network that matches the passed
3987 * {@link NetworkRequest}, and to bring up one that does if none currently satisfies the
3988 * criteria. The platform will evaluate which network is the best at its own discretion.
3989 * Throughput, latency, cost per byte, policy, user preference and other considerations
3990 * may be factored in the decision of what is considered the best network.
3991 *
3992 * <p>As long as this request is outstanding, the platform will try to maintain the best network
3993 * matching this request, while always attempting to match the request to a better network if
3994 * possible. If a better match is found, the platform will switch this request to the now-best
3995 * network and inform the app of the newly best network by invoking
3996 * {@link NetworkCallback#onAvailable(Network)} on the provided callback. Note that the platform
3997 * will not try to maintain any other network than the best one currently matching the request:
3998 * a network not matching any network request may be disconnected at any time.
3999 *
4000 * <p>For example, an application could use this method to obtain a connected cellular network
4001 * even if the device currently has a data connection over Ethernet. This may cause the cellular
4002 * radio to consume additional power. Or, an application could inform the system that it wants
4003 * a network supporting sending MMSes and have the system let it know about the currently best
4004 * MMS-supporting network through the provided {@link NetworkCallback}.
4005 *
4006 * <p>The status of the request can be followed by listening to the various callbacks described
4007 * in {@link NetworkCallback}. The {@link Network} object passed to the callback methods can be
4008 * used to direct traffic to the network (although accessing some networks may be subject to
4009 * holding specific permissions). Callers will learn about the specific characteristics of the
4010 * network through
4011 * {@link NetworkCallback#onCapabilitiesChanged(Network, NetworkCapabilities)} and
4012 * {@link NetworkCallback#onLinkPropertiesChanged(Network, LinkProperties)}. The methods of the
4013 * provided {@link NetworkCallback} will only be invoked due to changes in the best network
4014 * matching the request at any given time; therefore when a better network matching the request
4015 * becomes available, the {@link NetworkCallback#onAvailable(Network)} method is called
4016 * with the new network after which no further updates are given about the previously-best
4017 * network, unless it becomes the best again at some later time. All callbacks are invoked
4018 * in order on the same thread, which by default is a thread created by the framework running
4019 * in the app.
4020 * {@see #requestNetwork(NetworkRequest, NetworkCallback, Handler)} to change where the
4021 * callbacks are invoked.
4022 *
4023 * <p>This{@link NetworkRequest} will live until released via
4024 * {@link #unregisterNetworkCallback(NetworkCallback)} or the calling application exits, at
4025 * which point the system may let go of the network at any time.
4026 *
4027 * <p>A version of this method which takes a timeout is
4028 * {@link #requestNetwork(NetworkRequest, NetworkCallback, int)}, that an app can use to only
4029 * wait for a limited amount of time for the network to become unavailable.
4030 *
Paul Jensenee52d232015-06-16 15:11:58 -04004031 * <p>It is presently unsupported to request a network with mutable
4032 * {@link NetworkCapabilities} such as
4033 * {@link NetworkCapabilities#NET_CAPABILITY_VALIDATED} or
4034 * {@link NetworkCapabilities#NET_CAPABILITY_CAPTIVE_PORTAL}
4035 * as these {@code NetworkCapabilities} represent states that a particular
4036 * network may never attain, and whether a network will attain these states
4037 * is unknown prior to bringing up the network so the framework does not
Chalard Jean01378b62019-08-30 16:27:28 +09004038 * know how to go about satisfying a request with these capabilities.
Lorenzo Colitti0b40c872015-10-15 16:29:00 +09004039 *
4040 * <p>This method requires the caller to hold either the
4041 * {@link android.Manifest.permission#CHANGE_NETWORK_STATE} permission
4042 * or the ability to modify system settings as determined by
4043 * {@link android.provider.Settings.System#canWrite}.</p>
Robert Greenwaltf99b8392014-03-26 16:47:06 -07004044 *
Chalard Jeana5ff1132020-05-20 16:11:50 +09004045 * <p>To avoid performance issues due to apps leaking callbacks, the system will limit the
4046 * number of outstanding requests to 100 per app (identified by their UID), shared with
4047 * all variants of this method, of {@link #registerNetworkCallback} as well as
4048 * {@link ConnectivityDiagnosticsManager#registerConnectivityDiagnosticsCallback}.
4049 * Requesting a network with this method will count toward this limit. If this limit is
4050 * exceeded, an exception will be thrown. To avoid hitting this issue and to conserve resources,
4051 * make sure to unregister the callbacks with
4052 * {@link #unregisterNetworkCallback(NetworkCallback)}.
4053 *
Robert Greenwaltf57c03c2014-06-11 16:05:07 -07004054 * @param request {@link NetworkRequest} describing this request.
Hugo Benichifd44e912017-02-02 17:02:36 +09004055 * @param networkCallback The {@link NetworkCallback} to be utilized for this request. Note
4056 * the callback must not be shared - it uniquely specifies this request.
4057 * The callback is invoked on the default internal Handler.
Chalard Jean31740e42019-05-13 15:13:58 +09004058 * @throws IllegalArgumentException if {@code request} contains invalid network capabilities.
4059 * @throws SecurityException if missing the appropriate permissions.
Chalard Jeana5ff1132020-05-20 16:11:50 +09004060 * @throws RuntimeException if the app already has too many callbacks registered.
Robert Greenwaltf99b8392014-03-26 16:47:06 -07004061 */
Chalard Jean158702d2019-01-07 19:26:34 +09004062 public void requestNetwork(@NonNull NetworkRequest request,
4063 @NonNull NetworkCallback networkCallback) {
Hugo Benichifd44e912017-02-02 17:02:36 +09004064 requestNetwork(request, networkCallback, getDefaultHandler());
4065 }
4066
4067 /**
Roshan Pius951c0032020-12-22 15:10:42 -08004068 * Request a network to satisfy a set of {@link NetworkCapabilities}.
Hugo Benichifd44e912017-02-02 17:02:36 +09004069 *
Chalard Jean01378b62019-08-30 16:27:28 +09004070 * This method behaves identically to {@link #requestNetwork(NetworkRequest, NetworkCallback)}
4071 * but runs all the callbacks on the passed Handler.
Hugo Benichifd44e912017-02-02 17:02:36 +09004072 *
Chalard Jean01378b62019-08-30 16:27:28 +09004073 * <p>This method has the same permission requirements as
Chalard Jeana5ff1132020-05-20 16:11:50 +09004074 * {@link #requestNetwork(NetworkRequest, NetworkCallback)}, is subject to the same limitations,
4075 * and throws the same exceptions in the same conditions.
Hugo Benichifd44e912017-02-02 17:02:36 +09004076 *
4077 * @param request {@link NetworkRequest} describing this request.
4078 * @param networkCallback The {@link NetworkCallback} to be utilized for this request. Note
4079 * the callback must not be shared - it uniquely specifies this request.
4080 * @param handler {@link Handler} to specify the thread upon which the callback will be invoked.
Hugo Benichifd44e912017-02-02 17:02:36 +09004081 */
Chalard Jean158702d2019-01-07 19:26:34 +09004082 public void requestNetwork(@NonNull NetworkRequest request,
4083 @NonNull NetworkCallback networkCallback, @NonNull Handler handler) {
Hugo Benichifd44e912017-02-02 17:02:36 +09004084 CallbackHandler cbHandler = new CallbackHandler(handler);
markchienfac84a22020-03-18 21:16:15 +08004085 NetworkCapabilities nc = request.networkCapabilities;
4086 sendRequestForNetwork(nc, networkCallback, 0, REQUEST, TYPE_NONE, cbHandler);
Robert Greenwaltf99b8392014-03-26 16:47:06 -07004087 }
4088
4089 /**
Roshan Pius951c0032020-12-22 15:10:42 -08004090 * Request a network to satisfy a set of {@link NetworkCapabilities}, limited
Etan Cohenf67bde92017-03-01 12:47:28 -08004091 * by a timeout.
4092 *
4093 * This function behaves identically to the non-timed-out version
4094 * {@link #requestNetwork(NetworkRequest, NetworkCallback)}, but if a suitable network
4095 * is not found within the given time (in milliseconds) the
4096 * {@link NetworkCallback#onUnavailable()} callback is called. The request can still be
4097 * released normally by calling {@link #unregisterNetworkCallback(NetworkCallback)} but does
4098 * not have to be released if timed-out (it is automatically released). Unregistering a
4099 * request that timed out is not an error.
4100 *
4101 * <p>Do not use this method to poll for the existence of specific networks (e.g. with a small
4102 * timeout) - {@link #registerNetworkCallback(NetworkRequest, NetworkCallback)} is provided
4103 * for that purpose. Calling this method will attempt to bring up the requested network.
4104 *
Chalard Jean01378b62019-08-30 16:27:28 +09004105 * <p>This method has the same permission requirements as
Chalard Jeana5ff1132020-05-20 16:11:50 +09004106 * {@link #requestNetwork(NetworkRequest, NetworkCallback)}, is subject to the same limitations,
4107 * and throws the same exceptions in the same conditions.
Etan Cohenf67bde92017-03-01 12:47:28 -08004108 *
4109 * @param request {@link NetworkRequest} describing this request.
Lorenzo Colitti139a48c2017-04-28 00:56:30 +09004110 * @param networkCallback The {@link NetworkCallback} to be utilized for this request. Note
4111 * the callback must not be shared - it uniquely specifies this request.
Etan Cohenf67bde92017-03-01 12:47:28 -08004112 * @param timeoutMs The time in milliseconds to attempt looking for a suitable network
4113 * before {@link NetworkCallback#onUnavailable()} is called. The timeout must
4114 * be a positive value (i.e. >0).
Etan Cohenf67bde92017-03-01 12:47:28 -08004115 */
Chalard Jean158702d2019-01-07 19:26:34 +09004116 public void requestNetwork(@NonNull NetworkRequest request,
4117 @NonNull NetworkCallback networkCallback, int timeoutMs) {
Hugo Benichibc1104b2017-05-09 15:19:01 +09004118 checkTimeout(timeoutMs);
markchienfac84a22020-03-18 21:16:15 +08004119 NetworkCapabilities nc = request.networkCapabilities;
4120 sendRequestForNetwork(nc, networkCallback, timeoutMs, REQUEST, TYPE_NONE,
4121 getDefaultHandler());
Hugo Benichifd44e912017-02-02 17:02:36 +09004122 }
4123
Hugo Benichifd44e912017-02-02 17:02:36 +09004124 /**
Roshan Pius951c0032020-12-22 15:10:42 -08004125 * Request a network to satisfy a set of {@link NetworkCapabilities}, limited
Hugo Benichifd44e912017-02-02 17:02:36 +09004126 * by a timeout.
4127 *
Chalard Jean01378b62019-08-30 16:27:28 +09004128 * This method behaves identically to
4129 * {@link #requestNetwork(NetworkRequest, NetworkCallback, int)} but runs all the callbacks
4130 * on the passed Handler.
Etan Cohenf67bde92017-03-01 12:47:28 -08004131 *
Chalard Jean01378b62019-08-30 16:27:28 +09004132 * <p>This method has the same permission requirements as
Chalard Jeana5ff1132020-05-20 16:11:50 +09004133 * {@link #requestNetwork(NetworkRequest, NetworkCallback)}, is subject to the same limitations,
4134 * and throws the same exceptions in the same conditions.
Hugo Benichifd44e912017-02-02 17:02:36 +09004135 *
4136 * @param request {@link NetworkRequest} describing this request.
Etan Cohenf67bde92017-03-01 12:47:28 -08004137 * @param networkCallback The {@link NetworkCallback} to be utilized for this request. Note
4138 * the callback must not be shared - it uniquely specifies this request.
Hugo Benichifd44e912017-02-02 17:02:36 +09004139 * @param handler {@link Handler} to specify the thread upon which the callback will be invoked.
Lorenzo Colitti139a48c2017-04-28 00:56:30 +09004140 * @param timeoutMs The time in milliseconds to attempt looking for a suitable network
4141 * before {@link NetworkCallback#onUnavailable} is called.
Hugo Benichifd44e912017-02-02 17:02:36 +09004142 */
Chalard Jean158702d2019-01-07 19:26:34 +09004143 public void requestNetwork(@NonNull NetworkRequest request,
4144 @NonNull NetworkCallback networkCallback, @NonNull Handler handler, int timeoutMs) {
Hugo Benichibc1104b2017-05-09 15:19:01 +09004145 checkTimeout(timeoutMs);
Hugo Benichifd44e912017-02-02 17:02:36 +09004146 CallbackHandler cbHandler = new CallbackHandler(handler);
markchienfac84a22020-03-18 21:16:15 +08004147 NetworkCapabilities nc = request.networkCapabilities;
4148 sendRequestForNetwork(nc, networkCallback, timeoutMs, REQUEST, TYPE_NONE, cbHandler);
Robert Greenwaltf99b8392014-03-26 16:47:06 -07004149 }
4150
4151 /**
Robert Greenwaltf3017f72014-05-18 23:07:25 -07004152 * The lookup key for a {@link Network} object included with the intent after
Jeremy Joslin60d379b2014-11-05 10:32:09 -08004153 * successfully finding a network for the applications request. Retrieve it with
Robert Greenwaltf3017f72014-05-18 23:07:25 -07004154 * {@link android.content.Intent#getParcelableExtra(String)}.
Jeremy Joslinc5302632015-02-11 16:51:13 -08004155 * <p>
Paul Jensenee2f45d2015-03-10 10:54:12 -04004156 * Note that if you intend to invoke {@link Network#openConnection(java.net.URL)}
4157 * then you must get a ConnectivityManager instance before doing so.
Robert Greenwaltf3017f72014-05-18 23:07:25 -07004158 */
Erik Kline8a826212014-11-19 12:12:24 +09004159 public static final String EXTRA_NETWORK = "android.net.extra.NETWORK";
Robert Greenwaltf3017f72014-05-18 23:07:25 -07004160
4161 /**
Robert Greenwaltf57c03c2014-06-11 16:05:07 -07004162 * The lookup key for a {@link NetworkRequest} object included with the intent after
Jeremy Joslin60d379b2014-11-05 10:32:09 -08004163 * successfully finding a network for the applications request. Retrieve it with
Robert Greenwaltf3017f72014-05-18 23:07:25 -07004164 * {@link android.content.Intent#getParcelableExtra(String)}.
4165 */
Erik Kline8a826212014-11-19 12:12:24 +09004166 public static final String EXTRA_NETWORK_REQUEST = "android.net.extra.NETWORK_REQUEST";
Robert Greenwaltf3017f72014-05-18 23:07:25 -07004167
4168
4169 /**
Roshan Pius951c0032020-12-22 15:10:42 -08004170 * Request a network to satisfy a set of {@link NetworkCapabilities}.
Robert Greenwaltf99b8392014-03-26 16:47:06 -07004171 *
Jeremy Joslin60d379b2014-11-05 10:32:09 -08004172 * This function behaves identically to the version that takes a NetworkCallback, but instead
Robert Greenwaltf57c03c2014-06-11 16:05:07 -07004173 * of {@link NetworkCallback} a {@link PendingIntent} is used. This means
Robert Greenwaltf3017f72014-05-18 23:07:25 -07004174 * the request may outlive the calling application and get called back when a suitable
4175 * network is found.
Robert Greenwaltf99b8392014-03-26 16:47:06 -07004176 * <p>
4177 * The operation is an Intent broadcast that goes to a broadcast receiver that
4178 * you registered with {@link Context#registerReceiver} or through the
4179 * &lt;receiver&gt; tag in an AndroidManifest.xml file
4180 * <p>
4181 * The operation Intent is delivered with two extras, a {@link Network} typed
Erik Kline8a826212014-11-19 12:12:24 +09004182 * extra called {@link #EXTRA_NETWORK} and a {@link NetworkRequest}
4183 * typed extra called {@link #EXTRA_NETWORK_REQUEST} containing
Robert Greenwaltf99b8392014-03-26 16:47:06 -07004184 * the original requests parameters. It is important to create a new,
Robert Greenwaltf57c03c2014-06-11 16:05:07 -07004185 * {@link NetworkCallback} based request before completing the processing of the
Robert Greenwaltf99b8392014-03-26 16:47:06 -07004186 * Intent to reserve the network or it will be released shortly after the Intent
4187 * is processed.
4188 * <p>
Paul Jensenc8873fc2015-06-17 14:15:39 -04004189 * If there is already a request for this Intent registered (with the equality of
Robert Greenwaltf99b8392014-03-26 16:47:06 -07004190 * two Intents defined by {@link Intent#filterEquals}), then it will be removed and
Robert Greenwaltf3017f72014-05-18 23:07:25 -07004191 * replaced by this one, effectively releasing the previous {@link NetworkRequest}.
Robert Greenwaltf99b8392014-03-26 16:47:06 -07004192 * <p>
Jeremy Joslin60d379b2014-11-05 10:32:09 -08004193 * The request may be released normally by calling
4194 * {@link #releaseNetworkRequest(android.app.PendingIntent)}.
Paul Jensenee52d232015-06-16 15:11:58 -04004195 * <p>It is presently unsupported to request a network with either
4196 * {@link NetworkCapabilities#NET_CAPABILITY_VALIDATED} or
4197 * {@link NetworkCapabilities#NET_CAPABILITY_CAPTIVE_PORTAL}
4198 * as these {@code NetworkCapabilities} represent states that a particular
4199 * network may never attain, and whether a network will attain these states
4200 * is unknown prior to bringing up the network so the framework does not
Chalard Jean9dd11612018-06-04 16:52:49 +09004201 * know how to go about satisfying a request with these capabilities.
Lorenzo Colitti0b40c872015-10-15 16:29:00 +09004202 *
Chalard Jeana5ff1132020-05-20 16:11:50 +09004203 * <p>To avoid performance issues due to apps leaking callbacks, the system will limit the
4204 * number of outstanding requests to 100 per app (identified by their UID), shared with
4205 * all variants of this method, of {@link #registerNetworkCallback} as well as
4206 * {@link ConnectivityDiagnosticsManager#registerConnectivityDiagnosticsCallback}.
4207 * Requesting a network with this method will count toward this limit. If this limit is
4208 * exceeded, an exception will be thrown. To avoid hitting this issue and to conserve resources,
4209 * make sure to unregister the callbacks with {@link #unregisterNetworkCallback(PendingIntent)}
4210 * or {@link #releaseNetworkRequest(PendingIntent)}.
4211 *
Lorenzo Colitti0b40c872015-10-15 16:29:00 +09004212 * <p>This method requires the caller to hold either the
4213 * {@link android.Manifest.permission#CHANGE_NETWORK_STATE} permission
4214 * or the ability to modify system settings as determined by
4215 * {@link android.provider.Settings.System#canWrite}.</p>
4216 *
Robert Greenwaltf57c03c2014-06-11 16:05:07 -07004217 * @param request {@link NetworkRequest} describing this request.
Robert Greenwaltf99b8392014-03-26 16:47:06 -07004218 * @param operation Action to perform when the network is available (corresponds
Robert Greenwaltf57c03c2014-06-11 16:05:07 -07004219 * to the {@link NetworkCallback#onAvailable} call. Typically
Jeremy Joslin60d379b2014-11-05 10:32:09 -08004220 * comes from {@link PendingIntent#getBroadcast}. Cannot be null.
Chalard Jean31740e42019-05-13 15:13:58 +09004221 * @throws IllegalArgumentException if {@code request} contains invalid network capabilities.
4222 * @throws SecurityException if missing the appropriate permissions.
Chalard Jeana5ff1132020-05-20 16:11:50 +09004223 * @throws RuntimeException if the app already has too many callbacks registered.
Robert Greenwaltf99b8392014-03-26 16:47:06 -07004224 */
Chalard Jean158702d2019-01-07 19:26:34 +09004225 public void requestNetwork(@NonNull NetworkRequest request,
4226 @NonNull PendingIntent operation) {
Soi, Yoshinarib8102ef2015-11-12 12:09:02 +09004227 printStackTrace();
Hugo Benichibc1104b2017-05-09 15:19:01 +09004228 checkPendingIntentNotNull(operation);
Robert Greenwaltf99b8392014-03-26 16:47:06 -07004229 try {
Roshan Piusbc7e37d2020-01-16 12:17:17 -08004230 mService.pendingRequestForNetwork(
Philip P. Moltmann7bc33df2020-03-26 11:50:35 -07004231 request.networkCapabilities, operation, mContext.getOpPackageName(),
4232 getAttributionTag());
Jeff Sharkeyc78f85c2016-03-01 19:27:23 -07004233 } catch (RemoteException e) {
4234 throw e.rethrowFromSystemServer();
Hugo Benichia590ab82017-05-11 13:16:17 +09004235 } catch (ServiceSpecificException e) {
4236 throw convertServiceException(e);
Jeff Sharkeyc78f85c2016-03-01 19:27:23 -07004237 }
Robert Greenwaltf99b8392014-03-26 16:47:06 -07004238 }
4239
4240 /**
Jeremy Joslin60d379b2014-11-05 10:32:09 -08004241 * Removes a request made via {@link #requestNetwork(NetworkRequest, android.app.PendingIntent)}
4242 * <p>
Lorenzo Colitti15874cd2016-04-13 22:00:02 +09004243 * This method has the same behavior as
4244 * {@link #unregisterNetworkCallback(android.app.PendingIntent)} with respect to
Jeremy Joslin60d379b2014-11-05 10:32:09 -08004245 * releasing network resources and disconnecting.
4246 *
4247 * @param operation A PendingIntent equal (as defined by {@link Intent#filterEquals}) to the
4248 * PendingIntent passed to
4249 * {@link #requestNetwork(NetworkRequest, android.app.PendingIntent)} with the
4250 * corresponding NetworkRequest you'd like to remove. Cannot be null.
4251 */
Chalard Jean158702d2019-01-07 19:26:34 +09004252 public void releaseNetworkRequest(@NonNull PendingIntent operation) {
Soi, Yoshinarib8102ef2015-11-12 12:09:02 +09004253 printStackTrace();
Hugo Benichibc1104b2017-05-09 15:19:01 +09004254 checkPendingIntentNotNull(operation);
Jeremy Joslin60d379b2014-11-05 10:32:09 -08004255 try {
4256 mService.releasePendingNetworkRequest(operation);
Jeff Sharkeyc78f85c2016-03-01 19:27:23 -07004257 } catch (RemoteException e) {
4258 throw e.rethrowFromSystemServer();
4259 }
Jeremy Joslin60d379b2014-11-05 10:32:09 -08004260 }
4261
Hugo Benichibc1104b2017-05-09 15:19:01 +09004262 private static void checkPendingIntentNotNull(PendingIntent intent) {
Remi NGUYEN VAN81f68422021-03-15 07:31:54 +00004263 Objects.requireNonNull(intent, "PendingIntent cannot be null.");
Hugo Benichibc1104b2017-05-09 15:19:01 +09004264 }
4265
4266 private static void checkCallbackNotNull(NetworkCallback callback) {
Remi NGUYEN VAN81f68422021-03-15 07:31:54 +00004267 Objects.requireNonNull(callback, "null NetworkCallback");
Hugo Benichibc1104b2017-05-09 15:19:01 +09004268 }
4269
4270 private static void checkTimeout(int timeoutMs) {
Remi NGUYEN VAN81f68422021-03-15 07:31:54 +00004271 if (timeoutMs <= 0) {
4272 throw new IllegalArgumentException("timeoutMs must be strictly positive.");
4273 }
Jeremy Joslin60d379b2014-11-05 10:32:09 -08004274 }
4275
4276 /**
Robert Greenwaltf99b8392014-03-26 16:47:06 -07004277 * Registers to receive notifications about all networks which satisfy the given
Robert Greenwaltf57c03c2014-06-11 16:05:07 -07004278 * {@link NetworkRequest}. The callbacks will continue to be called until
Chiachang Wang3b723c22019-02-27 17:14:50 +08004279 * either the application exits or {@link #unregisterNetworkCallback(NetworkCallback)} is
4280 * called.
Robert Greenwaltf99b8392014-03-26 16:47:06 -07004281 *
Chalard Jeana5ff1132020-05-20 16:11:50 +09004282 * <p>To avoid performance issues due to apps leaking callbacks, the system will limit the
4283 * number of outstanding requests to 100 per app (identified by their UID), shared with
4284 * all variants of this method, of {@link #requestNetwork} as well as
4285 * {@link ConnectivityDiagnosticsManager#registerConnectivityDiagnosticsCallback}.
4286 * Requesting a network with this method will count toward this limit. If this limit is
4287 * exceeded, an exception will be thrown. To avoid hitting this issue and to conserve resources,
4288 * make sure to unregister the callbacks with
4289 * {@link #unregisterNetworkCallback(NetworkCallback)}.
4290 *
Robert Greenwaltf57c03c2014-06-11 16:05:07 -07004291 * @param request {@link NetworkRequest} describing this request.
4292 * @param networkCallback The {@link NetworkCallback} that the system will call as suitable
4293 * networks change state.
Hugo Benichifd44e912017-02-02 17:02:36 +09004294 * The callback is invoked on the default internal Handler.
Chalard Jeana5ff1132020-05-20 16:11:50 +09004295 * @throws RuntimeException if the app already has too many callbacks registered.
Robert Greenwaltf99b8392014-03-26 16:47:06 -07004296 */
Jeff Sharkey656584a2017-04-24 11:18:03 -06004297 @RequiresPermission(android.Manifest.permission.ACCESS_NETWORK_STATE)
Chalard Jean158702d2019-01-07 19:26:34 +09004298 public void registerNetworkCallback(@NonNull NetworkRequest request,
4299 @NonNull NetworkCallback networkCallback) {
Hugo Benichifd44e912017-02-02 17:02:36 +09004300 registerNetworkCallback(request, networkCallback, getDefaultHandler());
4301 }
4302
4303 /**
4304 * Registers to receive notifications about all networks which satisfy the given
4305 * {@link NetworkRequest}. The callbacks will continue to be called until
Chiachang Wang3b723c22019-02-27 17:14:50 +08004306 * either the application exits or {@link #unregisterNetworkCallback(NetworkCallback)} is
4307 * called.
Hugo Benichifd44e912017-02-02 17:02:36 +09004308 *
Chalard Jeana5ff1132020-05-20 16:11:50 +09004309 * <p>To avoid performance issues due to apps leaking callbacks, the system will limit the
4310 * number of outstanding requests to 100 per app (identified by their UID), shared with
4311 * all variants of this method, of {@link #requestNetwork} as well as
4312 * {@link ConnectivityDiagnosticsManager#registerConnectivityDiagnosticsCallback}.
4313 * Requesting a network with this method will count toward this limit. If this limit is
4314 * exceeded, an exception will be thrown. To avoid hitting this issue and to conserve resources,
4315 * make sure to unregister the callbacks with
4316 * {@link #unregisterNetworkCallback(NetworkCallback)}.
4317 *
4318 *
Hugo Benichifd44e912017-02-02 17:02:36 +09004319 * @param request {@link NetworkRequest} describing this request.
4320 * @param networkCallback The {@link NetworkCallback} that the system will call as suitable
4321 * networks change state.
4322 * @param handler {@link Handler} to specify the thread upon which the callback will be invoked.
Chalard Jeana5ff1132020-05-20 16:11:50 +09004323 * @throws RuntimeException if the app already has too many callbacks registered.
Hugo Benichifd44e912017-02-02 17:02:36 +09004324 */
Jeff Sharkey656584a2017-04-24 11:18:03 -06004325 @RequiresPermission(android.Manifest.permission.ACCESS_NETWORK_STATE)
Chalard Jean158702d2019-01-07 19:26:34 +09004326 public void registerNetworkCallback(@NonNull NetworkRequest request,
4327 @NonNull NetworkCallback networkCallback, @NonNull Handler handler) {
Hugo Benichifd44e912017-02-02 17:02:36 +09004328 CallbackHandler cbHandler = new CallbackHandler(handler);
4329 NetworkCapabilities nc = request.networkCapabilities;
4330 sendRequestForNetwork(nc, networkCallback, 0, LISTEN, TYPE_NONE, cbHandler);
Robert Greenwaltf99b8392014-03-26 16:47:06 -07004331 }
4332
4333 /**
Paul Jensenc8873fc2015-06-17 14:15:39 -04004334 * Registers a PendingIntent to be sent when a network is available which satisfies the given
4335 * {@link NetworkRequest}.
4336 *
4337 * This function behaves identically to the version that takes a NetworkCallback, but instead
4338 * of {@link NetworkCallback} a {@link PendingIntent} is used. This means
4339 * the request may outlive the calling application and get called back when a suitable
4340 * network is found.
4341 * <p>
4342 * The operation is an Intent broadcast that goes to a broadcast receiver that
4343 * you registered with {@link Context#registerReceiver} or through the
4344 * &lt;receiver&gt; tag in an AndroidManifest.xml file
4345 * <p>
4346 * The operation Intent is delivered with two extras, a {@link Network} typed
4347 * extra called {@link #EXTRA_NETWORK} and a {@link NetworkRequest}
4348 * typed extra called {@link #EXTRA_NETWORK_REQUEST} containing
4349 * the original requests parameters.
4350 * <p>
4351 * If there is already a request for this Intent registered (with the equality of
4352 * two Intents defined by {@link Intent#filterEquals}), then it will be removed and
4353 * replaced by this one, effectively releasing the previous {@link NetworkRequest}.
4354 * <p>
4355 * The request may be released normally by calling
Paul Jensen169f6622015-06-30 14:29:18 -04004356 * {@link #unregisterNetworkCallback(android.app.PendingIntent)}.
Chalard Jeana5ff1132020-05-20 16:11:50 +09004357 *
4358 * <p>To avoid performance issues due to apps leaking callbacks, the system will limit the
4359 * number of outstanding requests to 100 per app (identified by their UID), shared with
4360 * all variants of this method, of {@link #requestNetwork} as well as
4361 * {@link ConnectivityDiagnosticsManager#registerConnectivityDiagnosticsCallback}.
4362 * Requesting a network with this method will count toward this limit. If this limit is
4363 * exceeded, an exception will be thrown. To avoid hitting this issue and to conserve resources,
4364 * make sure to unregister the callbacks with {@link #unregisterNetworkCallback(PendingIntent)}
4365 * or {@link #releaseNetworkRequest(PendingIntent)}.
4366 *
Paul Jensenc8873fc2015-06-17 14:15:39 -04004367 * @param request {@link NetworkRequest} describing this request.
4368 * @param operation Action to perform when the network is available (corresponds
4369 * to the {@link NetworkCallback#onAvailable} call. Typically
4370 * comes from {@link PendingIntent#getBroadcast}. Cannot be null.
Chalard Jeana5ff1132020-05-20 16:11:50 +09004371 * @throws RuntimeException if the app already has too many callbacks registered.
Paul Jensenc8873fc2015-06-17 14:15:39 -04004372 */
Jeff Sharkey656584a2017-04-24 11:18:03 -06004373 @RequiresPermission(android.Manifest.permission.ACCESS_NETWORK_STATE)
Chalard Jean158702d2019-01-07 19:26:34 +09004374 public void registerNetworkCallback(@NonNull NetworkRequest request,
4375 @NonNull PendingIntent operation) {
Soi, Yoshinarib8102ef2015-11-12 12:09:02 +09004376 printStackTrace();
Hugo Benichibc1104b2017-05-09 15:19:01 +09004377 checkPendingIntentNotNull(operation);
Paul Jensenc8873fc2015-06-17 14:15:39 -04004378 try {
Roshan Piusbc7e37d2020-01-16 12:17:17 -08004379 mService.pendingListenForNetwork(
Roshan Piusaa24fde2020-12-17 14:53:09 -08004380 request.networkCapabilities, operation, mContext.getOpPackageName(),
4381 getAttributionTag());
Jeff Sharkeyc78f85c2016-03-01 19:27:23 -07004382 } catch (RemoteException e) {
4383 throw e.rethrowFromSystemServer();
Hugo Benichia590ab82017-05-11 13:16:17 +09004384 } catch (ServiceSpecificException e) {
4385 throw convertServiceException(e);
Jeff Sharkeyc78f85c2016-03-01 19:27:23 -07004386 }
Paul Jensenc8873fc2015-06-17 14:15:39 -04004387 }
4388
4389 /**
Lorenzo Colitti76b639e2021-01-29 20:14:04 +09004390 * Registers to receive notifications about changes in the application's default network. This
4391 * may be a physical network or a virtual network, such as a VPN that applies to the
4392 * application. The callbacks will continue to be called until either the application exits or
Lorenzo Colitti15874cd2016-04-13 22:00:02 +09004393 * {@link #unregisterNetworkCallback(NetworkCallback)} is called.
Erik Kline23bf99c2016-03-16 15:31:39 +09004394 *
Chalard Jeana5ff1132020-05-20 16:11:50 +09004395 * <p>To avoid performance issues due to apps leaking callbacks, the system will limit the
4396 * number of outstanding requests to 100 per app (identified by their UID), shared with
4397 * all variants of this method, of {@link #requestNetwork} as well as
4398 * {@link ConnectivityDiagnosticsManager#registerConnectivityDiagnosticsCallback}.
4399 * Requesting a network with this method will count toward this limit. If this limit is
4400 * exceeded, an exception will be thrown. To avoid hitting this issue and to conserve resources,
4401 * make sure to unregister the callbacks with
4402 * {@link #unregisterNetworkCallback(NetworkCallback)}.
4403 *
Erik Kline23bf99c2016-03-16 15:31:39 +09004404 * @param networkCallback The {@link NetworkCallback} that the system will call as the
Lorenzo Colitti76b639e2021-01-29 20:14:04 +09004405 * application's default network changes.
Hugo Benichifd44e912017-02-02 17:02:36 +09004406 * The callback is invoked on the default internal Handler.
Chalard Jeana5ff1132020-05-20 16:11:50 +09004407 * @throws RuntimeException if the app already has too many callbacks registered.
Erik Kline23bf99c2016-03-16 15:31:39 +09004408 */
Jeff Sharkey656584a2017-04-24 11:18:03 -06004409 @RequiresPermission(android.Manifest.permission.ACCESS_NETWORK_STATE)
Chalard Jean158702d2019-01-07 19:26:34 +09004410 public void registerDefaultNetworkCallback(@NonNull NetworkCallback networkCallback) {
Hugo Benichifd44e912017-02-02 17:02:36 +09004411 registerDefaultNetworkCallback(networkCallback, getDefaultHandler());
4412 }
4413
4414 /**
Lorenzo Colitti76b639e2021-01-29 20:14:04 +09004415 * Registers to receive notifications about changes in the application's default network. This
4416 * may be a physical network or a virtual network, such as a VPN that applies to the
4417 * application. The callbacks will continue to be called until either the application exits or
4418 * {@link #unregisterNetworkCallback(NetworkCallback)} is called.
4419 *
4420 * <p>To avoid performance issues due to apps leaking callbacks, the system will limit the
4421 * number of outstanding requests to 100 per app (identified by their UID), shared with
4422 * all variants of this method, of {@link #requestNetwork} as well as
4423 * {@link ConnectivityDiagnosticsManager#registerConnectivityDiagnosticsCallback}.
4424 * Requesting a network with this method will count toward this limit. If this limit is
4425 * exceeded, an exception will be thrown. To avoid hitting this issue and to conserve resources,
4426 * make sure to unregister the callbacks with
4427 * {@link #unregisterNetworkCallback(NetworkCallback)}.
4428 *
4429 * @param networkCallback The {@link NetworkCallback} that the system will call as the
4430 * application's default network changes.
4431 * @param handler {@link Handler} to specify the thread upon which the callback will be invoked.
4432 * @throws RuntimeException if the app already has too many callbacks registered.
4433 */
4434 @RequiresPermission(android.Manifest.permission.ACCESS_NETWORK_STATE)
4435 public void registerDefaultNetworkCallback(@NonNull NetworkCallback networkCallback,
4436 @NonNull Handler handler) {
Chiachang Wang6ec9b8d2021-04-20 15:41:24 +08004437 registerDefaultNetworkCallbackForUid(Process.INVALID_UID, networkCallback, handler);
Lorenzo Colitti2a49c5d2021-03-12 22:48:07 +09004438 }
4439
4440 /**
4441 * Registers to receive notifications about changes in the default network for the specified
4442 * UID. This may be a physical network or a virtual network, such as a VPN that applies to the
4443 * UID. The callbacks will continue to be called until either the application exits or
4444 * {@link #unregisterNetworkCallback(NetworkCallback)} is called.
4445 *
4446 * <p>To avoid performance issues due to apps leaking callbacks, the system will limit the
4447 * number of outstanding requests to 100 per app (identified by their UID), shared with
4448 * all variants of this method, of {@link #requestNetwork} as well as
4449 * {@link ConnectivityDiagnosticsManager#registerConnectivityDiagnosticsCallback}.
4450 * Requesting a network with this method will count toward this limit. If this limit is
4451 * exceeded, an exception will be thrown. To avoid hitting this issue and to conserve resources,
4452 * make sure to unregister the callbacks with
4453 * {@link #unregisterNetworkCallback(NetworkCallback)}.
4454 *
4455 * @param uid the UID for which to track default network changes.
4456 * @param networkCallback The {@link NetworkCallback} that the system will call as the
4457 * UID's default network changes.
4458 * @param handler {@link Handler} to specify the thread upon which the callback will be invoked.
4459 * @throws RuntimeException if the app already has too many callbacks registered.
4460 * @hide
4461 */
Lorenzo Colitti92ed8b92021-03-22 18:23:21 +09004462 @SystemApi(client = MODULE_LIBRARIES)
Lorenzo Colitti2a49c5d2021-03-12 22:48:07 +09004463 @SuppressLint({"ExecutorRegistration", "PairedRegistration"})
4464 @RequiresPermission(anyOf = {
4465 NetworkStack.PERMISSION_MAINLINE_NETWORK_STACK,
4466 android.Manifest.permission.NETWORK_SETTINGS})
Chiachang Wang6ec9b8d2021-04-20 15:41:24 +08004467 public void registerDefaultNetworkCallbackForUid(int uid,
Lorenzo Colitti2a49c5d2021-03-12 22:48:07 +09004468 @NonNull NetworkCallback networkCallback, @NonNull Handler handler) {
Lorenzo Colitti76b639e2021-01-29 20:14:04 +09004469 CallbackHandler cbHandler = new CallbackHandler(handler);
Lorenzo Colitti2a49c5d2021-03-12 22:48:07 +09004470 sendRequestForNetwork(uid, null /* need */, networkCallback, 0 /* timeoutMs */,
Lorenzo Colitti76b639e2021-01-29 20:14:04 +09004471 TRACK_DEFAULT, TYPE_NONE, cbHandler);
4472 }
4473
4474 /**
Hugo Benichifd44e912017-02-02 17:02:36 +09004475 * Registers to receive notifications about changes in the system default network. The callbacks
4476 * will continue to be called until either the application exits or
4477 * {@link #unregisterNetworkCallback(NetworkCallback)} is called.
Hugo Benichifd44e912017-02-02 17:02:36 +09004478 *
Lorenzo Colitti76b639e2021-01-29 20:14:04 +09004479 * This method should not be used to determine networking state seen by applications, because in
4480 * many cases, most or even all application traffic may not use the default network directly,
4481 * and traffic from different applications may go on different networks by default. As an
4482 * example, if a VPN is connected, traffic from all applications might be sent through the VPN
4483 * and not onto the system default network. Applications or system components desiring to do
4484 * determine network state as seen by applications should use other methods such as
4485 * {@link #registerDefaultNetworkCallback(NetworkCallback, Handler)}.
4486 *
Chalard Jeana5ff1132020-05-20 16:11:50 +09004487 * <p>To avoid performance issues due to apps leaking callbacks, the system will limit the
4488 * number of outstanding requests to 100 per app (identified by their UID), shared with
4489 * all variants of this method, of {@link #requestNetwork} as well as
4490 * {@link ConnectivityDiagnosticsManager#registerConnectivityDiagnosticsCallback}.
4491 * Requesting a network with this method will count toward this limit. If this limit is
4492 * exceeded, an exception will be thrown. To avoid hitting this issue and to conserve resources,
4493 * make sure to unregister the callbacks with
4494 * {@link #unregisterNetworkCallback(NetworkCallback)}.
4495 *
Hugo Benichifd44e912017-02-02 17:02:36 +09004496 * @param networkCallback The {@link NetworkCallback} that the system will call as the
4497 * system default network changes.
4498 * @param handler {@link Handler} to specify the thread upon which the callback will be invoked.
Chalard Jeana5ff1132020-05-20 16:11:50 +09004499 * @throws RuntimeException if the app already has too many callbacks registered.
Lorenzo Colitti76b639e2021-01-29 20:14:04 +09004500 *
4501 * @hide
Hugo Benichifd44e912017-02-02 17:02:36 +09004502 */
Lorenzo Colitti76b639e2021-01-29 20:14:04 +09004503 @SystemApi(client = MODULE_LIBRARIES)
4504 @SuppressLint({"ExecutorRegistration", "PairedRegistration"})
4505 @RequiresPermission(anyOf = {
4506 NetworkStack.PERMISSION_MAINLINE_NETWORK_STACK,
4507 android.Manifest.permission.NETWORK_SETTINGS})
4508 public void registerSystemDefaultNetworkCallback(@NonNull NetworkCallback networkCallback,
Chalard Jean158702d2019-01-07 19:26:34 +09004509 @NonNull Handler handler) {
Hugo Benichifd44e912017-02-02 17:02:36 +09004510 CallbackHandler cbHandler = new CallbackHandler(handler);
Chalard Jean9dd11612018-06-04 16:52:49 +09004511 sendRequestForNetwork(null /* NetworkCapabilities need */, networkCallback, 0,
Lorenzo Colitti76b639e2021-01-29 20:14:04 +09004512 TRACK_SYSTEM_DEFAULT, TYPE_NONE, cbHandler);
Erik Kline23bf99c2016-03-16 15:31:39 +09004513 }
4514
4515 /**
junyulai8eb13a22021-03-15 11:48:48 +08004516 * Registers to receive notifications about the best matching network which satisfy the given
4517 * {@link NetworkRequest}. The callbacks will continue to be called until
4518 * either the application exits or {@link #unregisterNetworkCallback(NetworkCallback)} is
4519 * called.
4520 *
4521 * <p>To avoid performance issues due to apps leaking callbacks, the system will limit the
4522 * number of outstanding requests to 100 per app (identified by their UID), shared with
4523 * {@link #registerNetworkCallback} and its variants and {@link #requestNetwork} as well as
4524 * {@link ConnectivityDiagnosticsManager#registerConnectivityDiagnosticsCallback}.
4525 * Requesting a network with this method will count toward this limit. If this limit is
4526 * exceeded, an exception will be thrown. To avoid hitting this issue and to conserve resources,
4527 * make sure to unregister the callbacks with
4528 * {@link #unregisterNetworkCallback(NetworkCallback)}.
4529 *
4530 *
4531 * @param request {@link NetworkRequest} describing this request.
4532 * @param networkCallback The {@link NetworkCallback} that the system will call as suitable
4533 * networks change state.
4534 * @param handler {@link Handler} to specify the thread upon which the callback will be invoked.
4535 * @throws RuntimeException if the app already has too many callbacks registered.
junyulai7514e312021-03-05 15:51:17 +08004536 */
junyulai7514e312021-03-05 15:51:17 +08004537 @SuppressLint("ExecutorRegistration")
4538 public void registerBestMatchingNetworkCallback(@NonNull NetworkRequest request,
4539 @NonNull NetworkCallback networkCallback, @NonNull Handler handler) {
4540 final NetworkCapabilities nc = request.networkCapabilities;
4541 final CallbackHandler cbHandler = new CallbackHandler(handler);
junyulai1b1c8742021-03-12 20:05:08 +08004542 sendRequestForNetwork(nc, networkCallback, 0, LISTEN_FOR_BEST, TYPE_NONE, cbHandler);
junyulai7514e312021-03-05 15:51:17 +08004543 }
4544
4545 /**
fenglub00f4882015-04-21 17:12:05 -07004546 * Requests bandwidth update for a given {@link Network} and returns whether the update request
4547 * is accepted by ConnectivityService. Once accepted, ConnectivityService will poll underlying
4548 * network connection for updated bandwidth information. The caller will be notified via
4549 * {@link ConnectivityManager.NetworkCallback} if there is an update. Notice that this
Lorenzo Colitti15874cd2016-04-13 22:00:02 +09004550 * method assumes that the caller has previously called
4551 * {@link #registerNetworkCallback(NetworkRequest, NetworkCallback)} to listen for network
4552 * changes.
fenglu3f357402015-03-20 11:29:56 -07004553 *
fengluea2d9282015-04-27 14:28:04 -07004554 * @param network {@link Network} specifying which network you're interested.
fenglub00f4882015-04-21 17:12:05 -07004555 * @return {@code true} on success, {@code false} if the {@link Network} is no longer valid.
fenglu3f357402015-03-20 11:29:56 -07004556 */
Chalard Jean158702d2019-01-07 19:26:34 +09004557 public boolean requestBandwidthUpdate(@NonNull Network network) {
fenglu3f357402015-03-20 11:29:56 -07004558 try {
fenglub00f4882015-04-21 17:12:05 -07004559 return mService.requestBandwidthUpdate(network);
fenglu3f357402015-03-20 11:29:56 -07004560 } catch (RemoteException e) {
Jeff Sharkeyc78f85c2016-03-01 19:27:23 -07004561 throw e.rethrowFromSystemServer();
fenglu3f357402015-03-20 11:29:56 -07004562 }
4563 }
4564
4565 /**
Hugo Benichi2aa65af2017-03-06 09:17:06 +09004566 * Unregisters a {@code NetworkCallback} and possibly releases networks originating from
Lorenzo Colitti15874cd2016-04-13 22:00:02 +09004567 * {@link #requestNetwork(NetworkRequest, NetworkCallback)} and
4568 * {@link #registerNetworkCallback(NetworkRequest, NetworkCallback)} calls.
4569 * If the given {@code NetworkCallback} had previously been used with
Lorenzo Colitti7f6a2bf2015-04-24 17:03:31 +09004570 * {@code #requestNetwork}, any networks that had been connected to only to satisfy that request
4571 * will be disconnected.
Robert Greenwaltf99b8392014-03-26 16:47:06 -07004572 *
Hugo Benichi2aa65af2017-03-06 09:17:06 +09004573 * Notifications that would have triggered that {@code NetworkCallback} will immediately stop
4574 * triggering it as soon as this call returns.
4575 *
Robert Greenwaltf57c03c2014-06-11 16:05:07 -07004576 * @param networkCallback The {@link NetworkCallback} used when making the request.
Robert Greenwaltf99b8392014-03-26 16:47:06 -07004577 */
Chalard Jean158702d2019-01-07 19:26:34 +09004578 public void unregisterNetworkCallback(@NonNull NetworkCallback networkCallback) {
Soi, Yoshinarib8102ef2015-11-12 12:09:02 +09004579 printStackTrace();
Hugo Benichibc1104b2017-05-09 15:19:01 +09004580 checkCallbackNotNull(networkCallback);
Hugo Benichi2aa65af2017-03-06 09:17:06 +09004581 final List<NetworkRequest> reqs = new ArrayList<>();
4582 // Find all requests associated to this callback and stop callback triggers immediately.
4583 // Callback is reusable immediately. http://b/20701525, http://b/35921499.
4584 synchronized (sCallbacks) {
Remi NGUYEN VAN81f68422021-03-15 07:31:54 +00004585 if (networkCallback.networkRequest == null) {
4586 throw new IllegalArgumentException("NetworkCallback was not registered");
4587 }
Etan Cohen6cb65992019-04-16 15:07:55 -07004588 if (networkCallback.networkRequest == ALREADY_UNREGISTERED) {
4589 Log.d(TAG, "NetworkCallback was already unregistered");
4590 return;
4591 }
Hugo Benichi2aa65af2017-03-06 09:17:06 +09004592 for (Map.Entry<NetworkRequest, NetworkCallback> e : sCallbacks.entrySet()) {
4593 if (e.getValue() == networkCallback) {
4594 reqs.add(e.getKey());
4595 }
4596 }
4597 // TODO: throw exception if callback was registered more than once (http://b/20701525).
4598 for (NetworkRequest r : reqs) {
4599 try {
4600 mService.releaseNetworkRequest(r);
4601 } catch (RemoteException e) {
4602 throw e.rethrowFromSystemServer();
4603 }
4604 // Only remove mapping if rpc was successful.
4605 sCallbacks.remove(r);
4606 }
Hugo Benichibee30fe2017-06-17 13:14:12 +09004607 networkCallback.networkRequest = ALREADY_UNREGISTERED;
Jeff Sharkeyc78f85c2016-03-01 19:27:23 -07004608 }
Robert Greenwaltf99b8392014-03-26 16:47:06 -07004609 }
Paul Jensen8cdda642014-05-29 10:12:39 -04004610
4611 /**
Paul Jensen169f6622015-06-30 14:29:18 -04004612 * Unregisters a callback previously registered via
4613 * {@link #registerNetworkCallback(NetworkRequest, android.app.PendingIntent)}.
4614 *
4615 * @param operation A PendingIntent equal (as defined by {@link Intent#filterEquals}) to the
4616 * PendingIntent passed to
4617 * {@link #registerNetworkCallback(NetworkRequest, android.app.PendingIntent)}.
4618 * Cannot be null.
4619 */
Chalard Jean158702d2019-01-07 19:26:34 +09004620 public void unregisterNetworkCallback(@NonNull PendingIntent operation) {
Paul Jensen169f6622015-06-30 14:29:18 -04004621 releaseNetworkRequest(operation);
4622 }
4623
4624 /**
Lorenzo Colitti6947c062015-04-03 16:38:52 +09004625 * Informs the system whether it should switch to {@code network} regardless of whether it is
4626 * validated or not. If {@code accept} is true, and the network was explicitly selected by the
4627 * user (e.g., by selecting a Wi-Fi network in the Settings app), then the network will become
4628 * the system default network regardless of any other network that's currently connected. If
4629 * {@code always} is true, then the choice is remembered, so that the next time the user
4630 * connects to this network, the system will switch to it.
4631 *
Lorenzo Colitti6947c062015-04-03 16:38:52 +09004632 * @param network The network to accept.
4633 * @param accept Whether to accept the network even if unvalidated.
4634 * @param always Whether to remember this choice in the future.
4635 *
4636 * @hide
4637 */
Chiachang Wang4d513572021-03-18 09:44:34 +08004638 @SystemApi(client = MODULE_LIBRARIES)
4639 @RequiresPermission(anyOf = {
4640 android.Manifest.permission.NETWORK_SETTINGS,
4641 android.Manifest.permission.NETWORK_SETUP_WIZARD,
4642 android.Manifest.permission.NETWORK_STACK,
4643 NetworkStack.PERMISSION_MAINLINE_NETWORK_STACK})
4644 public void setAcceptUnvalidated(@NonNull Network network, boolean accept, boolean always) {
Lorenzo Colitti6947c062015-04-03 16:38:52 +09004645 try {
4646 mService.setAcceptUnvalidated(network, accept, always);
Jeff Sharkeyc78f85c2016-03-01 19:27:23 -07004647 } catch (RemoteException e) {
4648 throw e.rethrowFromSystemServer();
4649 }
Lorenzo Colitti6947c062015-04-03 16:38:52 +09004650 }
4651
4652 /**
lucaslin2240ef62019-03-12 13:08:03 +08004653 * Informs the system whether it should consider the network as validated even if it only has
4654 * partial connectivity. If {@code accept} is true, then the network will be considered as
4655 * validated even if connectivity is only partial. If {@code always} is true, then the choice
4656 * is remembered, so that the next time the user connects to this network, the system will
4657 * switch to it.
4658 *
4659 * @param network The network to accept.
4660 * @param accept Whether to consider the network as validated even if it has partial
4661 * connectivity.
4662 * @param always Whether to remember this choice in the future.
4663 *
4664 * @hide
4665 */
Chiachang Wang4d513572021-03-18 09:44:34 +08004666 @SystemApi(client = MODULE_LIBRARIES)
4667 @RequiresPermission(anyOf = {
4668 android.Manifest.permission.NETWORK_SETTINGS,
4669 android.Manifest.permission.NETWORK_SETUP_WIZARD,
4670 android.Manifest.permission.NETWORK_STACK,
4671 NetworkStack.PERMISSION_MAINLINE_NETWORK_STACK})
4672 public void setAcceptPartialConnectivity(@NonNull Network network, boolean accept,
4673 boolean always) {
lucaslin2240ef62019-03-12 13:08:03 +08004674 try {
4675 mService.setAcceptPartialConnectivity(network, accept, always);
4676 } catch (RemoteException e) {
4677 throw e.rethrowFromSystemServer();
4678 }
4679 }
4680
4681 /**
Lorenzo Colittic65750c2016-09-19 01:00:19 +09004682 * Informs the system to penalize {@code network}'s score when it becomes unvalidated. This is
4683 * only meaningful if the system is configured not to penalize such networks, e.g., if the
4684 * {@code config_networkAvoidBadWifi} configuration variable is set to 0 and the {@code
4685 * NETWORK_AVOID_BAD_WIFI setting is unset}.
4686 *
Lorenzo Colittic65750c2016-09-19 01:00:19 +09004687 * @param network The network to accept.
4688 *
4689 * @hide
4690 */
Chiachang Wang4d513572021-03-18 09:44:34 +08004691 @SystemApi(client = MODULE_LIBRARIES)
4692 @RequiresPermission(anyOf = {
4693 android.Manifest.permission.NETWORK_SETTINGS,
4694 android.Manifest.permission.NETWORK_SETUP_WIZARD,
4695 android.Manifest.permission.NETWORK_STACK,
4696 NetworkStack.PERMISSION_MAINLINE_NETWORK_STACK})
4697 public void setAvoidUnvalidated(@NonNull Network network) {
Lorenzo Colittic65750c2016-09-19 01:00:19 +09004698 try {
4699 mService.setAvoidUnvalidated(network);
4700 } catch (RemoteException e) {
4701 throw e.rethrowFromSystemServer();
4702 }
4703 }
4704
4705 /**
Lorenzo Colitti500dbae2017-04-27 14:30:21 +09004706 * Requests that the system open the captive portal app on the specified network.
4707 *
Remi NGUYEN VAN4cf96ab2021-03-16 18:06:06 +09004708 * <p>This is to be used on networks where a captive portal was detected, as per
4709 * {@link NetworkCapabilities#NET_CAPABILITY_CAPTIVE_PORTAL}.
4710 *
Lorenzo Colitti500dbae2017-04-27 14:30:21 +09004711 * @param network The network to log into.
4712 *
4713 * @hide
4714 */
Remi NGUYEN VAN4cf96ab2021-03-16 18:06:06 +09004715 @SystemApi(client = SystemApi.Client.MODULE_LIBRARIES)
4716 @RequiresPermission(anyOf = {
4717 android.Manifest.permission.NETWORK_SETTINGS,
4718 android.Manifest.permission.NETWORK_STACK,
4719 NetworkStack.PERMISSION_MAINLINE_NETWORK_STACK
4720 })
4721 public void startCaptivePortalApp(@NonNull Network network) {
Lorenzo Colitti500dbae2017-04-27 14:30:21 +09004722 try {
4723 mService.startCaptivePortalApp(network);
4724 } catch (RemoteException e) {
4725 throw e.rethrowFromSystemServer();
4726 }
4727 }
4728
4729 /**
Remi NGUYEN VAN94ff95b2019-02-04 11:32:20 +09004730 * Requests that the system open the captive portal app with the specified extras.
4731 *
4732 * <p>This endpoint is exclusively for use by the NetworkStack and is protected by the
4733 * corresponding permission.
Remi NGUYEN VANeab0f542019-02-13 20:58:59 +09004734 * @param network Network on which the captive portal was detected.
Remi NGUYEN VAN94ff95b2019-02-04 11:32:20 +09004735 * @param appExtras Extras to include in the app start intent.
4736 * @hide
4737 */
4738 @SystemApi
Remi NGUYEN VAN94ff95b2019-02-04 11:32:20 +09004739 @RequiresPermission(NetworkStack.PERMISSION_MAINLINE_NETWORK_STACK)
paulhucbbc3db2019-03-08 16:35:20 +08004740 public void startCaptivePortalApp(@NonNull Network network, @NonNull Bundle appExtras) {
Remi NGUYEN VAN94ff95b2019-02-04 11:32:20 +09004741 try {
Remi NGUYEN VANeab0f542019-02-13 20:58:59 +09004742 mService.startCaptivePortalAppInternal(network, appExtras);
Remi NGUYEN VAN94ff95b2019-02-04 11:32:20 +09004743 } catch (RemoteException e) {
4744 throw e.rethrowFromSystemServer();
4745 }
4746 }
4747
4748 /**
Remi NGUYEN VAN27de63e2019-01-20 20:35:06 +09004749 * Determine whether the device is configured to avoid bad wifi.
4750 * @hide
4751 */
4752 @SystemApi
Remi NGUYEN VAN1fb7cab2019-03-22 11:14:13 +09004753 @RequiresPermission(anyOf = {
4754 NetworkStack.PERMISSION_MAINLINE_NETWORK_STACK,
4755 android.Manifest.permission.NETWORK_STACK})
4756 public boolean shouldAvoidBadWifi() {
Remi NGUYEN VAN27de63e2019-01-20 20:35:06 +09004757 try {
Remi NGUYEN VAN1fb7cab2019-03-22 11:14:13 +09004758 return mService.shouldAvoidBadWifi();
Remi NGUYEN VAN27de63e2019-01-20 20:35:06 +09004759 } catch (RemoteException e) {
4760 throw e.rethrowFromSystemServer();
4761 }
4762 }
4763
4764 /**
Lorenzo Colitti48a2a322017-01-24 18:08:41 +09004765 * It is acceptable to briefly use multipath data to provide seamless connectivity for
4766 * time-sensitive user-facing operations when the system default network is temporarily
Lorenzo Colitti139a48c2017-04-28 00:56:30 +09004767 * unresponsive. The amount of data should be limited (less than one megabyte for every call to
4768 * this method), and the operation should be infrequent to ensure that data usage is limited.
Lorenzo Colitti48a2a322017-01-24 18:08:41 +09004769 *
4770 * An example of such an operation might be a time-sensitive foreground activity, such as a
4771 * voice command, that the user is performing while walking out of range of a Wi-Fi network.
4772 */
4773 public static final int MULTIPATH_PREFERENCE_HANDOVER = 1 << 0;
4774
4775 /**
4776 * It is acceptable to use small amounts of multipath data on an ongoing basis to provide
4777 * a backup channel for traffic that is primarily going over another network.
4778 *
4779 * An example might be maintaining backup connections to peers or servers for the purpose of
4780 * fast fallback if the default network is temporarily unresponsive or disconnects. The traffic
4781 * on backup paths should be negligible compared to the traffic on the main path.
4782 */
4783 public static final int MULTIPATH_PREFERENCE_RELIABILITY = 1 << 1;
4784
4785 /**
4786 * It is acceptable to use metered data to improve network latency and performance.
4787 */
4788 public static final int MULTIPATH_PREFERENCE_PERFORMANCE = 1 << 2;
4789
4790 /**
4791 * Return value to use for unmetered networks. On such networks we currently set all the flags
4792 * to true.
4793 * @hide
4794 */
4795 public static final int MULTIPATH_PREFERENCE_UNMETERED =
4796 MULTIPATH_PREFERENCE_HANDOVER |
4797 MULTIPATH_PREFERENCE_RELIABILITY |
4798 MULTIPATH_PREFERENCE_PERFORMANCE;
4799
4800 /** @hide */
4801 @Retention(RetentionPolicy.SOURCE)
4802 @IntDef(flag = true, value = {
4803 MULTIPATH_PREFERENCE_HANDOVER,
4804 MULTIPATH_PREFERENCE_RELIABILITY,
4805 MULTIPATH_PREFERENCE_PERFORMANCE,
4806 })
4807 public @interface MultipathPreference {
4808 }
4809
4810 /**
4811 * Provides a hint to the calling application on whether it is desirable to use the
4812 * multinetwork APIs (e.g., {@link Network#openConnection}, {@link Network#bindSocket}, etc.)
4813 * for multipath data transfer on this network when it is not the system default network.
4814 * Applications desiring to use multipath network protocols should call this method before
4815 * each such operation.
Lorenzo Colitti48a2a322017-01-24 18:08:41 +09004816 *
4817 * @param network The network on which the application desires to use multipath data.
4818 * If {@code null}, this method will return the a preference that will generally
4819 * apply to metered networks.
4820 * @return a bitwise OR of zero or more of the {@code MULTIPATH_PREFERENCE_*} constants.
4821 */
Jeff Sharkey656584a2017-04-24 11:18:03 -06004822 @RequiresPermission(android.Manifest.permission.ACCESS_NETWORK_STATE)
Chalard Jean158702d2019-01-07 19:26:34 +09004823 public @MultipathPreference int getMultipathPreference(@Nullable Network network) {
Lorenzo Colitti48a2a322017-01-24 18:08:41 +09004824 try {
4825 return mService.getMultipathPreference(network);
4826 } catch (RemoteException e) {
4827 throw e.rethrowFromSystemServer();
4828 }
4829 }
4830
4831 /**
Stuart Scott0f835ac2015-03-30 13:17:11 -07004832 * Resets all connectivity manager settings back to factory defaults.
4833 * @hide
4834 */
Chiachang Wang4d513572021-03-18 09:44:34 +08004835 @SystemApi(client = MODULE_LIBRARIES)
4836 @RequiresPermission(anyOf = {
4837 android.Manifest.permission.NETWORK_SETTINGS,
4838 NetworkStack.PERMISSION_MAINLINE_NETWORK_STACK})
Stuart Scott0f835ac2015-03-30 13:17:11 -07004839 public void factoryReset() {
Stuart Scott0f835ac2015-03-30 13:17:11 -07004840 try {
Stuart Scottd5463642015-04-02 18:00:02 -07004841 mService.factoryReset();
Amos Bianchia9b415a2020-03-04 11:07:38 -08004842 mTetheringManager.stopAllTethering();
Stuart Scott0f835ac2015-03-30 13:17:11 -07004843 } catch (RemoteException e) {
Jeff Sharkeyc78f85c2016-03-01 19:27:23 -07004844 throw e.rethrowFromSystemServer();
Stuart Scott0f835ac2015-03-30 13:17:11 -07004845 }
4846 }
4847
4848 /**
Paul Jensen8cdda642014-05-29 10:12:39 -04004849 * Binds the current process to {@code network}. All Sockets created in the future
4850 * (and not explicitly bound via a bound SocketFactory from
4851 * {@link Network#getSocketFactory() Network.getSocketFactory()}) will be bound to
4852 * {@code network}. All host name resolutions will be limited to {@code network} as well.
4853 * Note that if {@code network} ever disconnects, all Sockets created in this way will cease to
4854 * work and all host name resolutions will fail. This is by design so an application doesn't
4855 * accidentally use Sockets it thinks are still bound to a particular {@link Network}.
4856 * To clear binding pass {@code null} for {@code network}. Using individually bound
4857 * Sockets created by Network.getSocketFactory().createSocket() and
4858 * performing network-specific host name resolutions via
4859 * {@link Network#getAllByName Network.getAllByName} is preferred to calling
Paul Jensenee2f45d2015-03-10 10:54:12 -04004860 * {@code bindProcessToNetwork}.
Paul Jensen8cdda642014-05-29 10:12:39 -04004861 *
4862 * @param network The {@link Network} to bind the current process to, or {@code null} to clear
4863 * the current binding.
4864 * @return {@code true} on success, {@code false} if the {@link Network} is no longer valid.
4865 */
Chalard Jean158702d2019-01-07 19:26:34 +09004866 public boolean bindProcessToNetwork(@Nullable Network network) {
Chalard Jean9dd11612018-06-04 16:52:49 +09004867 // Forcing callers to call through non-static function ensures ConnectivityManager
Paul Jensenee2f45d2015-03-10 10:54:12 -04004868 // instantiated.
4869 return setProcessDefaultNetwork(network);
4870 }
4871
4872 /**
4873 * Binds the current process to {@code network}. All Sockets created in the future
4874 * (and not explicitly bound via a bound SocketFactory from
4875 * {@link Network#getSocketFactory() Network.getSocketFactory()}) will be bound to
4876 * {@code network}. All host name resolutions will be limited to {@code network} as well.
4877 * Note that if {@code network} ever disconnects, all Sockets created in this way will cease to
4878 * work and all host name resolutions will fail. This is by design so an application doesn't
4879 * accidentally use Sockets it thinks are still bound to a particular {@link Network}.
4880 * To clear binding pass {@code null} for {@code network}. Using individually bound
4881 * Sockets created by Network.getSocketFactory().createSocket() and
4882 * performing network-specific host name resolutions via
4883 * {@link Network#getAllByName Network.getAllByName} is preferred to calling
4884 * {@code setProcessDefaultNetwork}.
4885 *
4886 * @param network The {@link Network} to bind the current process to, or {@code null} to clear
4887 * the current binding.
4888 * @return {@code true} on success, {@code false} if the {@link Network} is no longer valid.
4889 * @deprecated This function can throw {@link IllegalStateException}. Use
4890 * {@link #bindProcessToNetwork} instead. {@code bindProcessToNetwork}
4891 * is a direct replacement.
4892 */
Aurimas Liutikase80a0eb2016-05-24 15:22:55 -07004893 @Deprecated
Chalard Jean158702d2019-01-07 19:26:34 +09004894 public static boolean setProcessDefaultNetwork(@Nullable Network network) {
Paul Jensen3e2917c2014-08-27 12:38:45 -04004895 int netId = (network == null) ? NETID_UNSET : network.netId;
Lorenzo Colitti3c766eb2019-01-31 13:08:24 +09004896 boolean isSameNetId = (netId == NetworkUtils.getBoundNetworkForProcess());
4897
Lorenzo Colitti3a1cb9d2019-01-30 23:04:54 +09004898 if (netId != NETID_UNSET) {
4899 netId = network.getNetIdForResolv();
Paul Jensen3e2917c2014-08-27 12:38:45 -04004900 }
Lorenzo Colitti3c766eb2019-01-31 13:08:24 +09004901
4902 if (!NetworkUtils.bindProcessToNetwork(netId)) {
4903 return false;
4904 }
4905
4906 if (!isSameNetId) {
Paul Jensenc0618a62014-12-10 15:12:18 -05004907 // Set HTTP proxy system properties to match network.
4908 // TODO: Deprecate this static method and replace it with a non-static version.
Lorenzo Colitti41b1fbc2015-04-22 11:52:48 +09004909 try {
Remi NGUYEN VANb33335c2021-02-03 10:18:20 +09004910 Proxy.setHttpProxyConfiguration(getInstance().getDefaultProxy());
Lorenzo Colitti41b1fbc2015-04-22 11:52:48 +09004911 } catch (SecurityException e) {
4912 // The process doesn't have ACCESS_NETWORK_STATE, so we can't fetch the proxy.
4913 Log.e(TAG, "Can't set proxy properties", e);
4914 }
Paul Jensen3e2917c2014-08-27 12:38:45 -04004915 // Must flush DNS cache as new network may have different DNS resolutions.
Remi NGUYEN VANe1b04f62021-03-18 23:27:19 +09004916 InetAddressCompat.clearDnsCache();
Paul Jensen3e2917c2014-08-27 12:38:45 -04004917 // Must flush socket pool as idle sockets will be bound to previous network and may
4918 // cause subsequent fetches to be performed on old network.
4919 NetworkEventDispatcher.getInstance().onNetworkConfigurationChanged();
Paul Jensen3e2917c2014-08-27 12:38:45 -04004920 }
Lorenzo Colitti3c766eb2019-01-31 13:08:24 +09004921
4922 return true;
Paul Jensen8cdda642014-05-29 10:12:39 -04004923 }
4924
4925 /**
4926 * Returns the {@link Network} currently bound to this process via
Paul Jensenee2f45d2015-03-10 10:54:12 -04004927 * {@link #bindProcessToNetwork}, or {@code null} if no {@link Network} is explicitly bound.
Paul Jensen8cdda642014-05-29 10:12:39 -04004928 *
4929 * @return {@code Network} to which this process is bound, or {@code null}.
4930 */
Chalard Jean158702d2019-01-07 19:26:34 +09004931 @Nullable
Paul Jensenee2f45d2015-03-10 10:54:12 -04004932 public Network getBoundNetworkForProcess() {
4933 // Forcing callers to call thru non-static function ensures ConnectivityManager
4934 // instantiated.
4935 return getProcessDefaultNetwork();
4936 }
4937
4938 /**
4939 * Returns the {@link Network} currently bound to this process via
4940 * {@link #bindProcessToNetwork}, or {@code null} if no {@link Network} is explicitly bound.
4941 *
4942 * @return {@code Network} to which this process is bound, or {@code null}.
4943 * @deprecated Using this function can lead to other functions throwing
4944 * {@link IllegalStateException}. Use {@link #getBoundNetworkForProcess} instead.
4945 * {@code getBoundNetworkForProcess} is a direct replacement.
4946 */
Aurimas Liutikase80a0eb2016-05-24 15:22:55 -07004947 @Deprecated
Chalard Jean158702d2019-01-07 19:26:34 +09004948 @Nullable
Paul Jensen8cdda642014-05-29 10:12:39 -04004949 public static Network getProcessDefaultNetwork() {
Paul Jensenee2f45d2015-03-10 10:54:12 -04004950 int netId = NetworkUtils.getBoundNetworkForProcess();
Paul Jensen65743a22014-07-11 08:17:29 -04004951 if (netId == NETID_UNSET) return null;
Paul Jensen8cdda642014-05-29 10:12:39 -04004952 return new Network(netId);
4953 }
4954
Lorenzo Colitti2780e4b2015-07-29 11:41:21 +09004955 private void unsupportedStartingFrom(int version) {
4956 if (Process.myUid() == Process.SYSTEM_UID) {
Lorenzo Colitti23862912018-09-28 11:31:55 +09004957 // The getApplicationInfo() call we make below is not supported in system context. Let
4958 // the call through here, and rely on the fact that ConnectivityService will refuse to
4959 // allow the system to use these APIs anyway.
Lorenzo Colitti2780e4b2015-07-29 11:41:21 +09004960 return;
4961 }
4962
4963 if (mContext.getApplicationInfo().targetSdkVersion >= version) {
4964 throw new UnsupportedOperationException(
4965 "This method is not supported in target SDK version " + version + " and above");
4966 }
4967 }
4968
4969 // Checks whether the calling app can use the legacy routing API (startUsingNetworkFeature,
4970 // stopUsingNetworkFeature, requestRouteToHost), and if not throw UnsupportedOperationException.
Lifu Tang0a922fd2016-01-07 23:20:38 -08004971 // TODO: convert the existing system users (Tethering, GnssLocationProvider) to the new APIs and
Lorenzo Colitti2780e4b2015-07-29 11:41:21 +09004972 // remove these exemptions. Note that this check is not secure, and apps can still access these
4973 // functions by accessing ConnectivityService directly. However, it should be clear that doing
4974 // so is unsupported and may break in the future. http://b/22728205
4975 private void checkLegacyRoutingApiAccess() {
Dianne Hackborn18c1d832015-07-31 10:35:34 -07004976 unsupportedStartingFrom(VERSION_CODES.M);
Lorenzo Colitti2780e4b2015-07-29 11:41:21 +09004977 }
4978
Paul Jensen8cdda642014-05-29 10:12:39 -04004979 /**
4980 * Binds host resolutions performed by this process to {@code network}.
Paul Jensenee2f45d2015-03-10 10:54:12 -04004981 * {@link #bindProcessToNetwork} takes precedence over this setting.
Paul Jensen8cdda642014-05-29 10:12:39 -04004982 *
4983 * @param network The {@link Network} to bind host resolutions from the current process to, or
4984 * {@code null} to clear the current binding.
4985 * @return {@code true} on success, {@code false} if the {@link Network} is no longer valid.
4986 * @hide
4987 * @deprecated This is strictly for legacy usage to support {@link #startUsingNetworkFeature}.
4988 */
Aurimas Liutikase80a0eb2016-05-24 15:22:55 -07004989 @Deprecated
Mathew Inwoode1a17ba2020-11-04 09:29:36 +00004990 @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.R, trackingBug = 170729553)
Paul Jensen8cdda642014-05-29 10:12:39 -04004991 public static boolean setProcessDefaultNetworkForHostResolution(Network network) {
Paul Jensen65743a22014-07-11 08:17:29 -04004992 return NetworkUtils.bindProcessToNetworkForHostResolution(
Erik Kline767b7f22018-04-27 22:48:33 +09004993 (network == null) ? NETID_UNSET : network.getNetIdForResolv());
Paul Jensen8cdda642014-05-29 10:12:39 -04004994 }
Felipe Leme30511352016-01-22 09:44:57 -08004995
4996 /**
4997 * Device is not restricting metered network activity while application is running on
4998 * background.
4999 */
5000 public static final int RESTRICT_BACKGROUND_STATUS_DISABLED = 1;
5001
5002 /**
5003 * Device is restricting metered network activity while application is running on background,
5004 * but application is allowed to bypass it.
5005 * <p>
5006 * In this state, application should take action to mitigate metered network access.
5007 * For example, a music streaming application should switch to a low-bandwidth bitrate.
5008 */
5009 public static final int RESTRICT_BACKGROUND_STATUS_WHITELISTED = 2;
5010
5011 /**
5012 * Device is restricting metered network activity while application is running on background.
Felipe Lemed34c9af2016-01-27 14:46:39 -08005013 * <p>
Felipe Leme30511352016-01-22 09:44:57 -08005014 * In this state, application should not try to use the network while running on background,
5015 * because it would be denied.
5016 */
5017 public static final int RESTRICT_BACKGROUND_STATUS_ENABLED = 3;
5018
Felipe Lemed34c9af2016-01-27 14:46:39 -08005019 /**
5020 * A change in the background metered network activity restriction has occurred.
5021 * <p>
5022 * Applications should call {@link #getRestrictBackgroundStatus()} to check if the restriction
5023 * applies to them.
5024 * <p>
5025 * This is only sent to registered receivers, not manifest receivers.
5026 */
5027 @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION)
5028 public static final String ACTION_RESTRICT_BACKGROUND_CHANGED =
5029 "android.net.conn.RESTRICT_BACKGROUND_CHANGED";
5030
Felipe Leme7e4c1852016-01-25 11:48:04 -08005031 /** @hide */
5032 @Retention(RetentionPolicy.SOURCE)
Felipe Leme30511352016-01-22 09:44:57 -08005033 @IntDef(flag = false, value = {
5034 RESTRICT_BACKGROUND_STATUS_DISABLED,
5035 RESTRICT_BACKGROUND_STATUS_WHITELISTED,
5036 RESTRICT_BACKGROUND_STATUS_ENABLED,
5037 })
Felipe Leme30511352016-01-22 09:44:57 -08005038 public @interface RestrictBackgroundStatus {
5039 }
5040
Felipe Leme30511352016-01-22 09:44:57 -08005041 /**
5042 * Determines if the calling application is subject to metered network restrictions while
5043 * running on background.
Felipe Leme3edc6162016-05-16 13:57:19 -07005044 *
5045 * @return {@link #RESTRICT_BACKGROUND_STATUS_DISABLED},
5046 * {@link #RESTRICT_BACKGROUND_STATUS_ENABLED},
5047 * or {@link #RESTRICT_BACKGROUND_STATUS_WHITELISTED}
Felipe Leme30511352016-01-22 09:44:57 -08005048 */
5049 public @RestrictBackgroundStatus int getRestrictBackgroundStatus() {
5050 try {
Remi NGUYEN VANe2139a02021-03-18 14:23:12 +09005051 return mService.getRestrictBackgroundStatusByCaller();
Felipe Leme30511352016-01-22 09:44:57 -08005052 } catch (RemoteException e) {
Jeff Sharkeyc78f85c2016-03-01 19:27:23 -07005053 throw e.rethrowFromSystemServer();
Felipe Leme30511352016-01-22 09:44:57 -08005054 }
5055 }
Ricky Wai7097cc92018-01-23 04:09:45 +00005056
5057 /**
5058 * The network watchlist is a list of domains and IP addresses that are associated with
Ricky Wai04baf112018-03-20 14:20:54 +00005059 * potentially harmful apps. This method returns the SHA-256 of the watchlist config file
5060 * currently used by the system for validation purposes.
Ricky Wai7097cc92018-01-23 04:09:45 +00005061 *
5062 * @return Hash of network watchlist config file. Null if config does not exist.
5063 */
Chalard Jean158702d2019-01-07 19:26:34 +09005064 @Nullable
Ricky Wai7097cc92018-01-23 04:09:45 +00005065 public byte[] getNetworkWatchlistConfigHash() {
5066 try {
5067 return mService.getNetworkWatchlistConfigHash();
5068 } catch (RemoteException e) {
5069 Log.e(TAG, "Unable to get watchlist config hash");
5070 throw e.rethrowFromSystemServer();
5071 }
5072 }
Jeff Vander Stoep39a51e02018-07-23 10:57:53 -07005073
5074 /**
5075 * Returns the {@code uid} of the owner of a network connection.
5076 *
Benedict Wong0bd4bba2020-01-20 22:14:59 -08005077 * @param protocol The protocol of the connection. Only {@code IPPROTO_TCP} and {@code
5078 * IPPROTO_UDP} currently supported.
Jeff Vander Stoep39a51e02018-07-23 10:57:53 -07005079 * @param local The local {@link InetSocketAddress} of a connection.
5080 * @param remote The remote {@link InetSocketAddress} of a connection.
Jeff Vander Stoep39a51e02018-07-23 10:57:53 -07005081 * @return {@code uid} if the connection is found and the app has permission to observe it
Benedict Wong0bd4bba2020-01-20 22:14:59 -08005082 * (e.g., if it is associated with the calling VPN app's VpnService tunnel) or {@link
5083 * android.os.Process#INVALID_UID} if the connection is not found.
5084 * @throws {@link SecurityException} if the caller is not the active VpnService for the current
5085 * user.
5086 * @throws {@link IllegalArgumentException} if an unsupported protocol is requested.
Jeff Vander Stoep39a51e02018-07-23 10:57:53 -07005087 */
Benedict Wong0bd4bba2020-01-20 22:14:59 -08005088 public int getConnectionOwnerUid(
5089 int protocol, @NonNull InetSocketAddress local, @NonNull InetSocketAddress remote) {
Jeff Vander Stoep39a51e02018-07-23 10:57:53 -07005090 ConnectionInfo connectionInfo = new ConnectionInfo(protocol, local, remote);
5091 try {
5092 return mService.getConnectionOwnerUid(connectionInfo);
5093 } catch (RemoteException e) {
5094 throw e.rethrowFromSystemServer();
5095 }
5096 }
Soi, Yoshinarib8102ef2015-11-12 12:09:02 +09005097
5098 private void printStackTrace() {
5099 if (DEBUG) {
5100 final StackTraceElement[] callStack = Thread.currentThread().getStackTrace();
5101 final StringBuffer sb = new StringBuffer();
5102 for (int i = 3; i < callStack.length; i++) {
5103 final String stackTrace = callStack[i].toString();
5104 if (stackTrace == null || stackTrace.contains("android.os")) {
5105 break;
5106 }
5107 sb.append(" [").append(stackTrace).append("]");
5108 }
5109 Log.d(TAG, "StackLog:" + sb.toString());
5110 }
5111 }
Cody Kestingf53a0752020-04-15 12:33:28 -07005112
Remi NGUYEN VAN5f406422021-01-15 23:02:47 +09005113 /** @hide */
5114 public TestNetworkManager startOrGetTestNetworkManager() {
5115 final IBinder tnBinder;
5116 try {
5117 tnBinder = mService.startOrGetTestNetworkService();
5118 } catch (RemoteException e) {
5119 throw e.rethrowFromSystemServer();
5120 }
5121
5122 return new TestNetworkManager(ITestNetworkManager.Stub.asInterface(tnBinder));
5123 }
5124
Remi NGUYEN VAN5f406422021-01-15 23:02:47 +09005125 /** @hide */
5126 public ConnectivityDiagnosticsManager createDiagnosticsManager() {
5127 return new ConnectivityDiagnosticsManager(mContext, mService);
5128 }
5129
Cody Kestingf53a0752020-04-15 12:33:28 -07005130 /**
5131 * Simulates a Data Stall for the specified Network.
5132 *
Remi NGUYEN VAN761c7ad2021-01-12 18:40:04 +09005133 * <p>This method should only be used for tests.
5134 *
Remi NGUYEN VANd2d60c62021-04-08 16:26:20 +09005135 * <p>The caller must be the owner of the specified Network. This simulates a data stall to
5136 * have the system behave as if it had happened, but does not actually stall connectivity.
Cody Kestingf53a0752020-04-15 12:33:28 -07005137 *
5138 * @param detectionMethod The detection method used to identify the Data Stall.
Remi NGUYEN VANd2d60c62021-04-08 16:26:20 +09005139 * See ConnectivityDiagnosticsManager.DataStallReport.DETECTION_METHOD_*.
5140 * @param timestampMillis The timestamp at which the stall 'occurred', in milliseconds, as per
5141 * SystemClock.elapsedRealtime.
Cody Kestingf53a0752020-04-15 12:33:28 -07005142 * @param network The Network for which a Data Stall is being simluated.
5143 * @param extras The PersistableBundle of extras included in the Data Stall notification.
5144 * @throws SecurityException if the caller is not the owner of the given network.
5145 * @hide
5146 */
Remi NGUYEN VAN761c7ad2021-01-12 18:40:04 +09005147 @SystemApi(client = SystemApi.Client.MODULE_LIBRARIES)
Cody Kestingf53a0752020-04-15 12:33:28 -07005148 @RequiresPermission(anyOf = {android.Manifest.permission.MANAGE_TEST_NETWORKS,
5149 android.Manifest.permission.NETWORK_STACK})
Remi NGUYEN VANd2d60c62021-04-08 16:26:20 +09005150 public void simulateDataStall(@DetectionMethod int detectionMethod, long timestampMillis,
Cody Kestingf53a0752020-04-15 12:33:28 -07005151 @NonNull Network network, @NonNull PersistableBundle extras) {
5152 try {
5153 mService.simulateDataStall(detectionMethod, timestampMillis, network, extras);
5154 } catch (RemoteException e) {
5155 e.rethrowFromSystemServer();
5156 }
5157 }
James Mattis356a8792020-10-28 21:48:54 -07005158
Daniel Brightf9e945b2020-06-15 16:10:01 -07005159 @NonNull
5160 private final List<QosCallbackConnection> mQosCallbackConnections = new ArrayList<>();
5161
5162 /**
5163 * Registers a {@link QosSocketInfo} with an associated {@link QosCallback}. The callback will
5164 * receive available QoS events related to the {@link Network} and local ip + port
5165 * specified within socketInfo.
5166 * <p/>
5167 * The same {@link QosCallback} must be unregistered before being registered a second time,
5168 * otherwise {@link QosCallbackRegistrationException} is thrown.
5169 * <p/>
5170 * This API does not, in itself, require any permission if called with a network that is not
5171 * restricted. However, the underlying implementation currently only supports the IMS network,
5172 * which is always restricted. That means non-preinstalled callers can't possibly find this API
5173 * useful, because they'd never be called back on networks that they would have access to.
5174 *
5175 * @throws SecurityException if {@link QosSocketInfo#getNetwork()} is restricted and the app is
5176 * missing CONNECTIVITY_USE_RESTRICTED_NETWORKS permission.
5177 * @throws QosCallback.QosCallbackRegistrationException if qosCallback is already registered.
5178 * @throws RuntimeException if the app already has too many callbacks registered.
5179 *
5180 * Exceptions after the time of registration is passed through
5181 * {@link QosCallback#onError(QosCallbackException)}. see: {@link QosCallbackException}.
5182 *
5183 * @param socketInfo the socket information used to match QoS events
Daniel Brightf9e945b2020-06-15 16:10:01 -07005184 * @param executor The executor on which the callback will be invoked. The provided
5185 * {@link Executor} must run callback sequentially, otherwise the order of
Daniel Bright7ee5f522021-03-10 11:51:50 -08005186 * callbacks cannot be guaranteed.onQosCallbackRegistered
5187 * @param callback receives qos events that satisfy socketInfo
Daniel Brightf9e945b2020-06-15 16:10:01 -07005188 *
5189 * @hide
5190 */
5191 @SystemApi
5192 public void registerQosCallback(@NonNull final QosSocketInfo socketInfo,
Daniel Bright7ee5f522021-03-10 11:51:50 -08005193 @CallbackExecutor @NonNull final Executor executor,
5194 @NonNull final QosCallback callback) {
Daniel Brightf9e945b2020-06-15 16:10:01 -07005195 Objects.requireNonNull(socketInfo, "socketInfo must be non-null");
Daniel Brightf9e945b2020-06-15 16:10:01 -07005196 Objects.requireNonNull(executor, "executor must be non-null");
Daniel Bright7ee5f522021-03-10 11:51:50 -08005197 Objects.requireNonNull(callback, "callback must be non-null");
Daniel Brightf9e945b2020-06-15 16:10:01 -07005198
5199 try {
5200 synchronized (mQosCallbackConnections) {
5201 if (getQosCallbackConnection(callback) == null) {
5202 final QosCallbackConnection connection =
5203 new QosCallbackConnection(this, callback, executor);
5204 mQosCallbackConnections.add(connection);
5205 mService.registerQosSocketCallback(socketInfo, connection);
5206 } else {
5207 Log.e(TAG, "registerQosCallback: Callback already registered");
5208 throw new QosCallbackRegistrationException();
5209 }
5210 }
5211 } catch (final RemoteException e) {
5212 Log.e(TAG, "registerQosCallback: Error while registering ", e);
5213
5214 // The same unregister method method is called for consistency even though nothing
5215 // will be sent to the ConnectivityService since the callback was never successfully
5216 // registered.
5217 unregisterQosCallback(callback);
5218 e.rethrowFromSystemServer();
5219 } catch (final ServiceSpecificException e) {
5220 Log.e(TAG, "registerQosCallback: Error while registering ", e);
5221 unregisterQosCallback(callback);
5222 throw convertServiceException(e);
5223 }
5224 }
5225
5226 /**
5227 * Unregisters the given {@link QosCallback}. The {@link QosCallback} will no longer receive
5228 * events once unregistered and can be registered a second time.
5229 * <p/>
5230 * If the {@link QosCallback} does not have an active registration, it is a no-op.
5231 *
5232 * @param callback the callback being unregistered
5233 *
5234 * @hide
5235 */
5236 @SystemApi
5237 public void unregisterQosCallback(@NonNull final QosCallback callback) {
5238 Objects.requireNonNull(callback, "The callback must be non-null");
5239 try {
5240 synchronized (mQosCallbackConnections) {
5241 final QosCallbackConnection connection = getQosCallbackConnection(callback);
5242 if (connection != null) {
5243 connection.stopReceivingMessages();
5244 mService.unregisterQosCallback(connection);
5245 mQosCallbackConnections.remove(connection);
5246 } else {
5247 Log.d(TAG, "unregisterQosCallback: Callback not registered");
5248 }
5249 }
5250 } catch (final RemoteException e) {
5251 Log.e(TAG, "unregisterQosCallback: Error while unregistering ", e);
5252 e.rethrowFromSystemServer();
5253 }
5254 }
5255
5256 /**
5257 * Gets the connection related to the callback.
5258 *
5259 * @param callback the callback to look up
5260 * @return the related connection
5261 */
5262 @Nullable
5263 private QosCallbackConnection getQosCallbackConnection(final QosCallback callback) {
5264 for (final QosCallbackConnection connection : mQosCallbackConnections) {
5265 // Checking by reference here is intentional
5266 if (connection.getCallback() == callback) {
5267 return connection;
5268 }
5269 }
5270 return null;
5271 }
Junyu Laia62493f2021-01-19 11:10:56 +00005272
5273 /**
Roshan Pius951c0032020-12-22 15:10:42 -08005274 * Request a network to satisfy a set of {@link NetworkCapabilities}, but
Junyu Laia62493f2021-01-19 11:10:56 +00005275 * does not cause any networks to retain the NET_CAPABILITY_FOREGROUND capability. This can
5276 * be used to request that the system provide a network without causing the network to be
5277 * in the foreground.
5278 *
5279 * <p>This method will attempt to find the best network that matches the passed
5280 * {@link NetworkRequest}, and to bring up one that does if none currently satisfies the
5281 * criteria. The platform will evaluate which network is the best at its own discretion.
5282 * Throughput, latency, cost per byte, policy, user preference and other considerations
5283 * may be factored in the decision of what is considered the best network.
5284 *
5285 * <p>As long as this request is outstanding, the platform will try to maintain the best network
5286 * matching this request, while always attempting to match the request to a better network if
5287 * possible. If a better match is found, the platform will switch this request to the now-best
5288 * network and inform the app of the newly best network by invoking
5289 * {@link NetworkCallback#onAvailable(Network)} on the provided callback. Note that the platform
5290 * will not try to maintain any other network than the best one currently matching the request:
5291 * a network not matching any network request may be disconnected at any time.
5292 *
5293 * <p>For example, an application could use this method to obtain a connected cellular network
5294 * even if the device currently has a data connection over Ethernet. This may cause the cellular
5295 * radio to consume additional power. Or, an application could inform the system that it wants
5296 * a network supporting sending MMSes and have the system let it know about the currently best
5297 * MMS-supporting network through the provided {@link NetworkCallback}.
5298 *
5299 * <p>The status of the request can be followed by listening to the various callbacks described
5300 * in {@link NetworkCallback}. The {@link Network} object passed to the callback methods can be
5301 * used to direct traffic to the network (although accessing some networks may be subject to
5302 * holding specific permissions). Callers will learn about the specific characteristics of the
5303 * network through
5304 * {@link NetworkCallback#onCapabilitiesChanged(Network, NetworkCapabilities)} and
5305 * {@link NetworkCallback#onLinkPropertiesChanged(Network, LinkProperties)}. The methods of the
5306 * provided {@link NetworkCallback} will only be invoked due to changes in the best network
5307 * matching the request at any given time; therefore when a better network matching the request
5308 * becomes available, the {@link NetworkCallback#onAvailable(Network)} method is called
5309 * with the new network after which no further updates are given about the previously-best
5310 * network, unless it becomes the best again at some later time. All callbacks are invoked
5311 * in order on the same thread, which by default is a thread created by the framework running
5312 * in the app.
5313 *
5314 * <p>This{@link NetworkRequest} will live until released via
5315 * {@link #unregisterNetworkCallback(NetworkCallback)} or the calling application exits, at
5316 * which point the system may let go of the network at any time.
5317 *
5318 * <p>It is presently unsupported to request a network with mutable
5319 * {@link NetworkCapabilities} such as
5320 * {@link NetworkCapabilities#NET_CAPABILITY_VALIDATED} or
5321 * {@link NetworkCapabilities#NET_CAPABILITY_CAPTIVE_PORTAL}
5322 * as these {@code NetworkCapabilities} represent states that a particular
5323 * network may never attain, and whether a network will attain these states
5324 * is unknown prior to bringing up the network so the framework does not
5325 * know how to go about satisfying a request with these capabilities.
5326 *
5327 * <p>To avoid performance issues due to apps leaking callbacks, the system will limit the
5328 * number of outstanding requests to 100 per app (identified by their UID), shared with
5329 * all variants of this method, of {@link #registerNetworkCallback} as well as
5330 * {@link ConnectivityDiagnosticsManager#registerConnectivityDiagnosticsCallback}.
5331 * Requesting a network with this method will count toward this limit. If this limit is
5332 * exceeded, an exception will be thrown. To avoid hitting this issue and to conserve resources,
5333 * make sure to unregister the callbacks with
5334 * {@link #unregisterNetworkCallback(NetworkCallback)}.
5335 *
5336 * @param request {@link NetworkRequest} describing this request.
Junyu Laia62493f2021-01-19 11:10:56 +00005337 * @param networkCallback The {@link NetworkCallback} to be utilized for this request. Note
5338 * the callback must not be shared - it uniquely specifies this request.
junyulai05738ae2021-04-15 00:39:49 +08005339 * @param handler {@link Handler} to specify the thread upon which the callback will be invoked.
5340 * If null, the callback is invoked on the default internal Handler.
Junyu Laia62493f2021-01-19 11:10:56 +00005341 * @throws IllegalArgumentException if {@code request} contains invalid network capabilities.
5342 * @throws SecurityException if missing the appropriate permissions.
5343 * @throws RuntimeException if the app already has too many callbacks registered.
5344 *
5345 * @hide
5346 */
5347 @SystemApi(client = MODULE_LIBRARIES)
5348 @SuppressLint("ExecutorRegistration")
5349 @RequiresPermission(anyOf = {
5350 android.Manifest.permission.NETWORK_SETTINGS,
5351 android.Manifest.permission.NETWORK_STACK,
5352 NetworkStack.PERMISSION_MAINLINE_NETWORK_STACK
5353 })
5354 public void requestBackgroundNetwork(@NonNull NetworkRequest request,
junyulai05738ae2021-04-15 00:39:49 +08005355 @NonNull NetworkCallback networkCallback,
5356 @SuppressLint("ListenerLast") @NonNull Handler handler) {
Junyu Laia62493f2021-01-19 11:10:56 +00005357 final NetworkCapabilities nc = request.networkCapabilities;
5358 sendRequestForNetwork(nc, networkCallback, 0, BACKGROUND_REQUEST,
junyulai962bdb82021-03-09 20:49:48 +08005359 TYPE_NONE, new CallbackHandler(handler));
Junyu Laia62493f2021-01-19 11:10:56 +00005360 }
James Mattis45d81842021-01-10 14:24:24 -08005361
5362 /**
James Mattis45d81842021-01-10 14:24:24 -08005363 * Used by automotive devices to set the network preferences used to direct traffic at an
5364 * application level as per the given OemNetworkPreferences. An example use-case would be an
5365 * automotive OEM wanting to provide connectivity for applications critical to the usage of a
5366 * vehicle via a particular network.
5367 *
5368 * Calling this will overwrite the existing preference.
5369 *
5370 * @param preference {@link OemNetworkPreferences} The application network preference to be set.
5371 * @param executor the executor on which listener will be invoked.
5372 * @param listener {@link OnSetOemNetworkPreferenceListener} optional listener used to
5373 * communicate completion of setOemNetworkPreference(). This will only be
5374 * called once upon successful completion of setOemNetworkPreference().
5375 * @throws IllegalArgumentException if {@code preference} contains invalid preference values.
5376 * @throws SecurityException if missing the appropriate permissions.
5377 * @throws UnsupportedOperationException if called on a non-automotive device.
James Mattisda32cfe2021-01-26 16:23:52 -08005378 * @hide
James Mattis45d81842021-01-10 14:24:24 -08005379 */
James Mattisda32cfe2021-01-26 16:23:52 -08005380 @SystemApi
James Mattis8378aec2021-01-26 14:05:36 -08005381 @RequiresPermission(android.Manifest.permission.CONTROL_OEM_PAID_NETWORK_PREFERENCE)
James Mattisda32cfe2021-01-26 16:23:52 -08005382 public void setOemNetworkPreference(@NonNull final OemNetworkPreferences preference,
James Mattis45d81842021-01-10 14:24:24 -08005383 @Nullable @CallbackExecutor final Executor executor,
Chalard Jean6010c002021-03-03 16:37:13 +09005384 @Nullable final Runnable listener) {
James Mattis45d81842021-01-10 14:24:24 -08005385 Objects.requireNonNull(preference, "OemNetworkPreferences must be non-null");
5386 if (null != listener) {
5387 Objects.requireNonNull(executor, "Executor must be non-null");
5388 }
Chalard Jean6010c002021-03-03 16:37:13 +09005389 final IOnCompleteListener listenerInternal = listener == null ? null :
5390 new IOnCompleteListener.Stub() {
James Mattis45d81842021-01-10 14:24:24 -08005391 @Override
5392 public void onComplete() {
Chalard Jean6010c002021-03-03 16:37:13 +09005393 executor.execute(listener::run);
James Mattis45d81842021-01-10 14:24:24 -08005394 }
5395 };
5396
5397 try {
5398 mService.setOemNetworkPreference(preference, listenerInternal);
5399 } catch (RemoteException e) {
5400 Log.e(TAG, "setOemNetworkPreference() failed for preference: " + preference.toString());
5401 throw e.rethrowFromSystemServer();
5402 }
5403 }
lucaslin30e70a82021-03-12 00:46:33 +08005404
Chalard Jeanfa45a682021-02-25 17:23:40 +09005405 /**
5406 * Request that a user profile is put by default on a network matching a given preference.
5407 *
5408 * See the documentation for the individual preferences for a description of the supported
5409 * behaviors.
5410 *
5411 * @param profile the profile concerned.
5412 * @param preference the preference for this profile.
5413 * @param executor an executor to execute the listener on. Optional if listener is null.
5414 * @param listener an optional listener to listen for completion of the operation.
5415 * @throws IllegalArgumentException if {@code profile} is not a valid user profile.
5416 * @throws SecurityException if missing the appropriate permissions.
5417 * @hide
5418 */
Chalard Jean6010c002021-03-03 16:37:13 +09005419 // This function is for establishing per-profile default networking and can only be called by
5420 // the device policy manager, running as the system server. It would make no sense to call it
5421 // on a context for a user because it does not establish a setting on behalf of a user, rather
5422 // it establishes a setting for a user on behalf of the DPM.
5423 @SuppressLint({"UserHandle"})
5424 @SystemApi(client = MODULE_LIBRARIES)
Chalard Jeanfa45a682021-02-25 17:23:40 +09005425 @RequiresPermission(android.Manifest.permission.NETWORK_STACK)
5426 public void setProfileNetworkPreference(@NonNull final UserHandle profile,
5427 @ProfileNetworkPreference final int preference,
5428 @Nullable @CallbackExecutor final Executor executor,
5429 @Nullable final Runnable listener) {
5430 if (null != listener) {
5431 Objects.requireNonNull(executor, "Pass a non-null executor, or a null listener");
5432 }
5433 final IOnCompleteListener proxy;
5434 if (null == listener) {
5435 proxy = null;
5436 } else {
5437 proxy = new IOnCompleteListener.Stub() {
5438 @Override
5439 public void onComplete() {
5440 executor.execute(listener::run);
5441 }
5442 };
5443 }
5444 try {
5445 mService.setProfileNetworkPreference(profile, preference, proxy);
5446 } catch (RemoteException e) {
5447 throw e.rethrowFromSystemServer();
5448 }
5449 }
5450
lucaslin30e70a82021-03-12 00:46:33 +08005451 // The first network ID of IPSec tunnel interface.
lucaslin0cdcea12021-03-15 17:24:12 +08005452 private static final int TUN_INTF_NETID_START = 0xFC00; // 0xFC00 = 64512
lucaslin30e70a82021-03-12 00:46:33 +08005453 // The network ID range of IPSec tunnel interface.
lucaslin0cdcea12021-03-15 17:24:12 +08005454 private static final int TUN_INTF_NETID_RANGE = 0x0400; // 0x0400 = 1024
lucaslin30e70a82021-03-12 00:46:33 +08005455
5456 /**
5457 * Get the network ID range reserved for IPSec tunnel interfaces.
5458 *
5459 * @return A Range which indicates the network ID range of IPSec tunnel interface.
5460 * @hide
5461 */
5462 @SystemApi(client = MODULE_LIBRARIES)
5463 @NonNull
5464 public static Range<Integer> getIpSecNetIdRange() {
5465 return new Range(TUN_INTF_NETID_START, TUN_INTF_NETID_START + TUN_INTF_NETID_RANGE - 1);
5466 }
The Android Open Source Project28527d22009-03-03 19:31:44 -08005467}