blob: 0418450ccda9ae1da16a2aba5fb675408e1fdd8a [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;
paulhu56e09df2021-03-17 20:30:33 +080019import static android.net.ConnectivitySettingsManager.PRIVATE_DNS_DEFAULT_MODE;
20import static android.net.ConnectivitySettingsManager.PRIVATE_DNS_MODE;
Junyu Laia62493f2021-01-19 11:10:56 +000021import static android.net.NetworkRequest.Type.BACKGROUND_REQUEST;
junyulaiad010792021-01-11 16:53:38 +080022import static android.net.NetworkRequest.Type.LISTEN;
junyulai1b1c8742021-03-12 20:05:08 +080023import static android.net.NetworkRequest.Type.LISTEN_FOR_BEST;
junyulaiad010792021-01-11 16:53:38 +080024import static android.net.NetworkRequest.Type.REQUEST;
25import static android.net.NetworkRequest.Type.TRACK_DEFAULT;
Lorenzo Colitti76b639e2021-01-29 20:14:04 +090026import static android.net.NetworkRequest.Type.TRACK_SYSTEM_DEFAULT;
Daniel Brightf9e945b2020-06-15 16:10:01 -070027import static android.net.QosCallback.QosCallbackRegistrationException;
junyulaid05a1922019-01-15 11:32:44 +080028
junyulai4c95b082018-12-27 17:25:29 +080029import android.annotation.CallbackExecutor;
Felipe Leme30511352016-01-22 09:44:57 -080030import android.annotation.IntDef;
Chalard Jean158702d2019-01-07 19:26:34 +090031import android.annotation.NonNull;
Robin Leee5d5ed52016-01-05 18:03:46 +000032import android.annotation.Nullable;
Jeff Sharkey656584a2017-04-24 11:18:03 -060033import android.annotation.RequiresPermission;
The Android Open Source Project28527d22009-03-03 19:31:44 -080034import android.annotation.SdkConstant;
35import android.annotation.SdkConstant.SdkConstantType;
lucaslin705c3332021-03-12 17:56:09 +080036import android.annotation.StringDef;
Junyu Laia62493f2021-01-19 11:10:56 +000037import android.annotation.SuppressLint;
Udam Sainicd645462016-01-04 12:16:14 -080038import android.annotation.SystemApi;
Jeff Sharkey9b39e9a2017-06-02 17:36:26 -060039import android.annotation.SystemService;
Robert Greenwaltf99b8392014-03-26 16:47:06 -070040import android.app.PendingIntent;
Lorenzo Colitti79c6f222021-03-18 00:54:57 +090041import android.app.admin.DevicePolicyManager;
Artur Satayev9c2add62019-12-10 17:47:52 +000042import android.compat.annotation.UnsupportedAppUsage;
Lorenzo Colitti79c6f222021-03-18 00:54:57 +090043import android.content.ComponentName;
lucaslin705c3332021-03-12 17:56:09 +080044import android.content.ContentResolver;
Jeff Sharkey2d9e5a52012-04-04 20:40:58 -070045import android.content.Context;
Robert Greenwaltf3017f72014-05-18 23:07:25 -070046import android.content.Intent;
Remi NGUYEN VANd2d60c62021-04-08 16:26:20 +090047import android.net.ConnectivityDiagnosticsManager.DataStallReport.DetectionMethod;
junyulai4c95b082018-12-27 17:25:29 +080048import android.net.IpSecManager.UdpEncapsulationSocket;
49import android.net.SocketKeepalive.Callback;
markchien91c78e52020-01-20 19:31:56 +080050import android.net.TetheringManager.StartTetheringCallback;
markchien6ae63e52020-01-21 13:11:06 +080051import android.net.TetheringManager.TetheringEventCallback;
markchien91c78e52020-01-20 19:31:56 +080052import android.net.TetheringManager.TetheringRequest;
Roshan Pius951c0032020-12-22 15:10:42 -080053import android.net.wifi.WifiNetworkSuggestion;
Robert Greenwalt2034b912009-08-12 16:08:25 -070054import android.os.Binder;
Mathew Inwoodbdfc1fc2018-12-20 15:30:45 +000055import android.os.Build;
Jeff Sharkey39c01eb2011-08-16 14:37:57 -070056import android.os.Build.VERSION_CODES;
Jeremy Klein3dabcb92016-01-22 14:11:45 -080057import android.os.Bundle;
Robert Greenwaltf99b8392014-03-26 16:47:06 -070058import android.os.Handler;
Dianne Hackborn5ac88162014-02-26 16:20:52 -080059import android.os.IBinder;
Robert Greenwaltf99b8392014-03-26 16:47:06 -070060import android.os.Looper;
61import android.os.Message;
Robert Greenwalt15a41532012-08-21 19:27:00 -070062import android.os.Messenger;
junyulai7e06ad42019-03-04 22:45:36 +080063import android.os.ParcelFileDescriptor;
Cody Kestingf53a0752020-04-15 12:33:28 -070064import android.os.PersistableBundle;
Lorenzo Colitti2780e4b2015-07-29 11:41:21 +090065import android.os.Process;
The Android Open Source Project28527d22009-03-03 19:31:44 -080066import android.os.RemoteException;
Jeremy Klein3dabcb92016-01-22 14:11:45 -080067import android.os.ResultReceiver;
Hugo Benichia590ab82017-05-11 13:16:17 +090068import android.os.ServiceSpecificException;
Chalard Jeanfa45a682021-02-25 17:23:40 +090069import android.os.UserHandle;
Jeff Sharkey971cd162011-08-29 16:02:57 -070070import android.provider.Settings;
Wink Saville689f7042014-12-05 11:10:30 -080071import android.telephony.SubscriptionManager;
Meng Wanged6f4412019-11-18 17:10:00 -080072import android.telephony.TelephonyManager;
lucaslin705c3332021-03-12 17:56:09 +080073import android.text.TextUtils;
Dianne Hackborn5ac88162014-02-26 16:20:52 -080074import android.util.ArrayMap;
Robert Greenwaltf99b8392014-03-26 16:47:06 -070075import android.util.Log;
Lorenzo Colitti3f54f102020-12-12 00:51:11 +090076import android.util.Range;
Erik Klinece55eb12017-01-26 18:08:28 +090077import android.util.SparseIntArray;
The Android Open Source Project28527d22009-03-03 19:31:44 -080078
markchien6ae63e52020-01-21 13:11:06 +080079import com.android.internal.annotations.GuardedBy;
Robert Greenwalt0c150c02014-05-21 20:04:36 -070080
Paul Jensen3e2917c2014-08-27 12:38:45 -040081import libcore.net.event.NetworkEventDispatcher;
82
junyulai7e06ad42019-03-04 22:45:36 +080083import java.io.IOException;
84import java.io.UncheckedIOException;
Felipe Leme30511352016-01-22 09:44:57 -080085import java.lang.annotation.Retention;
86import java.lang.annotation.RetentionPolicy;
Lorenzo Colitti3f54f102020-12-12 00:51:11 +090087import java.net.DatagramSocket;
Jeremy Klein434835a2015-12-28 15:11:58 -080088import java.net.InetAddress;
Jeff Vander Stoep39a51e02018-07-23 10:57:53 -070089import java.net.InetSocketAddress;
junyulai0835a1e2019-01-08 20:04:33 +080090import java.net.Socket;
Hugo Benichi2aa65af2017-03-06 09:17:06 +090091import java.util.ArrayList;
Lorenzo Colitti3f54f102020-12-12 00:51:11 +090092import java.util.Collection;
Jeremy Klein434835a2015-12-28 15:11:58 -080093import java.util.HashMap;
Hugo Benichi2aa65af2017-03-06 09:17:06 +090094import java.util.List;
95import java.util.Map;
markchien6ae63e52020-01-21 13:11:06 +080096import java.util.Objects;
junyulai4c95b082018-12-27 17:25:29 +080097import java.util.concurrent.Executor;
junyulai070f9ff2019-01-16 20:23:34 +080098import java.util.concurrent.ExecutorService;
99import java.util.concurrent.Executors;
100import java.util.concurrent.RejectedExecutionException;
Jeremy Klein434835a2015-12-28 15:11:58 -0800101
The Android Open Source Project28527d22009-03-03 19:31:44 -0800102/**
103 * Class that answers queries about the state of network connectivity. It also
Jeff Sharkey9b39e9a2017-06-02 17:36:26 -0600104 * notifies applications when network connectivity changes.
The Android Open Source Project28527d22009-03-03 19:31:44 -0800105 * <p>
106 * The primary responsibilities of this class are to:
107 * <ol>
108 * <li>Monitor network connections (Wi-Fi, GPRS, UMTS, etc.)</li>
109 * <li>Send broadcast intents when network connectivity changes</li>
110 * <li>Attempt to "fail over" to another network when connectivity to a network
111 * is lost</li>
112 * <li>Provide an API that allows applications to query the coarse-grained or fine-grained
113 * state of the available networks</li>
Robert Greenwaltf3017f72014-05-18 23:07:25 -0700114 * <li>Provide an API that allows applications to request and select networks for their data
115 * traffic</li>
The Android Open Source Project28527d22009-03-03 19:31:44 -0800116 * </ol>
117 */
Jeff Sharkey9b39e9a2017-06-02 17:36:26 -0600118@SystemService(Context.CONNECTIVITY_SERVICE)
Jeff Sharkey921ebf22011-05-19 17:12:49 -0700119public class ConnectivityManager {
120 private static final String TAG = "ConnectivityManager";
Soi, Yoshinarib8102ef2015-11-12 12:09:02 +0900121 private static final boolean DEBUG = Log.isLoggable(TAG, Log.DEBUG);
Jeff Sharkey921ebf22011-05-19 17:12:49 -0700122
The Android Open Source Project28527d22009-03-03 19:31:44 -0800123 /**
Robert Greenwaltf3017f72014-05-18 23:07:25 -0700124 * A change in network connectivity has occurred. A default connection has either
The Android Open Source Project28527d22009-03-03 19:31:44 -0800125 * been established or lost. The NetworkInfo for the affected network is
126 * sent as an extra; it should be consulted to see what kind of
127 * connectivity event occurred.
128 * <p/>
Mark Lu3e422ac2016-12-05 10:57:55 -0800129 * Apps targeting Android 7.0 (API level 24) and higher do not receive this
130 * broadcast if they declare the broadcast receiver in their manifest. Apps
131 * will still receive broadcasts if they register their
132 * {@link android.content.BroadcastReceiver} with
133 * {@link android.content.Context#registerReceiver Context.registerReceiver()}
134 * and that context is still valid.
135 * <p/>
The Android Open Source Project28527d22009-03-03 19:31:44 -0800136 * If this is a connection that was the result of failing over from a
137 * disconnected network, then the FAILOVER_CONNECTION boolean extra is
138 * set to true.
139 * <p/>
140 * For a loss of connectivity, if the connectivity manager is attempting
141 * to connect (or has already connected) to another network, the
142 * NetworkInfo for the new network is also passed as an extra. This lets
143 * any receivers of the broadcast know that they should not necessarily
144 * tell the user that no data traffic will be possible. Instead, the
Robert Greenwaltfaa4b402013-02-15 10:56:35 -0800145 * receiver should expect another broadcast soon, indicating either that
The Android Open Source Project28527d22009-03-03 19:31:44 -0800146 * the failover attempt succeeded (and so there is still overall data
147 * connectivity), or that the failover attempt failed, meaning that all
148 * connectivity has been lost.
149 * <p/>
150 * For a disconnect event, the boolean extra EXTRA_NO_CONNECTIVITY
151 * is set to {@code true} if there are no connected networks at all.
Chalard Jean52e23962018-02-10 05:33:50 +0900152 *
153 * @deprecated apps should use the more versatile {@link #requestNetwork},
154 * {@link #registerNetworkCallback} or {@link #registerDefaultNetworkCallback}
155 * functions instead for faster and more detailed updates about the network
156 * changes they care about.
The Android Open Source Project28527d22009-03-03 19:31:44 -0800157 */
Jeff Sharkeyee9275b2013-02-20 18:21:19 -0800158 @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION)
Chalard Jean52e23962018-02-10 05:33:50 +0900159 @Deprecated
The Android Open Source Project28527d22009-03-03 19:31:44 -0800160 public static final String CONNECTIVITY_ACTION = "android.net.conn.CONNECTIVITY_CHANGE";
Jeff Sharkey66fa9682011-08-02 17:22:34 -0700161
The Android Open Source Project28527d22009-03-03 19:31:44 -0800162 /**
Paul Jensen60df4aa2015-02-27 22:55:47 -0500163 * The device has connected to a network that has presented a captive
164 * portal, which is blocking Internet connectivity. The user was presented
165 * with a notification that network sign in is required,
166 * and the user invoked the notification's action indicating they
Paul Jensen75e0adb2015-05-22 10:50:39 -0400167 * desire to sign in to the network. Apps handling this activity should
Paul Jensen60df4aa2015-02-27 22:55:47 -0500168 * facilitate signing in to the network. This action includes a
169 * {@link Network} typed extra called {@link #EXTRA_NETWORK} that represents
170 * the network presenting the captive portal; all communication with the
171 * captive portal must be done using this {@code Network} object.
172 * <p/>
Paul Jensen75e0adb2015-05-22 10:50:39 -0400173 * This activity includes a {@link CaptivePortal} extra named
174 * {@link #EXTRA_CAPTIVE_PORTAL} that can be used to indicate different
175 * outcomes of the captive portal sign in to the system:
176 * <ul>
177 * <li> When the app handling this action believes the user has signed in to
178 * the network and the captive portal has been dismissed, the app should
179 * call {@link CaptivePortal#reportCaptivePortalDismissed} so the system can
180 * reevaluate the network. If reevaluation finds the network no longer
181 * subject to a captive portal, the network may become the default active
Chalard Jean9dd11612018-06-04 16:52:49 +0900182 * data network.</li>
Paul Jensen75e0adb2015-05-22 10:50:39 -0400183 * <li> When the app handling this action believes the user explicitly wants
Paul Jensen60df4aa2015-02-27 22:55:47 -0500184 * to ignore the captive portal and the network, the app should call
Paul Jensen75e0adb2015-05-22 10:50:39 -0400185 * {@link CaptivePortal#ignoreNetwork}. </li>
186 * </ul>
Paul Jensen60df4aa2015-02-27 22:55:47 -0500187 */
188 @SdkConstant(SdkConstantType.ACTIVITY_INTENT_ACTION)
189 public static final String ACTION_CAPTIVE_PORTAL_SIGN_IN = "android.net.conn.CAPTIVE_PORTAL";
190
191 /**
The Android Open Source Project28527d22009-03-03 19:31:44 -0800192 * The lookup key for a {@link NetworkInfo} object. Retrieve with
193 * {@link android.content.Intent#getParcelableExtra(String)}.
Jeff Sharkey66fa9682011-08-02 17:22:34 -0700194 *
Chalard Jean97021a12019-01-11 16:47:53 +0900195 * @deprecated The {@link NetworkInfo} object is deprecated, as many of its properties
196 * can't accurately represent modern network characteristics.
197 * Please obtain information about networks from the {@link NetworkCapabilities}
198 * or {@link LinkProperties} objects instead.
The Android Open Source Project28527d22009-03-03 19:31:44 -0800199 */
Jeff Sharkey66fa9682011-08-02 17:22:34 -0700200 @Deprecated
The Android Open Source Project28527d22009-03-03 19:31:44 -0800201 public static final String EXTRA_NETWORK_INFO = "networkInfo";
Jeff Sharkey66fa9682011-08-02 17:22:34 -0700202
The Android Open Source Project28527d22009-03-03 19:31:44 -0800203 /**
Jeff Sharkey47905d12012-08-06 11:41:50 -0700204 * Network type which triggered a {@link #CONNECTIVITY_ACTION} broadcast.
Jeff Sharkey47905d12012-08-06 11:41:50 -0700205 *
206 * @see android.content.Intent#getIntExtra(String, int)
Chalard Jean97021a12019-01-11 16:47:53 +0900207 * @deprecated The network type is not rich enough to represent the characteristics
208 * of modern networks. Please use {@link NetworkCapabilities} instead,
209 * in particular the transports.
Jeff Sharkey47905d12012-08-06 11:41:50 -0700210 */
Chalard Jean97021a12019-01-11 16:47:53 +0900211 @Deprecated
Jeff Sharkey47905d12012-08-06 11:41:50 -0700212 public static final String EXTRA_NETWORK_TYPE = "networkType";
213
214 /**
The Android Open Source Project28527d22009-03-03 19:31:44 -0800215 * The lookup key for a boolean that indicates whether a connect event
216 * is for a network to which the connectivity manager was failing over
217 * following a disconnect on another network.
218 * Retrieve it with {@link android.content.Intent#getBooleanExtra(String,boolean)}.
junyulai9826e7f2018-12-13 12:47:51 +0800219 *
220 * @deprecated See {@link NetworkInfo}.
The Android Open Source Project28527d22009-03-03 19:31:44 -0800221 */
junyulai9826e7f2018-12-13 12:47:51 +0800222 @Deprecated
The Android Open Source Project28527d22009-03-03 19:31:44 -0800223 public static final String EXTRA_IS_FAILOVER = "isFailover";
224 /**
225 * The lookup key for a {@link NetworkInfo} object. This is supplied when
226 * there is another network that it may be possible to connect to. Retrieve with
227 * {@link android.content.Intent#getParcelableExtra(String)}.
junyulai9826e7f2018-12-13 12:47:51 +0800228 *
229 * @deprecated See {@link NetworkInfo}.
The Android Open Source Project28527d22009-03-03 19:31:44 -0800230 */
junyulai9826e7f2018-12-13 12:47:51 +0800231 @Deprecated
The Android Open Source Project28527d22009-03-03 19:31:44 -0800232 public static final String EXTRA_OTHER_NETWORK_INFO = "otherNetwork";
233 /**
234 * The lookup key for a boolean that indicates whether there is a
235 * complete lack of connectivity, i.e., no network is available.
236 * Retrieve it with {@link android.content.Intent#getBooleanExtra(String,boolean)}.
237 */
238 public static final String EXTRA_NO_CONNECTIVITY = "noConnectivity";
239 /**
240 * The lookup key for a string that indicates why an attempt to connect
241 * to a network failed. The string has no particular structure. It is
242 * intended to be used in notifications presented to users. Retrieve
243 * it with {@link android.content.Intent#getStringExtra(String)}.
244 */
245 public static final String EXTRA_REASON = "reason";
246 /**
247 * The lookup key for a string that provides optionally supplied
248 * extra information about the network state. The information
249 * may be passed up from the lower networking layers, and its
250 * meaning may be specific to a particular network type. Retrieve
251 * it with {@link android.content.Intent#getStringExtra(String)}.
junyulai9826e7f2018-12-13 12:47:51 +0800252 *
253 * @deprecated See {@link NetworkInfo#getExtraInfo()}.
The Android Open Source Project28527d22009-03-03 19:31:44 -0800254 */
junyulai9826e7f2018-12-13 12:47:51 +0800255 @Deprecated
The Android Open Source Project28527d22009-03-03 19:31:44 -0800256 public static final String EXTRA_EXTRA_INFO = "extraInfo";
Robert Greenwalt986c7412010-09-08 15:24:47 -0700257 /**
258 * The lookup key for an int that provides information about
259 * our connection to the internet at large. 0 indicates no connection,
260 * 100 indicates a great connection. Retrieve it with
Jeff Sharkey921ebf22011-05-19 17:12:49 -0700261 * {@link android.content.Intent#getIntExtra(String, int)}.
Robert Greenwalt986c7412010-09-08 15:24:47 -0700262 * {@hide}
263 */
264 public static final String EXTRA_INET_CONDITION = "inetCondition";
The Android Open Source Project28527d22009-03-03 19:31:44 -0800265 /**
Paul Jensen75e0adb2015-05-22 10:50:39 -0400266 * The lookup key for a {@link CaptivePortal} object included with the
267 * {@link #ACTION_CAPTIVE_PORTAL_SIGN_IN} intent. The {@code CaptivePortal}
268 * object can be used to either indicate to the system that the captive
269 * portal has been dismissed or that the user does not want to pursue
270 * signing in to captive portal. Retrieve it with
271 * {@link android.content.Intent#getParcelableExtra(String)}.
Paul Jensen60df4aa2015-02-27 22:55:47 -0500272 */
Paul Jensen75e0adb2015-05-22 10:50:39 -0400273 public static final String EXTRA_CAPTIVE_PORTAL = "android.net.extra.CAPTIVE_PORTAL";
Jan Nordqvist032e2672015-09-22 15:54:32 -0700274
275 /**
276 * Key for passing a URL to the captive portal login activity.
277 */
278 public static final String EXTRA_CAPTIVE_PORTAL_URL = "android.net.extra.CAPTIVE_PORTAL_URL";
279
Paul Jensen60df4aa2015-02-27 22:55:47 -0500280 /**
Remi NGUYEN VANc2491572018-05-22 10:01:53 +0900281 * Key for passing a {@link android.net.captiveportal.CaptivePortalProbeSpec} to the captive
282 * portal login activity.
283 * {@hide}
284 */
Remi NGUYEN VAN70ab67f2019-01-17 14:38:31 +0900285 @SystemApi
Remi NGUYEN VANc2491572018-05-22 10:01:53 +0900286 public static final String EXTRA_CAPTIVE_PORTAL_PROBE_SPEC =
287 "android.net.extra.CAPTIVE_PORTAL_PROBE_SPEC";
288
289 /**
Hugo Benichi454e0662016-12-14 08:23:40 +0900290 * Key for passing a user agent string to the captive portal login activity.
291 * {@hide}
292 */
Remi NGUYEN VAN70ab67f2019-01-17 14:38:31 +0900293 @SystemApi
Hugo Benichi454e0662016-12-14 08:23:40 +0900294 public static final String EXTRA_CAPTIVE_PORTAL_USER_AGENT =
295 "android.net.extra.CAPTIVE_PORTAL_USER_AGENT";
296
297 /**
Haoyu Baib5da5752012-06-20 14:29:57 -0700298 * Broadcast action to indicate the change of data activity status
299 * (idle or active) on a network in a recent period.
Robert Greenwaltfaa4b402013-02-15 10:56:35 -0800300 * The network becomes active when data transmission is started, or
301 * idle if there is no data transmission for a period of time.
Haoyu Baib5da5752012-06-20 14:29:57 -0700302 * {@hide}
303 */
304 @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION)
Chalard Jean9dd11612018-06-04 16:52:49 +0900305 public static final String ACTION_DATA_ACTIVITY_CHANGE =
306 "android.net.conn.DATA_ACTIVITY_CHANGE";
Haoyu Baib5da5752012-06-20 14:29:57 -0700307 /**
308 * The lookup key for an enum that indicates the network device type on which this data activity
309 * change happens.
310 * {@hide}
311 */
312 public static final String EXTRA_DEVICE_TYPE = "deviceType";
313 /**
314 * The lookup key for a boolean that indicates the device is active or not. {@code true} means
315 * it is actively sending or receiving data and {@code false} means it is idle.
316 * {@hide}
317 */
318 public static final String EXTRA_IS_ACTIVE = "isActive";
Ashish Sharma29f7e0e2014-03-12 18:42:23 -0700319 /**
320 * The lookup key for a long that contains the timestamp (nanos) of the radio state change.
321 * {@hide}
322 */
323 public static final String EXTRA_REALTIME_NS = "tsNanos";
Haoyu Baib5da5752012-06-20 14:29:57 -0700324
325 /**
The Android Open Source Project28527d22009-03-03 19:31:44 -0800326 * Broadcast Action: The setting for background data usage has changed
327 * values. Use {@link #getBackgroundDataSetting()} to get the current value.
328 * <p>
329 * If an application uses the network in the background, it should listen
330 * for this broadcast and stop using the background data if the value is
Jeff Sharkey921ebf22011-05-19 17:12:49 -0700331 * {@code false}.
Jeff Sharkeyc958c772012-01-30 16:29:24 -0800332 * <p>
333 *
334 * @deprecated As of {@link VERSION_CODES#ICE_CREAM_SANDWICH}, availability
335 * of background data depends on several combined factors, and
336 * this broadcast is no longer sent. Instead, when background
337 * data is unavailable, {@link #getActiveNetworkInfo()} will now
338 * appear disconnected. During first boot after a platform
339 * upgrade, this broadcast will be sent once if
340 * {@link #getBackgroundDataSetting()} was {@code false} before
341 * the upgrade.
The Android Open Source Project28527d22009-03-03 19:31:44 -0800342 */
343 @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION)
Jeff Sharkeyc958c772012-01-30 16:29:24 -0800344 @Deprecated
The Android Open Source Project28527d22009-03-03 19:31:44 -0800345 public static final String ACTION_BACKGROUND_DATA_SETTING_CHANGED =
346 "android.net.conn.BACKGROUND_DATA_SETTING_CHANGED";
347
Robert Greenwaltd3401f92010-09-15 17:36:33 -0700348 /**
349 * Broadcast Action: The network connection may not be good
350 * uses {@code ConnectivityManager.EXTRA_INET_CONDITION} and
351 * {@code ConnectivityManager.EXTRA_NETWORK_INFO} to specify
352 * the network and it's condition.
353 * @hide
354 */
Jeff Sharkeyee9275b2013-02-20 18:21:19 -0800355 @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION)
Mathew Inwoodc5b9bec2018-08-08 14:52:47 +0100356 @UnsupportedAppUsage
Robert Greenwaltd3401f92010-09-15 17:36:33 -0700357 public static final String INET_CONDITION_ACTION =
358 "android.net.conn.INET_CONDITION_ACTION";
359
Robert Greenwalt2034b912009-08-12 16:08:25 -0700360 /**
Robert Greenwaltfaa4b402013-02-15 10:56:35 -0800361 * Broadcast Action: A tetherable connection has come or gone.
362 * Uses {@code ConnectivityManager.EXTRA_AVAILABLE_TETHER},
Erik Kline762fa8e2017-04-17 16:47:23 +0900363 * {@code ConnectivityManager.EXTRA_ACTIVE_LOCAL_ONLY},
364 * {@code ConnectivityManager.EXTRA_ACTIVE_TETHER}, and
Robert Greenwaltfaa4b402013-02-15 10:56:35 -0800365 * {@code ConnectivityManager.EXTRA_ERRORED_TETHER} to indicate
366 * the current state of tethering. Each include a list of
367 * interface names in that state (may be empty).
Robert Greenwalt0c4828c2010-01-26 11:40:34 -0800368 * @hide
369 */
Jeff Sharkeyee9275b2013-02-20 18:21:19 -0800370 @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION)
Mathew Inwoode1a17ba2020-11-04 09:29:36 +0000371 @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.R, trackingBug = 170729553)
Robert Greenwalt0c4828c2010-01-26 11:40:34 -0800372 public static final String ACTION_TETHER_STATE_CHANGED =
markchiena0f8fd92019-12-25 19:40:32 +0800373 TetheringManager.ACTION_TETHER_STATE_CHANGED;
Robert Greenwalt0c4828c2010-01-26 11:40:34 -0800374
375 /**
376 * @hide
Robert Greenwaltfaa4b402013-02-15 10:56:35 -0800377 * gives a String[] listing all the interfaces configured for
378 * tethering and currently available for tethering.
Robert Greenwalt0c4828c2010-01-26 11:40:34 -0800379 */
Mathew Inwoode1a17ba2020-11-04 09:29:36 +0000380 @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.R, trackingBug = 170729553)
markchiena0f8fd92019-12-25 19:40:32 +0800381 public static final String EXTRA_AVAILABLE_TETHER = TetheringManager.EXTRA_AVAILABLE_TETHER;
Robert Greenwalt0c4828c2010-01-26 11:40:34 -0800382
383 /**
384 * @hide
Erik Kline762fa8e2017-04-17 16:47:23 +0900385 * gives a String[] listing all the interfaces currently in local-only
386 * mode (ie, has DHCPv4+IPv6-ULA support and no packet forwarding)
Robert Greenwalt0c4828c2010-01-26 11:40:34 -0800387 */
markchiena0f8fd92019-12-25 19:40:32 +0800388 public static final String EXTRA_ACTIVE_LOCAL_ONLY = TetheringManager.EXTRA_ACTIVE_LOCAL_ONLY;
Erik Kline762fa8e2017-04-17 16:47:23 +0900389
390 /**
391 * @hide
392 * gives a String[] listing all the interfaces currently tethered
393 * (ie, has DHCPv4 support and packets potentially forwarded/NATed)
394 */
Mathew Inwoode1a17ba2020-11-04 09:29:36 +0000395 @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.R, trackingBug = 170729553)
markchiena0f8fd92019-12-25 19:40:32 +0800396 public static final String EXTRA_ACTIVE_TETHER = TetheringManager.EXTRA_ACTIVE_TETHER;
Robert Greenwalt8e87f122010-02-11 18:18:40 -0800397
398 /**
399 * @hide
Robert Greenwaltfaa4b402013-02-15 10:56:35 -0800400 * gives a String[] listing all the interfaces we tried to tether and
401 * failed. Use {@link #getLastTetherError} to find the error code
402 * for any interfaces listed here.
Robert Greenwalt8e87f122010-02-11 18:18:40 -0800403 */
Mathew Inwoode1a17ba2020-11-04 09:29:36 +0000404 @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.R, trackingBug = 170729553)
markchiena0f8fd92019-12-25 19:40:32 +0800405 public static final String EXTRA_ERRORED_TETHER = TetheringManager.EXTRA_ERRORED_TETHER;
Robert Greenwalt0c4828c2010-01-26 11:40:34 -0800406
407 /**
Russell Brenner30fe2642013-02-12 10:03:14 -0800408 * Broadcast Action: The captive portal tracker has finished its test.
409 * Sent only while running Setup Wizard, in lieu of showing a user
410 * notification.
411 * @hide
412 */
Jeff Sharkeyee9275b2013-02-20 18:21:19 -0800413 @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION)
Russell Brenner30fe2642013-02-12 10:03:14 -0800414 public static final String ACTION_CAPTIVE_PORTAL_TEST_COMPLETED =
415 "android.net.conn.CAPTIVE_PORTAL_TEST_COMPLETED";
416 /**
417 * The lookup key for a boolean that indicates whether a captive portal was detected.
418 * Retrieve it with {@link android.content.Intent#getBooleanExtra(String,boolean)}.
419 * @hide
420 */
421 public static final String EXTRA_IS_CAPTIVE_PORTAL = "captivePortal";
422
423 /**
Lorenzo Colitti6947c062015-04-03 16:38:52 +0900424 * Action used to display a dialog that asks the user whether to connect to a network that is
425 * not validated. This intent is used to start the dialog in settings via startActivity.
426 *
427 * @hide
428 */
lucasline8b3f9c2021-03-04 17:09:51 +0800429 @SystemApi(client = MODULE_LIBRARIES)
430 public static final String ACTION_PROMPT_UNVALIDATED = "android.net.action.PROMPT_UNVALIDATED";
Lorenzo Colitti6947c062015-04-03 16:38:52 +0900431
432 /**
Lorenzo Colitti199ecfc2016-09-15 14:02:29 +0900433 * Action used to display a dialog that asks the user whether to avoid a network that is no
434 * longer validated. This intent is used to start the dialog in settings via startActivity.
435 *
436 * @hide
437 */
lucasline8b3f9c2021-03-04 17:09:51 +0800438 @SystemApi(client = MODULE_LIBRARIES)
Lorenzo Colitti199ecfc2016-09-15 14:02:29 +0900439 public static final String ACTION_PROMPT_LOST_VALIDATION =
lucasline8b3f9c2021-03-04 17:09:51 +0800440 "android.net.action.PROMPT_LOST_VALIDATION";
Lorenzo Colitti199ecfc2016-09-15 14:02:29 +0900441
442 /**
lucaslin2240ef62019-03-12 13:08:03 +0800443 * Action used to display a dialog that asks the user whether to stay connected to a network
444 * that has not validated. This intent is used to start the dialog in settings via
445 * startActivity.
446 *
447 * @hide
448 */
lucasline8b3f9c2021-03-04 17:09:51 +0800449 @SystemApi(client = MODULE_LIBRARIES)
lucaslin2240ef62019-03-12 13:08:03 +0800450 public static final String ACTION_PROMPT_PARTIAL_CONNECTIVITY =
lucasline8b3f9c2021-03-04 17:09:51 +0800451 "android.net.action.PROMPT_PARTIAL_CONNECTIVITY";
lucaslin2240ef62019-03-12 13:08:03 +0800452
453 /**
paulhue3fe4d02021-04-07 16:18:13 +0800454 * Clear DNS Cache Action: This is broadcast when networks have changed and old
455 * DNS entries should be cleared.
456 * @hide
457 */
458 @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION)
459 @SystemApi(client = SystemApi.Client.MODULE_LIBRARIES)
460 public static final String ACTION_CLEAR_DNS_CACHE = "android.net.action.CLEAR_DNS_CACHE";
461
462 /**
Jeremy Klein3dabcb92016-01-22 14:11:45 -0800463 * Invalid tethering type.
Chalard Jean9dd11612018-06-04 16:52:49 +0900464 * @see #startTethering(int, boolean, OnStartTetheringCallback)
Jeremy Klein3dabcb92016-01-22 14:11:45 -0800465 * @hide
466 */
markchiena0f8fd92019-12-25 19:40:32 +0800467 public static final int TETHERING_INVALID = TetheringManager.TETHERING_INVALID;
Jeremy Klein3dabcb92016-01-22 14:11:45 -0800468
469 /**
470 * Wifi tethering type.
Chalard Jean9dd11612018-06-04 16:52:49 +0900471 * @see #startTethering(int, boolean, OnStartTetheringCallback)
Jeremy Klein3dabcb92016-01-22 14:11:45 -0800472 * @hide
473 */
474 @SystemApi
Remi NGUYEN VAN09f8ed22021-02-15 18:52:06 +0900475 public static final int TETHERING_WIFI = 0;
Jeremy Klein3dabcb92016-01-22 14:11:45 -0800476
477 /**
478 * USB tethering type.
Chalard Jean9dd11612018-06-04 16:52:49 +0900479 * @see #startTethering(int, boolean, OnStartTetheringCallback)
Jeremy Klein3dabcb92016-01-22 14:11:45 -0800480 * @hide
481 */
482 @SystemApi
Remi NGUYEN VAN09f8ed22021-02-15 18:52:06 +0900483 public static final int TETHERING_USB = 1;
Jeremy Klein3dabcb92016-01-22 14:11:45 -0800484
485 /**
486 * Bluetooth tethering type.
Chalard Jean9dd11612018-06-04 16:52:49 +0900487 * @see #startTethering(int, boolean, OnStartTetheringCallback)
Jeremy Klein3dabcb92016-01-22 14:11:45 -0800488 * @hide
489 */
490 @SystemApi
Remi NGUYEN VAN09f8ed22021-02-15 18:52:06 +0900491 public static final int TETHERING_BLUETOOTH = 2;
Jeremy Klein3dabcb92016-01-22 14:11:45 -0800492
493 /**
Jimmy Chendd31bc42019-07-15 18:03:23 +0800494 * Wifi P2p tethering type.
495 * Wifi P2p tethering is set through events automatically, and don't
496 * need to start from #startTethering(int, boolean, OnStartTetheringCallback).
497 * @hide
498 */
markchiena0f8fd92019-12-25 19:40:32 +0800499 public static final int TETHERING_WIFI_P2P = TetheringManager.TETHERING_WIFI_P2P;
Jimmy Chendd31bc42019-07-15 18:03:23 +0800500
501 /**
Jeremy Klein3dabcb92016-01-22 14:11:45 -0800502 * Extra used for communicating with the TetherService. Includes the type of tethering to
503 * enable if any.
504 * @hide
505 */
markchien6ae63e52020-01-21 13:11:06 +0800506 public static final String EXTRA_ADD_TETHER_TYPE = TetheringConstants.EXTRA_ADD_TETHER_TYPE;
Jeremy Klein3dabcb92016-01-22 14:11:45 -0800507
508 /**
509 * Extra used for communicating with the TetherService. Includes the type of tethering for
510 * which to cancel provisioning.
511 * @hide
512 */
markchien6ae63e52020-01-21 13:11:06 +0800513 public static final String EXTRA_REM_TETHER_TYPE = TetheringConstants.EXTRA_REM_TETHER_TYPE;
Jeremy Klein3dabcb92016-01-22 14:11:45 -0800514
515 /**
516 * Extra used for communicating with the TetherService. True to schedule a recheck of tether
517 * provisioning.
518 * @hide
519 */
markchien6ae63e52020-01-21 13:11:06 +0800520 public static final String EXTRA_SET_ALARM = TetheringConstants.EXTRA_SET_ALARM;
Jeremy Klein3dabcb92016-01-22 14:11:45 -0800521
522 /**
523 * Tells the TetherService to run a provision check now.
524 * @hide
525 */
markchien6ae63e52020-01-21 13:11:06 +0800526 public static final String EXTRA_RUN_PROVISION = TetheringConstants.EXTRA_RUN_PROVISION;
Jeremy Klein3dabcb92016-01-22 14:11:45 -0800527
528 /**
529 * Extra used for communicating with the TetherService. Contains the {@link ResultReceiver}
530 * which will receive provisioning results. Can be left empty.
531 * @hide
532 */
markchien6ae63e52020-01-21 13:11:06 +0800533 public static final String EXTRA_PROVISION_CALLBACK =
534 TetheringConstants.EXTRA_PROVISION_CALLBACK;
Jeremy Klein3dabcb92016-01-22 14:11:45 -0800535
536 /**
Robert Greenwaltfaa4b402013-02-15 10:56:35 -0800537 * The absence of a connection type.
Robert Greenwalt33cdcdf2011-06-02 17:30:47 -0700538 * @hide
539 */
paulhu74357e72020-01-13 16:46:45 +0800540 @SystemApi
Robert Greenwalt33cdcdf2011-06-02 17:30:47 -0700541 public static final int TYPE_NONE = -1;
542
543 /**
Chalard Jean7eaf3b12018-03-08 13:54:53 +0900544 * A Mobile data connection. Devices may support more than one.
545 *
546 * @deprecated Applications should instead use {@link NetworkCapabilities#hasTransport} or
547 * {@link #requestNetwork(NetworkRequest, NetworkCallback)} to request an
548 * appropriate network. {@see NetworkCapabilities} for supported transports.
Robert Greenwalt2034b912009-08-12 16:08:25 -0700549 */
Chalard Jean7eaf3b12018-03-08 13:54:53 +0900550 @Deprecated
Robert Greenwalt2034b912009-08-12 16:08:25 -0700551 public static final int TYPE_MOBILE = 0;
Chalard Jean7eaf3b12018-03-08 13:54:53 +0900552
Robert Greenwalt2034b912009-08-12 16:08:25 -0700553 /**
Chalard Jean7eaf3b12018-03-08 13:54:53 +0900554 * A WIFI data connection. Devices may support more than one.
555 *
556 * @deprecated Applications should instead use {@link NetworkCapabilities#hasTransport} or
557 * {@link #requestNetwork(NetworkRequest, NetworkCallback)} to request an
558 * appropriate network. {@see NetworkCapabilities} for supported transports.
Robert Greenwalt2034b912009-08-12 16:08:25 -0700559 */
Chalard Jean7eaf3b12018-03-08 13:54:53 +0900560 @Deprecated
Robert Greenwalt2034b912009-08-12 16:08:25 -0700561 public static final int TYPE_WIFI = 1;
Chalard Jean7eaf3b12018-03-08 13:54:53 +0900562
Robert Greenwalt2034b912009-08-12 16:08:25 -0700563 /**
Robert Greenwaltfaa4b402013-02-15 10:56:35 -0800564 * An MMS-specific Mobile data connection. This network type may use the
565 * same network interface as {@link #TYPE_MOBILE} or it may use a different
566 * one. This is used by applications needing to talk to the carrier's
567 * Multimedia Messaging Service servers.
Lorenzo Colitti21e9a152015-04-23 15:32:42 +0900568 *
Chalard Jean7eaf3b12018-03-08 13:54:53 +0900569 * @deprecated Applications should instead use {@link NetworkCapabilities#hasCapability} or
Lorenzo Colitti7f6a2bf2015-04-24 17:03:31 +0900570 * {@link #requestNetwork(NetworkRequest, NetworkCallback)} to request a network that
Lorenzo Colitti21e9a152015-04-23 15:32:42 +0900571 * provides the {@link NetworkCapabilities#NET_CAPABILITY_MMS} capability.
Robert Greenwalt2034b912009-08-12 16:08:25 -0700572 */
Aurimas Liutikase80a0eb2016-05-24 15:22:55 -0700573 @Deprecated
Robert Greenwalt2034b912009-08-12 16:08:25 -0700574 public static final int TYPE_MOBILE_MMS = 2;
Chalard Jean7eaf3b12018-03-08 13:54:53 +0900575
Robert Greenwalt2034b912009-08-12 16:08:25 -0700576 /**
Robert Greenwaltfaa4b402013-02-15 10:56:35 -0800577 * A SUPL-specific Mobile data connection. This network type may use the
578 * same network interface as {@link #TYPE_MOBILE} or it may use a different
579 * one. This is used by applications needing to talk to the carrier's
580 * Secure User Plane Location servers for help locating the device.
Lorenzo Colitti21e9a152015-04-23 15:32:42 +0900581 *
Chalard Jean7eaf3b12018-03-08 13:54:53 +0900582 * @deprecated Applications should instead use {@link NetworkCapabilities#hasCapability} or
Lorenzo Colitti7f6a2bf2015-04-24 17:03:31 +0900583 * {@link #requestNetwork(NetworkRequest, NetworkCallback)} to request a network that
Lorenzo Colitti21e9a152015-04-23 15:32:42 +0900584 * provides the {@link NetworkCapabilities#NET_CAPABILITY_SUPL} capability.
Robert Greenwalt2034b912009-08-12 16:08:25 -0700585 */
Aurimas Liutikase80a0eb2016-05-24 15:22:55 -0700586 @Deprecated
Robert Greenwalt2034b912009-08-12 16:08:25 -0700587 public static final int TYPE_MOBILE_SUPL = 3;
Chalard Jean7eaf3b12018-03-08 13:54:53 +0900588
Robert Greenwalt2034b912009-08-12 16:08:25 -0700589 /**
Robert Greenwaltfaa4b402013-02-15 10:56:35 -0800590 * A DUN-specific Mobile data connection. This network type may use the
591 * same network interface as {@link #TYPE_MOBILE} or it may use a different
592 * one. This is sometimes by the system when setting up an upstream connection
593 * for tethering so that the carrier is aware of DUN traffic.
Chalard Jean7eaf3b12018-03-08 13:54:53 +0900594 *
595 * @deprecated Applications should instead use {@link NetworkCapabilities#hasCapability} or
596 * {@link #requestNetwork(NetworkRequest, NetworkCallback)} to request a network that
597 * provides the {@link NetworkCapabilities#NET_CAPABILITY_DUN} capability.
Robert Greenwalt2034b912009-08-12 16:08:25 -0700598 */
Chalard Jean7eaf3b12018-03-08 13:54:53 +0900599 @Deprecated
Robert Greenwalt2034b912009-08-12 16:08:25 -0700600 public static final int TYPE_MOBILE_DUN = 4;
Chalard Jean7eaf3b12018-03-08 13:54:53 +0900601
Robert Greenwalt2034b912009-08-12 16:08:25 -0700602 /**
Robert Greenwaltfaa4b402013-02-15 10:56:35 -0800603 * A High Priority Mobile data connection. This network type uses the
604 * same network interface as {@link #TYPE_MOBILE} but the routing setup
Lorenzo Colitti21e9a152015-04-23 15:32:42 +0900605 * is different.
606 *
Chalard Jean7eaf3b12018-03-08 13:54:53 +0900607 * @deprecated Applications should instead use {@link NetworkCapabilities#hasTransport} or
608 * {@link #requestNetwork(NetworkRequest, NetworkCallback)} to request an
609 * appropriate network. {@see NetworkCapabilities} for supported transports.
Robert Greenwalt2034b912009-08-12 16:08:25 -0700610 */
Aurimas Liutikase80a0eb2016-05-24 15:22:55 -0700611 @Deprecated
Robert Greenwalt2034b912009-08-12 16:08:25 -0700612 public static final int TYPE_MOBILE_HIPRI = 5;
Chalard Jean7eaf3b12018-03-08 13:54:53 +0900613
jshbfa81722010-03-11 15:04:43 -0800614 /**
Chalard Jean7eaf3b12018-03-08 13:54:53 +0900615 * A WiMAX data connection.
616 *
617 * @deprecated Applications should instead use {@link NetworkCapabilities#hasTransport} or
618 * {@link #requestNetwork(NetworkRequest, NetworkCallback)} to request an
619 * appropriate network. {@see NetworkCapabilities} for supported transports.
jshbfa81722010-03-11 15:04:43 -0800620 */
Chalard Jean7eaf3b12018-03-08 13:54:53 +0900621 @Deprecated
jshbfa81722010-03-11 15:04:43 -0800622 public static final int TYPE_WIMAX = 6;
Robert Greenwalteb123ac2010-12-06 13:56:24 -0800623
Jaikumar Ganesh0db51a02010-12-21 22:31:44 -0800624 /**
Chalard Jean7eaf3b12018-03-08 13:54:53 +0900625 * A Bluetooth 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.
Jaikumar Ganesh0db51a02010-12-21 22:31:44 -0800630 */
Chalard Jean7eaf3b12018-03-08 13:54:53 +0900631 @Deprecated
Jaikumar Ganesh0db51a02010-12-21 22:31:44 -0800632 public static final int TYPE_BLUETOOTH = 7;
633
Robert Greenwaltf76c55d2011-04-22 15:28:18 -0700634 /**
Chiachang Wang3b9549f2020-07-28 13:53:09 +0800635 * Fake data connection. This should not be used on shipping devices.
Chalard Jean7eaf3b12018-03-08 13:54:53 +0900636 * @deprecated This is not used any more.
Robert Greenwaltf76c55d2011-04-22 15:28:18 -0700637 */
Chalard Jean7eaf3b12018-03-08 13:54:53 +0900638 @Deprecated
Jaikumar Ganesh0db51a02010-12-21 22:31:44 -0800639 public static final int TYPE_DUMMY = 8;
Wink Savilleb7c92c72011-03-12 14:52:01 -0800640
Robert Greenwaltf76c55d2011-04-22 15:28:18 -0700641 /**
Chalard Jean7eaf3b12018-03-08 13:54:53 +0900642 * An Ethernet data connection.
643 *
644 * @deprecated Applications should instead use {@link NetworkCapabilities#hasTransport} or
645 * {@link #requestNetwork(NetworkRequest, NetworkCallback)} to request an
646 * appropriate network. {@see NetworkCapabilities} for supported transports.
Robert Greenwaltf76c55d2011-04-22 15:28:18 -0700647 */
Chalard Jean7eaf3b12018-03-08 13:54:53 +0900648 @Deprecated
Robert Greenwalt9d077812011-01-28 14:48:37 -0800649 public static final int TYPE_ETHERNET = 9;
Robert Greenwaltf76c55d2011-04-22 15:28:18 -0700650
Wink Savilleb7c92c72011-03-12 14:52:01 -0800651 /**
Robert Greenwaltfaa4b402013-02-15 10:56:35 -0800652 * Over the air Administration.
Chalard Jean7eaf3b12018-03-08 13:54:53 +0900653 * @deprecated Use {@link NetworkCapabilities} instead.
Wink Savilleb7c92c72011-03-12 14:52:01 -0800654 * {@hide}
655 */
Chalard Jean7eaf3b12018-03-08 13:54:53 +0900656 @Deprecated
Chalard Jeana3b77512019-04-09 15:46:21 +0900657 @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P, trackingBug = 130143562)
Wink Savilleb7c92c72011-03-12 14:52:01 -0800658 public static final int TYPE_MOBILE_FOTA = 10;
659
660 /**
Robert Greenwaltfaa4b402013-02-15 10:56:35 -0800661 * IP Multimedia Subsystem.
Chalard Jean7eaf3b12018-03-08 13:54:53 +0900662 * @deprecated Use {@link NetworkCapabilities#NET_CAPABILITY_IMS} instead.
Wink Savilleb7c92c72011-03-12 14:52:01 -0800663 * {@hide}
664 */
Chalard Jean7eaf3b12018-03-08 13:54:53 +0900665 @Deprecated
Mathew Inwoodc5b9bec2018-08-08 14:52:47 +0100666 @UnsupportedAppUsage
Wink Savilleb7c92c72011-03-12 14:52:01 -0800667 public static final int TYPE_MOBILE_IMS = 11;
668
669 /**
Robert Greenwaltfaa4b402013-02-15 10:56:35 -0800670 * Carrier Branded Services.
Chalard Jean7eaf3b12018-03-08 13:54:53 +0900671 * @deprecated Use {@link NetworkCapabilities#NET_CAPABILITY_CBS} instead.
Wink Savilleb7c92c72011-03-12 14:52:01 -0800672 * {@hide}
673 */
Chalard Jean7eaf3b12018-03-08 13:54:53 +0900674 @Deprecated
Chalard Jeana3b77512019-04-09 15:46:21 +0900675 @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P, trackingBug = 130143562)
Wink Savilleb7c92c72011-03-12 14:52:01 -0800676 public static final int TYPE_MOBILE_CBS = 12;
677
repo syncf5de5572011-07-29 23:55:49 -0700678 /**
679 * A Wi-Fi p2p connection. Only requesting processes will have access to
680 * the peers connected.
Chalard Jean7eaf3b12018-03-08 13:54:53 +0900681 * @deprecated Use {@link NetworkCapabilities#NET_CAPABILITY_WIFI_P2P} instead.
repo syncf5de5572011-07-29 23:55:49 -0700682 * {@hide}
683 */
Chalard Jean7eaf3b12018-03-08 13:54:53 +0900684 @Deprecated
paulhue102b0b2020-01-15 15:38:23 +0800685 @SystemApi
repo syncf5de5572011-07-29 23:55:49 -0700686 public static final int TYPE_WIFI_P2P = 13;
Wink Savilleb7c92c72011-03-12 14:52:01 -0800687
Wink Saville512c2202013-07-29 15:00:57 -0700688 /**
689 * The network to use for initially attaching to the network
Chalard Jean7eaf3b12018-03-08 13:54:53 +0900690 * @deprecated Use {@link NetworkCapabilities#NET_CAPABILITY_IA} instead.
Wink Saville512c2202013-07-29 15:00:57 -0700691 * {@hide}
692 */
Chalard Jean7eaf3b12018-03-08 13:54:53 +0900693 @Deprecated
Mathew Inwoodc5b9bec2018-08-08 14:52:47 +0100694 @UnsupportedAppUsage
Wink Saville512c2202013-07-29 15:00:57 -0700695 public static final int TYPE_MOBILE_IA = 14;
repo syncf5de5572011-07-29 23:55:49 -0700696
Lorenzo Colitti21e9a152015-04-23 15:32:42 +0900697 /**
Robert Greenwaltb1f7f752015-07-09 14:49:35 -0700698 * Emergency PDN connection for emergency services. This
699 * may include IMS and MMS in emergency situations.
Chalard Jean7eaf3b12018-03-08 13:54:53 +0900700 * @deprecated Use {@link NetworkCapabilities#NET_CAPABILITY_EIMS} instead.
Ram693d07a2014-06-26 11:03:44 -0700701 * {@hide}
702 */
Chalard Jean7eaf3b12018-03-08 13:54:53 +0900703 @Deprecated
Chalard Jeana3b77512019-04-09 15:46:21 +0900704 @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P, trackingBug = 130143562)
Ram693d07a2014-06-26 11:03:44 -0700705 public static final int TYPE_MOBILE_EMERGENCY = 15;
706
Hui Lu865b70d2014-01-15 11:05:36 -0500707 /**
708 * The network that uses proxy to achieve connectivity.
Chalard Jean7eaf3b12018-03-08 13:54:53 +0900709 * @deprecated Use {@link NetworkCapabilities} instead.
Hui Lu865b70d2014-01-15 11:05:36 -0500710 * {@hide}
711 */
Chalard Jean7eaf3b12018-03-08 13:54:53 +0900712 @Deprecated
Remi NGUYEN VANee660cf2020-11-30 19:23:45 +0900713 @SystemApi
Hui Lu865b70d2014-01-15 11:05:36 -0500714 public static final int TYPE_PROXY = 16;
Wink Saville512c2202013-07-29 15:00:57 -0700715
Robert Greenwaltce7a1442014-07-07 17:09:01 -0700716 /**
717 * A virtual network using one or more native bearers.
718 * It may or may not be providing security services.
Chalard Jean7eaf3b12018-03-08 13:54:53 +0900719 * @deprecated Applications should use {@link NetworkCapabilities#TRANSPORT_VPN} instead.
Robert Greenwaltce7a1442014-07-07 17:09:01 -0700720 */
Chalard Jean7eaf3b12018-03-08 13:54:53 +0900721 @Deprecated
Robert Greenwaltce7a1442014-07-07 17:09:01 -0700722 public static final int TYPE_VPN = 17;
Hui Lu865b70d2014-01-15 11:05:36 -0500723
Benedict Wongbdfaa482018-11-14 17:40:55 -0800724 /**
725 * A network that is exclusively meant to be used for testing
726 *
727 * @deprecated Use {@link NetworkCapabilities} instead.
728 * @hide
729 */
730 @Deprecated
731 public static final int TYPE_TEST = 18; // TODO: Remove this once NetworkTypes are unused.
Robert Greenwaltce7a1442014-07-07 17:09:01 -0700732
Chalard Jeanaea539a2020-03-25 01:24:04 +0900733 /**
734 * @deprecated Use {@link NetworkCapabilities} instead.
735 * @hide
736 */
737 @Deprecated
738 @Retention(RetentionPolicy.SOURCE)
739 @IntDef(prefix = { "TYPE_" }, value = {
740 TYPE_NONE,
741 TYPE_MOBILE,
742 TYPE_WIFI,
743 TYPE_MOBILE_MMS,
744 TYPE_MOBILE_SUPL,
745 TYPE_MOBILE_DUN,
746 TYPE_MOBILE_HIPRI,
747 TYPE_WIMAX,
748 TYPE_BLUETOOTH,
749 TYPE_DUMMY,
750 TYPE_ETHERNET,
751 TYPE_MOBILE_FOTA,
752 TYPE_MOBILE_IMS,
753 TYPE_MOBILE_CBS,
754 TYPE_WIFI_P2P,
755 TYPE_MOBILE_IA,
756 TYPE_MOBILE_EMERGENCY,
757 TYPE_PROXY,
758 TYPE_VPN,
759 TYPE_TEST
760 })
761 public @interface LegacyNetworkType {}
762
Chalard Jeanafaed1a2019-11-21 14:48:00 +0900763 // Deprecated constants for return values of startUsingNetworkFeature. They used to live
764 // in com.android.internal.telephony.PhoneConstants until they were made inaccessible.
765 private static final int DEPRECATED_PHONE_CONSTANT_APN_ALREADY_ACTIVE = 0;
766 private static final int DEPRECATED_PHONE_CONSTANT_APN_REQUEST_STARTED = 1;
767 private static final int DEPRECATED_PHONE_CONSTANT_APN_REQUEST_FAILED = 3;
768
Robert Greenwaltce7a1442014-07-07 17:09:01 -0700769 /** {@hide} */
Benedict Wongbdfaa482018-11-14 17:40:55 -0800770 public static final int MAX_RADIO_TYPE = TYPE_TEST;
771
772 /** {@hide} */
773 public static final int MAX_NETWORK_TYPE = TYPE_TEST;
The Android Open Source Project28527d22009-03-03 19:31:44 -0800774
Hugo Benichiad353f42017-06-20 14:07:59 +0900775 private static final int MIN_NETWORK_TYPE = TYPE_MOBILE;
776
Jianzheng Zhou028d2032012-11-16 13:45:20 +0800777 /**
778 * If you want to set the default network preference,you can directly
779 * change the networkAttributes array in framework's config.xml.
780 *
781 * @deprecated Since we support so many more networks now, the single
782 * network default network preference can't really express
Robert Greenwaltfaa4b402013-02-15 10:56:35 -0800783 * the hierarchy. Instead, the default is defined by the
Jianzheng Zhou028d2032012-11-16 13:45:20 +0800784 * networkAttributes in config.xml. You can determine
Robert Greenwaltf909cb12012-12-07 09:56:50 -0800785 * the current value by calling {@link #getNetworkPreference()}
Jianzheng Zhou028d2032012-11-16 13:45:20 +0800786 * from an App.
787 */
788 @Deprecated
The Android Open Source Project28527d22009-03-03 19:31:44 -0800789 public static final int DEFAULT_NETWORK_PREFERENCE = TYPE_WIFI;
790
Jeff Sharkey8c870452012-09-26 22:03:49 -0700791 /**
Robert Greenwalt42a0e1e2014-03-19 17:56:12 -0700792 * @hide
793 */
Hugo Benichi7ab07a32017-06-20 14:10:14 +0900794 public static final int REQUEST_ID_UNSET = 0;
Robert Greenwaltbfd1f4c2014-06-08 16:42:59 -0700795
Paul Jensen5dea4352014-07-11 12:28:19 -0400796 /**
Hugo Benichibee30fe2017-06-17 13:14:12 +0900797 * Static unique request used as a tombstone for NetworkCallbacks that have been unregistered.
798 * This allows to distinguish when unregistering NetworkCallbacks those that were never
Chalard Jean9dd11612018-06-04 16:52:49 +0900799 * registered from those that were already unregistered.
Hugo Benichibee30fe2017-06-17 13:14:12 +0900800 * @hide
801 */
Hugo Benichi7ab07a32017-06-20 14:10:14 +0900802 private static final NetworkRequest ALREADY_UNREGISTERED =
Hugo Benichibee30fe2017-06-17 13:14:12 +0900803 new NetworkRequest.Builder().clearCapabilities().build();
804
805 /**
Paul Jensen5dea4352014-07-11 12:28:19 -0400806 * A NetID indicating no Network is selected.
807 * Keep in sync with bionic/libc/dns/include/resolv_netid.h
808 * @hide
809 */
810 public static final int NETID_UNSET = 0;
811
Erik Klineb0be3e62017-10-30 15:29:44 +0900812 /**
813 * Private DNS Mode values.
814 *
815 * The "private_dns_mode" global setting stores a String value which is
816 * expected to be one of the following.
817 */
818
819 /**
820 * @hide
821 */
lucaslin705c3332021-03-12 17:56:09 +0800822 @SystemApi(client = MODULE_LIBRARIES)
Erik Klineb0be3e62017-10-30 15:29:44 +0900823 public static final String PRIVATE_DNS_MODE_OFF = "off";
824 /**
825 * @hide
826 */
lucaslin705c3332021-03-12 17:56:09 +0800827 @SystemApi(client = MODULE_LIBRARIES)
Erik Klineb0be3e62017-10-30 15:29:44 +0900828 public static final String PRIVATE_DNS_MODE_OPPORTUNISTIC = "opportunistic";
829 /**
830 * @hide
831 */
lucaslin705c3332021-03-12 17:56:09 +0800832 @SystemApi(client = MODULE_LIBRARIES)
Erik Klineb0be3e62017-10-30 15:29:44 +0900833 public static final String PRIVATE_DNS_MODE_PROVIDER_HOSTNAME = "hostname";
lucaslin705c3332021-03-12 17:56:09 +0800834
835 /** @hide */
836 @Retention(RetentionPolicy.SOURCE)
837 @StringDef(value = {
838 PRIVATE_DNS_MODE_OFF,
839 PRIVATE_DNS_MODE_OPPORTUNISTIC,
840 PRIVATE_DNS_MODE_PROVIDER_HOSTNAME,
841 })
842 public @interface PrivateDnsMode {}
Erik Klineb0be3e62017-10-30 15:29:44 +0900843
Sudheer Shanka98215562021-03-23 08:12:28 +0000844 /**
845 * Flag to indicate that an app is not subject to any restrictions that could result in its
846 * network access blocked.
847 *
848 * @hide
849 */
850 @SystemApi(client = SystemApi.Client.MODULE_LIBRARIES)
851 public static final int BLOCKED_REASON_NONE = 0;
852
853 /**
854 * Flag to indicate that an app is subject to Battery saver restrictions that would
855 * result in its network access being blocked.
856 *
857 * @hide
858 */
859 @SystemApi(client = SystemApi.Client.MODULE_LIBRARIES)
860 public static final int BLOCKED_REASON_BATTERY_SAVER = 1 << 0;
861
862 /**
863 * Flag to indicate that an app is subject to Doze restrictions that would
864 * result in its network access being blocked.
865 *
866 * @hide
867 */
868 @SystemApi(client = SystemApi.Client.MODULE_LIBRARIES)
869 public static final int BLOCKED_REASON_DOZE = 1 << 1;
870
871 /**
872 * Flag to indicate that an app is subject to App Standby restrictions that would
873 * result in its network access being blocked.
874 *
875 * @hide
876 */
877 @SystemApi(client = SystemApi.Client.MODULE_LIBRARIES)
878 public static final int BLOCKED_REASON_APP_STANDBY = 1 << 2;
879
880 /**
881 * Flag to indicate that an app is subject to Restricted mode restrictions that would
882 * result in its network access being blocked.
883 *
884 * @hide
885 */
886 @SystemApi(client = SystemApi.Client.MODULE_LIBRARIES)
887 public static final int BLOCKED_REASON_RESTRICTED_MODE = 1 << 3;
888
889 /**
Lorenzo Colitti79c6f222021-03-18 00:54:57 +0900890 * Flag to indicate that an app is blocked because it is subject to an always-on VPN but the VPN
891 * is not currently connected.
892 *
893 * @see DevicePolicyManager#setAlwaysOnVpnPackage(ComponentName, String, boolean)
894 *
895 * @hide
896 */
897 @SystemApi(client = SystemApi.Client.MODULE_LIBRARIES)
898 public static final int BLOCKED_REASON_LOCKDOWN_VPN = 1 << 4;
899
900 /**
Sudheer Shanka98215562021-03-23 08:12:28 +0000901 * Flag to indicate that an app is subject to Data saver restrictions that would
902 * result in its metered network access being blocked.
903 *
904 * @hide
905 */
906 @SystemApi(client = SystemApi.Client.MODULE_LIBRARIES)
907 public static final int BLOCKED_METERED_REASON_DATA_SAVER = 1 << 16;
908
909 /**
910 * Flag to indicate that an app is subject to user restrictions that would
911 * result in its metered network access being blocked.
912 *
913 * @hide
914 */
915 @SystemApi(client = SystemApi.Client.MODULE_LIBRARIES)
916 public static final int BLOCKED_METERED_REASON_USER_RESTRICTED = 1 << 17;
917
918 /**
919 * Flag to indicate that an app is subject to Device admin restrictions that would
920 * result in its metered network access being blocked.
921 *
922 * @hide
923 */
924 @SystemApi(client = SystemApi.Client.MODULE_LIBRARIES)
925 public static final int BLOCKED_METERED_REASON_ADMIN_DISABLED = 1 << 18;
926
927 /**
928 * @hide
929 */
930 @Retention(RetentionPolicy.SOURCE)
931 @IntDef(flag = true, prefix = {"BLOCKED_"}, value = {
932 BLOCKED_REASON_NONE,
933 BLOCKED_REASON_BATTERY_SAVER,
934 BLOCKED_REASON_DOZE,
935 BLOCKED_REASON_APP_STANDBY,
936 BLOCKED_REASON_RESTRICTED_MODE,
Lorenzo Colitti42fe2232021-03-25 23:17:36 +0900937 BLOCKED_REASON_LOCKDOWN_VPN,
Sudheer Shanka98215562021-03-23 08:12:28 +0000938 BLOCKED_METERED_REASON_DATA_SAVER,
939 BLOCKED_METERED_REASON_USER_RESTRICTED,
940 BLOCKED_METERED_REASON_ADMIN_DISABLED,
941 })
942 public @interface BlockedReason {}
943
Lorenzo Colitti79c6f222021-03-18 00:54:57 +0900944 /**
945 * Set of blocked reasons that are only applicable on metered networks.
946 *
947 * @hide
948 */
949 @SystemApi(client = SystemApi.Client.MODULE_LIBRARIES)
950 public static final int BLOCKED_METERED_REASON_MASK = 0xffff0000;
951
Chalard Jeana3b77512019-04-09 15:46:21 +0900952 @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P, trackingBug = 130143562)
Jeff Sharkey921ebf22011-05-19 17:12:49 -0700953 private final IConnectivityManager mService;
Lorenzo Colitticd675292021-02-04 17:32:07 +0900954
Paul Jensenc0618a62014-12-10 15:12:18 -0500955 /**
956 * A kludge to facilitate static access where a Context pointer isn't available, like in the
957 * case of the static set/getProcessDefaultNetwork methods and from the Network class.
958 * TODO: Remove this after deprecating the static methods in favor of non-static methods or
959 * methods that take a Context argument.
960 */
961 private static ConnectivityManager sInstance;
The Android Open Source Project28527d22009-03-03 19:31:44 -0800962
Lorenzo Colitti2780e4b2015-07-29 11:41:21 +0900963 private final Context mContext;
964
Amos Bianchia9b415a2020-03-04 11:07:38 -0800965 private final TetheringManager mTetheringManager;
Dianne Hackborn5ac88162014-02-26 16:20:52 -0800966
Robert Greenwaltfaa4b402013-02-15 10:56:35 -0800967 /**
968 * Tests if a given integer represents a valid network type.
969 * @param networkType the type to be tested
970 * @return a boolean. {@code true} if the type is valid, else {@code false}
Paul Jensenbbb61092015-05-06 10:42:25 -0400971 * @deprecated All APIs accepting a network type are deprecated. There should be no need to
972 * validate a network type.
Robert Greenwaltfaa4b402013-02-15 10:56:35 -0800973 */
Aurimas Liutikase80a0eb2016-05-24 15:22:55 -0700974 @Deprecated
Jeff Sharkey21062e72011-05-28 20:56:34 -0700975 public static boolean isNetworkTypeValid(int networkType) {
Hugo Benichiad353f42017-06-20 14:07:59 +0900976 return MIN_NETWORK_TYPE <= networkType && networkType <= MAX_NETWORK_TYPE;
The Android Open Source Project28527d22009-03-03 19:31:44 -0800977 }
978
Robert Greenwaltfaa4b402013-02-15 10:56:35 -0800979 /**
980 * Returns a non-localized string representing a given network type.
981 * ONLY used for debugging output.
982 * @param type the type needing naming
983 * @return a String for the given type, or a string version of the type ("87")
984 * if no name is known.
Chalard Jean7eaf3b12018-03-08 13:54:53 +0900985 * @deprecated Types are deprecated. Use {@link NetworkCapabilities} instead.
Robert Greenwaltfaa4b402013-02-15 10:56:35 -0800986 * {@hide}
987 */
Chalard Jean7eaf3b12018-03-08 13:54:53 +0900988 @Deprecated
Mathew Inwoode1a17ba2020-11-04 09:29:36 +0000989 @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.R, trackingBug = 170729553)
Jeff Sharkey21062e72011-05-28 20:56:34 -0700990 public static String getNetworkTypeName(int type) {
991 switch (type) {
Hugo Benichiad353f42017-06-20 14:07:59 +0900992 case TYPE_NONE:
993 return "NONE";
Jeff Sharkey21062e72011-05-28 20:56:34 -0700994 case TYPE_MOBILE:
995 return "MOBILE";
996 case TYPE_WIFI:
997 return "WIFI";
998 case TYPE_MOBILE_MMS:
999 return "MOBILE_MMS";
1000 case TYPE_MOBILE_SUPL:
1001 return "MOBILE_SUPL";
1002 case TYPE_MOBILE_DUN:
1003 return "MOBILE_DUN";
1004 case TYPE_MOBILE_HIPRI:
1005 return "MOBILE_HIPRI";
1006 case TYPE_WIMAX:
1007 return "WIMAX";
1008 case TYPE_BLUETOOTH:
1009 return "BLUETOOTH";
1010 case TYPE_DUMMY:
1011 return "DUMMY";
1012 case TYPE_ETHERNET:
1013 return "ETHERNET";
1014 case TYPE_MOBILE_FOTA:
1015 return "MOBILE_FOTA";
1016 case TYPE_MOBILE_IMS:
1017 return "MOBILE_IMS";
1018 case TYPE_MOBILE_CBS:
1019 return "MOBILE_CBS";
repo syncf5de5572011-07-29 23:55:49 -07001020 case TYPE_WIFI_P2P:
1021 return "WIFI_P2P";
Wink Saville512c2202013-07-29 15:00:57 -07001022 case TYPE_MOBILE_IA:
1023 return "MOBILE_IA";
Ram693d07a2014-06-26 11:03:44 -07001024 case TYPE_MOBILE_EMERGENCY:
1025 return "MOBILE_EMERGENCY";
Hui Lu865b70d2014-01-15 11:05:36 -05001026 case TYPE_PROXY:
1027 return "PROXY";
Erik Kline137fadc2014-11-19 17:23:41 +09001028 case TYPE_VPN:
1029 return "VPN";
Jeff Sharkey21062e72011-05-28 20:56:34 -07001030 default:
1031 return Integer.toString(type);
1032 }
1033 }
1034
Robert Greenwaltfaa4b402013-02-15 10:56:35 -08001035 /**
Aaron Huang96011892020-06-27 07:18:23 +08001036 * @hide
Aaron Huang96011892020-06-27 07:18:23 +08001037 */
lucaslin7fc930b2021-03-17 14:16:01 +08001038 @SystemApi(client = MODULE_LIBRARIES)
Aaron Huang96011892020-06-27 07:18:23 +08001039 public void systemReady() {
1040 try {
1041 mService.systemReady();
1042 } catch (RemoteException e) {
1043 throw e.rethrowFromSystemServer();
1044 }
1045 }
1046
1047 /**
Robert Greenwaltfaa4b402013-02-15 10:56:35 -08001048 * Checks if a given type uses the cellular data connection.
1049 * This should be replaced in the future by a network property.
1050 * @param networkType the type to check
1051 * @return a boolean - {@code true} if uses cellular network, else {@code false}
Chalard Jean7eaf3b12018-03-08 13:54:53 +09001052 * @deprecated Types are deprecated. Use {@link NetworkCapabilities} instead.
Robert Greenwaltfaa4b402013-02-15 10:56:35 -08001053 * {@hide}
1054 */
Chalard Jean7eaf3b12018-03-08 13:54:53 +09001055 @Deprecated
Chalard Jeana3b77512019-04-09 15:46:21 +09001056 @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P, trackingBug = 130143562)
Jeff Sharkey21062e72011-05-28 20:56:34 -07001057 public static boolean isNetworkTypeMobile(int networkType) {
1058 switch (networkType) {
1059 case TYPE_MOBILE:
1060 case TYPE_MOBILE_MMS:
1061 case TYPE_MOBILE_SUPL:
1062 case TYPE_MOBILE_DUN:
1063 case TYPE_MOBILE_HIPRI:
1064 case TYPE_MOBILE_FOTA:
1065 case TYPE_MOBILE_IMS:
1066 case TYPE_MOBILE_CBS:
Wink Saville512c2202013-07-29 15:00:57 -07001067 case TYPE_MOBILE_IA:
Ram693d07a2014-06-26 11:03:44 -07001068 case TYPE_MOBILE_EMERGENCY:
Jeff Sharkey21062e72011-05-28 20:56:34 -07001069 return true;
1070 default:
1071 return false;
1072 }
1073 }
1074
Robert Greenwaltfaa4b402013-02-15 10:56:35 -08001075 /**
Jeff Sharkey79f3e022013-06-04 12:29:00 -07001076 * Checks if the given network type is backed by a Wi-Fi radio.
1077 *
Chalard Jean7eaf3b12018-03-08 13:54:53 +09001078 * @deprecated Types are deprecated. Use {@link NetworkCapabilities} instead.
Jeff Sharkey79f3e022013-06-04 12:29:00 -07001079 * @hide
1080 */
Chalard Jean7eaf3b12018-03-08 13:54:53 +09001081 @Deprecated
Jeff Sharkey79f3e022013-06-04 12:29:00 -07001082 public static boolean isNetworkTypeWifi(int networkType) {
1083 switch (networkType) {
1084 case TYPE_WIFI:
1085 case TYPE_WIFI_P2P:
1086 return true;
1087 default:
1088 return false;
1089 }
1090 }
1091
1092 /**
Chalard Jeanfa45a682021-02-25 17:23:40 +09001093 * Preference for {@link #setNetworkPreferenceForUser(UserHandle, int, Executor, Runnable)}.
1094 * Specify that the traffic for this user should by follow the default rules.
1095 * @hide
1096 */
Chalard Jeanb43f3962021-03-17 14:33:24 +09001097 @SystemApi(client = MODULE_LIBRARIES)
Chalard Jeanfa45a682021-02-25 17:23:40 +09001098 public static final int PROFILE_NETWORK_PREFERENCE_DEFAULT = 0;
1099
1100 /**
1101 * Preference for {@link #setNetworkPreferenceForUser(UserHandle, int, Executor, Runnable)}.
1102 * Specify that the traffic for this user should by default go on a network with
1103 * {@link NetworkCapabilities#NET_CAPABILITY_ENTERPRISE}, and on the system default network
1104 * if no such network is available.
1105 * @hide
1106 */
Chalard Jeanb43f3962021-03-17 14:33:24 +09001107 @SystemApi(client = MODULE_LIBRARIES)
Chalard Jeanfa45a682021-02-25 17:23:40 +09001108 public static final int PROFILE_NETWORK_PREFERENCE_ENTERPRISE = 1;
1109
1110 /** @hide */
1111 @Retention(RetentionPolicy.SOURCE)
1112 @IntDef(value = {
1113 PROFILE_NETWORK_PREFERENCE_DEFAULT,
1114 PROFILE_NETWORK_PREFERENCE_ENTERPRISE
1115 })
1116 public @interface ProfileNetworkPreference {
1117 }
1118
1119 /**
Robert Greenwaltfaa4b402013-02-15 10:56:35 -08001120 * Specifies the preferred network type. When the device has more
1121 * than one type available the preferred network type will be used.
Robert Greenwaltfaa4b402013-02-15 10:56:35 -08001122 *
1123 * @param preference the network type to prefer over all others. It is
1124 * unspecified what happens to the old preferred network in the
1125 * overall ordering.
Robert Greenwaltf3017f72014-05-18 23:07:25 -07001126 * @deprecated Functionality has been removed as it no longer makes sense,
1127 * with many more than two networks - we'd need an array to express
1128 * preference. Instead we use dynamic network properties of
1129 * the networks to describe their precedence.
Robert Greenwaltfaa4b402013-02-15 10:56:35 -08001130 */
Aurimas Liutikase80a0eb2016-05-24 15:22:55 -07001131 @Deprecated
The Android Open Source Project28527d22009-03-03 19:31:44 -08001132 public void setNetworkPreference(int preference) {
The Android Open Source Project28527d22009-03-03 19:31:44 -08001133 }
1134
Robert Greenwaltfaa4b402013-02-15 10:56:35 -08001135 /**
1136 * Retrieves the current preferred network type.
Robert Greenwaltfaa4b402013-02-15 10:56:35 -08001137 *
1138 * @return an integer representing the preferred network type
1139 *
Robert Greenwaltf3017f72014-05-18 23:07:25 -07001140 * @deprecated Functionality has been removed as it no longer makes sense,
1141 * with many more than two networks - we'd need an array to express
1142 * preference. Instead we use dynamic network properties of
1143 * the networks to describe their precedence.
Robert Greenwaltfaa4b402013-02-15 10:56:35 -08001144 */
Aurimas Liutikase80a0eb2016-05-24 15:22:55 -07001145 @Deprecated
Jeff Sharkey656584a2017-04-24 11:18:03 -06001146 @RequiresPermission(android.Manifest.permission.ACCESS_NETWORK_STATE)
The Android Open Source Project28527d22009-03-03 19:31:44 -08001147 public int getNetworkPreference() {
Robert Greenwaltf3017f72014-05-18 23:07:25 -07001148 return TYPE_NONE;
The Android Open Source Project28527d22009-03-03 19:31:44 -08001149 }
1150
Scott Mainf58b7d82011-10-06 19:02:28 -07001151 /**
Robert Greenwaltfaa4b402013-02-15 10:56:35 -08001152 * Returns details about the currently active default data network. When
1153 * connected, this network is the default route for outgoing connections.
1154 * You should always check {@link NetworkInfo#isConnected()} before initiating
1155 * network traffic. This may return {@code null} when there is no default
1156 * network.
Chalard Jean96d10a72018-03-29 17:45:24 +09001157 * Note that if the default network is a VPN, this method will return the
1158 * NetworkInfo for one of its underlying networks instead, or null if the
1159 * VPN agent did not specify any. Apps interested in learning about VPNs
1160 * should use {@link #getNetworkInfo(android.net.Network)} instead.
Robert Greenwaltfaa4b402013-02-15 10:56:35 -08001161 *
1162 * @return a {@link NetworkInfo} object for the current default network
Paul Jensenbd2d3782015-02-13 14:18:39 -05001163 * or {@code null} if no default network is currently active
junyulai9826e7f2018-12-13 12:47:51 +08001164 * @deprecated See {@link NetworkInfo}.
Jeff Sharkeyd00b1302012-04-12 18:34:54 -07001165 */
junyulai9826e7f2018-12-13 12:47:51 +08001166 @Deprecated
Jeff Sharkey656584a2017-04-24 11:18:03 -06001167 @RequiresPermission(android.Manifest.permission.ACCESS_NETWORK_STATE)
Chalard Jean158702d2019-01-07 19:26:34 +09001168 @Nullable
The Android Open Source Project28527d22009-03-03 19:31:44 -08001169 public NetworkInfo getActiveNetworkInfo() {
1170 try {
1171 return mService.getActiveNetworkInfo();
1172 } catch (RemoteException e) {
Jeff Sharkeyc78f85c2016-03-01 19:27:23 -07001173 throw e.rethrowFromSystemServer();
The Android Open Source Project28527d22009-03-03 19:31:44 -08001174 }
1175 }
1176
Robert Greenwaltfaa4b402013-02-15 10:56:35 -08001177 /**
Paul Jensen1f567382015-02-13 14:18:39 -05001178 * Returns a {@link Network} object corresponding to the currently active
1179 * default data network. In the event that the current active default data
1180 * network disconnects, the returned {@code Network} object will no longer
1181 * be usable. This will return {@code null} when there is no default
1182 * network.
1183 *
1184 * @return a {@link Network} object for the current default network or
1185 * {@code null} if no default network is currently active
Paul Jensen1f567382015-02-13 14:18:39 -05001186 */
Jeff Sharkey656584a2017-04-24 11:18:03 -06001187 @RequiresPermission(android.Manifest.permission.ACCESS_NETWORK_STATE)
Chalard Jean158702d2019-01-07 19:26:34 +09001188 @Nullable
Paul Jensen1f567382015-02-13 14:18:39 -05001189 public Network getActiveNetwork() {
1190 try {
1191 return mService.getActiveNetwork();
1192 } catch (RemoteException e) {
Jeff Sharkeyc78f85c2016-03-01 19:27:23 -07001193 throw e.rethrowFromSystemServer();
Paul Jensen1f567382015-02-13 14:18:39 -05001194 }
1195 }
1196
1197 /**
Robin Lee5b52bef2016-03-24 12:07:00 +00001198 * Returns a {@link Network} object corresponding to the currently active
1199 * default data network for a specific UID. In the event that the default data
1200 * network disconnects, the returned {@code Network} object will no longer
1201 * be usable. This will return {@code null} when there is no default
1202 * network for the UID.
Robin Lee5b52bef2016-03-24 12:07:00 +00001203 *
1204 * @return a {@link Network} object for the current default network for the
1205 * given UID or {@code null} if no default network is currently active
lifr42a60c22021-03-11 20:11:09 +08001206 * TODO: b/183465229 Cleanup getActiveNetworkForUid once b/165835257 is fixed
Robin Lee5b52bef2016-03-24 12:07:00 +00001207 */
paulhu8e96a752019-08-12 16:25:11 +08001208 @RequiresPermission(android.Manifest.permission.NETWORK_STACK)
Chalard Jean158702d2019-01-07 19:26:34 +09001209 @Nullable
Robin Lee5b52bef2016-03-24 12:07:00 +00001210 public Network getActiveNetworkForUid(int uid) {
Jeff Sharkey7dbf83d2016-04-28 15:33:18 -06001211 return getActiveNetworkForUid(uid, false);
1212 }
1213
1214 /** {@hide} */
1215 public Network getActiveNetworkForUid(int uid, boolean ignoreBlocked) {
Robin Lee5b52bef2016-03-24 12:07:00 +00001216 try {
Jeff Sharkey7dbf83d2016-04-28 15:33:18 -06001217 return mService.getActiveNetworkForUid(uid, ignoreBlocked);
Robin Lee5b52bef2016-03-24 12:07:00 +00001218 } catch (RemoteException e) {
1219 throw e.rethrowFromSystemServer();
1220 }
1221 }
1222
1223 /**
Lorenzo Colitti3f54f102020-12-12 00:51:11 +09001224 * Adds or removes a requirement for given UID ranges to use the VPN.
1225 *
1226 * If set to {@code true}, informs the system that the UIDs in the specified ranges must not
1227 * have any connectivity except if a VPN is connected and applies to the UIDs, or if the UIDs
1228 * otherwise have permission to bypass the VPN (e.g., because they have the
1229 * {@link android.Manifest.permission.CONNECTIVITY_USE_RESTRICTED_NETWORKS} permission, or when
1230 * using a socket protected by a method such as {@link VpnService#protect(DatagramSocket)}. If
1231 * set to {@code false}, a previously-added restriction is removed.
1232 * <p>
1233 * Each of the UID ranges specified by this method is added and removed as is, and no processing
1234 * is performed on the ranges to de-duplicate, merge, split, or intersect them. In order to
1235 * remove a previously-added range, the exact range must be removed as is.
1236 * <p>
1237 * The changes are applied asynchronously and may not have been applied by the time the method
1238 * returns. Apps will be notified about any changes that apply to them via
1239 * {@link NetworkCallback#onBlockedStatusChanged} callbacks called after the changes take
1240 * effect.
1241 * <p>
1242 * This method should be called only by the VPN code.
1243 *
1244 * @param ranges the UID ranges to restrict
1245 * @param requireVpn whether the specified UID ranges must use a VPN
1246 *
Lorenzo Colitti3f54f102020-12-12 00:51:11 +09001247 * @hide
1248 */
1249 @RequiresPermission(anyOf = {
1250 NetworkStack.PERMISSION_MAINLINE_NETWORK_STACK,
lucasline257bce2021-03-22 11:51:27 +08001251 android.Manifest.permission.NETWORK_STACK,
1252 android.Manifest.permission.NETWORK_SETTINGS})
1253 @SystemApi(client = MODULE_LIBRARIES)
Lorenzo Colitti3f54f102020-12-12 00:51:11 +09001254 public void setRequireVpnForUids(boolean requireVpn,
1255 @NonNull Collection<Range<Integer>> ranges) {
1256 Objects.requireNonNull(ranges);
1257 // The Range class is not parcelable. Convert to UidRange, which is what is used internally.
1258 // This method is not necessarily expected to be used outside the system server, so
1259 // parceling may not be necessary, but it could be used out-of-process, e.g., by the network
1260 // stack process, or by tests.
1261 UidRange[] rangesArray = new UidRange[ranges.size()];
1262 int index = 0;
1263 for (Range<Integer> range : ranges) {
1264 rangesArray[index++] = new UidRange(range.getLower(), range.getUpper());
1265 }
1266 try {
1267 mService.setRequireVpnForUids(requireVpn, rangesArray);
1268 } catch (RemoteException e) {
1269 throw e.rethrowFromSystemServer();
1270 }
1271 }
1272
1273 /**
Lorenzo Colittibcd692f2021-01-15 01:29:01 +09001274 * Informs ConnectivityService of whether the legacy lockdown VPN, as implemented by
1275 * LockdownVpnTracker, is in use. This is deprecated for new devices starting from Android 12
1276 * but is still supported for backwards compatibility.
1277 * <p>
1278 * This type of VPN is assumed always to use the system default network, and must always declare
1279 * exactly one underlying network, which is the network that was the default when the VPN
1280 * connected.
1281 * <p>
1282 * Calling this method with {@code true} enables legacy behaviour, specifically:
1283 * <ul>
1284 * <li>Any VPN that applies to userId 0 behaves specially with respect to deprecated
1285 * {@link #CONNECTIVITY_ACTION} broadcasts. Any such broadcasts will have the state in the
1286 * {@link #EXTRA_NETWORK_INFO} replaced by state of the VPN network. Also, any time the VPN
1287 * connects, a {@link #CONNECTIVITY_ACTION} broadcast will be sent for the network
1288 * underlying the VPN.</li>
1289 * <li>Deprecated APIs that return {@link NetworkInfo} objects will have their state
1290 * similarly replaced by the VPN network state.</li>
1291 * <li>Information on current network interfaces passed to NetworkStatsService will not
1292 * include any VPN interfaces.</li>
1293 * </ul>
1294 *
1295 * @param enabled whether legacy lockdown VPN is enabled or disabled
1296 *
Lorenzo Colittibcd692f2021-01-15 01:29:01 +09001297 * @hide
1298 */
1299 @RequiresPermission(anyOf = {
1300 NetworkStack.PERMISSION_MAINLINE_NETWORK_STACK,
lucasline257bce2021-03-22 11:51:27 +08001301 android.Manifest.permission.NETWORK_STACK,
Lorenzo Colittibcd692f2021-01-15 01:29:01 +09001302 android.Manifest.permission.NETWORK_SETTINGS})
lucasline257bce2021-03-22 11:51:27 +08001303 @SystemApi(client = MODULE_LIBRARIES)
Lorenzo Colittibcd692f2021-01-15 01:29:01 +09001304 public void setLegacyLockdownVpnEnabled(boolean enabled) {
1305 try {
1306 mService.setLegacyLockdownVpnEnabled(enabled);
1307 } catch (RemoteException e) {
1308 throw e.rethrowFromSystemServer();
1309 }
1310 }
1311
1312 /**
Robert Greenwaltfaa4b402013-02-15 10:56:35 -08001313 * Returns details about the currently active default data network
1314 * for a given uid. This is for internal use only to avoid spying
1315 * other apps.
1316 *
1317 * @return a {@link NetworkInfo} object for the current default network
1318 * for the given uid or {@code null} if no default network is
1319 * available for the specified uid.
1320 *
Robert Greenwaltfaa4b402013-02-15 10:56:35 -08001321 * {@hide}
1322 */
paulhu8e96a752019-08-12 16:25:11 +08001323 @RequiresPermission(android.Manifest.permission.NETWORK_STACK)
Mathew Inwoode1a17ba2020-11-04 09:29:36 +00001324 @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.R, trackingBug = 170729553)
Jeff Sharkey921ebf22011-05-19 17:12:49 -07001325 public NetworkInfo getActiveNetworkInfoForUid(int uid) {
Jeff Sharkey7dbf83d2016-04-28 15:33:18 -06001326 return getActiveNetworkInfoForUid(uid, false);
1327 }
1328
1329 /** {@hide} */
1330 public NetworkInfo getActiveNetworkInfoForUid(int uid, boolean ignoreBlocked) {
Jeff Sharkey921ebf22011-05-19 17:12:49 -07001331 try {
Jeff Sharkey7dbf83d2016-04-28 15:33:18 -06001332 return mService.getActiveNetworkInfoForUid(uid, ignoreBlocked);
Jeff Sharkey921ebf22011-05-19 17:12:49 -07001333 } catch (RemoteException e) {
Jeff Sharkeyc78f85c2016-03-01 19:27:23 -07001334 throw e.rethrowFromSystemServer();
Jeff Sharkey921ebf22011-05-19 17:12:49 -07001335 }
1336 }
1337
Robert Greenwaltfaa4b402013-02-15 10:56:35 -08001338 /**
1339 * Returns connection status information about a particular
1340 * network type.
1341 *
1342 * @param networkType integer specifying which networkType in
1343 * which you're interested.
1344 * @return a {@link NetworkInfo} object for the requested
1345 * network type or {@code null} if the type is not
Chalard Jean96d10a72018-03-29 17:45:24 +09001346 * supported by the device. If {@code networkType} is
1347 * TYPE_VPN and a VPN is active for the calling app,
1348 * then this method will try to return one of the
1349 * underlying networks for the VPN or null if the
1350 * VPN agent didn't specify any.
Robert Greenwaltfaa4b402013-02-15 10:56:35 -08001351 *
Paul Jensendda8e592015-03-18 12:23:02 -04001352 * @deprecated This method does not support multiple connected networks
1353 * of the same type. Use {@link #getAllNetworks} and
1354 * {@link #getNetworkInfo(android.net.Network)} instead.
Robert Greenwaltfaa4b402013-02-15 10:56:35 -08001355 */
Aurimas Liutikase80a0eb2016-05-24 15:22:55 -07001356 @Deprecated
Jeff Sharkey656584a2017-04-24 11:18:03 -06001357 @RequiresPermission(android.Manifest.permission.ACCESS_NETWORK_STATE)
Chalard Jean158702d2019-01-07 19:26:34 +09001358 @Nullable
The Android Open Source Project28527d22009-03-03 19:31:44 -08001359 public NetworkInfo getNetworkInfo(int networkType) {
1360 try {
1361 return mService.getNetworkInfo(networkType);
1362 } catch (RemoteException e) {
Jeff Sharkeyc78f85c2016-03-01 19:27:23 -07001363 throw e.rethrowFromSystemServer();
The Android Open Source Project28527d22009-03-03 19:31:44 -08001364 }
1365 }
1366
Robert Greenwaltfaa4b402013-02-15 10:56:35 -08001367 /**
Robert Greenwalt02fe11e2014-06-23 11:40:00 -07001368 * Returns connection status information about a particular
1369 * Network.
1370 *
1371 * @param network {@link Network} specifying which network
1372 * in which you're interested.
1373 * @return a {@link NetworkInfo} object for the requested
1374 * network or {@code null} if the {@code Network}
1375 * is not valid.
junyulai9826e7f2018-12-13 12:47:51 +08001376 * @deprecated See {@link NetworkInfo}.
Robert Greenwalt02fe11e2014-06-23 11:40:00 -07001377 */
junyulai9826e7f2018-12-13 12:47:51 +08001378 @Deprecated
Jeff Sharkey656584a2017-04-24 11:18:03 -06001379 @RequiresPermission(android.Manifest.permission.ACCESS_NETWORK_STATE)
Chalard Jean158702d2019-01-07 19:26:34 +09001380 @Nullable
1381 public NetworkInfo getNetworkInfo(@Nullable Network network) {
Jeff Sharkey7dbf83d2016-04-28 15:33:18 -06001382 return getNetworkInfoForUid(network, Process.myUid(), false);
1383 }
1384
1385 /** {@hide} */
1386 public NetworkInfo getNetworkInfoForUid(Network network, int uid, boolean ignoreBlocked) {
Robert Greenwalt02fe11e2014-06-23 11:40:00 -07001387 try {
Jeff Sharkey7dbf83d2016-04-28 15:33:18 -06001388 return mService.getNetworkInfoForUid(network, uid, ignoreBlocked);
Robert Greenwalt02fe11e2014-06-23 11:40:00 -07001389 } catch (RemoteException e) {
Jeff Sharkeyc78f85c2016-03-01 19:27:23 -07001390 throw e.rethrowFromSystemServer();
Robert Greenwalt02fe11e2014-06-23 11:40:00 -07001391 }
1392 }
1393
1394 /**
Robert Greenwaltfaa4b402013-02-15 10:56:35 -08001395 * Returns connection status information about all network
1396 * types supported by the device.
1397 *
1398 * @return an array of {@link NetworkInfo} objects. Check each
1399 * {@link NetworkInfo#getType} for which type each applies.
1400 *
Paul Jensendda8e592015-03-18 12:23:02 -04001401 * @deprecated This method does not support multiple connected networks
1402 * of the same type. Use {@link #getAllNetworks} and
1403 * {@link #getNetworkInfo(android.net.Network)} instead.
Robert Greenwaltfaa4b402013-02-15 10:56:35 -08001404 */
Aurimas Liutikase80a0eb2016-05-24 15:22:55 -07001405 @Deprecated
Jeff Sharkey656584a2017-04-24 11:18:03 -06001406 @RequiresPermission(android.Manifest.permission.ACCESS_NETWORK_STATE)
Chalard Jean158702d2019-01-07 19:26:34 +09001407 @NonNull
The Android Open Source Project28527d22009-03-03 19:31:44 -08001408 public NetworkInfo[] getAllNetworkInfo() {
1409 try {
1410 return mService.getAllNetworkInfo();
1411 } catch (RemoteException e) {
Jeff Sharkeyc78f85c2016-03-01 19:27:23 -07001412 throw e.rethrowFromSystemServer();
The Android Open Source Project28527d22009-03-03 19:31:44 -08001413 }
1414 }
1415
Robert Greenwaltfaa4b402013-02-15 10:56:35 -08001416 /**
junyulaiebd15162021-03-03 12:09:05 +08001417 * Return a list of {@link NetworkStateSnapshot}s, one for each network that is currently
1418 * connected.
1419 * @hide
1420 */
1421 @SystemApi(client = MODULE_LIBRARIES)
1422 @RequiresPermission(anyOf = {
1423 NetworkStack.PERMISSION_MAINLINE_NETWORK_STACK,
1424 android.Manifest.permission.NETWORK_STACK,
1425 android.Manifest.permission.NETWORK_SETTINGS})
1426 @NonNull
1427 public List<NetworkStateSnapshot> getAllNetworkStateSnapshot() {
1428 try {
1429 return mService.getAllNetworkStateSnapshot();
1430 } catch (RemoteException e) {
1431 throw e.rethrowFromSystemServer();
1432 }
1433 }
1434
1435 /**
Lorenzo Colitti860a7aa2014-08-22 17:10:50 -07001436 * Returns the {@link Network} object currently serving a given type, or
1437 * null if the given type is not connected.
1438 *
Lorenzo Colitti860a7aa2014-08-22 17:10:50 -07001439 * @hide
Paul Jensendda8e592015-03-18 12:23:02 -04001440 * @deprecated This method does not support multiple connected networks
1441 * of the same type. Use {@link #getAllNetworks} and
1442 * {@link #getNetworkInfo(android.net.Network)} instead.
Lorenzo Colitti860a7aa2014-08-22 17:10:50 -07001443 */
Aurimas Liutikase80a0eb2016-05-24 15:22:55 -07001444 @Deprecated
Jeff Sharkey656584a2017-04-24 11:18:03 -06001445 @RequiresPermission(android.Manifest.permission.ACCESS_NETWORK_STATE)
Mathew Inwoodc5b9bec2018-08-08 14:52:47 +01001446 @UnsupportedAppUsage
Lorenzo Colitti860a7aa2014-08-22 17:10:50 -07001447 public Network getNetworkForType(int networkType) {
1448 try {
1449 return mService.getNetworkForType(networkType);
1450 } catch (RemoteException e) {
Jeff Sharkeyc78f85c2016-03-01 19:27:23 -07001451 throw e.rethrowFromSystemServer();
Lorenzo Colitti860a7aa2014-08-22 17:10:50 -07001452 }
1453 }
1454
1455 /**
Robert Greenwalt02fe11e2014-06-23 11:40:00 -07001456 * Returns an array of all {@link Network} currently tracked by the
1457 * framework.
Paul Jensen08f9dbb2015-05-06 11:10:18 -04001458 *
1459 * @return an array of {@link Network} objects.
Robert Greenwalt02fe11e2014-06-23 11:40:00 -07001460 */
Jeff Sharkey656584a2017-04-24 11:18:03 -06001461 @RequiresPermission(android.Manifest.permission.ACCESS_NETWORK_STATE)
Chalard Jean158702d2019-01-07 19:26:34 +09001462 @NonNull
Robert Greenwalt02fe11e2014-06-23 11:40:00 -07001463 public Network[] getAllNetworks() {
1464 try {
1465 return mService.getAllNetworks();
1466 } catch (RemoteException e) {
Jeff Sharkeyc78f85c2016-03-01 19:27:23 -07001467 throw e.rethrowFromSystemServer();
Robert Greenwalt02fe11e2014-06-23 11:40:00 -07001468 }
1469 }
1470
1471 /**
Roshan Pius951c0032020-12-22 15:10:42 -08001472 * Returns an array of {@link NetworkCapabilities} objects, representing
Lorenzo Colitti79bd2e22014-11-28 11:21:30 +09001473 * the Networks that applications run by the given user will use by default.
1474 * @hide
1475 */
Mathew Inwoodc5b9bec2018-08-08 14:52:47 +01001476 @UnsupportedAppUsage
Lorenzo Colitti79bd2e22014-11-28 11:21:30 +09001477 public NetworkCapabilities[] getDefaultNetworkCapabilitiesForUser(int userId) {
1478 try {
Qingxi Lib2748102020-01-08 12:51:49 -08001479 return mService.getDefaultNetworkCapabilitiesForUser(
Roshan Piusaa24fde2020-12-17 14:53:09 -08001480 userId, mContext.getOpPackageName(), getAttributionTag());
Lorenzo Colitti79bd2e22014-11-28 11:21:30 +09001481 } catch (RemoteException e) {
Jeff Sharkeyc78f85c2016-03-01 19:27:23 -07001482 throw e.rethrowFromSystemServer();
Lorenzo Colitti79bd2e22014-11-28 11:21:30 +09001483 }
1484 }
1485
1486 /**
Robert Greenwaltfaa4b402013-02-15 10:56:35 -08001487 * Returns the IP information for the current default network.
1488 *
1489 * @return a {@link LinkProperties} object describing the IP info
1490 * for the current default network, or {@code null} if there
1491 * is no current default network.
1492 *
Robert Greenwaltfaa4b402013-02-15 10:56:35 -08001493 * {@hide}
Chalard Jean97021a12019-01-11 16:47:53 +09001494 * @deprecated please use {@link #getLinkProperties(Network)} on the return
1495 * value of {@link #getActiveNetwork()} instead. In particular,
1496 * this method will return non-null LinkProperties even if the
1497 * app is blocked by policy from using this network.
Robert Greenwaltfaa4b402013-02-15 10:56:35 -08001498 */
Jeff Sharkey656584a2017-04-24 11:18:03 -06001499 @RequiresPermission(android.Manifest.permission.ACCESS_NETWORK_STATE)
Chalard Jean97021a12019-01-11 16:47:53 +09001500 @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P, trackingBug = 109783091)
Robert Greenwalt9f0ee4f2010-09-14 09:18:02 -07001501 public LinkProperties getActiveLinkProperties() {
1502 try {
1503 return mService.getActiveLinkProperties();
1504 } catch (RemoteException e) {
Jeff Sharkeyc78f85c2016-03-01 19:27:23 -07001505 throw e.rethrowFromSystemServer();
Robert Greenwalt9f0ee4f2010-09-14 09:18:02 -07001506 }
1507 }
1508
Robert Greenwaltfaa4b402013-02-15 10:56:35 -08001509 /**
1510 * Returns the IP information for a given network type.
1511 *
1512 * @param networkType the network type of interest.
1513 * @return a {@link LinkProperties} object describing the IP info
1514 * for the given networkType, or {@code null} if there is
1515 * no current default network.
1516 *
Robert Greenwaltfaa4b402013-02-15 10:56:35 -08001517 * {@hide}
Paul Jensendda8e592015-03-18 12:23:02 -04001518 * @deprecated This method does not support multiple connected networks
1519 * of the same type. Use {@link #getAllNetworks},
1520 * {@link #getNetworkInfo(android.net.Network)}, and
1521 * {@link #getLinkProperties(android.net.Network)} instead.
Robert Greenwaltfaa4b402013-02-15 10:56:35 -08001522 */
Aurimas Liutikase80a0eb2016-05-24 15:22:55 -07001523 @Deprecated
Jeff Sharkey656584a2017-04-24 11:18:03 -06001524 @RequiresPermission(android.Manifest.permission.ACCESS_NETWORK_STATE)
Chalard Jeana3b77512019-04-09 15:46:21 +09001525 @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P, trackingBug = 130143562)
Robert Greenwalt9f0ee4f2010-09-14 09:18:02 -07001526 public LinkProperties getLinkProperties(int networkType) {
1527 try {
Robert Greenwaltf99b8392014-03-26 16:47:06 -07001528 return mService.getLinkPropertiesForType(networkType);
1529 } catch (RemoteException e) {
Jeff Sharkeyc78f85c2016-03-01 19:27:23 -07001530 throw e.rethrowFromSystemServer();
Robert Greenwaltf99b8392014-03-26 16:47:06 -07001531 }
1532 }
1533
Robert Greenwaltf3017f72014-05-18 23:07:25 -07001534 /**
1535 * Get the {@link LinkProperties} for the given {@link Network}. This
1536 * will return {@code null} if the network is unknown.
1537 *
1538 * @param network The {@link Network} object identifying the network in question.
1539 * @return The {@link LinkProperties} for the network, or {@code null}.
Paul Jensen08f9dbb2015-05-06 11:10:18 -04001540 */
Jeff Sharkey656584a2017-04-24 11:18:03 -06001541 @RequiresPermission(android.Manifest.permission.ACCESS_NETWORK_STATE)
Chalard Jean158702d2019-01-07 19:26:34 +09001542 @Nullable
1543 public LinkProperties getLinkProperties(@Nullable Network network) {
Robert Greenwaltf99b8392014-03-26 16:47:06 -07001544 try {
1545 return mService.getLinkProperties(network);
1546 } catch (RemoteException e) {
Jeff Sharkeyc78f85c2016-03-01 19:27:23 -07001547 throw e.rethrowFromSystemServer();
Robert Greenwaltf99b8392014-03-26 16:47:06 -07001548 }
1549 }
1550
Robert Greenwaltf3017f72014-05-18 23:07:25 -07001551 /**
Roshan Pius951c0032020-12-22 15:10:42 -08001552 * Get the {@link NetworkCapabilities} for the given {@link Network}. This
Robert Greenwaltf3017f72014-05-18 23:07:25 -07001553 * will return {@code null} if the network is unknown.
1554 *
Roshan Pius951c0032020-12-22 15:10:42 -08001555 * This will remove any location sensitive data in {@link TransportInfo} embedded in
1556 * {@link NetworkCapabilities#getTransportInfo()}. Some transport info instances like
1557 * {@link android.net.wifi.WifiInfo} contain location sensitive information. Retrieving
1558 * this location sensitive information (subject to app's location permissions) will be
1559 * noted by system. To include any location sensitive data in {@link TransportInfo},
1560 * use a {@link NetworkCallback} with
1561 * {@link NetworkCallback#FLAG_INCLUDE_LOCATION_INFO} flag.
1562 *
Robert Greenwaltf3017f72014-05-18 23:07:25 -07001563 * @param network The {@link Network} object identifying the network in question.
Roshan Pius951c0032020-12-22 15:10:42 -08001564 * @return The {@link NetworkCapabilities} for the network, or {@code null}.
Robert Greenwaltf3017f72014-05-18 23:07:25 -07001565 */
Jeff Sharkey656584a2017-04-24 11:18:03 -06001566 @RequiresPermission(android.Manifest.permission.ACCESS_NETWORK_STATE)
Chalard Jean158702d2019-01-07 19:26:34 +09001567 @Nullable
1568 public NetworkCapabilities getNetworkCapabilities(@Nullable Network network) {
Robert Greenwaltf99b8392014-03-26 16:47:06 -07001569 try {
Roshan Piusaa24fde2020-12-17 14:53:09 -08001570 return mService.getNetworkCapabilities(
1571 network, mContext.getOpPackageName(), getAttributionTag());
Robert Greenwalt9f0ee4f2010-09-14 09:18:02 -07001572 } catch (RemoteException e) {
Jeff Sharkeyc78f85c2016-03-01 19:27:23 -07001573 throw e.rethrowFromSystemServer();
Robert Greenwalt9f0ee4f2010-09-14 09:18:02 -07001574 }
1575 }
1576
Robert Greenwaltfaa4b402013-02-15 10:56:35 -08001577 /**
Remi NGUYEN VAN035f6532019-03-20 14:22:49 +09001578 * Gets a URL that can be used for resolving whether a captive portal is present.
Udam Sainicd645462016-01-04 12:16:14 -08001579 * 1. This URL should respond with a 204 response to a GET request to indicate no captive
1580 * portal is present.
1581 * 2. This URL must be HTTP as redirect responses are used to find captive portal
1582 * sign-in pages. Captive portals cannot respond to HTTPS requests with redirects.
1583 *
Remi NGUYEN VAN035f6532019-03-20 14:22:49 +09001584 * The system network validation may be using different strategies to detect captive portals,
1585 * so this method does not necessarily return a URL used by the system. It only returns a URL
1586 * that may be relevant for other components trying to detect captive portals.
paulhu8e96a752019-08-12 16:25:11 +08001587 *
Udam Sainicd645462016-01-04 12:16:14 -08001588 * @hide
paulhu8e96a752019-08-12 16:25:11 +08001589 * @deprecated This API returns URL which is not guaranteed to be one of the URLs used by the
1590 * system.
Udam Sainicd645462016-01-04 12:16:14 -08001591 */
paulhu8e96a752019-08-12 16:25:11 +08001592 @Deprecated
Udam Sainicd645462016-01-04 12:16:14 -08001593 @SystemApi
paulhu8e96a752019-08-12 16:25:11 +08001594 @RequiresPermission(android.Manifest.permission.NETWORK_SETTINGS)
Udam Sainicd645462016-01-04 12:16:14 -08001595 public String getCaptivePortalServerUrl() {
1596 try {
1597 return mService.getCaptivePortalServerUrl();
1598 } catch (RemoteException e) {
Jeff Sharkeyc78f85c2016-03-01 19:27:23 -07001599 throw e.rethrowFromSystemServer();
Udam Sainicd645462016-01-04 12:16:14 -08001600 }
1601 }
1602
1603 /**
The Android Open Source Project28527d22009-03-03 19:31:44 -08001604 * Tells the underlying networking system that the caller wants to
1605 * begin using the named feature. The interpretation of {@code feature}
1606 * is completely up to each networking implementation.
Lorenzo Colitti0b40c872015-10-15 16:29:00 +09001607 *
1608 * <p>This method requires the caller to hold either the
1609 * {@link android.Manifest.permission#CHANGE_NETWORK_STATE} permission
1610 * or the ability to modify system settings as determined by
1611 * {@link android.provider.Settings.System#canWrite}.</p>
1612 *
The Android Open Source Project28527d22009-03-03 19:31:44 -08001613 * @param networkType specifies which network the request pertains to
1614 * @param feature the name of the feature to be used
1615 * @return an integer value representing the outcome of the request.
1616 * The interpretation of this value is specific to each networking
1617 * implementation+feature combination, except that the value {@code -1}
1618 * always indicates failure.
Robert Greenwaltf3017f72014-05-18 23:07:25 -07001619 *
Lorenzo Colitti7f6a2bf2015-04-24 17:03:31 +09001620 * @deprecated Deprecated in favor of the cleaner
1621 * {@link #requestNetwork(NetworkRequest, NetworkCallback)} API.
Dianne Hackborn18c1d832015-07-31 10:35:34 -07001622 * In {@link VERSION_CODES#M}, and above, this method is unsupported and will
Lorenzo Colitti2780e4b2015-07-29 11:41:21 +09001623 * throw {@code UnsupportedOperationException} if called.
Lorenzo Colitti84d7f072016-12-09 18:39:30 +09001624 * @removed
The Android Open Source Project28527d22009-03-03 19:31:44 -08001625 */
Aurimas Liutikase80a0eb2016-05-24 15:22:55 -07001626 @Deprecated
The Android Open Source Project28527d22009-03-03 19:31:44 -08001627 public int startUsingNetworkFeature(int networkType, String feature) {
Lorenzo Colitti2780e4b2015-07-29 11:41:21 +09001628 checkLegacyRoutingApiAccess();
Robert Greenwalt3dc73e52014-05-15 18:07:26 -07001629 NetworkCapabilities netCap = networkCapabilitiesForFeature(networkType, feature);
1630 if (netCap == null) {
1631 Log.d(TAG, "Can't satisfy startUsingNetworkFeature for " + networkType + ", " +
1632 feature);
Chalard Jeanafaed1a2019-11-21 14:48:00 +09001633 return DEPRECATED_PHONE_CONSTANT_APN_REQUEST_FAILED;
Robert Greenwalt3dc73e52014-05-15 18:07:26 -07001634 }
1635
1636 NetworkRequest request = null;
1637 synchronized (sLegacyRequests) {
1638 LegacyRequest l = sLegacyRequests.get(netCap);
1639 if (l != null) {
1640 Log.d(TAG, "renewing startUsingNetworkFeature request " + l.networkRequest);
1641 renewRequestLocked(l);
1642 if (l.currentNetwork != null) {
Chalard Jeanafaed1a2019-11-21 14:48:00 +09001643 return DEPRECATED_PHONE_CONSTANT_APN_ALREADY_ACTIVE;
Robert Greenwalt3dc73e52014-05-15 18:07:26 -07001644 } else {
Chalard Jeanafaed1a2019-11-21 14:48:00 +09001645 return DEPRECATED_PHONE_CONSTANT_APN_REQUEST_STARTED;
Robert Greenwalt3dc73e52014-05-15 18:07:26 -07001646 }
1647 }
1648
1649 request = requestNetworkForFeatureLocked(netCap);
1650 }
1651 if (request != null) {
Robert Greenwaltb8401732014-06-20 10:58:45 -07001652 Log.d(TAG, "starting startUsingNetworkFeature for request " + request);
Chalard Jeanafaed1a2019-11-21 14:48:00 +09001653 return DEPRECATED_PHONE_CONSTANT_APN_REQUEST_STARTED;
Robert Greenwalt3dc73e52014-05-15 18:07:26 -07001654 } else {
1655 Log.d(TAG, " request Failed");
Chalard Jeanafaed1a2019-11-21 14:48:00 +09001656 return DEPRECATED_PHONE_CONSTANT_APN_REQUEST_FAILED;
The Android Open Source Project28527d22009-03-03 19:31:44 -08001657 }
1658 }
1659
1660 /**
1661 * Tells the underlying networking system that the caller is finished
1662 * using the named feature. The interpretation of {@code feature}
1663 * is completely up to each networking implementation.
Lorenzo Colitti0b40c872015-10-15 16:29:00 +09001664 *
1665 * <p>This method requires the caller to hold either the
1666 * {@link android.Manifest.permission#CHANGE_NETWORK_STATE} permission
1667 * or the ability to modify system settings as determined by
1668 * {@link android.provider.Settings.System#canWrite}.</p>
1669 *
The Android Open Source Project28527d22009-03-03 19:31:44 -08001670 * @param networkType specifies which network the request pertains to
1671 * @param feature the name of the feature that is no longer needed
1672 * @return an integer value representing the outcome of the request.
1673 * The interpretation of this value is specific to each networking
1674 * implementation+feature combination, except that the value {@code -1}
1675 * always indicates failure.
Robert Greenwaltf3017f72014-05-18 23:07:25 -07001676 *
Lorenzo Colitti15874cd2016-04-13 22:00:02 +09001677 * @deprecated Deprecated in favor of the cleaner
1678 * {@link #unregisterNetworkCallback(NetworkCallback)} API.
Dianne Hackborn18c1d832015-07-31 10:35:34 -07001679 * In {@link VERSION_CODES#M}, and above, this method is unsupported and will
Lorenzo Colitti2780e4b2015-07-29 11:41:21 +09001680 * throw {@code UnsupportedOperationException} if called.
Lorenzo Colitti84d7f072016-12-09 18:39:30 +09001681 * @removed
The Android Open Source Project28527d22009-03-03 19:31:44 -08001682 */
Aurimas Liutikase80a0eb2016-05-24 15:22:55 -07001683 @Deprecated
The Android Open Source Project28527d22009-03-03 19:31:44 -08001684 public int stopUsingNetworkFeature(int networkType, String feature) {
Lorenzo Colitti2780e4b2015-07-29 11:41:21 +09001685 checkLegacyRoutingApiAccess();
Robert Greenwalt3dc73e52014-05-15 18:07:26 -07001686 NetworkCapabilities netCap = networkCapabilitiesForFeature(networkType, feature);
1687 if (netCap == null) {
1688 Log.d(TAG, "Can't satisfy stopUsingNetworkFeature for " + networkType + ", " +
1689 feature);
The Android Open Source Project28527d22009-03-03 19:31:44 -08001690 return -1;
1691 }
Robert Greenwalt3dc73e52014-05-15 18:07:26 -07001692
Paul Jensen49f74a32014-12-17 10:39:34 -05001693 if (removeRequestForFeature(netCap)) {
Robert Greenwalt3dc73e52014-05-15 18:07:26 -07001694 Log.d(TAG, "stopUsingNetworkFeature for " + networkType + ", " + feature);
Robert Greenwalt3dc73e52014-05-15 18:07:26 -07001695 }
1696 return 1;
1697 }
1698
Mathew Inwoode1a17ba2020-11-04 09:29:36 +00001699 @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.R, trackingBug = 170729553)
Robert Greenwalt3dc73e52014-05-15 18:07:26 -07001700 private NetworkCapabilities networkCapabilitiesForFeature(int networkType, String feature) {
1701 if (networkType == TYPE_MOBILE) {
Erik Klinece55eb12017-01-26 18:08:28 +09001702 switch (feature) {
1703 case "enableCBS":
1704 return networkCapabilitiesForType(TYPE_MOBILE_CBS);
1705 case "enableDUN":
1706 case "enableDUNAlways":
1707 return networkCapabilitiesForType(TYPE_MOBILE_DUN);
1708 case "enableFOTA":
1709 return networkCapabilitiesForType(TYPE_MOBILE_FOTA);
1710 case "enableHIPRI":
1711 return networkCapabilitiesForType(TYPE_MOBILE_HIPRI);
1712 case "enableIMS":
1713 return networkCapabilitiesForType(TYPE_MOBILE_IMS);
1714 case "enableMMS":
1715 return networkCapabilitiesForType(TYPE_MOBILE_MMS);
1716 case "enableSUPL":
1717 return networkCapabilitiesForType(TYPE_MOBILE_SUPL);
1718 default:
1719 return null;
Robert Greenwalt3dc73e52014-05-15 18:07:26 -07001720 }
Erik Klinece55eb12017-01-26 18:08:28 +09001721 } else if (networkType == TYPE_WIFI && "p2p".equals(feature)) {
1722 return networkCapabilitiesForType(TYPE_WIFI_P2P);
Robert Greenwalt3dc73e52014-05-15 18:07:26 -07001723 }
1724 return null;
1725 }
1726
Robert Greenwalt802c1102014-06-02 15:32:02 -07001727 private int legacyTypeForNetworkCapabilities(NetworkCapabilities netCap) {
Robert Greenwalt3dc73e52014-05-15 18:07:26 -07001728 if (netCap == null) return TYPE_NONE;
1729 if (netCap.hasCapability(NetworkCapabilities.NET_CAPABILITY_CBS)) {
1730 return TYPE_MOBILE_CBS;
1731 }
1732 if (netCap.hasCapability(NetworkCapabilities.NET_CAPABILITY_IMS)) {
1733 return TYPE_MOBILE_IMS;
1734 }
1735 if (netCap.hasCapability(NetworkCapabilities.NET_CAPABILITY_FOTA)) {
1736 return TYPE_MOBILE_FOTA;
1737 }
1738 if (netCap.hasCapability(NetworkCapabilities.NET_CAPABILITY_DUN)) {
1739 return TYPE_MOBILE_DUN;
1740 }
1741 if (netCap.hasCapability(NetworkCapabilities.NET_CAPABILITY_SUPL)) {
1742 return TYPE_MOBILE_SUPL;
1743 }
1744 if (netCap.hasCapability(NetworkCapabilities.NET_CAPABILITY_MMS)) {
1745 return TYPE_MOBILE_MMS;
1746 }
1747 if (netCap.hasCapability(NetworkCapabilities.NET_CAPABILITY_INTERNET)) {
1748 return TYPE_MOBILE_HIPRI;
1749 }
Robert Greenwalt802c1102014-06-02 15:32:02 -07001750 if (netCap.hasCapability(NetworkCapabilities.NET_CAPABILITY_WIFI_P2P)) {
1751 return TYPE_WIFI_P2P;
1752 }
Robert Greenwalt3dc73e52014-05-15 18:07:26 -07001753 return TYPE_NONE;
1754 }
1755
1756 private static class LegacyRequest {
1757 NetworkCapabilities networkCapabilities;
1758 NetworkRequest networkRequest;
1759 int expireSequenceNumber;
1760 Network currentNetwork;
1761 int delay = -1;
Paul Jensen49f74a32014-12-17 10:39:34 -05001762
1763 private void clearDnsBinding() {
1764 if (currentNetwork != null) {
1765 currentNetwork = null;
1766 setProcessDefaultNetworkForHostResolution(null);
1767 }
1768 }
1769
Robert Greenwaltf57c03c2014-06-11 16:05:07 -07001770 NetworkCallback networkCallback = new NetworkCallback() {
Robert Greenwalt3dc73e52014-05-15 18:07:26 -07001771 @Override
Robert Greenwaltf57c03c2014-06-11 16:05:07 -07001772 public void onAvailable(Network network) {
Robert Greenwalt3dc73e52014-05-15 18:07:26 -07001773 currentNetwork = network;
1774 Log.d(TAG, "startUsingNetworkFeature got Network:" + network);
Paul Jensen8cdda642014-05-29 10:12:39 -04001775 setProcessDefaultNetworkForHostResolution(network);
Robert Greenwalt3dc73e52014-05-15 18:07:26 -07001776 }
1777 @Override
Robert Greenwaltf57c03c2014-06-11 16:05:07 -07001778 public void onLost(Network network) {
Paul Jensen49f74a32014-12-17 10:39:34 -05001779 if (network.equals(currentNetwork)) clearDnsBinding();
Robert Greenwalt3dc73e52014-05-15 18:07:26 -07001780 Log.d(TAG, "startUsingNetworkFeature lost Network:" + network);
1781 }
1782 };
1783 }
1784
Mathew Inwoode1a17ba2020-11-04 09:29:36 +00001785 @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.R, trackingBug = 170729553)
Chalard Jean9dd11612018-06-04 16:52:49 +09001786 private static final HashMap<NetworkCapabilities, LegacyRequest> sLegacyRequests =
1787 new HashMap<>();
Robert Greenwalt3dc73e52014-05-15 18:07:26 -07001788
1789 private NetworkRequest findRequestForFeature(NetworkCapabilities netCap) {
1790 synchronized (sLegacyRequests) {
1791 LegacyRequest l = sLegacyRequests.get(netCap);
1792 if (l != null) return l.networkRequest;
1793 }
1794 return null;
1795 }
1796
1797 private void renewRequestLocked(LegacyRequest l) {
1798 l.expireSequenceNumber++;
1799 Log.d(TAG, "renewing request to seqNum " + l.expireSequenceNumber);
1800 sendExpireMsgForFeature(l.networkCapabilities, l.expireSequenceNumber, l.delay);
1801 }
1802
1803 private void expireRequest(NetworkCapabilities netCap, int sequenceNum) {
1804 int ourSeqNum = -1;
1805 synchronized (sLegacyRequests) {
1806 LegacyRequest l = sLegacyRequests.get(netCap);
1807 if (l == null) return;
1808 ourSeqNum = l.expireSequenceNumber;
Paul Jensen49f74a32014-12-17 10:39:34 -05001809 if (l.expireSequenceNumber == sequenceNum) removeRequestForFeature(netCap);
Robert Greenwalt3dc73e52014-05-15 18:07:26 -07001810 }
1811 Log.d(TAG, "expireRequest with " + ourSeqNum + ", " + sequenceNum);
1812 }
1813
Mathew Inwoode1a17ba2020-11-04 09:29:36 +00001814 @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.R, trackingBug = 170729553)
Robert Greenwalt3dc73e52014-05-15 18:07:26 -07001815 private NetworkRequest requestNetworkForFeatureLocked(NetworkCapabilities netCap) {
1816 int delay = -1;
Robert Greenwalt802c1102014-06-02 15:32:02 -07001817 int type = legacyTypeForNetworkCapabilities(netCap);
Robert Greenwalt3dc73e52014-05-15 18:07:26 -07001818 try {
1819 delay = mService.getRestoreDefaultNetworkDelay(type);
Jeff Sharkeyc78f85c2016-03-01 19:27:23 -07001820 } catch (RemoteException e) {
1821 throw e.rethrowFromSystemServer();
1822 }
Robert Greenwalt3dc73e52014-05-15 18:07:26 -07001823 LegacyRequest l = new LegacyRequest();
1824 l.networkCapabilities = netCap;
1825 l.delay = delay;
1826 l.expireSequenceNumber = 0;
Hugo Benichifd44e912017-02-02 17:02:36 +09001827 l.networkRequest = sendRequestForNetwork(
1828 netCap, l.networkCallback, 0, REQUEST, type, getDefaultHandler());
Robert Greenwalt3dc73e52014-05-15 18:07:26 -07001829 if (l.networkRequest == null) return null;
1830 sLegacyRequests.put(netCap, l);
1831 sendExpireMsgForFeature(netCap, l.expireSequenceNumber, delay);
1832 return l.networkRequest;
1833 }
1834
1835 private void sendExpireMsgForFeature(NetworkCapabilities netCap, int seqNum, int delay) {
1836 if (delay >= 0) {
1837 Log.d(TAG, "sending expire msg with seqNum " + seqNum + " and delay " + delay);
Hugo Benichifd44e912017-02-02 17:02:36 +09001838 CallbackHandler handler = getDefaultHandler();
Hugo Benichibc4ac972017-02-03 14:18:44 +09001839 Message msg = handler.obtainMessage(EXPIRE_LEGACY_REQUEST, seqNum, 0, netCap);
1840 handler.sendMessageDelayed(msg, delay);
Robert Greenwalt3dc73e52014-05-15 18:07:26 -07001841 }
1842 }
1843
Mathew Inwoode1a17ba2020-11-04 09:29:36 +00001844 @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.R, trackingBug = 170729553)
Paul Jensen49f74a32014-12-17 10:39:34 -05001845 private boolean removeRequestForFeature(NetworkCapabilities netCap) {
1846 final LegacyRequest l;
Robert Greenwalt3dc73e52014-05-15 18:07:26 -07001847 synchronized (sLegacyRequests) {
Paul Jensen49f74a32014-12-17 10:39:34 -05001848 l = sLegacyRequests.remove(netCap);
Robert Greenwalt3dc73e52014-05-15 18:07:26 -07001849 }
Paul Jensen49f74a32014-12-17 10:39:34 -05001850 if (l == null) return false;
1851 unregisterNetworkCallback(l.networkCallback);
1852 l.clearDnsBinding();
1853 return true;
The Android Open Source Project28527d22009-03-03 19:31:44 -08001854 }
1855
Erik Klinece55eb12017-01-26 18:08:28 +09001856 private static final SparseIntArray sLegacyTypeToTransport = new SparseIntArray();
1857 static {
1858 sLegacyTypeToTransport.put(TYPE_MOBILE, NetworkCapabilities.TRANSPORT_CELLULAR);
1859 sLegacyTypeToTransport.put(TYPE_MOBILE_CBS, NetworkCapabilities.TRANSPORT_CELLULAR);
1860 sLegacyTypeToTransport.put(TYPE_MOBILE_DUN, NetworkCapabilities.TRANSPORT_CELLULAR);
1861 sLegacyTypeToTransport.put(TYPE_MOBILE_FOTA, NetworkCapabilities.TRANSPORT_CELLULAR);
1862 sLegacyTypeToTransport.put(TYPE_MOBILE_HIPRI, NetworkCapabilities.TRANSPORT_CELLULAR);
1863 sLegacyTypeToTransport.put(TYPE_MOBILE_IMS, NetworkCapabilities.TRANSPORT_CELLULAR);
1864 sLegacyTypeToTransport.put(TYPE_MOBILE_MMS, NetworkCapabilities.TRANSPORT_CELLULAR);
1865 sLegacyTypeToTransport.put(TYPE_MOBILE_SUPL, NetworkCapabilities.TRANSPORT_CELLULAR);
1866 sLegacyTypeToTransport.put(TYPE_WIFI, NetworkCapabilities.TRANSPORT_WIFI);
1867 sLegacyTypeToTransport.put(TYPE_WIFI_P2P, NetworkCapabilities.TRANSPORT_WIFI);
1868 sLegacyTypeToTransport.put(TYPE_BLUETOOTH, NetworkCapabilities.TRANSPORT_BLUETOOTH);
1869 sLegacyTypeToTransport.put(TYPE_ETHERNET, NetworkCapabilities.TRANSPORT_ETHERNET);
1870 }
1871
1872 private static final SparseIntArray sLegacyTypeToCapability = new SparseIntArray();
1873 static {
1874 sLegacyTypeToCapability.put(TYPE_MOBILE_CBS, NetworkCapabilities.NET_CAPABILITY_CBS);
1875 sLegacyTypeToCapability.put(TYPE_MOBILE_DUN, NetworkCapabilities.NET_CAPABILITY_DUN);
1876 sLegacyTypeToCapability.put(TYPE_MOBILE_FOTA, NetworkCapabilities.NET_CAPABILITY_FOTA);
1877 sLegacyTypeToCapability.put(TYPE_MOBILE_IMS, NetworkCapabilities.NET_CAPABILITY_IMS);
1878 sLegacyTypeToCapability.put(TYPE_MOBILE_MMS, NetworkCapabilities.NET_CAPABILITY_MMS);
1879 sLegacyTypeToCapability.put(TYPE_MOBILE_SUPL, NetworkCapabilities.NET_CAPABILITY_SUPL);
1880 sLegacyTypeToCapability.put(TYPE_WIFI_P2P, NetworkCapabilities.NET_CAPABILITY_WIFI_P2P);
1881 }
1882
1883 /**
1884 * Given a legacy type (TYPE_WIFI, ...) returns a NetworkCapabilities
1885 * instance suitable for registering a request or callback. Throws an
1886 * IllegalArgumentException if no mapping from the legacy type to
1887 * NetworkCapabilities is known.
1888 *
Chalard Jean7eaf3b12018-03-08 13:54:53 +09001889 * @deprecated Types are deprecated. Use {@link NetworkCallback} or {@link NetworkRequest}
1890 * to find the network instead.
Erik Klinece55eb12017-01-26 18:08:28 +09001891 * @hide
1892 */
1893 public static NetworkCapabilities networkCapabilitiesForType(int type) {
1894 final NetworkCapabilities nc = new NetworkCapabilities();
1895
1896 // Map from type to transports.
1897 final int NOT_FOUND = -1;
1898 final int transport = sLegacyTypeToTransport.get(type, NOT_FOUND);
Remi NGUYEN VAN81f68422021-03-15 07:31:54 +00001899 if (transport == NOT_FOUND) {
1900 throw new IllegalArgumentException("unknown legacy type: " + type);
1901 }
Erik Klinece55eb12017-01-26 18:08:28 +09001902 nc.addTransportType(transport);
1903
1904 // Map from type to capabilities.
1905 nc.addCapability(sLegacyTypeToCapability.get(
1906 type, NetworkCapabilities.NET_CAPABILITY_INTERNET));
1907 nc.maybeMarkCapabilitiesRestricted();
1908 return nc;
1909 }
1910
Lorenzo Colitti0b798a82015-06-15 14:29:22 +09001911 /** @hide */
1912 public static class PacketKeepaliveCallback {
Mathew Inwoode1a17ba2020-11-04 09:29:36 +00001913 @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.R, trackingBug = 170729553)
Artur Satayev56cb6bb2019-11-04 17:50:59 +00001914 public PacketKeepaliveCallback() {
1915 }
Lorenzo Colitti0b798a82015-06-15 14:29:22 +09001916 /** The requested keepalive was successfully started. */
Mathew Inwoode1a17ba2020-11-04 09:29:36 +00001917 @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.R, trackingBug = 170729553)
Lorenzo Colitti0b798a82015-06-15 14:29:22 +09001918 public void onStarted() {}
1919 /** The keepalive was successfully stopped. */
Mathew Inwoode1a17ba2020-11-04 09:29:36 +00001920 @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.R, trackingBug = 170729553)
Lorenzo Colitti0b798a82015-06-15 14:29:22 +09001921 public void onStopped() {}
1922 /** An error occurred. */
Mathew Inwoode1a17ba2020-11-04 09:29:36 +00001923 @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.R, trackingBug = 170729553)
Lorenzo Colitti0b798a82015-06-15 14:29:22 +09001924 public void onError(int error) {}
1925 }
1926
1927 /**
1928 * Allows applications to request that the system periodically send specific packets on their
1929 * behalf, using hardware offload to save battery power.
1930 *
1931 * To request that the system send keepalives, call one of the methods that return a
1932 * {@link ConnectivityManager.PacketKeepalive} object, such as {@link #startNattKeepalive},
1933 * passing in a non-null callback. If the callback is successfully started, the callback's
1934 * {@code onStarted} method will be called. If an error occurs, {@code onError} will be called,
1935 * specifying one of the {@code ERROR_*} constants in this class.
1936 *
Chalard Jean9dd11612018-06-04 16:52:49 +09001937 * To stop an existing keepalive, call {@link PacketKeepalive#stop}. The system will call
1938 * {@link PacketKeepaliveCallback#onStopped} if the operation was successful or
1939 * {@link PacketKeepaliveCallback#onError} if an error occurred.
Lorenzo Colitti0b798a82015-06-15 14:29:22 +09001940 *
junyulai4c95b082018-12-27 17:25:29 +08001941 * @deprecated Use {@link SocketKeepalive} instead.
1942 *
Lorenzo Colitti0b798a82015-06-15 14:29:22 +09001943 * @hide
1944 */
1945 public class PacketKeepalive {
1946
1947 private static final String TAG = "PacketKeepalive";
1948
1949 /** @hide */
1950 public static final int SUCCESS = 0;
1951
1952 /** @hide */
1953 public static final int NO_KEEPALIVE = -1;
1954
1955 /** @hide */
1956 public static final int BINDER_DIED = -10;
1957
1958 /** The specified {@code Network} is not connected. */
1959 public static final int ERROR_INVALID_NETWORK = -20;
1960 /** The specified IP addresses are invalid. For example, the specified source IP address is
1961 * not configured on the specified {@code Network}. */
1962 public static final int ERROR_INVALID_IP_ADDRESS = -21;
1963 /** The requested port is invalid. */
1964 public static final int ERROR_INVALID_PORT = -22;
1965 /** The packet length is invalid (e.g., too long). */
1966 public static final int ERROR_INVALID_LENGTH = -23;
1967 /** The packet transmission interval is invalid (e.g., too short). */
1968 public static final int ERROR_INVALID_INTERVAL = -24;
1969
1970 /** The hardware does not support this request. */
1971 public static final int ERROR_HARDWARE_UNSUPPORTED = -30;
Lorenzo Colitti723f82f2015-09-08 16:46:36 +09001972 /** The hardware returned an error. */
1973 public static final int ERROR_HARDWARE_ERROR = -31;
Lorenzo Colitti0b798a82015-06-15 14:29:22 +09001974
Nathan Harold0990bc82018-02-14 13:09:45 -08001975 /** The NAT-T destination port for IPsec */
Lorenzo Colitti0b798a82015-06-15 14:29:22 +09001976 public static final int NATT_PORT = 4500;
1977
Nathan Harold0990bc82018-02-14 13:09:45 -08001978 /** The minimum interval in seconds between keepalive packet transmissions */
1979 public static final int MIN_INTERVAL = 10;
1980
Lorenzo Colitti0b798a82015-06-15 14:29:22 +09001981 private final Network mNetwork;
junyulai070f9ff2019-01-16 20:23:34 +08001982 private final ISocketKeepaliveCallback mCallback;
1983 private final ExecutorService mExecutor;
Lorenzo Colitti0b798a82015-06-15 14:29:22 +09001984
1985 private volatile Integer mSlot;
1986
Mathew Inwoode1a17ba2020-11-04 09:29:36 +00001987 @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.R, trackingBug = 170729553)
Lorenzo Colitti0b798a82015-06-15 14:29:22 +09001988 public void stop() {
1989 try {
junyulai070f9ff2019-01-16 20:23:34 +08001990 mExecutor.execute(() -> {
1991 try {
1992 if (mSlot != null) {
1993 mService.stopKeepalive(mNetwork, mSlot);
1994 }
1995 } catch (RemoteException e) {
1996 Log.e(TAG, "Error stopping packet keepalive: ", e);
1997 throw e.rethrowFromSystemServer();
1998 }
1999 });
2000 } catch (RejectedExecutionException e) {
2001 // The internal executor has already stopped due to previous event.
Lorenzo Colitti0b798a82015-06-15 14:29:22 +09002002 }
2003 }
2004
2005 private PacketKeepalive(Network network, PacketKeepaliveCallback callback) {
Remi NGUYEN VAN81f68422021-03-15 07:31:54 +00002006 Objects.requireNonNull(network, "network cannot be null");
2007 Objects.requireNonNull(callback, "callback cannot be null");
Lorenzo Colitti0b798a82015-06-15 14:29:22 +09002008 mNetwork = network;
junyulai070f9ff2019-01-16 20:23:34 +08002009 mExecutor = Executors.newSingleThreadExecutor();
2010 mCallback = new ISocketKeepaliveCallback.Stub() {
Lorenzo Colitti0b798a82015-06-15 14:29:22 +09002011 @Override
junyulai070f9ff2019-01-16 20:23:34 +08002012 public void onStarted(int slot) {
lucaslinbe801382020-12-30 11:54:55 +08002013 final long token = Binder.clearCallingIdentity();
2014 try {
2015 mExecutor.execute(() -> {
2016 mSlot = slot;
2017 callback.onStarted();
2018 });
2019 } finally {
2020 Binder.restoreCallingIdentity(token);
2021 }
Lorenzo Colitti0b798a82015-06-15 14:29:22 +09002022 }
junyulai070f9ff2019-01-16 20:23:34 +08002023
2024 @Override
2025 public void onStopped() {
lucaslinbe801382020-12-30 11:54:55 +08002026 final long token = Binder.clearCallingIdentity();
2027 try {
2028 mExecutor.execute(() -> {
2029 mSlot = null;
2030 callback.onStopped();
2031 });
2032 } finally {
2033 Binder.restoreCallingIdentity(token);
2034 }
junyulai070f9ff2019-01-16 20:23:34 +08002035 mExecutor.shutdown();
2036 }
2037
2038 @Override
2039 public void onError(int error) {
lucaslinbe801382020-12-30 11:54:55 +08002040 final long token = Binder.clearCallingIdentity();
2041 try {
2042 mExecutor.execute(() -> {
2043 mSlot = null;
2044 callback.onError(error);
2045 });
2046 } finally {
2047 Binder.restoreCallingIdentity(token);
2048 }
junyulai070f9ff2019-01-16 20:23:34 +08002049 mExecutor.shutdown();
2050 }
2051
2052 @Override
2053 public void onDataReceived() {
2054 // PacketKeepalive is only used for Nat-T keepalive and as such does not invoke
2055 // this callback when data is received.
2056 }
2057 };
Lorenzo Colitti0b798a82015-06-15 14:29:22 +09002058 }
2059 }
2060
2061 /**
2062 * Starts an IPsec NAT-T keepalive packet with the specified parameters.
2063 *
junyulai4c95b082018-12-27 17:25:29 +08002064 * @deprecated Use {@link #createSocketKeepalive} instead.
2065 *
Lorenzo Colitti0b798a82015-06-15 14:29:22 +09002066 * @hide
2067 */
Mathew Inwoode1a17ba2020-11-04 09:29:36 +00002068 @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.R, trackingBug = 170729553)
Lorenzo Colitti0b798a82015-06-15 14:29:22 +09002069 public PacketKeepalive startNattKeepalive(
2070 Network network, int intervalSeconds, PacketKeepaliveCallback callback,
2071 InetAddress srcAddr, int srcPort, InetAddress dstAddr) {
2072 final PacketKeepalive k = new PacketKeepalive(network, callback);
2073 try {
junyulai070f9ff2019-01-16 20:23:34 +08002074 mService.startNattKeepalive(network, intervalSeconds, k.mCallback,
Lorenzo Colitti0b798a82015-06-15 14:29:22 +09002075 srcAddr.getHostAddress(), srcPort, dstAddr.getHostAddress());
2076 } catch (RemoteException e) {
2077 Log.e(TAG, "Error starting packet keepalive: ", e);
junyulai070f9ff2019-01-16 20:23:34 +08002078 throw e.rethrowFromSystemServer();
Lorenzo Colitti0b798a82015-06-15 14:29:22 +09002079 }
2080 return k;
2081 }
2082
Chiachang Wang619319a2021-01-15 11:06:21 +08002083 // Construct an invalid fd.
2084 private ParcelFileDescriptor createInvalidFd() {
2085 final int invalidFd = -1;
2086 return ParcelFileDescriptor.adoptFd(invalidFd);
2087 }
2088
The Android Open Source Project28527d22009-03-03 19:31:44 -08002089 /**
junyulai4c95b082018-12-27 17:25:29 +08002090 * Request that keepalives be started on a IPsec NAT-T socket.
2091 *
2092 * @param network The {@link Network} the socket is on.
2093 * @param socket The socket that needs to be kept alive.
2094 * @param source The source address of the {@link UdpEncapsulationSocket}.
2095 * @param destination The destination address of the {@link UdpEncapsulationSocket}.
2096 * @param executor The executor on which callback will be invoked. The provided {@link Executor}
2097 * must run callback sequentially, otherwise the order of callbacks cannot be
2098 * guaranteed.
2099 * @param callback A {@link SocketKeepalive.Callback}. Used for notifications about keepalive
2100 * changes. Must be extended by applications that use this API.
2101 *
junyulai0835a1e2019-01-08 20:04:33 +08002102 * @return A {@link SocketKeepalive} object that can be used to control the keepalive on the
2103 * given socket.
junyulai4c95b082018-12-27 17:25:29 +08002104 **/
junyulai7e06ad42019-03-04 22:45:36 +08002105 public @NonNull SocketKeepalive createSocketKeepalive(@NonNull Network network,
junyulai4c95b082018-12-27 17:25:29 +08002106 @NonNull UdpEncapsulationSocket socket,
2107 @NonNull InetAddress source,
2108 @NonNull InetAddress destination,
2109 @NonNull @CallbackExecutor Executor executor,
2110 @NonNull Callback callback) {
junyulai7e06ad42019-03-04 22:45:36 +08002111 ParcelFileDescriptor dup;
2112 try {
junyulai828dad12019-03-27 11:00:37 +08002113 // Dup is needed here as the pfd inside the socket is owned by the IpSecService,
2114 // which cannot be obtained by the app process.
junyulai7e06ad42019-03-04 22:45:36 +08002115 dup = ParcelFileDescriptor.dup(socket.getFileDescriptor());
2116 } catch (IOException ignored) {
2117 // Construct an invalid fd, so that if the user later calls start(), it will fail with
2118 // ERROR_INVALID_SOCKET.
Chiachang Wang619319a2021-01-15 11:06:21 +08002119 dup = createInvalidFd();
junyulai7e06ad42019-03-04 22:45:36 +08002120 }
2121 return new NattSocketKeepalive(mService, network, dup, socket.getResourceId(), source,
2122 destination, executor, callback);
junyulaid05a1922019-01-15 11:32:44 +08002123 }
2124
2125 /**
2126 * Request that keepalives be started on a IPsec NAT-T socket file descriptor. Directly called
2127 * by system apps which don't use IpSecService to create {@link UdpEncapsulationSocket}.
2128 *
2129 * @param network The {@link Network} the socket is on.
junyulai7e06ad42019-03-04 22:45:36 +08002130 * @param pfd The {@link ParcelFileDescriptor} that needs to be kept alive. The provided
2131 * {@link ParcelFileDescriptor} must be bound to a port and the keepalives will be sent
2132 * from that port.
junyulaid05a1922019-01-15 11:32:44 +08002133 * @param source The source address of the {@link UdpEncapsulationSocket}.
2134 * @param destination The destination address of the {@link UdpEncapsulationSocket}. The
2135 * keepalive packets will always be sent to port 4500 of the given {@code destination}.
2136 * @param executor The executor on which callback will be invoked. The provided {@link Executor}
2137 * must run callback sequentially, otherwise the order of callbacks cannot be
2138 * guaranteed.
2139 * @param callback A {@link SocketKeepalive.Callback}. Used for notifications about keepalive
2140 * changes. Must be extended by applications that use this API.
2141 *
junyulai0835a1e2019-01-08 20:04:33 +08002142 * @return A {@link SocketKeepalive} object that can be used to control the keepalive on the
2143 * given socket.
junyulaid05a1922019-01-15 11:32:44 +08002144 * @hide
2145 */
2146 @SystemApi
2147 @RequiresPermission(android.Manifest.permission.PACKET_KEEPALIVE_OFFLOAD)
junyulai7e06ad42019-03-04 22:45:36 +08002148 public @NonNull SocketKeepalive createNattKeepalive(@NonNull Network network,
2149 @NonNull ParcelFileDescriptor pfd,
junyulaid05a1922019-01-15 11:32:44 +08002150 @NonNull InetAddress source,
2151 @NonNull InetAddress destination,
2152 @NonNull @CallbackExecutor Executor executor,
2153 @NonNull Callback callback) {
junyulai7e06ad42019-03-04 22:45:36 +08002154 ParcelFileDescriptor dup;
2155 try {
junyulai828dad12019-03-27 11:00:37 +08002156 // TODO: Consider remove unnecessary dup.
junyulai7e06ad42019-03-04 22:45:36 +08002157 dup = pfd.dup();
2158 } catch (IOException ignored) {
2159 // Construct an invalid fd, so that if the user later calls start(), it will fail with
2160 // ERROR_INVALID_SOCKET.
Chiachang Wang619319a2021-01-15 11:06:21 +08002161 dup = createInvalidFd();
junyulai7e06ad42019-03-04 22:45:36 +08002162 }
2163 return new NattSocketKeepalive(mService, network, dup,
Remi NGUYEN VANce355502021-03-11 10:56:49 +00002164 -1 /* Unused */, source, destination, executor, callback);
junyulai4c95b082018-12-27 17:25:29 +08002165 }
2166
2167 /**
junyulai0835a1e2019-01-08 20:04:33 +08002168 * Request that keepalives be started on a TCP socket.
2169 * The socket must be established.
2170 *
2171 * @param network The {@link Network} the socket is on.
2172 * @param socket The socket that needs to be kept alive.
2173 * @param executor The executor on which callback will be invoked. This implementation assumes
2174 * the provided {@link Executor} runs the callbacks in sequence with no
2175 * concurrency. Failing this, no guarantee of correctness can be made. It is
2176 * the responsibility of the caller to ensure the executor provides this
2177 * guarantee. A simple way of creating such an executor is with the standard
2178 * tool {@code Executors.newSingleThreadExecutor}.
2179 * @param callback A {@link SocketKeepalive.Callback}. Used for notifications about keepalive
2180 * changes. Must be extended by applications that use this API.
2181 *
2182 * @return A {@link SocketKeepalive} object that can be used to control the keepalive on the
2183 * given socket.
2184 * @hide
2185 */
2186 @SystemApi
2187 @RequiresPermission(android.Manifest.permission.PACKET_KEEPALIVE_OFFLOAD)
junyulai7e06ad42019-03-04 22:45:36 +08002188 public @NonNull SocketKeepalive createSocketKeepalive(@NonNull Network network,
junyulai0835a1e2019-01-08 20:04:33 +08002189 @NonNull Socket socket,
2190 @NonNull Executor executor,
2191 @NonNull Callback callback) {
junyulai7e06ad42019-03-04 22:45:36 +08002192 ParcelFileDescriptor dup;
2193 try {
2194 dup = ParcelFileDescriptor.fromSocket(socket);
2195 } catch (UncheckedIOException ignored) {
2196 // Construct an invalid fd, so that if the user later calls start(), it will fail with
2197 // ERROR_INVALID_SOCKET.
Chiachang Wang619319a2021-01-15 11:06:21 +08002198 dup = createInvalidFd();
junyulai7e06ad42019-03-04 22:45:36 +08002199 }
2200 return new TcpSocketKeepalive(mService, network, dup, executor, callback);
junyulai0835a1e2019-01-08 20:04:33 +08002201 }
2202
2203 /**
The Android Open Source Project28527d22009-03-03 19:31:44 -08002204 * Ensure that a network route exists to deliver traffic to the specified
2205 * host via the specified network interface. An attempt to add a route that
2206 * already exists is ignored, but treated as successful.
Lorenzo Colitti0b40c872015-10-15 16:29:00 +09002207 *
2208 * <p>This method requires the caller to hold either the
2209 * {@link android.Manifest.permission#CHANGE_NETWORK_STATE} permission
2210 * or the ability to modify system settings as determined by
2211 * {@link android.provider.Settings.System#canWrite}.</p>
2212 *
The Android Open Source Project28527d22009-03-03 19:31:44 -08002213 * @param networkType the type of the network over which traffic to the specified
2214 * host is to be routed
2215 * @param hostAddress the IP address of the host to which the route is desired
2216 * @return {@code true} on success, {@code false} on failure
Robert Greenwaltf3017f72014-05-18 23:07:25 -07002217 *
Lorenzo Colitti7f6a2bf2015-04-24 17:03:31 +09002218 * @deprecated Deprecated in favor of the
2219 * {@link #requestNetwork(NetworkRequest, NetworkCallback)},
2220 * {@link #bindProcessToNetwork} and {@link Network#getSocketFactory} API.
Dianne Hackborn18c1d832015-07-31 10:35:34 -07002221 * In {@link VERSION_CODES#M}, and above, this method is unsupported and will
Lorenzo Colitti2780e4b2015-07-29 11:41:21 +09002222 * throw {@code UnsupportedOperationException} if called.
Lorenzo Colitti84d7f072016-12-09 18:39:30 +09002223 * @removed
The Android Open Source Project28527d22009-03-03 19:31:44 -08002224 */
Aurimas Liutikase80a0eb2016-05-24 15:22:55 -07002225 @Deprecated
The Android Open Source Project28527d22009-03-03 19:31:44 -08002226 public boolean requestRouteToHost(int networkType, int hostAddress) {
Sreeram Ramachandranc06ec732014-07-19 23:21:46 -07002227 return requestRouteToHostAddress(networkType, NetworkUtils.intToInetAddress(hostAddress));
Robert Greenwalt7fe44cb2010-08-27 09:24:29 -07002228 }
2229
2230 /**
2231 * Ensure that a network route exists to deliver traffic to the specified
2232 * host via the specified network interface. An attempt to add a route that
2233 * already exists is ignored, but treated as successful.
Lorenzo Colitti0b40c872015-10-15 16:29:00 +09002234 *
2235 * <p>This method requires the caller to hold either the
2236 * {@link android.Manifest.permission#CHANGE_NETWORK_STATE} permission
2237 * or the ability to modify system settings as determined by
2238 * {@link android.provider.Settings.System#canWrite}.</p>
2239 *
Robert Greenwalt7fe44cb2010-08-27 09:24:29 -07002240 * @param networkType the type of the network over which traffic to the specified
2241 * host is to be routed
2242 * @param hostAddress the IP address of the host to which the route is desired
2243 * @return {@code true} on success, {@code false} on failure
2244 * @hide
Robert Greenwaltf3017f72014-05-18 23:07:25 -07002245 * @deprecated Deprecated in favor of the {@link #requestNetwork} and
Lorenzo Colitti7f6a2bf2015-04-24 17:03:31 +09002246 * {@link #bindProcessToNetwork} API.
Robert Greenwalt7fe44cb2010-08-27 09:24:29 -07002247 */
Aurimas Liutikase80a0eb2016-05-24 15:22:55 -07002248 @Deprecated
Mathew Inwoodc5b9bec2018-08-08 14:52:47 +01002249 @UnsupportedAppUsage
lucasline257bce2021-03-22 11:51:27 +08002250 @SystemApi(client = MODULE_LIBRARIES)
Robert Greenwalt7fe44cb2010-08-27 09:24:29 -07002251 public boolean requestRouteToHostAddress(int networkType, InetAddress hostAddress) {
Lorenzo Colitti2780e4b2015-07-29 11:41:21 +09002252 checkLegacyRoutingApiAccess();
The Android Open Source Project28527d22009-03-03 19:31:44 -08002253 try {
Philip P. Moltmann7bc33df2020-03-26 11:50:35 -07002254 return mService.requestRouteToHostAddress(networkType, hostAddress.getAddress(),
2255 mContext.getOpPackageName(), getAttributionTag());
The Android Open Source Project28527d22009-03-03 19:31:44 -08002256 } catch (RemoteException e) {
Jeff Sharkeyc78f85c2016-03-01 19:27:23 -07002257 throw e.rethrowFromSystemServer();
The Android Open Source Project28527d22009-03-03 19:31:44 -08002258 }
2259 }
2260
2261 /**
Philip P. Moltmann7bc33df2020-03-26 11:50:35 -07002262 * @return the context's attribution tag
2263 */
2264 // TODO: Remove method and replace with direct call once R code is pushed to AOSP
2265 private @Nullable String getAttributionTag() {
Roshan Piusaa24fde2020-12-17 14:53:09 -08002266 return mContext.getAttributionTag();
Philip P. Moltmann7bc33df2020-03-26 11:50:35 -07002267 }
2268
2269 /**
The Android Open Source Project28527d22009-03-03 19:31:44 -08002270 * Returns the value of the setting for background data usage. If false,
2271 * applications should not use the network if the application is not in the
2272 * foreground. Developers should respect this setting, and check the value
2273 * of this before performing any background data operations.
2274 * <p>
2275 * All applications that have background services that use the network
2276 * should listen to {@link #ACTION_BACKGROUND_DATA_SETTING_CHANGED}.
Jeff Sharkey39c01eb2011-08-16 14:37:57 -07002277 * <p>
Scott Main50589142011-10-06 18:32:43 -07002278 * @deprecated As of {@link VERSION_CODES#ICE_CREAM_SANDWICH}, availability of
Jeff Sharkey39c01eb2011-08-16 14:37:57 -07002279 * background data depends on several combined factors, and this method will
2280 * always return {@code true}. Instead, when background data is unavailable,
2281 * {@link #getActiveNetworkInfo()} will now appear disconnected.
Danica Chang96567052010-08-11 14:54:43 -07002282 *
The Android Open Source Project28527d22009-03-03 19:31:44 -08002283 * @return Whether background data usage is allowed.
2284 */
Jeff Sharkey39c01eb2011-08-16 14:37:57 -07002285 @Deprecated
The Android Open Source Project28527d22009-03-03 19:31:44 -08002286 public boolean getBackgroundDataSetting() {
Jeff Sharkey39c01eb2011-08-16 14:37:57 -07002287 // assume that background data is allowed; final authority is
2288 // NetworkInfo which may be blocked.
2289 return true;
The Android Open Source Project28527d22009-03-03 19:31:44 -08002290 }
2291
2292 /**
2293 * Sets the value of the setting for background data usage.
Robert Greenwalt1b21f6c2010-02-23 18:58:05 -08002294 *
The Android Open Source Project28527d22009-03-03 19:31:44 -08002295 * @param allowBackgroundData Whether an application should use data while
2296 * it is in the background.
Robert Greenwalt1b21f6c2010-02-23 18:58:05 -08002297 *
The Android Open Source Project28527d22009-03-03 19:31:44 -08002298 * @attr ref android.Manifest.permission#CHANGE_BACKGROUND_DATA_SETTING
2299 * @see #getBackgroundDataSetting()
2300 * @hide
2301 */
Jeff Sharkey39c01eb2011-08-16 14:37:57 -07002302 @Deprecated
Mathew Inwoodc5b9bec2018-08-08 14:52:47 +01002303 @UnsupportedAppUsage
The Android Open Source Project28527d22009-03-03 19:31:44 -08002304 public void setBackgroundDataSetting(boolean allowBackgroundData) {
Jeff Sharkey39c01eb2011-08-16 14:37:57 -07002305 // ignored
The Android Open Source Project28527d22009-03-03 19:31:44 -08002306 }
Robert Greenwalt1b21f6c2010-02-23 18:58:05 -08002307
Jeff Sharkey66fa9682011-08-02 17:22:34 -07002308 /**
Robert Greenwalt1b21f6c2010-02-23 18:58:05 -08002309 * @hide
Robert Greenwalt0c150c02014-05-21 20:04:36 -07002310 * @deprecated Talk to TelephonyManager directly
Robert Greenwalt1b21f6c2010-02-23 18:58:05 -08002311 */
Aurimas Liutikase80a0eb2016-05-24 15:22:55 -07002312 @Deprecated
Mathew Inwoodc5b9bec2018-08-08 14:52:47 +01002313 @UnsupportedAppUsage
Robert Greenwalt1b21f6c2010-02-23 18:58:05 -08002314 public boolean getMobileDataEnabled() {
Meng Wanged6f4412019-11-18 17:10:00 -08002315 TelephonyManager tm = mContext.getSystemService(TelephonyManager.class);
2316 if (tm != null) {
2317 int subId = SubscriptionManager.getDefaultDataSubscriptionId();
2318 Log.d("ConnectivityManager", "getMobileDataEnabled()+ subId=" + subId);
2319 boolean retVal = tm.createForSubscriptionId(subId).isDataEnabled();
2320 Log.d("ConnectivityManager", "getMobileDataEnabled()- subId=" + subId
2321 + " retVal=" + retVal);
2322 return retVal;
Robert Greenwalt1b21f6c2010-02-23 18:58:05 -08002323 }
Wink Saville689f7042014-12-05 11:10:30 -08002324 Log.d("ConnectivityManager", "getMobileDataEnabled()- remote exception retVal=false");
Robert Greenwalt0c150c02014-05-21 20:04:36 -07002325 return false;
Robert Greenwalt1b21f6c2010-02-23 18:58:05 -08002326 }
2327
The Android Open Source Project28527d22009-03-03 19:31:44 -08002328 /**
Robert Greenwaltad35b132014-09-04 16:44:35 -07002329 * Callback for use with {@link ConnectivityManager#addDefaultNetworkActiveListener}
Robert Greenwaltf57c03c2014-06-11 16:05:07 -07002330 * to find out when the system default network has gone in to a high power state.
Dianne Hackborn5ac88162014-02-26 16:20:52 -08002331 */
2332 public interface OnNetworkActiveListener {
2333 /**
2334 * Called on the main thread of the process to report that the current data network
2335 * has become active, and it is now a good time to perform any pending network
2336 * operations. Note that this listener only tells you when the network becomes
2337 * active; if at any other time you want to know whether it is active (and thus okay
2338 * to initiate network traffic), you can retrieve its instantaneous state with
Robert Greenwaltf57c03c2014-06-11 16:05:07 -07002339 * {@link ConnectivityManager#isDefaultNetworkActive}.
Dianne Hackborn5ac88162014-02-26 16:20:52 -08002340 */
Chalard Jean9dd11612018-06-04 16:52:49 +09002341 void onNetworkActive();
Dianne Hackborn5ac88162014-02-26 16:20:52 -08002342 }
2343
Dianne Hackborn5ac88162014-02-26 16:20:52 -08002344 private final ArrayMap<OnNetworkActiveListener, INetworkActivityListener>
Chalard Jean9dd11612018-06-04 16:52:49 +09002345 mNetworkActivityListeners = new ArrayMap<>();
Dianne Hackborn5ac88162014-02-26 16:20:52 -08002346
2347 /**
Robert Greenwaltf57c03c2014-06-11 16:05:07 -07002348 * Start listening to reports when the system's default data network is active, meaning it is
2349 * a good time to perform network traffic. Use {@link #isDefaultNetworkActive()}
2350 * to determine the current state of the system's default network after registering the
2351 * listener.
2352 * <p>
2353 * If the process default network has been set with
Paul Jensenee2f45d2015-03-10 10:54:12 -04002354 * {@link ConnectivityManager#bindProcessToNetwork} this function will not
Robert Greenwaltf57c03c2014-06-11 16:05:07 -07002355 * reflect the process's default, but the system default.
Dianne Hackborn5ac88162014-02-26 16:20:52 -08002356 *
2357 * @param l The listener to be told when the network is active.
2358 */
Robert Greenwaltad35b132014-09-04 16:44:35 -07002359 public void addDefaultNetworkActiveListener(final OnNetworkActiveListener l) {
Dianne Hackborn5ac88162014-02-26 16:20:52 -08002360 INetworkActivityListener rl = new INetworkActivityListener.Stub() {
2361 @Override
2362 public void onNetworkActive() throws RemoteException {
2363 l.onNetworkActive();
2364 }
2365 };
2366
2367 try {
lucaslin1193a5d2021-01-21 02:04:15 +08002368 mService.registerNetworkActivityListener(rl);
Dianne Hackborn5ac88162014-02-26 16:20:52 -08002369 mNetworkActivityListeners.put(l, rl);
2370 } catch (RemoteException e) {
Jeff Sharkeyc78f85c2016-03-01 19:27:23 -07002371 throw e.rethrowFromSystemServer();
Dianne Hackborn5ac88162014-02-26 16:20:52 -08002372 }
2373 }
2374
2375 /**
2376 * Remove network active listener previously registered with
Robert Greenwaltad35b132014-09-04 16:44:35 -07002377 * {@link #addDefaultNetworkActiveListener}.
Dianne Hackborn5ac88162014-02-26 16:20:52 -08002378 *
2379 * @param l Previously registered listener.
2380 */
Chalard Jean158702d2019-01-07 19:26:34 +09002381 public void removeDefaultNetworkActiveListener(@NonNull OnNetworkActiveListener l) {
Dianne Hackborn5ac88162014-02-26 16:20:52 -08002382 INetworkActivityListener rl = mNetworkActivityListeners.get(l);
Remi NGUYEN VAN81f68422021-03-15 07:31:54 +00002383 if (rl == null) {
2384 throw new IllegalArgumentException("Listener was not registered.");
2385 }
Dianne Hackborn5ac88162014-02-26 16:20:52 -08002386 try {
lucaslin1193a5d2021-01-21 02:04:15 +08002387 mService.registerNetworkActivityListener(rl);
Dianne Hackborn5ac88162014-02-26 16:20:52 -08002388 } catch (RemoteException e) {
Jeff Sharkeyc78f85c2016-03-01 19:27:23 -07002389 throw e.rethrowFromSystemServer();
Dianne Hackborn5ac88162014-02-26 16:20:52 -08002390 }
2391 }
2392
2393 /**
2394 * Return whether the data network is currently active. An active network means that
2395 * it is currently in a high power state for performing data transmission. On some
2396 * types of networks, it may be expensive to move and stay in such a state, so it is
2397 * more power efficient to batch network traffic together when the radio is already in
2398 * this state. This method tells you whether right now is currently a good time to
2399 * initiate network traffic, as the network is already active.
2400 */
Robert Greenwaltf57c03c2014-06-11 16:05:07 -07002401 public boolean isDefaultNetworkActive() {
Dianne Hackborn5ac88162014-02-26 16:20:52 -08002402 try {
lucaslin1193a5d2021-01-21 02:04:15 +08002403 return mService.isDefaultNetworkActive();
Dianne Hackborn5ac88162014-02-26 16:20:52 -08002404 } catch (RemoteException e) {
Jeff Sharkeyc78f85c2016-03-01 19:27:23 -07002405 throw e.rethrowFromSystemServer();
Dianne Hackborn5ac88162014-02-26 16:20:52 -08002406 }
Dianne Hackborn5ac88162014-02-26 16:20:52 -08002407 }
2408
2409 /**
The Android Open Source Project28527d22009-03-03 19:31:44 -08002410 * {@hide}
2411 */
Lorenzo Colitti2780e4b2015-07-29 11:41:21 +09002412 public ConnectivityManager(Context context, IConnectivityManager service) {
Remi NGUYEN VAN81f68422021-03-15 07:31:54 +00002413 mContext = Objects.requireNonNull(context, "missing context");
2414 mService = Objects.requireNonNull(service, "missing IConnectivityManager");
Amos Bianchia9b415a2020-03-04 11:07:38 -08002415 mTetheringManager = (TetheringManager) mContext.getSystemService(Context.TETHERING_SERVICE);
Paul Jensenc0618a62014-12-10 15:12:18 -05002416 sInstance = this;
The Android Open Source Project28527d22009-03-03 19:31:44 -08002417 }
Robert Greenwalt0c4828c2010-01-26 11:40:34 -08002418
Jeff Sharkey2d9e5a52012-04-04 20:40:58 -07002419 /** {@hide} */
Mathew Inwoodc5b9bec2018-08-08 14:52:47 +01002420 @UnsupportedAppUsage
Jeff Sharkey2d9e5a52012-04-04 20:40:58 -07002421 public static ConnectivityManager from(Context context) {
2422 return (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
2423 }
2424
Remi NGUYEN VAN85391292018-12-27 16:43:56 +09002425 /** @hide */
2426 public NetworkRequest getDefaultRequest() {
2427 try {
2428 // This is not racy as the default request is final in ConnectivityService.
2429 return mService.getDefaultRequest();
2430 } catch (RemoteException e) {
2431 throw e.rethrowFromSystemServer();
2432 }
2433 }
2434
Robert Greenwalt0c4828c2010-01-26 11:40:34 -08002435 /**
Philip P. Moltmann7bc33df2020-03-26 11:50:35 -07002436 * Check if the package is a allowed to write settings. This also accounts that such an access
2437 * happened.
2438 *
2439 * @return {@code true} iff the package is allowed to write settings.
2440 */
2441 // TODO: Remove method and replace with direct call once R code is pushed to AOSP
2442 private static boolean checkAndNoteWriteSettingsOperation(@NonNull Context context, int uid,
2443 @NonNull String callingPackage, @Nullable String callingAttributionTag,
2444 boolean throwException) {
2445 return Settings.checkAndNoteWriteSettingsOperation(context, uid, callingPackage,
2446 throwException);
2447 }
2448
2449 /**
Paul Jensenc0618a62014-12-10 15:12:18 -05002450 * @deprecated - use getSystemService. This is a kludge to support static access in certain
2451 * situations where a Context pointer is unavailable.
2452 * @hide
2453 */
Aurimas Liutikase80a0eb2016-05-24 15:22:55 -07002454 @Deprecated
Paul Jensenee2f45d2015-03-10 10:54:12 -04002455 static ConnectivityManager getInstanceOrNull() {
2456 return sInstance;
2457 }
2458
2459 /**
2460 * @deprecated - use getSystemService. This is a kludge to support static access in certain
2461 * situations where a Context pointer is unavailable.
2462 * @hide
2463 */
Aurimas Liutikase80a0eb2016-05-24 15:22:55 -07002464 @Deprecated
Mathew Inwoodc5b9bec2018-08-08 14:52:47 +01002465 @UnsupportedAppUsage
Paul Jensenee2f45d2015-03-10 10:54:12 -04002466 private static ConnectivityManager getInstance() {
2467 if (getInstanceOrNull() == null) {
Paul Jensenc0618a62014-12-10 15:12:18 -05002468 throw new IllegalStateException("No ConnectivityManager yet constructed");
2469 }
Paul Jensenee2f45d2015-03-10 10:54:12 -04002470 return getInstanceOrNull();
Paul Jensenc0618a62014-12-10 15:12:18 -05002471 }
2472
2473 /**
Robert Greenwaltfaa4b402013-02-15 10:56:35 -08002474 * Get the set of tetherable, available interfaces. This list is limited by
2475 * device configuration and current interface existence.
2476 *
2477 * @return an array of 0 or more Strings of tetherable interface names.
2478 *
markchien6ae63e52020-01-21 13:11:06 +08002479 * @deprecated Use {@link TetheringEventCallback#onTetherableInterfacesChanged(List)} instead.
Robert Greenwalt0c4828c2010-01-26 11:40:34 -08002480 * {@hide}
2481 */
Jeff Sharkey656584a2017-04-24 11:18:03 -06002482 @RequiresPermission(android.Manifest.permission.ACCESS_NETWORK_STATE)
Mathew Inwoodc5b9bec2018-08-08 14:52:47 +01002483 @UnsupportedAppUsage
markchien6ae63e52020-01-21 13:11:06 +08002484 @Deprecated
Robert Greenwalt0c4828c2010-01-26 11:40:34 -08002485 public String[] getTetherableIfaces() {
Amos Bianchia9b415a2020-03-04 11:07:38 -08002486 return mTetheringManager.getTetherableIfaces();
Robert Greenwalt0c4828c2010-01-26 11:40:34 -08002487 }
2488
2489 /**
Robert Greenwaltfaa4b402013-02-15 10:56:35 -08002490 * Get the set of tethered interfaces.
2491 *
2492 * @return an array of 0 or more String of currently tethered interface names.
2493 *
markchien6ae63e52020-01-21 13:11:06 +08002494 * @deprecated Use {@link TetheringEventCallback#onTetherableInterfacesChanged(List)} instead.
Robert Greenwalt0c4828c2010-01-26 11:40:34 -08002495 * {@hide}
2496 */
Jeff Sharkey656584a2017-04-24 11:18:03 -06002497 @RequiresPermission(android.Manifest.permission.ACCESS_NETWORK_STATE)
Mathew Inwoodc5b9bec2018-08-08 14:52:47 +01002498 @UnsupportedAppUsage
markchien6ae63e52020-01-21 13:11:06 +08002499 @Deprecated
Robert Greenwalt0c4828c2010-01-26 11:40:34 -08002500 public String[] getTetheredIfaces() {
Amos Bianchia9b415a2020-03-04 11:07:38 -08002501 return mTetheringManager.getTetheredIfaces();
Robert Greenwalt0c4828c2010-01-26 11:40:34 -08002502 }
2503
2504 /**
Robert Greenwaltfaa4b402013-02-15 10:56:35 -08002505 * Get the set of interface names which attempted to tether but
2506 * failed. Re-attempting to tether may cause them to reset to the Tethered
2507 * state. Alternatively, causing the interface to be destroyed and recreated
2508 * may cause them to reset to the available state.
2509 * {@link ConnectivityManager#getLastTetherError} can be used to get more
2510 * information on the cause of the errors.
2511 *
2512 * @return an array of 0 or more String indicating the interface names
2513 * which failed to tether.
2514 *
markchien6ae63e52020-01-21 13:11:06 +08002515 * @deprecated Use {@link TetheringEventCallback#onError(String, int)} instead.
Robert Greenwalt0c4828c2010-01-26 11:40:34 -08002516 * {@hide}
2517 */
Jeff Sharkey656584a2017-04-24 11:18:03 -06002518 @RequiresPermission(android.Manifest.permission.ACCESS_NETWORK_STATE)
Mathew Inwoodc5b9bec2018-08-08 14:52:47 +01002519 @UnsupportedAppUsage
markchien6ae63e52020-01-21 13:11:06 +08002520 @Deprecated
Robert Greenwalt4283ded2010-03-02 17:25:02 -08002521 public String[] getTetheringErroredIfaces() {
Amos Bianchia9b415a2020-03-04 11:07:38 -08002522 return mTetheringManager.getTetheringErroredIfaces();
Robert Greenwalt0c4828c2010-01-26 11:40:34 -08002523 }
2524
2525 /**
Robert Greenwalte594a762014-06-23 14:53:42 -07002526 * Get the set of tethered dhcp ranges.
2527 *
markchien2e6ba522020-02-14 11:55:48 +08002528 * @deprecated This method is not supported.
2529 * TODO: remove this function when all of clients are removed.
Robert Greenwalte594a762014-06-23 14:53:42 -07002530 * {@hide}
2531 */
paulhu8e96a752019-08-12 16:25:11 +08002532 @RequiresPermission(android.Manifest.permission.NETWORK_SETTINGS)
markchien6ae63e52020-01-21 13:11:06 +08002533 @Deprecated
Robert Greenwalte594a762014-06-23 14:53:42 -07002534 public String[] getTetheredDhcpRanges() {
markchien2e6ba522020-02-14 11:55:48 +08002535 throw new UnsupportedOperationException("getTetheredDhcpRanges is not supported");
Robert Greenwalte594a762014-06-23 14:53:42 -07002536 }
2537
2538 /**
Robert Greenwaltfaa4b402013-02-15 10:56:35 -08002539 * Attempt to tether the named interface. This will setup a dhcp server
2540 * on the interface, forward and NAT IP packets and forward DNS requests
2541 * to the best active upstream network interface. Note that if no upstream
2542 * IP network interface is available, dhcp will still run and traffic will be
2543 * allowed between the tethered devices and this device, though upstream net
2544 * access will of course fail until an upstream network interface becomes
2545 * active.
Lorenzo Colitti0b40c872015-10-15 16:29:00 +09002546 *
2547 * <p>This method requires the caller to hold either the
2548 * {@link android.Manifest.permission#CHANGE_NETWORK_STATE} permission
2549 * or the ability to modify system settings as determined by
2550 * {@link android.provider.Settings.System#canWrite}.</p>
Robert Greenwaltfaa4b402013-02-15 10:56:35 -08002551 *
Jeremy Klein3dabcb92016-01-22 14:11:45 -08002552 * <p>WARNING: New clients should not use this function. The only usages should be in PanService
2553 * and WifiStateMachine which need direct access. All other clients should use
2554 * {@link #startTethering} and {@link #stopTethering} which encapsulate proper provisioning
2555 * logic.</p>
2556 *
Robert Greenwaltfaa4b402013-02-15 10:56:35 -08002557 * @param iface the interface name to tether.
2558 * @return error a {@code TETHER_ERROR} value indicating success or failure type
markchien91c78e52020-01-20 19:31:56 +08002559 * @deprecated Use {@link TetheringManager#startTethering} instead
Robert Greenwaltfaa4b402013-02-15 10:56:35 -08002560 *
Robert Greenwalt0c4828c2010-01-26 11:40:34 -08002561 * {@hide}
2562 */
Mathew Inwoode1a17ba2020-11-04 09:29:36 +00002563 @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.R, trackingBug = 170729553)
markchien91c78e52020-01-20 19:31:56 +08002564 @Deprecated
Robert Greenwalt4283ded2010-03-02 17:25:02 -08002565 public int tether(String iface) {
Amos Bianchia9b415a2020-03-04 11:07:38 -08002566 return mTetheringManager.tether(iface);
Robert Greenwalt4283ded2010-03-02 17:25:02 -08002567 }
2568
2569 /**
Robert Greenwaltfaa4b402013-02-15 10:56:35 -08002570 * Stop tethering the named interface.
Lorenzo Colitti0b40c872015-10-15 16:29:00 +09002571 *
2572 * <p>This method requires the caller to hold either the
2573 * {@link android.Manifest.permission#CHANGE_NETWORK_STATE} permission
2574 * or the ability to modify system settings as determined by
2575 * {@link android.provider.Settings.System#canWrite}.</p>
Robert Greenwaltfaa4b402013-02-15 10:56:35 -08002576 *
Jeremy Klein3dabcb92016-01-22 14:11:45 -08002577 * <p>WARNING: New clients should not use this function. The only usages should be in PanService
2578 * and WifiStateMachine which need direct access. All other clients should use
2579 * {@link #startTethering} and {@link #stopTethering} which encapsulate proper provisioning
2580 * logic.</p>
2581 *
Robert Greenwaltfaa4b402013-02-15 10:56:35 -08002582 * @param iface the interface name to untether.
2583 * @return error a {@code TETHER_ERROR} value indicating success or failure type
2584 *
Robert Greenwalt4283ded2010-03-02 17:25:02 -08002585 * {@hide}
2586 */
Mathew Inwoodc5b9bec2018-08-08 14:52:47 +01002587 @UnsupportedAppUsage
markchien6ae63e52020-01-21 13:11:06 +08002588 @Deprecated
Robert Greenwalt4283ded2010-03-02 17:25:02 -08002589 public int untether(String iface) {
Amos Bianchia9b415a2020-03-04 11:07:38 -08002590 return mTetheringManager.untether(iface);
Robert Greenwalt0c4828c2010-01-26 11:40:34 -08002591 }
Robert Greenwalt8e87f122010-02-11 18:18:40 -08002592
2593 /**
Robert Greenwaltfaa4b402013-02-15 10:56:35 -08002594 * Check if the device allows for tethering. It may be disabled via
Dianne Hackborn5ac88162014-02-26 16:20:52 -08002595 * {@code ro.tether.denied} system property, Settings.TETHER_SUPPORTED or
Robert Greenwaltfaa4b402013-02-15 10:56:35 -08002596 * due to device configuration.
2597 *
Chalard Jeanffacaef2017-09-26 15:45:18 +09002598 * <p>If this app does not have permission to use this API, it will always
2599 * return false rather than throw an exception.</p>
2600 *
2601 * <p>If the device has a hotspot provisioning app, the caller is required to hold the
2602 * {@link android.Manifest.permission.TETHER_PRIVILEGED} permission.</p>
2603 *
2604 * <p>Otherwise, this method requires the caller to hold the ability to modify system
2605 * settings as determined by {@link android.provider.Settings.System#canWrite}.</p>
2606 *
Robert Greenwaltfaa4b402013-02-15 10:56:35 -08002607 * @return a boolean - {@code true} indicating Tethering is supported.
2608 *
markchien6ae63e52020-01-21 13:11:06 +08002609 * @deprecated Use {@link TetheringEventCallback#onTetheringSupported(boolean)} instead.
Robert Greenwalt8e87f122010-02-11 18:18:40 -08002610 * {@hide}
2611 */
Jeremy Klein3dabcb92016-01-22 14:11:45 -08002612 @SystemApi
Chalard Jeanffacaef2017-09-26 15:45:18 +09002613 @RequiresPermission(anyOf = {android.Manifest.permission.TETHER_PRIVILEGED,
2614 android.Manifest.permission.WRITE_SETTINGS})
Robert Greenwalt8e87f122010-02-11 18:18:40 -08002615 public boolean isTetheringSupported() {
Amos Bianchia9b415a2020-03-04 11:07:38 -08002616 return mTetheringManager.isTetheringSupported();
Robert Greenwalt8e87f122010-02-11 18:18:40 -08002617 }
2618
2619 /**
Jeremy Klein3dabcb92016-01-22 14:11:45 -08002620 * Callback for use with {@link #startTethering} to find out whether tethering succeeded.
markchien91c78e52020-01-20 19:31:56 +08002621 *
2622 * @deprecated Use {@link TetheringManager.StartTetheringCallback} instead.
Jeremy Klein3dabcb92016-01-22 14:11:45 -08002623 * @hide
2624 */
2625 @SystemApi
markchien91c78e52020-01-20 19:31:56 +08002626 @Deprecated
Jeremy Klein3dabcb92016-01-22 14:11:45 -08002627 public static abstract class OnStartTetheringCallback {
2628 /**
2629 * Called when tethering has been successfully started.
2630 */
Chalard Jean9dd11612018-06-04 16:52:49 +09002631 public void onTetheringStarted() {}
Jeremy Klein3dabcb92016-01-22 14:11:45 -08002632
2633 /**
2634 * Called when starting tethering failed.
2635 */
Chalard Jean9dd11612018-06-04 16:52:49 +09002636 public void onTetheringFailed() {}
Jeremy Klein3dabcb92016-01-22 14:11:45 -08002637 }
2638
2639 /**
2640 * Convenient overload for
2641 * {@link #startTethering(int, boolean, OnStartTetheringCallback, Handler)} which passes a null
2642 * handler to run on the current thread's {@link Looper}.
markchien91c78e52020-01-20 19:31:56 +08002643 *
2644 * @deprecated Use {@link TetheringManager#startTethering} instead.
Jeremy Klein3dabcb92016-01-22 14:11:45 -08002645 * @hide
2646 */
2647 @SystemApi
markchien91c78e52020-01-20 19:31:56 +08002648 @Deprecated
Udam Saini8f7d6a72017-06-07 12:06:28 -07002649 @RequiresPermission(android.Manifest.permission.TETHER_PRIVILEGED)
Jeremy Klein3dabcb92016-01-22 14:11:45 -08002650 public void startTethering(int type, boolean showProvisioningUi,
2651 final OnStartTetheringCallback callback) {
2652 startTethering(type, showProvisioningUi, callback, null);
2653 }
2654
2655 /**
2656 * Runs tether provisioning for the given type if needed and then starts tethering if
2657 * the check succeeds. If no carrier provisioning is required for tethering, tethering is
2658 * enabled immediately. If provisioning fails, tethering will not be enabled. It also
2659 * schedules tether provisioning re-checks if appropriate.
2660 *
2661 * @param type The type of tethering to start. Must be one of
2662 * {@link ConnectivityManager.TETHERING_WIFI},
2663 * {@link ConnectivityManager.TETHERING_USB}, or
2664 * {@link ConnectivityManager.TETHERING_BLUETOOTH}.
2665 * @param showProvisioningUi a boolean indicating to show the provisioning app UI if there
2666 * is one. This should be true the first time this function is called and also any time
2667 * the user can see this UI. It gives users information from their carrier about the
2668 * check failing and how they can sign up for tethering if possible.
2669 * @param callback an {@link OnStartTetheringCallback} which will be called to notify the caller
2670 * of the result of trying to tether.
2671 * @param handler {@link Handler} to specify the thread upon which the callback will be invoked.
markchien91c78e52020-01-20 19:31:56 +08002672 *
2673 * @deprecated Use {@link TetheringManager#startTethering} instead.
Jeremy Klein3dabcb92016-01-22 14:11:45 -08002674 * @hide
2675 */
2676 @SystemApi
markchien91c78e52020-01-20 19:31:56 +08002677 @Deprecated
Jeff Sharkey9b39e9a2017-06-02 17:36:26 -06002678 @RequiresPermission(android.Manifest.permission.TETHER_PRIVILEGED)
Jeremy Klein3dabcb92016-01-22 14:11:45 -08002679 public void startTethering(int type, boolean showProvisioningUi,
2680 final OnStartTetheringCallback callback, Handler handler) {
Remi NGUYEN VAN81f68422021-03-15 07:31:54 +00002681 Objects.requireNonNull(callback, "OnStartTetheringCallback cannot be null.");
Jeremy Klein35e99ea2016-03-12 16:29:54 -08002682
markchien91c78e52020-01-20 19:31:56 +08002683 final Executor executor = new Executor() {
Jeremy Klein3dabcb92016-01-22 14:11:45 -08002684 @Override
markchien91c78e52020-01-20 19:31:56 +08002685 public void execute(Runnable command) {
2686 if (handler == null) {
2687 command.run();
Jeremy Klein3dabcb92016-01-22 14:11:45 -08002688 } else {
markchien91c78e52020-01-20 19:31:56 +08002689 handler.post(command);
Jeremy Klein3dabcb92016-01-22 14:11:45 -08002690 }
2691 }
2692 };
Jeremy Klein35e99ea2016-03-12 16:29:54 -08002693
markchien91c78e52020-01-20 19:31:56 +08002694 final StartTetheringCallback tetheringCallback = new StartTetheringCallback() {
2695 @Override
2696 public void onTetheringStarted() {
2697 callback.onTetheringStarted();
2698 }
2699
2700 @Override
markchienf47d8342020-03-19 13:37:43 +08002701 public void onTetheringFailed(final int error) {
markchien91c78e52020-01-20 19:31:56 +08002702 callback.onTetheringFailed();
2703 }
2704 };
2705
2706 final TetheringRequest request = new TetheringRequest.Builder(type)
markchienf47d8342020-03-19 13:37:43 +08002707 .setShouldShowEntitlementUi(showProvisioningUi).build();
markchien91c78e52020-01-20 19:31:56 +08002708
Amos Bianchia9b415a2020-03-04 11:07:38 -08002709 mTetheringManager.startTethering(request, executor, tetheringCallback);
Jeremy Klein3dabcb92016-01-22 14:11:45 -08002710 }
2711
2712 /**
2713 * Stops tethering for the given type. Also cancels any provisioning rechecks for that type if
2714 * applicable.
2715 *
2716 * @param type The type of tethering to stop. Must be one of
2717 * {@link ConnectivityManager.TETHERING_WIFI},
2718 * {@link ConnectivityManager.TETHERING_USB}, or
2719 * {@link ConnectivityManager.TETHERING_BLUETOOTH}.
markchien6ae63e52020-01-21 13:11:06 +08002720 *
2721 * @deprecated Use {@link TetheringManager#stopTethering} instead.
Jeremy Klein3dabcb92016-01-22 14:11:45 -08002722 * @hide
2723 */
2724 @SystemApi
markchien6ae63e52020-01-21 13:11:06 +08002725 @Deprecated
Jeff Sharkey9b39e9a2017-06-02 17:36:26 -06002726 @RequiresPermission(android.Manifest.permission.TETHER_PRIVILEGED)
Jeremy Klein3dabcb92016-01-22 14:11:45 -08002727 public void stopTethering(int type) {
Amos Bianchia9b415a2020-03-04 11:07:38 -08002728 mTetheringManager.stopTethering(type);
Jeremy Klein3dabcb92016-01-22 14:11:45 -08002729 }
2730
2731 /**
markchien4ef53e82019-02-27 14:56:11 +08002732 * Callback for use with {@link registerTetheringEventCallback} to find out tethering
2733 * upstream status.
2734 *
Nathan Harold74f0fb82020-01-23 18:03:46 -08002735 * @deprecated Use {@link TetheringManager#OnTetheringEventCallback} instead.
markchien6ae63e52020-01-21 13:11:06 +08002736 * @hide
markchien4ef53e82019-02-27 14:56:11 +08002737 */
2738 @SystemApi
markchien6ae63e52020-01-21 13:11:06 +08002739 @Deprecated
markchien4ef53e82019-02-27 14:56:11 +08002740 public abstract static class OnTetheringEventCallback {
2741
2742 /**
2743 * Called when tethering upstream changed. This can be called multiple times and can be
2744 * called any time.
2745 *
2746 * @param network the {@link Network} of tethering upstream. Null means tethering doesn't
2747 * have any upstream.
2748 */
2749 public void onUpstreamChanged(@Nullable Network network) {}
2750 }
2751
markchien6ae63e52020-01-21 13:11:06 +08002752 @GuardedBy("mTetheringEventCallbacks")
2753 private final ArrayMap<OnTetheringEventCallback, TetheringEventCallback>
2754 mTetheringEventCallbacks = new ArrayMap<>();
2755
markchien4ef53e82019-02-27 14:56:11 +08002756 /**
2757 * Start listening to tethering change events. Any new added callback will receive the last
markchien42e22092019-04-03 10:43:09 +08002758 * tethering status right away. If callback is registered when tethering has no upstream or
markchien4ef53e82019-02-27 14:56:11 +08002759 * disabled, {@link OnTetheringEventCallback#onUpstreamChanged} will immediately be called
2760 * with a null argument. The same callback object cannot be registered twice.
2761 *
2762 * @param executor the executor on which callback will be invoked.
2763 * @param callback the callback to be called when tethering has change events.
markchien6ae63e52020-01-21 13:11:06 +08002764 *
Nathan Harold74f0fb82020-01-23 18:03:46 -08002765 * @deprecated Use {@link TetheringManager#registerTetheringEventCallback} instead.
markchien4ef53e82019-02-27 14:56:11 +08002766 * @hide
2767 */
2768 @SystemApi
markchien6ae63e52020-01-21 13:11:06 +08002769 @Deprecated
markchien4ef53e82019-02-27 14:56:11 +08002770 @RequiresPermission(android.Manifest.permission.TETHER_PRIVILEGED)
2771 public void registerTetheringEventCallback(
2772 @NonNull @CallbackExecutor Executor executor,
2773 @NonNull final OnTetheringEventCallback callback) {
Remi NGUYEN VAN81f68422021-03-15 07:31:54 +00002774 Objects.requireNonNull(callback, "OnTetheringEventCallback cannot be null.");
markchien4ef53e82019-02-27 14:56:11 +08002775
markchien6ae63e52020-01-21 13:11:06 +08002776 final TetheringEventCallback tetherCallback =
2777 new TetheringEventCallback() {
2778 @Override
2779 public void onUpstreamChanged(@Nullable Network network) {
2780 callback.onUpstreamChanged(network);
2781 }
2782 };
2783
2784 synchronized (mTetheringEventCallbacks) {
2785 mTetheringEventCallbacks.put(callback, tetherCallback);
Amos Bianchia9b415a2020-03-04 11:07:38 -08002786 mTetheringManager.registerTetheringEventCallback(executor, tetherCallback);
markchien6ae63e52020-01-21 13:11:06 +08002787 }
markchien4ef53e82019-02-27 14:56:11 +08002788 }
2789
2790 /**
2791 * Remove tethering event callback previously registered with
2792 * {@link #registerTetheringEventCallback}.
2793 *
2794 * @param callback previously registered callback.
markchien6ae63e52020-01-21 13:11:06 +08002795 *
2796 * @deprecated Use {@link TetheringManager#unregisterTetheringEventCallback} instead.
markchien4ef53e82019-02-27 14:56:11 +08002797 * @hide
2798 */
2799 @SystemApi
markchien6ae63e52020-01-21 13:11:06 +08002800 @Deprecated
markchien4ef53e82019-02-27 14:56:11 +08002801 @RequiresPermission(android.Manifest.permission.TETHER_PRIVILEGED)
2802 public void unregisterTetheringEventCallback(
2803 @NonNull final OnTetheringEventCallback callback) {
markchien6ae63e52020-01-21 13:11:06 +08002804 Objects.requireNonNull(callback, "The callback must be non-null");
2805 synchronized (mTetheringEventCallbacks) {
2806 final TetheringEventCallback tetherCallback =
2807 mTetheringEventCallbacks.remove(callback);
Amos Bianchia9b415a2020-03-04 11:07:38 -08002808 mTetheringManager.unregisterTetheringEventCallback(tetherCallback);
markchien6ae63e52020-01-21 13:11:06 +08002809 }
markchien4ef53e82019-02-27 14:56:11 +08002810 }
2811
2812
2813 /**
Robert Greenwaltfaa4b402013-02-15 10:56:35 -08002814 * Get the list of regular expressions that define any tetherable
2815 * USB network interfaces. If USB tethering is not supported by the
2816 * device, this list should be empty.
2817 *
2818 * @return an array of 0 or more regular expression Strings defining
2819 * what interfaces are considered tetherable usb interfaces.
2820 *
markchien6ae63e52020-01-21 13:11:06 +08002821 * @deprecated Use {@link TetheringEventCallback#onTetherableInterfaceRegexpsChanged} instead.
Robert Greenwalt8e87f122010-02-11 18:18:40 -08002822 * {@hide}
2823 */
Jeff Sharkey656584a2017-04-24 11:18:03 -06002824 @RequiresPermission(android.Manifest.permission.ACCESS_NETWORK_STATE)
Mathew Inwoodc5b9bec2018-08-08 14:52:47 +01002825 @UnsupportedAppUsage
markchien6ae63e52020-01-21 13:11:06 +08002826 @Deprecated
Robert Greenwalt8e87f122010-02-11 18:18:40 -08002827 public String[] getTetherableUsbRegexs() {
Amos Bianchia9b415a2020-03-04 11:07:38 -08002828 return mTetheringManager.getTetherableUsbRegexs();
Robert Greenwalt8e87f122010-02-11 18:18:40 -08002829 }
2830
2831 /**
Robert Greenwaltfaa4b402013-02-15 10:56:35 -08002832 * Get the list of regular expressions that define any tetherable
2833 * Wifi network interfaces. If Wifi tethering is not supported by the
2834 * device, this list should be empty.
2835 *
2836 * @return an array of 0 or more regular expression Strings defining
2837 * what interfaces are considered tetherable wifi interfaces.
2838 *
markchien6ae63e52020-01-21 13:11:06 +08002839 * @deprecated Use {@link TetheringEventCallback#onTetherableInterfaceRegexpsChanged} instead.
Robert Greenwalt8e87f122010-02-11 18:18:40 -08002840 * {@hide}
2841 */
Jeff Sharkey656584a2017-04-24 11:18:03 -06002842 @RequiresPermission(android.Manifest.permission.ACCESS_NETWORK_STATE)
Mathew Inwoodc5b9bec2018-08-08 14:52:47 +01002843 @UnsupportedAppUsage
markchien6ae63e52020-01-21 13:11:06 +08002844 @Deprecated
Robert Greenwalt8e87f122010-02-11 18:18:40 -08002845 public String[] getTetherableWifiRegexs() {
Amos Bianchia9b415a2020-03-04 11:07:38 -08002846 return mTetheringManager.getTetherableWifiRegexs();
Robert Greenwalt8e87f122010-02-11 18:18:40 -08002847 }
Robert Greenwalt4283ded2010-03-02 17:25:02 -08002848
Danica Chang96567052010-08-11 14:54:43 -07002849 /**
Robert Greenwaltfaa4b402013-02-15 10:56:35 -08002850 * Get the list of regular expressions that define any tetherable
2851 * Bluetooth network interfaces. If Bluetooth tethering is not supported by the
2852 * device, this list should be empty.
2853 *
2854 * @return an array of 0 or more regular expression Strings defining
2855 * what interfaces are considered tetherable bluetooth interfaces.
2856 *
markchien6ae63e52020-01-21 13:11:06 +08002857 * @deprecated Use {@link TetheringEventCallback#onTetherableInterfaceRegexpsChanged(
2858 *TetheringManager.TetheringInterfaceRegexps)} instead.
Danica Chang96567052010-08-11 14:54:43 -07002859 * {@hide}
2860 */
Jeff Sharkey656584a2017-04-24 11:18:03 -06002861 @RequiresPermission(android.Manifest.permission.ACCESS_NETWORK_STATE)
Mathew Inwoodc5b9bec2018-08-08 14:52:47 +01002862 @UnsupportedAppUsage
markchien6ae63e52020-01-21 13:11:06 +08002863 @Deprecated
Danica Chang96567052010-08-11 14:54:43 -07002864 public String[] getTetherableBluetoothRegexs() {
Amos Bianchia9b415a2020-03-04 11:07:38 -08002865 return mTetheringManager.getTetherableBluetoothRegexs();
Danica Chang96567052010-08-11 14:54:43 -07002866 }
2867
Mike Lockwooded4a1742011-07-19 13:04:47 -07002868 /**
Robert Greenwaltfaa4b402013-02-15 10:56:35 -08002869 * Attempt to both alter the mode of USB and Tethering of USB. A
2870 * utility method to deal with some of the complexity of USB - will
2871 * attempt to switch to Rndis and subsequently tether the resulting
2872 * interface on {@code true} or turn off tethering and switch off
2873 * Rndis on {@code false}.
Lorenzo Colitti0b40c872015-10-15 16:29:00 +09002874 *
2875 * <p>This method requires the caller to hold either the
2876 * {@link android.Manifest.permission#CHANGE_NETWORK_STATE} permission
2877 * or the ability to modify system settings as determined by
2878 * {@link android.provider.Settings.System#canWrite}.</p>
Robert Greenwaltfaa4b402013-02-15 10:56:35 -08002879 *
2880 * @param enable a boolean - {@code true} to enable tethering
2881 * @return error a {@code TETHER_ERROR} value indicating success or failure type
markchien91c78e52020-01-20 19:31:56 +08002882 * @deprecated Use {@link TetheringManager#startTethering} instead
Robert Greenwaltfaa4b402013-02-15 10:56:35 -08002883 *
Mike Lockwooded4a1742011-07-19 13:04:47 -07002884 * {@hide}
2885 */
Mathew Inwoodc5b9bec2018-08-08 14:52:47 +01002886 @UnsupportedAppUsage
markchien91c78e52020-01-20 19:31:56 +08002887 @Deprecated
Mike Lockwooded4a1742011-07-19 13:04:47 -07002888 public int setUsbTethering(boolean enable) {
Amos Bianchia9b415a2020-03-04 11:07:38 -08002889 return mTetheringManager.setUsbTethering(enable);
Mike Lockwooded4a1742011-07-19 13:04:47 -07002890 }
2891
markchien6ae63e52020-01-21 13:11:06 +08002892 /**
2893 * @deprecated Use {@link TetheringManager#TETHER_ERROR_NO_ERROR}.
2894 * {@hide}
2895 */
markchien0f45bb92019-01-16 17:44:13 +08002896 @SystemApi
markchien6ae63e52020-01-21 13:11:06 +08002897 @Deprecated
Remi NGUYEN VAN09f8ed22021-02-15 18:52:06 +09002898 public static final int TETHER_ERROR_NO_ERROR = 0;
markchien6ae63e52020-01-21 13:11:06 +08002899 /**
2900 * @deprecated Use {@link TetheringManager#TETHER_ERROR_UNKNOWN_IFACE}.
2901 * {@hide}
2902 */
2903 @Deprecated
2904 public static final int TETHER_ERROR_UNKNOWN_IFACE =
2905 TetheringManager.TETHER_ERROR_UNKNOWN_IFACE;
2906 /**
2907 * @deprecated Use {@link TetheringManager#TETHER_ERROR_SERVICE_UNAVAIL}.
2908 * {@hide}
2909 */
2910 @Deprecated
2911 public static final int TETHER_ERROR_SERVICE_UNAVAIL =
2912 TetheringManager.TETHER_ERROR_SERVICE_UNAVAIL;
2913 /**
2914 * @deprecated Use {@link TetheringManager#TETHER_ERROR_UNSUPPORTED}.
2915 * {@hide}
2916 */
2917 @Deprecated
2918 public static final int TETHER_ERROR_UNSUPPORTED = TetheringManager.TETHER_ERROR_UNSUPPORTED;
2919 /**
2920 * @deprecated Use {@link TetheringManager#TETHER_ERROR_UNAVAIL_IFACE}.
2921 * {@hide}
2922 */
2923 @Deprecated
2924 public static final int TETHER_ERROR_UNAVAIL_IFACE =
2925 TetheringManager.TETHER_ERROR_UNAVAIL_IFACE;
2926 /**
markchienf47d8342020-03-19 13:37:43 +08002927 * @deprecated Use {@link TetheringManager#TETHER_ERROR_INTERNAL_ERROR}.
markchien6ae63e52020-01-21 13:11:06 +08002928 * {@hide}
2929 */
2930 @Deprecated
markchienf47d8342020-03-19 13:37:43 +08002931 public static final int TETHER_ERROR_MASTER_ERROR =
2932 TetheringManager.TETHER_ERROR_INTERNAL_ERROR;
markchien6ae63e52020-01-21 13:11:06 +08002933 /**
2934 * @deprecated Use {@link TetheringManager#TETHER_ERROR_TETHER_IFACE_ERROR}.
2935 * {@hide}
2936 */
2937 @Deprecated
2938 public static final int TETHER_ERROR_TETHER_IFACE_ERROR =
2939 TetheringManager.TETHER_ERROR_TETHER_IFACE_ERROR;
2940 /**
2941 * @deprecated Use {@link TetheringManager#TETHER_ERROR_UNTETHER_IFACE_ERROR}.
2942 * {@hide}
2943 */
2944 @Deprecated
2945 public static final int TETHER_ERROR_UNTETHER_IFACE_ERROR =
2946 TetheringManager.TETHER_ERROR_UNTETHER_IFACE_ERROR;
2947 /**
markchienf47d8342020-03-19 13:37:43 +08002948 * @deprecated Use {@link TetheringManager#TETHER_ERROR_ENABLE_FORWARDING_ERROR}.
markchien6ae63e52020-01-21 13:11:06 +08002949 * {@hide}
2950 */
2951 @Deprecated
2952 public static final int TETHER_ERROR_ENABLE_NAT_ERROR =
markchienf47d8342020-03-19 13:37:43 +08002953 TetheringManager.TETHER_ERROR_ENABLE_FORWARDING_ERROR;
markchien6ae63e52020-01-21 13:11:06 +08002954 /**
markchienf47d8342020-03-19 13:37:43 +08002955 * @deprecated Use {@link TetheringManager#TETHER_ERROR_DISABLE_FORWARDING_ERROR}.
markchien6ae63e52020-01-21 13:11:06 +08002956 * {@hide}
2957 */
2958 @Deprecated
2959 public static final int TETHER_ERROR_DISABLE_NAT_ERROR =
markchienf47d8342020-03-19 13:37:43 +08002960 TetheringManager.TETHER_ERROR_DISABLE_FORWARDING_ERROR;
markchien6ae63e52020-01-21 13:11:06 +08002961 /**
2962 * @deprecated Use {@link TetheringManager#TETHER_ERROR_IFACE_CFG_ERROR}.
2963 * {@hide}
2964 */
2965 @Deprecated
2966 public static final int TETHER_ERROR_IFACE_CFG_ERROR =
2967 TetheringManager.TETHER_ERROR_IFACE_CFG_ERROR;
2968 /**
markchienf47d8342020-03-19 13:37:43 +08002969 * @deprecated Use {@link TetheringManager#TETHER_ERROR_PROVISIONING_FAILED}.
markchien6ae63e52020-01-21 13:11:06 +08002970 * {@hide}
2971 */
markchien0f45bb92019-01-16 17:44:13 +08002972 @SystemApi
markchien6ae63e52020-01-21 13:11:06 +08002973 @Deprecated
Remi NGUYEN VAN09f8ed22021-02-15 18:52:06 +09002974 public static final int TETHER_ERROR_PROVISION_FAILED = 11;
markchien6ae63e52020-01-21 13:11:06 +08002975 /**
2976 * @deprecated Use {@link TetheringManager#TETHER_ERROR_DHCPSERVER_ERROR}.
2977 * {@hide}
2978 */
2979 @Deprecated
2980 public static final int TETHER_ERROR_DHCPSERVER_ERROR =
2981 TetheringManager.TETHER_ERROR_DHCPSERVER_ERROR;
2982 /**
2983 * @deprecated Use {@link TetheringManager#TETHER_ERROR_ENTITLEMENT_UNKNOWN}.
2984 * {@hide}
2985 */
markchien0f45bb92019-01-16 17:44:13 +08002986 @SystemApi
markchien6ae63e52020-01-21 13:11:06 +08002987 @Deprecated
Remi NGUYEN VAN09f8ed22021-02-15 18:52:06 +09002988 public static final int TETHER_ERROR_ENTITLEMENT_UNKONWN = 13;
Robert Greenwalt4283ded2010-03-02 17:25:02 -08002989
2990 /**
Robert Greenwaltfaa4b402013-02-15 10:56:35 -08002991 * Get a more detailed error code after a Tethering or Untethering
2992 * request asynchronously failed.
2993 *
2994 * @param iface The name of the interface of interest
Robert Greenwalt4283ded2010-03-02 17:25:02 -08002995 * @return error The error code of the last error tethering or untethering the named
2996 * interface
Robert Greenwaltfaa4b402013-02-15 10:56:35 -08002997 *
markchien6ae63e52020-01-21 13:11:06 +08002998 * @deprecated Use {@link TetheringEventCallback#onError(String, int)} instead.
Robert Greenwalt4283ded2010-03-02 17:25:02 -08002999 * {@hide}
3000 */
Jeff Sharkey656584a2017-04-24 11:18:03 -06003001 @RequiresPermission(android.Manifest.permission.ACCESS_NETWORK_STATE)
Mathew Inwoode1a17ba2020-11-04 09:29:36 +00003002 @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.R, trackingBug = 170729553)
markchien6ae63e52020-01-21 13:11:06 +08003003 @Deprecated
Robert Greenwalt4283ded2010-03-02 17:25:02 -08003004 public int getLastTetherError(String iface) {
markchienf47d8342020-03-19 13:37:43 +08003005 int error = mTetheringManager.getLastTetherError(iface);
3006 if (error == TetheringManager.TETHER_ERROR_UNKNOWN_TYPE) {
3007 // TETHER_ERROR_UNKNOWN_TYPE was introduced with TetheringManager and has never been
3008 // returned by ConnectivityManager. Convert it to the legacy TETHER_ERROR_UNKNOWN_IFACE
3009 // instead.
3010 error = TetheringManager.TETHER_ERROR_UNKNOWN_IFACE;
3011 }
3012 return error;
Robert Greenwalt93dc1042010-06-15 12:19:37 -07003013 }
3014
markchienbf5ab012019-03-06 16:25:00 +08003015 /** @hide */
3016 @Retention(RetentionPolicy.SOURCE)
3017 @IntDef(value = {
3018 TETHER_ERROR_NO_ERROR,
3019 TETHER_ERROR_PROVISION_FAILED,
3020 TETHER_ERROR_ENTITLEMENT_UNKONWN,
3021 })
3022 public @interface EntitlementResultCode {
3023 }
3024
Robert Greenwalt93dc1042010-06-15 12:19:37 -07003025 /**
markchienbf5ab012019-03-06 16:25:00 +08003026 * Callback for use with {@link #getLatestTetheringEntitlementResult} to find out whether
markchien0f45bb92019-01-16 17:44:13 +08003027 * entitlement succeeded.
markchien6ae63e52020-01-21 13:11:06 +08003028 *
3029 * @deprecated Use {@link TetheringManager#OnTetheringEntitlementResultListener} instead.
markchien0f45bb92019-01-16 17:44:13 +08003030 * @hide
3031 */
3032 @SystemApi
markchien6ae63e52020-01-21 13:11:06 +08003033 @Deprecated
markchienbf5ab012019-03-06 16:25:00 +08003034 public interface OnTetheringEntitlementResultListener {
3035 /**
3036 * Called to notify entitlement result.
3037 *
3038 * @param resultCode an int value of entitlement result. It may be one of
3039 * {@link #TETHER_ERROR_NO_ERROR},
3040 * {@link #TETHER_ERROR_PROVISION_FAILED}, or
3041 * {@link #TETHER_ERROR_ENTITLEMENT_UNKONWN}.
3042 */
Jeremy Klein7e7c7422019-03-12 13:32:08 -07003043 void onTetheringEntitlementResult(@EntitlementResultCode int resultCode);
markchienbf5ab012019-03-06 16:25:00 +08003044 }
3045
3046 /**
markchien0f45bb92019-01-16 17:44:13 +08003047 * Get the last value of the entitlement check on this downstream. If the cached value is
3048 * {@link #TETHER_ERROR_NO_ERROR} or showEntitlementUi argument is false, it just return the
3049 * cached value. Otherwise, a UI-based entitlement check would be performed. It is not
3050 * guaranteed that the UI-based entitlement check will complete in any specific time period
3051 * and may in fact never complete. Any successful entitlement check the platform performs for
3052 * any reason will update the cached value.
3053 *
3054 * @param type the downstream type of tethering. Must be one of
3055 * {@link #TETHERING_WIFI},
3056 * {@link #TETHERING_USB}, or
3057 * {@link #TETHERING_BLUETOOTH}.
3058 * @param showEntitlementUi a boolean indicating whether to run UI-based entitlement check.
markchienbf5ab012019-03-06 16:25:00 +08003059 * @param executor the executor on which callback will be invoked.
3060 * @param listener an {@link OnTetheringEntitlementResultListener} which will be called to
3061 * notify the caller of the result of entitlement check. The listener may be called zero
3062 * or one time.
markchien6ae63e52020-01-21 13:11:06 +08003063 * @deprecated Use {@link TetheringManager#requestLatestTetheringEntitlementResult} instead.
markchien0f45bb92019-01-16 17:44:13 +08003064 * {@hide}
3065 */
3066 @SystemApi
markchien6ae63e52020-01-21 13:11:06 +08003067 @Deprecated
markchien0f45bb92019-01-16 17:44:13 +08003068 @RequiresPermission(android.Manifest.permission.TETHER_PRIVILEGED)
markchienbf5ab012019-03-06 16:25:00 +08003069 public void getLatestTetheringEntitlementResult(int type, boolean showEntitlementUi,
3070 @NonNull @CallbackExecutor Executor executor,
3071 @NonNull final OnTetheringEntitlementResultListener listener) {
Remi NGUYEN VAN81f68422021-03-15 07:31:54 +00003072 Objects.requireNonNull(listener, "TetheringEntitlementResultListener cannot be null.");
markchienbf5ab012019-03-06 16:25:00 +08003073 ResultReceiver wrappedListener = new ResultReceiver(null) {
3074 @Override
3075 protected void onReceiveResult(int resultCode, Bundle resultData) {
lucaslinc6170bb2021-03-04 09:38:21 +08003076 final long token = Binder.clearCallingIdentity();
3077 try {
3078 executor.execute(() -> {
3079 listener.onTetheringEntitlementResult(resultCode);
3080 });
3081 } finally {
3082 Binder.restoreCallingIdentity(token);
3083 }
markchienbf5ab012019-03-06 16:25:00 +08003084 }
3085 };
3086
Amos Bianchia9b415a2020-03-04 11:07:38 -08003087 mTetheringManager.requestLatestTetheringEntitlementResult(type, wrappedListener,
markchien5776f962019-12-16 20:15:20 +08003088 showEntitlementUi);
markchienbf5ab012019-03-06 16:25:00 +08003089 }
3090
3091 /**
Robert Greenwaltfaa4b402013-02-15 10:56:35 -08003092 * Report network connectivity status. This is currently used only
3093 * to alter status bar UI.
Paul Jensen08f9dbb2015-05-06 11:10:18 -04003094 * <p>This method requires the caller to hold the permission
3095 * {@link android.Manifest.permission#STATUS_BAR}.
Robert Greenwaltfaa4b402013-02-15 10:56:35 -08003096 *
Robert Greenwalt986c7412010-09-08 15:24:47 -07003097 * @param networkType The type of network you want to report on
3098 * @param percentage The quality of the connection 0 is bad, 100 is good
Chalard Jean7eaf3b12018-03-08 13:54:53 +09003099 * @deprecated Types are deprecated. Use {@link #reportNetworkConnectivity} instead.
Robert Greenwalt986c7412010-09-08 15:24:47 -07003100 * {@hide}
3101 */
3102 public void reportInetCondition(int networkType, int percentage) {
Soi, Yoshinarib8102ef2015-11-12 12:09:02 +09003103 printStackTrace();
Robert Greenwalt986c7412010-09-08 15:24:47 -07003104 try {
3105 mService.reportInetCondition(networkType, percentage);
3106 } catch (RemoteException e) {
Jeff Sharkeyc78f85c2016-03-01 19:27:23 -07003107 throw e.rethrowFromSystemServer();
Robert Greenwalt986c7412010-09-08 15:24:47 -07003108 }
3109 }
Robert Greenwaltc3c5f862010-10-11 16:00:27 -07003110
3111 /**
Robert Greenwaltf3017f72014-05-18 23:07:25 -07003112 * Report a problem network to the framework. This provides a hint to the system
Ye Wenca7abab2014-07-21 14:19:01 -07003113 * that there might be connectivity problems on this network and may cause
Robert Greenwaltf3017f72014-05-18 23:07:25 -07003114 * the framework to re-evaluate network connectivity and/or switch to another
3115 * network.
Robert Greenwaltf99b8392014-03-26 16:47:06 -07003116 *
Robert Greenwaltf3017f72014-05-18 23:07:25 -07003117 * @param network The {@link Network} the application was attempting to use
3118 * or {@code null} to indicate the current default network.
Paul Jensenb95d7952015-04-07 12:43:13 -04003119 * @deprecated Use {@link #reportNetworkConnectivity} which allows reporting both
3120 * working and non-working connectivity.
Robert Greenwaltf99b8392014-03-26 16:47:06 -07003121 */
Aurimas Liutikase80a0eb2016-05-24 15:22:55 -07003122 @Deprecated
Chalard Jean158702d2019-01-07 19:26:34 +09003123 public void reportBadNetwork(@Nullable Network network) {
Soi, Yoshinarib8102ef2015-11-12 12:09:02 +09003124 printStackTrace();
Robert Greenwaltf99b8392014-03-26 16:47:06 -07003125 try {
Paul Jensenb95d7952015-04-07 12:43:13 -04003126 // One of these will be ignored because it matches system's current state.
3127 // The other will trigger the necessary reevaluation.
3128 mService.reportNetworkConnectivity(network, true);
3129 mService.reportNetworkConnectivity(network, false);
3130 } catch (RemoteException e) {
Jeff Sharkeyc78f85c2016-03-01 19:27:23 -07003131 throw e.rethrowFromSystemServer();
Paul Jensenb95d7952015-04-07 12:43:13 -04003132 }
3133 }
3134
3135 /**
3136 * Report to the framework whether a network has working connectivity.
3137 * This provides a hint to the system that a particular network is providing
3138 * working connectivity or not. In response the framework may re-evaluate
3139 * the network's connectivity and might take further action thereafter.
3140 *
3141 * @param network The {@link Network} the application was attempting to use
3142 * or {@code null} to indicate the current default network.
3143 * @param hasConnectivity {@code true} if the application was able to successfully access the
3144 * Internet using {@code network} or {@code false} if not.
3145 */
Chalard Jean158702d2019-01-07 19:26:34 +09003146 public void reportNetworkConnectivity(@Nullable Network network, boolean hasConnectivity) {
Soi, Yoshinarib8102ef2015-11-12 12:09:02 +09003147 printStackTrace();
Paul Jensenb95d7952015-04-07 12:43:13 -04003148 try {
3149 mService.reportNetworkConnectivity(network, hasConnectivity);
Robert Greenwaltf99b8392014-03-26 16:47:06 -07003150 } catch (RemoteException e) {
Jeff Sharkeyc78f85c2016-03-01 19:27:23 -07003151 throw e.rethrowFromSystemServer();
Robert Greenwaltf99b8392014-03-26 16:47:06 -07003152 }
3153 }
3154
3155 /**
Chalard Jean48d60ea2021-03-17 17:03:34 +09003156 * Set a network-independent global HTTP proxy.
Paul Jensen08f9dbb2015-05-06 11:10:18 -04003157 *
Chalard Jean48d60ea2021-03-17 17:03:34 +09003158 * This sets an HTTP proxy that applies to all networks and overrides any network-specific
3159 * proxy. If set, HTTP libraries that are proxy-aware will use this global proxy when
3160 * accessing any network, regardless of what the settings for that network are.
3161 *
3162 * Note that HTTP proxies are by nature typically network-dependent, and setting a global
3163 * proxy is likely to break networking on multiple networks. This method is only meant
3164 * for device policy clients looking to do general internal filtering or similar use cases.
3165 *
3166 * {@see #getGlobalProxy}
3167 * {@see LinkProperties#getHttpProxy}
3168 *
3169 * @param p A {@link ProxyInfo} object defining the new global HTTP proxy. Calling this
3170 * method with a {@code null} value will clear the global HTTP proxy.
Robert Greenwaltf3017f72014-05-18 23:07:25 -07003171 * @hide
Robert Greenwaltc3c5f862010-10-11 16:00:27 -07003172 */
Chalard Jean48d60ea2021-03-17 17:03:34 +09003173 // Used by Device Policy Manager to set the global proxy.
Chiachang Wang4d513572021-03-18 09:44:34 +08003174 @SystemApi(client = MODULE_LIBRARIES)
paulhu8e96a752019-08-12 16:25:11 +08003175 @RequiresPermission(android.Manifest.permission.NETWORK_STACK)
Chalard Jean48d60ea2021-03-17 17:03:34 +09003176 public void setGlobalProxy(@Nullable final ProxyInfo p) {
Robert Greenwaltc3c5f862010-10-11 16:00:27 -07003177 try {
3178 mService.setGlobalProxy(p);
3179 } catch (RemoteException e) {
Jeff Sharkeyc78f85c2016-03-01 19:27:23 -07003180 throw e.rethrowFromSystemServer();
Robert Greenwaltc3c5f862010-10-11 16:00:27 -07003181 }
3182 }
3183
3184 /**
Robert Greenwaltfaa4b402013-02-15 10:56:35 -08003185 * Retrieve any network-independent global HTTP proxy.
3186 *
Jason Monk4d5e20f2014-04-25 15:00:09 -04003187 * @return {@link ProxyInfo} for the current global HTTP proxy or {@code null}
Robert Greenwaltfaa4b402013-02-15 10:56:35 -08003188 * if no global HTTP proxy is set.
Robert Greenwaltf3017f72014-05-18 23:07:25 -07003189 * @hide
Robert Greenwaltc3c5f862010-10-11 16:00:27 -07003190 */
Chiachang Wang4d513572021-03-18 09:44:34 +08003191 @SystemApi(client = MODULE_LIBRARIES)
3192 @Nullable
Jason Monk4d5e20f2014-04-25 15:00:09 -04003193 public ProxyInfo getGlobalProxy() {
Robert Greenwaltc3c5f862010-10-11 16:00:27 -07003194 try {
3195 return mService.getGlobalProxy();
3196 } catch (RemoteException e) {
Jeff Sharkeyc78f85c2016-03-01 19:27:23 -07003197 throw e.rethrowFromSystemServer();
Robert Greenwaltc3c5f862010-10-11 16:00:27 -07003198 }
3199 }
3200
3201 /**
Paul Jensendb93dd92015-05-06 07:32:40 -04003202 * Retrieve the global HTTP proxy, or if no global HTTP proxy is set, a
3203 * network-specific HTTP proxy. If {@code network} is null, the
3204 * network-specific proxy returned is the proxy of the default active
3205 * network.
3206 *
3207 * @return {@link ProxyInfo} for the current global HTTP proxy, or if no
3208 * global HTTP proxy is set, {@code ProxyInfo} for {@code network},
3209 * or when {@code network} is {@code null},
3210 * the {@code ProxyInfo} for the default active network. Returns
3211 * {@code null} when no proxy applies or the caller doesn't have
3212 * permission to use {@code network}.
3213 * @hide
3214 */
3215 public ProxyInfo getProxyForNetwork(Network network) {
3216 try {
3217 return mService.getProxyForNetwork(network);
3218 } catch (RemoteException e) {
Jeff Sharkeyc78f85c2016-03-01 19:27:23 -07003219 throw e.rethrowFromSystemServer();
Paul Jensendb93dd92015-05-06 07:32:40 -04003220 }
3221 }
3222
3223 /**
Paul Jensenc0618a62014-12-10 15:12:18 -05003224 * Get the current default HTTP proxy settings. If a global proxy is set it will be returned,
3225 * otherwise if this process is bound to a {@link Network} using
Paul Jensenee2f45d2015-03-10 10:54:12 -04003226 * {@link #bindProcessToNetwork} then that {@code Network}'s proxy is returned, otherwise
Paul Jensenc0618a62014-12-10 15:12:18 -05003227 * the default network's proxy is returned.
Robert Greenwaltfaa4b402013-02-15 10:56:35 -08003228 *
Jason Monk4d5e20f2014-04-25 15:00:09 -04003229 * @return the {@link ProxyInfo} for the current HTTP proxy, or {@code null} if no
Robert Greenwaltfaa4b402013-02-15 10:56:35 -08003230 * HTTP proxy is active.
Robert Greenwaltc3c5f862010-10-11 16:00:27 -07003231 */
Chalard Jean158702d2019-01-07 19:26:34 +09003232 @Nullable
Paul Jensenc0618a62014-12-10 15:12:18 -05003233 public ProxyInfo getDefaultProxy() {
Paul Jensendb93dd92015-05-06 07:32:40 -04003234 return getProxyForNetwork(getBoundNetworkForProcess());
Robert Greenwaltc3c5f862010-10-11 16:00:27 -07003235 }
Robert Greenwalt34848c02011-03-25 13:09:25 -07003236
3237 /**
Robert Greenwalt0114f6e2011-08-31 11:46:42 -07003238 * Returns true if the hardware supports the given network type
3239 * else it returns false. This doesn't indicate we have coverage
3240 * or are authorized onto a network, just whether or not the
Robert Greenwaltfaa4b402013-02-15 10:56:35 -08003241 * hardware supports it. For example a GSM phone without a SIM
3242 * should still return {@code true} for mobile data, but a wifi only
3243 * tablet would return {@code false}.
3244 *
3245 * @param networkType The network type we'd like to check
3246 * @return {@code true} if supported, else {@code false}
Chalard Jean7eaf3b12018-03-08 13:54:53 +09003247 * @deprecated Types are deprecated. Use {@link NetworkCapabilities} instead.
Robert Greenwalt0114f6e2011-08-31 11:46:42 -07003248 * @hide
3249 */
Chalard Jean7eaf3b12018-03-08 13:54:53 +09003250 @Deprecated
Jeff Sharkey656584a2017-04-24 11:18:03 -06003251 @RequiresPermission(android.Manifest.permission.ACCESS_NETWORK_STATE)
Chalard Jeana3b77512019-04-09 15:46:21 +09003252 @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P, trackingBug = 130143562)
Robert Greenwalt0114f6e2011-08-31 11:46:42 -07003253 public boolean isNetworkSupported(int networkType) {
3254 try {
3255 return mService.isNetworkSupported(networkType);
Jeff Sharkeyc78f85c2016-03-01 19:27:23 -07003256 } catch (RemoteException e) {
3257 throw e.rethrowFromSystemServer();
3258 }
Robert Greenwalt0114f6e2011-08-31 11:46:42 -07003259 }
Jeff Sharkeyd00b1302012-04-12 18:34:54 -07003260
3261 /**
3262 * Returns if the currently active data network is metered. A network is
3263 * classified as metered when the user is sensitive to heavy data usage on
Robert Greenwaltfaa4b402013-02-15 10:56:35 -08003264 * that connection due to monetary costs, data limitations or
3265 * battery/performance issues. You should check this before doing large
3266 * data transfers, and warn the user or delay the operation until another
3267 * network is available.
3268 *
3269 * @return {@code true} if large transfers should be avoided, otherwise
3270 * {@code false}.
Jeff Sharkeyd00b1302012-04-12 18:34:54 -07003271 */
Jeff Sharkey656584a2017-04-24 11:18:03 -06003272 @RequiresPermission(android.Manifest.permission.ACCESS_NETWORK_STATE)
Jeff Sharkeyd00b1302012-04-12 18:34:54 -07003273 public boolean isActiveNetworkMetered() {
3274 try {
3275 return mService.isActiveNetworkMetered();
3276 } catch (RemoteException e) {
Jeff Sharkeyc78f85c2016-03-01 19:27:23 -07003277 throw e.rethrowFromSystemServer();
Jeff Sharkeyd00b1302012-04-12 18:34:54 -07003278 }
3279 }
Jeff Sharkeyebcc7972012-08-25 00:05:46 -07003280
Robert Greenwaltfaa4b402013-02-15 10:56:35 -08003281 /**
Sarah Chincabcbff2020-11-25 12:15:14 -08003282 * Set sign in error notification to visible or invisible
Wink Saville9a1a7ef2013-08-29 08:55:16 -07003283 *
Sarah Chincabcbff2020-11-25 12:15:14 -08003284 * @hide
Paul Jensendda8e592015-03-18 12:23:02 -04003285 * @deprecated Doesn't properly deal with multiple connected networks of the same type.
Wink Saville9a1a7ef2013-08-29 08:55:16 -07003286 */
Aurimas Liutikase80a0eb2016-05-24 15:22:55 -07003287 @Deprecated
Wink Saville9a1a7ef2013-08-29 08:55:16 -07003288 public void setProvisioningNotificationVisible(boolean visible, int networkType,
Paul Jensenebeaecd2014-09-15 15:59:36 -04003289 String action) {
Wink Saville9a1a7ef2013-08-29 08:55:16 -07003290 try {
Paul Jensenebeaecd2014-09-15 15:59:36 -04003291 mService.setProvisioningNotificationVisible(visible, networkType, action);
Wink Saville9a1a7ef2013-08-29 08:55:16 -07003292 } catch (RemoteException e) {
Jeff Sharkeyc78f85c2016-03-01 19:27:23 -07003293 throw e.rethrowFromSystemServer();
Wink Saville9a1a7ef2013-08-29 08:55:16 -07003294 }
3295 }
Yuhao Zhengb77f15d2013-09-09 17:00:04 -07003296
3297 /**
3298 * Set the value for enabling/disabling airplane mode
3299 *
3300 * @param enable whether to enable airplane mode or not
3301 *
Yuhao Zhengb77f15d2013-09-09 17:00:04 -07003302 * @hide
3303 */
Lorenzo Colittic7da00d2018-10-09 18:55:11 +09003304 @RequiresPermission(anyOf = {
Edward Savage-Jonesd4723692019-11-26 13:18:08 +01003305 android.Manifest.permission.NETWORK_AIRPLANE_MODE,
Lorenzo Colittic7da00d2018-10-09 18:55:11 +09003306 android.Manifest.permission.NETWORK_SETTINGS,
3307 android.Manifest.permission.NETWORK_SETUP_WIZARD,
3308 android.Manifest.permission.NETWORK_STACK})
Lorenzo Colitti0bfef022018-10-09 18:50:32 +09003309 @SystemApi
Yuhao Zhengb77f15d2013-09-09 17:00:04 -07003310 public void setAirplaneMode(boolean enable) {
3311 try {
3312 mService.setAirplaneMode(enable);
3313 } catch (RemoteException e) {
Jeff Sharkeyc78f85c2016-03-01 19:27:23 -07003314 throw e.rethrowFromSystemServer();
Yuhao Zhengb77f15d2013-09-09 17:00:04 -07003315 }
3316 }
Robert Greenwalt7e45d112014-04-11 15:53:27 -07003317
Lorenzo Colitti6702d4b2020-01-08 00:04:09 +09003318 /**
3319 * Registers the specified {@link NetworkProvider}.
3320 * Each listener must only be registered once. The listener can be unregistered with
3321 * {@link #unregisterNetworkProvider}.
3322 *
3323 * @param provider the provider to register
3324 * @return the ID of the provider. This ID must be used by the provider when registering
3325 * {@link android.net.NetworkAgent}s.
3326 * @hide
3327 */
3328 @SystemApi
paulhub6ba8e82020-03-04 09:43:41 +08003329 @RequiresPermission(anyOf = {
3330 NetworkStack.PERMISSION_MAINLINE_NETWORK_STACK,
3331 android.Manifest.permission.NETWORK_FACTORY})
Lorenzo Colitti6702d4b2020-01-08 00:04:09 +09003332 public int registerNetworkProvider(@NonNull NetworkProvider provider) {
3333 if (provider.getProviderId() != NetworkProvider.ID_NONE) {
Lorenzo Colitti6702d4b2020-01-08 00:04:09 +09003334 throw new IllegalStateException("NetworkProviders can only be registered once");
3335 }
3336
3337 try {
3338 int providerId = mService.registerNetworkProvider(provider.getMessenger(),
3339 provider.getName());
3340 provider.setProviderId(providerId);
3341 } catch (RemoteException e) {
3342 throw e.rethrowFromSystemServer();
3343 }
3344 return provider.getProviderId();
3345 }
3346
3347 /**
3348 * Unregisters the specified NetworkProvider.
3349 *
3350 * @param provider the provider to unregister
3351 * @hide
3352 */
3353 @SystemApi
paulhub6ba8e82020-03-04 09:43:41 +08003354 @RequiresPermission(anyOf = {
3355 NetworkStack.PERMISSION_MAINLINE_NETWORK_STACK,
3356 android.Manifest.permission.NETWORK_FACTORY})
Lorenzo Colitti6702d4b2020-01-08 00:04:09 +09003357 public void unregisterNetworkProvider(@NonNull NetworkProvider provider) {
3358 try {
3359 mService.unregisterNetworkProvider(provider.getMessenger());
3360 } catch (RemoteException e) {
3361 throw e.rethrowFromSystemServer();
3362 }
3363 provider.setProviderId(NetworkProvider.ID_NONE);
3364 }
3365
3366
3367 /** @hide exposed via the NetworkProvider class. */
paulhub6ba8e82020-03-04 09:43:41 +08003368 @RequiresPermission(anyOf = {
3369 NetworkStack.PERMISSION_MAINLINE_NETWORK_STACK,
3370 android.Manifest.permission.NETWORK_FACTORY})
Lorenzo Colitti6702d4b2020-01-08 00:04:09 +09003371 public void declareNetworkRequestUnfulfillable(@NonNull NetworkRequest request) {
3372 try {
3373 mService.declareNetworkRequestUnfulfillable(request);
3374 } catch (RemoteException e) {
3375 throw e.rethrowFromSystemServer();
3376 }
3377 }
3378
Paul Jensen1f567382015-02-13 14:18:39 -05003379 /**
3380 * @hide
3381 * Register a NetworkAgent with ConnectivityService.
Chalard Jeanf78c9642019-12-13 19:47:12 +09003382 * @return Network corresponding to NetworkAgent.
Paul Jensen1f567382015-02-13 14:18:39 -05003383 */
paulhub6ba8e82020-03-04 09:43:41 +08003384 @RequiresPermission(anyOf = {
3385 NetworkStack.PERMISSION_MAINLINE_NETWORK_STACK,
3386 android.Manifest.permission.NETWORK_FACTORY})
Remi NGUYEN VAN77b0c1c2020-12-23 12:45:08 +09003387 public Network registerNetworkAgent(INetworkAgent na, NetworkInfo ni, LinkProperties lp,
Chalard Jean28018572020-12-21 18:36:52 +09003388 NetworkCapabilities nc, @NonNull NetworkScore score, NetworkAgentConfig config,
3389 int providerId) {
Robert Greenwalte20f7a22014-04-18 15:25:25 -07003390 try {
Remi NGUYEN VAN77b0c1c2020-12-23 12:45:08 +09003391 return mService.registerNetworkAgent(na, ni, lp, nc, score, config, providerId);
Paul Jensen1f567382015-02-13 14:18:39 -05003392 } catch (RemoteException e) {
Jeff Sharkeyc78f85c2016-03-01 19:27:23 -07003393 throw e.rethrowFromSystemServer();
Paul Jensen1f567382015-02-13 14:18:39 -05003394 }
Robert Greenwalte20f7a22014-04-18 15:25:25 -07003395 }
3396
Robert Greenwaltf99b8392014-03-26 16:47:06 -07003397 /**
Hugo Benichi2aa65af2017-03-06 09:17:06 +09003398 * Base class for {@code NetworkRequest} callbacks. Used for notifications about network
3399 * changes. Should be extended by applications wanting notifications.
3400 *
3401 * A {@code NetworkCallback} is registered by calling
3402 * {@link #requestNetwork(NetworkRequest, NetworkCallback)},
3403 * {@link #registerNetworkCallback(NetworkRequest, NetworkCallback)},
Hugo Benichicbfbb372018-02-07 21:17:43 +09003404 * or {@link #registerDefaultNetworkCallback(NetworkCallback)}. A {@code NetworkCallback} is
Hugo Benichi2aa65af2017-03-06 09:17:06 +09003405 * unregistered by calling {@link #unregisterNetworkCallback(NetworkCallback)}.
3406 * A {@code NetworkCallback} should be registered at most once at any time.
3407 * A {@code NetworkCallback} that has been unregistered can be registered again.
Robert Greenwaltf99b8392014-03-26 16:47:06 -07003408 */
Robert Greenwaltf57c03c2014-06-11 16:05:07 -07003409 public static class NetworkCallback {
Robert Greenwalte20f7a22014-04-18 15:25:25 -07003410 /**
Roshan Pius951c0032020-12-22 15:10:42 -08003411 * No flags associated with this callback.
3412 * @hide
3413 */
3414 public static final int FLAG_NONE = 0;
3415 /**
3416 * Use this flag to include any location sensitive data in {@link NetworkCapabilities} sent
3417 * via {@link #onCapabilitiesChanged(Network, NetworkCapabilities)}.
3418 * <p>
3419 * These include:
3420 * <li> Some transport info instances (retrieved via
3421 * {@link NetworkCapabilities#getTransportInfo()}) like {@link android.net.wifi.WifiInfo}
3422 * contain location sensitive information.
3423 * <li> OwnerUid (retrieved via {@link NetworkCapabilities#getOwnerUid()} is location
3424 * sensitive for wifi suggestor apps (i.e using {@link WifiNetworkSuggestion}).</li>
3425 * </p>
3426 * <p>
3427 * Note:
3428 * <li> Retrieving this location sensitive information (subject to app's location
3429 * permissions) will be noted by system. </li>
3430 * <li> Without this flag any {@link NetworkCapabilities} provided via the callback does
3431 * not include location sensitive info.
3432 * </p>
3433 */
Roshan Piuse1220262021-03-11 21:16:44 -08003434 // Note: Some existing fields which are location sensitive may still be included without
3435 // this flag if the app targets SDK < S (to maintain backwards compatibility).
Roshan Pius951c0032020-12-22 15:10:42 -08003436 public static final int FLAG_INCLUDE_LOCATION_INFO = 1 << 0;
3437
3438 /** @hide */
3439 @Retention(RetentionPolicy.SOURCE)
3440 @IntDef(flag = true, prefix = "FLAG_", value = {
3441 FLAG_NONE,
3442 FLAG_INCLUDE_LOCATION_INFO
3443 })
3444 public @interface Flag { }
3445
3446 /**
3447 * All the valid flags for error checking.
3448 */
3449 private static final int VALID_FLAGS = FLAG_INCLUDE_LOCATION_INFO;
3450
3451 public NetworkCallback() {
3452 this(FLAG_NONE);
3453 }
3454
3455 public NetworkCallback(@Flag int flags) {
Remi NGUYEN VAN81f68422021-03-15 07:31:54 +00003456 if ((flags & VALID_FLAGS) != flags) {
3457 throw new IllegalArgumentException("Invalid flags");
3458 }
Roshan Pius951c0032020-12-22 15:10:42 -08003459 mFlags = flags;
3460 }
3461
3462 /**
Lorenzo Colitti14574592015-04-24 12:23:24 +09003463 * Called when the framework connects to a new network to evaluate whether it satisfies this
3464 * request. If evaluation succeeds, this callback may be followed by an {@link #onAvailable}
3465 * callback. There is no guarantee that this new network will satisfy any requests, or that
3466 * the network will stay connected for longer than the time necessary to evaluate it.
3467 * <p>
3468 * Most applications <b>should not</b> act on this callback, and should instead use
3469 * {@link #onAvailable}. This callback is intended for use by applications that can assist
3470 * the framework in properly evaluating the network &mdash; for example, an application that
3471 * can automatically log in to a captive portal without user intervention.
3472 *
3473 * @param network The {@link Network} of the network that is being evaluated.
Lorenzo Colitti3a9df1a2015-06-11 14:27:17 +09003474 *
3475 * @hide
Robert Greenwalte20f7a22014-04-18 15:25:25 -07003476 */
paulhu1a407652019-03-22 16:35:06 +08003477 public void onPreCheck(@NonNull Network network) {}
Robert Greenwalte20f7a22014-04-18 15:25:25 -07003478
3479 /**
Lorenzo Colitti14574592015-04-24 12:23:24 +09003480 * Called when the framework connects and has declared a new network ready for use.
Robert Greenwaltf57c03c2014-06-11 16:05:07 -07003481 * This callback may be called more than once if the {@link Network} that is
3482 * satisfying the request changes.
Robert Greenwaltf3017f72014-05-18 23:07:25 -07003483 *
Robert Greenwaltf3017f72014-05-18 23:07:25 -07003484 * @param network The {@link Network} of the satisfying network.
Chalard Jeana23bc9e2018-01-30 22:41:41 +09003485 * @param networkCapabilities The {@link NetworkCapabilities} of the satisfying network.
3486 * @param linkProperties The {@link LinkProperties} of the satisfying network.
junyulaif2c67e42018-08-07 19:50:45 +08003487 * @param blocked Whether access to the {@link Network} is blocked due to system policy.
Chalard Jeana23bc9e2018-01-30 22:41:41 +09003488 * @hide
3489 */
Lorenzo Colitti79c6f222021-03-18 00:54:57 +09003490 public final void onAvailable(@NonNull Network network,
paulhu1a407652019-03-22 16:35:06 +08003491 @NonNull NetworkCapabilities networkCapabilities,
Lorenzo Colitti79c6f222021-03-18 00:54:57 +09003492 @NonNull LinkProperties linkProperties, @BlockedReason int blocked) {
Chalard Jeana23bc9e2018-01-30 22:41:41 +09003493 // Internally only this method is called when a new network is available, and
3494 // it calls the callback in the same way and order that older versions used
3495 // to call so as not to change the behavior.
Lorenzo Colitti79c6f222021-03-18 00:54:57 +09003496 onAvailable(network, networkCapabilities, linkProperties, blocked != 0);
3497 onBlockedStatusChanged(network, blocked);
3498 }
3499
3500 /**
3501 * Legacy variant of onAvailable that takes a boolean blocked reason.
3502 *
3503 * This method has never been public API, but it's not final, so there may be apps that
3504 * implemented it and rely on it being called. Do our best not to break them.
3505 * Note: such apps will also get a second call to onBlockedStatusChanged immediately after
3506 * this method is called. There does not seem to be a way to avoid this.
3507 * TODO: add a compat check to move apps off this method, and eventually stop calling it.
3508 *
3509 * @hide
3510 */
3511 public void onAvailable(@NonNull Network network,
3512 @NonNull NetworkCapabilities networkCapabilities,
3513 @NonNull LinkProperties linkProperties, boolean blocked) {
Chalard Jeana23bc9e2018-01-30 22:41:41 +09003514 onAvailable(network);
3515 if (!networkCapabilities.hasCapability(
3516 NetworkCapabilities.NET_CAPABILITY_NOT_SUSPENDED)) {
3517 onNetworkSuspended(network);
3518 }
3519 onCapabilitiesChanged(network, networkCapabilities);
3520 onLinkPropertiesChanged(network, linkProperties);
Lorenzo Colitti79c6f222021-03-18 00:54:57 +09003521 // No call to onBlockedStatusChanged here. That is done by the caller.
Chalard Jeana23bc9e2018-01-30 22:41:41 +09003522 }
3523
3524 /**
3525 * Called when the framework connects and has declared a new network ready for use.
Chalard Jean01378b62019-08-30 16:27:28 +09003526 *
3527 * <p>For callbacks registered with {@link #registerNetworkCallback}, multiple networks may
3528 * be available at the same time, and onAvailable will be called for each of these as they
3529 * appear.
3530 *
3531 * <p>For callbacks registered with {@link #requestNetwork} and
3532 * {@link #registerDefaultNetworkCallback}, this means the network passed as an argument
3533 * is the new best network for this request and is now tracked by this callback ; this
3534 * callback will no longer receive method calls about other networks that may have been
3535 * passed to this method previously. The previously-best network may have disconnected, or
3536 * it may still be around and the newly-best network may simply be better.
3537 *
3538 * <p>Starting with {@link android.os.Build.VERSION_CODES#O}, this will always immediately
3539 * be followed by a call to {@link #onCapabilitiesChanged(Network, NetworkCapabilities)}
3540 * then by a call to {@link #onLinkPropertiesChanged(Network, LinkProperties)}, and a call
3541 * to {@link #onBlockedStatusChanged(Network, boolean)}.
3542 *
3543 * <p>Do NOT call {@link #getNetworkCapabilities(Network)} or
3544 * {@link #getLinkProperties(Network)} or other synchronous ConnectivityManager methods in
3545 * this callback as this is prone to race conditions (there is no guarantee the objects
3546 * returned by these methods will be current). Instead, wait for a call to
3547 * {@link #onCapabilitiesChanged(Network, NetworkCapabilities)} and
3548 * {@link #onLinkPropertiesChanged(Network, LinkProperties)} whose arguments are guaranteed
3549 * to be well-ordered with respect to other callbacks.
Chalard Jeana23bc9e2018-01-30 22:41:41 +09003550 *
3551 * @param network The {@link Network} of the satisfying network.
Robert Greenwalte20f7a22014-04-18 15:25:25 -07003552 */
paulhu1a407652019-03-22 16:35:06 +08003553 public void onAvailable(@NonNull Network network) {}
Robert Greenwalte20f7a22014-04-18 15:25:25 -07003554
3555 /**
Chalard Jean01378b62019-08-30 16:27:28 +09003556 * Called when the network is about to be lost, typically because there are no outstanding
3557 * requests left for it. This may be paired with a {@link NetworkCallback#onAvailable} call
3558 * with the new replacement network for graceful handover. This method is not guaranteed
3559 * to be called before {@link NetworkCallback#onLost} is called, for example in case a
3560 * network is suddenly disconnected.
Robert Greenwaltf3017f72014-05-18 23:07:25 -07003561 *
Chalard Jean01378b62019-08-30 16:27:28 +09003562 * <p>Do NOT call {@link #getNetworkCapabilities(Network)} or
3563 * {@link #getLinkProperties(Network)} or other synchronous ConnectivityManager methods in
3564 * this callback as this is prone to race conditions ; calling these methods while in a
3565 * callback may return an outdated or even a null object.
3566 *
3567 * @param network The {@link Network} that is about to be lost.
3568 * @param maxMsToLive The time in milliseconds the system intends to keep the network
3569 * connected for graceful handover; note that the network may still
3570 * suffer a hard loss at any time.
Robert Greenwalte20f7a22014-04-18 15:25:25 -07003571 */
paulhu1a407652019-03-22 16:35:06 +08003572 public void onLosing(@NonNull Network network, int maxMsToLive) {}
Robert Greenwalte20f7a22014-04-18 15:25:25 -07003573
3574 /**
Chalard Jean01378b62019-08-30 16:27:28 +09003575 * Called when a network disconnects or otherwise no longer satisfies this request or
3576 * callback.
3577 *
3578 * <p>If the callback was registered with requestNetwork() or
3579 * registerDefaultNetworkCallback(), it will only be invoked against the last network
3580 * returned by onAvailable() when that network is lost and no other network satisfies
3581 * the criteria of the request.
3582 *
3583 * <p>If the callback was registered with registerNetworkCallback() it will be called for
3584 * each network which no longer satisfies the criteria of the callback.
3585 *
3586 * <p>Do NOT call {@link #getNetworkCapabilities(Network)} or
3587 * {@link #getLinkProperties(Network)} or other synchronous ConnectivityManager methods in
3588 * this callback as this is prone to race conditions ; calling these methods while in a
3589 * callback may return an outdated or even a null object.
Robert Greenwaltf3017f72014-05-18 23:07:25 -07003590 *
Robert Greenwaltf3017f72014-05-18 23:07:25 -07003591 * @param network The {@link Network} lost.
Robert Greenwalte20f7a22014-04-18 15:25:25 -07003592 */
paulhu1a407652019-03-22 16:35:06 +08003593 public void onLost(@NonNull Network network) {}
Robert Greenwalte20f7a22014-04-18 15:25:25 -07003594
3595 /**
Chalard Jean01378b62019-08-30 16:27:28 +09003596 * Called if no network is found within the timeout time specified in
Etan Cohenfbfdd842019-01-08 12:09:18 -08003597 * {@link #requestNetwork(NetworkRequest, NetworkCallback, int)} call or if the
3598 * requested network request cannot be fulfilled (whether or not a timeout was
3599 * specified). When this callback is invoked the associated
Etan Cohenf67bde92017-03-01 12:47:28 -08003600 * {@link NetworkRequest} will have already been removed and released, as if
3601 * {@link #unregisterNetworkCallback(NetworkCallback)} had been called.
Robert Greenwalte20f7a22014-04-18 15:25:25 -07003602 */
Robert Greenwaltf57c03c2014-06-11 16:05:07 -07003603 public void onUnavailable() {}
Robert Greenwalte20f7a22014-04-18 15:25:25 -07003604
3605 /**
Chalard Jean01378b62019-08-30 16:27:28 +09003606 * Called when the network corresponding to this request changes capabilities but still
3607 * satisfies the requested criteria.
3608 *
3609 * <p>Starting with {@link android.os.Build.VERSION_CODES#O} this method is guaranteed
3610 * to be called immediately after {@link #onAvailable}.
3611 *
3612 * <p>Do NOT call {@link #getLinkProperties(Network)} or other synchronous
3613 * ConnectivityManager methods in this callback as this is prone to race conditions :
3614 * calling these methods while in a callback may return an outdated or even a null object.
Robert Greenwaltf3017f72014-05-18 23:07:25 -07003615 *
Robert Greenwaltf3017f72014-05-18 23:07:25 -07003616 * @param network The {@link Network} whose capabilities have changed.
Roshan Pius951c0032020-12-22 15:10:42 -08003617 * @param networkCapabilities The new {@link NetworkCapabilities} for this
Chalard Jeana23bc9e2018-01-30 22:41:41 +09003618 * network.
Robert Greenwalte20f7a22014-04-18 15:25:25 -07003619 */
paulhu1a407652019-03-22 16:35:06 +08003620 public void onCapabilitiesChanged(@NonNull Network network,
3621 @NonNull NetworkCapabilities networkCapabilities) {}
Robert Greenwalte20f7a22014-04-18 15:25:25 -07003622
3623 /**
Chalard Jean01378b62019-08-30 16:27:28 +09003624 * Called when the network corresponding to this request changes {@link LinkProperties}.
3625 *
3626 * <p>Starting with {@link android.os.Build.VERSION_CODES#O} this method is guaranteed
3627 * to be called immediately after {@link #onAvailable}.
3628 *
3629 * <p>Do NOT call {@link #getNetworkCapabilities(Network)} or other synchronous
3630 * ConnectivityManager methods in this callback as this is prone to race conditions :
3631 * calling these methods while in a callback may return an outdated or even a null object.
Robert Greenwaltf3017f72014-05-18 23:07:25 -07003632 *
Robert Greenwaltf3017f72014-05-18 23:07:25 -07003633 * @param network The {@link Network} whose link properties have changed.
3634 * @param linkProperties The new {@link LinkProperties} for this network.
Robert Greenwalte20f7a22014-04-18 15:25:25 -07003635 */
paulhu1a407652019-03-22 16:35:06 +08003636 public void onLinkPropertiesChanged(@NonNull Network network,
3637 @NonNull LinkProperties linkProperties) {}
Robert Greenwalte20f7a22014-04-18 15:25:25 -07003638
Robert Greenwalt7fb8adc2015-06-24 13:23:42 -07003639 /**
Chalard Jean01378b62019-08-30 16:27:28 +09003640 * Called when the network the framework connected to for this request suspends data
3641 * transmission temporarily.
3642 *
3643 * <p>This generally means that while the TCP connections are still live temporarily
3644 * network data fails to transfer. To give a specific example, this is used on cellular
3645 * networks to mask temporary outages when driving through a tunnel, etc. In general this
3646 * means read operations on sockets on this network will block once the buffers are
3647 * drained, and write operations will block once the buffers are full.
3648 *
3649 * <p>Do NOT call {@link #getNetworkCapabilities(Network)} or
3650 * {@link #getLinkProperties(Network)} or other synchronous ConnectivityManager methods in
3651 * this callback as this is prone to race conditions (there is no guarantee the objects
3652 * returned by these methods will be current).
3653 *
Robert Greenwalt7fb8adc2015-06-24 13:23:42 -07003654 * @hide
3655 */
paulhu1a407652019-03-22 16:35:06 +08003656 public void onNetworkSuspended(@NonNull Network network) {}
Robert Greenwalt7fb8adc2015-06-24 13:23:42 -07003657
3658 /**
3659 * Called when the network the framework connected to for this request
Chalard Jeana23bc9e2018-01-30 22:41:41 +09003660 * returns from a {@link NetworkInfo.State#SUSPENDED} state. This should always be
3661 * preceded by a matching {@link NetworkCallback#onNetworkSuspended} call.
Chalard Jean01378b62019-08-30 16:27:28 +09003662
3663 * <p>Do NOT call {@link #getNetworkCapabilities(Network)} or
3664 * {@link #getLinkProperties(Network)} or other synchronous ConnectivityManager methods in
3665 * this callback as this is prone to race conditions : calling these methods while in a
3666 * callback may return an outdated or even a null object.
3667 *
Robert Greenwalt7fb8adc2015-06-24 13:23:42 -07003668 * @hide
3669 */
paulhu1a407652019-03-22 16:35:06 +08003670 public void onNetworkResumed(@NonNull Network network) {}
Robert Greenwalt7fb8adc2015-06-24 13:23:42 -07003671
junyulaif2c67e42018-08-07 19:50:45 +08003672 /**
3673 * Called when access to the specified network is blocked or unblocked.
3674 *
Chalard Jean01378b62019-08-30 16:27:28 +09003675 * <p>Do NOT call {@link #getNetworkCapabilities(Network)} or
3676 * {@link #getLinkProperties(Network)} or other synchronous ConnectivityManager methods in
3677 * this callback as this is prone to race conditions : calling these methods while in a
3678 * callback may return an outdated or even a null object.
3679 *
junyulaif2c67e42018-08-07 19:50:45 +08003680 * @param network The {@link Network} whose blocked status has changed.
3681 * @param blocked The blocked status of this {@link Network}.
3682 */
junyulai7e06ad42019-03-04 22:45:36 +08003683 public void onBlockedStatusChanged(@NonNull Network network, boolean blocked) {}
junyulaif2c67e42018-08-07 19:50:45 +08003684
Lorenzo Colitti79c6f222021-03-18 00:54:57 +09003685 /**
Lorenzo Colitti42fe2232021-03-25 23:17:36 +09003686 * Called when access to the specified network is blocked or unblocked, or the reason for
3687 * access being blocked changes.
Lorenzo Colitti79c6f222021-03-18 00:54:57 +09003688 *
3689 * If a NetworkCallback object implements this method,
3690 * {@link #onBlockedStatusChanged(Network, boolean)} will not be called.
3691 *
3692 * <p>Do NOT call {@link #getNetworkCapabilities(Network)} or
3693 * {@link #getLinkProperties(Network)} or other synchronous ConnectivityManager methods in
3694 * this callback as this is prone to race conditions : calling these methods while in a
3695 * callback may return an outdated or even a null object.
3696 *
3697 * @param network The {@link Network} whose blocked status has changed.
3698 * @param blocked The blocked status of this {@link Network}.
3699 * @hide
3700 */
3701 @SystemApi(client = MODULE_LIBRARIES)
3702 public void onBlockedStatusChanged(@NonNull Network network, @BlockedReason int blocked) {
3703 onBlockedStatusChanged(network, blocked != 0);
3704 }
3705
Robert Greenwaltf57c03c2014-06-11 16:05:07 -07003706 private NetworkRequest networkRequest;
Roshan Pius951c0032020-12-22 15:10:42 -08003707 private final int mFlags;
Robert Greenwalte20f7a22014-04-18 15:25:25 -07003708 }
3709
Hugo Benichia590ab82017-05-11 13:16:17 +09003710 /**
3711 * Constant error codes used by ConnectivityService to communicate about failures and errors
3712 * across a Binder boundary.
3713 * @hide
3714 */
3715 public interface Errors {
Chalard Jean9dd11612018-06-04 16:52:49 +09003716 int TOO_MANY_REQUESTS = 1;
Hugo Benichia590ab82017-05-11 13:16:17 +09003717 }
3718
3719 /** @hide */
3720 public static class TooManyRequestsException extends RuntimeException {}
3721
3722 private static RuntimeException convertServiceException(ServiceSpecificException e) {
3723 switch (e.errorCode) {
3724 case Errors.TOO_MANY_REQUESTS:
3725 return new TooManyRequestsException();
3726 default:
3727 Log.w(TAG, "Unknown service error code " + e.errorCode);
3728 return new RuntimeException(e);
3729 }
3730 }
3731
Robert Greenwaltf99b8392014-03-26 16:47:06 -07003732 /** @hide */
Remi NGUYEN VAN1fb66ad2021-03-12 15:24:06 +09003733 public static final int CALLBACK_PRECHECK = 1;
Robert Greenwalt7fb8adc2015-06-24 13:23:42 -07003734 /** @hide */
Remi NGUYEN VAN1fb66ad2021-03-12 15:24:06 +09003735 public static final int CALLBACK_AVAILABLE = 2;
Robert Greenwalt7fb8adc2015-06-24 13:23:42 -07003736 /** @hide arg1 = TTL */
Remi NGUYEN VAN1fb66ad2021-03-12 15:24:06 +09003737 public static final int CALLBACK_LOSING = 3;
Robert Greenwalt7fb8adc2015-06-24 13:23:42 -07003738 /** @hide */
Remi NGUYEN VAN1fb66ad2021-03-12 15:24:06 +09003739 public static final int CALLBACK_LOST = 4;
Robert Greenwalt7fb8adc2015-06-24 13:23:42 -07003740 /** @hide */
Remi NGUYEN VAN1fb66ad2021-03-12 15:24:06 +09003741 public static final int CALLBACK_UNAVAIL = 5;
Robert Greenwalt7fb8adc2015-06-24 13:23:42 -07003742 /** @hide */
Remi NGUYEN VAN1fb66ad2021-03-12 15:24:06 +09003743 public static final int CALLBACK_CAP_CHANGED = 6;
Robert Greenwalt7fb8adc2015-06-24 13:23:42 -07003744 /** @hide */
Remi NGUYEN VAN1fb66ad2021-03-12 15:24:06 +09003745 public static final int CALLBACK_IP_CHANGED = 7;
Robert Greenwalt3dc73e52014-05-15 18:07:26 -07003746 /** @hide obj = NetworkCapabilities, arg1 = seq number */
Remi NGUYEN VAN1fb66ad2021-03-12 15:24:06 +09003747 private static final int EXPIRE_LEGACY_REQUEST = 8;
Robert Greenwalt7fb8adc2015-06-24 13:23:42 -07003748 /** @hide */
Remi NGUYEN VAN1fb66ad2021-03-12 15:24:06 +09003749 public static final int CALLBACK_SUSPENDED = 9;
Robert Greenwalt7fb8adc2015-06-24 13:23:42 -07003750 /** @hide */
Remi NGUYEN VAN1fb66ad2021-03-12 15:24:06 +09003751 public static final int CALLBACK_RESUMED = 10;
junyulaif2c67e42018-08-07 19:50:45 +08003752 /** @hide */
Remi NGUYEN VAN1fb66ad2021-03-12 15:24:06 +09003753 public static final int CALLBACK_BLK_CHANGED = 11;
Robert Greenwaltf99b8392014-03-26 16:47:06 -07003754
Erik Kline155a59a2015-11-25 12:49:38 +09003755 /** @hide */
3756 public static String getCallbackName(int whichCallback) {
3757 switch (whichCallback) {
3758 case CALLBACK_PRECHECK: return "CALLBACK_PRECHECK";
3759 case CALLBACK_AVAILABLE: return "CALLBACK_AVAILABLE";
3760 case CALLBACK_LOSING: return "CALLBACK_LOSING";
3761 case CALLBACK_LOST: return "CALLBACK_LOST";
3762 case CALLBACK_UNAVAIL: return "CALLBACK_UNAVAIL";
3763 case CALLBACK_CAP_CHANGED: return "CALLBACK_CAP_CHANGED";
3764 case CALLBACK_IP_CHANGED: return "CALLBACK_IP_CHANGED";
Erik Kline155a59a2015-11-25 12:49:38 +09003765 case EXPIRE_LEGACY_REQUEST: return "EXPIRE_LEGACY_REQUEST";
3766 case CALLBACK_SUSPENDED: return "CALLBACK_SUSPENDED";
3767 case CALLBACK_RESUMED: return "CALLBACK_RESUMED";
junyulaif2c67e42018-08-07 19:50:45 +08003768 case CALLBACK_BLK_CHANGED: return "CALLBACK_BLK_CHANGED";
Erik Kline155a59a2015-11-25 12:49:38 +09003769 default:
3770 return Integer.toString(whichCallback);
3771 }
3772 }
3773
Robert Greenwalt3dc73e52014-05-15 18:07:26 -07003774 private class CallbackHandler extends Handler {
Robert Greenwaltf99b8392014-03-26 16:47:06 -07003775 private static final String TAG = "ConnectivityManager.CallbackHandler";
Robert Greenwalt72877c22015-09-03 16:41:45 -07003776 private static final boolean DBG = false;
Robert Greenwaltf99b8392014-03-26 16:47:06 -07003777
Hugo Benichi0b42baf2016-07-06 22:53:17 +09003778 CallbackHandler(Looper looper) {
Robert Greenwaltf99b8392014-03-26 16:47:06 -07003779 super(looper);
Robert Greenwaltf99b8392014-03-26 16:47:06 -07003780 }
3781
Hugo Benichifd44e912017-02-02 17:02:36 +09003782 CallbackHandler(Handler handler) {
Remi NGUYEN VAN81f68422021-03-15 07:31:54 +00003783 this(Objects.requireNonNull(handler, "Handler cannot be null.").getLooper());
Hugo Benichifd44e912017-02-02 17:02:36 +09003784 }
3785
Robert Greenwaltf99b8392014-03-26 16:47:06 -07003786 @Override
3787 public void handleMessage(Message message) {
Hugo Benichib6c24062017-05-09 14:36:02 +09003788 if (message.what == EXPIRE_LEGACY_REQUEST) {
3789 expireRequest((NetworkCapabilities) message.obj, message.arg1);
3790 return;
3791 }
3792
3793 final NetworkRequest request = getObject(message, NetworkRequest.class);
3794 final Network network = getObject(message, Network.class);
3795 final NetworkCallback callback;
3796 synchronized (sCallbacks) {
3797 callback = sCallbacks.get(request);
Etan Cohenb58e3662019-05-21 12:06:04 -07003798 if (callback == null) {
3799 Log.w(TAG,
3800 "callback not found for " + getCallbackName(message.what) + " message");
3801 return;
3802 }
Etan Cohen6cb65992019-04-16 15:07:55 -07003803 if (message.what == CALLBACK_UNAVAIL) {
3804 sCallbacks.remove(request);
3805 callback.networkRequest = ALREADY_UNREGISTERED;
3806 }
Hugo Benichib6c24062017-05-09 14:36:02 +09003807 }
Lorenzo Colittib027e6e2016-03-01 22:56:37 +09003808 if (DBG) {
Hugo Benichi8d962922017-03-22 17:07:57 +09003809 Log.d(TAG, getCallbackName(message.what) + " for network " + network);
Lorenzo Colittib027e6e2016-03-01 22:56:37 +09003810 }
Hugo Benichib6c24062017-05-09 14:36:02 +09003811
Robert Greenwaltf99b8392014-03-26 16:47:06 -07003812 switch (message.what) {
3813 case CALLBACK_PRECHECK: {
Hugo Benichib6c24062017-05-09 14:36:02 +09003814 callback.onPreCheck(network);
Robert Greenwaltf99b8392014-03-26 16:47:06 -07003815 break;
3816 }
3817 case CALLBACK_AVAILABLE: {
Chalard Jeana23bc9e2018-01-30 22:41:41 +09003818 NetworkCapabilities cap = getObject(message, NetworkCapabilities.class);
3819 LinkProperties lp = getObject(message, LinkProperties.class);
Lorenzo Colitti79c6f222021-03-18 00:54:57 +09003820 callback.onAvailable(network, cap, lp, message.arg1);
Robert Greenwaltf99b8392014-03-26 16:47:06 -07003821 break;
3822 }
3823 case CALLBACK_LOSING: {
Hugo Benichib6c24062017-05-09 14:36:02 +09003824 callback.onLosing(network, message.arg1);
Robert Greenwaltf99b8392014-03-26 16:47:06 -07003825 break;
3826 }
3827 case CALLBACK_LOST: {
Hugo Benichib6c24062017-05-09 14:36:02 +09003828 callback.onLost(network);
Robert Greenwaltf99b8392014-03-26 16:47:06 -07003829 break;
3830 }
3831 case CALLBACK_UNAVAIL: {
Hugo Benichib6c24062017-05-09 14:36:02 +09003832 callback.onUnavailable();
Robert Greenwaltf99b8392014-03-26 16:47:06 -07003833 break;
3834 }
3835 case CALLBACK_CAP_CHANGED: {
Hugo Benichib6c24062017-05-09 14:36:02 +09003836 NetworkCapabilities cap = getObject(message, NetworkCapabilities.class);
3837 callback.onCapabilitiesChanged(network, cap);
Robert Greenwaltf99b8392014-03-26 16:47:06 -07003838 break;
3839 }
3840 case CALLBACK_IP_CHANGED: {
Hugo Benichib6c24062017-05-09 14:36:02 +09003841 LinkProperties lp = getObject(message, LinkProperties.class);
3842 callback.onLinkPropertiesChanged(network, lp);
Robert Greenwaltf99b8392014-03-26 16:47:06 -07003843 break;
3844 }
Robert Greenwalt7fb8adc2015-06-24 13:23:42 -07003845 case CALLBACK_SUSPENDED: {
Hugo Benichib6c24062017-05-09 14:36:02 +09003846 callback.onNetworkSuspended(network);
Robert Greenwalt7fb8adc2015-06-24 13:23:42 -07003847 break;
3848 }
3849 case CALLBACK_RESUMED: {
Hugo Benichib6c24062017-05-09 14:36:02 +09003850 callback.onNetworkResumed(network);
Robert Greenwalt3dc73e52014-05-15 18:07:26 -07003851 break;
3852 }
junyulaif2c67e42018-08-07 19:50:45 +08003853 case CALLBACK_BLK_CHANGED: {
Lorenzo Colitti79c6f222021-03-18 00:54:57 +09003854 callback.onBlockedStatusChanged(network, message.arg1);
junyulaif2c67e42018-08-07 19:50:45 +08003855 }
Robert Greenwaltf99b8392014-03-26 16:47:06 -07003856 }
3857 }
3858
Hugo Benichi0b42baf2016-07-06 22:53:17 +09003859 private <T> T getObject(Message msg, Class<T> c) {
3860 return (T) msg.getData().getParcelable(c.getSimpleName());
Robert Greenwaltf99b8392014-03-26 16:47:06 -07003861 }
Robert Greenwaltf99b8392014-03-26 16:47:06 -07003862 }
3863
Hugo Benichifd44e912017-02-02 17:02:36 +09003864 private CallbackHandler getDefaultHandler() {
Hugo Benichi3b41f052016-07-07 10:15:56 +09003865 synchronized (sCallbacks) {
3866 if (sCallbackHandler == null) {
3867 sCallbackHandler = new CallbackHandler(ConnectivityThread.getInstanceLooper());
Robert Greenwaltf99b8392014-03-26 16:47:06 -07003868 }
Hugo Benichi3b41f052016-07-07 10:15:56 +09003869 return sCallbackHandler;
Robert Greenwaltf99b8392014-03-26 16:47:06 -07003870 }
3871 }
3872
Hugo Benichibc4ac972017-02-03 14:18:44 +09003873 private static final HashMap<NetworkRequest, NetworkCallback> sCallbacks = new HashMap<>();
3874 private static CallbackHandler sCallbackHandler;
Robert Greenwaltf99b8392014-03-26 16:47:06 -07003875
Lorenzo Colitti2a49c5d2021-03-12 22:48:07 +09003876 private NetworkRequest sendRequestForNetwork(int asUid, NetworkCapabilities need,
3877 NetworkCallback callback, int timeoutMs, NetworkRequest.Type reqType, int legacyType,
3878 CallbackHandler handler) {
Soi, Yoshinarib8102ef2015-11-12 12:09:02 +09003879 printStackTrace();
Hugo Benichibc1104b2017-05-09 15:19:01 +09003880 checkCallbackNotNull(callback);
Remi NGUYEN VAN81f68422021-03-15 07:31:54 +00003881 if (reqType != TRACK_DEFAULT && reqType != TRACK_SYSTEM_DEFAULT && need == null) {
3882 throw new IllegalArgumentException("null NetworkCapabilities");
3883 }
Hugo Benichi0b42baf2016-07-06 22:53:17 +09003884 final NetworkRequest request;
Roshan Piusbc7e37d2020-01-16 12:17:17 -08003885 final String callingPackageName = mContext.getOpPackageName();
Robert Greenwaltf99b8392014-03-26 16:47:06 -07003886 try {
Hugo Benichi0b42baf2016-07-06 22:53:17 +09003887 synchronized(sCallbacks) {
Hugo Benichibee30fe2017-06-17 13:14:12 +09003888 if (callback.networkRequest != null
3889 && callback.networkRequest != ALREADY_UNREGISTERED) {
Hugo Benichi2aa65af2017-03-06 09:17:06 +09003890 // TODO: throw exception instead and enforce 1:1 mapping of callbacks
3891 // and requests (http://b/20701525).
3892 Log.e(TAG, "NetworkCallback was already registered");
3893 }
Hugo Benichi3b41f052016-07-07 10:15:56 +09003894 Messenger messenger = new Messenger(handler);
Hugo Benichi0b42baf2016-07-06 22:53:17 +09003895 Binder binder = new Binder();
Roshan Pius951c0032020-12-22 15:10:42 -08003896 final int callbackFlags = callback.mFlags;
junyulaiad010792021-01-11 16:53:38 +08003897 if (reqType == LISTEN) {
Roshan Piusbc7e37d2020-01-16 12:17:17 -08003898 request = mService.listenForNetwork(
Roshan Pius951c0032020-12-22 15:10:42 -08003899 need, messenger, binder, callbackFlags, callingPackageName,
Roshan Piusaa24fde2020-12-17 14:53:09 -08003900 getAttributionTag());
Paul Jensenbb427682014-06-27 11:05:32 -04003901 } else {
Hugo Benichi0b42baf2016-07-06 22:53:17 +09003902 request = mService.requestNetwork(
Lorenzo Colitti2a49c5d2021-03-12 22:48:07 +09003903 asUid, need, reqType.ordinal(), messenger, timeoutMs, binder,
3904 legacyType, callbackFlags, callingPackageName, getAttributionTag());
Paul Jensenbb427682014-06-27 11:05:32 -04003905 }
Hugo Benichi0b42baf2016-07-06 22:53:17 +09003906 if (request != null) {
Hugo Benichi3b41f052016-07-07 10:15:56 +09003907 sCallbacks.put(request, callback);
Robert Greenwaltf99b8392014-03-26 16:47:06 -07003908 }
Hugo Benichi3b41f052016-07-07 10:15:56 +09003909 callback.networkRequest = request;
Robert Greenwaltf99b8392014-03-26 16:47:06 -07003910 }
Jeff Sharkeyc78f85c2016-03-01 19:27:23 -07003911 } catch (RemoteException e) {
3912 throw e.rethrowFromSystemServer();
Hugo Benichia590ab82017-05-11 13:16:17 +09003913 } catch (ServiceSpecificException e) {
3914 throw convertServiceException(e);
Jeff Sharkeyc78f85c2016-03-01 19:27:23 -07003915 }
Hugo Benichi0b42baf2016-07-06 22:53:17 +09003916 return request;
Robert Greenwaltf99b8392014-03-26 16:47:06 -07003917 }
3918
Lorenzo Colitti2a49c5d2021-03-12 22:48:07 +09003919 private NetworkRequest sendRequestForNetwork(NetworkCapabilities need, NetworkCallback callback,
3920 int timeoutMs, NetworkRequest.Type reqType, int legacyType, CallbackHandler handler) {
3921 return sendRequestForNetwork(Process.INVALID_UID, need, callback, timeoutMs, reqType,
3922 legacyType, handler);
3923 }
3924
Robert Greenwaltf99b8392014-03-26 16:47:06 -07003925 /**
Erik Kline23bf99c2016-03-16 15:31:39 +09003926 * Helper function to request a network with a particular legacy type.
Lorenzo Colitti6653c4d2015-11-25 12:00:52 +09003927 *
markchienfac84a22020-03-18 21:16:15 +08003928 * This API is only for use in internal system code that requests networks with legacy type and
3929 * relies on CONNECTIVITY_ACTION broadcasts instead of NetworkCallbacks. New caller should use
markchiend6eeffd2020-01-14 00:55:21 +08003930 * {@link #requestNetwork(NetworkRequest, NetworkCallback, Handler)} instead.
Lorenzo Colitti6653c4d2015-11-25 12:00:52 +09003931 *
markchiend6eeffd2020-01-14 00:55:21 +08003932 * @param request {@link NetworkRequest} describing this request.
markchiend6eeffd2020-01-14 00:55:21 +08003933 * @param timeoutMs The time in milliseconds to attempt looking for a suitable network
3934 * before {@link NetworkCallback#onUnavailable()} is called. The timeout must
3935 * be a positive value (i.e. >0).
3936 * @param legacyType to specify the network type(#TYPE_*).
3937 * @param handler {@link Handler} to specify the thread upon which the callback will be invoked.
markchienfac84a22020-03-18 21:16:15 +08003938 * @param networkCallback The {@link NetworkCallback} to be utilized for this request. Note
3939 * the callback must not be shared - it uniquely specifies this request.
Lorenzo Colitti6653c4d2015-11-25 12:00:52 +09003940 *
3941 * @hide
3942 */
markchiend6eeffd2020-01-14 00:55:21 +08003943 @SystemApi
markchienfac84a22020-03-18 21:16:15 +08003944 @RequiresPermission(NetworkStack.PERMISSION_MAINLINE_NETWORK_STACK)
Chalard Jean158702d2019-01-07 19:26:34 +09003945 public void requestNetwork(@NonNull NetworkRequest request,
markchienfac84a22020-03-18 21:16:15 +08003946 int timeoutMs, int legacyType, @NonNull Handler handler,
3947 @NonNull NetworkCallback networkCallback) {
3948 if (legacyType == TYPE_NONE) {
3949 throw new IllegalArgumentException("TYPE_NONE is meaningless legacy type");
3950 }
Hugo Benichifd44e912017-02-02 17:02:36 +09003951 CallbackHandler cbHandler = new CallbackHandler(handler);
3952 NetworkCapabilities nc = request.networkCapabilities;
3953 sendRequestForNetwork(nc, networkCallback, timeoutMs, REQUEST, legacyType, cbHandler);
Lorenzo Colitti6653c4d2015-11-25 12:00:52 +09003954 }
3955
3956 /**
Roshan Pius951c0032020-12-22 15:10:42 -08003957 * Request a network to satisfy a set of {@link NetworkCapabilities}.
Robert Greenwaltf99b8392014-03-26 16:47:06 -07003958 *
Chalard Jean01378b62019-08-30 16:27:28 +09003959 * <p>This method will attempt to find the best network that matches the passed
3960 * {@link NetworkRequest}, and to bring up one that does if none currently satisfies the
3961 * criteria. The platform will evaluate which network is the best at its own discretion.
3962 * Throughput, latency, cost per byte, policy, user preference and other considerations
3963 * may be factored in the decision of what is considered the best network.
3964 *
3965 * <p>As long as this request is outstanding, the platform will try to maintain the best network
3966 * matching this request, while always attempting to match the request to a better network if
3967 * possible. If a better match is found, the platform will switch this request to the now-best
3968 * network and inform the app of the newly best network by invoking
3969 * {@link NetworkCallback#onAvailable(Network)} on the provided callback. Note that the platform
3970 * will not try to maintain any other network than the best one currently matching the request:
3971 * a network not matching any network request may be disconnected at any time.
3972 *
3973 * <p>For example, an application could use this method to obtain a connected cellular network
3974 * even if the device currently has a data connection over Ethernet. This may cause the cellular
3975 * radio to consume additional power. Or, an application could inform the system that it wants
3976 * a network supporting sending MMSes and have the system let it know about the currently best
3977 * MMS-supporting network through the provided {@link NetworkCallback}.
3978 *
3979 * <p>The status of the request can be followed by listening to the various callbacks described
3980 * in {@link NetworkCallback}. The {@link Network} object passed to the callback methods can be
3981 * used to direct traffic to the network (although accessing some networks may be subject to
3982 * holding specific permissions). Callers will learn about the specific characteristics of the
3983 * network through
3984 * {@link NetworkCallback#onCapabilitiesChanged(Network, NetworkCapabilities)} and
3985 * {@link NetworkCallback#onLinkPropertiesChanged(Network, LinkProperties)}. The methods of the
3986 * provided {@link NetworkCallback} will only be invoked due to changes in the best network
3987 * matching the request at any given time; therefore when a better network matching the request
3988 * becomes available, the {@link NetworkCallback#onAvailable(Network)} method is called
3989 * with the new network after which no further updates are given about the previously-best
3990 * network, unless it becomes the best again at some later time. All callbacks are invoked
3991 * in order on the same thread, which by default is a thread created by the framework running
3992 * in the app.
3993 * {@see #requestNetwork(NetworkRequest, NetworkCallback, Handler)} to change where the
3994 * callbacks are invoked.
3995 *
3996 * <p>This{@link NetworkRequest} will live until released via
3997 * {@link #unregisterNetworkCallback(NetworkCallback)} or the calling application exits, at
3998 * which point the system may let go of the network at any time.
3999 *
4000 * <p>A version of this method which takes a timeout is
4001 * {@link #requestNetwork(NetworkRequest, NetworkCallback, int)}, that an app can use to only
4002 * wait for a limited amount of time for the network to become unavailable.
4003 *
Paul Jensenee52d232015-06-16 15:11:58 -04004004 * <p>It is presently unsupported to request a network with mutable
4005 * {@link NetworkCapabilities} such as
4006 * {@link NetworkCapabilities#NET_CAPABILITY_VALIDATED} or
4007 * {@link NetworkCapabilities#NET_CAPABILITY_CAPTIVE_PORTAL}
4008 * as these {@code NetworkCapabilities} represent states that a particular
4009 * network may never attain, and whether a network will attain these states
4010 * is unknown prior to bringing up the network so the framework does not
Chalard Jean01378b62019-08-30 16:27:28 +09004011 * know how to go about satisfying a request with these capabilities.
Lorenzo Colitti0b40c872015-10-15 16:29:00 +09004012 *
4013 * <p>This method requires the caller to hold either the
4014 * {@link android.Manifest.permission#CHANGE_NETWORK_STATE} permission
4015 * or the ability to modify system settings as determined by
4016 * {@link android.provider.Settings.System#canWrite}.</p>
Robert Greenwaltf99b8392014-03-26 16:47:06 -07004017 *
Chalard Jeana5ff1132020-05-20 16:11:50 +09004018 * <p>To avoid performance issues due to apps leaking callbacks, the system will limit the
4019 * number of outstanding requests to 100 per app (identified by their UID), shared with
4020 * all variants of this method, of {@link #registerNetworkCallback} as well as
4021 * {@link ConnectivityDiagnosticsManager#registerConnectivityDiagnosticsCallback}.
4022 * Requesting a network with this method will count toward this limit. If this limit is
4023 * exceeded, an exception will be thrown. To avoid hitting this issue and to conserve resources,
4024 * make sure to unregister the callbacks with
4025 * {@link #unregisterNetworkCallback(NetworkCallback)}.
4026 *
Robert Greenwaltf57c03c2014-06-11 16:05:07 -07004027 * @param request {@link NetworkRequest} describing this request.
Hugo Benichifd44e912017-02-02 17:02:36 +09004028 * @param networkCallback The {@link NetworkCallback} to be utilized for this request. Note
4029 * the callback must not be shared - it uniquely specifies this request.
4030 * The callback is invoked on the default internal Handler.
Chalard Jean31740e42019-05-13 15:13:58 +09004031 * @throws IllegalArgumentException if {@code request} contains invalid network capabilities.
4032 * @throws SecurityException if missing the appropriate permissions.
Chalard Jeana5ff1132020-05-20 16:11:50 +09004033 * @throws RuntimeException if the app already has too many callbacks registered.
Robert Greenwaltf99b8392014-03-26 16:47:06 -07004034 */
Chalard Jean158702d2019-01-07 19:26:34 +09004035 public void requestNetwork(@NonNull NetworkRequest request,
4036 @NonNull NetworkCallback networkCallback) {
Hugo Benichifd44e912017-02-02 17:02:36 +09004037 requestNetwork(request, networkCallback, getDefaultHandler());
4038 }
4039
4040 /**
Roshan Pius951c0032020-12-22 15:10:42 -08004041 * Request a network to satisfy a set of {@link NetworkCapabilities}.
Hugo Benichifd44e912017-02-02 17:02:36 +09004042 *
Chalard Jean01378b62019-08-30 16:27:28 +09004043 * This method behaves identically to {@link #requestNetwork(NetworkRequest, NetworkCallback)}
4044 * but runs all the callbacks on the passed Handler.
Hugo Benichifd44e912017-02-02 17:02:36 +09004045 *
Chalard Jean01378b62019-08-30 16:27:28 +09004046 * <p>This method has the same permission requirements as
Chalard Jeana5ff1132020-05-20 16:11:50 +09004047 * {@link #requestNetwork(NetworkRequest, NetworkCallback)}, is subject to the same limitations,
4048 * and throws the same exceptions in the same conditions.
Hugo Benichifd44e912017-02-02 17:02:36 +09004049 *
4050 * @param request {@link NetworkRequest} describing this request.
4051 * @param networkCallback The {@link NetworkCallback} to be utilized for this request. Note
4052 * the callback must not be shared - it uniquely specifies this request.
4053 * @param handler {@link Handler} to specify the thread upon which the callback will be invoked.
Hugo Benichifd44e912017-02-02 17:02:36 +09004054 */
Chalard Jean158702d2019-01-07 19:26:34 +09004055 public void requestNetwork(@NonNull NetworkRequest request,
4056 @NonNull NetworkCallback networkCallback, @NonNull Handler handler) {
Hugo Benichifd44e912017-02-02 17:02:36 +09004057 CallbackHandler cbHandler = new CallbackHandler(handler);
markchienfac84a22020-03-18 21:16:15 +08004058 NetworkCapabilities nc = request.networkCapabilities;
4059 sendRequestForNetwork(nc, networkCallback, 0, REQUEST, TYPE_NONE, cbHandler);
Robert Greenwaltf99b8392014-03-26 16:47:06 -07004060 }
4061
4062 /**
Roshan Pius951c0032020-12-22 15:10:42 -08004063 * Request a network to satisfy a set of {@link NetworkCapabilities}, limited
Etan Cohenf67bde92017-03-01 12:47:28 -08004064 * by a timeout.
4065 *
4066 * This function behaves identically to the non-timed-out version
4067 * {@link #requestNetwork(NetworkRequest, NetworkCallback)}, but if a suitable network
4068 * is not found within the given time (in milliseconds) the
4069 * {@link NetworkCallback#onUnavailable()} callback is called. The request can still be
4070 * released normally by calling {@link #unregisterNetworkCallback(NetworkCallback)} but does
4071 * not have to be released if timed-out (it is automatically released). Unregistering a
4072 * request that timed out is not an error.
4073 *
4074 * <p>Do not use this method to poll for the existence of specific networks (e.g. with a small
4075 * timeout) - {@link #registerNetworkCallback(NetworkRequest, NetworkCallback)} is provided
4076 * for that purpose. Calling this method will attempt to bring up the requested network.
4077 *
Chalard Jean01378b62019-08-30 16:27:28 +09004078 * <p>This method has the same permission requirements as
Chalard Jeana5ff1132020-05-20 16:11:50 +09004079 * {@link #requestNetwork(NetworkRequest, NetworkCallback)}, is subject to the same limitations,
4080 * and throws the same exceptions in the same conditions.
Etan Cohenf67bde92017-03-01 12:47:28 -08004081 *
4082 * @param request {@link NetworkRequest} describing this request.
Lorenzo Colitti139a48c2017-04-28 00:56:30 +09004083 * @param networkCallback The {@link NetworkCallback} to be utilized for this request. Note
4084 * the callback must not be shared - it uniquely specifies this request.
Etan Cohenf67bde92017-03-01 12:47:28 -08004085 * @param timeoutMs The time in milliseconds to attempt looking for a suitable network
4086 * before {@link NetworkCallback#onUnavailable()} is called. The timeout must
4087 * be a positive value (i.e. >0).
Etan Cohenf67bde92017-03-01 12:47:28 -08004088 */
Chalard Jean158702d2019-01-07 19:26:34 +09004089 public void requestNetwork(@NonNull NetworkRequest request,
4090 @NonNull NetworkCallback networkCallback, int timeoutMs) {
Hugo Benichibc1104b2017-05-09 15:19:01 +09004091 checkTimeout(timeoutMs);
markchienfac84a22020-03-18 21:16:15 +08004092 NetworkCapabilities nc = request.networkCapabilities;
4093 sendRequestForNetwork(nc, networkCallback, timeoutMs, REQUEST, TYPE_NONE,
4094 getDefaultHandler());
Hugo Benichifd44e912017-02-02 17:02:36 +09004095 }
4096
Hugo Benichifd44e912017-02-02 17:02:36 +09004097 /**
Roshan Pius951c0032020-12-22 15:10:42 -08004098 * Request a network to satisfy a set of {@link NetworkCapabilities}, limited
Hugo Benichifd44e912017-02-02 17:02:36 +09004099 * by a timeout.
4100 *
Chalard Jean01378b62019-08-30 16:27:28 +09004101 * This method behaves identically to
4102 * {@link #requestNetwork(NetworkRequest, NetworkCallback, int)} but runs all the callbacks
4103 * on the passed Handler.
Etan Cohenf67bde92017-03-01 12:47:28 -08004104 *
Chalard Jean01378b62019-08-30 16:27:28 +09004105 * <p>This method has the same permission requirements as
Chalard Jeana5ff1132020-05-20 16:11:50 +09004106 * {@link #requestNetwork(NetworkRequest, NetworkCallback)}, is subject to the same limitations,
4107 * and throws the same exceptions in the same conditions.
Hugo Benichifd44e912017-02-02 17:02:36 +09004108 *
4109 * @param request {@link NetworkRequest} describing this request.
Etan Cohenf67bde92017-03-01 12:47:28 -08004110 * @param networkCallback The {@link NetworkCallback} to be utilized for this request. Note
4111 * the callback must not be shared - it uniquely specifies this request.
Hugo Benichifd44e912017-02-02 17:02:36 +09004112 * @param handler {@link Handler} to specify the thread upon which the callback will be invoked.
Lorenzo Colitti139a48c2017-04-28 00:56:30 +09004113 * @param timeoutMs The time in milliseconds to attempt looking for a suitable network
4114 * before {@link NetworkCallback#onUnavailable} is called.
Hugo Benichifd44e912017-02-02 17:02:36 +09004115 */
Chalard Jean158702d2019-01-07 19:26:34 +09004116 public void requestNetwork(@NonNull NetworkRequest request,
4117 @NonNull NetworkCallback networkCallback, @NonNull Handler handler, int timeoutMs) {
Hugo Benichibc1104b2017-05-09 15:19:01 +09004118 checkTimeout(timeoutMs);
Hugo Benichifd44e912017-02-02 17:02:36 +09004119 CallbackHandler cbHandler = new CallbackHandler(handler);
markchienfac84a22020-03-18 21:16:15 +08004120 NetworkCapabilities nc = request.networkCapabilities;
4121 sendRequestForNetwork(nc, networkCallback, timeoutMs, REQUEST, TYPE_NONE, cbHandler);
Robert Greenwaltf99b8392014-03-26 16:47:06 -07004122 }
4123
4124 /**
Robert Greenwaltf3017f72014-05-18 23:07:25 -07004125 * The lookup key for a {@link Network} object included with the intent after
Jeremy Joslin60d379b2014-11-05 10:32:09 -08004126 * successfully finding a network for the applications request. Retrieve it with
Robert Greenwaltf3017f72014-05-18 23:07:25 -07004127 * {@link android.content.Intent#getParcelableExtra(String)}.
Jeremy Joslinc5302632015-02-11 16:51:13 -08004128 * <p>
Paul Jensenee2f45d2015-03-10 10:54:12 -04004129 * Note that if you intend to invoke {@link Network#openConnection(java.net.URL)}
4130 * then you must get a ConnectivityManager instance before doing so.
Robert Greenwaltf3017f72014-05-18 23:07:25 -07004131 */
Erik Kline8a826212014-11-19 12:12:24 +09004132 public static final String EXTRA_NETWORK = "android.net.extra.NETWORK";
Robert Greenwaltf3017f72014-05-18 23:07:25 -07004133
4134 /**
Robert Greenwaltf57c03c2014-06-11 16:05:07 -07004135 * The lookup key for a {@link NetworkRequest} object included with the intent after
Jeremy Joslin60d379b2014-11-05 10:32:09 -08004136 * successfully finding a network for the applications request. Retrieve it with
Robert Greenwaltf3017f72014-05-18 23:07:25 -07004137 * {@link android.content.Intent#getParcelableExtra(String)}.
4138 */
Erik Kline8a826212014-11-19 12:12:24 +09004139 public static final String EXTRA_NETWORK_REQUEST = "android.net.extra.NETWORK_REQUEST";
Robert Greenwaltf3017f72014-05-18 23:07:25 -07004140
4141
4142 /**
Roshan Pius951c0032020-12-22 15:10:42 -08004143 * Request a network to satisfy a set of {@link NetworkCapabilities}.
Robert Greenwaltf99b8392014-03-26 16:47:06 -07004144 *
Jeremy Joslin60d379b2014-11-05 10:32:09 -08004145 * This function behaves identically to the version that takes a NetworkCallback, but instead
Robert Greenwaltf57c03c2014-06-11 16:05:07 -07004146 * of {@link NetworkCallback} a {@link PendingIntent} is used. This means
Robert Greenwaltf3017f72014-05-18 23:07:25 -07004147 * the request may outlive the calling application and get called back when a suitable
4148 * network is found.
Robert Greenwaltf99b8392014-03-26 16:47:06 -07004149 * <p>
4150 * The operation is an Intent broadcast that goes to a broadcast receiver that
4151 * you registered with {@link Context#registerReceiver} or through the
4152 * &lt;receiver&gt; tag in an AndroidManifest.xml file
4153 * <p>
4154 * The operation Intent is delivered with two extras, a {@link Network} typed
Erik Kline8a826212014-11-19 12:12:24 +09004155 * extra called {@link #EXTRA_NETWORK} and a {@link NetworkRequest}
4156 * typed extra called {@link #EXTRA_NETWORK_REQUEST} containing
Robert Greenwaltf99b8392014-03-26 16:47:06 -07004157 * the original requests parameters. It is important to create a new,
Robert Greenwaltf57c03c2014-06-11 16:05:07 -07004158 * {@link NetworkCallback} based request before completing the processing of the
Robert Greenwaltf99b8392014-03-26 16:47:06 -07004159 * Intent to reserve the network or it will be released shortly after the Intent
4160 * is processed.
4161 * <p>
Paul Jensenc8873fc2015-06-17 14:15:39 -04004162 * If there is already a request for this Intent registered (with the equality of
Robert Greenwaltf99b8392014-03-26 16:47:06 -07004163 * two Intents defined by {@link Intent#filterEquals}), then it will be removed and
Robert Greenwaltf3017f72014-05-18 23:07:25 -07004164 * replaced by this one, effectively releasing the previous {@link NetworkRequest}.
Robert Greenwaltf99b8392014-03-26 16:47:06 -07004165 * <p>
Jeremy Joslin60d379b2014-11-05 10:32:09 -08004166 * The request may be released normally by calling
4167 * {@link #releaseNetworkRequest(android.app.PendingIntent)}.
Paul Jensenee52d232015-06-16 15:11:58 -04004168 * <p>It is presently unsupported to request a network with either
4169 * {@link NetworkCapabilities#NET_CAPABILITY_VALIDATED} or
4170 * {@link NetworkCapabilities#NET_CAPABILITY_CAPTIVE_PORTAL}
4171 * as these {@code NetworkCapabilities} represent states that a particular
4172 * network may never attain, and whether a network will attain these states
4173 * is unknown prior to bringing up the network so the framework does not
Chalard Jean9dd11612018-06-04 16:52:49 +09004174 * know how to go about satisfying a request with these capabilities.
Lorenzo Colitti0b40c872015-10-15 16:29:00 +09004175 *
Chalard Jeana5ff1132020-05-20 16:11:50 +09004176 * <p>To avoid performance issues due to apps leaking callbacks, the system will limit the
4177 * number of outstanding requests to 100 per app (identified by their UID), shared with
4178 * all variants of this method, of {@link #registerNetworkCallback} as well as
4179 * {@link ConnectivityDiagnosticsManager#registerConnectivityDiagnosticsCallback}.
4180 * Requesting a network with this method will count toward this limit. If this limit is
4181 * exceeded, an exception will be thrown. To avoid hitting this issue and to conserve resources,
4182 * make sure to unregister the callbacks with {@link #unregisterNetworkCallback(PendingIntent)}
4183 * or {@link #releaseNetworkRequest(PendingIntent)}.
4184 *
Lorenzo Colitti0b40c872015-10-15 16:29:00 +09004185 * <p>This method requires the caller to hold either the
4186 * {@link android.Manifest.permission#CHANGE_NETWORK_STATE} permission
4187 * or the ability to modify system settings as determined by
4188 * {@link android.provider.Settings.System#canWrite}.</p>
4189 *
Robert Greenwaltf57c03c2014-06-11 16:05:07 -07004190 * @param request {@link NetworkRequest} describing this request.
Robert Greenwaltf99b8392014-03-26 16:47:06 -07004191 * @param operation Action to perform when the network is available (corresponds
Robert Greenwaltf57c03c2014-06-11 16:05:07 -07004192 * to the {@link NetworkCallback#onAvailable} call. Typically
Jeremy Joslin60d379b2014-11-05 10:32:09 -08004193 * comes from {@link PendingIntent#getBroadcast}. Cannot be null.
Chalard Jean31740e42019-05-13 15:13:58 +09004194 * @throws IllegalArgumentException if {@code request} contains invalid network capabilities.
4195 * @throws SecurityException if missing the appropriate permissions.
Chalard Jeana5ff1132020-05-20 16:11:50 +09004196 * @throws RuntimeException if the app already has too many callbacks registered.
Robert Greenwaltf99b8392014-03-26 16:47:06 -07004197 */
Chalard Jean158702d2019-01-07 19:26:34 +09004198 public void requestNetwork(@NonNull NetworkRequest request,
4199 @NonNull PendingIntent operation) {
Soi, Yoshinarib8102ef2015-11-12 12:09:02 +09004200 printStackTrace();
Hugo Benichibc1104b2017-05-09 15:19:01 +09004201 checkPendingIntentNotNull(operation);
Robert Greenwaltf99b8392014-03-26 16:47:06 -07004202 try {
Roshan Piusbc7e37d2020-01-16 12:17:17 -08004203 mService.pendingRequestForNetwork(
Philip P. Moltmann7bc33df2020-03-26 11:50:35 -07004204 request.networkCapabilities, operation, mContext.getOpPackageName(),
4205 getAttributionTag());
Jeff Sharkeyc78f85c2016-03-01 19:27:23 -07004206 } catch (RemoteException e) {
4207 throw e.rethrowFromSystemServer();
Hugo Benichia590ab82017-05-11 13:16:17 +09004208 } catch (ServiceSpecificException e) {
4209 throw convertServiceException(e);
Jeff Sharkeyc78f85c2016-03-01 19:27:23 -07004210 }
Robert Greenwaltf99b8392014-03-26 16:47:06 -07004211 }
4212
4213 /**
Jeremy Joslin60d379b2014-11-05 10:32:09 -08004214 * Removes a request made via {@link #requestNetwork(NetworkRequest, android.app.PendingIntent)}
4215 * <p>
Lorenzo Colitti15874cd2016-04-13 22:00:02 +09004216 * This method has the same behavior as
4217 * {@link #unregisterNetworkCallback(android.app.PendingIntent)} with respect to
Jeremy Joslin60d379b2014-11-05 10:32:09 -08004218 * releasing network resources and disconnecting.
4219 *
4220 * @param operation A PendingIntent equal (as defined by {@link Intent#filterEquals}) to the
4221 * PendingIntent passed to
4222 * {@link #requestNetwork(NetworkRequest, android.app.PendingIntent)} with the
4223 * corresponding NetworkRequest you'd like to remove. Cannot be null.
4224 */
Chalard Jean158702d2019-01-07 19:26:34 +09004225 public void releaseNetworkRequest(@NonNull PendingIntent operation) {
Soi, Yoshinarib8102ef2015-11-12 12:09:02 +09004226 printStackTrace();
Hugo Benichibc1104b2017-05-09 15:19:01 +09004227 checkPendingIntentNotNull(operation);
Jeremy Joslin60d379b2014-11-05 10:32:09 -08004228 try {
4229 mService.releasePendingNetworkRequest(operation);
Jeff Sharkeyc78f85c2016-03-01 19:27:23 -07004230 } catch (RemoteException e) {
4231 throw e.rethrowFromSystemServer();
4232 }
Jeremy Joslin60d379b2014-11-05 10:32:09 -08004233 }
4234
Hugo Benichibc1104b2017-05-09 15:19:01 +09004235 private static void checkPendingIntentNotNull(PendingIntent intent) {
Remi NGUYEN VAN81f68422021-03-15 07:31:54 +00004236 Objects.requireNonNull(intent, "PendingIntent cannot be null.");
Hugo Benichibc1104b2017-05-09 15:19:01 +09004237 }
4238
4239 private static void checkCallbackNotNull(NetworkCallback callback) {
Remi NGUYEN VAN81f68422021-03-15 07:31:54 +00004240 Objects.requireNonNull(callback, "null NetworkCallback");
Hugo Benichibc1104b2017-05-09 15:19:01 +09004241 }
4242
4243 private static void checkTimeout(int timeoutMs) {
Remi NGUYEN VAN81f68422021-03-15 07:31:54 +00004244 if (timeoutMs <= 0) {
4245 throw new IllegalArgumentException("timeoutMs must be strictly positive.");
4246 }
Jeremy Joslin60d379b2014-11-05 10:32:09 -08004247 }
4248
4249 /**
Robert Greenwaltf99b8392014-03-26 16:47:06 -07004250 * Registers to receive notifications about all networks which satisfy the given
Robert Greenwaltf57c03c2014-06-11 16:05:07 -07004251 * {@link NetworkRequest}. The callbacks will continue to be called until
Chiachang Wang3b723c22019-02-27 17:14:50 +08004252 * either the application exits or {@link #unregisterNetworkCallback(NetworkCallback)} is
4253 * called.
Robert Greenwaltf99b8392014-03-26 16:47:06 -07004254 *
Chalard Jeana5ff1132020-05-20 16:11:50 +09004255 * <p>To avoid performance issues due to apps leaking callbacks, the system will limit the
4256 * number of outstanding requests to 100 per app (identified by their UID), shared with
4257 * all variants of this method, of {@link #requestNetwork} as well as
4258 * {@link ConnectivityDiagnosticsManager#registerConnectivityDiagnosticsCallback}.
4259 * Requesting a network with this method will count toward this limit. If this limit is
4260 * exceeded, an exception will be thrown. To avoid hitting this issue and to conserve resources,
4261 * make sure to unregister the callbacks with
4262 * {@link #unregisterNetworkCallback(NetworkCallback)}.
4263 *
Robert Greenwaltf57c03c2014-06-11 16:05:07 -07004264 * @param request {@link NetworkRequest} describing this request.
4265 * @param networkCallback The {@link NetworkCallback} that the system will call as suitable
4266 * networks change state.
Hugo Benichifd44e912017-02-02 17:02:36 +09004267 * The callback is invoked on the default internal Handler.
Chalard Jeana5ff1132020-05-20 16:11:50 +09004268 * @throws RuntimeException if the app already has too many callbacks registered.
Robert Greenwaltf99b8392014-03-26 16:47:06 -07004269 */
Jeff Sharkey656584a2017-04-24 11:18:03 -06004270 @RequiresPermission(android.Manifest.permission.ACCESS_NETWORK_STATE)
Chalard Jean158702d2019-01-07 19:26:34 +09004271 public void registerNetworkCallback(@NonNull NetworkRequest request,
4272 @NonNull NetworkCallback networkCallback) {
Hugo Benichifd44e912017-02-02 17:02:36 +09004273 registerNetworkCallback(request, networkCallback, getDefaultHandler());
4274 }
4275
4276 /**
4277 * Registers to receive notifications about all networks which satisfy the given
4278 * {@link NetworkRequest}. The callbacks will continue to be called until
Chiachang Wang3b723c22019-02-27 17:14:50 +08004279 * either the application exits or {@link #unregisterNetworkCallback(NetworkCallback)} is
4280 * called.
Hugo Benichifd44e912017-02-02 17:02:36 +09004281 *
Chalard Jeana5ff1132020-05-20 16:11:50 +09004282 * <p>To avoid performance issues due to apps leaking callbacks, the system will limit the
4283 * number of outstanding requests to 100 per app (identified by their UID), shared with
4284 * all variants of this method, of {@link #requestNetwork} as well as
4285 * {@link ConnectivityDiagnosticsManager#registerConnectivityDiagnosticsCallback}.
4286 * Requesting a network with this method will count toward this limit. If this limit is
4287 * exceeded, an exception will be thrown. To avoid hitting this issue and to conserve resources,
4288 * make sure to unregister the callbacks with
4289 * {@link #unregisterNetworkCallback(NetworkCallback)}.
4290 *
4291 *
Hugo Benichifd44e912017-02-02 17:02:36 +09004292 * @param request {@link NetworkRequest} describing this request.
4293 * @param networkCallback The {@link NetworkCallback} that the system will call as suitable
4294 * networks change state.
4295 * @param handler {@link Handler} to specify the thread upon which the callback will be invoked.
Chalard Jeana5ff1132020-05-20 16:11:50 +09004296 * @throws RuntimeException if the app already has too many callbacks registered.
Hugo Benichifd44e912017-02-02 17:02:36 +09004297 */
Jeff Sharkey656584a2017-04-24 11:18:03 -06004298 @RequiresPermission(android.Manifest.permission.ACCESS_NETWORK_STATE)
Chalard Jean158702d2019-01-07 19:26:34 +09004299 public void registerNetworkCallback(@NonNull NetworkRequest request,
4300 @NonNull NetworkCallback networkCallback, @NonNull Handler handler) {
Hugo Benichifd44e912017-02-02 17:02:36 +09004301 CallbackHandler cbHandler = new CallbackHandler(handler);
4302 NetworkCapabilities nc = request.networkCapabilities;
4303 sendRequestForNetwork(nc, networkCallback, 0, LISTEN, TYPE_NONE, cbHandler);
Robert Greenwaltf99b8392014-03-26 16:47:06 -07004304 }
4305
4306 /**
Paul Jensenc8873fc2015-06-17 14:15:39 -04004307 * Registers a PendingIntent to be sent when a network is available which satisfies the given
4308 * {@link NetworkRequest}.
4309 *
4310 * This function behaves identically to the version that takes a NetworkCallback, but instead
4311 * of {@link NetworkCallback} a {@link PendingIntent} is used. This means
4312 * the request may outlive the calling application and get called back when a suitable
4313 * network is found.
4314 * <p>
4315 * The operation is an Intent broadcast that goes to a broadcast receiver that
4316 * you registered with {@link Context#registerReceiver} or through the
4317 * &lt;receiver&gt; tag in an AndroidManifest.xml file
4318 * <p>
4319 * The operation Intent is delivered with two extras, a {@link Network} typed
4320 * extra called {@link #EXTRA_NETWORK} and a {@link NetworkRequest}
4321 * typed extra called {@link #EXTRA_NETWORK_REQUEST} containing
4322 * the original requests parameters.
4323 * <p>
4324 * If there is already a request for this Intent registered (with the equality of
4325 * two Intents defined by {@link Intent#filterEquals}), then it will be removed and
4326 * replaced by this one, effectively releasing the previous {@link NetworkRequest}.
4327 * <p>
4328 * The request may be released normally by calling
Paul Jensen169f6622015-06-30 14:29:18 -04004329 * {@link #unregisterNetworkCallback(android.app.PendingIntent)}.
Chalard Jeana5ff1132020-05-20 16:11:50 +09004330 *
4331 * <p>To avoid performance issues due to apps leaking callbacks, the system will limit the
4332 * number of outstanding requests to 100 per app (identified by their UID), shared with
4333 * all variants of this method, of {@link #requestNetwork} as well as
4334 * {@link ConnectivityDiagnosticsManager#registerConnectivityDiagnosticsCallback}.
4335 * Requesting a network with this method will count toward this limit. If this limit is
4336 * exceeded, an exception will be thrown. To avoid hitting this issue and to conserve resources,
4337 * make sure to unregister the callbacks with {@link #unregisterNetworkCallback(PendingIntent)}
4338 * or {@link #releaseNetworkRequest(PendingIntent)}.
4339 *
Paul Jensenc8873fc2015-06-17 14:15:39 -04004340 * @param request {@link NetworkRequest} describing this request.
4341 * @param operation Action to perform when the network is available (corresponds
4342 * to the {@link NetworkCallback#onAvailable} call. Typically
4343 * comes from {@link PendingIntent#getBroadcast}. Cannot be null.
Chalard Jeana5ff1132020-05-20 16:11:50 +09004344 * @throws RuntimeException if the app already has too many callbacks registered.
Paul Jensenc8873fc2015-06-17 14:15:39 -04004345 */
Jeff Sharkey656584a2017-04-24 11:18:03 -06004346 @RequiresPermission(android.Manifest.permission.ACCESS_NETWORK_STATE)
Chalard Jean158702d2019-01-07 19:26:34 +09004347 public void registerNetworkCallback(@NonNull NetworkRequest request,
4348 @NonNull PendingIntent operation) {
Soi, Yoshinarib8102ef2015-11-12 12:09:02 +09004349 printStackTrace();
Hugo Benichibc1104b2017-05-09 15:19:01 +09004350 checkPendingIntentNotNull(operation);
Paul Jensenc8873fc2015-06-17 14:15:39 -04004351 try {
Roshan Piusbc7e37d2020-01-16 12:17:17 -08004352 mService.pendingListenForNetwork(
Roshan Piusaa24fde2020-12-17 14:53:09 -08004353 request.networkCapabilities, operation, mContext.getOpPackageName(),
4354 getAttributionTag());
Jeff Sharkeyc78f85c2016-03-01 19:27:23 -07004355 } catch (RemoteException e) {
4356 throw e.rethrowFromSystemServer();
Hugo Benichia590ab82017-05-11 13:16:17 +09004357 } catch (ServiceSpecificException e) {
4358 throw convertServiceException(e);
Jeff Sharkeyc78f85c2016-03-01 19:27:23 -07004359 }
Paul Jensenc8873fc2015-06-17 14:15:39 -04004360 }
4361
4362 /**
Lorenzo Colitti76b639e2021-01-29 20:14:04 +09004363 * Registers to receive notifications about changes in the application's default network. This
4364 * may be a physical network or a virtual network, such as a VPN that applies to the
4365 * application. The callbacks will continue to be called until either the application exits or
Lorenzo Colitti15874cd2016-04-13 22:00:02 +09004366 * {@link #unregisterNetworkCallback(NetworkCallback)} is called.
Erik Kline23bf99c2016-03-16 15:31:39 +09004367 *
Chalard Jeana5ff1132020-05-20 16:11:50 +09004368 * <p>To avoid performance issues due to apps leaking callbacks, the system will limit the
4369 * number of outstanding requests to 100 per app (identified by their UID), shared with
4370 * all variants of this method, of {@link #requestNetwork} as well as
4371 * {@link ConnectivityDiagnosticsManager#registerConnectivityDiagnosticsCallback}.
4372 * Requesting a network with this method will count toward this limit. If this limit is
4373 * exceeded, an exception will be thrown. To avoid hitting this issue and to conserve resources,
4374 * make sure to unregister the callbacks with
4375 * {@link #unregisterNetworkCallback(NetworkCallback)}.
4376 *
Erik Kline23bf99c2016-03-16 15:31:39 +09004377 * @param networkCallback The {@link NetworkCallback} that the system will call as the
Lorenzo Colitti76b639e2021-01-29 20:14:04 +09004378 * application's default network changes.
Hugo Benichifd44e912017-02-02 17:02:36 +09004379 * The callback is invoked on the default internal Handler.
Chalard Jeana5ff1132020-05-20 16:11:50 +09004380 * @throws RuntimeException if the app already has too many callbacks registered.
Erik Kline23bf99c2016-03-16 15:31:39 +09004381 */
Jeff Sharkey656584a2017-04-24 11:18:03 -06004382 @RequiresPermission(android.Manifest.permission.ACCESS_NETWORK_STATE)
Chalard Jean158702d2019-01-07 19:26:34 +09004383 public void registerDefaultNetworkCallback(@NonNull NetworkCallback networkCallback) {
Hugo Benichifd44e912017-02-02 17:02:36 +09004384 registerDefaultNetworkCallback(networkCallback, getDefaultHandler());
4385 }
4386
4387 /**
Lorenzo Colitti76b639e2021-01-29 20:14:04 +09004388 * Registers to receive notifications about changes in the application's default network. This
4389 * may be a physical network or a virtual network, such as a VPN that applies to the
4390 * application. The callbacks will continue to be called until either the application exits or
4391 * {@link #unregisterNetworkCallback(NetworkCallback)} is called.
4392 *
4393 * <p>To avoid performance issues due to apps leaking callbacks, the system will limit the
4394 * number of outstanding requests to 100 per app (identified by their UID), shared with
4395 * all variants of this method, of {@link #requestNetwork} as well as
4396 * {@link ConnectivityDiagnosticsManager#registerConnectivityDiagnosticsCallback}.
4397 * Requesting a network with this method will count toward this limit. If this limit is
4398 * exceeded, an exception will be thrown. To avoid hitting this issue and to conserve resources,
4399 * make sure to unregister the callbacks with
4400 * {@link #unregisterNetworkCallback(NetworkCallback)}.
4401 *
4402 * @param networkCallback The {@link NetworkCallback} that the system will call as the
4403 * application's default network changes.
4404 * @param handler {@link Handler} to specify the thread upon which the callback will be invoked.
4405 * @throws RuntimeException if the app already has too many callbacks registered.
4406 */
4407 @RequiresPermission(android.Manifest.permission.ACCESS_NETWORK_STATE)
4408 public void registerDefaultNetworkCallback(@NonNull NetworkCallback networkCallback,
4409 @NonNull Handler handler) {
Lorenzo Colitti2a49c5d2021-03-12 22:48:07 +09004410 registerDefaultNetworkCallbackAsUid(Process.INVALID_UID, networkCallback, handler);
4411 }
4412
4413 /**
4414 * Registers to receive notifications about changes in the default network for the specified
4415 * UID. This may be a physical network or a virtual network, such as a VPN that applies to the
4416 * UID. The callbacks will continue to be called until either the application exits or
4417 * {@link #unregisterNetworkCallback(NetworkCallback)} is called.
4418 *
4419 * <p>To avoid performance issues due to apps leaking callbacks, the system will limit the
4420 * number of outstanding requests to 100 per app (identified by their UID), shared with
4421 * all variants of this method, of {@link #requestNetwork} as well as
4422 * {@link ConnectivityDiagnosticsManager#registerConnectivityDiagnosticsCallback}.
4423 * Requesting a network with this method will count toward this limit. If this limit is
4424 * exceeded, an exception will be thrown. To avoid hitting this issue and to conserve resources,
4425 * make sure to unregister the callbacks with
4426 * {@link #unregisterNetworkCallback(NetworkCallback)}.
4427 *
4428 * @param uid the UID for which to track default network changes.
4429 * @param networkCallback The {@link NetworkCallback} that the system will call as the
4430 * UID's default network changes.
4431 * @param handler {@link Handler} to specify the thread upon which the callback will be invoked.
4432 * @throws RuntimeException if the app already has too many callbacks registered.
4433 * @hide
4434 */
Lorenzo Colitti92ed8b92021-03-22 18:23:21 +09004435 @SystemApi(client = MODULE_LIBRARIES)
Lorenzo Colitti2a49c5d2021-03-12 22:48:07 +09004436 @SuppressLint({"ExecutorRegistration", "PairedRegistration"})
4437 @RequiresPermission(anyOf = {
4438 NetworkStack.PERMISSION_MAINLINE_NETWORK_STACK,
4439 android.Manifest.permission.NETWORK_SETTINGS})
4440 public void registerDefaultNetworkCallbackAsUid(int uid,
4441 @NonNull NetworkCallback networkCallback, @NonNull Handler handler) {
Lorenzo Colitti76b639e2021-01-29 20:14:04 +09004442 CallbackHandler cbHandler = new CallbackHandler(handler);
Lorenzo Colitti2a49c5d2021-03-12 22:48:07 +09004443 sendRequestForNetwork(uid, null /* need */, networkCallback, 0 /* timeoutMs */,
Lorenzo Colitti76b639e2021-01-29 20:14:04 +09004444 TRACK_DEFAULT, TYPE_NONE, cbHandler);
4445 }
4446
4447 /**
Hugo Benichifd44e912017-02-02 17:02:36 +09004448 * Registers to receive notifications about changes in the system default network. The callbacks
4449 * will continue to be called until either the application exits or
4450 * {@link #unregisterNetworkCallback(NetworkCallback)} is called.
Hugo Benichifd44e912017-02-02 17:02:36 +09004451 *
Lorenzo Colitti76b639e2021-01-29 20:14:04 +09004452 * This method should not be used to determine networking state seen by applications, because in
4453 * many cases, most or even all application traffic may not use the default network directly,
4454 * and traffic from different applications may go on different networks by default. As an
4455 * example, if a VPN is connected, traffic from all applications might be sent through the VPN
4456 * and not onto the system default network. Applications or system components desiring to do
4457 * determine network state as seen by applications should use other methods such as
4458 * {@link #registerDefaultNetworkCallback(NetworkCallback, Handler)}.
4459 *
Chalard Jeana5ff1132020-05-20 16:11:50 +09004460 * <p>To avoid performance issues due to apps leaking callbacks, the system will limit the
4461 * number of outstanding requests to 100 per app (identified by their UID), shared with
4462 * all variants of this method, of {@link #requestNetwork} as well as
4463 * {@link ConnectivityDiagnosticsManager#registerConnectivityDiagnosticsCallback}.
4464 * Requesting a network with this method will count toward this limit. If this limit is
4465 * exceeded, an exception will be thrown. To avoid hitting this issue and to conserve resources,
4466 * make sure to unregister the callbacks with
4467 * {@link #unregisterNetworkCallback(NetworkCallback)}.
4468 *
Hugo Benichifd44e912017-02-02 17:02:36 +09004469 * @param networkCallback The {@link NetworkCallback} that the system will call as the
4470 * system default network changes.
4471 * @param handler {@link Handler} to specify the thread upon which the callback will be invoked.
Chalard Jeana5ff1132020-05-20 16:11:50 +09004472 * @throws RuntimeException if the app already has too many callbacks registered.
Lorenzo Colitti76b639e2021-01-29 20:14:04 +09004473 *
4474 * @hide
Hugo Benichifd44e912017-02-02 17:02:36 +09004475 */
Lorenzo Colitti76b639e2021-01-29 20:14:04 +09004476 @SystemApi(client = MODULE_LIBRARIES)
4477 @SuppressLint({"ExecutorRegistration", "PairedRegistration"})
4478 @RequiresPermission(anyOf = {
4479 NetworkStack.PERMISSION_MAINLINE_NETWORK_STACK,
4480 android.Manifest.permission.NETWORK_SETTINGS})
4481 public void registerSystemDefaultNetworkCallback(@NonNull NetworkCallback networkCallback,
Chalard Jean158702d2019-01-07 19:26:34 +09004482 @NonNull Handler handler) {
Hugo Benichifd44e912017-02-02 17:02:36 +09004483 CallbackHandler cbHandler = new CallbackHandler(handler);
Chalard Jean9dd11612018-06-04 16:52:49 +09004484 sendRequestForNetwork(null /* NetworkCapabilities need */, networkCallback, 0,
Lorenzo Colitti76b639e2021-01-29 20:14:04 +09004485 TRACK_SYSTEM_DEFAULT, TYPE_NONE, cbHandler);
Erik Kline23bf99c2016-03-16 15:31:39 +09004486 }
4487
4488 /**
junyulai8eb13a22021-03-15 11:48:48 +08004489 * Registers to receive notifications about the best matching network which satisfy the given
4490 * {@link NetworkRequest}. The callbacks will continue to be called until
4491 * either the application exits or {@link #unregisterNetworkCallback(NetworkCallback)} is
4492 * called.
4493 *
4494 * <p>To avoid performance issues due to apps leaking callbacks, the system will limit the
4495 * number of outstanding requests to 100 per app (identified by their UID), shared with
4496 * {@link #registerNetworkCallback} and its variants and {@link #requestNetwork} as well as
4497 * {@link ConnectivityDiagnosticsManager#registerConnectivityDiagnosticsCallback}.
4498 * Requesting a network with this method will count toward this limit. If this limit is
4499 * exceeded, an exception will be thrown. To avoid hitting this issue and to conserve resources,
4500 * make sure to unregister the callbacks with
4501 * {@link #unregisterNetworkCallback(NetworkCallback)}.
4502 *
4503 *
4504 * @param request {@link NetworkRequest} describing this request.
4505 * @param networkCallback The {@link NetworkCallback} that the system will call as suitable
4506 * networks change state.
4507 * @param handler {@link Handler} to specify the thread upon which the callback will be invoked.
4508 * @throws RuntimeException if the app already has too many callbacks registered.
junyulai7514e312021-03-05 15:51:17 +08004509 */
junyulai7514e312021-03-05 15:51:17 +08004510 @SuppressLint("ExecutorRegistration")
4511 public void registerBestMatchingNetworkCallback(@NonNull NetworkRequest request,
4512 @NonNull NetworkCallback networkCallback, @NonNull Handler handler) {
4513 final NetworkCapabilities nc = request.networkCapabilities;
4514 final CallbackHandler cbHandler = new CallbackHandler(handler);
junyulai1b1c8742021-03-12 20:05:08 +08004515 sendRequestForNetwork(nc, networkCallback, 0, LISTEN_FOR_BEST, TYPE_NONE, cbHandler);
junyulai7514e312021-03-05 15:51:17 +08004516 }
4517
4518 /**
fenglub00f4882015-04-21 17:12:05 -07004519 * Requests bandwidth update for a given {@link Network} and returns whether the update request
4520 * is accepted by ConnectivityService. Once accepted, ConnectivityService will poll underlying
4521 * network connection for updated bandwidth information. The caller will be notified via
4522 * {@link ConnectivityManager.NetworkCallback} if there is an update. Notice that this
Lorenzo Colitti15874cd2016-04-13 22:00:02 +09004523 * method assumes that the caller has previously called
4524 * {@link #registerNetworkCallback(NetworkRequest, NetworkCallback)} to listen for network
4525 * changes.
fenglu3f357402015-03-20 11:29:56 -07004526 *
fengluea2d9282015-04-27 14:28:04 -07004527 * @param network {@link Network} specifying which network you're interested.
fenglub00f4882015-04-21 17:12:05 -07004528 * @return {@code true} on success, {@code false} if the {@link Network} is no longer valid.
fenglu3f357402015-03-20 11:29:56 -07004529 */
Chalard Jean158702d2019-01-07 19:26:34 +09004530 public boolean requestBandwidthUpdate(@NonNull Network network) {
fenglu3f357402015-03-20 11:29:56 -07004531 try {
fenglub00f4882015-04-21 17:12:05 -07004532 return mService.requestBandwidthUpdate(network);
fenglu3f357402015-03-20 11:29:56 -07004533 } catch (RemoteException e) {
Jeff Sharkeyc78f85c2016-03-01 19:27:23 -07004534 throw e.rethrowFromSystemServer();
fenglu3f357402015-03-20 11:29:56 -07004535 }
4536 }
4537
4538 /**
Hugo Benichi2aa65af2017-03-06 09:17:06 +09004539 * Unregisters a {@code NetworkCallback} and possibly releases networks originating from
Lorenzo Colitti15874cd2016-04-13 22:00:02 +09004540 * {@link #requestNetwork(NetworkRequest, NetworkCallback)} and
4541 * {@link #registerNetworkCallback(NetworkRequest, NetworkCallback)} calls.
4542 * If the given {@code NetworkCallback} had previously been used with
Lorenzo Colitti7f6a2bf2015-04-24 17:03:31 +09004543 * {@code #requestNetwork}, any networks that had been connected to only to satisfy that request
4544 * will be disconnected.
Robert Greenwaltf99b8392014-03-26 16:47:06 -07004545 *
Hugo Benichi2aa65af2017-03-06 09:17:06 +09004546 * Notifications that would have triggered that {@code NetworkCallback} will immediately stop
4547 * triggering it as soon as this call returns.
4548 *
Robert Greenwaltf57c03c2014-06-11 16:05:07 -07004549 * @param networkCallback The {@link NetworkCallback} used when making the request.
Robert Greenwaltf99b8392014-03-26 16:47:06 -07004550 */
Chalard Jean158702d2019-01-07 19:26:34 +09004551 public void unregisterNetworkCallback(@NonNull NetworkCallback networkCallback) {
Soi, Yoshinarib8102ef2015-11-12 12:09:02 +09004552 printStackTrace();
Hugo Benichibc1104b2017-05-09 15:19:01 +09004553 checkCallbackNotNull(networkCallback);
Hugo Benichi2aa65af2017-03-06 09:17:06 +09004554 final List<NetworkRequest> reqs = new ArrayList<>();
4555 // Find all requests associated to this callback and stop callback triggers immediately.
4556 // Callback is reusable immediately. http://b/20701525, http://b/35921499.
4557 synchronized (sCallbacks) {
Remi NGUYEN VAN81f68422021-03-15 07:31:54 +00004558 if (networkCallback.networkRequest == null) {
4559 throw new IllegalArgumentException("NetworkCallback was not registered");
4560 }
Etan Cohen6cb65992019-04-16 15:07:55 -07004561 if (networkCallback.networkRequest == ALREADY_UNREGISTERED) {
4562 Log.d(TAG, "NetworkCallback was already unregistered");
4563 return;
4564 }
Hugo Benichi2aa65af2017-03-06 09:17:06 +09004565 for (Map.Entry<NetworkRequest, NetworkCallback> e : sCallbacks.entrySet()) {
4566 if (e.getValue() == networkCallback) {
4567 reqs.add(e.getKey());
4568 }
4569 }
4570 // TODO: throw exception if callback was registered more than once (http://b/20701525).
4571 for (NetworkRequest r : reqs) {
4572 try {
4573 mService.releaseNetworkRequest(r);
4574 } catch (RemoteException e) {
4575 throw e.rethrowFromSystemServer();
4576 }
4577 // Only remove mapping if rpc was successful.
4578 sCallbacks.remove(r);
4579 }
Hugo Benichibee30fe2017-06-17 13:14:12 +09004580 networkCallback.networkRequest = ALREADY_UNREGISTERED;
Jeff Sharkeyc78f85c2016-03-01 19:27:23 -07004581 }
Robert Greenwaltf99b8392014-03-26 16:47:06 -07004582 }
Paul Jensen8cdda642014-05-29 10:12:39 -04004583
4584 /**
Paul Jensen169f6622015-06-30 14:29:18 -04004585 * Unregisters a callback previously registered via
4586 * {@link #registerNetworkCallback(NetworkRequest, android.app.PendingIntent)}.
4587 *
4588 * @param operation A PendingIntent equal (as defined by {@link Intent#filterEquals}) to the
4589 * PendingIntent passed to
4590 * {@link #registerNetworkCallback(NetworkRequest, android.app.PendingIntent)}.
4591 * Cannot be null.
4592 */
Chalard Jean158702d2019-01-07 19:26:34 +09004593 public void unregisterNetworkCallback(@NonNull PendingIntent operation) {
Paul Jensen169f6622015-06-30 14:29:18 -04004594 releaseNetworkRequest(operation);
4595 }
4596
4597 /**
Lorenzo Colitti6947c062015-04-03 16:38:52 +09004598 * Informs the system whether it should switch to {@code network} regardless of whether it is
4599 * validated or not. If {@code accept} is true, and the network was explicitly selected by the
4600 * user (e.g., by selecting a Wi-Fi network in the Settings app), then the network will become
4601 * the system default network regardless of any other network that's currently connected. If
4602 * {@code always} is true, then the choice is remembered, so that the next time the user
4603 * connects to this network, the system will switch to it.
4604 *
Lorenzo Colitti6947c062015-04-03 16:38:52 +09004605 * @param network The network to accept.
4606 * @param accept Whether to accept the network even if unvalidated.
4607 * @param always Whether to remember this choice in the future.
4608 *
4609 * @hide
4610 */
Chiachang Wang4d513572021-03-18 09:44:34 +08004611 @SystemApi(client = MODULE_LIBRARIES)
4612 @RequiresPermission(anyOf = {
4613 android.Manifest.permission.NETWORK_SETTINGS,
4614 android.Manifest.permission.NETWORK_SETUP_WIZARD,
4615 android.Manifest.permission.NETWORK_STACK,
4616 NetworkStack.PERMISSION_MAINLINE_NETWORK_STACK})
4617 public void setAcceptUnvalidated(@NonNull Network network, boolean accept, boolean always) {
Lorenzo Colitti6947c062015-04-03 16:38:52 +09004618 try {
4619 mService.setAcceptUnvalidated(network, accept, always);
Jeff Sharkeyc78f85c2016-03-01 19:27:23 -07004620 } catch (RemoteException e) {
4621 throw e.rethrowFromSystemServer();
4622 }
Lorenzo Colitti6947c062015-04-03 16:38:52 +09004623 }
4624
4625 /**
lucaslin2240ef62019-03-12 13:08:03 +08004626 * Informs the system whether it should consider the network as validated even if it only has
4627 * partial connectivity. If {@code accept} is true, then the network will be considered as
4628 * validated even if connectivity is only partial. If {@code always} is true, then the choice
4629 * is remembered, so that the next time the user connects to this network, the system will
4630 * switch to it.
4631 *
4632 * @param network The network to accept.
4633 * @param accept Whether to consider the network as validated even if it has partial
4634 * connectivity.
4635 * @param always Whether to remember this choice in the future.
4636 *
4637 * @hide
4638 */
Chiachang Wang4d513572021-03-18 09:44:34 +08004639 @SystemApi(client = MODULE_LIBRARIES)
4640 @RequiresPermission(anyOf = {
4641 android.Manifest.permission.NETWORK_SETTINGS,
4642 android.Manifest.permission.NETWORK_SETUP_WIZARD,
4643 android.Manifest.permission.NETWORK_STACK,
4644 NetworkStack.PERMISSION_MAINLINE_NETWORK_STACK})
4645 public void setAcceptPartialConnectivity(@NonNull Network network, boolean accept,
4646 boolean always) {
lucaslin2240ef62019-03-12 13:08:03 +08004647 try {
4648 mService.setAcceptPartialConnectivity(network, accept, always);
4649 } catch (RemoteException e) {
4650 throw e.rethrowFromSystemServer();
4651 }
4652 }
4653
4654 /**
Lorenzo Colittic65750c2016-09-19 01:00:19 +09004655 * Informs the system to penalize {@code network}'s score when it becomes unvalidated. This is
4656 * only meaningful if the system is configured not to penalize such networks, e.g., if the
4657 * {@code config_networkAvoidBadWifi} configuration variable is set to 0 and the {@code
4658 * NETWORK_AVOID_BAD_WIFI setting is unset}.
4659 *
Lorenzo Colittic65750c2016-09-19 01:00:19 +09004660 * @param network The network to accept.
4661 *
4662 * @hide
4663 */
Chiachang Wang4d513572021-03-18 09:44:34 +08004664 @SystemApi(client = MODULE_LIBRARIES)
4665 @RequiresPermission(anyOf = {
4666 android.Manifest.permission.NETWORK_SETTINGS,
4667 android.Manifest.permission.NETWORK_SETUP_WIZARD,
4668 android.Manifest.permission.NETWORK_STACK,
4669 NetworkStack.PERMISSION_MAINLINE_NETWORK_STACK})
4670 public void setAvoidUnvalidated(@NonNull Network network) {
Lorenzo Colittic65750c2016-09-19 01:00:19 +09004671 try {
4672 mService.setAvoidUnvalidated(network);
4673 } catch (RemoteException e) {
4674 throw e.rethrowFromSystemServer();
4675 }
4676 }
4677
4678 /**
Lorenzo Colitti500dbae2017-04-27 14:30:21 +09004679 * Requests that the system open the captive portal app on the specified network.
4680 *
Remi NGUYEN VAN4cf96ab2021-03-16 18:06:06 +09004681 * <p>This is to be used on networks where a captive portal was detected, as per
4682 * {@link NetworkCapabilities#NET_CAPABILITY_CAPTIVE_PORTAL}.
4683 *
Lorenzo Colitti500dbae2017-04-27 14:30:21 +09004684 * @param network The network to log into.
4685 *
4686 * @hide
4687 */
Remi NGUYEN VAN4cf96ab2021-03-16 18:06:06 +09004688 @SystemApi(client = SystemApi.Client.MODULE_LIBRARIES)
4689 @RequiresPermission(anyOf = {
4690 android.Manifest.permission.NETWORK_SETTINGS,
4691 android.Manifest.permission.NETWORK_STACK,
4692 NetworkStack.PERMISSION_MAINLINE_NETWORK_STACK
4693 })
4694 public void startCaptivePortalApp(@NonNull Network network) {
Lorenzo Colitti500dbae2017-04-27 14:30:21 +09004695 try {
4696 mService.startCaptivePortalApp(network);
4697 } catch (RemoteException e) {
4698 throw e.rethrowFromSystemServer();
4699 }
4700 }
4701
4702 /**
Remi NGUYEN VAN94ff95b2019-02-04 11:32:20 +09004703 * Requests that the system open the captive portal app with the specified extras.
4704 *
4705 * <p>This endpoint is exclusively for use by the NetworkStack and is protected by the
4706 * corresponding permission.
Remi NGUYEN VANeab0f542019-02-13 20:58:59 +09004707 * @param network Network on which the captive portal was detected.
Remi NGUYEN VAN94ff95b2019-02-04 11:32:20 +09004708 * @param appExtras Extras to include in the app start intent.
4709 * @hide
4710 */
4711 @SystemApi
Remi NGUYEN VAN94ff95b2019-02-04 11:32:20 +09004712 @RequiresPermission(NetworkStack.PERMISSION_MAINLINE_NETWORK_STACK)
paulhucbbc3db2019-03-08 16:35:20 +08004713 public void startCaptivePortalApp(@NonNull Network network, @NonNull Bundle appExtras) {
Remi NGUYEN VAN94ff95b2019-02-04 11:32:20 +09004714 try {
Remi NGUYEN VANeab0f542019-02-13 20:58:59 +09004715 mService.startCaptivePortalAppInternal(network, appExtras);
Remi NGUYEN VAN94ff95b2019-02-04 11:32:20 +09004716 } catch (RemoteException e) {
4717 throw e.rethrowFromSystemServer();
4718 }
4719 }
4720
4721 /**
Remi NGUYEN VAN27de63e2019-01-20 20:35:06 +09004722 * Determine whether the device is configured to avoid bad wifi.
4723 * @hide
4724 */
4725 @SystemApi
Remi NGUYEN VAN1fb7cab2019-03-22 11:14:13 +09004726 @RequiresPermission(anyOf = {
4727 NetworkStack.PERMISSION_MAINLINE_NETWORK_STACK,
4728 android.Manifest.permission.NETWORK_STACK})
4729 public boolean shouldAvoidBadWifi() {
Remi NGUYEN VAN27de63e2019-01-20 20:35:06 +09004730 try {
Remi NGUYEN VAN1fb7cab2019-03-22 11:14:13 +09004731 return mService.shouldAvoidBadWifi();
Remi NGUYEN VAN27de63e2019-01-20 20:35:06 +09004732 } catch (RemoteException e) {
4733 throw e.rethrowFromSystemServer();
4734 }
4735 }
4736
4737 /**
Lorenzo Colitti48a2a322017-01-24 18:08:41 +09004738 * It is acceptable to briefly use multipath data to provide seamless connectivity for
4739 * time-sensitive user-facing operations when the system default network is temporarily
Lorenzo Colitti139a48c2017-04-28 00:56:30 +09004740 * unresponsive. The amount of data should be limited (less than one megabyte for every call to
4741 * this method), and the operation should be infrequent to ensure that data usage is limited.
Lorenzo Colitti48a2a322017-01-24 18:08:41 +09004742 *
4743 * An example of such an operation might be a time-sensitive foreground activity, such as a
4744 * voice command, that the user is performing while walking out of range of a Wi-Fi network.
4745 */
4746 public static final int MULTIPATH_PREFERENCE_HANDOVER = 1 << 0;
4747
4748 /**
4749 * It is acceptable to use small amounts of multipath data on an ongoing basis to provide
4750 * a backup channel for traffic that is primarily going over another network.
4751 *
4752 * An example might be maintaining backup connections to peers or servers for the purpose of
4753 * fast fallback if the default network is temporarily unresponsive or disconnects. The traffic
4754 * on backup paths should be negligible compared to the traffic on the main path.
4755 */
4756 public static final int MULTIPATH_PREFERENCE_RELIABILITY = 1 << 1;
4757
4758 /**
4759 * It is acceptable to use metered data to improve network latency and performance.
4760 */
4761 public static final int MULTIPATH_PREFERENCE_PERFORMANCE = 1 << 2;
4762
4763 /**
4764 * Return value to use for unmetered networks. On such networks we currently set all the flags
4765 * to true.
4766 * @hide
4767 */
4768 public static final int MULTIPATH_PREFERENCE_UNMETERED =
4769 MULTIPATH_PREFERENCE_HANDOVER |
4770 MULTIPATH_PREFERENCE_RELIABILITY |
4771 MULTIPATH_PREFERENCE_PERFORMANCE;
4772
4773 /** @hide */
4774 @Retention(RetentionPolicy.SOURCE)
4775 @IntDef(flag = true, value = {
4776 MULTIPATH_PREFERENCE_HANDOVER,
4777 MULTIPATH_PREFERENCE_RELIABILITY,
4778 MULTIPATH_PREFERENCE_PERFORMANCE,
4779 })
4780 public @interface MultipathPreference {
4781 }
4782
4783 /**
4784 * Provides a hint to the calling application on whether it is desirable to use the
4785 * multinetwork APIs (e.g., {@link Network#openConnection}, {@link Network#bindSocket}, etc.)
4786 * for multipath data transfer on this network when it is not the system default network.
4787 * Applications desiring to use multipath network protocols should call this method before
4788 * each such operation.
Lorenzo Colitti48a2a322017-01-24 18:08:41 +09004789 *
4790 * @param network The network on which the application desires to use multipath data.
4791 * If {@code null}, this method will return the a preference that will generally
4792 * apply to metered networks.
4793 * @return a bitwise OR of zero or more of the {@code MULTIPATH_PREFERENCE_*} constants.
4794 */
Jeff Sharkey656584a2017-04-24 11:18:03 -06004795 @RequiresPermission(android.Manifest.permission.ACCESS_NETWORK_STATE)
Chalard Jean158702d2019-01-07 19:26:34 +09004796 public @MultipathPreference int getMultipathPreference(@Nullable Network network) {
Lorenzo Colitti48a2a322017-01-24 18:08:41 +09004797 try {
4798 return mService.getMultipathPreference(network);
4799 } catch (RemoteException e) {
4800 throw e.rethrowFromSystemServer();
4801 }
4802 }
4803
4804 /**
Stuart Scott0f835ac2015-03-30 13:17:11 -07004805 * Resets all connectivity manager settings back to factory defaults.
4806 * @hide
4807 */
Chiachang Wang4d513572021-03-18 09:44:34 +08004808 @SystemApi(client = MODULE_LIBRARIES)
4809 @RequiresPermission(anyOf = {
4810 android.Manifest.permission.NETWORK_SETTINGS,
4811 NetworkStack.PERMISSION_MAINLINE_NETWORK_STACK})
Stuart Scott0f835ac2015-03-30 13:17:11 -07004812 public void factoryReset() {
Stuart Scott0f835ac2015-03-30 13:17:11 -07004813 try {
Stuart Scottd5463642015-04-02 18:00:02 -07004814 mService.factoryReset();
Amos Bianchia9b415a2020-03-04 11:07:38 -08004815 mTetheringManager.stopAllTethering();
Stuart Scott0f835ac2015-03-30 13:17:11 -07004816 } catch (RemoteException e) {
Jeff Sharkeyc78f85c2016-03-01 19:27:23 -07004817 throw e.rethrowFromSystemServer();
Stuart Scott0f835ac2015-03-30 13:17:11 -07004818 }
4819 }
4820
4821 /**
Paul Jensen8cdda642014-05-29 10:12:39 -04004822 * Binds the current process to {@code network}. All Sockets created in the future
4823 * (and not explicitly bound via a bound SocketFactory from
4824 * {@link Network#getSocketFactory() Network.getSocketFactory()}) will be bound to
4825 * {@code network}. All host name resolutions will be limited to {@code network} as well.
4826 * Note that if {@code network} ever disconnects, all Sockets created in this way will cease to
4827 * work and all host name resolutions will fail. This is by design so an application doesn't
4828 * accidentally use Sockets it thinks are still bound to a particular {@link Network}.
4829 * To clear binding pass {@code null} for {@code network}. Using individually bound
4830 * Sockets created by Network.getSocketFactory().createSocket() and
4831 * performing network-specific host name resolutions via
4832 * {@link Network#getAllByName Network.getAllByName} is preferred to calling
Paul Jensenee2f45d2015-03-10 10:54:12 -04004833 * {@code bindProcessToNetwork}.
Paul Jensen8cdda642014-05-29 10:12:39 -04004834 *
4835 * @param network The {@link Network} to bind the current process to, or {@code null} to clear
4836 * the current binding.
4837 * @return {@code true} on success, {@code false} if the {@link Network} is no longer valid.
4838 */
Chalard Jean158702d2019-01-07 19:26:34 +09004839 public boolean bindProcessToNetwork(@Nullable Network network) {
Chalard Jean9dd11612018-06-04 16:52:49 +09004840 // Forcing callers to call through non-static function ensures ConnectivityManager
Paul Jensenee2f45d2015-03-10 10:54:12 -04004841 // instantiated.
4842 return setProcessDefaultNetwork(network);
4843 }
4844
4845 /**
4846 * Binds the current process to {@code network}. All Sockets created in the future
4847 * (and not explicitly bound via a bound SocketFactory from
4848 * {@link Network#getSocketFactory() Network.getSocketFactory()}) will be bound to
4849 * {@code network}. All host name resolutions will be limited to {@code network} as well.
4850 * Note that if {@code network} ever disconnects, all Sockets created in this way will cease to
4851 * work and all host name resolutions will fail. This is by design so an application doesn't
4852 * accidentally use Sockets it thinks are still bound to a particular {@link Network}.
4853 * To clear binding pass {@code null} for {@code network}. Using individually bound
4854 * Sockets created by Network.getSocketFactory().createSocket() and
4855 * performing network-specific host name resolutions via
4856 * {@link Network#getAllByName Network.getAllByName} is preferred to calling
4857 * {@code setProcessDefaultNetwork}.
4858 *
4859 * @param network The {@link Network} to bind the current process to, or {@code null} to clear
4860 * the current binding.
4861 * @return {@code true} on success, {@code false} if the {@link Network} is no longer valid.
4862 * @deprecated This function can throw {@link IllegalStateException}. Use
4863 * {@link #bindProcessToNetwork} instead. {@code bindProcessToNetwork}
4864 * is a direct replacement.
4865 */
Aurimas Liutikase80a0eb2016-05-24 15:22:55 -07004866 @Deprecated
Chalard Jean158702d2019-01-07 19:26:34 +09004867 public static boolean setProcessDefaultNetwork(@Nullable Network network) {
Paul Jensen3e2917c2014-08-27 12:38:45 -04004868 int netId = (network == null) ? NETID_UNSET : network.netId;
Lorenzo Colitti3c766eb2019-01-31 13:08:24 +09004869 boolean isSameNetId = (netId == NetworkUtils.getBoundNetworkForProcess());
4870
Lorenzo Colitti3a1cb9d2019-01-30 23:04:54 +09004871 if (netId != NETID_UNSET) {
4872 netId = network.getNetIdForResolv();
Paul Jensen3e2917c2014-08-27 12:38:45 -04004873 }
Lorenzo Colitti3c766eb2019-01-31 13:08:24 +09004874
4875 if (!NetworkUtils.bindProcessToNetwork(netId)) {
4876 return false;
4877 }
4878
4879 if (!isSameNetId) {
Paul Jensenc0618a62014-12-10 15:12:18 -05004880 // Set HTTP proxy system properties to match network.
4881 // TODO: Deprecate this static method and replace it with a non-static version.
Lorenzo Colitti41b1fbc2015-04-22 11:52:48 +09004882 try {
Remi NGUYEN VANb33335c2021-02-03 10:18:20 +09004883 Proxy.setHttpProxyConfiguration(getInstance().getDefaultProxy());
Lorenzo Colitti41b1fbc2015-04-22 11:52:48 +09004884 } catch (SecurityException e) {
4885 // The process doesn't have ACCESS_NETWORK_STATE, so we can't fetch the proxy.
4886 Log.e(TAG, "Can't set proxy properties", e);
4887 }
Paul Jensen3e2917c2014-08-27 12:38:45 -04004888 // Must flush DNS cache as new network may have different DNS resolutions.
Remi NGUYEN VANe1b04f62021-03-18 23:27:19 +09004889 InetAddressCompat.clearDnsCache();
Paul Jensen3e2917c2014-08-27 12:38:45 -04004890 // Must flush socket pool as idle sockets will be bound to previous network and may
4891 // cause subsequent fetches to be performed on old network.
4892 NetworkEventDispatcher.getInstance().onNetworkConfigurationChanged();
Paul Jensen3e2917c2014-08-27 12:38:45 -04004893 }
Lorenzo Colitti3c766eb2019-01-31 13:08:24 +09004894
4895 return true;
Paul Jensen8cdda642014-05-29 10:12:39 -04004896 }
4897
4898 /**
4899 * Returns the {@link Network} currently bound to this process via
Paul Jensenee2f45d2015-03-10 10:54:12 -04004900 * {@link #bindProcessToNetwork}, or {@code null} if no {@link Network} is explicitly bound.
Paul Jensen8cdda642014-05-29 10:12:39 -04004901 *
4902 * @return {@code Network} to which this process is bound, or {@code null}.
4903 */
Chalard Jean158702d2019-01-07 19:26:34 +09004904 @Nullable
Paul Jensenee2f45d2015-03-10 10:54:12 -04004905 public Network getBoundNetworkForProcess() {
4906 // Forcing callers to call thru non-static function ensures ConnectivityManager
4907 // instantiated.
4908 return getProcessDefaultNetwork();
4909 }
4910
4911 /**
4912 * Returns the {@link Network} currently bound to this process via
4913 * {@link #bindProcessToNetwork}, or {@code null} if no {@link Network} is explicitly bound.
4914 *
4915 * @return {@code Network} to which this process is bound, or {@code null}.
4916 * @deprecated Using this function can lead to other functions throwing
4917 * {@link IllegalStateException}. Use {@link #getBoundNetworkForProcess} instead.
4918 * {@code getBoundNetworkForProcess} is a direct replacement.
4919 */
Aurimas Liutikase80a0eb2016-05-24 15:22:55 -07004920 @Deprecated
Chalard Jean158702d2019-01-07 19:26:34 +09004921 @Nullable
Paul Jensen8cdda642014-05-29 10:12:39 -04004922 public static Network getProcessDefaultNetwork() {
Paul Jensenee2f45d2015-03-10 10:54:12 -04004923 int netId = NetworkUtils.getBoundNetworkForProcess();
Paul Jensen65743a22014-07-11 08:17:29 -04004924 if (netId == NETID_UNSET) return null;
Paul Jensen8cdda642014-05-29 10:12:39 -04004925 return new Network(netId);
4926 }
4927
Lorenzo Colitti2780e4b2015-07-29 11:41:21 +09004928 private void unsupportedStartingFrom(int version) {
4929 if (Process.myUid() == Process.SYSTEM_UID) {
Lorenzo Colitti23862912018-09-28 11:31:55 +09004930 // The getApplicationInfo() call we make below is not supported in system context. Let
4931 // the call through here, and rely on the fact that ConnectivityService will refuse to
4932 // allow the system to use these APIs anyway.
Lorenzo Colitti2780e4b2015-07-29 11:41:21 +09004933 return;
4934 }
4935
4936 if (mContext.getApplicationInfo().targetSdkVersion >= version) {
4937 throw new UnsupportedOperationException(
4938 "This method is not supported in target SDK version " + version + " and above");
4939 }
4940 }
4941
4942 // Checks whether the calling app can use the legacy routing API (startUsingNetworkFeature,
4943 // stopUsingNetworkFeature, requestRouteToHost), and if not throw UnsupportedOperationException.
Lifu Tang0a922fd2016-01-07 23:20:38 -08004944 // TODO: convert the existing system users (Tethering, GnssLocationProvider) to the new APIs and
Lorenzo Colitti2780e4b2015-07-29 11:41:21 +09004945 // remove these exemptions. Note that this check is not secure, and apps can still access these
4946 // functions by accessing ConnectivityService directly. However, it should be clear that doing
4947 // so is unsupported and may break in the future. http://b/22728205
4948 private void checkLegacyRoutingApiAccess() {
Dianne Hackborn18c1d832015-07-31 10:35:34 -07004949 unsupportedStartingFrom(VERSION_CODES.M);
Lorenzo Colitti2780e4b2015-07-29 11:41:21 +09004950 }
4951
Paul Jensen8cdda642014-05-29 10:12:39 -04004952 /**
4953 * Binds host resolutions performed by this process to {@code network}.
Paul Jensenee2f45d2015-03-10 10:54:12 -04004954 * {@link #bindProcessToNetwork} takes precedence over this setting.
Paul Jensen8cdda642014-05-29 10:12:39 -04004955 *
4956 * @param network The {@link Network} to bind host resolutions from the current process to, or
4957 * {@code null} to clear the current binding.
4958 * @return {@code true} on success, {@code false} if the {@link Network} is no longer valid.
4959 * @hide
4960 * @deprecated This is strictly for legacy usage to support {@link #startUsingNetworkFeature}.
4961 */
Aurimas Liutikase80a0eb2016-05-24 15:22:55 -07004962 @Deprecated
Mathew Inwoode1a17ba2020-11-04 09:29:36 +00004963 @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.R, trackingBug = 170729553)
Paul Jensen8cdda642014-05-29 10:12:39 -04004964 public static boolean setProcessDefaultNetworkForHostResolution(Network network) {
Paul Jensen65743a22014-07-11 08:17:29 -04004965 return NetworkUtils.bindProcessToNetworkForHostResolution(
Erik Kline767b7f22018-04-27 22:48:33 +09004966 (network == null) ? NETID_UNSET : network.getNetIdForResolv());
Paul Jensen8cdda642014-05-29 10:12:39 -04004967 }
Felipe Leme30511352016-01-22 09:44:57 -08004968
4969 /**
4970 * Device is not restricting metered network activity while application is running on
4971 * background.
4972 */
4973 public static final int RESTRICT_BACKGROUND_STATUS_DISABLED = 1;
4974
4975 /**
4976 * Device is restricting metered network activity while application is running on background,
4977 * but application is allowed to bypass it.
4978 * <p>
4979 * In this state, application should take action to mitigate metered network access.
4980 * For example, a music streaming application should switch to a low-bandwidth bitrate.
4981 */
4982 public static final int RESTRICT_BACKGROUND_STATUS_WHITELISTED = 2;
4983
4984 /**
4985 * Device is restricting metered network activity while application is running on background.
Felipe Lemed34c9af2016-01-27 14:46:39 -08004986 * <p>
Felipe Leme30511352016-01-22 09:44:57 -08004987 * In this state, application should not try to use the network while running on background,
4988 * because it would be denied.
4989 */
4990 public static final int RESTRICT_BACKGROUND_STATUS_ENABLED = 3;
4991
Felipe Lemed34c9af2016-01-27 14:46:39 -08004992 /**
4993 * A change in the background metered network activity restriction has occurred.
4994 * <p>
4995 * Applications should call {@link #getRestrictBackgroundStatus()} to check if the restriction
4996 * applies to them.
4997 * <p>
4998 * This is only sent to registered receivers, not manifest receivers.
4999 */
5000 @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION)
5001 public static final String ACTION_RESTRICT_BACKGROUND_CHANGED =
5002 "android.net.conn.RESTRICT_BACKGROUND_CHANGED";
5003
Felipe Leme7e4c1852016-01-25 11:48:04 -08005004 /** @hide */
5005 @Retention(RetentionPolicy.SOURCE)
Felipe Leme30511352016-01-22 09:44:57 -08005006 @IntDef(flag = false, value = {
5007 RESTRICT_BACKGROUND_STATUS_DISABLED,
5008 RESTRICT_BACKGROUND_STATUS_WHITELISTED,
5009 RESTRICT_BACKGROUND_STATUS_ENABLED,
5010 })
Felipe Leme30511352016-01-22 09:44:57 -08005011 public @interface RestrictBackgroundStatus {
5012 }
5013
Felipe Leme30511352016-01-22 09:44:57 -08005014 /**
5015 * Determines if the calling application is subject to metered network restrictions while
5016 * running on background.
Felipe Leme3edc6162016-05-16 13:57:19 -07005017 *
5018 * @return {@link #RESTRICT_BACKGROUND_STATUS_DISABLED},
5019 * {@link #RESTRICT_BACKGROUND_STATUS_ENABLED},
5020 * or {@link #RESTRICT_BACKGROUND_STATUS_WHITELISTED}
Felipe Leme30511352016-01-22 09:44:57 -08005021 */
5022 public @RestrictBackgroundStatus int getRestrictBackgroundStatus() {
5023 try {
Remi NGUYEN VANe2139a02021-03-18 14:23:12 +09005024 return mService.getRestrictBackgroundStatusByCaller();
Felipe Leme30511352016-01-22 09:44:57 -08005025 } catch (RemoteException e) {
Jeff Sharkeyc78f85c2016-03-01 19:27:23 -07005026 throw e.rethrowFromSystemServer();
Felipe Leme30511352016-01-22 09:44:57 -08005027 }
5028 }
Ricky Wai7097cc92018-01-23 04:09:45 +00005029
5030 /**
5031 * The network watchlist is a list of domains and IP addresses that are associated with
Ricky Wai04baf112018-03-20 14:20:54 +00005032 * potentially harmful apps. This method returns the SHA-256 of the watchlist config file
5033 * currently used by the system for validation purposes.
Ricky Wai7097cc92018-01-23 04:09:45 +00005034 *
5035 * @return Hash of network watchlist config file. Null if config does not exist.
5036 */
Chalard Jean158702d2019-01-07 19:26:34 +09005037 @Nullable
Ricky Wai7097cc92018-01-23 04:09:45 +00005038 public byte[] getNetworkWatchlistConfigHash() {
5039 try {
5040 return mService.getNetworkWatchlistConfigHash();
5041 } catch (RemoteException e) {
5042 Log.e(TAG, "Unable to get watchlist config hash");
5043 throw e.rethrowFromSystemServer();
5044 }
5045 }
Jeff Vander Stoep39a51e02018-07-23 10:57:53 -07005046
5047 /**
5048 * Returns the {@code uid} of the owner of a network connection.
5049 *
Benedict Wong0bd4bba2020-01-20 22:14:59 -08005050 * @param protocol The protocol of the connection. Only {@code IPPROTO_TCP} and {@code
5051 * IPPROTO_UDP} currently supported.
Jeff Vander Stoep39a51e02018-07-23 10:57:53 -07005052 * @param local The local {@link InetSocketAddress} of a connection.
5053 * @param remote The remote {@link InetSocketAddress} of a connection.
Jeff Vander Stoep39a51e02018-07-23 10:57:53 -07005054 * @return {@code uid} if the connection is found and the app has permission to observe it
Benedict Wong0bd4bba2020-01-20 22:14:59 -08005055 * (e.g., if it is associated with the calling VPN app's VpnService tunnel) or {@link
5056 * android.os.Process#INVALID_UID} if the connection is not found.
5057 * @throws {@link SecurityException} if the caller is not the active VpnService for the current
5058 * user.
5059 * @throws {@link IllegalArgumentException} if an unsupported protocol is requested.
Jeff Vander Stoep39a51e02018-07-23 10:57:53 -07005060 */
Benedict Wong0bd4bba2020-01-20 22:14:59 -08005061 public int getConnectionOwnerUid(
5062 int protocol, @NonNull InetSocketAddress local, @NonNull InetSocketAddress remote) {
Jeff Vander Stoep39a51e02018-07-23 10:57:53 -07005063 ConnectionInfo connectionInfo = new ConnectionInfo(protocol, local, remote);
5064 try {
5065 return mService.getConnectionOwnerUid(connectionInfo);
5066 } catch (RemoteException e) {
5067 throw e.rethrowFromSystemServer();
5068 }
5069 }
Soi, Yoshinarib8102ef2015-11-12 12:09:02 +09005070
5071 private void printStackTrace() {
5072 if (DEBUG) {
5073 final StackTraceElement[] callStack = Thread.currentThread().getStackTrace();
5074 final StringBuffer sb = new StringBuffer();
5075 for (int i = 3; i < callStack.length; i++) {
5076 final String stackTrace = callStack[i].toString();
5077 if (stackTrace == null || stackTrace.contains("android.os")) {
5078 break;
5079 }
5080 sb.append(" [").append(stackTrace).append("]");
5081 }
5082 Log.d(TAG, "StackLog:" + sb.toString());
5083 }
5084 }
Cody Kestingf53a0752020-04-15 12:33:28 -07005085
Remi NGUYEN VAN5f406422021-01-15 23:02:47 +09005086 /** @hide */
5087 public TestNetworkManager startOrGetTestNetworkManager() {
5088 final IBinder tnBinder;
5089 try {
5090 tnBinder = mService.startOrGetTestNetworkService();
5091 } catch (RemoteException e) {
5092 throw e.rethrowFromSystemServer();
5093 }
5094
5095 return new TestNetworkManager(ITestNetworkManager.Stub.asInterface(tnBinder));
5096 }
5097
Remi NGUYEN VAN5f406422021-01-15 23:02:47 +09005098 /** @hide */
5099 public ConnectivityDiagnosticsManager createDiagnosticsManager() {
5100 return new ConnectivityDiagnosticsManager(mContext, mService);
5101 }
5102
Cody Kestingf53a0752020-04-15 12:33:28 -07005103 /**
5104 * Simulates a Data Stall for the specified Network.
5105 *
Remi NGUYEN VAN761c7ad2021-01-12 18:40:04 +09005106 * <p>This method should only be used for tests.
5107 *
Remi NGUYEN VANd2d60c62021-04-08 16:26:20 +09005108 * <p>The caller must be the owner of the specified Network. This simulates a data stall to
5109 * have the system behave as if it had happened, but does not actually stall connectivity.
Cody Kestingf53a0752020-04-15 12:33:28 -07005110 *
5111 * @param detectionMethod The detection method used to identify the Data Stall.
Remi NGUYEN VANd2d60c62021-04-08 16:26:20 +09005112 * See ConnectivityDiagnosticsManager.DataStallReport.DETECTION_METHOD_*.
5113 * @param timestampMillis The timestamp at which the stall 'occurred', in milliseconds, as per
5114 * SystemClock.elapsedRealtime.
Cody Kestingf53a0752020-04-15 12:33:28 -07005115 * @param network The Network for which a Data Stall is being simluated.
5116 * @param extras The PersistableBundle of extras included in the Data Stall notification.
5117 * @throws SecurityException if the caller is not the owner of the given network.
5118 * @hide
5119 */
Remi NGUYEN VAN761c7ad2021-01-12 18:40:04 +09005120 @SystemApi(client = SystemApi.Client.MODULE_LIBRARIES)
Cody Kestingf53a0752020-04-15 12:33:28 -07005121 @RequiresPermission(anyOf = {android.Manifest.permission.MANAGE_TEST_NETWORKS,
5122 android.Manifest.permission.NETWORK_STACK})
Remi NGUYEN VANd2d60c62021-04-08 16:26:20 +09005123 public void simulateDataStall(@DetectionMethod int detectionMethod, long timestampMillis,
Cody Kestingf53a0752020-04-15 12:33:28 -07005124 @NonNull Network network, @NonNull PersistableBundle extras) {
5125 try {
5126 mService.simulateDataStall(detectionMethod, timestampMillis, network, extras);
5127 } catch (RemoteException e) {
5128 e.rethrowFromSystemServer();
5129 }
5130 }
James Mattis356a8792020-10-28 21:48:54 -07005131
Daniel Brightf9e945b2020-06-15 16:10:01 -07005132 @NonNull
5133 private final List<QosCallbackConnection> mQosCallbackConnections = new ArrayList<>();
5134
5135 /**
5136 * Registers a {@link QosSocketInfo} with an associated {@link QosCallback}. The callback will
5137 * receive available QoS events related to the {@link Network} and local ip + port
5138 * specified within socketInfo.
5139 * <p/>
5140 * The same {@link QosCallback} must be unregistered before being registered a second time,
5141 * otherwise {@link QosCallbackRegistrationException} is thrown.
5142 * <p/>
5143 * This API does not, in itself, require any permission if called with a network that is not
5144 * restricted. However, the underlying implementation currently only supports the IMS network,
5145 * which is always restricted. That means non-preinstalled callers can't possibly find this API
5146 * useful, because they'd never be called back on networks that they would have access to.
5147 *
5148 * @throws SecurityException if {@link QosSocketInfo#getNetwork()} is restricted and the app is
5149 * missing CONNECTIVITY_USE_RESTRICTED_NETWORKS permission.
5150 * @throws QosCallback.QosCallbackRegistrationException if qosCallback is already registered.
5151 * @throws RuntimeException if the app already has too many callbacks registered.
5152 *
5153 * Exceptions after the time of registration is passed through
5154 * {@link QosCallback#onError(QosCallbackException)}. see: {@link QosCallbackException}.
5155 *
5156 * @param socketInfo the socket information used to match QoS events
Daniel Brightf9e945b2020-06-15 16:10:01 -07005157 * @param executor The executor on which the callback will be invoked. The provided
5158 * {@link Executor} must run callback sequentially, otherwise the order of
Daniel Bright7ee5f522021-03-10 11:51:50 -08005159 * callbacks cannot be guaranteed.onQosCallbackRegistered
5160 * @param callback receives qos events that satisfy socketInfo
Daniel Brightf9e945b2020-06-15 16:10:01 -07005161 *
5162 * @hide
5163 */
5164 @SystemApi
5165 public void registerQosCallback(@NonNull final QosSocketInfo socketInfo,
Daniel Bright7ee5f522021-03-10 11:51:50 -08005166 @CallbackExecutor @NonNull final Executor executor,
5167 @NonNull final QosCallback callback) {
Daniel Brightf9e945b2020-06-15 16:10:01 -07005168 Objects.requireNonNull(socketInfo, "socketInfo must be non-null");
Daniel Brightf9e945b2020-06-15 16:10:01 -07005169 Objects.requireNonNull(executor, "executor must be non-null");
Daniel Bright7ee5f522021-03-10 11:51:50 -08005170 Objects.requireNonNull(callback, "callback must be non-null");
Daniel Brightf9e945b2020-06-15 16:10:01 -07005171
5172 try {
5173 synchronized (mQosCallbackConnections) {
5174 if (getQosCallbackConnection(callback) == null) {
5175 final QosCallbackConnection connection =
5176 new QosCallbackConnection(this, callback, executor);
5177 mQosCallbackConnections.add(connection);
5178 mService.registerQosSocketCallback(socketInfo, connection);
5179 } else {
5180 Log.e(TAG, "registerQosCallback: Callback already registered");
5181 throw new QosCallbackRegistrationException();
5182 }
5183 }
5184 } catch (final RemoteException e) {
5185 Log.e(TAG, "registerQosCallback: Error while registering ", e);
5186
5187 // The same unregister method method is called for consistency even though nothing
5188 // will be sent to the ConnectivityService since the callback was never successfully
5189 // registered.
5190 unregisterQosCallback(callback);
5191 e.rethrowFromSystemServer();
5192 } catch (final ServiceSpecificException e) {
5193 Log.e(TAG, "registerQosCallback: Error while registering ", e);
5194 unregisterQosCallback(callback);
5195 throw convertServiceException(e);
5196 }
5197 }
5198
5199 /**
5200 * Unregisters the given {@link QosCallback}. The {@link QosCallback} will no longer receive
5201 * events once unregistered and can be registered a second time.
5202 * <p/>
5203 * If the {@link QosCallback} does not have an active registration, it is a no-op.
5204 *
5205 * @param callback the callback being unregistered
5206 *
5207 * @hide
5208 */
5209 @SystemApi
5210 public void unregisterQosCallback(@NonNull final QosCallback callback) {
5211 Objects.requireNonNull(callback, "The callback must be non-null");
5212 try {
5213 synchronized (mQosCallbackConnections) {
5214 final QosCallbackConnection connection = getQosCallbackConnection(callback);
5215 if (connection != null) {
5216 connection.stopReceivingMessages();
5217 mService.unregisterQosCallback(connection);
5218 mQosCallbackConnections.remove(connection);
5219 } else {
5220 Log.d(TAG, "unregisterQosCallback: Callback not registered");
5221 }
5222 }
5223 } catch (final RemoteException e) {
5224 Log.e(TAG, "unregisterQosCallback: Error while unregistering ", e);
5225 e.rethrowFromSystemServer();
5226 }
5227 }
5228
5229 /**
5230 * Gets the connection related to the callback.
5231 *
5232 * @param callback the callback to look up
5233 * @return the related connection
5234 */
5235 @Nullable
5236 private QosCallbackConnection getQosCallbackConnection(final QosCallback callback) {
5237 for (final QosCallbackConnection connection : mQosCallbackConnections) {
5238 // Checking by reference here is intentional
5239 if (connection.getCallback() == callback) {
5240 return connection;
5241 }
5242 }
5243 return null;
5244 }
Junyu Laia62493f2021-01-19 11:10:56 +00005245
5246 /**
Roshan Pius951c0032020-12-22 15:10:42 -08005247 * Request a network to satisfy a set of {@link NetworkCapabilities}, but
Junyu Laia62493f2021-01-19 11:10:56 +00005248 * does not cause any networks to retain the NET_CAPABILITY_FOREGROUND capability. This can
5249 * be used to request that the system provide a network without causing the network to be
5250 * in the foreground.
5251 *
5252 * <p>This method will attempt to find the best network that matches the passed
5253 * {@link NetworkRequest}, and to bring up one that does if none currently satisfies the
5254 * criteria. The platform will evaluate which network is the best at its own discretion.
5255 * Throughput, latency, cost per byte, policy, user preference and other considerations
5256 * may be factored in the decision of what is considered the best network.
5257 *
5258 * <p>As long as this request is outstanding, the platform will try to maintain the best network
5259 * matching this request, while always attempting to match the request to a better network if
5260 * possible. If a better match is found, the platform will switch this request to the now-best
5261 * network and inform the app of the newly best network by invoking
5262 * {@link NetworkCallback#onAvailable(Network)} on the provided callback. Note that the platform
5263 * will not try to maintain any other network than the best one currently matching the request:
5264 * a network not matching any network request may be disconnected at any time.
5265 *
5266 * <p>For example, an application could use this method to obtain a connected cellular network
5267 * even if the device currently has a data connection over Ethernet. This may cause the cellular
5268 * radio to consume additional power. Or, an application could inform the system that it wants
5269 * a network supporting sending MMSes and have the system let it know about the currently best
5270 * MMS-supporting network through the provided {@link NetworkCallback}.
5271 *
5272 * <p>The status of the request can be followed by listening to the various callbacks described
5273 * in {@link NetworkCallback}. The {@link Network} object passed to the callback methods can be
5274 * used to direct traffic to the network (although accessing some networks may be subject to
5275 * holding specific permissions). Callers will learn about the specific characteristics of the
5276 * network through
5277 * {@link NetworkCallback#onCapabilitiesChanged(Network, NetworkCapabilities)} and
5278 * {@link NetworkCallback#onLinkPropertiesChanged(Network, LinkProperties)}. The methods of the
5279 * provided {@link NetworkCallback} will only be invoked due to changes in the best network
5280 * matching the request at any given time; therefore when a better network matching the request
5281 * becomes available, the {@link NetworkCallback#onAvailable(Network)} method is called
5282 * with the new network after which no further updates are given about the previously-best
5283 * network, unless it becomes the best again at some later time. All callbacks are invoked
5284 * in order on the same thread, which by default is a thread created by the framework running
5285 * in the app.
5286 *
5287 * <p>This{@link NetworkRequest} will live until released via
5288 * {@link #unregisterNetworkCallback(NetworkCallback)} or the calling application exits, at
5289 * which point the system may let go of the network at any time.
5290 *
5291 * <p>It is presently unsupported to request a network with mutable
5292 * {@link NetworkCapabilities} such as
5293 * {@link NetworkCapabilities#NET_CAPABILITY_VALIDATED} or
5294 * {@link NetworkCapabilities#NET_CAPABILITY_CAPTIVE_PORTAL}
5295 * as these {@code NetworkCapabilities} represent states that a particular
5296 * network may never attain, and whether a network will attain these states
5297 * is unknown prior to bringing up the network so the framework does not
5298 * know how to go about satisfying a request with these capabilities.
5299 *
5300 * <p>To avoid performance issues due to apps leaking callbacks, the system will limit the
5301 * number of outstanding requests to 100 per app (identified by their UID), shared with
5302 * all variants of this method, of {@link #registerNetworkCallback} as well as
5303 * {@link ConnectivityDiagnosticsManager#registerConnectivityDiagnosticsCallback}.
5304 * Requesting a network with this method will count toward this limit. If this limit is
5305 * exceeded, an exception will be thrown. To avoid hitting this issue and to conserve resources,
5306 * make sure to unregister the callbacks with
5307 * {@link #unregisterNetworkCallback(NetworkCallback)}.
5308 *
5309 * @param request {@link NetworkRequest} describing this request.
5310 * @param handler {@link Handler} to specify the thread upon which the callback will be invoked.
5311 * If null, the callback is invoked on the default internal Handler.
5312 * @param networkCallback The {@link NetworkCallback} to be utilized for this request. Note
5313 * the callback must not be shared - it uniquely specifies this request.
5314 * @throws IllegalArgumentException if {@code request} contains invalid network capabilities.
5315 * @throws SecurityException if missing the appropriate permissions.
5316 * @throws RuntimeException if the app already has too many callbacks registered.
5317 *
5318 * @hide
5319 */
5320 @SystemApi(client = MODULE_LIBRARIES)
5321 @SuppressLint("ExecutorRegistration")
5322 @RequiresPermission(anyOf = {
5323 android.Manifest.permission.NETWORK_SETTINGS,
5324 android.Manifest.permission.NETWORK_STACK,
5325 NetworkStack.PERMISSION_MAINLINE_NETWORK_STACK
5326 })
5327 public void requestBackgroundNetwork(@NonNull NetworkRequest request,
junyulai962bdb82021-03-09 20:49:48 +08005328 @NonNull Handler handler, @NonNull NetworkCallback networkCallback) {
Junyu Laia62493f2021-01-19 11:10:56 +00005329 final NetworkCapabilities nc = request.networkCapabilities;
5330 sendRequestForNetwork(nc, networkCallback, 0, BACKGROUND_REQUEST,
junyulai962bdb82021-03-09 20:49:48 +08005331 TYPE_NONE, new CallbackHandler(handler));
Junyu Laia62493f2021-01-19 11:10:56 +00005332 }
James Mattis45d81842021-01-10 14:24:24 -08005333
5334 /**
James Mattis45d81842021-01-10 14:24:24 -08005335 * Used by automotive devices to set the network preferences used to direct traffic at an
5336 * application level as per the given OemNetworkPreferences. An example use-case would be an
5337 * automotive OEM wanting to provide connectivity for applications critical to the usage of a
5338 * vehicle via a particular network.
5339 *
5340 * Calling this will overwrite the existing preference.
5341 *
5342 * @param preference {@link OemNetworkPreferences} The application network preference to be set.
5343 * @param executor the executor on which listener will be invoked.
5344 * @param listener {@link OnSetOemNetworkPreferenceListener} optional listener used to
5345 * communicate completion of setOemNetworkPreference(). This will only be
5346 * called once upon successful completion of setOemNetworkPreference().
5347 * @throws IllegalArgumentException if {@code preference} contains invalid preference values.
5348 * @throws SecurityException if missing the appropriate permissions.
5349 * @throws UnsupportedOperationException if called on a non-automotive device.
James Mattisda32cfe2021-01-26 16:23:52 -08005350 * @hide
James Mattis45d81842021-01-10 14:24:24 -08005351 */
James Mattisda32cfe2021-01-26 16:23:52 -08005352 @SystemApi
James Mattis8378aec2021-01-26 14:05:36 -08005353 @RequiresPermission(android.Manifest.permission.CONTROL_OEM_PAID_NETWORK_PREFERENCE)
James Mattisda32cfe2021-01-26 16:23:52 -08005354 public void setOemNetworkPreference(@NonNull final OemNetworkPreferences preference,
James Mattis45d81842021-01-10 14:24:24 -08005355 @Nullable @CallbackExecutor final Executor executor,
Chalard Jean6010c002021-03-03 16:37:13 +09005356 @Nullable final Runnable listener) {
James Mattis45d81842021-01-10 14:24:24 -08005357 Objects.requireNonNull(preference, "OemNetworkPreferences must be non-null");
5358 if (null != listener) {
5359 Objects.requireNonNull(executor, "Executor must be non-null");
5360 }
Chalard Jean6010c002021-03-03 16:37:13 +09005361 final IOnCompleteListener listenerInternal = listener == null ? null :
5362 new IOnCompleteListener.Stub() {
James Mattis45d81842021-01-10 14:24:24 -08005363 @Override
5364 public void onComplete() {
Chalard Jean6010c002021-03-03 16:37:13 +09005365 executor.execute(listener::run);
James Mattis45d81842021-01-10 14:24:24 -08005366 }
5367 };
5368
5369 try {
5370 mService.setOemNetworkPreference(preference, listenerInternal);
5371 } catch (RemoteException e) {
5372 Log.e(TAG, "setOemNetworkPreference() failed for preference: " + preference.toString());
5373 throw e.rethrowFromSystemServer();
5374 }
5375 }
lucaslin30e70a82021-03-12 00:46:33 +08005376
Chalard Jeanfa45a682021-02-25 17:23:40 +09005377 /**
5378 * Request that a user profile is put by default on a network matching a given preference.
5379 *
5380 * See the documentation for the individual preferences for a description of the supported
5381 * behaviors.
5382 *
5383 * @param profile the profile concerned.
5384 * @param preference the preference for this profile.
5385 * @param executor an executor to execute the listener on. Optional if listener is null.
5386 * @param listener an optional listener to listen for completion of the operation.
5387 * @throws IllegalArgumentException if {@code profile} is not a valid user profile.
5388 * @throws SecurityException if missing the appropriate permissions.
5389 * @hide
5390 */
Chalard Jean6010c002021-03-03 16:37:13 +09005391 // This function is for establishing per-profile default networking and can only be called by
5392 // the device policy manager, running as the system server. It would make no sense to call it
5393 // on a context for a user because it does not establish a setting on behalf of a user, rather
5394 // it establishes a setting for a user on behalf of the DPM.
5395 @SuppressLint({"UserHandle"})
5396 @SystemApi(client = MODULE_LIBRARIES)
Chalard Jeanfa45a682021-02-25 17:23:40 +09005397 @RequiresPermission(android.Manifest.permission.NETWORK_STACK)
5398 public void setProfileNetworkPreference(@NonNull final UserHandle profile,
5399 @ProfileNetworkPreference final int preference,
5400 @Nullable @CallbackExecutor final Executor executor,
5401 @Nullable final Runnable listener) {
5402 if (null != listener) {
5403 Objects.requireNonNull(executor, "Pass a non-null executor, or a null listener");
5404 }
5405 final IOnCompleteListener proxy;
5406 if (null == listener) {
5407 proxy = null;
5408 } else {
5409 proxy = new IOnCompleteListener.Stub() {
5410 @Override
5411 public void onComplete() {
5412 executor.execute(listener::run);
5413 }
5414 };
5415 }
5416 try {
5417 mService.setProfileNetworkPreference(profile, preference, proxy);
5418 } catch (RemoteException e) {
5419 throw e.rethrowFromSystemServer();
5420 }
5421 }
5422
lucaslin30e70a82021-03-12 00:46:33 +08005423 // The first network ID of IPSec tunnel interface.
lucaslin0cdcea12021-03-15 17:24:12 +08005424 private static final int TUN_INTF_NETID_START = 0xFC00; // 0xFC00 = 64512
lucaslin30e70a82021-03-12 00:46:33 +08005425 // The network ID range of IPSec tunnel interface.
lucaslin0cdcea12021-03-15 17:24:12 +08005426 private static final int TUN_INTF_NETID_RANGE = 0x0400; // 0x0400 = 1024
lucaslin30e70a82021-03-12 00:46:33 +08005427
5428 /**
5429 * Get the network ID range reserved for IPSec tunnel interfaces.
5430 *
5431 * @return A Range which indicates the network ID range of IPSec tunnel interface.
5432 * @hide
5433 */
5434 @SystemApi(client = MODULE_LIBRARIES)
5435 @NonNull
5436 public static Range<Integer> getIpSecNetIdRange() {
5437 return new Range(TUN_INTF_NETID_START, TUN_INTF_NETID_START + TUN_INTF_NETID_RANGE - 1);
5438 }
lucaslin705c3332021-03-12 17:56:09 +08005439
5440 /**
5441 * Get private DNS mode from settings.
5442 *
lucaslin7abe7e02021-03-17 14:53:35 +08005443 * @param context The Context to query the private DNS mode from settings.
lucaslin705c3332021-03-12 17:56:09 +08005444 * @return A string of private DNS mode as one of the PRIVATE_DNS_MODE_* constants.
5445 *
5446 * @hide
5447 */
5448 @SystemApi(client = MODULE_LIBRARIES)
5449 @NonNull
5450 @PrivateDnsMode
lucaslin20f04882021-03-16 17:11:14 +08005451 public static String getPrivateDnsMode(@NonNull Context context) {
5452 final ContentResolver cr = context.getContentResolver();
lucaslin705c3332021-03-12 17:56:09 +08005453 String mode = Settings.Global.getString(cr, PRIVATE_DNS_MODE);
5454 if (TextUtils.isEmpty(mode)) mode = Settings.Global.getString(cr, PRIVATE_DNS_DEFAULT_MODE);
5455 // If both PRIVATE_DNS_MODE and PRIVATE_DNS_DEFAULT_MODE are not set, choose
5456 // PRIVATE_DNS_MODE_OPPORTUNISTIC as default mode.
5457 if (TextUtils.isEmpty(mode)) mode = PRIVATE_DNS_MODE_OPPORTUNISTIC;
5458 return mode;
5459 }
paulhu89a762a2021-03-25 12:36:56 +08005460
5461 /**
5462 * Set private DNS mode to settings.
5463 *
5464 * @param context The {@link Context} to set the private DNS mode.
5465 * @param mode The private dns mode. This should be one of the PRIVATE_DNS_MODE_* constants.
5466 *
5467 * @hide
5468 */
5469 @SystemApi(client = MODULE_LIBRARIES)
5470 public static void setPrivateDnsMode(@NonNull Context context,
5471 @NonNull @PrivateDnsMode String mode) {
5472 if (!(mode == PRIVATE_DNS_MODE_OFF
5473 || mode == PRIVATE_DNS_MODE_OPPORTUNISTIC
5474 || mode == PRIVATE_DNS_MODE_PROVIDER_HOSTNAME)) {
5475 throw new IllegalArgumentException("Invalid private dns mode");
5476 }
5477 Settings.Global.putString(context.getContentResolver(), PRIVATE_DNS_MODE, mode);
5478 }
The Android Open Source Project28527d22009-03-03 19:31:44 -08005479}