blob: df81603d00250d6cfb38f2ff0cc6c3e9e6867cf1 [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 Lai23568a42021-01-19 11:10:56 +000018import static android.annotation.SystemApi.Client.MODULE_LIBRARIES;
Junyu Lai23568a42021-01-19 11:10:56 +000019import static android.net.NetworkRequest.Type.BACKGROUND_REQUEST;
junyulaid1a78162021-01-11 16:53:38 +080020import static android.net.NetworkRequest.Type.LISTEN;
junyulai8cae3c72021-03-12 20:05:08 +080021import static android.net.NetworkRequest.Type.LISTEN_FOR_BEST;
junyulaid1a78162021-01-11 16:53:38 +080022import static android.net.NetworkRequest.Type.REQUEST;
23import static android.net.NetworkRequest.Type.TRACK_DEFAULT;
Lorenzo Colitti80b48bc2021-01-29 20:14:04 +090024import static android.net.NetworkRequest.Type.TRACK_SYSTEM_DEFAULT;
Daniel Bright60f02ed2020-06-15 16:10:01 -070025import static android.net.QosCallback.QosCallbackRegistrationException;
junyulai48a59382019-01-15 11:32:44 +080026
junyulaia86defc2018-12-27 17:25:29 +080027import android.annotation.CallbackExecutor;
Felipe Lemed16384b2016-01-22 09:44:57 -080028import android.annotation.IntDef;
Chalard Jean42a9c292019-01-07 19:26:34 +090029import android.annotation.NonNull;
Robin Lee33c73e22016-01-05 18:03:46 +000030import android.annotation.Nullable;
Jeff Sharkey2ac62992017-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 Lai23568a42021-01-19 11:10:56 +000034import android.annotation.SuppressLint;
Udam Saini53b71ee2016-01-04 12:16:14 -080035import android.annotation.SystemApi;
Jeff Sharkeyb8c73032017-06-02 17:36:26 -060036import android.annotation.SystemService;
Robert Greenwalt2bebbfb2014-03-26 16:47:06 -070037import android.app.PendingIntent;
Lorenzo Colitti60104f52021-03-18 00:54:57 +090038import android.app.admin.DevicePolicyManager;
Artur Satayevb9ea02f2019-12-10 17:47:52 +000039import android.compat.annotation.UnsupportedAppUsage;
Lorenzo Colitti60104f52021-03-18 00:54:57 +090040import android.content.ComponentName;
Jeff Sharkey2d9e5a52012-04-04 20:40:58 -070041import android.content.Context;
Robert Greenwaltd0ebdbb2014-05-18 23:07:25 -070042import android.content.Intent;
Remi NGUYEN VAN5738b862021-04-08 16:26:20 +090043import android.net.ConnectivityDiagnosticsManager.DataStallReport.DetectionMethod;
junyulaia86defc2018-12-27 17:25:29 +080044import android.net.IpSecManager.UdpEncapsulationSocket;
45import android.net.SocketKeepalive.Callback;
markchien10ddd022020-01-20 19:31:56 +080046import android.net.TetheringManager.StartTetheringCallback;
markchien75721e42020-01-21 13:11:06 +080047import android.net.TetheringManager.TetheringEventCallback;
markchien10ddd022020-01-20 19:31:56 +080048import android.net.TetheringManager.TetheringRequest;
Roshan Pius7992afd2020-12-22 15:10:42 -080049import android.net.wifi.WifiNetworkSuggestion;
Robert Greenwalt2034b912009-08-12 16:08:25 -070050import android.os.Binder;
Mathew Inwoodac5968e2018-12-20 15:30:45 +000051import android.os.Build;
Jeff Sharkey39c01eb2011-08-16 14:37:57 -070052import android.os.Build.VERSION_CODES;
Jeremy Klein9a36ec82016-01-22 14:11:45 -080053import android.os.Bundle;
Robert Greenwalt2bebbfb2014-03-26 16:47:06 -070054import android.os.Handler;
Dianne Hackbornb4b09e82014-02-26 16:20:52 -080055import android.os.IBinder;
Robert Greenwalt2bebbfb2014-03-26 16:47:06 -070056import android.os.Looper;
57import android.os.Message;
Robert Greenwalt030e1d32012-08-21 19:27:00 -070058import android.os.Messenger;
junyulai61143782019-03-04 22:45:36 +080059import android.os.ParcelFileDescriptor;
Cody Kestingb5c7abd2020-04-15 12:33:28 -070060import android.os.PersistableBundle;
Lorenzo Colitti9b026fa2015-07-29 11:41:21 +090061import android.os.Process;
The Android Open Source Project28527d22009-03-03 19:31:44 -080062import android.os.RemoteException;
Jeremy Klein9a36ec82016-01-22 14:11:45 -080063import android.os.ResultReceiver;
Hugo Benichi145e3792017-05-11 13:16:17 +090064import android.os.ServiceSpecificException;
Chalard Jean03433052021-02-25 17:23:40 +090065import android.os.UserHandle;
Jeff Sharkey971cd162011-08-29 16:02:57 -070066import android.provider.Settings;
Wink Saville02eb35c2014-12-05 11:10:30 -080067import android.telephony.SubscriptionManager;
Meng Wang91311c02019-11-18 17:10:00 -080068import android.telephony.TelephonyManager;
Dianne Hackbornb4b09e82014-02-26 16:20:52 -080069import android.util.ArrayMap;
Robert Greenwalt2bebbfb2014-03-26 16:47:06 -070070import android.util.Log;
Lorenzo Colittie8ce2052020-12-12 00:51:11 +090071import android.util.Range;
Erik Kline50068e52017-01-26 18:08:28 +090072import android.util.SparseIntArray;
The Android Open Source Project28527d22009-03-03 19:31:44 -080073
markchien75721e42020-01-21 13:11:06 +080074import com.android.internal.annotations.GuardedBy;
Robert Greenwalt3d6c9582014-05-21 20:04:36 -070075
Paul Jensen99c36662014-08-27 12:38:45 -040076import libcore.net.event.NetworkEventDispatcher;
77
junyulai61143782019-03-04 22:45:36 +080078import java.io.IOException;
79import java.io.UncheckedIOException;
Felipe Lemed16384b2016-01-22 09:44:57 -080080import java.lang.annotation.Retention;
81import java.lang.annotation.RetentionPolicy;
Lorenzo Colittie8ce2052020-12-12 00:51:11 +090082import java.net.DatagramSocket;
Jeremy Klein04863332015-12-28 15:11:58 -080083import java.net.InetAddress;
Jeff Vander Stoepe7205d62018-07-23 10:57:53 -070084import java.net.InetSocketAddress;
junyulaic7ea1242019-01-08 20:04:33 +080085import java.net.Socket;
Hugo Benichi45a49542017-03-06 09:17:06 +090086import java.util.ArrayList;
Lorenzo Colittie8ce2052020-12-12 00:51:11 +090087import java.util.Collection;
Jeremy Klein04863332015-12-28 15:11:58 -080088import java.util.HashMap;
Hugo Benichi45a49542017-03-06 09:17:06 +090089import java.util.List;
90import java.util.Map;
markchien75721e42020-01-21 13:11:06 +080091import java.util.Objects;
junyulaia86defc2018-12-27 17:25:29 +080092import java.util.concurrent.Executor;
junyulai9f872232019-01-16 20:23:34 +080093import java.util.concurrent.ExecutorService;
94import java.util.concurrent.Executors;
95import java.util.concurrent.RejectedExecutionException;
Jeremy Klein04863332015-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 Sharkeyb8c73032017-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 Greenwaltd0ebdbb2014-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 Sharkeyb8c73032017-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, Yoshinari99efcbd2015-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 Greenwaltd0ebdbb2014-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 Lue1682a02016-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 Greenwalt26744a52013-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 Jeanae2424a2018-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 Sharkey6932ff62013-02-20 18:21:19 -0800153 @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION)
Chalard Jeanae2424a2018-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 Jensened4d55c2015-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 Jensen4173cce2015-05-22 10:50:39 -0400162 * desire to sign in to the network. Apps handling this activity should
Paul Jensened4d55c2015-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 Jensen4173cce2015-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 Jean73d9db72018-06-04 16:52:49 +0900177 * data network.</li>
Paul Jensen4173cce2015-05-22 10:50:39 -0400178 * <li> When the app handling this action believes the user explicitly wants
Paul Jensened4d55c2015-02-27 22:55:47 -0500179 * to ignore the captive portal and the network, the app should call
Paul Jensen4173cce2015-05-22 10:50:39 -0400180 * {@link CaptivePortal#ignoreNetwork}. </li>
181 * </ul>
Paul Jensened4d55c2015-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 Jeancc47b522019-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 Jeancc47b522019-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 Jeancc47b522019-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)}.
junyulai5c2f6262018-12-13 12:47:51 +0800214 *
215 * @deprecated See {@link NetworkInfo}.
The Android Open Source Project28527d22009-03-03 19:31:44 -0800216 */
junyulai5c2f6262018-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)}.
junyulai5c2f6262018-12-13 12:47:51 +0800223 *
224 * @deprecated See {@link NetworkInfo}.
The Android Open Source Project28527d22009-03-03 19:31:44 -0800225 */
junyulai5c2f6262018-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)}.
junyulai5c2f6262018-12-13 12:47:51 +0800247 *
248 * @deprecated See {@link NetworkInfo#getExtraInfo()}.
The Android Open Source Project28527d22009-03-03 19:31:44 -0800249 */
junyulai5c2f6262018-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 Jensen4173cce2015-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 Jensened4d55c2015-02-27 22:55:47 -0500267 */
Paul Jensen4173cce2015-05-22 10:50:39 -0400268 public static final String EXTRA_CAPTIVE_PORTAL = "android.net.extra.CAPTIVE_PORTAL";
Jan Nordqvist0f3b4442015-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 Jensened4d55c2015-02-27 22:55:47 -0500275 /**
Remi NGUYEN VANd937e3a2018-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 VANe541d182019-01-17 14:38:31 +0900280 @SystemApi
Remi NGUYEN VANd937e3a2018-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 Benichidce21c52016-12-14 08:23:40 +0900285 * Key for passing a user agent string to the captive portal login activity.
286 * {@hide}
287 */
Remi NGUYEN VANe541d182019-01-17 14:38:31 +0900288 @SystemApi
Hugo Benichidce21c52016-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 Greenwalt26744a52013-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 Jean73d9db72018-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 Sharma601fba92014-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 Sharkey6932ff62013-02-20 18:21:19 -0800350 @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION)
Mathew Inwood0b8f8612018-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 Greenwalt26744a52013-02-15 10:56:35 -0800356 * Broadcast Action: A tetherable connection has come or gone.
357 * Uses {@code ConnectivityManager.EXTRA_AVAILABLE_TETHER},
Erik Kline449ccfe2017-04-17 16:47:23 +0900358 * {@code ConnectivityManager.EXTRA_ACTIVE_LOCAL_ONLY},
359 * {@code ConnectivityManager.EXTRA_ACTIVE_TETHER}, and
Robert Greenwalt26744a52013-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 Sharkey6932ff62013-02-20 18:21:19 -0800365 @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION)
Mathew Inwoodd078d3d2020-10-27 11:47:29 +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 =
markchien1f523702019-12-25 19:40:32 +0800368 TetheringManager.ACTION_TETHER_STATE_CHANGED;
Robert Greenwalt0c4828c2010-01-26 11:40:34 -0800369
370 /**
371 * @hide
Robert Greenwalt26744a52013-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 Inwoodd078d3d2020-10-27 11:47:29 +0000375 @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.R, trackingBug = 170729553)
markchien1f523702019-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 Kline449ccfe2017-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 */
markchien1f523702019-12-25 19:40:32 +0800383 public static final String EXTRA_ACTIVE_LOCAL_ONLY = TetheringManager.EXTRA_ACTIVE_LOCAL_ONLY;
Erik Kline449ccfe2017-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 Inwoodd078d3d2020-10-27 11:47:29 +0000390 @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.R, trackingBug = 170729553)
markchien1f523702019-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 Greenwalt26744a52013-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 Inwoodd078d3d2020-10-27 11:47:29 +0000399 @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.R, trackingBug = 170729553)
markchien1f523702019-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 Brenner4774b022013-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 Sharkey6932ff62013-02-20 18:21:19 -0800408 @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION)
Russell Brenner4774b022013-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 Colitti36fbebe2015-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 *
lucaslin8a8cb5d2021-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 Colitti36fbebe2015-04-03 16:38:52 +0900425 * @hide
426 */
lucaslin1a0ca5f2021-03-04 17:09:51 +0800427 @SystemApi(client = MODULE_LIBRARIES)
428 public static final String ACTION_PROMPT_UNVALIDATED = "android.net.action.PROMPT_UNVALIDATED";
Lorenzo Colitti36fbebe2015-04-03 16:38:52 +0900429
430 /**
Lorenzo Colitti477bf5c2016-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 *
lucaslin8a8cb5d2021-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 Colitti477bf5c2016-09-15 14:02:29 +0900438 * @hide
439 */
lucaslin1a0ca5f2021-03-04 17:09:51 +0800440 @SystemApi(client = MODULE_LIBRARIES)
Lorenzo Colitti477bf5c2016-09-15 14:02:29 +0900441 public static final String ACTION_PROMPT_LOST_VALIDATION =
lucaslin1a0ca5f2021-03-04 17:09:51 +0800442 "android.net.action.PROMPT_LOST_VALIDATION";
Lorenzo Colitti477bf5c2016-09-15 14:02:29 +0900443
444 /**
lucaslin25a50472019-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 *
lucaslin8a8cb5d2021-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 *
lucaslin25a50472019-03-12 13:08:03 +0800453 * @hide
454 */
lucaslin1a0ca5f2021-03-04 17:09:51 +0800455 @SystemApi(client = MODULE_LIBRARIES)
lucaslin25a50472019-03-12 13:08:03 +0800456 public static final String ACTION_PROMPT_PARTIAL_CONNECTIVITY =
lucaslin1a0ca5f2021-03-04 17:09:51 +0800457 "android.net.action.PROMPT_PARTIAL_CONNECTIVITY";
lucaslin25a50472019-03-12 13:08:03 +0800458
459 /**
paulhu164c97d2021-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 Klein9a36ec82016-01-22 14:11:45 -0800469 * Invalid tethering type.
Chalard Jean73d9db72018-06-04 16:52:49 +0900470 * @see #startTethering(int, boolean, OnStartTetheringCallback)
Jeremy Klein9a36ec82016-01-22 14:11:45 -0800471 * @hide
472 */
markchien1f523702019-12-25 19:40:32 +0800473 public static final int TETHERING_INVALID = TetheringManager.TETHERING_INVALID;
Jeremy Klein9a36ec82016-01-22 14:11:45 -0800474
475 /**
476 * Wifi tethering type.
Chalard Jean73d9db72018-06-04 16:52:49 +0900477 * @see #startTethering(int, boolean, OnStartTetheringCallback)
Jeremy Klein9a36ec82016-01-22 14:11:45 -0800478 * @hide
479 */
480 @SystemApi
Remi NGUYEN VAN23b853b2021-02-15 18:52:06 +0900481 public static final int TETHERING_WIFI = 0;
Jeremy Klein9a36ec82016-01-22 14:11:45 -0800482
483 /**
484 * USB tethering type.
Chalard Jean73d9db72018-06-04 16:52:49 +0900485 * @see #startTethering(int, boolean, OnStartTetheringCallback)
Jeremy Klein9a36ec82016-01-22 14:11:45 -0800486 * @hide
487 */
488 @SystemApi
Remi NGUYEN VAN23b853b2021-02-15 18:52:06 +0900489 public static final int TETHERING_USB = 1;
Jeremy Klein9a36ec82016-01-22 14:11:45 -0800490
491 /**
492 * Bluetooth tethering type.
Chalard Jean73d9db72018-06-04 16:52:49 +0900493 * @see #startTethering(int, boolean, OnStartTetheringCallback)
Jeremy Klein9a36ec82016-01-22 14:11:45 -0800494 * @hide
495 */
496 @SystemApi
Remi NGUYEN VAN23b853b2021-02-15 18:52:06 +0900497 public static final int TETHERING_BLUETOOTH = 2;
Jeremy Klein9a36ec82016-01-22 14:11:45 -0800498
499 /**
Jimmy Chen87db1542019-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 */
markchien1f523702019-12-25 19:40:32 +0800505 public static final int TETHERING_WIFI_P2P = TetheringManager.TETHERING_WIFI_P2P;
Jimmy Chen87db1542019-07-15 18:03:23 +0800506
507 /**
Jeremy Klein9a36ec82016-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 */
markchien75721e42020-01-21 13:11:06 +0800512 public static final String EXTRA_ADD_TETHER_TYPE = TetheringConstants.EXTRA_ADD_TETHER_TYPE;
Jeremy Klein9a36ec82016-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 */
markchien75721e42020-01-21 13:11:06 +0800519 public static final String EXTRA_REM_TETHER_TYPE = TetheringConstants.EXTRA_REM_TETHER_TYPE;
Jeremy Klein9a36ec82016-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 */
markchien75721e42020-01-21 13:11:06 +0800526 public static final String EXTRA_SET_ALARM = TetheringConstants.EXTRA_SET_ALARM;
Jeremy Klein9a36ec82016-01-22 14:11:45 -0800527
528 /**
529 * Tells the TetherService to run a provision check now.
530 * @hide
531 */
markchien75721e42020-01-21 13:11:06 +0800532 public static final String EXTRA_RUN_PROVISION = TetheringConstants.EXTRA_RUN_PROVISION;
Jeremy Klein9a36ec82016-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 */
markchien75721e42020-01-21 13:11:06 +0800539 public static final String EXTRA_PROVISION_CALLBACK =
540 TetheringConstants.EXTRA_PROVISION_CALLBACK;
Jeremy Klein9a36ec82016-01-22 14:11:45 -0800541
542 /**
Robert Greenwalt26744a52013-02-15 10:56:35 -0800543 * The absence of a connection type.
Robert Greenwalt33cdcdf2011-06-02 17:30:47 -0700544 * @hide
545 */
paulhu62af6122020-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 Jean5acb7b72018-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 Jean5acb7b72018-03-08 13:54:53 +0900556 @Deprecated
Robert Greenwalt2034b912009-08-12 16:08:25 -0700557 public static final int TYPE_MOBILE = 0;
Chalard Jean5acb7b72018-03-08 13:54:53 +0900558
Robert Greenwalt2034b912009-08-12 16:08:25 -0700559 /**
Chalard Jean5acb7b72018-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 Jean5acb7b72018-03-08 13:54:53 +0900566 @Deprecated
Robert Greenwalt2034b912009-08-12 16:08:25 -0700567 public static final int TYPE_WIFI = 1;
Chalard Jean5acb7b72018-03-08 13:54:53 +0900568
Robert Greenwalt2034b912009-08-12 16:08:25 -0700569 /**
Robert Greenwalt26744a52013-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 Colitti7cc32c52015-04-23 15:32:42 +0900574 *
Chalard Jean5acb7b72018-03-08 13:54:53 +0900575 * @deprecated Applications should instead use {@link NetworkCapabilities#hasCapability} or
Lorenzo Colittiefd396e2015-04-24 17:03:31 +0900576 * {@link #requestNetwork(NetworkRequest, NetworkCallback)} to request a network that
Lorenzo Colitti7cc32c52015-04-23 15:32:42 +0900577 * provides the {@link NetworkCapabilities#NET_CAPABILITY_MMS} capability.
Robert Greenwalt2034b912009-08-12 16:08:25 -0700578 */
Aurimas Liutikasa6bb0232016-05-24 15:22:55 -0700579 @Deprecated
Robert Greenwalt2034b912009-08-12 16:08:25 -0700580 public static final int TYPE_MOBILE_MMS = 2;
Chalard Jean5acb7b72018-03-08 13:54:53 +0900581
Robert Greenwalt2034b912009-08-12 16:08:25 -0700582 /**
Robert Greenwalt26744a52013-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 Colitti7cc32c52015-04-23 15:32:42 +0900587 *
Chalard Jean5acb7b72018-03-08 13:54:53 +0900588 * @deprecated Applications should instead use {@link NetworkCapabilities#hasCapability} or
Lorenzo Colittiefd396e2015-04-24 17:03:31 +0900589 * {@link #requestNetwork(NetworkRequest, NetworkCallback)} to request a network that
Lorenzo Colitti7cc32c52015-04-23 15:32:42 +0900590 * provides the {@link NetworkCapabilities#NET_CAPABILITY_SUPL} capability.
Robert Greenwalt2034b912009-08-12 16:08:25 -0700591 */
Aurimas Liutikasa6bb0232016-05-24 15:22:55 -0700592 @Deprecated
Robert Greenwalt2034b912009-08-12 16:08:25 -0700593 public static final int TYPE_MOBILE_SUPL = 3;
Chalard Jean5acb7b72018-03-08 13:54:53 +0900594
Robert Greenwalt2034b912009-08-12 16:08:25 -0700595 /**
Robert Greenwalt26744a52013-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 Jean5acb7b72018-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 Jean5acb7b72018-03-08 13:54:53 +0900605 @Deprecated
Robert Greenwalt2034b912009-08-12 16:08:25 -0700606 public static final int TYPE_MOBILE_DUN = 4;
Chalard Jean5acb7b72018-03-08 13:54:53 +0900607
Robert Greenwalt2034b912009-08-12 16:08:25 -0700608 /**
Robert Greenwalt26744a52013-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 Colitti7cc32c52015-04-23 15:32:42 +0900611 * is different.
612 *
Chalard Jean5acb7b72018-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 Liutikasa6bb0232016-05-24 15:22:55 -0700617 @Deprecated
Robert Greenwalt2034b912009-08-12 16:08:25 -0700618 public static final int TYPE_MOBILE_HIPRI = 5;
Chalard Jean5acb7b72018-03-08 13:54:53 +0900619
jshbfa81722010-03-11 15:04:43 -0800620 /**
Chalard Jean5acb7b72018-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 Jean5acb7b72018-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 Jean5acb7b72018-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 Jean5acb7b72018-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 Wang7d5f3782020-07-28 13:53:09 +0800641 * Fake data connection. This should not be used on shipping devices.
Chalard Jean5acb7b72018-03-08 13:54:53 +0900642 * @deprecated This is not used any more.
Robert Greenwaltf76c55d2011-04-22 15:28:18 -0700643 */
Chalard Jean5acb7b72018-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 Jean5acb7b72018-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 Jean5acb7b72018-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 Greenwalt26744a52013-02-15 10:56:35 -0800658 * Over the air Administration.
Chalard Jean5acb7b72018-03-08 13:54:53 +0900659 * @deprecated Use {@link NetworkCapabilities} instead.
Wink Savilleb7c92c72011-03-12 14:52:01 -0800660 * {@hide}
661 */
Chalard Jean5acb7b72018-03-08 13:54:53 +0900662 @Deprecated
Chalard Jeanaa91c9d2019-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 Greenwalt26744a52013-02-15 10:56:35 -0800667 * IP Multimedia Subsystem.
Chalard Jean5acb7b72018-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 Jean5acb7b72018-03-08 13:54:53 +0900671 @Deprecated
Mathew Inwood0b8f8612018-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 Greenwalt26744a52013-02-15 10:56:35 -0800676 * Carrier Branded Services.
Chalard Jean5acb7b72018-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 Jean5acb7b72018-03-08 13:54:53 +0900680 @Deprecated
Chalard Jeanaa91c9d2019-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 Jean5acb7b72018-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 Jean5acb7b72018-03-08 13:54:53 +0900690 @Deprecated
paulhu028732e2020-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 Saville70dbdcc2013-07-29 15:00:57 -0700694 /**
695 * The network to use for initially attaching to the network
Chalard Jean5acb7b72018-03-08 13:54:53 +0900696 * @deprecated Use {@link NetworkCapabilities#NET_CAPABILITY_IA} instead.
Wink Saville70dbdcc2013-07-29 15:00:57 -0700697 * {@hide}
698 */
Chalard Jean5acb7b72018-03-08 13:54:53 +0900699 @Deprecated
Mathew Inwood0b8f8612018-08-08 14:52:47 +0100700 @UnsupportedAppUsage
Wink Saville70dbdcc2013-07-29 15:00:57 -0700701 public static final int TYPE_MOBILE_IA = 14;
repo syncf5de5572011-07-29 23:55:49 -0700702
Lorenzo Colitti7cc32c52015-04-23 15:32:42 +0900703 /**
Robert Greenwalt9db5f3c2015-07-09 14:49:35 -0700704 * Emergency PDN connection for emergency services. This
705 * may include IMS and MMS in emergency situations.
Chalard Jean5acb7b72018-03-08 13:54:53 +0900706 * @deprecated Use {@link NetworkCapabilities#NET_CAPABILITY_EIMS} instead.
Ram201d45f2014-06-26 11:03:44 -0700707 * {@hide}
708 */
Chalard Jean5acb7b72018-03-08 13:54:53 +0900709 @Deprecated
Chalard Jeanaa91c9d2019-04-09 15:46:21 +0900710 @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P, trackingBug = 130143562)
Ram201d45f2014-06-26 11:03:44 -0700711 public static final int TYPE_MOBILE_EMERGENCY = 15;
712
Hui Lu07f29332014-01-15 11:05:36 -0500713 /**
714 * The network that uses proxy to achieve connectivity.
Chalard Jean5acb7b72018-03-08 13:54:53 +0900715 * @deprecated Use {@link NetworkCapabilities} instead.
Hui Lu07f29332014-01-15 11:05:36 -0500716 * {@hide}
717 */
Chalard Jean5acb7b72018-03-08 13:54:53 +0900718 @Deprecated
Remi NGUYEN VANdaeafee2020-11-30 19:23:45 +0900719 @SystemApi
Hui Lu07f29332014-01-15 11:05:36 -0500720 public static final int TYPE_PROXY = 16;
Wink Saville70dbdcc2013-07-29 15:00:57 -0700721
Robert Greenwaltb2a03d12014-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 Jean5acb7b72018-03-08 13:54:53 +0900725 * @deprecated Applications should use {@link NetworkCapabilities#TRANSPORT_VPN} instead.
Robert Greenwaltb2a03d12014-07-07 17:09:01 -0700726 */
Chalard Jean5acb7b72018-03-08 13:54:53 +0900727 @Deprecated
Robert Greenwaltb2a03d12014-07-07 17:09:01 -0700728 public static final int TYPE_VPN = 17;
Hui Lu07f29332014-01-15 11:05:36 -0500729
Benedict Wong80156022018-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 Greenwaltb2a03d12014-07-07 17:09:01 -0700738
Chalard Jean74fe1c72020-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 Jean1f42df12019-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 Greenwaltb2a03d12014-07-07 17:09:01 -0700775 /** {@hide} */
Benedict Wong80156022018-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 Benichi37d5c3c2017-06-20 14:07:59 +0900781 private static final int MIN_NETWORK_TYPE = TYPE_MOBILE;
782
Jianzheng Zhoua8aa1602012-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 Greenwalt26744a52013-02-15 10:56:35 -0800789 * the hierarchy. Instead, the default is defined by the
Jianzheng Zhoua8aa1602012-11-16 13:45:20 +0800790 * networkAttributes in config.xml. You can determine
Robert Greenwalt1ee3d2c2012-12-07 09:56:50 -0800791 * the current value by calling {@link #getNetworkPreference()}
Jianzheng Zhoua8aa1602012-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 Sharkey5eccd9c2012-09-26 22:03:49 -0700797 /**
Robert Greenwalta1402df2014-03-19 17:56:12 -0700798 * @hide
799 */
Hugo Benichibcc81df2017-06-20 14:10:14 +0900800 public static final int REQUEST_ID_UNSET = 0;
Robert Greenwaltc34f83b2014-06-08 16:42:59 -0700801
Paul Jensen0478ace2014-07-11 12:28:19 -0400802 /**
Hugo Benichi1c027fe2017-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 Jean73d9db72018-06-04 16:52:49 +0900805 * registered from those that were already unregistered.
Hugo Benichi1c027fe2017-06-17 13:14:12 +0900806 * @hide
807 */
Hugo Benichibcc81df2017-06-20 14:10:14 +0900808 private static final NetworkRequest ALREADY_UNREGISTERED =
Hugo Benichi1c027fe2017-06-17 13:14:12 +0900809 new NetworkRequest.Builder().clearCapabilities().build();
810
811 /**
Paul Jensen0478ace2014-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 Kline1ecdd962017-10-30 15:29:44 +0900818 /**
Sudheer Shanka457dfd52021-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 Colitti60104f52021-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 Shanka457dfd52021-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 Colittia37eaff2021-03-25 23:17:36 +0900911 BLOCKED_REASON_LOCKDOWN_VPN,
Sudheer Shanka457dfd52021-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 Colitti60104f52021-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 Jeanaa91c9d2019-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 Colitti0dd4b6f2021-02-04 17:32:07 +0900928
Paul Jensen12131352014-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 Colitti9b026fa2015-07-29 11:41:21 +0900937 private final Context mContext;
938
Automerger Merge Worker9b0c0c62020-03-06 00:38:43 +0000939 private final TetheringManager mTetheringManager;
Dianne Hackbornb4b09e82014-02-26 16:20:52 -0800940
Robert Greenwalt26744a52013-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 Jensen1c9f2e42015-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 Greenwalt26744a52013-02-15 10:56:35 -0800947 */
Aurimas Liutikasa6bb0232016-05-24 15:22:55 -0700948 @Deprecated
Jeff Sharkey21062e72011-05-28 20:56:34 -0700949 public static boolean isNetworkTypeValid(int networkType) {
Hugo Benichi37d5c3c2017-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 Greenwalt26744a52013-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 Jean5acb7b72018-03-08 13:54:53 +0900959 * @deprecated Types are deprecated. Use {@link NetworkCapabilities} instead.
Robert Greenwalt26744a52013-02-15 10:56:35 -0800960 * {@hide}
961 */
Chalard Jean5acb7b72018-03-08 13:54:53 +0900962 @Deprecated
Mathew Inwoodd078d3d2020-10-27 11:47:29 +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 Benichi37d5c3c2017-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 Saville70dbdcc2013-07-29 15:00:57 -0700996 case TYPE_MOBILE_IA:
997 return "MOBILE_IA";
Ram201d45f2014-06-26 11:03:44 -0700998 case TYPE_MOBILE_EMERGENCY:
999 return "MOBILE_EMERGENCY";
Hui Lu07f29332014-01-15 11:05:36 -05001000 case TYPE_PROXY:
1001 return "PROXY";
Erik Klineb1ff7002014-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 Greenwalt26744a52013-02-15 10:56:35 -08001009 /**
Aaron Huangdfba5d12020-06-27 07:18:23 +08001010 * @hide
Aaron Huangdfba5d12020-06-27 07:18:23 +08001011 */
lucaslin8c121bf2021-03-17 14:16:01 +08001012 @SystemApi(client = MODULE_LIBRARIES)
Aaron Huangdfba5d12020-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 Greenwalt26744a52013-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 Jean5acb7b72018-03-08 13:54:53 +09001026 * @deprecated Types are deprecated. Use {@link NetworkCapabilities} instead.
Robert Greenwalt26744a52013-02-15 10:56:35 -08001027 * {@hide}
1028 */
Chalard Jean5acb7b72018-03-08 13:54:53 +09001029 @Deprecated
Chalard Jeanaa91c9d2019-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 Saville70dbdcc2013-07-29 15:00:57 -07001041 case TYPE_MOBILE_IA:
Ram201d45f2014-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 Greenwalt26744a52013-02-15 10:56:35 -08001049 /**
Jeff Sharkeye9bda1d2013-06-04 12:29:00 -07001050 * Checks if the given network type is backed by a Wi-Fi radio.
1051 *
Chalard Jean5acb7b72018-03-08 13:54:53 +09001052 * @deprecated Types are deprecated. Use {@link NetworkCapabilities} instead.
Jeff Sharkeye9bda1d2013-06-04 12:29:00 -07001053 * @hide
1054 */
Chalard Jean5acb7b72018-03-08 13:54:53 +09001055 @Deprecated
Jeff Sharkeye9bda1d2013-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 Jean03433052021-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 Jean560d4032021-03-17 14:33:24 +09001071 @SystemApi(client = MODULE_LIBRARIES)
Chalard Jean03433052021-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 Jean560d4032021-03-17 14:33:24 +09001081 @SystemApi(client = MODULE_LIBRARIES)
Chalard Jean03433052021-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 Greenwalt26744a52013-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 Greenwalt26744a52013-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 Greenwaltd0ebdbb2014-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 Greenwalt26744a52013-02-15 10:56:35 -08001104 */
Aurimas Liutikasa6bb0232016-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 Greenwalt26744a52013-02-15 10:56:35 -08001109 /**
1110 * Retrieves the current preferred network type.
Robert Greenwalt26744a52013-02-15 10:56:35 -08001111 *
1112 * @return an integer representing the preferred network type
1113 *
Robert Greenwaltd0ebdbb2014-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 Greenwalt26744a52013-02-15 10:56:35 -08001118 */
Aurimas Liutikasa6bb0232016-05-24 15:22:55 -07001119 @Deprecated
Jeff Sharkey2ac62992017-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 Greenwaltd0ebdbb2014-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 Greenwalt26744a52013-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 Jean7c85ba42018-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 Greenwalt26744a52013-02-15 10:56:35 -08001135 *
1136 * @return a {@link NetworkInfo} object for the current default network
Paul Jensena9208b92015-02-13 14:18:39 -05001137 * or {@code null} if no default network is currently active
junyulai5c2f6262018-12-13 12:47:51 +08001138 * @deprecated See {@link NetworkInfo}.
Jeff Sharkeyd00b1302012-04-12 18:34:54 -07001139 */
junyulai5c2f6262018-12-13 12:47:51 +08001140 @Deprecated
Jeff Sharkey2ac62992017-04-24 11:18:03 -06001141 @RequiresPermission(android.Manifest.permission.ACCESS_NETWORK_STATE)
Chalard Jean42a9c292019-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 Sharkeya6066fd2016-03-01 19:27:23 -07001147 throw e.rethrowFromSystemServer();
The Android Open Source Project28527d22009-03-03 19:31:44 -08001148 }
1149 }
1150
Robert Greenwalt26744a52013-02-15 10:56:35 -08001151 /**
Paul Jensenc2569432015-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 Jensenc2569432015-02-13 14:18:39 -05001160 */
Jeff Sharkey2ac62992017-04-24 11:18:03 -06001161 @RequiresPermission(android.Manifest.permission.ACCESS_NETWORK_STATE)
Chalard Jean42a9c292019-01-07 19:26:34 +09001162 @Nullable
Paul Jensenc2569432015-02-13 14:18:39 -05001163 public Network getActiveNetwork() {
1164 try {
1165 return mService.getActiveNetwork();
1166 } catch (RemoteException e) {
Jeff Sharkeya6066fd2016-03-01 19:27:23 -07001167 throw e.rethrowFromSystemServer();
Paul Jensenc2569432015-02-13 14:18:39 -05001168 }
1169 }
1170
1171 /**
Robin Leeda4d2e22016-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 Leeda4d2e22016-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
lifr5f1c1102021-03-30 21:04:53 +08001180 *
1181 * @hide
Robin Leeda4d2e22016-03-24 12:07:00 +00001182 */
paulhuec0a9632019-08-12 16:25:11 +08001183 @RequiresPermission(android.Manifest.permission.NETWORK_STACK)
Chalard Jean42a9c292019-01-07 19:26:34 +09001184 @Nullable
Robin Leeda4d2e22016-03-24 12:07:00 +00001185 public Network getActiveNetworkForUid(int uid) {
Jeff Sharkey833c35b2016-04-28 15:33:18 -06001186 return getActiveNetworkForUid(uid, false);
1187 }
1188
1189 /** {@hide} */
1190 public Network getActiveNetworkForUid(int uid, boolean ignoreBlocked) {
Robin Leeda4d2e22016-03-24 12:07:00 +00001191 try {
Jeff Sharkey833c35b2016-04-28 15:33:18 -06001192 return mService.getActiveNetworkForUid(uid, ignoreBlocked);
Robin Leeda4d2e22016-03-24 12:07:00 +00001193 } catch (RemoteException e) {
1194 throw e.rethrowFromSystemServer();
1195 }
1196 }
1197
1198 /**
Lorenzo Colittie8ce2052020-12-12 00:51:11 +09001199 * Adds or removes a requirement for given UID ranges to use the VPN.
1200 *
1201 * If set to {@code true}, informs the system that the UIDs in the specified ranges must not
1202 * have any connectivity except if a VPN is connected and applies to the UIDs, or if the UIDs
1203 * otherwise have permission to bypass the VPN (e.g., because they have the
1204 * {@link android.Manifest.permission.CONNECTIVITY_USE_RESTRICTED_NETWORKS} permission, or when
1205 * using a socket protected by a method such as {@link VpnService#protect(DatagramSocket)}. If
1206 * set to {@code false}, a previously-added restriction is removed.
1207 * <p>
1208 * Each of the UID ranges specified by this method is added and removed as is, and no processing
1209 * is performed on the ranges to de-duplicate, merge, split, or intersect them. In order to
1210 * remove a previously-added range, the exact range must be removed as is.
1211 * <p>
1212 * The changes are applied asynchronously and may not have been applied by the time the method
1213 * returns. Apps will be notified about any changes that apply to them via
1214 * {@link NetworkCallback#onBlockedStatusChanged} callbacks called after the changes take
1215 * effect.
1216 * <p>
1217 * This method should be called only by the VPN code.
1218 *
1219 * @param ranges the UID ranges to restrict
1220 * @param requireVpn whether the specified UID ranges must use a VPN
1221 *
Lorenzo Colittie8ce2052020-12-12 00:51:11 +09001222 * @hide
1223 */
1224 @RequiresPermission(anyOf = {
1225 NetworkStack.PERMISSION_MAINLINE_NETWORK_STACK,
lucaslin5140e482021-03-22 11:51:27 +08001226 android.Manifest.permission.NETWORK_STACK,
1227 android.Manifest.permission.NETWORK_SETTINGS})
1228 @SystemApi(client = MODULE_LIBRARIES)
Lorenzo Colittie8ce2052020-12-12 00:51:11 +09001229 public void setRequireVpnForUids(boolean requireVpn,
1230 @NonNull Collection<Range<Integer>> ranges) {
1231 Objects.requireNonNull(ranges);
1232 // The Range class is not parcelable. Convert to UidRange, which is what is used internally.
1233 // This method is not necessarily expected to be used outside the system server, so
1234 // parceling may not be necessary, but it could be used out-of-process, e.g., by the network
1235 // stack process, or by tests.
1236 UidRange[] rangesArray = new UidRange[ranges.size()];
1237 int index = 0;
1238 for (Range<Integer> range : ranges) {
1239 rangesArray[index++] = new UidRange(range.getLower(), range.getUpper());
1240 }
1241 try {
1242 mService.setRequireVpnForUids(requireVpn, rangesArray);
1243 } catch (RemoteException e) {
1244 throw e.rethrowFromSystemServer();
1245 }
1246 }
1247
1248 /**
Lorenzo Colitti549e9ac2021-01-15 01:29:01 +09001249 * Informs ConnectivityService of whether the legacy lockdown VPN, as implemented by
1250 * LockdownVpnTracker, is in use. This is deprecated for new devices starting from Android 12
1251 * but is still supported for backwards compatibility.
1252 * <p>
1253 * This type of VPN is assumed always to use the system default network, and must always declare
1254 * exactly one underlying network, which is the network that was the default when the VPN
1255 * connected.
1256 * <p>
1257 * Calling this method with {@code true} enables legacy behaviour, specifically:
1258 * <ul>
1259 * <li>Any VPN that applies to userId 0 behaves specially with respect to deprecated
1260 * {@link #CONNECTIVITY_ACTION} broadcasts. Any such broadcasts will have the state in the
1261 * {@link #EXTRA_NETWORK_INFO} replaced by state of the VPN network. Also, any time the VPN
1262 * connects, a {@link #CONNECTIVITY_ACTION} broadcast will be sent for the network
1263 * underlying the VPN.</li>
1264 * <li>Deprecated APIs that return {@link NetworkInfo} objects will have their state
1265 * similarly replaced by the VPN network state.</li>
1266 * <li>Information on current network interfaces passed to NetworkStatsService will not
1267 * include any VPN interfaces.</li>
1268 * </ul>
1269 *
1270 * @param enabled whether legacy lockdown VPN is enabled or disabled
1271 *
Lorenzo Colitti549e9ac2021-01-15 01:29:01 +09001272 * @hide
1273 */
1274 @RequiresPermission(anyOf = {
1275 NetworkStack.PERMISSION_MAINLINE_NETWORK_STACK,
lucaslin5140e482021-03-22 11:51:27 +08001276 android.Manifest.permission.NETWORK_STACK,
Lorenzo Colitti549e9ac2021-01-15 01:29:01 +09001277 android.Manifest.permission.NETWORK_SETTINGS})
lucaslin5140e482021-03-22 11:51:27 +08001278 @SystemApi(client = MODULE_LIBRARIES)
Lorenzo Colitti549e9ac2021-01-15 01:29:01 +09001279 public void setLegacyLockdownVpnEnabled(boolean enabled) {
1280 try {
1281 mService.setLegacyLockdownVpnEnabled(enabled);
1282 } catch (RemoteException e) {
1283 throw e.rethrowFromSystemServer();
1284 }
1285 }
1286
1287 /**
Robert Greenwalt26744a52013-02-15 10:56:35 -08001288 * Returns details about the currently active default data network
1289 * for a given uid. This is for internal use only to avoid spying
1290 * other apps.
1291 *
1292 * @return a {@link NetworkInfo} object for the current default network
1293 * for the given uid or {@code null} if no default network is
1294 * available for the specified uid.
1295 *
Robert Greenwalt26744a52013-02-15 10:56:35 -08001296 * {@hide}
1297 */
paulhuec0a9632019-08-12 16:25:11 +08001298 @RequiresPermission(android.Manifest.permission.NETWORK_STACK)
Mathew Inwoodd078d3d2020-10-27 11:47:29 +00001299 @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.R, trackingBug = 170729553)
Jeff Sharkey921ebf22011-05-19 17:12:49 -07001300 public NetworkInfo getActiveNetworkInfoForUid(int uid) {
Jeff Sharkey833c35b2016-04-28 15:33:18 -06001301 return getActiveNetworkInfoForUid(uid, false);
1302 }
1303
1304 /** {@hide} */
1305 public NetworkInfo getActiveNetworkInfoForUid(int uid, boolean ignoreBlocked) {
Jeff Sharkey921ebf22011-05-19 17:12:49 -07001306 try {
Jeff Sharkey833c35b2016-04-28 15:33:18 -06001307 return mService.getActiveNetworkInfoForUid(uid, ignoreBlocked);
Jeff Sharkey921ebf22011-05-19 17:12:49 -07001308 } catch (RemoteException e) {
Jeff Sharkeya6066fd2016-03-01 19:27:23 -07001309 throw e.rethrowFromSystemServer();
Jeff Sharkey921ebf22011-05-19 17:12:49 -07001310 }
1311 }
1312
Robert Greenwalt26744a52013-02-15 10:56:35 -08001313 /**
1314 * Returns connection status information about a particular
1315 * network type.
1316 *
1317 * @param networkType integer specifying which networkType in
1318 * which you're interested.
1319 * @return a {@link NetworkInfo} object for the requested
1320 * network type or {@code null} if the type is not
Chalard Jean7c85ba42018-03-29 17:45:24 +09001321 * supported by the device. If {@code networkType} is
1322 * TYPE_VPN and a VPN is active for the calling app,
1323 * then this method will try to return one of the
1324 * underlying networks for the VPN or null if the
1325 * VPN agent didn't specify any.
Robert Greenwalt26744a52013-02-15 10:56:35 -08001326 *
Paul Jensena8e6dc62015-03-18 12:23:02 -04001327 * @deprecated This method does not support multiple connected networks
1328 * of the same type. Use {@link #getAllNetworks} and
1329 * {@link #getNetworkInfo(android.net.Network)} instead.
Robert Greenwalt26744a52013-02-15 10:56:35 -08001330 */
Aurimas Liutikasa6bb0232016-05-24 15:22:55 -07001331 @Deprecated
Jeff Sharkey2ac62992017-04-24 11:18:03 -06001332 @RequiresPermission(android.Manifest.permission.ACCESS_NETWORK_STATE)
Chalard Jean42a9c292019-01-07 19:26:34 +09001333 @Nullable
The Android Open Source Project28527d22009-03-03 19:31:44 -08001334 public NetworkInfo getNetworkInfo(int networkType) {
1335 try {
1336 return mService.getNetworkInfo(networkType);
1337 } catch (RemoteException e) {
Jeff Sharkeya6066fd2016-03-01 19:27:23 -07001338 throw e.rethrowFromSystemServer();
The Android Open Source Project28527d22009-03-03 19:31:44 -08001339 }
1340 }
1341
Robert Greenwalt26744a52013-02-15 10:56:35 -08001342 /**
Robert Greenwalt701a5c32014-06-23 11:40:00 -07001343 * Returns connection status information about a particular
1344 * Network.
1345 *
1346 * @param network {@link Network} specifying which network
1347 * in which you're interested.
1348 * @return a {@link NetworkInfo} object for the requested
1349 * network or {@code null} if the {@code Network}
1350 * is not valid.
junyulai5c2f6262018-12-13 12:47:51 +08001351 * @deprecated See {@link NetworkInfo}.
Robert Greenwalt701a5c32014-06-23 11:40:00 -07001352 */
junyulai5c2f6262018-12-13 12:47:51 +08001353 @Deprecated
Jeff Sharkey2ac62992017-04-24 11:18:03 -06001354 @RequiresPermission(android.Manifest.permission.ACCESS_NETWORK_STATE)
Chalard Jean42a9c292019-01-07 19:26:34 +09001355 @Nullable
1356 public NetworkInfo getNetworkInfo(@Nullable Network network) {
Jeff Sharkey833c35b2016-04-28 15:33:18 -06001357 return getNetworkInfoForUid(network, Process.myUid(), false);
1358 }
1359
1360 /** {@hide} */
1361 public NetworkInfo getNetworkInfoForUid(Network network, int uid, boolean ignoreBlocked) {
Robert Greenwalt701a5c32014-06-23 11:40:00 -07001362 try {
Jeff Sharkey833c35b2016-04-28 15:33:18 -06001363 return mService.getNetworkInfoForUid(network, uid, ignoreBlocked);
Robert Greenwalt701a5c32014-06-23 11:40:00 -07001364 } catch (RemoteException e) {
Jeff Sharkeya6066fd2016-03-01 19:27:23 -07001365 throw e.rethrowFromSystemServer();
Robert Greenwalt701a5c32014-06-23 11:40:00 -07001366 }
1367 }
1368
1369 /**
Robert Greenwalt26744a52013-02-15 10:56:35 -08001370 * Returns connection status information about all network
1371 * types supported by the device.
1372 *
1373 * @return an array of {@link NetworkInfo} objects. Check each
1374 * {@link NetworkInfo#getType} for which type each applies.
1375 *
Paul Jensena8e6dc62015-03-18 12:23:02 -04001376 * @deprecated This method does not support multiple connected networks
1377 * of the same type. Use {@link #getAllNetworks} and
1378 * {@link #getNetworkInfo(android.net.Network)} instead.
Robert Greenwalt26744a52013-02-15 10:56:35 -08001379 */
Aurimas Liutikasa6bb0232016-05-24 15:22:55 -07001380 @Deprecated
Jeff Sharkey2ac62992017-04-24 11:18:03 -06001381 @RequiresPermission(android.Manifest.permission.ACCESS_NETWORK_STATE)
Chalard Jean42a9c292019-01-07 19:26:34 +09001382 @NonNull
The Android Open Source Project28527d22009-03-03 19:31:44 -08001383 public NetworkInfo[] getAllNetworkInfo() {
1384 try {
1385 return mService.getAllNetworkInfo();
1386 } catch (RemoteException e) {
Jeff Sharkeya6066fd2016-03-01 19:27:23 -07001387 throw e.rethrowFromSystemServer();
The Android Open Source Project28527d22009-03-03 19:31:44 -08001388 }
1389 }
1390
Robert Greenwalt26744a52013-02-15 10:56:35 -08001391 /**
junyulai57840802021-03-03 12:09:05 +08001392 * Return a list of {@link NetworkStateSnapshot}s, one for each network that is currently
1393 * connected.
1394 * @hide
1395 */
1396 @SystemApi(client = MODULE_LIBRARIES)
1397 @RequiresPermission(anyOf = {
1398 NetworkStack.PERMISSION_MAINLINE_NETWORK_STACK,
1399 android.Manifest.permission.NETWORK_STACK,
1400 android.Manifest.permission.NETWORK_SETTINGS})
1401 @NonNull
Aaron Huang20605e52021-04-17 13:46:25 +08001402 public List<NetworkStateSnapshot> getAllNetworkStateSnapshots() {
junyulai57840802021-03-03 12:09:05 +08001403 try {
Aaron Huang20605e52021-04-17 13:46:25 +08001404 return mService.getAllNetworkStateSnapshots();
junyulai57840802021-03-03 12:09:05 +08001405 } catch (RemoteException e) {
1406 throw e.rethrowFromSystemServer();
1407 }
1408 }
1409
1410 /**
Lorenzo Colitti2b2d73c2014-08-22 17:10:50 -07001411 * Returns the {@link Network} object currently serving a given type, or
1412 * null if the given type is not connected.
1413 *
Lorenzo Colitti2b2d73c2014-08-22 17:10:50 -07001414 * @hide
Paul Jensena8e6dc62015-03-18 12:23:02 -04001415 * @deprecated This method does not support multiple connected networks
1416 * of the same type. Use {@link #getAllNetworks} and
1417 * {@link #getNetworkInfo(android.net.Network)} instead.
Lorenzo Colitti2b2d73c2014-08-22 17:10:50 -07001418 */
Aurimas Liutikasa6bb0232016-05-24 15:22:55 -07001419 @Deprecated
Jeff Sharkey2ac62992017-04-24 11:18:03 -06001420 @RequiresPermission(android.Manifest.permission.ACCESS_NETWORK_STATE)
Mathew Inwood0b8f8612018-08-08 14:52:47 +01001421 @UnsupportedAppUsage
Lorenzo Colitti2b2d73c2014-08-22 17:10:50 -07001422 public Network getNetworkForType(int networkType) {
1423 try {
1424 return mService.getNetworkForType(networkType);
1425 } catch (RemoteException e) {
Jeff Sharkeya6066fd2016-03-01 19:27:23 -07001426 throw e.rethrowFromSystemServer();
Lorenzo Colitti2b2d73c2014-08-22 17:10:50 -07001427 }
1428 }
1429
1430 /**
Robert Greenwalt701a5c32014-06-23 11:40:00 -07001431 * Returns an array of all {@link Network} currently tracked by the
1432 * framework.
Paul Jensen19b3ee72015-05-06 11:10:18 -04001433 *
1434 * @return an array of {@link Network} objects.
Robert Greenwalt701a5c32014-06-23 11:40:00 -07001435 */
Jeff Sharkey2ac62992017-04-24 11:18:03 -06001436 @RequiresPermission(android.Manifest.permission.ACCESS_NETWORK_STATE)
Chalard Jean42a9c292019-01-07 19:26:34 +09001437 @NonNull
Robert Greenwalt701a5c32014-06-23 11:40:00 -07001438 public Network[] getAllNetworks() {
1439 try {
1440 return mService.getAllNetworks();
1441 } catch (RemoteException e) {
Jeff Sharkeya6066fd2016-03-01 19:27:23 -07001442 throw e.rethrowFromSystemServer();
Robert Greenwalt701a5c32014-06-23 11:40:00 -07001443 }
1444 }
1445
1446 /**
Roshan Pius7992afd2020-12-22 15:10:42 -08001447 * Returns an array of {@link NetworkCapabilities} objects, representing
Lorenzo Colitti8db934a2014-11-28 11:21:30 +09001448 * the Networks that applications run by the given user will use by default.
1449 * @hide
1450 */
Mathew Inwood0b8f8612018-08-08 14:52:47 +01001451 @UnsupportedAppUsage
Lorenzo Colitti8db934a2014-11-28 11:21:30 +09001452 public NetworkCapabilities[] getDefaultNetworkCapabilitiesForUser(int userId) {
1453 try {
Qingxi Libf6bf082020-01-08 12:51:49 -08001454 return mService.getDefaultNetworkCapabilitiesForUser(
Roshan Piusc97d8062020-12-17 14:53:09 -08001455 userId, mContext.getOpPackageName(), getAttributionTag());
Lorenzo Colitti8db934a2014-11-28 11:21:30 +09001456 } catch (RemoteException e) {
Jeff Sharkeya6066fd2016-03-01 19:27:23 -07001457 throw e.rethrowFromSystemServer();
Lorenzo Colitti8db934a2014-11-28 11:21:30 +09001458 }
1459 }
1460
1461 /**
Robert Greenwalt26744a52013-02-15 10:56:35 -08001462 * Returns the IP information for the current default network.
1463 *
1464 * @return a {@link LinkProperties} object describing the IP info
1465 * for the current default network, or {@code null} if there
1466 * is no current default network.
1467 *
Robert Greenwalt26744a52013-02-15 10:56:35 -08001468 * {@hide}
Chalard Jeancc47b522019-01-11 16:47:53 +09001469 * @deprecated please use {@link #getLinkProperties(Network)} on the return
1470 * value of {@link #getActiveNetwork()} instead. In particular,
1471 * this method will return non-null LinkProperties even if the
1472 * app is blocked by policy from using this network.
Robert Greenwalt26744a52013-02-15 10:56:35 -08001473 */
Jeff Sharkey2ac62992017-04-24 11:18:03 -06001474 @RequiresPermission(android.Manifest.permission.ACCESS_NETWORK_STATE)
Chalard Jeancc47b522019-01-11 16:47:53 +09001475 @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P, trackingBug = 109783091)
Robert Greenwalt9f0ee4f2010-09-14 09:18:02 -07001476 public LinkProperties getActiveLinkProperties() {
1477 try {
1478 return mService.getActiveLinkProperties();
1479 } catch (RemoteException e) {
Jeff Sharkeya6066fd2016-03-01 19:27:23 -07001480 throw e.rethrowFromSystemServer();
Robert Greenwalt9f0ee4f2010-09-14 09:18:02 -07001481 }
1482 }
1483
Robert Greenwalt26744a52013-02-15 10:56:35 -08001484 /**
1485 * Returns the IP information for a given network type.
1486 *
1487 * @param networkType the network type of interest.
1488 * @return a {@link LinkProperties} object describing the IP info
1489 * for the given networkType, or {@code null} if there is
1490 * no current default network.
1491 *
Robert Greenwalt26744a52013-02-15 10:56:35 -08001492 * {@hide}
Paul Jensena8e6dc62015-03-18 12:23:02 -04001493 * @deprecated This method does not support multiple connected networks
1494 * of the same type. Use {@link #getAllNetworks},
1495 * {@link #getNetworkInfo(android.net.Network)}, and
1496 * {@link #getLinkProperties(android.net.Network)} instead.
Robert Greenwalt26744a52013-02-15 10:56:35 -08001497 */
Aurimas Liutikasa6bb0232016-05-24 15:22:55 -07001498 @Deprecated
Jeff Sharkey2ac62992017-04-24 11:18:03 -06001499 @RequiresPermission(android.Manifest.permission.ACCESS_NETWORK_STATE)
Chalard Jeanaa91c9d2019-04-09 15:46:21 +09001500 @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P, trackingBug = 130143562)
Robert Greenwalt9f0ee4f2010-09-14 09:18:02 -07001501 public LinkProperties getLinkProperties(int networkType) {
1502 try {
Robert Greenwalt2bebbfb2014-03-26 16:47:06 -07001503 return mService.getLinkPropertiesForType(networkType);
1504 } catch (RemoteException e) {
Jeff Sharkeya6066fd2016-03-01 19:27:23 -07001505 throw e.rethrowFromSystemServer();
Robert Greenwalt2bebbfb2014-03-26 16:47:06 -07001506 }
1507 }
1508
Robert Greenwaltd0ebdbb2014-05-18 23:07:25 -07001509 /**
1510 * Get the {@link LinkProperties} for the given {@link Network}. This
1511 * will return {@code null} if the network is unknown.
1512 *
1513 * @param network The {@link Network} object identifying the network in question.
1514 * @return The {@link LinkProperties} for the network, or {@code null}.
Paul Jensen19b3ee72015-05-06 11:10:18 -04001515 */
Jeff Sharkey2ac62992017-04-24 11:18:03 -06001516 @RequiresPermission(android.Manifest.permission.ACCESS_NETWORK_STATE)
Chalard Jean42a9c292019-01-07 19:26:34 +09001517 @Nullable
1518 public LinkProperties getLinkProperties(@Nullable Network network) {
Robert Greenwalt2bebbfb2014-03-26 16:47:06 -07001519 try {
1520 return mService.getLinkProperties(network);
1521 } catch (RemoteException e) {
Jeff Sharkeya6066fd2016-03-01 19:27:23 -07001522 throw e.rethrowFromSystemServer();
Robert Greenwalt2bebbfb2014-03-26 16:47:06 -07001523 }
1524 }
1525
Robert Greenwaltd0ebdbb2014-05-18 23:07:25 -07001526 /**
Roshan Pius7992afd2020-12-22 15:10:42 -08001527 * Get the {@link NetworkCapabilities} for the given {@link Network}. This
Chalard Jean39b21a32021-04-30 20:22:10 +09001528 * will return {@code null} if the network is unknown or if the |network| argument is null.
Robert Greenwaltd0ebdbb2014-05-18 23:07:25 -07001529 *
Roshan Pius7992afd2020-12-22 15:10:42 -08001530 * This will remove any location sensitive data in {@link TransportInfo} embedded in
1531 * {@link NetworkCapabilities#getTransportInfo()}. Some transport info instances like
1532 * {@link android.net.wifi.WifiInfo} contain location sensitive information. Retrieving
1533 * this location sensitive information (subject to app's location permissions) will be
1534 * noted by system. To include any location sensitive data in {@link TransportInfo},
1535 * use a {@link NetworkCallback} with
1536 * {@link NetworkCallback#FLAG_INCLUDE_LOCATION_INFO} flag.
1537 *
Robert Greenwaltd0ebdbb2014-05-18 23:07:25 -07001538 * @param network The {@link Network} object identifying the network in question.
Roshan Pius7992afd2020-12-22 15:10:42 -08001539 * @return The {@link NetworkCapabilities} for the network, or {@code null}.
Robert Greenwaltd0ebdbb2014-05-18 23:07:25 -07001540 */
Jeff Sharkey2ac62992017-04-24 11:18:03 -06001541 @RequiresPermission(android.Manifest.permission.ACCESS_NETWORK_STATE)
Chalard Jean42a9c292019-01-07 19:26:34 +09001542 @Nullable
1543 public NetworkCapabilities getNetworkCapabilities(@Nullable Network network) {
Robert Greenwalt2bebbfb2014-03-26 16:47:06 -07001544 try {
Roshan Piusc97d8062020-12-17 14:53:09 -08001545 return mService.getNetworkCapabilities(
1546 network, mContext.getOpPackageName(), getAttributionTag());
Robert Greenwalt9f0ee4f2010-09-14 09:18:02 -07001547 } catch (RemoteException e) {
Jeff Sharkeya6066fd2016-03-01 19:27:23 -07001548 throw e.rethrowFromSystemServer();
Robert Greenwalt9f0ee4f2010-09-14 09:18:02 -07001549 }
1550 }
1551
Robert Greenwalt26744a52013-02-15 10:56:35 -08001552 /**
Remi NGUYEN VAN69a57cb2019-03-20 14:22:49 +09001553 * Gets a URL that can be used for resolving whether a captive portal is present.
Udam Saini53b71ee2016-01-04 12:16:14 -08001554 * 1. This URL should respond with a 204 response to a GET request to indicate no captive
1555 * portal is present.
1556 * 2. This URL must be HTTP as redirect responses are used to find captive portal
1557 * sign-in pages. Captive portals cannot respond to HTTPS requests with redirects.
1558 *
Remi NGUYEN VAN69a57cb2019-03-20 14:22:49 +09001559 * The system network validation may be using different strategies to detect captive portals,
1560 * so this method does not necessarily return a URL used by the system. It only returns a URL
1561 * that may be relevant for other components trying to detect captive portals.
paulhuec0a9632019-08-12 16:25:11 +08001562 *
Udam Saini53b71ee2016-01-04 12:16:14 -08001563 * @hide
paulhuec0a9632019-08-12 16:25:11 +08001564 * @deprecated This API returns URL which is not guaranteed to be one of the URLs used by the
1565 * system.
Udam Saini53b71ee2016-01-04 12:16:14 -08001566 */
paulhuec0a9632019-08-12 16:25:11 +08001567 @Deprecated
Udam Saini53b71ee2016-01-04 12:16:14 -08001568 @SystemApi
paulhuec0a9632019-08-12 16:25:11 +08001569 @RequiresPermission(android.Manifest.permission.NETWORK_SETTINGS)
Udam Saini53b71ee2016-01-04 12:16:14 -08001570 public String getCaptivePortalServerUrl() {
1571 try {
1572 return mService.getCaptivePortalServerUrl();
1573 } catch (RemoteException e) {
Jeff Sharkeya6066fd2016-03-01 19:27:23 -07001574 throw e.rethrowFromSystemServer();
Udam Saini53b71ee2016-01-04 12:16:14 -08001575 }
1576 }
1577
1578 /**
The Android Open Source Project28527d22009-03-03 19:31:44 -08001579 * Tells the underlying networking system that the caller wants to
1580 * begin using the named feature. The interpretation of {@code feature}
1581 * is completely up to each networking implementation.
Lorenzo Colitti91de63b2015-10-15 16:29:00 +09001582 *
1583 * <p>This method requires the caller to hold either the
1584 * {@link android.Manifest.permission#CHANGE_NETWORK_STATE} permission
1585 * or the ability to modify system settings as determined by
1586 * {@link android.provider.Settings.System#canWrite}.</p>
1587 *
The Android Open Source Project28527d22009-03-03 19:31:44 -08001588 * @param networkType specifies which network the request pertains to
1589 * @param feature the name of the feature to be used
1590 * @return an integer value representing the outcome of the request.
1591 * The interpretation of this value is specific to each networking
1592 * implementation+feature combination, except that the value {@code -1}
1593 * always indicates failure.
Robert Greenwaltd0ebdbb2014-05-18 23:07:25 -07001594 *
Lorenzo Colittiefd396e2015-04-24 17:03:31 +09001595 * @deprecated Deprecated in favor of the cleaner
1596 * {@link #requestNetwork(NetworkRequest, NetworkCallback)} API.
Dianne Hackbornc16abda2015-07-31 10:35:34 -07001597 * In {@link VERSION_CODES#M}, and above, this method is unsupported and will
Lorenzo Colitti9b026fa2015-07-29 11:41:21 +09001598 * throw {@code UnsupportedOperationException} if called.
Lorenzo Colitti7cb18342016-12-09 18:39:30 +09001599 * @removed
The Android Open Source Project28527d22009-03-03 19:31:44 -08001600 */
Aurimas Liutikasa6bb0232016-05-24 15:22:55 -07001601 @Deprecated
The Android Open Source Project28527d22009-03-03 19:31:44 -08001602 public int startUsingNetworkFeature(int networkType, String feature) {
Lorenzo Colitti9b026fa2015-07-29 11:41:21 +09001603 checkLegacyRoutingApiAccess();
Robert Greenwaltb59a91b2014-05-15 18:07:26 -07001604 NetworkCapabilities netCap = networkCapabilitiesForFeature(networkType, feature);
1605 if (netCap == null) {
1606 Log.d(TAG, "Can't satisfy startUsingNetworkFeature for " + networkType + ", " +
1607 feature);
Chalard Jean1f42df12019-11-21 14:48:00 +09001608 return DEPRECATED_PHONE_CONSTANT_APN_REQUEST_FAILED;
Robert Greenwaltb59a91b2014-05-15 18:07:26 -07001609 }
1610
1611 NetworkRequest request = null;
1612 synchronized (sLegacyRequests) {
1613 LegacyRequest l = sLegacyRequests.get(netCap);
1614 if (l != null) {
1615 Log.d(TAG, "renewing startUsingNetworkFeature request " + l.networkRequest);
1616 renewRequestLocked(l);
1617 if (l.currentNetwork != null) {
Chalard Jean1f42df12019-11-21 14:48:00 +09001618 return DEPRECATED_PHONE_CONSTANT_APN_ALREADY_ACTIVE;
Robert Greenwaltb59a91b2014-05-15 18:07:26 -07001619 } else {
Chalard Jean1f42df12019-11-21 14:48:00 +09001620 return DEPRECATED_PHONE_CONSTANT_APN_REQUEST_STARTED;
Robert Greenwaltb59a91b2014-05-15 18:07:26 -07001621 }
1622 }
1623
1624 request = requestNetworkForFeatureLocked(netCap);
1625 }
1626 if (request != null) {
Robert Greenwaltca3c5cf2014-06-20 10:58:45 -07001627 Log.d(TAG, "starting startUsingNetworkFeature for request " + request);
Chalard Jean1f42df12019-11-21 14:48:00 +09001628 return DEPRECATED_PHONE_CONSTANT_APN_REQUEST_STARTED;
Robert Greenwaltb59a91b2014-05-15 18:07:26 -07001629 } else {
1630 Log.d(TAG, " request Failed");
Chalard Jean1f42df12019-11-21 14:48:00 +09001631 return DEPRECATED_PHONE_CONSTANT_APN_REQUEST_FAILED;
The Android Open Source Project28527d22009-03-03 19:31:44 -08001632 }
1633 }
1634
1635 /**
1636 * Tells the underlying networking system that the caller is finished
1637 * using the named feature. The interpretation of {@code feature}
1638 * is completely up to each networking implementation.
Lorenzo Colitti91de63b2015-10-15 16:29:00 +09001639 *
1640 * <p>This method requires the caller to hold either the
1641 * {@link android.Manifest.permission#CHANGE_NETWORK_STATE} permission
1642 * or the ability to modify system settings as determined by
1643 * {@link android.provider.Settings.System#canWrite}.</p>
1644 *
The Android Open Source Project28527d22009-03-03 19:31:44 -08001645 * @param networkType specifies which network the request pertains to
1646 * @param feature the name of the feature that is no longer needed
1647 * @return an integer value representing the outcome of the request.
1648 * The interpretation of this value is specific to each networking
1649 * implementation+feature combination, except that the value {@code -1}
1650 * always indicates failure.
Robert Greenwaltd0ebdbb2014-05-18 23:07:25 -07001651 *
Lorenzo Colittiad458ad2016-04-13 22:00:02 +09001652 * @deprecated Deprecated in favor of the cleaner
1653 * {@link #unregisterNetworkCallback(NetworkCallback)} API.
Dianne Hackbornc16abda2015-07-31 10:35:34 -07001654 * In {@link VERSION_CODES#M}, and above, this method is unsupported and will
Lorenzo Colitti9b026fa2015-07-29 11:41:21 +09001655 * throw {@code UnsupportedOperationException} if called.
Lorenzo Colitti7cb18342016-12-09 18:39:30 +09001656 * @removed
The Android Open Source Project28527d22009-03-03 19:31:44 -08001657 */
Aurimas Liutikasa6bb0232016-05-24 15:22:55 -07001658 @Deprecated
The Android Open Source Project28527d22009-03-03 19:31:44 -08001659 public int stopUsingNetworkFeature(int networkType, String feature) {
Lorenzo Colitti9b026fa2015-07-29 11:41:21 +09001660 checkLegacyRoutingApiAccess();
Robert Greenwaltb59a91b2014-05-15 18:07:26 -07001661 NetworkCapabilities netCap = networkCapabilitiesForFeature(networkType, feature);
1662 if (netCap == null) {
1663 Log.d(TAG, "Can't satisfy stopUsingNetworkFeature for " + networkType + ", " +
1664 feature);
The Android Open Source Project28527d22009-03-03 19:31:44 -08001665 return -1;
1666 }
Robert Greenwaltb59a91b2014-05-15 18:07:26 -07001667
Paul Jensen034dea32014-12-17 10:39:34 -05001668 if (removeRequestForFeature(netCap)) {
Robert Greenwaltb59a91b2014-05-15 18:07:26 -07001669 Log.d(TAG, "stopUsingNetworkFeature for " + networkType + ", " + feature);
Robert Greenwaltb59a91b2014-05-15 18:07:26 -07001670 }
1671 return 1;
1672 }
1673
Mathew Inwoodd078d3d2020-10-27 11:47:29 +00001674 @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.R, trackingBug = 170729553)
Robert Greenwaltb59a91b2014-05-15 18:07:26 -07001675 private NetworkCapabilities networkCapabilitiesForFeature(int networkType, String feature) {
1676 if (networkType == TYPE_MOBILE) {
Erik Kline50068e52017-01-26 18:08:28 +09001677 switch (feature) {
1678 case "enableCBS":
1679 return networkCapabilitiesForType(TYPE_MOBILE_CBS);
1680 case "enableDUN":
1681 case "enableDUNAlways":
1682 return networkCapabilitiesForType(TYPE_MOBILE_DUN);
1683 case "enableFOTA":
1684 return networkCapabilitiesForType(TYPE_MOBILE_FOTA);
1685 case "enableHIPRI":
1686 return networkCapabilitiesForType(TYPE_MOBILE_HIPRI);
1687 case "enableIMS":
1688 return networkCapabilitiesForType(TYPE_MOBILE_IMS);
1689 case "enableMMS":
1690 return networkCapabilitiesForType(TYPE_MOBILE_MMS);
1691 case "enableSUPL":
1692 return networkCapabilitiesForType(TYPE_MOBILE_SUPL);
1693 default:
1694 return null;
Robert Greenwaltb59a91b2014-05-15 18:07:26 -07001695 }
Erik Kline50068e52017-01-26 18:08:28 +09001696 } else if (networkType == TYPE_WIFI && "p2p".equals(feature)) {
1697 return networkCapabilitiesForType(TYPE_WIFI_P2P);
Robert Greenwaltb59a91b2014-05-15 18:07:26 -07001698 }
1699 return null;
1700 }
1701
Robert Greenwalt5a367872014-06-02 15:32:02 -07001702 private int legacyTypeForNetworkCapabilities(NetworkCapabilities netCap) {
Robert Greenwaltb59a91b2014-05-15 18:07:26 -07001703 if (netCap == null) return TYPE_NONE;
1704 if (netCap.hasCapability(NetworkCapabilities.NET_CAPABILITY_CBS)) {
1705 return TYPE_MOBILE_CBS;
1706 }
1707 if (netCap.hasCapability(NetworkCapabilities.NET_CAPABILITY_IMS)) {
1708 return TYPE_MOBILE_IMS;
1709 }
1710 if (netCap.hasCapability(NetworkCapabilities.NET_CAPABILITY_FOTA)) {
1711 return TYPE_MOBILE_FOTA;
1712 }
1713 if (netCap.hasCapability(NetworkCapabilities.NET_CAPABILITY_DUN)) {
1714 return TYPE_MOBILE_DUN;
1715 }
1716 if (netCap.hasCapability(NetworkCapabilities.NET_CAPABILITY_SUPL)) {
1717 return TYPE_MOBILE_SUPL;
1718 }
1719 if (netCap.hasCapability(NetworkCapabilities.NET_CAPABILITY_MMS)) {
1720 return TYPE_MOBILE_MMS;
1721 }
1722 if (netCap.hasCapability(NetworkCapabilities.NET_CAPABILITY_INTERNET)) {
1723 return TYPE_MOBILE_HIPRI;
1724 }
Robert Greenwalt5a367872014-06-02 15:32:02 -07001725 if (netCap.hasCapability(NetworkCapabilities.NET_CAPABILITY_WIFI_P2P)) {
1726 return TYPE_WIFI_P2P;
1727 }
Robert Greenwaltb59a91b2014-05-15 18:07:26 -07001728 return TYPE_NONE;
1729 }
1730
1731 private static class LegacyRequest {
1732 NetworkCapabilities networkCapabilities;
1733 NetworkRequest networkRequest;
1734 int expireSequenceNumber;
1735 Network currentNetwork;
1736 int delay = -1;
Paul Jensen034dea32014-12-17 10:39:34 -05001737
1738 private void clearDnsBinding() {
1739 if (currentNetwork != null) {
1740 currentNetwork = null;
1741 setProcessDefaultNetworkForHostResolution(null);
1742 }
1743 }
1744
Robert Greenwalt09e5cff2014-06-11 16:05:07 -07001745 NetworkCallback networkCallback = new NetworkCallback() {
Robert Greenwaltb59a91b2014-05-15 18:07:26 -07001746 @Override
Robert Greenwalt09e5cff2014-06-11 16:05:07 -07001747 public void onAvailable(Network network) {
Robert Greenwaltb59a91b2014-05-15 18:07:26 -07001748 currentNetwork = network;
1749 Log.d(TAG, "startUsingNetworkFeature got Network:" + network);
Paul Jensene98c6e02014-05-29 10:12:39 -04001750 setProcessDefaultNetworkForHostResolution(network);
Robert Greenwaltb59a91b2014-05-15 18:07:26 -07001751 }
1752 @Override
Robert Greenwalt09e5cff2014-06-11 16:05:07 -07001753 public void onLost(Network network) {
Paul Jensen034dea32014-12-17 10:39:34 -05001754 if (network.equals(currentNetwork)) clearDnsBinding();
Robert Greenwaltb59a91b2014-05-15 18:07:26 -07001755 Log.d(TAG, "startUsingNetworkFeature lost Network:" + network);
1756 }
1757 };
1758 }
1759
Mathew Inwoodd078d3d2020-10-27 11:47:29 +00001760 @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.R, trackingBug = 170729553)
Chalard Jean73d9db72018-06-04 16:52:49 +09001761 private static final HashMap<NetworkCapabilities, LegacyRequest> sLegacyRequests =
1762 new HashMap<>();
Robert Greenwaltb59a91b2014-05-15 18:07:26 -07001763
1764 private NetworkRequest findRequestForFeature(NetworkCapabilities netCap) {
1765 synchronized (sLegacyRequests) {
1766 LegacyRequest l = sLegacyRequests.get(netCap);
1767 if (l != null) return l.networkRequest;
1768 }
1769 return null;
1770 }
1771
1772 private void renewRequestLocked(LegacyRequest l) {
1773 l.expireSequenceNumber++;
1774 Log.d(TAG, "renewing request to seqNum " + l.expireSequenceNumber);
1775 sendExpireMsgForFeature(l.networkCapabilities, l.expireSequenceNumber, l.delay);
1776 }
1777
1778 private void expireRequest(NetworkCapabilities netCap, int sequenceNum) {
1779 int ourSeqNum = -1;
1780 synchronized (sLegacyRequests) {
1781 LegacyRequest l = sLegacyRequests.get(netCap);
1782 if (l == null) return;
1783 ourSeqNum = l.expireSequenceNumber;
Paul Jensen034dea32014-12-17 10:39:34 -05001784 if (l.expireSequenceNumber == sequenceNum) removeRequestForFeature(netCap);
Robert Greenwaltb59a91b2014-05-15 18:07:26 -07001785 }
1786 Log.d(TAG, "expireRequest with " + ourSeqNum + ", " + sequenceNum);
1787 }
1788
Mathew Inwoodd078d3d2020-10-27 11:47:29 +00001789 @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.R, trackingBug = 170729553)
Robert Greenwaltb59a91b2014-05-15 18:07:26 -07001790 private NetworkRequest requestNetworkForFeatureLocked(NetworkCapabilities netCap) {
1791 int delay = -1;
Robert Greenwalt5a367872014-06-02 15:32:02 -07001792 int type = legacyTypeForNetworkCapabilities(netCap);
Robert Greenwaltb59a91b2014-05-15 18:07:26 -07001793 try {
1794 delay = mService.getRestoreDefaultNetworkDelay(type);
Jeff Sharkeya6066fd2016-03-01 19:27:23 -07001795 } catch (RemoteException e) {
1796 throw e.rethrowFromSystemServer();
1797 }
Robert Greenwaltb59a91b2014-05-15 18:07:26 -07001798 LegacyRequest l = new LegacyRequest();
1799 l.networkCapabilities = netCap;
1800 l.delay = delay;
1801 l.expireSequenceNumber = 0;
Hugo Benichicd5a0e02017-02-02 17:02:36 +09001802 l.networkRequest = sendRequestForNetwork(
1803 netCap, l.networkCallback, 0, REQUEST, type, getDefaultHandler());
Robert Greenwaltb59a91b2014-05-15 18:07:26 -07001804 if (l.networkRequest == null) return null;
1805 sLegacyRequests.put(netCap, l);
1806 sendExpireMsgForFeature(netCap, l.expireSequenceNumber, delay);
1807 return l.networkRequest;
1808 }
1809
1810 private void sendExpireMsgForFeature(NetworkCapabilities netCap, int seqNum, int delay) {
1811 if (delay >= 0) {
1812 Log.d(TAG, "sending expire msg with seqNum " + seqNum + " and delay " + delay);
Hugo Benichicd5a0e02017-02-02 17:02:36 +09001813 CallbackHandler handler = getDefaultHandler();
Hugo Benichiec180d52017-02-03 14:18:44 +09001814 Message msg = handler.obtainMessage(EXPIRE_LEGACY_REQUEST, seqNum, 0, netCap);
1815 handler.sendMessageDelayed(msg, delay);
Robert Greenwaltb59a91b2014-05-15 18:07:26 -07001816 }
1817 }
1818
Mathew Inwoodd078d3d2020-10-27 11:47:29 +00001819 @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.R, trackingBug = 170729553)
Paul Jensen034dea32014-12-17 10:39:34 -05001820 private boolean removeRequestForFeature(NetworkCapabilities netCap) {
1821 final LegacyRequest l;
Robert Greenwaltb59a91b2014-05-15 18:07:26 -07001822 synchronized (sLegacyRequests) {
Paul Jensen034dea32014-12-17 10:39:34 -05001823 l = sLegacyRequests.remove(netCap);
Robert Greenwaltb59a91b2014-05-15 18:07:26 -07001824 }
Paul Jensen034dea32014-12-17 10:39:34 -05001825 if (l == null) return false;
1826 unregisterNetworkCallback(l.networkCallback);
1827 l.clearDnsBinding();
1828 return true;
The Android Open Source Project28527d22009-03-03 19:31:44 -08001829 }
1830
Erik Kline50068e52017-01-26 18:08:28 +09001831 private static final SparseIntArray sLegacyTypeToTransport = new SparseIntArray();
1832 static {
1833 sLegacyTypeToTransport.put(TYPE_MOBILE, NetworkCapabilities.TRANSPORT_CELLULAR);
1834 sLegacyTypeToTransport.put(TYPE_MOBILE_CBS, NetworkCapabilities.TRANSPORT_CELLULAR);
1835 sLegacyTypeToTransport.put(TYPE_MOBILE_DUN, NetworkCapabilities.TRANSPORT_CELLULAR);
1836 sLegacyTypeToTransport.put(TYPE_MOBILE_FOTA, NetworkCapabilities.TRANSPORT_CELLULAR);
1837 sLegacyTypeToTransport.put(TYPE_MOBILE_HIPRI, NetworkCapabilities.TRANSPORT_CELLULAR);
1838 sLegacyTypeToTransport.put(TYPE_MOBILE_IMS, NetworkCapabilities.TRANSPORT_CELLULAR);
1839 sLegacyTypeToTransport.put(TYPE_MOBILE_MMS, NetworkCapabilities.TRANSPORT_CELLULAR);
1840 sLegacyTypeToTransport.put(TYPE_MOBILE_SUPL, NetworkCapabilities.TRANSPORT_CELLULAR);
1841 sLegacyTypeToTransport.put(TYPE_WIFI, NetworkCapabilities.TRANSPORT_WIFI);
1842 sLegacyTypeToTransport.put(TYPE_WIFI_P2P, NetworkCapabilities.TRANSPORT_WIFI);
1843 sLegacyTypeToTransport.put(TYPE_BLUETOOTH, NetworkCapabilities.TRANSPORT_BLUETOOTH);
1844 sLegacyTypeToTransport.put(TYPE_ETHERNET, NetworkCapabilities.TRANSPORT_ETHERNET);
1845 }
1846
1847 private static final SparseIntArray sLegacyTypeToCapability = new SparseIntArray();
1848 static {
1849 sLegacyTypeToCapability.put(TYPE_MOBILE_CBS, NetworkCapabilities.NET_CAPABILITY_CBS);
1850 sLegacyTypeToCapability.put(TYPE_MOBILE_DUN, NetworkCapabilities.NET_CAPABILITY_DUN);
1851 sLegacyTypeToCapability.put(TYPE_MOBILE_FOTA, NetworkCapabilities.NET_CAPABILITY_FOTA);
1852 sLegacyTypeToCapability.put(TYPE_MOBILE_IMS, NetworkCapabilities.NET_CAPABILITY_IMS);
1853 sLegacyTypeToCapability.put(TYPE_MOBILE_MMS, NetworkCapabilities.NET_CAPABILITY_MMS);
1854 sLegacyTypeToCapability.put(TYPE_MOBILE_SUPL, NetworkCapabilities.NET_CAPABILITY_SUPL);
1855 sLegacyTypeToCapability.put(TYPE_WIFI_P2P, NetworkCapabilities.NET_CAPABILITY_WIFI_P2P);
1856 }
1857
1858 /**
1859 * Given a legacy type (TYPE_WIFI, ...) returns a NetworkCapabilities
1860 * instance suitable for registering a request or callback. Throws an
1861 * IllegalArgumentException if no mapping from the legacy type to
1862 * NetworkCapabilities is known.
1863 *
Chalard Jean5acb7b72018-03-08 13:54:53 +09001864 * @deprecated Types are deprecated. Use {@link NetworkCallback} or {@link NetworkRequest}
1865 * to find the network instead.
Erik Kline50068e52017-01-26 18:08:28 +09001866 * @hide
1867 */
1868 public static NetworkCapabilities networkCapabilitiesForType(int type) {
1869 final NetworkCapabilities nc = new NetworkCapabilities();
1870
1871 // Map from type to transports.
1872 final int NOT_FOUND = -1;
1873 final int transport = sLegacyTypeToTransport.get(type, NOT_FOUND);
Remi NGUYEN VANc4dd8c92021-03-15 07:31:54 +00001874 if (transport == NOT_FOUND) {
1875 throw new IllegalArgumentException("unknown legacy type: " + type);
1876 }
Erik Kline50068e52017-01-26 18:08:28 +09001877 nc.addTransportType(transport);
1878
1879 // Map from type to capabilities.
1880 nc.addCapability(sLegacyTypeToCapability.get(
1881 type, NetworkCapabilities.NET_CAPABILITY_INTERNET));
1882 nc.maybeMarkCapabilitiesRestricted();
1883 return nc;
1884 }
1885
Lorenzo Colittid1039d12015-06-15 14:29:22 +09001886 /** @hide */
1887 public static class PacketKeepaliveCallback {
Mathew Inwoodd078d3d2020-10-27 11:47:29 +00001888 @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.R, trackingBug = 170729553)
Artur Satayevfc03a992019-11-15 19:12:49 +00001889 public PacketKeepaliveCallback() {
1890 }
Lorenzo Colittid1039d12015-06-15 14:29:22 +09001891 /** The requested keepalive was successfully started. */
Mathew Inwoodd078d3d2020-10-27 11:47:29 +00001892 @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.R, trackingBug = 170729553)
Lorenzo Colittid1039d12015-06-15 14:29:22 +09001893 public void onStarted() {}
1894 /** The keepalive was successfully stopped. */
Mathew Inwoodd078d3d2020-10-27 11:47:29 +00001895 @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.R, trackingBug = 170729553)
Lorenzo Colittid1039d12015-06-15 14:29:22 +09001896 public void onStopped() {}
1897 /** An error occurred. */
Mathew Inwoodd078d3d2020-10-27 11:47:29 +00001898 @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.R, trackingBug = 170729553)
Lorenzo Colittid1039d12015-06-15 14:29:22 +09001899 public void onError(int error) {}
1900 }
1901
1902 /**
1903 * Allows applications to request that the system periodically send specific packets on their
1904 * behalf, using hardware offload to save battery power.
1905 *
1906 * To request that the system send keepalives, call one of the methods that return a
1907 * {@link ConnectivityManager.PacketKeepalive} object, such as {@link #startNattKeepalive},
1908 * passing in a non-null callback. If the callback is successfully started, the callback's
1909 * {@code onStarted} method will be called. If an error occurs, {@code onError} will be called,
1910 * specifying one of the {@code ERROR_*} constants in this class.
1911 *
Chalard Jean73d9db72018-06-04 16:52:49 +09001912 * To stop an existing keepalive, call {@link PacketKeepalive#stop}. The system will call
1913 * {@link PacketKeepaliveCallback#onStopped} if the operation was successful or
1914 * {@link PacketKeepaliveCallback#onError} if an error occurred.
Lorenzo Colittid1039d12015-06-15 14:29:22 +09001915 *
junyulaia86defc2018-12-27 17:25:29 +08001916 * @deprecated Use {@link SocketKeepalive} instead.
1917 *
Lorenzo Colittid1039d12015-06-15 14:29:22 +09001918 * @hide
1919 */
1920 public class PacketKeepalive {
1921
1922 private static final String TAG = "PacketKeepalive";
1923
1924 /** @hide */
1925 public static final int SUCCESS = 0;
1926
1927 /** @hide */
1928 public static final int NO_KEEPALIVE = -1;
1929
1930 /** @hide */
1931 public static final int BINDER_DIED = -10;
1932
1933 /** The specified {@code Network} is not connected. */
1934 public static final int ERROR_INVALID_NETWORK = -20;
1935 /** The specified IP addresses are invalid. For example, the specified source IP address is
1936 * not configured on the specified {@code Network}. */
1937 public static final int ERROR_INVALID_IP_ADDRESS = -21;
1938 /** The requested port is invalid. */
1939 public static final int ERROR_INVALID_PORT = -22;
1940 /** The packet length is invalid (e.g., too long). */
1941 public static final int ERROR_INVALID_LENGTH = -23;
1942 /** The packet transmission interval is invalid (e.g., too short). */
1943 public static final int ERROR_INVALID_INTERVAL = -24;
1944
1945 /** The hardware does not support this request. */
1946 public static final int ERROR_HARDWARE_UNSUPPORTED = -30;
Lorenzo Colitti2aab7182015-09-08 16:46:36 +09001947 /** The hardware returned an error. */
1948 public static final int ERROR_HARDWARE_ERROR = -31;
Lorenzo Colittid1039d12015-06-15 14:29:22 +09001949
Nathan Harolde8ae0eb2018-02-14 13:09:45 -08001950 /** The NAT-T destination port for IPsec */
Lorenzo Colittid1039d12015-06-15 14:29:22 +09001951 public static final int NATT_PORT = 4500;
1952
Nathan Harolde8ae0eb2018-02-14 13:09:45 -08001953 /** The minimum interval in seconds between keepalive packet transmissions */
1954 public static final int MIN_INTERVAL = 10;
1955
Lorenzo Colittid1039d12015-06-15 14:29:22 +09001956 private final Network mNetwork;
junyulai9f872232019-01-16 20:23:34 +08001957 private final ISocketKeepaliveCallback mCallback;
1958 private final ExecutorService mExecutor;
Lorenzo Colittid1039d12015-06-15 14:29:22 +09001959
1960 private volatile Integer mSlot;
1961
Mathew Inwoodd078d3d2020-10-27 11:47:29 +00001962 @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.R, trackingBug = 170729553)
Lorenzo Colittid1039d12015-06-15 14:29:22 +09001963 public void stop() {
1964 try {
junyulai9f872232019-01-16 20:23:34 +08001965 mExecutor.execute(() -> {
1966 try {
1967 if (mSlot != null) {
1968 mService.stopKeepalive(mNetwork, mSlot);
1969 }
1970 } catch (RemoteException e) {
1971 Log.e(TAG, "Error stopping packet keepalive: ", e);
1972 throw e.rethrowFromSystemServer();
1973 }
1974 });
1975 } catch (RejectedExecutionException e) {
1976 // The internal executor has already stopped due to previous event.
Lorenzo Colittid1039d12015-06-15 14:29:22 +09001977 }
1978 }
1979
1980 private PacketKeepalive(Network network, PacketKeepaliveCallback callback) {
Remi NGUYEN VANc4dd8c92021-03-15 07:31:54 +00001981 Objects.requireNonNull(network, "network cannot be null");
1982 Objects.requireNonNull(callback, "callback cannot be null");
Lorenzo Colittid1039d12015-06-15 14:29:22 +09001983 mNetwork = network;
junyulai9f872232019-01-16 20:23:34 +08001984 mExecutor = Executors.newSingleThreadExecutor();
1985 mCallback = new ISocketKeepaliveCallback.Stub() {
Lorenzo Colittid1039d12015-06-15 14:29:22 +09001986 @Override
junyulai9f872232019-01-16 20:23:34 +08001987 public void onStarted(int slot) {
lucaslinad369e32020-12-30 11:54:55 +08001988 final long token = Binder.clearCallingIdentity();
1989 try {
1990 mExecutor.execute(() -> {
1991 mSlot = slot;
1992 callback.onStarted();
1993 });
1994 } finally {
1995 Binder.restoreCallingIdentity(token);
1996 }
Lorenzo Colittid1039d12015-06-15 14:29:22 +09001997 }
junyulai9f872232019-01-16 20:23:34 +08001998
1999 @Override
2000 public void onStopped() {
lucaslinad369e32020-12-30 11:54:55 +08002001 final long token = Binder.clearCallingIdentity();
2002 try {
2003 mExecutor.execute(() -> {
2004 mSlot = null;
2005 callback.onStopped();
2006 });
2007 } finally {
2008 Binder.restoreCallingIdentity(token);
2009 }
junyulai9f872232019-01-16 20:23:34 +08002010 mExecutor.shutdown();
2011 }
2012
2013 @Override
2014 public void onError(int error) {
lucaslinad369e32020-12-30 11:54:55 +08002015 final long token = Binder.clearCallingIdentity();
2016 try {
2017 mExecutor.execute(() -> {
2018 mSlot = null;
2019 callback.onError(error);
2020 });
2021 } finally {
2022 Binder.restoreCallingIdentity(token);
2023 }
junyulai9f872232019-01-16 20:23:34 +08002024 mExecutor.shutdown();
2025 }
2026
2027 @Override
2028 public void onDataReceived() {
2029 // PacketKeepalive is only used for Nat-T keepalive and as such does not invoke
2030 // this callback when data is received.
2031 }
2032 };
Lorenzo Colittid1039d12015-06-15 14:29:22 +09002033 }
2034 }
2035
2036 /**
2037 * Starts an IPsec NAT-T keepalive packet with the specified parameters.
2038 *
junyulaia86defc2018-12-27 17:25:29 +08002039 * @deprecated Use {@link #createSocketKeepalive} instead.
2040 *
Lorenzo Colittid1039d12015-06-15 14:29:22 +09002041 * @hide
2042 */
Mathew Inwoodd078d3d2020-10-27 11:47:29 +00002043 @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.R, trackingBug = 170729553)
Lorenzo Colittid1039d12015-06-15 14:29:22 +09002044 public PacketKeepalive startNattKeepalive(
2045 Network network, int intervalSeconds, PacketKeepaliveCallback callback,
2046 InetAddress srcAddr, int srcPort, InetAddress dstAddr) {
2047 final PacketKeepalive k = new PacketKeepalive(network, callback);
2048 try {
junyulai9f872232019-01-16 20:23:34 +08002049 mService.startNattKeepalive(network, intervalSeconds, k.mCallback,
Lorenzo Colittid1039d12015-06-15 14:29:22 +09002050 srcAddr.getHostAddress(), srcPort, dstAddr.getHostAddress());
2051 } catch (RemoteException e) {
2052 Log.e(TAG, "Error starting packet keepalive: ", e);
junyulai9f872232019-01-16 20:23:34 +08002053 throw e.rethrowFromSystemServer();
Lorenzo Colittid1039d12015-06-15 14:29:22 +09002054 }
2055 return k;
2056 }
2057
Chiachang Wangc5f86f42021-01-15 11:06:21 +08002058 // Construct an invalid fd.
2059 private ParcelFileDescriptor createInvalidFd() {
2060 final int invalidFd = -1;
2061 return ParcelFileDescriptor.adoptFd(invalidFd);
2062 }
2063
The Android Open Source Project28527d22009-03-03 19:31:44 -08002064 /**
junyulaia86defc2018-12-27 17:25:29 +08002065 * Request that keepalives be started on a IPsec NAT-T socket.
2066 *
2067 * @param network The {@link Network} the socket is on.
2068 * @param socket The socket that needs to be kept alive.
2069 * @param source The source address of the {@link UdpEncapsulationSocket}.
2070 * @param destination The destination address of the {@link UdpEncapsulationSocket}.
2071 * @param executor The executor on which callback will be invoked. The provided {@link Executor}
2072 * must run callback sequentially, otherwise the order of callbacks cannot be
2073 * guaranteed.
2074 * @param callback A {@link SocketKeepalive.Callback}. Used for notifications about keepalive
2075 * changes. Must be extended by applications that use this API.
2076 *
junyulaic7ea1242019-01-08 20:04:33 +08002077 * @return A {@link SocketKeepalive} object that can be used to control the keepalive on the
2078 * given socket.
junyulaia86defc2018-12-27 17:25:29 +08002079 **/
junyulai61143782019-03-04 22:45:36 +08002080 public @NonNull SocketKeepalive createSocketKeepalive(@NonNull Network network,
junyulaia86defc2018-12-27 17:25:29 +08002081 @NonNull UdpEncapsulationSocket socket,
2082 @NonNull InetAddress source,
2083 @NonNull InetAddress destination,
2084 @NonNull @CallbackExecutor Executor executor,
2085 @NonNull Callback callback) {
junyulai61143782019-03-04 22:45:36 +08002086 ParcelFileDescriptor dup;
2087 try {
junyulaic4fb2482019-03-27 11:00:37 +08002088 // Dup is needed here as the pfd inside the socket is owned by the IpSecService,
2089 // which cannot be obtained by the app process.
junyulai61143782019-03-04 22:45:36 +08002090 dup = ParcelFileDescriptor.dup(socket.getFileDescriptor());
2091 } catch (IOException ignored) {
2092 // Construct an invalid fd, so that if the user later calls start(), it will fail with
2093 // ERROR_INVALID_SOCKET.
Chiachang Wangc5f86f42021-01-15 11:06:21 +08002094 dup = createInvalidFd();
junyulai61143782019-03-04 22:45:36 +08002095 }
2096 return new NattSocketKeepalive(mService, network, dup, socket.getResourceId(), source,
2097 destination, executor, callback);
junyulai48a59382019-01-15 11:32:44 +08002098 }
2099
2100 /**
2101 * Request that keepalives be started on a IPsec NAT-T socket file descriptor. Directly called
2102 * by system apps which don't use IpSecService to create {@link UdpEncapsulationSocket}.
2103 *
2104 * @param network The {@link Network} the socket is on.
junyulai61143782019-03-04 22:45:36 +08002105 * @param pfd The {@link ParcelFileDescriptor} that needs to be kept alive. The provided
2106 * {@link ParcelFileDescriptor} must be bound to a port and the keepalives will be sent
2107 * from that port.
junyulai48a59382019-01-15 11:32:44 +08002108 * @param source The source address of the {@link UdpEncapsulationSocket}.
2109 * @param destination The destination address of the {@link UdpEncapsulationSocket}. The
2110 * keepalive packets will always be sent to port 4500 of the given {@code destination}.
2111 * @param executor The executor on which callback will be invoked. The provided {@link Executor}
2112 * must run callback sequentially, otherwise the order of callbacks cannot be
2113 * guaranteed.
2114 * @param callback A {@link SocketKeepalive.Callback}. Used for notifications about keepalive
2115 * changes. Must be extended by applications that use this API.
2116 *
junyulaic7ea1242019-01-08 20:04:33 +08002117 * @return A {@link SocketKeepalive} object that can be used to control the keepalive on the
2118 * given socket.
junyulai48a59382019-01-15 11:32:44 +08002119 * @hide
2120 */
2121 @SystemApi
2122 @RequiresPermission(android.Manifest.permission.PACKET_KEEPALIVE_OFFLOAD)
junyulai61143782019-03-04 22:45:36 +08002123 public @NonNull SocketKeepalive createNattKeepalive(@NonNull Network network,
2124 @NonNull ParcelFileDescriptor pfd,
junyulai48a59382019-01-15 11:32:44 +08002125 @NonNull InetAddress source,
2126 @NonNull InetAddress destination,
2127 @NonNull @CallbackExecutor Executor executor,
2128 @NonNull Callback callback) {
junyulai61143782019-03-04 22:45:36 +08002129 ParcelFileDescriptor dup;
2130 try {
junyulaic4fb2482019-03-27 11:00:37 +08002131 // TODO: Consider remove unnecessary dup.
junyulai61143782019-03-04 22:45:36 +08002132 dup = pfd.dup();
2133 } catch (IOException ignored) {
2134 // Construct an invalid fd, so that if the user later calls start(), it will fail with
2135 // ERROR_INVALID_SOCKET.
Chiachang Wangc5f86f42021-01-15 11:06:21 +08002136 dup = createInvalidFd();
junyulai61143782019-03-04 22:45:36 +08002137 }
2138 return new NattSocketKeepalive(mService, network, dup,
Remi NGUYEN VAN642d6ab2021-03-11 10:56:49 +00002139 -1 /* Unused */, source, destination, executor, callback);
junyulaia86defc2018-12-27 17:25:29 +08002140 }
2141
2142 /**
junyulaic7ea1242019-01-08 20:04:33 +08002143 * Request that keepalives be started on a TCP socket.
2144 * The socket must be established.
2145 *
2146 * @param network The {@link Network} the socket is on.
2147 * @param socket The socket that needs to be kept alive.
2148 * @param executor The executor on which callback will be invoked. This implementation assumes
2149 * the provided {@link Executor} runs the callbacks in sequence with no
2150 * concurrency. Failing this, no guarantee of correctness can be made. It is
2151 * the responsibility of the caller to ensure the executor provides this
2152 * guarantee. A simple way of creating such an executor is with the standard
2153 * tool {@code Executors.newSingleThreadExecutor}.
2154 * @param callback A {@link SocketKeepalive.Callback}. Used for notifications about keepalive
2155 * changes. Must be extended by applications that use this API.
2156 *
2157 * @return A {@link SocketKeepalive} object that can be used to control the keepalive on the
2158 * given socket.
2159 * @hide
2160 */
2161 @SystemApi
2162 @RequiresPermission(android.Manifest.permission.PACKET_KEEPALIVE_OFFLOAD)
junyulai61143782019-03-04 22:45:36 +08002163 public @NonNull SocketKeepalive createSocketKeepalive(@NonNull Network network,
junyulaic7ea1242019-01-08 20:04:33 +08002164 @NonNull Socket socket,
2165 @NonNull Executor executor,
2166 @NonNull Callback callback) {
junyulai61143782019-03-04 22:45:36 +08002167 ParcelFileDescriptor dup;
2168 try {
2169 dup = ParcelFileDescriptor.fromSocket(socket);
2170 } catch (UncheckedIOException ignored) {
2171 // Construct an invalid fd, so that if the user later calls start(), it will fail with
2172 // ERROR_INVALID_SOCKET.
Chiachang Wangc5f86f42021-01-15 11:06:21 +08002173 dup = createInvalidFd();
junyulai61143782019-03-04 22:45:36 +08002174 }
2175 return new TcpSocketKeepalive(mService, network, dup, executor, callback);
junyulaic7ea1242019-01-08 20:04:33 +08002176 }
2177
2178 /**
The Android Open Source Project28527d22009-03-03 19:31:44 -08002179 * Ensure that a network route exists to deliver traffic to the specified
2180 * host via the specified network interface. An attempt to add a route that
2181 * already exists is ignored, but treated as successful.
Lorenzo Colitti91de63b2015-10-15 16:29:00 +09002182 *
2183 * <p>This method requires the caller to hold either the
2184 * {@link android.Manifest.permission#CHANGE_NETWORK_STATE} permission
2185 * or the ability to modify system settings as determined by
2186 * {@link android.provider.Settings.System#canWrite}.</p>
2187 *
The Android Open Source Project28527d22009-03-03 19:31:44 -08002188 * @param networkType the type of the network over which traffic to the specified
2189 * host is to be routed
2190 * @param hostAddress the IP address of the host to which the route is desired
2191 * @return {@code true} on success, {@code false} on failure
Robert Greenwaltd0ebdbb2014-05-18 23:07:25 -07002192 *
Lorenzo Colittiefd396e2015-04-24 17:03:31 +09002193 * @deprecated Deprecated in favor of the
2194 * {@link #requestNetwork(NetworkRequest, NetworkCallback)},
2195 * {@link #bindProcessToNetwork} and {@link Network#getSocketFactory} API.
Dianne Hackbornc16abda2015-07-31 10:35:34 -07002196 * In {@link VERSION_CODES#M}, and above, this method is unsupported and will
Lorenzo Colitti9b026fa2015-07-29 11:41:21 +09002197 * throw {@code UnsupportedOperationException} if called.
Lorenzo Colitti7cb18342016-12-09 18:39:30 +09002198 * @removed
The Android Open Source Project28527d22009-03-03 19:31:44 -08002199 */
Aurimas Liutikasa6bb0232016-05-24 15:22:55 -07002200 @Deprecated
The Android Open Source Project28527d22009-03-03 19:31:44 -08002201 public boolean requestRouteToHost(int networkType, int hostAddress) {
Sreeram Ramachandranc05e2a92014-07-19 23:21:46 -07002202 return requestRouteToHostAddress(networkType, NetworkUtils.intToInetAddress(hostAddress));
Robert Greenwalt7fe44cb2010-08-27 09:24:29 -07002203 }
2204
2205 /**
2206 * Ensure that a network route exists to deliver traffic to the specified
2207 * host via the specified network interface. An attempt to add a route that
2208 * already exists is ignored, but treated as successful.
Lorenzo Colitti91de63b2015-10-15 16:29:00 +09002209 *
2210 * <p>This method requires the caller to hold either the
2211 * {@link android.Manifest.permission#CHANGE_NETWORK_STATE} permission
2212 * or the ability to modify system settings as determined by
2213 * {@link android.provider.Settings.System#canWrite}.</p>
2214 *
Robert Greenwalt7fe44cb2010-08-27 09:24:29 -07002215 * @param networkType the type of the network over which traffic to the specified
2216 * host is to be routed
2217 * @param hostAddress the IP address of the host to which the route is desired
2218 * @return {@code true} on success, {@code false} on failure
2219 * @hide
Robert Greenwaltd0ebdbb2014-05-18 23:07:25 -07002220 * @deprecated Deprecated in favor of the {@link #requestNetwork} and
Lorenzo Colittiefd396e2015-04-24 17:03:31 +09002221 * {@link #bindProcessToNetwork} API.
Robert Greenwalt7fe44cb2010-08-27 09:24:29 -07002222 */
Aurimas Liutikasa6bb0232016-05-24 15:22:55 -07002223 @Deprecated
Mathew Inwood0b8f8612018-08-08 14:52:47 +01002224 @UnsupportedAppUsage
lucaslin5140e482021-03-22 11:51:27 +08002225 @SystemApi(client = MODULE_LIBRARIES)
Robert Greenwalt7fe44cb2010-08-27 09:24:29 -07002226 public boolean requestRouteToHostAddress(int networkType, InetAddress hostAddress) {
Lorenzo Colitti9b026fa2015-07-29 11:41:21 +09002227 checkLegacyRoutingApiAccess();
The Android Open Source Project28527d22009-03-03 19:31:44 -08002228 try {
Philip P. Moltmannddbcf5b2020-03-24 15:57:49 -07002229 return mService.requestRouteToHostAddress(networkType, hostAddress.getAddress(),
2230 mContext.getOpPackageName(), getAttributionTag());
The Android Open Source Project28527d22009-03-03 19:31:44 -08002231 } catch (RemoteException e) {
Jeff Sharkeya6066fd2016-03-01 19:27:23 -07002232 throw e.rethrowFromSystemServer();
The Android Open Source Project28527d22009-03-03 19:31:44 -08002233 }
2234 }
2235
2236 /**
Philip P. Moltmannddbcf5b2020-03-24 15:57:49 -07002237 * @return the context's attribution tag
2238 */
2239 // TODO: Remove method and replace with direct call once R code is pushed to AOSP
2240 private @Nullable String getAttributionTag() {
2241 return mContext.getAttributionTag();
2242 }
2243
2244 /**
The Android Open Source Project28527d22009-03-03 19:31:44 -08002245 * Returns the value of the setting for background data usage. If false,
2246 * applications should not use the network if the application is not in the
2247 * foreground. Developers should respect this setting, and check the value
2248 * of this before performing any background data operations.
2249 * <p>
2250 * All applications that have background services that use the network
2251 * should listen to {@link #ACTION_BACKGROUND_DATA_SETTING_CHANGED}.
Jeff Sharkey39c01eb2011-08-16 14:37:57 -07002252 * <p>
Scott Main50589142011-10-06 18:32:43 -07002253 * @deprecated As of {@link VERSION_CODES#ICE_CREAM_SANDWICH}, availability of
Jeff Sharkey39c01eb2011-08-16 14:37:57 -07002254 * background data depends on several combined factors, and this method will
2255 * always return {@code true}. Instead, when background data is unavailable,
2256 * {@link #getActiveNetworkInfo()} will now appear disconnected.
Danica Chang96567052010-08-11 14:54:43 -07002257 *
The Android Open Source Project28527d22009-03-03 19:31:44 -08002258 * @return Whether background data usage is allowed.
2259 */
Jeff Sharkey39c01eb2011-08-16 14:37:57 -07002260 @Deprecated
The Android Open Source Project28527d22009-03-03 19:31:44 -08002261 public boolean getBackgroundDataSetting() {
Jeff Sharkey39c01eb2011-08-16 14:37:57 -07002262 // assume that background data is allowed; final authority is
2263 // NetworkInfo which may be blocked.
2264 return true;
The Android Open Source Project28527d22009-03-03 19:31:44 -08002265 }
2266
2267 /**
2268 * Sets the value of the setting for background data usage.
Robert Greenwalt1b21f6c2010-02-23 18:58:05 -08002269 *
The Android Open Source Project28527d22009-03-03 19:31:44 -08002270 * @param allowBackgroundData Whether an application should use data while
2271 * it is in the background.
Robert Greenwalt1b21f6c2010-02-23 18:58:05 -08002272 *
The Android Open Source Project28527d22009-03-03 19:31:44 -08002273 * @attr ref android.Manifest.permission#CHANGE_BACKGROUND_DATA_SETTING
2274 * @see #getBackgroundDataSetting()
2275 * @hide
2276 */
Jeff Sharkey39c01eb2011-08-16 14:37:57 -07002277 @Deprecated
Mathew Inwood0b8f8612018-08-08 14:52:47 +01002278 @UnsupportedAppUsage
The Android Open Source Project28527d22009-03-03 19:31:44 -08002279 public void setBackgroundDataSetting(boolean allowBackgroundData) {
Jeff Sharkey39c01eb2011-08-16 14:37:57 -07002280 // ignored
The Android Open Source Project28527d22009-03-03 19:31:44 -08002281 }
Robert Greenwalt1b21f6c2010-02-23 18:58:05 -08002282
Jeff Sharkey66fa9682011-08-02 17:22:34 -07002283 /**
Robert Greenwalt1b21f6c2010-02-23 18:58:05 -08002284 * @hide
Robert Greenwalt3d6c9582014-05-21 20:04:36 -07002285 * @deprecated Talk to TelephonyManager directly
Robert Greenwalt1b21f6c2010-02-23 18:58:05 -08002286 */
Aurimas Liutikasa6bb0232016-05-24 15:22:55 -07002287 @Deprecated
Mathew Inwood0b8f8612018-08-08 14:52:47 +01002288 @UnsupportedAppUsage
Robert Greenwalt1b21f6c2010-02-23 18:58:05 -08002289 public boolean getMobileDataEnabled() {
Meng Wang91311c02019-11-18 17:10:00 -08002290 TelephonyManager tm = mContext.getSystemService(TelephonyManager.class);
2291 if (tm != null) {
2292 int subId = SubscriptionManager.getDefaultDataSubscriptionId();
2293 Log.d("ConnectivityManager", "getMobileDataEnabled()+ subId=" + subId);
2294 boolean retVal = tm.createForSubscriptionId(subId).isDataEnabled();
2295 Log.d("ConnectivityManager", "getMobileDataEnabled()- subId=" + subId
2296 + " retVal=" + retVal);
2297 return retVal;
Robert Greenwalt1b21f6c2010-02-23 18:58:05 -08002298 }
Wink Saville02eb35c2014-12-05 11:10:30 -08002299 Log.d("ConnectivityManager", "getMobileDataEnabled()- remote exception retVal=false");
Robert Greenwalt3d6c9582014-05-21 20:04:36 -07002300 return false;
Robert Greenwalt1b21f6c2010-02-23 18:58:05 -08002301 }
2302
The Android Open Source Project28527d22009-03-03 19:31:44 -08002303 /**
Robert Greenwalt2e4731f2014-09-04 16:44:35 -07002304 * Callback for use with {@link ConnectivityManager#addDefaultNetworkActiveListener}
Robert Greenwalt09e5cff2014-06-11 16:05:07 -07002305 * to find out when the system default network has gone in to a high power state.
Dianne Hackbornb4b09e82014-02-26 16:20:52 -08002306 */
2307 public interface OnNetworkActiveListener {
2308 /**
2309 * Called on the main thread of the process to report that the current data network
2310 * has become active, and it is now a good time to perform any pending network
2311 * operations. Note that this listener only tells you when the network becomes
2312 * active; if at any other time you want to know whether it is active (and thus okay
2313 * to initiate network traffic), you can retrieve its instantaneous state with
Robert Greenwalt09e5cff2014-06-11 16:05:07 -07002314 * {@link ConnectivityManager#isDefaultNetworkActive}.
Dianne Hackbornb4b09e82014-02-26 16:20:52 -08002315 */
Chalard Jean73d9db72018-06-04 16:52:49 +09002316 void onNetworkActive();
Dianne Hackbornb4b09e82014-02-26 16:20:52 -08002317 }
2318
Dianne Hackbornb4b09e82014-02-26 16:20:52 -08002319 private final ArrayMap<OnNetworkActiveListener, INetworkActivityListener>
Chalard Jean73d9db72018-06-04 16:52:49 +09002320 mNetworkActivityListeners = new ArrayMap<>();
Dianne Hackbornb4b09e82014-02-26 16:20:52 -08002321
2322 /**
Robert Greenwalt09e5cff2014-06-11 16:05:07 -07002323 * Start listening to reports when the system's default data network is active, meaning it is
2324 * a good time to perform network traffic. Use {@link #isDefaultNetworkActive()}
2325 * to determine the current state of the system's default network after registering the
2326 * listener.
2327 * <p>
2328 * If the process default network has been set with
Paul Jensen895c3942015-03-10 10:54:12 -04002329 * {@link ConnectivityManager#bindProcessToNetwork} this function will not
Robert Greenwalt09e5cff2014-06-11 16:05:07 -07002330 * reflect the process's default, but the system default.
Dianne Hackbornb4b09e82014-02-26 16:20:52 -08002331 *
2332 * @param l The listener to be told when the network is active.
2333 */
Robert Greenwalt2e4731f2014-09-04 16:44:35 -07002334 public void addDefaultNetworkActiveListener(final OnNetworkActiveListener l) {
Dianne Hackbornb4b09e82014-02-26 16:20:52 -08002335 INetworkActivityListener rl = new INetworkActivityListener.Stub() {
2336 @Override
2337 public void onNetworkActive() throws RemoteException {
2338 l.onNetworkActive();
2339 }
2340 };
2341
2342 try {
lucaslina5e9bdb2021-01-21 02:04:15 +08002343 mService.registerNetworkActivityListener(rl);
Dianne Hackbornb4b09e82014-02-26 16:20:52 -08002344 mNetworkActivityListeners.put(l, rl);
2345 } catch (RemoteException e) {
Jeff Sharkeya6066fd2016-03-01 19:27:23 -07002346 throw e.rethrowFromSystemServer();
Dianne Hackbornb4b09e82014-02-26 16:20:52 -08002347 }
2348 }
2349
2350 /**
2351 * Remove network active listener previously registered with
Robert Greenwalt2e4731f2014-09-04 16:44:35 -07002352 * {@link #addDefaultNetworkActiveListener}.
Dianne Hackbornb4b09e82014-02-26 16:20:52 -08002353 *
2354 * @param l Previously registered listener.
2355 */
Chalard Jean42a9c292019-01-07 19:26:34 +09002356 public void removeDefaultNetworkActiveListener(@NonNull OnNetworkActiveListener l) {
Dianne Hackbornb4b09e82014-02-26 16:20:52 -08002357 INetworkActivityListener rl = mNetworkActivityListeners.get(l);
Remi NGUYEN VANc4dd8c92021-03-15 07:31:54 +00002358 if (rl == null) {
2359 throw new IllegalArgumentException("Listener was not registered.");
2360 }
Dianne Hackbornb4b09e82014-02-26 16:20:52 -08002361 try {
lucaslina5e9bdb2021-01-21 02:04:15 +08002362 mService.registerNetworkActivityListener(rl);
Dianne Hackbornb4b09e82014-02-26 16:20:52 -08002363 } catch (RemoteException e) {
Jeff Sharkeya6066fd2016-03-01 19:27:23 -07002364 throw e.rethrowFromSystemServer();
Dianne Hackbornb4b09e82014-02-26 16:20:52 -08002365 }
2366 }
2367
2368 /**
2369 * Return whether the data network is currently active. An active network means that
2370 * it is currently in a high power state for performing data transmission. On some
2371 * types of networks, it may be expensive to move and stay in such a state, so it is
2372 * more power efficient to batch network traffic together when the radio is already in
2373 * this state. This method tells you whether right now is currently a good time to
2374 * initiate network traffic, as the network is already active.
2375 */
Robert Greenwalt09e5cff2014-06-11 16:05:07 -07002376 public boolean isDefaultNetworkActive() {
Dianne Hackbornb4b09e82014-02-26 16:20:52 -08002377 try {
lucaslina5e9bdb2021-01-21 02:04:15 +08002378 return mService.isDefaultNetworkActive();
Dianne Hackbornb4b09e82014-02-26 16:20:52 -08002379 } catch (RemoteException e) {
Jeff Sharkeya6066fd2016-03-01 19:27:23 -07002380 throw e.rethrowFromSystemServer();
Dianne Hackbornb4b09e82014-02-26 16:20:52 -08002381 }
Dianne Hackbornb4b09e82014-02-26 16:20:52 -08002382 }
2383
2384 /**
The Android Open Source Project28527d22009-03-03 19:31:44 -08002385 * {@hide}
2386 */
Lorenzo Colitti9b026fa2015-07-29 11:41:21 +09002387 public ConnectivityManager(Context context, IConnectivityManager service) {
Remi NGUYEN VANc4dd8c92021-03-15 07:31:54 +00002388 mContext = Objects.requireNonNull(context, "missing context");
2389 mService = Objects.requireNonNull(service, "missing IConnectivityManager");
Automerger Merge Worker9b0c0c62020-03-06 00:38:43 +00002390 mTetheringManager = (TetheringManager) mContext.getSystemService(Context.TETHERING_SERVICE);
Paul Jensen12131352014-12-10 15:12:18 -05002391 sInstance = this;
The Android Open Source Project28527d22009-03-03 19:31:44 -08002392 }
Robert Greenwalt0c4828c2010-01-26 11:40:34 -08002393
Jeff Sharkey2d9e5a52012-04-04 20:40:58 -07002394 /** {@hide} */
Mathew Inwood0b8f8612018-08-08 14:52:47 +01002395 @UnsupportedAppUsage
Jeff Sharkey2d9e5a52012-04-04 20:40:58 -07002396 public static ConnectivityManager from(Context context) {
2397 return (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
2398 }
2399
Remi NGUYEN VAN1becbe72018-12-27 16:43:56 +09002400 /** @hide */
2401 public NetworkRequest getDefaultRequest() {
2402 try {
2403 // This is not racy as the default request is final in ConnectivityService.
2404 return mService.getDefaultRequest();
2405 } catch (RemoteException e) {
2406 throw e.rethrowFromSystemServer();
2407 }
2408 }
2409
Robert Greenwalt0c4828c2010-01-26 11:40:34 -08002410 /**
Philip P. Moltmannddbcf5b2020-03-24 15:57:49 -07002411 * Check if the package is a allowed to write settings. This also accounts that such an access
2412 * happened.
2413 *
2414 * @return {@code true} iff the package is allowed to write settings.
2415 */
2416 // TODO: Remove method and replace with direct call once R code is pushed to AOSP
2417 private static boolean checkAndNoteWriteSettingsOperation(@NonNull Context context, int uid,
2418 @NonNull String callingPackage, @Nullable String callingAttributionTag,
2419 boolean throwException) {
2420 return Settings.checkAndNoteWriteSettingsOperation(context, uid, callingPackage,
2421 callingAttributionTag, throwException);
2422 }
2423
2424 /**
Paul Jensen12131352014-12-10 15:12:18 -05002425 * @deprecated - use getSystemService. This is a kludge to support static access in certain
2426 * situations where a Context pointer is unavailable.
2427 * @hide
2428 */
Aurimas Liutikasa6bb0232016-05-24 15:22:55 -07002429 @Deprecated
Paul Jensen895c3942015-03-10 10:54:12 -04002430 static ConnectivityManager getInstanceOrNull() {
2431 return sInstance;
2432 }
2433
2434 /**
2435 * @deprecated - use getSystemService. This is a kludge to support static access in certain
2436 * situations where a Context pointer is unavailable.
2437 * @hide
2438 */
Aurimas Liutikasa6bb0232016-05-24 15:22:55 -07002439 @Deprecated
Mathew Inwood0b8f8612018-08-08 14:52:47 +01002440 @UnsupportedAppUsage
Paul Jensen895c3942015-03-10 10:54:12 -04002441 private static ConnectivityManager getInstance() {
2442 if (getInstanceOrNull() == null) {
Paul Jensen12131352014-12-10 15:12:18 -05002443 throw new IllegalStateException("No ConnectivityManager yet constructed");
2444 }
Paul Jensen895c3942015-03-10 10:54:12 -04002445 return getInstanceOrNull();
Paul Jensen12131352014-12-10 15:12:18 -05002446 }
2447
2448 /**
Robert Greenwalt26744a52013-02-15 10:56:35 -08002449 * Get the set of tetherable, available interfaces. This list is limited by
2450 * device configuration and current interface existence.
2451 *
2452 * @return an array of 0 or more Strings of tetherable interface names.
2453 *
markchien75721e42020-01-21 13:11:06 +08002454 * @deprecated Use {@link TetheringEventCallback#onTetherableInterfacesChanged(List)} instead.
Robert Greenwalt0c4828c2010-01-26 11:40:34 -08002455 * {@hide}
2456 */
Jeff Sharkey2ac62992017-04-24 11:18:03 -06002457 @RequiresPermission(android.Manifest.permission.ACCESS_NETWORK_STATE)
Mathew Inwood0b8f8612018-08-08 14:52:47 +01002458 @UnsupportedAppUsage
markchien75721e42020-01-21 13:11:06 +08002459 @Deprecated
Robert Greenwalt0c4828c2010-01-26 11:40:34 -08002460 public String[] getTetherableIfaces() {
Automerger Merge Worker9b0c0c62020-03-06 00:38:43 +00002461 return mTetheringManager.getTetherableIfaces();
Robert Greenwalt0c4828c2010-01-26 11:40:34 -08002462 }
2463
2464 /**
Robert Greenwalt26744a52013-02-15 10:56:35 -08002465 * Get the set of tethered interfaces.
2466 *
2467 * @return an array of 0 or more String of currently tethered interface names.
2468 *
markchien75721e42020-01-21 13:11:06 +08002469 * @deprecated Use {@link TetheringEventCallback#onTetherableInterfacesChanged(List)} instead.
Robert Greenwalt0c4828c2010-01-26 11:40:34 -08002470 * {@hide}
2471 */
Jeff Sharkey2ac62992017-04-24 11:18:03 -06002472 @RequiresPermission(android.Manifest.permission.ACCESS_NETWORK_STATE)
Mathew Inwood0b8f8612018-08-08 14:52:47 +01002473 @UnsupportedAppUsage
markchien75721e42020-01-21 13:11:06 +08002474 @Deprecated
Robert Greenwalt0c4828c2010-01-26 11:40:34 -08002475 public String[] getTetheredIfaces() {
Automerger Merge Worker9b0c0c62020-03-06 00:38:43 +00002476 return mTetheringManager.getTetheredIfaces();
Robert Greenwalt0c4828c2010-01-26 11:40:34 -08002477 }
2478
2479 /**
Robert Greenwalt26744a52013-02-15 10:56:35 -08002480 * Get the set of interface names which attempted to tether but
2481 * failed. Re-attempting to tether may cause them to reset to the Tethered
2482 * state. Alternatively, causing the interface to be destroyed and recreated
2483 * may cause them to reset to the available state.
2484 * {@link ConnectivityManager#getLastTetherError} can be used to get more
2485 * information on the cause of the errors.
2486 *
2487 * @return an array of 0 or more String indicating the interface names
2488 * which failed to tether.
2489 *
markchien75721e42020-01-21 13:11:06 +08002490 * @deprecated Use {@link TetheringEventCallback#onError(String, int)} instead.
Robert Greenwalt0c4828c2010-01-26 11:40:34 -08002491 * {@hide}
2492 */
Jeff Sharkey2ac62992017-04-24 11:18:03 -06002493 @RequiresPermission(android.Manifest.permission.ACCESS_NETWORK_STATE)
Mathew Inwood0b8f8612018-08-08 14:52:47 +01002494 @UnsupportedAppUsage
markchien75721e42020-01-21 13:11:06 +08002495 @Deprecated
Robert Greenwalt4283ded2010-03-02 17:25:02 -08002496 public String[] getTetheringErroredIfaces() {
Automerger Merge Worker9b0c0c62020-03-06 00:38:43 +00002497 return mTetheringManager.getTetheringErroredIfaces();
Robert Greenwalt0c4828c2010-01-26 11:40:34 -08002498 }
2499
2500 /**
Robert Greenwalta75c4652014-06-23 14:53:42 -07002501 * Get the set of tethered dhcp ranges.
2502 *
markchien9e046242020-02-06 19:23:26 +08002503 * @deprecated This method is not supported.
2504 * TODO: remove this function when all of clients are removed.
Robert Greenwalta75c4652014-06-23 14:53:42 -07002505 * {@hide}
2506 */
paulhuec0a9632019-08-12 16:25:11 +08002507 @RequiresPermission(android.Manifest.permission.NETWORK_SETTINGS)
markchien75721e42020-01-21 13:11:06 +08002508 @Deprecated
Robert Greenwalta75c4652014-06-23 14:53:42 -07002509 public String[] getTetheredDhcpRanges() {
markchien9e046242020-02-06 19:23:26 +08002510 throw new UnsupportedOperationException("getTetheredDhcpRanges is not supported");
Robert Greenwalta75c4652014-06-23 14:53:42 -07002511 }
2512
2513 /**
Robert Greenwalt26744a52013-02-15 10:56:35 -08002514 * Attempt to tether the named interface. This will setup a dhcp server
2515 * on the interface, forward and NAT IP packets and forward DNS requests
2516 * to the best active upstream network interface. Note that if no upstream
2517 * IP network interface is available, dhcp will still run and traffic will be
2518 * allowed between the tethered devices and this device, though upstream net
2519 * access will of course fail until an upstream network interface becomes
2520 * active.
Lorenzo Colitti91de63b2015-10-15 16:29:00 +09002521 *
2522 * <p>This method requires the caller to hold either the
2523 * {@link android.Manifest.permission#CHANGE_NETWORK_STATE} permission
2524 * or the ability to modify system settings as determined by
2525 * {@link android.provider.Settings.System#canWrite}.</p>
Robert Greenwalt26744a52013-02-15 10:56:35 -08002526 *
Jeremy Klein9a36ec82016-01-22 14:11:45 -08002527 * <p>WARNING: New clients should not use this function. The only usages should be in PanService
2528 * and WifiStateMachine which need direct access. All other clients should use
2529 * {@link #startTethering} and {@link #stopTethering} which encapsulate proper provisioning
2530 * logic.</p>
2531 *
Robert Greenwalt26744a52013-02-15 10:56:35 -08002532 * @param iface the interface name to tether.
2533 * @return error a {@code TETHER_ERROR} value indicating success or failure type
markchien10ddd022020-01-20 19:31:56 +08002534 * @deprecated Use {@link TetheringManager#startTethering} instead
Robert Greenwalt26744a52013-02-15 10:56:35 -08002535 *
Robert Greenwalt0c4828c2010-01-26 11:40:34 -08002536 * {@hide}
2537 */
Mathew Inwoodd078d3d2020-10-27 11:47:29 +00002538 @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.R, trackingBug = 170729553)
markchien10ddd022020-01-20 19:31:56 +08002539 @Deprecated
Robert Greenwalt4283ded2010-03-02 17:25:02 -08002540 public int tether(String iface) {
Automerger Merge Worker9b0c0c62020-03-06 00:38:43 +00002541 return mTetheringManager.tether(iface);
Robert Greenwalt4283ded2010-03-02 17:25:02 -08002542 }
2543
2544 /**
Robert Greenwalt26744a52013-02-15 10:56:35 -08002545 * Stop tethering the named interface.
Lorenzo Colitti91de63b2015-10-15 16:29:00 +09002546 *
2547 * <p>This method requires the caller to hold either the
2548 * {@link android.Manifest.permission#CHANGE_NETWORK_STATE} permission
2549 * or the ability to modify system settings as determined by
2550 * {@link android.provider.Settings.System#canWrite}.</p>
Robert Greenwalt26744a52013-02-15 10:56:35 -08002551 *
Jeremy Klein9a36ec82016-01-22 14:11:45 -08002552 * <p>WARNING: New clients should not use this function. The only usages should be in PanService
2553 * and WifiStateMachine which need direct access. All other clients should use
2554 * {@link #startTethering} and {@link #stopTethering} which encapsulate proper provisioning
2555 * logic.</p>
2556 *
Robert Greenwalt26744a52013-02-15 10:56:35 -08002557 * @param iface the interface name to untether.
2558 * @return error a {@code TETHER_ERROR} value indicating success or failure type
2559 *
Robert Greenwalt4283ded2010-03-02 17:25:02 -08002560 * {@hide}
2561 */
Mathew Inwood0b8f8612018-08-08 14:52:47 +01002562 @UnsupportedAppUsage
markchien75721e42020-01-21 13:11:06 +08002563 @Deprecated
Robert Greenwalt4283ded2010-03-02 17:25:02 -08002564 public int untether(String iface) {
Automerger Merge Worker9b0c0c62020-03-06 00:38:43 +00002565 return mTetheringManager.untether(iface);
Robert Greenwalt0c4828c2010-01-26 11:40:34 -08002566 }
Robert Greenwalt8e87f122010-02-11 18:18:40 -08002567
2568 /**
Robert Greenwalt26744a52013-02-15 10:56:35 -08002569 * Check if the device allows for tethering. It may be disabled via
Dianne Hackbornb4b09e82014-02-26 16:20:52 -08002570 * {@code ro.tether.denied} system property, Settings.TETHER_SUPPORTED or
Robert Greenwalt26744a52013-02-15 10:56:35 -08002571 * due to device configuration.
2572 *
Chalard Jean4e1bb722017-09-26 15:45:18 +09002573 * <p>If this app does not have permission to use this API, it will always
2574 * return false rather than throw an exception.</p>
2575 *
2576 * <p>If the device has a hotspot provisioning app, the caller is required to hold the
2577 * {@link android.Manifest.permission.TETHER_PRIVILEGED} permission.</p>
2578 *
2579 * <p>Otherwise, this method requires the caller to hold the ability to modify system
2580 * settings as determined by {@link android.provider.Settings.System#canWrite}.</p>
2581 *
Robert Greenwalt26744a52013-02-15 10:56:35 -08002582 * @return a boolean - {@code true} indicating Tethering is supported.
2583 *
markchien75721e42020-01-21 13:11:06 +08002584 * @deprecated Use {@link TetheringEventCallback#onTetheringSupported(boolean)} instead.
Robert Greenwalt8e87f122010-02-11 18:18:40 -08002585 * {@hide}
2586 */
Jeremy Klein9a36ec82016-01-22 14:11:45 -08002587 @SystemApi
Chalard Jean4e1bb722017-09-26 15:45:18 +09002588 @RequiresPermission(anyOf = {android.Manifest.permission.TETHER_PRIVILEGED,
2589 android.Manifest.permission.WRITE_SETTINGS})
Robert Greenwalt8e87f122010-02-11 18:18:40 -08002590 public boolean isTetheringSupported() {
Automerger Merge Worker9b0c0c62020-03-06 00:38:43 +00002591 return mTetheringManager.isTetheringSupported();
Robert Greenwalt8e87f122010-02-11 18:18:40 -08002592 }
2593
2594 /**
Jeremy Klein9a36ec82016-01-22 14:11:45 -08002595 * Callback for use with {@link #startTethering} to find out whether tethering succeeded.
markchien10ddd022020-01-20 19:31:56 +08002596 *
2597 * @deprecated Use {@link TetheringManager.StartTetheringCallback} instead.
Jeremy Klein9a36ec82016-01-22 14:11:45 -08002598 * @hide
2599 */
2600 @SystemApi
markchien10ddd022020-01-20 19:31:56 +08002601 @Deprecated
Jeremy Klein9a36ec82016-01-22 14:11:45 -08002602 public static abstract class OnStartTetheringCallback {
2603 /**
2604 * Called when tethering has been successfully started.
2605 */
Chalard Jean73d9db72018-06-04 16:52:49 +09002606 public void onTetheringStarted() {}
Jeremy Klein9a36ec82016-01-22 14:11:45 -08002607
2608 /**
2609 * Called when starting tethering failed.
2610 */
Chalard Jean73d9db72018-06-04 16:52:49 +09002611 public void onTetheringFailed() {}
Jeremy Klein9a36ec82016-01-22 14:11:45 -08002612 }
2613
2614 /**
2615 * Convenient overload for
2616 * {@link #startTethering(int, boolean, OnStartTetheringCallback, Handler)} which passes a null
2617 * handler to run on the current thread's {@link Looper}.
markchien10ddd022020-01-20 19:31:56 +08002618 *
2619 * @deprecated Use {@link TetheringManager#startTethering} instead.
Jeremy Klein9a36ec82016-01-22 14:11:45 -08002620 * @hide
2621 */
2622 @SystemApi
markchien10ddd022020-01-20 19:31:56 +08002623 @Deprecated
Udam Saini15072332017-06-07 12:06:28 -07002624 @RequiresPermission(android.Manifest.permission.TETHER_PRIVILEGED)
Jeremy Klein9a36ec82016-01-22 14:11:45 -08002625 public void startTethering(int type, boolean showProvisioningUi,
2626 final OnStartTetheringCallback callback) {
2627 startTethering(type, showProvisioningUi, callback, null);
2628 }
2629
2630 /**
2631 * Runs tether provisioning for the given type if needed and then starts tethering if
2632 * the check succeeds. If no carrier provisioning is required for tethering, tethering is
2633 * enabled immediately. If provisioning fails, tethering will not be enabled. It also
2634 * schedules tether provisioning re-checks if appropriate.
2635 *
2636 * @param type The type of tethering to start. Must be one of
2637 * {@link ConnectivityManager.TETHERING_WIFI},
2638 * {@link ConnectivityManager.TETHERING_USB}, or
2639 * {@link ConnectivityManager.TETHERING_BLUETOOTH}.
2640 * @param showProvisioningUi a boolean indicating to show the provisioning app UI if there
2641 * is one. This should be true the first time this function is called and also any time
2642 * the user can see this UI. It gives users information from their carrier about the
2643 * check failing and how they can sign up for tethering if possible.
2644 * @param callback an {@link OnStartTetheringCallback} which will be called to notify the caller
2645 * of the result of trying to tether.
2646 * @param handler {@link Handler} to specify the thread upon which the callback will be invoked.
markchien10ddd022020-01-20 19:31:56 +08002647 *
2648 * @deprecated Use {@link TetheringManager#startTethering} instead.
Jeremy Klein9a36ec82016-01-22 14:11:45 -08002649 * @hide
2650 */
2651 @SystemApi
markchien10ddd022020-01-20 19:31:56 +08002652 @Deprecated
Jeff Sharkeyb8c73032017-06-02 17:36:26 -06002653 @RequiresPermission(android.Manifest.permission.TETHER_PRIVILEGED)
Jeremy Klein9a36ec82016-01-22 14:11:45 -08002654 public void startTethering(int type, boolean showProvisioningUi,
2655 final OnStartTetheringCallback callback, Handler handler) {
Remi NGUYEN VANc4dd8c92021-03-15 07:31:54 +00002656 Objects.requireNonNull(callback, "OnStartTetheringCallback cannot be null.");
Jeremy Klein49a57952016-03-12 16:29:54 -08002657
markchien10ddd022020-01-20 19:31:56 +08002658 final Executor executor = new Executor() {
Jeremy Klein9a36ec82016-01-22 14:11:45 -08002659 @Override
markchien10ddd022020-01-20 19:31:56 +08002660 public void execute(Runnable command) {
2661 if (handler == null) {
2662 command.run();
Jeremy Klein9a36ec82016-01-22 14:11:45 -08002663 } else {
markchien10ddd022020-01-20 19:31:56 +08002664 handler.post(command);
Jeremy Klein9a36ec82016-01-22 14:11:45 -08002665 }
2666 }
2667 };
Jeremy Klein49a57952016-03-12 16:29:54 -08002668
markchien10ddd022020-01-20 19:31:56 +08002669 final StartTetheringCallback tetheringCallback = new StartTetheringCallback() {
2670 @Override
2671 public void onTetheringStarted() {
2672 callback.onTetheringStarted();
2673 }
2674
2675 @Override
markchien68b67842020-03-19 13:37:43 +08002676 public void onTetheringFailed(final int error) {
markchien10ddd022020-01-20 19:31:56 +08002677 callback.onTetheringFailed();
2678 }
2679 };
2680
2681 final TetheringRequest request = new TetheringRequest.Builder(type)
markchien68b67842020-03-19 13:37:43 +08002682 .setShouldShowEntitlementUi(showProvisioningUi).build();
markchien10ddd022020-01-20 19:31:56 +08002683
Automerger Merge Worker9b0c0c62020-03-06 00:38:43 +00002684 mTetheringManager.startTethering(request, executor, tetheringCallback);
Jeremy Klein9a36ec82016-01-22 14:11:45 -08002685 }
2686
2687 /**
2688 * Stops tethering for the given type. Also cancels any provisioning rechecks for that type if
2689 * applicable.
2690 *
2691 * @param type The type of tethering to stop. Must be one of
2692 * {@link ConnectivityManager.TETHERING_WIFI},
2693 * {@link ConnectivityManager.TETHERING_USB}, or
2694 * {@link ConnectivityManager.TETHERING_BLUETOOTH}.
markchien75721e42020-01-21 13:11:06 +08002695 *
2696 * @deprecated Use {@link TetheringManager#stopTethering} instead.
Jeremy Klein9a36ec82016-01-22 14:11:45 -08002697 * @hide
2698 */
2699 @SystemApi
markchien75721e42020-01-21 13:11:06 +08002700 @Deprecated
Jeff Sharkeyb8c73032017-06-02 17:36:26 -06002701 @RequiresPermission(android.Manifest.permission.TETHER_PRIVILEGED)
Jeremy Klein9a36ec82016-01-22 14:11:45 -08002702 public void stopTethering(int type) {
Automerger Merge Worker9b0c0c62020-03-06 00:38:43 +00002703 mTetheringManager.stopTethering(type);
Jeremy Klein9a36ec82016-01-22 14:11:45 -08002704 }
2705
2706 /**
markchiena005c992019-02-27 14:56:11 +08002707 * Callback for use with {@link registerTetheringEventCallback} to find out tethering
2708 * upstream status.
2709 *
Nathan Haroldf6fc8ab2020-01-23 18:03:46 -08002710 * @deprecated Use {@link TetheringManager#OnTetheringEventCallback} instead.
markchien75721e42020-01-21 13:11:06 +08002711 * @hide
markchiena005c992019-02-27 14:56:11 +08002712 */
2713 @SystemApi
markchien75721e42020-01-21 13:11:06 +08002714 @Deprecated
markchiena005c992019-02-27 14:56:11 +08002715 public abstract static class OnTetheringEventCallback {
2716
2717 /**
2718 * Called when tethering upstream changed. This can be called multiple times and can be
2719 * called any time.
2720 *
2721 * @param network the {@link Network} of tethering upstream. Null means tethering doesn't
2722 * have any upstream.
2723 */
2724 public void onUpstreamChanged(@Nullable Network network) {}
2725 }
2726
markchien75721e42020-01-21 13:11:06 +08002727 @GuardedBy("mTetheringEventCallbacks")
2728 private final ArrayMap<OnTetheringEventCallback, TetheringEventCallback>
2729 mTetheringEventCallbacks = new ArrayMap<>();
2730
markchiena005c992019-02-27 14:56:11 +08002731 /**
2732 * Start listening to tethering change events. Any new added callback will receive the last
markchiend378cd32019-04-03 10:43:09 +08002733 * tethering status right away. If callback is registered when tethering has no upstream or
markchiena005c992019-02-27 14:56:11 +08002734 * disabled, {@link OnTetheringEventCallback#onUpstreamChanged} will immediately be called
2735 * with a null argument. The same callback object cannot be registered twice.
2736 *
2737 * @param executor the executor on which callback will be invoked.
2738 * @param callback the callback to be called when tethering has change events.
markchien75721e42020-01-21 13:11:06 +08002739 *
Nathan Haroldf6fc8ab2020-01-23 18:03:46 -08002740 * @deprecated Use {@link TetheringManager#registerTetheringEventCallback} instead.
markchiena005c992019-02-27 14:56:11 +08002741 * @hide
2742 */
2743 @SystemApi
markchien75721e42020-01-21 13:11:06 +08002744 @Deprecated
markchiena005c992019-02-27 14:56:11 +08002745 @RequiresPermission(android.Manifest.permission.TETHER_PRIVILEGED)
2746 public void registerTetheringEventCallback(
2747 @NonNull @CallbackExecutor Executor executor,
2748 @NonNull final OnTetheringEventCallback callback) {
Remi NGUYEN VANc4dd8c92021-03-15 07:31:54 +00002749 Objects.requireNonNull(callback, "OnTetheringEventCallback cannot be null.");
markchiena005c992019-02-27 14:56:11 +08002750
markchien75721e42020-01-21 13:11:06 +08002751 final TetheringEventCallback tetherCallback =
2752 new TetheringEventCallback() {
2753 @Override
2754 public void onUpstreamChanged(@Nullable Network network) {
2755 callback.onUpstreamChanged(network);
2756 }
2757 };
2758
2759 synchronized (mTetheringEventCallbacks) {
2760 mTetheringEventCallbacks.put(callback, tetherCallback);
Automerger Merge Worker9b0c0c62020-03-06 00:38:43 +00002761 mTetheringManager.registerTetheringEventCallback(executor, tetherCallback);
markchien75721e42020-01-21 13:11:06 +08002762 }
markchiena005c992019-02-27 14:56:11 +08002763 }
2764
2765 /**
2766 * Remove tethering event callback previously registered with
2767 * {@link #registerTetheringEventCallback}.
2768 *
2769 * @param callback previously registered callback.
markchien75721e42020-01-21 13:11:06 +08002770 *
2771 * @deprecated Use {@link TetheringManager#unregisterTetheringEventCallback} instead.
markchiena005c992019-02-27 14:56:11 +08002772 * @hide
2773 */
2774 @SystemApi
markchien75721e42020-01-21 13:11:06 +08002775 @Deprecated
markchiena005c992019-02-27 14:56:11 +08002776 @RequiresPermission(android.Manifest.permission.TETHER_PRIVILEGED)
2777 public void unregisterTetheringEventCallback(
2778 @NonNull final OnTetheringEventCallback callback) {
markchien75721e42020-01-21 13:11:06 +08002779 Objects.requireNonNull(callback, "The callback must be non-null");
2780 synchronized (mTetheringEventCallbacks) {
2781 final TetheringEventCallback tetherCallback =
2782 mTetheringEventCallbacks.remove(callback);
Automerger Merge Worker9b0c0c62020-03-06 00:38:43 +00002783 mTetheringManager.unregisterTetheringEventCallback(tetherCallback);
markchien75721e42020-01-21 13:11:06 +08002784 }
markchiena005c992019-02-27 14:56:11 +08002785 }
2786
2787
2788 /**
Robert Greenwalt26744a52013-02-15 10:56:35 -08002789 * Get the list of regular expressions that define any tetherable
2790 * USB network interfaces. If USB tethering is not supported by the
2791 * device, this list should be empty.
2792 *
2793 * @return an array of 0 or more regular expression Strings defining
2794 * what interfaces are considered tetherable usb interfaces.
2795 *
markchien75721e42020-01-21 13:11:06 +08002796 * @deprecated Use {@link TetheringEventCallback#onTetherableInterfaceRegexpsChanged} instead.
Robert Greenwalt8e87f122010-02-11 18:18:40 -08002797 * {@hide}
2798 */
Jeff Sharkey2ac62992017-04-24 11:18:03 -06002799 @RequiresPermission(android.Manifest.permission.ACCESS_NETWORK_STATE)
Mathew Inwood0b8f8612018-08-08 14:52:47 +01002800 @UnsupportedAppUsage
markchien75721e42020-01-21 13:11:06 +08002801 @Deprecated
Robert Greenwalt8e87f122010-02-11 18:18:40 -08002802 public String[] getTetherableUsbRegexs() {
Automerger Merge Worker9b0c0c62020-03-06 00:38:43 +00002803 return mTetheringManager.getTetherableUsbRegexs();
Robert Greenwalt8e87f122010-02-11 18:18:40 -08002804 }
2805
2806 /**
Robert Greenwalt26744a52013-02-15 10:56:35 -08002807 * Get the list of regular expressions that define any tetherable
2808 * Wifi network interfaces. If Wifi tethering is not supported by the
2809 * device, this list should be empty.
2810 *
2811 * @return an array of 0 or more regular expression Strings defining
2812 * what interfaces are considered tetherable wifi interfaces.
2813 *
markchien75721e42020-01-21 13:11:06 +08002814 * @deprecated Use {@link TetheringEventCallback#onTetherableInterfaceRegexpsChanged} instead.
Robert Greenwalt8e87f122010-02-11 18:18:40 -08002815 * {@hide}
2816 */
Jeff Sharkey2ac62992017-04-24 11:18:03 -06002817 @RequiresPermission(android.Manifest.permission.ACCESS_NETWORK_STATE)
Mathew Inwood0b8f8612018-08-08 14:52:47 +01002818 @UnsupportedAppUsage
markchien75721e42020-01-21 13:11:06 +08002819 @Deprecated
Robert Greenwalt8e87f122010-02-11 18:18:40 -08002820 public String[] getTetherableWifiRegexs() {
Automerger Merge Worker9b0c0c62020-03-06 00:38:43 +00002821 return mTetheringManager.getTetherableWifiRegexs();
Robert Greenwalt8e87f122010-02-11 18:18:40 -08002822 }
Robert Greenwalt4283ded2010-03-02 17:25:02 -08002823
Danica Chang96567052010-08-11 14:54:43 -07002824 /**
Robert Greenwalt26744a52013-02-15 10:56:35 -08002825 * Get the list of regular expressions that define any tetherable
2826 * Bluetooth network interfaces. If Bluetooth tethering is not supported by the
2827 * device, this list should be empty.
2828 *
2829 * @return an array of 0 or more regular expression Strings defining
2830 * what interfaces are considered tetherable bluetooth interfaces.
2831 *
markchien75721e42020-01-21 13:11:06 +08002832 * @deprecated Use {@link TetheringEventCallback#onTetherableInterfaceRegexpsChanged(
2833 *TetheringManager.TetheringInterfaceRegexps)} instead.
Danica Chang96567052010-08-11 14:54:43 -07002834 * {@hide}
2835 */
Jeff Sharkey2ac62992017-04-24 11:18:03 -06002836 @RequiresPermission(android.Manifest.permission.ACCESS_NETWORK_STATE)
Mathew Inwood0b8f8612018-08-08 14:52:47 +01002837 @UnsupportedAppUsage
markchien75721e42020-01-21 13:11:06 +08002838 @Deprecated
Danica Chang96567052010-08-11 14:54:43 -07002839 public String[] getTetherableBluetoothRegexs() {
Automerger Merge Worker9b0c0c62020-03-06 00:38:43 +00002840 return mTetheringManager.getTetherableBluetoothRegexs();
Danica Chang96567052010-08-11 14:54:43 -07002841 }
2842
Mike Lockwooded4a1742011-07-19 13:04:47 -07002843 /**
Robert Greenwalt26744a52013-02-15 10:56:35 -08002844 * Attempt to both alter the mode of USB and Tethering of USB. A
2845 * utility method to deal with some of the complexity of USB - will
2846 * attempt to switch to Rndis and subsequently tether the resulting
2847 * interface on {@code true} or turn off tethering and switch off
2848 * Rndis on {@code false}.
Lorenzo Colitti91de63b2015-10-15 16:29:00 +09002849 *
2850 * <p>This method requires the caller to hold either the
2851 * {@link android.Manifest.permission#CHANGE_NETWORK_STATE} permission
2852 * or the ability to modify system settings as determined by
2853 * {@link android.provider.Settings.System#canWrite}.</p>
Robert Greenwalt26744a52013-02-15 10:56:35 -08002854 *
2855 * @param enable a boolean - {@code true} to enable tethering
2856 * @return error a {@code TETHER_ERROR} value indicating success or failure type
markchien10ddd022020-01-20 19:31:56 +08002857 * @deprecated Use {@link TetheringManager#startTethering} instead
Robert Greenwalt26744a52013-02-15 10:56:35 -08002858 *
Mike Lockwooded4a1742011-07-19 13:04:47 -07002859 * {@hide}
2860 */
Mathew Inwood0b8f8612018-08-08 14:52:47 +01002861 @UnsupportedAppUsage
markchien10ddd022020-01-20 19:31:56 +08002862 @Deprecated
Mike Lockwooded4a1742011-07-19 13:04:47 -07002863 public int setUsbTethering(boolean enable) {
Automerger Merge Worker9b0c0c62020-03-06 00:38:43 +00002864 return mTetheringManager.setUsbTethering(enable);
Mike Lockwooded4a1742011-07-19 13:04:47 -07002865 }
2866
markchien75721e42020-01-21 13:11:06 +08002867 /**
2868 * @deprecated Use {@link TetheringManager#TETHER_ERROR_NO_ERROR}.
2869 * {@hide}
2870 */
markchien5bd499c2019-01-16 17:44:13 +08002871 @SystemApi
markchien75721e42020-01-21 13:11:06 +08002872 @Deprecated
Remi NGUYEN VAN23b853b2021-02-15 18:52:06 +09002873 public static final int TETHER_ERROR_NO_ERROR = 0;
markchien75721e42020-01-21 13:11:06 +08002874 /**
2875 * @deprecated Use {@link TetheringManager#TETHER_ERROR_UNKNOWN_IFACE}.
2876 * {@hide}
2877 */
2878 @Deprecated
2879 public static final int TETHER_ERROR_UNKNOWN_IFACE =
2880 TetheringManager.TETHER_ERROR_UNKNOWN_IFACE;
2881 /**
2882 * @deprecated Use {@link TetheringManager#TETHER_ERROR_SERVICE_UNAVAIL}.
2883 * {@hide}
2884 */
2885 @Deprecated
2886 public static final int TETHER_ERROR_SERVICE_UNAVAIL =
2887 TetheringManager.TETHER_ERROR_SERVICE_UNAVAIL;
2888 /**
2889 * @deprecated Use {@link TetheringManager#TETHER_ERROR_UNSUPPORTED}.
2890 * {@hide}
2891 */
2892 @Deprecated
2893 public static final int TETHER_ERROR_UNSUPPORTED = TetheringManager.TETHER_ERROR_UNSUPPORTED;
2894 /**
2895 * @deprecated Use {@link TetheringManager#TETHER_ERROR_UNAVAIL_IFACE}.
2896 * {@hide}
2897 */
2898 @Deprecated
2899 public static final int TETHER_ERROR_UNAVAIL_IFACE =
2900 TetheringManager.TETHER_ERROR_UNAVAIL_IFACE;
2901 /**
markchien68b67842020-03-19 13:37:43 +08002902 * @deprecated Use {@link TetheringManager#TETHER_ERROR_INTERNAL_ERROR}.
markchien75721e42020-01-21 13:11:06 +08002903 * {@hide}
2904 */
2905 @Deprecated
markchien68b67842020-03-19 13:37:43 +08002906 public static final int TETHER_ERROR_MASTER_ERROR =
2907 TetheringManager.TETHER_ERROR_INTERNAL_ERROR;
markchien75721e42020-01-21 13:11:06 +08002908 /**
2909 * @deprecated Use {@link TetheringManager#TETHER_ERROR_TETHER_IFACE_ERROR}.
2910 * {@hide}
2911 */
2912 @Deprecated
2913 public static final int TETHER_ERROR_TETHER_IFACE_ERROR =
2914 TetheringManager.TETHER_ERROR_TETHER_IFACE_ERROR;
2915 /**
2916 * @deprecated Use {@link TetheringManager#TETHER_ERROR_UNTETHER_IFACE_ERROR}.
2917 * {@hide}
2918 */
2919 @Deprecated
2920 public static final int TETHER_ERROR_UNTETHER_IFACE_ERROR =
2921 TetheringManager.TETHER_ERROR_UNTETHER_IFACE_ERROR;
2922 /**
markchien68b67842020-03-19 13:37:43 +08002923 * @deprecated Use {@link TetheringManager#TETHER_ERROR_ENABLE_FORWARDING_ERROR}.
markchien75721e42020-01-21 13:11:06 +08002924 * {@hide}
2925 */
2926 @Deprecated
2927 public static final int TETHER_ERROR_ENABLE_NAT_ERROR =
markchien68b67842020-03-19 13:37:43 +08002928 TetheringManager.TETHER_ERROR_ENABLE_FORWARDING_ERROR;
markchien75721e42020-01-21 13:11:06 +08002929 /**
markchien68b67842020-03-19 13:37:43 +08002930 * @deprecated Use {@link TetheringManager#TETHER_ERROR_DISABLE_FORWARDING_ERROR}.
markchien75721e42020-01-21 13:11:06 +08002931 * {@hide}
2932 */
2933 @Deprecated
2934 public static final int TETHER_ERROR_DISABLE_NAT_ERROR =
markchien68b67842020-03-19 13:37:43 +08002935 TetheringManager.TETHER_ERROR_DISABLE_FORWARDING_ERROR;
markchien75721e42020-01-21 13:11:06 +08002936 /**
2937 * @deprecated Use {@link TetheringManager#TETHER_ERROR_IFACE_CFG_ERROR}.
2938 * {@hide}
2939 */
2940 @Deprecated
2941 public static final int TETHER_ERROR_IFACE_CFG_ERROR =
2942 TetheringManager.TETHER_ERROR_IFACE_CFG_ERROR;
2943 /**
markchien68b67842020-03-19 13:37:43 +08002944 * @deprecated Use {@link TetheringManager#TETHER_ERROR_PROVISIONING_FAILED}.
markchien75721e42020-01-21 13:11:06 +08002945 * {@hide}
2946 */
markchien5bd499c2019-01-16 17:44:13 +08002947 @SystemApi
markchien75721e42020-01-21 13:11:06 +08002948 @Deprecated
Remi NGUYEN VAN23b853b2021-02-15 18:52:06 +09002949 public static final int TETHER_ERROR_PROVISION_FAILED = 11;
markchien75721e42020-01-21 13:11:06 +08002950 /**
2951 * @deprecated Use {@link TetheringManager#TETHER_ERROR_DHCPSERVER_ERROR}.
2952 * {@hide}
2953 */
2954 @Deprecated
2955 public static final int TETHER_ERROR_DHCPSERVER_ERROR =
2956 TetheringManager.TETHER_ERROR_DHCPSERVER_ERROR;
2957 /**
2958 * @deprecated Use {@link TetheringManager#TETHER_ERROR_ENTITLEMENT_UNKNOWN}.
2959 * {@hide}
2960 */
markchien5bd499c2019-01-16 17:44:13 +08002961 @SystemApi
markchien75721e42020-01-21 13:11:06 +08002962 @Deprecated
Remi NGUYEN VAN23b853b2021-02-15 18:52:06 +09002963 public static final int TETHER_ERROR_ENTITLEMENT_UNKONWN = 13;
Robert Greenwalt4283ded2010-03-02 17:25:02 -08002964
2965 /**
Robert Greenwalt26744a52013-02-15 10:56:35 -08002966 * Get a more detailed error code after a Tethering or Untethering
2967 * request asynchronously failed.
2968 *
2969 * @param iface The name of the interface of interest
Robert Greenwalt4283ded2010-03-02 17:25:02 -08002970 * @return error The error code of the last error tethering or untethering the named
2971 * interface
Robert Greenwalt26744a52013-02-15 10:56:35 -08002972 *
markchien75721e42020-01-21 13:11:06 +08002973 * @deprecated Use {@link TetheringEventCallback#onError(String, int)} instead.
Robert Greenwalt4283ded2010-03-02 17:25:02 -08002974 * {@hide}
2975 */
Jeff Sharkey2ac62992017-04-24 11:18:03 -06002976 @RequiresPermission(android.Manifest.permission.ACCESS_NETWORK_STATE)
Mathew Inwoodd078d3d2020-10-27 11:47:29 +00002977 @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.R, trackingBug = 170729553)
markchien75721e42020-01-21 13:11:06 +08002978 @Deprecated
Robert Greenwalt4283ded2010-03-02 17:25:02 -08002979 public int getLastTetherError(String iface) {
markchien68b67842020-03-19 13:37:43 +08002980 int error = mTetheringManager.getLastTetherError(iface);
2981 if (error == TetheringManager.TETHER_ERROR_UNKNOWN_TYPE) {
2982 // TETHER_ERROR_UNKNOWN_TYPE was introduced with TetheringManager and has never been
2983 // returned by ConnectivityManager. Convert it to the legacy TETHER_ERROR_UNKNOWN_IFACE
2984 // instead.
2985 error = TetheringManager.TETHER_ERROR_UNKNOWN_IFACE;
2986 }
2987 return error;
Robert Greenwalt93dc1042010-06-15 12:19:37 -07002988 }
2989
markchiendd41c822019-03-06 16:25:00 +08002990 /** @hide */
2991 @Retention(RetentionPolicy.SOURCE)
2992 @IntDef(value = {
2993 TETHER_ERROR_NO_ERROR,
2994 TETHER_ERROR_PROVISION_FAILED,
2995 TETHER_ERROR_ENTITLEMENT_UNKONWN,
2996 })
2997 public @interface EntitlementResultCode {
2998 }
2999
Robert Greenwalt93dc1042010-06-15 12:19:37 -07003000 /**
markchiendd41c822019-03-06 16:25:00 +08003001 * Callback for use with {@link #getLatestTetheringEntitlementResult} to find out whether
markchien5bd499c2019-01-16 17:44:13 +08003002 * entitlement succeeded.
markchien75721e42020-01-21 13:11:06 +08003003 *
3004 * @deprecated Use {@link TetheringManager#OnTetheringEntitlementResultListener} instead.
markchien5bd499c2019-01-16 17:44:13 +08003005 * @hide
3006 */
3007 @SystemApi
markchien75721e42020-01-21 13:11:06 +08003008 @Deprecated
markchiendd41c822019-03-06 16:25:00 +08003009 public interface OnTetheringEntitlementResultListener {
3010 /**
3011 * Called to notify entitlement result.
3012 *
3013 * @param resultCode an int value of entitlement result. It may be one of
3014 * {@link #TETHER_ERROR_NO_ERROR},
3015 * {@link #TETHER_ERROR_PROVISION_FAILED}, or
3016 * {@link #TETHER_ERROR_ENTITLEMENT_UNKONWN}.
3017 */
Jeremy Kleina9aad902019-03-12 13:32:08 -07003018 void onTetheringEntitlementResult(@EntitlementResultCode int resultCode);
markchiendd41c822019-03-06 16:25:00 +08003019 }
3020
3021 /**
markchien5bd499c2019-01-16 17:44:13 +08003022 * Get the last value of the entitlement check on this downstream. If the cached value is
3023 * {@link #TETHER_ERROR_NO_ERROR} or showEntitlementUi argument is false, it just return the
3024 * cached value. Otherwise, a UI-based entitlement check would be performed. It is not
3025 * guaranteed that the UI-based entitlement check will complete in any specific time period
3026 * and may in fact never complete. Any successful entitlement check the platform performs for
3027 * any reason will update the cached value.
3028 *
3029 * @param type the downstream type of tethering. Must be one of
3030 * {@link #TETHERING_WIFI},
3031 * {@link #TETHERING_USB}, or
3032 * {@link #TETHERING_BLUETOOTH}.
3033 * @param showEntitlementUi a boolean indicating whether to run UI-based entitlement check.
markchiendd41c822019-03-06 16:25:00 +08003034 * @param executor the executor on which callback will be invoked.
3035 * @param listener an {@link OnTetheringEntitlementResultListener} which will be called to
3036 * notify the caller of the result of entitlement check. The listener may be called zero
3037 * or one time.
markchien75721e42020-01-21 13:11:06 +08003038 * @deprecated Use {@link TetheringManager#requestLatestTetheringEntitlementResult} instead.
markchien5bd499c2019-01-16 17:44:13 +08003039 * {@hide}
3040 */
3041 @SystemApi
markchien75721e42020-01-21 13:11:06 +08003042 @Deprecated
markchien5bd499c2019-01-16 17:44:13 +08003043 @RequiresPermission(android.Manifest.permission.TETHER_PRIVILEGED)
markchiendd41c822019-03-06 16:25:00 +08003044 public void getLatestTetheringEntitlementResult(int type, boolean showEntitlementUi,
3045 @NonNull @CallbackExecutor Executor executor,
3046 @NonNull final OnTetheringEntitlementResultListener listener) {
Remi NGUYEN VANc4dd8c92021-03-15 07:31:54 +00003047 Objects.requireNonNull(listener, "TetheringEntitlementResultListener cannot be null.");
markchiendd41c822019-03-06 16:25:00 +08003048 ResultReceiver wrappedListener = new ResultReceiver(null) {
3049 @Override
3050 protected void onReceiveResult(int resultCode, Bundle resultData) {
lucaslin9cc04192021-03-04 09:38:21 +08003051 final long token = Binder.clearCallingIdentity();
3052 try {
3053 executor.execute(() -> {
3054 listener.onTetheringEntitlementResult(resultCode);
3055 });
3056 } finally {
3057 Binder.restoreCallingIdentity(token);
3058 }
markchiendd41c822019-03-06 16:25:00 +08003059 }
3060 };
3061
Automerger Merge Worker9b0c0c62020-03-06 00:38:43 +00003062 mTetheringManager.requestLatestTetheringEntitlementResult(type, wrappedListener,
markchienb0bb7862019-12-16 20:15:20 +08003063 showEntitlementUi);
markchiendd41c822019-03-06 16:25:00 +08003064 }
3065
3066 /**
Robert Greenwalt26744a52013-02-15 10:56:35 -08003067 * Report network connectivity status. This is currently used only
3068 * to alter status bar UI.
Paul Jensen19b3ee72015-05-06 11:10:18 -04003069 * <p>This method requires the caller to hold the permission
3070 * {@link android.Manifest.permission#STATUS_BAR}.
Robert Greenwalt26744a52013-02-15 10:56:35 -08003071 *
Robert Greenwalt986c7412010-09-08 15:24:47 -07003072 * @param networkType The type of network you want to report on
3073 * @param percentage The quality of the connection 0 is bad, 100 is good
Chalard Jean5acb7b72018-03-08 13:54:53 +09003074 * @deprecated Types are deprecated. Use {@link #reportNetworkConnectivity} instead.
Robert Greenwalt986c7412010-09-08 15:24:47 -07003075 * {@hide}
3076 */
3077 public void reportInetCondition(int networkType, int percentage) {
Soi, Yoshinari99efcbd2015-11-12 12:09:02 +09003078 printStackTrace();
Robert Greenwalt986c7412010-09-08 15:24:47 -07003079 try {
3080 mService.reportInetCondition(networkType, percentage);
3081 } catch (RemoteException e) {
Jeff Sharkeya6066fd2016-03-01 19:27:23 -07003082 throw e.rethrowFromSystemServer();
Robert Greenwalt986c7412010-09-08 15:24:47 -07003083 }
3084 }
Robert Greenwaltc3c5f862010-10-11 16:00:27 -07003085
3086 /**
Robert Greenwaltd0ebdbb2014-05-18 23:07:25 -07003087 * Report a problem network to the framework. This provides a hint to the system
Ye Wen849c72a2014-07-21 14:19:01 -07003088 * that there might be connectivity problems on this network and may cause
Robert Greenwaltd0ebdbb2014-05-18 23:07:25 -07003089 * the framework to re-evaluate network connectivity and/or switch to another
3090 * network.
Robert Greenwalt2bebbfb2014-03-26 16:47:06 -07003091 *
Robert Greenwaltd0ebdbb2014-05-18 23:07:25 -07003092 * @param network The {@link Network} the application was attempting to use
3093 * or {@code null} to indicate the current default network.
Paul Jensen7aac1b32015-04-07 12:43:13 -04003094 * @deprecated Use {@link #reportNetworkConnectivity} which allows reporting both
3095 * working and non-working connectivity.
Robert Greenwalt2bebbfb2014-03-26 16:47:06 -07003096 */
Aurimas Liutikasa6bb0232016-05-24 15:22:55 -07003097 @Deprecated
Chalard Jean42a9c292019-01-07 19:26:34 +09003098 public void reportBadNetwork(@Nullable Network network) {
Soi, Yoshinari99efcbd2015-11-12 12:09:02 +09003099 printStackTrace();
Robert Greenwalt2bebbfb2014-03-26 16:47:06 -07003100 try {
Paul Jensen7aac1b32015-04-07 12:43:13 -04003101 // One of these will be ignored because it matches system's current state.
3102 // The other will trigger the necessary reevaluation.
3103 mService.reportNetworkConnectivity(network, true);
3104 mService.reportNetworkConnectivity(network, false);
3105 } catch (RemoteException e) {
Jeff Sharkeya6066fd2016-03-01 19:27:23 -07003106 throw e.rethrowFromSystemServer();
Paul Jensen7aac1b32015-04-07 12:43:13 -04003107 }
3108 }
3109
3110 /**
3111 * Report to the framework whether a network has working connectivity.
3112 * This provides a hint to the system that a particular network is providing
3113 * working connectivity or not. In response the framework may re-evaluate
3114 * the network's connectivity and might take further action thereafter.
3115 *
3116 * @param network The {@link Network} the application was attempting to use
3117 * or {@code null} to indicate the current default network.
3118 * @param hasConnectivity {@code true} if the application was able to successfully access the
3119 * Internet using {@code network} or {@code false} if not.
3120 */
Chalard Jean42a9c292019-01-07 19:26:34 +09003121 public void reportNetworkConnectivity(@Nullable Network network, boolean hasConnectivity) {
Soi, Yoshinari99efcbd2015-11-12 12:09:02 +09003122 printStackTrace();
Paul Jensen7aac1b32015-04-07 12:43:13 -04003123 try {
3124 mService.reportNetworkConnectivity(network, hasConnectivity);
Robert Greenwalt2bebbfb2014-03-26 16:47:06 -07003125 } catch (RemoteException e) {
Jeff Sharkeya6066fd2016-03-01 19:27:23 -07003126 throw e.rethrowFromSystemServer();
Robert Greenwalt2bebbfb2014-03-26 16:47:06 -07003127 }
3128 }
3129
3130 /**
Chalard Jeanaac3b872021-03-17 17:03:34 +09003131 * Set a network-independent global HTTP proxy.
Paul Jensen19b3ee72015-05-06 11:10:18 -04003132 *
Chalard Jeanaac3b872021-03-17 17:03:34 +09003133 * This sets an HTTP proxy that applies to all networks and overrides any network-specific
3134 * proxy. If set, HTTP libraries that are proxy-aware will use this global proxy when
3135 * accessing any network, regardless of what the settings for that network are.
3136 *
3137 * Note that HTTP proxies are by nature typically network-dependent, and setting a global
3138 * proxy is likely to break networking on multiple networks. This method is only meant
3139 * for device policy clients looking to do general internal filtering or similar use cases.
3140 *
3141 * {@see #getGlobalProxy}
3142 * {@see LinkProperties#getHttpProxy}
3143 *
3144 * @param p A {@link ProxyInfo} object defining the new global HTTP proxy. Calling this
3145 * method with a {@code null} value will clear the global HTTP proxy.
Robert Greenwaltd0ebdbb2014-05-18 23:07:25 -07003146 * @hide
Robert Greenwaltc3c5f862010-10-11 16:00:27 -07003147 */
Chalard Jeanaac3b872021-03-17 17:03:34 +09003148 // Used by Device Policy Manager to set the global proxy.
Chiachang Wang0cefc1c2021-03-18 09:44:34 +08003149 @SystemApi(client = MODULE_LIBRARIES)
paulhuec0a9632019-08-12 16:25:11 +08003150 @RequiresPermission(android.Manifest.permission.NETWORK_STACK)
Chalard Jeanaac3b872021-03-17 17:03:34 +09003151 public void setGlobalProxy(@Nullable final ProxyInfo p) {
Robert Greenwaltc3c5f862010-10-11 16:00:27 -07003152 try {
3153 mService.setGlobalProxy(p);
3154 } catch (RemoteException e) {
Jeff Sharkeya6066fd2016-03-01 19:27:23 -07003155 throw e.rethrowFromSystemServer();
Robert Greenwaltc3c5f862010-10-11 16:00:27 -07003156 }
3157 }
3158
3159 /**
Robert Greenwalt26744a52013-02-15 10:56:35 -08003160 * Retrieve any network-independent global HTTP proxy.
3161 *
Jason Monk1e3df5d2014-04-25 15:00:09 -04003162 * @return {@link ProxyInfo} for the current global HTTP proxy or {@code null}
Robert Greenwalt26744a52013-02-15 10:56:35 -08003163 * if no global HTTP proxy is set.
Robert Greenwaltd0ebdbb2014-05-18 23:07:25 -07003164 * @hide
Robert Greenwaltc3c5f862010-10-11 16:00:27 -07003165 */
Chiachang Wang0cefc1c2021-03-18 09:44:34 +08003166 @SystemApi(client = MODULE_LIBRARIES)
3167 @Nullable
Jason Monk1e3df5d2014-04-25 15:00:09 -04003168 public ProxyInfo getGlobalProxy() {
Robert Greenwaltc3c5f862010-10-11 16:00:27 -07003169 try {
3170 return mService.getGlobalProxy();
3171 } catch (RemoteException e) {
Jeff Sharkeya6066fd2016-03-01 19:27:23 -07003172 throw e.rethrowFromSystemServer();
Robert Greenwaltc3c5f862010-10-11 16:00:27 -07003173 }
3174 }
3175
3176 /**
Paul Jensenfdec88b2015-05-06 07:32:40 -04003177 * Retrieve the global HTTP proxy, or if no global HTTP proxy is set, a
3178 * network-specific HTTP proxy. If {@code network} is null, the
3179 * network-specific proxy returned is the proxy of the default active
3180 * network.
3181 *
3182 * @return {@link ProxyInfo} for the current global HTTP proxy, or if no
3183 * global HTTP proxy is set, {@code ProxyInfo} for {@code network},
3184 * or when {@code network} is {@code null},
3185 * the {@code ProxyInfo} for the default active network. Returns
3186 * {@code null} when no proxy applies or the caller doesn't have
3187 * permission to use {@code network}.
3188 * @hide
3189 */
3190 public ProxyInfo getProxyForNetwork(Network network) {
3191 try {
3192 return mService.getProxyForNetwork(network);
3193 } catch (RemoteException e) {
Jeff Sharkeya6066fd2016-03-01 19:27:23 -07003194 throw e.rethrowFromSystemServer();
Paul Jensenfdec88b2015-05-06 07:32:40 -04003195 }
3196 }
3197
3198 /**
Paul Jensen12131352014-12-10 15:12:18 -05003199 * Get the current default HTTP proxy settings. If a global proxy is set it will be returned,
3200 * otherwise if this process is bound to a {@link Network} using
Paul Jensen895c3942015-03-10 10:54:12 -04003201 * {@link #bindProcessToNetwork} then that {@code Network}'s proxy is returned, otherwise
Paul Jensen12131352014-12-10 15:12:18 -05003202 * the default network's proxy is returned.
Robert Greenwalt26744a52013-02-15 10:56:35 -08003203 *
Jason Monk1e3df5d2014-04-25 15:00:09 -04003204 * @return the {@link ProxyInfo} for the current HTTP proxy, or {@code null} if no
Robert Greenwalt26744a52013-02-15 10:56:35 -08003205 * HTTP proxy is active.
Robert Greenwaltc3c5f862010-10-11 16:00:27 -07003206 */
Chalard Jean42a9c292019-01-07 19:26:34 +09003207 @Nullable
Paul Jensen12131352014-12-10 15:12:18 -05003208 public ProxyInfo getDefaultProxy() {
Paul Jensenfdec88b2015-05-06 07:32:40 -04003209 return getProxyForNetwork(getBoundNetworkForProcess());
Robert Greenwaltc3c5f862010-10-11 16:00:27 -07003210 }
Robert Greenwalt34848c02011-03-25 13:09:25 -07003211
3212 /**
Robert Greenwalt0114f6e2011-08-31 11:46:42 -07003213 * Returns true if the hardware supports the given network type
3214 * else it returns false. This doesn't indicate we have coverage
3215 * or are authorized onto a network, just whether or not the
Robert Greenwalt26744a52013-02-15 10:56:35 -08003216 * hardware supports it. For example a GSM phone without a SIM
3217 * should still return {@code true} for mobile data, but a wifi only
3218 * tablet would return {@code false}.
3219 *
3220 * @param networkType The network type we'd like to check
3221 * @return {@code true} if supported, else {@code false}
Chalard Jean5acb7b72018-03-08 13:54:53 +09003222 * @deprecated Types are deprecated. Use {@link NetworkCapabilities} instead.
Robert Greenwalt0114f6e2011-08-31 11:46:42 -07003223 * @hide
3224 */
Chalard Jean5acb7b72018-03-08 13:54:53 +09003225 @Deprecated
Jeff Sharkey2ac62992017-04-24 11:18:03 -06003226 @RequiresPermission(android.Manifest.permission.ACCESS_NETWORK_STATE)
Chalard Jeanaa91c9d2019-04-09 15:46:21 +09003227 @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P, trackingBug = 130143562)
Robert Greenwalt0114f6e2011-08-31 11:46:42 -07003228 public boolean isNetworkSupported(int networkType) {
3229 try {
3230 return mService.isNetworkSupported(networkType);
Jeff Sharkeya6066fd2016-03-01 19:27:23 -07003231 } catch (RemoteException e) {
3232 throw e.rethrowFromSystemServer();
3233 }
Robert Greenwalt0114f6e2011-08-31 11:46:42 -07003234 }
Jeff Sharkeyd00b1302012-04-12 18:34:54 -07003235
3236 /**
3237 * Returns if the currently active data network is metered. A network is
3238 * classified as metered when the user is sensitive to heavy data usage on
Robert Greenwalt26744a52013-02-15 10:56:35 -08003239 * that connection due to monetary costs, data limitations or
3240 * battery/performance issues. You should check this before doing large
3241 * data transfers, and warn the user or delay the operation until another
3242 * network is available.
3243 *
3244 * @return {@code true} if large transfers should be avoided, otherwise
3245 * {@code false}.
Jeff Sharkeyd00b1302012-04-12 18:34:54 -07003246 */
Jeff Sharkey2ac62992017-04-24 11:18:03 -06003247 @RequiresPermission(android.Manifest.permission.ACCESS_NETWORK_STATE)
Jeff Sharkeyd00b1302012-04-12 18:34:54 -07003248 public boolean isActiveNetworkMetered() {
3249 try {
3250 return mService.isActiveNetworkMetered();
3251 } catch (RemoteException e) {
Jeff Sharkeya6066fd2016-03-01 19:27:23 -07003252 throw e.rethrowFromSystemServer();
Jeff Sharkeyd00b1302012-04-12 18:34:54 -07003253 }
3254 }
Jeff Sharkeyf2e722a2012-08-25 00:05:46 -07003255
Robert Greenwalt26744a52013-02-15 10:56:35 -08003256 /**
Sarah Chincab74db2020-11-25 12:15:14 -08003257 * Set sign in error notification to visible or invisible
Wink Saville89c87b92013-08-29 08:55:16 -07003258 *
Sarah Chincab74db2020-11-25 12:15:14 -08003259 * @hide
Paul Jensena8e6dc62015-03-18 12:23:02 -04003260 * @deprecated Doesn't properly deal with multiple connected networks of the same type.
Wink Saville89c87b92013-08-29 08:55:16 -07003261 */
Aurimas Liutikasa6bb0232016-05-24 15:22:55 -07003262 @Deprecated
Wink Saville89c87b92013-08-29 08:55:16 -07003263 public void setProvisioningNotificationVisible(boolean visible, int networkType,
Paul Jensen82c7e422014-09-15 15:59:36 -04003264 String action) {
Wink Saville89c87b92013-08-29 08:55:16 -07003265 try {
Paul Jensen82c7e422014-09-15 15:59:36 -04003266 mService.setProvisioningNotificationVisible(visible, networkType, action);
Wink Saville89c87b92013-08-29 08:55:16 -07003267 } catch (RemoteException e) {
Jeff Sharkeya6066fd2016-03-01 19:27:23 -07003268 throw e.rethrowFromSystemServer();
Wink Saville89c87b92013-08-29 08:55:16 -07003269 }
3270 }
Yuhao Zheng15019892013-09-09 17:00:04 -07003271
3272 /**
3273 * Set the value for enabling/disabling airplane mode
3274 *
3275 * @param enable whether to enable airplane mode or not
3276 *
Yuhao Zheng15019892013-09-09 17:00:04 -07003277 * @hide
3278 */
Lorenzo Colittic4d01862018-10-09 18:55:11 +09003279 @RequiresPermission(anyOf = {
Edward Savage-Jones3b9fe322019-11-26 13:18:08 +01003280 android.Manifest.permission.NETWORK_AIRPLANE_MODE,
Lorenzo Colittic4d01862018-10-09 18:55:11 +09003281 android.Manifest.permission.NETWORK_SETTINGS,
3282 android.Manifest.permission.NETWORK_SETUP_WIZARD,
3283 android.Manifest.permission.NETWORK_STACK})
Lorenzo Colitti272a77d2018-10-09 18:50:32 +09003284 @SystemApi
Yuhao Zheng15019892013-09-09 17:00:04 -07003285 public void setAirplaneMode(boolean enable) {
3286 try {
3287 mService.setAirplaneMode(enable);
3288 } catch (RemoteException e) {
Jeff Sharkeya6066fd2016-03-01 19:27:23 -07003289 throw e.rethrowFromSystemServer();
Yuhao Zheng15019892013-09-09 17:00:04 -07003290 }
3291 }
Robert Greenwalt948aea52014-04-11 15:53:27 -07003292
Lorenzo Colitti92e6c272020-01-08 00:04:09 +09003293 /**
3294 * Registers the specified {@link NetworkProvider}.
3295 * Each listener must only be registered once. The listener can be unregistered with
3296 * {@link #unregisterNetworkProvider}.
3297 *
3298 * @param provider the provider to register
3299 * @return the ID of the provider. This ID must be used by the provider when registering
3300 * {@link android.net.NetworkAgent}s.
3301 * @hide
3302 */
3303 @SystemApi
Automerger Merge Workeref12b642020-03-04 18:04:01 +00003304 @RequiresPermission(anyOf = {
3305 NetworkStack.PERMISSION_MAINLINE_NETWORK_STACK,
3306 android.Manifest.permission.NETWORK_FACTORY})
Lorenzo Colitti92e6c272020-01-08 00:04:09 +09003307 public int registerNetworkProvider(@NonNull NetworkProvider provider) {
3308 if (provider.getProviderId() != NetworkProvider.ID_NONE) {
Lorenzo Colitti92e6c272020-01-08 00:04:09 +09003309 throw new IllegalStateException("NetworkProviders can only be registered once");
3310 }
3311
3312 try {
3313 int providerId = mService.registerNetworkProvider(provider.getMessenger(),
3314 provider.getName());
3315 provider.setProviderId(providerId);
3316 } catch (RemoteException e) {
3317 throw e.rethrowFromSystemServer();
3318 }
3319 return provider.getProviderId();
3320 }
3321
3322 /**
3323 * Unregisters the specified NetworkProvider.
3324 *
3325 * @param provider the provider to unregister
3326 * @hide
3327 */
3328 @SystemApi
Automerger Merge Workeref12b642020-03-04 18:04:01 +00003329 @RequiresPermission(anyOf = {
3330 NetworkStack.PERMISSION_MAINLINE_NETWORK_STACK,
3331 android.Manifest.permission.NETWORK_FACTORY})
Lorenzo Colitti92e6c272020-01-08 00:04:09 +09003332 public void unregisterNetworkProvider(@NonNull NetworkProvider provider) {
3333 try {
3334 mService.unregisterNetworkProvider(provider.getMessenger());
3335 } catch (RemoteException e) {
3336 throw e.rethrowFromSystemServer();
3337 }
3338 provider.setProviderId(NetworkProvider.ID_NONE);
3339 }
3340
Chalard Jean1ef1d902021-01-05 08:40:09 +09003341 /**
3342 * Register or update a network offer with ConnectivityService.
3343 *
3344 * ConnectivityService keeps track of offers made by the various providers and matches
Chalard Jeand7268cd2021-03-24 17:43:10 +09003345 * them to networking requests made by apps or the system. A callback identifies an offer
3346 * uniquely, and later calls with the same callback update the offer. The provider supplies a
3347 * score and the capabilities of the network it might be able to bring up ; these act as
3348 * filters used by ConnectivityService to only send those requests that can be fulfilled by the
Chalard Jean1ef1d902021-01-05 08:40:09 +09003349 * provider.
3350 *
3351 * The provider is under no obligation to be able to bring up the network it offers at any
3352 * given time. Instead, this mechanism is meant to limit requests received by providers
3353 * to those they actually have a chance to fulfill, as providers don't have a way to compare
3354 * the quality of the network satisfying a given request to their own offer.
3355 *
3356 * An offer can be updated by calling this again with the same callback object. This is
3357 * similar to calling unofferNetwork and offerNetwork again, but will only update the
3358 * provider with the changes caused by the changes in the offer.
3359 *
3360 * @param provider The provider making this offer.
3361 * @param score The prospective score of the network.
3362 * @param caps The prospective capabilities of the network.
3363 * @param callback The callback to call when this offer is needed or unneeded.
Chalard Jean7b6a33b2021-01-12 10:58:56 +09003364 * @hide exposed via the NetworkProvider class.
Chalard Jean1ef1d902021-01-05 08:40:09 +09003365 */
3366 @RequiresPermission(anyOf = {
3367 NetworkStack.PERMISSION_MAINLINE_NETWORK_STACK,
3368 android.Manifest.permission.NETWORK_FACTORY})
Chalard Jeanb004da52021-03-22 22:44:02 +09003369 public void offerNetwork(@NonNull final int providerId,
Chalard Jean1ef1d902021-01-05 08:40:09 +09003370 @NonNull final NetworkScore score, @NonNull final NetworkCapabilities caps,
3371 @NonNull final INetworkOfferCallback callback) {
3372 try {
Chalard Jeanb004da52021-03-22 22:44:02 +09003373 mService.offerNetwork(providerId,
Chalard Jean1ef1d902021-01-05 08:40:09 +09003374 Objects.requireNonNull(score, "null score"),
3375 Objects.requireNonNull(caps, "null caps"),
3376 Objects.requireNonNull(callback, "null callback"));
3377 } catch (RemoteException e) {
3378 throw e.rethrowFromSystemServer();
3379 }
3380 }
Lorenzo Colitti92e6c272020-01-08 00:04:09 +09003381
Chalard Jean1ef1d902021-01-05 08:40:09 +09003382 /**
3383 * Withdraw a network offer made with {@link #offerNetwork}.
3384 *
3385 * @param callback The callback passed at registration time. This must be the same object
3386 * that was passed to {@link #offerNetwork}
Chalard Jean7b6a33b2021-01-12 10:58:56 +09003387 * @hide exposed via the NetworkProvider class.
Chalard Jean1ef1d902021-01-05 08:40:09 +09003388 */
3389 public void unofferNetwork(@NonNull final INetworkOfferCallback callback) {
3390 try {
3391 mService.unofferNetwork(Objects.requireNonNull(callback));
3392 } catch (RemoteException e) {
3393 throw e.rethrowFromSystemServer();
3394 }
3395 }
Lorenzo Colitti92e6c272020-01-08 00:04:09 +09003396 /** @hide exposed via the NetworkProvider class. */
Automerger Merge Workeref12b642020-03-04 18:04:01 +00003397 @RequiresPermission(anyOf = {
3398 NetworkStack.PERMISSION_MAINLINE_NETWORK_STACK,
3399 android.Manifest.permission.NETWORK_FACTORY})
Lorenzo Colitti92e6c272020-01-08 00:04:09 +09003400 public void declareNetworkRequestUnfulfillable(@NonNull NetworkRequest request) {
3401 try {
3402 mService.declareNetworkRequestUnfulfillable(request);
3403 } catch (RemoteException e) {
3404 throw e.rethrowFromSystemServer();
3405 }
3406 }
3407
Paul Jensenc2569432015-02-13 14:18:39 -05003408 /**
3409 * @hide
3410 * Register a NetworkAgent with ConnectivityService.
Chalard Jean1aea87a2019-12-13 19:47:12 +09003411 * @return Network corresponding to NetworkAgent.
Paul Jensenc2569432015-02-13 14:18:39 -05003412 */
Automerger Merge Workeref12b642020-03-04 18:04:01 +00003413 @RequiresPermission(anyOf = {
3414 NetworkStack.PERMISSION_MAINLINE_NETWORK_STACK,
3415 android.Manifest.permission.NETWORK_FACTORY})
Remi NGUYEN VAN37ebfa82020-12-23 12:45:08 +09003416 public Network registerNetworkAgent(INetworkAgent na, NetworkInfo ni, LinkProperties lp,
Chalard Jeandd753522020-12-21 18:36:52 +09003417 NetworkCapabilities nc, @NonNull NetworkScore score, NetworkAgentConfig config,
3418 int providerId) {
Robert Greenwaltdb6fac52014-04-18 15:25:25 -07003419 try {
Remi NGUYEN VAN37ebfa82020-12-23 12:45:08 +09003420 return mService.registerNetworkAgent(na, ni, lp, nc, score, config, providerId);
Paul Jensenc2569432015-02-13 14:18:39 -05003421 } catch (RemoteException e) {
Jeff Sharkeya6066fd2016-03-01 19:27:23 -07003422 throw e.rethrowFromSystemServer();
Paul Jensenc2569432015-02-13 14:18:39 -05003423 }
Robert Greenwaltdb6fac52014-04-18 15:25:25 -07003424 }
3425
Robert Greenwalt2bebbfb2014-03-26 16:47:06 -07003426 /**
Hugo Benichi45a49542017-03-06 09:17:06 +09003427 * Base class for {@code NetworkRequest} callbacks. Used for notifications about network
3428 * changes. Should be extended by applications wanting notifications.
3429 *
3430 * A {@code NetworkCallback} is registered by calling
3431 * {@link #requestNetwork(NetworkRequest, NetworkCallback)},
3432 * {@link #registerNetworkCallback(NetworkRequest, NetworkCallback)},
Hugo Benichi7665a142018-02-07 21:17:43 +09003433 * or {@link #registerDefaultNetworkCallback(NetworkCallback)}. A {@code NetworkCallback} is
Hugo Benichi45a49542017-03-06 09:17:06 +09003434 * unregistered by calling {@link #unregisterNetworkCallback(NetworkCallback)}.
3435 * A {@code NetworkCallback} should be registered at most once at any time.
3436 * A {@code NetworkCallback} that has been unregistered can be registered again.
Robert Greenwalt2bebbfb2014-03-26 16:47:06 -07003437 */
Robert Greenwalt09e5cff2014-06-11 16:05:07 -07003438 public static class NetworkCallback {
Robert Greenwaltdb6fac52014-04-18 15:25:25 -07003439 /**
Roshan Pius7992afd2020-12-22 15:10:42 -08003440 * No flags associated with this callback.
3441 * @hide
3442 */
3443 public static final int FLAG_NONE = 0;
3444 /**
3445 * Use this flag to include any location sensitive data in {@link NetworkCapabilities} sent
3446 * via {@link #onCapabilitiesChanged(Network, NetworkCapabilities)}.
3447 * <p>
3448 * These include:
3449 * <li> Some transport info instances (retrieved via
3450 * {@link NetworkCapabilities#getTransportInfo()}) like {@link android.net.wifi.WifiInfo}
3451 * contain location sensitive information.
3452 * <li> OwnerUid (retrieved via {@link NetworkCapabilities#getOwnerUid()} is location
3453 * sensitive for wifi suggestor apps (i.e using {@link WifiNetworkSuggestion}).</li>
3454 * </p>
3455 * <p>
3456 * Note:
3457 * <li> Retrieving this location sensitive information (subject to app's location
3458 * permissions) will be noted by system. </li>
3459 * <li> Without this flag any {@link NetworkCapabilities} provided via the callback does
3460 * not include location sensitive info.
3461 * </p>
3462 */
Roshan Piusf75ffaa2021-03-11 21:16:44 -08003463 // Note: Some existing fields which are location sensitive may still be included without
3464 // this flag if the app targets SDK < S (to maintain backwards compatibility).
Roshan Pius7992afd2020-12-22 15:10:42 -08003465 public static final int FLAG_INCLUDE_LOCATION_INFO = 1 << 0;
3466
3467 /** @hide */
3468 @Retention(RetentionPolicy.SOURCE)
3469 @IntDef(flag = true, prefix = "FLAG_", value = {
3470 FLAG_NONE,
3471 FLAG_INCLUDE_LOCATION_INFO
3472 })
3473 public @interface Flag { }
3474
3475 /**
3476 * All the valid flags for error checking.
3477 */
3478 private static final int VALID_FLAGS = FLAG_INCLUDE_LOCATION_INFO;
3479
3480 public NetworkCallback() {
3481 this(FLAG_NONE);
3482 }
3483
3484 public NetworkCallback(@Flag int flags) {
Remi NGUYEN VANc4dd8c92021-03-15 07:31:54 +00003485 if ((flags & VALID_FLAGS) != flags) {
3486 throw new IllegalArgumentException("Invalid flags");
3487 }
Roshan Pius7992afd2020-12-22 15:10:42 -08003488 mFlags = flags;
3489 }
3490
3491 /**
Lorenzo Colitticcf82102015-04-24 12:23:24 +09003492 * Called when the framework connects to a new network to evaluate whether it satisfies this
3493 * request. If evaluation succeeds, this callback may be followed by an {@link #onAvailable}
3494 * callback. There is no guarantee that this new network will satisfy any requests, or that
3495 * the network will stay connected for longer than the time necessary to evaluate it.
3496 * <p>
3497 * Most applications <b>should not</b> act on this callback, and should instead use
3498 * {@link #onAvailable}. This callback is intended for use by applications that can assist
3499 * the framework in properly evaluating the network &mdash; for example, an application that
3500 * can automatically log in to a captive portal without user intervention.
3501 *
3502 * @param network The {@link Network} of the network that is being evaluated.
Lorenzo Colittia04936a2015-06-11 14:27:17 +09003503 *
3504 * @hide
Robert Greenwaltdb6fac52014-04-18 15:25:25 -07003505 */
paulhua9a6e2a2019-03-22 16:35:06 +08003506 public void onPreCheck(@NonNull Network network) {}
Robert Greenwaltdb6fac52014-04-18 15:25:25 -07003507
3508 /**
Lorenzo Colitticcf82102015-04-24 12:23:24 +09003509 * Called when the framework connects and has declared a new network ready for use.
Robert Greenwalt09e5cff2014-06-11 16:05:07 -07003510 * This callback may be called more than once if the {@link Network} that is
3511 * satisfying the request changes.
Robert Greenwaltd0ebdbb2014-05-18 23:07:25 -07003512 *
Robert Greenwaltd0ebdbb2014-05-18 23:07:25 -07003513 * @param network The {@link Network} of the satisfying network.
Chalard Jeanb6a0c6d2018-01-30 22:41:41 +09003514 * @param networkCapabilities The {@link NetworkCapabilities} of the satisfying network.
3515 * @param linkProperties The {@link LinkProperties} of the satisfying network.
junyulai5ab727b2018-08-07 19:50:45 +08003516 * @param blocked Whether access to the {@link Network} is blocked due to system policy.
Chalard Jeanb6a0c6d2018-01-30 22:41:41 +09003517 * @hide
3518 */
Lorenzo Colitti60104f52021-03-18 00:54:57 +09003519 public final void onAvailable(@NonNull Network network,
paulhua9a6e2a2019-03-22 16:35:06 +08003520 @NonNull NetworkCapabilities networkCapabilities,
Lorenzo Colitti60104f52021-03-18 00:54:57 +09003521 @NonNull LinkProperties linkProperties, @BlockedReason int blocked) {
Chalard Jeanb6a0c6d2018-01-30 22:41:41 +09003522 // Internally only this method is called when a new network is available, and
3523 // it calls the callback in the same way and order that older versions used
3524 // to call so as not to change the behavior.
Lorenzo Colitti60104f52021-03-18 00:54:57 +09003525 onAvailable(network, networkCapabilities, linkProperties, blocked != 0);
3526 onBlockedStatusChanged(network, blocked);
3527 }
3528
3529 /**
3530 * Legacy variant of onAvailable that takes a boolean blocked reason.
3531 *
3532 * This method has never been public API, but it's not final, so there may be apps that
3533 * implemented it and rely on it being called. Do our best not to break them.
3534 * Note: such apps will also get a second call to onBlockedStatusChanged immediately after
3535 * this method is called. There does not seem to be a way to avoid this.
3536 * TODO: add a compat check to move apps off this method, and eventually stop calling it.
3537 *
3538 * @hide
3539 */
3540 public void onAvailable(@NonNull Network network,
3541 @NonNull NetworkCapabilities networkCapabilities,
3542 @NonNull LinkProperties linkProperties, boolean blocked) {
Chalard Jeanb6a0c6d2018-01-30 22:41:41 +09003543 onAvailable(network);
3544 if (!networkCapabilities.hasCapability(
3545 NetworkCapabilities.NET_CAPABILITY_NOT_SUSPENDED)) {
3546 onNetworkSuspended(network);
3547 }
3548 onCapabilitiesChanged(network, networkCapabilities);
3549 onLinkPropertiesChanged(network, linkProperties);
Lorenzo Colitti60104f52021-03-18 00:54:57 +09003550 // No call to onBlockedStatusChanged here. That is done by the caller.
Chalard Jeanb6a0c6d2018-01-30 22:41:41 +09003551 }
3552
3553 /**
3554 * Called when the framework connects and has declared a new network ready for use.
Chalard Jean7f3a8742019-08-30 16:27:28 +09003555 *
3556 * <p>For callbacks registered with {@link #registerNetworkCallback}, multiple networks may
3557 * be available at the same time, and onAvailable will be called for each of these as they
3558 * appear.
3559 *
3560 * <p>For callbacks registered with {@link #requestNetwork} and
3561 * {@link #registerDefaultNetworkCallback}, this means the network passed as an argument
3562 * is the new best network for this request and is now tracked by this callback ; this
3563 * callback will no longer receive method calls about other networks that may have been
3564 * passed to this method previously. The previously-best network may have disconnected, or
3565 * it may still be around and the newly-best network may simply be better.
3566 *
3567 * <p>Starting with {@link android.os.Build.VERSION_CODES#O}, this will always immediately
3568 * be followed by a call to {@link #onCapabilitiesChanged(Network, NetworkCapabilities)}
3569 * then by a call to {@link #onLinkPropertiesChanged(Network, LinkProperties)}, and a call
3570 * to {@link #onBlockedStatusChanged(Network, boolean)}.
3571 *
3572 * <p>Do NOT call {@link #getNetworkCapabilities(Network)} or
3573 * {@link #getLinkProperties(Network)} or other synchronous ConnectivityManager methods in
3574 * this callback as this is prone to race conditions (there is no guarantee the objects
3575 * returned by these methods will be current). Instead, wait for a call to
3576 * {@link #onCapabilitiesChanged(Network, NetworkCapabilities)} and
3577 * {@link #onLinkPropertiesChanged(Network, LinkProperties)} whose arguments are guaranteed
3578 * to be well-ordered with respect to other callbacks.
Chalard Jeanb6a0c6d2018-01-30 22:41:41 +09003579 *
3580 * @param network The {@link Network} of the satisfying network.
Robert Greenwaltdb6fac52014-04-18 15:25:25 -07003581 */
paulhua9a6e2a2019-03-22 16:35:06 +08003582 public void onAvailable(@NonNull Network network) {}
Robert Greenwaltdb6fac52014-04-18 15:25:25 -07003583
3584 /**
Chalard Jean7f3a8742019-08-30 16:27:28 +09003585 * Called when the network is about to be lost, typically because there are no outstanding
3586 * requests left for it. This may be paired with a {@link NetworkCallback#onAvailable} call
3587 * with the new replacement network for graceful handover. This method is not guaranteed
3588 * to be called before {@link NetworkCallback#onLost} is called, for example in case a
3589 * network is suddenly disconnected.
Robert Greenwaltd0ebdbb2014-05-18 23:07:25 -07003590 *
Chalard Jean7f3a8742019-08-30 16:27:28 +09003591 * <p>Do NOT call {@link #getNetworkCapabilities(Network)} or
3592 * {@link #getLinkProperties(Network)} or other synchronous ConnectivityManager methods in
3593 * this callback as this is prone to race conditions ; calling these methods while in a
3594 * callback may return an outdated or even a null object.
3595 *
3596 * @param network The {@link Network} that is about to be lost.
3597 * @param maxMsToLive The time in milliseconds the system intends to keep the network
3598 * connected for graceful handover; note that the network may still
3599 * suffer a hard loss at any time.
Robert Greenwaltdb6fac52014-04-18 15:25:25 -07003600 */
paulhua9a6e2a2019-03-22 16:35:06 +08003601 public void onLosing(@NonNull Network network, int maxMsToLive) {}
Robert Greenwaltdb6fac52014-04-18 15:25:25 -07003602
3603 /**
Chalard Jean7f3a8742019-08-30 16:27:28 +09003604 * Called when a network disconnects or otherwise no longer satisfies this request or
3605 * callback.
3606 *
3607 * <p>If the callback was registered with requestNetwork() or
3608 * registerDefaultNetworkCallback(), it will only be invoked against the last network
3609 * returned by onAvailable() when that network is lost and no other network satisfies
3610 * the criteria of the request.
3611 *
3612 * <p>If the callback was registered with registerNetworkCallback() it will be called for
3613 * each network which no longer satisfies the criteria of the callback.
3614 *
3615 * <p>Do NOT call {@link #getNetworkCapabilities(Network)} or
3616 * {@link #getLinkProperties(Network)} or other synchronous ConnectivityManager methods in
3617 * this callback as this is prone to race conditions ; calling these methods while in a
3618 * callback may return an outdated or even a null object.
Robert Greenwaltd0ebdbb2014-05-18 23:07:25 -07003619 *
Robert Greenwaltd0ebdbb2014-05-18 23:07:25 -07003620 * @param network The {@link Network} lost.
Robert Greenwaltdb6fac52014-04-18 15:25:25 -07003621 */
paulhua9a6e2a2019-03-22 16:35:06 +08003622 public void onLost(@NonNull Network network) {}
Robert Greenwaltdb6fac52014-04-18 15:25:25 -07003623
3624 /**
Chalard Jean7f3a8742019-08-30 16:27:28 +09003625 * Called if no network is found within the timeout time specified in
Etan Cohen06633de2019-01-08 12:09:18 -08003626 * {@link #requestNetwork(NetworkRequest, NetworkCallback, int)} call or if the
3627 * requested network request cannot be fulfilled (whether or not a timeout was
3628 * specified). When this callback is invoked the associated
Etan Cohen67e58a02017-03-01 12:47:28 -08003629 * {@link NetworkRequest} will have already been removed and released, as if
3630 * {@link #unregisterNetworkCallback(NetworkCallback)} had been called.
Robert Greenwaltdb6fac52014-04-18 15:25:25 -07003631 */
Robert Greenwalt09e5cff2014-06-11 16:05:07 -07003632 public void onUnavailable() {}
Robert Greenwaltdb6fac52014-04-18 15:25:25 -07003633
3634 /**
Chalard Jean7f3a8742019-08-30 16:27:28 +09003635 * Called when the network corresponding to this request changes capabilities but still
3636 * satisfies the requested criteria.
3637 *
3638 * <p>Starting with {@link android.os.Build.VERSION_CODES#O} this method is guaranteed
3639 * to be called immediately after {@link #onAvailable}.
3640 *
3641 * <p>Do NOT call {@link #getLinkProperties(Network)} or other synchronous
3642 * ConnectivityManager methods in this callback as this is prone to race conditions :
3643 * calling these methods while in a callback may return an outdated or even a null object.
Robert Greenwaltd0ebdbb2014-05-18 23:07:25 -07003644 *
Robert Greenwaltd0ebdbb2014-05-18 23:07:25 -07003645 * @param network The {@link Network} whose capabilities have changed.
Roshan Pius7992afd2020-12-22 15:10:42 -08003646 * @param networkCapabilities The new {@link NetworkCapabilities} for this
Chalard Jeanb6a0c6d2018-01-30 22:41:41 +09003647 * network.
Robert Greenwaltdb6fac52014-04-18 15:25:25 -07003648 */
paulhua9a6e2a2019-03-22 16:35:06 +08003649 public void onCapabilitiesChanged(@NonNull Network network,
3650 @NonNull NetworkCapabilities networkCapabilities) {}
Robert Greenwaltdb6fac52014-04-18 15:25:25 -07003651
3652 /**
Chalard Jean7f3a8742019-08-30 16:27:28 +09003653 * Called when the network corresponding to this request changes {@link LinkProperties}.
3654 *
3655 * <p>Starting with {@link android.os.Build.VERSION_CODES#O} this method is guaranteed
3656 * to be called immediately after {@link #onAvailable}.
3657 *
3658 * <p>Do NOT call {@link #getNetworkCapabilities(Network)} or other synchronous
3659 * ConnectivityManager methods in this callback as this is prone to race conditions :
3660 * calling these methods while in a callback may return an outdated or even a null object.
Robert Greenwaltd0ebdbb2014-05-18 23:07:25 -07003661 *
Robert Greenwaltd0ebdbb2014-05-18 23:07:25 -07003662 * @param network The {@link Network} whose link properties have changed.
3663 * @param linkProperties The new {@link LinkProperties} for this network.
Robert Greenwaltdb6fac52014-04-18 15:25:25 -07003664 */
paulhua9a6e2a2019-03-22 16:35:06 +08003665 public void onLinkPropertiesChanged(@NonNull Network network,
3666 @NonNull LinkProperties linkProperties) {}
Robert Greenwaltdb6fac52014-04-18 15:25:25 -07003667
Robert Greenwalt41c19a02015-06-24 13:23:42 -07003668 /**
Chalard Jean7f3a8742019-08-30 16:27:28 +09003669 * Called when the network the framework connected to for this request suspends data
3670 * transmission temporarily.
3671 *
3672 * <p>This generally means that while the TCP connections are still live temporarily
3673 * network data fails to transfer. To give a specific example, this is used on cellular
3674 * networks to mask temporary outages when driving through a tunnel, etc. In general this
3675 * means read operations on sockets on this network will block once the buffers are
3676 * drained, and write operations will block once the buffers are full.
3677 *
3678 * <p>Do NOT call {@link #getNetworkCapabilities(Network)} or
3679 * {@link #getLinkProperties(Network)} or other synchronous ConnectivityManager methods in
3680 * this callback as this is prone to race conditions (there is no guarantee the objects
3681 * returned by these methods will be current).
3682 *
Robert Greenwalt41c19a02015-06-24 13:23:42 -07003683 * @hide
3684 */
paulhua9a6e2a2019-03-22 16:35:06 +08003685 public void onNetworkSuspended(@NonNull Network network) {}
Robert Greenwalt41c19a02015-06-24 13:23:42 -07003686
3687 /**
3688 * Called when the network the framework connected to for this request
Chalard Jeanb6a0c6d2018-01-30 22:41:41 +09003689 * returns from a {@link NetworkInfo.State#SUSPENDED} state. This should always be
3690 * preceded by a matching {@link NetworkCallback#onNetworkSuspended} call.
Chalard Jean7f3a8742019-08-30 16:27:28 +09003691
3692 * <p>Do NOT call {@link #getNetworkCapabilities(Network)} or
3693 * {@link #getLinkProperties(Network)} or other synchronous ConnectivityManager methods in
3694 * this callback as this is prone to race conditions : calling these methods while in a
3695 * callback may return an outdated or even a null object.
3696 *
Robert Greenwalt41c19a02015-06-24 13:23:42 -07003697 * @hide
3698 */
paulhua9a6e2a2019-03-22 16:35:06 +08003699 public void onNetworkResumed(@NonNull Network network) {}
Robert Greenwalt41c19a02015-06-24 13:23:42 -07003700
junyulai5ab727b2018-08-07 19:50:45 +08003701 /**
3702 * Called when access to the specified network is blocked or unblocked.
3703 *
Chalard Jean7f3a8742019-08-30 16:27:28 +09003704 * <p>Do NOT call {@link #getNetworkCapabilities(Network)} or
3705 * {@link #getLinkProperties(Network)} or other synchronous ConnectivityManager methods in
3706 * this callback as this is prone to race conditions : calling these methods while in a
3707 * callback may return an outdated or even a null object.
3708 *
junyulai5ab727b2018-08-07 19:50:45 +08003709 * @param network The {@link Network} whose blocked status has changed.
3710 * @param blocked The blocked status of this {@link Network}.
3711 */
junyulai61143782019-03-04 22:45:36 +08003712 public void onBlockedStatusChanged(@NonNull Network network, boolean blocked) {}
junyulai5ab727b2018-08-07 19:50:45 +08003713
Lorenzo Colitti60104f52021-03-18 00:54:57 +09003714 /**
Lorenzo Colittia37eaff2021-03-25 23:17:36 +09003715 * Called when access to the specified network is blocked or unblocked, or the reason for
3716 * access being blocked changes.
Lorenzo Colitti60104f52021-03-18 00:54:57 +09003717 *
3718 * If a NetworkCallback object implements this method,
3719 * {@link #onBlockedStatusChanged(Network, boolean)} will not be called.
3720 *
3721 * <p>Do NOT call {@link #getNetworkCapabilities(Network)} or
3722 * {@link #getLinkProperties(Network)} or other synchronous ConnectivityManager methods in
3723 * this callback as this is prone to race conditions : calling these methods while in a
3724 * callback may return an outdated or even a null object.
3725 *
3726 * @param network The {@link Network} whose blocked status has changed.
3727 * @param blocked The blocked status of this {@link Network}.
3728 * @hide
3729 */
3730 @SystemApi(client = MODULE_LIBRARIES)
3731 public void onBlockedStatusChanged(@NonNull Network network, @BlockedReason int blocked) {
3732 onBlockedStatusChanged(network, blocked != 0);
3733 }
3734
Robert Greenwalt09e5cff2014-06-11 16:05:07 -07003735 private NetworkRequest networkRequest;
Roshan Pius7992afd2020-12-22 15:10:42 -08003736 private final int mFlags;
Robert Greenwaltdb6fac52014-04-18 15:25:25 -07003737 }
3738
Hugo Benichi145e3792017-05-11 13:16:17 +09003739 /**
3740 * Constant error codes used by ConnectivityService to communicate about failures and errors
3741 * across a Binder boundary.
3742 * @hide
3743 */
3744 public interface Errors {
Chalard Jean73d9db72018-06-04 16:52:49 +09003745 int TOO_MANY_REQUESTS = 1;
Hugo Benichi145e3792017-05-11 13:16:17 +09003746 }
3747
3748 /** @hide */
3749 public static class TooManyRequestsException extends RuntimeException {}
3750
3751 private static RuntimeException convertServiceException(ServiceSpecificException e) {
3752 switch (e.errorCode) {
3753 case Errors.TOO_MANY_REQUESTS:
3754 return new TooManyRequestsException();
3755 default:
3756 Log.w(TAG, "Unknown service error code " + e.errorCode);
3757 return new RuntimeException(e);
3758 }
3759 }
3760
Robert Greenwalt2bebbfb2014-03-26 16:47:06 -07003761 /** @hide */
Remi NGUYEN VANa711b3f2021-03-12 15:24:06 +09003762 public static final int CALLBACK_PRECHECK = 1;
Robert Greenwalt41c19a02015-06-24 13:23:42 -07003763 /** @hide */
Remi NGUYEN VANa711b3f2021-03-12 15:24:06 +09003764 public static final int CALLBACK_AVAILABLE = 2;
Robert Greenwalt41c19a02015-06-24 13:23:42 -07003765 /** @hide arg1 = TTL */
Remi NGUYEN VANa711b3f2021-03-12 15:24:06 +09003766 public static final int CALLBACK_LOSING = 3;
Robert Greenwalt41c19a02015-06-24 13:23:42 -07003767 /** @hide */
Remi NGUYEN VANa711b3f2021-03-12 15:24:06 +09003768 public static final int CALLBACK_LOST = 4;
Robert Greenwalt41c19a02015-06-24 13:23:42 -07003769 /** @hide */
Remi NGUYEN VANa711b3f2021-03-12 15:24:06 +09003770 public static final int CALLBACK_UNAVAIL = 5;
Robert Greenwalt41c19a02015-06-24 13:23:42 -07003771 /** @hide */
Remi NGUYEN VANa711b3f2021-03-12 15:24:06 +09003772 public static final int CALLBACK_CAP_CHANGED = 6;
Robert Greenwalt41c19a02015-06-24 13:23:42 -07003773 /** @hide */
Remi NGUYEN VANa711b3f2021-03-12 15:24:06 +09003774 public static final int CALLBACK_IP_CHANGED = 7;
Robert Greenwaltb59a91b2014-05-15 18:07:26 -07003775 /** @hide obj = NetworkCapabilities, arg1 = seq number */
Remi NGUYEN VANa711b3f2021-03-12 15:24:06 +09003776 private static final int EXPIRE_LEGACY_REQUEST = 8;
Robert Greenwalt41c19a02015-06-24 13:23:42 -07003777 /** @hide */
Remi NGUYEN VANa711b3f2021-03-12 15:24:06 +09003778 public static final int CALLBACK_SUSPENDED = 9;
Robert Greenwalt41c19a02015-06-24 13:23:42 -07003779 /** @hide */
Remi NGUYEN VANa711b3f2021-03-12 15:24:06 +09003780 public static final int CALLBACK_RESUMED = 10;
junyulai5ab727b2018-08-07 19:50:45 +08003781 /** @hide */
Remi NGUYEN VANa711b3f2021-03-12 15:24:06 +09003782 public static final int CALLBACK_BLK_CHANGED = 11;
Robert Greenwalt2bebbfb2014-03-26 16:47:06 -07003783
Erik Kline66721822015-11-25 12:49:38 +09003784 /** @hide */
3785 public static String getCallbackName(int whichCallback) {
3786 switch (whichCallback) {
3787 case CALLBACK_PRECHECK: return "CALLBACK_PRECHECK";
3788 case CALLBACK_AVAILABLE: return "CALLBACK_AVAILABLE";
3789 case CALLBACK_LOSING: return "CALLBACK_LOSING";
3790 case CALLBACK_LOST: return "CALLBACK_LOST";
3791 case CALLBACK_UNAVAIL: return "CALLBACK_UNAVAIL";
3792 case CALLBACK_CAP_CHANGED: return "CALLBACK_CAP_CHANGED";
3793 case CALLBACK_IP_CHANGED: return "CALLBACK_IP_CHANGED";
Erik Kline66721822015-11-25 12:49:38 +09003794 case EXPIRE_LEGACY_REQUEST: return "EXPIRE_LEGACY_REQUEST";
3795 case CALLBACK_SUSPENDED: return "CALLBACK_SUSPENDED";
3796 case CALLBACK_RESUMED: return "CALLBACK_RESUMED";
junyulai5ab727b2018-08-07 19:50:45 +08003797 case CALLBACK_BLK_CHANGED: return "CALLBACK_BLK_CHANGED";
Erik Kline66721822015-11-25 12:49:38 +09003798 default:
3799 return Integer.toString(whichCallback);
3800 }
3801 }
3802
Robert Greenwaltb59a91b2014-05-15 18:07:26 -07003803 private class CallbackHandler extends Handler {
Robert Greenwalt2bebbfb2014-03-26 16:47:06 -07003804 private static final String TAG = "ConnectivityManager.CallbackHandler";
Robert Greenwalt9b330772015-09-03 16:41:45 -07003805 private static final boolean DBG = false;
Robert Greenwalt2bebbfb2014-03-26 16:47:06 -07003806
Hugo Benichife3325f2016-07-06 22:53:17 +09003807 CallbackHandler(Looper looper) {
Robert Greenwalt2bebbfb2014-03-26 16:47:06 -07003808 super(looper);
Robert Greenwalt2bebbfb2014-03-26 16:47:06 -07003809 }
3810
Hugo Benichicd5a0e02017-02-02 17:02:36 +09003811 CallbackHandler(Handler handler) {
Remi NGUYEN VANc4dd8c92021-03-15 07:31:54 +00003812 this(Objects.requireNonNull(handler, "Handler cannot be null.").getLooper());
Hugo Benichicd5a0e02017-02-02 17:02:36 +09003813 }
3814
Robert Greenwalt2bebbfb2014-03-26 16:47:06 -07003815 @Override
3816 public void handleMessage(Message message) {
Hugo Benichi3b926992017-05-09 14:36:02 +09003817 if (message.what == EXPIRE_LEGACY_REQUEST) {
3818 expireRequest((NetworkCapabilities) message.obj, message.arg1);
3819 return;
3820 }
3821
3822 final NetworkRequest request = getObject(message, NetworkRequest.class);
3823 final Network network = getObject(message, Network.class);
3824 final NetworkCallback callback;
3825 synchronized (sCallbacks) {
3826 callback = sCallbacks.get(request);
Etan Cohenbc3a4eb2019-05-21 12:06:04 -07003827 if (callback == null) {
3828 Log.w(TAG,
3829 "callback not found for " + getCallbackName(message.what) + " message");
3830 return;
3831 }
Etan Cohene0e2ca92019-04-16 15:07:55 -07003832 if (message.what == CALLBACK_UNAVAIL) {
3833 sCallbacks.remove(request);
3834 callback.networkRequest = ALREADY_UNREGISTERED;
3835 }
Hugo Benichi3b926992017-05-09 14:36:02 +09003836 }
Lorenzo Colitti7e286832016-03-01 22:56:37 +09003837 if (DBG) {
Hugo Benichi94f0bfc2017-03-22 17:07:57 +09003838 Log.d(TAG, getCallbackName(message.what) + " for network " + network);
Lorenzo Colitti7e286832016-03-01 22:56:37 +09003839 }
Hugo Benichi3b926992017-05-09 14:36:02 +09003840
Robert Greenwalt2bebbfb2014-03-26 16:47:06 -07003841 switch (message.what) {
3842 case CALLBACK_PRECHECK: {
Hugo Benichi3b926992017-05-09 14:36:02 +09003843 callback.onPreCheck(network);
Robert Greenwalt2bebbfb2014-03-26 16:47:06 -07003844 break;
3845 }
3846 case CALLBACK_AVAILABLE: {
Chalard Jeanb6a0c6d2018-01-30 22:41:41 +09003847 NetworkCapabilities cap = getObject(message, NetworkCapabilities.class);
3848 LinkProperties lp = getObject(message, LinkProperties.class);
Lorenzo Colitti60104f52021-03-18 00:54:57 +09003849 callback.onAvailable(network, cap, lp, message.arg1);
Robert Greenwalt2bebbfb2014-03-26 16:47:06 -07003850 break;
3851 }
3852 case CALLBACK_LOSING: {
Hugo Benichi3b926992017-05-09 14:36:02 +09003853 callback.onLosing(network, message.arg1);
Robert Greenwalt2bebbfb2014-03-26 16:47:06 -07003854 break;
3855 }
3856 case CALLBACK_LOST: {
Hugo Benichi3b926992017-05-09 14:36:02 +09003857 callback.onLost(network);
Robert Greenwalt2bebbfb2014-03-26 16:47:06 -07003858 break;
3859 }
3860 case CALLBACK_UNAVAIL: {
Hugo Benichi3b926992017-05-09 14:36:02 +09003861 callback.onUnavailable();
Robert Greenwalt2bebbfb2014-03-26 16:47:06 -07003862 break;
3863 }
3864 case CALLBACK_CAP_CHANGED: {
Hugo Benichi3b926992017-05-09 14:36:02 +09003865 NetworkCapabilities cap = getObject(message, NetworkCapabilities.class);
3866 callback.onCapabilitiesChanged(network, cap);
Robert Greenwalt2bebbfb2014-03-26 16:47:06 -07003867 break;
3868 }
3869 case CALLBACK_IP_CHANGED: {
Hugo Benichi3b926992017-05-09 14:36:02 +09003870 LinkProperties lp = getObject(message, LinkProperties.class);
3871 callback.onLinkPropertiesChanged(network, lp);
Robert Greenwalt2bebbfb2014-03-26 16:47:06 -07003872 break;
3873 }
Robert Greenwalt41c19a02015-06-24 13:23:42 -07003874 case CALLBACK_SUSPENDED: {
Hugo Benichi3b926992017-05-09 14:36:02 +09003875 callback.onNetworkSuspended(network);
Robert Greenwalt41c19a02015-06-24 13:23:42 -07003876 break;
3877 }
3878 case CALLBACK_RESUMED: {
Hugo Benichi3b926992017-05-09 14:36:02 +09003879 callback.onNetworkResumed(network);
Robert Greenwaltb59a91b2014-05-15 18:07:26 -07003880 break;
3881 }
junyulai5ab727b2018-08-07 19:50:45 +08003882 case CALLBACK_BLK_CHANGED: {
Lorenzo Colitti60104f52021-03-18 00:54:57 +09003883 callback.onBlockedStatusChanged(network, message.arg1);
junyulai5ab727b2018-08-07 19:50:45 +08003884 }
Robert Greenwalt2bebbfb2014-03-26 16:47:06 -07003885 }
3886 }
3887
Hugo Benichife3325f2016-07-06 22:53:17 +09003888 private <T> T getObject(Message msg, Class<T> c) {
3889 return (T) msg.getData().getParcelable(c.getSimpleName());
Robert Greenwalt2bebbfb2014-03-26 16:47:06 -07003890 }
Robert Greenwalt2bebbfb2014-03-26 16:47:06 -07003891 }
3892
Hugo Benichicd5a0e02017-02-02 17:02:36 +09003893 private CallbackHandler getDefaultHandler() {
Hugo Benichi3a545552016-07-07 10:15:56 +09003894 synchronized (sCallbacks) {
3895 if (sCallbackHandler == null) {
3896 sCallbackHandler = new CallbackHandler(ConnectivityThread.getInstanceLooper());
Robert Greenwalt2bebbfb2014-03-26 16:47:06 -07003897 }
Hugo Benichi3a545552016-07-07 10:15:56 +09003898 return sCallbackHandler;
Robert Greenwalt2bebbfb2014-03-26 16:47:06 -07003899 }
3900 }
3901
Hugo Benichiec180d52017-02-03 14:18:44 +09003902 private static final HashMap<NetworkRequest, NetworkCallback> sCallbacks = new HashMap<>();
3903 private static CallbackHandler sCallbackHandler;
Robert Greenwalt2bebbfb2014-03-26 16:47:06 -07003904
Lorenzo Colittib199b962021-03-12 22:48:07 +09003905 private NetworkRequest sendRequestForNetwork(int asUid, NetworkCapabilities need,
3906 NetworkCallback callback, int timeoutMs, NetworkRequest.Type reqType, int legacyType,
3907 CallbackHandler handler) {
Soi, Yoshinari99efcbd2015-11-12 12:09:02 +09003908 printStackTrace();
Hugo Benichiff4bf602017-05-09 15:19:01 +09003909 checkCallbackNotNull(callback);
Remi NGUYEN VANc4dd8c92021-03-15 07:31:54 +00003910 if (reqType != TRACK_DEFAULT && reqType != TRACK_SYSTEM_DEFAULT && need == null) {
3911 throw new IllegalArgumentException("null NetworkCapabilities");
3912 }
Hugo Benichife3325f2016-07-06 22:53:17 +09003913 final NetworkRequest request;
Roshan Piusd26ae412020-01-16 12:17:17 -08003914 final String callingPackageName = mContext.getOpPackageName();
Robert Greenwalt2bebbfb2014-03-26 16:47:06 -07003915 try {
Hugo Benichife3325f2016-07-06 22:53:17 +09003916 synchronized(sCallbacks) {
Hugo Benichi1c027fe2017-06-17 13:14:12 +09003917 if (callback.networkRequest != null
3918 && callback.networkRequest != ALREADY_UNREGISTERED) {
Hugo Benichi45a49542017-03-06 09:17:06 +09003919 // TODO: throw exception instead and enforce 1:1 mapping of callbacks
3920 // and requests (http://b/20701525).
3921 Log.e(TAG, "NetworkCallback was already registered");
3922 }
Hugo Benichi3a545552016-07-07 10:15:56 +09003923 Messenger messenger = new Messenger(handler);
Hugo Benichife3325f2016-07-06 22:53:17 +09003924 Binder binder = new Binder();
Roshan Pius7992afd2020-12-22 15:10:42 -08003925 final int callbackFlags = callback.mFlags;
junyulaid1a78162021-01-11 16:53:38 +08003926 if (reqType == LISTEN) {
Roshan Piusd26ae412020-01-16 12:17:17 -08003927 request = mService.listenForNetwork(
Roshan Pius7992afd2020-12-22 15:10:42 -08003928 need, messenger, binder, callbackFlags, callingPackageName,
Roshan Piusc97d8062020-12-17 14:53:09 -08003929 getAttributionTag());
Paul Jensenf980a0f2014-06-27 11:05:32 -04003930 } else {
Hugo Benichife3325f2016-07-06 22:53:17 +09003931 request = mService.requestNetwork(
Lorenzo Colittib199b962021-03-12 22:48:07 +09003932 asUid, need, reqType.ordinal(), messenger, timeoutMs, binder,
3933 legacyType, callbackFlags, callingPackageName, getAttributionTag());
Paul Jensenf980a0f2014-06-27 11:05:32 -04003934 }
Hugo Benichife3325f2016-07-06 22:53:17 +09003935 if (request != null) {
Hugo Benichi3a545552016-07-07 10:15:56 +09003936 sCallbacks.put(request, callback);
Robert Greenwalt2bebbfb2014-03-26 16:47:06 -07003937 }
Hugo Benichi3a545552016-07-07 10:15:56 +09003938 callback.networkRequest = request;
Robert Greenwalt2bebbfb2014-03-26 16:47:06 -07003939 }
Jeff Sharkeya6066fd2016-03-01 19:27:23 -07003940 } catch (RemoteException e) {
3941 throw e.rethrowFromSystemServer();
Hugo Benichi145e3792017-05-11 13:16:17 +09003942 } catch (ServiceSpecificException e) {
3943 throw convertServiceException(e);
Jeff Sharkeya6066fd2016-03-01 19:27:23 -07003944 }
Hugo Benichife3325f2016-07-06 22:53:17 +09003945 return request;
Robert Greenwalt2bebbfb2014-03-26 16:47:06 -07003946 }
3947
Lorenzo Colittib199b962021-03-12 22:48:07 +09003948 private NetworkRequest sendRequestForNetwork(NetworkCapabilities need, NetworkCallback callback,
3949 int timeoutMs, NetworkRequest.Type reqType, int legacyType, CallbackHandler handler) {
3950 return sendRequestForNetwork(Process.INVALID_UID, need, callback, timeoutMs, reqType,
3951 legacyType, handler);
3952 }
3953
Robert Greenwalt2bebbfb2014-03-26 16:47:06 -07003954 /**
Erik Klinee0aed632016-03-16 15:31:39 +09003955 * Helper function to request a network with a particular legacy type.
Lorenzo Colitti873f7bb2015-11-25 12:00:52 +09003956 *
markchien0fe11402020-03-18 21:16:15 +08003957 * This API is only for use in internal system code that requests networks with legacy type and
3958 * relies on CONNECTIVITY_ACTION broadcasts instead of NetworkCallbacks. New caller should use
markchien769c5752020-01-14 12:54:40 +08003959 * {@link #requestNetwork(NetworkRequest, NetworkCallback, Handler)} instead.
Lorenzo Colitti873f7bb2015-11-25 12:00:52 +09003960 *
markchien769c5752020-01-14 12:54:40 +08003961 * @param request {@link NetworkRequest} describing this request.
markchien769c5752020-01-14 12:54:40 +08003962 * @param timeoutMs The time in milliseconds to attempt looking for a suitable network
3963 * before {@link NetworkCallback#onUnavailable()} is called. The timeout must
3964 * be a positive value (i.e. >0).
3965 * @param legacyType to specify the network type(#TYPE_*).
3966 * @param handler {@link Handler} to specify the thread upon which the callback will be invoked.
markchien0fe11402020-03-18 21:16:15 +08003967 * @param networkCallback The {@link NetworkCallback} to be utilized for this request. Note
3968 * the callback must not be shared - it uniquely specifies this request.
Lorenzo Colitti873f7bb2015-11-25 12:00:52 +09003969 *
3970 * @hide
3971 */
markchien769c5752020-01-14 12:54:40 +08003972 @SystemApi
markchien0fe11402020-03-18 21:16:15 +08003973 @RequiresPermission(NetworkStack.PERMISSION_MAINLINE_NETWORK_STACK)
Chalard Jean42a9c292019-01-07 19:26:34 +09003974 public void requestNetwork(@NonNull NetworkRequest request,
markchien0fe11402020-03-18 21:16:15 +08003975 int timeoutMs, int legacyType, @NonNull Handler handler,
3976 @NonNull NetworkCallback networkCallback) {
3977 if (legacyType == TYPE_NONE) {
3978 throw new IllegalArgumentException("TYPE_NONE is meaningless legacy type");
3979 }
Hugo Benichicd5a0e02017-02-02 17:02:36 +09003980 CallbackHandler cbHandler = new CallbackHandler(handler);
3981 NetworkCapabilities nc = request.networkCapabilities;
3982 sendRequestForNetwork(nc, networkCallback, timeoutMs, REQUEST, legacyType, cbHandler);
Lorenzo Colitti873f7bb2015-11-25 12:00:52 +09003983 }
3984
3985 /**
Roshan Pius7992afd2020-12-22 15:10:42 -08003986 * Request a network to satisfy a set of {@link NetworkCapabilities}.
Robert Greenwalt2bebbfb2014-03-26 16:47:06 -07003987 *
Chalard Jean7f3a8742019-08-30 16:27:28 +09003988 * <p>This method will attempt to find the best network that matches the passed
3989 * {@link NetworkRequest}, and to bring up one that does if none currently satisfies the
3990 * criteria. The platform will evaluate which network is the best at its own discretion.
3991 * Throughput, latency, cost per byte, policy, user preference and other considerations
3992 * may be factored in the decision of what is considered the best network.
3993 *
3994 * <p>As long as this request is outstanding, the platform will try to maintain the best network
3995 * matching this request, while always attempting to match the request to a better network if
3996 * possible. If a better match is found, the platform will switch this request to the now-best
3997 * network and inform the app of the newly best network by invoking
3998 * {@link NetworkCallback#onAvailable(Network)} on the provided callback. Note that the platform
3999 * will not try to maintain any other network than the best one currently matching the request:
4000 * a network not matching any network request may be disconnected at any time.
4001 *
4002 * <p>For example, an application could use this method to obtain a connected cellular network
4003 * even if the device currently has a data connection over Ethernet. This may cause the cellular
4004 * radio to consume additional power. Or, an application could inform the system that it wants
4005 * a network supporting sending MMSes and have the system let it know about the currently best
4006 * MMS-supporting network through the provided {@link NetworkCallback}.
4007 *
4008 * <p>The status of the request can be followed by listening to the various callbacks described
4009 * in {@link NetworkCallback}. The {@link Network} object passed to the callback methods can be
4010 * used to direct traffic to the network (although accessing some networks may be subject to
4011 * holding specific permissions). Callers will learn about the specific characteristics of the
4012 * network through
4013 * {@link NetworkCallback#onCapabilitiesChanged(Network, NetworkCapabilities)} and
4014 * {@link NetworkCallback#onLinkPropertiesChanged(Network, LinkProperties)}. The methods of the
4015 * provided {@link NetworkCallback} will only be invoked due to changes in the best network
4016 * matching the request at any given time; therefore when a better network matching the request
4017 * becomes available, the {@link NetworkCallback#onAvailable(Network)} method is called
4018 * with the new network after which no further updates are given about the previously-best
4019 * network, unless it becomes the best again at some later time. All callbacks are invoked
4020 * in order on the same thread, which by default is a thread created by the framework running
4021 * in the app.
4022 * {@see #requestNetwork(NetworkRequest, NetworkCallback, Handler)} to change where the
4023 * callbacks are invoked.
4024 *
4025 * <p>This{@link NetworkRequest} will live until released via
4026 * {@link #unregisterNetworkCallback(NetworkCallback)} or the calling application exits, at
4027 * which point the system may let go of the network at any time.
4028 *
4029 * <p>A version of this method which takes a timeout is
4030 * {@link #requestNetwork(NetworkRequest, NetworkCallback, int)}, that an app can use to only
4031 * wait for a limited amount of time for the network to become unavailable.
4032 *
Paul Jensen1fcbf9c2015-06-16 15:11:58 -04004033 * <p>It is presently unsupported to request a network with mutable
4034 * {@link NetworkCapabilities} such as
4035 * {@link NetworkCapabilities#NET_CAPABILITY_VALIDATED} or
4036 * {@link NetworkCapabilities#NET_CAPABILITY_CAPTIVE_PORTAL}
4037 * as these {@code NetworkCapabilities} represent states that a particular
4038 * network may never attain, and whether a network will attain these states
4039 * is unknown prior to bringing up the network so the framework does not
Chalard Jean7f3a8742019-08-30 16:27:28 +09004040 * know how to go about satisfying a request with these capabilities.
Lorenzo Colitti91de63b2015-10-15 16:29:00 +09004041 *
4042 * <p>This method requires the caller to hold either the
4043 * {@link android.Manifest.permission#CHANGE_NETWORK_STATE} permission
4044 * or the ability to modify system settings as determined by
4045 * {@link android.provider.Settings.System#canWrite}.</p>
Robert Greenwalt2bebbfb2014-03-26 16:47:06 -07004046 *
Chalard Jean7f06b342020-05-20 16:11:50 +09004047 * <p>To avoid performance issues due to apps leaking callbacks, the system will limit the
4048 * number of outstanding requests to 100 per app (identified by their UID), shared with
4049 * all variants of this method, of {@link #registerNetworkCallback} as well as
4050 * {@link ConnectivityDiagnosticsManager#registerConnectivityDiagnosticsCallback}.
4051 * Requesting a network with this method will count toward this limit. If this limit is
4052 * exceeded, an exception will be thrown. To avoid hitting this issue and to conserve resources,
4053 * make sure to unregister the callbacks with
4054 * {@link #unregisterNetworkCallback(NetworkCallback)}.
4055 *
Robert Greenwalt09e5cff2014-06-11 16:05:07 -07004056 * @param request {@link NetworkRequest} describing this request.
Hugo Benichicd5a0e02017-02-02 17:02:36 +09004057 * @param networkCallback The {@link NetworkCallback} to be utilized for this request. Note
4058 * the callback must not be shared - it uniquely specifies this request.
4059 * The callback is invoked on the default internal Handler.
Chalard Jean9f1a7632019-05-13 15:13:58 +09004060 * @throws IllegalArgumentException if {@code request} contains invalid network capabilities.
4061 * @throws SecurityException if missing the appropriate permissions.
Chalard Jean7f06b342020-05-20 16:11:50 +09004062 * @throws RuntimeException if the app already has too many callbacks registered.
Robert Greenwalt2bebbfb2014-03-26 16:47:06 -07004063 */
Chalard Jean42a9c292019-01-07 19:26:34 +09004064 public void requestNetwork(@NonNull NetworkRequest request,
4065 @NonNull NetworkCallback networkCallback) {
Hugo Benichicd5a0e02017-02-02 17:02:36 +09004066 requestNetwork(request, networkCallback, getDefaultHandler());
4067 }
4068
4069 /**
Roshan Pius7992afd2020-12-22 15:10:42 -08004070 * Request a network to satisfy a set of {@link NetworkCapabilities}.
Hugo Benichicd5a0e02017-02-02 17:02:36 +09004071 *
Chalard Jean7f3a8742019-08-30 16:27:28 +09004072 * This method behaves identically to {@link #requestNetwork(NetworkRequest, NetworkCallback)}
4073 * but runs all the callbacks on the passed Handler.
Hugo Benichicd5a0e02017-02-02 17:02:36 +09004074 *
Chalard Jean7f3a8742019-08-30 16:27:28 +09004075 * <p>This method has the same permission requirements as
Chalard Jean7f06b342020-05-20 16:11:50 +09004076 * {@link #requestNetwork(NetworkRequest, NetworkCallback)}, is subject to the same limitations,
4077 * and throws the same exceptions in the same conditions.
Hugo Benichicd5a0e02017-02-02 17:02:36 +09004078 *
4079 * @param request {@link NetworkRequest} describing this request.
4080 * @param networkCallback The {@link NetworkCallback} to be utilized for this request. Note
4081 * the callback must not be shared - it uniquely specifies this request.
4082 * @param handler {@link Handler} to specify the thread upon which the callback will be invoked.
Hugo Benichicd5a0e02017-02-02 17:02:36 +09004083 */
Chalard Jean42a9c292019-01-07 19:26:34 +09004084 public void requestNetwork(@NonNull NetworkRequest request,
4085 @NonNull NetworkCallback networkCallback, @NonNull Handler handler) {
Hugo Benichicd5a0e02017-02-02 17:02:36 +09004086 CallbackHandler cbHandler = new CallbackHandler(handler);
markchien0fe11402020-03-18 21:16:15 +08004087 NetworkCapabilities nc = request.networkCapabilities;
4088 sendRequestForNetwork(nc, networkCallback, 0, REQUEST, TYPE_NONE, cbHandler);
Robert Greenwalt2bebbfb2014-03-26 16:47:06 -07004089 }
4090
4091 /**
Roshan Pius7992afd2020-12-22 15:10:42 -08004092 * Request a network to satisfy a set of {@link NetworkCapabilities}, limited
Etan Cohen67e58a02017-03-01 12:47:28 -08004093 * by a timeout.
4094 *
4095 * This function behaves identically to the non-timed-out version
4096 * {@link #requestNetwork(NetworkRequest, NetworkCallback)}, but if a suitable network
4097 * is not found within the given time (in milliseconds) the
4098 * {@link NetworkCallback#onUnavailable()} callback is called. The request can still be
4099 * released normally by calling {@link #unregisterNetworkCallback(NetworkCallback)} but does
4100 * not have to be released if timed-out (it is automatically released). Unregistering a
4101 * request that timed out is not an error.
4102 *
4103 * <p>Do not use this method to poll for the existence of specific networks (e.g. with a small
4104 * timeout) - {@link #registerNetworkCallback(NetworkRequest, NetworkCallback)} is provided
4105 * for that purpose. Calling this method will attempt to bring up the requested network.
4106 *
Chalard Jean7f3a8742019-08-30 16:27:28 +09004107 * <p>This method has the same permission requirements as
Chalard Jean7f06b342020-05-20 16:11:50 +09004108 * {@link #requestNetwork(NetworkRequest, NetworkCallback)}, is subject to the same limitations,
4109 * and throws the same exceptions in the same conditions.
Etan Cohen67e58a02017-03-01 12:47:28 -08004110 *
4111 * @param request {@link NetworkRequest} describing this request.
Lorenzo Colitti38ea8102017-04-28 00:56:30 +09004112 * @param networkCallback The {@link NetworkCallback} to be utilized for this request. Note
4113 * the callback must not be shared - it uniquely specifies this request.
Etan Cohen67e58a02017-03-01 12:47:28 -08004114 * @param timeoutMs The time in milliseconds to attempt looking for a suitable network
4115 * before {@link NetworkCallback#onUnavailable()} is called. The timeout must
4116 * be a positive value (i.e. >0).
Etan Cohen67e58a02017-03-01 12:47:28 -08004117 */
Chalard Jean42a9c292019-01-07 19:26:34 +09004118 public void requestNetwork(@NonNull NetworkRequest request,
4119 @NonNull NetworkCallback networkCallback, int timeoutMs) {
Hugo Benichiff4bf602017-05-09 15:19:01 +09004120 checkTimeout(timeoutMs);
markchien0fe11402020-03-18 21:16:15 +08004121 NetworkCapabilities nc = request.networkCapabilities;
4122 sendRequestForNetwork(nc, networkCallback, timeoutMs, REQUEST, TYPE_NONE,
4123 getDefaultHandler());
Hugo Benichicd5a0e02017-02-02 17:02:36 +09004124 }
4125
Hugo Benichicd5a0e02017-02-02 17:02:36 +09004126 /**
Roshan Pius7992afd2020-12-22 15:10:42 -08004127 * Request a network to satisfy a set of {@link NetworkCapabilities}, limited
Hugo Benichicd5a0e02017-02-02 17:02:36 +09004128 * by a timeout.
4129 *
Chalard Jean7f3a8742019-08-30 16:27:28 +09004130 * This method behaves identically to
4131 * {@link #requestNetwork(NetworkRequest, NetworkCallback, int)} but runs all the callbacks
4132 * on the passed Handler.
Etan Cohen67e58a02017-03-01 12:47:28 -08004133 *
Chalard Jean7f3a8742019-08-30 16:27:28 +09004134 * <p>This method has the same permission requirements as
Chalard Jean7f06b342020-05-20 16:11:50 +09004135 * {@link #requestNetwork(NetworkRequest, NetworkCallback)}, is subject to the same limitations,
4136 * and throws the same exceptions in the same conditions.
Hugo Benichicd5a0e02017-02-02 17:02:36 +09004137 *
4138 * @param request {@link NetworkRequest} describing this request.
Etan Cohen67e58a02017-03-01 12:47:28 -08004139 * @param networkCallback The {@link NetworkCallback} to be utilized for this request. Note
4140 * the callback must not be shared - it uniquely specifies this request.
Hugo Benichicd5a0e02017-02-02 17:02:36 +09004141 * @param handler {@link Handler} to specify the thread upon which the callback will be invoked.
Lorenzo Colitti38ea8102017-04-28 00:56:30 +09004142 * @param timeoutMs The time in milliseconds to attempt looking for a suitable network
4143 * before {@link NetworkCallback#onUnavailable} is called.
Hugo Benichicd5a0e02017-02-02 17:02:36 +09004144 */
Chalard Jean42a9c292019-01-07 19:26:34 +09004145 public void requestNetwork(@NonNull NetworkRequest request,
4146 @NonNull NetworkCallback networkCallback, @NonNull Handler handler, int timeoutMs) {
Hugo Benichiff4bf602017-05-09 15:19:01 +09004147 checkTimeout(timeoutMs);
Hugo Benichicd5a0e02017-02-02 17:02:36 +09004148 CallbackHandler cbHandler = new CallbackHandler(handler);
markchien0fe11402020-03-18 21:16:15 +08004149 NetworkCapabilities nc = request.networkCapabilities;
4150 sendRequestForNetwork(nc, networkCallback, timeoutMs, REQUEST, TYPE_NONE, cbHandler);
Robert Greenwalt2bebbfb2014-03-26 16:47:06 -07004151 }
4152
4153 /**
Robert Greenwaltd0ebdbb2014-05-18 23:07:25 -07004154 * The lookup key for a {@link Network} object included with the intent after
Jeremy Joslin94c30992014-11-05 10:32:09 -08004155 * successfully finding a network for the applications request. Retrieve it with
Robert Greenwaltd0ebdbb2014-05-18 23:07:25 -07004156 * {@link android.content.Intent#getParcelableExtra(String)}.
Jeremy Joslinfe76e0e2015-02-11 16:51:13 -08004157 * <p>
Paul Jensen895c3942015-03-10 10:54:12 -04004158 * Note that if you intend to invoke {@link Network#openConnection(java.net.URL)}
4159 * then you must get a ConnectivityManager instance before doing so.
Robert Greenwaltd0ebdbb2014-05-18 23:07:25 -07004160 */
Erik Klinefb087f12014-11-19 12:12:24 +09004161 public static final String EXTRA_NETWORK = "android.net.extra.NETWORK";
Robert Greenwaltd0ebdbb2014-05-18 23:07:25 -07004162
4163 /**
Robert Greenwalt09e5cff2014-06-11 16:05:07 -07004164 * The lookup key for a {@link NetworkRequest} object included with the intent after
Jeremy Joslin94c30992014-11-05 10:32:09 -08004165 * successfully finding a network for the applications request. Retrieve it with
Robert Greenwaltd0ebdbb2014-05-18 23:07:25 -07004166 * {@link android.content.Intent#getParcelableExtra(String)}.
4167 */
Erik Klinefb087f12014-11-19 12:12:24 +09004168 public static final String EXTRA_NETWORK_REQUEST = "android.net.extra.NETWORK_REQUEST";
Robert Greenwaltd0ebdbb2014-05-18 23:07:25 -07004169
4170
4171 /**
Roshan Pius7992afd2020-12-22 15:10:42 -08004172 * Request a network to satisfy a set of {@link NetworkCapabilities}.
Robert Greenwalt2bebbfb2014-03-26 16:47:06 -07004173 *
Jeremy Joslin94c30992014-11-05 10:32:09 -08004174 * This function behaves identically to the version that takes a NetworkCallback, but instead
Robert Greenwalt09e5cff2014-06-11 16:05:07 -07004175 * of {@link NetworkCallback} a {@link PendingIntent} is used. This means
Robert Greenwaltd0ebdbb2014-05-18 23:07:25 -07004176 * the request may outlive the calling application and get called back when a suitable
4177 * network is found.
Robert Greenwalt2bebbfb2014-03-26 16:47:06 -07004178 * <p>
4179 * The operation is an Intent broadcast that goes to a broadcast receiver that
4180 * you registered with {@link Context#registerReceiver} or through the
4181 * &lt;receiver&gt; tag in an AndroidManifest.xml file
4182 * <p>
4183 * The operation Intent is delivered with two extras, a {@link Network} typed
Erik Klinefb087f12014-11-19 12:12:24 +09004184 * extra called {@link #EXTRA_NETWORK} and a {@link NetworkRequest}
4185 * typed extra called {@link #EXTRA_NETWORK_REQUEST} containing
Robert Greenwalt2bebbfb2014-03-26 16:47:06 -07004186 * the original requests parameters. It is important to create a new,
Robert Greenwalt09e5cff2014-06-11 16:05:07 -07004187 * {@link NetworkCallback} based request before completing the processing of the
Robert Greenwalt2bebbfb2014-03-26 16:47:06 -07004188 * Intent to reserve the network or it will be released shortly after the Intent
4189 * is processed.
4190 * <p>
Paul Jensen9ceed862015-06-17 14:15:39 -04004191 * If there is already a request for this Intent registered (with the equality of
Robert Greenwalt2bebbfb2014-03-26 16:47:06 -07004192 * two Intents defined by {@link Intent#filterEquals}), then it will be removed and
Robert Greenwaltd0ebdbb2014-05-18 23:07:25 -07004193 * replaced by this one, effectively releasing the previous {@link NetworkRequest}.
Robert Greenwalt2bebbfb2014-03-26 16:47:06 -07004194 * <p>
Jeremy Joslin94c30992014-11-05 10:32:09 -08004195 * The request may be released normally by calling
4196 * {@link #releaseNetworkRequest(android.app.PendingIntent)}.
Paul Jensen1fcbf9c2015-06-16 15:11:58 -04004197 * <p>It is presently unsupported to request a network with either
4198 * {@link NetworkCapabilities#NET_CAPABILITY_VALIDATED} or
4199 * {@link NetworkCapabilities#NET_CAPABILITY_CAPTIVE_PORTAL}
4200 * as these {@code NetworkCapabilities} represent states that a particular
4201 * network may never attain, and whether a network will attain these states
4202 * is unknown prior to bringing up the network so the framework does not
Chalard Jean73d9db72018-06-04 16:52:49 +09004203 * know how to go about satisfying a request with these capabilities.
Lorenzo Colitti91de63b2015-10-15 16:29:00 +09004204 *
Chalard Jean7f06b342020-05-20 16:11:50 +09004205 * <p>To avoid performance issues due to apps leaking callbacks, the system will limit the
4206 * number of outstanding requests to 100 per app (identified by their UID), shared with
4207 * all variants of this method, of {@link #registerNetworkCallback} as well as
4208 * {@link ConnectivityDiagnosticsManager#registerConnectivityDiagnosticsCallback}.
4209 * Requesting a network with this method will count toward this limit. If this limit is
4210 * exceeded, an exception will be thrown. To avoid hitting this issue and to conserve resources,
4211 * make sure to unregister the callbacks with {@link #unregisterNetworkCallback(PendingIntent)}
4212 * or {@link #releaseNetworkRequest(PendingIntent)}.
4213 *
Lorenzo Colitti91de63b2015-10-15 16:29:00 +09004214 * <p>This method requires the caller to hold either the
4215 * {@link android.Manifest.permission#CHANGE_NETWORK_STATE} permission
4216 * or the ability to modify system settings as determined by
4217 * {@link android.provider.Settings.System#canWrite}.</p>
4218 *
Robert Greenwalt09e5cff2014-06-11 16:05:07 -07004219 * @param request {@link NetworkRequest} describing this request.
Robert Greenwalt2bebbfb2014-03-26 16:47:06 -07004220 * @param operation Action to perform when the network is available (corresponds
Robert Greenwalt09e5cff2014-06-11 16:05:07 -07004221 * to the {@link NetworkCallback#onAvailable} call. Typically
Jeremy Joslin94c30992014-11-05 10:32:09 -08004222 * comes from {@link PendingIntent#getBroadcast}. Cannot be null.
Chalard Jean9f1a7632019-05-13 15:13:58 +09004223 * @throws IllegalArgumentException if {@code request} contains invalid network capabilities.
4224 * @throws SecurityException if missing the appropriate permissions.
Chalard Jean7f06b342020-05-20 16:11:50 +09004225 * @throws RuntimeException if the app already has too many callbacks registered.
Robert Greenwalt2bebbfb2014-03-26 16:47:06 -07004226 */
Chalard Jean42a9c292019-01-07 19:26:34 +09004227 public void requestNetwork(@NonNull NetworkRequest request,
4228 @NonNull PendingIntent operation) {
Soi, Yoshinari99efcbd2015-11-12 12:09:02 +09004229 printStackTrace();
Hugo Benichiff4bf602017-05-09 15:19:01 +09004230 checkPendingIntentNotNull(operation);
Robert Greenwalt2bebbfb2014-03-26 16:47:06 -07004231 try {
Roshan Piusd26ae412020-01-16 12:17:17 -08004232 mService.pendingRequestForNetwork(
Philip P. Moltmannddbcf5b2020-03-24 15:57:49 -07004233 request.networkCapabilities, operation, mContext.getOpPackageName(),
4234 getAttributionTag());
Jeff Sharkeya6066fd2016-03-01 19:27:23 -07004235 } catch (RemoteException e) {
4236 throw e.rethrowFromSystemServer();
Hugo Benichi145e3792017-05-11 13:16:17 +09004237 } catch (ServiceSpecificException e) {
4238 throw convertServiceException(e);
Jeff Sharkeya6066fd2016-03-01 19:27:23 -07004239 }
Robert Greenwalt2bebbfb2014-03-26 16:47:06 -07004240 }
4241
4242 /**
Jeremy Joslin94c30992014-11-05 10:32:09 -08004243 * Removes a request made via {@link #requestNetwork(NetworkRequest, android.app.PendingIntent)}
4244 * <p>
Lorenzo Colittiad458ad2016-04-13 22:00:02 +09004245 * This method has the same behavior as
4246 * {@link #unregisterNetworkCallback(android.app.PendingIntent)} with respect to
Jeremy Joslin94c30992014-11-05 10:32:09 -08004247 * releasing network resources and disconnecting.
4248 *
4249 * @param operation A PendingIntent equal (as defined by {@link Intent#filterEquals}) to the
4250 * PendingIntent passed to
4251 * {@link #requestNetwork(NetworkRequest, android.app.PendingIntent)} with the
4252 * corresponding NetworkRequest you'd like to remove. Cannot be null.
4253 */
Chalard Jean42a9c292019-01-07 19:26:34 +09004254 public void releaseNetworkRequest(@NonNull PendingIntent operation) {
Soi, Yoshinari99efcbd2015-11-12 12:09:02 +09004255 printStackTrace();
Hugo Benichiff4bf602017-05-09 15:19:01 +09004256 checkPendingIntentNotNull(operation);
Jeremy Joslin94c30992014-11-05 10:32:09 -08004257 try {
4258 mService.releasePendingNetworkRequest(operation);
Jeff Sharkeya6066fd2016-03-01 19:27:23 -07004259 } catch (RemoteException e) {
4260 throw e.rethrowFromSystemServer();
4261 }
Jeremy Joslin94c30992014-11-05 10:32:09 -08004262 }
4263
Hugo Benichiff4bf602017-05-09 15:19:01 +09004264 private static void checkPendingIntentNotNull(PendingIntent intent) {
Remi NGUYEN VANc4dd8c92021-03-15 07:31:54 +00004265 Objects.requireNonNull(intent, "PendingIntent cannot be null.");
Hugo Benichiff4bf602017-05-09 15:19:01 +09004266 }
4267
4268 private static void checkCallbackNotNull(NetworkCallback callback) {
Remi NGUYEN VANc4dd8c92021-03-15 07:31:54 +00004269 Objects.requireNonNull(callback, "null NetworkCallback");
Hugo Benichiff4bf602017-05-09 15:19:01 +09004270 }
4271
4272 private static void checkTimeout(int timeoutMs) {
Remi NGUYEN VANc4dd8c92021-03-15 07:31:54 +00004273 if (timeoutMs <= 0) {
4274 throw new IllegalArgumentException("timeoutMs must be strictly positive.");
4275 }
Jeremy Joslin94c30992014-11-05 10:32:09 -08004276 }
4277
4278 /**
Robert Greenwalt2bebbfb2014-03-26 16:47:06 -07004279 * Registers to receive notifications about all networks which satisfy the given
Robert Greenwalt09e5cff2014-06-11 16:05:07 -07004280 * {@link NetworkRequest}. The callbacks will continue to be called until
Chiachang Wang0d92d1a2019-02-27 17:14:50 +08004281 * either the application exits or {@link #unregisterNetworkCallback(NetworkCallback)} is
4282 * called.
Robert Greenwalt2bebbfb2014-03-26 16:47:06 -07004283 *
Chalard Jean7f06b342020-05-20 16:11:50 +09004284 * <p>To avoid performance issues due to apps leaking callbacks, the system will limit the
4285 * number of outstanding requests to 100 per app (identified by their UID), shared with
4286 * all variants of this method, of {@link #requestNetwork} as well as
4287 * {@link ConnectivityDiagnosticsManager#registerConnectivityDiagnosticsCallback}.
4288 * Requesting a network with this method will count toward this limit. If this limit is
4289 * exceeded, an exception will be thrown. To avoid hitting this issue and to conserve resources,
4290 * make sure to unregister the callbacks with
4291 * {@link #unregisterNetworkCallback(NetworkCallback)}.
4292 *
Robert Greenwalt09e5cff2014-06-11 16:05:07 -07004293 * @param request {@link NetworkRequest} describing this request.
4294 * @param networkCallback The {@link NetworkCallback} that the system will call as suitable
4295 * networks change state.
Hugo Benichicd5a0e02017-02-02 17:02:36 +09004296 * The callback is invoked on the default internal Handler.
Chalard Jean7f06b342020-05-20 16:11:50 +09004297 * @throws RuntimeException if the app already has too many callbacks registered.
Robert Greenwalt2bebbfb2014-03-26 16:47:06 -07004298 */
Jeff Sharkey2ac62992017-04-24 11:18:03 -06004299 @RequiresPermission(android.Manifest.permission.ACCESS_NETWORK_STATE)
Chalard Jean42a9c292019-01-07 19:26:34 +09004300 public void registerNetworkCallback(@NonNull NetworkRequest request,
4301 @NonNull NetworkCallback networkCallback) {
Hugo Benichicd5a0e02017-02-02 17:02:36 +09004302 registerNetworkCallback(request, networkCallback, getDefaultHandler());
4303 }
4304
4305 /**
4306 * Registers to receive notifications about all networks which satisfy the given
4307 * {@link NetworkRequest}. The callbacks will continue to be called until
Chiachang Wang0d92d1a2019-02-27 17:14:50 +08004308 * either the application exits or {@link #unregisterNetworkCallback(NetworkCallback)} is
4309 * called.
Hugo Benichicd5a0e02017-02-02 17:02:36 +09004310 *
Chalard Jean7f06b342020-05-20 16:11:50 +09004311 * <p>To avoid performance issues due to apps leaking callbacks, the system will limit the
4312 * number of outstanding requests to 100 per app (identified by their UID), shared with
4313 * all variants of this method, of {@link #requestNetwork} as well as
4314 * {@link ConnectivityDiagnosticsManager#registerConnectivityDiagnosticsCallback}.
4315 * Requesting a network with this method will count toward this limit. If this limit is
4316 * exceeded, an exception will be thrown. To avoid hitting this issue and to conserve resources,
4317 * make sure to unregister the callbacks with
4318 * {@link #unregisterNetworkCallback(NetworkCallback)}.
4319 *
4320 *
Hugo Benichicd5a0e02017-02-02 17:02:36 +09004321 * @param request {@link NetworkRequest} describing this request.
4322 * @param networkCallback The {@link NetworkCallback} that the system will call as suitable
4323 * networks change state.
4324 * @param handler {@link Handler} to specify the thread upon which the callback will be invoked.
Chalard Jean7f06b342020-05-20 16:11:50 +09004325 * @throws RuntimeException if the app already has too many callbacks registered.
Hugo Benichicd5a0e02017-02-02 17:02:36 +09004326 */
Jeff Sharkey2ac62992017-04-24 11:18:03 -06004327 @RequiresPermission(android.Manifest.permission.ACCESS_NETWORK_STATE)
Chalard Jean42a9c292019-01-07 19:26:34 +09004328 public void registerNetworkCallback(@NonNull NetworkRequest request,
4329 @NonNull NetworkCallback networkCallback, @NonNull Handler handler) {
Hugo Benichicd5a0e02017-02-02 17:02:36 +09004330 CallbackHandler cbHandler = new CallbackHandler(handler);
4331 NetworkCapabilities nc = request.networkCapabilities;
4332 sendRequestForNetwork(nc, networkCallback, 0, LISTEN, TYPE_NONE, cbHandler);
Robert Greenwalt2bebbfb2014-03-26 16:47:06 -07004333 }
4334
4335 /**
Paul Jensen9ceed862015-06-17 14:15:39 -04004336 * Registers a PendingIntent to be sent when a network is available which satisfies the given
4337 * {@link NetworkRequest}.
4338 *
4339 * This function behaves identically to the version that takes a NetworkCallback, but instead
4340 * of {@link NetworkCallback} a {@link PendingIntent} is used. This means
4341 * the request may outlive the calling application and get called back when a suitable
4342 * network is found.
4343 * <p>
4344 * The operation is an Intent broadcast that goes to a broadcast receiver that
4345 * you registered with {@link Context#registerReceiver} or through the
4346 * &lt;receiver&gt; tag in an AndroidManifest.xml file
4347 * <p>
4348 * The operation Intent is delivered with two extras, a {@link Network} typed
4349 * extra called {@link #EXTRA_NETWORK} and a {@link NetworkRequest}
4350 * typed extra called {@link #EXTRA_NETWORK_REQUEST} containing
4351 * the original requests parameters.
4352 * <p>
4353 * If there is already a request for this Intent registered (with the equality of
4354 * two Intents defined by {@link Intent#filterEquals}), then it will be removed and
4355 * replaced by this one, effectively releasing the previous {@link NetworkRequest}.
4356 * <p>
4357 * The request may be released normally by calling
Paul Jensen78f6b802015-06-30 14:29:18 -04004358 * {@link #unregisterNetworkCallback(android.app.PendingIntent)}.
Chalard Jean7f06b342020-05-20 16:11:50 +09004359 *
4360 * <p>To avoid performance issues due to apps leaking callbacks, the system will limit the
4361 * number of outstanding requests to 100 per app (identified by their UID), shared with
4362 * all variants of this method, of {@link #requestNetwork} as well as
4363 * {@link ConnectivityDiagnosticsManager#registerConnectivityDiagnosticsCallback}.
4364 * Requesting a network with this method will count toward this limit. If this limit is
4365 * exceeded, an exception will be thrown. To avoid hitting this issue and to conserve resources,
4366 * make sure to unregister the callbacks with {@link #unregisterNetworkCallback(PendingIntent)}
4367 * or {@link #releaseNetworkRequest(PendingIntent)}.
4368 *
Paul Jensen9ceed862015-06-17 14:15:39 -04004369 * @param request {@link NetworkRequest} describing this request.
4370 * @param operation Action to perform when the network is available (corresponds
4371 * to the {@link NetworkCallback#onAvailable} call. Typically
4372 * comes from {@link PendingIntent#getBroadcast}. Cannot be null.
Chalard Jean7f06b342020-05-20 16:11:50 +09004373 * @throws RuntimeException if the app already has too many callbacks registered.
Paul Jensen9ceed862015-06-17 14:15:39 -04004374 */
Jeff Sharkey2ac62992017-04-24 11:18:03 -06004375 @RequiresPermission(android.Manifest.permission.ACCESS_NETWORK_STATE)
Chalard Jean42a9c292019-01-07 19:26:34 +09004376 public void registerNetworkCallback(@NonNull NetworkRequest request,
4377 @NonNull PendingIntent operation) {
Soi, Yoshinari99efcbd2015-11-12 12:09:02 +09004378 printStackTrace();
Hugo Benichiff4bf602017-05-09 15:19:01 +09004379 checkPendingIntentNotNull(operation);
Paul Jensen9ceed862015-06-17 14:15:39 -04004380 try {
Roshan Piusd26ae412020-01-16 12:17:17 -08004381 mService.pendingListenForNetwork(
Roshan Piusc97d8062020-12-17 14:53:09 -08004382 request.networkCapabilities, operation, mContext.getOpPackageName(),
4383 getAttributionTag());
Jeff Sharkeya6066fd2016-03-01 19:27:23 -07004384 } catch (RemoteException e) {
4385 throw e.rethrowFromSystemServer();
Hugo Benichi145e3792017-05-11 13:16:17 +09004386 } catch (ServiceSpecificException e) {
4387 throw convertServiceException(e);
Jeff Sharkeya6066fd2016-03-01 19:27:23 -07004388 }
Paul Jensen9ceed862015-06-17 14:15:39 -04004389 }
4390
4391 /**
Lorenzo Colitti80b48bc2021-01-29 20:14:04 +09004392 * Registers to receive notifications about changes in the application's default network. This
4393 * may be a physical network or a virtual network, such as a VPN that applies to the
4394 * application. The callbacks will continue to be called until either the application exits or
Lorenzo Colittiad458ad2016-04-13 22:00:02 +09004395 * {@link #unregisterNetworkCallback(NetworkCallback)} is called.
Erik Klinee0aed632016-03-16 15:31:39 +09004396 *
Chalard Jean7f06b342020-05-20 16:11:50 +09004397 * <p>To avoid performance issues due to apps leaking callbacks, the system will limit the
4398 * number of outstanding requests to 100 per app (identified by their UID), shared with
4399 * all variants of this method, of {@link #requestNetwork} as well as
4400 * {@link ConnectivityDiagnosticsManager#registerConnectivityDiagnosticsCallback}.
4401 * Requesting a network with this method will count toward this limit. If this limit is
4402 * exceeded, an exception will be thrown. To avoid hitting this issue and to conserve resources,
4403 * make sure to unregister the callbacks with
4404 * {@link #unregisterNetworkCallback(NetworkCallback)}.
4405 *
Erik Klinee0aed632016-03-16 15:31:39 +09004406 * @param networkCallback The {@link NetworkCallback} that the system will call as the
Lorenzo Colitti80b48bc2021-01-29 20:14:04 +09004407 * application's default network changes.
Hugo Benichicd5a0e02017-02-02 17:02:36 +09004408 * The callback is invoked on the default internal Handler.
Chalard Jean7f06b342020-05-20 16:11:50 +09004409 * @throws RuntimeException if the app already has too many callbacks registered.
Erik Klinee0aed632016-03-16 15:31:39 +09004410 */
Jeff Sharkey2ac62992017-04-24 11:18:03 -06004411 @RequiresPermission(android.Manifest.permission.ACCESS_NETWORK_STATE)
Chalard Jean42a9c292019-01-07 19:26:34 +09004412 public void registerDefaultNetworkCallback(@NonNull NetworkCallback networkCallback) {
Hugo Benichicd5a0e02017-02-02 17:02:36 +09004413 registerDefaultNetworkCallback(networkCallback, getDefaultHandler());
4414 }
4415
4416 /**
Lorenzo Colitti80b48bc2021-01-29 20:14:04 +09004417 * Registers to receive notifications about changes in the application's default network. This
4418 * may be a physical network or a virtual network, such as a VPN that applies to the
4419 * application. The callbacks will continue to be called until either the application exits or
4420 * {@link #unregisterNetworkCallback(NetworkCallback)} is called.
4421 *
4422 * <p>To avoid performance issues due to apps leaking callbacks, the system will limit the
4423 * number of outstanding requests to 100 per app (identified by their UID), shared with
4424 * all variants of this method, of {@link #requestNetwork} as well as
4425 * {@link ConnectivityDiagnosticsManager#registerConnectivityDiagnosticsCallback}.
4426 * Requesting a network with this method will count toward this limit. If this limit is
4427 * exceeded, an exception will be thrown. To avoid hitting this issue and to conserve resources,
4428 * make sure to unregister the callbacks with
4429 * {@link #unregisterNetworkCallback(NetworkCallback)}.
4430 *
4431 * @param networkCallback The {@link NetworkCallback} that the system will call as the
4432 * application's default network changes.
4433 * @param handler {@link Handler} to specify the thread upon which the callback will be invoked.
4434 * @throws RuntimeException if the app already has too many callbacks registered.
4435 */
4436 @RequiresPermission(android.Manifest.permission.ACCESS_NETWORK_STATE)
4437 public void registerDefaultNetworkCallback(@NonNull NetworkCallback networkCallback,
4438 @NonNull Handler handler) {
Chiachang Wang2e98f6c2021-04-20 15:41:24 +08004439 registerDefaultNetworkCallbackForUid(Process.INVALID_UID, networkCallback, handler);
Lorenzo Colittib199b962021-03-12 22:48:07 +09004440 }
4441
4442 /**
4443 * Registers to receive notifications about changes in the default network for the specified
4444 * UID. This may be a physical network or a virtual network, such as a VPN that applies to the
4445 * UID. The callbacks will continue to be called until either the application exits or
4446 * {@link #unregisterNetworkCallback(NetworkCallback)} is called.
4447 *
4448 * <p>To avoid performance issues due to apps leaking callbacks, the system will limit the
4449 * number of outstanding requests to 100 per app (identified by their UID), shared with
4450 * all variants of this method, of {@link #requestNetwork} as well as
4451 * {@link ConnectivityDiagnosticsManager#registerConnectivityDiagnosticsCallback}.
4452 * Requesting a network with this method will count toward this limit. If this limit is
4453 * exceeded, an exception will be thrown. To avoid hitting this issue and to conserve resources,
4454 * make sure to unregister the callbacks with
4455 * {@link #unregisterNetworkCallback(NetworkCallback)}.
4456 *
4457 * @param uid the UID for which to track default network changes.
4458 * @param networkCallback The {@link NetworkCallback} that the system will call as the
4459 * UID's default network changes.
4460 * @param handler {@link Handler} to specify the thread upon which the callback will be invoked.
4461 * @throws RuntimeException if the app already has too many callbacks registered.
4462 * @hide
4463 */
Lorenzo Colitti3949d6e2021-03-22 18:23:21 +09004464 @SystemApi(client = MODULE_LIBRARIES)
Lorenzo Colittib199b962021-03-12 22:48:07 +09004465 @SuppressLint({"ExecutorRegistration", "PairedRegistration"})
4466 @RequiresPermission(anyOf = {
4467 NetworkStack.PERMISSION_MAINLINE_NETWORK_STACK,
4468 android.Manifest.permission.NETWORK_SETTINGS})
Chiachang Wang2e98f6c2021-04-20 15:41:24 +08004469 public void registerDefaultNetworkCallbackForUid(int uid,
Lorenzo Colittib199b962021-03-12 22:48:07 +09004470 @NonNull NetworkCallback networkCallback, @NonNull Handler handler) {
Lorenzo Colitti80b48bc2021-01-29 20:14:04 +09004471 CallbackHandler cbHandler = new CallbackHandler(handler);
Lorenzo Colittib199b962021-03-12 22:48:07 +09004472 sendRequestForNetwork(uid, null /* need */, networkCallback, 0 /* timeoutMs */,
Lorenzo Colitti80b48bc2021-01-29 20:14:04 +09004473 TRACK_DEFAULT, TYPE_NONE, cbHandler);
4474 }
4475
4476 /**
Hugo Benichicd5a0e02017-02-02 17:02:36 +09004477 * Registers to receive notifications about changes in the system default network. The callbacks
4478 * will continue to be called until either the application exits or
4479 * {@link #unregisterNetworkCallback(NetworkCallback)} is called.
Hugo Benichicd5a0e02017-02-02 17:02:36 +09004480 *
Lorenzo Colitti80b48bc2021-01-29 20:14:04 +09004481 * This method should not be used to determine networking state seen by applications, because in
4482 * many cases, most or even all application traffic may not use the default network directly,
4483 * and traffic from different applications may go on different networks by default. As an
4484 * example, if a VPN is connected, traffic from all applications might be sent through the VPN
4485 * and not onto the system default network. Applications or system components desiring to do
4486 * determine network state as seen by applications should use other methods such as
4487 * {@link #registerDefaultNetworkCallback(NetworkCallback, Handler)}.
4488 *
Chalard Jean7f06b342020-05-20 16:11:50 +09004489 * <p>To avoid performance issues due to apps leaking callbacks, the system will limit the
4490 * number of outstanding requests to 100 per app (identified by their UID), shared with
4491 * all variants of this method, of {@link #requestNetwork} as well as
4492 * {@link ConnectivityDiagnosticsManager#registerConnectivityDiagnosticsCallback}.
4493 * Requesting a network with this method will count toward this limit. If this limit is
4494 * exceeded, an exception will be thrown. To avoid hitting this issue and to conserve resources,
4495 * make sure to unregister the callbacks with
4496 * {@link #unregisterNetworkCallback(NetworkCallback)}.
4497 *
Hugo Benichicd5a0e02017-02-02 17:02:36 +09004498 * @param networkCallback The {@link NetworkCallback} that the system will call as the
4499 * system default network changes.
4500 * @param handler {@link Handler} to specify the thread upon which the callback will be invoked.
Chalard Jean7f06b342020-05-20 16:11:50 +09004501 * @throws RuntimeException if the app already has too many callbacks registered.
Lorenzo Colitti80b48bc2021-01-29 20:14:04 +09004502 *
4503 * @hide
Hugo Benichicd5a0e02017-02-02 17:02:36 +09004504 */
Lorenzo Colitti80b48bc2021-01-29 20:14:04 +09004505 @SystemApi(client = MODULE_LIBRARIES)
4506 @SuppressLint({"ExecutorRegistration", "PairedRegistration"})
4507 @RequiresPermission(anyOf = {
4508 NetworkStack.PERMISSION_MAINLINE_NETWORK_STACK,
4509 android.Manifest.permission.NETWORK_SETTINGS})
4510 public void registerSystemDefaultNetworkCallback(@NonNull NetworkCallback networkCallback,
Chalard Jean42a9c292019-01-07 19:26:34 +09004511 @NonNull Handler handler) {
Hugo Benichicd5a0e02017-02-02 17:02:36 +09004512 CallbackHandler cbHandler = new CallbackHandler(handler);
Chalard Jean73d9db72018-06-04 16:52:49 +09004513 sendRequestForNetwork(null /* NetworkCapabilities need */, networkCallback, 0,
Lorenzo Colitti80b48bc2021-01-29 20:14:04 +09004514 TRACK_SYSTEM_DEFAULT, TYPE_NONE, cbHandler);
Erik Klinee0aed632016-03-16 15:31:39 +09004515 }
4516
4517 /**
junyulaibd622262021-03-15 11:48:48 +08004518 * Registers to receive notifications about the best matching network which satisfy the given
4519 * {@link NetworkRequest}. The callbacks will continue to be called until
4520 * either the application exits or {@link #unregisterNetworkCallback(NetworkCallback)} is
4521 * called.
4522 *
4523 * <p>To avoid performance issues due to apps leaking callbacks, the system will limit the
4524 * number of outstanding requests to 100 per app (identified by their UID), shared with
4525 * {@link #registerNetworkCallback} and its variants and {@link #requestNetwork} as well as
4526 * {@link ConnectivityDiagnosticsManager#registerConnectivityDiagnosticsCallback}.
4527 * Requesting a network with this method will count toward this limit. If this limit is
4528 * exceeded, an exception will be thrown. To avoid hitting this issue and to conserve resources,
4529 * make sure to unregister the callbacks with
4530 * {@link #unregisterNetworkCallback(NetworkCallback)}.
4531 *
4532 *
4533 * @param request {@link NetworkRequest} describing this request.
4534 * @param networkCallback The {@link NetworkCallback} that the system will call as suitable
4535 * networks change state.
4536 * @param handler {@link Handler} to specify the thread upon which the callback will be invoked.
4537 * @throws RuntimeException if the app already has too many callbacks registered.
junyulai6b0a6a22021-03-05 15:51:17 +08004538 */
junyulai6b0a6a22021-03-05 15:51:17 +08004539 @SuppressLint("ExecutorRegistration")
4540 public void registerBestMatchingNetworkCallback(@NonNull NetworkRequest request,
4541 @NonNull NetworkCallback networkCallback, @NonNull Handler handler) {
4542 final NetworkCapabilities nc = request.networkCapabilities;
4543 final CallbackHandler cbHandler = new CallbackHandler(handler);
junyulai8cae3c72021-03-12 20:05:08 +08004544 sendRequestForNetwork(nc, networkCallback, 0, LISTEN_FOR_BEST, TYPE_NONE, cbHandler);
junyulai6b0a6a22021-03-05 15:51:17 +08004545 }
4546
4547 /**
fenglu73169332015-04-21 17:12:05 -07004548 * Requests bandwidth update for a given {@link Network} and returns whether the update request
4549 * is accepted by ConnectivityService. Once accepted, ConnectivityService will poll underlying
4550 * network connection for updated bandwidth information. The caller will be notified via
4551 * {@link ConnectivityManager.NetworkCallback} if there is an update. Notice that this
Lorenzo Colittiad458ad2016-04-13 22:00:02 +09004552 * method assumes that the caller has previously called
4553 * {@link #registerNetworkCallback(NetworkRequest, NetworkCallback)} to listen for network
4554 * changes.
fenglu76564332015-03-20 11:29:56 -07004555 *
fenglu41808e82015-04-27 14:28:04 -07004556 * @param network {@link Network} specifying which network you're interested.
fenglu73169332015-04-21 17:12:05 -07004557 * @return {@code true} on success, {@code false} if the {@link Network} is no longer valid.
fenglu76564332015-03-20 11:29:56 -07004558 */
Chalard Jean42a9c292019-01-07 19:26:34 +09004559 public boolean requestBandwidthUpdate(@NonNull Network network) {
fenglu76564332015-03-20 11:29:56 -07004560 try {
fenglu73169332015-04-21 17:12:05 -07004561 return mService.requestBandwidthUpdate(network);
fenglu76564332015-03-20 11:29:56 -07004562 } catch (RemoteException e) {
Jeff Sharkeya6066fd2016-03-01 19:27:23 -07004563 throw e.rethrowFromSystemServer();
fenglu76564332015-03-20 11:29:56 -07004564 }
4565 }
4566
4567 /**
Hugo Benichi45a49542017-03-06 09:17:06 +09004568 * Unregisters a {@code NetworkCallback} and possibly releases networks originating from
Lorenzo Colittiad458ad2016-04-13 22:00:02 +09004569 * {@link #requestNetwork(NetworkRequest, NetworkCallback)} and
4570 * {@link #registerNetworkCallback(NetworkRequest, NetworkCallback)} calls.
4571 * If the given {@code NetworkCallback} had previously been used with
Lorenzo Colittiefd396e2015-04-24 17:03:31 +09004572 * {@code #requestNetwork}, any networks that had been connected to only to satisfy that request
4573 * will be disconnected.
Robert Greenwalt2bebbfb2014-03-26 16:47:06 -07004574 *
Hugo Benichi45a49542017-03-06 09:17:06 +09004575 * Notifications that would have triggered that {@code NetworkCallback} will immediately stop
4576 * triggering it as soon as this call returns.
4577 *
Robert Greenwalt09e5cff2014-06-11 16:05:07 -07004578 * @param networkCallback The {@link NetworkCallback} used when making the request.
Robert Greenwalt2bebbfb2014-03-26 16:47:06 -07004579 */
Chalard Jean42a9c292019-01-07 19:26:34 +09004580 public void unregisterNetworkCallback(@NonNull NetworkCallback networkCallback) {
Soi, Yoshinari99efcbd2015-11-12 12:09:02 +09004581 printStackTrace();
Hugo Benichiff4bf602017-05-09 15:19:01 +09004582 checkCallbackNotNull(networkCallback);
Hugo Benichi45a49542017-03-06 09:17:06 +09004583 final List<NetworkRequest> reqs = new ArrayList<>();
4584 // Find all requests associated to this callback and stop callback triggers immediately.
4585 // Callback is reusable immediately. http://b/20701525, http://b/35921499.
4586 synchronized (sCallbacks) {
Remi NGUYEN VANc4dd8c92021-03-15 07:31:54 +00004587 if (networkCallback.networkRequest == null) {
4588 throw new IllegalArgumentException("NetworkCallback was not registered");
4589 }
Etan Cohene0e2ca92019-04-16 15:07:55 -07004590 if (networkCallback.networkRequest == ALREADY_UNREGISTERED) {
4591 Log.d(TAG, "NetworkCallback was already unregistered");
4592 return;
4593 }
Hugo Benichi45a49542017-03-06 09:17:06 +09004594 for (Map.Entry<NetworkRequest, NetworkCallback> e : sCallbacks.entrySet()) {
4595 if (e.getValue() == networkCallback) {
4596 reqs.add(e.getKey());
4597 }
4598 }
4599 // TODO: throw exception if callback was registered more than once (http://b/20701525).
4600 for (NetworkRequest r : reqs) {
4601 try {
4602 mService.releaseNetworkRequest(r);
4603 } catch (RemoteException e) {
4604 throw e.rethrowFromSystemServer();
4605 }
4606 // Only remove mapping if rpc was successful.
4607 sCallbacks.remove(r);
4608 }
Hugo Benichi1c027fe2017-06-17 13:14:12 +09004609 networkCallback.networkRequest = ALREADY_UNREGISTERED;
Jeff Sharkeya6066fd2016-03-01 19:27:23 -07004610 }
Robert Greenwalt2bebbfb2014-03-26 16:47:06 -07004611 }
Paul Jensene98c6e02014-05-29 10:12:39 -04004612
4613 /**
Paul Jensen78f6b802015-06-30 14:29:18 -04004614 * Unregisters a callback previously registered via
4615 * {@link #registerNetworkCallback(NetworkRequest, android.app.PendingIntent)}.
4616 *
4617 * @param operation A PendingIntent equal (as defined by {@link Intent#filterEquals}) to the
4618 * PendingIntent passed to
4619 * {@link #registerNetworkCallback(NetworkRequest, android.app.PendingIntent)}.
4620 * Cannot be null.
4621 */
Chalard Jean42a9c292019-01-07 19:26:34 +09004622 public void unregisterNetworkCallback(@NonNull PendingIntent operation) {
Paul Jensen78f6b802015-06-30 14:29:18 -04004623 releaseNetworkRequest(operation);
4624 }
4625
4626 /**
Lorenzo Colitti36fbebe2015-04-03 16:38:52 +09004627 * Informs the system whether it should switch to {@code network} regardless of whether it is
4628 * validated or not. If {@code accept} is true, and the network was explicitly selected by the
4629 * user (e.g., by selecting a Wi-Fi network in the Settings app), then the network will become
4630 * the system default network regardless of any other network that's currently connected. If
4631 * {@code always} is true, then the choice is remembered, so that the next time the user
4632 * connects to this network, the system will switch to it.
4633 *
Lorenzo Colitti36fbebe2015-04-03 16:38:52 +09004634 * @param network The network to accept.
4635 * @param accept Whether to accept the network even if unvalidated.
4636 * @param always Whether to remember this choice in the future.
4637 *
4638 * @hide
4639 */
Chiachang Wang0cefc1c2021-03-18 09:44:34 +08004640 @SystemApi(client = MODULE_LIBRARIES)
4641 @RequiresPermission(anyOf = {
4642 android.Manifest.permission.NETWORK_SETTINGS,
4643 android.Manifest.permission.NETWORK_SETUP_WIZARD,
4644 android.Manifest.permission.NETWORK_STACK,
4645 NetworkStack.PERMISSION_MAINLINE_NETWORK_STACK})
4646 public void setAcceptUnvalidated(@NonNull Network network, boolean accept, boolean always) {
Lorenzo Colitti36fbebe2015-04-03 16:38:52 +09004647 try {
4648 mService.setAcceptUnvalidated(network, accept, always);
Jeff Sharkeya6066fd2016-03-01 19:27:23 -07004649 } catch (RemoteException e) {
4650 throw e.rethrowFromSystemServer();
4651 }
Lorenzo Colitti36fbebe2015-04-03 16:38:52 +09004652 }
4653
4654 /**
lucaslin25a50472019-03-12 13:08:03 +08004655 * Informs the system whether it should consider the network as validated even if it only has
4656 * partial connectivity. If {@code accept} is true, then the network will be considered as
4657 * validated even if connectivity is only partial. If {@code always} is true, then the choice
4658 * is remembered, so that the next time the user connects to this network, the system will
4659 * switch to it.
4660 *
4661 * @param network The network to accept.
4662 * @param accept Whether to consider the network as validated even if it has partial
4663 * connectivity.
4664 * @param always Whether to remember this choice in the future.
4665 *
4666 * @hide
4667 */
Chiachang Wang0cefc1c2021-03-18 09:44:34 +08004668 @SystemApi(client = MODULE_LIBRARIES)
4669 @RequiresPermission(anyOf = {
4670 android.Manifest.permission.NETWORK_SETTINGS,
4671 android.Manifest.permission.NETWORK_SETUP_WIZARD,
4672 android.Manifest.permission.NETWORK_STACK,
4673 NetworkStack.PERMISSION_MAINLINE_NETWORK_STACK})
4674 public void setAcceptPartialConnectivity(@NonNull Network network, boolean accept,
4675 boolean always) {
lucaslin25a50472019-03-12 13:08:03 +08004676 try {
4677 mService.setAcceptPartialConnectivity(network, accept, always);
4678 } catch (RemoteException e) {
4679 throw e.rethrowFromSystemServer();
4680 }
4681 }
4682
4683 /**
Lorenzo Colittie98557f2016-09-19 01:00:19 +09004684 * Informs the system to penalize {@code network}'s score when it becomes unvalidated. This is
4685 * only meaningful if the system is configured not to penalize such networks, e.g., if the
4686 * {@code config_networkAvoidBadWifi} configuration variable is set to 0 and the {@code
4687 * NETWORK_AVOID_BAD_WIFI setting is unset}.
4688 *
Lorenzo Colittie98557f2016-09-19 01:00:19 +09004689 * @param network The network to accept.
4690 *
4691 * @hide
4692 */
Chiachang Wang0cefc1c2021-03-18 09:44:34 +08004693 @SystemApi(client = MODULE_LIBRARIES)
4694 @RequiresPermission(anyOf = {
4695 android.Manifest.permission.NETWORK_SETTINGS,
4696 android.Manifest.permission.NETWORK_SETUP_WIZARD,
4697 android.Manifest.permission.NETWORK_STACK,
4698 NetworkStack.PERMISSION_MAINLINE_NETWORK_STACK})
4699 public void setAvoidUnvalidated(@NonNull Network network) {
Lorenzo Colittie98557f2016-09-19 01:00:19 +09004700 try {
4701 mService.setAvoidUnvalidated(network);
4702 } catch (RemoteException e) {
4703 throw e.rethrowFromSystemServer();
4704 }
4705 }
4706
4707 /**
Lorenzo Colitti755e7962017-04-27 14:30:21 +09004708 * Requests that the system open the captive portal app on the specified network.
4709 *
Remi NGUYEN VANbffe51d2021-03-16 18:06:06 +09004710 * <p>This is to be used on networks where a captive portal was detected, as per
4711 * {@link NetworkCapabilities#NET_CAPABILITY_CAPTIVE_PORTAL}.
4712 *
Lorenzo Colitti755e7962017-04-27 14:30:21 +09004713 * @param network The network to log into.
4714 *
4715 * @hide
4716 */
Remi NGUYEN VANbffe51d2021-03-16 18:06:06 +09004717 @SystemApi(client = SystemApi.Client.MODULE_LIBRARIES)
4718 @RequiresPermission(anyOf = {
4719 android.Manifest.permission.NETWORK_SETTINGS,
4720 android.Manifest.permission.NETWORK_STACK,
4721 NetworkStack.PERMISSION_MAINLINE_NETWORK_STACK
4722 })
4723 public void startCaptivePortalApp(@NonNull Network network) {
Lorenzo Colitti755e7962017-04-27 14:30:21 +09004724 try {
4725 mService.startCaptivePortalApp(network);
4726 } catch (RemoteException e) {
4727 throw e.rethrowFromSystemServer();
4728 }
4729 }
4730
4731 /**
Remi NGUYEN VANf363f5f2019-02-04 11:32:20 +09004732 * Requests that the system open the captive portal app with the specified extras.
4733 *
4734 * <p>This endpoint is exclusively for use by the NetworkStack and is protected by the
4735 * corresponding permission.
Remi NGUYEN VAN89fbdc32019-02-13 20:58:59 +09004736 * @param network Network on which the captive portal was detected.
Remi NGUYEN VANf363f5f2019-02-04 11:32:20 +09004737 * @param appExtras Extras to include in the app start intent.
4738 * @hide
4739 */
4740 @SystemApi
Remi NGUYEN VANf363f5f2019-02-04 11:32:20 +09004741 @RequiresPermission(NetworkStack.PERMISSION_MAINLINE_NETWORK_STACK)
paulhu9bb04802019-03-08 16:35:20 +08004742 public void startCaptivePortalApp(@NonNull Network network, @NonNull Bundle appExtras) {
Remi NGUYEN VANf363f5f2019-02-04 11:32:20 +09004743 try {
Remi NGUYEN VAN89fbdc32019-02-13 20:58:59 +09004744 mService.startCaptivePortalAppInternal(network, appExtras);
Remi NGUYEN VANf363f5f2019-02-04 11:32:20 +09004745 } catch (RemoteException e) {
4746 throw e.rethrowFromSystemServer();
4747 }
4748 }
4749
4750 /**
Remi NGUYEN VAN3436b6f2019-01-20 20:35:06 +09004751 * Determine whether the device is configured to avoid bad wifi.
4752 * @hide
4753 */
4754 @SystemApi
Remi NGUYEN VAN338eb2a2019-03-22 11:14:13 +09004755 @RequiresPermission(anyOf = {
4756 NetworkStack.PERMISSION_MAINLINE_NETWORK_STACK,
4757 android.Manifest.permission.NETWORK_STACK})
4758 public boolean shouldAvoidBadWifi() {
Remi NGUYEN VAN3436b6f2019-01-20 20:35:06 +09004759 try {
Remi NGUYEN VAN338eb2a2019-03-22 11:14:13 +09004760 return mService.shouldAvoidBadWifi();
Remi NGUYEN VAN3436b6f2019-01-20 20:35:06 +09004761 } catch (RemoteException e) {
4762 throw e.rethrowFromSystemServer();
4763 }
4764 }
4765
4766 /**
Lorenzo Colitti231efb02017-01-24 18:08:41 +09004767 * It is acceptable to briefly use multipath data to provide seamless connectivity for
4768 * time-sensitive user-facing operations when the system default network is temporarily
Lorenzo Colitti38ea8102017-04-28 00:56:30 +09004769 * unresponsive. The amount of data should be limited (less than one megabyte for every call to
4770 * this method), and the operation should be infrequent to ensure that data usage is limited.
Lorenzo Colitti231efb02017-01-24 18:08:41 +09004771 *
4772 * An example of such an operation might be a time-sensitive foreground activity, such as a
4773 * voice command, that the user is performing while walking out of range of a Wi-Fi network.
4774 */
4775 public static final int MULTIPATH_PREFERENCE_HANDOVER = 1 << 0;
4776
4777 /**
4778 * It is acceptable to use small amounts of multipath data on an ongoing basis to provide
4779 * a backup channel for traffic that is primarily going over another network.
4780 *
4781 * An example might be maintaining backup connections to peers or servers for the purpose of
4782 * fast fallback if the default network is temporarily unresponsive or disconnects. The traffic
4783 * on backup paths should be negligible compared to the traffic on the main path.
4784 */
4785 public static final int MULTIPATH_PREFERENCE_RELIABILITY = 1 << 1;
4786
4787 /**
4788 * It is acceptable to use metered data to improve network latency and performance.
4789 */
4790 public static final int MULTIPATH_PREFERENCE_PERFORMANCE = 1 << 2;
4791
4792 /**
4793 * Return value to use for unmetered networks. On such networks we currently set all the flags
4794 * to true.
4795 * @hide
4796 */
4797 public static final int MULTIPATH_PREFERENCE_UNMETERED =
4798 MULTIPATH_PREFERENCE_HANDOVER |
4799 MULTIPATH_PREFERENCE_RELIABILITY |
4800 MULTIPATH_PREFERENCE_PERFORMANCE;
4801
4802 /** @hide */
4803 @Retention(RetentionPolicy.SOURCE)
4804 @IntDef(flag = true, value = {
4805 MULTIPATH_PREFERENCE_HANDOVER,
4806 MULTIPATH_PREFERENCE_RELIABILITY,
4807 MULTIPATH_PREFERENCE_PERFORMANCE,
4808 })
4809 public @interface MultipathPreference {
4810 }
4811
4812 /**
4813 * Provides a hint to the calling application on whether it is desirable to use the
4814 * multinetwork APIs (e.g., {@link Network#openConnection}, {@link Network#bindSocket}, etc.)
4815 * for multipath data transfer on this network when it is not the system default network.
4816 * Applications desiring to use multipath network protocols should call this method before
4817 * each such operation.
Lorenzo Colitti231efb02017-01-24 18:08:41 +09004818 *
4819 * @param network The network on which the application desires to use multipath data.
4820 * If {@code null}, this method will return the a preference that will generally
4821 * apply to metered networks.
4822 * @return a bitwise OR of zero or more of the {@code MULTIPATH_PREFERENCE_*} constants.
4823 */
Jeff Sharkey2ac62992017-04-24 11:18:03 -06004824 @RequiresPermission(android.Manifest.permission.ACCESS_NETWORK_STATE)
Chalard Jean42a9c292019-01-07 19:26:34 +09004825 public @MultipathPreference int getMultipathPreference(@Nullable Network network) {
Lorenzo Colitti231efb02017-01-24 18:08:41 +09004826 try {
4827 return mService.getMultipathPreference(network);
4828 } catch (RemoteException e) {
4829 throw e.rethrowFromSystemServer();
4830 }
4831 }
4832
4833 /**
Stuart Scottd3bb5082015-03-30 13:17:11 -07004834 * Resets all connectivity manager settings back to factory defaults.
4835 * @hide
4836 */
Chiachang Wang0cefc1c2021-03-18 09:44:34 +08004837 @SystemApi(client = MODULE_LIBRARIES)
4838 @RequiresPermission(anyOf = {
4839 android.Manifest.permission.NETWORK_SETTINGS,
4840 NetworkStack.PERMISSION_MAINLINE_NETWORK_STACK})
Stuart Scottd3bb5082015-03-30 13:17:11 -07004841 public void factoryReset() {
Stuart Scottd3bb5082015-03-30 13:17:11 -07004842 try {
Stuart Scottca888bf2015-04-02 18:00:02 -07004843 mService.factoryReset();
Automerger Merge Worker9b0c0c62020-03-06 00:38:43 +00004844 mTetheringManager.stopAllTethering();
Stuart Scottd3bb5082015-03-30 13:17:11 -07004845 } catch (RemoteException e) {
Jeff Sharkeya6066fd2016-03-01 19:27:23 -07004846 throw e.rethrowFromSystemServer();
Stuart Scottd3bb5082015-03-30 13:17:11 -07004847 }
4848 }
4849
4850 /**
Paul Jensene98c6e02014-05-29 10:12:39 -04004851 * Binds the current process to {@code network}. All Sockets created in the future
4852 * (and not explicitly bound via a bound SocketFactory from
4853 * {@link Network#getSocketFactory() Network.getSocketFactory()}) will be bound to
4854 * {@code network}. All host name resolutions will be limited to {@code network} as well.
4855 * Note that if {@code network} ever disconnects, all Sockets created in this way will cease to
4856 * work and all host name resolutions will fail. This is by design so an application doesn't
4857 * accidentally use Sockets it thinks are still bound to a particular {@link Network}.
4858 * To clear binding pass {@code null} for {@code network}. Using individually bound
4859 * Sockets created by Network.getSocketFactory().createSocket() and
4860 * performing network-specific host name resolutions via
4861 * {@link Network#getAllByName Network.getAllByName} is preferred to calling
Paul Jensen895c3942015-03-10 10:54:12 -04004862 * {@code bindProcessToNetwork}.
Paul Jensene98c6e02014-05-29 10:12:39 -04004863 *
4864 * @param network The {@link Network} to bind the current process to, or {@code null} to clear
4865 * the current binding.
4866 * @return {@code true} on success, {@code false} if the {@link Network} is no longer valid.
4867 */
Chalard Jean42a9c292019-01-07 19:26:34 +09004868 public boolean bindProcessToNetwork(@Nullable Network network) {
Chalard Jean73d9db72018-06-04 16:52:49 +09004869 // Forcing callers to call through non-static function ensures ConnectivityManager
Paul Jensen895c3942015-03-10 10:54:12 -04004870 // instantiated.
4871 return setProcessDefaultNetwork(network);
4872 }
4873
4874 /**
4875 * Binds the current process to {@code network}. All Sockets created in the future
4876 * (and not explicitly bound via a bound SocketFactory from
4877 * {@link Network#getSocketFactory() Network.getSocketFactory()}) will be bound to
4878 * {@code network}. All host name resolutions will be limited to {@code network} as well.
4879 * Note that if {@code network} ever disconnects, all Sockets created in this way will cease to
4880 * work and all host name resolutions will fail. This is by design so an application doesn't
4881 * accidentally use Sockets it thinks are still bound to a particular {@link Network}.
4882 * To clear binding pass {@code null} for {@code network}. Using individually bound
4883 * Sockets created by Network.getSocketFactory().createSocket() and
4884 * performing network-specific host name resolutions via
4885 * {@link Network#getAllByName Network.getAllByName} is preferred to calling
4886 * {@code setProcessDefaultNetwork}.
4887 *
4888 * @param network The {@link Network} to bind the current process to, or {@code null} to clear
4889 * the current binding.
4890 * @return {@code true} on success, {@code false} if the {@link Network} is no longer valid.
4891 * @deprecated This function can throw {@link IllegalStateException}. Use
4892 * {@link #bindProcessToNetwork} instead. {@code bindProcessToNetwork}
4893 * is a direct replacement.
4894 */
Aurimas Liutikasa6bb0232016-05-24 15:22:55 -07004895 @Deprecated
Chalard Jean42a9c292019-01-07 19:26:34 +09004896 public static boolean setProcessDefaultNetwork(@Nullable Network network) {
Paul Jensen99c36662014-08-27 12:38:45 -04004897 int netId = (network == null) ? NETID_UNSET : network.netId;
Lorenzo Colitti3fadff92019-01-31 13:08:24 +09004898 boolean isSameNetId = (netId == NetworkUtils.getBoundNetworkForProcess());
4899
Lorenzo Colitti580483e2019-01-30 23:04:54 +09004900 if (netId != NETID_UNSET) {
4901 netId = network.getNetIdForResolv();
Paul Jensen99c36662014-08-27 12:38:45 -04004902 }
Lorenzo Colitti3fadff92019-01-31 13:08:24 +09004903
4904 if (!NetworkUtils.bindProcessToNetwork(netId)) {
4905 return false;
4906 }
4907
4908 if (!isSameNetId) {
Paul Jensen12131352014-12-10 15:12:18 -05004909 // Set HTTP proxy system properties to match network.
4910 // TODO: Deprecate this static method and replace it with a non-static version.
Lorenzo Colitti40898252015-04-22 11:52:48 +09004911 try {
Remi NGUYEN VANa1860ff2021-02-03 10:18:20 +09004912 Proxy.setHttpProxyConfiguration(getInstance().getDefaultProxy());
Lorenzo Colitti40898252015-04-22 11:52:48 +09004913 } catch (SecurityException e) {
4914 // The process doesn't have ACCESS_NETWORK_STATE, so we can't fetch the proxy.
4915 Log.e(TAG, "Can't set proxy properties", e);
4916 }
Paul Jensen99c36662014-08-27 12:38:45 -04004917 // Must flush DNS cache as new network may have different DNS resolutions.
Remi NGUYEN VAN33e3abb2021-03-18 23:27:19 +09004918 InetAddressCompat.clearDnsCache();
Paul Jensen99c36662014-08-27 12:38:45 -04004919 // Must flush socket pool as idle sockets will be bound to previous network and may
4920 // cause subsequent fetches to be performed on old network.
4921 NetworkEventDispatcher.getInstance().onNetworkConfigurationChanged();
Paul Jensen99c36662014-08-27 12:38:45 -04004922 }
Lorenzo Colitti3fadff92019-01-31 13:08:24 +09004923
4924 return true;
Paul Jensene98c6e02014-05-29 10:12:39 -04004925 }
4926
4927 /**
4928 * Returns the {@link Network} currently bound to this process via
Paul Jensen895c3942015-03-10 10:54:12 -04004929 * {@link #bindProcessToNetwork}, or {@code null} if no {@link Network} is explicitly bound.
Paul Jensene98c6e02014-05-29 10:12:39 -04004930 *
4931 * @return {@code Network} to which this process is bound, or {@code null}.
4932 */
Chalard Jean42a9c292019-01-07 19:26:34 +09004933 @Nullable
Paul Jensen895c3942015-03-10 10:54:12 -04004934 public Network getBoundNetworkForProcess() {
4935 // Forcing callers to call thru non-static function ensures ConnectivityManager
4936 // instantiated.
4937 return getProcessDefaultNetwork();
4938 }
4939
4940 /**
4941 * Returns the {@link Network} currently bound to this process via
4942 * {@link #bindProcessToNetwork}, or {@code null} if no {@link Network} is explicitly bound.
4943 *
4944 * @return {@code Network} to which this process is bound, or {@code null}.
4945 * @deprecated Using this function can lead to other functions throwing
4946 * {@link IllegalStateException}. Use {@link #getBoundNetworkForProcess} instead.
4947 * {@code getBoundNetworkForProcess} is a direct replacement.
4948 */
Aurimas Liutikasa6bb0232016-05-24 15:22:55 -07004949 @Deprecated
Chalard Jean42a9c292019-01-07 19:26:34 +09004950 @Nullable
Paul Jensene98c6e02014-05-29 10:12:39 -04004951 public static Network getProcessDefaultNetwork() {
Paul Jensen895c3942015-03-10 10:54:12 -04004952 int netId = NetworkUtils.getBoundNetworkForProcess();
Paul Jensen8b6260f2014-07-11 08:17:29 -04004953 if (netId == NETID_UNSET) return null;
Paul Jensene98c6e02014-05-29 10:12:39 -04004954 return new Network(netId);
4955 }
4956
Lorenzo Colitti9b026fa2015-07-29 11:41:21 +09004957 private void unsupportedStartingFrom(int version) {
4958 if (Process.myUid() == Process.SYSTEM_UID) {
Lorenzo Colitti62f752c2018-09-28 11:31:55 +09004959 // The getApplicationInfo() call we make below is not supported in system context. Let
4960 // the call through here, and rely on the fact that ConnectivityService will refuse to
4961 // allow the system to use these APIs anyway.
Lorenzo Colitti9b026fa2015-07-29 11:41:21 +09004962 return;
4963 }
4964
4965 if (mContext.getApplicationInfo().targetSdkVersion >= version) {
4966 throw new UnsupportedOperationException(
4967 "This method is not supported in target SDK version " + version + " and above");
4968 }
4969 }
4970
4971 // Checks whether the calling app can use the legacy routing API (startUsingNetworkFeature,
4972 // stopUsingNetworkFeature, requestRouteToHost), and if not throw UnsupportedOperationException.
Lifu Tangae4adaa2016-01-07 23:20:38 -08004973 // TODO: convert the existing system users (Tethering, GnssLocationProvider) to the new APIs and
Lorenzo Colitti9b026fa2015-07-29 11:41:21 +09004974 // remove these exemptions. Note that this check is not secure, and apps can still access these
4975 // functions by accessing ConnectivityService directly. However, it should be clear that doing
4976 // so is unsupported and may break in the future. http://b/22728205
4977 private void checkLegacyRoutingApiAccess() {
Dianne Hackbornc16abda2015-07-31 10:35:34 -07004978 unsupportedStartingFrom(VERSION_CODES.M);
Lorenzo Colitti9b026fa2015-07-29 11:41:21 +09004979 }
4980
Paul Jensene98c6e02014-05-29 10:12:39 -04004981 /**
4982 * Binds host resolutions performed by this process to {@code network}.
Paul Jensen895c3942015-03-10 10:54:12 -04004983 * {@link #bindProcessToNetwork} takes precedence over this setting.
Paul Jensene98c6e02014-05-29 10:12:39 -04004984 *
4985 * @param network The {@link Network} to bind host resolutions from the current process to, or
4986 * {@code null} to clear the current binding.
4987 * @return {@code true} on success, {@code false} if the {@link Network} is no longer valid.
4988 * @hide
4989 * @deprecated This is strictly for legacy usage to support {@link #startUsingNetworkFeature}.
4990 */
Aurimas Liutikasa6bb0232016-05-24 15:22:55 -07004991 @Deprecated
Mathew Inwoodd078d3d2020-10-27 11:47:29 +00004992 @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.R, trackingBug = 170729553)
Paul Jensene98c6e02014-05-29 10:12:39 -04004993 public static boolean setProcessDefaultNetworkForHostResolution(Network network) {
Paul Jensen8b6260f2014-07-11 08:17:29 -04004994 return NetworkUtils.bindProcessToNetworkForHostResolution(
Erik Klinef2420792018-04-27 22:48:33 +09004995 (network == null) ? NETID_UNSET : network.getNetIdForResolv());
Paul Jensene98c6e02014-05-29 10:12:39 -04004996 }
Felipe Lemed16384b2016-01-22 09:44:57 -08004997
4998 /**
4999 * Device is not restricting metered network activity while application is running on
5000 * background.
5001 */
5002 public static final int RESTRICT_BACKGROUND_STATUS_DISABLED = 1;
5003
5004 /**
5005 * Device is restricting metered network activity while application is running on background,
5006 * but application is allowed to bypass it.
5007 * <p>
5008 * In this state, application should take action to mitigate metered network access.
5009 * For example, a music streaming application should switch to a low-bandwidth bitrate.
5010 */
5011 public static final int RESTRICT_BACKGROUND_STATUS_WHITELISTED = 2;
5012
5013 /**
5014 * Device is restricting metered network activity while application is running on background.
Felipe Leme6a5b7692016-01-27 14:46:39 -08005015 * <p>
Felipe Lemed16384b2016-01-22 09:44:57 -08005016 * In this state, application should not try to use the network while running on background,
5017 * because it would be denied.
5018 */
5019 public static final int RESTRICT_BACKGROUND_STATUS_ENABLED = 3;
5020
Felipe Leme6a5b7692016-01-27 14:46:39 -08005021 /**
5022 * A change in the background metered network activity restriction has occurred.
5023 * <p>
5024 * Applications should call {@link #getRestrictBackgroundStatus()} to check if the restriction
5025 * applies to them.
5026 * <p>
5027 * This is only sent to registered receivers, not manifest receivers.
5028 */
5029 @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION)
5030 public static final String ACTION_RESTRICT_BACKGROUND_CHANGED =
5031 "android.net.conn.RESTRICT_BACKGROUND_CHANGED";
5032
Felipe Leme1b42ef92016-01-25 11:48:04 -08005033 /** @hide */
5034 @Retention(RetentionPolicy.SOURCE)
Felipe Lemed16384b2016-01-22 09:44:57 -08005035 @IntDef(flag = false, value = {
5036 RESTRICT_BACKGROUND_STATUS_DISABLED,
5037 RESTRICT_BACKGROUND_STATUS_WHITELISTED,
5038 RESTRICT_BACKGROUND_STATUS_ENABLED,
5039 })
Felipe Lemed16384b2016-01-22 09:44:57 -08005040 public @interface RestrictBackgroundStatus {
5041 }
5042
Felipe Lemed16384b2016-01-22 09:44:57 -08005043 /**
5044 * Determines if the calling application is subject to metered network restrictions while
5045 * running on background.
Felipe Leme6bd46fc2016-05-16 13:57:19 -07005046 *
5047 * @return {@link #RESTRICT_BACKGROUND_STATUS_DISABLED},
5048 * {@link #RESTRICT_BACKGROUND_STATUS_ENABLED},
5049 * or {@link #RESTRICT_BACKGROUND_STATUS_WHITELISTED}
Felipe Lemed16384b2016-01-22 09:44:57 -08005050 */
5051 public @RestrictBackgroundStatus int getRestrictBackgroundStatus() {
5052 try {
Remi NGUYEN VAN92f20602021-03-18 14:23:12 +09005053 return mService.getRestrictBackgroundStatusByCaller();
Felipe Lemed16384b2016-01-22 09:44:57 -08005054 } catch (RemoteException e) {
Jeff Sharkeya6066fd2016-03-01 19:27:23 -07005055 throw e.rethrowFromSystemServer();
Felipe Lemed16384b2016-01-22 09:44:57 -08005056 }
5057 }
Ricky Waid53cf002018-01-23 04:09:45 +00005058
5059 /**
5060 * The network watchlist is a list of domains and IP addresses that are associated with
Ricky Wai573dcab2018-03-20 14:20:54 +00005061 * potentially harmful apps. This method returns the SHA-256 of the watchlist config file
5062 * currently used by the system for validation purposes.
Ricky Waid53cf002018-01-23 04:09:45 +00005063 *
5064 * @return Hash of network watchlist config file. Null if config does not exist.
5065 */
Chalard Jean42a9c292019-01-07 19:26:34 +09005066 @Nullable
Ricky Waid53cf002018-01-23 04:09:45 +00005067 public byte[] getNetworkWatchlistConfigHash() {
5068 try {
5069 return mService.getNetworkWatchlistConfigHash();
5070 } catch (RemoteException e) {
5071 Log.e(TAG, "Unable to get watchlist config hash");
5072 throw e.rethrowFromSystemServer();
5073 }
5074 }
Jeff Vander Stoepe7205d62018-07-23 10:57:53 -07005075
5076 /**
5077 * Returns the {@code uid} of the owner of a network connection.
5078 *
Benedict Wong8974a1c2020-01-20 22:14:59 -08005079 * @param protocol The protocol of the connection. Only {@code IPPROTO_TCP} and {@code
5080 * IPPROTO_UDP} currently supported.
Jeff Vander Stoepe7205d62018-07-23 10:57:53 -07005081 * @param local The local {@link InetSocketAddress} of a connection.
5082 * @param remote The remote {@link InetSocketAddress} of a connection.
Jeff Vander Stoepe7205d62018-07-23 10:57:53 -07005083 * @return {@code uid} if the connection is found and the app has permission to observe it
Benedict Wong8974a1c2020-01-20 22:14:59 -08005084 * (e.g., if it is associated with the calling VPN app's VpnService tunnel) or {@link
5085 * android.os.Process#INVALID_UID} if the connection is not found.
5086 * @throws {@link SecurityException} if the caller is not the active VpnService for the current
5087 * user.
5088 * @throws {@link IllegalArgumentException} if an unsupported protocol is requested.
Jeff Vander Stoepe7205d62018-07-23 10:57:53 -07005089 */
Benedict Wong8974a1c2020-01-20 22:14:59 -08005090 public int getConnectionOwnerUid(
5091 int protocol, @NonNull InetSocketAddress local, @NonNull InetSocketAddress remote) {
Jeff Vander Stoepe7205d62018-07-23 10:57:53 -07005092 ConnectionInfo connectionInfo = new ConnectionInfo(protocol, local, remote);
5093 try {
5094 return mService.getConnectionOwnerUid(connectionInfo);
5095 } catch (RemoteException e) {
5096 throw e.rethrowFromSystemServer();
5097 }
5098 }
Soi, Yoshinari99efcbd2015-11-12 12:09:02 +09005099
5100 private void printStackTrace() {
5101 if (DEBUG) {
5102 final StackTraceElement[] callStack = Thread.currentThread().getStackTrace();
5103 final StringBuffer sb = new StringBuffer();
5104 for (int i = 3; i < callStack.length; i++) {
5105 final String stackTrace = callStack[i].toString();
5106 if (stackTrace == null || stackTrace.contains("android.os")) {
5107 break;
5108 }
5109 sb.append(" [").append(stackTrace).append("]");
5110 }
5111 Log.d(TAG, "StackLog:" + sb.toString());
5112 }
5113 }
Cody Kestingb5c7abd2020-04-15 12:33:28 -07005114
Remi NGUYEN VANfe97b482021-01-15 23:02:47 +09005115 /** @hide */
5116 public TestNetworkManager startOrGetTestNetworkManager() {
5117 final IBinder tnBinder;
5118 try {
5119 tnBinder = mService.startOrGetTestNetworkService();
5120 } catch (RemoteException e) {
5121 throw e.rethrowFromSystemServer();
5122 }
5123
5124 return new TestNetworkManager(ITestNetworkManager.Stub.asInterface(tnBinder));
5125 }
5126
Remi NGUYEN VANfe97b482021-01-15 23:02:47 +09005127 /** @hide */
5128 public ConnectivityDiagnosticsManager createDiagnosticsManager() {
5129 return new ConnectivityDiagnosticsManager(mContext, mService);
5130 }
5131
Cody Kestingb5c7abd2020-04-15 12:33:28 -07005132 /**
5133 * Simulates a Data Stall for the specified Network.
5134 *
Remi NGUYEN VAN7d4530b2021-01-12 18:40:04 +09005135 * <p>This method should only be used for tests.
5136 *
Remi NGUYEN VAN5738b862021-04-08 16:26:20 +09005137 * <p>The caller must be the owner of the specified Network. This simulates a data stall to
5138 * have the system behave as if it had happened, but does not actually stall connectivity.
Cody Kestingb5c7abd2020-04-15 12:33:28 -07005139 *
5140 * @param detectionMethod The detection method used to identify the Data Stall.
Remi NGUYEN VAN5738b862021-04-08 16:26:20 +09005141 * See ConnectivityDiagnosticsManager.DataStallReport.DETECTION_METHOD_*.
5142 * @param timestampMillis The timestamp at which the stall 'occurred', in milliseconds, as per
5143 * SystemClock.elapsedRealtime.
Cody Kestingb5c7abd2020-04-15 12:33:28 -07005144 * @param network The Network for which a Data Stall is being simluated.
5145 * @param extras The PersistableBundle of extras included in the Data Stall notification.
5146 * @throws SecurityException if the caller is not the owner of the given network.
5147 * @hide
5148 */
Remi NGUYEN VAN7d4530b2021-01-12 18:40:04 +09005149 @SystemApi(client = SystemApi.Client.MODULE_LIBRARIES)
Cody Kestingb5c7abd2020-04-15 12:33:28 -07005150 @RequiresPermission(anyOf = {android.Manifest.permission.MANAGE_TEST_NETWORKS,
5151 android.Manifest.permission.NETWORK_STACK})
Remi NGUYEN VAN5738b862021-04-08 16:26:20 +09005152 public void simulateDataStall(@DetectionMethod int detectionMethod, long timestampMillis,
Cody Kestingb5c7abd2020-04-15 12:33:28 -07005153 @NonNull Network network, @NonNull PersistableBundle extras) {
5154 try {
5155 mService.simulateDataStall(detectionMethod, timestampMillis, network, extras);
5156 } catch (RemoteException e) {
5157 e.rethrowFromSystemServer();
5158 }
5159 }
James Mattisdcea9fb2020-10-28 21:48:54 -07005160
Daniel Bright60f02ed2020-06-15 16:10:01 -07005161 @NonNull
5162 private final List<QosCallbackConnection> mQosCallbackConnections = new ArrayList<>();
5163
5164 /**
5165 * Registers a {@link QosSocketInfo} with an associated {@link QosCallback}. The callback will
5166 * receive available QoS events related to the {@link Network} and local ip + port
5167 * specified within socketInfo.
5168 * <p/>
5169 * The same {@link QosCallback} must be unregistered before being registered a second time,
5170 * otherwise {@link QosCallbackRegistrationException} is thrown.
5171 * <p/>
5172 * This API does not, in itself, require any permission if called with a network that is not
5173 * restricted. However, the underlying implementation currently only supports the IMS network,
5174 * which is always restricted. That means non-preinstalled callers can't possibly find this API
5175 * useful, because they'd never be called back on networks that they would have access to.
5176 *
5177 * @throws SecurityException if {@link QosSocketInfo#getNetwork()} is restricted and the app is
5178 * missing CONNECTIVITY_USE_RESTRICTED_NETWORKS permission.
5179 * @throws QosCallback.QosCallbackRegistrationException if qosCallback is already registered.
5180 * @throws RuntimeException if the app already has too many callbacks registered.
5181 *
5182 * Exceptions after the time of registration is passed through
5183 * {@link QosCallback#onError(QosCallbackException)}. see: {@link QosCallbackException}.
5184 *
5185 * @param socketInfo the socket information used to match QoS events
Daniel Bright60f02ed2020-06-15 16:10:01 -07005186 * @param executor The executor on which the callback will be invoked. The provided
5187 * {@link Executor} must run callback sequentially, otherwise the order of
Daniel Brightb953a3e2021-03-10 11:51:50 -08005188 * callbacks cannot be guaranteed.onQosCallbackRegistered
5189 * @param callback receives qos events that satisfy socketInfo
Daniel Bright60f02ed2020-06-15 16:10:01 -07005190 *
5191 * @hide
5192 */
5193 @SystemApi
5194 public void registerQosCallback(@NonNull final QosSocketInfo socketInfo,
Daniel Brightb953a3e2021-03-10 11:51:50 -08005195 @CallbackExecutor @NonNull final Executor executor,
5196 @NonNull final QosCallback callback) {
Daniel Bright60f02ed2020-06-15 16:10:01 -07005197 Objects.requireNonNull(socketInfo, "socketInfo must be non-null");
Daniel Bright60f02ed2020-06-15 16:10:01 -07005198 Objects.requireNonNull(executor, "executor must be non-null");
Daniel Brightb953a3e2021-03-10 11:51:50 -08005199 Objects.requireNonNull(callback, "callback must be non-null");
Daniel Bright60f02ed2020-06-15 16:10:01 -07005200
5201 try {
5202 synchronized (mQosCallbackConnections) {
5203 if (getQosCallbackConnection(callback) == null) {
5204 final QosCallbackConnection connection =
5205 new QosCallbackConnection(this, callback, executor);
5206 mQosCallbackConnections.add(connection);
5207 mService.registerQosSocketCallback(socketInfo, connection);
5208 } else {
5209 Log.e(TAG, "registerQosCallback: Callback already registered");
5210 throw new QosCallbackRegistrationException();
5211 }
5212 }
5213 } catch (final RemoteException e) {
5214 Log.e(TAG, "registerQosCallback: Error while registering ", e);
5215
5216 // The same unregister method method is called for consistency even though nothing
5217 // will be sent to the ConnectivityService since the callback was never successfully
5218 // registered.
5219 unregisterQosCallback(callback);
5220 e.rethrowFromSystemServer();
5221 } catch (final ServiceSpecificException e) {
5222 Log.e(TAG, "registerQosCallback: Error while registering ", e);
5223 unregisterQosCallback(callback);
5224 throw convertServiceException(e);
5225 }
5226 }
5227
5228 /**
5229 * Unregisters the given {@link QosCallback}. The {@link QosCallback} will no longer receive
5230 * events once unregistered and can be registered a second time.
5231 * <p/>
5232 * If the {@link QosCallback} does not have an active registration, it is a no-op.
5233 *
5234 * @param callback the callback being unregistered
5235 *
5236 * @hide
5237 */
5238 @SystemApi
5239 public void unregisterQosCallback(@NonNull final QosCallback callback) {
5240 Objects.requireNonNull(callback, "The callback must be non-null");
5241 try {
5242 synchronized (mQosCallbackConnections) {
5243 final QosCallbackConnection connection = getQosCallbackConnection(callback);
5244 if (connection != null) {
5245 connection.stopReceivingMessages();
5246 mService.unregisterQosCallback(connection);
5247 mQosCallbackConnections.remove(connection);
5248 } else {
5249 Log.d(TAG, "unregisterQosCallback: Callback not registered");
5250 }
5251 }
5252 } catch (final RemoteException e) {
5253 Log.e(TAG, "unregisterQosCallback: Error while unregistering ", e);
5254 e.rethrowFromSystemServer();
5255 }
5256 }
5257
5258 /**
5259 * Gets the connection related to the callback.
5260 *
5261 * @param callback the callback to look up
5262 * @return the related connection
5263 */
5264 @Nullable
5265 private QosCallbackConnection getQosCallbackConnection(final QosCallback callback) {
5266 for (final QosCallbackConnection connection : mQosCallbackConnections) {
5267 // Checking by reference here is intentional
5268 if (connection.getCallback() == callback) {
5269 return connection;
5270 }
5271 }
5272 return null;
5273 }
Junyu Lai23568a42021-01-19 11:10:56 +00005274
5275 /**
Roshan Pius7992afd2020-12-22 15:10:42 -08005276 * Request a network to satisfy a set of {@link NetworkCapabilities}, but
Junyu Lai23568a42021-01-19 11:10:56 +00005277 * does not cause any networks to retain the NET_CAPABILITY_FOREGROUND capability. This can
5278 * be used to request that the system provide a network without causing the network to be
5279 * in the foreground.
5280 *
5281 * <p>This method will attempt to find the best network that matches the passed
5282 * {@link NetworkRequest}, and to bring up one that does if none currently satisfies the
5283 * criteria. The platform will evaluate which network is the best at its own discretion.
5284 * Throughput, latency, cost per byte, policy, user preference and other considerations
5285 * may be factored in the decision of what is considered the best network.
5286 *
5287 * <p>As long as this request is outstanding, the platform will try to maintain the best network
5288 * matching this request, while always attempting to match the request to a better network if
5289 * possible. If a better match is found, the platform will switch this request to the now-best
5290 * network and inform the app of the newly best network by invoking
5291 * {@link NetworkCallback#onAvailable(Network)} on the provided callback. Note that the platform
5292 * will not try to maintain any other network than the best one currently matching the request:
5293 * a network not matching any network request may be disconnected at any time.
5294 *
5295 * <p>For example, an application could use this method to obtain a connected cellular network
5296 * even if the device currently has a data connection over Ethernet. This may cause the cellular
5297 * radio to consume additional power. Or, an application could inform the system that it wants
5298 * a network supporting sending MMSes and have the system let it know about the currently best
5299 * MMS-supporting network through the provided {@link NetworkCallback}.
5300 *
5301 * <p>The status of the request can be followed by listening to the various callbacks described
5302 * in {@link NetworkCallback}. The {@link Network} object passed to the callback methods can be
5303 * used to direct traffic to the network (although accessing some networks may be subject to
5304 * holding specific permissions). Callers will learn about the specific characteristics of the
5305 * network through
5306 * {@link NetworkCallback#onCapabilitiesChanged(Network, NetworkCapabilities)} and
5307 * {@link NetworkCallback#onLinkPropertiesChanged(Network, LinkProperties)}. The methods of the
5308 * provided {@link NetworkCallback} will only be invoked due to changes in the best network
5309 * matching the request at any given time; therefore when a better network matching the request
5310 * becomes available, the {@link NetworkCallback#onAvailable(Network)} method is called
5311 * with the new network after which no further updates are given about the previously-best
5312 * network, unless it becomes the best again at some later time. All callbacks are invoked
5313 * in order on the same thread, which by default is a thread created by the framework running
5314 * in the app.
5315 *
5316 * <p>This{@link NetworkRequest} will live until released via
5317 * {@link #unregisterNetworkCallback(NetworkCallback)} or the calling application exits, at
5318 * which point the system may let go of the network at any time.
5319 *
5320 * <p>It is presently unsupported to request a network with mutable
5321 * {@link NetworkCapabilities} such as
5322 * {@link NetworkCapabilities#NET_CAPABILITY_VALIDATED} or
5323 * {@link NetworkCapabilities#NET_CAPABILITY_CAPTIVE_PORTAL}
5324 * as these {@code NetworkCapabilities} represent states that a particular
5325 * network may never attain, and whether a network will attain these states
5326 * is unknown prior to bringing up the network so the framework does not
5327 * know how to go about satisfying a request with these capabilities.
5328 *
5329 * <p>To avoid performance issues due to apps leaking callbacks, the system will limit the
5330 * number of outstanding requests to 100 per app (identified by their UID), shared with
5331 * all variants of this method, of {@link #registerNetworkCallback} as well as
5332 * {@link ConnectivityDiagnosticsManager#registerConnectivityDiagnosticsCallback}.
5333 * Requesting a network with this method will count toward this limit. If this limit is
5334 * exceeded, an exception will be thrown. To avoid hitting this issue and to conserve resources,
5335 * make sure to unregister the callbacks with
5336 * {@link #unregisterNetworkCallback(NetworkCallback)}.
5337 *
5338 * @param request {@link NetworkRequest} describing this request.
Junyu Lai23568a42021-01-19 11:10:56 +00005339 * @param networkCallback The {@link NetworkCallback} to be utilized for this request. Note
5340 * the callback must not be shared - it uniquely specifies this request.
junyulai56ed9a32021-04-15 00:39:49 +08005341 * @param handler {@link Handler} to specify the thread upon which the callback will be invoked.
5342 * If null, the callback is invoked on the default internal Handler.
Junyu Lai23568a42021-01-19 11:10:56 +00005343 * @throws IllegalArgumentException if {@code request} contains invalid network capabilities.
5344 * @throws SecurityException if missing the appropriate permissions.
5345 * @throws RuntimeException if the app already has too many callbacks registered.
5346 *
5347 * @hide
5348 */
5349 @SystemApi(client = MODULE_LIBRARIES)
5350 @SuppressLint("ExecutorRegistration")
5351 @RequiresPermission(anyOf = {
5352 android.Manifest.permission.NETWORK_SETTINGS,
5353 android.Manifest.permission.NETWORK_STACK,
5354 NetworkStack.PERMISSION_MAINLINE_NETWORK_STACK
5355 })
5356 public void requestBackgroundNetwork(@NonNull NetworkRequest request,
junyulai56ed9a32021-04-15 00:39:49 +08005357 @NonNull NetworkCallback networkCallback,
5358 @SuppressLint("ListenerLast") @NonNull Handler handler) {
Junyu Lai23568a42021-01-19 11:10:56 +00005359 final NetworkCapabilities nc = request.networkCapabilities;
5360 sendRequestForNetwork(nc, networkCallback, 0, BACKGROUND_REQUEST,
junyulaif4bc12f2021-03-09 20:49:48 +08005361 TYPE_NONE, new CallbackHandler(handler));
Junyu Lai23568a42021-01-19 11:10:56 +00005362 }
James Mattis6e6fabf2021-01-10 14:24:24 -08005363
5364 /**
James Mattis6e6fabf2021-01-10 14:24:24 -08005365 * Used by automotive devices to set the network preferences used to direct traffic at an
5366 * application level as per the given OemNetworkPreferences. An example use-case would be an
5367 * automotive OEM wanting to provide connectivity for applications critical to the usage of a
5368 * vehicle via a particular network.
5369 *
5370 * Calling this will overwrite the existing preference.
5371 *
5372 * @param preference {@link OemNetworkPreferences} The application network preference to be set.
5373 * @param executor the executor on which listener will be invoked.
5374 * @param listener {@link OnSetOemNetworkPreferenceListener} optional listener used to
5375 * communicate completion of setOemNetworkPreference(). This will only be
5376 * called once upon successful completion of setOemNetworkPreference().
5377 * @throws IllegalArgumentException if {@code preference} contains invalid preference values.
5378 * @throws SecurityException if missing the appropriate permissions.
5379 * @throws UnsupportedOperationException if called on a non-automotive device.
James Mattis7a253542021-01-26 16:23:52 -08005380 * @hide
James Mattis6e6fabf2021-01-10 14:24:24 -08005381 */
James Mattis7a253542021-01-26 16:23:52 -08005382 @SystemApi
James Mattis981865c2021-01-26 14:05:36 -08005383 @RequiresPermission(android.Manifest.permission.CONTROL_OEM_PAID_NETWORK_PREFERENCE)
James Mattis7a253542021-01-26 16:23:52 -08005384 public void setOemNetworkPreference(@NonNull final OemNetworkPreferences preference,
James Mattis6e6fabf2021-01-10 14:24:24 -08005385 @Nullable @CallbackExecutor final Executor executor,
Chalard Jeancc9ad152021-03-03 16:37:13 +09005386 @Nullable final Runnable listener) {
James Mattis6e6fabf2021-01-10 14:24:24 -08005387 Objects.requireNonNull(preference, "OemNetworkPreferences must be non-null");
5388 if (null != listener) {
5389 Objects.requireNonNull(executor, "Executor must be non-null");
5390 }
Chalard Jeancc9ad152021-03-03 16:37:13 +09005391 final IOnCompleteListener listenerInternal = listener == null ? null :
5392 new IOnCompleteListener.Stub() {
James Mattis6e6fabf2021-01-10 14:24:24 -08005393 @Override
5394 public void onComplete() {
Chalard Jeancc9ad152021-03-03 16:37:13 +09005395 executor.execute(listener::run);
James Mattis6e6fabf2021-01-10 14:24:24 -08005396 }
5397 };
5398
5399 try {
5400 mService.setOemNetworkPreference(preference, listenerInternal);
5401 } catch (RemoteException e) {
5402 Log.e(TAG, "setOemNetworkPreference() failed for preference: " + preference.toString());
5403 throw e.rethrowFromSystemServer();
5404 }
5405 }
lucaslin1a6095c2021-03-12 00:46:33 +08005406
Chalard Jean03433052021-02-25 17:23:40 +09005407 /**
5408 * Request that a user profile is put by default on a network matching a given preference.
5409 *
5410 * See the documentation for the individual preferences for a description of the supported
5411 * behaviors.
5412 *
5413 * @param profile the profile concerned.
5414 * @param preference the preference for this profile.
5415 * @param executor an executor to execute the listener on. Optional if listener is null.
5416 * @param listener an optional listener to listen for completion of the operation.
5417 * @throws IllegalArgumentException if {@code profile} is not a valid user profile.
5418 * @throws SecurityException if missing the appropriate permissions.
5419 * @hide
5420 */
Chalard Jeancc9ad152021-03-03 16:37:13 +09005421 // This function is for establishing per-profile default networking and can only be called by
5422 // the device policy manager, running as the system server. It would make no sense to call it
5423 // on a context for a user because it does not establish a setting on behalf of a user, rather
5424 // it establishes a setting for a user on behalf of the DPM.
5425 @SuppressLint({"UserHandle"})
5426 @SystemApi(client = MODULE_LIBRARIES)
Chalard Jean03433052021-02-25 17:23:40 +09005427 @RequiresPermission(android.Manifest.permission.NETWORK_STACK)
5428 public void setProfileNetworkPreference(@NonNull final UserHandle profile,
5429 @ProfileNetworkPreference final int preference,
5430 @Nullable @CallbackExecutor final Executor executor,
5431 @Nullable final Runnable listener) {
5432 if (null != listener) {
5433 Objects.requireNonNull(executor, "Pass a non-null executor, or a null listener");
5434 }
5435 final IOnCompleteListener proxy;
5436 if (null == listener) {
5437 proxy = null;
5438 } else {
5439 proxy = new IOnCompleteListener.Stub() {
5440 @Override
5441 public void onComplete() {
5442 executor.execute(listener::run);
5443 }
5444 };
5445 }
5446 try {
5447 mService.setProfileNetworkPreference(profile, preference, proxy);
5448 } catch (RemoteException e) {
5449 throw e.rethrowFromSystemServer();
5450 }
5451 }
5452
lucaslin1a6095c2021-03-12 00:46:33 +08005453 // The first network ID of IPSec tunnel interface.
lucaslin28633072021-03-15 17:24:12 +08005454 private static final int TUN_INTF_NETID_START = 0xFC00; // 0xFC00 = 64512
lucaslin1a6095c2021-03-12 00:46:33 +08005455 // The network ID range of IPSec tunnel interface.
lucaslin28633072021-03-15 17:24:12 +08005456 private static final int TUN_INTF_NETID_RANGE = 0x0400; // 0x0400 = 1024
lucaslin1a6095c2021-03-12 00:46:33 +08005457
5458 /**
5459 * Get the network ID range reserved for IPSec tunnel interfaces.
5460 *
5461 * @return A Range which indicates the network ID range of IPSec tunnel interface.
5462 * @hide
5463 */
5464 @SystemApi(client = MODULE_LIBRARIES)
5465 @NonNull
5466 public static Range<Integer> getIpSecNetIdRange() {
5467 return new Range(TUN_INTF_NETID_START, TUN_INTF_NETID_START + TUN_INTF_NETID_RANGE - 1);
5468 }
The Android Open Source Project28527d22009-03-03 19:31:44 -08005469}