blob: 8c66db9a205af74270e7ee0ceab33bf81604fb8d [file] [log] [blame]
The Android Open Source Project28527d22009-03-03 19:31:44 -08001/*
2 * Copyright (C) 2008 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
The Android Open Source Project28527d22009-03-03 19:31:44 -080016package android.net;
17
Junyu Laia62493f2021-01-19 11:10:56 +000018import static android.annotation.SystemApi.Client.MODULE_LIBRARIES;
Junyu Laia62493f2021-01-19 11:10:56 +000019import static android.net.NetworkRequest.Type.BACKGROUND_REQUEST;
junyulaiad010792021-01-11 16:53:38 +080020import static android.net.NetworkRequest.Type.LISTEN;
21import static android.net.NetworkRequest.Type.REQUEST;
junyulai7514e312021-03-05 15:51:17 +080022import static android.net.NetworkRequest.Type.TRACK_BEST;
junyulaiad010792021-01-11 16:53:38 +080023import static android.net.NetworkRequest.Type.TRACK_DEFAULT;
Lorenzo Colitti76b639e2021-01-29 20:14:04 +090024import static android.net.NetworkRequest.Type.TRACK_SYSTEM_DEFAULT;
Daniel Brightf9e945b2020-06-15 16:10:01 -070025import static android.net.QosCallback.QosCallbackRegistrationException;
junyulaid05a1922019-01-15 11:32:44 +080026
junyulai4c95b082018-12-27 17:25:29 +080027import android.annotation.CallbackExecutor;
Felipe Leme30511352016-01-22 09:44:57 -080028import android.annotation.IntDef;
Chalard Jean158702d2019-01-07 19:26:34 +090029import android.annotation.NonNull;
Robin Leee5d5ed52016-01-05 18:03:46 +000030import android.annotation.Nullable;
Jeff Sharkey656584a2017-04-24 11:18:03 -060031import android.annotation.RequiresPermission;
The Android Open Source Project28527d22009-03-03 19:31:44 -080032import android.annotation.SdkConstant;
33import android.annotation.SdkConstant.SdkConstantType;
Junyu Laia62493f2021-01-19 11:10:56 +000034import android.annotation.SuppressLint;
Udam Sainicd645462016-01-04 12:16:14 -080035import android.annotation.SystemApi;
Jeff Sharkey9b39e9a2017-06-02 17:36:26 -060036import android.annotation.SystemService;
Robert Greenwaltf99b8392014-03-26 16:47:06 -070037import android.app.PendingIntent;
Artur Satayev9c2add62019-12-10 17:47:52 +000038import android.compat.annotation.UnsupportedAppUsage;
Jeff Sharkey2d9e5a52012-04-04 20:40:58 -070039import android.content.Context;
Robert Greenwaltf3017f72014-05-18 23:07:25 -070040import android.content.Intent;
junyulai4c95b082018-12-27 17:25:29 +080041import android.net.IpSecManager.UdpEncapsulationSocket;
42import android.net.SocketKeepalive.Callback;
markchien91c78e52020-01-20 19:31:56 +080043import android.net.TetheringManager.StartTetheringCallback;
markchien6ae63e52020-01-21 13:11:06 +080044import android.net.TetheringManager.TetheringEventCallback;
markchien91c78e52020-01-20 19:31:56 +080045import android.net.TetheringManager.TetheringRequest;
Roshan Pius951c0032020-12-22 15:10:42 -080046import android.net.wifi.WifiNetworkSuggestion;
Robert Greenwalt2034b912009-08-12 16:08:25 -070047import android.os.Binder;
Mathew Inwoodbdfc1fc2018-12-20 15:30:45 +000048import android.os.Build;
Jeff Sharkey39c01eb2011-08-16 14:37:57 -070049import android.os.Build.VERSION_CODES;
Jeremy Klein3dabcb92016-01-22 14:11:45 -080050import android.os.Bundle;
Robert Greenwaltf99b8392014-03-26 16:47:06 -070051import android.os.Handler;
Dianne Hackborn5ac88162014-02-26 16:20:52 -080052import android.os.IBinder;
Robert Greenwaltf99b8392014-03-26 16:47:06 -070053import android.os.Looper;
54import android.os.Message;
Robert Greenwalt15a41532012-08-21 19:27:00 -070055import android.os.Messenger;
junyulai7e06ad42019-03-04 22:45:36 +080056import android.os.ParcelFileDescriptor;
Cody Kestingf53a0752020-04-15 12:33:28 -070057import android.os.PersistableBundle;
Lorenzo Colitti2780e4b2015-07-29 11:41:21 +090058import android.os.Process;
The Android Open Source Project28527d22009-03-03 19:31:44 -080059import android.os.RemoteException;
Jeremy Klein3dabcb92016-01-22 14:11:45 -080060import android.os.ResultReceiver;
Dianne Hackborn5ac88162014-02-26 16:20:52 -080061import android.os.ServiceManager;
Hugo Benichia590ab82017-05-11 13:16:17 +090062import android.os.ServiceSpecificException;
Jeff Sharkey971cd162011-08-29 16:02:57 -070063import android.provider.Settings;
Wink Saville689f7042014-12-05 11:10:30 -080064import android.telephony.SubscriptionManager;
Meng Wanged6f4412019-11-18 17:10:00 -080065import android.telephony.TelephonyManager;
Dianne Hackborn5ac88162014-02-26 16:20:52 -080066import android.util.ArrayMap;
Robert Greenwaltf99b8392014-03-26 16:47:06 -070067import android.util.Log;
Lorenzo Colitti3f54f102020-12-12 00:51:11 +090068import android.util.Range;
Erik Klinece55eb12017-01-26 18:08:28 +090069import android.util.SparseIntArray;
The Android Open Source Project28527d22009-03-03 19:31:44 -080070
Remi NGUYEN VAN77b0c1c2020-12-23 12:45:08 +090071import com.android.connectivity.aidl.INetworkAgent;
markchien6ae63e52020-01-21 13:11:06 +080072import com.android.internal.annotations.GuardedBy;
Remi NGUYEN VAN5086c292021-03-15 07:27:44 +000073import com.android.internal.util.Preconditions;
Hugo Benichi2aa65af2017-03-06 09:17:06 +090074import com.android.internal.util.Protocol;
Robert Greenwalt0c150c02014-05-21 20:04:36 -070075
Paul Jensen3e2917c2014-08-27 12:38:45 -040076import libcore.net.event.NetworkEventDispatcher;
77
junyulai7e06ad42019-03-04 22:45:36 +080078import java.io.IOException;
79import java.io.UncheckedIOException;
Felipe Leme30511352016-01-22 09:44:57 -080080import java.lang.annotation.Retention;
81import java.lang.annotation.RetentionPolicy;
Lorenzo Colitti3f54f102020-12-12 00:51:11 +090082import java.net.DatagramSocket;
Jeremy Klein434835a2015-12-28 15:11:58 -080083import java.net.InetAddress;
Jeff Vander Stoep39a51e02018-07-23 10:57:53 -070084import java.net.InetSocketAddress;
junyulai0835a1e2019-01-08 20:04:33 +080085import java.net.Socket;
Hugo Benichi2aa65af2017-03-06 09:17:06 +090086import java.util.ArrayList;
Lorenzo Colitti3f54f102020-12-12 00:51:11 +090087import java.util.Collection;
Jeremy Klein434835a2015-12-28 15:11:58 -080088import java.util.HashMap;
Hugo Benichi2aa65af2017-03-06 09:17:06 +090089import java.util.List;
90import java.util.Map;
markchien6ae63e52020-01-21 13:11:06 +080091import java.util.Objects;
junyulai4c95b082018-12-27 17:25:29 +080092import java.util.concurrent.Executor;
junyulai070f9ff2019-01-16 20:23:34 +080093import java.util.concurrent.ExecutorService;
94import java.util.concurrent.Executors;
95import java.util.concurrent.RejectedExecutionException;
Jeremy Klein434835a2015-12-28 15:11:58 -080096
The Android Open Source Project28527d22009-03-03 19:31:44 -080097/**
98 * Class that answers queries about the state of network connectivity. It also
Jeff Sharkey9b39e9a2017-06-02 17:36:26 -060099 * notifies applications when network connectivity changes.
The Android Open Source Project28527d22009-03-03 19:31:44 -0800100 * <p>
101 * The primary responsibilities of this class are to:
102 * <ol>
103 * <li>Monitor network connections (Wi-Fi, GPRS, UMTS, etc.)</li>
104 * <li>Send broadcast intents when network connectivity changes</li>
105 * <li>Attempt to "fail over" to another network when connectivity to a network
106 * is lost</li>
107 * <li>Provide an API that allows applications to query the coarse-grained or fine-grained
108 * state of the available networks</li>
Robert Greenwaltf3017f72014-05-18 23:07:25 -0700109 * <li>Provide an API that allows applications to request and select networks for their data
110 * traffic</li>
The Android Open Source Project28527d22009-03-03 19:31:44 -0800111 * </ol>
112 */
Jeff Sharkey9b39e9a2017-06-02 17:36:26 -0600113@SystemService(Context.CONNECTIVITY_SERVICE)
Jeff Sharkey921ebf22011-05-19 17:12:49 -0700114public class ConnectivityManager {
115 private static final String TAG = "ConnectivityManager";
Soi, Yoshinarib8102ef2015-11-12 12:09:02 +0900116 private static final boolean DEBUG = Log.isLoggable(TAG, Log.DEBUG);
Jeff Sharkey921ebf22011-05-19 17:12:49 -0700117
The Android Open Source Project28527d22009-03-03 19:31:44 -0800118 /**
Robert Greenwaltf3017f72014-05-18 23:07:25 -0700119 * A change in network connectivity has occurred. A default connection has either
The Android Open Source Project28527d22009-03-03 19:31:44 -0800120 * been established or lost. The NetworkInfo for the affected network is
121 * sent as an extra; it should be consulted to see what kind of
122 * connectivity event occurred.
123 * <p/>
Mark Lu3e422ac2016-12-05 10:57:55 -0800124 * Apps targeting Android 7.0 (API level 24) and higher do not receive this
125 * broadcast if they declare the broadcast receiver in their manifest. Apps
126 * will still receive broadcasts if they register their
127 * {@link android.content.BroadcastReceiver} with
128 * {@link android.content.Context#registerReceiver Context.registerReceiver()}
129 * and that context is still valid.
130 * <p/>
The Android Open Source Project28527d22009-03-03 19:31:44 -0800131 * If this is a connection that was the result of failing over from a
132 * disconnected network, then the FAILOVER_CONNECTION boolean extra is
133 * set to true.
134 * <p/>
135 * For a loss of connectivity, if the connectivity manager is attempting
136 * to connect (or has already connected) to another network, the
137 * NetworkInfo for the new network is also passed as an extra. This lets
138 * any receivers of the broadcast know that they should not necessarily
139 * tell the user that no data traffic will be possible. Instead, the
Robert Greenwaltfaa4b402013-02-15 10:56:35 -0800140 * receiver should expect another broadcast soon, indicating either that
The Android Open Source Project28527d22009-03-03 19:31:44 -0800141 * the failover attempt succeeded (and so there is still overall data
142 * connectivity), or that the failover attempt failed, meaning that all
143 * connectivity has been lost.
144 * <p/>
145 * For a disconnect event, the boolean extra EXTRA_NO_CONNECTIVITY
146 * is set to {@code true} if there are no connected networks at all.
Chalard Jean52e23962018-02-10 05:33:50 +0900147 *
148 * @deprecated apps should use the more versatile {@link #requestNetwork},
149 * {@link #registerNetworkCallback} or {@link #registerDefaultNetworkCallback}
150 * functions instead for faster and more detailed updates about the network
151 * changes they care about.
The Android Open Source Project28527d22009-03-03 19:31:44 -0800152 */
Jeff Sharkeyee9275b2013-02-20 18:21:19 -0800153 @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION)
Chalard Jean52e23962018-02-10 05:33:50 +0900154 @Deprecated
The Android Open Source Project28527d22009-03-03 19:31:44 -0800155 public static final String CONNECTIVITY_ACTION = "android.net.conn.CONNECTIVITY_CHANGE";
Jeff Sharkey66fa9682011-08-02 17:22:34 -0700156
The Android Open Source Project28527d22009-03-03 19:31:44 -0800157 /**
Paul Jensen60df4aa2015-02-27 22:55:47 -0500158 * The device has connected to a network that has presented a captive
159 * portal, which is blocking Internet connectivity. The user was presented
160 * with a notification that network sign in is required,
161 * and the user invoked the notification's action indicating they
Paul Jensen75e0adb2015-05-22 10:50:39 -0400162 * desire to sign in to the network. Apps handling this activity should
Paul Jensen60df4aa2015-02-27 22:55:47 -0500163 * facilitate signing in to the network. This action includes a
164 * {@link Network} typed extra called {@link #EXTRA_NETWORK} that represents
165 * the network presenting the captive portal; all communication with the
166 * captive portal must be done using this {@code Network} object.
167 * <p/>
Paul Jensen75e0adb2015-05-22 10:50:39 -0400168 * This activity includes a {@link CaptivePortal} extra named
169 * {@link #EXTRA_CAPTIVE_PORTAL} that can be used to indicate different
170 * outcomes of the captive portal sign in to the system:
171 * <ul>
172 * <li> When the app handling this action believes the user has signed in to
173 * the network and the captive portal has been dismissed, the app should
174 * call {@link CaptivePortal#reportCaptivePortalDismissed} so the system can
175 * reevaluate the network. If reevaluation finds the network no longer
176 * subject to a captive portal, the network may become the default active
Chalard Jean9dd11612018-06-04 16:52:49 +0900177 * data network.</li>
Paul Jensen75e0adb2015-05-22 10:50:39 -0400178 * <li> When the app handling this action believes the user explicitly wants
Paul Jensen60df4aa2015-02-27 22:55:47 -0500179 * to ignore the captive portal and the network, the app should call
Paul Jensen75e0adb2015-05-22 10:50:39 -0400180 * {@link CaptivePortal#ignoreNetwork}. </li>
181 * </ul>
Paul Jensen60df4aa2015-02-27 22:55:47 -0500182 */
183 @SdkConstant(SdkConstantType.ACTIVITY_INTENT_ACTION)
184 public static final String ACTION_CAPTIVE_PORTAL_SIGN_IN = "android.net.conn.CAPTIVE_PORTAL";
185
186 /**
The Android Open Source Project28527d22009-03-03 19:31:44 -0800187 * The lookup key for a {@link NetworkInfo} object. Retrieve with
188 * {@link android.content.Intent#getParcelableExtra(String)}.
Jeff Sharkey66fa9682011-08-02 17:22:34 -0700189 *
Chalard Jean97021a12019-01-11 16:47:53 +0900190 * @deprecated The {@link NetworkInfo} object is deprecated, as many of its properties
191 * can't accurately represent modern network characteristics.
192 * Please obtain information about networks from the {@link NetworkCapabilities}
193 * or {@link LinkProperties} objects instead.
The Android Open Source Project28527d22009-03-03 19:31:44 -0800194 */
Jeff Sharkey66fa9682011-08-02 17:22:34 -0700195 @Deprecated
The Android Open Source Project28527d22009-03-03 19:31:44 -0800196 public static final String EXTRA_NETWORK_INFO = "networkInfo";
Jeff Sharkey66fa9682011-08-02 17:22:34 -0700197
The Android Open Source Project28527d22009-03-03 19:31:44 -0800198 /**
Jeff Sharkey47905d12012-08-06 11:41:50 -0700199 * Network type which triggered a {@link #CONNECTIVITY_ACTION} broadcast.
Jeff Sharkey47905d12012-08-06 11:41:50 -0700200 *
201 * @see android.content.Intent#getIntExtra(String, int)
Chalard Jean97021a12019-01-11 16:47:53 +0900202 * @deprecated The network type is not rich enough to represent the characteristics
203 * of modern networks. Please use {@link NetworkCapabilities} instead,
204 * in particular the transports.
Jeff Sharkey47905d12012-08-06 11:41:50 -0700205 */
Chalard Jean97021a12019-01-11 16:47:53 +0900206 @Deprecated
Jeff Sharkey47905d12012-08-06 11:41:50 -0700207 public static final String EXTRA_NETWORK_TYPE = "networkType";
208
209 /**
The Android Open Source Project28527d22009-03-03 19:31:44 -0800210 * The lookup key for a boolean that indicates whether a connect event
211 * is for a network to which the connectivity manager was failing over
212 * following a disconnect on another network.
213 * Retrieve it with {@link android.content.Intent#getBooleanExtra(String,boolean)}.
junyulai9826e7f2018-12-13 12:47:51 +0800214 *
215 * @deprecated See {@link NetworkInfo}.
The Android Open Source Project28527d22009-03-03 19:31:44 -0800216 */
junyulai9826e7f2018-12-13 12:47:51 +0800217 @Deprecated
The Android Open Source Project28527d22009-03-03 19:31:44 -0800218 public static final String EXTRA_IS_FAILOVER = "isFailover";
219 /**
220 * The lookup key for a {@link NetworkInfo} object. This is supplied when
221 * there is another network that it may be possible to connect to. Retrieve with
222 * {@link android.content.Intent#getParcelableExtra(String)}.
junyulai9826e7f2018-12-13 12:47:51 +0800223 *
224 * @deprecated See {@link NetworkInfo}.
The Android Open Source Project28527d22009-03-03 19:31:44 -0800225 */
junyulai9826e7f2018-12-13 12:47:51 +0800226 @Deprecated
The Android Open Source Project28527d22009-03-03 19:31:44 -0800227 public static final String EXTRA_OTHER_NETWORK_INFO = "otherNetwork";
228 /**
229 * The lookup key for a boolean that indicates whether there is a
230 * complete lack of connectivity, i.e., no network is available.
231 * Retrieve it with {@link android.content.Intent#getBooleanExtra(String,boolean)}.
232 */
233 public static final String EXTRA_NO_CONNECTIVITY = "noConnectivity";
234 /**
235 * The lookup key for a string that indicates why an attempt to connect
236 * to a network failed. The string has no particular structure. It is
237 * intended to be used in notifications presented to users. Retrieve
238 * it with {@link android.content.Intent#getStringExtra(String)}.
239 */
240 public static final String EXTRA_REASON = "reason";
241 /**
242 * The lookup key for a string that provides optionally supplied
243 * extra information about the network state. The information
244 * may be passed up from the lower networking layers, and its
245 * meaning may be specific to a particular network type. Retrieve
246 * it with {@link android.content.Intent#getStringExtra(String)}.
junyulai9826e7f2018-12-13 12:47:51 +0800247 *
248 * @deprecated See {@link NetworkInfo#getExtraInfo()}.
The Android Open Source Project28527d22009-03-03 19:31:44 -0800249 */
junyulai9826e7f2018-12-13 12:47:51 +0800250 @Deprecated
The Android Open Source Project28527d22009-03-03 19:31:44 -0800251 public static final String EXTRA_EXTRA_INFO = "extraInfo";
Robert Greenwalt986c7412010-09-08 15:24:47 -0700252 /**
253 * The lookup key for an int that provides information about
254 * our connection to the internet at large. 0 indicates no connection,
255 * 100 indicates a great connection. Retrieve it with
Jeff Sharkey921ebf22011-05-19 17:12:49 -0700256 * {@link android.content.Intent#getIntExtra(String, int)}.
Robert Greenwalt986c7412010-09-08 15:24:47 -0700257 * {@hide}
258 */
259 public static final String EXTRA_INET_CONDITION = "inetCondition";
The Android Open Source Project28527d22009-03-03 19:31:44 -0800260 /**
Paul Jensen75e0adb2015-05-22 10:50:39 -0400261 * The lookup key for a {@link CaptivePortal} object included with the
262 * {@link #ACTION_CAPTIVE_PORTAL_SIGN_IN} intent. The {@code CaptivePortal}
263 * object can be used to either indicate to the system that the captive
264 * portal has been dismissed or that the user does not want to pursue
265 * signing in to captive portal. Retrieve it with
266 * {@link android.content.Intent#getParcelableExtra(String)}.
Paul Jensen60df4aa2015-02-27 22:55:47 -0500267 */
Paul Jensen75e0adb2015-05-22 10:50:39 -0400268 public static final String EXTRA_CAPTIVE_PORTAL = "android.net.extra.CAPTIVE_PORTAL";
Jan Nordqvist032e2672015-09-22 15:54:32 -0700269
270 /**
271 * Key for passing a URL to the captive portal login activity.
272 */
273 public static final String EXTRA_CAPTIVE_PORTAL_URL = "android.net.extra.CAPTIVE_PORTAL_URL";
274
Paul Jensen60df4aa2015-02-27 22:55:47 -0500275 /**
Remi NGUYEN VANc2491572018-05-22 10:01:53 +0900276 * Key for passing a {@link android.net.captiveportal.CaptivePortalProbeSpec} to the captive
277 * portal login activity.
278 * {@hide}
279 */
Remi NGUYEN VAN70ab67f2019-01-17 14:38:31 +0900280 @SystemApi
Remi NGUYEN VANc2491572018-05-22 10:01:53 +0900281 public static final String EXTRA_CAPTIVE_PORTAL_PROBE_SPEC =
282 "android.net.extra.CAPTIVE_PORTAL_PROBE_SPEC";
283
284 /**
Hugo Benichi454e0662016-12-14 08:23:40 +0900285 * Key for passing a user agent string to the captive portal login activity.
286 * {@hide}
287 */
Remi NGUYEN VAN70ab67f2019-01-17 14:38:31 +0900288 @SystemApi
Hugo Benichi454e0662016-12-14 08:23:40 +0900289 public static final String EXTRA_CAPTIVE_PORTAL_USER_AGENT =
290 "android.net.extra.CAPTIVE_PORTAL_USER_AGENT";
291
292 /**
Haoyu Baib5da5752012-06-20 14:29:57 -0700293 * Broadcast action to indicate the change of data activity status
294 * (idle or active) on a network in a recent period.
Robert Greenwaltfaa4b402013-02-15 10:56:35 -0800295 * The network becomes active when data transmission is started, or
296 * idle if there is no data transmission for a period of time.
Haoyu Baib5da5752012-06-20 14:29:57 -0700297 * {@hide}
298 */
299 @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION)
Chalard Jean9dd11612018-06-04 16:52:49 +0900300 public static final String ACTION_DATA_ACTIVITY_CHANGE =
301 "android.net.conn.DATA_ACTIVITY_CHANGE";
Haoyu Baib5da5752012-06-20 14:29:57 -0700302 /**
303 * The lookup key for an enum that indicates the network device type on which this data activity
304 * change happens.
305 * {@hide}
306 */
307 public static final String EXTRA_DEVICE_TYPE = "deviceType";
308 /**
309 * The lookup key for a boolean that indicates the device is active or not. {@code true} means
310 * it is actively sending or receiving data and {@code false} means it is idle.
311 * {@hide}
312 */
313 public static final String EXTRA_IS_ACTIVE = "isActive";
Ashish Sharma29f7e0e2014-03-12 18:42:23 -0700314 /**
315 * The lookup key for a long that contains the timestamp (nanos) of the radio state change.
316 * {@hide}
317 */
318 public static final String EXTRA_REALTIME_NS = "tsNanos";
Haoyu Baib5da5752012-06-20 14:29:57 -0700319
320 /**
The Android Open Source Project28527d22009-03-03 19:31:44 -0800321 * Broadcast Action: The setting for background data usage has changed
322 * values. Use {@link #getBackgroundDataSetting()} to get the current value.
323 * <p>
324 * If an application uses the network in the background, it should listen
325 * for this broadcast and stop using the background data if the value is
Jeff Sharkey921ebf22011-05-19 17:12:49 -0700326 * {@code false}.
Jeff Sharkeyc958c772012-01-30 16:29:24 -0800327 * <p>
328 *
329 * @deprecated As of {@link VERSION_CODES#ICE_CREAM_SANDWICH}, availability
330 * of background data depends on several combined factors, and
331 * this broadcast is no longer sent. Instead, when background
332 * data is unavailable, {@link #getActiveNetworkInfo()} will now
333 * appear disconnected. During first boot after a platform
334 * upgrade, this broadcast will be sent once if
335 * {@link #getBackgroundDataSetting()} was {@code false} before
336 * the upgrade.
The Android Open Source Project28527d22009-03-03 19:31:44 -0800337 */
338 @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION)
Jeff Sharkeyc958c772012-01-30 16:29:24 -0800339 @Deprecated
The Android Open Source Project28527d22009-03-03 19:31:44 -0800340 public static final String ACTION_BACKGROUND_DATA_SETTING_CHANGED =
341 "android.net.conn.BACKGROUND_DATA_SETTING_CHANGED";
342
Robert Greenwaltd3401f92010-09-15 17:36:33 -0700343 /**
344 * Broadcast Action: The network connection may not be good
345 * uses {@code ConnectivityManager.EXTRA_INET_CONDITION} and
346 * {@code ConnectivityManager.EXTRA_NETWORK_INFO} to specify
347 * the network and it's condition.
348 * @hide
349 */
Jeff Sharkeyee9275b2013-02-20 18:21:19 -0800350 @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION)
Mathew Inwoodc5b9bec2018-08-08 14:52:47 +0100351 @UnsupportedAppUsage
Robert Greenwaltd3401f92010-09-15 17:36:33 -0700352 public static final String INET_CONDITION_ACTION =
353 "android.net.conn.INET_CONDITION_ACTION";
354
Robert Greenwalt2034b912009-08-12 16:08:25 -0700355 /**
Robert Greenwaltfaa4b402013-02-15 10:56:35 -0800356 * Broadcast Action: A tetherable connection has come or gone.
357 * Uses {@code ConnectivityManager.EXTRA_AVAILABLE_TETHER},
Erik Kline762fa8e2017-04-17 16:47:23 +0900358 * {@code ConnectivityManager.EXTRA_ACTIVE_LOCAL_ONLY},
359 * {@code ConnectivityManager.EXTRA_ACTIVE_TETHER}, and
Robert Greenwaltfaa4b402013-02-15 10:56:35 -0800360 * {@code ConnectivityManager.EXTRA_ERRORED_TETHER} to indicate
361 * the current state of tethering. Each include a list of
362 * interface names in that state (may be empty).
Robert Greenwalt0c4828c2010-01-26 11:40:34 -0800363 * @hide
364 */
Jeff Sharkeyee9275b2013-02-20 18:21:19 -0800365 @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION)
Mathew Inwoode1a17ba2020-11-04 09:29:36 +0000366 @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.R, trackingBug = 170729553)
Robert Greenwalt0c4828c2010-01-26 11:40:34 -0800367 public static final String ACTION_TETHER_STATE_CHANGED =
markchiena0f8fd92019-12-25 19:40:32 +0800368 TetheringManager.ACTION_TETHER_STATE_CHANGED;
Robert Greenwalt0c4828c2010-01-26 11:40:34 -0800369
370 /**
371 * @hide
Robert Greenwaltfaa4b402013-02-15 10:56:35 -0800372 * gives a String[] listing all the interfaces configured for
373 * tethering and currently available for tethering.
Robert Greenwalt0c4828c2010-01-26 11:40:34 -0800374 */
Mathew Inwoode1a17ba2020-11-04 09:29:36 +0000375 @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.R, trackingBug = 170729553)
markchiena0f8fd92019-12-25 19:40:32 +0800376 public static final String EXTRA_AVAILABLE_TETHER = TetheringManager.EXTRA_AVAILABLE_TETHER;
Robert Greenwalt0c4828c2010-01-26 11:40:34 -0800377
378 /**
379 * @hide
Erik Kline762fa8e2017-04-17 16:47:23 +0900380 * gives a String[] listing all the interfaces currently in local-only
381 * mode (ie, has DHCPv4+IPv6-ULA support and no packet forwarding)
Robert Greenwalt0c4828c2010-01-26 11:40:34 -0800382 */
markchiena0f8fd92019-12-25 19:40:32 +0800383 public static final String EXTRA_ACTIVE_LOCAL_ONLY = TetheringManager.EXTRA_ACTIVE_LOCAL_ONLY;
Erik Kline762fa8e2017-04-17 16:47:23 +0900384
385 /**
386 * @hide
387 * gives a String[] listing all the interfaces currently tethered
388 * (ie, has DHCPv4 support and packets potentially forwarded/NATed)
389 */
Mathew Inwoode1a17ba2020-11-04 09:29:36 +0000390 @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.R, trackingBug = 170729553)
markchiena0f8fd92019-12-25 19:40:32 +0800391 public static final String EXTRA_ACTIVE_TETHER = TetheringManager.EXTRA_ACTIVE_TETHER;
Robert Greenwalt8e87f122010-02-11 18:18:40 -0800392
393 /**
394 * @hide
Robert Greenwaltfaa4b402013-02-15 10:56:35 -0800395 * gives a String[] listing all the interfaces we tried to tether and
396 * failed. Use {@link #getLastTetherError} to find the error code
397 * for any interfaces listed here.
Robert Greenwalt8e87f122010-02-11 18:18:40 -0800398 */
Mathew Inwoode1a17ba2020-11-04 09:29:36 +0000399 @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.R, trackingBug = 170729553)
markchiena0f8fd92019-12-25 19:40:32 +0800400 public static final String EXTRA_ERRORED_TETHER = TetheringManager.EXTRA_ERRORED_TETHER;
Robert Greenwalt0c4828c2010-01-26 11:40:34 -0800401
402 /**
Russell Brenner30fe2642013-02-12 10:03:14 -0800403 * Broadcast Action: The captive portal tracker has finished its test.
404 * Sent only while running Setup Wizard, in lieu of showing a user
405 * notification.
406 * @hide
407 */
Jeff Sharkeyee9275b2013-02-20 18:21:19 -0800408 @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION)
Russell Brenner30fe2642013-02-12 10:03:14 -0800409 public static final String ACTION_CAPTIVE_PORTAL_TEST_COMPLETED =
410 "android.net.conn.CAPTIVE_PORTAL_TEST_COMPLETED";
411 /**
412 * The lookup key for a boolean that indicates whether a captive portal was detected.
413 * Retrieve it with {@link android.content.Intent#getBooleanExtra(String,boolean)}.
414 * @hide
415 */
416 public static final String EXTRA_IS_CAPTIVE_PORTAL = "captivePortal";
417
418 /**
Lorenzo Colitti6947c062015-04-03 16:38:52 +0900419 * Action used to display a dialog that asks the user whether to connect to a network that is
420 * not validated. This intent is used to start the dialog in settings via startActivity.
421 *
422 * @hide
423 */
424 public static final String ACTION_PROMPT_UNVALIDATED = "android.net.conn.PROMPT_UNVALIDATED";
425
426 /**
Lorenzo Colitti199ecfc2016-09-15 14:02:29 +0900427 * Action used to display a dialog that asks the user whether to avoid a network that is no
428 * longer validated. This intent is used to start the dialog in settings via startActivity.
429 *
430 * @hide
431 */
432 public static final String ACTION_PROMPT_LOST_VALIDATION =
433 "android.net.conn.PROMPT_LOST_VALIDATION";
434
435 /**
lucaslin2240ef62019-03-12 13:08:03 +0800436 * Action used to display a dialog that asks the user whether to stay connected to a network
437 * that has not validated. This intent is used to start the dialog in settings via
438 * startActivity.
439 *
440 * @hide
441 */
442 public static final String ACTION_PROMPT_PARTIAL_CONNECTIVITY =
443 "android.net.conn.PROMPT_PARTIAL_CONNECTIVITY";
444
445 /**
Jeremy Klein3dabcb92016-01-22 14:11:45 -0800446 * Invalid tethering type.
Chalard Jean9dd11612018-06-04 16:52:49 +0900447 * @see #startTethering(int, boolean, OnStartTetheringCallback)
Jeremy Klein3dabcb92016-01-22 14:11:45 -0800448 * @hide
449 */
markchiena0f8fd92019-12-25 19:40:32 +0800450 public static final int TETHERING_INVALID = TetheringManager.TETHERING_INVALID;
Jeremy Klein3dabcb92016-01-22 14:11:45 -0800451
452 /**
453 * Wifi tethering type.
Chalard Jean9dd11612018-06-04 16:52:49 +0900454 * @see #startTethering(int, boolean, OnStartTetheringCallback)
Jeremy Klein3dabcb92016-01-22 14:11:45 -0800455 * @hide
456 */
457 @SystemApi
Remi NGUYEN VAN09f8ed22021-02-15 18:52:06 +0900458 public static final int TETHERING_WIFI = 0;
Jeremy Klein3dabcb92016-01-22 14:11:45 -0800459
460 /**
461 * USB tethering type.
Chalard Jean9dd11612018-06-04 16:52:49 +0900462 * @see #startTethering(int, boolean, OnStartTetheringCallback)
Jeremy Klein3dabcb92016-01-22 14:11:45 -0800463 * @hide
464 */
465 @SystemApi
Remi NGUYEN VAN09f8ed22021-02-15 18:52:06 +0900466 public static final int TETHERING_USB = 1;
Jeremy Klein3dabcb92016-01-22 14:11:45 -0800467
468 /**
469 * Bluetooth tethering type.
Chalard Jean9dd11612018-06-04 16:52:49 +0900470 * @see #startTethering(int, boolean, OnStartTetheringCallback)
Jeremy Klein3dabcb92016-01-22 14:11:45 -0800471 * @hide
472 */
473 @SystemApi
Remi NGUYEN VAN09f8ed22021-02-15 18:52:06 +0900474 public static final int TETHERING_BLUETOOTH = 2;
Jeremy Klein3dabcb92016-01-22 14:11:45 -0800475
476 /**
Jimmy Chendd31bc42019-07-15 18:03:23 +0800477 * Wifi P2p tethering type.
478 * Wifi P2p tethering is set through events automatically, and don't
479 * need to start from #startTethering(int, boolean, OnStartTetheringCallback).
480 * @hide
481 */
markchiena0f8fd92019-12-25 19:40:32 +0800482 public static final int TETHERING_WIFI_P2P = TetheringManager.TETHERING_WIFI_P2P;
Jimmy Chendd31bc42019-07-15 18:03:23 +0800483
484 /**
Jeremy Klein3dabcb92016-01-22 14:11:45 -0800485 * Extra used for communicating with the TetherService. Includes the type of tethering to
486 * enable if any.
487 * @hide
488 */
markchien6ae63e52020-01-21 13:11:06 +0800489 public static final String EXTRA_ADD_TETHER_TYPE = TetheringConstants.EXTRA_ADD_TETHER_TYPE;
Jeremy Klein3dabcb92016-01-22 14:11:45 -0800490
491 /**
492 * Extra used for communicating with the TetherService. Includes the type of tethering for
493 * which to cancel provisioning.
494 * @hide
495 */
markchien6ae63e52020-01-21 13:11:06 +0800496 public static final String EXTRA_REM_TETHER_TYPE = TetheringConstants.EXTRA_REM_TETHER_TYPE;
Jeremy Klein3dabcb92016-01-22 14:11:45 -0800497
498 /**
499 * Extra used for communicating with the TetherService. True to schedule a recheck of tether
500 * provisioning.
501 * @hide
502 */
markchien6ae63e52020-01-21 13:11:06 +0800503 public static final String EXTRA_SET_ALARM = TetheringConstants.EXTRA_SET_ALARM;
Jeremy Klein3dabcb92016-01-22 14:11:45 -0800504
505 /**
506 * Tells the TetherService to run a provision check now.
507 * @hide
508 */
markchien6ae63e52020-01-21 13:11:06 +0800509 public static final String EXTRA_RUN_PROVISION = TetheringConstants.EXTRA_RUN_PROVISION;
Jeremy Klein3dabcb92016-01-22 14:11:45 -0800510
511 /**
512 * Extra used for communicating with the TetherService. Contains the {@link ResultReceiver}
513 * which will receive provisioning results. Can be left empty.
514 * @hide
515 */
markchien6ae63e52020-01-21 13:11:06 +0800516 public static final String EXTRA_PROVISION_CALLBACK =
517 TetheringConstants.EXTRA_PROVISION_CALLBACK;
Jeremy Klein3dabcb92016-01-22 14:11:45 -0800518
519 /**
Robert Greenwaltfaa4b402013-02-15 10:56:35 -0800520 * The absence of a connection type.
Robert Greenwalt33cdcdf2011-06-02 17:30:47 -0700521 * @hide
522 */
paulhu74357e72020-01-13 16:46:45 +0800523 @SystemApi
Robert Greenwalt33cdcdf2011-06-02 17:30:47 -0700524 public static final int TYPE_NONE = -1;
525
526 /**
Chalard Jean7eaf3b12018-03-08 13:54:53 +0900527 * A Mobile data connection. Devices may support more than one.
528 *
529 * @deprecated Applications should instead use {@link NetworkCapabilities#hasTransport} or
530 * {@link #requestNetwork(NetworkRequest, NetworkCallback)} to request an
531 * appropriate network. {@see NetworkCapabilities} for supported transports.
Robert Greenwalt2034b912009-08-12 16:08:25 -0700532 */
Chalard Jean7eaf3b12018-03-08 13:54:53 +0900533 @Deprecated
Robert Greenwalt2034b912009-08-12 16:08:25 -0700534 public static final int TYPE_MOBILE = 0;
Chalard Jean7eaf3b12018-03-08 13:54:53 +0900535
Robert Greenwalt2034b912009-08-12 16:08:25 -0700536 /**
Chalard Jean7eaf3b12018-03-08 13:54:53 +0900537 * A WIFI data connection. Devices may support more than one.
538 *
539 * @deprecated Applications should instead use {@link NetworkCapabilities#hasTransport} or
540 * {@link #requestNetwork(NetworkRequest, NetworkCallback)} to request an
541 * appropriate network. {@see NetworkCapabilities} for supported transports.
Robert Greenwalt2034b912009-08-12 16:08:25 -0700542 */
Chalard Jean7eaf3b12018-03-08 13:54:53 +0900543 @Deprecated
Robert Greenwalt2034b912009-08-12 16:08:25 -0700544 public static final int TYPE_WIFI = 1;
Chalard Jean7eaf3b12018-03-08 13:54:53 +0900545
Robert Greenwalt2034b912009-08-12 16:08:25 -0700546 /**
Robert Greenwaltfaa4b402013-02-15 10:56:35 -0800547 * An MMS-specific Mobile data connection. This network type may use the
548 * same network interface as {@link #TYPE_MOBILE} or it may use a different
549 * one. This is used by applications needing to talk to the carrier's
550 * Multimedia Messaging Service servers.
Lorenzo Colitti21e9a152015-04-23 15:32:42 +0900551 *
Chalard Jean7eaf3b12018-03-08 13:54:53 +0900552 * @deprecated Applications should instead use {@link NetworkCapabilities#hasCapability} or
Lorenzo Colitti7f6a2bf2015-04-24 17:03:31 +0900553 * {@link #requestNetwork(NetworkRequest, NetworkCallback)} to request a network that
Lorenzo Colitti21e9a152015-04-23 15:32:42 +0900554 * provides the {@link NetworkCapabilities#NET_CAPABILITY_MMS} capability.
Robert Greenwalt2034b912009-08-12 16:08:25 -0700555 */
Aurimas Liutikase80a0eb2016-05-24 15:22:55 -0700556 @Deprecated
Robert Greenwalt2034b912009-08-12 16:08:25 -0700557 public static final int TYPE_MOBILE_MMS = 2;
Chalard Jean7eaf3b12018-03-08 13:54:53 +0900558
Robert Greenwalt2034b912009-08-12 16:08:25 -0700559 /**
Robert Greenwaltfaa4b402013-02-15 10:56:35 -0800560 * A SUPL-specific Mobile data connection. This network type may use the
561 * same network interface as {@link #TYPE_MOBILE} or it may use a different
562 * one. This is used by applications needing to talk to the carrier's
563 * Secure User Plane Location servers for help locating the device.
Lorenzo Colitti21e9a152015-04-23 15:32:42 +0900564 *
Chalard Jean7eaf3b12018-03-08 13:54:53 +0900565 * @deprecated Applications should instead use {@link NetworkCapabilities#hasCapability} or
Lorenzo Colitti7f6a2bf2015-04-24 17:03:31 +0900566 * {@link #requestNetwork(NetworkRequest, NetworkCallback)} to request a network that
Lorenzo Colitti21e9a152015-04-23 15:32:42 +0900567 * provides the {@link NetworkCapabilities#NET_CAPABILITY_SUPL} capability.
Robert Greenwalt2034b912009-08-12 16:08:25 -0700568 */
Aurimas Liutikase80a0eb2016-05-24 15:22:55 -0700569 @Deprecated
Robert Greenwalt2034b912009-08-12 16:08:25 -0700570 public static final int TYPE_MOBILE_SUPL = 3;
Chalard Jean7eaf3b12018-03-08 13:54:53 +0900571
Robert Greenwalt2034b912009-08-12 16:08:25 -0700572 /**
Robert Greenwaltfaa4b402013-02-15 10:56:35 -0800573 * A DUN-specific Mobile data connection. This network type may use the
574 * same network interface as {@link #TYPE_MOBILE} or it may use a different
575 * one. This is sometimes by the system when setting up an upstream connection
576 * for tethering so that the carrier is aware of DUN traffic.
Chalard Jean7eaf3b12018-03-08 13:54:53 +0900577 *
578 * @deprecated Applications should instead use {@link NetworkCapabilities#hasCapability} or
579 * {@link #requestNetwork(NetworkRequest, NetworkCallback)} to request a network that
580 * provides the {@link NetworkCapabilities#NET_CAPABILITY_DUN} capability.
Robert Greenwalt2034b912009-08-12 16:08:25 -0700581 */
Chalard Jean7eaf3b12018-03-08 13:54:53 +0900582 @Deprecated
Robert Greenwalt2034b912009-08-12 16:08:25 -0700583 public static final int TYPE_MOBILE_DUN = 4;
Chalard Jean7eaf3b12018-03-08 13:54:53 +0900584
Robert Greenwalt2034b912009-08-12 16:08:25 -0700585 /**
Robert Greenwaltfaa4b402013-02-15 10:56:35 -0800586 * A High Priority Mobile data connection. This network type uses the
587 * same network interface as {@link #TYPE_MOBILE} but the routing setup
Lorenzo Colitti21e9a152015-04-23 15:32:42 +0900588 * is different.
589 *
Chalard Jean7eaf3b12018-03-08 13:54:53 +0900590 * @deprecated Applications should instead use {@link NetworkCapabilities#hasTransport} or
591 * {@link #requestNetwork(NetworkRequest, NetworkCallback)} to request an
592 * appropriate network. {@see NetworkCapabilities} for supported transports.
Robert Greenwalt2034b912009-08-12 16:08:25 -0700593 */
Aurimas Liutikase80a0eb2016-05-24 15:22:55 -0700594 @Deprecated
Robert Greenwalt2034b912009-08-12 16:08:25 -0700595 public static final int TYPE_MOBILE_HIPRI = 5;
Chalard Jean7eaf3b12018-03-08 13:54:53 +0900596
jshbfa81722010-03-11 15:04:43 -0800597 /**
Chalard Jean7eaf3b12018-03-08 13:54:53 +0900598 * A WiMAX data connection.
599 *
600 * @deprecated Applications should instead use {@link NetworkCapabilities#hasTransport} or
601 * {@link #requestNetwork(NetworkRequest, NetworkCallback)} to request an
602 * appropriate network. {@see NetworkCapabilities} for supported transports.
jshbfa81722010-03-11 15:04:43 -0800603 */
Chalard Jean7eaf3b12018-03-08 13:54:53 +0900604 @Deprecated
jshbfa81722010-03-11 15:04:43 -0800605 public static final int TYPE_WIMAX = 6;
Robert Greenwalteb123ac2010-12-06 13:56:24 -0800606
Jaikumar Ganesh0db51a02010-12-21 22:31:44 -0800607 /**
Chalard Jean7eaf3b12018-03-08 13:54:53 +0900608 * A Bluetooth data connection.
609 *
610 * @deprecated Applications should instead use {@link NetworkCapabilities#hasTransport} or
611 * {@link #requestNetwork(NetworkRequest, NetworkCallback)} to request an
612 * appropriate network. {@see NetworkCapabilities} for supported transports.
Jaikumar Ganesh0db51a02010-12-21 22:31:44 -0800613 */
Chalard Jean7eaf3b12018-03-08 13:54:53 +0900614 @Deprecated
Jaikumar Ganesh0db51a02010-12-21 22:31:44 -0800615 public static final int TYPE_BLUETOOTH = 7;
616
Robert Greenwaltf76c55d2011-04-22 15:28:18 -0700617 /**
Chiachang Wang3b9549f2020-07-28 13:53:09 +0800618 * Fake data connection. This should not be used on shipping devices.
Chalard Jean7eaf3b12018-03-08 13:54:53 +0900619 * @deprecated This is not used any more.
Robert Greenwaltf76c55d2011-04-22 15:28:18 -0700620 */
Chalard Jean7eaf3b12018-03-08 13:54:53 +0900621 @Deprecated
Jaikumar Ganesh0db51a02010-12-21 22:31:44 -0800622 public static final int TYPE_DUMMY = 8;
Wink Savilleb7c92c72011-03-12 14:52:01 -0800623
Robert Greenwaltf76c55d2011-04-22 15:28:18 -0700624 /**
Chalard Jean7eaf3b12018-03-08 13:54:53 +0900625 * An Ethernet data connection.
626 *
627 * @deprecated Applications should instead use {@link NetworkCapabilities#hasTransport} or
628 * {@link #requestNetwork(NetworkRequest, NetworkCallback)} to request an
629 * appropriate network. {@see NetworkCapabilities} for supported transports.
Robert Greenwaltf76c55d2011-04-22 15:28:18 -0700630 */
Chalard Jean7eaf3b12018-03-08 13:54:53 +0900631 @Deprecated
Robert Greenwalt9d077812011-01-28 14:48:37 -0800632 public static final int TYPE_ETHERNET = 9;
Robert Greenwaltf76c55d2011-04-22 15:28:18 -0700633
Wink Savilleb7c92c72011-03-12 14:52:01 -0800634 /**
Robert Greenwaltfaa4b402013-02-15 10:56:35 -0800635 * Over the air Administration.
Chalard Jean7eaf3b12018-03-08 13:54:53 +0900636 * @deprecated Use {@link NetworkCapabilities} instead.
Wink Savilleb7c92c72011-03-12 14:52:01 -0800637 * {@hide}
638 */
Chalard Jean7eaf3b12018-03-08 13:54:53 +0900639 @Deprecated
Chalard Jeana3b77512019-04-09 15:46:21 +0900640 @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P, trackingBug = 130143562)
Wink Savilleb7c92c72011-03-12 14:52:01 -0800641 public static final int TYPE_MOBILE_FOTA = 10;
642
643 /**
Robert Greenwaltfaa4b402013-02-15 10:56:35 -0800644 * IP Multimedia Subsystem.
Chalard Jean7eaf3b12018-03-08 13:54:53 +0900645 * @deprecated Use {@link NetworkCapabilities#NET_CAPABILITY_IMS} instead.
Wink Savilleb7c92c72011-03-12 14:52:01 -0800646 * {@hide}
647 */
Chalard Jean7eaf3b12018-03-08 13:54:53 +0900648 @Deprecated
Mathew Inwoodc5b9bec2018-08-08 14:52:47 +0100649 @UnsupportedAppUsage
Wink Savilleb7c92c72011-03-12 14:52:01 -0800650 public static final int TYPE_MOBILE_IMS = 11;
651
652 /**
Robert Greenwaltfaa4b402013-02-15 10:56:35 -0800653 * Carrier Branded Services.
Chalard Jean7eaf3b12018-03-08 13:54:53 +0900654 * @deprecated Use {@link NetworkCapabilities#NET_CAPABILITY_CBS} instead.
Wink Savilleb7c92c72011-03-12 14:52:01 -0800655 * {@hide}
656 */
Chalard Jean7eaf3b12018-03-08 13:54:53 +0900657 @Deprecated
Chalard Jeana3b77512019-04-09 15:46:21 +0900658 @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P, trackingBug = 130143562)
Wink Savilleb7c92c72011-03-12 14:52:01 -0800659 public static final int TYPE_MOBILE_CBS = 12;
660
repo syncf5de5572011-07-29 23:55:49 -0700661 /**
662 * A Wi-Fi p2p connection. Only requesting processes will have access to
663 * the peers connected.
Chalard Jean7eaf3b12018-03-08 13:54:53 +0900664 * @deprecated Use {@link NetworkCapabilities#NET_CAPABILITY_WIFI_P2P} instead.
repo syncf5de5572011-07-29 23:55:49 -0700665 * {@hide}
666 */
Chalard Jean7eaf3b12018-03-08 13:54:53 +0900667 @Deprecated
paulhue102b0b2020-01-15 15:38:23 +0800668 @SystemApi
repo syncf5de5572011-07-29 23:55:49 -0700669 public static final int TYPE_WIFI_P2P = 13;
Wink Savilleb7c92c72011-03-12 14:52:01 -0800670
Wink Saville512c2202013-07-29 15:00:57 -0700671 /**
672 * The network to use for initially attaching to the network
Chalard Jean7eaf3b12018-03-08 13:54:53 +0900673 * @deprecated Use {@link NetworkCapabilities#NET_CAPABILITY_IA} instead.
Wink Saville512c2202013-07-29 15:00:57 -0700674 * {@hide}
675 */
Chalard Jean7eaf3b12018-03-08 13:54:53 +0900676 @Deprecated
Mathew Inwoodc5b9bec2018-08-08 14:52:47 +0100677 @UnsupportedAppUsage
Wink Saville512c2202013-07-29 15:00:57 -0700678 public static final int TYPE_MOBILE_IA = 14;
repo syncf5de5572011-07-29 23:55:49 -0700679
Lorenzo Colitti21e9a152015-04-23 15:32:42 +0900680 /**
Robert Greenwaltb1f7f752015-07-09 14:49:35 -0700681 * Emergency PDN connection for emergency services. This
682 * may include IMS and MMS in emergency situations.
Chalard Jean7eaf3b12018-03-08 13:54:53 +0900683 * @deprecated Use {@link NetworkCapabilities#NET_CAPABILITY_EIMS} instead.
Ram693d07a2014-06-26 11:03:44 -0700684 * {@hide}
685 */
Chalard Jean7eaf3b12018-03-08 13:54:53 +0900686 @Deprecated
Chalard Jeana3b77512019-04-09 15:46:21 +0900687 @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P, trackingBug = 130143562)
Ram693d07a2014-06-26 11:03:44 -0700688 public static final int TYPE_MOBILE_EMERGENCY = 15;
689
Hui Lu865b70d2014-01-15 11:05:36 -0500690 /**
691 * The network that uses proxy to achieve connectivity.
Chalard Jean7eaf3b12018-03-08 13:54:53 +0900692 * @deprecated Use {@link NetworkCapabilities} instead.
Hui Lu865b70d2014-01-15 11:05:36 -0500693 * {@hide}
694 */
Chalard Jean7eaf3b12018-03-08 13:54:53 +0900695 @Deprecated
Remi NGUYEN VANee660cf2020-11-30 19:23:45 +0900696 @SystemApi
Hui Lu865b70d2014-01-15 11:05:36 -0500697 public static final int TYPE_PROXY = 16;
Wink Saville512c2202013-07-29 15:00:57 -0700698
Robert Greenwaltce7a1442014-07-07 17:09:01 -0700699 /**
700 * A virtual network using one or more native bearers.
701 * It may or may not be providing security services.
Chalard Jean7eaf3b12018-03-08 13:54:53 +0900702 * @deprecated Applications should use {@link NetworkCapabilities#TRANSPORT_VPN} instead.
Robert Greenwaltce7a1442014-07-07 17:09:01 -0700703 */
Chalard Jean7eaf3b12018-03-08 13:54:53 +0900704 @Deprecated
Robert Greenwaltce7a1442014-07-07 17:09:01 -0700705 public static final int TYPE_VPN = 17;
Hui Lu865b70d2014-01-15 11:05:36 -0500706
Benedict Wongbdfaa482018-11-14 17:40:55 -0800707 /**
708 * A network that is exclusively meant to be used for testing
709 *
710 * @deprecated Use {@link NetworkCapabilities} instead.
711 * @hide
712 */
713 @Deprecated
714 public static final int TYPE_TEST = 18; // TODO: Remove this once NetworkTypes are unused.
Robert Greenwaltce7a1442014-07-07 17:09:01 -0700715
Chalard Jeanaea539a2020-03-25 01:24:04 +0900716 /**
717 * @deprecated Use {@link NetworkCapabilities} instead.
718 * @hide
719 */
720 @Deprecated
721 @Retention(RetentionPolicy.SOURCE)
722 @IntDef(prefix = { "TYPE_" }, value = {
723 TYPE_NONE,
724 TYPE_MOBILE,
725 TYPE_WIFI,
726 TYPE_MOBILE_MMS,
727 TYPE_MOBILE_SUPL,
728 TYPE_MOBILE_DUN,
729 TYPE_MOBILE_HIPRI,
730 TYPE_WIMAX,
731 TYPE_BLUETOOTH,
732 TYPE_DUMMY,
733 TYPE_ETHERNET,
734 TYPE_MOBILE_FOTA,
735 TYPE_MOBILE_IMS,
736 TYPE_MOBILE_CBS,
737 TYPE_WIFI_P2P,
738 TYPE_MOBILE_IA,
739 TYPE_MOBILE_EMERGENCY,
740 TYPE_PROXY,
741 TYPE_VPN,
742 TYPE_TEST
743 })
744 public @interface LegacyNetworkType {}
745
Chalard Jeanafaed1a2019-11-21 14:48:00 +0900746 // Deprecated constants for return values of startUsingNetworkFeature. They used to live
747 // in com.android.internal.telephony.PhoneConstants until they were made inaccessible.
748 private static final int DEPRECATED_PHONE_CONSTANT_APN_ALREADY_ACTIVE = 0;
749 private static final int DEPRECATED_PHONE_CONSTANT_APN_REQUEST_STARTED = 1;
750 private static final int DEPRECATED_PHONE_CONSTANT_APN_REQUEST_FAILED = 3;
751
Robert Greenwaltce7a1442014-07-07 17:09:01 -0700752 /** {@hide} */
Benedict Wongbdfaa482018-11-14 17:40:55 -0800753 public static final int MAX_RADIO_TYPE = TYPE_TEST;
754
755 /** {@hide} */
756 public static final int MAX_NETWORK_TYPE = TYPE_TEST;
The Android Open Source Project28527d22009-03-03 19:31:44 -0800757
Hugo Benichiad353f42017-06-20 14:07:59 +0900758 private static final int MIN_NETWORK_TYPE = TYPE_MOBILE;
759
Jianzheng Zhou028d2032012-11-16 13:45:20 +0800760 /**
761 * If you want to set the default network preference,you can directly
762 * change the networkAttributes array in framework's config.xml.
763 *
764 * @deprecated Since we support so many more networks now, the single
765 * network default network preference can't really express
Robert Greenwaltfaa4b402013-02-15 10:56:35 -0800766 * the hierarchy. Instead, the default is defined by the
Jianzheng Zhou028d2032012-11-16 13:45:20 +0800767 * networkAttributes in config.xml. You can determine
Robert Greenwaltf909cb12012-12-07 09:56:50 -0800768 * the current value by calling {@link #getNetworkPreference()}
Jianzheng Zhou028d2032012-11-16 13:45:20 +0800769 * from an App.
770 */
771 @Deprecated
The Android Open Source Project28527d22009-03-03 19:31:44 -0800772 public static final int DEFAULT_NETWORK_PREFERENCE = TYPE_WIFI;
773
Jeff Sharkey8c870452012-09-26 22:03:49 -0700774 /**
Robert Greenwalt42a0e1e2014-03-19 17:56:12 -0700775 * @hide
776 */
Hugo Benichi7ab07a32017-06-20 14:10:14 +0900777 public static final int REQUEST_ID_UNSET = 0;
Robert Greenwaltbfd1f4c2014-06-08 16:42:59 -0700778
Paul Jensen5dea4352014-07-11 12:28:19 -0400779 /**
Hugo Benichibee30fe2017-06-17 13:14:12 +0900780 * Static unique request used as a tombstone for NetworkCallbacks that have been unregistered.
781 * This allows to distinguish when unregistering NetworkCallbacks those that were never
Chalard Jean9dd11612018-06-04 16:52:49 +0900782 * registered from those that were already unregistered.
Hugo Benichibee30fe2017-06-17 13:14:12 +0900783 * @hide
784 */
Hugo Benichi7ab07a32017-06-20 14:10:14 +0900785 private static final NetworkRequest ALREADY_UNREGISTERED =
Hugo Benichibee30fe2017-06-17 13:14:12 +0900786 new NetworkRequest.Builder().clearCapabilities().build();
787
788 /**
Paul Jensen5dea4352014-07-11 12:28:19 -0400789 * A NetID indicating no Network is selected.
790 * Keep in sync with bionic/libc/dns/include/resolv_netid.h
791 * @hide
792 */
793 public static final int NETID_UNSET = 0;
794
Erik Klineb0be3e62017-10-30 15:29:44 +0900795 /**
796 * Private DNS Mode values.
797 *
798 * The "private_dns_mode" global setting stores a String value which is
799 * expected to be one of the following.
800 */
801
802 /**
803 * @hide
804 */
805 public static final String PRIVATE_DNS_MODE_OFF = "off";
806 /**
807 * @hide
808 */
809 public static final String PRIVATE_DNS_MODE_OPPORTUNISTIC = "opportunistic";
810 /**
811 * @hide
812 */
813 public static final String PRIVATE_DNS_MODE_PROVIDER_HOSTNAME = "hostname";
814 /**
815 * The default Private DNS mode.
816 *
817 * This may change from release to release or may become dependent upon
818 * the capabilities of the underlying platform.
819 *
820 * @hide
821 */
Erik Klinea7edf6f2018-05-16 16:41:57 +0900822 public static final String PRIVATE_DNS_DEFAULT_MODE_FALLBACK = PRIVATE_DNS_MODE_OPPORTUNISTIC;
Erik Klineb0be3e62017-10-30 15:29:44 +0900823
Chalard Jeana3b77512019-04-09 15:46:21 +0900824 @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P, trackingBug = 130143562)
Jeff Sharkey921ebf22011-05-19 17:12:49 -0700825 private final IConnectivityManager mService;
Lorenzo Colitticd675292021-02-04 17:32:07 +0900826
Paul Jensenc0618a62014-12-10 15:12:18 -0500827 /**
828 * A kludge to facilitate static access where a Context pointer isn't available, like in the
829 * case of the static set/getProcessDefaultNetwork methods and from the Network class.
830 * TODO: Remove this after deprecating the static methods in favor of non-static methods or
831 * methods that take a Context argument.
832 */
833 private static ConnectivityManager sInstance;
The Android Open Source Project28527d22009-03-03 19:31:44 -0800834
Lorenzo Colitti2780e4b2015-07-29 11:41:21 +0900835 private final Context mContext;
836
Felipe Leme30511352016-01-22 09:44:57 -0800837 private INetworkPolicyManager mNPManager;
Amos Bianchia9b415a2020-03-04 11:07:38 -0800838 private final TetheringManager mTetheringManager;
Dianne Hackborn5ac88162014-02-26 16:20:52 -0800839
Robert Greenwaltfaa4b402013-02-15 10:56:35 -0800840 /**
841 * Tests if a given integer represents a valid network type.
842 * @param networkType the type to be tested
843 * @return a boolean. {@code true} if the type is valid, else {@code false}
Paul Jensenbbb61092015-05-06 10:42:25 -0400844 * @deprecated All APIs accepting a network type are deprecated. There should be no need to
845 * validate a network type.
Robert Greenwaltfaa4b402013-02-15 10:56:35 -0800846 */
Aurimas Liutikase80a0eb2016-05-24 15:22:55 -0700847 @Deprecated
Jeff Sharkey21062e72011-05-28 20:56:34 -0700848 public static boolean isNetworkTypeValid(int networkType) {
Hugo Benichiad353f42017-06-20 14:07:59 +0900849 return MIN_NETWORK_TYPE <= networkType && networkType <= MAX_NETWORK_TYPE;
The Android Open Source Project28527d22009-03-03 19:31:44 -0800850 }
851
Robert Greenwaltfaa4b402013-02-15 10:56:35 -0800852 /**
853 * Returns a non-localized string representing a given network type.
854 * ONLY used for debugging output.
855 * @param type the type needing naming
856 * @return a String for the given type, or a string version of the type ("87")
857 * if no name is known.
Chalard Jean7eaf3b12018-03-08 13:54:53 +0900858 * @deprecated Types are deprecated. Use {@link NetworkCapabilities} instead.
Robert Greenwaltfaa4b402013-02-15 10:56:35 -0800859 * {@hide}
860 */
Chalard Jean7eaf3b12018-03-08 13:54:53 +0900861 @Deprecated
Mathew Inwoode1a17ba2020-11-04 09:29:36 +0000862 @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.R, trackingBug = 170729553)
Jeff Sharkey21062e72011-05-28 20:56:34 -0700863 public static String getNetworkTypeName(int type) {
864 switch (type) {
Hugo Benichiad353f42017-06-20 14:07:59 +0900865 case TYPE_NONE:
866 return "NONE";
Jeff Sharkey21062e72011-05-28 20:56:34 -0700867 case TYPE_MOBILE:
868 return "MOBILE";
869 case TYPE_WIFI:
870 return "WIFI";
871 case TYPE_MOBILE_MMS:
872 return "MOBILE_MMS";
873 case TYPE_MOBILE_SUPL:
874 return "MOBILE_SUPL";
875 case TYPE_MOBILE_DUN:
876 return "MOBILE_DUN";
877 case TYPE_MOBILE_HIPRI:
878 return "MOBILE_HIPRI";
879 case TYPE_WIMAX:
880 return "WIMAX";
881 case TYPE_BLUETOOTH:
882 return "BLUETOOTH";
883 case TYPE_DUMMY:
884 return "DUMMY";
885 case TYPE_ETHERNET:
886 return "ETHERNET";
887 case TYPE_MOBILE_FOTA:
888 return "MOBILE_FOTA";
889 case TYPE_MOBILE_IMS:
890 return "MOBILE_IMS";
891 case TYPE_MOBILE_CBS:
892 return "MOBILE_CBS";
repo syncf5de5572011-07-29 23:55:49 -0700893 case TYPE_WIFI_P2P:
894 return "WIFI_P2P";
Wink Saville512c2202013-07-29 15:00:57 -0700895 case TYPE_MOBILE_IA:
896 return "MOBILE_IA";
Ram693d07a2014-06-26 11:03:44 -0700897 case TYPE_MOBILE_EMERGENCY:
898 return "MOBILE_EMERGENCY";
Hui Lu865b70d2014-01-15 11:05:36 -0500899 case TYPE_PROXY:
900 return "PROXY";
Erik Kline137fadc2014-11-19 17:23:41 +0900901 case TYPE_VPN:
902 return "VPN";
Jeff Sharkey21062e72011-05-28 20:56:34 -0700903 default:
904 return Integer.toString(type);
905 }
906 }
907
Robert Greenwaltfaa4b402013-02-15 10:56:35 -0800908 /**
Aaron Huang96011892020-06-27 07:18:23 +0800909 * @hide
910 * TODO: Expose for SystemServer when becomes a module.
911 */
912 public void systemReady() {
913 try {
914 mService.systemReady();
915 } catch (RemoteException e) {
916 throw e.rethrowFromSystemServer();
917 }
918 }
919
920 /**
Robert Greenwaltfaa4b402013-02-15 10:56:35 -0800921 * Checks if a given type uses the cellular data connection.
922 * This should be replaced in the future by a network property.
923 * @param networkType the type to check
924 * @return a boolean - {@code true} if uses cellular network, else {@code false}
Chalard Jean7eaf3b12018-03-08 13:54:53 +0900925 * @deprecated Types are deprecated. Use {@link NetworkCapabilities} instead.
Robert Greenwaltfaa4b402013-02-15 10:56:35 -0800926 * {@hide}
927 */
Chalard Jean7eaf3b12018-03-08 13:54:53 +0900928 @Deprecated
Chalard Jeana3b77512019-04-09 15:46:21 +0900929 @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P, trackingBug = 130143562)
Jeff Sharkey21062e72011-05-28 20:56:34 -0700930 public static boolean isNetworkTypeMobile(int networkType) {
931 switch (networkType) {
932 case TYPE_MOBILE:
933 case TYPE_MOBILE_MMS:
934 case TYPE_MOBILE_SUPL:
935 case TYPE_MOBILE_DUN:
936 case TYPE_MOBILE_HIPRI:
937 case TYPE_MOBILE_FOTA:
938 case TYPE_MOBILE_IMS:
939 case TYPE_MOBILE_CBS:
Wink Saville512c2202013-07-29 15:00:57 -0700940 case TYPE_MOBILE_IA:
Ram693d07a2014-06-26 11:03:44 -0700941 case TYPE_MOBILE_EMERGENCY:
Jeff Sharkey21062e72011-05-28 20:56:34 -0700942 return true;
943 default:
944 return false;
945 }
946 }
947
Robert Greenwaltfaa4b402013-02-15 10:56:35 -0800948 /**
Jeff Sharkey79f3e022013-06-04 12:29:00 -0700949 * Checks if the given network type is backed by a Wi-Fi radio.
950 *
Chalard Jean7eaf3b12018-03-08 13:54:53 +0900951 * @deprecated Types are deprecated. Use {@link NetworkCapabilities} instead.
Jeff Sharkey79f3e022013-06-04 12:29:00 -0700952 * @hide
953 */
Chalard Jean7eaf3b12018-03-08 13:54:53 +0900954 @Deprecated
Jeff Sharkey79f3e022013-06-04 12:29:00 -0700955 public static boolean isNetworkTypeWifi(int networkType) {
956 switch (networkType) {
957 case TYPE_WIFI:
958 case TYPE_WIFI_P2P:
959 return true;
960 default:
961 return false;
962 }
963 }
964
965 /**
Robert Greenwaltfaa4b402013-02-15 10:56:35 -0800966 * Specifies the preferred network type. When the device has more
967 * than one type available the preferred network type will be used.
Robert Greenwaltfaa4b402013-02-15 10:56:35 -0800968 *
969 * @param preference the network type to prefer over all others. It is
970 * unspecified what happens to the old preferred network in the
971 * overall ordering.
Robert Greenwaltf3017f72014-05-18 23:07:25 -0700972 * @deprecated Functionality has been removed as it no longer makes sense,
973 * with many more than two networks - we'd need an array to express
974 * preference. Instead we use dynamic network properties of
975 * the networks to describe their precedence.
Robert Greenwaltfaa4b402013-02-15 10:56:35 -0800976 */
Aurimas Liutikase80a0eb2016-05-24 15:22:55 -0700977 @Deprecated
The Android Open Source Project28527d22009-03-03 19:31:44 -0800978 public void setNetworkPreference(int preference) {
The Android Open Source Project28527d22009-03-03 19:31:44 -0800979 }
980
Robert Greenwaltfaa4b402013-02-15 10:56:35 -0800981 /**
982 * Retrieves the current preferred network type.
Robert Greenwaltfaa4b402013-02-15 10:56:35 -0800983 *
984 * @return an integer representing the preferred network type
985 *
Robert Greenwaltf3017f72014-05-18 23:07:25 -0700986 * @deprecated Functionality has been removed as it no longer makes sense,
987 * with many more than two networks - we'd need an array to express
988 * preference. Instead we use dynamic network properties of
989 * the networks to describe their precedence.
Robert Greenwaltfaa4b402013-02-15 10:56:35 -0800990 */
Aurimas Liutikase80a0eb2016-05-24 15:22:55 -0700991 @Deprecated
Jeff Sharkey656584a2017-04-24 11:18:03 -0600992 @RequiresPermission(android.Manifest.permission.ACCESS_NETWORK_STATE)
The Android Open Source Project28527d22009-03-03 19:31:44 -0800993 public int getNetworkPreference() {
Robert Greenwaltf3017f72014-05-18 23:07:25 -0700994 return TYPE_NONE;
The Android Open Source Project28527d22009-03-03 19:31:44 -0800995 }
996
Scott Mainf58b7d82011-10-06 19:02:28 -0700997 /**
Robert Greenwaltfaa4b402013-02-15 10:56:35 -0800998 * Returns details about the currently active default data network. When
999 * connected, this network is the default route for outgoing connections.
1000 * You should always check {@link NetworkInfo#isConnected()} before initiating
1001 * network traffic. This may return {@code null} when there is no default
1002 * network.
Chalard Jean96d10a72018-03-29 17:45:24 +09001003 * Note that if the default network is a VPN, this method will return the
1004 * NetworkInfo for one of its underlying networks instead, or null if the
1005 * VPN agent did not specify any. Apps interested in learning about VPNs
1006 * should use {@link #getNetworkInfo(android.net.Network)} instead.
Robert Greenwaltfaa4b402013-02-15 10:56:35 -08001007 *
1008 * @return a {@link NetworkInfo} object for the current default network
Paul Jensenbd2d3782015-02-13 14:18:39 -05001009 * or {@code null} if no default network is currently active
junyulai9826e7f2018-12-13 12:47:51 +08001010 * @deprecated See {@link NetworkInfo}.
Jeff Sharkeyd00b1302012-04-12 18:34:54 -07001011 */
junyulai9826e7f2018-12-13 12:47:51 +08001012 @Deprecated
Jeff Sharkey656584a2017-04-24 11:18:03 -06001013 @RequiresPermission(android.Manifest.permission.ACCESS_NETWORK_STATE)
Chalard Jean158702d2019-01-07 19:26:34 +09001014 @Nullable
The Android Open Source Project28527d22009-03-03 19:31:44 -08001015 public NetworkInfo getActiveNetworkInfo() {
1016 try {
1017 return mService.getActiveNetworkInfo();
1018 } catch (RemoteException e) {
Jeff Sharkeyc78f85c2016-03-01 19:27:23 -07001019 throw e.rethrowFromSystemServer();
The Android Open Source Project28527d22009-03-03 19:31:44 -08001020 }
1021 }
1022
Robert Greenwaltfaa4b402013-02-15 10:56:35 -08001023 /**
Paul Jensen1f567382015-02-13 14:18:39 -05001024 * Returns a {@link Network} object corresponding to the currently active
1025 * default data network. In the event that the current active default data
1026 * network disconnects, the returned {@code Network} object will no longer
1027 * be usable. This will return {@code null} when there is no default
1028 * network.
1029 *
1030 * @return a {@link Network} object for the current default network or
1031 * {@code null} if no default network is currently active
Paul Jensen1f567382015-02-13 14:18:39 -05001032 */
Jeff Sharkey656584a2017-04-24 11:18:03 -06001033 @RequiresPermission(android.Manifest.permission.ACCESS_NETWORK_STATE)
Chalard Jean158702d2019-01-07 19:26:34 +09001034 @Nullable
Paul Jensen1f567382015-02-13 14:18:39 -05001035 public Network getActiveNetwork() {
1036 try {
1037 return mService.getActiveNetwork();
1038 } catch (RemoteException e) {
Jeff Sharkeyc78f85c2016-03-01 19:27:23 -07001039 throw e.rethrowFromSystemServer();
Paul Jensen1f567382015-02-13 14:18:39 -05001040 }
1041 }
1042
1043 /**
Robin Lee5b52bef2016-03-24 12:07:00 +00001044 * Returns a {@link Network} object corresponding to the currently active
1045 * default data network for a specific UID. In the event that the default data
1046 * network disconnects, the returned {@code Network} object will no longer
1047 * be usable. This will return {@code null} when there is no default
1048 * network for the UID.
Robin Lee5b52bef2016-03-24 12:07:00 +00001049 *
1050 * @return a {@link Network} object for the current default network for the
1051 * given UID or {@code null} if no default network is currently active
1052 *
1053 * @hide
1054 */
paulhu8e96a752019-08-12 16:25:11 +08001055 @RequiresPermission(android.Manifest.permission.NETWORK_STACK)
Chalard Jean158702d2019-01-07 19:26:34 +09001056 @Nullable
Robin Lee5b52bef2016-03-24 12:07:00 +00001057 public Network getActiveNetworkForUid(int uid) {
Jeff Sharkey7dbf83d2016-04-28 15:33:18 -06001058 return getActiveNetworkForUid(uid, false);
1059 }
1060
1061 /** {@hide} */
1062 public Network getActiveNetworkForUid(int uid, boolean ignoreBlocked) {
Robin Lee5b52bef2016-03-24 12:07:00 +00001063 try {
Jeff Sharkey7dbf83d2016-04-28 15:33:18 -06001064 return mService.getActiveNetworkForUid(uid, ignoreBlocked);
Robin Lee5b52bef2016-03-24 12:07:00 +00001065 } catch (RemoteException e) {
1066 throw e.rethrowFromSystemServer();
1067 }
1068 }
1069
1070 /**
Lorenzo Colitti3f54f102020-12-12 00:51:11 +09001071 * Adds or removes a requirement for given UID ranges to use the VPN.
1072 *
1073 * If set to {@code true}, informs the system that the UIDs in the specified ranges must not
1074 * have any connectivity except if a VPN is connected and applies to the UIDs, or if the UIDs
1075 * otherwise have permission to bypass the VPN (e.g., because they have the
1076 * {@link android.Manifest.permission.CONNECTIVITY_USE_RESTRICTED_NETWORKS} permission, or when
1077 * using a socket protected by a method such as {@link VpnService#protect(DatagramSocket)}. If
1078 * set to {@code false}, a previously-added restriction is removed.
1079 * <p>
1080 * Each of the UID ranges specified by this method is added and removed as is, and no processing
1081 * is performed on the ranges to de-duplicate, merge, split, or intersect them. In order to
1082 * remove a previously-added range, the exact range must be removed as is.
1083 * <p>
1084 * The changes are applied asynchronously and may not have been applied by the time the method
1085 * returns. Apps will be notified about any changes that apply to them via
1086 * {@link NetworkCallback#onBlockedStatusChanged} callbacks called after the changes take
1087 * effect.
1088 * <p>
1089 * This method should be called only by the VPN code.
1090 *
1091 * @param ranges the UID ranges to restrict
1092 * @param requireVpn whether the specified UID ranges must use a VPN
1093 *
1094 * TODO: expose as @SystemApi.
1095 * @hide
1096 */
1097 @RequiresPermission(anyOf = {
1098 NetworkStack.PERMISSION_MAINLINE_NETWORK_STACK,
1099 android.Manifest.permission.NETWORK_STACK})
1100 public void setRequireVpnForUids(boolean requireVpn,
1101 @NonNull Collection<Range<Integer>> ranges) {
1102 Objects.requireNonNull(ranges);
1103 // The Range class is not parcelable. Convert to UidRange, which is what is used internally.
1104 // This method is not necessarily expected to be used outside the system server, so
1105 // parceling may not be necessary, but it could be used out-of-process, e.g., by the network
1106 // stack process, or by tests.
1107 UidRange[] rangesArray = new UidRange[ranges.size()];
1108 int index = 0;
1109 for (Range<Integer> range : ranges) {
1110 rangesArray[index++] = new UidRange(range.getLower(), range.getUpper());
1111 }
1112 try {
1113 mService.setRequireVpnForUids(requireVpn, rangesArray);
1114 } catch (RemoteException e) {
1115 throw e.rethrowFromSystemServer();
1116 }
1117 }
1118
1119 /**
Lorenzo Colittibcd692f2021-01-15 01:29:01 +09001120 * Informs ConnectivityService of whether the legacy lockdown VPN, as implemented by
1121 * LockdownVpnTracker, is in use. This is deprecated for new devices starting from Android 12
1122 * but is still supported for backwards compatibility.
1123 * <p>
1124 * This type of VPN is assumed always to use the system default network, and must always declare
1125 * exactly one underlying network, which is the network that was the default when the VPN
1126 * connected.
1127 * <p>
1128 * Calling this method with {@code true} enables legacy behaviour, specifically:
1129 * <ul>
1130 * <li>Any VPN that applies to userId 0 behaves specially with respect to deprecated
1131 * {@link #CONNECTIVITY_ACTION} broadcasts. Any such broadcasts will have the state in the
1132 * {@link #EXTRA_NETWORK_INFO} replaced by state of the VPN network. Also, any time the VPN
1133 * connects, a {@link #CONNECTIVITY_ACTION} broadcast will be sent for the network
1134 * underlying the VPN.</li>
1135 * <li>Deprecated APIs that return {@link NetworkInfo} objects will have their state
1136 * similarly replaced by the VPN network state.</li>
1137 * <li>Information on current network interfaces passed to NetworkStatsService will not
1138 * include any VPN interfaces.</li>
1139 * </ul>
1140 *
1141 * @param enabled whether legacy lockdown VPN is enabled or disabled
1142 *
1143 * TODO: @SystemApi(client = MODULE_LIBRARIES)
1144 *
1145 * @hide
1146 */
1147 @RequiresPermission(anyOf = {
1148 NetworkStack.PERMISSION_MAINLINE_NETWORK_STACK,
1149 android.Manifest.permission.NETWORK_SETTINGS})
1150 public void setLegacyLockdownVpnEnabled(boolean enabled) {
1151 try {
1152 mService.setLegacyLockdownVpnEnabled(enabled);
1153 } catch (RemoteException e) {
1154 throw e.rethrowFromSystemServer();
1155 }
1156 }
1157
1158 /**
Robert Greenwaltfaa4b402013-02-15 10:56:35 -08001159 * Returns details about the currently active default data network
1160 * for a given uid. This is for internal use only to avoid spying
1161 * other apps.
1162 *
1163 * @return a {@link NetworkInfo} object for the current default network
1164 * for the given uid or {@code null} if no default network is
1165 * available for the specified uid.
1166 *
Robert Greenwaltfaa4b402013-02-15 10:56:35 -08001167 * {@hide}
1168 */
paulhu8e96a752019-08-12 16:25:11 +08001169 @RequiresPermission(android.Manifest.permission.NETWORK_STACK)
Mathew Inwoode1a17ba2020-11-04 09:29:36 +00001170 @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.R, trackingBug = 170729553)
Jeff Sharkey921ebf22011-05-19 17:12:49 -07001171 public NetworkInfo getActiveNetworkInfoForUid(int uid) {
Jeff Sharkey7dbf83d2016-04-28 15:33:18 -06001172 return getActiveNetworkInfoForUid(uid, false);
1173 }
1174
1175 /** {@hide} */
1176 public NetworkInfo getActiveNetworkInfoForUid(int uid, boolean ignoreBlocked) {
Jeff Sharkey921ebf22011-05-19 17:12:49 -07001177 try {
Jeff Sharkey7dbf83d2016-04-28 15:33:18 -06001178 return mService.getActiveNetworkInfoForUid(uid, ignoreBlocked);
Jeff Sharkey921ebf22011-05-19 17:12:49 -07001179 } catch (RemoteException e) {
Jeff Sharkeyc78f85c2016-03-01 19:27:23 -07001180 throw e.rethrowFromSystemServer();
Jeff Sharkey921ebf22011-05-19 17:12:49 -07001181 }
1182 }
1183
Robert Greenwaltfaa4b402013-02-15 10:56:35 -08001184 /**
1185 * Returns connection status information about a particular
1186 * network type.
1187 *
1188 * @param networkType integer specifying which networkType in
1189 * which you're interested.
1190 * @return a {@link NetworkInfo} object for the requested
1191 * network type or {@code null} if the type is not
Chalard Jean96d10a72018-03-29 17:45:24 +09001192 * supported by the device. If {@code networkType} is
1193 * TYPE_VPN and a VPN is active for the calling app,
1194 * then this method will try to return one of the
1195 * underlying networks for the VPN or null if the
1196 * VPN agent didn't specify any.
Robert Greenwaltfaa4b402013-02-15 10:56:35 -08001197 *
Paul Jensendda8e592015-03-18 12:23:02 -04001198 * @deprecated This method does not support multiple connected networks
1199 * of the same type. Use {@link #getAllNetworks} and
1200 * {@link #getNetworkInfo(android.net.Network)} instead.
Robert Greenwaltfaa4b402013-02-15 10:56:35 -08001201 */
Aurimas Liutikase80a0eb2016-05-24 15:22:55 -07001202 @Deprecated
Jeff Sharkey656584a2017-04-24 11:18:03 -06001203 @RequiresPermission(android.Manifest.permission.ACCESS_NETWORK_STATE)
Chalard Jean158702d2019-01-07 19:26:34 +09001204 @Nullable
The Android Open Source Project28527d22009-03-03 19:31:44 -08001205 public NetworkInfo getNetworkInfo(int networkType) {
1206 try {
1207 return mService.getNetworkInfo(networkType);
1208 } catch (RemoteException e) {
Jeff Sharkeyc78f85c2016-03-01 19:27:23 -07001209 throw e.rethrowFromSystemServer();
The Android Open Source Project28527d22009-03-03 19:31:44 -08001210 }
1211 }
1212
Robert Greenwaltfaa4b402013-02-15 10:56:35 -08001213 /**
Robert Greenwalt02fe11e2014-06-23 11:40:00 -07001214 * Returns connection status information about a particular
1215 * Network.
1216 *
1217 * @param network {@link Network} specifying which network
1218 * in which you're interested.
1219 * @return a {@link NetworkInfo} object for the requested
1220 * network or {@code null} if the {@code Network}
1221 * is not valid.
junyulai9826e7f2018-12-13 12:47:51 +08001222 * @deprecated See {@link NetworkInfo}.
Robert Greenwalt02fe11e2014-06-23 11:40:00 -07001223 */
junyulai9826e7f2018-12-13 12:47:51 +08001224 @Deprecated
Jeff Sharkey656584a2017-04-24 11:18:03 -06001225 @RequiresPermission(android.Manifest.permission.ACCESS_NETWORK_STATE)
Chalard Jean158702d2019-01-07 19:26:34 +09001226 @Nullable
1227 public NetworkInfo getNetworkInfo(@Nullable Network network) {
Jeff Sharkey7dbf83d2016-04-28 15:33:18 -06001228 return getNetworkInfoForUid(network, Process.myUid(), false);
1229 }
1230
1231 /** {@hide} */
1232 public NetworkInfo getNetworkInfoForUid(Network network, int uid, boolean ignoreBlocked) {
Robert Greenwalt02fe11e2014-06-23 11:40:00 -07001233 try {
Jeff Sharkey7dbf83d2016-04-28 15:33:18 -06001234 return mService.getNetworkInfoForUid(network, uid, ignoreBlocked);
Robert Greenwalt02fe11e2014-06-23 11:40:00 -07001235 } catch (RemoteException e) {
Jeff Sharkeyc78f85c2016-03-01 19:27:23 -07001236 throw e.rethrowFromSystemServer();
Robert Greenwalt02fe11e2014-06-23 11:40:00 -07001237 }
1238 }
1239
1240 /**
Robert Greenwaltfaa4b402013-02-15 10:56:35 -08001241 * Returns connection status information about all network
1242 * types supported by the device.
1243 *
1244 * @return an array of {@link NetworkInfo} objects. Check each
1245 * {@link NetworkInfo#getType} for which type each applies.
1246 *
Paul Jensendda8e592015-03-18 12:23:02 -04001247 * @deprecated This method does not support multiple connected networks
1248 * of the same type. Use {@link #getAllNetworks} and
1249 * {@link #getNetworkInfo(android.net.Network)} instead.
Robert Greenwaltfaa4b402013-02-15 10:56:35 -08001250 */
Aurimas Liutikase80a0eb2016-05-24 15:22:55 -07001251 @Deprecated
Jeff Sharkey656584a2017-04-24 11:18:03 -06001252 @RequiresPermission(android.Manifest.permission.ACCESS_NETWORK_STATE)
Chalard Jean158702d2019-01-07 19:26:34 +09001253 @NonNull
The Android Open Source Project28527d22009-03-03 19:31:44 -08001254 public NetworkInfo[] getAllNetworkInfo() {
1255 try {
1256 return mService.getAllNetworkInfo();
1257 } catch (RemoteException e) {
Jeff Sharkeyc78f85c2016-03-01 19:27:23 -07001258 throw e.rethrowFromSystemServer();
The Android Open Source Project28527d22009-03-03 19:31:44 -08001259 }
1260 }
1261
Robert Greenwaltfaa4b402013-02-15 10:56:35 -08001262 /**
junyulaiebd15162021-03-03 12:09:05 +08001263 * Return a list of {@link NetworkStateSnapshot}s, one for each network that is currently
1264 * connected.
1265 * @hide
1266 */
1267 @SystemApi(client = MODULE_LIBRARIES)
1268 @RequiresPermission(anyOf = {
1269 NetworkStack.PERMISSION_MAINLINE_NETWORK_STACK,
1270 android.Manifest.permission.NETWORK_STACK,
1271 android.Manifest.permission.NETWORK_SETTINGS})
1272 @NonNull
1273 public List<NetworkStateSnapshot> getAllNetworkStateSnapshot() {
1274 try {
1275 return mService.getAllNetworkStateSnapshot();
1276 } catch (RemoteException e) {
1277 throw e.rethrowFromSystemServer();
1278 }
1279 }
1280
1281 /**
Lorenzo Colitti860a7aa2014-08-22 17:10:50 -07001282 * Returns the {@link Network} object currently serving a given type, or
1283 * null if the given type is not connected.
1284 *
Lorenzo Colitti860a7aa2014-08-22 17:10:50 -07001285 * @hide
Paul Jensendda8e592015-03-18 12:23:02 -04001286 * @deprecated This method does not support multiple connected networks
1287 * of the same type. Use {@link #getAllNetworks} and
1288 * {@link #getNetworkInfo(android.net.Network)} instead.
Lorenzo Colitti860a7aa2014-08-22 17:10:50 -07001289 */
Aurimas Liutikase80a0eb2016-05-24 15:22:55 -07001290 @Deprecated
Jeff Sharkey656584a2017-04-24 11:18:03 -06001291 @RequiresPermission(android.Manifest.permission.ACCESS_NETWORK_STATE)
Mathew Inwoodc5b9bec2018-08-08 14:52:47 +01001292 @UnsupportedAppUsage
Lorenzo Colitti860a7aa2014-08-22 17:10:50 -07001293 public Network getNetworkForType(int networkType) {
1294 try {
1295 return mService.getNetworkForType(networkType);
1296 } catch (RemoteException e) {
Jeff Sharkeyc78f85c2016-03-01 19:27:23 -07001297 throw e.rethrowFromSystemServer();
Lorenzo Colitti860a7aa2014-08-22 17:10:50 -07001298 }
1299 }
1300
1301 /**
Robert Greenwalt02fe11e2014-06-23 11:40:00 -07001302 * Returns an array of all {@link Network} currently tracked by the
1303 * framework.
Paul Jensen08f9dbb2015-05-06 11:10:18 -04001304 *
1305 * @return an array of {@link Network} objects.
Robert Greenwalt02fe11e2014-06-23 11:40:00 -07001306 */
Jeff Sharkey656584a2017-04-24 11:18:03 -06001307 @RequiresPermission(android.Manifest.permission.ACCESS_NETWORK_STATE)
Chalard Jean158702d2019-01-07 19:26:34 +09001308 @NonNull
Robert Greenwalt02fe11e2014-06-23 11:40:00 -07001309 public Network[] getAllNetworks() {
1310 try {
1311 return mService.getAllNetworks();
1312 } catch (RemoteException e) {
Jeff Sharkeyc78f85c2016-03-01 19:27:23 -07001313 throw e.rethrowFromSystemServer();
Robert Greenwalt02fe11e2014-06-23 11:40:00 -07001314 }
1315 }
1316
1317 /**
Roshan Pius951c0032020-12-22 15:10:42 -08001318 * Returns an array of {@link NetworkCapabilities} objects, representing
Lorenzo Colitti79bd2e22014-11-28 11:21:30 +09001319 * the Networks that applications run by the given user will use by default.
1320 * @hide
1321 */
Mathew Inwoodc5b9bec2018-08-08 14:52:47 +01001322 @UnsupportedAppUsage
Lorenzo Colitti79bd2e22014-11-28 11:21:30 +09001323 public NetworkCapabilities[] getDefaultNetworkCapabilitiesForUser(int userId) {
1324 try {
Qingxi Lib2748102020-01-08 12:51:49 -08001325 return mService.getDefaultNetworkCapabilitiesForUser(
Roshan Piusaa24fde2020-12-17 14:53:09 -08001326 userId, mContext.getOpPackageName(), getAttributionTag());
Lorenzo Colitti79bd2e22014-11-28 11:21:30 +09001327 } catch (RemoteException e) {
Jeff Sharkeyc78f85c2016-03-01 19:27:23 -07001328 throw e.rethrowFromSystemServer();
Lorenzo Colitti79bd2e22014-11-28 11:21:30 +09001329 }
1330 }
1331
1332 /**
Robert Greenwaltfaa4b402013-02-15 10:56:35 -08001333 * Returns the IP information for the current default network.
1334 *
1335 * @return a {@link LinkProperties} object describing the IP info
1336 * for the current default network, or {@code null} if there
1337 * is no current default network.
1338 *
Robert Greenwaltfaa4b402013-02-15 10:56:35 -08001339 * {@hide}
Chalard Jean97021a12019-01-11 16:47:53 +09001340 * @deprecated please use {@link #getLinkProperties(Network)} on the return
1341 * value of {@link #getActiveNetwork()} instead. In particular,
1342 * this method will return non-null LinkProperties even if the
1343 * app is blocked by policy from using this network.
Robert Greenwaltfaa4b402013-02-15 10:56:35 -08001344 */
Jeff Sharkey656584a2017-04-24 11:18:03 -06001345 @RequiresPermission(android.Manifest.permission.ACCESS_NETWORK_STATE)
Chalard Jean97021a12019-01-11 16:47:53 +09001346 @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P, trackingBug = 109783091)
Robert Greenwalt9f0ee4f2010-09-14 09:18:02 -07001347 public LinkProperties getActiveLinkProperties() {
1348 try {
1349 return mService.getActiveLinkProperties();
1350 } catch (RemoteException e) {
Jeff Sharkeyc78f85c2016-03-01 19:27:23 -07001351 throw e.rethrowFromSystemServer();
Robert Greenwalt9f0ee4f2010-09-14 09:18:02 -07001352 }
1353 }
1354
Robert Greenwaltfaa4b402013-02-15 10:56:35 -08001355 /**
1356 * Returns the IP information for a given network type.
1357 *
1358 * @param networkType the network type of interest.
1359 * @return a {@link LinkProperties} object describing the IP info
1360 * for the given networkType, or {@code null} if there is
1361 * no current default network.
1362 *
Robert Greenwaltfaa4b402013-02-15 10:56:35 -08001363 * {@hide}
Paul Jensendda8e592015-03-18 12:23:02 -04001364 * @deprecated This method does not support multiple connected networks
1365 * of the same type. Use {@link #getAllNetworks},
1366 * {@link #getNetworkInfo(android.net.Network)}, and
1367 * {@link #getLinkProperties(android.net.Network)} instead.
Robert Greenwaltfaa4b402013-02-15 10:56:35 -08001368 */
Aurimas Liutikase80a0eb2016-05-24 15:22:55 -07001369 @Deprecated
Jeff Sharkey656584a2017-04-24 11:18:03 -06001370 @RequiresPermission(android.Manifest.permission.ACCESS_NETWORK_STATE)
Chalard Jeana3b77512019-04-09 15:46:21 +09001371 @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P, trackingBug = 130143562)
Robert Greenwalt9f0ee4f2010-09-14 09:18:02 -07001372 public LinkProperties getLinkProperties(int networkType) {
1373 try {
Robert Greenwaltf99b8392014-03-26 16:47:06 -07001374 return mService.getLinkPropertiesForType(networkType);
1375 } catch (RemoteException e) {
Jeff Sharkeyc78f85c2016-03-01 19:27:23 -07001376 throw e.rethrowFromSystemServer();
Robert Greenwaltf99b8392014-03-26 16:47:06 -07001377 }
1378 }
1379
Robert Greenwaltf3017f72014-05-18 23:07:25 -07001380 /**
1381 * Get the {@link LinkProperties} for the given {@link Network}. This
1382 * will return {@code null} if the network is unknown.
1383 *
1384 * @param network The {@link Network} object identifying the network in question.
1385 * @return The {@link LinkProperties} for the network, or {@code null}.
Paul Jensen08f9dbb2015-05-06 11:10:18 -04001386 */
Jeff Sharkey656584a2017-04-24 11:18:03 -06001387 @RequiresPermission(android.Manifest.permission.ACCESS_NETWORK_STATE)
Chalard Jean158702d2019-01-07 19:26:34 +09001388 @Nullable
1389 public LinkProperties getLinkProperties(@Nullable Network network) {
Robert Greenwaltf99b8392014-03-26 16:47:06 -07001390 try {
1391 return mService.getLinkProperties(network);
1392 } catch (RemoteException e) {
Jeff Sharkeyc78f85c2016-03-01 19:27:23 -07001393 throw e.rethrowFromSystemServer();
Robert Greenwaltf99b8392014-03-26 16:47:06 -07001394 }
1395 }
1396
Robert Greenwaltf3017f72014-05-18 23:07:25 -07001397 /**
Roshan Pius951c0032020-12-22 15:10:42 -08001398 * Get the {@link NetworkCapabilities} for the given {@link Network}. This
Robert Greenwaltf3017f72014-05-18 23:07:25 -07001399 * will return {@code null} if the network is unknown.
1400 *
Roshan Pius951c0032020-12-22 15:10:42 -08001401 * This will remove any location sensitive data in {@link TransportInfo} embedded in
1402 * {@link NetworkCapabilities#getTransportInfo()}. Some transport info instances like
1403 * {@link android.net.wifi.WifiInfo} contain location sensitive information. Retrieving
1404 * this location sensitive information (subject to app's location permissions) will be
1405 * noted by system. To include any location sensitive data in {@link TransportInfo},
1406 * use a {@link NetworkCallback} with
1407 * {@link NetworkCallback#FLAG_INCLUDE_LOCATION_INFO} flag.
1408 *
Robert Greenwaltf3017f72014-05-18 23:07:25 -07001409 * @param network The {@link Network} object identifying the network in question.
Roshan Pius951c0032020-12-22 15:10:42 -08001410 * @return The {@link NetworkCapabilities} for the network, or {@code null}.
Robert Greenwaltf3017f72014-05-18 23:07:25 -07001411 */
Jeff Sharkey656584a2017-04-24 11:18:03 -06001412 @RequiresPermission(android.Manifest.permission.ACCESS_NETWORK_STATE)
Chalard Jean158702d2019-01-07 19:26:34 +09001413 @Nullable
1414 public NetworkCapabilities getNetworkCapabilities(@Nullable Network network) {
Robert Greenwaltf99b8392014-03-26 16:47:06 -07001415 try {
Roshan Piusaa24fde2020-12-17 14:53:09 -08001416 return mService.getNetworkCapabilities(
1417 network, mContext.getOpPackageName(), getAttributionTag());
Robert Greenwalt9f0ee4f2010-09-14 09:18:02 -07001418 } catch (RemoteException e) {
Jeff Sharkeyc78f85c2016-03-01 19:27:23 -07001419 throw e.rethrowFromSystemServer();
Robert Greenwalt9f0ee4f2010-09-14 09:18:02 -07001420 }
1421 }
1422
Robert Greenwaltfaa4b402013-02-15 10:56:35 -08001423 /**
Remi NGUYEN VAN035f6532019-03-20 14:22:49 +09001424 * Gets a URL that can be used for resolving whether a captive portal is present.
Udam Sainicd645462016-01-04 12:16:14 -08001425 * 1. This URL should respond with a 204 response to a GET request to indicate no captive
1426 * portal is present.
1427 * 2. This URL must be HTTP as redirect responses are used to find captive portal
1428 * sign-in pages. Captive portals cannot respond to HTTPS requests with redirects.
1429 *
Remi NGUYEN VAN035f6532019-03-20 14:22:49 +09001430 * The system network validation may be using different strategies to detect captive portals,
1431 * so this method does not necessarily return a URL used by the system. It only returns a URL
1432 * that may be relevant for other components trying to detect captive portals.
paulhu8e96a752019-08-12 16:25:11 +08001433 *
Udam Sainicd645462016-01-04 12:16:14 -08001434 * @hide
paulhu8e96a752019-08-12 16:25:11 +08001435 * @deprecated This API returns URL which is not guaranteed to be one of the URLs used by the
1436 * system.
Udam Sainicd645462016-01-04 12:16:14 -08001437 */
paulhu8e96a752019-08-12 16:25:11 +08001438 @Deprecated
Udam Sainicd645462016-01-04 12:16:14 -08001439 @SystemApi
paulhu8e96a752019-08-12 16:25:11 +08001440 @RequiresPermission(android.Manifest.permission.NETWORK_SETTINGS)
Udam Sainicd645462016-01-04 12:16:14 -08001441 public String getCaptivePortalServerUrl() {
1442 try {
1443 return mService.getCaptivePortalServerUrl();
1444 } catch (RemoteException e) {
Jeff Sharkeyc78f85c2016-03-01 19:27:23 -07001445 throw e.rethrowFromSystemServer();
Udam Sainicd645462016-01-04 12:16:14 -08001446 }
1447 }
1448
1449 /**
The Android Open Source Project28527d22009-03-03 19:31:44 -08001450 * Tells the underlying networking system that the caller wants to
1451 * begin using the named feature. The interpretation of {@code feature}
1452 * is completely up to each networking implementation.
Lorenzo Colitti0b40c872015-10-15 16:29:00 +09001453 *
1454 * <p>This method requires the caller to hold either the
1455 * {@link android.Manifest.permission#CHANGE_NETWORK_STATE} permission
1456 * or the ability to modify system settings as determined by
1457 * {@link android.provider.Settings.System#canWrite}.</p>
1458 *
The Android Open Source Project28527d22009-03-03 19:31:44 -08001459 * @param networkType specifies which network the request pertains to
1460 * @param feature the name of the feature to be used
1461 * @return an integer value representing the outcome of the request.
1462 * The interpretation of this value is specific to each networking
1463 * implementation+feature combination, except that the value {@code -1}
1464 * always indicates failure.
Robert Greenwaltf3017f72014-05-18 23:07:25 -07001465 *
Lorenzo Colitti7f6a2bf2015-04-24 17:03:31 +09001466 * @deprecated Deprecated in favor of the cleaner
1467 * {@link #requestNetwork(NetworkRequest, NetworkCallback)} API.
Dianne Hackborn18c1d832015-07-31 10:35:34 -07001468 * In {@link VERSION_CODES#M}, and above, this method is unsupported and will
Lorenzo Colitti2780e4b2015-07-29 11:41:21 +09001469 * throw {@code UnsupportedOperationException} if called.
Lorenzo Colitti84d7f072016-12-09 18:39:30 +09001470 * @removed
The Android Open Source Project28527d22009-03-03 19:31:44 -08001471 */
Aurimas Liutikase80a0eb2016-05-24 15:22:55 -07001472 @Deprecated
The Android Open Source Project28527d22009-03-03 19:31:44 -08001473 public int startUsingNetworkFeature(int networkType, String feature) {
Lorenzo Colitti2780e4b2015-07-29 11:41:21 +09001474 checkLegacyRoutingApiAccess();
Robert Greenwalt3dc73e52014-05-15 18:07:26 -07001475 NetworkCapabilities netCap = networkCapabilitiesForFeature(networkType, feature);
1476 if (netCap == null) {
1477 Log.d(TAG, "Can't satisfy startUsingNetworkFeature for " + networkType + ", " +
1478 feature);
Chalard Jeanafaed1a2019-11-21 14:48:00 +09001479 return DEPRECATED_PHONE_CONSTANT_APN_REQUEST_FAILED;
Robert Greenwalt3dc73e52014-05-15 18:07:26 -07001480 }
1481
1482 NetworkRequest request = null;
1483 synchronized (sLegacyRequests) {
1484 LegacyRequest l = sLegacyRequests.get(netCap);
1485 if (l != null) {
1486 Log.d(TAG, "renewing startUsingNetworkFeature request " + l.networkRequest);
1487 renewRequestLocked(l);
1488 if (l.currentNetwork != null) {
Chalard Jeanafaed1a2019-11-21 14:48:00 +09001489 return DEPRECATED_PHONE_CONSTANT_APN_ALREADY_ACTIVE;
Robert Greenwalt3dc73e52014-05-15 18:07:26 -07001490 } else {
Chalard Jeanafaed1a2019-11-21 14:48:00 +09001491 return DEPRECATED_PHONE_CONSTANT_APN_REQUEST_STARTED;
Robert Greenwalt3dc73e52014-05-15 18:07:26 -07001492 }
1493 }
1494
1495 request = requestNetworkForFeatureLocked(netCap);
1496 }
1497 if (request != null) {
Robert Greenwaltb8401732014-06-20 10:58:45 -07001498 Log.d(TAG, "starting startUsingNetworkFeature for request " + request);
Chalard Jeanafaed1a2019-11-21 14:48:00 +09001499 return DEPRECATED_PHONE_CONSTANT_APN_REQUEST_STARTED;
Robert Greenwalt3dc73e52014-05-15 18:07:26 -07001500 } else {
1501 Log.d(TAG, " request Failed");
Chalard Jeanafaed1a2019-11-21 14:48:00 +09001502 return DEPRECATED_PHONE_CONSTANT_APN_REQUEST_FAILED;
The Android Open Source Project28527d22009-03-03 19:31:44 -08001503 }
1504 }
1505
1506 /**
1507 * Tells the underlying networking system that the caller is finished
1508 * using the named feature. The interpretation of {@code feature}
1509 * is completely up to each networking implementation.
Lorenzo Colitti0b40c872015-10-15 16:29:00 +09001510 *
1511 * <p>This method requires the caller to hold either the
1512 * {@link android.Manifest.permission#CHANGE_NETWORK_STATE} permission
1513 * or the ability to modify system settings as determined by
1514 * {@link android.provider.Settings.System#canWrite}.</p>
1515 *
The Android Open Source Project28527d22009-03-03 19:31:44 -08001516 * @param networkType specifies which network the request pertains to
1517 * @param feature the name of the feature that is no longer needed
1518 * @return an integer value representing the outcome of the request.
1519 * The interpretation of this value is specific to each networking
1520 * implementation+feature combination, except that the value {@code -1}
1521 * always indicates failure.
Robert Greenwaltf3017f72014-05-18 23:07:25 -07001522 *
Lorenzo Colitti15874cd2016-04-13 22:00:02 +09001523 * @deprecated Deprecated in favor of the cleaner
1524 * {@link #unregisterNetworkCallback(NetworkCallback)} API.
Dianne Hackborn18c1d832015-07-31 10:35:34 -07001525 * In {@link VERSION_CODES#M}, and above, this method is unsupported and will
Lorenzo Colitti2780e4b2015-07-29 11:41:21 +09001526 * throw {@code UnsupportedOperationException} if called.
Lorenzo Colitti84d7f072016-12-09 18:39:30 +09001527 * @removed
The Android Open Source Project28527d22009-03-03 19:31:44 -08001528 */
Aurimas Liutikase80a0eb2016-05-24 15:22:55 -07001529 @Deprecated
The Android Open Source Project28527d22009-03-03 19:31:44 -08001530 public int stopUsingNetworkFeature(int networkType, String feature) {
Lorenzo Colitti2780e4b2015-07-29 11:41:21 +09001531 checkLegacyRoutingApiAccess();
Robert Greenwalt3dc73e52014-05-15 18:07:26 -07001532 NetworkCapabilities netCap = networkCapabilitiesForFeature(networkType, feature);
1533 if (netCap == null) {
1534 Log.d(TAG, "Can't satisfy stopUsingNetworkFeature for " + networkType + ", " +
1535 feature);
The Android Open Source Project28527d22009-03-03 19:31:44 -08001536 return -1;
1537 }
Robert Greenwalt3dc73e52014-05-15 18:07:26 -07001538
Paul Jensen49f74a32014-12-17 10:39:34 -05001539 if (removeRequestForFeature(netCap)) {
Robert Greenwalt3dc73e52014-05-15 18:07:26 -07001540 Log.d(TAG, "stopUsingNetworkFeature for " + networkType + ", " + feature);
Robert Greenwalt3dc73e52014-05-15 18:07:26 -07001541 }
1542 return 1;
1543 }
1544
Mathew Inwoode1a17ba2020-11-04 09:29:36 +00001545 @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.R, trackingBug = 170729553)
Robert Greenwalt3dc73e52014-05-15 18:07:26 -07001546 private NetworkCapabilities networkCapabilitiesForFeature(int networkType, String feature) {
1547 if (networkType == TYPE_MOBILE) {
Erik Klinece55eb12017-01-26 18:08:28 +09001548 switch (feature) {
1549 case "enableCBS":
1550 return networkCapabilitiesForType(TYPE_MOBILE_CBS);
1551 case "enableDUN":
1552 case "enableDUNAlways":
1553 return networkCapabilitiesForType(TYPE_MOBILE_DUN);
1554 case "enableFOTA":
1555 return networkCapabilitiesForType(TYPE_MOBILE_FOTA);
1556 case "enableHIPRI":
1557 return networkCapabilitiesForType(TYPE_MOBILE_HIPRI);
1558 case "enableIMS":
1559 return networkCapabilitiesForType(TYPE_MOBILE_IMS);
1560 case "enableMMS":
1561 return networkCapabilitiesForType(TYPE_MOBILE_MMS);
1562 case "enableSUPL":
1563 return networkCapabilitiesForType(TYPE_MOBILE_SUPL);
1564 default:
1565 return null;
Robert Greenwalt3dc73e52014-05-15 18:07:26 -07001566 }
Erik Klinece55eb12017-01-26 18:08:28 +09001567 } else if (networkType == TYPE_WIFI && "p2p".equals(feature)) {
1568 return networkCapabilitiesForType(TYPE_WIFI_P2P);
Robert Greenwalt3dc73e52014-05-15 18:07:26 -07001569 }
1570 return null;
1571 }
1572
Robert Greenwalt802c1102014-06-02 15:32:02 -07001573 private int legacyTypeForNetworkCapabilities(NetworkCapabilities netCap) {
Robert Greenwalt3dc73e52014-05-15 18:07:26 -07001574 if (netCap == null) return TYPE_NONE;
1575 if (netCap.hasCapability(NetworkCapabilities.NET_CAPABILITY_CBS)) {
1576 return TYPE_MOBILE_CBS;
1577 }
1578 if (netCap.hasCapability(NetworkCapabilities.NET_CAPABILITY_IMS)) {
1579 return TYPE_MOBILE_IMS;
1580 }
1581 if (netCap.hasCapability(NetworkCapabilities.NET_CAPABILITY_FOTA)) {
1582 return TYPE_MOBILE_FOTA;
1583 }
1584 if (netCap.hasCapability(NetworkCapabilities.NET_CAPABILITY_DUN)) {
1585 return TYPE_MOBILE_DUN;
1586 }
1587 if (netCap.hasCapability(NetworkCapabilities.NET_CAPABILITY_SUPL)) {
1588 return TYPE_MOBILE_SUPL;
1589 }
1590 if (netCap.hasCapability(NetworkCapabilities.NET_CAPABILITY_MMS)) {
1591 return TYPE_MOBILE_MMS;
1592 }
1593 if (netCap.hasCapability(NetworkCapabilities.NET_CAPABILITY_INTERNET)) {
1594 return TYPE_MOBILE_HIPRI;
1595 }
Robert Greenwalt802c1102014-06-02 15:32:02 -07001596 if (netCap.hasCapability(NetworkCapabilities.NET_CAPABILITY_WIFI_P2P)) {
1597 return TYPE_WIFI_P2P;
1598 }
Robert Greenwalt3dc73e52014-05-15 18:07:26 -07001599 return TYPE_NONE;
1600 }
1601
1602 private static class LegacyRequest {
1603 NetworkCapabilities networkCapabilities;
1604 NetworkRequest networkRequest;
1605 int expireSequenceNumber;
1606 Network currentNetwork;
1607 int delay = -1;
Paul Jensen49f74a32014-12-17 10:39:34 -05001608
1609 private void clearDnsBinding() {
1610 if (currentNetwork != null) {
1611 currentNetwork = null;
1612 setProcessDefaultNetworkForHostResolution(null);
1613 }
1614 }
1615
Robert Greenwaltf57c03c2014-06-11 16:05:07 -07001616 NetworkCallback networkCallback = new NetworkCallback() {
Robert Greenwalt3dc73e52014-05-15 18:07:26 -07001617 @Override
Robert Greenwaltf57c03c2014-06-11 16:05:07 -07001618 public void onAvailable(Network network) {
Robert Greenwalt3dc73e52014-05-15 18:07:26 -07001619 currentNetwork = network;
1620 Log.d(TAG, "startUsingNetworkFeature got Network:" + network);
Paul Jensen8cdda642014-05-29 10:12:39 -04001621 setProcessDefaultNetworkForHostResolution(network);
Robert Greenwalt3dc73e52014-05-15 18:07:26 -07001622 }
1623 @Override
Robert Greenwaltf57c03c2014-06-11 16:05:07 -07001624 public void onLost(Network network) {
Paul Jensen49f74a32014-12-17 10:39:34 -05001625 if (network.equals(currentNetwork)) clearDnsBinding();
Robert Greenwalt3dc73e52014-05-15 18:07:26 -07001626 Log.d(TAG, "startUsingNetworkFeature lost Network:" + network);
1627 }
1628 };
1629 }
1630
Mathew Inwoode1a17ba2020-11-04 09:29:36 +00001631 @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.R, trackingBug = 170729553)
Chalard Jean9dd11612018-06-04 16:52:49 +09001632 private static final HashMap<NetworkCapabilities, LegacyRequest> sLegacyRequests =
1633 new HashMap<>();
Robert Greenwalt3dc73e52014-05-15 18:07:26 -07001634
1635 private NetworkRequest findRequestForFeature(NetworkCapabilities netCap) {
1636 synchronized (sLegacyRequests) {
1637 LegacyRequest l = sLegacyRequests.get(netCap);
1638 if (l != null) return l.networkRequest;
1639 }
1640 return null;
1641 }
1642
1643 private void renewRequestLocked(LegacyRequest l) {
1644 l.expireSequenceNumber++;
1645 Log.d(TAG, "renewing request to seqNum " + l.expireSequenceNumber);
1646 sendExpireMsgForFeature(l.networkCapabilities, l.expireSequenceNumber, l.delay);
1647 }
1648
1649 private void expireRequest(NetworkCapabilities netCap, int sequenceNum) {
1650 int ourSeqNum = -1;
1651 synchronized (sLegacyRequests) {
1652 LegacyRequest l = sLegacyRequests.get(netCap);
1653 if (l == null) return;
1654 ourSeqNum = l.expireSequenceNumber;
Paul Jensen49f74a32014-12-17 10:39:34 -05001655 if (l.expireSequenceNumber == sequenceNum) removeRequestForFeature(netCap);
Robert Greenwalt3dc73e52014-05-15 18:07:26 -07001656 }
1657 Log.d(TAG, "expireRequest with " + ourSeqNum + ", " + sequenceNum);
1658 }
1659
Mathew Inwoode1a17ba2020-11-04 09:29:36 +00001660 @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.R, trackingBug = 170729553)
Robert Greenwalt3dc73e52014-05-15 18:07:26 -07001661 private NetworkRequest requestNetworkForFeatureLocked(NetworkCapabilities netCap) {
1662 int delay = -1;
Robert Greenwalt802c1102014-06-02 15:32:02 -07001663 int type = legacyTypeForNetworkCapabilities(netCap);
Robert Greenwalt3dc73e52014-05-15 18:07:26 -07001664 try {
1665 delay = mService.getRestoreDefaultNetworkDelay(type);
Jeff Sharkeyc78f85c2016-03-01 19:27:23 -07001666 } catch (RemoteException e) {
1667 throw e.rethrowFromSystemServer();
1668 }
Robert Greenwalt3dc73e52014-05-15 18:07:26 -07001669 LegacyRequest l = new LegacyRequest();
1670 l.networkCapabilities = netCap;
1671 l.delay = delay;
1672 l.expireSequenceNumber = 0;
Hugo Benichifd44e912017-02-02 17:02:36 +09001673 l.networkRequest = sendRequestForNetwork(
1674 netCap, l.networkCallback, 0, REQUEST, type, getDefaultHandler());
Robert Greenwalt3dc73e52014-05-15 18:07:26 -07001675 if (l.networkRequest == null) return null;
1676 sLegacyRequests.put(netCap, l);
1677 sendExpireMsgForFeature(netCap, l.expireSequenceNumber, delay);
1678 return l.networkRequest;
1679 }
1680
1681 private void sendExpireMsgForFeature(NetworkCapabilities netCap, int seqNum, int delay) {
1682 if (delay >= 0) {
1683 Log.d(TAG, "sending expire msg with seqNum " + seqNum + " and delay " + delay);
Hugo Benichifd44e912017-02-02 17:02:36 +09001684 CallbackHandler handler = getDefaultHandler();
Hugo Benichibc4ac972017-02-03 14:18:44 +09001685 Message msg = handler.obtainMessage(EXPIRE_LEGACY_REQUEST, seqNum, 0, netCap);
1686 handler.sendMessageDelayed(msg, delay);
Robert Greenwalt3dc73e52014-05-15 18:07:26 -07001687 }
1688 }
1689
Mathew Inwoode1a17ba2020-11-04 09:29:36 +00001690 @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.R, trackingBug = 170729553)
Paul Jensen49f74a32014-12-17 10:39:34 -05001691 private boolean removeRequestForFeature(NetworkCapabilities netCap) {
1692 final LegacyRequest l;
Robert Greenwalt3dc73e52014-05-15 18:07:26 -07001693 synchronized (sLegacyRequests) {
Paul Jensen49f74a32014-12-17 10:39:34 -05001694 l = sLegacyRequests.remove(netCap);
Robert Greenwalt3dc73e52014-05-15 18:07:26 -07001695 }
Paul Jensen49f74a32014-12-17 10:39:34 -05001696 if (l == null) return false;
1697 unregisterNetworkCallback(l.networkCallback);
1698 l.clearDnsBinding();
1699 return true;
The Android Open Source Project28527d22009-03-03 19:31:44 -08001700 }
1701
Erik Klinece55eb12017-01-26 18:08:28 +09001702 private static final SparseIntArray sLegacyTypeToTransport = new SparseIntArray();
1703 static {
1704 sLegacyTypeToTransport.put(TYPE_MOBILE, NetworkCapabilities.TRANSPORT_CELLULAR);
1705 sLegacyTypeToTransport.put(TYPE_MOBILE_CBS, NetworkCapabilities.TRANSPORT_CELLULAR);
1706 sLegacyTypeToTransport.put(TYPE_MOBILE_DUN, NetworkCapabilities.TRANSPORT_CELLULAR);
1707 sLegacyTypeToTransport.put(TYPE_MOBILE_FOTA, NetworkCapabilities.TRANSPORT_CELLULAR);
1708 sLegacyTypeToTransport.put(TYPE_MOBILE_HIPRI, NetworkCapabilities.TRANSPORT_CELLULAR);
1709 sLegacyTypeToTransport.put(TYPE_MOBILE_IMS, NetworkCapabilities.TRANSPORT_CELLULAR);
1710 sLegacyTypeToTransport.put(TYPE_MOBILE_MMS, NetworkCapabilities.TRANSPORT_CELLULAR);
1711 sLegacyTypeToTransport.put(TYPE_MOBILE_SUPL, NetworkCapabilities.TRANSPORT_CELLULAR);
1712 sLegacyTypeToTransport.put(TYPE_WIFI, NetworkCapabilities.TRANSPORT_WIFI);
1713 sLegacyTypeToTransport.put(TYPE_WIFI_P2P, NetworkCapabilities.TRANSPORT_WIFI);
1714 sLegacyTypeToTransport.put(TYPE_BLUETOOTH, NetworkCapabilities.TRANSPORT_BLUETOOTH);
1715 sLegacyTypeToTransport.put(TYPE_ETHERNET, NetworkCapabilities.TRANSPORT_ETHERNET);
1716 }
1717
1718 private static final SparseIntArray sLegacyTypeToCapability = new SparseIntArray();
1719 static {
1720 sLegacyTypeToCapability.put(TYPE_MOBILE_CBS, NetworkCapabilities.NET_CAPABILITY_CBS);
1721 sLegacyTypeToCapability.put(TYPE_MOBILE_DUN, NetworkCapabilities.NET_CAPABILITY_DUN);
1722 sLegacyTypeToCapability.put(TYPE_MOBILE_FOTA, NetworkCapabilities.NET_CAPABILITY_FOTA);
1723 sLegacyTypeToCapability.put(TYPE_MOBILE_IMS, NetworkCapabilities.NET_CAPABILITY_IMS);
1724 sLegacyTypeToCapability.put(TYPE_MOBILE_MMS, NetworkCapabilities.NET_CAPABILITY_MMS);
1725 sLegacyTypeToCapability.put(TYPE_MOBILE_SUPL, NetworkCapabilities.NET_CAPABILITY_SUPL);
1726 sLegacyTypeToCapability.put(TYPE_WIFI_P2P, NetworkCapabilities.NET_CAPABILITY_WIFI_P2P);
1727 }
1728
1729 /**
1730 * Given a legacy type (TYPE_WIFI, ...) returns a NetworkCapabilities
1731 * instance suitable for registering a request or callback. Throws an
1732 * IllegalArgumentException if no mapping from the legacy type to
1733 * NetworkCapabilities is known.
1734 *
Chalard Jean7eaf3b12018-03-08 13:54:53 +09001735 * @deprecated Types are deprecated. Use {@link NetworkCallback} or {@link NetworkRequest}
1736 * to find the network instead.
Erik Klinece55eb12017-01-26 18:08:28 +09001737 * @hide
1738 */
1739 public static NetworkCapabilities networkCapabilitiesForType(int type) {
1740 final NetworkCapabilities nc = new NetworkCapabilities();
1741
1742 // Map from type to transports.
1743 final int NOT_FOUND = -1;
1744 final int transport = sLegacyTypeToTransport.get(type, NOT_FOUND);
Remi NGUYEN VAN5086c292021-03-15 07:27:44 +00001745 Preconditions.checkArgument(transport != NOT_FOUND, "unknown legacy type: " + type);
Erik Klinece55eb12017-01-26 18:08:28 +09001746 nc.addTransportType(transport);
1747
1748 // Map from type to capabilities.
1749 nc.addCapability(sLegacyTypeToCapability.get(
1750 type, NetworkCapabilities.NET_CAPABILITY_INTERNET));
1751 nc.maybeMarkCapabilitiesRestricted();
1752 return nc;
1753 }
1754
Lorenzo Colitti0b798a82015-06-15 14:29:22 +09001755 /** @hide */
1756 public static class PacketKeepaliveCallback {
Mathew Inwoode1a17ba2020-11-04 09:29:36 +00001757 @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.R, trackingBug = 170729553)
Artur Satayev56cb6bb2019-11-04 17:50:59 +00001758 public PacketKeepaliveCallback() {
1759 }
Lorenzo Colitti0b798a82015-06-15 14:29:22 +09001760 /** The requested keepalive was successfully started. */
Mathew Inwoode1a17ba2020-11-04 09:29:36 +00001761 @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.R, trackingBug = 170729553)
Lorenzo Colitti0b798a82015-06-15 14:29:22 +09001762 public void onStarted() {}
1763 /** The keepalive was successfully stopped. */
Mathew Inwoode1a17ba2020-11-04 09:29:36 +00001764 @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.R, trackingBug = 170729553)
Lorenzo Colitti0b798a82015-06-15 14:29:22 +09001765 public void onStopped() {}
1766 /** An error occurred. */
Mathew Inwoode1a17ba2020-11-04 09:29:36 +00001767 @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.R, trackingBug = 170729553)
Lorenzo Colitti0b798a82015-06-15 14:29:22 +09001768 public void onError(int error) {}
1769 }
1770
1771 /**
1772 * Allows applications to request that the system periodically send specific packets on their
1773 * behalf, using hardware offload to save battery power.
1774 *
1775 * To request that the system send keepalives, call one of the methods that return a
1776 * {@link ConnectivityManager.PacketKeepalive} object, such as {@link #startNattKeepalive},
1777 * passing in a non-null callback. If the callback is successfully started, the callback's
1778 * {@code onStarted} method will be called. If an error occurs, {@code onError} will be called,
1779 * specifying one of the {@code ERROR_*} constants in this class.
1780 *
Chalard Jean9dd11612018-06-04 16:52:49 +09001781 * To stop an existing keepalive, call {@link PacketKeepalive#stop}. The system will call
1782 * {@link PacketKeepaliveCallback#onStopped} if the operation was successful or
1783 * {@link PacketKeepaliveCallback#onError} if an error occurred.
Lorenzo Colitti0b798a82015-06-15 14:29:22 +09001784 *
junyulai4c95b082018-12-27 17:25:29 +08001785 * @deprecated Use {@link SocketKeepalive} instead.
1786 *
Lorenzo Colitti0b798a82015-06-15 14:29:22 +09001787 * @hide
1788 */
1789 public class PacketKeepalive {
1790
1791 private static final String TAG = "PacketKeepalive";
1792
1793 /** @hide */
1794 public static final int SUCCESS = 0;
1795
1796 /** @hide */
1797 public static final int NO_KEEPALIVE = -1;
1798
1799 /** @hide */
1800 public static final int BINDER_DIED = -10;
1801
1802 /** The specified {@code Network} is not connected. */
1803 public static final int ERROR_INVALID_NETWORK = -20;
1804 /** The specified IP addresses are invalid. For example, the specified source IP address is
1805 * not configured on the specified {@code Network}. */
1806 public static final int ERROR_INVALID_IP_ADDRESS = -21;
1807 /** The requested port is invalid. */
1808 public static final int ERROR_INVALID_PORT = -22;
1809 /** The packet length is invalid (e.g., too long). */
1810 public static final int ERROR_INVALID_LENGTH = -23;
1811 /** The packet transmission interval is invalid (e.g., too short). */
1812 public static final int ERROR_INVALID_INTERVAL = -24;
1813
1814 /** The hardware does not support this request. */
1815 public static final int ERROR_HARDWARE_UNSUPPORTED = -30;
Lorenzo Colitti723f82f2015-09-08 16:46:36 +09001816 /** The hardware returned an error. */
1817 public static final int ERROR_HARDWARE_ERROR = -31;
Lorenzo Colitti0b798a82015-06-15 14:29:22 +09001818
Nathan Harold0990bc82018-02-14 13:09:45 -08001819 /** The NAT-T destination port for IPsec */
Lorenzo Colitti0b798a82015-06-15 14:29:22 +09001820 public static final int NATT_PORT = 4500;
1821
Nathan Harold0990bc82018-02-14 13:09:45 -08001822 /** The minimum interval in seconds between keepalive packet transmissions */
1823 public static final int MIN_INTERVAL = 10;
1824
Lorenzo Colitti0b798a82015-06-15 14:29:22 +09001825 private final Network mNetwork;
junyulai070f9ff2019-01-16 20:23:34 +08001826 private final ISocketKeepaliveCallback mCallback;
1827 private final ExecutorService mExecutor;
Lorenzo Colitti0b798a82015-06-15 14:29:22 +09001828
1829 private volatile Integer mSlot;
1830
Mathew Inwoode1a17ba2020-11-04 09:29:36 +00001831 @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.R, trackingBug = 170729553)
Lorenzo Colitti0b798a82015-06-15 14:29:22 +09001832 public void stop() {
1833 try {
junyulai070f9ff2019-01-16 20:23:34 +08001834 mExecutor.execute(() -> {
1835 try {
1836 if (mSlot != null) {
1837 mService.stopKeepalive(mNetwork, mSlot);
1838 }
1839 } catch (RemoteException e) {
1840 Log.e(TAG, "Error stopping packet keepalive: ", e);
1841 throw e.rethrowFromSystemServer();
1842 }
1843 });
1844 } catch (RejectedExecutionException e) {
1845 // The internal executor has already stopped due to previous event.
Lorenzo Colitti0b798a82015-06-15 14:29:22 +09001846 }
1847 }
1848
1849 private PacketKeepalive(Network network, PacketKeepaliveCallback callback) {
Remi NGUYEN VAN5086c292021-03-15 07:27:44 +00001850 Preconditions.checkNotNull(network, "network cannot be null");
1851 Preconditions.checkNotNull(callback, "callback cannot be null");
Lorenzo Colitti0b798a82015-06-15 14:29:22 +09001852 mNetwork = network;
junyulai070f9ff2019-01-16 20:23:34 +08001853 mExecutor = Executors.newSingleThreadExecutor();
1854 mCallback = new ISocketKeepaliveCallback.Stub() {
Lorenzo Colitti0b798a82015-06-15 14:29:22 +09001855 @Override
junyulai070f9ff2019-01-16 20:23:34 +08001856 public void onStarted(int slot) {
lucaslinbe801382020-12-30 11:54:55 +08001857 final long token = Binder.clearCallingIdentity();
1858 try {
1859 mExecutor.execute(() -> {
1860 mSlot = slot;
1861 callback.onStarted();
1862 });
1863 } finally {
1864 Binder.restoreCallingIdentity(token);
1865 }
Lorenzo Colitti0b798a82015-06-15 14:29:22 +09001866 }
junyulai070f9ff2019-01-16 20:23:34 +08001867
1868 @Override
1869 public void onStopped() {
lucaslinbe801382020-12-30 11:54:55 +08001870 final long token = Binder.clearCallingIdentity();
1871 try {
1872 mExecutor.execute(() -> {
1873 mSlot = null;
1874 callback.onStopped();
1875 });
1876 } finally {
1877 Binder.restoreCallingIdentity(token);
1878 }
junyulai070f9ff2019-01-16 20:23:34 +08001879 mExecutor.shutdown();
1880 }
1881
1882 @Override
1883 public void onError(int error) {
lucaslinbe801382020-12-30 11:54:55 +08001884 final long token = Binder.clearCallingIdentity();
1885 try {
1886 mExecutor.execute(() -> {
1887 mSlot = null;
1888 callback.onError(error);
1889 });
1890 } finally {
1891 Binder.restoreCallingIdentity(token);
1892 }
junyulai070f9ff2019-01-16 20:23:34 +08001893 mExecutor.shutdown();
1894 }
1895
1896 @Override
1897 public void onDataReceived() {
1898 // PacketKeepalive is only used for Nat-T keepalive and as such does not invoke
1899 // this callback when data is received.
1900 }
1901 };
Lorenzo Colitti0b798a82015-06-15 14:29:22 +09001902 }
1903 }
1904
1905 /**
1906 * Starts an IPsec NAT-T keepalive packet with the specified parameters.
1907 *
junyulai4c95b082018-12-27 17:25:29 +08001908 * @deprecated Use {@link #createSocketKeepalive} instead.
1909 *
Lorenzo Colitti0b798a82015-06-15 14:29:22 +09001910 * @hide
1911 */
Mathew Inwoode1a17ba2020-11-04 09:29:36 +00001912 @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.R, trackingBug = 170729553)
Lorenzo Colitti0b798a82015-06-15 14:29:22 +09001913 public PacketKeepalive startNattKeepalive(
1914 Network network, int intervalSeconds, PacketKeepaliveCallback callback,
1915 InetAddress srcAddr, int srcPort, InetAddress dstAddr) {
1916 final PacketKeepalive k = new PacketKeepalive(network, callback);
1917 try {
junyulai070f9ff2019-01-16 20:23:34 +08001918 mService.startNattKeepalive(network, intervalSeconds, k.mCallback,
Lorenzo Colitti0b798a82015-06-15 14:29:22 +09001919 srcAddr.getHostAddress(), srcPort, dstAddr.getHostAddress());
1920 } catch (RemoteException e) {
1921 Log.e(TAG, "Error starting packet keepalive: ", e);
junyulai070f9ff2019-01-16 20:23:34 +08001922 throw e.rethrowFromSystemServer();
Lorenzo Colitti0b798a82015-06-15 14:29:22 +09001923 }
1924 return k;
1925 }
1926
Chiachang Wang619319a2021-01-15 11:06:21 +08001927 // Construct an invalid fd.
1928 private ParcelFileDescriptor createInvalidFd() {
1929 final int invalidFd = -1;
1930 return ParcelFileDescriptor.adoptFd(invalidFd);
1931 }
1932
The Android Open Source Project28527d22009-03-03 19:31:44 -08001933 /**
junyulai4c95b082018-12-27 17:25:29 +08001934 * Request that keepalives be started on a IPsec NAT-T socket.
1935 *
1936 * @param network The {@link Network} the socket is on.
1937 * @param socket The socket that needs to be kept alive.
1938 * @param source The source address of the {@link UdpEncapsulationSocket}.
1939 * @param destination The destination address of the {@link UdpEncapsulationSocket}.
1940 * @param executor The executor on which callback will be invoked. The provided {@link Executor}
1941 * must run callback sequentially, otherwise the order of callbacks cannot be
1942 * guaranteed.
1943 * @param callback A {@link SocketKeepalive.Callback}. Used for notifications about keepalive
1944 * changes. Must be extended by applications that use this API.
1945 *
junyulai0835a1e2019-01-08 20:04:33 +08001946 * @return A {@link SocketKeepalive} object that can be used to control the keepalive on the
1947 * given socket.
junyulai4c95b082018-12-27 17:25:29 +08001948 **/
junyulai7e06ad42019-03-04 22:45:36 +08001949 public @NonNull SocketKeepalive createSocketKeepalive(@NonNull Network network,
junyulai4c95b082018-12-27 17:25:29 +08001950 @NonNull UdpEncapsulationSocket socket,
1951 @NonNull InetAddress source,
1952 @NonNull InetAddress destination,
1953 @NonNull @CallbackExecutor Executor executor,
1954 @NonNull Callback callback) {
junyulai7e06ad42019-03-04 22:45:36 +08001955 ParcelFileDescriptor dup;
1956 try {
junyulai828dad12019-03-27 11:00:37 +08001957 // Dup is needed here as the pfd inside the socket is owned by the IpSecService,
1958 // which cannot be obtained by the app process.
junyulai7e06ad42019-03-04 22:45:36 +08001959 dup = ParcelFileDescriptor.dup(socket.getFileDescriptor());
1960 } catch (IOException ignored) {
1961 // Construct an invalid fd, so that if the user later calls start(), it will fail with
1962 // ERROR_INVALID_SOCKET.
Chiachang Wang619319a2021-01-15 11:06:21 +08001963 dup = createInvalidFd();
junyulai7e06ad42019-03-04 22:45:36 +08001964 }
1965 return new NattSocketKeepalive(mService, network, dup, socket.getResourceId(), source,
1966 destination, executor, callback);
junyulaid05a1922019-01-15 11:32:44 +08001967 }
1968
1969 /**
1970 * Request that keepalives be started on a IPsec NAT-T socket file descriptor. Directly called
1971 * by system apps which don't use IpSecService to create {@link UdpEncapsulationSocket}.
1972 *
1973 * @param network The {@link Network} the socket is on.
junyulai7e06ad42019-03-04 22:45:36 +08001974 * @param pfd The {@link ParcelFileDescriptor} that needs to be kept alive. The provided
1975 * {@link ParcelFileDescriptor} must be bound to a port and the keepalives will be sent
1976 * from that port.
junyulaid05a1922019-01-15 11:32:44 +08001977 * @param source The source address of the {@link UdpEncapsulationSocket}.
1978 * @param destination The destination address of the {@link UdpEncapsulationSocket}. The
1979 * keepalive packets will always be sent to port 4500 of the given {@code destination}.
1980 * @param executor The executor on which callback will be invoked. The provided {@link Executor}
1981 * must run callback sequentially, otherwise the order of callbacks cannot be
1982 * guaranteed.
1983 * @param callback A {@link SocketKeepalive.Callback}. Used for notifications about keepalive
1984 * changes. Must be extended by applications that use this API.
1985 *
junyulai0835a1e2019-01-08 20:04:33 +08001986 * @return A {@link SocketKeepalive} object that can be used to control the keepalive on the
1987 * given socket.
junyulaid05a1922019-01-15 11:32:44 +08001988 * @hide
1989 */
1990 @SystemApi
1991 @RequiresPermission(android.Manifest.permission.PACKET_KEEPALIVE_OFFLOAD)
junyulai7e06ad42019-03-04 22:45:36 +08001992 public @NonNull SocketKeepalive createNattKeepalive(@NonNull Network network,
1993 @NonNull ParcelFileDescriptor pfd,
junyulaid05a1922019-01-15 11:32:44 +08001994 @NonNull InetAddress source,
1995 @NonNull InetAddress destination,
1996 @NonNull @CallbackExecutor Executor executor,
1997 @NonNull Callback callback) {
junyulai7e06ad42019-03-04 22:45:36 +08001998 ParcelFileDescriptor dup;
1999 try {
junyulai828dad12019-03-27 11:00:37 +08002000 // TODO: Consider remove unnecessary dup.
junyulai7e06ad42019-03-04 22:45:36 +08002001 dup = pfd.dup();
2002 } catch (IOException ignored) {
2003 // Construct an invalid fd, so that if the user later calls start(), it will fail with
2004 // ERROR_INVALID_SOCKET.
Chiachang Wang619319a2021-01-15 11:06:21 +08002005 dup = createInvalidFd();
junyulai7e06ad42019-03-04 22:45:36 +08002006 }
2007 return new NattSocketKeepalive(mService, network, dup,
Remi NGUYEN VANce355502021-03-11 10:56:49 +00002008 -1 /* Unused */, source, destination, executor, callback);
junyulai4c95b082018-12-27 17:25:29 +08002009 }
2010
2011 /**
junyulai0835a1e2019-01-08 20:04:33 +08002012 * Request that keepalives be started on a TCP socket.
2013 * The socket must be established.
2014 *
2015 * @param network The {@link Network} the socket is on.
2016 * @param socket The socket that needs to be kept alive.
2017 * @param executor The executor on which callback will be invoked. This implementation assumes
2018 * the provided {@link Executor} runs the callbacks in sequence with no
2019 * concurrency. Failing this, no guarantee of correctness can be made. It is
2020 * the responsibility of the caller to ensure the executor provides this
2021 * guarantee. A simple way of creating such an executor is with the standard
2022 * tool {@code Executors.newSingleThreadExecutor}.
2023 * @param callback A {@link SocketKeepalive.Callback}. Used for notifications about keepalive
2024 * changes. Must be extended by applications that use this API.
2025 *
2026 * @return A {@link SocketKeepalive} object that can be used to control the keepalive on the
2027 * given socket.
2028 * @hide
2029 */
2030 @SystemApi
2031 @RequiresPermission(android.Manifest.permission.PACKET_KEEPALIVE_OFFLOAD)
junyulai7e06ad42019-03-04 22:45:36 +08002032 public @NonNull SocketKeepalive createSocketKeepalive(@NonNull Network network,
junyulai0835a1e2019-01-08 20:04:33 +08002033 @NonNull Socket socket,
2034 @NonNull Executor executor,
2035 @NonNull Callback callback) {
junyulai7e06ad42019-03-04 22:45:36 +08002036 ParcelFileDescriptor dup;
2037 try {
2038 dup = ParcelFileDescriptor.fromSocket(socket);
2039 } catch (UncheckedIOException ignored) {
2040 // Construct an invalid fd, so that if the user later calls start(), it will fail with
2041 // ERROR_INVALID_SOCKET.
Chiachang Wang619319a2021-01-15 11:06:21 +08002042 dup = createInvalidFd();
junyulai7e06ad42019-03-04 22:45:36 +08002043 }
2044 return new TcpSocketKeepalive(mService, network, dup, executor, callback);
junyulai0835a1e2019-01-08 20:04:33 +08002045 }
2046
2047 /**
The Android Open Source Project28527d22009-03-03 19:31:44 -08002048 * Ensure that a network route exists to deliver traffic to the specified
2049 * host via the specified network interface. An attempt to add a route that
2050 * already exists is ignored, but treated as successful.
Lorenzo Colitti0b40c872015-10-15 16:29:00 +09002051 *
2052 * <p>This method requires the caller to hold either the
2053 * {@link android.Manifest.permission#CHANGE_NETWORK_STATE} permission
2054 * or the ability to modify system settings as determined by
2055 * {@link android.provider.Settings.System#canWrite}.</p>
2056 *
The Android Open Source Project28527d22009-03-03 19:31:44 -08002057 * @param networkType the type of the network over which traffic to the specified
2058 * host is to be routed
2059 * @param hostAddress the IP address of the host to which the route is desired
2060 * @return {@code true} on success, {@code false} on failure
Robert Greenwaltf3017f72014-05-18 23:07:25 -07002061 *
Lorenzo Colitti7f6a2bf2015-04-24 17:03:31 +09002062 * @deprecated Deprecated in favor of the
2063 * {@link #requestNetwork(NetworkRequest, NetworkCallback)},
2064 * {@link #bindProcessToNetwork} and {@link Network#getSocketFactory} API.
Dianne Hackborn18c1d832015-07-31 10:35:34 -07002065 * In {@link VERSION_CODES#M}, and above, this method is unsupported and will
Lorenzo Colitti2780e4b2015-07-29 11:41:21 +09002066 * throw {@code UnsupportedOperationException} if called.
Lorenzo Colitti84d7f072016-12-09 18:39:30 +09002067 * @removed
The Android Open Source Project28527d22009-03-03 19:31:44 -08002068 */
Aurimas Liutikase80a0eb2016-05-24 15:22:55 -07002069 @Deprecated
The Android Open Source Project28527d22009-03-03 19:31:44 -08002070 public boolean requestRouteToHost(int networkType, int hostAddress) {
Sreeram Ramachandranc06ec732014-07-19 23:21:46 -07002071 return requestRouteToHostAddress(networkType, NetworkUtils.intToInetAddress(hostAddress));
Robert Greenwalt7fe44cb2010-08-27 09:24:29 -07002072 }
2073
2074 /**
2075 * Ensure that a network route exists to deliver traffic to the specified
2076 * host via the specified network interface. An attempt to add a route that
2077 * already exists is ignored, but treated as successful.
Lorenzo Colitti0b40c872015-10-15 16:29:00 +09002078 *
2079 * <p>This method requires the caller to hold either the
2080 * {@link android.Manifest.permission#CHANGE_NETWORK_STATE} permission
2081 * or the ability to modify system settings as determined by
2082 * {@link android.provider.Settings.System#canWrite}.</p>
2083 *
Robert Greenwalt7fe44cb2010-08-27 09:24:29 -07002084 * @param networkType the type of the network over which traffic to the specified
2085 * host is to be routed
2086 * @param hostAddress the IP address of the host to which the route is desired
2087 * @return {@code true} on success, {@code false} on failure
2088 * @hide
Robert Greenwaltf3017f72014-05-18 23:07:25 -07002089 * @deprecated Deprecated in favor of the {@link #requestNetwork} and
Lorenzo Colitti7f6a2bf2015-04-24 17:03:31 +09002090 * {@link #bindProcessToNetwork} API.
Robert Greenwalt7fe44cb2010-08-27 09:24:29 -07002091 */
Aurimas Liutikase80a0eb2016-05-24 15:22:55 -07002092 @Deprecated
Mathew Inwoodc5b9bec2018-08-08 14:52:47 +01002093 @UnsupportedAppUsage
Robert Greenwalt7fe44cb2010-08-27 09:24:29 -07002094 public boolean requestRouteToHostAddress(int networkType, InetAddress hostAddress) {
Lorenzo Colitti2780e4b2015-07-29 11:41:21 +09002095 checkLegacyRoutingApiAccess();
The Android Open Source Project28527d22009-03-03 19:31:44 -08002096 try {
Philip P. Moltmann7bc33df2020-03-26 11:50:35 -07002097 return mService.requestRouteToHostAddress(networkType, hostAddress.getAddress(),
2098 mContext.getOpPackageName(), getAttributionTag());
The Android Open Source Project28527d22009-03-03 19:31:44 -08002099 } catch (RemoteException e) {
Jeff Sharkeyc78f85c2016-03-01 19:27:23 -07002100 throw e.rethrowFromSystemServer();
The Android Open Source Project28527d22009-03-03 19:31:44 -08002101 }
2102 }
2103
2104 /**
Philip P. Moltmann7bc33df2020-03-26 11:50:35 -07002105 * @return the context's attribution tag
2106 */
2107 // TODO: Remove method and replace with direct call once R code is pushed to AOSP
2108 private @Nullable String getAttributionTag() {
Roshan Piusaa24fde2020-12-17 14:53:09 -08002109 return mContext.getAttributionTag();
Philip P. Moltmann7bc33df2020-03-26 11:50:35 -07002110 }
2111
2112 /**
The Android Open Source Project28527d22009-03-03 19:31:44 -08002113 * Returns the value of the setting for background data usage. If false,
2114 * applications should not use the network if the application is not in the
2115 * foreground. Developers should respect this setting, and check the value
2116 * of this before performing any background data operations.
2117 * <p>
2118 * All applications that have background services that use the network
2119 * should listen to {@link #ACTION_BACKGROUND_DATA_SETTING_CHANGED}.
Jeff Sharkey39c01eb2011-08-16 14:37:57 -07002120 * <p>
Scott Main50589142011-10-06 18:32:43 -07002121 * @deprecated As of {@link VERSION_CODES#ICE_CREAM_SANDWICH}, availability of
Jeff Sharkey39c01eb2011-08-16 14:37:57 -07002122 * background data depends on several combined factors, and this method will
2123 * always return {@code true}. Instead, when background data is unavailable,
2124 * {@link #getActiveNetworkInfo()} will now appear disconnected.
Danica Chang96567052010-08-11 14:54:43 -07002125 *
The Android Open Source Project28527d22009-03-03 19:31:44 -08002126 * @return Whether background data usage is allowed.
2127 */
Jeff Sharkey39c01eb2011-08-16 14:37:57 -07002128 @Deprecated
The Android Open Source Project28527d22009-03-03 19:31:44 -08002129 public boolean getBackgroundDataSetting() {
Jeff Sharkey39c01eb2011-08-16 14:37:57 -07002130 // assume that background data is allowed; final authority is
2131 // NetworkInfo which may be blocked.
2132 return true;
The Android Open Source Project28527d22009-03-03 19:31:44 -08002133 }
2134
2135 /**
2136 * Sets the value of the setting for background data usage.
Robert Greenwalt1b21f6c2010-02-23 18:58:05 -08002137 *
The Android Open Source Project28527d22009-03-03 19:31:44 -08002138 * @param allowBackgroundData Whether an application should use data while
2139 * it is in the background.
Robert Greenwalt1b21f6c2010-02-23 18:58:05 -08002140 *
The Android Open Source Project28527d22009-03-03 19:31:44 -08002141 * @attr ref android.Manifest.permission#CHANGE_BACKGROUND_DATA_SETTING
2142 * @see #getBackgroundDataSetting()
2143 * @hide
2144 */
Jeff Sharkey39c01eb2011-08-16 14:37:57 -07002145 @Deprecated
Mathew Inwoodc5b9bec2018-08-08 14:52:47 +01002146 @UnsupportedAppUsage
The Android Open Source Project28527d22009-03-03 19:31:44 -08002147 public void setBackgroundDataSetting(boolean allowBackgroundData) {
Jeff Sharkey39c01eb2011-08-16 14:37:57 -07002148 // ignored
The Android Open Source Project28527d22009-03-03 19:31:44 -08002149 }
Robert Greenwalt1b21f6c2010-02-23 18:58:05 -08002150
Jeff Sharkey66fa9682011-08-02 17:22:34 -07002151 /**
Robert Greenwalt1b21f6c2010-02-23 18:58:05 -08002152 * @hide
Robert Greenwalt0c150c02014-05-21 20:04:36 -07002153 * @deprecated Talk to TelephonyManager directly
Robert Greenwalt1b21f6c2010-02-23 18:58:05 -08002154 */
Aurimas Liutikase80a0eb2016-05-24 15:22:55 -07002155 @Deprecated
Mathew Inwoodc5b9bec2018-08-08 14:52:47 +01002156 @UnsupportedAppUsage
Robert Greenwalt1b21f6c2010-02-23 18:58:05 -08002157 public boolean getMobileDataEnabled() {
Meng Wanged6f4412019-11-18 17:10:00 -08002158 TelephonyManager tm = mContext.getSystemService(TelephonyManager.class);
2159 if (tm != null) {
2160 int subId = SubscriptionManager.getDefaultDataSubscriptionId();
2161 Log.d("ConnectivityManager", "getMobileDataEnabled()+ subId=" + subId);
2162 boolean retVal = tm.createForSubscriptionId(subId).isDataEnabled();
2163 Log.d("ConnectivityManager", "getMobileDataEnabled()- subId=" + subId
2164 + " retVal=" + retVal);
2165 return retVal;
Robert Greenwalt1b21f6c2010-02-23 18:58:05 -08002166 }
Wink Saville689f7042014-12-05 11:10:30 -08002167 Log.d("ConnectivityManager", "getMobileDataEnabled()- remote exception retVal=false");
Robert Greenwalt0c150c02014-05-21 20:04:36 -07002168 return false;
Robert Greenwalt1b21f6c2010-02-23 18:58:05 -08002169 }
2170
The Android Open Source Project28527d22009-03-03 19:31:44 -08002171 /**
Robert Greenwaltad35b132014-09-04 16:44:35 -07002172 * Callback for use with {@link ConnectivityManager#addDefaultNetworkActiveListener}
Robert Greenwaltf57c03c2014-06-11 16:05:07 -07002173 * to find out when the system default network has gone in to a high power state.
Dianne Hackborn5ac88162014-02-26 16:20:52 -08002174 */
2175 public interface OnNetworkActiveListener {
2176 /**
2177 * Called on the main thread of the process to report that the current data network
2178 * has become active, and it is now a good time to perform any pending network
2179 * operations. Note that this listener only tells you when the network becomes
2180 * active; if at any other time you want to know whether it is active (and thus okay
2181 * to initiate network traffic), you can retrieve its instantaneous state with
Robert Greenwaltf57c03c2014-06-11 16:05:07 -07002182 * {@link ConnectivityManager#isDefaultNetworkActive}.
Dianne Hackborn5ac88162014-02-26 16:20:52 -08002183 */
Chalard Jean9dd11612018-06-04 16:52:49 +09002184 void onNetworkActive();
Dianne Hackborn5ac88162014-02-26 16:20:52 -08002185 }
2186
Dianne Hackborn5ac88162014-02-26 16:20:52 -08002187 private final ArrayMap<OnNetworkActiveListener, INetworkActivityListener>
Chalard Jean9dd11612018-06-04 16:52:49 +09002188 mNetworkActivityListeners = new ArrayMap<>();
Dianne Hackborn5ac88162014-02-26 16:20:52 -08002189
2190 /**
Robert Greenwaltf57c03c2014-06-11 16:05:07 -07002191 * Start listening to reports when the system's default data network is active, meaning it is
2192 * a good time to perform network traffic. Use {@link #isDefaultNetworkActive()}
2193 * to determine the current state of the system's default network after registering the
2194 * listener.
2195 * <p>
2196 * If the process default network has been set with
Paul Jensenee2f45d2015-03-10 10:54:12 -04002197 * {@link ConnectivityManager#bindProcessToNetwork} this function will not
Robert Greenwaltf57c03c2014-06-11 16:05:07 -07002198 * reflect the process's default, but the system default.
Dianne Hackborn5ac88162014-02-26 16:20:52 -08002199 *
2200 * @param l The listener to be told when the network is active.
2201 */
Robert Greenwaltad35b132014-09-04 16:44:35 -07002202 public void addDefaultNetworkActiveListener(final OnNetworkActiveListener l) {
Dianne Hackborn5ac88162014-02-26 16:20:52 -08002203 INetworkActivityListener rl = new INetworkActivityListener.Stub() {
2204 @Override
2205 public void onNetworkActive() throws RemoteException {
2206 l.onNetworkActive();
2207 }
2208 };
2209
2210 try {
lucaslin1193a5d2021-01-21 02:04:15 +08002211 mService.registerNetworkActivityListener(rl);
Dianne Hackborn5ac88162014-02-26 16:20:52 -08002212 mNetworkActivityListeners.put(l, rl);
2213 } catch (RemoteException e) {
Jeff Sharkeyc78f85c2016-03-01 19:27:23 -07002214 throw e.rethrowFromSystemServer();
Dianne Hackborn5ac88162014-02-26 16:20:52 -08002215 }
2216 }
2217
2218 /**
2219 * Remove network active listener previously registered with
Robert Greenwaltad35b132014-09-04 16:44:35 -07002220 * {@link #addDefaultNetworkActiveListener}.
Dianne Hackborn5ac88162014-02-26 16:20:52 -08002221 *
2222 * @param l Previously registered listener.
2223 */
Chalard Jean158702d2019-01-07 19:26:34 +09002224 public void removeDefaultNetworkActiveListener(@NonNull OnNetworkActiveListener l) {
Dianne Hackborn5ac88162014-02-26 16:20:52 -08002225 INetworkActivityListener rl = mNetworkActivityListeners.get(l);
Remi NGUYEN VAN5086c292021-03-15 07:27:44 +00002226 Preconditions.checkArgument(rl != null, "Listener was not registered.");
Dianne Hackborn5ac88162014-02-26 16:20:52 -08002227 try {
lucaslin1193a5d2021-01-21 02:04:15 +08002228 mService.registerNetworkActivityListener(rl);
Dianne Hackborn5ac88162014-02-26 16:20:52 -08002229 } catch (RemoteException e) {
Jeff Sharkeyc78f85c2016-03-01 19:27:23 -07002230 throw e.rethrowFromSystemServer();
Dianne Hackborn5ac88162014-02-26 16:20:52 -08002231 }
2232 }
2233
2234 /**
2235 * Return whether the data network is currently active. An active network means that
2236 * it is currently in a high power state for performing data transmission. On some
2237 * types of networks, it may be expensive to move and stay in such a state, so it is
2238 * more power efficient to batch network traffic together when the radio is already in
2239 * this state. This method tells you whether right now is currently a good time to
2240 * initiate network traffic, as the network is already active.
2241 */
Robert Greenwaltf57c03c2014-06-11 16:05:07 -07002242 public boolean isDefaultNetworkActive() {
Dianne Hackborn5ac88162014-02-26 16:20:52 -08002243 try {
lucaslin1193a5d2021-01-21 02:04:15 +08002244 return mService.isDefaultNetworkActive();
Dianne Hackborn5ac88162014-02-26 16:20:52 -08002245 } catch (RemoteException e) {
Jeff Sharkeyc78f85c2016-03-01 19:27:23 -07002246 throw e.rethrowFromSystemServer();
Dianne Hackborn5ac88162014-02-26 16:20:52 -08002247 }
Dianne Hackborn5ac88162014-02-26 16:20:52 -08002248 }
2249
2250 /**
The Android Open Source Project28527d22009-03-03 19:31:44 -08002251 * {@hide}
2252 */
Lorenzo Colitti2780e4b2015-07-29 11:41:21 +09002253 public ConnectivityManager(Context context, IConnectivityManager service) {
Remi NGUYEN VAN5086c292021-03-15 07:27:44 +00002254 mContext = Preconditions.checkNotNull(context, "missing context");
2255 mService = Preconditions.checkNotNull(service, "missing IConnectivityManager");
Amos Bianchia9b415a2020-03-04 11:07:38 -08002256 mTetheringManager = (TetheringManager) mContext.getSystemService(Context.TETHERING_SERVICE);
Paul Jensenc0618a62014-12-10 15:12:18 -05002257 sInstance = this;
The Android Open Source Project28527d22009-03-03 19:31:44 -08002258 }
Robert Greenwalt0c4828c2010-01-26 11:40:34 -08002259
Jeff Sharkey2d9e5a52012-04-04 20:40:58 -07002260 /** {@hide} */
Mathew Inwoodc5b9bec2018-08-08 14:52:47 +01002261 @UnsupportedAppUsage
Jeff Sharkey2d9e5a52012-04-04 20:40:58 -07002262 public static ConnectivityManager from(Context context) {
2263 return (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
2264 }
2265
Remi NGUYEN VAN85391292018-12-27 16:43:56 +09002266 /** @hide */
2267 public NetworkRequest getDefaultRequest() {
2268 try {
2269 // This is not racy as the default request is final in ConnectivityService.
2270 return mService.getDefaultRequest();
2271 } catch (RemoteException e) {
2272 throw e.rethrowFromSystemServer();
2273 }
2274 }
2275
Robert Greenwalt0c4828c2010-01-26 11:40:34 -08002276 /**
Philip P. Moltmann7bc33df2020-03-26 11:50:35 -07002277 * Check if the package is a allowed to write settings. This also accounts that such an access
2278 * happened.
2279 *
2280 * @return {@code true} iff the package is allowed to write settings.
2281 */
2282 // TODO: Remove method and replace with direct call once R code is pushed to AOSP
2283 private static boolean checkAndNoteWriteSettingsOperation(@NonNull Context context, int uid,
2284 @NonNull String callingPackage, @Nullable String callingAttributionTag,
2285 boolean throwException) {
2286 return Settings.checkAndNoteWriteSettingsOperation(context, uid, callingPackage,
2287 throwException);
2288 }
2289
2290 /**
Paul Jensenc0618a62014-12-10 15:12:18 -05002291 * @deprecated - use getSystemService. This is a kludge to support static access in certain
2292 * situations where a Context pointer is unavailable.
2293 * @hide
2294 */
Aurimas Liutikase80a0eb2016-05-24 15:22:55 -07002295 @Deprecated
Paul Jensenee2f45d2015-03-10 10:54:12 -04002296 static ConnectivityManager getInstanceOrNull() {
2297 return sInstance;
2298 }
2299
2300 /**
2301 * @deprecated - use getSystemService. This is a kludge to support static access in certain
2302 * situations where a Context pointer is unavailable.
2303 * @hide
2304 */
Aurimas Liutikase80a0eb2016-05-24 15:22:55 -07002305 @Deprecated
Mathew Inwoodc5b9bec2018-08-08 14:52:47 +01002306 @UnsupportedAppUsage
Paul Jensenee2f45d2015-03-10 10:54:12 -04002307 private static ConnectivityManager getInstance() {
2308 if (getInstanceOrNull() == null) {
Paul Jensenc0618a62014-12-10 15:12:18 -05002309 throw new IllegalStateException("No ConnectivityManager yet constructed");
2310 }
Paul Jensenee2f45d2015-03-10 10:54:12 -04002311 return getInstanceOrNull();
Paul Jensenc0618a62014-12-10 15:12:18 -05002312 }
2313
2314 /**
Robert Greenwaltfaa4b402013-02-15 10:56:35 -08002315 * Get the set of tetherable, available interfaces. This list is limited by
2316 * device configuration and current interface existence.
2317 *
2318 * @return an array of 0 or more Strings of tetherable interface names.
2319 *
markchien6ae63e52020-01-21 13:11:06 +08002320 * @deprecated Use {@link TetheringEventCallback#onTetherableInterfacesChanged(List)} instead.
Robert Greenwalt0c4828c2010-01-26 11:40:34 -08002321 * {@hide}
2322 */
Jeff Sharkey656584a2017-04-24 11:18:03 -06002323 @RequiresPermission(android.Manifest.permission.ACCESS_NETWORK_STATE)
Mathew Inwoodc5b9bec2018-08-08 14:52:47 +01002324 @UnsupportedAppUsage
markchien6ae63e52020-01-21 13:11:06 +08002325 @Deprecated
Robert Greenwalt0c4828c2010-01-26 11:40:34 -08002326 public String[] getTetherableIfaces() {
Amos Bianchia9b415a2020-03-04 11:07:38 -08002327 return mTetheringManager.getTetherableIfaces();
Robert Greenwalt0c4828c2010-01-26 11:40:34 -08002328 }
2329
2330 /**
Robert Greenwaltfaa4b402013-02-15 10:56:35 -08002331 * Get the set of tethered interfaces.
2332 *
2333 * @return an array of 0 or more String of currently tethered interface names.
2334 *
markchien6ae63e52020-01-21 13:11:06 +08002335 * @deprecated Use {@link TetheringEventCallback#onTetherableInterfacesChanged(List)} instead.
Robert Greenwalt0c4828c2010-01-26 11:40:34 -08002336 * {@hide}
2337 */
Jeff Sharkey656584a2017-04-24 11:18:03 -06002338 @RequiresPermission(android.Manifest.permission.ACCESS_NETWORK_STATE)
Mathew Inwoodc5b9bec2018-08-08 14:52:47 +01002339 @UnsupportedAppUsage
markchien6ae63e52020-01-21 13:11:06 +08002340 @Deprecated
Robert Greenwalt0c4828c2010-01-26 11:40:34 -08002341 public String[] getTetheredIfaces() {
Amos Bianchia9b415a2020-03-04 11:07:38 -08002342 return mTetheringManager.getTetheredIfaces();
Robert Greenwalt0c4828c2010-01-26 11:40:34 -08002343 }
2344
2345 /**
Robert Greenwaltfaa4b402013-02-15 10:56:35 -08002346 * Get the set of interface names which attempted to tether but
2347 * failed. Re-attempting to tether may cause them to reset to the Tethered
2348 * state. Alternatively, causing the interface to be destroyed and recreated
2349 * may cause them to reset to the available state.
2350 * {@link ConnectivityManager#getLastTetherError} can be used to get more
2351 * information on the cause of the errors.
2352 *
2353 * @return an array of 0 or more String indicating the interface names
2354 * which failed to tether.
2355 *
markchien6ae63e52020-01-21 13:11:06 +08002356 * @deprecated Use {@link TetheringEventCallback#onError(String, int)} instead.
Robert Greenwalt0c4828c2010-01-26 11:40:34 -08002357 * {@hide}
2358 */
Jeff Sharkey656584a2017-04-24 11:18:03 -06002359 @RequiresPermission(android.Manifest.permission.ACCESS_NETWORK_STATE)
Mathew Inwoodc5b9bec2018-08-08 14:52:47 +01002360 @UnsupportedAppUsage
markchien6ae63e52020-01-21 13:11:06 +08002361 @Deprecated
Robert Greenwalt4283ded2010-03-02 17:25:02 -08002362 public String[] getTetheringErroredIfaces() {
Amos Bianchia9b415a2020-03-04 11:07:38 -08002363 return mTetheringManager.getTetheringErroredIfaces();
Robert Greenwalt0c4828c2010-01-26 11:40:34 -08002364 }
2365
2366 /**
Robert Greenwalte594a762014-06-23 14:53:42 -07002367 * Get the set of tethered dhcp ranges.
2368 *
markchien2e6ba522020-02-14 11:55:48 +08002369 * @deprecated This method is not supported.
2370 * TODO: remove this function when all of clients are removed.
Robert Greenwalte594a762014-06-23 14:53:42 -07002371 * {@hide}
2372 */
paulhu8e96a752019-08-12 16:25:11 +08002373 @RequiresPermission(android.Manifest.permission.NETWORK_SETTINGS)
markchien6ae63e52020-01-21 13:11:06 +08002374 @Deprecated
Robert Greenwalte594a762014-06-23 14:53:42 -07002375 public String[] getTetheredDhcpRanges() {
markchien2e6ba522020-02-14 11:55:48 +08002376 throw new UnsupportedOperationException("getTetheredDhcpRanges is not supported");
Robert Greenwalte594a762014-06-23 14:53:42 -07002377 }
2378
2379 /**
Robert Greenwaltfaa4b402013-02-15 10:56:35 -08002380 * Attempt to tether the named interface. This will setup a dhcp server
2381 * on the interface, forward and NAT IP packets and forward DNS requests
2382 * to the best active upstream network interface. Note that if no upstream
2383 * IP network interface is available, dhcp will still run and traffic will be
2384 * allowed between the tethered devices and this device, though upstream net
2385 * access will of course fail until an upstream network interface becomes
2386 * active.
Lorenzo Colitti0b40c872015-10-15 16:29:00 +09002387 *
2388 * <p>This method requires the caller to hold either the
2389 * {@link android.Manifest.permission#CHANGE_NETWORK_STATE} permission
2390 * or the ability to modify system settings as determined by
2391 * {@link android.provider.Settings.System#canWrite}.</p>
Robert Greenwaltfaa4b402013-02-15 10:56:35 -08002392 *
Jeremy Klein3dabcb92016-01-22 14:11:45 -08002393 * <p>WARNING: New clients should not use this function. The only usages should be in PanService
2394 * and WifiStateMachine which need direct access. All other clients should use
2395 * {@link #startTethering} and {@link #stopTethering} which encapsulate proper provisioning
2396 * logic.</p>
2397 *
Robert Greenwaltfaa4b402013-02-15 10:56:35 -08002398 * @param iface the interface name to tether.
2399 * @return error a {@code TETHER_ERROR} value indicating success or failure type
markchien91c78e52020-01-20 19:31:56 +08002400 * @deprecated Use {@link TetheringManager#startTethering} instead
Robert Greenwaltfaa4b402013-02-15 10:56:35 -08002401 *
Robert Greenwalt0c4828c2010-01-26 11:40:34 -08002402 * {@hide}
2403 */
Mathew Inwoode1a17ba2020-11-04 09:29:36 +00002404 @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.R, trackingBug = 170729553)
markchien91c78e52020-01-20 19:31:56 +08002405 @Deprecated
Robert Greenwalt4283ded2010-03-02 17:25:02 -08002406 public int tether(String iface) {
Amos Bianchia9b415a2020-03-04 11:07:38 -08002407 return mTetheringManager.tether(iface);
Robert Greenwalt4283ded2010-03-02 17:25:02 -08002408 }
2409
2410 /**
Robert Greenwaltfaa4b402013-02-15 10:56:35 -08002411 * Stop tethering the named interface.
Lorenzo Colitti0b40c872015-10-15 16:29:00 +09002412 *
2413 * <p>This method requires the caller to hold either the
2414 * {@link android.Manifest.permission#CHANGE_NETWORK_STATE} permission
2415 * or the ability to modify system settings as determined by
2416 * {@link android.provider.Settings.System#canWrite}.</p>
Robert Greenwaltfaa4b402013-02-15 10:56:35 -08002417 *
Jeremy Klein3dabcb92016-01-22 14:11:45 -08002418 * <p>WARNING: New clients should not use this function. The only usages should be in PanService
2419 * and WifiStateMachine which need direct access. All other clients should use
2420 * {@link #startTethering} and {@link #stopTethering} which encapsulate proper provisioning
2421 * logic.</p>
2422 *
Robert Greenwaltfaa4b402013-02-15 10:56:35 -08002423 * @param iface the interface name to untether.
2424 * @return error a {@code TETHER_ERROR} value indicating success or failure type
2425 *
Robert Greenwalt4283ded2010-03-02 17:25:02 -08002426 * {@hide}
2427 */
Mathew Inwoodc5b9bec2018-08-08 14:52:47 +01002428 @UnsupportedAppUsage
markchien6ae63e52020-01-21 13:11:06 +08002429 @Deprecated
Robert Greenwalt4283ded2010-03-02 17:25:02 -08002430 public int untether(String iface) {
Amos Bianchia9b415a2020-03-04 11:07:38 -08002431 return mTetheringManager.untether(iface);
Robert Greenwalt0c4828c2010-01-26 11:40:34 -08002432 }
Robert Greenwalt8e87f122010-02-11 18:18:40 -08002433
2434 /**
Robert Greenwaltfaa4b402013-02-15 10:56:35 -08002435 * Check if the device allows for tethering. It may be disabled via
Dianne Hackborn5ac88162014-02-26 16:20:52 -08002436 * {@code ro.tether.denied} system property, Settings.TETHER_SUPPORTED or
Robert Greenwaltfaa4b402013-02-15 10:56:35 -08002437 * due to device configuration.
2438 *
Chalard Jeanffacaef2017-09-26 15:45:18 +09002439 * <p>If this app does not have permission to use this API, it will always
2440 * return false rather than throw an exception.</p>
2441 *
2442 * <p>If the device has a hotspot provisioning app, the caller is required to hold the
2443 * {@link android.Manifest.permission.TETHER_PRIVILEGED} permission.</p>
2444 *
2445 * <p>Otherwise, this method requires the caller to hold the ability to modify system
2446 * settings as determined by {@link android.provider.Settings.System#canWrite}.</p>
2447 *
Robert Greenwaltfaa4b402013-02-15 10:56:35 -08002448 * @return a boolean - {@code true} indicating Tethering is supported.
2449 *
markchien6ae63e52020-01-21 13:11:06 +08002450 * @deprecated Use {@link TetheringEventCallback#onTetheringSupported(boolean)} instead.
Robert Greenwalt8e87f122010-02-11 18:18:40 -08002451 * {@hide}
2452 */
Jeremy Klein3dabcb92016-01-22 14:11:45 -08002453 @SystemApi
Chalard Jeanffacaef2017-09-26 15:45:18 +09002454 @RequiresPermission(anyOf = {android.Manifest.permission.TETHER_PRIVILEGED,
2455 android.Manifest.permission.WRITE_SETTINGS})
Robert Greenwalt8e87f122010-02-11 18:18:40 -08002456 public boolean isTetheringSupported() {
Amos Bianchia9b415a2020-03-04 11:07:38 -08002457 return mTetheringManager.isTetheringSupported();
Robert Greenwalt8e87f122010-02-11 18:18:40 -08002458 }
2459
2460 /**
Jeremy Klein3dabcb92016-01-22 14:11:45 -08002461 * Callback for use with {@link #startTethering} to find out whether tethering succeeded.
markchien91c78e52020-01-20 19:31:56 +08002462 *
2463 * @deprecated Use {@link TetheringManager.StartTetheringCallback} instead.
Jeremy Klein3dabcb92016-01-22 14:11:45 -08002464 * @hide
2465 */
2466 @SystemApi
markchien91c78e52020-01-20 19:31:56 +08002467 @Deprecated
Jeremy Klein3dabcb92016-01-22 14:11:45 -08002468 public static abstract class OnStartTetheringCallback {
2469 /**
2470 * Called when tethering has been successfully started.
2471 */
Chalard Jean9dd11612018-06-04 16:52:49 +09002472 public void onTetheringStarted() {}
Jeremy Klein3dabcb92016-01-22 14:11:45 -08002473
2474 /**
2475 * Called when starting tethering failed.
2476 */
Chalard Jean9dd11612018-06-04 16:52:49 +09002477 public void onTetheringFailed() {}
Jeremy Klein3dabcb92016-01-22 14:11:45 -08002478 }
2479
2480 /**
2481 * Convenient overload for
2482 * {@link #startTethering(int, boolean, OnStartTetheringCallback, Handler)} which passes a null
2483 * handler to run on the current thread's {@link Looper}.
markchien91c78e52020-01-20 19:31:56 +08002484 *
2485 * @deprecated Use {@link TetheringManager#startTethering} instead.
Jeremy Klein3dabcb92016-01-22 14:11:45 -08002486 * @hide
2487 */
2488 @SystemApi
markchien91c78e52020-01-20 19:31:56 +08002489 @Deprecated
Udam Saini8f7d6a72017-06-07 12:06:28 -07002490 @RequiresPermission(android.Manifest.permission.TETHER_PRIVILEGED)
Jeremy Klein3dabcb92016-01-22 14:11:45 -08002491 public void startTethering(int type, boolean showProvisioningUi,
2492 final OnStartTetheringCallback callback) {
2493 startTethering(type, showProvisioningUi, callback, null);
2494 }
2495
2496 /**
2497 * Runs tether provisioning for the given type if needed and then starts tethering if
2498 * the check succeeds. If no carrier provisioning is required for tethering, tethering is
2499 * enabled immediately. If provisioning fails, tethering will not be enabled. It also
2500 * schedules tether provisioning re-checks if appropriate.
2501 *
2502 * @param type The type of tethering to start. Must be one of
2503 * {@link ConnectivityManager.TETHERING_WIFI},
2504 * {@link ConnectivityManager.TETHERING_USB}, or
2505 * {@link ConnectivityManager.TETHERING_BLUETOOTH}.
2506 * @param showProvisioningUi a boolean indicating to show the provisioning app UI if there
2507 * is one. This should be true the first time this function is called and also any time
2508 * the user can see this UI. It gives users information from their carrier about the
2509 * check failing and how they can sign up for tethering if possible.
2510 * @param callback an {@link OnStartTetheringCallback} which will be called to notify the caller
2511 * of the result of trying to tether.
2512 * @param handler {@link Handler} to specify the thread upon which the callback will be invoked.
markchien91c78e52020-01-20 19:31:56 +08002513 *
2514 * @deprecated Use {@link TetheringManager#startTethering} instead.
Jeremy Klein3dabcb92016-01-22 14:11:45 -08002515 * @hide
2516 */
2517 @SystemApi
markchien91c78e52020-01-20 19:31:56 +08002518 @Deprecated
Jeff Sharkey9b39e9a2017-06-02 17:36:26 -06002519 @RequiresPermission(android.Manifest.permission.TETHER_PRIVILEGED)
Jeremy Klein3dabcb92016-01-22 14:11:45 -08002520 public void startTethering(int type, boolean showProvisioningUi,
2521 final OnStartTetheringCallback callback, Handler handler) {
Remi NGUYEN VAN5086c292021-03-15 07:27:44 +00002522 Preconditions.checkNotNull(callback, "OnStartTetheringCallback cannot be null.");
Jeremy Klein35e99ea2016-03-12 16:29:54 -08002523
markchien91c78e52020-01-20 19:31:56 +08002524 final Executor executor = new Executor() {
Jeremy Klein3dabcb92016-01-22 14:11:45 -08002525 @Override
markchien91c78e52020-01-20 19:31:56 +08002526 public void execute(Runnable command) {
2527 if (handler == null) {
2528 command.run();
Jeremy Klein3dabcb92016-01-22 14:11:45 -08002529 } else {
markchien91c78e52020-01-20 19:31:56 +08002530 handler.post(command);
Jeremy Klein3dabcb92016-01-22 14:11:45 -08002531 }
2532 }
2533 };
Jeremy Klein35e99ea2016-03-12 16:29:54 -08002534
markchien91c78e52020-01-20 19:31:56 +08002535 final StartTetheringCallback tetheringCallback = new StartTetheringCallback() {
2536 @Override
2537 public void onTetheringStarted() {
2538 callback.onTetheringStarted();
2539 }
2540
2541 @Override
markchienf47d8342020-03-19 13:37:43 +08002542 public void onTetheringFailed(final int error) {
markchien91c78e52020-01-20 19:31:56 +08002543 callback.onTetheringFailed();
2544 }
2545 };
2546
2547 final TetheringRequest request = new TetheringRequest.Builder(type)
markchienf47d8342020-03-19 13:37:43 +08002548 .setShouldShowEntitlementUi(showProvisioningUi).build();
markchien91c78e52020-01-20 19:31:56 +08002549
Amos Bianchia9b415a2020-03-04 11:07:38 -08002550 mTetheringManager.startTethering(request, executor, tetheringCallback);
Jeremy Klein3dabcb92016-01-22 14:11:45 -08002551 }
2552
2553 /**
2554 * Stops tethering for the given type. Also cancels any provisioning rechecks for that type if
2555 * applicable.
2556 *
2557 * @param type The type of tethering to stop. Must be one of
2558 * {@link ConnectivityManager.TETHERING_WIFI},
2559 * {@link ConnectivityManager.TETHERING_USB}, or
2560 * {@link ConnectivityManager.TETHERING_BLUETOOTH}.
markchien6ae63e52020-01-21 13:11:06 +08002561 *
2562 * @deprecated Use {@link TetheringManager#stopTethering} instead.
Jeremy Klein3dabcb92016-01-22 14:11:45 -08002563 * @hide
2564 */
2565 @SystemApi
markchien6ae63e52020-01-21 13:11:06 +08002566 @Deprecated
Jeff Sharkey9b39e9a2017-06-02 17:36:26 -06002567 @RequiresPermission(android.Manifest.permission.TETHER_PRIVILEGED)
Jeremy Klein3dabcb92016-01-22 14:11:45 -08002568 public void stopTethering(int type) {
Amos Bianchia9b415a2020-03-04 11:07:38 -08002569 mTetheringManager.stopTethering(type);
Jeremy Klein3dabcb92016-01-22 14:11:45 -08002570 }
2571
2572 /**
markchien4ef53e82019-02-27 14:56:11 +08002573 * Callback for use with {@link registerTetheringEventCallback} to find out tethering
2574 * upstream status.
2575 *
Nathan Harold74f0fb82020-01-23 18:03:46 -08002576 * @deprecated Use {@link TetheringManager#OnTetheringEventCallback} instead.
markchien6ae63e52020-01-21 13:11:06 +08002577 * @hide
markchien4ef53e82019-02-27 14:56:11 +08002578 */
2579 @SystemApi
markchien6ae63e52020-01-21 13:11:06 +08002580 @Deprecated
markchien4ef53e82019-02-27 14:56:11 +08002581 public abstract static class OnTetheringEventCallback {
2582
2583 /**
2584 * Called when tethering upstream changed. This can be called multiple times and can be
2585 * called any time.
2586 *
2587 * @param network the {@link Network} of tethering upstream. Null means tethering doesn't
2588 * have any upstream.
2589 */
2590 public void onUpstreamChanged(@Nullable Network network) {}
2591 }
2592
markchien6ae63e52020-01-21 13:11:06 +08002593 @GuardedBy("mTetheringEventCallbacks")
2594 private final ArrayMap<OnTetheringEventCallback, TetheringEventCallback>
2595 mTetheringEventCallbacks = new ArrayMap<>();
2596
markchien4ef53e82019-02-27 14:56:11 +08002597 /**
2598 * Start listening to tethering change events. Any new added callback will receive the last
markchien42e22092019-04-03 10:43:09 +08002599 * tethering status right away. If callback is registered when tethering has no upstream or
markchien4ef53e82019-02-27 14:56:11 +08002600 * disabled, {@link OnTetheringEventCallback#onUpstreamChanged} will immediately be called
2601 * with a null argument. The same callback object cannot be registered twice.
2602 *
2603 * @param executor the executor on which callback will be invoked.
2604 * @param callback the callback to be called when tethering has change events.
markchien6ae63e52020-01-21 13:11:06 +08002605 *
Nathan Harold74f0fb82020-01-23 18:03:46 -08002606 * @deprecated Use {@link TetheringManager#registerTetheringEventCallback} instead.
markchien4ef53e82019-02-27 14:56:11 +08002607 * @hide
2608 */
2609 @SystemApi
markchien6ae63e52020-01-21 13:11:06 +08002610 @Deprecated
markchien4ef53e82019-02-27 14:56:11 +08002611 @RequiresPermission(android.Manifest.permission.TETHER_PRIVILEGED)
2612 public void registerTetheringEventCallback(
2613 @NonNull @CallbackExecutor Executor executor,
2614 @NonNull final OnTetheringEventCallback callback) {
Remi NGUYEN VAN5086c292021-03-15 07:27:44 +00002615 Preconditions.checkNotNull(callback, "OnTetheringEventCallback cannot be null.");
markchien4ef53e82019-02-27 14:56:11 +08002616
markchien6ae63e52020-01-21 13:11:06 +08002617 final TetheringEventCallback tetherCallback =
2618 new TetheringEventCallback() {
2619 @Override
2620 public void onUpstreamChanged(@Nullable Network network) {
2621 callback.onUpstreamChanged(network);
2622 }
2623 };
2624
2625 synchronized (mTetheringEventCallbacks) {
2626 mTetheringEventCallbacks.put(callback, tetherCallback);
Amos Bianchia9b415a2020-03-04 11:07:38 -08002627 mTetheringManager.registerTetheringEventCallback(executor, tetherCallback);
markchien6ae63e52020-01-21 13:11:06 +08002628 }
markchien4ef53e82019-02-27 14:56:11 +08002629 }
2630
2631 /**
2632 * Remove tethering event callback previously registered with
2633 * {@link #registerTetheringEventCallback}.
2634 *
2635 * @param callback previously registered callback.
markchien6ae63e52020-01-21 13:11:06 +08002636 *
2637 * @deprecated Use {@link TetheringManager#unregisterTetheringEventCallback} instead.
markchien4ef53e82019-02-27 14:56:11 +08002638 * @hide
2639 */
2640 @SystemApi
markchien6ae63e52020-01-21 13:11:06 +08002641 @Deprecated
markchien4ef53e82019-02-27 14:56:11 +08002642 @RequiresPermission(android.Manifest.permission.TETHER_PRIVILEGED)
2643 public void unregisterTetheringEventCallback(
2644 @NonNull final OnTetheringEventCallback callback) {
markchien6ae63e52020-01-21 13:11:06 +08002645 Objects.requireNonNull(callback, "The callback must be non-null");
2646 synchronized (mTetheringEventCallbacks) {
2647 final TetheringEventCallback tetherCallback =
2648 mTetheringEventCallbacks.remove(callback);
Amos Bianchia9b415a2020-03-04 11:07:38 -08002649 mTetheringManager.unregisterTetheringEventCallback(tetherCallback);
markchien6ae63e52020-01-21 13:11:06 +08002650 }
markchien4ef53e82019-02-27 14:56:11 +08002651 }
2652
2653
2654 /**
Robert Greenwaltfaa4b402013-02-15 10:56:35 -08002655 * Get the list of regular expressions that define any tetherable
2656 * USB network interfaces. If USB tethering is not supported by the
2657 * device, this list should be empty.
2658 *
2659 * @return an array of 0 or more regular expression Strings defining
2660 * what interfaces are considered tetherable usb interfaces.
2661 *
markchien6ae63e52020-01-21 13:11:06 +08002662 * @deprecated Use {@link TetheringEventCallback#onTetherableInterfaceRegexpsChanged} instead.
Robert Greenwalt8e87f122010-02-11 18:18:40 -08002663 * {@hide}
2664 */
Jeff Sharkey656584a2017-04-24 11:18:03 -06002665 @RequiresPermission(android.Manifest.permission.ACCESS_NETWORK_STATE)
Mathew Inwoodc5b9bec2018-08-08 14:52:47 +01002666 @UnsupportedAppUsage
markchien6ae63e52020-01-21 13:11:06 +08002667 @Deprecated
Robert Greenwalt8e87f122010-02-11 18:18:40 -08002668 public String[] getTetherableUsbRegexs() {
Amos Bianchia9b415a2020-03-04 11:07:38 -08002669 return mTetheringManager.getTetherableUsbRegexs();
Robert Greenwalt8e87f122010-02-11 18:18:40 -08002670 }
2671
2672 /**
Robert Greenwaltfaa4b402013-02-15 10:56:35 -08002673 * Get the list of regular expressions that define any tetherable
2674 * Wifi network interfaces. If Wifi tethering is not supported by the
2675 * device, this list should be empty.
2676 *
2677 * @return an array of 0 or more regular expression Strings defining
2678 * what interfaces are considered tetherable wifi interfaces.
2679 *
markchien6ae63e52020-01-21 13:11:06 +08002680 * @deprecated Use {@link TetheringEventCallback#onTetherableInterfaceRegexpsChanged} instead.
Robert Greenwalt8e87f122010-02-11 18:18:40 -08002681 * {@hide}
2682 */
Jeff Sharkey656584a2017-04-24 11:18:03 -06002683 @RequiresPermission(android.Manifest.permission.ACCESS_NETWORK_STATE)
Mathew Inwoodc5b9bec2018-08-08 14:52:47 +01002684 @UnsupportedAppUsage
markchien6ae63e52020-01-21 13:11:06 +08002685 @Deprecated
Robert Greenwalt8e87f122010-02-11 18:18:40 -08002686 public String[] getTetherableWifiRegexs() {
Amos Bianchia9b415a2020-03-04 11:07:38 -08002687 return mTetheringManager.getTetherableWifiRegexs();
Robert Greenwalt8e87f122010-02-11 18:18:40 -08002688 }
Robert Greenwalt4283ded2010-03-02 17:25:02 -08002689
Danica Chang96567052010-08-11 14:54:43 -07002690 /**
Robert Greenwaltfaa4b402013-02-15 10:56:35 -08002691 * Get the list of regular expressions that define any tetherable
2692 * Bluetooth network interfaces. If Bluetooth tethering is not supported by the
2693 * device, this list should be empty.
2694 *
2695 * @return an array of 0 or more regular expression Strings defining
2696 * what interfaces are considered tetherable bluetooth interfaces.
2697 *
markchien6ae63e52020-01-21 13:11:06 +08002698 * @deprecated Use {@link TetheringEventCallback#onTetherableInterfaceRegexpsChanged(
2699 *TetheringManager.TetheringInterfaceRegexps)} instead.
Danica Chang96567052010-08-11 14:54:43 -07002700 * {@hide}
2701 */
Jeff Sharkey656584a2017-04-24 11:18:03 -06002702 @RequiresPermission(android.Manifest.permission.ACCESS_NETWORK_STATE)
Mathew Inwoodc5b9bec2018-08-08 14:52:47 +01002703 @UnsupportedAppUsage
markchien6ae63e52020-01-21 13:11:06 +08002704 @Deprecated
Danica Chang96567052010-08-11 14:54:43 -07002705 public String[] getTetherableBluetoothRegexs() {
Amos Bianchia9b415a2020-03-04 11:07:38 -08002706 return mTetheringManager.getTetherableBluetoothRegexs();
Danica Chang96567052010-08-11 14:54:43 -07002707 }
2708
Mike Lockwooded4a1742011-07-19 13:04:47 -07002709 /**
Robert Greenwaltfaa4b402013-02-15 10:56:35 -08002710 * Attempt to both alter the mode of USB and Tethering of USB. A
2711 * utility method to deal with some of the complexity of USB - will
2712 * attempt to switch to Rndis and subsequently tether the resulting
2713 * interface on {@code true} or turn off tethering and switch off
2714 * Rndis on {@code false}.
Lorenzo Colitti0b40c872015-10-15 16:29:00 +09002715 *
2716 * <p>This method requires the caller to hold either the
2717 * {@link android.Manifest.permission#CHANGE_NETWORK_STATE} permission
2718 * or the ability to modify system settings as determined by
2719 * {@link android.provider.Settings.System#canWrite}.</p>
Robert Greenwaltfaa4b402013-02-15 10:56:35 -08002720 *
2721 * @param enable a boolean - {@code true} to enable tethering
2722 * @return error a {@code TETHER_ERROR} value indicating success or failure type
markchien91c78e52020-01-20 19:31:56 +08002723 * @deprecated Use {@link TetheringManager#startTethering} instead
Robert Greenwaltfaa4b402013-02-15 10:56:35 -08002724 *
Mike Lockwooded4a1742011-07-19 13:04:47 -07002725 * {@hide}
2726 */
Mathew Inwoodc5b9bec2018-08-08 14:52:47 +01002727 @UnsupportedAppUsage
markchien91c78e52020-01-20 19:31:56 +08002728 @Deprecated
Mike Lockwooded4a1742011-07-19 13:04:47 -07002729 public int setUsbTethering(boolean enable) {
Amos Bianchia9b415a2020-03-04 11:07:38 -08002730 return mTetheringManager.setUsbTethering(enable);
Mike Lockwooded4a1742011-07-19 13:04:47 -07002731 }
2732
markchien6ae63e52020-01-21 13:11:06 +08002733 /**
2734 * @deprecated Use {@link TetheringManager#TETHER_ERROR_NO_ERROR}.
2735 * {@hide}
2736 */
markchien0f45bb92019-01-16 17:44:13 +08002737 @SystemApi
markchien6ae63e52020-01-21 13:11:06 +08002738 @Deprecated
Remi NGUYEN VAN09f8ed22021-02-15 18:52:06 +09002739 public static final int TETHER_ERROR_NO_ERROR = 0;
markchien6ae63e52020-01-21 13:11:06 +08002740 /**
2741 * @deprecated Use {@link TetheringManager#TETHER_ERROR_UNKNOWN_IFACE}.
2742 * {@hide}
2743 */
2744 @Deprecated
2745 public static final int TETHER_ERROR_UNKNOWN_IFACE =
2746 TetheringManager.TETHER_ERROR_UNKNOWN_IFACE;
2747 /**
2748 * @deprecated Use {@link TetheringManager#TETHER_ERROR_SERVICE_UNAVAIL}.
2749 * {@hide}
2750 */
2751 @Deprecated
2752 public static final int TETHER_ERROR_SERVICE_UNAVAIL =
2753 TetheringManager.TETHER_ERROR_SERVICE_UNAVAIL;
2754 /**
2755 * @deprecated Use {@link TetheringManager#TETHER_ERROR_UNSUPPORTED}.
2756 * {@hide}
2757 */
2758 @Deprecated
2759 public static final int TETHER_ERROR_UNSUPPORTED = TetheringManager.TETHER_ERROR_UNSUPPORTED;
2760 /**
2761 * @deprecated Use {@link TetheringManager#TETHER_ERROR_UNAVAIL_IFACE}.
2762 * {@hide}
2763 */
2764 @Deprecated
2765 public static final int TETHER_ERROR_UNAVAIL_IFACE =
2766 TetheringManager.TETHER_ERROR_UNAVAIL_IFACE;
2767 /**
markchienf47d8342020-03-19 13:37:43 +08002768 * @deprecated Use {@link TetheringManager#TETHER_ERROR_INTERNAL_ERROR}.
markchien6ae63e52020-01-21 13:11:06 +08002769 * {@hide}
2770 */
2771 @Deprecated
markchienf47d8342020-03-19 13:37:43 +08002772 public static final int TETHER_ERROR_MASTER_ERROR =
2773 TetheringManager.TETHER_ERROR_INTERNAL_ERROR;
markchien6ae63e52020-01-21 13:11:06 +08002774 /**
2775 * @deprecated Use {@link TetheringManager#TETHER_ERROR_TETHER_IFACE_ERROR}.
2776 * {@hide}
2777 */
2778 @Deprecated
2779 public static final int TETHER_ERROR_TETHER_IFACE_ERROR =
2780 TetheringManager.TETHER_ERROR_TETHER_IFACE_ERROR;
2781 /**
2782 * @deprecated Use {@link TetheringManager#TETHER_ERROR_UNTETHER_IFACE_ERROR}.
2783 * {@hide}
2784 */
2785 @Deprecated
2786 public static final int TETHER_ERROR_UNTETHER_IFACE_ERROR =
2787 TetheringManager.TETHER_ERROR_UNTETHER_IFACE_ERROR;
2788 /**
markchienf47d8342020-03-19 13:37:43 +08002789 * @deprecated Use {@link TetheringManager#TETHER_ERROR_ENABLE_FORWARDING_ERROR}.
markchien6ae63e52020-01-21 13:11:06 +08002790 * {@hide}
2791 */
2792 @Deprecated
2793 public static final int TETHER_ERROR_ENABLE_NAT_ERROR =
markchienf47d8342020-03-19 13:37:43 +08002794 TetheringManager.TETHER_ERROR_ENABLE_FORWARDING_ERROR;
markchien6ae63e52020-01-21 13:11:06 +08002795 /**
markchienf47d8342020-03-19 13:37:43 +08002796 * @deprecated Use {@link TetheringManager#TETHER_ERROR_DISABLE_FORWARDING_ERROR}.
markchien6ae63e52020-01-21 13:11:06 +08002797 * {@hide}
2798 */
2799 @Deprecated
2800 public static final int TETHER_ERROR_DISABLE_NAT_ERROR =
markchienf47d8342020-03-19 13:37:43 +08002801 TetheringManager.TETHER_ERROR_DISABLE_FORWARDING_ERROR;
markchien6ae63e52020-01-21 13:11:06 +08002802 /**
2803 * @deprecated Use {@link TetheringManager#TETHER_ERROR_IFACE_CFG_ERROR}.
2804 * {@hide}
2805 */
2806 @Deprecated
2807 public static final int TETHER_ERROR_IFACE_CFG_ERROR =
2808 TetheringManager.TETHER_ERROR_IFACE_CFG_ERROR;
2809 /**
markchienf47d8342020-03-19 13:37:43 +08002810 * @deprecated Use {@link TetheringManager#TETHER_ERROR_PROVISIONING_FAILED}.
markchien6ae63e52020-01-21 13:11:06 +08002811 * {@hide}
2812 */
markchien0f45bb92019-01-16 17:44:13 +08002813 @SystemApi
markchien6ae63e52020-01-21 13:11:06 +08002814 @Deprecated
Remi NGUYEN VAN09f8ed22021-02-15 18:52:06 +09002815 public static final int TETHER_ERROR_PROVISION_FAILED = 11;
markchien6ae63e52020-01-21 13:11:06 +08002816 /**
2817 * @deprecated Use {@link TetheringManager#TETHER_ERROR_DHCPSERVER_ERROR}.
2818 * {@hide}
2819 */
2820 @Deprecated
2821 public static final int TETHER_ERROR_DHCPSERVER_ERROR =
2822 TetheringManager.TETHER_ERROR_DHCPSERVER_ERROR;
2823 /**
2824 * @deprecated Use {@link TetheringManager#TETHER_ERROR_ENTITLEMENT_UNKNOWN}.
2825 * {@hide}
2826 */
markchien0f45bb92019-01-16 17:44:13 +08002827 @SystemApi
markchien6ae63e52020-01-21 13:11:06 +08002828 @Deprecated
Remi NGUYEN VAN09f8ed22021-02-15 18:52:06 +09002829 public static final int TETHER_ERROR_ENTITLEMENT_UNKONWN = 13;
Robert Greenwalt4283ded2010-03-02 17:25:02 -08002830
2831 /**
Robert Greenwaltfaa4b402013-02-15 10:56:35 -08002832 * Get a more detailed error code after a Tethering or Untethering
2833 * request asynchronously failed.
2834 *
2835 * @param iface The name of the interface of interest
Robert Greenwalt4283ded2010-03-02 17:25:02 -08002836 * @return error The error code of the last error tethering or untethering the named
2837 * interface
Robert Greenwaltfaa4b402013-02-15 10:56:35 -08002838 *
markchien6ae63e52020-01-21 13:11:06 +08002839 * @deprecated Use {@link TetheringEventCallback#onError(String, int)} instead.
Robert Greenwalt4283ded2010-03-02 17:25:02 -08002840 * {@hide}
2841 */
Jeff Sharkey656584a2017-04-24 11:18:03 -06002842 @RequiresPermission(android.Manifest.permission.ACCESS_NETWORK_STATE)
Mathew Inwoode1a17ba2020-11-04 09:29:36 +00002843 @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.R, trackingBug = 170729553)
markchien6ae63e52020-01-21 13:11:06 +08002844 @Deprecated
Robert Greenwalt4283ded2010-03-02 17:25:02 -08002845 public int getLastTetherError(String iface) {
markchienf47d8342020-03-19 13:37:43 +08002846 int error = mTetheringManager.getLastTetherError(iface);
2847 if (error == TetheringManager.TETHER_ERROR_UNKNOWN_TYPE) {
2848 // TETHER_ERROR_UNKNOWN_TYPE was introduced with TetheringManager and has never been
2849 // returned by ConnectivityManager. Convert it to the legacy TETHER_ERROR_UNKNOWN_IFACE
2850 // instead.
2851 error = TetheringManager.TETHER_ERROR_UNKNOWN_IFACE;
2852 }
2853 return error;
Robert Greenwalt93dc1042010-06-15 12:19:37 -07002854 }
2855
markchienbf5ab012019-03-06 16:25:00 +08002856 /** @hide */
2857 @Retention(RetentionPolicy.SOURCE)
2858 @IntDef(value = {
2859 TETHER_ERROR_NO_ERROR,
2860 TETHER_ERROR_PROVISION_FAILED,
2861 TETHER_ERROR_ENTITLEMENT_UNKONWN,
2862 })
2863 public @interface EntitlementResultCode {
2864 }
2865
Robert Greenwalt93dc1042010-06-15 12:19:37 -07002866 /**
markchienbf5ab012019-03-06 16:25:00 +08002867 * Callback for use with {@link #getLatestTetheringEntitlementResult} to find out whether
markchien0f45bb92019-01-16 17:44:13 +08002868 * entitlement succeeded.
markchien6ae63e52020-01-21 13:11:06 +08002869 *
2870 * @deprecated Use {@link TetheringManager#OnTetheringEntitlementResultListener} instead.
markchien0f45bb92019-01-16 17:44:13 +08002871 * @hide
2872 */
2873 @SystemApi
markchien6ae63e52020-01-21 13:11:06 +08002874 @Deprecated
markchienbf5ab012019-03-06 16:25:00 +08002875 public interface OnTetheringEntitlementResultListener {
2876 /**
2877 * Called to notify entitlement result.
2878 *
2879 * @param resultCode an int value of entitlement result. It may be one of
2880 * {@link #TETHER_ERROR_NO_ERROR},
2881 * {@link #TETHER_ERROR_PROVISION_FAILED}, or
2882 * {@link #TETHER_ERROR_ENTITLEMENT_UNKONWN}.
2883 */
Jeremy Klein7e7c7422019-03-12 13:32:08 -07002884 void onTetheringEntitlementResult(@EntitlementResultCode int resultCode);
markchienbf5ab012019-03-06 16:25:00 +08002885 }
2886
2887 /**
markchien0f45bb92019-01-16 17:44:13 +08002888 * Get the last value of the entitlement check on this downstream. If the cached value is
2889 * {@link #TETHER_ERROR_NO_ERROR} or showEntitlementUi argument is false, it just return the
2890 * cached value. Otherwise, a UI-based entitlement check would be performed. It is not
2891 * guaranteed that the UI-based entitlement check will complete in any specific time period
2892 * and may in fact never complete. Any successful entitlement check the platform performs for
2893 * any reason will update the cached value.
2894 *
2895 * @param type the downstream type of tethering. Must be one of
2896 * {@link #TETHERING_WIFI},
2897 * {@link #TETHERING_USB}, or
2898 * {@link #TETHERING_BLUETOOTH}.
2899 * @param showEntitlementUi a boolean indicating whether to run UI-based entitlement check.
markchienbf5ab012019-03-06 16:25:00 +08002900 * @param executor the executor on which callback will be invoked.
2901 * @param listener an {@link OnTetheringEntitlementResultListener} which will be called to
2902 * notify the caller of the result of entitlement check. The listener may be called zero
2903 * or one time.
markchien6ae63e52020-01-21 13:11:06 +08002904 * @deprecated Use {@link TetheringManager#requestLatestTetheringEntitlementResult} instead.
markchien0f45bb92019-01-16 17:44:13 +08002905 * {@hide}
2906 */
2907 @SystemApi
markchien6ae63e52020-01-21 13:11:06 +08002908 @Deprecated
markchien0f45bb92019-01-16 17:44:13 +08002909 @RequiresPermission(android.Manifest.permission.TETHER_PRIVILEGED)
markchienbf5ab012019-03-06 16:25:00 +08002910 public void getLatestTetheringEntitlementResult(int type, boolean showEntitlementUi,
2911 @NonNull @CallbackExecutor Executor executor,
2912 @NonNull final OnTetheringEntitlementResultListener listener) {
Remi NGUYEN VAN5086c292021-03-15 07:27:44 +00002913 Preconditions.checkNotNull(listener, "TetheringEntitlementResultListener cannot be null.");
markchienbf5ab012019-03-06 16:25:00 +08002914 ResultReceiver wrappedListener = new ResultReceiver(null) {
2915 @Override
2916 protected void onReceiveResult(int resultCode, Bundle resultData) {
lucaslinc6170bb2021-03-04 09:38:21 +08002917 final long token = Binder.clearCallingIdentity();
2918 try {
2919 executor.execute(() -> {
2920 listener.onTetheringEntitlementResult(resultCode);
2921 });
2922 } finally {
2923 Binder.restoreCallingIdentity(token);
2924 }
markchienbf5ab012019-03-06 16:25:00 +08002925 }
2926 };
2927
Amos Bianchia9b415a2020-03-04 11:07:38 -08002928 mTetheringManager.requestLatestTetheringEntitlementResult(type, wrappedListener,
markchien5776f962019-12-16 20:15:20 +08002929 showEntitlementUi);
markchienbf5ab012019-03-06 16:25:00 +08002930 }
2931
2932 /**
Robert Greenwaltfaa4b402013-02-15 10:56:35 -08002933 * Report network connectivity status. This is currently used only
2934 * to alter status bar UI.
Paul Jensen08f9dbb2015-05-06 11:10:18 -04002935 * <p>This method requires the caller to hold the permission
2936 * {@link android.Manifest.permission#STATUS_BAR}.
Robert Greenwaltfaa4b402013-02-15 10:56:35 -08002937 *
Robert Greenwalt986c7412010-09-08 15:24:47 -07002938 * @param networkType The type of network you want to report on
2939 * @param percentage The quality of the connection 0 is bad, 100 is good
Chalard Jean7eaf3b12018-03-08 13:54:53 +09002940 * @deprecated Types are deprecated. Use {@link #reportNetworkConnectivity} instead.
Robert Greenwalt986c7412010-09-08 15:24:47 -07002941 * {@hide}
2942 */
2943 public void reportInetCondition(int networkType, int percentage) {
Soi, Yoshinarib8102ef2015-11-12 12:09:02 +09002944 printStackTrace();
Robert Greenwalt986c7412010-09-08 15:24:47 -07002945 try {
2946 mService.reportInetCondition(networkType, percentage);
2947 } catch (RemoteException e) {
Jeff Sharkeyc78f85c2016-03-01 19:27:23 -07002948 throw e.rethrowFromSystemServer();
Robert Greenwalt986c7412010-09-08 15:24:47 -07002949 }
2950 }
Robert Greenwaltc3c5f862010-10-11 16:00:27 -07002951
2952 /**
Robert Greenwaltf3017f72014-05-18 23:07:25 -07002953 * Report a problem network to the framework. This provides a hint to the system
Ye Wenca7abab2014-07-21 14:19:01 -07002954 * that there might be connectivity problems on this network and may cause
Robert Greenwaltf3017f72014-05-18 23:07:25 -07002955 * the framework to re-evaluate network connectivity and/or switch to another
2956 * network.
Robert Greenwaltf99b8392014-03-26 16:47:06 -07002957 *
Robert Greenwaltf3017f72014-05-18 23:07:25 -07002958 * @param network The {@link Network} the application was attempting to use
2959 * or {@code null} to indicate the current default network.
Paul Jensenb95d7952015-04-07 12:43:13 -04002960 * @deprecated Use {@link #reportNetworkConnectivity} which allows reporting both
2961 * working and non-working connectivity.
Robert Greenwaltf99b8392014-03-26 16:47:06 -07002962 */
Aurimas Liutikase80a0eb2016-05-24 15:22:55 -07002963 @Deprecated
Chalard Jean158702d2019-01-07 19:26:34 +09002964 public void reportBadNetwork(@Nullable Network network) {
Soi, Yoshinarib8102ef2015-11-12 12:09:02 +09002965 printStackTrace();
Robert Greenwaltf99b8392014-03-26 16:47:06 -07002966 try {
Paul Jensenb95d7952015-04-07 12:43:13 -04002967 // One of these will be ignored because it matches system's current state.
2968 // The other will trigger the necessary reevaluation.
2969 mService.reportNetworkConnectivity(network, true);
2970 mService.reportNetworkConnectivity(network, false);
2971 } catch (RemoteException e) {
Jeff Sharkeyc78f85c2016-03-01 19:27:23 -07002972 throw e.rethrowFromSystemServer();
Paul Jensenb95d7952015-04-07 12:43:13 -04002973 }
2974 }
2975
2976 /**
2977 * Report to the framework whether a network has working connectivity.
2978 * This provides a hint to the system that a particular network is providing
2979 * working connectivity or not. In response the framework may re-evaluate
2980 * the network's connectivity and might take further action thereafter.
2981 *
2982 * @param network The {@link Network} the application was attempting to use
2983 * or {@code null} to indicate the current default network.
2984 * @param hasConnectivity {@code true} if the application was able to successfully access the
2985 * Internet using {@code network} or {@code false} if not.
2986 */
Chalard Jean158702d2019-01-07 19:26:34 +09002987 public void reportNetworkConnectivity(@Nullable Network network, boolean hasConnectivity) {
Soi, Yoshinarib8102ef2015-11-12 12:09:02 +09002988 printStackTrace();
Paul Jensenb95d7952015-04-07 12:43:13 -04002989 try {
2990 mService.reportNetworkConnectivity(network, hasConnectivity);
Robert Greenwaltf99b8392014-03-26 16:47:06 -07002991 } catch (RemoteException e) {
Jeff Sharkeyc78f85c2016-03-01 19:27:23 -07002992 throw e.rethrowFromSystemServer();
Robert Greenwaltf99b8392014-03-26 16:47:06 -07002993 }
2994 }
2995
2996 /**
Robert Greenwaltfaa4b402013-02-15 10:56:35 -08002997 * Set a network-independent global http proxy. This is not normally what you want
2998 * for typical HTTP proxies - they are general network dependent. However if you're
2999 * doing something unusual like general internal filtering this may be useful. On
3000 * a private network where the proxy is not accessible, you may break HTTP using this.
Paul Jensen08f9dbb2015-05-06 11:10:18 -04003001 *
3002 * @param p A {@link ProxyInfo} object defining the new global
3003 * HTTP proxy. A {@code null} value will clear the global HTTP proxy.
Robert Greenwaltf3017f72014-05-18 23:07:25 -07003004 * @hide
Robert Greenwaltc3c5f862010-10-11 16:00:27 -07003005 */
paulhu8e96a752019-08-12 16:25:11 +08003006 @RequiresPermission(android.Manifest.permission.NETWORK_STACK)
Jason Monk4d5e20f2014-04-25 15:00:09 -04003007 public void setGlobalProxy(ProxyInfo p) {
Robert Greenwaltc3c5f862010-10-11 16:00:27 -07003008 try {
3009 mService.setGlobalProxy(p);
3010 } catch (RemoteException e) {
Jeff Sharkeyc78f85c2016-03-01 19:27:23 -07003011 throw e.rethrowFromSystemServer();
Robert Greenwaltc3c5f862010-10-11 16:00:27 -07003012 }
3013 }
3014
3015 /**
Robert Greenwaltfaa4b402013-02-15 10:56:35 -08003016 * Retrieve any network-independent global HTTP proxy.
3017 *
Jason Monk4d5e20f2014-04-25 15:00:09 -04003018 * @return {@link ProxyInfo} for the current global HTTP proxy or {@code null}
Robert Greenwaltfaa4b402013-02-15 10:56:35 -08003019 * if no global HTTP proxy is set.
Robert Greenwaltf3017f72014-05-18 23:07:25 -07003020 * @hide
Robert Greenwaltc3c5f862010-10-11 16:00:27 -07003021 */
Jason Monk4d5e20f2014-04-25 15:00:09 -04003022 public ProxyInfo getGlobalProxy() {
Robert Greenwaltc3c5f862010-10-11 16:00:27 -07003023 try {
3024 return mService.getGlobalProxy();
3025 } catch (RemoteException e) {
Jeff Sharkeyc78f85c2016-03-01 19:27:23 -07003026 throw e.rethrowFromSystemServer();
Robert Greenwaltc3c5f862010-10-11 16:00:27 -07003027 }
3028 }
3029
3030 /**
Paul Jensendb93dd92015-05-06 07:32:40 -04003031 * Retrieve the global HTTP proxy, or if no global HTTP proxy is set, a
3032 * network-specific HTTP proxy. If {@code network} is null, the
3033 * network-specific proxy returned is the proxy of the default active
3034 * network.
3035 *
3036 * @return {@link ProxyInfo} for the current global HTTP proxy, or if no
3037 * global HTTP proxy is set, {@code ProxyInfo} for {@code network},
3038 * or when {@code network} is {@code null},
3039 * the {@code ProxyInfo} for the default active network. Returns
3040 * {@code null} when no proxy applies or the caller doesn't have
3041 * permission to use {@code network}.
3042 * @hide
3043 */
3044 public ProxyInfo getProxyForNetwork(Network network) {
3045 try {
3046 return mService.getProxyForNetwork(network);
3047 } catch (RemoteException e) {
Jeff Sharkeyc78f85c2016-03-01 19:27:23 -07003048 throw e.rethrowFromSystemServer();
Paul Jensendb93dd92015-05-06 07:32:40 -04003049 }
3050 }
3051
3052 /**
Paul Jensenc0618a62014-12-10 15:12:18 -05003053 * Get the current default HTTP proxy settings. If a global proxy is set it will be returned,
3054 * otherwise if this process is bound to a {@link Network} using
Paul Jensenee2f45d2015-03-10 10:54:12 -04003055 * {@link #bindProcessToNetwork} then that {@code Network}'s proxy is returned, otherwise
Paul Jensenc0618a62014-12-10 15:12:18 -05003056 * the default network's proxy is returned.
Robert Greenwaltfaa4b402013-02-15 10:56:35 -08003057 *
Jason Monk4d5e20f2014-04-25 15:00:09 -04003058 * @return the {@link ProxyInfo} for the current HTTP proxy, or {@code null} if no
Robert Greenwaltfaa4b402013-02-15 10:56:35 -08003059 * HTTP proxy is active.
Robert Greenwaltc3c5f862010-10-11 16:00:27 -07003060 */
Chalard Jean158702d2019-01-07 19:26:34 +09003061 @Nullable
Paul Jensenc0618a62014-12-10 15:12:18 -05003062 public ProxyInfo getDefaultProxy() {
Paul Jensendb93dd92015-05-06 07:32:40 -04003063 return getProxyForNetwork(getBoundNetworkForProcess());
Robert Greenwaltc3c5f862010-10-11 16:00:27 -07003064 }
Robert Greenwalt34848c02011-03-25 13:09:25 -07003065
3066 /**
Robert Greenwalt0114f6e2011-08-31 11:46:42 -07003067 * Returns true if the hardware supports the given network type
3068 * else it returns false. This doesn't indicate we have coverage
3069 * or are authorized onto a network, just whether or not the
Robert Greenwaltfaa4b402013-02-15 10:56:35 -08003070 * hardware supports it. For example a GSM phone without a SIM
3071 * should still return {@code true} for mobile data, but a wifi only
3072 * tablet would return {@code false}.
3073 *
3074 * @param networkType The network type we'd like to check
3075 * @return {@code true} if supported, else {@code false}
Chalard Jean7eaf3b12018-03-08 13:54:53 +09003076 * @deprecated Types are deprecated. Use {@link NetworkCapabilities} instead.
Robert Greenwalt0114f6e2011-08-31 11:46:42 -07003077 * @hide
3078 */
Chalard Jean7eaf3b12018-03-08 13:54:53 +09003079 @Deprecated
Jeff Sharkey656584a2017-04-24 11:18:03 -06003080 @RequiresPermission(android.Manifest.permission.ACCESS_NETWORK_STATE)
Chalard Jeana3b77512019-04-09 15:46:21 +09003081 @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P, trackingBug = 130143562)
Robert Greenwalt0114f6e2011-08-31 11:46:42 -07003082 public boolean isNetworkSupported(int networkType) {
3083 try {
3084 return mService.isNetworkSupported(networkType);
Jeff Sharkeyc78f85c2016-03-01 19:27:23 -07003085 } catch (RemoteException e) {
3086 throw e.rethrowFromSystemServer();
3087 }
Robert Greenwalt0114f6e2011-08-31 11:46:42 -07003088 }
Jeff Sharkeyd00b1302012-04-12 18:34:54 -07003089
3090 /**
3091 * Returns if the currently active data network is metered. A network is
3092 * classified as metered when the user is sensitive to heavy data usage on
Robert Greenwaltfaa4b402013-02-15 10:56:35 -08003093 * that connection due to monetary costs, data limitations or
3094 * battery/performance issues. You should check this before doing large
3095 * data transfers, and warn the user or delay the operation until another
3096 * network is available.
3097 *
3098 * @return {@code true} if large transfers should be avoided, otherwise
3099 * {@code false}.
Jeff Sharkeyd00b1302012-04-12 18:34:54 -07003100 */
Jeff Sharkey656584a2017-04-24 11:18:03 -06003101 @RequiresPermission(android.Manifest.permission.ACCESS_NETWORK_STATE)
Jeff Sharkeyd00b1302012-04-12 18:34:54 -07003102 public boolean isActiveNetworkMetered() {
3103 try {
3104 return mService.isActiveNetworkMetered();
3105 } catch (RemoteException e) {
Jeff Sharkeyc78f85c2016-03-01 19:27:23 -07003106 throw e.rethrowFromSystemServer();
Jeff Sharkeyd00b1302012-04-12 18:34:54 -07003107 }
3108 }
Jeff Sharkeyebcc7972012-08-25 00:05:46 -07003109
Robert Greenwaltfaa4b402013-02-15 10:56:35 -08003110 /**
Sarah Chincabcbff2020-11-25 12:15:14 -08003111 * Set sign in error notification to visible or invisible
Wink Saville9a1a7ef2013-08-29 08:55:16 -07003112 *
Sarah Chincabcbff2020-11-25 12:15:14 -08003113 * @hide
Paul Jensendda8e592015-03-18 12:23:02 -04003114 * @deprecated Doesn't properly deal with multiple connected networks of the same type.
Wink Saville9a1a7ef2013-08-29 08:55:16 -07003115 */
Aurimas Liutikase80a0eb2016-05-24 15:22:55 -07003116 @Deprecated
Wink Saville9a1a7ef2013-08-29 08:55:16 -07003117 public void setProvisioningNotificationVisible(boolean visible, int networkType,
Paul Jensenebeaecd2014-09-15 15:59:36 -04003118 String action) {
Wink Saville9a1a7ef2013-08-29 08:55:16 -07003119 try {
Paul Jensenebeaecd2014-09-15 15:59:36 -04003120 mService.setProvisioningNotificationVisible(visible, networkType, action);
Wink Saville9a1a7ef2013-08-29 08:55:16 -07003121 } catch (RemoteException e) {
Jeff Sharkeyc78f85c2016-03-01 19:27:23 -07003122 throw e.rethrowFromSystemServer();
Wink Saville9a1a7ef2013-08-29 08:55:16 -07003123 }
3124 }
Yuhao Zhengb77f15d2013-09-09 17:00:04 -07003125
3126 /**
3127 * Set the value for enabling/disabling airplane mode
3128 *
3129 * @param enable whether to enable airplane mode or not
3130 *
Yuhao Zhengb77f15d2013-09-09 17:00:04 -07003131 * @hide
3132 */
Lorenzo Colittic7da00d2018-10-09 18:55:11 +09003133 @RequiresPermission(anyOf = {
Edward Savage-Jonesd4723692019-11-26 13:18:08 +01003134 android.Manifest.permission.NETWORK_AIRPLANE_MODE,
Lorenzo Colittic7da00d2018-10-09 18:55:11 +09003135 android.Manifest.permission.NETWORK_SETTINGS,
3136 android.Manifest.permission.NETWORK_SETUP_WIZARD,
3137 android.Manifest.permission.NETWORK_STACK})
Lorenzo Colitti0bfef022018-10-09 18:50:32 +09003138 @SystemApi
Yuhao Zhengb77f15d2013-09-09 17:00:04 -07003139 public void setAirplaneMode(boolean enable) {
3140 try {
3141 mService.setAirplaneMode(enable);
3142 } catch (RemoteException e) {
Jeff Sharkeyc78f85c2016-03-01 19:27:23 -07003143 throw e.rethrowFromSystemServer();
Yuhao Zhengb77f15d2013-09-09 17:00:04 -07003144 }
3145 }
Robert Greenwalt7e45d112014-04-11 15:53:27 -07003146
Lorenzo Colitti6702d4b2020-01-08 00:04:09 +09003147 /**
3148 * Registers the specified {@link NetworkProvider}.
3149 * Each listener must only be registered once. The listener can be unregistered with
3150 * {@link #unregisterNetworkProvider}.
3151 *
3152 * @param provider the provider to register
3153 * @return the ID of the provider. This ID must be used by the provider when registering
3154 * {@link android.net.NetworkAgent}s.
3155 * @hide
3156 */
3157 @SystemApi
paulhub6ba8e82020-03-04 09:43:41 +08003158 @RequiresPermission(anyOf = {
3159 NetworkStack.PERMISSION_MAINLINE_NETWORK_STACK,
3160 android.Manifest.permission.NETWORK_FACTORY})
Lorenzo Colitti6702d4b2020-01-08 00:04:09 +09003161 public int registerNetworkProvider(@NonNull NetworkProvider provider) {
3162 if (provider.getProviderId() != NetworkProvider.ID_NONE) {
Lorenzo Colitti6702d4b2020-01-08 00:04:09 +09003163 throw new IllegalStateException("NetworkProviders can only be registered once");
3164 }
3165
3166 try {
3167 int providerId = mService.registerNetworkProvider(provider.getMessenger(),
3168 provider.getName());
3169 provider.setProviderId(providerId);
3170 } catch (RemoteException e) {
3171 throw e.rethrowFromSystemServer();
3172 }
3173 return provider.getProviderId();
3174 }
3175
3176 /**
3177 * Unregisters the specified NetworkProvider.
3178 *
3179 * @param provider the provider to unregister
3180 * @hide
3181 */
3182 @SystemApi
paulhub6ba8e82020-03-04 09:43:41 +08003183 @RequiresPermission(anyOf = {
3184 NetworkStack.PERMISSION_MAINLINE_NETWORK_STACK,
3185 android.Manifest.permission.NETWORK_FACTORY})
Lorenzo Colitti6702d4b2020-01-08 00:04:09 +09003186 public void unregisterNetworkProvider(@NonNull NetworkProvider provider) {
3187 try {
3188 mService.unregisterNetworkProvider(provider.getMessenger());
3189 } catch (RemoteException e) {
3190 throw e.rethrowFromSystemServer();
3191 }
3192 provider.setProviderId(NetworkProvider.ID_NONE);
3193 }
3194
3195
3196 /** @hide exposed via the NetworkProvider class. */
paulhub6ba8e82020-03-04 09:43:41 +08003197 @RequiresPermission(anyOf = {
3198 NetworkStack.PERMISSION_MAINLINE_NETWORK_STACK,
3199 android.Manifest.permission.NETWORK_FACTORY})
Lorenzo Colitti6702d4b2020-01-08 00:04:09 +09003200 public void declareNetworkRequestUnfulfillable(@NonNull NetworkRequest request) {
3201 try {
3202 mService.declareNetworkRequestUnfulfillable(request);
3203 } catch (RemoteException e) {
3204 throw e.rethrowFromSystemServer();
3205 }
3206 }
3207
Chalard Jean29d06db2018-05-02 21:14:54 +09003208 // TODO : remove this method. It is a stopgap measure to help sheperding a number
3209 // of dependent changes that would conflict throughout the automerger graph. Having this
3210 // temporarily helps with the process of going through with all these dependent changes across
3211 // the entire tree.
Paul Jensen1f567382015-02-13 14:18:39 -05003212 /**
3213 * @hide
3214 * Register a NetworkAgent with ConnectivityService.
Chalard Jeanf78c9642019-12-13 19:47:12 +09003215 * @return Network corresponding to NetworkAgent.
Paul Jensen1f567382015-02-13 14:18:39 -05003216 */
paulhub6ba8e82020-03-04 09:43:41 +08003217 @RequiresPermission(anyOf = {
3218 NetworkStack.PERMISSION_MAINLINE_NETWORK_STACK,
3219 android.Manifest.permission.NETWORK_FACTORY})
Remi NGUYEN VAN77b0c1c2020-12-23 12:45:08 +09003220 public Network registerNetworkAgent(INetworkAgent na, NetworkInfo ni, LinkProperties lp,
Lorenzo Colittiab2fed72020-01-12 22:28:37 +09003221 NetworkCapabilities nc, int score, NetworkAgentConfig config) {
Remi NGUYEN VAN77b0c1c2020-12-23 12:45:08 +09003222 return registerNetworkAgent(na, ni, lp, nc, score, config, NetworkProvider.ID_NONE);
Chalard Jean29d06db2018-05-02 21:14:54 +09003223 }
3224
3225 /**
3226 * @hide
3227 * Register a NetworkAgent with ConnectivityService.
Chalard Jeanf78c9642019-12-13 19:47:12 +09003228 * @return Network corresponding to NetworkAgent.
Chalard Jean29d06db2018-05-02 21:14:54 +09003229 */
paulhub6ba8e82020-03-04 09:43:41 +08003230 @RequiresPermission(anyOf = {
3231 NetworkStack.PERMISSION_MAINLINE_NETWORK_STACK,
3232 android.Manifest.permission.NETWORK_FACTORY})
Remi NGUYEN VAN77b0c1c2020-12-23 12:45:08 +09003233 public Network registerNetworkAgent(INetworkAgent na, NetworkInfo ni, LinkProperties lp,
Lorenzo Colittiab2fed72020-01-12 22:28:37 +09003234 NetworkCapabilities nc, int score, NetworkAgentConfig config, int providerId) {
Robert Greenwalte20f7a22014-04-18 15:25:25 -07003235 try {
Remi NGUYEN VAN77b0c1c2020-12-23 12:45:08 +09003236 return mService.registerNetworkAgent(na, ni, lp, nc, score, config, providerId);
Paul Jensen1f567382015-02-13 14:18:39 -05003237 } catch (RemoteException e) {
Jeff Sharkeyc78f85c2016-03-01 19:27:23 -07003238 throw e.rethrowFromSystemServer();
Paul Jensen1f567382015-02-13 14:18:39 -05003239 }
Robert Greenwalte20f7a22014-04-18 15:25:25 -07003240 }
3241
Robert Greenwaltf99b8392014-03-26 16:47:06 -07003242 /**
Hugo Benichi2aa65af2017-03-06 09:17:06 +09003243 * Base class for {@code NetworkRequest} callbacks. Used for notifications about network
3244 * changes. Should be extended by applications wanting notifications.
3245 *
3246 * A {@code NetworkCallback} is registered by calling
3247 * {@link #requestNetwork(NetworkRequest, NetworkCallback)},
3248 * {@link #registerNetworkCallback(NetworkRequest, NetworkCallback)},
Hugo Benichicbfbb372018-02-07 21:17:43 +09003249 * or {@link #registerDefaultNetworkCallback(NetworkCallback)}. A {@code NetworkCallback} is
Hugo Benichi2aa65af2017-03-06 09:17:06 +09003250 * unregistered by calling {@link #unregisterNetworkCallback(NetworkCallback)}.
3251 * A {@code NetworkCallback} should be registered at most once at any time.
3252 * A {@code NetworkCallback} that has been unregistered can be registered again.
Robert Greenwaltf99b8392014-03-26 16:47:06 -07003253 */
Robert Greenwaltf57c03c2014-06-11 16:05:07 -07003254 public static class NetworkCallback {
Robert Greenwalte20f7a22014-04-18 15:25:25 -07003255 /**
Roshan Pius951c0032020-12-22 15:10:42 -08003256 * No flags associated with this callback.
3257 * @hide
3258 */
3259 public static final int FLAG_NONE = 0;
3260 /**
3261 * Use this flag to include any location sensitive data in {@link NetworkCapabilities} sent
3262 * via {@link #onCapabilitiesChanged(Network, NetworkCapabilities)}.
3263 * <p>
3264 * These include:
3265 * <li> Some transport info instances (retrieved via
3266 * {@link NetworkCapabilities#getTransportInfo()}) like {@link android.net.wifi.WifiInfo}
3267 * contain location sensitive information.
3268 * <li> OwnerUid (retrieved via {@link NetworkCapabilities#getOwnerUid()} is location
3269 * sensitive for wifi suggestor apps (i.e using {@link WifiNetworkSuggestion}).</li>
3270 * </p>
3271 * <p>
3272 * Note:
3273 * <li> Retrieving this location sensitive information (subject to app's location
3274 * permissions) will be noted by system. </li>
3275 * <li> Without this flag any {@link NetworkCapabilities} provided via the callback does
3276 * not include location sensitive info.
3277 * </p>
3278 */
3279 public static final int FLAG_INCLUDE_LOCATION_INFO = 1 << 0;
3280
3281 /** @hide */
3282 @Retention(RetentionPolicy.SOURCE)
3283 @IntDef(flag = true, prefix = "FLAG_", value = {
3284 FLAG_NONE,
3285 FLAG_INCLUDE_LOCATION_INFO
3286 })
3287 public @interface Flag { }
3288
3289 /**
3290 * All the valid flags for error checking.
3291 */
3292 private static final int VALID_FLAGS = FLAG_INCLUDE_LOCATION_INFO;
3293
3294 public NetworkCallback() {
3295 this(FLAG_NONE);
3296 }
3297
3298 public NetworkCallback(@Flag int flags) {
3299 Preconditions.checkArgument((flags & VALID_FLAGS) == flags);
3300 mFlags = flags;
3301 }
3302
3303 /**
Lorenzo Colitti14574592015-04-24 12:23:24 +09003304 * Called when the framework connects to a new network to evaluate whether it satisfies this
3305 * request. If evaluation succeeds, this callback may be followed by an {@link #onAvailable}
3306 * callback. There is no guarantee that this new network will satisfy any requests, or that
3307 * the network will stay connected for longer than the time necessary to evaluate it.
3308 * <p>
3309 * Most applications <b>should not</b> act on this callback, and should instead use
3310 * {@link #onAvailable}. This callback is intended for use by applications that can assist
3311 * the framework in properly evaluating the network &mdash; for example, an application that
3312 * can automatically log in to a captive portal without user intervention.
3313 *
3314 * @param network The {@link Network} of the network that is being evaluated.
Lorenzo Colitti3a9df1a2015-06-11 14:27:17 +09003315 *
3316 * @hide
Robert Greenwalte20f7a22014-04-18 15:25:25 -07003317 */
paulhu1a407652019-03-22 16:35:06 +08003318 public void onPreCheck(@NonNull Network network) {}
Robert Greenwalte20f7a22014-04-18 15:25:25 -07003319
3320 /**
Lorenzo Colitti14574592015-04-24 12:23:24 +09003321 * Called when the framework connects and has declared a new network ready for use.
Robert Greenwaltf57c03c2014-06-11 16:05:07 -07003322 * This callback may be called more than once if the {@link Network} that is
3323 * satisfying the request changes.
Robert Greenwaltf3017f72014-05-18 23:07:25 -07003324 *
Robert Greenwaltf3017f72014-05-18 23:07:25 -07003325 * @param network The {@link Network} of the satisfying network.
Chalard Jeana23bc9e2018-01-30 22:41:41 +09003326 * @param networkCapabilities The {@link NetworkCapabilities} of the satisfying network.
3327 * @param linkProperties The {@link LinkProperties} of the satisfying network.
junyulaif2c67e42018-08-07 19:50:45 +08003328 * @param blocked Whether access to the {@link Network} is blocked due to system policy.
Chalard Jeana23bc9e2018-01-30 22:41:41 +09003329 * @hide
3330 */
paulhu1a407652019-03-22 16:35:06 +08003331 public void onAvailable(@NonNull Network network,
3332 @NonNull NetworkCapabilities networkCapabilities,
3333 @NonNull LinkProperties linkProperties, boolean blocked) {
Chalard Jeana23bc9e2018-01-30 22:41:41 +09003334 // Internally only this method is called when a new network is available, and
3335 // it calls the callback in the same way and order that older versions used
3336 // to call so as not to change the behavior.
3337 onAvailable(network);
3338 if (!networkCapabilities.hasCapability(
3339 NetworkCapabilities.NET_CAPABILITY_NOT_SUSPENDED)) {
3340 onNetworkSuspended(network);
3341 }
3342 onCapabilitiesChanged(network, networkCapabilities);
3343 onLinkPropertiesChanged(network, linkProperties);
junyulaif2c67e42018-08-07 19:50:45 +08003344 onBlockedStatusChanged(network, blocked);
Chalard Jeana23bc9e2018-01-30 22:41:41 +09003345 }
3346
3347 /**
3348 * Called when the framework connects and has declared a new network ready for use.
Chalard Jean01378b62019-08-30 16:27:28 +09003349 *
3350 * <p>For callbacks registered with {@link #registerNetworkCallback}, multiple networks may
3351 * be available at the same time, and onAvailable will be called for each of these as they
3352 * appear.
3353 *
3354 * <p>For callbacks registered with {@link #requestNetwork} and
3355 * {@link #registerDefaultNetworkCallback}, this means the network passed as an argument
3356 * is the new best network for this request and is now tracked by this callback ; this
3357 * callback will no longer receive method calls about other networks that may have been
3358 * passed to this method previously. The previously-best network may have disconnected, or
3359 * it may still be around and the newly-best network may simply be better.
3360 *
3361 * <p>Starting with {@link android.os.Build.VERSION_CODES#O}, this will always immediately
3362 * be followed by a call to {@link #onCapabilitiesChanged(Network, NetworkCapabilities)}
3363 * then by a call to {@link #onLinkPropertiesChanged(Network, LinkProperties)}, and a call
3364 * to {@link #onBlockedStatusChanged(Network, boolean)}.
3365 *
3366 * <p>Do NOT call {@link #getNetworkCapabilities(Network)} or
3367 * {@link #getLinkProperties(Network)} or other synchronous ConnectivityManager methods in
3368 * this callback as this is prone to race conditions (there is no guarantee the objects
3369 * returned by these methods will be current). Instead, wait for a call to
3370 * {@link #onCapabilitiesChanged(Network, NetworkCapabilities)} and
3371 * {@link #onLinkPropertiesChanged(Network, LinkProperties)} whose arguments are guaranteed
3372 * to be well-ordered with respect to other callbacks.
Chalard Jeana23bc9e2018-01-30 22:41:41 +09003373 *
3374 * @param network The {@link Network} of the satisfying network.
Robert Greenwalte20f7a22014-04-18 15:25:25 -07003375 */
paulhu1a407652019-03-22 16:35:06 +08003376 public void onAvailable(@NonNull Network network) {}
Robert Greenwalte20f7a22014-04-18 15:25:25 -07003377
3378 /**
Chalard Jean01378b62019-08-30 16:27:28 +09003379 * Called when the network is about to be lost, typically because there are no outstanding
3380 * requests left for it. This may be paired with a {@link NetworkCallback#onAvailable} call
3381 * with the new replacement network for graceful handover. This method is not guaranteed
3382 * to be called before {@link NetworkCallback#onLost} is called, for example in case a
3383 * network is suddenly disconnected.
Robert Greenwaltf3017f72014-05-18 23:07:25 -07003384 *
Chalard Jean01378b62019-08-30 16:27:28 +09003385 * <p>Do NOT call {@link #getNetworkCapabilities(Network)} or
3386 * {@link #getLinkProperties(Network)} or other synchronous ConnectivityManager methods in
3387 * this callback as this is prone to race conditions ; calling these methods while in a
3388 * callback may return an outdated or even a null object.
3389 *
3390 * @param network The {@link Network} that is about to be lost.
3391 * @param maxMsToLive The time in milliseconds the system intends to keep the network
3392 * connected for graceful handover; note that the network may still
3393 * suffer a hard loss at any time.
Robert Greenwalte20f7a22014-04-18 15:25:25 -07003394 */
paulhu1a407652019-03-22 16:35:06 +08003395 public void onLosing(@NonNull Network network, int maxMsToLive) {}
Robert Greenwalte20f7a22014-04-18 15:25:25 -07003396
3397 /**
Chalard Jean01378b62019-08-30 16:27:28 +09003398 * Called when a network disconnects or otherwise no longer satisfies this request or
3399 * callback.
3400 *
3401 * <p>If the callback was registered with requestNetwork() or
3402 * registerDefaultNetworkCallback(), it will only be invoked against the last network
3403 * returned by onAvailable() when that network is lost and no other network satisfies
3404 * the criteria of the request.
3405 *
3406 * <p>If the callback was registered with registerNetworkCallback() it will be called for
3407 * each network which no longer satisfies the criteria of the callback.
3408 *
3409 * <p>Do NOT call {@link #getNetworkCapabilities(Network)} or
3410 * {@link #getLinkProperties(Network)} or other synchronous ConnectivityManager methods in
3411 * this callback as this is prone to race conditions ; calling these methods while in a
3412 * callback may return an outdated or even a null object.
Robert Greenwaltf3017f72014-05-18 23:07:25 -07003413 *
Robert Greenwaltf3017f72014-05-18 23:07:25 -07003414 * @param network The {@link Network} lost.
Robert Greenwalte20f7a22014-04-18 15:25:25 -07003415 */
paulhu1a407652019-03-22 16:35:06 +08003416 public void onLost(@NonNull Network network) {}
Robert Greenwalte20f7a22014-04-18 15:25:25 -07003417
3418 /**
Chalard Jean01378b62019-08-30 16:27:28 +09003419 * Called if no network is found within the timeout time specified in
Etan Cohenfbfdd842019-01-08 12:09:18 -08003420 * {@link #requestNetwork(NetworkRequest, NetworkCallback, int)} call or if the
3421 * requested network request cannot be fulfilled (whether or not a timeout was
3422 * specified). When this callback is invoked the associated
Etan Cohenf67bde92017-03-01 12:47:28 -08003423 * {@link NetworkRequest} will have already been removed and released, as if
3424 * {@link #unregisterNetworkCallback(NetworkCallback)} had been called.
Robert Greenwalte20f7a22014-04-18 15:25:25 -07003425 */
Robert Greenwaltf57c03c2014-06-11 16:05:07 -07003426 public void onUnavailable() {}
Robert Greenwalte20f7a22014-04-18 15:25:25 -07003427
3428 /**
Chalard Jean01378b62019-08-30 16:27:28 +09003429 * Called when the network corresponding to this request changes capabilities but still
3430 * satisfies the requested criteria.
3431 *
3432 * <p>Starting with {@link android.os.Build.VERSION_CODES#O} this method is guaranteed
3433 * to be called immediately after {@link #onAvailable}.
3434 *
3435 * <p>Do NOT call {@link #getLinkProperties(Network)} or other synchronous
3436 * ConnectivityManager methods in this callback as this is prone to race conditions :
3437 * calling these methods while in a callback may return an outdated or even a null object.
Robert Greenwaltf3017f72014-05-18 23:07:25 -07003438 *
Robert Greenwaltf3017f72014-05-18 23:07:25 -07003439 * @param network The {@link Network} whose capabilities have changed.
Roshan Pius951c0032020-12-22 15:10:42 -08003440 * @param networkCapabilities The new {@link NetworkCapabilities} for this
Chalard Jeana23bc9e2018-01-30 22:41:41 +09003441 * network.
Robert Greenwalte20f7a22014-04-18 15:25:25 -07003442 */
paulhu1a407652019-03-22 16:35:06 +08003443 public void onCapabilitiesChanged(@NonNull Network network,
3444 @NonNull NetworkCapabilities networkCapabilities) {}
Robert Greenwalte20f7a22014-04-18 15:25:25 -07003445
3446 /**
Chalard Jean01378b62019-08-30 16:27:28 +09003447 * Called when the network corresponding to this request changes {@link LinkProperties}.
3448 *
3449 * <p>Starting with {@link android.os.Build.VERSION_CODES#O} this method is guaranteed
3450 * to be called immediately after {@link #onAvailable}.
3451 *
3452 * <p>Do NOT call {@link #getNetworkCapabilities(Network)} or other synchronous
3453 * ConnectivityManager methods in this callback as this is prone to race conditions :
3454 * calling these methods while in a callback may return an outdated or even a null object.
Robert Greenwaltf3017f72014-05-18 23:07:25 -07003455 *
Robert Greenwaltf3017f72014-05-18 23:07:25 -07003456 * @param network The {@link Network} whose link properties have changed.
3457 * @param linkProperties The new {@link LinkProperties} for this network.
Robert Greenwalte20f7a22014-04-18 15:25:25 -07003458 */
paulhu1a407652019-03-22 16:35:06 +08003459 public void onLinkPropertiesChanged(@NonNull Network network,
3460 @NonNull LinkProperties linkProperties) {}
Robert Greenwalte20f7a22014-04-18 15:25:25 -07003461
Robert Greenwalt7fb8adc2015-06-24 13:23:42 -07003462 /**
Chalard Jean01378b62019-08-30 16:27:28 +09003463 * Called when the network the framework connected to for this request suspends data
3464 * transmission temporarily.
3465 *
3466 * <p>This generally means that while the TCP connections are still live temporarily
3467 * network data fails to transfer. To give a specific example, this is used on cellular
3468 * networks to mask temporary outages when driving through a tunnel, etc. In general this
3469 * means read operations on sockets on this network will block once the buffers are
3470 * drained, and write operations will block once the buffers are full.
3471 *
3472 * <p>Do NOT call {@link #getNetworkCapabilities(Network)} or
3473 * {@link #getLinkProperties(Network)} or other synchronous ConnectivityManager methods in
3474 * this callback as this is prone to race conditions (there is no guarantee the objects
3475 * returned by these methods will be current).
3476 *
Robert Greenwalt7fb8adc2015-06-24 13:23:42 -07003477 * @hide
3478 */
paulhu1a407652019-03-22 16:35:06 +08003479 public void onNetworkSuspended(@NonNull Network network) {}
Robert Greenwalt7fb8adc2015-06-24 13:23:42 -07003480
3481 /**
3482 * Called when the network the framework connected to for this request
Chalard Jeana23bc9e2018-01-30 22:41:41 +09003483 * returns from a {@link NetworkInfo.State#SUSPENDED} state. This should always be
3484 * preceded by a matching {@link NetworkCallback#onNetworkSuspended} call.
Chalard Jean01378b62019-08-30 16:27:28 +09003485
3486 * <p>Do NOT call {@link #getNetworkCapabilities(Network)} or
3487 * {@link #getLinkProperties(Network)} or other synchronous ConnectivityManager methods in
3488 * this callback as this is prone to race conditions : calling these methods while in a
3489 * callback may return an outdated or even a null object.
3490 *
Robert Greenwalt7fb8adc2015-06-24 13:23:42 -07003491 * @hide
3492 */
paulhu1a407652019-03-22 16:35:06 +08003493 public void onNetworkResumed(@NonNull Network network) {}
Robert Greenwalt7fb8adc2015-06-24 13:23:42 -07003494
junyulaif2c67e42018-08-07 19:50:45 +08003495 /**
3496 * Called when access to the specified network is blocked or unblocked.
3497 *
Chalard Jean01378b62019-08-30 16:27:28 +09003498 * <p>Do NOT call {@link #getNetworkCapabilities(Network)} or
3499 * {@link #getLinkProperties(Network)} or other synchronous ConnectivityManager methods in
3500 * this callback as this is prone to race conditions : calling these methods while in a
3501 * callback may return an outdated or even a null object.
3502 *
junyulaif2c67e42018-08-07 19:50:45 +08003503 * @param network The {@link Network} whose blocked status has changed.
3504 * @param blocked The blocked status of this {@link Network}.
3505 */
junyulai7e06ad42019-03-04 22:45:36 +08003506 public void onBlockedStatusChanged(@NonNull Network network, boolean blocked) {}
junyulaif2c67e42018-08-07 19:50:45 +08003507
Robert Greenwaltf57c03c2014-06-11 16:05:07 -07003508 private NetworkRequest networkRequest;
Roshan Pius951c0032020-12-22 15:10:42 -08003509 private final int mFlags;
Robert Greenwalte20f7a22014-04-18 15:25:25 -07003510 }
3511
Hugo Benichia590ab82017-05-11 13:16:17 +09003512 /**
3513 * Constant error codes used by ConnectivityService to communicate about failures and errors
3514 * across a Binder boundary.
3515 * @hide
3516 */
3517 public interface Errors {
Chalard Jean9dd11612018-06-04 16:52:49 +09003518 int TOO_MANY_REQUESTS = 1;
Hugo Benichia590ab82017-05-11 13:16:17 +09003519 }
3520
3521 /** @hide */
3522 public static class TooManyRequestsException extends RuntimeException {}
3523
3524 private static RuntimeException convertServiceException(ServiceSpecificException e) {
3525 switch (e.errorCode) {
3526 case Errors.TOO_MANY_REQUESTS:
3527 return new TooManyRequestsException();
3528 default:
3529 Log.w(TAG, "Unknown service error code " + e.errorCode);
3530 return new RuntimeException(e);
3531 }
3532 }
3533
Robert Greenwaltf99b8392014-03-26 16:47:06 -07003534 private static final int BASE = Protocol.BASE_CONNECTIVITY_MANAGER;
Robert Greenwaltf99b8392014-03-26 16:47:06 -07003535 /** @hide */
Robert Greenwalt7fb8adc2015-06-24 13:23:42 -07003536 public static final int CALLBACK_PRECHECK = BASE + 1;
3537 /** @hide */
3538 public static final int CALLBACK_AVAILABLE = BASE + 2;
3539 /** @hide arg1 = TTL */
3540 public static final int CALLBACK_LOSING = BASE + 3;
3541 /** @hide */
3542 public static final int CALLBACK_LOST = BASE + 4;
3543 /** @hide */
3544 public static final int CALLBACK_UNAVAIL = BASE + 5;
3545 /** @hide */
3546 public static final int CALLBACK_CAP_CHANGED = BASE + 6;
3547 /** @hide */
3548 public static final int CALLBACK_IP_CHANGED = BASE + 7;
Robert Greenwalt3dc73e52014-05-15 18:07:26 -07003549 /** @hide obj = NetworkCapabilities, arg1 = seq number */
Hugo Benichif8a0f9f2017-03-23 22:40:44 +09003550 private static final int EXPIRE_LEGACY_REQUEST = BASE + 8;
Robert Greenwalt7fb8adc2015-06-24 13:23:42 -07003551 /** @hide */
Hugo Benichif8a0f9f2017-03-23 22:40:44 +09003552 public static final int CALLBACK_SUSPENDED = BASE + 9;
Robert Greenwalt7fb8adc2015-06-24 13:23:42 -07003553 /** @hide */
Hugo Benichif8a0f9f2017-03-23 22:40:44 +09003554 public static final int CALLBACK_RESUMED = BASE + 10;
junyulaif2c67e42018-08-07 19:50:45 +08003555 /** @hide */
3556 public static final int CALLBACK_BLK_CHANGED = BASE + 11;
Robert Greenwaltf99b8392014-03-26 16:47:06 -07003557
Erik Kline155a59a2015-11-25 12:49:38 +09003558 /** @hide */
3559 public static String getCallbackName(int whichCallback) {
3560 switch (whichCallback) {
3561 case CALLBACK_PRECHECK: return "CALLBACK_PRECHECK";
3562 case CALLBACK_AVAILABLE: return "CALLBACK_AVAILABLE";
3563 case CALLBACK_LOSING: return "CALLBACK_LOSING";
3564 case CALLBACK_LOST: return "CALLBACK_LOST";
3565 case CALLBACK_UNAVAIL: return "CALLBACK_UNAVAIL";
3566 case CALLBACK_CAP_CHANGED: return "CALLBACK_CAP_CHANGED";
3567 case CALLBACK_IP_CHANGED: return "CALLBACK_IP_CHANGED";
Erik Kline155a59a2015-11-25 12:49:38 +09003568 case EXPIRE_LEGACY_REQUEST: return "EXPIRE_LEGACY_REQUEST";
3569 case CALLBACK_SUSPENDED: return "CALLBACK_SUSPENDED";
3570 case CALLBACK_RESUMED: return "CALLBACK_RESUMED";
junyulaif2c67e42018-08-07 19:50:45 +08003571 case CALLBACK_BLK_CHANGED: return "CALLBACK_BLK_CHANGED";
Erik Kline155a59a2015-11-25 12:49:38 +09003572 default:
3573 return Integer.toString(whichCallback);
3574 }
3575 }
3576
Robert Greenwalt3dc73e52014-05-15 18:07:26 -07003577 private class CallbackHandler extends Handler {
Robert Greenwaltf99b8392014-03-26 16:47:06 -07003578 private static final String TAG = "ConnectivityManager.CallbackHandler";
Robert Greenwalt72877c22015-09-03 16:41:45 -07003579 private static final boolean DBG = false;
Robert Greenwaltf99b8392014-03-26 16:47:06 -07003580
Hugo Benichi0b42baf2016-07-06 22:53:17 +09003581 CallbackHandler(Looper looper) {
Robert Greenwaltf99b8392014-03-26 16:47:06 -07003582 super(looper);
Robert Greenwaltf99b8392014-03-26 16:47:06 -07003583 }
3584
Hugo Benichifd44e912017-02-02 17:02:36 +09003585 CallbackHandler(Handler handler) {
Remi NGUYEN VAN5086c292021-03-15 07:27:44 +00003586 this(Preconditions.checkNotNull(handler, "Handler cannot be null.").getLooper());
Hugo Benichifd44e912017-02-02 17:02:36 +09003587 }
3588
Robert Greenwaltf99b8392014-03-26 16:47:06 -07003589 @Override
3590 public void handleMessage(Message message) {
Hugo Benichib6c24062017-05-09 14:36:02 +09003591 if (message.what == EXPIRE_LEGACY_REQUEST) {
3592 expireRequest((NetworkCapabilities) message.obj, message.arg1);
3593 return;
3594 }
3595
3596 final NetworkRequest request = getObject(message, NetworkRequest.class);
3597 final Network network = getObject(message, Network.class);
3598 final NetworkCallback callback;
3599 synchronized (sCallbacks) {
3600 callback = sCallbacks.get(request);
Etan Cohenb58e3662019-05-21 12:06:04 -07003601 if (callback == null) {
3602 Log.w(TAG,
3603 "callback not found for " + getCallbackName(message.what) + " message");
3604 return;
3605 }
Etan Cohen6cb65992019-04-16 15:07:55 -07003606 if (message.what == CALLBACK_UNAVAIL) {
3607 sCallbacks.remove(request);
3608 callback.networkRequest = ALREADY_UNREGISTERED;
3609 }
Hugo Benichib6c24062017-05-09 14:36:02 +09003610 }
Lorenzo Colittib027e6e2016-03-01 22:56:37 +09003611 if (DBG) {
Hugo Benichi8d962922017-03-22 17:07:57 +09003612 Log.d(TAG, getCallbackName(message.what) + " for network " + network);
Lorenzo Colittib027e6e2016-03-01 22:56:37 +09003613 }
Hugo Benichib6c24062017-05-09 14:36:02 +09003614
Robert Greenwaltf99b8392014-03-26 16:47:06 -07003615 switch (message.what) {
3616 case CALLBACK_PRECHECK: {
Hugo Benichib6c24062017-05-09 14:36:02 +09003617 callback.onPreCheck(network);
Robert Greenwaltf99b8392014-03-26 16:47:06 -07003618 break;
3619 }
3620 case CALLBACK_AVAILABLE: {
Chalard Jeana23bc9e2018-01-30 22:41:41 +09003621 NetworkCapabilities cap = getObject(message, NetworkCapabilities.class);
3622 LinkProperties lp = getObject(message, LinkProperties.class);
junyulaif2c67e42018-08-07 19:50:45 +08003623 callback.onAvailable(network, cap, lp, message.arg1 != 0);
Robert Greenwaltf99b8392014-03-26 16:47:06 -07003624 break;
3625 }
3626 case CALLBACK_LOSING: {
Hugo Benichib6c24062017-05-09 14:36:02 +09003627 callback.onLosing(network, message.arg1);
Robert Greenwaltf99b8392014-03-26 16:47:06 -07003628 break;
3629 }
3630 case CALLBACK_LOST: {
Hugo Benichib6c24062017-05-09 14:36:02 +09003631 callback.onLost(network);
Robert Greenwaltf99b8392014-03-26 16:47:06 -07003632 break;
3633 }
3634 case CALLBACK_UNAVAIL: {
Hugo Benichib6c24062017-05-09 14:36:02 +09003635 callback.onUnavailable();
Robert Greenwaltf99b8392014-03-26 16:47:06 -07003636 break;
3637 }
3638 case CALLBACK_CAP_CHANGED: {
Hugo Benichib6c24062017-05-09 14:36:02 +09003639 NetworkCapabilities cap = getObject(message, NetworkCapabilities.class);
3640 callback.onCapabilitiesChanged(network, cap);
Robert Greenwaltf99b8392014-03-26 16:47:06 -07003641 break;
3642 }
3643 case CALLBACK_IP_CHANGED: {
Hugo Benichib6c24062017-05-09 14:36:02 +09003644 LinkProperties lp = getObject(message, LinkProperties.class);
3645 callback.onLinkPropertiesChanged(network, lp);
Robert Greenwaltf99b8392014-03-26 16:47:06 -07003646 break;
3647 }
Robert Greenwalt7fb8adc2015-06-24 13:23:42 -07003648 case CALLBACK_SUSPENDED: {
Hugo Benichib6c24062017-05-09 14:36:02 +09003649 callback.onNetworkSuspended(network);
Robert Greenwalt7fb8adc2015-06-24 13:23:42 -07003650 break;
3651 }
3652 case CALLBACK_RESUMED: {
Hugo Benichib6c24062017-05-09 14:36:02 +09003653 callback.onNetworkResumed(network);
Robert Greenwalt3dc73e52014-05-15 18:07:26 -07003654 break;
3655 }
junyulaif2c67e42018-08-07 19:50:45 +08003656 case CALLBACK_BLK_CHANGED: {
3657 boolean blocked = message.arg1 != 0;
3658 callback.onBlockedStatusChanged(network, blocked);
3659 }
Robert Greenwaltf99b8392014-03-26 16:47:06 -07003660 }
3661 }
3662
Hugo Benichi0b42baf2016-07-06 22:53:17 +09003663 private <T> T getObject(Message msg, Class<T> c) {
3664 return (T) msg.getData().getParcelable(c.getSimpleName());
Robert Greenwaltf99b8392014-03-26 16:47:06 -07003665 }
Robert Greenwaltf99b8392014-03-26 16:47:06 -07003666 }
3667
Hugo Benichifd44e912017-02-02 17:02:36 +09003668 private CallbackHandler getDefaultHandler() {
Hugo Benichi3b41f052016-07-07 10:15:56 +09003669 synchronized (sCallbacks) {
3670 if (sCallbackHandler == null) {
3671 sCallbackHandler = new CallbackHandler(ConnectivityThread.getInstanceLooper());
Robert Greenwaltf99b8392014-03-26 16:47:06 -07003672 }
Hugo Benichi3b41f052016-07-07 10:15:56 +09003673 return sCallbackHandler;
Robert Greenwaltf99b8392014-03-26 16:47:06 -07003674 }
3675 }
3676
Hugo Benichibc4ac972017-02-03 14:18:44 +09003677 private static final HashMap<NetworkRequest, NetworkCallback> sCallbacks = new HashMap<>();
3678 private static CallbackHandler sCallbackHandler;
Robert Greenwaltf99b8392014-03-26 16:47:06 -07003679
Hugo Benichibc4ac972017-02-03 14:18:44 +09003680 private NetworkRequest sendRequestForNetwork(NetworkCapabilities need, NetworkCallback callback,
junyulaiad010792021-01-11 16:53:38 +08003681 int timeoutMs, NetworkRequest.Type reqType, int legacyType, CallbackHandler handler) {
Soi, Yoshinarib8102ef2015-11-12 12:09:02 +09003682 printStackTrace();
Hugo Benichibc1104b2017-05-09 15:19:01 +09003683 checkCallbackNotNull(callback);
Remi NGUYEN VAN5086c292021-03-15 07:27:44 +00003684 Preconditions.checkArgument(
3685 reqType == TRACK_DEFAULT || reqType == TRACK_SYSTEM_DEFAULT || need != null,
3686 "null NetworkCapabilities");
Hugo Benichi0b42baf2016-07-06 22:53:17 +09003687 final NetworkRequest request;
Roshan Piusbc7e37d2020-01-16 12:17:17 -08003688 final String callingPackageName = mContext.getOpPackageName();
Robert Greenwaltf99b8392014-03-26 16:47:06 -07003689 try {
Hugo Benichi0b42baf2016-07-06 22:53:17 +09003690 synchronized(sCallbacks) {
Hugo Benichibee30fe2017-06-17 13:14:12 +09003691 if (callback.networkRequest != null
3692 && callback.networkRequest != ALREADY_UNREGISTERED) {
Hugo Benichi2aa65af2017-03-06 09:17:06 +09003693 // TODO: throw exception instead and enforce 1:1 mapping of callbacks
3694 // and requests (http://b/20701525).
3695 Log.e(TAG, "NetworkCallback was already registered");
3696 }
Hugo Benichi3b41f052016-07-07 10:15:56 +09003697 Messenger messenger = new Messenger(handler);
Hugo Benichi0b42baf2016-07-06 22:53:17 +09003698 Binder binder = new Binder();
Roshan Pius951c0032020-12-22 15:10:42 -08003699 final int callbackFlags = callback.mFlags;
junyulaiad010792021-01-11 16:53:38 +08003700 if (reqType == LISTEN) {
Roshan Piusbc7e37d2020-01-16 12:17:17 -08003701 request = mService.listenForNetwork(
Roshan Pius951c0032020-12-22 15:10:42 -08003702 need, messenger, binder, callbackFlags, callingPackageName,
Roshan Piusaa24fde2020-12-17 14:53:09 -08003703 getAttributionTag());
Paul Jensenbb427682014-06-27 11:05:32 -04003704 } else {
Hugo Benichi0b42baf2016-07-06 22:53:17 +09003705 request = mService.requestNetwork(
junyulaiad010792021-01-11 16:53:38 +08003706 need, reqType.ordinal(), messenger, timeoutMs, binder, legacyType,
Roshan Pius951c0032020-12-22 15:10:42 -08003707 callbackFlags, callingPackageName, getAttributionTag());
Paul Jensenbb427682014-06-27 11:05:32 -04003708 }
Hugo Benichi0b42baf2016-07-06 22:53:17 +09003709 if (request != null) {
Hugo Benichi3b41f052016-07-07 10:15:56 +09003710 sCallbacks.put(request, callback);
Robert Greenwaltf99b8392014-03-26 16:47:06 -07003711 }
Hugo Benichi3b41f052016-07-07 10:15:56 +09003712 callback.networkRequest = request;
Robert Greenwaltf99b8392014-03-26 16:47:06 -07003713 }
Jeff Sharkeyc78f85c2016-03-01 19:27:23 -07003714 } catch (RemoteException e) {
3715 throw e.rethrowFromSystemServer();
Hugo Benichia590ab82017-05-11 13:16:17 +09003716 } catch (ServiceSpecificException e) {
3717 throw convertServiceException(e);
Jeff Sharkeyc78f85c2016-03-01 19:27:23 -07003718 }
Hugo Benichi0b42baf2016-07-06 22:53:17 +09003719 return request;
Robert Greenwaltf99b8392014-03-26 16:47:06 -07003720 }
3721
3722 /**
Erik Kline23bf99c2016-03-16 15:31:39 +09003723 * Helper function to request a network with a particular legacy type.
Lorenzo Colitti6653c4d2015-11-25 12:00:52 +09003724 *
markchienfac84a22020-03-18 21:16:15 +08003725 * This API is only for use in internal system code that requests networks with legacy type and
3726 * relies on CONNECTIVITY_ACTION broadcasts instead of NetworkCallbacks. New caller should use
markchiend6eeffd2020-01-14 00:55:21 +08003727 * {@link #requestNetwork(NetworkRequest, NetworkCallback, Handler)} instead.
Lorenzo Colitti6653c4d2015-11-25 12:00:52 +09003728 *
markchiend6eeffd2020-01-14 00:55:21 +08003729 * @param request {@link NetworkRequest} describing this request.
markchiend6eeffd2020-01-14 00:55:21 +08003730 * @param timeoutMs The time in milliseconds to attempt looking for a suitable network
3731 * before {@link NetworkCallback#onUnavailable()} is called. The timeout must
3732 * be a positive value (i.e. >0).
3733 * @param legacyType to specify the network type(#TYPE_*).
3734 * @param handler {@link Handler} to specify the thread upon which the callback will be invoked.
markchienfac84a22020-03-18 21:16:15 +08003735 * @param networkCallback The {@link NetworkCallback} to be utilized for this request. Note
3736 * the callback must not be shared - it uniquely specifies this request.
Lorenzo Colitti6653c4d2015-11-25 12:00:52 +09003737 *
3738 * @hide
3739 */
markchiend6eeffd2020-01-14 00:55:21 +08003740 @SystemApi
markchienfac84a22020-03-18 21:16:15 +08003741 @RequiresPermission(NetworkStack.PERMISSION_MAINLINE_NETWORK_STACK)
Chalard Jean158702d2019-01-07 19:26:34 +09003742 public void requestNetwork(@NonNull NetworkRequest request,
markchienfac84a22020-03-18 21:16:15 +08003743 int timeoutMs, int legacyType, @NonNull Handler handler,
3744 @NonNull NetworkCallback networkCallback) {
3745 if (legacyType == TYPE_NONE) {
3746 throw new IllegalArgumentException("TYPE_NONE is meaningless legacy type");
3747 }
Hugo Benichifd44e912017-02-02 17:02:36 +09003748 CallbackHandler cbHandler = new CallbackHandler(handler);
3749 NetworkCapabilities nc = request.networkCapabilities;
3750 sendRequestForNetwork(nc, networkCallback, timeoutMs, REQUEST, legacyType, cbHandler);
Lorenzo Colitti6653c4d2015-11-25 12:00:52 +09003751 }
3752
3753 /**
Roshan Pius951c0032020-12-22 15:10:42 -08003754 * Request a network to satisfy a set of {@link NetworkCapabilities}.
Robert Greenwaltf99b8392014-03-26 16:47:06 -07003755 *
Chalard Jean01378b62019-08-30 16:27:28 +09003756 * <p>This method will attempt to find the best network that matches the passed
3757 * {@link NetworkRequest}, and to bring up one that does if none currently satisfies the
3758 * criteria. The platform will evaluate which network is the best at its own discretion.
3759 * Throughput, latency, cost per byte, policy, user preference and other considerations
3760 * may be factored in the decision of what is considered the best network.
3761 *
3762 * <p>As long as this request is outstanding, the platform will try to maintain the best network
3763 * matching this request, while always attempting to match the request to a better network if
3764 * possible. If a better match is found, the platform will switch this request to the now-best
3765 * network and inform the app of the newly best network by invoking
3766 * {@link NetworkCallback#onAvailable(Network)} on the provided callback. Note that the platform
3767 * will not try to maintain any other network than the best one currently matching the request:
3768 * a network not matching any network request may be disconnected at any time.
3769 *
3770 * <p>For example, an application could use this method to obtain a connected cellular network
3771 * even if the device currently has a data connection over Ethernet. This may cause the cellular
3772 * radio to consume additional power. Or, an application could inform the system that it wants
3773 * a network supporting sending MMSes and have the system let it know about the currently best
3774 * MMS-supporting network through the provided {@link NetworkCallback}.
3775 *
3776 * <p>The status of the request can be followed by listening to the various callbacks described
3777 * in {@link NetworkCallback}. The {@link Network} object passed to the callback methods can be
3778 * used to direct traffic to the network (although accessing some networks may be subject to
3779 * holding specific permissions). Callers will learn about the specific characteristics of the
3780 * network through
3781 * {@link NetworkCallback#onCapabilitiesChanged(Network, NetworkCapabilities)} and
3782 * {@link NetworkCallback#onLinkPropertiesChanged(Network, LinkProperties)}. The methods of the
3783 * provided {@link NetworkCallback} will only be invoked due to changes in the best network
3784 * matching the request at any given time; therefore when a better network matching the request
3785 * becomes available, the {@link NetworkCallback#onAvailable(Network)} method is called
3786 * with the new network after which no further updates are given about the previously-best
3787 * network, unless it becomes the best again at some later time. All callbacks are invoked
3788 * in order on the same thread, which by default is a thread created by the framework running
3789 * in the app.
3790 * {@see #requestNetwork(NetworkRequest, NetworkCallback, Handler)} to change where the
3791 * callbacks are invoked.
3792 *
3793 * <p>This{@link NetworkRequest} will live until released via
3794 * {@link #unregisterNetworkCallback(NetworkCallback)} or the calling application exits, at
3795 * which point the system may let go of the network at any time.
3796 *
3797 * <p>A version of this method which takes a timeout is
3798 * {@link #requestNetwork(NetworkRequest, NetworkCallback, int)}, that an app can use to only
3799 * wait for a limited amount of time for the network to become unavailable.
3800 *
Paul Jensenee52d232015-06-16 15:11:58 -04003801 * <p>It is presently unsupported to request a network with mutable
3802 * {@link NetworkCapabilities} such as
3803 * {@link NetworkCapabilities#NET_CAPABILITY_VALIDATED} or
3804 * {@link NetworkCapabilities#NET_CAPABILITY_CAPTIVE_PORTAL}
3805 * as these {@code NetworkCapabilities} represent states that a particular
3806 * network may never attain, and whether a network will attain these states
3807 * is unknown prior to bringing up the network so the framework does not
Chalard Jean01378b62019-08-30 16:27:28 +09003808 * know how to go about satisfying a request with these capabilities.
Lorenzo Colitti0b40c872015-10-15 16:29:00 +09003809 *
3810 * <p>This method requires the caller to hold either the
3811 * {@link android.Manifest.permission#CHANGE_NETWORK_STATE} permission
3812 * or the ability to modify system settings as determined by
3813 * {@link android.provider.Settings.System#canWrite}.</p>
Robert Greenwaltf99b8392014-03-26 16:47:06 -07003814 *
Chalard Jeana5ff1132020-05-20 16:11:50 +09003815 * <p>To avoid performance issues due to apps leaking callbacks, the system will limit the
3816 * number of outstanding requests to 100 per app (identified by their UID), shared with
3817 * all variants of this method, of {@link #registerNetworkCallback} as well as
3818 * {@link ConnectivityDiagnosticsManager#registerConnectivityDiagnosticsCallback}.
3819 * Requesting a network with this method will count toward this limit. If this limit is
3820 * exceeded, an exception will be thrown. To avoid hitting this issue and to conserve resources,
3821 * make sure to unregister the callbacks with
3822 * {@link #unregisterNetworkCallback(NetworkCallback)}.
3823 *
Robert Greenwaltf57c03c2014-06-11 16:05:07 -07003824 * @param request {@link NetworkRequest} describing this request.
Hugo Benichifd44e912017-02-02 17:02:36 +09003825 * @param networkCallback The {@link NetworkCallback} to be utilized for this request. Note
3826 * the callback must not be shared - it uniquely specifies this request.
3827 * The callback is invoked on the default internal Handler.
Chalard Jean31740e42019-05-13 15:13:58 +09003828 * @throws IllegalArgumentException if {@code request} contains invalid network capabilities.
3829 * @throws SecurityException if missing the appropriate permissions.
Chalard Jeana5ff1132020-05-20 16:11:50 +09003830 * @throws RuntimeException if the app already has too many callbacks registered.
Robert Greenwaltf99b8392014-03-26 16:47:06 -07003831 */
Chalard Jean158702d2019-01-07 19:26:34 +09003832 public void requestNetwork(@NonNull NetworkRequest request,
3833 @NonNull NetworkCallback networkCallback) {
Hugo Benichifd44e912017-02-02 17:02:36 +09003834 requestNetwork(request, networkCallback, getDefaultHandler());
3835 }
3836
3837 /**
Roshan Pius951c0032020-12-22 15:10:42 -08003838 * Request a network to satisfy a set of {@link NetworkCapabilities}.
Hugo Benichifd44e912017-02-02 17:02:36 +09003839 *
Chalard Jean01378b62019-08-30 16:27:28 +09003840 * This method behaves identically to {@link #requestNetwork(NetworkRequest, NetworkCallback)}
3841 * but runs all the callbacks on the passed Handler.
Hugo Benichifd44e912017-02-02 17:02:36 +09003842 *
Chalard Jean01378b62019-08-30 16:27:28 +09003843 * <p>This method has the same permission requirements as
Chalard Jeana5ff1132020-05-20 16:11:50 +09003844 * {@link #requestNetwork(NetworkRequest, NetworkCallback)}, is subject to the same limitations,
3845 * and throws the same exceptions in the same conditions.
Hugo Benichifd44e912017-02-02 17:02:36 +09003846 *
3847 * @param request {@link NetworkRequest} describing this request.
3848 * @param networkCallback The {@link NetworkCallback} to be utilized for this request. Note
3849 * the callback must not be shared - it uniquely specifies this request.
3850 * @param handler {@link Handler} to specify the thread upon which the callback will be invoked.
Hugo Benichifd44e912017-02-02 17:02:36 +09003851 */
Chalard Jean158702d2019-01-07 19:26:34 +09003852 public void requestNetwork(@NonNull NetworkRequest request,
3853 @NonNull NetworkCallback networkCallback, @NonNull Handler handler) {
Hugo Benichifd44e912017-02-02 17:02:36 +09003854 CallbackHandler cbHandler = new CallbackHandler(handler);
markchienfac84a22020-03-18 21:16:15 +08003855 NetworkCapabilities nc = request.networkCapabilities;
3856 sendRequestForNetwork(nc, networkCallback, 0, REQUEST, TYPE_NONE, cbHandler);
Robert Greenwaltf99b8392014-03-26 16:47:06 -07003857 }
3858
3859 /**
Roshan Pius951c0032020-12-22 15:10:42 -08003860 * Request a network to satisfy a set of {@link NetworkCapabilities}, limited
Etan Cohenf67bde92017-03-01 12:47:28 -08003861 * by a timeout.
3862 *
3863 * This function behaves identically to the non-timed-out version
3864 * {@link #requestNetwork(NetworkRequest, NetworkCallback)}, but if a suitable network
3865 * is not found within the given time (in milliseconds) the
3866 * {@link NetworkCallback#onUnavailable()} callback is called. The request can still be
3867 * released normally by calling {@link #unregisterNetworkCallback(NetworkCallback)} but does
3868 * not have to be released if timed-out (it is automatically released). Unregistering a
3869 * request that timed out is not an error.
3870 *
3871 * <p>Do not use this method to poll for the existence of specific networks (e.g. with a small
3872 * timeout) - {@link #registerNetworkCallback(NetworkRequest, NetworkCallback)} is provided
3873 * for that purpose. Calling this method will attempt to bring up the requested network.
3874 *
Chalard Jean01378b62019-08-30 16:27:28 +09003875 * <p>This method has the same permission requirements as
Chalard Jeana5ff1132020-05-20 16:11:50 +09003876 * {@link #requestNetwork(NetworkRequest, NetworkCallback)}, is subject to the same limitations,
3877 * and throws the same exceptions in the same conditions.
Etan Cohenf67bde92017-03-01 12:47:28 -08003878 *
3879 * @param request {@link NetworkRequest} describing this request.
Lorenzo Colitti139a48c2017-04-28 00:56:30 +09003880 * @param networkCallback The {@link NetworkCallback} to be utilized for this request. Note
3881 * the callback must not be shared - it uniquely specifies this request.
Etan Cohenf67bde92017-03-01 12:47:28 -08003882 * @param timeoutMs The time in milliseconds to attempt looking for a suitable network
3883 * before {@link NetworkCallback#onUnavailable()} is called. The timeout must
3884 * be a positive value (i.e. >0).
Etan Cohenf67bde92017-03-01 12:47:28 -08003885 */
Chalard Jean158702d2019-01-07 19:26:34 +09003886 public void requestNetwork(@NonNull NetworkRequest request,
3887 @NonNull NetworkCallback networkCallback, int timeoutMs) {
Hugo Benichibc1104b2017-05-09 15:19:01 +09003888 checkTimeout(timeoutMs);
markchienfac84a22020-03-18 21:16:15 +08003889 NetworkCapabilities nc = request.networkCapabilities;
3890 sendRequestForNetwork(nc, networkCallback, timeoutMs, REQUEST, TYPE_NONE,
3891 getDefaultHandler());
Hugo Benichifd44e912017-02-02 17:02:36 +09003892 }
3893
Hugo Benichifd44e912017-02-02 17:02:36 +09003894 /**
Roshan Pius951c0032020-12-22 15:10:42 -08003895 * Request a network to satisfy a set of {@link NetworkCapabilities}, limited
Hugo Benichifd44e912017-02-02 17:02:36 +09003896 * by a timeout.
3897 *
Chalard Jean01378b62019-08-30 16:27:28 +09003898 * This method behaves identically to
3899 * {@link #requestNetwork(NetworkRequest, NetworkCallback, int)} but runs all the callbacks
3900 * on the passed Handler.
Etan Cohenf67bde92017-03-01 12:47:28 -08003901 *
Chalard Jean01378b62019-08-30 16:27:28 +09003902 * <p>This method has the same permission requirements as
Chalard Jeana5ff1132020-05-20 16:11:50 +09003903 * {@link #requestNetwork(NetworkRequest, NetworkCallback)}, is subject to the same limitations,
3904 * and throws the same exceptions in the same conditions.
Hugo Benichifd44e912017-02-02 17:02:36 +09003905 *
3906 * @param request {@link NetworkRequest} describing this request.
Etan Cohenf67bde92017-03-01 12:47:28 -08003907 * @param networkCallback The {@link NetworkCallback} to be utilized for this request. Note
3908 * the callback must not be shared - it uniquely specifies this request.
Hugo Benichifd44e912017-02-02 17:02:36 +09003909 * @param handler {@link Handler} to specify the thread upon which the callback will be invoked.
Lorenzo Colitti139a48c2017-04-28 00:56:30 +09003910 * @param timeoutMs The time in milliseconds to attempt looking for a suitable network
3911 * before {@link NetworkCallback#onUnavailable} is called.
Hugo Benichifd44e912017-02-02 17:02:36 +09003912 */
Chalard Jean158702d2019-01-07 19:26:34 +09003913 public void requestNetwork(@NonNull NetworkRequest request,
3914 @NonNull NetworkCallback networkCallback, @NonNull Handler handler, int timeoutMs) {
Hugo Benichibc1104b2017-05-09 15:19:01 +09003915 checkTimeout(timeoutMs);
Hugo Benichifd44e912017-02-02 17:02:36 +09003916 CallbackHandler cbHandler = new CallbackHandler(handler);
markchienfac84a22020-03-18 21:16:15 +08003917 NetworkCapabilities nc = request.networkCapabilities;
3918 sendRequestForNetwork(nc, networkCallback, timeoutMs, REQUEST, TYPE_NONE, cbHandler);
Robert Greenwaltf99b8392014-03-26 16:47:06 -07003919 }
3920
3921 /**
Robert Greenwaltf3017f72014-05-18 23:07:25 -07003922 * The lookup key for a {@link Network} object included with the intent after
Jeremy Joslin60d379b2014-11-05 10:32:09 -08003923 * successfully finding a network for the applications request. Retrieve it with
Robert Greenwaltf3017f72014-05-18 23:07:25 -07003924 * {@link android.content.Intent#getParcelableExtra(String)}.
Jeremy Joslinc5302632015-02-11 16:51:13 -08003925 * <p>
Paul Jensenee2f45d2015-03-10 10:54:12 -04003926 * Note that if you intend to invoke {@link Network#openConnection(java.net.URL)}
3927 * then you must get a ConnectivityManager instance before doing so.
Robert Greenwaltf3017f72014-05-18 23:07:25 -07003928 */
Erik Kline8a826212014-11-19 12:12:24 +09003929 public static final String EXTRA_NETWORK = "android.net.extra.NETWORK";
Robert Greenwaltf3017f72014-05-18 23:07:25 -07003930
3931 /**
Robert Greenwaltf57c03c2014-06-11 16:05:07 -07003932 * The lookup key for a {@link NetworkRequest} object included with the intent after
Jeremy Joslin60d379b2014-11-05 10:32:09 -08003933 * successfully finding a network for the applications request. Retrieve it with
Robert Greenwaltf3017f72014-05-18 23:07:25 -07003934 * {@link android.content.Intent#getParcelableExtra(String)}.
3935 */
Erik Kline8a826212014-11-19 12:12:24 +09003936 public static final String EXTRA_NETWORK_REQUEST = "android.net.extra.NETWORK_REQUEST";
Robert Greenwaltf3017f72014-05-18 23:07:25 -07003937
3938
3939 /**
Roshan Pius951c0032020-12-22 15:10:42 -08003940 * Request a network to satisfy a set of {@link NetworkCapabilities}.
Robert Greenwaltf99b8392014-03-26 16:47:06 -07003941 *
Jeremy Joslin60d379b2014-11-05 10:32:09 -08003942 * This function behaves identically to the version that takes a NetworkCallback, but instead
Robert Greenwaltf57c03c2014-06-11 16:05:07 -07003943 * of {@link NetworkCallback} a {@link PendingIntent} is used. This means
Robert Greenwaltf3017f72014-05-18 23:07:25 -07003944 * the request may outlive the calling application and get called back when a suitable
3945 * network is found.
Robert Greenwaltf99b8392014-03-26 16:47:06 -07003946 * <p>
3947 * The operation is an Intent broadcast that goes to a broadcast receiver that
3948 * you registered with {@link Context#registerReceiver} or through the
3949 * &lt;receiver&gt; tag in an AndroidManifest.xml file
3950 * <p>
3951 * The operation Intent is delivered with two extras, a {@link Network} typed
Erik Kline8a826212014-11-19 12:12:24 +09003952 * extra called {@link #EXTRA_NETWORK} and a {@link NetworkRequest}
3953 * typed extra called {@link #EXTRA_NETWORK_REQUEST} containing
Robert Greenwaltf99b8392014-03-26 16:47:06 -07003954 * the original requests parameters. It is important to create a new,
Robert Greenwaltf57c03c2014-06-11 16:05:07 -07003955 * {@link NetworkCallback} based request before completing the processing of the
Robert Greenwaltf99b8392014-03-26 16:47:06 -07003956 * Intent to reserve the network or it will be released shortly after the Intent
3957 * is processed.
3958 * <p>
Paul Jensenc8873fc2015-06-17 14:15:39 -04003959 * If there is already a request for this Intent registered (with the equality of
Robert Greenwaltf99b8392014-03-26 16:47:06 -07003960 * two Intents defined by {@link Intent#filterEquals}), then it will be removed and
Robert Greenwaltf3017f72014-05-18 23:07:25 -07003961 * replaced by this one, effectively releasing the previous {@link NetworkRequest}.
Robert Greenwaltf99b8392014-03-26 16:47:06 -07003962 * <p>
Jeremy Joslin60d379b2014-11-05 10:32:09 -08003963 * The request may be released normally by calling
3964 * {@link #releaseNetworkRequest(android.app.PendingIntent)}.
Paul Jensenee52d232015-06-16 15:11:58 -04003965 * <p>It is presently unsupported to request a network with either
3966 * {@link NetworkCapabilities#NET_CAPABILITY_VALIDATED} or
3967 * {@link NetworkCapabilities#NET_CAPABILITY_CAPTIVE_PORTAL}
3968 * as these {@code NetworkCapabilities} represent states that a particular
3969 * network may never attain, and whether a network will attain these states
3970 * is unknown prior to bringing up the network so the framework does not
Chalard Jean9dd11612018-06-04 16:52:49 +09003971 * know how to go about satisfying a request with these capabilities.
Lorenzo Colitti0b40c872015-10-15 16:29:00 +09003972 *
Chalard Jeana5ff1132020-05-20 16:11:50 +09003973 * <p>To avoid performance issues due to apps leaking callbacks, the system will limit the
3974 * number of outstanding requests to 100 per app (identified by their UID), shared with
3975 * all variants of this method, of {@link #registerNetworkCallback} as well as
3976 * {@link ConnectivityDiagnosticsManager#registerConnectivityDiagnosticsCallback}.
3977 * Requesting a network with this method will count toward this limit. If this limit is
3978 * exceeded, an exception will be thrown. To avoid hitting this issue and to conserve resources,
3979 * make sure to unregister the callbacks with {@link #unregisterNetworkCallback(PendingIntent)}
3980 * or {@link #releaseNetworkRequest(PendingIntent)}.
3981 *
Lorenzo Colitti0b40c872015-10-15 16:29:00 +09003982 * <p>This method requires the caller to hold either the
3983 * {@link android.Manifest.permission#CHANGE_NETWORK_STATE} permission
3984 * or the ability to modify system settings as determined by
3985 * {@link android.provider.Settings.System#canWrite}.</p>
3986 *
Robert Greenwaltf57c03c2014-06-11 16:05:07 -07003987 * @param request {@link NetworkRequest} describing this request.
Robert Greenwaltf99b8392014-03-26 16:47:06 -07003988 * @param operation Action to perform when the network is available (corresponds
Robert Greenwaltf57c03c2014-06-11 16:05:07 -07003989 * to the {@link NetworkCallback#onAvailable} call. Typically
Jeremy Joslin60d379b2014-11-05 10:32:09 -08003990 * comes from {@link PendingIntent#getBroadcast}. Cannot be null.
Chalard Jean31740e42019-05-13 15:13:58 +09003991 * @throws IllegalArgumentException if {@code request} contains invalid network capabilities.
3992 * @throws SecurityException if missing the appropriate permissions.
Chalard Jeana5ff1132020-05-20 16:11:50 +09003993 * @throws RuntimeException if the app already has too many callbacks registered.
Robert Greenwaltf99b8392014-03-26 16:47:06 -07003994 */
Chalard Jean158702d2019-01-07 19:26:34 +09003995 public void requestNetwork(@NonNull NetworkRequest request,
3996 @NonNull PendingIntent operation) {
Soi, Yoshinarib8102ef2015-11-12 12:09:02 +09003997 printStackTrace();
Hugo Benichibc1104b2017-05-09 15:19:01 +09003998 checkPendingIntentNotNull(operation);
Robert Greenwaltf99b8392014-03-26 16:47:06 -07003999 try {
Roshan Piusbc7e37d2020-01-16 12:17:17 -08004000 mService.pendingRequestForNetwork(
Philip P. Moltmann7bc33df2020-03-26 11:50:35 -07004001 request.networkCapabilities, operation, mContext.getOpPackageName(),
4002 getAttributionTag());
Jeff Sharkeyc78f85c2016-03-01 19:27:23 -07004003 } catch (RemoteException e) {
4004 throw e.rethrowFromSystemServer();
Hugo Benichia590ab82017-05-11 13:16:17 +09004005 } catch (ServiceSpecificException e) {
4006 throw convertServiceException(e);
Jeff Sharkeyc78f85c2016-03-01 19:27:23 -07004007 }
Robert Greenwaltf99b8392014-03-26 16:47:06 -07004008 }
4009
4010 /**
Jeremy Joslin60d379b2014-11-05 10:32:09 -08004011 * Removes a request made via {@link #requestNetwork(NetworkRequest, android.app.PendingIntent)}
4012 * <p>
Lorenzo Colitti15874cd2016-04-13 22:00:02 +09004013 * This method has the same behavior as
4014 * {@link #unregisterNetworkCallback(android.app.PendingIntent)} with respect to
Jeremy Joslin60d379b2014-11-05 10:32:09 -08004015 * releasing network resources and disconnecting.
4016 *
4017 * @param operation A PendingIntent equal (as defined by {@link Intent#filterEquals}) to the
4018 * PendingIntent passed to
4019 * {@link #requestNetwork(NetworkRequest, android.app.PendingIntent)} with the
4020 * corresponding NetworkRequest you'd like to remove. Cannot be null.
4021 */
Chalard Jean158702d2019-01-07 19:26:34 +09004022 public void releaseNetworkRequest(@NonNull PendingIntent operation) {
Soi, Yoshinarib8102ef2015-11-12 12:09:02 +09004023 printStackTrace();
Hugo Benichibc1104b2017-05-09 15:19:01 +09004024 checkPendingIntentNotNull(operation);
Jeremy Joslin60d379b2014-11-05 10:32:09 -08004025 try {
4026 mService.releasePendingNetworkRequest(operation);
Jeff Sharkeyc78f85c2016-03-01 19:27:23 -07004027 } catch (RemoteException e) {
4028 throw e.rethrowFromSystemServer();
4029 }
Jeremy Joslin60d379b2014-11-05 10:32:09 -08004030 }
4031
Hugo Benichibc1104b2017-05-09 15:19:01 +09004032 private static void checkPendingIntentNotNull(PendingIntent intent) {
Remi NGUYEN VAN5086c292021-03-15 07:27:44 +00004033 Preconditions.checkNotNull(intent, "PendingIntent cannot be null.");
Hugo Benichibc1104b2017-05-09 15:19:01 +09004034 }
4035
4036 private static void checkCallbackNotNull(NetworkCallback callback) {
Remi NGUYEN VAN5086c292021-03-15 07:27:44 +00004037 Preconditions.checkNotNull(callback, "null NetworkCallback");
Hugo Benichibc1104b2017-05-09 15:19:01 +09004038 }
4039
4040 private static void checkTimeout(int timeoutMs) {
Remi NGUYEN VAN5086c292021-03-15 07:27:44 +00004041 Preconditions.checkArgumentPositive(timeoutMs, "timeoutMs must be strictly positive.");
Jeremy Joslin60d379b2014-11-05 10:32:09 -08004042 }
4043
4044 /**
Robert Greenwaltf99b8392014-03-26 16:47:06 -07004045 * Registers to receive notifications about all networks which satisfy the given
Robert Greenwaltf57c03c2014-06-11 16:05:07 -07004046 * {@link NetworkRequest}. The callbacks will continue to be called until
Chiachang Wang3b723c22019-02-27 17:14:50 +08004047 * either the application exits or {@link #unregisterNetworkCallback(NetworkCallback)} is
4048 * called.
Robert Greenwaltf99b8392014-03-26 16:47:06 -07004049 *
Chalard Jeana5ff1132020-05-20 16:11:50 +09004050 * <p>To avoid performance issues due to apps leaking callbacks, the system will limit the
4051 * number of outstanding requests to 100 per app (identified by their UID), shared with
4052 * all variants of this method, of {@link #requestNetwork} as well as
4053 * {@link ConnectivityDiagnosticsManager#registerConnectivityDiagnosticsCallback}.
4054 * Requesting a network with this method will count toward this limit. If this limit is
4055 * exceeded, an exception will be thrown. To avoid hitting this issue and to conserve resources,
4056 * make sure to unregister the callbacks with
4057 * {@link #unregisterNetworkCallback(NetworkCallback)}.
4058 *
Robert Greenwaltf57c03c2014-06-11 16:05:07 -07004059 * @param request {@link NetworkRequest} describing this request.
4060 * @param networkCallback The {@link NetworkCallback} that the system will call as suitable
4061 * networks change state.
Hugo Benichifd44e912017-02-02 17:02:36 +09004062 * The callback is invoked on the default internal Handler.
Chalard Jeana5ff1132020-05-20 16:11:50 +09004063 * @throws RuntimeException if the app already has too many callbacks registered.
Robert Greenwaltf99b8392014-03-26 16:47:06 -07004064 */
Jeff Sharkey656584a2017-04-24 11:18:03 -06004065 @RequiresPermission(android.Manifest.permission.ACCESS_NETWORK_STATE)
Chalard Jean158702d2019-01-07 19:26:34 +09004066 public void registerNetworkCallback(@NonNull NetworkRequest request,
4067 @NonNull NetworkCallback networkCallback) {
Hugo Benichifd44e912017-02-02 17:02:36 +09004068 registerNetworkCallback(request, networkCallback, getDefaultHandler());
4069 }
4070
4071 /**
4072 * Registers to receive notifications about all networks which satisfy the given
4073 * {@link NetworkRequest}. The callbacks will continue to be called until
Chiachang Wang3b723c22019-02-27 17:14:50 +08004074 * either the application exits or {@link #unregisterNetworkCallback(NetworkCallback)} is
4075 * called.
Hugo Benichifd44e912017-02-02 17:02:36 +09004076 *
Chalard Jeana5ff1132020-05-20 16:11:50 +09004077 * <p>To avoid performance issues due to apps leaking callbacks, the system will limit the
4078 * number of outstanding requests to 100 per app (identified by their UID), shared with
4079 * all variants of this method, of {@link #requestNetwork} as well as
4080 * {@link ConnectivityDiagnosticsManager#registerConnectivityDiagnosticsCallback}.
4081 * Requesting a network with this method will count toward this limit. If this limit is
4082 * exceeded, an exception will be thrown. To avoid hitting this issue and to conserve resources,
4083 * make sure to unregister the callbacks with
4084 * {@link #unregisterNetworkCallback(NetworkCallback)}.
4085 *
4086 *
Hugo Benichifd44e912017-02-02 17:02:36 +09004087 * @param request {@link NetworkRequest} describing this request.
4088 * @param networkCallback The {@link NetworkCallback} that the system will call as suitable
4089 * networks change state.
4090 * @param handler {@link Handler} to specify the thread upon which the callback will be invoked.
Chalard Jeana5ff1132020-05-20 16:11:50 +09004091 * @throws RuntimeException if the app already has too many callbacks registered.
Hugo Benichifd44e912017-02-02 17:02:36 +09004092 */
Jeff Sharkey656584a2017-04-24 11:18:03 -06004093 @RequiresPermission(android.Manifest.permission.ACCESS_NETWORK_STATE)
Chalard Jean158702d2019-01-07 19:26:34 +09004094 public void registerNetworkCallback(@NonNull NetworkRequest request,
4095 @NonNull NetworkCallback networkCallback, @NonNull Handler handler) {
Hugo Benichifd44e912017-02-02 17:02:36 +09004096 CallbackHandler cbHandler = new CallbackHandler(handler);
4097 NetworkCapabilities nc = request.networkCapabilities;
4098 sendRequestForNetwork(nc, networkCallback, 0, LISTEN, TYPE_NONE, cbHandler);
Robert Greenwaltf99b8392014-03-26 16:47:06 -07004099 }
4100
4101 /**
Paul Jensenc8873fc2015-06-17 14:15:39 -04004102 * Registers a PendingIntent to be sent when a network is available which satisfies the given
4103 * {@link NetworkRequest}.
4104 *
4105 * This function behaves identically to the version that takes a NetworkCallback, but instead
4106 * of {@link NetworkCallback} a {@link PendingIntent} is used. This means
4107 * the request may outlive the calling application and get called back when a suitable
4108 * network is found.
4109 * <p>
4110 * The operation is an Intent broadcast that goes to a broadcast receiver that
4111 * you registered with {@link Context#registerReceiver} or through the
4112 * &lt;receiver&gt; tag in an AndroidManifest.xml file
4113 * <p>
4114 * The operation Intent is delivered with two extras, a {@link Network} typed
4115 * extra called {@link #EXTRA_NETWORK} and a {@link NetworkRequest}
4116 * typed extra called {@link #EXTRA_NETWORK_REQUEST} containing
4117 * the original requests parameters.
4118 * <p>
4119 * If there is already a request for this Intent registered (with the equality of
4120 * two Intents defined by {@link Intent#filterEquals}), then it will be removed and
4121 * replaced by this one, effectively releasing the previous {@link NetworkRequest}.
4122 * <p>
4123 * The request may be released normally by calling
Paul Jensen169f6622015-06-30 14:29:18 -04004124 * {@link #unregisterNetworkCallback(android.app.PendingIntent)}.
Chalard Jeana5ff1132020-05-20 16:11:50 +09004125 *
4126 * <p>To avoid performance issues due to apps leaking callbacks, the system will limit the
4127 * number of outstanding requests to 100 per app (identified by their UID), shared with
4128 * all variants of this method, of {@link #requestNetwork} as well as
4129 * {@link ConnectivityDiagnosticsManager#registerConnectivityDiagnosticsCallback}.
4130 * Requesting a network with this method will count toward this limit. If this limit is
4131 * exceeded, an exception will be thrown. To avoid hitting this issue and to conserve resources,
4132 * make sure to unregister the callbacks with {@link #unregisterNetworkCallback(PendingIntent)}
4133 * or {@link #releaseNetworkRequest(PendingIntent)}.
4134 *
Paul Jensenc8873fc2015-06-17 14:15:39 -04004135 * @param request {@link NetworkRequest} describing this request.
4136 * @param operation Action to perform when the network is available (corresponds
4137 * to the {@link NetworkCallback#onAvailable} call. Typically
4138 * comes from {@link PendingIntent#getBroadcast}. Cannot be null.
Chalard Jeana5ff1132020-05-20 16:11:50 +09004139 * @throws RuntimeException if the app already has too many callbacks registered.
Paul Jensenc8873fc2015-06-17 14:15:39 -04004140 */
Jeff Sharkey656584a2017-04-24 11:18:03 -06004141 @RequiresPermission(android.Manifest.permission.ACCESS_NETWORK_STATE)
Chalard Jean158702d2019-01-07 19:26:34 +09004142 public void registerNetworkCallback(@NonNull NetworkRequest request,
4143 @NonNull PendingIntent operation) {
Soi, Yoshinarib8102ef2015-11-12 12:09:02 +09004144 printStackTrace();
Hugo Benichibc1104b2017-05-09 15:19:01 +09004145 checkPendingIntentNotNull(operation);
Paul Jensenc8873fc2015-06-17 14:15:39 -04004146 try {
Roshan Piusbc7e37d2020-01-16 12:17:17 -08004147 mService.pendingListenForNetwork(
Roshan Piusaa24fde2020-12-17 14:53:09 -08004148 request.networkCapabilities, operation, mContext.getOpPackageName(),
4149 getAttributionTag());
Jeff Sharkeyc78f85c2016-03-01 19:27:23 -07004150 } catch (RemoteException e) {
4151 throw e.rethrowFromSystemServer();
Hugo Benichia590ab82017-05-11 13:16:17 +09004152 } catch (ServiceSpecificException e) {
4153 throw convertServiceException(e);
Jeff Sharkeyc78f85c2016-03-01 19:27:23 -07004154 }
Paul Jensenc8873fc2015-06-17 14:15:39 -04004155 }
4156
4157 /**
Lorenzo Colitti76b639e2021-01-29 20:14:04 +09004158 * Registers to receive notifications about changes in the application's default network. This
4159 * may be a physical network or a virtual network, such as a VPN that applies to the
4160 * application. The callbacks will continue to be called until either the application exits or
Lorenzo Colitti15874cd2016-04-13 22:00:02 +09004161 * {@link #unregisterNetworkCallback(NetworkCallback)} is called.
Erik Kline23bf99c2016-03-16 15:31:39 +09004162 *
Chalard Jeana5ff1132020-05-20 16:11:50 +09004163 * <p>To avoid performance issues due to apps leaking callbacks, the system will limit the
4164 * number of outstanding requests to 100 per app (identified by their UID), shared with
4165 * all variants of this method, of {@link #requestNetwork} as well as
4166 * {@link ConnectivityDiagnosticsManager#registerConnectivityDiagnosticsCallback}.
4167 * Requesting a network with this method will count toward this limit. If this limit is
4168 * exceeded, an exception will be thrown. To avoid hitting this issue and to conserve resources,
4169 * make sure to unregister the callbacks with
4170 * {@link #unregisterNetworkCallback(NetworkCallback)}.
4171 *
Erik Kline23bf99c2016-03-16 15:31:39 +09004172 * @param networkCallback The {@link NetworkCallback} that the system will call as the
Lorenzo Colitti76b639e2021-01-29 20:14:04 +09004173 * application's default network changes.
Hugo Benichifd44e912017-02-02 17:02:36 +09004174 * The callback is invoked on the default internal Handler.
Chalard Jeana5ff1132020-05-20 16:11:50 +09004175 * @throws RuntimeException if the app already has too many callbacks registered.
Erik Kline23bf99c2016-03-16 15:31:39 +09004176 */
Jeff Sharkey656584a2017-04-24 11:18:03 -06004177 @RequiresPermission(android.Manifest.permission.ACCESS_NETWORK_STATE)
Chalard Jean158702d2019-01-07 19:26:34 +09004178 public void registerDefaultNetworkCallback(@NonNull NetworkCallback networkCallback) {
Hugo Benichifd44e912017-02-02 17:02:36 +09004179 registerDefaultNetworkCallback(networkCallback, getDefaultHandler());
4180 }
4181
4182 /**
Lorenzo Colitti76b639e2021-01-29 20:14:04 +09004183 * Registers to receive notifications about changes in the application's default network. This
4184 * may be a physical network or a virtual network, such as a VPN that applies to the
4185 * application. The callbacks will continue to be called until either the application exits or
4186 * {@link #unregisterNetworkCallback(NetworkCallback)} is called.
4187 *
4188 * <p>To avoid performance issues due to apps leaking callbacks, the system will limit the
4189 * number of outstanding requests to 100 per app (identified by their UID), shared with
4190 * all variants of this method, of {@link #requestNetwork} as well as
4191 * {@link ConnectivityDiagnosticsManager#registerConnectivityDiagnosticsCallback}.
4192 * Requesting a network with this method will count toward this limit. If this limit is
4193 * exceeded, an exception will be thrown. To avoid hitting this issue and to conserve resources,
4194 * make sure to unregister the callbacks with
4195 * {@link #unregisterNetworkCallback(NetworkCallback)}.
4196 *
4197 * @param networkCallback The {@link NetworkCallback} that the system will call as the
4198 * application's default network changes.
4199 * @param handler {@link Handler} to specify the thread upon which the callback will be invoked.
4200 * @throws RuntimeException if the app already has too many callbacks registered.
4201 */
4202 @RequiresPermission(android.Manifest.permission.ACCESS_NETWORK_STATE)
4203 public void registerDefaultNetworkCallback(@NonNull NetworkCallback networkCallback,
4204 @NonNull Handler handler) {
4205 CallbackHandler cbHandler = new CallbackHandler(handler);
4206 sendRequestForNetwork(null /* NetworkCapabilities need */, networkCallback, 0,
4207 TRACK_DEFAULT, TYPE_NONE, cbHandler);
4208 }
4209
4210 /**
Hugo Benichifd44e912017-02-02 17:02:36 +09004211 * Registers to receive notifications about changes in the system default network. The callbacks
4212 * will continue to be called until either the application exits or
4213 * {@link #unregisterNetworkCallback(NetworkCallback)} is called.
Hugo Benichifd44e912017-02-02 17:02:36 +09004214 *
Lorenzo Colitti76b639e2021-01-29 20:14:04 +09004215 * This method should not be used to determine networking state seen by applications, because in
4216 * many cases, most or even all application traffic may not use the default network directly,
4217 * and traffic from different applications may go on different networks by default. As an
4218 * example, if a VPN is connected, traffic from all applications might be sent through the VPN
4219 * and not onto the system default network. Applications or system components desiring to do
4220 * determine network state as seen by applications should use other methods such as
4221 * {@link #registerDefaultNetworkCallback(NetworkCallback, Handler)}.
4222 *
Chalard Jeana5ff1132020-05-20 16:11:50 +09004223 * <p>To avoid performance issues due to apps leaking callbacks, the system will limit the
4224 * number of outstanding requests to 100 per app (identified by their UID), shared with
4225 * all variants of this method, of {@link #requestNetwork} as well as
4226 * {@link ConnectivityDiagnosticsManager#registerConnectivityDiagnosticsCallback}.
4227 * Requesting a network with this method will count toward this limit. If this limit is
4228 * exceeded, an exception will be thrown. To avoid hitting this issue and to conserve resources,
4229 * make sure to unregister the callbacks with
4230 * {@link #unregisterNetworkCallback(NetworkCallback)}.
4231 *
Hugo Benichifd44e912017-02-02 17:02:36 +09004232 * @param networkCallback The {@link NetworkCallback} that the system will call as the
4233 * system default network changes.
4234 * @param handler {@link Handler} to specify the thread upon which the callback will be invoked.
Chalard Jeana5ff1132020-05-20 16:11:50 +09004235 * @throws RuntimeException if the app already has too many callbacks registered.
Lorenzo Colitti76b639e2021-01-29 20:14:04 +09004236 *
4237 * @hide
Hugo Benichifd44e912017-02-02 17:02:36 +09004238 */
Lorenzo Colitti76b639e2021-01-29 20:14:04 +09004239 @SystemApi(client = MODULE_LIBRARIES)
4240 @SuppressLint({"ExecutorRegistration", "PairedRegistration"})
4241 @RequiresPermission(anyOf = {
4242 NetworkStack.PERMISSION_MAINLINE_NETWORK_STACK,
4243 android.Manifest.permission.NETWORK_SETTINGS})
4244 public void registerSystemDefaultNetworkCallback(@NonNull NetworkCallback networkCallback,
Chalard Jean158702d2019-01-07 19:26:34 +09004245 @NonNull Handler handler) {
Hugo Benichifd44e912017-02-02 17:02:36 +09004246 CallbackHandler cbHandler = new CallbackHandler(handler);
Chalard Jean9dd11612018-06-04 16:52:49 +09004247 sendRequestForNetwork(null /* NetworkCapabilities need */, networkCallback, 0,
Lorenzo Colitti76b639e2021-01-29 20:14:04 +09004248 TRACK_SYSTEM_DEFAULT, TYPE_NONE, cbHandler);
Erik Kline23bf99c2016-03-16 15:31:39 +09004249 }
4250
4251 /**
junyulai7514e312021-03-05 15:51:17 +08004252 * @hide
4253 */
4254 // TODO: Make it public api.
4255 @SuppressLint("ExecutorRegistration")
4256 public void registerBestMatchingNetworkCallback(@NonNull NetworkRequest request,
4257 @NonNull NetworkCallback networkCallback, @NonNull Handler handler) {
4258 final NetworkCapabilities nc = request.networkCapabilities;
4259 final CallbackHandler cbHandler = new CallbackHandler(handler);
4260 sendRequestForNetwork(nc, networkCallback, 0, TRACK_BEST, TYPE_NONE, cbHandler);
4261 }
4262
4263 /**
fenglub00f4882015-04-21 17:12:05 -07004264 * Requests bandwidth update for a given {@link Network} and returns whether the update request
4265 * is accepted by ConnectivityService. Once accepted, ConnectivityService will poll underlying
4266 * network connection for updated bandwidth information. The caller will be notified via
4267 * {@link ConnectivityManager.NetworkCallback} if there is an update. Notice that this
Lorenzo Colitti15874cd2016-04-13 22:00:02 +09004268 * method assumes that the caller has previously called
4269 * {@link #registerNetworkCallback(NetworkRequest, NetworkCallback)} to listen for network
4270 * changes.
fenglu3f357402015-03-20 11:29:56 -07004271 *
fengluea2d9282015-04-27 14:28:04 -07004272 * @param network {@link Network} specifying which network you're interested.
fenglub00f4882015-04-21 17:12:05 -07004273 * @return {@code true} on success, {@code false} if the {@link Network} is no longer valid.
fenglu3f357402015-03-20 11:29:56 -07004274 */
Chalard Jean158702d2019-01-07 19:26:34 +09004275 public boolean requestBandwidthUpdate(@NonNull Network network) {
fenglu3f357402015-03-20 11:29:56 -07004276 try {
fenglub00f4882015-04-21 17:12:05 -07004277 return mService.requestBandwidthUpdate(network);
fenglu3f357402015-03-20 11:29:56 -07004278 } catch (RemoteException e) {
Jeff Sharkeyc78f85c2016-03-01 19:27:23 -07004279 throw e.rethrowFromSystemServer();
fenglu3f357402015-03-20 11:29:56 -07004280 }
4281 }
4282
4283 /**
Hugo Benichi2aa65af2017-03-06 09:17:06 +09004284 * Unregisters a {@code NetworkCallback} and possibly releases networks originating from
Lorenzo Colitti15874cd2016-04-13 22:00:02 +09004285 * {@link #requestNetwork(NetworkRequest, NetworkCallback)} and
4286 * {@link #registerNetworkCallback(NetworkRequest, NetworkCallback)} calls.
4287 * If the given {@code NetworkCallback} had previously been used with
Lorenzo Colitti7f6a2bf2015-04-24 17:03:31 +09004288 * {@code #requestNetwork}, any networks that had been connected to only to satisfy that request
4289 * will be disconnected.
Robert Greenwaltf99b8392014-03-26 16:47:06 -07004290 *
Hugo Benichi2aa65af2017-03-06 09:17:06 +09004291 * Notifications that would have triggered that {@code NetworkCallback} will immediately stop
4292 * triggering it as soon as this call returns.
4293 *
Robert Greenwaltf57c03c2014-06-11 16:05:07 -07004294 * @param networkCallback The {@link NetworkCallback} used when making the request.
Robert Greenwaltf99b8392014-03-26 16:47:06 -07004295 */
Chalard Jean158702d2019-01-07 19:26:34 +09004296 public void unregisterNetworkCallback(@NonNull NetworkCallback networkCallback) {
Soi, Yoshinarib8102ef2015-11-12 12:09:02 +09004297 printStackTrace();
Hugo Benichibc1104b2017-05-09 15:19:01 +09004298 checkCallbackNotNull(networkCallback);
Hugo Benichi2aa65af2017-03-06 09:17:06 +09004299 final List<NetworkRequest> reqs = new ArrayList<>();
4300 // Find all requests associated to this callback and stop callback triggers immediately.
4301 // Callback is reusable immediately. http://b/20701525, http://b/35921499.
4302 synchronized (sCallbacks) {
Remi NGUYEN VAN5086c292021-03-15 07:27:44 +00004303 Preconditions.checkArgument(networkCallback.networkRequest != null,
4304 "NetworkCallback was not registered");
Etan Cohen6cb65992019-04-16 15:07:55 -07004305 if (networkCallback.networkRequest == ALREADY_UNREGISTERED) {
4306 Log.d(TAG, "NetworkCallback was already unregistered");
4307 return;
4308 }
Hugo Benichi2aa65af2017-03-06 09:17:06 +09004309 for (Map.Entry<NetworkRequest, NetworkCallback> e : sCallbacks.entrySet()) {
4310 if (e.getValue() == networkCallback) {
4311 reqs.add(e.getKey());
4312 }
4313 }
4314 // TODO: throw exception if callback was registered more than once (http://b/20701525).
4315 for (NetworkRequest r : reqs) {
4316 try {
4317 mService.releaseNetworkRequest(r);
4318 } catch (RemoteException e) {
4319 throw e.rethrowFromSystemServer();
4320 }
4321 // Only remove mapping if rpc was successful.
4322 sCallbacks.remove(r);
4323 }
Hugo Benichibee30fe2017-06-17 13:14:12 +09004324 networkCallback.networkRequest = ALREADY_UNREGISTERED;
Jeff Sharkeyc78f85c2016-03-01 19:27:23 -07004325 }
Robert Greenwaltf99b8392014-03-26 16:47:06 -07004326 }
Paul Jensen8cdda642014-05-29 10:12:39 -04004327
4328 /**
Paul Jensen169f6622015-06-30 14:29:18 -04004329 * Unregisters a callback previously registered via
4330 * {@link #registerNetworkCallback(NetworkRequest, android.app.PendingIntent)}.
4331 *
4332 * @param operation A PendingIntent equal (as defined by {@link Intent#filterEquals}) to the
4333 * PendingIntent passed to
4334 * {@link #registerNetworkCallback(NetworkRequest, android.app.PendingIntent)}.
4335 * Cannot be null.
4336 */
Chalard Jean158702d2019-01-07 19:26:34 +09004337 public void unregisterNetworkCallback(@NonNull PendingIntent operation) {
Paul Jensen169f6622015-06-30 14:29:18 -04004338 releaseNetworkRequest(operation);
4339 }
4340
4341 /**
Lorenzo Colitti6947c062015-04-03 16:38:52 +09004342 * Informs the system whether it should switch to {@code network} regardless of whether it is
4343 * validated or not. If {@code accept} is true, and the network was explicitly selected by the
4344 * user (e.g., by selecting a Wi-Fi network in the Settings app), then the network will become
4345 * the system default network regardless of any other network that's currently connected. If
4346 * {@code always} is true, then the choice is remembered, so that the next time the user
4347 * connects to this network, the system will switch to it.
4348 *
Lorenzo Colitti6947c062015-04-03 16:38:52 +09004349 * @param network The network to accept.
4350 * @param accept Whether to accept the network even if unvalidated.
4351 * @param always Whether to remember this choice in the future.
4352 *
4353 * @hide
4354 */
lucaslin2240ef62019-03-12 13:08:03 +08004355 @RequiresPermission(android.Manifest.permission.NETWORK_SETTINGS)
Lorenzo Colitti6947c062015-04-03 16:38:52 +09004356 public void setAcceptUnvalidated(Network network, boolean accept, boolean always) {
4357 try {
4358 mService.setAcceptUnvalidated(network, accept, always);
Jeff Sharkeyc78f85c2016-03-01 19:27:23 -07004359 } catch (RemoteException e) {
4360 throw e.rethrowFromSystemServer();
4361 }
Lorenzo Colitti6947c062015-04-03 16:38:52 +09004362 }
4363
4364 /**
lucaslin2240ef62019-03-12 13:08:03 +08004365 * Informs the system whether it should consider the network as validated even if it only has
4366 * partial connectivity. If {@code accept} is true, then the network will be considered as
4367 * validated even if connectivity is only partial. If {@code always} is true, then the choice
4368 * is remembered, so that the next time the user connects to this network, the system will
4369 * switch to it.
4370 *
4371 * @param network The network to accept.
4372 * @param accept Whether to consider the network as validated even if it has partial
4373 * connectivity.
4374 * @param always Whether to remember this choice in the future.
4375 *
4376 * @hide
4377 */
4378 @RequiresPermission(android.Manifest.permission.NETWORK_STACK)
4379 public void setAcceptPartialConnectivity(Network network, boolean accept, boolean always) {
4380 try {
4381 mService.setAcceptPartialConnectivity(network, accept, always);
4382 } catch (RemoteException e) {
4383 throw e.rethrowFromSystemServer();
4384 }
4385 }
4386
4387 /**
Lorenzo Colittic65750c2016-09-19 01:00:19 +09004388 * Informs the system to penalize {@code network}'s score when it becomes unvalidated. This is
4389 * only meaningful if the system is configured not to penalize such networks, e.g., if the
4390 * {@code config_networkAvoidBadWifi} configuration variable is set to 0 and the {@code
4391 * NETWORK_AVOID_BAD_WIFI setting is unset}.
4392 *
Lorenzo Colittic65750c2016-09-19 01:00:19 +09004393 * @param network The network to accept.
4394 *
4395 * @hide
4396 */
lucaslin2240ef62019-03-12 13:08:03 +08004397 @RequiresPermission(android.Manifest.permission.NETWORK_SETTINGS)
Lorenzo Colittic65750c2016-09-19 01:00:19 +09004398 public void setAvoidUnvalidated(Network network) {
4399 try {
4400 mService.setAvoidUnvalidated(network);
4401 } catch (RemoteException e) {
4402 throw e.rethrowFromSystemServer();
4403 }
4404 }
4405
4406 /**
Lorenzo Colitti500dbae2017-04-27 14:30:21 +09004407 * Requests that the system open the captive portal app on the specified network.
4408 *
4409 * @param network The network to log into.
4410 *
4411 * @hide
4412 */
paulhu8e96a752019-08-12 16:25:11 +08004413 @RequiresPermission(android.Manifest.permission.NETWORK_SETTINGS)
Lorenzo Colitti500dbae2017-04-27 14:30:21 +09004414 public void startCaptivePortalApp(Network network) {
4415 try {
4416 mService.startCaptivePortalApp(network);
4417 } catch (RemoteException e) {
4418 throw e.rethrowFromSystemServer();
4419 }
4420 }
4421
4422 /**
Remi NGUYEN VAN94ff95b2019-02-04 11:32:20 +09004423 * Requests that the system open the captive portal app with the specified extras.
4424 *
4425 * <p>This endpoint is exclusively for use by the NetworkStack and is protected by the
4426 * corresponding permission.
Remi NGUYEN VANeab0f542019-02-13 20:58:59 +09004427 * @param network Network on which the captive portal was detected.
Remi NGUYEN VAN94ff95b2019-02-04 11:32:20 +09004428 * @param appExtras Extras to include in the app start intent.
4429 * @hide
4430 */
4431 @SystemApi
Remi NGUYEN VAN94ff95b2019-02-04 11:32:20 +09004432 @RequiresPermission(NetworkStack.PERMISSION_MAINLINE_NETWORK_STACK)
paulhucbbc3db2019-03-08 16:35:20 +08004433 public void startCaptivePortalApp(@NonNull Network network, @NonNull Bundle appExtras) {
Remi NGUYEN VAN94ff95b2019-02-04 11:32:20 +09004434 try {
Remi NGUYEN VANeab0f542019-02-13 20:58:59 +09004435 mService.startCaptivePortalAppInternal(network, appExtras);
Remi NGUYEN VAN94ff95b2019-02-04 11:32:20 +09004436 } catch (RemoteException e) {
4437 throw e.rethrowFromSystemServer();
4438 }
4439 }
4440
4441 /**
Remi NGUYEN VAN27de63e2019-01-20 20:35:06 +09004442 * Determine whether the device is configured to avoid bad wifi.
4443 * @hide
4444 */
4445 @SystemApi
Remi NGUYEN VAN1fb7cab2019-03-22 11:14:13 +09004446 @RequiresPermission(anyOf = {
4447 NetworkStack.PERMISSION_MAINLINE_NETWORK_STACK,
4448 android.Manifest.permission.NETWORK_STACK})
4449 public boolean shouldAvoidBadWifi() {
Remi NGUYEN VAN27de63e2019-01-20 20:35:06 +09004450 try {
Remi NGUYEN VAN1fb7cab2019-03-22 11:14:13 +09004451 return mService.shouldAvoidBadWifi();
Remi NGUYEN VAN27de63e2019-01-20 20:35:06 +09004452 } catch (RemoteException e) {
4453 throw e.rethrowFromSystemServer();
4454 }
4455 }
4456
4457 /**
Lorenzo Colitti48a2a322017-01-24 18:08:41 +09004458 * It is acceptable to briefly use multipath data to provide seamless connectivity for
4459 * time-sensitive user-facing operations when the system default network is temporarily
Lorenzo Colitti139a48c2017-04-28 00:56:30 +09004460 * unresponsive. The amount of data should be limited (less than one megabyte for every call to
4461 * this method), and the operation should be infrequent to ensure that data usage is limited.
Lorenzo Colitti48a2a322017-01-24 18:08:41 +09004462 *
4463 * An example of such an operation might be a time-sensitive foreground activity, such as a
4464 * voice command, that the user is performing while walking out of range of a Wi-Fi network.
4465 */
4466 public static final int MULTIPATH_PREFERENCE_HANDOVER = 1 << 0;
4467
4468 /**
4469 * It is acceptable to use small amounts of multipath data on an ongoing basis to provide
4470 * a backup channel for traffic that is primarily going over another network.
4471 *
4472 * An example might be maintaining backup connections to peers or servers for the purpose of
4473 * fast fallback if the default network is temporarily unresponsive or disconnects. The traffic
4474 * on backup paths should be negligible compared to the traffic on the main path.
4475 */
4476 public static final int MULTIPATH_PREFERENCE_RELIABILITY = 1 << 1;
4477
4478 /**
4479 * It is acceptable to use metered data to improve network latency and performance.
4480 */
4481 public static final int MULTIPATH_PREFERENCE_PERFORMANCE = 1 << 2;
4482
4483 /**
4484 * Return value to use for unmetered networks. On such networks we currently set all the flags
4485 * to true.
4486 * @hide
4487 */
4488 public static final int MULTIPATH_PREFERENCE_UNMETERED =
4489 MULTIPATH_PREFERENCE_HANDOVER |
4490 MULTIPATH_PREFERENCE_RELIABILITY |
4491 MULTIPATH_PREFERENCE_PERFORMANCE;
4492
4493 /** @hide */
4494 @Retention(RetentionPolicy.SOURCE)
4495 @IntDef(flag = true, value = {
4496 MULTIPATH_PREFERENCE_HANDOVER,
4497 MULTIPATH_PREFERENCE_RELIABILITY,
4498 MULTIPATH_PREFERENCE_PERFORMANCE,
4499 })
4500 public @interface MultipathPreference {
4501 }
4502
4503 /**
4504 * Provides a hint to the calling application on whether it is desirable to use the
4505 * multinetwork APIs (e.g., {@link Network#openConnection}, {@link Network#bindSocket}, etc.)
4506 * for multipath data transfer on this network when it is not the system default network.
4507 * Applications desiring to use multipath network protocols should call this method before
4508 * each such operation.
Lorenzo Colitti48a2a322017-01-24 18:08:41 +09004509 *
4510 * @param network The network on which the application desires to use multipath data.
4511 * If {@code null}, this method will return the a preference that will generally
4512 * apply to metered networks.
4513 * @return a bitwise OR of zero or more of the {@code MULTIPATH_PREFERENCE_*} constants.
4514 */
Jeff Sharkey656584a2017-04-24 11:18:03 -06004515 @RequiresPermission(android.Manifest.permission.ACCESS_NETWORK_STATE)
Chalard Jean158702d2019-01-07 19:26:34 +09004516 public @MultipathPreference int getMultipathPreference(@Nullable Network network) {
Lorenzo Colitti48a2a322017-01-24 18:08:41 +09004517 try {
4518 return mService.getMultipathPreference(network);
4519 } catch (RemoteException e) {
4520 throw e.rethrowFromSystemServer();
4521 }
4522 }
4523
4524 /**
Stuart Scott0f835ac2015-03-30 13:17:11 -07004525 * Resets all connectivity manager settings back to factory defaults.
4526 * @hide
4527 */
paulhu8e96a752019-08-12 16:25:11 +08004528 @RequiresPermission(android.Manifest.permission.NETWORK_SETTINGS)
Stuart Scott0f835ac2015-03-30 13:17:11 -07004529 public void factoryReset() {
Stuart Scott0f835ac2015-03-30 13:17:11 -07004530 try {
Stuart Scottd5463642015-04-02 18:00:02 -07004531 mService.factoryReset();
Amos Bianchia9b415a2020-03-04 11:07:38 -08004532 mTetheringManager.stopAllTethering();
Stuart Scott0f835ac2015-03-30 13:17:11 -07004533 } catch (RemoteException e) {
Jeff Sharkeyc78f85c2016-03-01 19:27:23 -07004534 throw e.rethrowFromSystemServer();
Stuart Scott0f835ac2015-03-30 13:17:11 -07004535 }
4536 }
4537
4538 /**
Paul Jensen8cdda642014-05-29 10:12:39 -04004539 * Binds the current process to {@code network}. All Sockets created in the future
4540 * (and not explicitly bound via a bound SocketFactory from
4541 * {@link Network#getSocketFactory() Network.getSocketFactory()}) will be bound to
4542 * {@code network}. All host name resolutions will be limited to {@code network} as well.
4543 * Note that if {@code network} ever disconnects, all Sockets created in this way will cease to
4544 * work and all host name resolutions will fail. This is by design so an application doesn't
4545 * accidentally use Sockets it thinks are still bound to a particular {@link Network}.
4546 * To clear binding pass {@code null} for {@code network}. Using individually bound
4547 * Sockets created by Network.getSocketFactory().createSocket() and
4548 * performing network-specific host name resolutions via
4549 * {@link Network#getAllByName Network.getAllByName} is preferred to calling
Paul Jensenee2f45d2015-03-10 10:54:12 -04004550 * {@code bindProcessToNetwork}.
Paul Jensen8cdda642014-05-29 10:12:39 -04004551 *
4552 * @param network The {@link Network} to bind the current process to, or {@code null} to clear
4553 * the current binding.
4554 * @return {@code true} on success, {@code false} if the {@link Network} is no longer valid.
4555 */
Chalard Jean158702d2019-01-07 19:26:34 +09004556 public boolean bindProcessToNetwork(@Nullable Network network) {
Chalard Jean9dd11612018-06-04 16:52:49 +09004557 // Forcing callers to call through non-static function ensures ConnectivityManager
Paul Jensenee2f45d2015-03-10 10:54:12 -04004558 // instantiated.
4559 return setProcessDefaultNetwork(network);
4560 }
4561
4562 /**
4563 * Binds the current process to {@code network}. All Sockets created in the future
4564 * (and not explicitly bound via a bound SocketFactory from
4565 * {@link Network#getSocketFactory() Network.getSocketFactory()}) will be bound to
4566 * {@code network}. All host name resolutions will be limited to {@code network} as well.
4567 * Note that if {@code network} ever disconnects, all Sockets created in this way will cease to
4568 * work and all host name resolutions will fail. This is by design so an application doesn't
4569 * accidentally use Sockets it thinks are still bound to a particular {@link Network}.
4570 * To clear binding pass {@code null} for {@code network}. Using individually bound
4571 * Sockets created by Network.getSocketFactory().createSocket() and
4572 * performing network-specific host name resolutions via
4573 * {@link Network#getAllByName Network.getAllByName} is preferred to calling
4574 * {@code setProcessDefaultNetwork}.
4575 *
4576 * @param network The {@link Network} to bind the current process to, or {@code null} to clear
4577 * the current binding.
4578 * @return {@code true} on success, {@code false} if the {@link Network} is no longer valid.
4579 * @deprecated This function can throw {@link IllegalStateException}. Use
4580 * {@link #bindProcessToNetwork} instead. {@code bindProcessToNetwork}
4581 * is a direct replacement.
4582 */
Aurimas Liutikase80a0eb2016-05-24 15:22:55 -07004583 @Deprecated
Chalard Jean158702d2019-01-07 19:26:34 +09004584 public static boolean setProcessDefaultNetwork(@Nullable Network network) {
Paul Jensen3e2917c2014-08-27 12:38:45 -04004585 int netId = (network == null) ? NETID_UNSET : network.netId;
Lorenzo Colitti3c766eb2019-01-31 13:08:24 +09004586 boolean isSameNetId = (netId == NetworkUtils.getBoundNetworkForProcess());
4587
Lorenzo Colitti3a1cb9d2019-01-30 23:04:54 +09004588 if (netId != NETID_UNSET) {
4589 netId = network.getNetIdForResolv();
Paul Jensen3e2917c2014-08-27 12:38:45 -04004590 }
Lorenzo Colitti3c766eb2019-01-31 13:08:24 +09004591
4592 if (!NetworkUtils.bindProcessToNetwork(netId)) {
4593 return false;
4594 }
4595
4596 if (!isSameNetId) {
Paul Jensenc0618a62014-12-10 15:12:18 -05004597 // Set HTTP proxy system properties to match network.
4598 // TODO: Deprecate this static method and replace it with a non-static version.
Lorenzo Colitti41b1fbc2015-04-22 11:52:48 +09004599 try {
Remi NGUYEN VANb33335c2021-02-03 10:18:20 +09004600 Proxy.setHttpProxyConfiguration(getInstance().getDefaultProxy());
Lorenzo Colitti41b1fbc2015-04-22 11:52:48 +09004601 } catch (SecurityException e) {
4602 // The process doesn't have ACCESS_NETWORK_STATE, so we can't fetch the proxy.
4603 Log.e(TAG, "Can't set proxy properties", e);
4604 }
Paul Jensen3e2917c2014-08-27 12:38:45 -04004605 // Must flush DNS cache as new network may have different DNS resolutions.
4606 InetAddress.clearDnsCache();
4607 // Must flush socket pool as idle sockets will be bound to previous network and may
4608 // cause subsequent fetches to be performed on old network.
4609 NetworkEventDispatcher.getInstance().onNetworkConfigurationChanged();
Paul Jensen3e2917c2014-08-27 12:38:45 -04004610 }
Lorenzo Colitti3c766eb2019-01-31 13:08:24 +09004611
4612 return true;
Paul Jensen8cdda642014-05-29 10:12:39 -04004613 }
4614
4615 /**
4616 * Returns the {@link Network} currently bound to this process via
Paul Jensenee2f45d2015-03-10 10:54:12 -04004617 * {@link #bindProcessToNetwork}, or {@code null} if no {@link Network} is explicitly bound.
Paul Jensen8cdda642014-05-29 10:12:39 -04004618 *
4619 * @return {@code Network} to which this process is bound, or {@code null}.
4620 */
Chalard Jean158702d2019-01-07 19:26:34 +09004621 @Nullable
Paul Jensenee2f45d2015-03-10 10:54:12 -04004622 public Network getBoundNetworkForProcess() {
4623 // Forcing callers to call thru non-static function ensures ConnectivityManager
4624 // instantiated.
4625 return getProcessDefaultNetwork();
4626 }
4627
4628 /**
4629 * Returns the {@link Network} currently bound to this process via
4630 * {@link #bindProcessToNetwork}, or {@code null} if no {@link Network} is explicitly bound.
4631 *
4632 * @return {@code Network} to which this process is bound, or {@code null}.
4633 * @deprecated Using this function can lead to other functions throwing
4634 * {@link IllegalStateException}. Use {@link #getBoundNetworkForProcess} instead.
4635 * {@code getBoundNetworkForProcess} is a direct replacement.
4636 */
Aurimas Liutikase80a0eb2016-05-24 15:22:55 -07004637 @Deprecated
Chalard Jean158702d2019-01-07 19:26:34 +09004638 @Nullable
Paul Jensen8cdda642014-05-29 10:12:39 -04004639 public static Network getProcessDefaultNetwork() {
Paul Jensenee2f45d2015-03-10 10:54:12 -04004640 int netId = NetworkUtils.getBoundNetworkForProcess();
Paul Jensen65743a22014-07-11 08:17:29 -04004641 if (netId == NETID_UNSET) return null;
Paul Jensen8cdda642014-05-29 10:12:39 -04004642 return new Network(netId);
4643 }
4644
Lorenzo Colitti2780e4b2015-07-29 11:41:21 +09004645 private void unsupportedStartingFrom(int version) {
4646 if (Process.myUid() == Process.SYSTEM_UID) {
Lorenzo Colitti23862912018-09-28 11:31:55 +09004647 // The getApplicationInfo() call we make below is not supported in system context. Let
4648 // the call through here, and rely on the fact that ConnectivityService will refuse to
4649 // allow the system to use these APIs anyway.
Lorenzo Colitti2780e4b2015-07-29 11:41:21 +09004650 return;
4651 }
4652
4653 if (mContext.getApplicationInfo().targetSdkVersion >= version) {
4654 throw new UnsupportedOperationException(
4655 "This method is not supported in target SDK version " + version + " and above");
4656 }
4657 }
4658
4659 // Checks whether the calling app can use the legacy routing API (startUsingNetworkFeature,
4660 // stopUsingNetworkFeature, requestRouteToHost), and if not throw UnsupportedOperationException.
Lifu Tang0a922fd2016-01-07 23:20:38 -08004661 // TODO: convert the existing system users (Tethering, GnssLocationProvider) to the new APIs and
Lorenzo Colitti2780e4b2015-07-29 11:41:21 +09004662 // remove these exemptions. Note that this check is not secure, and apps can still access these
4663 // functions by accessing ConnectivityService directly. However, it should be clear that doing
4664 // so is unsupported and may break in the future. http://b/22728205
4665 private void checkLegacyRoutingApiAccess() {
Dianne Hackborn18c1d832015-07-31 10:35:34 -07004666 unsupportedStartingFrom(VERSION_CODES.M);
Lorenzo Colitti2780e4b2015-07-29 11:41:21 +09004667 }
4668
Paul Jensen8cdda642014-05-29 10:12:39 -04004669 /**
4670 * Binds host resolutions performed by this process to {@code network}.
Paul Jensenee2f45d2015-03-10 10:54:12 -04004671 * {@link #bindProcessToNetwork} takes precedence over this setting.
Paul Jensen8cdda642014-05-29 10:12:39 -04004672 *
4673 * @param network The {@link Network} to bind host resolutions from the current process to, or
4674 * {@code null} to clear the current binding.
4675 * @return {@code true} on success, {@code false} if the {@link Network} is no longer valid.
4676 * @hide
4677 * @deprecated This is strictly for legacy usage to support {@link #startUsingNetworkFeature}.
4678 */
Aurimas Liutikase80a0eb2016-05-24 15:22:55 -07004679 @Deprecated
Mathew Inwoode1a17ba2020-11-04 09:29:36 +00004680 @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.R, trackingBug = 170729553)
Paul Jensen8cdda642014-05-29 10:12:39 -04004681 public static boolean setProcessDefaultNetworkForHostResolution(Network network) {
Paul Jensen65743a22014-07-11 08:17:29 -04004682 return NetworkUtils.bindProcessToNetworkForHostResolution(
Erik Kline767b7f22018-04-27 22:48:33 +09004683 (network == null) ? NETID_UNSET : network.getNetIdForResolv());
Paul Jensen8cdda642014-05-29 10:12:39 -04004684 }
Felipe Leme30511352016-01-22 09:44:57 -08004685
4686 /**
4687 * Device is not restricting metered network activity while application is running on
4688 * background.
4689 */
4690 public static final int RESTRICT_BACKGROUND_STATUS_DISABLED = 1;
4691
4692 /**
4693 * Device is restricting metered network activity while application is running on background,
4694 * but application is allowed to bypass it.
4695 * <p>
4696 * In this state, application should take action to mitigate metered network access.
4697 * For example, a music streaming application should switch to a low-bandwidth bitrate.
4698 */
4699 public static final int RESTRICT_BACKGROUND_STATUS_WHITELISTED = 2;
4700
4701 /**
4702 * Device is restricting metered network activity while application is running on background.
Felipe Lemed34c9af2016-01-27 14:46:39 -08004703 * <p>
Felipe Leme30511352016-01-22 09:44:57 -08004704 * In this state, application should not try to use the network while running on background,
4705 * because it would be denied.
4706 */
4707 public static final int RESTRICT_BACKGROUND_STATUS_ENABLED = 3;
4708
Felipe Lemed34c9af2016-01-27 14:46:39 -08004709 /**
4710 * A change in the background metered network activity restriction has occurred.
4711 * <p>
4712 * Applications should call {@link #getRestrictBackgroundStatus()} to check if the restriction
4713 * applies to them.
4714 * <p>
4715 * This is only sent to registered receivers, not manifest receivers.
4716 */
4717 @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION)
4718 public static final String ACTION_RESTRICT_BACKGROUND_CHANGED =
4719 "android.net.conn.RESTRICT_BACKGROUND_CHANGED";
4720
Felipe Leme7e4c1852016-01-25 11:48:04 -08004721 /** @hide */
4722 @Retention(RetentionPolicy.SOURCE)
Felipe Leme30511352016-01-22 09:44:57 -08004723 @IntDef(flag = false, value = {
4724 RESTRICT_BACKGROUND_STATUS_DISABLED,
4725 RESTRICT_BACKGROUND_STATUS_WHITELISTED,
4726 RESTRICT_BACKGROUND_STATUS_ENABLED,
4727 })
Felipe Leme30511352016-01-22 09:44:57 -08004728 public @interface RestrictBackgroundStatus {
4729 }
4730
4731 private INetworkPolicyManager getNetworkPolicyManager() {
4732 synchronized (this) {
4733 if (mNPManager != null) {
4734 return mNPManager;
4735 }
4736 mNPManager = INetworkPolicyManager.Stub.asInterface(ServiceManager
4737 .getService(Context.NETWORK_POLICY_SERVICE));
4738 return mNPManager;
4739 }
4740 }
4741
4742 /**
4743 * Determines if the calling application is subject to metered network restrictions while
4744 * running on background.
Felipe Leme3edc6162016-05-16 13:57:19 -07004745 *
4746 * @return {@link #RESTRICT_BACKGROUND_STATUS_DISABLED},
4747 * {@link #RESTRICT_BACKGROUND_STATUS_ENABLED},
4748 * or {@link #RESTRICT_BACKGROUND_STATUS_WHITELISTED}
Felipe Leme30511352016-01-22 09:44:57 -08004749 */
4750 public @RestrictBackgroundStatus int getRestrictBackgroundStatus() {
4751 try {
4752 return getNetworkPolicyManager().getRestrictBackgroundByCaller();
4753 } catch (RemoteException e) {
Jeff Sharkeyc78f85c2016-03-01 19:27:23 -07004754 throw e.rethrowFromSystemServer();
Felipe Leme30511352016-01-22 09:44:57 -08004755 }
4756 }
Ricky Wai7097cc92018-01-23 04:09:45 +00004757
4758 /**
4759 * The network watchlist is a list of domains and IP addresses that are associated with
Ricky Wai04baf112018-03-20 14:20:54 +00004760 * potentially harmful apps. This method returns the SHA-256 of the watchlist config file
4761 * currently used by the system for validation purposes.
Ricky Wai7097cc92018-01-23 04:09:45 +00004762 *
4763 * @return Hash of network watchlist config file. Null if config does not exist.
4764 */
Chalard Jean158702d2019-01-07 19:26:34 +09004765 @Nullable
Ricky Wai7097cc92018-01-23 04:09:45 +00004766 public byte[] getNetworkWatchlistConfigHash() {
4767 try {
4768 return mService.getNetworkWatchlistConfigHash();
4769 } catch (RemoteException e) {
4770 Log.e(TAG, "Unable to get watchlist config hash");
4771 throw e.rethrowFromSystemServer();
4772 }
4773 }
Jeff Vander Stoep39a51e02018-07-23 10:57:53 -07004774
4775 /**
4776 * Returns the {@code uid} of the owner of a network connection.
4777 *
Benedict Wong0bd4bba2020-01-20 22:14:59 -08004778 * @param protocol The protocol of the connection. Only {@code IPPROTO_TCP} and {@code
4779 * IPPROTO_UDP} currently supported.
Jeff Vander Stoep39a51e02018-07-23 10:57:53 -07004780 * @param local The local {@link InetSocketAddress} of a connection.
4781 * @param remote The remote {@link InetSocketAddress} of a connection.
Jeff Vander Stoep39a51e02018-07-23 10:57:53 -07004782 * @return {@code uid} if the connection is found and the app has permission to observe it
Benedict Wong0bd4bba2020-01-20 22:14:59 -08004783 * (e.g., if it is associated with the calling VPN app's VpnService tunnel) or {@link
4784 * android.os.Process#INVALID_UID} if the connection is not found.
4785 * @throws {@link SecurityException} if the caller is not the active VpnService for the current
4786 * user.
4787 * @throws {@link IllegalArgumentException} if an unsupported protocol is requested.
Jeff Vander Stoep39a51e02018-07-23 10:57:53 -07004788 */
Benedict Wong0bd4bba2020-01-20 22:14:59 -08004789 public int getConnectionOwnerUid(
4790 int protocol, @NonNull InetSocketAddress local, @NonNull InetSocketAddress remote) {
Jeff Vander Stoep39a51e02018-07-23 10:57:53 -07004791 ConnectionInfo connectionInfo = new ConnectionInfo(protocol, local, remote);
4792 try {
4793 return mService.getConnectionOwnerUid(connectionInfo);
4794 } catch (RemoteException e) {
4795 throw e.rethrowFromSystemServer();
4796 }
4797 }
Soi, Yoshinarib8102ef2015-11-12 12:09:02 +09004798
4799 private void printStackTrace() {
4800 if (DEBUG) {
4801 final StackTraceElement[] callStack = Thread.currentThread().getStackTrace();
4802 final StringBuffer sb = new StringBuffer();
4803 for (int i = 3; i < callStack.length; i++) {
4804 final String stackTrace = callStack[i].toString();
4805 if (stackTrace == null || stackTrace.contains("android.os")) {
4806 break;
4807 }
4808 sb.append(" [").append(stackTrace).append("]");
4809 }
4810 Log.d(TAG, "StackLog:" + sb.toString());
4811 }
4812 }
Cody Kestingf53a0752020-04-15 12:33:28 -07004813
Remi NGUYEN VAN5f406422021-01-15 23:02:47 +09004814 /** @hide */
4815 public TestNetworkManager startOrGetTestNetworkManager() {
4816 final IBinder tnBinder;
4817 try {
4818 tnBinder = mService.startOrGetTestNetworkService();
4819 } catch (RemoteException e) {
4820 throw e.rethrowFromSystemServer();
4821 }
4822
4823 return new TestNetworkManager(ITestNetworkManager.Stub.asInterface(tnBinder));
4824 }
4825
Remi NGUYEN VAN5f406422021-01-15 23:02:47 +09004826 /** @hide */
4827 public ConnectivityDiagnosticsManager createDiagnosticsManager() {
4828 return new ConnectivityDiagnosticsManager(mContext, mService);
4829 }
4830
Cody Kestingf53a0752020-04-15 12:33:28 -07004831 /**
4832 * Simulates a Data Stall for the specified Network.
4833 *
Remi NGUYEN VAN761c7ad2021-01-12 18:40:04 +09004834 * <p>This method should only be used for tests.
4835 *
Cody Kestingf53a0752020-04-15 12:33:28 -07004836 * <p>The caller must be the owner of the specified Network.
4837 *
4838 * @param detectionMethod The detection method used to identify the Data Stall.
4839 * @param timestampMillis The timestamp at which the stall 'occurred', in milliseconds.
4840 * @param network The Network for which a Data Stall is being simluated.
4841 * @param extras The PersistableBundle of extras included in the Data Stall notification.
4842 * @throws SecurityException if the caller is not the owner of the given network.
4843 * @hide
4844 */
Remi NGUYEN VAN761c7ad2021-01-12 18:40:04 +09004845 @SystemApi(client = SystemApi.Client.MODULE_LIBRARIES)
Cody Kestingf53a0752020-04-15 12:33:28 -07004846 @RequiresPermission(anyOf = {android.Manifest.permission.MANAGE_TEST_NETWORKS,
4847 android.Manifest.permission.NETWORK_STACK})
4848 public void simulateDataStall(int detectionMethod, long timestampMillis,
4849 @NonNull Network network, @NonNull PersistableBundle extras) {
4850 try {
4851 mService.simulateDataStall(detectionMethod, timestampMillis, network, extras);
4852 } catch (RemoteException e) {
4853 e.rethrowFromSystemServer();
4854 }
4855 }
James Mattis356a8792020-10-28 21:48:54 -07004856
Daniel Brightf9e945b2020-06-15 16:10:01 -07004857 @NonNull
4858 private final List<QosCallbackConnection> mQosCallbackConnections = new ArrayList<>();
4859
4860 /**
4861 * Registers a {@link QosSocketInfo} with an associated {@link QosCallback}. The callback will
4862 * receive available QoS events related to the {@link Network} and local ip + port
4863 * specified within socketInfo.
4864 * <p/>
4865 * The same {@link QosCallback} must be unregistered before being registered a second time,
4866 * otherwise {@link QosCallbackRegistrationException} is thrown.
4867 * <p/>
4868 * This API does not, in itself, require any permission if called with a network that is not
4869 * restricted. However, the underlying implementation currently only supports the IMS network,
4870 * which is always restricted. That means non-preinstalled callers can't possibly find this API
4871 * useful, because they'd never be called back on networks that they would have access to.
4872 *
4873 * @throws SecurityException if {@link QosSocketInfo#getNetwork()} is restricted and the app is
4874 * missing CONNECTIVITY_USE_RESTRICTED_NETWORKS permission.
4875 * @throws QosCallback.QosCallbackRegistrationException if qosCallback is already registered.
4876 * @throws RuntimeException if the app already has too many callbacks registered.
4877 *
4878 * Exceptions after the time of registration is passed through
4879 * {@link QosCallback#onError(QosCallbackException)}. see: {@link QosCallbackException}.
4880 *
4881 * @param socketInfo the socket information used to match QoS events
4882 * @param callback receives qos events that satisfy socketInfo
4883 * @param executor The executor on which the callback will be invoked. The provided
4884 * {@link Executor} must run callback sequentially, otherwise the order of
4885 * callbacks cannot be guaranteed.
4886 *
4887 * @hide
4888 */
4889 @SystemApi
4890 public void registerQosCallback(@NonNull final QosSocketInfo socketInfo,
4891 @NonNull final QosCallback callback,
4892 @CallbackExecutor @NonNull final Executor executor) {
4893 Objects.requireNonNull(socketInfo, "socketInfo must be non-null");
4894 Objects.requireNonNull(callback, "callback must be non-null");
4895 Objects.requireNonNull(executor, "executor must be non-null");
4896
4897 try {
4898 synchronized (mQosCallbackConnections) {
4899 if (getQosCallbackConnection(callback) == null) {
4900 final QosCallbackConnection connection =
4901 new QosCallbackConnection(this, callback, executor);
4902 mQosCallbackConnections.add(connection);
4903 mService.registerQosSocketCallback(socketInfo, connection);
4904 } else {
4905 Log.e(TAG, "registerQosCallback: Callback already registered");
4906 throw new QosCallbackRegistrationException();
4907 }
4908 }
4909 } catch (final RemoteException e) {
4910 Log.e(TAG, "registerQosCallback: Error while registering ", e);
4911
4912 // The same unregister method method is called for consistency even though nothing
4913 // will be sent to the ConnectivityService since the callback was never successfully
4914 // registered.
4915 unregisterQosCallback(callback);
4916 e.rethrowFromSystemServer();
4917 } catch (final ServiceSpecificException e) {
4918 Log.e(TAG, "registerQosCallback: Error while registering ", e);
4919 unregisterQosCallback(callback);
4920 throw convertServiceException(e);
4921 }
4922 }
4923
4924 /**
4925 * Unregisters the given {@link QosCallback}. The {@link QosCallback} will no longer receive
4926 * events once unregistered and can be registered a second time.
4927 * <p/>
4928 * If the {@link QosCallback} does not have an active registration, it is a no-op.
4929 *
4930 * @param callback the callback being unregistered
4931 *
4932 * @hide
4933 */
4934 @SystemApi
4935 public void unregisterQosCallback(@NonNull final QosCallback callback) {
4936 Objects.requireNonNull(callback, "The callback must be non-null");
4937 try {
4938 synchronized (mQosCallbackConnections) {
4939 final QosCallbackConnection connection = getQosCallbackConnection(callback);
4940 if (connection != null) {
4941 connection.stopReceivingMessages();
4942 mService.unregisterQosCallback(connection);
4943 mQosCallbackConnections.remove(connection);
4944 } else {
4945 Log.d(TAG, "unregisterQosCallback: Callback not registered");
4946 }
4947 }
4948 } catch (final RemoteException e) {
4949 Log.e(TAG, "unregisterQosCallback: Error while unregistering ", e);
4950 e.rethrowFromSystemServer();
4951 }
4952 }
4953
4954 /**
4955 * Gets the connection related to the callback.
4956 *
4957 * @param callback the callback to look up
4958 * @return the related connection
4959 */
4960 @Nullable
4961 private QosCallbackConnection getQosCallbackConnection(final QosCallback callback) {
4962 for (final QosCallbackConnection connection : mQosCallbackConnections) {
4963 // Checking by reference here is intentional
4964 if (connection.getCallback() == callback) {
4965 return connection;
4966 }
4967 }
4968 return null;
4969 }
Junyu Laia62493f2021-01-19 11:10:56 +00004970
4971 /**
Roshan Pius951c0032020-12-22 15:10:42 -08004972 * Request a network to satisfy a set of {@link NetworkCapabilities}, but
Junyu Laia62493f2021-01-19 11:10:56 +00004973 * does not cause any networks to retain the NET_CAPABILITY_FOREGROUND capability. This can
4974 * be used to request that the system provide a network without causing the network to be
4975 * in the foreground.
4976 *
4977 * <p>This method will attempt to find the best network that matches the passed
4978 * {@link NetworkRequest}, and to bring up one that does if none currently satisfies the
4979 * criteria. The platform will evaluate which network is the best at its own discretion.
4980 * Throughput, latency, cost per byte, policy, user preference and other considerations
4981 * may be factored in the decision of what is considered the best network.
4982 *
4983 * <p>As long as this request is outstanding, the platform will try to maintain the best network
4984 * matching this request, while always attempting to match the request to a better network if
4985 * possible. If a better match is found, the platform will switch this request to the now-best
4986 * network and inform the app of the newly best network by invoking
4987 * {@link NetworkCallback#onAvailable(Network)} on the provided callback. Note that the platform
4988 * will not try to maintain any other network than the best one currently matching the request:
4989 * a network not matching any network request may be disconnected at any time.
4990 *
4991 * <p>For example, an application could use this method to obtain a connected cellular network
4992 * even if the device currently has a data connection over Ethernet. This may cause the cellular
4993 * radio to consume additional power. Or, an application could inform the system that it wants
4994 * a network supporting sending MMSes and have the system let it know about the currently best
4995 * MMS-supporting network through the provided {@link NetworkCallback}.
4996 *
4997 * <p>The status of the request can be followed by listening to the various callbacks described
4998 * in {@link NetworkCallback}. The {@link Network} object passed to the callback methods can be
4999 * used to direct traffic to the network (although accessing some networks may be subject to
5000 * holding specific permissions). Callers will learn about the specific characteristics of the
5001 * network through
5002 * {@link NetworkCallback#onCapabilitiesChanged(Network, NetworkCapabilities)} and
5003 * {@link NetworkCallback#onLinkPropertiesChanged(Network, LinkProperties)}. The methods of the
5004 * provided {@link NetworkCallback} will only be invoked due to changes in the best network
5005 * matching the request at any given time; therefore when a better network matching the request
5006 * becomes available, the {@link NetworkCallback#onAvailable(Network)} method is called
5007 * with the new network after which no further updates are given about the previously-best
5008 * network, unless it becomes the best again at some later time. All callbacks are invoked
5009 * in order on the same thread, which by default is a thread created by the framework running
5010 * in the app.
5011 *
5012 * <p>This{@link NetworkRequest} will live until released via
5013 * {@link #unregisterNetworkCallback(NetworkCallback)} or the calling application exits, at
5014 * which point the system may let go of the network at any time.
5015 *
5016 * <p>It is presently unsupported to request a network with mutable
5017 * {@link NetworkCapabilities} such as
5018 * {@link NetworkCapabilities#NET_CAPABILITY_VALIDATED} or
5019 * {@link NetworkCapabilities#NET_CAPABILITY_CAPTIVE_PORTAL}
5020 * as these {@code NetworkCapabilities} represent states that a particular
5021 * network may never attain, and whether a network will attain these states
5022 * is unknown prior to bringing up the network so the framework does not
5023 * know how to go about satisfying a request with these capabilities.
5024 *
5025 * <p>To avoid performance issues due to apps leaking callbacks, the system will limit the
5026 * number of outstanding requests to 100 per app (identified by their UID), shared with
5027 * all variants of this method, of {@link #registerNetworkCallback} as well as
5028 * {@link ConnectivityDiagnosticsManager#registerConnectivityDiagnosticsCallback}.
5029 * Requesting a network with this method will count toward this limit. If this limit is
5030 * exceeded, an exception will be thrown. To avoid hitting this issue and to conserve resources,
5031 * make sure to unregister the callbacks with
5032 * {@link #unregisterNetworkCallback(NetworkCallback)}.
5033 *
5034 * @param request {@link NetworkRequest} describing this request.
5035 * @param handler {@link Handler} to specify the thread upon which the callback will be invoked.
5036 * If null, the callback is invoked on the default internal Handler.
5037 * @param networkCallback The {@link NetworkCallback} to be utilized for this request. Note
5038 * the callback must not be shared - it uniquely specifies this request.
5039 * @throws IllegalArgumentException if {@code request} contains invalid network capabilities.
5040 * @throws SecurityException if missing the appropriate permissions.
5041 * @throws RuntimeException if the app already has too many callbacks registered.
5042 *
5043 * @hide
5044 */
5045 @SystemApi(client = MODULE_LIBRARIES)
5046 @SuppressLint("ExecutorRegistration")
5047 @RequiresPermission(anyOf = {
5048 android.Manifest.permission.NETWORK_SETTINGS,
5049 android.Manifest.permission.NETWORK_STACK,
5050 NetworkStack.PERMISSION_MAINLINE_NETWORK_STACK
5051 })
5052 public void requestBackgroundNetwork(@NonNull NetworkRequest request,
junyulai962bdb82021-03-09 20:49:48 +08005053 @NonNull Handler handler, @NonNull NetworkCallback networkCallback) {
Junyu Laia62493f2021-01-19 11:10:56 +00005054 final NetworkCapabilities nc = request.networkCapabilities;
5055 sendRequestForNetwork(nc, networkCallback, 0, BACKGROUND_REQUEST,
junyulai962bdb82021-03-09 20:49:48 +08005056 TYPE_NONE, new CallbackHandler(handler));
Junyu Laia62493f2021-01-19 11:10:56 +00005057 }
James Mattis45d81842021-01-10 14:24:24 -08005058
5059 /**
5060 * Listener for {@link #setOemNetworkPreference(OemNetworkPreferences, Executor,
5061 * OnSetOemNetworkPreferenceListener)}.
James Mattisda32cfe2021-01-26 16:23:52 -08005062 * @hide
James Mattis45d81842021-01-10 14:24:24 -08005063 */
James Mattisda32cfe2021-01-26 16:23:52 -08005064 @SystemApi
5065 public interface OnSetOemNetworkPreferenceListener {
James Mattis45d81842021-01-10 14:24:24 -08005066 /**
5067 * Called when setOemNetworkPreference() successfully completes.
5068 */
5069 void onComplete();
5070 }
5071
5072 /**
5073 * Used by automotive devices to set the network preferences used to direct traffic at an
5074 * application level as per the given OemNetworkPreferences. An example use-case would be an
5075 * automotive OEM wanting to provide connectivity for applications critical to the usage of a
5076 * vehicle via a particular network.
5077 *
5078 * Calling this will overwrite the existing preference.
5079 *
5080 * @param preference {@link OemNetworkPreferences} The application network preference to be set.
5081 * @param executor the executor on which listener will be invoked.
5082 * @param listener {@link OnSetOemNetworkPreferenceListener} optional listener used to
5083 * communicate completion of setOemNetworkPreference(). This will only be
5084 * called once upon successful completion of setOemNetworkPreference().
5085 * @throws IllegalArgumentException if {@code preference} contains invalid preference values.
5086 * @throws SecurityException if missing the appropriate permissions.
5087 * @throws UnsupportedOperationException if called on a non-automotive device.
James Mattisda32cfe2021-01-26 16:23:52 -08005088 * @hide
James Mattis45d81842021-01-10 14:24:24 -08005089 */
James Mattisda32cfe2021-01-26 16:23:52 -08005090 @SystemApi
James Mattis8378aec2021-01-26 14:05:36 -08005091 @RequiresPermission(android.Manifest.permission.CONTROL_OEM_PAID_NETWORK_PREFERENCE)
James Mattisda32cfe2021-01-26 16:23:52 -08005092 public void setOemNetworkPreference(@NonNull final OemNetworkPreferences preference,
James Mattis45d81842021-01-10 14:24:24 -08005093 @Nullable @CallbackExecutor final Executor executor,
5094 @Nullable final OnSetOemNetworkPreferenceListener listener) {
5095 Objects.requireNonNull(preference, "OemNetworkPreferences must be non-null");
5096 if (null != listener) {
5097 Objects.requireNonNull(executor, "Executor must be non-null");
5098 }
5099 final IOnSetOemNetworkPreferenceListener listenerInternal = listener == null ? null :
5100 new IOnSetOemNetworkPreferenceListener.Stub() {
5101 @Override
5102 public void onComplete() {
5103 executor.execute(listener::onComplete);
5104 }
5105 };
5106
5107 try {
5108 mService.setOemNetworkPreference(preference, listenerInternal);
5109 } catch (RemoteException e) {
5110 Log.e(TAG, "setOemNetworkPreference() failed for preference: " + preference.toString());
5111 throw e.rethrowFromSystemServer();
5112 }
5113 }
lucaslin30e70a82021-03-12 00:46:33 +08005114
5115 // The first network ID of IPSec tunnel interface.
5116 private static final int TUN_INTF_NETID_START = 0xFC00;
5117 // The network ID range of IPSec tunnel interface.
5118 private static final int TUN_INTF_NETID_RANGE = 0x0400;
5119
5120 /**
5121 * Get the network ID range reserved for IPSec tunnel interfaces.
5122 *
5123 * @return A Range which indicates the network ID range of IPSec tunnel interface.
5124 * @hide
5125 */
5126 @SystemApi(client = MODULE_LIBRARIES)
5127 @NonNull
5128 public static Range<Integer> getIpSecNetIdRange() {
5129 return new Range(TUN_INTF_NETID_START, TUN_INTF_NETID_START + TUN_INTF_NETID_RANGE - 1);
5130 }
The Android Open Source Project28527d22009-03-03 19:31:44 -08005131}