blob: d3f07d80b5ef2a93c5a1861a602880a0281c9b0d [file] [log] [blame]
The Android Open Source Project28527d22009-03-03 19:31:44 -08001/*
2 * Copyright (C) 2008 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
The Android Open Source Project28527d22009-03-03 19:31:44 -080016package android.net;
17
Junyu Lai23568a42021-01-19 11:10:56 +000018import static android.annotation.SystemApi.Client.MODULE_LIBRARIES;
Junyu Lai23568a42021-01-19 11:10:56 +000019import static android.net.NetworkRequest.Type.BACKGROUND_REQUEST;
junyulaid1a78162021-01-11 16:53:38 +080020import static android.net.NetworkRequest.Type.LISTEN;
21import static android.net.NetworkRequest.Type.REQUEST;
junyulai6b0a6a22021-03-05 15:51:17 +080022import static android.net.NetworkRequest.Type.TRACK_BEST;
junyulaid1a78162021-01-11 16:53:38 +080023import static android.net.NetworkRequest.Type.TRACK_DEFAULT;
Lorenzo Colitti80b48bc2021-01-29 20:14:04 +090024import static android.net.NetworkRequest.Type.TRACK_SYSTEM_DEFAULT;
Daniel Bright60f02ed2020-06-15 16:10:01 -070025import static android.net.QosCallback.QosCallbackRegistrationException;
junyulai48a59382019-01-15 11:32:44 +080026
junyulaia86defc2018-12-27 17:25:29 +080027import android.annotation.CallbackExecutor;
Felipe Lemed16384b2016-01-22 09:44:57 -080028import android.annotation.IntDef;
Chalard Jean42a9c292019-01-07 19:26:34 +090029import android.annotation.NonNull;
Robin Lee33c73e22016-01-05 18:03:46 +000030import android.annotation.Nullable;
Jeff Sharkey2ac62992017-04-24 11:18:03 -060031import android.annotation.RequiresPermission;
The Android Open Source Project28527d22009-03-03 19:31:44 -080032import android.annotation.SdkConstant;
33import android.annotation.SdkConstant.SdkConstantType;
Junyu Lai23568a42021-01-19 11:10:56 +000034import android.annotation.SuppressLint;
Udam Saini53b71ee2016-01-04 12:16:14 -080035import android.annotation.SystemApi;
Jeff Sharkeyb8c73032017-06-02 17:36:26 -060036import android.annotation.SystemService;
Robert Greenwalt2bebbfb2014-03-26 16:47:06 -070037import android.app.PendingIntent;
Artur Satayev0e45d782019-12-10 17:47:52 +000038import android.compat.annotation.UnsupportedAppUsage;
Jeff Sharkey2d9e5a52012-04-04 20:40:58 -070039import android.content.Context;
Robert Greenwaltd0ebdbb2014-05-18 23:07:25 -070040import android.content.Intent;
junyulaia86defc2018-12-27 17:25:29 +080041import android.net.IpSecManager.UdpEncapsulationSocket;
42import android.net.SocketKeepalive.Callback;
markchiencc1bc1b2020-01-20 19:31:56 +080043import android.net.TetheringManager.StartTetheringCallback;
markchieneeea0be2020-01-21 13:11:06 +080044import android.net.TetheringManager.TetheringEventCallback;
markchiencc1bc1b2020-01-20 19:31:56 +080045import android.net.TetheringManager.TetheringRequest;
Roshan Pius7992afd2020-12-22 15:10:42 -080046import android.net.wifi.WifiNetworkSuggestion;
Robert Greenwalt2034b912009-08-12 16:08:25 -070047import android.os.Binder;
Mathew Inwoodac5968e2018-12-20 15:30:45 +000048import android.os.Build;
Jeff Sharkey39c01eb2011-08-16 14:37:57 -070049import android.os.Build.VERSION_CODES;
Jeremy Klein9a36ec82016-01-22 14:11:45 -080050import android.os.Bundle;
Robert Greenwalt2bebbfb2014-03-26 16:47:06 -070051import android.os.Handler;
Dianne Hackbornb4b09e82014-02-26 16:20:52 -080052import android.os.IBinder;
Robert Greenwalt2bebbfb2014-03-26 16:47:06 -070053import android.os.Looper;
54import android.os.Message;
Robert Greenwalt030e1d32012-08-21 19:27:00 -070055import android.os.Messenger;
junyulai61143782019-03-04 22:45:36 +080056import android.os.ParcelFileDescriptor;
Cody Kestingb5c7abd2020-04-15 12:33:28 -070057import android.os.PersistableBundle;
Lorenzo Colitti9b026fa2015-07-29 11:41:21 +090058import android.os.Process;
The Android Open Source Project28527d22009-03-03 19:31:44 -080059import android.os.RemoteException;
Jeremy Klein9a36ec82016-01-22 14:11:45 -080060import android.os.ResultReceiver;
Dianne Hackbornb4b09e82014-02-26 16:20:52 -080061import android.os.ServiceManager;
Hugo Benichi145e3792017-05-11 13:16:17 +090062import android.os.ServiceSpecificException;
Jeff Sharkey971cd162011-08-29 16:02:57 -070063import android.provider.Settings;
Wink Saville02eb35c2014-12-05 11:10:30 -080064import android.telephony.SubscriptionManager;
Meng Wang91311c02019-11-18 17:10:00 -080065import android.telephony.TelephonyManager;
Dianne Hackbornb4b09e82014-02-26 16:20:52 -080066import android.util.ArrayMap;
Robert Greenwalt2bebbfb2014-03-26 16:47:06 -070067import android.util.Log;
Lorenzo Colittie8ce2052020-12-12 00:51:11 +090068import android.util.Range;
Erik Kline50068e52017-01-26 18:08:28 +090069import android.util.SparseIntArray;
The Android Open Source Project28527d22009-03-03 19:31:44 -080070
Remi NGUYEN VAN96be83b2020-12-23 12:45:08 +090071import com.android.connectivity.aidl.INetworkAgent;
markchieneeea0be2020-01-21 13:11:06 +080072import com.android.internal.annotations.GuardedBy;
Hugo Benichi45a49542017-03-06 09:17:06 +090073import com.android.internal.util.Protocol;
Robert Greenwalt3d6c9582014-05-21 20:04:36 -070074
Paul Jensen99c36662014-08-27 12:38:45 -040075import libcore.net.event.NetworkEventDispatcher;
76
junyulai61143782019-03-04 22:45:36 +080077import java.io.IOException;
78import java.io.UncheckedIOException;
Felipe Lemed16384b2016-01-22 09:44:57 -080079import java.lang.annotation.Retention;
80import java.lang.annotation.RetentionPolicy;
Lorenzo Colittie8ce2052020-12-12 00:51:11 +090081import java.net.DatagramSocket;
Jeremy Klein04863332015-12-28 15:11:58 -080082import java.net.InetAddress;
Jeff Vander Stoepe7205d62018-07-23 10:57:53 -070083import java.net.InetSocketAddress;
junyulaic7ea1242019-01-08 20:04:33 +080084import java.net.Socket;
Hugo Benichi45a49542017-03-06 09:17:06 +090085import java.util.ArrayList;
Lorenzo Colittie8ce2052020-12-12 00:51:11 +090086import java.util.Collection;
Jeremy Klein04863332015-12-28 15:11:58 -080087import java.util.HashMap;
Hugo Benichi45a49542017-03-06 09:17:06 +090088import java.util.List;
89import java.util.Map;
markchieneeea0be2020-01-21 13:11:06 +080090import java.util.Objects;
junyulaia86defc2018-12-27 17:25:29 +080091import java.util.concurrent.Executor;
junyulai9f872232019-01-16 20:23:34 +080092import java.util.concurrent.ExecutorService;
93import java.util.concurrent.Executors;
94import java.util.concurrent.RejectedExecutionException;
Jeremy Klein04863332015-12-28 15:11:58 -080095
The Android Open Source Project28527d22009-03-03 19:31:44 -080096/**
97 * Class that answers queries about the state of network connectivity. It also
Jeff Sharkeyb8c73032017-06-02 17:36:26 -060098 * notifies applications when network connectivity changes.
The Android Open Source Project28527d22009-03-03 19:31:44 -080099 * <p>
100 * The primary responsibilities of this class are to:
101 * <ol>
102 * <li>Monitor network connections (Wi-Fi, GPRS, UMTS, etc.)</li>
103 * <li>Send broadcast intents when network connectivity changes</li>
104 * <li>Attempt to "fail over" to another network when connectivity to a network
105 * is lost</li>
106 * <li>Provide an API that allows applications to query the coarse-grained or fine-grained
107 * state of the available networks</li>
Robert Greenwaltd0ebdbb2014-05-18 23:07:25 -0700108 * <li>Provide an API that allows applications to request and select networks for their data
109 * traffic</li>
The Android Open Source Project28527d22009-03-03 19:31:44 -0800110 * </ol>
111 */
Jeff Sharkeyb8c73032017-06-02 17:36:26 -0600112@SystemService(Context.CONNECTIVITY_SERVICE)
Jeff Sharkey921ebf22011-05-19 17:12:49 -0700113public class ConnectivityManager {
114 private static final String TAG = "ConnectivityManager";
Soi, Yoshinari99efcbd2015-11-12 12:09:02 +0900115 private static final boolean DEBUG = Log.isLoggable(TAG, Log.DEBUG);
Jeff Sharkey921ebf22011-05-19 17:12:49 -0700116
The Android Open Source Project28527d22009-03-03 19:31:44 -0800117 /**
Robert Greenwaltd0ebdbb2014-05-18 23:07:25 -0700118 * A change in network connectivity has occurred. A default connection has either
The Android Open Source Project28527d22009-03-03 19:31:44 -0800119 * been established or lost. The NetworkInfo for the affected network is
120 * sent as an extra; it should be consulted to see what kind of
121 * connectivity event occurred.
122 * <p/>
Mark Lue1682a02016-12-05 10:57:55 -0800123 * Apps targeting Android 7.0 (API level 24) and higher do not receive this
124 * broadcast if they declare the broadcast receiver in their manifest. Apps
125 * will still receive broadcasts if they register their
126 * {@link android.content.BroadcastReceiver} with
127 * {@link android.content.Context#registerReceiver Context.registerReceiver()}
128 * and that context is still valid.
129 * <p/>
The Android Open Source Project28527d22009-03-03 19:31:44 -0800130 * If this is a connection that was the result of failing over from a
131 * disconnected network, then the FAILOVER_CONNECTION boolean extra is
132 * set to true.
133 * <p/>
134 * For a loss of connectivity, if the connectivity manager is attempting
135 * to connect (or has already connected) to another network, the
136 * NetworkInfo for the new network is also passed as an extra. This lets
137 * any receivers of the broadcast know that they should not necessarily
138 * tell the user that no data traffic will be possible. Instead, the
Robert Greenwalt26744a52013-02-15 10:56:35 -0800139 * receiver should expect another broadcast soon, indicating either that
The Android Open Source Project28527d22009-03-03 19:31:44 -0800140 * the failover attempt succeeded (and so there is still overall data
141 * connectivity), or that the failover attempt failed, meaning that all
142 * connectivity has been lost.
143 * <p/>
144 * For a disconnect event, the boolean extra EXTRA_NO_CONNECTIVITY
145 * is set to {@code true} if there are no connected networks at all.
Chalard Jeanae2424a2018-02-10 05:33:50 +0900146 *
147 * @deprecated apps should use the more versatile {@link #requestNetwork},
148 * {@link #registerNetworkCallback} or {@link #registerDefaultNetworkCallback}
149 * functions instead for faster and more detailed updates about the network
150 * changes they care about.
The Android Open Source Project28527d22009-03-03 19:31:44 -0800151 */
Jeff Sharkey6932ff62013-02-20 18:21:19 -0800152 @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION)
Chalard Jeanae2424a2018-02-10 05:33:50 +0900153 @Deprecated
The Android Open Source Project28527d22009-03-03 19:31:44 -0800154 public static final String CONNECTIVITY_ACTION = "android.net.conn.CONNECTIVITY_CHANGE";
Jeff Sharkey66fa9682011-08-02 17:22:34 -0700155
The Android Open Source Project28527d22009-03-03 19:31:44 -0800156 /**
Paul Jensened4d55c2015-02-27 22:55:47 -0500157 * The device has connected to a network that has presented a captive
158 * portal, which is blocking Internet connectivity. The user was presented
159 * with a notification that network sign in is required,
160 * and the user invoked the notification's action indicating they
Paul Jensen4173cce2015-05-22 10:50:39 -0400161 * desire to sign in to the network. Apps handling this activity should
Paul Jensened4d55c2015-02-27 22:55:47 -0500162 * facilitate signing in to the network. This action includes a
163 * {@link Network} typed extra called {@link #EXTRA_NETWORK} that represents
164 * the network presenting the captive portal; all communication with the
165 * captive portal must be done using this {@code Network} object.
166 * <p/>
Paul Jensen4173cce2015-05-22 10:50:39 -0400167 * This activity includes a {@link CaptivePortal} extra named
168 * {@link #EXTRA_CAPTIVE_PORTAL} that can be used to indicate different
169 * outcomes of the captive portal sign in to the system:
170 * <ul>
171 * <li> When the app handling this action believes the user has signed in to
172 * the network and the captive portal has been dismissed, the app should
173 * call {@link CaptivePortal#reportCaptivePortalDismissed} so the system can
174 * reevaluate the network. If reevaluation finds the network no longer
175 * subject to a captive portal, the network may become the default active
Chalard Jean73d9db72018-06-04 16:52:49 +0900176 * data network.</li>
Paul Jensen4173cce2015-05-22 10:50:39 -0400177 * <li> When the app handling this action believes the user explicitly wants
Paul Jensened4d55c2015-02-27 22:55:47 -0500178 * to ignore the captive portal and the network, the app should call
Paul Jensen4173cce2015-05-22 10:50:39 -0400179 * {@link CaptivePortal#ignoreNetwork}. </li>
180 * </ul>
Paul Jensened4d55c2015-02-27 22:55:47 -0500181 */
182 @SdkConstant(SdkConstantType.ACTIVITY_INTENT_ACTION)
183 public static final String ACTION_CAPTIVE_PORTAL_SIGN_IN = "android.net.conn.CAPTIVE_PORTAL";
184
185 /**
The Android Open Source Project28527d22009-03-03 19:31:44 -0800186 * The lookup key for a {@link NetworkInfo} object. Retrieve with
187 * {@link android.content.Intent#getParcelableExtra(String)}.
Jeff Sharkey66fa9682011-08-02 17:22:34 -0700188 *
Chalard Jeancc47b522019-01-11 16:47:53 +0900189 * @deprecated The {@link NetworkInfo} object is deprecated, as many of its properties
190 * can't accurately represent modern network characteristics.
191 * Please obtain information about networks from the {@link NetworkCapabilities}
192 * or {@link LinkProperties} objects instead.
The Android Open Source Project28527d22009-03-03 19:31:44 -0800193 */
Jeff Sharkey66fa9682011-08-02 17:22:34 -0700194 @Deprecated
The Android Open Source Project28527d22009-03-03 19:31:44 -0800195 public static final String EXTRA_NETWORK_INFO = "networkInfo";
Jeff Sharkey66fa9682011-08-02 17:22:34 -0700196
The Android Open Source Project28527d22009-03-03 19:31:44 -0800197 /**
Jeff Sharkey47905d12012-08-06 11:41:50 -0700198 * Network type which triggered a {@link #CONNECTIVITY_ACTION} broadcast.
Jeff Sharkey47905d12012-08-06 11:41:50 -0700199 *
200 * @see android.content.Intent#getIntExtra(String, int)
Chalard Jeancc47b522019-01-11 16:47:53 +0900201 * @deprecated The network type is not rich enough to represent the characteristics
202 * of modern networks. Please use {@link NetworkCapabilities} instead,
203 * in particular the transports.
Jeff Sharkey47905d12012-08-06 11:41:50 -0700204 */
Chalard Jeancc47b522019-01-11 16:47:53 +0900205 @Deprecated
Jeff Sharkey47905d12012-08-06 11:41:50 -0700206 public static final String EXTRA_NETWORK_TYPE = "networkType";
207
208 /**
The Android Open Source Project28527d22009-03-03 19:31:44 -0800209 * The lookup key for a boolean that indicates whether a connect event
210 * is for a network to which the connectivity manager was failing over
211 * following a disconnect on another network.
212 * Retrieve it with {@link android.content.Intent#getBooleanExtra(String,boolean)}.
junyulai5c2f6262018-12-13 12:47:51 +0800213 *
214 * @deprecated See {@link NetworkInfo}.
The Android Open Source Project28527d22009-03-03 19:31:44 -0800215 */
junyulai5c2f6262018-12-13 12:47:51 +0800216 @Deprecated
The Android Open Source Project28527d22009-03-03 19:31:44 -0800217 public static final String EXTRA_IS_FAILOVER = "isFailover";
218 /**
219 * The lookup key for a {@link NetworkInfo} object. This is supplied when
220 * there is another network that it may be possible to connect to. Retrieve with
221 * {@link android.content.Intent#getParcelableExtra(String)}.
junyulai5c2f6262018-12-13 12:47:51 +0800222 *
223 * @deprecated See {@link NetworkInfo}.
The Android Open Source Project28527d22009-03-03 19:31:44 -0800224 */
junyulai5c2f6262018-12-13 12:47:51 +0800225 @Deprecated
The Android Open Source Project28527d22009-03-03 19:31:44 -0800226 public static final String EXTRA_OTHER_NETWORK_INFO = "otherNetwork";
227 /**
228 * The lookup key for a boolean that indicates whether there is a
229 * complete lack of connectivity, i.e., no network is available.
230 * Retrieve it with {@link android.content.Intent#getBooleanExtra(String,boolean)}.
231 */
232 public static final String EXTRA_NO_CONNECTIVITY = "noConnectivity";
233 /**
234 * The lookup key for a string that indicates why an attempt to connect
235 * to a network failed. The string has no particular structure. It is
236 * intended to be used in notifications presented to users. Retrieve
237 * it with {@link android.content.Intent#getStringExtra(String)}.
238 */
239 public static final String EXTRA_REASON = "reason";
240 /**
241 * The lookup key for a string that provides optionally supplied
242 * extra information about the network state. The information
243 * may be passed up from the lower networking layers, and its
244 * meaning may be specific to a particular network type. Retrieve
245 * it with {@link android.content.Intent#getStringExtra(String)}.
junyulai5c2f6262018-12-13 12:47:51 +0800246 *
247 * @deprecated See {@link NetworkInfo#getExtraInfo()}.
The Android Open Source Project28527d22009-03-03 19:31:44 -0800248 */
junyulai5c2f6262018-12-13 12:47:51 +0800249 @Deprecated
The Android Open Source Project28527d22009-03-03 19:31:44 -0800250 public static final String EXTRA_EXTRA_INFO = "extraInfo";
Robert Greenwalt986c7412010-09-08 15:24:47 -0700251 /**
252 * The lookup key for an int that provides information about
253 * our connection to the internet at large. 0 indicates no connection,
254 * 100 indicates a great connection. Retrieve it with
Jeff Sharkey921ebf22011-05-19 17:12:49 -0700255 * {@link android.content.Intent#getIntExtra(String, int)}.
Robert Greenwalt986c7412010-09-08 15:24:47 -0700256 * {@hide}
257 */
258 public static final String EXTRA_INET_CONDITION = "inetCondition";
The Android Open Source Project28527d22009-03-03 19:31:44 -0800259 /**
Paul Jensen4173cce2015-05-22 10:50:39 -0400260 * The lookup key for a {@link CaptivePortal} object included with the
261 * {@link #ACTION_CAPTIVE_PORTAL_SIGN_IN} intent. The {@code CaptivePortal}
262 * object can be used to either indicate to the system that the captive
263 * portal has been dismissed or that the user does not want to pursue
264 * signing in to captive portal. Retrieve it with
265 * {@link android.content.Intent#getParcelableExtra(String)}.
Paul Jensened4d55c2015-02-27 22:55:47 -0500266 */
Paul Jensen4173cce2015-05-22 10:50:39 -0400267 public static final String EXTRA_CAPTIVE_PORTAL = "android.net.extra.CAPTIVE_PORTAL";
Jan Nordqvist0f3b4442015-09-22 15:54:32 -0700268
269 /**
270 * Key for passing a URL to the captive portal login activity.
271 */
272 public static final String EXTRA_CAPTIVE_PORTAL_URL = "android.net.extra.CAPTIVE_PORTAL_URL";
273
Paul Jensened4d55c2015-02-27 22:55:47 -0500274 /**
Remi NGUYEN VANd937e3a2018-05-22 10:01:53 +0900275 * Key for passing a {@link android.net.captiveportal.CaptivePortalProbeSpec} to the captive
276 * portal login activity.
277 * {@hide}
278 */
Remi NGUYEN VANe541d182019-01-17 14:38:31 +0900279 @SystemApi
Remi NGUYEN VANd937e3a2018-05-22 10:01:53 +0900280 public static final String EXTRA_CAPTIVE_PORTAL_PROBE_SPEC =
281 "android.net.extra.CAPTIVE_PORTAL_PROBE_SPEC";
282
283 /**
Hugo Benichidce21c52016-12-14 08:23:40 +0900284 * Key for passing a user agent string to the captive portal login activity.
285 * {@hide}
286 */
Remi NGUYEN VANe541d182019-01-17 14:38:31 +0900287 @SystemApi
Hugo Benichidce21c52016-12-14 08:23:40 +0900288 public static final String EXTRA_CAPTIVE_PORTAL_USER_AGENT =
289 "android.net.extra.CAPTIVE_PORTAL_USER_AGENT";
290
291 /**
Haoyu Baib5da5752012-06-20 14:29:57 -0700292 * Broadcast action to indicate the change of data activity status
293 * (idle or active) on a network in a recent period.
Robert Greenwalt26744a52013-02-15 10:56:35 -0800294 * The network becomes active when data transmission is started, or
295 * idle if there is no data transmission for a period of time.
Haoyu Baib5da5752012-06-20 14:29:57 -0700296 * {@hide}
297 */
298 @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION)
Chalard Jean73d9db72018-06-04 16:52:49 +0900299 public static final String ACTION_DATA_ACTIVITY_CHANGE =
300 "android.net.conn.DATA_ACTIVITY_CHANGE";
Haoyu Baib5da5752012-06-20 14:29:57 -0700301 /**
302 * The lookup key for an enum that indicates the network device type on which this data activity
303 * change happens.
304 * {@hide}
305 */
306 public static final String EXTRA_DEVICE_TYPE = "deviceType";
307 /**
308 * The lookup key for a boolean that indicates the device is active or not. {@code true} means
309 * it is actively sending or receiving data and {@code false} means it is idle.
310 * {@hide}
311 */
312 public static final String EXTRA_IS_ACTIVE = "isActive";
Ashish Sharma601fba92014-03-12 18:42:23 -0700313 /**
314 * The lookup key for a long that contains the timestamp (nanos) of the radio state change.
315 * {@hide}
316 */
317 public static final String EXTRA_REALTIME_NS = "tsNanos";
Haoyu Baib5da5752012-06-20 14:29:57 -0700318
319 /**
The Android Open Source Project28527d22009-03-03 19:31:44 -0800320 * Broadcast Action: The setting for background data usage has changed
321 * values. Use {@link #getBackgroundDataSetting()} to get the current value.
322 * <p>
323 * If an application uses the network in the background, it should listen
324 * for this broadcast and stop using the background data if the value is
Jeff Sharkey921ebf22011-05-19 17:12:49 -0700325 * {@code false}.
Jeff Sharkeyc958c772012-01-30 16:29:24 -0800326 * <p>
327 *
328 * @deprecated As of {@link VERSION_CODES#ICE_CREAM_SANDWICH}, availability
329 * of background data depends on several combined factors, and
330 * this broadcast is no longer sent. Instead, when background
331 * data is unavailable, {@link #getActiveNetworkInfo()} will now
332 * appear disconnected. During first boot after a platform
333 * upgrade, this broadcast will be sent once if
334 * {@link #getBackgroundDataSetting()} was {@code false} before
335 * the upgrade.
The Android Open Source Project28527d22009-03-03 19:31:44 -0800336 */
337 @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION)
Jeff Sharkeyc958c772012-01-30 16:29:24 -0800338 @Deprecated
The Android Open Source Project28527d22009-03-03 19:31:44 -0800339 public static final String ACTION_BACKGROUND_DATA_SETTING_CHANGED =
340 "android.net.conn.BACKGROUND_DATA_SETTING_CHANGED";
341
Robert Greenwaltd3401f92010-09-15 17:36:33 -0700342 /**
343 * Broadcast Action: The network connection may not be good
344 * uses {@code ConnectivityManager.EXTRA_INET_CONDITION} and
345 * {@code ConnectivityManager.EXTRA_NETWORK_INFO} to specify
346 * the network and it's condition.
347 * @hide
348 */
Jeff Sharkey6932ff62013-02-20 18:21:19 -0800349 @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION)
Mathew Inwood0b8f8612018-08-08 14:52:47 +0100350 @UnsupportedAppUsage
Robert Greenwaltd3401f92010-09-15 17:36:33 -0700351 public static final String INET_CONDITION_ACTION =
352 "android.net.conn.INET_CONDITION_ACTION";
353
Robert Greenwalt2034b912009-08-12 16:08:25 -0700354 /**
Robert Greenwalt26744a52013-02-15 10:56:35 -0800355 * Broadcast Action: A tetherable connection has come or gone.
356 * Uses {@code ConnectivityManager.EXTRA_AVAILABLE_TETHER},
Erik Kline449ccfe2017-04-17 16:47:23 +0900357 * {@code ConnectivityManager.EXTRA_ACTIVE_LOCAL_ONLY},
358 * {@code ConnectivityManager.EXTRA_ACTIVE_TETHER}, and
Robert Greenwalt26744a52013-02-15 10:56:35 -0800359 * {@code ConnectivityManager.EXTRA_ERRORED_TETHER} to indicate
360 * the current state of tethering. Each include a list of
361 * interface names in that state (may be empty).
Robert Greenwalt0c4828c2010-01-26 11:40:34 -0800362 * @hide
363 */
Jeff Sharkey6932ff62013-02-20 18:21:19 -0800364 @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION)
Mathew Inwood5a09a712020-11-04 09:29:36 +0000365 @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.R, trackingBug = 170729553)
Robert Greenwalt0c4828c2010-01-26 11:40:34 -0800366 public static final String ACTION_TETHER_STATE_CHANGED =
markchien1f523702019-12-25 19:40:32 +0800367 TetheringManager.ACTION_TETHER_STATE_CHANGED;
Robert Greenwalt0c4828c2010-01-26 11:40:34 -0800368
369 /**
370 * @hide
Robert Greenwalt26744a52013-02-15 10:56:35 -0800371 * gives a String[] listing all the interfaces configured for
372 * tethering and currently available for tethering.
Robert Greenwalt0c4828c2010-01-26 11:40:34 -0800373 */
Mathew Inwood5a09a712020-11-04 09:29:36 +0000374 @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.R, trackingBug = 170729553)
markchien1f523702019-12-25 19:40:32 +0800375 public static final String EXTRA_AVAILABLE_TETHER = TetheringManager.EXTRA_AVAILABLE_TETHER;
Robert Greenwalt0c4828c2010-01-26 11:40:34 -0800376
377 /**
378 * @hide
Erik Kline449ccfe2017-04-17 16:47:23 +0900379 * gives a String[] listing all the interfaces currently in local-only
380 * mode (ie, has DHCPv4+IPv6-ULA support and no packet forwarding)
Robert Greenwalt0c4828c2010-01-26 11:40:34 -0800381 */
markchien1f523702019-12-25 19:40:32 +0800382 public static final String EXTRA_ACTIVE_LOCAL_ONLY = TetheringManager.EXTRA_ACTIVE_LOCAL_ONLY;
Erik Kline449ccfe2017-04-17 16:47:23 +0900383
384 /**
385 * @hide
386 * gives a String[] listing all the interfaces currently tethered
387 * (ie, has DHCPv4 support and packets potentially forwarded/NATed)
388 */
Mathew Inwood5a09a712020-11-04 09:29:36 +0000389 @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.R, trackingBug = 170729553)
markchien1f523702019-12-25 19:40:32 +0800390 public static final String EXTRA_ACTIVE_TETHER = TetheringManager.EXTRA_ACTIVE_TETHER;
Robert Greenwalt8e87f122010-02-11 18:18:40 -0800391
392 /**
393 * @hide
Robert Greenwalt26744a52013-02-15 10:56:35 -0800394 * gives a String[] listing all the interfaces we tried to tether and
395 * failed. Use {@link #getLastTetherError} to find the error code
396 * for any interfaces listed here.
Robert Greenwalt8e87f122010-02-11 18:18:40 -0800397 */
Mathew Inwood5a09a712020-11-04 09:29:36 +0000398 @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.R, trackingBug = 170729553)
markchien1f523702019-12-25 19:40:32 +0800399 public static final String EXTRA_ERRORED_TETHER = TetheringManager.EXTRA_ERRORED_TETHER;
Robert Greenwalt0c4828c2010-01-26 11:40:34 -0800400
401 /**
Russell Brenner4774b022013-02-12 10:03:14 -0800402 * Broadcast Action: The captive portal tracker has finished its test.
403 * Sent only while running Setup Wizard, in lieu of showing a user
404 * notification.
405 * @hide
406 */
Jeff Sharkey6932ff62013-02-20 18:21:19 -0800407 @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION)
Russell Brenner4774b022013-02-12 10:03:14 -0800408 public static final String ACTION_CAPTIVE_PORTAL_TEST_COMPLETED =
409 "android.net.conn.CAPTIVE_PORTAL_TEST_COMPLETED";
410 /**
411 * The lookup key for a boolean that indicates whether a captive portal was detected.
412 * Retrieve it with {@link android.content.Intent#getBooleanExtra(String,boolean)}.
413 * @hide
414 */
415 public static final String EXTRA_IS_CAPTIVE_PORTAL = "captivePortal";
416
417 /**
Lorenzo Colitti36fbebe2015-04-03 16:38:52 +0900418 * Action used to display a dialog that asks the user whether to connect to a network that is
419 * not validated. This intent is used to start the dialog in settings via startActivity.
420 *
421 * @hide
422 */
423 public static final String ACTION_PROMPT_UNVALIDATED = "android.net.conn.PROMPT_UNVALIDATED";
424
425 /**
Lorenzo Colitti477bf5c2016-09-15 14:02:29 +0900426 * Action used to display a dialog that asks the user whether to avoid a network that is no
427 * longer validated. This intent is used to start the dialog in settings via startActivity.
428 *
429 * @hide
430 */
431 public static final String ACTION_PROMPT_LOST_VALIDATION =
432 "android.net.conn.PROMPT_LOST_VALIDATION";
433
434 /**
lucaslin25a50472019-03-12 13:08:03 +0800435 * Action used to display a dialog that asks the user whether to stay connected to a network
436 * that has not validated. This intent is used to start the dialog in settings via
437 * startActivity.
438 *
439 * @hide
440 */
441 public static final String ACTION_PROMPT_PARTIAL_CONNECTIVITY =
442 "android.net.conn.PROMPT_PARTIAL_CONNECTIVITY";
443
444 /**
Jeremy Klein9a36ec82016-01-22 14:11:45 -0800445 * Invalid tethering type.
Chalard Jean73d9db72018-06-04 16:52:49 +0900446 * @see #startTethering(int, boolean, OnStartTetheringCallback)
Jeremy Klein9a36ec82016-01-22 14:11:45 -0800447 * @hide
448 */
markchien1f523702019-12-25 19:40:32 +0800449 public static final int TETHERING_INVALID = TetheringManager.TETHERING_INVALID;
Jeremy Klein9a36ec82016-01-22 14:11:45 -0800450
451 /**
452 * Wifi tethering type.
Chalard Jean73d9db72018-06-04 16:52:49 +0900453 * @see #startTethering(int, boolean, OnStartTetheringCallback)
Jeremy Klein9a36ec82016-01-22 14:11:45 -0800454 * @hide
455 */
456 @SystemApi
Remi NGUYEN VAN23b853b2021-02-15 18:52:06 +0900457 public static final int TETHERING_WIFI = 0;
Jeremy Klein9a36ec82016-01-22 14:11:45 -0800458
459 /**
460 * USB tethering type.
Chalard Jean73d9db72018-06-04 16:52:49 +0900461 * @see #startTethering(int, boolean, OnStartTetheringCallback)
Jeremy Klein9a36ec82016-01-22 14:11:45 -0800462 * @hide
463 */
464 @SystemApi
Remi NGUYEN VAN23b853b2021-02-15 18:52:06 +0900465 public static final int TETHERING_USB = 1;
Jeremy Klein9a36ec82016-01-22 14:11:45 -0800466
467 /**
468 * Bluetooth tethering type.
Chalard Jean73d9db72018-06-04 16:52:49 +0900469 * @see #startTethering(int, boolean, OnStartTetheringCallback)
Jeremy Klein9a36ec82016-01-22 14:11:45 -0800470 * @hide
471 */
472 @SystemApi
Remi NGUYEN VAN23b853b2021-02-15 18:52:06 +0900473 public static final int TETHERING_BLUETOOTH = 2;
Jeremy Klein9a36ec82016-01-22 14:11:45 -0800474
475 /**
Jimmy Chen87db1542019-07-15 18:03:23 +0800476 * Wifi P2p tethering type.
477 * Wifi P2p tethering is set through events automatically, and don't
478 * need to start from #startTethering(int, boolean, OnStartTetheringCallback).
479 * @hide
480 */
markchien1f523702019-12-25 19:40:32 +0800481 public static final int TETHERING_WIFI_P2P = TetheringManager.TETHERING_WIFI_P2P;
Jimmy Chen87db1542019-07-15 18:03:23 +0800482
483 /**
Jeremy Klein9a36ec82016-01-22 14:11:45 -0800484 * Extra used for communicating with the TetherService. Includes the type of tethering to
485 * enable if any.
486 * @hide
487 */
markchieneeea0be2020-01-21 13:11:06 +0800488 public static final String EXTRA_ADD_TETHER_TYPE = TetheringConstants.EXTRA_ADD_TETHER_TYPE;
Jeremy Klein9a36ec82016-01-22 14:11:45 -0800489
490 /**
491 * Extra used for communicating with the TetherService. Includes the type of tethering for
492 * which to cancel provisioning.
493 * @hide
494 */
markchieneeea0be2020-01-21 13:11:06 +0800495 public static final String EXTRA_REM_TETHER_TYPE = TetheringConstants.EXTRA_REM_TETHER_TYPE;
Jeremy Klein9a36ec82016-01-22 14:11:45 -0800496
497 /**
498 * Extra used for communicating with the TetherService. True to schedule a recheck of tether
499 * provisioning.
500 * @hide
501 */
markchieneeea0be2020-01-21 13:11:06 +0800502 public static final String EXTRA_SET_ALARM = TetheringConstants.EXTRA_SET_ALARM;
Jeremy Klein9a36ec82016-01-22 14:11:45 -0800503
504 /**
505 * Tells the TetherService to run a provision check now.
506 * @hide
507 */
markchieneeea0be2020-01-21 13:11:06 +0800508 public static final String EXTRA_RUN_PROVISION = TetheringConstants.EXTRA_RUN_PROVISION;
Jeremy Klein9a36ec82016-01-22 14:11:45 -0800509
510 /**
511 * Extra used for communicating with the TetherService. Contains the {@link ResultReceiver}
512 * which will receive provisioning results. Can be left empty.
513 * @hide
514 */
markchieneeea0be2020-01-21 13:11:06 +0800515 public static final String EXTRA_PROVISION_CALLBACK =
516 TetheringConstants.EXTRA_PROVISION_CALLBACK;
Jeremy Klein9a36ec82016-01-22 14:11:45 -0800517
518 /**
Robert Greenwalt26744a52013-02-15 10:56:35 -0800519 * The absence of a connection type.
Robert Greenwalt33cdcdf2011-06-02 17:30:47 -0700520 * @hide
521 */
paulhu62af6122020-01-13 16:46:45 +0800522 @SystemApi
Robert Greenwalt33cdcdf2011-06-02 17:30:47 -0700523 public static final int TYPE_NONE = -1;
524
525 /**
Chalard Jean5acb7b72018-03-08 13:54:53 +0900526 * A Mobile data connection. Devices may support more than one.
527 *
528 * @deprecated Applications should instead use {@link NetworkCapabilities#hasTransport} or
529 * {@link #requestNetwork(NetworkRequest, NetworkCallback)} to request an
530 * appropriate network. {@see NetworkCapabilities} for supported transports.
Robert Greenwalt2034b912009-08-12 16:08:25 -0700531 */
Chalard Jean5acb7b72018-03-08 13:54:53 +0900532 @Deprecated
Robert Greenwalt2034b912009-08-12 16:08:25 -0700533 public static final int TYPE_MOBILE = 0;
Chalard Jean5acb7b72018-03-08 13:54:53 +0900534
Robert Greenwalt2034b912009-08-12 16:08:25 -0700535 /**
Chalard Jean5acb7b72018-03-08 13:54:53 +0900536 * A WIFI data connection. Devices may support more than one.
537 *
538 * @deprecated Applications should instead use {@link NetworkCapabilities#hasTransport} or
539 * {@link #requestNetwork(NetworkRequest, NetworkCallback)} to request an
540 * appropriate network. {@see NetworkCapabilities} for supported transports.
Robert Greenwalt2034b912009-08-12 16:08:25 -0700541 */
Chalard Jean5acb7b72018-03-08 13:54:53 +0900542 @Deprecated
Robert Greenwalt2034b912009-08-12 16:08:25 -0700543 public static final int TYPE_WIFI = 1;
Chalard Jean5acb7b72018-03-08 13:54:53 +0900544
Robert Greenwalt2034b912009-08-12 16:08:25 -0700545 /**
Robert Greenwalt26744a52013-02-15 10:56:35 -0800546 * An MMS-specific Mobile data connection. This network type may use the
547 * same network interface as {@link #TYPE_MOBILE} or it may use a different
548 * one. This is used by applications needing to talk to the carrier's
549 * Multimedia Messaging Service servers.
Lorenzo Colitti7cc32c52015-04-23 15:32:42 +0900550 *
Chalard Jean5acb7b72018-03-08 13:54:53 +0900551 * @deprecated Applications should instead use {@link NetworkCapabilities#hasCapability} or
Lorenzo Colittiefd396e2015-04-24 17:03:31 +0900552 * {@link #requestNetwork(NetworkRequest, NetworkCallback)} to request a network that
Lorenzo Colitti7cc32c52015-04-23 15:32:42 +0900553 * provides the {@link NetworkCapabilities#NET_CAPABILITY_MMS} capability.
Robert Greenwalt2034b912009-08-12 16:08:25 -0700554 */
Aurimas Liutikasa6bb0232016-05-24 15:22:55 -0700555 @Deprecated
Robert Greenwalt2034b912009-08-12 16:08:25 -0700556 public static final int TYPE_MOBILE_MMS = 2;
Chalard Jean5acb7b72018-03-08 13:54:53 +0900557
Robert Greenwalt2034b912009-08-12 16:08:25 -0700558 /**
Robert Greenwalt26744a52013-02-15 10:56:35 -0800559 * A SUPL-specific Mobile data connection. This network type may use the
560 * same network interface as {@link #TYPE_MOBILE} or it may use a different
561 * one. This is used by applications needing to talk to the carrier's
562 * Secure User Plane Location servers for help locating the device.
Lorenzo Colitti7cc32c52015-04-23 15:32:42 +0900563 *
Chalard Jean5acb7b72018-03-08 13:54:53 +0900564 * @deprecated Applications should instead use {@link NetworkCapabilities#hasCapability} or
Lorenzo Colittiefd396e2015-04-24 17:03:31 +0900565 * {@link #requestNetwork(NetworkRequest, NetworkCallback)} to request a network that
Lorenzo Colitti7cc32c52015-04-23 15:32:42 +0900566 * provides the {@link NetworkCapabilities#NET_CAPABILITY_SUPL} capability.
Robert Greenwalt2034b912009-08-12 16:08:25 -0700567 */
Aurimas Liutikasa6bb0232016-05-24 15:22:55 -0700568 @Deprecated
Robert Greenwalt2034b912009-08-12 16:08:25 -0700569 public static final int TYPE_MOBILE_SUPL = 3;
Chalard Jean5acb7b72018-03-08 13:54:53 +0900570
Robert Greenwalt2034b912009-08-12 16:08:25 -0700571 /**
Robert Greenwalt26744a52013-02-15 10:56:35 -0800572 * A DUN-specific Mobile data connection. This network type may use the
573 * same network interface as {@link #TYPE_MOBILE} or it may use a different
574 * one. This is sometimes by the system when setting up an upstream connection
575 * for tethering so that the carrier is aware of DUN traffic.
Chalard Jean5acb7b72018-03-08 13:54:53 +0900576 *
577 * @deprecated Applications should instead use {@link NetworkCapabilities#hasCapability} or
578 * {@link #requestNetwork(NetworkRequest, NetworkCallback)} to request a network that
579 * provides the {@link NetworkCapabilities#NET_CAPABILITY_DUN} capability.
Robert Greenwalt2034b912009-08-12 16:08:25 -0700580 */
Chalard Jean5acb7b72018-03-08 13:54:53 +0900581 @Deprecated
Robert Greenwalt2034b912009-08-12 16:08:25 -0700582 public static final int TYPE_MOBILE_DUN = 4;
Chalard Jean5acb7b72018-03-08 13:54:53 +0900583
Robert Greenwalt2034b912009-08-12 16:08:25 -0700584 /**
Robert Greenwalt26744a52013-02-15 10:56:35 -0800585 * A High Priority Mobile data connection. This network type uses the
586 * same network interface as {@link #TYPE_MOBILE} but the routing setup
Lorenzo Colitti7cc32c52015-04-23 15:32:42 +0900587 * is different.
588 *
Chalard Jean5acb7b72018-03-08 13:54:53 +0900589 * @deprecated Applications should instead use {@link NetworkCapabilities#hasTransport} or
590 * {@link #requestNetwork(NetworkRequest, NetworkCallback)} to request an
591 * appropriate network. {@see NetworkCapabilities} for supported transports.
Robert Greenwalt2034b912009-08-12 16:08:25 -0700592 */
Aurimas Liutikasa6bb0232016-05-24 15:22:55 -0700593 @Deprecated
Robert Greenwalt2034b912009-08-12 16:08:25 -0700594 public static final int TYPE_MOBILE_HIPRI = 5;
Chalard Jean5acb7b72018-03-08 13:54:53 +0900595
jshbfa81722010-03-11 15:04:43 -0800596 /**
Chalard Jean5acb7b72018-03-08 13:54:53 +0900597 * A WiMAX data connection.
598 *
599 * @deprecated Applications should instead use {@link NetworkCapabilities#hasTransport} or
600 * {@link #requestNetwork(NetworkRequest, NetworkCallback)} to request an
601 * appropriate network. {@see NetworkCapabilities} for supported transports.
jshbfa81722010-03-11 15:04:43 -0800602 */
Chalard Jean5acb7b72018-03-08 13:54:53 +0900603 @Deprecated
jshbfa81722010-03-11 15:04:43 -0800604 public static final int TYPE_WIMAX = 6;
Robert Greenwalteb123ac2010-12-06 13:56:24 -0800605
Jaikumar Ganesh0db51a02010-12-21 22:31:44 -0800606 /**
Chalard Jean5acb7b72018-03-08 13:54:53 +0900607 * A Bluetooth data connection.
608 *
609 * @deprecated Applications should instead use {@link NetworkCapabilities#hasTransport} or
610 * {@link #requestNetwork(NetworkRequest, NetworkCallback)} to request an
611 * appropriate network. {@see NetworkCapabilities} for supported transports.
Jaikumar Ganesh0db51a02010-12-21 22:31:44 -0800612 */
Chalard Jean5acb7b72018-03-08 13:54:53 +0900613 @Deprecated
Jaikumar Ganesh0db51a02010-12-21 22:31:44 -0800614 public static final int TYPE_BLUETOOTH = 7;
615
Robert Greenwaltf76c55d2011-04-22 15:28:18 -0700616 /**
Chiachang Wang7d5f3782020-07-28 13:53:09 +0800617 * Fake data connection. This should not be used on shipping devices.
Chalard Jean5acb7b72018-03-08 13:54:53 +0900618 * @deprecated This is not used any more.
Robert Greenwaltf76c55d2011-04-22 15:28:18 -0700619 */
Chalard Jean5acb7b72018-03-08 13:54:53 +0900620 @Deprecated
Jaikumar Ganesh0db51a02010-12-21 22:31:44 -0800621 public static final int TYPE_DUMMY = 8;
Wink Savilleb7c92c72011-03-12 14:52:01 -0800622
Robert Greenwaltf76c55d2011-04-22 15:28:18 -0700623 /**
Chalard Jean5acb7b72018-03-08 13:54:53 +0900624 * An Ethernet data connection.
625 *
626 * @deprecated Applications should instead use {@link NetworkCapabilities#hasTransport} or
627 * {@link #requestNetwork(NetworkRequest, NetworkCallback)} to request an
628 * appropriate network. {@see NetworkCapabilities} for supported transports.
Robert Greenwaltf76c55d2011-04-22 15:28:18 -0700629 */
Chalard Jean5acb7b72018-03-08 13:54:53 +0900630 @Deprecated
Robert Greenwalt9d077812011-01-28 14:48:37 -0800631 public static final int TYPE_ETHERNET = 9;
Robert Greenwaltf76c55d2011-04-22 15:28:18 -0700632
Wink Savilleb7c92c72011-03-12 14:52:01 -0800633 /**
Robert Greenwalt26744a52013-02-15 10:56:35 -0800634 * Over the air Administration.
Chalard Jean5acb7b72018-03-08 13:54:53 +0900635 * @deprecated Use {@link NetworkCapabilities} instead.
Wink Savilleb7c92c72011-03-12 14:52:01 -0800636 * {@hide}
637 */
Chalard Jean5acb7b72018-03-08 13:54:53 +0900638 @Deprecated
Chalard Jeanaa91c9d2019-04-09 15:46:21 +0900639 @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P, trackingBug = 130143562)
Wink Savilleb7c92c72011-03-12 14:52:01 -0800640 public static final int TYPE_MOBILE_FOTA = 10;
641
642 /**
Robert Greenwalt26744a52013-02-15 10:56:35 -0800643 * IP Multimedia Subsystem.
Chalard Jean5acb7b72018-03-08 13:54:53 +0900644 * @deprecated Use {@link NetworkCapabilities#NET_CAPABILITY_IMS} instead.
Wink Savilleb7c92c72011-03-12 14:52:01 -0800645 * {@hide}
646 */
Chalard Jean5acb7b72018-03-08 13:54:53 +0900647 @Deprecated
Mathew Inwood0b8f8612018-08-08 14:52:47 +0100648 @UnsupportedAppUsage
Wink Savilleb7c92c72011-03-12 14:52:01 -0800649 public static final int TYPE_MOBILE_IMS = 11;
650
651 /**
Robert Greenwalt26744a52013-02-15 10:56:35 -0800652 * Carrier Branded Services.
Chalard Jean5acb7b72018-03-08 13:54:53 +0900653 * @deprecated Use {@link NetworkCapabilities#NET_CAPABILITY_CBS} instead.
Wink Savilleb7c92c72011-03-12 14:52:01 -0800654 * {@hide}
655 */
Chalard Jean5acb7b72018-03-08 13:54:53 +0900656 @Deprecated
Chalard Jeanaa91c9d2019-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_CBS = 12;
659
repo syncf5de5572011-07-29 23:55:49 -0700660 /**
661 * A Wi-Fi p2p connection. Only requesting processes will have access to
662 * the peers connected.
Chalard Jean5acb7b72018-03-08 13:54:53 +0900663 * @deprecated Use {@link NetworkCapabilities#NET_CAPABILITY_WIFI_P2P} instead.
repo syncf5de5572011-07-29 23:55:49 -0700664 * {@hide}
665 */
Chalard Jean5acb7b72018-03-08 13:54:53 +0900666 @Deprecated
paulhu028732e2020-01-15 15:38:23 +0800667 @SystemApi
repo syncf5de5572011-07-29 23:55:49 -0700668 public static final int TYPE_WIFI_P2P = 13;
Wink Savilleb7c92c72011-03-12 14:52:01 -0800669
Wink Saville70dbdcc2013-07-29 15:00:57 -0700670 /**
671 * The network to use for initially attaching to the network
Chalard Jean5acb7b72018-03-08 13:54:53 +0900672 * @deprecated Use {@link NetworkCapabilities#NET_CAPABILITY_IA} instead.
Wink Saville70dbdcc2013-07-29 15:00:57 -0700673 * {@hide}
674 */
Chalard Jean5acb7b72018-03-08 13:54:53 +0900675 @Deprecated
Mathew Inwood0b8f8612018-08-08 14:52:47 +0100676 @UnsupportedAppUsage
Wink Saville70dbdcc2013-07-29 15:00:57 -0700677 public static final int TYPE_MOBILE_IA = 14;
repo syncf5de5572011-07-29 23:55:49 -0700678
Lorenzo Colitti7cc32c52015-04-23 15:32:42 +0900679 /**
Robert Greenwalt9db5f3c2015-07-09 14:49:35 -0700680 * Emergency PDN connection for emergency services. This
681 * may include IMS and MMS in emergency situations.
Chalard Jean5acb7b72018-03-08 13:54:53 +0900682 * @deprecated Use {@link NetworkCapabilities#NET_CAPABILITY_EIMS} instead.
Ram201d45f2014-06-26 11:03:44 -0700683 * {@hide}
684 */
Chalard Jean5acb7b72018-03-08 13:54:53 +0900685 @Deprecated
Chalard Jeanaa91c9d2019-04-09 15:46:21 +0900686 @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P, trackingBug = 130143562)
Ram201d45f2014-06-26 11:03:44 -0700687 public static final int TYPE_MOBILE_EMERGENCY = 15;
688
Hui Lu07f29332014-01-15 11:05:36 -0500689 /**
690 * The network that uses proxy to achieve connectivity.
Chalard Jean5acb7b72018-03-08 13:54:53 +0900691 * @deprecated Use {@link NetworkCapabilities} instead.
Hui Lu07f29332014-01-15 11:05:36 -0500692 * {@hide}
693 */
Chalard Jean5acb7b72018-03-08 13:54:53 +0900694 @Deprecated
Remi NGUYEN VANdaeafee2020-11-30 19:23:45 +0900695 @SystemApi
Hui Lu07f29332014-01-15 11:05:36 -0500696 public static final int TYPE_PROXY = 16;
Wink Saville70dbdcc2013-07-29 15:00:57 -0700697
Robert Greenwaltb2a03d12014-07-07 17:09:01 -0700698 /**
699 * A virtual network using one or more native bearers.
700 * It may or may not be providing security services.
Chalard Jean5acb7b72018-03-08 13:54:53 +0900701 * @deprecated Applications should use {@link NetworkCapabilities#TRANSPORT_VPN} instead.
Robert Greenwaltb2a03d12014-07-07 17:09:01 -0700702 */
Chalard Jean5acb7b72018-03-08 13:54:53 +0900703 @Deprecated
Robert Greenwaltb2a03d12014-07-07 17:09:01 -0700704 public static final int TYPE_VPN = 17;
Hui Lu07f29332014-01-15 11:05:36 -0500705
Benedict Wong80156022018-11-14 17:40:55 -0800706 /**
707 * A network that is exclusively meant to be used for testing
708 *
709 * @deprecated Use {@link NetworkCapabilities} instead.
710 * @hide
711 */
712 @Deprecated
713 public static final int TYPE_TEST = 18; // TODO: Remove this once NetworkTypes are unused.
Robert Greenwaltb2a03d12014-07-07 17:09:01 -0700714
Chalard Jeane5b9e822020-03-25 01:24:04 +0900715 /**
716 * @deprecated Use {@link NetworkCapabilities} instead.
717 * @hide
718 */
719 @Deprecated
720 @Retention(RetentionPolicy.SOURCE)
721 @IntDef(prefix = { "TYPE_" }, value = {
722 TYPE_NONE,
723 TYPE_MOBILE,
724 TYPE_WIFI,
725 TYPE_MOBILE_MMS,
726 TYPE_MOBILE_SUPL,
727 TYPE_MOBILE_DUN,
728 TYPE_MOBILE_HIPRI,
729 TYPE_WIMAX,
730 TYPE_BLUETOOTH,
731 TYPE_DUMMY,
732 TYPE_ETHERNET,
733 TYPE_MOBILE_FOTA,
734 TYPE_MOBILE_IMS,
735 TYPE_MOBILE_CBS,
736 TYPE_WIFI_P2P,
737 TYPE_MOBILE_IA,
738 TYPE_MOBILE_EMERGENCY,
739 TYPE_PROXY,
740 TYPE_VPN,
741 TYPE_TEST
742 })
743 public @interface LegacyNetworkType {}
744
Chalard Jean1f42df12019-11-21 14:48:00 +0900745 // Deprecated constants for return values of startUsingNetworkFeature. They used to live
746 // in com.android.internal.telephony.PhoneConstants until they were made inaccessible.
747 private static final int DEPRECATED_PHONE_CONSTANT_APN_ALREADY_ACTIVE = 0;
748 private static final int DEPRECATED_PHONE_CONSTANT_APN_REQUEST_STARTED = 1;
749 private static final int DEPRECATED_PHONE_CONSTANT_APN_REQUEST_FAILED = 3;
750
Robert Greenwaltb2a03d12014-07-07 17:09:01 -0700751 /** {@hide} */
Benedict Wong80156022018-11-14 17:40:55 -0800752 public static final int MAX_RADIO_TYPE = TYPE_TEST;
753
754 /** {@hide} */
755 public static final int MAX_NETWORK_TYPE = TYPE_TEST;
The Android Open Source Project28527d22009-03-03 19:31:44 -0800756
Hugo Benichi37d5c3c2017-06-20 14:07:59 +0900757 private static final int MIN_NETWORK_TYPE = TYPE_MOBILE;
758
Jianzheng Zhoua8aa1602012-11-16 13:45:20 +0800759 /**
760 * If you want to set the default network preference,you can directly
761 * change the networkAttributes array in framework's config.xml.
762 *
763 * @deprecated Since we support so many more networks now, the single
764 * network default network preference can't really express
Robert Greenwalt26744a52013-02-15 10:56:35 -0800765 * the hierarchy. Instead, the default is defined by the
Jianzheng Zhoua8aa1602012-11-16 13:45:20 +0800766 * networkAttributes in config.xml. You can determine
Robert Greenwalt1ee3d2c2012-12-07 09:56:50 -0800767 * the current value by calling {@link #getNetworkPreference()}
Jianzheng Zhoua8aa1602012-11-16 13:45:20 +0800768 * from an App.
769 */
770 @Deprecated
The Android Open Source Project28527d22009-03-03 19:31:44 -0800771 public static final int DEFAULT_NETWORK_PREFERENCE = TYPE_WIFI;
772
Jeff Sharkey5eccd9c2012-09-26 22:03:49 -0700773 /**
Robert Greenwalta1402df2014-03-19 17:56:12 -0700774 * @hide
775 */
Hugo Benichibcc81df2017-06-20 14:10:14 +0900776 public static final int REQUEST_ID_UNSET = 0;
Robert Greenwaltc34f83b2014-06-08 16:42:59 -0700777
Paul Jensen0478ace2014-07-11 12:28:19 -0400778 /**
Hugo Benichi1c027fe2017-06-17 13:14:12 +0900779 * Static unique request used as a tombstone for NetworkCallbacks that have been unregistered.
780 * This allows to distinguish when unregistering NetworkCallbacks those that were never
Chalard Jean73d9db72018-06-04 16:52:49 +0900781 * registered from those that were already unregistered.
Hugo Benichi1c027fe2017-06-17 13:14:12 +0900782 * @hide
783 */
Hugo Benichibcc81df2017-06-20 14:10:14 +0900784 private static final NetworkRequest ALREADY_UNREGISTERED =
Hugo Benichi1c027fe2017-06-17 13:14:12 +0900785 new NetworkRequest.Builder().clearCapabilities().build();
786
787 /**
Paul Jensen0478ace2014-07-11 12:28:19 -0400788 * A NetID indicating no Network is selected.
789 * Keep in sync with bionic/libc/dns/include/resolv_netid.h
790 * @hide
791 */
792 public static final int NETID_UNSET = 0;
793
Erik Kline1ecdd962017-10-30 15:29:44 +0900794 /**
795 * Private DNS Mode values.
796 *
797 * The "private_dns_mode" global setting stores a String value which is
798 * expected to be one of the following.
799 */
800
801 /**
802 * @hide
803 */
804 public static final String PRIVATE_DNS_MODE_OFF = "off";
805 /**
806 * @hide
807 */
808 public static final String PRIVATE_DNS_MODE_OPPORTUNISTIC = "opportunistic";
809 /**
810 * @hide
811 */
812 public static final String PRIVATE_DNS_MODE_PROVIDER_HOSTNAME = "hostname";
813 /**
814 * The default Private DNS mode.
815 *
816 * This may change from release to release or may become dependent upon
817 * the capabilities of the underlying platform.
818 *
819 * @hide
820 */
Erik Klineae1a3af2018-05-16 16:41:57 +0900821 public static final String PRIVATE_DNS_DEFAULT_MODE_FALLBACK = PRIVATE_DNS_MODE_OPPORTUNISTIC;
Erik Kline1ecdd962017-10-30 15:29:44 +0900822
Chalard Jeanaa91c9d2019-04-09 15:46:21 +0900823 @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P, trackingBug = 130143562)
Jeff Sharkey921ebf22011-05-19 17:12:49 -0700824 private final IConnectivityManager mService;
Lorenzo Colitti0dd4b6f2021-02-04 17:32:07 +0900825
Paul Jensen12131352014-12-10 15:12:18 -0500826 /**
827 * A kludge to facilitate static access where a Context pointer isn't available, like in the
828 * case of the static set/getProcessDefaultNetwork methods and from the Network class.
829 * TODO: Remove this after deprecating the static methods in favor of non-static methods or
830 * methods that take a Context argument.
831 */
832 private static ConnectivityManager sInstance;
The Android Open Source Project28527d22009-03-03 19:31:44 -0800833
Lorenzo Colitti9b026fa2015-07-29 11:41:21 +0900834 private final Context mContext;
835
Felipe Lemed16384b2016-01-22 09:44:57 -0800836 private INetworkPolicyManager mNPManager;
Amos Bianchiddadca42020-03-04 11:07:38 -0800837 private final TetheringManager mTetheringManager;
Dianne Hackbornb4b09e82014-02-26 16:20:52 -0800838
Robert Greenwalt26744a52013-02-15 10:56:35 -0800839 /**
840 * Tests if a given integer represents a valid network type.
841 * @param networkType the type to be tested
842 * @return a boolean. {@code true} if the type is valid, else {@code false}
Paul Jensen1c9f2e42015-05-06 10:42:25 -0400843 * @deprecated All APIs accepting a network type are deprecated. There should be no need to
844 * validate a network type.
Robert Greenwalt26744a52013-02-15 10:56:35 -0800845 */
Aurimas Liutikasa6bb0232016-05-24 15:22:55 -0700846 @Deprecated
Jeff Sharkey21062e72011-05-28 20:56:34 -0700847 public static boolean isNetworkTypeValid(int networkType) {
Hugo Benichi37d5c3c2017-06-20 14:07:59 +0900848 return MIN_NETWORK_TYPE <= networkType && networkType <= MAX_NETWORK_TYPE;
The Android Open Source Project28527d22009-03-03 19:31:44 -0800849 }
850
Robert Greenwalt26744a52013-02-15 10:56:35 -0800851 /**
852 * Returns a non-localized string representing a given network type.
853 * ONLY used for debugging output.
854 * @param type the type needing naming
855 * @return a String for the given type, or a string version of the type ("87")
856 * if no name is known.
Chalard Jean5acb7b72018-03-08 13:54:53 +0900857 * @deprecated Types are deprecated. Use {@link NetworkCapabilities} instead.
Robert Greenwalt26744a52013-02-15 10:56:35 -0800858 * {@hide}
859 */
Chalard Jean5acb7b72018-03-08 13:54:53 +0900860 @Deprecated
Mathew Inwood5a09a712020-11-04 09:29:36 +0000861 @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.R, trackingBug = 170729553)
Jeff Sharkey21062e72011-05-28 20:56:34 -0700862 public static String getNetworkTypeName(int type) {
863 switch (type) {
Hugo Benichi37d5c3c2017-06-20 14:07:59 +0900864 case TYPE_NONE:
865 return "NONE";
Jeff Sharkey21062e72011-05-28 20:56:34 -0700866 case TYPE_MOBILE:
867 return "MOBILE";
868 case TYPE_WIFI:
869 return "WIFI";
870 case TYPE_MOBILE_MMS:
871 return "MOBILE_MMS";
872 case TYPE_MOBILE_SUPL:
873 return "MOBILE_SUPL";
874 case TYPE_MOBILE_DUN:
875 return "MOBILE_DUN";
876 case TYPE_MOBILE_HIPRI:
877 return "MOBILE_HIPRI";
878 case TYPE_WIMAX:
879 return "WIMAX";
880 case TYPE_BLUETOOTH:
881 return "BLUETOOTH";
882 case TYPE_DUMMY:
883 return "DUMMY";
884 case TYPE_ETHERNET:
885 return "ETHERNET";
886 case TYPE_MOBILE_FOTA:
887 return "MOBILE_FOTA";
888 case TYPE_MOBILE_IMS:
889 return "MOBILE_IMS";
890 case TYPE_MOBILE_CBS:
891 return "MOBILE_CBS";
repo syncf5de5572011-07-29 23:55:49 -0700892 case TYPE_WIFI_P2P:
893 return "WIFI_P2P";
Wink Saville70dbdcc2013-07-29 15:00:57 -0700894 case TYPE_MOBILE_IA:
895 return "MOBILE_IA";
Ram201d45f2014-06-26 11:03:44 -0700896 case TYPE_MOBILE_EMERGENCY:
897 return "MOBILE_EMERGENCY";
Hui Lu07f29332014-01-15 11:05:36 -0500898 case TYPE_PROXY:
899 return "PROXY";
Erik Klineb1ff7002014-11-19 17:23:41 +0900900 case TYPE_VPN:
901 return "VPN";
Jeff Sharkey21062e72011-05-28 20:56:34 -0700902 default:
903 return Integer.toString(type);
904 }
905 }
906
Robert Greenwalt26744a52013-02-15 10:56:35 -0800907 /**
Aaron Huang925d09b2020-06-27 07:18:23 +0800908 * @hide
909 * TODO: Expose for SystemServer when becomes a module.
910 */
911 public void systemReady() {
912 try {
913 mService.systemReady();
914 } catch (RemoteException e) {
915 throw e.rethrowFromSystemServer();
916 }
917 }
918
919 /**
Robert Greenwalt26744a52013-02-15 10:56:35 -0800920 * Checks if a given type uses the cellular data connection.
921 * This should be replaced in the future by a network property.
922 * @param networkType the type to check
923 * @return a boolean - {@code true} if uses cellular network, else {@code false}
Chalard Jean5acb7b72018-03-08 13:54:53 +0900924 * @deprecated Types are deprecated. Use {@link NetworkCapabilities} instead.
Robert Greenwalt26744a52013-02-15 10:56:35 -0800925 * {@hide}
926 */
Chalard Jean5acb7b72018-03-08 13:54:53 +0900927 @Deprecated
Chalard Jeanaa91c9d2019-04-09 15:46:21 +0900928 @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P, trackingBug = 130143562)
Jeff Sharkey21062e72011-05-28 20:56:34 -0700929 public static boolean isNetworkTypeMobile(int networkType) {
930 switch (networkType) {
931 case TYPE_MOBILE:
932 case TYPE_MOBILE_MMS:
933 case TYPE_MOBILE_SUPL:
934 case TYPE_MOBILE_DUN:
935 case TYPE_MOBILE_HIPRI:
936 case TYPE_MOBILE_FOTA:
937 case TYPE_MOBILE_IMS:
938 case TYPE_MOBILE_CBS:
Wink Saville70dbdcc2013-07-29 15:00:57 -0700939 case TYPE_MOBILE_IA:
Ram201d45f2014-06-26 11:03:44 -0700940 case TYPE_MOBILE_EMERGENCY:
Jeff Sharkey21062e72011-05-28 20:56:34 -0700941 return true;
942 default:
943 return false;
944 }
945 }
946
Robert Greenwalt26744a52013-02-15 10:56:35 -0800947 /**
Jeff Sharkeye9bda1d2013-06-04 12:29:00 -0700948 * Checks if the given network type is backed by a Wi-Fi radio.
949 *
Chalard Jean5acb7b72018-03-08 13:54:53 +0900950 * @deprecated Types are deprecated. Use {@link NetworkCapabilities} instead.
Jeff Sharkeye9bda1d2013-06-04 12:29:00 -0700951 * @hide
952 */
Chalard Jean5acb7b72018-03-08 13:54:53 +0900953 @Deprecated
Jeff Sharkeye9bda1d2013-06-04 12:29:00 -0700954 public static boolean isNetworkTypeWifi(int networkType) {
955 switch (networkType) {
956 case TYPE_WIFI:
957 case TYPE_WIFI_P2P:
958 return true;
959 default:
960 return false;
961 }
962 }
963
964 /**
Robert Greenwalt26744a52013-02-15 10:56:35 -0800965 * Specifies the preferred network type. When the device has more
966 * than one type available the preferred network type will be used.
Robert Greenwalt26744a52013-02-15 10:56:35 -0800967 *
968 * @param preference the network type to prefer over all others. It is
969 * unspecified what happens to the old preferred network in the
970 * overall ordering.
Robert Greenwaltd0ebdbb2014-05-18 23:07:25 -0700971 * @deprecated Functionality has been removed as it no longer makes sense,
972 * with many more than two networks - we'd need an array to express
973 * preference. Instead we use dynamic network properties of
974 * the networks to describe their precedence.
Robert Greenwalt26744a52013-02-15 10:56:35 -0800975 */
Aurimas Liutikasa6bb0232016-05-24 15:22:55 -0700976 @Deprecated
The Android Open Source Project28527d22009-03-03 19:31:44 -0800977 public void setNetworkPreference(int preference) {
The Android Open Source Project28527d22009-03-03 19:31:44 -0800978 }
979
Robert Greenwalt26744a52013-02-15 10:56:35 -0800980 /**
981 * Retrieves the current preferred network type.
Robert Greenwalt26744a52013-02-15 10:56:35 -0800982 *
983 * @return an integer representing the preferred network type
984 *
Robert Greenwaltd0ebdbb2014-05-18 23:07:25 -0700985 * @deprecated Functionality has been removed as it no longer makes sense,
986 * with many more than two networks - we'd need an array to express
987 * preference. Instead we use dynamic network properties of
988 * the networks to describe their precedence.
Robert Greenwalt26744a52013-02-15 10:56:35 -0800989 */
Aurimas Liutikasa6bb0232016-05-24 15:22:55 -0700990 @Deprecated
Jeff Sharkey2ac62992017-04-24 11:18:03 -0600991 @RequiresPermission(android.Manifest.permission.ACCESS_NETWORK_STATE)
The Android Open Source Project28527d22009-03-03 19:31:44 -0800992 public int getNetworkPreference() {
Robert Greenwaltd0ebdbb2014-05-18 23:07:25 -0700993 return TYPE_NONE;
The Android Open Source Project28527d22009-03-03 19:31:44 -0800994 }
995
Scott Mainf58b7d82011-10-06 19:02:28 -0700996 /**
Robert Greenwalt26744a52013-02-15 10:56:35 -0800997 * Returns details about the currently active default data network. When
998 * connected, this network is the default route for outgoing connections.
999 * You should always check {@link NetworkInfo#isConnected()} before initiating
1000 * network traffic. This may return {@code null} when there is no default
1001 * network.
Chalard Jean7c85ba42018-03-29 17:45:24 +09001002 * Note that if the default network is a VPN, this method will return the
1003 * NetworkInfo for one of its underlying networks instead, or null if the
1004 * VPN agent did not specify any. Apps interested in learning about VPNs
1005 * should use {@link #getNetworkInfo(android.net.Network)} instead.
Robert Greenwalt26744a52013-02-15 10:56:35 -08001006 *
1007 * @return a {@link NetworkInfo} object for the current default network
Paul Jensena9208b92015-02-13 14:18:39 -05001008 * or {@code null} if no default network is currently active
junyulai5c2f6262018-12-13 12:47:51 +08001009 * @deprecated See {@link NetworkInfo}.
Jeff Sharkeyd00b1302012-04-12 18:34:54 -07001010 */
junyulai5c2f6262018-12-13 12:47:51 +08001011 @Deprecated
Jeff Sharkey2ac62992017-04-24 11:18:03 -06001012 @RequiresPermission(android.Manifest.permission.ACCESS_NETWORK_STATE)
Chalard Jean42a9c292019-01-07 19:26:34 +09001013 @Nullable
The Android Open Source Project28527d22009-03-03 19:31:44 -08001014 public NetworkInfo getActiveNetworkInfo() {
1015 try {
1016 return mService.getActiveNetworkInfo();
1017 } catch (RemoteException e) {
Jeff Sharkeya6066fd2016-03-01 19:27:23 -07001018 throw e.rethrowFromSystemServer();
The Android Open Source Project28527d22009-03-03 19:31:44 -08001019 }
1020 }
1021
Robert Greenwalt26744a52013-02-15 10:56:35 -08001022 /**
Paul Jensenc2569432015-02-13 14:18:39 -05001023 * Returns a {@link Network} object corresponding to the currently active
1024 * default data network. In the event that the current active default data
1025 * network disconnects, the returned {@code Network} object will no longer
1026 * be usable. This will return {@code null} when there is no default
1027 * network.
1028 *
1029 * @return a {@link Network} object for the current default network or
1030 * {@code null} if no default network is currently active
Paul Jensenc2569432015-02-13 14:18:39 -05001031 */
Jeff Sharkey2ac62992017-04-24 11:18:03 -06001032 @RequiresPermission(android.Manifest.permission.ACCESS_NETWORK_STATE)
Chalard Jean42a9c292019-01-07 19:26:34 +09001033 @Nullable
Paul Jensenc2569432015-02-13 14:18:39 -05001034 public Network getActiveNetwork() {
1035 try {
1036 return mService.getActiveNetwork();
1037 } catch (RemoteException e) {
Jeff Sharkeya6066fd2016-03-01 19:27:23 -07001038 throw e.rethrowFromSystemServer();
Paul Jensenc2569432015-02-13 14:18:39 -05001039 }
1040 }
1041
1042 /**
Robin Leeda4d2e22016-03-24 12:07:00 +00001043 * Returns a {@link Network} object corresponding to the currently active
1044 * default data network for a specific UID. In the event that the default data
1045 * network disconnects, the returned {@code Network} object will no longer
1046 * be usable. This will return {@code null} when there is no default
1047 * network for the UID.
Robin Leeda4d2e22016-03-24 12:07:00 +00001048 *
1049 * @return a {@link Network} object for the current default network for the
1050 * given UID or {@code null} if no default network is currently active
1051 *
1052 * @hide
1053 */
paulhud70b7dd2019-08-12 16:25:11 +08001054 @RequiresPermission(android.Manifest.permission.NETWORK_STACK)
Chalard Jean42a9c292019-01-07 19:26:34 +09001055 @Nullable
Robin Leeda4d2e22016-03-24 12:07:00 +00001056 public Network getActiveNetworkForUid(int uid) {
Jeff Sharkey833c35b2016-04-28 15:33:18 -06001057 return getActiveNetworkForUid(uid, false);
1058 }
1059
1060 /** {@hide} */
1061 public Network getActiveNetworkForUid(int uid, boolean ignoreBlocked) {
Robin Leeda4d2e22016-03-24 12:07:00 +00001062 try {
Jeff Sharkey833c35b2016-04-28 15:33:18 -06001063 return mService.getActiveNetworkForUid(uid, ignoreBlocked);
Robin Leeda4d2e22016-03-24 12:07:00 +00001064 } catch (RemoteException e) {
1065 throw e.rethrowFromSystemServer();
1066 }
1067 }
1068
1069 /**
Lorenzo Colittie8ce2052020-12-12 00:51:11 +09001070 * Adds or removes a requirement for given UID ranges to use the VPN.
1071 *
1072 * If set to {@code true}, informs the system that the UIDs in the specified ranges must not
1073 * have any connectivity except if a VPN is connected and applies to the UIDs, or if the UIDs
1074 * otherwise have permission to bypass the VPN (e.g., because they have the
1075 * {@link android.Manifest.permission.CONNECTIVITY_USE_RESTRICTED_NETWORKS} permission, or when
1076 * using a socket protected by a method such as {@link VpnService#protect(DatagramSocket)}. If
1077 * set to {@code false}, a previously-added restriction is removed.
1078 * <p>
1079 * Each of the UID ranges specified by this method is added and removed as is, and no processing
1080 * is performed on the ranges to de-duplicate, merge, split, or intersect them. In order to
1081 * remove a previously-added range, the exact range must be removed as is.
1082 * <p>
1083 * The changes are applied asynchronously and may not have been applied by the time the method
1084 * returns. Apps will be notified about any changes that apply to them via
1085 * {@link NetworkCallback#onBlockedStatusChanged} callbacks called after the changes take
1086 * effect.
1087 * <p>
1088 * This method should be called only by the VPN code.
1089 *
1090 * @param ranges the UID ranges to restrict
1091 * @param requireVpn whether the specified UID ranges must use a VPN
1092 *
1093 * TODO: expose as @SystemApi.
1094 * @hide
1095 */
1096 @RequiresPermission(anyOf = {
1097 NetworkStack.PERMISSION_MAINLINE_NETWORK_STACK,
1098 android.Manifest.permission.NETWORK_STACK})
1099 public void setRequireVpnForUids(boolean requireVpn,
1100 @NonNull Collection<Range<Integer>> ranges) {
1101 Objects.requireNonNull(ranges);
1102 // The Range class is not parcelable. Convert to UidRange, which is what is used internally.
1103 // This method is not necessarily expected to be used outside the system server, so
1104 // parceling may not be necessary, but it could be used out-of-process, e.g., by the network
1105 // stack process, or by tests.
1106 UidRange[] rangesArray = new UidRange[ranges.size()];
1107 int index = 0;
1108 for (Range<Integer> range : ranges) {
1109 rangesArray[index++] = new UidRange(range.getLower(), range.getUpper());
1110 }
1111 try {
1112 mService.setRequireVpnForUids(requireVpn, rangesArray);
1113 } catch (RemoteException e) {
1114 throw e.rethrowFromSystemServer();
1115 }
1116 }
1117
1118 /**
Lorenzo Colitti549e9ac2021-01-15 01:29:01 +09001119 * Informs ConnectivityService of whether the legacy lockdown VPN, as implemented by
1120 * LockdownVpnTracker, is in use. This is deprecated for new devices starting from Android 12
1121 * but is still supported for backwards compatibility.
1122 * <p>
1123 * This type of VPN is assumed always to use the system default network, and must always declare
1124 * exactly one underlying network, which is the network that was the default when the VPN
1125 * connected.
1126 * <p>
1127 * Calling this method with {@code true} enables legacy behaviour, specifically:
1128 * <ul>
1129 * <li>Any VPN that applies to userId 0 behaves specially with respect to deprecated
1130 * {@link #CONNECTIVITY_ACTION} broadcasts. Any such broadcasts will have the state in the
1131 * {@link #EXTRA_NETWORK_INFO} replaced by state of the VPN network. Also, any time the VPN
1132 * connects, a {@link #CONNECTIVITY_ACTION} broadcast will be sent for the network
1133 * underlying the VPN.</li>
1134 * <li>Deprecated APIs that return {@link NetworkInfo} objects will have their state
1135 * similarly replaced by the VPN network state.</li>
1136 * <li>Information on current network interfaces passed to NetworkStatsService will not
1137 * include any VPN interfaces.</li>
1138 * </ul>
1139 *
1140 * @param enabled whether legacy lockdown VPN is enabled or disabled
1141 *
1142 * TODO: @SystemApi(client = MODULE_LIBRARIES)
1143 *
1144 * @hide
1145 */
1146 @RequiresPermission(anyOf = {
1147 NetworkStack.PERMISSION_MAINLINE_NETWORK_STACK,
1148 android.Manifest.permission.NETWORK_SETTINGS})
1149 public void setLegacyLockdownVpnEnabled(boolean enabled) {
1150 try {
1151 mService.setLegacyLockdownVpnEnabled(enabled);
1152 } catch (RemoteException e) {
1153 throw e.rethrowFromSystemServer();
1154 }
1155 }
1156
1157 /**
Robert Greenwalt26744a52013-02-15 10:56:35 -08001158 * Returns details about the currently active default data network
1159 * for a given uid. This is for internal use only to avoid spying
1160 * other apps.
1161 *
1162 * @return a {@link NetworkInfo} object for the current default network
1163 * for the given uid or {@code null} if no default network is
1164 * available for the specified uid.
1165 *
Robert Greenwalt26744a52013-02-15 10:56:35 -08001166 * {@hide}
1167 */
paulhud70b7dd2019-08-12 16:25:11 +08001168 @RequiresPermission(android.Manifest.permission.NETWORK_STACK)
Mathew Inwood5a09a712020-11-04 09:29:36 +00001169 @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.R, trackingBug = 170729553)
Jeff Sharkey921ebf22011-05-19 17:12:49 -07001170 public NetworkInfo getActiveNetworkInfoForUid(int uid) {
Jeff Sharkey833c35b2016-04-28 15:33:18 -06001171 return getActiveNetworkInfoForUid(uid, false);
1172 }
1173
1174 /** {@hide} */
1175 public NetworkInfo getActiveNetworkInfoForUid(int uid, boolean ignoreBlocked) {
Jeff Sharkey921ebf22011-05-19 17:12:49 -07001176 try {
Jeff Sharkey833c35b2016-04-28 15:33:18 -06001177 return mService.getActiveNetworkInfoForUid(uid, ignoreBlocked);
Jeff Sharkey921ebf22011-05-19 17:12:49 -07001178 } catch (RemoteException e) {
Jeff Sharkeya6066fd2016-03-01 19:27:23 -07001179 throw e.rethrowFromSystemServer();
Jeff Sharkey921ebf22011-05-19 17:12:49 -07001180 }
1181 }
1182
Robert Greenwalt26744a52013-02-15 10:56:35 -08001183 /**
1184 * Returns connection status information about a particular
1185 * network type.
1186 *
1187 * @param networkType integer specifying which networkType in
1188 * which you're interested.
1189 * @return a {@link NetworkInfo} object for the requested
1190 * network type or {@code null} if the type is not
Chalard Jean7c85ba42018-03-29 17:45:24 +09001191 * supported by the device. If {@code networkType} is
1192 * TYPE_VPN and a VPN is active for the calling app,
1193 * then this method will try to return one of the
1194 * underlying networks for the VPN or null if the
1195 * VPN agent didn't specify any.
Robert Greenwalt26744a52013-02-15 10:56:35 -08001196 *
Paul Jensena8e6dc62015-03-18 12:23:02 -04001197 * @deprecated This method does not support multiple connected networks
1198 * of the same type. Use {@link #getAllNetworks} and
1199 * {@link #getNetworkInfo(android.net.Network)} instead.
Robert Greenwalt26744a52013-02-15 10:56:35 -08001200 */
Aurimas Liutikasa6bb0232016-05-24 15:22:55 -07001201 @Deprecated
Jeff Sharkey2ac62992017-04-24 11:18:03 -06001202 @RequiresPermission(android.Manifest.permission.ACCESS_NETWORK_STATE)
Chalard Jean42a9c292019-01-07 19:26:34 +09001203 @Nullable
The Android Open Source Project28527d22009-03-03 19:31:44 -08001204 public NetworkInfo getNetworkInfo(int networkType) {
1205 try {
1206 return mService.getNetworkInfo(networkType);
1207 } catch (RemoteException e) {
Jeff Sharkeya6066fd2016-03-01 19:27:23 -07001208 throw e.rethrowFromSystemServer();
The Android Open Source Project28527d22009-03-03 19:31:44 -08001209 }
1210 }
1211
Robert Greenwalt26744a52013-02-15 10:56:35 -08001212 /**
Robert Greenwalt701a5c32014-06-23 11:40:00 -07001213 * Returns connection status information about a particular
1214 * Network.
1215 *
1216 * @param network {@link Network} specifying which network
1217 * in which you're interested.
1218 * @return a {@link NetworkInfo} object for the requested
1219 * network or {@code null} if the {@code Network}
1220 * is not valid.
junyulai5c2f6262018-12-13 12:47:51 +08001221 * @deprecated See {@link NetworkInfo}.
Robert Greenwalt701a5c32014-06-23 11:40:00 -07001222 */
junyulai5c2f6262018-12-13 12:47:51 +08001223 @Deprecated
Jeff Sharkey2ac62992017-04-24 11:18:03 -06001224 @RequiresPermission(android.Manifest.permission.ACCESS_NETWORK_STATE)
Chalard Jean42a9c292019-01-07 19:26:34 +09001225 @Nullable
1226 public NetworkInfo getNetworkInfo(@Nullable Network network) {
Jeff Sharkey833c35b2016-04-28 15:33:18 -06001227 return getNetworkInfoForUid(network, Process.myUid(), false);
1228 }
1229
1230 /** {@hide} */
1231 public NetworkInfo getNetworkInfoForUid(Network network, int uid, boolean ignoreBlocked) {
Robert Greenwalt701a5c32014-06-23 11:40:00 -07001232 try {
Jeff Sharkey833c35b2016-04-28 15:33:18 -06001233 return mService.getNetworkInfoForUid(network, uid, ignoreBlocked);
Robert Greenwalt701a5c32014-06-23 11:40:00 -07001234 } catch (RemoteException e) {
Jeff Sharkeya6066fd2016-03-01 19:27:23 -07001235 throw e.rethrowFromSystemServer();
Robert Greenwalt701a5c32014-06-23 11:40:00 -07001236 }
1237 }
1238
1239 /**
Robert Greenwalt26744a52013-02-15 10:56:35 -08001240 * Returns connection status information about all network
1241 * types supported by the device.
1242 *
1243 * @return an array of {@link NetworkInfo} objects. Check each
1244 * {@link NetworkInfo#getType} for which type each applies.
1245 *
Paul Jensena8e6dc62015-03-18 12:23:02 -04001246 * @deprecated This method does not support multiple connected networks
1247 * of the same type. Use {@link #getAllNetworks} and
1248 * {@link #getNetworkInfo(android.net.Network)} instead.
Robert Greenwalt26744a52013-02-15 10:56:35 -08001249 */
Aurimas Liutikasa6bb0232016-05-24 15:22:55 -07001250 @Deprecated
Jeff Sharkey2ac62992017-04-24 11:18:03 -06001251 @RequiresPermission(android.Manifest.permission.ACCESS_NETWORK_STATE)
Chalard Jean42a9c292019-01-07 19:26:34 +09001252 @NonNull
The Android Open Source Project28527d22009-03-03 19:31:44 -08001253 public NetworkInfo[] getAllNetworkInfo() {
1254 try {
1255 return mService.getAllNetworkInfo();
1256 } catch (RemoteException e) {
Jeff Sharkeya6066fd2016-03-01 19:27:23 -07001257 throw e.rethrowFromSystemServer();
The Android Open Source Project28527d22009-03-03 19:31:44 -08001258 }
1259 }
1260
Robert Greenwalt26744a52013-02-15 10:56:35 -08001261 /**
junyulai57840802021-03-03 12:09:05 +08001262 * Return a list of {@link NetworkStateSnapshot}s, one for each network that is currently
1263 * connected.
1264 * @hide
1265 */
1266 @SystemApi(client = MODULE_LIBRARIES)
1267 @RequiresPermission(anyOf = {
1268 NetworkStack.PERMISSION_MAINLINE_NETWORK_STACK,
1269 android.Manifest.permission.NETWORK_STACK,
1270 android.Manifest.permission.NETWORK_SETTINGS})
1271 @NonNull
1272 public List<NetworkStateSnapshot> getAllNetworkStateSnapshot() {
1273 try {
1274 return mService.getAllNetworkStateSnapshot();
1275 } catch (RemoteException e) {
1276 throw e.rethrowFromSystemServer();
1277 }
1278 }
1279
1280 /**
Lorenzo Colitti2b2d73c2014-08-22 17:10:50 -07001281 * Returns the {@link Network} object currently serving a given type, or
1282 * null if the given type is not connected.
1283 *
Lorenzo Colitti2b2d73c2014-08-22 17:10:50 -07001284 * @hide
Paul Jensena8e6dc62015-03-18 12:23:02 -04001285 * @deprecated This method does not support multiple connected networks
1286 * of the same type. Use {@link #getAllNetworks} and
1287 * {@link #getNetworkInfo(android.net.Network)} instead.
Lorenzo Colitti2b2d73c2014-08-22 17:10:50 -07001288 */
Aurimas Liutikasa6bb0232016-05-24 15:22:55 -07001289 @Deprecated
Jeff Sharkey2ac62992017-04-24 11:18:03 -06001290 @RequiresPermission(android.Manifest.permission.ACCESS_NETWORK_STATE)
Mathew Inwood0b8f8612018-08-08 14:52:47 +01001291 @UnsupportedAppUsage
Lorenzo Colitti2b2d73c2014-08-22 17:10:50 -07001292 public Network getNetworkForType(int networkType) {
1293 try {
1294 return mService.getNetworkForType(networkType);
1295 } catch (RemoteException e) {
Jeff Sharkeya6066fd2016-03-01 19:27:23 -07001296 throw e.rethrowFromSystemServer();
Lorenzo Colitti2b2d73c2014-08-22 17:10:50 -07001297 }
1298 }
1299
1300 /**
Robert Greenwalt701a5c32014-06-23 11:40:00 -07001301 * Returns an array of all {@link Network} currently tracked by the
1302 * framework.
Paul Jensen19b3ee72015-05-06 11:10:18 -04001303 *
1304 * @return an array of {@link Network} objects.
Robert Greenwalt701a5c32014-06-23 11:40:00 -07001305 */
Jeff Sharkey2ac62992017-04-24 11:18:03 -06001306 @RequiresPermission(android.Manifest.permission.ACCESS_NETWORK_STATE)
Chalard Jean42a9c292019-01-07 19:26:34 +09001307 @NonNull
Robert Greenwalt701a5c32014-06-23 11:40:00 -07001308 public Network[] getAllNetworks() {
1309 try {
1310 return mService.getAllNetworks();
1311 } catch (RemoteException e) {
Jeff Sharkeya6066fd2016-03-01 19:27:23 -07001312 throw e.rethrowFromSystemServer();
Robert Greenwalt701a5c32014-06-23 11:40:00 -07001313 }
1314 }
1315
1316 /**
Roshan Pius7992afd2020-12-22 15:10:42 -08001317 * Returns an array of {@link NetworkCapabilities} objects, representing
Lorenzo Colitti8db934a2014-11-28 11:21:30 +09001318 * the Networks that applications run by the given user will use by default.
1319 * @hide
1320 */
Mathew Inwood0b8f8612018-08-08 14:52:47 +01001321 @UnsupportedAppUsage
Lorenzo Colitti8db934a2014-11-28 11:21:30 +09001322 public NetworkCapabilities[] getDefaultNetworkCapabilitiesForUser(int userId) {
1323 try {
Qingxi Libf6bf082020-01-08 12:51:49 -08001324 return mService.getDefaultNetworkCapabilitiesForUser(
Roshan Piusc97d8062020-12-17 14:53:09 -08001325 userId, mContext.getOpPackageName(), getAttributionTag());
Lorenzo Colitti8db934a2014-11-28 11:21:30 +09001326 } catch (RemoteException e) {
Jeff Sharkeya6066fd2016-03-01 19:27:23 -07001327 throw e.rethrowFromSystemServer();
Lorenzo Colitti8db934a2014-11-28 11:21:30 +09001328 }
1329 }
1330
1331 /**
Robert Greenwalt26744a52013-02-15 10:56:35 -08001332 * Returns the IP information for the current default network.
1333 *
1334 * @return a {@link LinkProperties} object describing the IP info
1335 * for the current default network, or {@code null} if there
1336 * is no current default network.
1337 *
Robert Greenwalt26744a52013-02-15 10:56:35 -08001338 * {@hide}
Chalard Jeancc47b522019-01-11 16:47:53 +09001339 * @deprecated please use {@link #getLinkProperties(Network)} on the return
1340 * value of {@link #getActiveNetwork()} instead. In particular,
1341 * this method will return non-null LinkProperties even if the
1342 * app is blocked by policy from using this network.
Robert Greenwalt26744a52013-02-15 10:56:35 -08001343 */
Jeff Sharkey2ac62992017-04-24 11:18:03 -06001344 @RequiresPermission(android.Manifest.permission.ACCESS_NETWORK_STATE)
Chalard Jeancc47b522019-01-11 16:47:53 +09001345 @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P, trackingBug = 109783091)
Robert Greenwalt9f0ee4f2010-09-14 09:18:02 -07001346 public LinkProperties getActiveLinkProperties() {
1347 try {
1348 return mService.getActiveLinkProperties();
1349 } catch (RemoteException e) {
Jeff Sharkeya6066fd2016-03-01 19:27:23 -07001350 throw e.rethrowFromSystemServer();
Robert Greenwalt9f0ee4f2010-09-14 09:18:02 -07001351 }
1352 }
1353
Robert Greenwalt26744a52013-02-15 10:56:35 -08001354 /**
1355 * Returns the IP information for a given network type.
1356 *
1357 * @param networkType the network type of interest.
1358 * @return a {@link LinkProperties} object describing the IP info
1359 * for the given networkType, or {@code null} if there is
1360 * no current default network.
1361 *
Robert Greenwalt26744a52013-02-15 10:56:35 -08001362 * {@hide}
Paul Jensena8e6dc62015-03-18 12:23:02 -04001363 * @deprecated This method does not support multiple connected networks
1364 * of the same type. Use {@link #getAllNetworks},
1365 * {@link #getNetworkInfo(android.net.Network)}, and
1366 * {@link #getLinkProperties(android.net.Network)} instead.
Robert Greenwalt26744a52013-02-15 10:56:35 -08001367 */
Aurimas Liutikasa6bb0232016-05-24 15:22:55 -07001368 @Deprecated
Jeff Sharkey2ac62992017-04-24 11:18:03 -06001369 @RequiresPermission(android.Manifest.permission.ACCESS_NETWORK_STATE)
Chalard Jeanaa91c9d2019-04-09 15:46:21 +09001370 @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P, trackingBug = 130143562)
Robert Greenwalt9f0ee4f2010-09-14 09:18:02 -07001371 public LinkProperties getLinkProperties(int networkType) {
1372 try {
Robert Greenwalt2bebbfb2014-03-26 16:47:06 -07001373 return mService.getLinkPropertiesForType(networkType);
1374 } catch (RemoteException e) {
Jeff Sharkeya6066fd2016-03-01 19:27:23 -07001375 throw e.rethrowFromSystemServer();
Robert Greenwalt2bebbfb2014-03-26 16:47:06 -07001376 }
1377 }
1378
Robert Greenwaltd0ebdbb2014-05-18 23:07:25 -07001379 /**
1380 * Get the {@link LinkProperties} for the given {@link Network}. This
1381 * will return {@code null} if the network is unknown.
1382 *
1383 * @param network The {@link Network} object identifying the network in question.
1384 * @return The {@link LinkProperties} for the network, or {@code null}.
Paul Jensen19b3ee72015-05-06 11:10:18 -04001385 */
Jeff Sharkey2ac62992017-04-24 11:18:03 -06001386 @RequiresPermission(android.Manifest.permission.ACCESS_NETWORK_STATE)
Chalard Jean42a9c292019-01-07 19:26:34 +09001387 @Nullable
1388 public LinkProperties getLinkProperties(@Nullable Network network) {
Robert Greenwalt2bebbfb2014-03-26 16:47:06 -07001389 try {
1390 return mService.getLinkProperties(network);
1391 } catch (RemoteException e) {
Jeff Sharkeya6066fd2016-03-01 19:27:23 -07001392 throw e.rethrowFromSystemServer();
Robert Greenwalt2bebbfb2014-03-26 16:47:06 -07001393 }
1394 }
1395
Robert Greenwaltd0ebdbb2014-05-18 23:07:25 -07001396 /**
Roshan Pius7992afd2020-12-22 15:10:42 -08001397 * Get the {@link NetworkCapabilities} for the given {@link Network}. This
Robert Greenwaltd0ebdbb2014-05-18 23:07:25 -07001398 * will return {@code null} if the network is unknown.
1399 *
Roshan Pius7992afd2020-12-22 15:10:42 -08001400 * This will remove any location sensitive data in {@link TransportInfo} embedded in
1401 * {@link NetworkCapabilities#getTransportInfo()}. Some transport info instances like
1402 * {@link android.net.wifi.WifiInfo} contain location sensitive information. Retrieving
1403 * this location sensitive information (subject to app's location permissions) will be
1404 * noted by system. To include any location sensitive data in {@link TransportInfo},
1405 * use a {@link NetworkCallback} with
1406 * {@link NetworkCallback#FLAG_INCLUDE_LOCATION_INFO} flag.
1407 *
Robert Greenwaltd0ebdbb2014-05-18 23:07:25 -07001408 * @param network The {@link Network} object identifying the network in question.
Roshan Pius7992afd2020-12-22 15:10:42 -08001409 * @return The {@link NetworkCapabilities} for the network, or {@code null}.
Robert Greenwaltd0ebdbb2014-05-18 23:07:25 -07001410 */
Jeff Sharkey2ac62992017-04-24 11:18:03 -06001411 @RequiresPermission(android.Manifest.permission.ACCESS_NETWORK_STATE)
Chalard Jean42a9c292019-01-07 19:26:34 +09001412 @Nullable
1413 public NetworkCapabilities getNetworkCapabilities(@Nullable Network network) {
Robert Greenwalt2bebbfb2014-03-26 16:47:06 -07001414 try {
Roshan Piusc97d8062020-12-17 14:53:09 -08001415 return mService.getNetworkCapabilities(
1416 network, mContext.getOpPackageName(), getAttributionTag());
Robert Greenwalt9f0ee4f2010-09-14 09:18:02 -07001417 } catch (RemoteException e) {
Jeff Sharkeya6066fd2016-03-01 19:27:23 -07001418 throw e.rethrowFromSystemServer();
Robert Greenwalt9f0ee4f2010-09-14 09:18:02 -07001419 }
1420 }
1421
Robert Greenwalt26744a52013-02-15 10:56:35 -08001422 /**
Remi NGUYEN VAN69a57cb2019-03-20 14:22:49 +09001423 * Gets a URL that can be used for resolving whether a captive portal is present.
Udam Saini53b71ee2016-01-04 12:16:14 -08001424 * 1. This URL should respond with a 204 response to a GET request to indicate no captive
1425 * portal is present.
1426 * 2. This URL must be HTTP as redirect responses are used to find captive portal
1427 * sign-in pages. Captive portals cannot respond to HTTPS requests with redirects.
1428 *
Remi NGUYEN VAN69a57cb2019-03-20 14:22:49 +09001429 * The system network validation may be using different strategies to detect captive portals,
1430 * so this method does not necessarily return a URL used by the system. It only returns a URL
1431 * that may be relevant for other components trying to detect captive portals.
paulhud70b7dd2019-08-12 16:25:11 +08001432 *
Udam Saini53b71ee2016-01-04 12:16:14 -08001433 * @hide
paulhud70b7dd2019-08-12 16:25:11 +08001434 * @deprecated This API returns URL which is not guaranteed to be one of the URLs used by the
1435 * system.
Udam Saini53b71ee2016-01-04 12:16:14 -08001436 */
paulhud70b7dd2019-08-12 16:25:11 +08001437 @Deprecated
Udam Saini53b71ee2016-01-04 12:16:14 -08001438 @SystemApi
paulhud70b7dd2019-08-12 16:25:11 +08001439 @RequiresPermission(android.Manifest.permission.NETWORK_SETTINGS)
Udam Saini53b71ee2016-01-04 12:16:14 -08001440 public String getCaptivePortalServerUrl() {
1441 try {
1442 return mService.getCaptivePortalServerUrl();
1443 } catch (RemoteException e) {
Jeff Sharkeya6066fd2016-03-01 19:27:23 -07001444 throw e.rethrowFromSystemServer();
Udam Saini53b71ee2016-01-04 12:16:14 -08001445 }
1446 }
1447
1448 /**
The Android Open Source Project28527d22009-03-03 19:31:44 -08001449 * Tells the underlying networking system that the caller wants to
1450 * begin using the named feature. The interpretation of {@code feature}
1451 * is completely up to each networking implementation.
Lorenzo Colitti91de63b2015-10-15 16:29:00 +09001452 *
1453 * <p>This method requires the caller to hold either the
1454 * {@link android.Manifest.permission#CHANGE_NETWORK_STATE} permission
1455 * or the ability to modify system settings as determined by
1456 * {@link android.provider.Settings.System#canWrite}.</p>
1457 *
The Android Open Source Project28527d22009-03-03 19:31:44 -08001458 * @param networkType specifies which network the request pertains to
1459 * @param feature the name of the feature to be used
1460 * @return an integer value representing the outcome of the request.
1461 * The interpretation of this value is specific to each networking
1462 * implementation+feature combination, except that the value {@code -1}
1463 * always indicates failure.
Robert Greenwaltd0ebdbb2014-05-18 23:07:25 -07001464 *
Lorenzo Colittiefd396e2015-04-24 17:03:31 +09001465 * @deprecated Deprecated in favor of the cleaner
1466 * {@link #requestNetwork(NetworkRequest, NetworkCallback)} API.
Dianne Hackbornc16abda2015-07-31 10:35:34 -07001467 * In {@link VERSION_CODES#M}, and above, this method is unsupported and will
Lorenzo Colitti9b026fa2015-07-29 11:41:21 +09001468 * throw {@code UnsupportedOperationException} if called.
Lorenzo Colitti7cb18342016-12-09 18:39:30 +09001469 * @removed
The Android Open Source Project28527d22009-03-03 19:31:44 -08001470 */
Aurimas Liutikasa6bb0232016-05-24 15:22:55 -07001471 @Deprecated
The Android Open Source Project28527d22009-03-03 19:31:44 -08001472 public int startUsingNetworkFeature(int networkType, String feature) {
Lorenzo Colitti9b026fa2015-07-29 11:41:21 +09001473 checkLegacyRoutingApiAccess();
Robert Greenwaltb59a91b2014-05-15 18:07:26 -07001474 NetworkCapabilities netCap = networkCapabilitiesForFeature(networkType, feature);
1475 if (netCap == null) {
1476 Log.d(TAG, "Can't satisfy startUsingNetworkFeature for " + networkType + ", " +
1477 feature);
Chalard Jean1f42df12019-11-21 14:48:00 +09001478 return DEPRECATED_PHONE_CONSTANT_APN_REQUEST_FAILED;
Robert Greenwaltb59a91b2014-05-15 18:07:26 -07001479 }
1480
1481 NetworkRequest request = null;
1482 synchronized (sLegacyRequests) {
1483 LegacyRequest l = sLegacyRequests.get(netCap);
1484 if (l != null) {
1485 Log.d(TAG, "renewing startUsingNetworkFeature request " + l.networkRequest);
1486 renewRequestLocked(l);
1487 if (l.currentNetwork != null) {
Chalard Jean1f42df12019-11-21 14:48:00 +09001488 return DEPRECATED_PHONE_CONSTANT_APN_ALREADY_ACTIVE;
Robert Greenwaltb59a91b2014-05-15 18:07:26 -07001489 } else {
Chalard Jean1f42df12019-11-21 14:48:00 +09001490 return DEPRECATED_PHONE_CONSTANT_APN_REQUEST_STARTED;
Robert Greenwaltb59a91b2014-05-15 18:07:26 -07001491 }
1492 }
1493
1494 request = requestNetworkForFeatureLocked(netCap);
1495 }
1496 if (request != null) {
Robert Greenwaltca3c5cf2014-06-20 10:58:45 -07001497 Log.d(TAG, "starting startUsingNetworkFeature for request " + request);
Chalard Jean1f42df12019-11-21 14:48:00 +09001498 return DEPRECATED_PHONE_CONSTANT_APN_REQUEST_STARTED;
Robert Greenwaltb59a91b2014-05-15 18:07:26 -07001499 } else {
1500 Log.d(TAG, " request Failed");
Chalard Jean1f42df12019-11-21 14:48:00 +09001501 return DEPRECATED_PHONE_CONSTANT_APN_REQUEST_FAILED;
The Android Open Source Project28527d22009-03-03 19:31:44 -08001502 }
1503 }
1504
1505 /**
1506 * Tells the underlying networking system that the caller is finished
1507 * using the named feature. The interpretation of {@code feature}
1508 * is completely up to each networking implementation.
Lorenzo Colitti91de63b2015-10-15 16:29:00 +09001509 *
1510 * <p>This method requires the caller to hold either the
1511 * {@link android.Manifest.permission#CHANGE_NETWORK_STATE} permission
1512 * or the ability to modify system settings as determined by
1513 * {@link android.provider.Settings.System#canWrite}.</p>
1514 *
The Android Open Source Project28527d22009-03-03 19:31:44 -08001515 * @param networkType specifies which network the request pertains to
1516 * @param feature the name of the feature that is no longer needed
1517 * @return an integer value representing the outcome of the request.
1518 * The interpretation of this value is specific to each networking
1519 * implementation+feature combination, except that the value {@code -1}
1520 * always indicates failure.
Robert Greenwaltd0ebdbb2014-05-18 23:07:25 -07001521 *
Lorenzo Colittiad458ad2016-04-13 22:00:02 +09001522 * @deprecated Deprecated in favor of the cleaner
1523 * {@link #unregisterNetworkCallback(NetworkCallback)} API.
Dianne Hackbornc16abda2015-07-31 10:35:34 -07001524 * In {@link VERSION_CODES#M}, and above, this method is unsupported and will
Lorenzo Colitti9b026fa2015-07-29 11:41:21 +09001525 * throw {@code UnsupportedOperationException} if called.
Lorenzo Colitti7cb18342016-12-09 18:39:30 +09001526 * @removed
The Android Open Source Project28527d22009-03-03 19:31:44 -08001527 */
Aurimas Liutikasa6bb0232016-05-24 15:22:55 -07001528 @Deprecated
The Android Open Source Project28527d22009-03-03 19:31:44 -08001529 public int stopUsingNetworkFeature(int networkType, String feature) {
Lorenzo Colitti9b026fa2015-07-29 11:41:21 +09001530 checkLegacyRoutingApiAccess();
Robert Greenwaltb59a91b2014-05-15 18:07:26 -07001531 NetworkCapabilities netCap = networkCapabilitiesForFeature(networkType, feature);
1532 if (netCap == null) {
1533 Log.d(TAG, "Can't satisfy stopUsingNetworkFeature for " + networkType + ", " +
1534 feature);
The Android Open Source Project28527d22009-03-03 19:31:44 -08001535 return -1;
1536 }
Robert Greenwaltb59a91b2014-05-15 18:07:26 -07001537
Paul Jensen034dea32014-12-17 10:39:34 -05001538 if (removeRequestForFeature(netCap)) {
Robert Greenwaltb59a91b2014-05-15 18:07:26 -07001539 Log.d(TAG, "stopUsingNetworkFeature for " + networkType + ", " + feature);
Robert Greenwaltb59a91b2014-05-15 18:07:26 -07001540 }
1541 return 1;
1542 }
1543
Mathew Inwood5a09a712020-11-04 09:29:36 +00001544 @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.R, trackingBug = 170729553)
Robert Greenwaltb59a91b2014-05-15 18:07:26 -07001545 private NetworkCapabilities networkCapabilitiesForFeature(int networkType, String feature) {
1546 if (networkType == TYPE_MOBILE) {
Erik Kline50068e52017-01-26 18:08:28 +09001547 switch (feature) {
1548 case "enableCBS":
1549 return networkCapabilitiesForType(TYPE_MOBILE_CBS);
1550 case "enableDUN":
1551 case "enableDUNAlways":
1552 return networkCapabilitiesForType(TYPE_MOBILE_DUN);
1553 case "enableFOTA":
1554 return networkCapabilitiesForType(TYPE_MOBILE_FOTA);
1555 case "enableHIPRI":
1556 return networkCapabilitiesForType(TYPE_MOBILE_HIPRI);
1557 case "enableIMS":
1558 return networkCapabilitiesForType(TYPE_MOBILE_IMS);
1559 case "enableMMS":
1560 return networkCapabilitiesForType(TYPE_MOBILE_MMS);
1561 case "enableSUPL":
1562 return networkCapabilitiesForType(TYPE_MOBILE_SUPL);
1563 default:
1564 return null;
Robert Greenwaltb59a91b2014-05-15 18:07:26 -07001565 }
Erik Kline50068e52017-01-26 18:08:28 +09001566 } else if (networkType == TYPE_WIFI && "p2p".equals(feature)) {
1567 return networkCapabilitiesForType(TYPE_WIFI_P2P);
Robert Greenwaltb59a91b2014-05-15 18:07:26 -07001568 }
1569 return null;
1570 }
1571
Robert Greenwalt5a367872014-06-02 15:32:02 -07001572 private int legacyTypeForNetworkCapabilities(NetworkCapabilities netCap) {
Robert Greenwaltb59a91b2014-05-15 18:07:26 -07001573 if (netCap == null) return TYPE_NONE;
1574 if (netCap.hasCapability(NetworkCapabilities.NET_CAPABILITY_CBS)) {
1575 return TYPE_MOBILE_CBS;
1576 }
1577 if (netCap.hasCapability(NetworkCapabilities.NET_CAPABILITY_IMS)) {
1578 return TYPE_MOBILE_IMS;
1579 }
1580 if (netCap.hasCapability(NetworkCapabilities.NET_CAPABILITY_FOTA)) {
1581 return TYPE_MOBILE_FOTA;
1582 }
1583 if (netCap.hasCapability(NetworkCapabilities.NET_CAPABILITY_DUN)) {
1584 return TYPE_MOBILE_DUN;
1585 }
1586 if (netCap.hasCapability(NetworkCapabilities.NET_CAPABILITY_SUPL)) {
1587 return TYPE_MOBILE_SUPL;
1588 }
1589 if (netCap.hasCapability(NetworkCapabilities.NET_CAPABILITY_MMS)) {
1590 return TYPE_MOBILE_MMS;
1591 }
1592 if (netCap.hasCapability(NetworkCapabilities.NET_CAPABILITY_INTERNET)) {
1593 return TYPE_MOBILE_HIPRI;
1594 }
Robert Greenwalt5a367872014-06-02 15:32:02 -07001595 if (netCap.hasCapability(NetworkCapabilities.NET_CAPABILITY_WIFI_P2P)) {
1596 return TYPE_WIFI_P2P;
1597 }
Robert Greenwaltb59a91b2014-05-15 18:07:26 -07001598 return TYPE_NONE;
1599 }
1600
1601 private static class LegacyRequest {
1602 NetworkCapabilities networkCapabilities;
1603 NetworkRequest networkRequest;
1604 int expireSequenceNumber;
1605 Network currentNetwork;
1606 int delay = -1;
Paul Jensen034dea32014-12-17 10:39:34 -05001607
1608 private void clearDnsBinding() {
1609 if (currentNetwork != null) {
1610 currentNetwork = null;
1611 setProcessDefaultNetworkForHostResolution(null);
1612 }
1613 }
1614
Robert Greenwalt09e5cff2014-06-11 16:05:07 -07001615 NetworkCallback networkCallback = new NetworkCallback() {
Robert Greenwaltb59a91b2014-05-15 18:07:26 -07001616 @Override
Robert Greenwalt09e5cff2014-06-11 16:05:07 -07001617 public void onAvailable(Network network) {
Robert Greenwaltb59a91b2014-05-15 18:07:26 -07001618 currentNetwork = network;
1619 Log.d(TAG, "startUsingNetworkFeature got Network:" + network);
Paul Jensene98c6e02014-05-29 10:12:39 -04001620 setProcessDefaultNetworkForHostResolution(network);
Robert Greenwaltb59a91b2014-05-15 18:07:26 -07001621 }
1622 @Override
Robert Greenwalt09e5cff2014-06-11 16:05:07 -07001623 public void onLost(Network network) {
Paul Jensen034dea32014-12-17 10:39:34 -05001624 if (network.equals(currentNetwork)) clearDnsBinding();
Robert Greenwaltb59a91b2014-05-15 18:07:26 -07001625 Log.d(TAG, "startUsingNetworkFeature lost Network:" + network);
1626 }
1627 };
1628 }
1629
Mathew Inwood5a09a712020-11-04 09:29:36 +00001630 @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.R, trackingBug = 170729553)
Chalard Jean73d9db72018-06-04 16:52:49 +09001631 private static final HashMap<NetworkCapabilities, LegacyRequest> sLegacyRequests =
1632 new HashMap<>();
Robert Greenwaltb59a91b2014-05-15 18:07:26 -07001633
1634 private NetworkRequest findRequestForFeature(NetworkCapabilities netCap) {
1635 synchronized (sLegacyRequests) {
1636 LegacyRequest l = sLegacyRequests.get(netCap);
1637 if (l != null) return l.networkRequest;
1638 }
1639 return null;
1640 }
1641
1642 private void renewRequestLocked(LegacyRequest l) {
1643 l.expireSequenceNumber++;
1644 Log.d(TAG, "renewing request to seqNum " + l.expireSequenceNumber);
1645 sendExpireMsgForFeature(l.networkCapabilities, l.expireSequenceNumber, l.delay);
1646 }
1647
1648 private void expireRequest(NetworkCapabilities netCap, int sequenceNum) {
1649 int ourSeqNum = -1;
1650 synchronized (sLegacyRequests) {
1651 LegacyRequest l = sLegacyRequests.get(netCap);
1652 if (l == null) return;
1653 ourSeqNum = l.expireSequenceNumber;
Paul Jensen034dea32014-12-17 10:39:34 -05001654 if (l.expireSequenceNumber == sequenceNum) removeRequestForFeature(netCap);
Robert Greenwaltb59a91b2014-05-15 18:07:26 -07001655 }
1656 Log.d(TAG, "expireRequest with " + ourSeqNum + ", " + sequenceNum);
1657 }
1658
Mathew Inwood5a09a712020-11-04 09:29:36 +00001659 @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.R, trackingBug = 170729553)
Robert Greenwaltb59a91b2014-05-15 18:07:26 -07001660 private NetworkRequest requestNetworkForFeatureLocked(NetworkCapabilities netCap) {
1661 int delay = -1;
Robert Greenwalt5a367872014-06-02 15:32:02 -07001662 int type = legacyTypeForNetworkCapabilities(netCap);
Robert Greenwaltb59a91b2014-05-15 18:07:26 -07001663 try {
1664 delay = mService.getRestoreDefaultNetworkDelay(type);
Jeff Sharkeya6066fd2016-03-01 19:27:23 -07001665 } catch (RemoteException e) {
1666 throw e.rethrowFromSystemServer();
1667 }
Robert Greenwaltb59a91b2014-05-15 18:07:26 -07001668 LegacyRequest l = new LegacyRequest();
1669 l.networkCapabilities = netCap;
1670 l.delay = delay;
1671 l.expireSequenceNumber = 0;
Hugo Benichicd5a0e02017-02-02 17:02:36 +09001672 l.networkRequest = sendRequestForNetwork(
1673 netCap, l.networkCallback, 0, REQUEST, type, getDefaultHandler());
Robert Greenwaltb59a91b2014-05-15 18:07:26 -07001674 if (l.networkRequest == null) return null;
1675 sLegacyRequests.put(netCap, l);
1676 sendExpireMsgForFeature(netCap, l.expireSequenceNumber, delay);
1677 return l.networkRequest;
1678 }
1679
1680 private void sendExpireMsgForFeature(NetworkCapabilities netCap, int seqNum, int delay) {
1681 if (delay >= 0) {
1682 Log.d(TAG, "sending expire msg with seqNum " + seqNum + " and delay " + delay);
Hugo Benichicd5a0e02017-02-02 17:02:36 +09001683 CallbackHandler handler = getDefaultHandler();
Hugo Benichiec180d52017-02-03 14:18:44 +09001684 Message msg = handler.obtainMessage(EXPIRE_LEGACY_REQUEST, seqNum, 0, netCap);
1685 handler.sendMessageDelayed(msg, delay);
Robert Greenwaltb59a91b2014-05-15 18:07:26 -07001686 }
1687 }
1688
Mathew Inwood5a09a712020-11-04 09:29:36 +00001689 @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.R, trackingBug = 170729553)
Paul Jensen034dea32014-12-17 10:39:34 -05001690 private boolean removeRequestForFeature(NetworkCapabilities netCap) {
1691 final LegacyRequest l;
Robert Greenwaltb59a91b2014-05-15 18:07:26 -07001692 synchronized (sLegacyRequests) {
Paul Jensen034dea32014-12-17 10:39:34 -05001693 l = sLegacyRequests.remove(netCap);
Robert Greenwaltb59a91b2014-05-15 18:07:26 -07001694 }
Paul Jensen034dea32014-12-17 10:39:34 -05001695 if (l == null) return false;
1696 unregisterNetworkCallback(l.networkCallback);
1697 l.clearDnsBinding();
1698 return true;
The Android Open Source Project28527d22009-03-03 19:31:44 -08001699 }
1700
Erik Kline50068e52017-01-26 18:08:28 +09001701 private static final SparseIntArray sLegacyTypeToTransport = new SparseIntArray();
1702 static {
1703 sLegacyTypeToTransport.put(TYPE_MOBILE, NetworkCapabilities.TRANSPORT_CELLULAR);
1704 sLegacyTypeToTransport.put(TYPE_MOBILE_CBS, NetworkCapabilities.TRANSPORT_CELLULAR);
1705 sLegacyTypeToTransport.put(TYPE_MOBILE_DUN, NetworkCapabilities.TRANSPORT_CELLULAR);
1706 sLegacyTypeToTransport.put(TYPE_MOBILE_FOTA, NetworkCapabilities.TRANSPORT_CELLULAR);
1707 sLegacyTypeToTransport.put(TYPE_MOBILE_HIPRI, NetworkCapabilities.TRANSPORT_CELLULAR);
1708 sLegacyTypeToTransport.put(TYPE_MOBILE_IMS, NetworkCapabilities.TRANSPORT_CELLULAR);
1709 sLegacyTypeToTransport.put(TYPE_MOBILE_MMS, NetworkCapabilities.TRANSPORT_CELLULAR);
1710 sLegacyTypeToTransport.put(TYPE_MOBILE_SUPL, NetworkCapabilities.TRANSPORT_CELLULAR);
1711 sLegacyTypeToTransport.put(TYPE_WIFI, NetworkCapabilities.TRANSPORT_WIFI);
1712 sLegacyTypeToTransport.put(TYPE_WIFI_P2P, NetworkCapabilities.TRANSPORT_WIFI);
1713 sLegacyTypeToTransport.put(TYPE_BLUETOOTH, NetworkCapabilities.TRANSPORT_BLUETOOTH);
1714 sLegacyTypeToTransport.put(TYPE_ETHERNET, NetworkCapabilities.TRANSPORT_ETHERNET);
1715 }
1716
1717 private static final SparseIntArray sLegacyTypeToCapability = new SparseIntArray();
1718 static {
1719 sLegacyTypeToCapability.put(TYPE_MOBILE_CBS, NetworkCapabilities.NET_CAPABILITY_CBS);
1720 sLegacyTypeToCapability.put(TYPE_MOBILE_DUN, NetworkCapabilities.NET_CAPABILITY_DUN);
1721 sLegacyTypeToCapability.put(TYPE_MOBILE_FOTA, NetworkCapabilities.NET_CAPABILITY_FOTA);
1722 sLegacyTypeToCapability.put(TYPE_MOBILE_IMS, NetworkCapabilities.NET_CAPABILITY_IMS);
1723 sLegacyTypeToCapability.put(TYPE_MOBILE_MMS, NetworkCapabilities.NET_CAPABILITY_MMS);
1724 sLegacyTypeToCapability.put(TYPE_MOBILE_SUPL, NetworkCapabilities.NET_CAPABILITY_SUPL);
1725 sLegacyTypeToCapability.put(TYPE_WIFI_P2P, NetworkCapabilities.NET_CAPABILITY_WIFI_P2P);
1726 }
1727
1728 /**
1729 * Given a legacy type (TYPE_WIFI, ...) returns a NetworkCapabilities
1730 * instance suitable for registering a request or callback. Throws an
1731 * IllegalArgumentException if no mapping from the legacy type to
1732 * NetworkCapabilities is known.
1733 *
Chalard Jean5acb7b72018-03-08 13:54:53 +09001734 * @deprecated Types are deprecated. Use {@link NetworkCallback} or {@link NetworkRequest}
1735 * to find the network instead.
Erik Kline50068e52017-01-26 18:08:28 +09001736 * @hide
1737 */
1738 public static NetworkCapabilities networkCapabilitiesForType(int type) {
1739 final NetworkCapabilities nc = new NetworkCapabilities();
1740
1741 // Map from type to transports.
1742 final int NOT_FOUND = -1;
1743 final int transport = sLegacyTypeToTransport.get(type, NOT_FOUND);
Remi NGUYEN VAN1c17c882021-03-11 20:49:13 +09001744 if (transport == NOT_FOUND) {
1745 throw new IllegalArgumentException("unknown legacy type: " + type);
1746 }
Erik Kline50068e52017-01-26 18:08:28 +09001747 nc.addTransportType(transport);
1748
1749 // Map from type to capabilities.
1750 nc.addCapability(sLegacyTypeToCapability.get(
1751 type, NetworkCapabilities.NET_CAPABILITY_INTERNET));
1752 nc.maybeMarkCapabilitiesRestricted();
1753 return nc;
1754 }
1755
Lorenzo Colittid1039d12015-06-15 14:29:22 +09001756 /** @hide */
1757 public static class PacketKeepaliveCallback {
Mathew Inwood5a09a712020-11-04 09:29:36 +00001758 @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.R, trackingBug = 170729553)
Artur Satayev198d6502019-11-04 17:50:59 +00001759 public PacketKeepaliveCallback() {
1760 }
Lorenzo Colittid1039d12015-06-15 14:29:22 +09001761 /** The requested keepalive was successfully started. */
Mathew Inwood5a09a712020-11-04 09:29:36 +00001762 @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.R, trackingBug = 170729553)
Lorenzo Colittid1039d12015-06-15 14:29:22 +09001763 public void onStarted() {}
1764 /** The keepalive was successfully stopped. */
Mathew Inwood5a09a712020-11-04 09:29:36 +00001765 @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.R, trackingBug = 170729553)
Lorenzo Colittid1039d12015-06-15 14:29:22 +09001766 public void onStopped() {}
1767 /** An error occurred. */
Mathew Inwood5a09a712020-11-04 09:29:36 +00001768 @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.R, trackingBug = 170729553)
Lorenzo Colittid1039d12015-06-15 14:29:22 +09001769 public void onError(int error) {}
1770 }
1771
1772 /**
1773 * Allows applications to request that the system periodically send specific packets on their
1774 * behalf, using hardware offload to save battery power.
1775 *
1776 * To request that the system send keepalives, call one of the methods that return a
1777 * {@link ConnectivityManager.PacketKeepalive} object, such as {@link #startNattKeepalive},
1778 * passing in a non-null callback. If the callback is successfully started, the callback's
1779 * {@code onStarted} method will be called. If an error occurs, {@code onError} will be called,
1780 * specifying one of the {@code ERROR_*} constants in this class.
1781 *
Chalard Jean73d9db72018-06-04 16:52:49 +09001782 * To stop an existing keepalive, call {@link PacketKeepalive#stop}. The system will call
1783 * {@link PacketKeepaliveCallback#onStopped} if the operation was successful or
1784 * {@link PacketKeepaliveCallback#onError} if an error occurred.
Lorenzo Colittid1039d12015-06-15 14:29:22 +09001785 *
junyulaia86defc2018-12-27 17:25:29 +08001786 * @deprecated Use {@link SocketKeepalive} instead.
1787 *
Lorenzo Colittid1039d12015-06-15 14:29:22 +09001788 * @hide
1789 */
1790 public class PacketKeepalive {
1791
1792 private static final String TAG = "PacketKeepalive";
1793
1794 /** @hide */
1795 public static final int SUCCESS = 0;
1796
1797 /** @hide */
1798 public static final int NO_KEEPALIVE = -1;
1799
1800 /** @hide */
1801 public static final int BINDER_DIED = -10;
1802
1803 /** The specified {@code Network} is not connected. */
1804 public static final int ERROR_INVALID_NETWORK = -20;
1805 /** The specified IP addresses are invalid. For example, the specified source IP address is
1806 * not configured on the specified {@code Network}. */
1807 public static final int ERROR_INVALID_IP_ADDRESS = -21;
1808 /** The requested port is invalid. */
1809 public static final int ERROR_INVALID_PORT = -22;
1810 /** The packet length is invalid (e.g., too long). */
1811 public static final int ERROR_INVALID_LENGTH = -23;
1812 /** The packet transmission interval is invalid (e.g., too short). */
1813 public static final int ERROR_INVALID_INTERVAL = -24;
1814
1815 /** The hardware does not support this request. */
1816 public static final int ERROR_HARDWARE_UNSUPPORTED = -30;
Lorenzo Colitti2aab7182015-09-08 16:46:36 +09001817 /** The hardware returned an error. */
1818 public static final int ERROR_HARDWARE_ERROR = -31;
Lorenzo Colittid1039d12015-06-15 14:29:22 +09001819
Nathan Harolde8ae0eb2018-02-14 13:09:45 -08001820 /** The NAT-T destination port for IPsec */
Lorenzo Colittid1039d12015-06-15 14:29:22 +09001821 public static final int NATT_PORT = 4500;
1822
Nathan Harolde8ae0eb2018-02-14 13:09:45 -08001823 /** The minimum interval in seconds between keepalive packet transmissions */
1824 public static final int MIN_INTERVAL = 10;
1825
Lorenzo Colittid1039d12015-06-15 14:29:22 +09001826 private final Network mNetwork;
junyulai9f872232019-01-16 20:23:34 +08001827 private final ISocketKeepaliveCallback mCallback;
1828 private final ExecutorService mExecutor;
Lorenzo Colittid1039d12015-06-15 14:29:22 +09001829
1830 private volatile Integer mSlot;
1831
Mathew Inwood5a09a712020-11-04 09:29:36 +00001832 @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.R, trackingBug = 170729553)
Lorenzo Colittid1039d12015-06-15 14:29:22 +09001833 public void stop() {
1834 try {
junyulai9f872232019-01-16 20:23:34 +08001835 mExecutor.execute(() -> {
1836 try {
1837 if (mSlot != null) {
1838 mService.stopKeepalive(mNetwork, mSlot);
1839 }
1840 } catch (RemoteException e) {
1841 Log.e(TAG, "Error stopping packet keepalive: ", e);
1842 throw e.rethrowFromSystemServer();
1843 }
1844 });
1845 } catch (RejectedExecutionException e) {
1846 // The internal executor has already stopped due to previous event.
Lorenzo Colittid1039d12015-06-15 14:29:22 +09001847 }
1848 }
1849
1850 private PacketKeepalive(Network network, PacketKeepaliveCallback callback) {
Remi NGUYEN VAN1c17c882021-03-11 20:49:13 +09001851 Objects.requireNonNull(network, "network cannot be null");
1852 Objects.requireNonNull(callback, "callback cannot be null");
Lorenzo Colittid1039d12015-06-15 14:29:22 +09001853 mNetwork = network;
junyulai9f872232019-01-16 20:23:34 +08001854 mExecutor = Executors.newSingleThreadExecutor();
1855 mCallback = new ISocketKeepaliveCallback.Stub() {
Lorenzo Colittid1039d12015-06-15 14:29:22 +09001856 @Override
junyulai9f872232019-01-16 20:23:34 +08001857 public void onStarted(int slot) {
lucaslinad369e32020-12-30 11:54:55 +08001858 final long token = Binder.clearCallingIdentity();
1859 try {
1860 mExecutor.execute(() -> {
1861 mSlot = slot;
1862 callback.onStarted();
1863 });
1864 } finally {
1865 Binder.restoreCallingIdentity(token);
1866 }
Lorenzo Colittid1039d12015-06-15 14:29:22 +09001867 }
junyulai9f872232019-01-16 20:23:34 +08001868
1869 @Override
1870 public void onStopped() {
lucaslinad369e32020-12-30 11:54:55 +08001871 final long token = Binder.clearCallingIdentity();
1872 try {
1873 mExecutor.execute(() -> {
1874 mSlot = null;
1875 callback.onStopped();
1876 });
1877 } finally {
1878 Binder.restoreCallingIdentity(token);
1879 }
junyulai9f872232019-01-16 20:23:34 +08001880 mExecutor.shutdown();
1881 }
1882
1883 @Override
1884 public void onError(int error) {
lucaslinad369e32020-12-30 11:54:55 +08001885 final long token = Binder.clearCallingIdentity();
1886 try {
1887 mExecutor.execute(() -> {
1888 mSlot = null;
1889 callback.onError(error);
1890 });
1891 } finally {
1892 Binder.restoreCallingIdentity(token);
1893 }
junyulai9f872232019-01-16 20:23:34 +08001894 mExecutor.shutdown();
1895 }
1896
1897 @Override
1898 public void onDataReceived() {
1899 // PacketKeepalive is only used for Nat-T keepalive and as such does not invoke
1900 // this callback when data is received.
1901 }
1902 };
Lorenzo Colittid1039d12015-06-15 14:29:22 +09001903 }
1904 }
1905
1906 /**
1907 * Starts an IPsec NAT-T keepalive packet with the specified parameters.
1908 *
junyulaia86defc2018-12-27 17:25:29 +08001909 * @deprecated Use {@link #createSocketKeepalive} instead.
1910 *
Lorenzo Colittid1039d12015-06-15 14:29:22 +09001911 * @hide
1912 */
Mathew Inwood5a09a712020-11-04 09:29:36 +00001913 @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.R, trackingBug = 170729553)
Lorenzo Colittid1039d12015-06-15 14:29:22 +09001914 public PacketKeepalive startNattKeepalive(
1915 Network network, int intervalSeconds, PacketKeepaliveCallback callback,
1916 InetAddress srcAddr, int srcPort, InetAddress dstAddr) {
1917 final PacketKeepalive k = new PacketKeepalive(network, callback);
1918 try {
junyulai9f872232019-01-16 20:23:34 +08001919 mService.startNattKeepalive(network, intervalSeconds, k.mCallback,
Lorenzo Colittid1039d12015-06-15 14:29:22 +09001920 srcAddr.getHostAddress(), srcPort, dstAddr.getHostAddress());
1921 } catch (RemoteException e) {
1922 Log.e(TAG, "Error starting packet keepalive: ", e);
junyulai9f872232019-01-16 20:23:34 +08001923 throw e.rethrowFromSystemServer();
Lorenzo Colittid1039d12015-06-15 14:29:22 +09001924 }
1925 return k;
1926 }
1927
Chiachang Wangc5f86f42021-01-15 11:06:21 +08001928 // Construct an invalid fd.
1929 private ParcelFileDescriptor createInvalidFd() {
1930 final int invalidFd = -1;
1931 return ParcelFileDescriptor.adoptFd(invalidFd);
1932 }
1933
The Android Open Source Project28527d22009-03-03 19:31:44 -08001934 /**
junyulaia86defc2018-12-27 17:25:29 +08001935 * Request that keepalives be started on a IPsec NAT-T socket.
1936 *
1937 * @param network The {@link Network} the socket is on.
1938 * @param socket The socket that needs to be kept alive.
1939 * @param source The source address of the {@link UdpEncapsulationSocket}.
1940 * @param destination The destination address of the {@link UdpEncapsulationSocket}.
1941 * @param executor The executor on which callback will be invoked. The provided {@link Executor}
1942 * must run callback sequentially, otherwise the order of callbacks cannot be
1943 * guaranteed.
1944 * @param callback A {@link SocketKeepalive.Callback}. Used for notifications about keepalive
1945 * changes. Must be extended by applications that use this API.
1946 *
junyulaic7ea1242019-01-08 20:04:33 +08001947 * @return A {@link SocketKeepalive} object that can be used to control the keepalive on the
1948 * given socket.
junyulaia86defc2018-12-27 17:25:29 +08001949 **/
junyulai61143782019-03-04 22:45:36 +08001950 public @NonNull SocketKeepalive createSocketKeepalive(@NonNull Network network,
junyulaia86defc2018-12-27 17:25:29 +08001951 @NonNull UdpEncapsulationSocket socket,
1952 @NonNull InetAddress source,
1953 @NonNull InetAddress destination,
1954 @NonNull @CallbackExecutor Executor executor,
1955 @NonNull Callback callback) {
junyulai61143782019-03-04 22:45:36 +08001956 ParcelFileDescriptor dup;
1957 try {
junyulaic4fb2482019-03-27 11:00:37 +08001958 // Dup is needed here as the pfd inside the socket is owned by the IpSecService,
1959 // which cannot be obtained by the app process.
junyulai61143782019-03-04 22:45:36 +08001960 dup = ParcelFileDescriptor.dup(socket.getFileDescriptor());
1961 } catch (IOException ignored) {
1962 // Construct an invalid fd, so that if the user later calls start(), it will fail with
1963 // ERROR_INVALID_SOCKET.
Chiachang Wangc5f86f42021-01-15 11:06:21 +08001964 dup = createInvalidFd();
junyulai61143782019-03-04 22:45:36 +08001965 }
1966 return new NattSocketKeepalive(mService, network, dup, socket.getResourceId(), source,
1967 destination, executor, callback);
junyulai48a59382019-01-15 11:32:44 +08001968 }
1969
1970 /**
1971 * Request that keepalives be started on a IPsec NAT-T socket file descriptor. Directly called
1972 * by system apps which don't use IpSecService to create {@link UdpEncapsulationSocket}.
1973 *
1974 * @param network The {@link Network} the socket is on.
junyulai61143782019-03-04 22:45:36 +08001975 * @param pfd The {@link ParcelFileDescriptor} that needs to be kept alive. The provided
1976 * {@link ParcelFileDescriptor} must be bound to a port and the keepalives will be sent
1977 * from that port.
junyulai48a59382019-01-15 11:32:44 +08001978 * @param source The source address of the {@link UdpEncapsulationSocket}.
1979 * @param destination The destination address of the {@link UdpEncapsulationSocket}. The
1980 * keepalive packets will always be sent to port 4500 of the given {@code destination}.
1981 * @param executor The executor on which callback will be invoked. The provided {@link Executor}
1982 * must run callback sequentially, otherwise the order of callbacks cannot be
1983 * guaranteed.
1984 * @param callback A {@link SocketKeepalive.Callback}. Used for notifications about keepalive
1985 * changes. Must be extended by applications that use this API.
1986 *
junyulaic7ea1242019-01-08 20:04:33 +08001987 * @return A {@link SocketKeepalive} object that can be used to control the keepalive on the
1988 * given socket.
junyulai48a59382019-01-15 11:32:44 +08001989 * @hide
1990 */
1991 @SystemApi
1992 @RequiresPermission(android.Manifest.permission.PACKET_KEEPALIVE_OFFLOAD)
junyulai61143782019-03-04 22:45:36 +08001993 public @NonNull SocketKeepalive createNattKeepalive(@NonNull Network network,
1994 @NonNull ParcelFileDescriptor pfd,
junyulai48a59382019-01-15 11:32:44 +08001995 @NonNull InetAddress source,
1996 @NonNull InetAddress destination,
1997 @NonNull @CallbackExecutor Executor executor,
1998 @NonNull Callback callback) {
junyulai61143782019-03-04 22:45:36 +08001999 ParcelFileDescriptor dup;
2000 try {
junyulaic4fb2482019-03-27 11:00:37 +08002001 // TODO: Consider remove unnecessary dup.
junyulai61143782019-03-04 22:45:36 +08002002 dup = pfd.dup();
2003 } catch (IOException ignored) {
2004 // Construct an invalid fd, so that if the user later calls start(), it will fail with
2005 // ERROR_INVALID_SOCKET.
Chiachang Wangc5f86f42021-01-15 11:06:21 +08002006 dup = createInvalidFd();
junyulai61143782019-03-04 22:45:36 +08002007 }
2008 return new NattSocketKeepalive(mService, network, dup,
Remi NGUYEN VAN642d6ab2021-03-11 10:56:49 +00002009 -1 /* Unused */, source, destination, executor, callback);
junyulaia86defc2018-12-27 17:25:29 +08002010 }
2011
2012 /**
junyulaic7ea1242019-01-08 20:04:33 +08002013 * Request that keepalives be started on a TCP socket.
2014 * The socket must be established.
2015 *
2016 * @param network The {@link Network} the socket is on.
2017 * @param socket The socket that needs to be kept alive.
2018 * @param executor The executor on which callback will be invoked. This implementation assumes
2019 * the provided {@link Executor} runs the callbacks in sequence with no
2020 * concurrency. Failing this, no guarantee of correctness can be made. It is
2021 * the responsibility of the caller to ensure the executor provides this
2022 * guarantee. A simple way of creating such an executor is with the standard
2023 * tool {@code Executors.newSingleThreadExecutor}.
2024 * @param callback A {@link SocketKeepalive.Callback}. Used for notifications about keepalive
2025 * changes. Must be extended by applications that use this API.
2026 *
2027 * @return A {@link SocketKeepalive} object that can be used to control the keepalive on the
2028 * given socket.
2029 * @hide
2030 */
2031 @SystemApi
2032 @RequiresPermission(android.Manifest.permission.PACKET_KEEPALIVE_OFFLOAD)
junyulai61143782019-03-04 22:45:36 +08002033 public @NonNull SocketKeepalive createSocketKeepalive(@NonNull Network network,
junyulaic7ea1242019-01-08 20:04:33 +08002034 @NonNull Socket socket,
2035 @NonNull Executor executor,
2036 @NonNull Callback callback) {
junyulai61143782019-03-04 22:45:36 +08002037 ParcelFileDescriptor dup;
2038 try {
2039 dup = ParcelFileDescriptor.fromSocket(socket);
2040 } catch (UncheckedIOException ignored) {
2041 // Construct an invalid fd, so that if the user later calls start(), it will fail with
2042 // ERROR_INVALID_SOCKET.
Chiachang Wangc5f86f42021-01-15 11:06:21 +08002043 dup = createInvalidFd();
junyulai61143782019-03-04 22:45:36 +08002044 }
2045 return new TcpSocketKeepalive(mService, network, dup, executor, callback);
junyulaic7ea1242019-01-08 20:04:33 +08002046 }
2047
2048 /**
The Android Open Source Project28527d22009-03-03 19:31:44 -08002049 * Ensure that a network route exists to deliver traffic to the specified
2050 * host via the specified network interface. An attempt to add a route that
2051 * already exists is ignored, but treated as successful.
Lorenzo Colitti91de63b2015-10-15 16:29:00 +09002052 *
2053 * <p>This method requires the caller to hold either the
2054 * {@link android.Manifest.permission#CHANGE_NETWORK_STATE} permission
2055 * or the ability to modify system settings as determined by
2056 * {@link android.provider.Settings.System#canWrite}.</p>
2057 *
The Android Open Source Project28527d22009-03-03 19:31:44 -08002058 * @param networkType the type of the network over which traffic to the specified
2059 * host is to be routed
2060 * @param hostAddress the IP address of the host to which the route is desired
2061 * @return {@code true} on success, {@code false} on failure
Robert Greenwaltd0ebdbb2014-05-18 23:07:25 -07002062 *
Lorenzo Colittiefd396e2015-04-24 17:03:31 +09002063 * @deprecated Deprecated in favor of the
2064 * {@link #requestNetwork(NetworkRequest, NetworkCallback)},
2065 * {@link #bindProcessToNetwork} and {@link Network#getSocketFactory} API.
Dianne Hackbornc16abda2015-07-31 10:35:34 -07002066 * In {@link VERSION_CODES#M}, and above, this method is unsupported and will
Lorenzo Colitti9b026fa2015-07-29 11:41:21 +09002067 * throw {@code UnsupportedOperationException} if called.
Lorenzo Colitti7cb18342016-12-09 18:39:30 +09002068 * @removed
The Android Open Source Project28527d22009-03-03 19:31:44 -08002069 */
Aurimas Liutikasa6bb0232016-05-24 15:22:55 -07002070 @Deprecated
The Android Open Source Project28527d22009-03-03 19:31:44 -08002071 public boolean requestRouteToHost(int networkType, int hostAddress) {
Sreeram Ramachandranc05e2a92014-07-19 23:21:46 -07002072 return requestRouteToHostAddress(networkType, NetworkUtils.intToInetAddress(hostAddress));
Robert Greenwalt7fe44cb2010-08-27 09:24:29 -07002073 }
2074
2075 /**
2076 * Ensure that a network route exists to deliver traffic to the specified
2077 * host via the specified network interface. An attempt to add a route that
2078 * already exists is ignored, but treated as successful.
Lorenzo Colitti91de63b2015-10-15 16:29:00 +09002079 *
2080 * <p>This method requires the caller to hold either the
2081 * {@link android.Manifest.permission#CHANGE_NETWORK_STATE} permission
2082 * or the ability to modify system settings as determined by
2083 * {@link android.provider.Settings.System#canWrite}.</p>
2084 *
Robert Greenwalt7fe44cb2010-08-27 09:24:29 -07002085 * @param networkType the type of the network over which traffic to the specified
2086 * host is to be routed
2087 * @param hostAddress the IP address of the host to which the route is desired
2088 * @return {@code true} on success, {@code false} on failure
2089 * @hide
Robert Greenwaltd0ebdbb2014-05-18 23:07:25 -07002090 * @deprecated Deprecated in favor of the {@link #requestNetwork} and
Lorenzo Colittiefd396e2015-04-24 17:03:31 +09002091 * {@link #bindProcessToNetwork} API.
Robert Greenwalt7fe44cb2010-08-27 09:24:29 -07002092 */
Aurimas Liutikasa6bb0232016-05-24 15:22:55 -07002093 @Deprecated
Mathew Inwood0b8f8612018-08-08 14:52:47 +01002094 @UnsupportedAppUsage
Robert Greenwalt7fe44cb2010-08-27 09:24:29 -07002095 public boolean requestRouteToHostAddress(int networkType, InetAddress hostAddress) {
Lorenzo Colitti9b026fa2015-07-29 11:41:21 +09002096 checkLegacyRoutingApiAccess();
The Android Open Source Project28527d22009-03-03 19:31:44 -08002097 try {
Philip P. Moltmannbbf050b2020-03-26 11:50:35 -07002098 return mService.requestRouteToHostAddress(networkType, hostAddress.getAddress(),
2099 mContext.getOpPackageName(), getAttributionTag());
The Android Open Source Project28527d22009-03-03 19:31:44 -08002100 } catch (RemoteException e) {
Jeff Sharkeya6066fd2016-03-01 19:27:23 -07002101 throw e.rethrowFromSystemServer();
The Android Open Source Project28527d22009-03-03 19:31:44 -08002102 }
2103 }
2104
2105 /**
Philip P. Moltmannbbf050b2020-03-26 11:50:35 -07002106 * @return the context's attribution tag
2107 */
2108 // TODO: Remove method and replace with direct call once R code is pushed to AOSP
2109 private @Nullable String getAttributionTag() {
Roshan Piusc97d8062020-12-17 14:53:09 -08002110 return mContext.getAttributionTag();
Philip P. Moltmannbbf050b2020-03-26 11:50:35 -07002111 }
2112
2113 /**
The Android Open Source Project28527d22009-03-03 19:31:44 -08002114 * Returns the value of the setting for background data usage. If false,
2115 * applications should not use the network if the application is not in the
2116 * foreground. Developers should respect this setting, and check the value
2117 * of this before performing any background data operations.
2118 * <p>
2119 * All applications that have background services that use the network
2120 * should listen to {@link #ACTION_BACKGROUND_DATA_SETTING_CHANGED}.
Jeff Sharkey39c01eb2011-08-16 14:37:57 -07002121 * <p>
Scott Main50589142011-10-06 18:32:43 -07002122 * @deprecated As of {@link VERSION_CODES#ICE_CREAM_SANDWICH}, availability of
Jeff Sharkey39c01eb2011-08-16 14:37:57 -07002123 * background data depends on several combined factors, and this method will
2124 * always return {@code true}. Instead, when background data is unavailable,
2125 * {@link #getActiveNetworkInfo()} will now appear disconnected.
Danica Chang96567052010-08-11 14:54:43 -07002126 *
The Android Open Source Project28527d22009-03-03 19:31:44 -08002127 * @return Whether background data usage is allowed.
2128 */
Jeff Sharkey39c01eb2011-08-16 14:37:57 -07002129 @Deprecated
The Android Open Source Project28527d22009-03-03 19:31:44 -08002130 public boolean getBackgroundDataSetting() {
Jeff Sharkey39c01eb2011-08-16 14:37:57 -07002131 // assume that background data is allowed; final authority is
2132 // NetworkInfo which may be blocked.
2133 return true;
The Android Open Source Project28527d22009-03-03 19:31:44 -08002134 }
2135
2136 /**
2137 * Sets the value of the setting for background data usage.
Robert Greenwalt1b21f6c2010-02-23 18:58:05 -08002138 *
The Android Open Source Project28527d22009-03-03 19:31:44 -08002139 * @param allowBackgroundData Whether an application should use data while
2140 * it is in the background.
Robert Greenwalt1b21f6c2010-02-23 18:58:05 -08002141 *
The Android Open Source Project28527d22009-03-03 19:31:44 -08002142 * @attr ref android.Manifest.permission#CHANGE_BACKGROUND_DATA_SETTING
2143 * @see #getBackgroundDataSetting()
2144 * @hide
2145 */
Jeff Sharkey39c01eb2011-08-16 14:37:57 -07002146 @Deprecated
Mathew Inwood0b8f8612018-08-08 14:52:47 +01002147 @UnsupportedAppUsage
The Android Open Source Project28527d22009-03-03 19:31:44 -08002148 public void setBackgroundDataSetting(boolean allowBackgroundData) {
Jeff Sharkey39c01eb2011-08-16 14:37:57 -07002149 // ignored
The Android Open Source Project28527d22009-03-03 19:31:44 -08002150 }
Robert Greenwalt1b21f6c2010-02-23 18:58:05 -08002151
Jeff Sharkey66fa9682011-08-02 17:22:34 -07002152 /**
Robert Greenwalt1b21f6c2010-02-23 18:58:05 -08002153 * @hide
Robert Greenwalt3d6c9582014-05-21 20:04:36 -07002154 * @deprecated Talk to TelephonyManager directly
Robert Greenwalt1b21f6c2010-02-23 18:58:05 -08002155 */
Aurimas Liutikasa6bb0232016-05-24 15:22:55 -07002156 @Deprecated
Mathew Inwood0b8f8612018-08-08 14:52:47 +01002157 @UnsupportedAppUsage
Robert Greenwalt1b21f6c2010-02-23 18:58:05 -08002158 public boolean getMobileDataEnabled() {
Meng Wang91311c02019-11-18 17:10:00 -08002159 TelephonyManager tm = mContext.getSystemService(TelephonyManager.class);
2160 if (tm != null) {
2161 int subId = SubscriptionManager.getDefaultDataSubscriptionId();
2162 Log.d("ConnectivityManager", "getMobileDataEnabled()+ subId=" + subId);
2163 boolean retVal = tm.createForSubscriptionId(subId).isDataEnabled();
2164 Log.d("ConnectivityManager", "getMobileDataEnabled()- subId=" + subId
2165 + " retVal=" + retVal);
2166 return retVal;
Robert Greenwalt1b21f6c2010-02-23 18:58:05 -08002167 }
Wink Saville02eb35c2014-12-05 11:10:30 -08002168 Log.d("ConnectivityManager", "getMobileDataEnabled()- remote exception retVal=false");
Robert Greenwalt3d6c9582014-05-21 20:04:36 -07002169 return false;
Robert Greenwalt1b21f6c2010-02-23 18:58:05 -08002170 }
2171
The Android Open Source Project28527d22009-03-03 19:31:44 -08002172 /**
Robert Greenwalt2e4731f2014-09-04 16:44:35 -07002173 * Callback for use with {@link ConnectivityManager#addDefaultNetworkActiveListener}
Robert Greenwalt09e5cff2014-06-11 16:05:07 -07002174 * to find out when the system default network has gone in to a high power state.
Dianne Hackbornb4b09e82014-02-26 16:20:52 -08002175 */
2176 public interface OnNetworkActiveListener {
2177 /**
2178 * Called on the main thread of the process to report that the current data network
2179 * has become active, and it is now a good time to perform any pending network
2180 * operations. Note that this listener only tells you when the network becomes
2181 * active; if at any other time you want to know whether it is active (and thus okay
2182 * to initiate network traffic), you can retrieve its instantaneous state with
Robert Greenwalt09e5cff2014-06-11 16:05:07 -07002183 * {@link ConnectivityManager#isDefaultNetworkActive}.
Dianne Hackbornb4b09e82014-02-26 16:20:52 -08002184 */
Chalard Jean73d9db72018-06-04 16:52:49 +09002185 void onNetworkActive();
Dianne Hackbornb4b09e82014-02-26 16:20:52 -08002186 }
2187
Dianne Hackbornb4b09e82014-02-26 16:20:52 -08002188 private final ArrayMap<OnNetworkActiveListener, INetworkActivityListener>
Chalard Jean73d9db72018-06-04 16:52:49 +09002189 mNetworkActivityListeners = new ArrayMap<>();
Dianne Hackbornb4b09e82014-02-26 16:20:52 -08002190
2191 /**
Robert Greenwalt09e5cff2014-06-11 16:05:07 -07002192 * Start listening to reports when the system's default data network is active, meaning it is
2193 * a good time to perform network traffic. Use {@link #isDefaultNetworkActive()}
2194 * to determine the current state of the system's default network after registering the
2195 * listener.
2196 * <p>
2197 * If the process default network has been set with
Paul Jensen895c3942015-03-10 10:54:12 -04002198 * {@link ConnectivityManager#bindProcessToNetwork} this function will not
Robert Greenwalt09e5cff2014-06-11 16:05:07 -07002199 * reflect the process's default, but the system default.
Dianne Hackbornb4b09e82014-02-26 16:20:52 -08002200 *
2201 * @param l The listener to be told when the network is active.
2202 */
Robert Greenwalt2e4731f2014-09-04 16:44:35 -07002203 public void addDefaultNetworkActiveListener(final OnNetworkActiveListener l) {
Dianne Hackbornb4b09e82014-02-26 16:20:52 -08002204 INetworkActivityListener rl = new INetworkActivityListener.Stub() {
2205 @Override
2206 public void onNetworkActive() throws RemoteException {
2207 l.onNetworkActive();
2208 }
2209 };
2210
2211 try {
lucaslina5e9bdb2021-01-21 02:04:15 +08002212 mService.registerNetworkActivityListener(rl);
Dianne Hackbornb4b09e82014-02-26 16:20:52 -08002213 mNetworkActivityListeners.put(l, rl);
2214 } catch (RemoteException e) {
Jeff Sharkeya6066fd2016-03-01 19:27:23 -07002215 throw e.rethrowFromSystemServer();
Dianne Hackbornb4b09e82014-02-26 16:20:52 -08002216 }
2217 }
2218
2219 /**
2220 * Remove network active listener previously registered with
Robert Greenwalt2e4731f2014-09-04 16:44:35 -07002221 * {@link #addDefaultNetworkActiveListener}.
Dianne Hackbornb4b09e82014-02-26 16:20:52 -08002222 *
2223 * @param l Previously registered listener.
2224 */
Chalard Jean42a9c292019-01-07 19:26:34 +09002225 public void removeDefaultNetworkActiveListener(@NonNull OnNetworkActiveListener l) {
Dianne Hackbornb4b09e82014-02-26 16:20:52 -08002226 INetworkActivityListener rl = mNetworkActivityListeners.get(l);
Remi NGUYEN VAN1c17c882021-03-11 20:49:13 +09002227 if (rl == null) {
2228 throw new IllegalArgumentException("Listener was not registered.");
2229 }
Dianne Hackbornb4b09e82014-02-26 16:20:52 -08002230 try {
lucaslina5e9bdb2021-01-21 02:04:15 +08002231 mService.registerNetworkActivityListener(rl);
Dianne Hackbornb4b09e82014-02-26 16:20:52 -08002232 } catch (RemoteException e) {
Jeff Sharkeya6066fd2016-03-01 19:27:23 -07002233 throw e.rethrowFromSystemServer();
Dianne Hackbornb4b09e82014-02-26 16:20:52 -08002234 }
2235 }
2236
2237 /**
2238 * Return whether the data network is currently active. An active network means that
2239 * it is currently in a high power state for performing data transmission. On some
2240 * types of networks, it may be expensive to move and stay in such a state, so it is
2241 * more power efficient to batch network traffic together when the radio is already in
2242 * this state. This method tells you whether right now is currently a good time to
2243 * initiate network traffic, as the network is already active.
2244 */
Robert Greenwalt09e5cff2014-06-11 16:05:07 -07002245 public boolean isDefaultNetworkActive() {
Dianne Hackbornb4b09e82014-02-26 16:20:52 -08002246 try {
lucaslina5e9bdb2021-01-21 02:04:15 +08002247 return mService.isDefaultNetworkActive();
Dianne Hackbornb4b09e82014-02-26 16:20:52 -08002248 } catch (RemoteException e) {
Jeff Sharkeya6066fd2016-03-01 19:27:23 -07002249 throw e.rethrowFromSystemServer();
Dianne Hackbornb4b09e82014-02-26 16:20:52 -08002250 }
Dianne Hackbornb4b09e82014-02-26 16:20:52 -08002251 }
2252
2253 /**
The Android Open Source Project28527d22009-03-03 19:31:44 -08002254 * {@hide}
2255 */
Lorenzo Colitti9b026fa2015-07-29 11:41:21 +09002256 public ConnectivityManager(Context context, IConnectivityManager service) {
Remi NGUYEN VAN1c17c882021-03-11 20:49:13 +09002257 mContext = Objects.requireNonNull(context, "missing context");
2258 mService = Objects.requireNonNull(service, "missing IConnectivityManager");
Amos Bianchiddadca42020-03-04 11:07:38 -08002259 mTetheringManager = (TetheringManager) mContext.getSystemService(Context.TETHERING_SERVICE);
Paul Jensen12131352014-12-10 15:12:18 -05002260 sInstance = this;
The Android Open Source Project28527d22009-03-03 19:31:44 -08002261 }
Robert Greenwalt0c4828c2010-01-26 11:40:34 -08002262
Jeff Sharkey2d9e5a52012-04-04 20:40:58 -07002263 /** {@hide} */
Mathew Inwood0b8f8612018-08-08 14:52:47 +01002264 @UnsupportedAppUsage
Jeff Sharkey2d9e5a52012-04-04 20:40:58 -07002265 public static ConnectivityManager from(Context context) {
2266 return (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
2267 }
2268
Remi NGUYEN VAN1becbe72018-12-27 16:43:56 +09002269 /** @hide */
2270 public NetworkRequest getDefaultRequest() {
2271 try {
2272 // This is not racy as the default request is final in ConnectivityService.
2273 return mService.getDefaultRequest();
2274 } catch (RemoteException e) {
2275 throw e.rethrowFromSystemServer();
2276 }
2277 }
2278
Robert Greenwalt0c4828c2010-01-26 11:40:34 -08002279 /**
Philip P. Moltmannbbf050b2020-03-26 11:50:35 -07002280 * Check if the package is a allowed to write settings. This also accounts that such an access
2281 * happened.
2282 *
2283 * @return {@code true} iff the package is allowed to write settings.
2284 */
2285 // TODO: Remove method and replace with direct call once R code is pushed to AOSP
2286 private static boolean checkAndNoteWriteSettingsOperation(@NonNull Context context, int uid,
2287 @NonNull String callingPackage, @Nullable String callingAttributionTag,
2288 boolean throwException) {
2289 return Settings.checkAndNoteWriteSettingsOperation(context, uid, callingPackage,
2290 throwException);
2291 }
2292
2293 /**
Paul Jensen12131352014-12-10 15:12:18 -05002294 * @deprecated - use getSystemService. This is a kludge to support static access in certain
2295 * situations where a Context pointer is unavailable.
2296 * @hide
2297 */
Aurimas Liutikasa6bb0232016-05-24 15:22:55 -07002298 @Deprecated
Paul Jensen895c3942015-03-10 10:54:12 -04002299 static ConnectivityManager getInstanceOrNull() {
2300 return sInstance;
2301 }
2302
2303 /**
2304 * @deprecated - use getSystemService. This is a kludge to support static access in certain
2305 * situations where a Context pointer is unavailable.
2306 * @hide
2307 */
Aurimas Liutikasa6bb0232016-05-24 15:22:55 -07002308 @Deprecated
Mathew Inwood0b8f8612018-08-08 14:52:47 +01002309 @UnsupportedAppUsage
Paul Jensen895c3942015-03-10 10:54:12 -04002310 private static ConnectivityManager getInstance() {
2311 if (getInstanceOrNull() == null) {
Paul Jensen12131352014-12-10 15:12:18 -05002312 throw new IllegalStateException("No ConnectivityManager yet constructed");
2313 }
Paul Jensen895c3942015-03-10 10:54:12 -04002314 return getInstanceOrNull();
Paul Jensen12131352014-12-10 15:12:18 -05002315 }
2316
2317 /**
Robert Greenwalt26744a52013-02-15 10:56:35 -08002318 * Get the set of tetherable, available interfaces. This list is limited by
2319 * device configuration and current interface existence.
2320 *
2321 * @return an array of 0 or more Strings of tetherable interface names.
2322 *
markchieneeea0be2020-01-21 13:11:06 +08002323 * @deprecated Use {@link TetheringEventCallback#onTetherableInterfacesChanged(List)} instead.
Robert Greenwalt0c4828c2010-01-26 11:40:34 -08002324 * {@hide}
2325 */
Jeff Sharkey2ac62992017-04-24 11:18:03 -06002326 @RequiresPermission(android.Manifest.permission.ACCESS_NETWORK_STATE)
Mathew Inwood0b8f8612018-08-08 14:52:47 +01002327 @UnsupportedAppUsage
markchieneeea0be2020-01-21 13:11:06 +08002328 @Deprecated
Robert Greenwalt0c4828c2010-01-26 11:40:34 -08002329 public String[] getTetherableIfaces() {
Amos Bianchiddadca42020-03-04 11:07:38 -08002330 return mTetheringManager.getTetherableIfaces();
Robert Greenwalt0c4828c2010-01-26 11:40:34 -08002331 }
2332
2333 /**
Robert Greenwalt26744a52013-02-15 10:56:35 -08002334 * Get the set of tethered interfaces.
2335 *
2336 * @return an array of 0 or more String of currently tethered interface names.
2337 *
markchieneeea0be2020-01-21 13:11:06 +08002338 * @deprecated Use {@link TetheringEventCallback#onTetherableInterfacesChanged(List)} instead.
Robert Greenwalt0c4828c2010-01-26 11:40:34 -08002339 * {@hide}
2340 */
Jeff Sharkey2ac62992017-04-24 11:18:03 -06002341 @RequiresPermission(android.Manifest.permission.ACCESS_NETWORK_STATE)
Mathew Inwood0b8f8612018-08-08 14:52:47 +01002342 @UnsupportedAppUsage
markchieneeea0be2020-01-21 13:11:06 +08002343 @Deprecated
Robert Greenwalt0c4828c2010-01-26 11:40:34 -08002344 public String[] getTetheredIfaces() {
Amos Bianchiddadca42020-03-04 11:07:38 -08002345 return mTetheringManager.getTetheredIfaces();
Robert Greenwalt0c4828c2010-01-26 11:40:34 -08002346 }
2347
2348 /**
Robert Greenwalt26744a52013-02-15 10:56:35 -08002349 * Get the set of interface names which attempted to tether but
2350 * failed. Re-attempting to tether may cause them to reset to the Tethered
2351 * state. Alternatively, causing the interface to be destroyed and recreated
2352 * may cause them to reset to the available state.
2353 * {@link ConnectivityManager#getLastTetherError} can be used to get more
2354 * information on the cause of the errors.
2355 *
2356 * @return an array of 0 or more String indicating the interface names
2357 * which failed to tether.
2358 *
markchieneeea0be2020-01-21 13:11:06 +08002359 * @deprecated Use {@link TetheringEventCallback#onError(String, int)} instead.
Robert Greenwalt0c4828c2010-01-26 11:40:34 -08002360 * {@hide}
2361 */
Jeff Sharkey2ac62992017-04-24 11:18:03 -06002362 @RequiresPermission(android.Manifest.permission.ACCESS_NETWORK_STATE)
Mathew Inwood0b8f8612018-08-08 14:52:47 +01002363 @UnsupportedAppUsage
markchieneeea0be2020-01-21 13:11:06 +08002364 @Deprecated
Robert Greenwalt4283ded2010-03-02 17:25:02 -08002365 public String[] getTetheringErroredIfaces() {
Amos Bianchiddadca42020-03-04 11:07:38 -08002366 return mTetheringManager.getTetheringErroredIfaces();
Robert Greenwalt0c4828c2010-01-26 11:40:34 -08002367 }
2368
2369 /**
Robert Greenwalta75c4652014-06-23 14:53:42 -07002370 * Get the set of tethered dhcp ranges.
2371 *
markchien218230f2020-02-14 11:55:48 +08002372 * @deprecated This method is not supported.
2373 * TODO: remove this function when all of clients are removed.
Robert Greenwalta75c4652014-06-23 14:53:42 -07002374 * {@hide}
2375 */
paulhud70b7dd2019-08-12 16:25:11 +08002376 @RequiresPermission(android.Manifest.permission.NETWORK_SETTINGS)
markchieneeea0be2020-01-21 13:11:06 +08002377 @Deprecated
Robert Greenwalta75c4652014-06-23 14:53:42 -07002378 public String[] getTetheredDhcpRanges() {
markchien218230f2020-02-14 11:55:48 +08002379 throw new UnsupportedOperationException("getTetheredDhcpRanges is not supported");
Robert Greenwalta75c4652014-06-23 14:53:42 -07002380 }
2381
2382 /**
Robert Greenwalt26744a52013-02-15 10:56:35 -08002383 * Attempt to tether the named interface. This will setup a dhcp server
2384 * on the interface, forward and NAT IP packets and forward DNS requests
2385 * to the best active upstream network interface. Note that if no upstream
2386 * IP network interface is available, dhcp will still run and traffic will be
2387 * allowed between the tethered devices and this device, though upstream net
2388 * access will of course fail until an upstream network interface becomes
2389 * active.
Lorenzo Colitti91de63b2015-10-15 16:29:00 +09002390 *
2391 * <p>This method requires the caller to hold either the
2392 * {@link android.Manifest.permission#CHANGE_NETWORK_STATE} permission
2393 * or the ability to modify system settings as determined by
2394 * {@link android.provider.Settings.System#canWrite}.</p>
Robert Greenwalt26744a52013-02-15 10:56:35 -08002395 *
Jeremy Klein9a36ec82016-01-22 14:11:45 -08002396 * <p>WARNING: New clients should not use this function. The only usages should be in PanService
2397 * and WifiStateMachine which need direct access. All other clients should use
2398 * {@link #startTethering} and {@link #stopTethering} which encapsulate proper provisioning
2399 * logic.</p>
2400 *
Robert Greenwalt26744a52013-02-15 10:56:35 -08002401 * @param iface the interface name to tether.
2402 * @return error a {@code TETHER_ERROR} value indicating success or failure type
markchiencc1bc1b2020-01-20 19:31:56 +08002403 * @deprecated Use {@link TetheringManager#startTethering} instead
Robert Greenwalt26744a52013-02-15 10:56:35 -08002404 *
Robert Greenwalt0c4828c2010-01-26 11:40:34 -08002405 * {@hide}
2406 */
Mathew Inwood5a09a712020-11-04 09:29:36 +00002407 @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.R, trackingBug = 170729553)
markchiencc1bc1b2020-01-20 19:31:56 +08002408 @Deprecated
Robert Greenwalt4283ded2010-03-02 17:25:02 -08002409 public int tether(String iface) {
Amos Bianchiddadca42020-03-04 11:07:38 -08002410 return mTetheringManager.tether(iface);
Robert Greenwalt4283ded2010-03-02 17:25:02 -08002411 }
2412
2413 /**
Robert Greenwalt26744a52013-02-15 10:56:35 -08002414 * Stop tethering the named interface.
Lorenzo Colitti91de63b2015-10-15 16:29:00 +09002415 *
2416 * <p>This method requires the caller to hold either the
2417 * {@link android.Manifest.permission#CHANGE_NETWORK_STATE} permission
2418 * or the ability to modify system settings as determined by
2419 * {@link android.provider.Settings.System#canWrite}.</p>
Robert Greenwalt26744a52013-02-15 10:56:35 -08002420 *
Jeremy Klein9a36ec82016-01-22 14:11:45 -08002421 * <p>WARNING: New clients should not use this function. The only usages should be in PanService
2422 * and WifiStateMachine which need direct access. All other clients should use
2423 * {@link #startTethering} and {@link #stopTethering} which encapsulate proper provisioning
2424 * logic.</p>
2425 *
Robert Greenwalt26744a52013-02-15 10:56:35 -08002426 * @param iface the interface name to untether.
2427 * @return error a {@code TETHER_ERROR} value indicating success or failure type
2428 *
Robert Greenwalt4283ded2010-03-02 17:25:02 -08002429 * {@hide}
2430 */
Mathew Inwood0b8f8612018-08-08 14:52:47 +01002431 @UnsupportedAppUsage
markchieneeea0be2020-01-21 13:11:06 +08002432 @Deprecated
Robert Greenwalt4283ded2010-03-02 17:25:02 -08002433 public int untether(String iface) {
Amos Bianchiddadca42020-03-04 11:07:38 -08002434 return mTetheringManager.untether(iface);
Robert Greenwalt0c4828c2010-01-26 11:40:34 -08002435 }
Robert Greenwalt8e87f122010-02-11 18:18:40 -08002436
2437 /**
Robert Greenwalt26744a52013-02-15 10:56:35 -08002438 * Check if the device allows for tethering. It may be disabled via
Dianne Hackbornb4b09e82014-02-26 16:20:52 -08002439 * {@code ro.tether.denied} system property, Settings.TETHER_SUPPORTED or
Robert Greenwalt26744a52013-02-15 10:56:35 -08002440 * due to device configuration.
2441 *
Chalard Jean4e1bb722017-09-26 15:45:18 +09002442 * <p>If this app does not have permission to use this API, it will always
2443 * return false rather than throw an exception.</p>
2444 *
2445 * <p>If the device has a hotspot provisioning app, the caller is required to hold the
2446 * {@link android.Manifest.permission.TETHER_PRIVILEGED} permission.</p>
2447 *
2448 * <p>Otherwise, this method requires the caller to hold the ability to modify system
2449 * settings as determined by {@link android.provider.Settings.System#canWrite}.</p>
2450 *
Robert Greenwalt26744a52013-02-15 10:56:35 -08002451 * @return a boolean - {@code true} indicating Tethering is supported.
2452 *
markchieneeea0be2020-01-21 13:11:06 +08002453 * @deprecated Use {@link TetheringEventCallback#onTetheringSupported(boolean)} instead.
Robert Greenwalt8e87f122010-02-11 18:18:40 -08002454 * {@hide}
2455 */
Jeremy Klein9a36ec82016-01-22 14:11:45 -08002456 @SystemApi
Chalard Jean4e1bb722017-09-26 15:45:18 +09002457 @RequiresPermission(anyOf = {android.Manifest.permission.TETHER_PRIVILEGED,
2458 android.Manifest.permission.WRITE_SETTINGS})
Robert Greenwalt8e87f122010-02-11 18:18:40 -08002459 public boolean isTetheringSupported() {
Amos Bianchiddadca42020-03-04 11:07:38 -08002460 return mTetheringManager.isTetheringSupported();
Robert Greenwalt8e87f122010-02-11 18:18:40 -08002461 }
2462
2463 /**
Jeremy Klein9a36ec82016-01-22 14:11:45 -08002464 * Callback for use with {@link #startTethering} to find out whether tethering succeeded.
markchiencc1bc1b2020-01-20 19:31:56 +08002465 *
2466 * @deprecated Use {@link TetheringManager.StartTetheringCallback} instead.
Jeremy Klein9a36ec82016-01-22 14:11:45 -08002467 * @hide
2468 */
2469 @SystemApi
markchiencc1bc1b2020-01-20 19:31:56 +08002470 @Deprecated
Jeremy Klein9a36ec82016-01-22 14:11:45 -08002471 public static abstract class OnStartTetheringCallback {
2472 /**
2473 * Called when tethering has been successfully started.
2474 */
Chalard Jean73d9db72018-06-04 16:52:49 +09002475 public void onTetheringStarted() {}
Jeremy Klein9a36ec82016-01-22 14:11:45 -08002476
2477 /**
2478 * Called when starting tethering failed.
2479 */
Chalard Jean73d9db72018-06-04 16:52:49 +09002480 public void onTetheringFailed() {}
Jeremy Klein9a36ec82016-01-22 14:11:45 -08002481 }
2482
2483 /**
2484 * Convenient overload for
2485 * {@link #startTethering(int, boolean, OnStartTetheringCallback, Handler)} which passes a null
2486 * handler to run on the current thread's {@link Looper}.
markchiencc1bc1b2020-01-20 19:31:56 +08002487 *
2488 * @deprecated Use {@link TetheringManager#startTethering} instead.
Jeremy Klein9a36ec82016-01-22 14:11:45 -08002489 * @hide
2490 */
2491 @SystemApi
markchiencc1bc1b2020-01-20 19:31:56 +08002492 @Deprecated
Udam Saini15072332017-06-07 12:06:28 -07002493 @RequiresPermission(android.Manifest.permission.TETHER_PRIVILEGED)
Jeremy Klein9a36ec82016-01-22 14:11:45 -08002494 public void startTethering(int type, boolean showProvisioningUi,
2495 final OnStartTetheringCallback callback) {
2496 startTethering(type, showProvisioningUi, callback, null);
2497 }
2498
2499 /**
2500 * Runs tether provisioning for the given type if needed and then starts tethering if
2501 * the check succeeds. If no carrier provisioning is required for tethering, tethering is
2502 * enabled immediately. If provisioning fails, tethering will not be enabled. It also
2503 * schedules tether provisioning re-checks if appropriate.
2504 *
2505 * @param type The type of tethering to start. Must be one of
2506 * {@link ConnectivityManager.TETHERING_WIFI},
2507 * {@link ConnectivityManager.TETHERING_USB}, or
2508 * {@link ConnectivityManager.TETHERING_BLUETOOTH}.
2509 * @param showProvisioningUi a boolean indicating to show the provisioning app UI if there
2510 * is one. This should be true the first time this function is called and also any time
2511 * the user can see this UI. It gives users information from their carrier about the
2512 * check failing and how they can sign up for tethering if possible.
2513 * @param callback an {@link OnStartTetheringCallback} which will be called to notify the caller
2514 * of the result of trying to tether.
2515 * @param handler {@link Handler} to specify the thread upon which the callback will be invoked.
markchiencc1bc1b2020-01-20 19:31:56 +08002516 *
2517 * @deprecated Use {@link TetheringManager#startTethering} instead.
Jeremy Klein9a36ec82016-01-22 14:11:45 -08002518 * @hide
2519 */
2520 @SystemApi
markchiencc1bc1b2020-01-20 19:31:56 +08002521 @Deprecated
Jeff Sharkeyb8c73032017-06-02 17:36:26 -06002522 @RequiresPermission(android.Manifest.permission.TETHER_PRIVILEGED)
Jeremy Klein9a36ec82016-01-22 14:11:45 -08002523 public void startTethering(int type, boolean showProvisioningUi,
2524 final OnStartTetheringCallback callback, Handler handler) {
Remi NGUYEN VAN1c17c882021-03-11 20:49:13 +09002525 Objects.requireNonNull(callback, "OnStartTetheringCallback cannot be null.");
Jeremy Klein49a57952016-03-12 16:29:54 -08002526
markchiencc1bc1b2020-01-20 19:31:56 +08002527 final Executor executor = new Executor() {
Jeremy Klein9a36ec82016-01-22 14:11:45 -08002528 @Override
markchiencc1bc1b2020-01-20 19:31:56 +08002529 public void execute(Runnable command) {
2530 if (handler == null) {
2531 command.run();
Jeremy Klein9a36ec82016-01-22 14:11:45 -08002532 } else {
markchiencc1bc1b2020-01-20 19:31:56 +08002533 handler.post(command);
Jeremy Klein9a36ec82016-01-22 14:11:45 -08002534 }
2535 }
2536 };
Jeremy Klein49a57952016-03-12 16:29:54 -08002537
markchiencc1bc1b2020-01-20 19:31:56 +08002538 final StartTetheringCallback tetheringCallback = new StartTetheringCallback() {
2539 @Override
2540 public void onTetheringStarted() {
2541 callback.onTetheringStarted();
2542 }
2543
2544 @Override
markchien19336c22020-03-19 13:37:43 +08002545 public void onTetheringFailed(final int error) {
markchiencc1bc1b2020-01-20 19:31:56 +08002546 callback.onTetheringFailed();
2547 }
2548 };
2549
2550 final TetheringRequest request = new TetheringRequest.Builder(type)
markchien19336c22020-03-19 13:37:43 +08002551 .setShouldShowEntitlementUi(showProvisioningUi).build();
markchiencc1bc1b2020-01-20 19:31:56 +08002552
Amos Bianchiddadca42020-03-04 11:07:38 -08002553 mTetheringManager.startTethering(request, executor, tetheringCallback);
Jeremy Klein9a36ec82016-01-22 14:11:45 -08002554 }
2555
2556 /**
2557 * Stops tethering for the given type. Also cancels any provisioning rechecks for that type if
2558 * applicable.
2559 *
2560 * @param type The type of tethering to stop. Must be one of
2561 * {@link ConnectivityManager.TETHERING_WIFI},
2562 * {@link ConnectivityManager.TETHERING_USB}, or
2563 * {@link ConnectivityManager.TETHERING_BLUETOOTH}.
markchieneeea0be2020-01-21 13:11:06 +08002564 *
2565 * @deprecated Use {@link TetheringManager#stopTethering} instead.
Jeremy Klein9a36ec82016-01-22 14:11:45 -08002566 * @hide
2567 */
2568 @SystemApi
markchieneeea0be2020-01-21 13:11:06 +08002569 @Deprecated
Jeff Sharkeyb8c73032017-06-02 17:36:26 -06002570 @RequiresPermission(android.Manifest.permission.TETHER_PRIVILEGED)
Jeremy Klein9a36ec82016-01-22 14:11:45 -08002571 public void stopTethering(int type) {
Amos Bianchiddadca42020-03-04 11:07:38 -08002572 mTetheringManager.stopTethering(type);
Jeremy Klein9a36ec82016-01-22 14:11:45 -08002573 }
2574
2575 /**
markchiena005c992019-02-27 14:56:11 +08002576 * Callback for use with {@link registerTetheringEventCallback} to find out tethering
2577 * upstream status.
2578 *
Nathan Haroldbbb48c62020-01-23 18:03:46 -08002579 * @deprecated Use {@link TetheringManager#OnTetheringEventCallback} instead.
markchieneeea0be2020-01-21 13:11:06 +08002580 * @hide
markchiena005c992019-02-27 14:56:11 +08002581 */
2582 @SystemApi
markchieneeea0be2020-01-21 13:11:06 +08002583 @Deprecated
markchiena005c992019-02-27 14:56:11 +08002584 public abstract static class OnTetheringEventCallback {
2585
2586 /**
2587 * Called when tethering upstream changed. This can be called multiple times and can be
2588 * called any time.
2589 *
2590 * @param network the {@link Network} of tethering upstream. Null means tethering doesn't
2591 * have any upstream.
2592 */
2593 public void onUpstreamChanged(@Nullable Network network) {}
2594 }
2595
markchieneeea0be2020-01-21 13:11:06 +08002596 @GuardedBy("mTetheringEventCallbacks")
2597 private final ArrayMap<OnTetheringEventCallback, TetheringEventCallback>
2598 mTetheringEventCallbacks = new ArrayMap<>();
2599
markchiena005c992019-02-27 14:56:11 +08002600 /**
2601 * Start listening to tethering change events. Any new added callback will receive the last
markchiend378cd32019-04-03 10:43:09 +08002602 * tethering status right away. If callback is registered when tethering has no upstream or
markchiena005c992019-02-27 14:56:11 +08002603 * disabled, {@link OnTetheringEventCallback#onUpstreamChanged} will immediately be called
2604 * with a null argument. The same callback object cannot be registered twice.
2605 *
2606 * @param executor the executor on which callback will be invoked.
2607 * @param callback the callback to be called when tethering has change events.
markchieneeea0be2020-01-21 13:11:06 +08002608 *
Nathan Haroldbbb48c62020-01-23 18:03:46 -08002609 * @deprecated Use {@link TetheringManager#registerTetheringEventCallback} instead.
markchiena005c992019-02-27 14:56:11 +08002610 * @hide
2611 */
2612 @SystemApi
markchieneeea0be2020-01-21 13:11:06 +08002613 @Deprecated
markchiena005c992019-02-27 14:56:11 +08002614 @RequiresPermission(android.Manifest.permission.TETHER_PRIVILEGED)
2615 public void registerTetheringEventCallback(
2616 @NonNull @CallbackExecutor Executor executor,
2617 @NonNull final OnTetheringEventCallback callback) {
Remi NGUYEN VAN1c17c882021-03-11 20:49:13 +09002618 Objects.requireNonNull(callback, "OnTetheringEventCallback cannot be null.");
markchiena005c992019-02-27 14:56:11 +08002619
markchieneeea0be2020-01-21 13:11:06 +08002620 final TetheringEventCallback tetherCallback =
2621 new TetheringEventCallback() {
2622 @Override
2623 public void onUpstreamChanged(@Nullable Network network) {
2624 callback.onUpstreamChanged(network);
2625 }
2626 };
2627
2628 synchronized (mTetheringEventCallbacks) {
2629 mTetheringEventCallbacks.put(callback, tetherCallback);
Amos Bianchiddadca42020-03-04 11:07:38 -08002630 mTetheringManager.registerTetheringEventCallback(executor, tetherCallback);
markchieneeea0be2020-01-21 13:11:06 +08002631 }
markchiena005c992019-02-27 14:56:11 +08002632 }
2633
2634 /**
2635 * Remove tethering event callback previously registered with
2636 * {@link #registerTetheringEventCallback}.
2637 *
2638 * @param callback previously registered callback.
markchieneeea0be2020-01-21 13:11:06 +08002639 *
2640 * @deprecated Use {@link TetheringManager#unregisterTetheringEventCallback} instead.
markchiena005c992019-02-27 14:56:11 +08002641 * @hide
2642 */
2643 @SystemApi
markchieneeea0be2020-01-21 13:11:06 +08002644 @Deprecated
markchiena005c992019-02-27 14:56:11 +08002645 @RequiresPermission(android.Manifest.permission.TETHER_PRIVILEGED)
2646 public void unregisterTetheringEventCallback(
2647 @NonNull final OnTetheringEventCallback callback) {
markchieneeea0be2020-01-21 13:11:06 +08002648 Objects.requireNonNull(callback, "The callback must be non-null");
2649 synchronized (mTetheringEventCallbacks) {
2650 final TetheringEventCallback tetherCallback =
2651 mTetheringEventCallbacks.remove(callback);
Amos Bianchiddadca42020-03-04 11:07:38 -08002652 mTetheringManager.unregisterTetheringEventCallback(tetherCallback);
markchieneeea0be2020-01-21 13:11:06 +08002653 }
markchiena005c992019-02-27 14:56:11 +08002654 }
2655
2656
2657 /**
Robert Greenwalt26744a52013-02-15 10:56:35 -08002658 * Get the list of regular expressions that define any tetherable
2659 * USB network interfaces. If USB tethering is not supported by the
2660 * device, this list should be empty.
2661 *
2662 * @return an array of 0 or more regular expression Strings defining
2663 * what interfaces are considered tetherable usb interfaces.
2664 *
markchieneeea0be2020-01-21 13:11:06 +08002665 * @deprecated Use {@link TetheringEventCallback#onTetherableInterfaceRegexpsChanged} instead.
Robert Greenwalt8e87f122010-02-11 18:18:40 -08002666 * {@hide}
2667 */
Jeff Sharkey2ac62992017-04-24 11:18:03 -06002668 @RequiresPermission(android.Manifest.permission.ACCESS_NETWORK_STATE)
Mathew Inwood0b8f8612018-08-08 14:52:47 +01002669 @UnsupportedAppUsage
markchieneeea0be2020-01-21 13:11:06 +08002670 @Deprecated
Robert Greenwalt8e87f122010-02-11 18:18:40 -08002671 public String[] getTetherableUsbRegexs() {
Amos Bianchiddadca42020-03-04 11:07:38 -08002672 return mTetheringManager.getTetherableUsbRegexs();
Robert Greenwalt8e87f122010-02-11 18:18:40 -08002673 }
2674
2675 /**
Robert Greenwalt26744a52013-02-15 10:56:35 -08002676 * Get the list of regular expressions that define any tetherable
2677 * Wifi network interfaces. If Wifi tethering is not supported by the
2678 * device, this list should be empty.
2679 *
2680 * @return an array of 0 or more regular expression Strings defining
2681 * what interfaces are considered tetherable wifi interfaces.
2682 *
markchieneeea0be2020-01-21 13:11:06 +08002683 * @deprecated Use {@link TetheringEventCallback#onTetherableInterfaceRegexpsChanged} instead.
Robert Greenwalt8e87f122010-02-11 18:18:40 -08002684 * {@hide}
2685 */
Jeff Sharkey2ac62992017-04-24 11:18:03 -06002686 @RequiresPermission(android.Manifest.permission.ACCESS_NETWORK_STATE)
Mathew Inwood0b8f8612018-08-08 14:52:47 +01002687 @UnsupportedAppUsage
markchieneeea0be2020-01-21 13:11:06 +08002688 @Deprecated
Robert Greenwalt8e87f122010-02-11 18:18:40 -08002689 public String[] getTetherableWifiRegexs() {
Amos Bianchiddadca42020-03-04 11:07:38 -08002690 return mTetheringManager.getTetherableWifiRegexs();
Robert Greenwalt8e87f122010-02-11 18:18:40 -08002691 }
Robert Greenwalt4283ded2010-03-02 17:25:02 -08002692
Danica Chang96567052010-08-11 14:54:43 -07002693 /**
Robert Greenwalt26744a52013-02-15 10:56:35 -08002694 * Get the list of regular expressions that define any tetherable
2695 * Bluetooth network interfaces. If Bluetooth tethering is not supported by the
2696 * device, this list should be empty.
2697 *
2698 * @return an array of 0 or more regular expression Strings defining
2699 * what interfaces are considered tetherable bluetooth interfaces.
2700 *
markchieneeea0be2020-01-21 13:11:06 +08002701 * @deprecated Use {@link TetheringEventCallback#onTetherableInterfaceRegexpsChanged(
2702 *TetheringManager.TetheringInterfaceRegexps)} instead.
Danica Chang96567052010-08-11 14:54:43 -07002703 * {@hide}
2704 */
Jeff Sharkey2ac62992017-04-24 11:18:03 -06002705 @RequiresPermission(android.Manifest.permission.ACCESS_NETWORK_STATE)
Mathew Inwood0b8f8612018-08-08 14:52:47 +01002706 @UnsupportedAppUsage
markchieneeea0be2020-01-21 13:11:06 +08002707 @Deprecated
Danica Chang96567052010-08-11 14:54:43 -07002708 public String[] getTetherableBluetoothRegexs() {
Amos Bianchiddadca42020-03-04 11:07:38 -08002709 return mTetheringManager.getTetherableBluetoothRegexs();
Danica Chang96567052010-08-11 14:54:43 -07002710 }
2711
Mike Lockwooded4a1742011-07-19 13:04:47 -07002712 /**
Robert Greenwalt26744a52013-02-15 10:56:35 -08002713 * Attempt to both alter the mode of USB and Tethering of USB. A
2714 * utility method to deal with some of the complexity of USB - will
2715 * attempt to switch to Rndis and subsequently tether the resulting
2716 * interface on {@code true} or turn off tethering and switch off
2717 * Rndis on {@code false}.
Lorenzo Colitti91de63b2015-10-15 16:29:00 +09002718 *
2719 * <p>This method requires the caller to hold either the
2720 * {@link android.Manifest.permission#CHANGE_NETWORK_STATE} permission
2721 * or the ability to modify system settings as determined by
2722 * {@link android.provider.Settings.System#canWrite}.</p>
Robert Greenwalt26744a52013-02-15 10:56:35 -08002723 *
2724 * @param enable a boolean - {@code true} to enable tethering
2725 * @return error a {@code TETHER_ERROR} value indicating success or failure type
markchiencc1bc1b2020-01-20 19:31:56 +08002726 * @deprecated Use {@link TetheringManager#startTethering} instead
Robert Greenwalt26744a52013-02-15 10:56:35 -08002727 *
Mike Lockwooded4a1742011-07-19 13:04:47 -07002728 * {@hide}
2729 */
Mathew Inwood0b8f8612018-08-08 14:52:47 +01002730 @UnsupportedAppUsage
markchiencc1bc1b2020-01-20 19:31:56 +08002731 @Deprecated
Mike Lockwooded4a1742011-07-19 13:04:47 -07002732 public int setUsbTethering(boolean enable) {
Amos Bianchiddadca42020-03-04 11:07:38 -08002733 return mTetheringManager.setUsbTethering(enable);
Mike Lockwooded4a1742011-07-19 13:04:47 -07002734 }
2735
markchieneeea0be2020-01-21 13:11:06 +08002736 /**
2737 * @deprecated Use {@link TetheringManager#TETHER_ERROR_NO_ERROR}.
2738 * {@hide}
2739 */
markchien5bd499c2019-01-16 17:44:13 +08002740 @SystemApi
markchieneeea0be2020-01-21 13:11:06 +08002741 @Deprecated
Remi NGUYEN VAN23b853b2021-02-15 18:52:06 +09002742 public static final int TETHER_ERROR_NO_ERROR = 0;
markchieneeea0be2020-01-21 13:11:06 +08002743 /**
2744 * @deprecated Use {@link TetheringManager#TETHER_ERROR_UNKNOWN_IFACE}.
2745 * {@hide}
2746 */
2747 @Deprecated
2748 public static final int TETHER_ERROR_UNKNOWN_IFACE =
2749 TetheringManager.TETHER_ERROR_UNKNOWN_IFACE;
2750 /**
2751 * @deprecated Use {@link TetheringManager#TETHER_ERROR_SERVICE_UNAVAIL}.
2752 * {@hide}
2753 */
2754 @Deprecated
2755 public static final int TETHER_ERROR_SERVICE_UNAVAIL =
2756 TetheringManager.TETHER_ERROR_SERVICE_UNAVAIL;
2757 /**
2758 * @deprecated Use {@link TetheringManager#TETHER_ERROR_UNSUPPORTED}.
2759 * {@hide}
2760 */
2761 @Deprecated
2762 public static final int TETHER_ERROR_UNSUPPORTED = TetheringManager.TETHER_ERROR_UNSUPPORTED;
2763 /**
2764 * @deprecated Use {@link TetheringManager#TETHER_ERROR_UNAVAIL_IFACE}.
2765 * {@hide}
2766 */
2767 @Deprecated
2768 public static final int TETHER_ERROR_UNAVAIL_IFACE =
2769 TetheringManager.TETHER_ERROR_UNAVAIL_IFACE;
2770 /**
markchien19336c22020-03-19 13:37:43 +08002771 * @deprecated Use {@link TetheringManager#TETHER_ERROR_INTERNAL_ERROR}.
markchieneeea0be2020-01-21 13:11:06 +08002772 * {@hide}
2773 */
2774 @Deprecated
markchien19336c22020-03-19 13:37:43 +08002775 public static final int TETHER_ERROR_MASTER_ERROR =
2776 TetheringManager.TETHER_ERROR_INTERNAL_ERROR;
markchieneeea0be2020-01-21 13:11:06 +08002777 /**
2778 * @deprecated Use {@link TetheringManager#TETHER_ERROR_TETHER_IFACE_ERROR}.
2779 * {@hide}
2780 */
2781 @Deprecated
2782 public static final int TETHER_ERROR_TETHER_IFACE_ERROR =
2783 TetheringManager.TETHER_ERROR_TETHER_IFACE_ERROR;
2784 /**
2785 * @deprecated Use {@link TetheringManager#TETHER_ERROR_UNTETHER_IFACE_ERROR}.
2786 * {@hide}
2787 */
2788 @Deprecated
2789 public static final int TETHER_ERROR_UNTETHER_IFACE_ERROR =
2790 TetheringManager.TETHER_ERROR_UNTETHER_IFACE_ERROR;
2791 /**
markchien19336c22020-03-19 13:37:43 +08002792 * @deprecated Use {@link TetheringManager#TETHER_ERROR_ENABLE_FORWARDING_ERROR}.
markchieneeea0be2020-01-21 13:11:06 +08002793 * {@hide}
2794 */
2795 @Deprecated
2796 public static final int TETHER_ERROR_ENABLE_NAT_ERROR =
markchien19336c22020-03-19 13:37:43 +08002797 TetheringManager.TETHER_ERROR_ENABLE_FORWARDING_ERROR;
markchieneeea0be2020-01-21 13:11:06 +08002798 /**
markchien19336c22020-03-19 13:37:43 +08002799 * @deprecated Use {@link TetheringManager#TETHER_ERROR_DISABLE_FORWARDING_ERROR}.
markchieneeea0be2020-01-21 13:11:06 +08002800 * {@hide}
2801 */
2802 @Deprecated
2803 public static final int TETHER_ERROR_DISABLE_NAT_ERROR =
markchien19336c22020-03-19 13:37:43 +08002804 TetheringManager.TETHER_ERROR_DISABLE_FORWARDING_ERROR;
markchieneeea0be2020-01-21 13:11:06 +08002805 /**
2806 * @deprecated Use {@link TetheringManager#TETHER_ERROR_IFACE_CFG_ERROR}.
2807 * {@hide}
2808 */
2809 @Deprecated
2810 public static final int TETHER_ERROR_IFACE_CFG_ERROR =
2811 TetheringManager.TETHER_ERROR_IFACE_CFG_ERROR;
2812 /**
markchien19336c22020-03-19 13:37:43 +08002813 * @deprecated Use {@link TetheringManager#TETHER_ERROR_PROVISIONING_FAILED}.
markchieneeea0be2020-01-21 13:11:06 +08002814 * {@hide}
2815 */
markchien5bd499c2019-01-16 17:44:13 +08002816 @SystemApi
markchieneeea0be2020-01-21 13:11:06 +08002817 @Deprecated
Remi NGUYEN VAN23b853b2021-02-15 18:52:06 +09002818 public static final int TETHER_ERROR_PROVISION_FAILED = 11;
markchieneeea0be2020-01-21 13:11:06 +08002819 /**
2820 * @deprecated Use {@link TetheringManager#TETHER_ERROR_DHCPSERVER_ERROR}.
2821 * {@hide}
2822 */
2823 @Deprecated
2824 public static final int TETHER_ERROR_DHCPSERVER_ERROR =
2825 TetheringManager.TETHER_ERROR_DHCPSERVER_ERROR;
2826 /**
2827 * @deprecated Use {@link TetheringManager#TETHER_ERROR_ENTITLEMENT_UNKNOWN}.
2828 * {@hide}
2829 */
markchien5bd499c2019-01-16 17:44:13 +08002830 @SystemApi
markchieneeea0be2020-01-21 13:11:06 +08002831 @Deprecated
Remi NGUYEN VAN23b853b2021-02-15 18:52:06 +09002832 public static final int TETHER_ERROR_ENTITLEMENT_UNKONWN = 13;
Robert Greenwalt4283ded2010-03-02 17:25:02 -08002833
2834 /**
Robert Greenwalt26744a52013-02-15 10:56:35 -08002835 * Get a more detailed error code after a Tethering or Untethering
2836 * request asynchronously failed.
2837 *
2838 * @param iface The name of the interface of interest
Robert Greenwalt4283ded2010-03-02 17:25:02 -08002839 * @return error The error code of the last error tethering or untethering the named
2840 * interface
Robert Greenwalt26744a52013-02-15 10:56:35 -08002841 *
markchieneeea0be2020-01-21 13:11:06 +08002842 * @deprecated Use {@link TetheringEventCallback#onError(String, int)} instead.
Robert Greenwalt4283ded2010-03-02 17:25:02 -08002843 * {@hide}
2844 */
Jeff Sharkey2ac62992017-04-24 11:18:03 -06002845 @RequiresPermission(android.Manifest.permission.ACCESS_NETWORK_STATE)
Mathew Inwood5a09a712020-11-04 09:29:36 +00002846 @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.R, trackingBug = 170729553)
markchieneeea0be2020-01-21 13:11:06 +08002847 @Deprecated
Robert Greenwalt4283ded2010-03-02 17:25:02 -08002848 public int getLastTetherError(String iface) {
markchien19336c22020-03-19 13:37:43 +08002849 int error = mTetheringManager.getLastTetherError(iface);
2850 if (error == TetheringManager.TETHER_ERROR_UNKNOWN_TYPE) {
2851 // TETHER_ERROR_UNKNOWN_TYPE was introduced with TetheringManager and has never been
2852 // returned by ConnectivityManager. Convert it to the legacy TETHER_ERROR_UNKNOWN_IFACE
2853 // instead.
2854 error = TetheringManager.TETHER_ERROR_UNKNOWN_IFACE;
2855 }
2856 return error;
Robert Greenwalt93dc1042010-06-15 12:19:37 -07002857 }
2858
markchiendd41c822019-03-06 16:25:00 +08002859 /** @hide */
2860 @Retention(RetentionPolicy.SOURCE)
2861 @IntDef(value = {
2862 TETHER_ERROR_NO_ERROR,
2863 TETHER_ERROR_PROVISION_FAILED,
2864 TETHER_ERROR_ENTITLEMENT_UNKONWN,
2865 })
2866 public @interface EntitlementResultCode {
2867 }
2868
Robert Greenwalt93dc1042010-06-15 12:19:37 -07002869 /**
markchiendd41c822019-03-06 16:25:00 +08002870 * Callback for use with {@link #getLatestTetheringEntitlementResult} to find out whether
markchien5bd499c2019-01-16 17:44:13 +08002871 * entitlement succeeded.
markchieneeea0be2020-01-21 13:11:06 +08002872 *
2873 * @deprecated Use {@link TetheringManager#OnTetheringEntitlementResultListener} instead.
markchien5bd499c2019-01-16 17:44:13 +08002874 * @hide
2875 */
2876 @SystemApi
markchieneeea0be2020-01-21 13:11:06 +08002877 @Deprecated
markchiendd41c822019-03-06 16:25:00 +08002878 public interface OnTetheringEntitlementResultListener {
2879 /**
2880 * Called to notify entitlement result.
2881 *
2882 * @param resultCode an int value of entitlement result. It may be one of
2883 * {@link #TETHER_ERROR_NO_ERROR},
2884 * {@link #TETHER_ERROR_PROVISION_FAILED}, or
2885 * {@link #TETHER_ERROR_ENTITLEMENT_UNKONWN}.
2886 */
Jeremy Kleina9aad902019-03-12 13:32:08 -07002887 void onTetheringEntitlementResult(@EntitlementResultCode int resultCode);
markchiendd41c822019-03-06 16:25:00 +08002888 }
2889
2890 /**
markchien5bd499c2019-01-16 17:44:13 +08002891 * Get the last value of the entitlement check on this downstream. If the cached value is
2892 * {@link #TETHER_ERROR_NO_ERROR} or showEntitlementUi argument is false, it just return the
2893 * cached value. Otherwise, a UI-based entitlement check would be performed. It is not
2894 * guaranteed that the UI-based entitlement check will complete in any specific time period
2895 * and may in fact never complete. Any successful entitlement check the platform performs for
2896 * any reason will update the cached value.
2897 *
2898 * @param type the downstream type of tethering. Must be one of
2899 * {@link #TETHERING_WIFI},
2900 * {@link #TETHERING_USB}, or
2901 * {@link #TETHERING_BLUETOOTH}.
2902 * @param showEntitlementUi a boolean indicating whether to run UI-based entitlement check.
markchiendd41c822019-03-06 16:25:00 +08002903 * @param executor the executor on which callback will be invoked.
2904 * @param listener an {@link OnTetheringEntitlementResultListener} which will be called to
2905 * notify the caller of the result of entitlement check. The listener may be called zero
2906 * or one time.
markchieneeea0be2020-01-21 13:11:06 +08002907 * @deprecated Use {@link TetheringManager#requestLatestTetheringEntitlementResult} instead.
markchien5bd499c2019-01-16 17:44:13 +08002908 * {@hide}
2909 */
2910 @SystemApi
markchieneeea0be2020-01-21 13:11:06 +08002911 @Deprecated
markchien5bd499c2019-01-16 17:44:13 +08002912 @RequiresPermission(android.Manifest.permission.TETHER_PRIVILEGED)
markchiendd41c822019-03-06 16:25:00 +08002913 public void getLatestTetheringEntitlementResult(int type, boolean showEntitlementUi,
2914 @NonNull @CallbackExecutor Executor executor,
2915 @NonNull final OnTetheringEntitlementResultListener listener) {
Remi NGUYEN VAN1c17c882021-03-11 20:49:13 +09002916 Objects.requireNonNull(listener, "TetheringEntitlementResultListener cannot be null.");
markchiendd41c822019-03-06 16:25:00 +08002917 ResultReceiver wrappedListener = new ResultReceiver(null) {
2918 @Override
2919 protected void onReceiveResult(int resultCode, Bundle resultData) {
lucaslin9cc04192021-03-04 09:38:21 +08002920 final long token = Binder.clearCallingIdentity();
2921 try {
2922 executor.execute(() -> {
2923 listener.onTetheringEntitlementResult(resultCode);
2924 });
2925 } finally {
2926 Binder.restoreCallingIdentity(token);
2927 }
markchiendd41c822019-03-06 16:25:00 +08002928 }
2929 };
2930
Amos Bianchiddadca42020-03-04 11:07:38 -08002931 mTetheringManager.requestLatestTetheringEntitlementResult(type, wrappedListener,
markchien4f949812019-12-16 20:15:20 +08002932 showEntitlementUi);
markchiendd41c822019-03-06 16:25:00 +08002933 }
2934
2935 /**
Robert Greenwalt26744a52013-02-15 10:56:35 -08002936 * Report network connectivity status. This is currently used only
2937 * to alter status bar UI.
Paul Jensen19b3ee72015-05-06 11:10:18 -04002938 * <p>This method requires the caller to hold the permission
2939 * {@link android.Manifest.permission#STATUS_BAR}.
Robert Greenwalt26744a52013-02-15 10:56:35 -08002940 *
Robert Greenwalt986c7412010-09-08 15:24:47 -07002941 * @param networkType The type of network you want to report on
2942 * @param percentage The quality of the connection 0 is bad, 100 is good
Chalard Jean5acb7b72018-03-08 13:54:53 +09002943 * @deprecated Types are deprecated. Use {@link #reportNetworkConnectivity} instead.
Robert Greenwalt986c7412010-09-08 15:24:47 -07002944 * {@hide}
2945 */
2946 public void reportInetCondition(int networkType, int percentage) {
Soi, Yoshinari99efcbd2015-11-12 12:09:02 +09002947 printStackTrace();
Robert Greenwalt986c7412010-09-08 15:24:47 -07002948 try {
2949 mService.reportInetCondition(networkType, percentage);
2950 } catch (RemoteException e) {
Jeff Sharkeya6066fd2016-03-01 19:27:23 -07002951 throw e.rethrowFromSystemServer();
Robert Greenwalt986c7412010-09-08 15:24:47 -07002952 }
2953 }
Robert Greenwaltc3c5f862010-10-11 16:00:27 -07002954
2955 /**
Robert Greenwaltd0ebdbb2014-05-18 23:07:25 -07002956 * Report a problem network to the framework. This provides a hint to the system
Ye Wen849c72a2014-07-21 14:19:01 -07002957 * that there might be connectivity problems on this network and may cause
Robert Greenwaltd0ebdbb2014-05-18 23:07:25 -07002958 * the framework to re-evaluate network connectivity and/or switch to another
2959 * network.
Robert Greenwalt2bebbfb2014-03-26 16:47:06 -07002960 *
Robert Greenwaltd0ebdbb2014-05-18 23:07:25 -07002961 * @param network The {@link Network} the application was attempting to use
2962 * or {@code null} to indicate the current default network.
Paul Jensen7aac1b32015-04-07 12:43:13 -04002963 * @deprecated Use {@link #reportNetworkConnectivity} which allows reporting both
2964 * working and non-working connectivity.
Robert Greenwalt2bebbfb2014-03-26 16:47:06 -07002965 */
Aurimas Liutikasa6bb0232016-05-24 15:22:55 -07002966 @Deprecated
Chalard Jean42a9c292019-01-07 19:26:34 +09002967 public void reportBadNetwork(@Nullable Network network) {
Soi, Yoshinari99efcbd2015-11-12 12:09:02 +09002968 printStackTrace();
Robert Greenwalt2bebbfb2014-03-26 16:47:06 -07002969 try {
Paul Jensen7aac1b32015-04-07 12:43:13 -04002970 // One of these will be ignored because it matches system's current state.
2971 // The other will trigger the necessary reevaluation.
2972 mService.reportNetworkConnectivity(network, true);
2973 mService.reportNetworkConnectivity(network, false);
2974 } catch (RemoteException e) {
Jeff Sharkeya6066fd2016-03-01 19:27:23 -07002975 throw e.rethrowFromSystemServer();
Paul Jensen7aac1b32015-04-07 12:43:13 -04002976 }
2977 }
2978
2979 /**
2980 * Report to the framework whether a network has working connectivity.
2981 * This provides a hint to the system that a particular network is providing
2982 * working connectivity or not. In response the framework may re-evaluate
2983 * the network's connectivity and might take further action thereafter.
2984 *
2985 * @param network The {@link Network} the application was attempting to use
2986 * or {@code null} to indicate the current default network.
2987 * @param hasConnectivity {@code true} if the application was able to successfully access the
2988 * Internet using {@code network} or {@code false} if not.
2989 */
Chalard Jean42a9c292019-01-07 19:26:34 +09002990 public void reportNetworkConnectivity(@Nullable Network network, boolean hasConnectivity) {
Soi, Yoshinari99efcbd2015-11-12 12:09:02 +09002991 printStackTrace();
Paul Jensen7aac1b32015-04-07 12:43:13 -04002992 try {
2993 mService.reportNetworkConnectivity(network, hasConnectivity);
Robert Greenwalt2bebbfb2014-03-26 16:47:06 -07002994 } catch (RemoteException e) {
Jeff Sharkeya6066fd2016-03-01 19:27:23 -07002995 throw e.rethrowFromSystemServer();
Robert Greenwalt2bebbfb2014-03-26 16:47:06 -07002996 }
2997 }
2998
2999 /**
Robert Greenwalt26744a52013-02-15 10:56:35 -08003000 * Set a network-independent global http proxy. This is not normally what you want
3001 * for typical HTTP proxies - they are general network dependent. However if you're
3002 * doing something unusual like general internal filtering this may be useful. On
3003 * a private network where the proxy is not accessible, you may break HTTP using this.
Paul Jensen19b3ee72015-05-06 11:10:18 -04003004 *
3005 * @param p A {@link ProxyInfo} object defining the new global
3006 * HTTP proxy. A {@code null} value will clear the global HTTP proxy.
Robert Greenwaltd0ebdbb2014-05-18 23:07:25 -07003007 * @hide
Robert Greenwaltc3c5f862010-10-11 16:00:27 -07003008 */
paulhud70b7dd2019-08-12 16:25:11 +08003009 @RequiresPermission(android.Manifest.permission.NETWORK_STACK)
Jason Monk1e3df5d2014-04-25 15:00:09 -04003010 public void setGlobalProxy(ProxyInfo p) {
Robert Greenwaltc3c5f862010-10-11 16:00:27 -07003011 try {
3012 mService.setGlobalProxy(p);
3013 } catch (RemoteException e) {
Jeff Sharkeya6066fd2016-03-01 19:27:23 -07003014 throw e.rethrowFromSystemServer();
Robert Greenwaltc3c5f862010-10-11 16:00:27 -07003015 }
3016 }
3017
3018 /**
Robert Greenwalt26744a52013-02-15 10:56:35 -08003019 * Retrieve any network-independent global HTTP proxy.
3020 *
Jason Monk1e3df5d2014-04-25 15:00:09 -04003021 * @return {@link ProxyInfo} for the current global HTTP proxy or {@code null}
Robert Greenwalt26744a52013-02-15 10:56:35 -08003022 * if no global HTTP proxy is set.
Robert Greenwaltd0ebdbb2014-05-18 23:07:25 -07003023 * @hide
Robert Greenwaltc3c5f862010-10-11 16:00:27 -07003024 */
Jason Monk1e3df5d2014-04-25 15:00:09 -04003025 public ProxyInfo getGlobalProxy() {
Robert Greenwaltc3c5f862010-10-11 16:00:27 -07003026 try {
3027 return mService.getGlobalProxy();
3028 } catch (RemoteException e) {
Jeff Sharkeya6066fd2016-03-01 19:27:23 -07003029 throw e.rethrowFromSystemServer();
Robert Greenwaltc3c5f862010-10-11 16:00:27 -07003030 }
3031 }
3032
3033 /**
Paul Jensenfdec88b2015-05-06 07:32:40 -04003034 * Retrieve the global HTTP proxy, or if no global HTTP proxy is set, a
3035 * network-specific HTTP proxy. If {@code network} is null, the
3036 * network-specific proxy returned is the proxy of the default active
3037 * network.
3038 *
3039 * @return {@link ProxyInfo} for the current global HTTP proxy, or if no
3040 * global HTTP proxy is set, {@code ProxyInfo} for {@code network},
3041 * or when {@code network} is {@code null},
3042 * the {@code ProxyInfo} for the default active network. Returns
3043 * {@code null} when no proxy applies or the caller doesn't have
3044 * permission to use {@code network}.
3045 * @hide
3046 */
3047 public ProxyInfo getProxyForNetwork(Network network) {
3048 try {
3049 return mService.getProxyForNetwork(network);
3050 } catch (RemoteException e) {
Jeff Sharkeya6066fd2016-03-01 19:27:23 -07003051 throw e.rethrowFromSystemServer();
Paul Jensenfdec88b2015-05-06 07:32:40 -04003052 }
3053 }
3054
3055 /**
Paul Jensen12131352014-12-10 15:12:18 -05003056 * Get the current default HTTP proxy settings. If a global proxy is set it will be returned,
3057 * otherwise if this process is bound to a {@link Network} using
Paul Jensen895c3942015-03-10 10:54:12 -04003058 * {@link #bindProcessToNetwork} then that {@code Network}'s proxy is returned, otherwise
Paul Jensen12131352014-12-10 15:12:18 -05003059 * the default network's proxy is returned.
Robert Greenwalt26744a52013-02-15 10:56:35 -08003060 *
Jason Monk1e3df5d2014-04-25 15:00:09 -04003061 * @return the {@link ProxyInfo} for the current HTTP proxy, or {@code null} if no
Robert Greenwalt26744a52013-02-15 10:56:35 -08003062 * HTTP proxy is active.
Robert Greenwaltc3c5f862010-10-11 16:00:27 -07003063 */
Chalard Jean42a9c292019-01-07 19:26:34 +09003064 @Nullable
Paul Jensen12131352014-12-10 15:12:18 -05003065 public ProxyInfo getDefaultProxy() {
Paul Jensenfdec88b2015-05-06 07:32:40 -04003066 return getProxyForNetwork(getBoundNetworkForProcess());
Robert Greenwaltc3c5f862010-10-11 16:00:27 -07003067 }
Robert Greenwalt34848c02011-03-25 13:09:25 -07003068
3069 /**
Robert Greenwalt0114f6e2011-08-31 11:46:42 -07003070 * Returns true if the hardware supports the given network type
3071 * else it returns false. This doesn't indicate we have coverage
3072 * or are authorized onto a network, just whether or not the
Robert Greenwalt26744a52013-02-15 10:56:35 -08003073 * hardware supports it. For example a GSM phone without a SIM
3074 * should still return {@code true} for mobile data, but a wifi only
3075 * tablet would return {@code false}.
3076 *
3077 * @param networkType The network type we'd like to check
3078 * @return {@code true} if supported, else {@code false}
Chalard Jean5acb7b72018-03-08 13:54:53 +09003079 * @deprecated Types are deprecated. Use {@link NetworkCapabilities} instead.
Robert Greenwalt0114f6e2011-08-31 11:46:42 -07003080 * @hide
3081 */
Chalard Jean5acb7b72018-03-08 13:54:53 +09003082 @Deprecated
Jeff Sharkey2ac62992017-04-24 11:18:03 -06003083 @RequiresPermission(android.Manifest.permission.ACCESS_NETWORK_STATE)
Chalard Jeanaa91c9d2019-04-09 15:46:21 +09003084 @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P, trackingBug = 130143562)
Robert Greenwalt0114f6e2011-08-31 11:46:42 -07003085 public boolean isNetworkSupported(int networkType) {
3086 try {
3087 return mService.isNetworkSupported(networkType);
Jeff Sharkeya6066fd2016-03-01 19:27:23 -07003088 } catch (RemoteException e) {
3089 throw e.rethrowFromSystemServer();
3090 }
Robert Greenwalt0114f6e2011-08-31 11:46:42 -07003091 }
Jeff Sharkeyd00b1302012-04-12 18:34:54 -07003092
3093 /**
3094 * Returns if the currently active data network is metered. A network is
3095 * classified as metered when the user is sensitive to heavy data usage on
Robert Greenwalt26744a52013-02-15 10:56:35 -08003096 * that connection due to monetary costs, data limitations or
3097 * battery/performance issues. You should check this before doing large
3098 * data transfers, and warn the user or delay the operation until another
3099 * network is available.
3100 *
3101 * @return {@code true} if large transfers should be avoided, otherwise
3102 * {@code false}.
Jeff Sharkeyd00b1302012-04-12 18:34:54 -07003103 */
Jeff Sharkey2ac62992017-04-24 11:18:03 -06003104 @RequiresPermission(android.Manifest.permission.ACCESS_NETWORK_STATE)
Jeff Sharkeyd00b1302012-04-12 18:34:54 -07003105 public boolean isActiveNetworkMetered() {
3106 try {
3107 return mService.isActiveNetworkMetered();
3108 } catch (RemoteException e) {
Jeff Sharkeya6066fd2016-03-01 19:27:23 -07003109 throw e.rethrowFromSystemServer();
Jeff Sharkeyd00b1302012-04-12 18:34:54 -07003110 }
3111 }
Jeff Sharkeyf2e722a2012-08-25 00:05:46 -07003112
Robert Greenwalt26744a52013-02-15 10:56:35 -08003113 /**
Sarah Chincf44f2f2020-11-25 12:15:14 -08003114 * Set sign in error notification to visible or invisible
Wink Saville89c87b92013-08-29 08:55:16 -07003115 *
Sarah Chincf44f2f2020-11-25 12:15:14 -08003116 * @hide
Paul Jensena8e6dc62015-03-18 12:23:02 -04003117 * @deprecated Doesn't properly deal with multiple connected networks of the same type.
Wink Saville89c87b92013-08-29 08:55:16 -07003118 */
Aurimas Liutikasa6bb0232016-05-24 15:22:55 -07003119 @Deprecated
Wink Saville89c87b92013-08-29 08:55:16 -07003120 public void setProvisioningNotificationVisible(boolean visible, int networkType,
Paul Jensen82c7e422014-09-15 15:59:36 -04003121 String action) {
Wink Saville89c87b92013-08-29 08:55:16 -07003122 try {
Paul Jensen82c7e422014-09-15 15:59:36 -04003123 mService.setProvisioningNotificationVisible(visible, networkType, action);
Wink Saville89c87b92013-08-29 08:55:16 -07003124 } catch (RemoteException e) {
Jeff Sharkeya6066fd2016-03-01 19:27:23 -07003125 throw e.rethrowFromSystemServer();
Wink Saville89c87b92013-08-29 08:55:16 -07003126 }
3127 }
Yuhao Zheng15019892013-09-09 17:00:04 -07003128
3129 /**
3130 * Set the value for enabling/disabling airplane mode
3131 *
3132 * @param enable whether to enable airplane mode or not
3133 *
Yuhao Zheng15019892013-09-09 17:00:04 -07003134 * @hide
3135 */
Lorenzo Colittic4d01862018-10-09 18:55:11 +09003136 @RequiresPermission(anyOf = {
Edward Savage-Jones3b9fe322019-11-26 13:18:08 +01003137 android.Manifest.permission.NETWORK_AIRPLANE_MODE,
Lorenzo Colittic4d01862018-10-09 18:55:11 +09003138 android.Manifest.permission.NETWORK_SETTINGS,
3139 android.Manifest.permission.NETWORK_SETUP_WIZARD,
3140 android.Manifest.permission.NETWORK_STACK})
Lorenzo Colitti272a77d2018-10-09 18:50:32 +09003141 @SystemApi
Yuhao Zheng15019892013-09-09 17:00:04 -07003142 public void setAirplaneMode(boolean enable) {
3143 try {
3144 mService.setAirplaneMode(enable);
3145 } catch (RemoteException e) {
Jeff Sharkeya6066fd2016-03-01 19:27:23 -07003146 throw e.rethrowFromSystemServer();
Yuhao Zheng15019892013-09-09 17:00:04 -07003147 }
3148 }
Robert Greenwalt948aea52014-04-11 15:53:27 -07003149
Lorenzo Colitti92e6c272020-01-08 00:04:09 +09003150 /**
3151 * Registers the specified {@link NetworkProvider}.
3152 * Each listener must only be registered once. The listener can be unregistered with
3153 * {@link #unregisterNetworkProvider}.
3154 *
3155 * @param provider the provider to register
3156 * @return the ID of the provider. This ID must be used by the provider when registering
3157 * {@link android.net.NetworkAgent}s.
3158 * @hide
3159 */
3160 @SystemApi
paulhu44939832020-03-04 09:43:41 +08003161 @RequiresPermission(anyOf = {
3162 NetworkStack.PERMISSION_MAINLINE_NETWORK_STACK,
3163 android.Manifest.permission.NETWORK_FACTORY})
Lorenzo Colitti92e6c272020-01-08 00:04:09 +09003164 public int registerNetworkProvider(@NonNull NetworkProvider provider) {
3165 if (provider.getProviderId() != NetworkProvider.ID_NONE) {
Lorenzo Colitti92e6c272020-01-08 00:04:09 +09003166 throw new IllegalStateException("NetworkProviders can only be registered once");
3167 }
3168
3169 try {
3170 int providerId = mService.registerNetworkProvider(provider.getMessenger(),
3171 provider.getName());
3172 provider.setProviderId(providerId);
3173 } catch (RemoteException e) {
3174 throw e.rethrowFromSystemServer();
3175 }
3176 return provider.getProviderId();
3177 }
3178
3179 /**
3180 * Unregisters the specified NetworkProvider.
3181 *
3182 * @param provider the provider to unregister
3183 * @hide
3184 */
3185 @SystemApi
paulhu44939832020-03-04 09:43:41 +08003186 @RequiresPermission(anyOf = {
3187 NetworkStack.PERMISSION_MAINLINE_NETWORK_STACK,
3188 android.Manifest.permission.NETWORK_FACTORY})
Lorenzo Colitti92e6c272020-01-08 00:04:09 +09003189 public void unregisterNetworkProvider(@NonNull NetworkProvider provider) {
3190 try {
3191 mService.unregisterNetworkProvider(provider.getMessenger());
3192 } catch (RemoteException e) {
3193 throw e.rethrowFromSystemServer();
3194 }
3195 provider.setProviderId(NetworkProvider.ID_NONE);
3196 }
3197
3198
3199 /** @hide exposed via the NetworkProvider class. */
paulhu44939832020-03-04 09:43:41 +08003200 @RequiresPermission(anyOf = {
3201 NetworkStack.PERMISSION_MAINLINE_NETWORK_STACK,
3202 android.Manifest.permission.NETWORK_FACTORY})
Lorenzo Colitti92e6c272020-01-08 00:04:09 +09003203 public void declareNetworkRequestUnfulfillable(@NonNull NetworkRequest request) {
3204 try {
3205 mService.declareNetworkRequestUnfulfillable(request);
3206 } catch (RemoteException e) {
3207 throw e.rethrowFromSystemServer();
3208 }
3209 }
3210
Chalard Jean44689aa2018-05-02 21:14:54 +09003211 // TODO : remove this method. It is a stopgap measure to help sheperding a number
3212 // of dependent changes that would conflict throughout the automerger graph. Having this
3213 // temporarily helps with the process of going through with all these dependent changes across
3214 // the entire tree.
Paul Jensenc2569432015-02-13 14:18:39 -05003215 /**
3216 * @hide
3217 * Register a NetworkAgent with ConnectivityService.
Chalard Jeana973c7a2019-12-13 19:47:12 +09003218 * @return Network corresponding to NetworkAgent.
Paul Jensenc2569432015-02-13 14:18:39 -05003219 */
paulhu44939832020-03-04 09:43:41 +08003220 @RequiresPermission(anyOf = {
3221 NetworkStack.PERMISSION_MAINLINE_NETWORK_STACK,
3222 android.Manifest.permission.NETWORK_FACTORY})
Remi NGUYEN VAN96be83b2020-12-23 12:45:08 +09003223 public Network registerNetworkAgent(INetworkAgent na, NetworkInfo ni, LinkProperties lp,
Lorenzo Colitticf595362020-01-12 22:28:37 +09003224 NetworkCapabilities nc, int score, NetworkAgentConfig config) {
Remi NGUYEN VAN96be83b2020-12-23 12:45:08 +09003225 return registerNetworkAgent(na, ni, lp, nc, score, config, NetworkProvider.ID_NONE);
Chalard Jean44689aa2018-05-02 21:14:54 +09003226 }
3227
3228 /**
3229 * @hide
3230 * Register a NetworkAgent with ConnectivityService.
Chalard Jeana973c7a2019-12-13 19:47:12 +09003231 * @return Network corresponding to NetworkAgent.
Chalard Jean44689aa2018-05-02 21:14:54 +09003232 */
paulhu44939832020-03-04 09:43:41 +08003233 @RequiresPermission(anyOf = {
3234 NetworkStack.PERMISSION_MAINLINE_NETWORK_STACK,
3235 android.Manifest.permission.NETWORK_FACTORY})
Remi NGUYEN VAN96be83b2020-12-23 12:45:08 +09003236 public Network registerNetworkAgent(INetworkAgent na, NetworkInfo ni, LinkProperties lp,
Lorenzo Colitticf595362020-01-12 22:28:37 +09003237 NetworkCapabilities nc, int score, NetworkAgentConfig config, int providerId) {
Robert Greenwaltdb6fac52014-04-18 15:25:25 -07003238 try {
Remi NGUYEN VAN96be83b2020-12-23 12:45:08 +09003239 return mService.registerNetworkAgent(na, ni, lp, nc, score, config, providerId);
Paul Jensenc2569432015-02-13 14:18:39 -05003240 } catch (RemoteException e) {
Jeff Sharkeya6066fd2016-03-01 19:27:23 -07003241 throw e.rethrowFromSystemServer();
Paul Jensenc2569432015-02-13 14:18:39 -05003242 }
Robert Greenwaltdb6fac52014-04-18 15:25:25 -07003243 }
3244
Robert Greenwalt2bebbfb2014-03-26 16:47:06 -07003245 /**
Hugo Benichi45a49542017-03-06 09:17:06 +09003246 * Base class for {@code NetworkRequest} callbacks. Used for notifications about network
3247 * changes. Should be extended by applications wanting notifications.
3248 *
3249 * A {@code NetworkCallback} is registered by calling
3250 * {@link #requestNetwork(NetworkRequest, NetworkCallback)},
3251 * {@link #registerNetworkCallback(NetworkRequest, NetworkCallback)},
Hugo Benichi7665a142018-02-07 21:17:43 +09003252 * or {@link #registerDefaultNetworkCallback(NetworkCallback)}. A {@code NetworkCallback} is
Hugo Benichi45a49542017-03-06 09:17:06 +09003253 * unregistered by calling {@link #unregisterNetworkCallback(NetworkCallback)}.
3254 * A {@code NetworkCallback} should be registered at most once at any time.
3255 * A {@code NetworkCallback} that has been unregistered can be registered again.
Robert Greenwalt2bebbfb2014-03-26 16:47:06 -07003256 */
Robert Greenwalt09e5cff2014-06-11 16:05:07 -07003257 public static class NetworkCallback {
Robert Greenwaltdb6fac52014-04-18 15:25:25 -07003258 /**
Roshan Pius7992afd2020-12-22 15:10:42 -08003259 * No flags associated with this callback.
3260 * @hide
3261 */
3262 public static final int FLAG_NONE = 0;
3263 /**
3264 * Use this flag to include any location sensitive data in {@link NetworkCapabilities} sent
3265 * via {@link #onCapabilitiesChanged(Network, NetworkCapabilities)}.
3266 * <p>
3267 * These include:
3268 * <li> Some transport info instances (retrieved via
3269 * {@link NetworkCapabilities#getTransportInfo()}) like {@link android.net.wifi.WifiInfo}
3270 * contain location sensitive information.
3271 * <li> OwnerUid (retrieved via {@link NetworkCapabilities#getOwnerUid()} is location
3272 * sensitive for wifi suggestor apps (i.e using {@link WifiNetworkSuggestion}).</li>
3273 * </p>
3274 * <p>
3275 * Note:
3276 * <li> Retrieving this location sensitive information (subject to app's location
3277 * permissions) will be noted by system. </li>
3278 * <li> Without this flag any {@link NetworkCapabilities} provided via the callback does
3279 * not include location sensitive info.
3280 * </p>
3281 */
3282 public static final int FLAG_INCLUDE_LOCATION_INFO = 1 << 0;
3283
3284 /** @hide */
3285 @Retention(RetentionPolicy.SOURCE)
3286 @IntDef(flag = true, prefix = "FLAG_", value = {
3287 FLAG_NONE,
3288 FLAG_INCLUDE_LOCATION_INFO
3289 })
3290 public @interface Flag { }
3291
3292 /**
3293 * All the valid flags for error checking.
3294 */
3295 private static final int VALID_FLAGS = FLAG_INCLUDE_LOCATION_INFO;
3296
3297 public NetworkCallback() {
3298 this(FLAG_NONE);
3299 }
3300
3301 public NetworkCallback(@Flag int flags) {
3302 Preconditions.checkArgument((flags & VALID_FLAGS) == flags);
3303 mFlags = flags;
3304 }
3305
3306 /**
Lorenzo Colitticcf82102015-04-24 12:23:24 +09003307 * Called when the framework connects to a new network to evaluate whether it satisfies this
3308 * request. If evaluation succeeds, this callback may be followed by an {@link #onAvailable}
3309 * callback. There is no guarantee that this new network will satisfy any requests, or that
3310 * the network will stay connected for longer than the time necessary to evaluate it.
3311 * <p>
3312 * Most applications <b>should not</b> act on this callback, and should instead use
3313 * {@link #onAvailable}. This callback is intended for use by applications that can assist
3314 * the framework in properly evaluating the network &mdash; for example, an application that
3315 * can automatically log in to a captive portal without user intervention.
3316 *
3317 * @param network The {@link Network} of the network that is being evaluated.
Lorenzo Colittia04936a2015-06-11 14:27:17 +09003318 *
3319 * @hide
Robert Greenwaltdb6fac52014-04-18 15:25:25 -07003320 */
paulhua9a6e2a2019-03-22 16:35:06 +08003321 public void onPreCheck(@NonNull Network network) {}
Robert Greenwaltdb6fac52014-04-18 15:25:25 -07003322
3323 /**
Lorenzo Colitticcf82102015-04-24 12:23:24 +09003324 * Called when the framework connects and has declared a new network ready for use.
Robert Greenwalt09e5cff2014-06-11 16:05:07 -07003325 * This callback may be called more than once if the {@link Network} that is
3326 * satisfying the request changes.
Robert Greenwaltd0ebdbb2014-05-18 23:07:25 -07003327 *
Robert Greenwaltd0ebdbb2014-05-18 23:07:25 -07003328 * @param network The {@link Network} of the satisfying network.
Chalard Jeanb6a0c6d2018-01-30 22:41:41 +09003329 * @param networkCapabilities The {@link NetworkCapabilities} of the satisfying network.
3330 * @param linkProperties The {@link LinkProperties} of the satisfying network.
junyulai5ab727b2018-08-07 19:50:45 +08003331 * @param blocked Whether access to the {@link Network} is blocked due to system policy.
Chalard Jeanb6a0c6d2018-01-30 22:41:41 +09003332 * @hide
3333 */
paulhua9a6e2a2019-03-22 16:35:06 +08003334 public void onAvailable(@NonNull Network network,
3335 @NonNull NetworkCapabilities networkCapabilities,
3336 @NonNull LinkProperties linkProperties, boolean blocked) {
Chalard Jeanb6a0c6d2018-01-30 22:41:41 +09003337 // Internally only this method is called when a new network is available, and
3338 // it calls the callback in the same way and order that older versions used
3339 // to call so as not to change the behavior.
3340 onAvailable(network);
3341 if (!networkCapabilities.hasCapability(
3342 NetworkCapabilities.NET_CAPABILITY_NOT_SUSPENDED)) {
3343 onNetworkSuspended(network);
3344 }
3345 onCapabilitiesChanged(network, networkCapabilities);
3346 onLinkPropertiesChanged(network, linkProperties);
junyulai5ab727b2018-08-07 19:50:45 +08003347 onBlockedStatusChanged(network, blocked);
Chalard Jeanb6a0c6d2018-01-30 22:41:41 +09003348 }
3349
3350 /**
3351 * Called when the framework connects and has declared a new network ready for use.
Chalard Jean7f3a8742019-08-30 16:27:28 +09003352 *
3353 * <p>For callbacks registered with {@link #registerNetworkCallback}, multiple networks may
3354 * be available at the same time, and onAvailable will be called for each of these as they
3355 * appear.
3356 *
3357 * <p>For callbacks registered with {@link #requestNetwork} and
3358 * {@link #registerDefaultNetworkCallback}, this means the network passed as an argument
3359 * is the new best network for this request and is now tracked by this callback ; this
3360 * callback will no longer receive method calls about other networks that may have been
3361 * passed to this method previously. The previously-best network may have disconnected, or
3362 * it may still be around and the newly-best network may simply be better.
3363 *
3364 * <p>Starting with {@link android.os.Build.VERSION_CODES#O}, this will always immediately
3365 * be followed by a call to {@link #onCapabilitiesChanged(Network, NetworkCapabilities)}
3366 * then by a call to {@link #onLinkPropertiesChanged(Network, LinkProperties)}, and a call
3367 * to {@link #onBlockedStatusChanged(Network, boolean)}.
3368 *
3369 * <p>Do NOT call {@link #getNetworkCapabilities(Network)} or
3370 * {@link #getLinkProperties(Network)} or other synchronous ConnectivityManager methods in
3371 * this callback as this is prone to race conditions (there is no guarantee the objects
3372 * returned by these methods will be current). Instead, wait for a call to
3373 * {@link #onCapabilitiesChanged(Network, NetworkCapabilities)} and
3374 * {@link #onLinkPropertiesChanged(Network, LinkProperties)} whose arguments are guaranteed
3375 * to be well-ordered with respect to other callbacks.
Chalard Jeanb6a0c6d2018-01-30 22:41:41 +09003376 *
3377 * @param network The {@link Network} of the satisfying network.
Robert Greenwaltdb6fac52014-04-18 15:25:25 -07003378 */
paulhua9a6e2a2019-03-22 16:35:06 +08003379 public void onAvailable(@NonNull Network network) {}
Robert Greenwaltdb6fac52014-04-18 15:25:25 -07003380
3381 /**
Chalard Jean7f3a8742019-08-30 16:27:28 +09003382 * Called when the network is about to be lost, typically because there are no outstanding
3383 * requests left for it. This may be paired with a {@link NetworkCallback#onAvailable} call
3384 * with the new replacement network for graceful handover. This method is not guaranteed
3385 * to be called before {@link NetworkCallback#onLost} is called, for example in case a
3386 * network is suddenly disconnected.
Robert Greenwaltd0ebdbb2014-05-18 23:07:25 -07003387 *
Chalard Jean7f3a8742019-08-30 16:27:28 +09003388 * <p>Do NOT call {@link #getNetworkCapabilities(Network)} or
3389 * {@link #getLinkProperties(Network)} or other synchronous ConnectivityManager methods in
3390 * this callback as this is prone to race conditions ; calling these methods while in a
3391 * callback may return an outdated or even a null object.
3392 *
3393 * @param network The {@link Network} that is about to be lost.
3394 * @param maxMsToLive The time in milliseconds the system intends to keep the network
3395 * connected for graceful handover; note that the network may still
3396 * suffer a hard loss at any time.
Robert Greenwaltdb6fac52014-04-18 15:25:25 -07003397 */
paulhua9a6e2a2019-03-22 16:35:06 +08003398 public void onLosing(@NonNull Network network, int maxMsToLive) {}
Robert Greenwaltdb6fac52014-04-18 15:25:25 -07003399
3400 /**
Chalard Jean7f3a8742019-08-30 16:27:28 +09003401 * Called when a network disconnects or otherwise no longer satisfies this request or
3402 * callback.
3403 *
3404 * <p>If the callback was registered with requestNetwork() or
3405 * registerDefaultNetworkCallback(), it will only be invoked against the last network
3406 * returned by onAvailable() when that network is lost and no other network satisfies
3407 * the criteria of the request.
3408 *
3409 * <p>If the callback was registered with registerNetworkCallback() it will be called for
3410 * each network which no longer satisfies the criteria of the callback.
3411 *
3412 * <p>Do NOT call {@link #getNetworkCapabilities(Network)} or
3413 * {@link #getLinkProperties(Network)} or other synchronous ConnectivityManager methods in
3414 * this callback as this is prone to race conditions ; calling these methods while in a
3415 * callback may return an outdated or even a null object.
Robert Greenwaltd0ebdbb2014-05-18 23:07:25 -07003416 *
Robert Greenwaltd0ebdbb2014-05-18 23:07:25 -07003417 * @param network The {@link Network} lost.
Robert Greenwaltdb6fac52014-04-18 15:25:25 -07003418 */
paulhua9a6e2a2019-03-22 16:35:06 +08003419 public void onLost(@NonNull Network network) {}
Robert Greenwaltdb6fac52014-04-18 15:25:25 -07003420
3421 /**
Chalard Jean7f3a8742019-08-30 16:27:28 +09003422 * Called if no network is found within the timeout time specified in
Etan Cohen06633de2019-01-08 12:09:18 -08003423 * {@link #requestNetwork(NetworkRequest, NetworkCallback, int)} call or if the
3424 * requested network request cannot be fulfilled (whether or not a timeout was
3425 * specified). When this callback is invoked the associated
Etan Cohen67e58a02017-03-01 12:47:28 -08003426 * {@link NetworkRequest} will have already been removed and released, as if
3427 * {@link #unregisterNetworkCallback(NetworkCallback)} had been called.
Robert Greenwaltdb6fac52014-04-18 15:25:25 -07003428 */
Robert Greenwalt09e5cff2014-06-11 16:05:07 -07003429 public void onUnavailable() {}
Robert Greenwaltdb6fac52014-04-18 15:25:25 -07003430
3431 /**
Chalard Jean7f3a8742019-08-30 16:27:28 +09003432 * Called when the network corresponding to this request changes capabilities but still
3433 * satisfies the requested criteria.
3434 *
3435 * <p>Starting with {@link android.os.Build.VERSION_CODES#O} this method is guaranteed
3436 * to be called immediately after {@link #onAvailable}.
3437 *
3438 * <p>Do NOT call {@link #getLinkProperties(Network)} or other synchronous
3439 * ConnectivityManager methods in this callback as this is prone to race conditions :
3440 * calling these methods while in a callback may return an outdated or even a null object.
Robert Greenwaltd0ebdbb2014-05-18 23:07:25 -07003441 *
Robert Greenwaltd0ebdbb2014-05-18 23:07:25 -07003442 * @param network The {@link Network} whose capabilities have changed.
Roshan Pius7992afd2020-12-22 15:10:42 -08003443 * @param networkCapabilities The new {@link NetworkCapabilities} for this
Chalard Jeanb6a0c6d2018-01-30 22:41:41 +09003444 * network.
Robert Greenwaltdb6fac52014-04-18 15:25:25 -07003445 */
paulhua9a6e2a2019-03-22 16:35:06 +08003446 public void onCapabilitiesChanged(@NonNull Network network,
3447 @NonNull NetworkCapabilities networkCapabilities) {}
Robert Greenwaltdb6fac52014-04-18 15:25:25 -07003448
3449 /**
Chalard Jean7f3a8742019-08-30 16:27:28 +09003450 * Called when the network corresponding to this request changes {@link LinkProperties}.
3451 *
3452 * <p>Starting with {@link android.os.Build.VERSION_CODES#O} this method is guaranteed
3453 * to be called immediately after {@link #onAvailable}.
3454 *
3455 * <p>Do NOT call {@link #getNetworkCapabilities(Network)} or other synchronous
3456 * ConnectivityManager methods in this callback as this is prone to race conditions :
3457 * calling these methods while in a callback may return an outdated or even a null object.
Robert Greenwaltd0ebdbb2014-05-18 23:07:25 -07003458 *
Robert Greenwaltd0ebdbb2014-05-18 23:07:25 -07003459 * @param network The {@link Network} whose link properties have changed.
3460 * @param linkProperties The new {@link LinkProperties} for this network.
Robert Greenwaltdb6fac52014-04-18 15:25:25 -07003461 */
paulhua9a6e2a2019-03-22 16:35:06 +08003462 public void onLinkPropertiesChanged(@NonNull Network network,
3463 @NonNull LinkProperties linkProperties) {}
Robert Greenwaltdb6fac52014-04-18 15:25:25 -07003464
Robert Greenwalt41c19a02015-06-24 13:23:42 -07003465 /**
Chalard Jean7f3a8742019-08-30 16:27:28 +09003466 * Called when the network the framework connected to for this request suspends data
3467 * transmission temporarily.
3468 *
3469 * <p>This generally means that while the TCP connections are still live temporarily
3470 * network data fails to transfer. To give a specific example, this is used on cellular
3471 * networks to mask temporary outages when driving through a tunnel, etc. In general this
3472 * means read operations on sockets on this network will block once the buffers are
3473 * drained, and write operations will block once the buffers are full.
3474 *
3475 * <p>Do NOT call {@link #getNetworkCapabilities(Network)} or
3476 * {@link #getLinkProperties(Network)} or other synchronous ConnectivityManager methods in
3477 * this callback as this is prone to race conditions (there is no guarantee the objects
3478 * returned by these methods will be current).
3479 *
Robert Greenwalt41c19a02015-06-24 13:23:42 -07003480 * @hide
3481 */
paulhua9a6e2a2019-03-22 16:35:06 +08003482 public void onNetworkSuspended(@NonNull Network network) {}
Robert Greenwalt41c19a02015-06-24 13:23:42 -07003483
3484 /**
3485 * Called when the network the framework connected to for this request
Chalard Jeanb6a0c6d2018-01-30 22:41:41 +09003486 * returns from a {@link NetworkInfo.State#SUSPENDED} state. This should always be
3487 * preceded by a matching {@link NetworkCallback#onNetworkSuspended} call.
Chalard Jean7f3a8742019-08-30 16:27:28 +09003488
3489 * <p>Do NOT call {@link #getNetworkCapabilities(Network)} or
3490 * {@link #getLinkProperties(Network)} or other synchronous ConnectivityManager methods in
3491 * this callback as this is prone to race conditions : calling these methods while in a
3492 * callback may return an outdated or even a null object.
3493 *
Robert Greenwalt41c19a02015-06-24 13:23:42 -07003494 * @hide
3495 */
paulhua9a6e2a2019-03-22 16:35:06 +08003496 public void onNetworkResumed(@NonNull Network network) {}
Robert Greenwalt41c19a02015-06-24 13:23:42 -07003497
junyulai5ab727b2018-08-07 19:50:45 +08003498 /**
3499 * Called when access to the specified network is blocked or unblocked.
3500 *
Chalard Jean7f3a8742019-08-30 16:27:28 +09003501 * <p>Do NOT call {@link #getNetworkCapabilities(Network)} or
3502 * {@link #getLinkProperties(Network)} or other synchronous ConnectivityManager methods in
3503 * this callback as this is prone to race conditions : calling these methods while in a
3504 * callback may return an outdated or even a null object.
3505 *
junyulai5ab727b2018-08-07 19:50:45 +08003506 * @param network The {@link Network} whose blocked status has changed.
3507 * @param blocked The blocked status of this {@link Network}.
3508 */
junyulai61143782019-03-04 22:45:36 +08003509 public void onBlockedStatusChanged(@NonNull Network network, boolean blocked) {}
junyulai5ab727b2018-08-07 19:50:45 +08003510
Robert Greenwalt09e5cff2014-06-11 16:05:07 -07003511 private NetworkRequest networkRequest;
Roshan Pius7992afd2020-12-22 15:10:42 -08003512 private final int mFlags;
Robert Greenwaltdb6fac52014-04-18 15:25:25 -07003513 }
3514
Hugo Benichi145e3792017-05-11 13:16:17 +09003515 /**
3516 * Constant error codes used by ConnectivityService to communicate about failures and errors
3517 * across a Binder boundary.
3518 * @hide
3519 */
3520 public interface Errors {
Chalard Jean73d9db72018-06-04 16:52:49 +09003521 int TOO_MANY_REQUESTS = 1;
Hugo Benichi145e3792017-05-11 13:16:17 +09003522 }
3523
3524 /** @hide */
3525 public static class TooManyRequestsException extends RuntimeException {}
3526
3527 private static RuntimeException convertServiceException(ServiceSpecificException e) {
3528 switch (e.errorCode) {
3529 case Errors.TOO_MANY_REQUESTS:
3530 return new TooManyRequestsException();
3531 default:
3532 Log.w(TAG, "Unknown service error code " + e.errorCode);
3533 return new RuntimeException(e);
3534 }
3535 }
3536
Robert Greenwalt2bebbfb2014-03-26 16:47:06 -07003537 private static final int BASE = Protocol.BASE_CONNECTIVITY_MANAGER;
Robert Greenwalt2bebbfb2014-03-26 16:47:06 -07003538 /** @hide */
Robert Greenwalt41c19a02015-06-24 13:23:42 -07003539 public static final int CALLBACK_PRECHECK = BASE + 1;
3540 /** @hide */
3541 public static final int CALLBACK_AVAILABLE = BASE + 2;
3542 /** @hide arg1 = TTL */
3543 public static final int CALLBACK_LOSING = BASE + 3;
3544 /** @hide */
3545 public static final int CALLBACK_LOST = BASE + 4;
3546 /** @hide */
3547 public static final int CALLBACK_UNAVAIL = BASE + 5;
3548 /** @hide */
3549 public static final int CALLBACK_CAP_CHANGED = BASE + 6;
3550 /** @hide */
3551 public static final int CALLBACK_IP_CHANGED = BASE + 7;
Robert Greenwaltb59a91b2014-05-15 18:07:26 -07003552 /** @hide obj = NetworkCapabilities, arg1 = seq number */
Hugo Benichi4802c802017-03-23 22:40:44 +09003553 private static final int EXPIRE_LEGACY_REQUEST = BASE + 8;
Robert Greenwalt41c19a02015-06-24 13:23:42 -07003554 /** @hide */
Hugo Benichi4802c802017-03-23 22:40:44 +09003555 public static final int CALLBACK_SUSPENDED = BASE + 9;
Robert Greenwalt41c19a02015-06-24 13:23:42 -07003556 /** @hide */
Hugo Benichi4802c802017-03-23 22:40:44 +09003557 public static final int CALLBACK_RESUMED = BASE + 10;
junyulai5ab727b2018-08-07 19:50:45 +08003558 /** @hide */
3559 public static final int CALLBACK_BLK_CHANGED = BASE + 11;
Robert Greenwalt2bebbfb2014-03-26 16:47:06 -07003560
Erik Kline66721822015-11-25 12:49:38 +09003561 /** @hide */
3562 public static String getCallbackName(int whichCallback) {
3563 switch (whichCallback) {
3564 case CALLBACK_PRECHECK: return "CALLBACK_PRECHECK";
3565 case CALLBACK_AVAILABLE: return "CALLBACK_AVAILABLE";
3566 case CALLBACK_LOSING: return "CALLBACK_LOSING";
3567 case CALLBACK_LOST: return "CALLBACK_LOST";
3568 case CALLBACK_UNAVAIL: return "CALLBACK_UNAVAIL";
3569 case CALLBACK_CAP_CHANGED: return "CALLBACK_CAP_CHANGED";
3570 case CALLBACK_IP_CHANGED: return "CALLBACK_IP_CHANGED";
Erik Kline66721822015-11-25 12:49:38 +09003571 case EXPIRE_LEGACY_REQUEST: return "EXPIRE_LEGACY_REQUEST";
3572 case CALLBACK_SUSPENDED: return "CALLBACK_SUSPENDED";
3573 case CALLBACK_RESUMED: return "CALLBACK_RESUMED";
junyulai5ab727b2018-08-07 19:50:45 +08003574 case CALLBACK_BLK_CHANGED: return "CALLBACK_BLK_CHANGED";
Erik Kline66721822015-11-25 12:49:38 +09003575 default:
3576 return Integer.toString(whichCallback);
3577 }
3578 }
3579
Robert Greenwaltb59a91b2014-05-15 18:07:26 -07003580 private class CallbackHandler extends Handler {
Robert Greenwalt2bebbfb2014-03-26 16:47:06 -07003581 private static final String TAG = "ConnectivityManager.CallbackHandler";
Robert Greenwalt9b330772015-09-03 16:41:45 -07003582 private static final boolean DBG = false;
Robert Greenwalt2bebbfb2014-03-26 16:47:06 -07003583
Hugo Benichife3325f2016-07-06 22:53:17 +09003584 CallbackHandler(Looper looper) {
Robert Greenwalt2bebbfb2014-03-26 16:47:06 -07003585 super(looper);
Robert Greenwalt2bebbfb2014-03-26 16:47:06 -07003586 }
3587
Hugo Benichicd5a0e02017-02-02 17:02:36 +09003588 CallbackHandler(Handler handler) {
Remi NGUYEN VAN1c17c882021-03-11 20:49:13 +09003589 this(Objects.requireNonNull(handler, "Handler cannot be null.").getLooper());
Hugo Benichicd5a0e02017-02-02 17:02:36 +09003590 }
3591
Robert Greenwalt2bebbfb2014-03-26 16:47:06 -07003592 @Override
3593 public void handleMessage(Message message) {
Hugo Benichi3b926992017-05-09 14:36:02 +09003594 if (message.what == EXPIRE_LEGACY_REQUEST) {
3595 expireRequest((NetworkCapabilities) message.obj, message.arg1);
3596 return;
3597 }
3598
3599 final NetworkRequest request = getObject(message, NetworkRequest.class);
3600 final Network network = getObject(message, Network.class);
3601 final NetworkCallback callback;
3602 synchronized (sCallbacks) {
3603 callback = sCallbacks.get(request);
Etan Cohenbc3a4eb2019-05-21 12:06:04 -07003604 if (callback == null) {
3605 Log.w(TAG,
3606 "callback not found for " + getCallbackName(message.what) + " message");
3607 return;
3608 }
Etan Cohene0e2ca92019-04-16 15:07:55 -07003609 if (message.what == CALLBACK_UNAVAIL) {
3610 sCallbacks.remove(request);
3611 callback.networkRequest = ALREADY_UNREGISTERED;
3612 }
Hugo Benichi3b926992017-05-09 14:36:02 +09003613 }
Lorenzo Colitti7e286832016-03-01 22:56:37 +09003614 if (DBG) {
Hugo Benichi94f0bfc2017-03-22 17:07:57 +09003615 Log.d(TAG, getCallbackName(message.what) + " for network " + network);
Lorenzo Colitti7e286832016-03-01 22:56:37 +09003616 }
Hugo Benichi3b926992017-05-09 14:36:02 +09003617
Robert Greenwalt2bebbfb2014-03-26 16:47:06 -07003618 switch (message.what) {
3619 case CALLBACK_PRECHECK: {
Hugo Benichi3b926992017-05-09 14:36:02 +09003620 callback.onPreCheck(network);
Robert Greenwalt2bebbfb2014-03-26 16:47:06 -07003621 break;
3622 }
3623 case CALLBACK_AVAILABLE: {
Chalard Jeanb6a0c6d2018-01-30 22:41:41 +09003624 NetworkCapabilities cap = getObject(message, NetworkCapabilities.class);
3625 LinkProperties lp = getObject(message, LinkProperties.class);
junyulai5ab727b2018-08-07 19:50:45 +08003626 callback.onAvailable(network, cap, lp, message.arg1 != 0);
Robert Greenwalt2bebbfb2014-03-26 16:47:06 -07003627 break;
3628 }
3629 case CALLBACK_LOSING: {
Hugo Benichi3b926992017-05-09 14:36:02 +09003630 callback.onLosing(network, message.arg1);
Robert Greenwalt2bebbfb2014-03-26 16:47:06 -07003631 break;
3632 }
3633 case CALLBACK_LOST: {
Hugo Benichi3b926992017-05-09 14:36:02 +09003634 callback.onLost(network);
Robert Greenwalt2bebbfb2014-03-26 16:47:06 -07003635 break;
3636 }
3637 case CALLBACK_UNAVAIL: {
Hugo Benichi3b926992017-05-09 14:36:02 +09003638 callback.onUnavailable();
Robert Greenwalt2bebbfb2014-03-26 16:47:06 -07003639 break;
3640 }
3641 case CALLBACK_CAP_CHANGED: {
Hugo Benichi3b926992017-05-09 14:36:02 +09003642 NetworkCapabilities cap = getObject(message, NetworkCapabilities.class);
3643 callback.onCapabilitiesChanged(network, cap);
Robert Greenwalt2bebbfb2014-03-26 16:47:06 -07003644 break;
3645 }
3646 case CALLBACK_IP_CHANGED: {
Hugo Benichi3b926992017-05-09 14:36:02 +09003647 LinkProperties lp = getObject(message, LinkProperties.class);
3648 callback.onLinkPropertiesChanged(network, lp);
Robert Greenwalt2bebbfb2014-03-26 16:47:06 -07003649 break;
3650 }
Robert Greenwalt41c19a02015-06-24 13:23:42 -07003651 case CALLBACK_SUSPENDED: {
Hugo Benichi3b926992017-05-09 14:36:02 +09003652 callback.onNetworkSuspended(network);
Robert Greenwalt41c19a02015-06-24 13:23:42 -07003653 break;
3654 }
3655 case CALLBACK_RESUMED: {
Hugo Benichi3b926992017-05-09 14:36:02 +09003656 callback.onNetworkResumed(network);
Robert Greenwaltb59a91b2014-05-15 18:07:26 -07003657 break;
3658 }
junyulai5ab727b2018-08-07 19:50:45 +08003659 case CALLBACK_BLK_CHANGED: {
3660 boolean blocked = message.arg1 != 0;
3661 callback.onBlockedStatusChanged(network, blocked);
3662 }
Robert Greenwalt2bebbfb2014-03-26 16:47:06 -07003663 }
3664 }
3665
Hugo Benichife3325f2016-07-06 22:53:17 +09003666 private <T> T getObject(Message msg, Class<T> c) {
3667 return (T) msg.getData().getParcelable(c.getSimpleName());
Robert Greenwalt2bebbfb2014-03-26 16:47:06 -07003668 }
Robert Greenwalt2bebbfb2014-03-26 16:47:06 -07003669 }
3670
Hugo Benichicd5a0e02017-02-02 17:02:36 +09003671 private CallbackHandler getDefaultHandler() {
Hugo Benichi3a545552016-07-07 10:15:56 +09003672 synchronized (sCallbacks) {
3673 if (sCallbackHandler == null) {
3674 sCallbackHandler = new CallbackHandler(ConnectivityThread.getInstanceLooper());
Robert Greenwalt2bebbfb2014-03-26 16:47:06 -07003675 }
Hugo Benichi3a545552016-07-07 10:15:56 +09003676 return sCallbackHandler;
Robert Greenwalt2bebbfb2014-03-26 16:47:06 -07003677 }
3678 }
3679
Hugo Benichiec180d52017-02-03 14:18:44 +09003680 private static final HashMap<NetworkRequest, NetworkCallback> sCallbacks = new HashMap<>();
3681 private static CallbackHandler sCallbackHandler;
Robert Greenwalt2bebbfb2014-03-26 16:47:06 -07003682
Hugo Benichiec180d52017-02-03 14:18:44 +09003683 private NetworkRequest sendRequestForNetwork(NetworkCapabilities need, NetworkCallback callback,
junyulaid1a78162021-01-11 16:53:38 +08003684 int timeoutMs, NetworkRequest.Type reqType, int legacyType, CallbackHandler handler) {
Soi, Yoshinari99efcbd2015-11-12 12:09:02 +09003685 printStackTrace();
Hugo Benichiff4bf602017-05-09 15:19:01 +09003686 checkCallbackNotNull(callback);
Remi NGUYEN VAN1c17c882021-03-11 20:49:13 +09003687 if (reqType != TRACK_DEFAULT && reqType != TRACK_SYSTEM_DEFAULT && need == null) {
3688 throw new IllegalArgumentException("null NetworkCapabilities");
3689 }
Hugo Benichife3325f2016-07-06 22:53:17 +09003690 final NetworkRequest request;
Roshan Pius34dc4be2020-01-16 12:17:17 -08003691 final String callingPackageName = mContext.getOpPackageName();
Robert Greenwalt2bebbfb2014-03-26 16:47:06 -07003692 try {
Hugo Benichife3325f2016-07-06 22:53:17 +09003693 synchronized(sCallbacks) {
Hugo Benichi1c027fe2017-06-17 13:14:12 +09003694 if (callback.networkRequest != null
3695 && callback.networkRequest != ALREADY_UNREGISTERED) {
Hugo Benichi45a49542017-03-06 09:17:06 +09003696 // TODO: throw exception instead and enforce 1:1 mapping of callbacks
3697 // and requests (http://b/20701525).
3698 Log.e(TAG, "NetworkCallback was already registered");
3699 }
Hugo Benichi3a545552016-07-07 10:15:56 +09003700 Messenger messenger = new Messenger(handler);
Hugo Benichife3325f2016-07-06 22:53:17 +09003701 Binder binder = new Binder();
Roshan Pius7992afd2020-12-22 15:10:42 -08003702 final int callbackFlags = callback.mFlags;
junyulaid1a78162021-01-11 16:53:38 +08003703 if (reqType == LISTEN) {
Roshan Pius34dc4be2020-01-16 12:17:17 -08003704 request = mService.listenForNetwork(
Roshan Pius7992afd2020-12-22 15:10:42 -08003705 need, messenger, binder, callbackFlags, callingPackageName,
Roshan Piusc97d8062020-12-17 14:53:09 -08003706 getAttributionTag());
Paul Jensenf980a0f2014-06-27 11:05:32 -04003707 } else {
Hugo Benichife3325f2016-07-06 22:53:17 +09003708 request = mService.requestNetwork(
junyulaid1a78162021-01-11 16:53:38 +08003709 need, reqType.ordinal(), messenger, timeoutMs, binder, legacyType,
Roshan Pius7992afd2020-12-22 15:10:42 -08003710 callbackFlags, callingPackageName, getAttributionTag());
Paul Jensenf980a0f2014-06-27 11:05:32 -04003711 }
Hugo Benichife3325f2016-07-06 22:53:17 +09003712 if (request != null) {
Hugo Benichi3a545552016-07-07 10:15:56 +09003713 sCallbacks.put(request, callback);
Robert Greenwalt2bebbfb2014-03-26 16:47:06 -07003714 }
Hugo Benichi3a545552016-07-07 10:15:56 +09003715 callback.networkRequest = request;
Robert Greenwalt2bebbfb2014-03-26 16:47:06 -07003716 }
Jeff Sharkeya6066fd2016-03-01 19:27:23 -07003717 } catch (RemoteException e) {
3718 throw e.rethrowFromSystemServer();
Hugo Benichi145e3792017-05-11 13:16:17 +09003719 } catch (ServiceSpecificException e) {
3720 throw convertServiceException(e);
Jeff Sharkeya6066fd2016-03-01 19:27:23 -07003721 }
Hugo Benichife3325f2016-07-06 22:53:17 +09003722 return request;
Robert Greenwalt2bebbfb2014-03-26 16:47:06 -07003723 }
3724
3725 /**
Erik Klinee0aed632016-03-16 15:31:39 +09003726 * Helper function to request a network with a particular legacy type.
Lorenzo Colitti873f7bb2015-11-25 12:00:52 +09003727 *
markchieneddbe2b2020-03-18 21:16:15 +08003728 * This API is only for use in internal system code that requests networks with legacy type and
3729 * relies on CONNECTIVITY_ACTION broadcasts instead of NetworkCallbacks. New caller should use
markchienddd59372020-01-14 00:55:21 +08003730 * {@link #requestNetwork(NetworkRequest, NetworkCallback, Handler)} instead.
Lorenzo Colitti873f7bb2015-11-25 12:00:52 +09003731 *
markchienddd59372020-01-14 00:55:21 +08003732 * @param request {@link NetworkRequest} describing this request.
markchienddd59372020-01-14 00:55:21 +08003733 * @param timeoutMs The time in milliseconds to attempt looking for a suitable network
3734 * before {@link NetworkCallback#onUnavailable()} is called. The timeout must
3735 * be a positive value (i.e. >0).
3736 * @param legacyType to specify the network type(#TYPE_*).
3737 * @param handler {@link Handler} to specify the thread upon which the callback will be invoked.
markchieneddbe2b2020-03-18 21:16:15 +08003738 * @param networkCallback The {@link NetworkCallback} to be utilized for this request. Note
3739 * the callback must not be shared - it uniquely specifies this request.
Lorenzo Colitti873f7bb2015-11-25 12:00:52 +09003740 *
3741 * @hide
3742 */
markchienddd59372020-01-14 00:55:21 +08003743 @SystemApi
markchieneddbe2b2020-03-18 21:16:15 +08003744 @RequiresPermission(NetworkStack.PERMISSION_MAINLINE_NETWORK_STACK)
Chalard Jean42a9c292019-01-07 19:26:34 +09003745 public void requestNetwork(@NonNull NetworkRequest request,
markchieneddbe2b2020-03-18 21:16:15 +08003746 int timeoutMs, int legacyType, @NonNull Handler handler,
3747 @NonNull NetworkCallback networkCallback) {
3748 if (legacyType == TYPE_NONE) {
3749 throw new IllegalArgumentException("TYPE_NONE is meaningless legacy type");
3750 }
Hugo Benichicd5a0e02017-02-02 17:02:36 +09003751 CallbackHandler cbHandler = new CallbackHandler(handler);
3752 NetworkCapabilities nc = request.networkCapabilities;
3753 sendRequestForNetwork(nc, networkCallback, timeoutMs, REQUEST, legacyType, cbHandler);
Lorenzo Colitti873f7bb2015-11-25 12:00:52 +09003754 }
3755
3756 /**
Roshan Pius7992afd2020-12-22 15:10:42 -08003757 * Request a network to satisfy a set of {@link NetworkCapabilities}.
Robert Greenwalt2bebbfb2014-03-26 16:47:06 -07003758 *
Chalard Jean7f3a8742019-08-30 16:27:28 +09003759 * <p>This method will attempt to find the best network that matches the passed
3760 * {@link NetworkRequest}, and to bring up one that does if none currently satisfies the
3761 * criteria. The platform will evaluate which network is the best at its own discretion.
3762 * Throughput, latency, cost per byte, policy, user preference and other considerations
3763 * may be factored in the decision of what is considered the best network.
3764 *
3765 * <p>As long as this request is outstanding, the platform will try to maintain the best network
3766 * matching this request, while always attempting to match the request to a better network if
3767 * possible. If a better match is found, the platform will switch this request to the now-best
3768 * network and inform the app of the newly best network by invoking
3769 * {@link NetworkCallback#onAvailable(Network)} on the provided callback. Note that the platform
3770 * will not try to maintain any other network than the best one currently matching the request:
3771 * a network not matching any network request may be disconnected at any time.
3772 *
3773 * <p>For example, an application could use this method to obtain a connected cellular network
3774 * even if the device currently has a data connection over Ethernet. This may cause the cellular
3775 * radio to consume additional power. Or, an application could inform the system that it wants
3776 * a network supporting sending MMSes and have the system let it know about the currently best
3777 * MMS-supporting network through the provided {@link NetworkCallback}.
3778 *
3779 * <p>The status of the request can be followed by listening to the various callbacks described
3780 * in {@link NetworkCallback}. The {@link Network} object passed to the callback methods can be
3781 * used to direct traffic to the network (although accessing some networks may be subject to
3782 * holding specific permissions). Callers will learn about the specific characteristics of the
3783 * network through
3784 * {@link NetworkCallback#onCapabilitiesChanged(Network, NetworkCapabilities)} and
3785 * {@link NetworkCallback#onLinkPropertiesChanged(Network, LinkProperties)}. The methods of the
3786 * provided {@link NetworkCallback} will only be invoked due to changes in the best network
3787 * matching the request at any given time; therefore when a better network matching the request
3788 * becomes available, the {@link NetworkCallback#onAvailable(Network)} method is called
3789 * with the new network after which no further updates are given about the previously-best
3790 * network, unless it becomes the best again at some later time. All callbacks are invoked
3791 * in order on the same thread, which by default is a thread created by the framework running
3792 * in the app.
3793 * {@see #requestNetwork(NetworkRequest, NetworkCallback, Handler)} to change where the
3794 * callbacks are invoked.
3795 *
3796 * <p>This{@link NetworkRequest} will live until released via
3797 * {@link #unregisterNetworkCallback(NetworkCallback)} or the calling application exits, at
3798 * which point the system may let go of the network at any time.
3799 *
3800 * <p>A version of this method which takes a timeout is
3801 * {@link #requestNetwork(NetworkRequest, NetworkCallback, int)}, that an app can use to only
3802 * wait for a limited amount of time for the network to become unavailable.
3803 *
Paul Jensen1fcbf9c2015-06-16 15:11:58 -04003804 * <p>It is presently unsupported to request a network with mutable
3805 * {@link NetworkCapabilities} such as
3806 * {@link NetworkCapabilities#NET_CAPABILITY_VALIDATED} or
3807 * {@link NetworkCapabilities#NET_CAPABILITY_CAPTIVE_PORTAL}
3808 * as these {@code NetworkCapabilities} represent states that a particular
3809 * network may never attain, and whether a network will attain these states
3810 * is unknown prior to bringing up the network so the framework does not
Chalard Jean7f3a8742019-08-30 16:27:28 +09003811 * know how to go about satisfying a request with these capabilities.
Lorenzo Colitti91de63b2015-10-15 16:29:00 +09003812 *
3813 * <p>This method requires the caller to hold either the
3814 * {@link android.Manifest.permission#CHANGE_NETWORK_STATE} permission
3815 * or the ability to modify system settings as determined by
3816 * {@link android.provider.Settings.System#canWrite}.</p>
Robert Greenwalt2bebbfb2014-03-26 16:47:06 -07003817 *
Chalard Jean7f06b342020-05-20 16:11:50 +09003818 * <p>To avoid performance issues due to apps leaking callbacks, the system will limit the
3819 * number of outstanding requests to 100 per app (identified by their UID), shared with
3820 * all variants of this method, of {@link #registerNetworkCallback} as well as
3821 * {@link ConnectivityDiagnosticsManager#registerConnectivityDiagnosticsCallback}.
3822 * Requesting a network with this method will count toward this limit. If this limit is
3823 * exceeded, an exception will be thrown. To avoid hitting this issue and to conserve resources,
3824 * make sure to unregister the callbacks with
3825 * {@link #unregisterNetworkCallback(NetworkCallback)}.
3826 *
Robert Greenwalt09e5cff2014-06-11 16:05:07 -07003827 * @param request {@link NetworkRequest} describing this request.
Hugo Benichicd5a0e02017-02-02 17:02:36 +09003828 * @param networkCallback The {@link NetworkCallback} to be utilized for this request. Note
3829 * the callback must not be shared - it uniquely specifies this request.
3830 * The callback is invoked on the default internal Handler.
Chalard Jean9f1a7632019-05-13 15:13:58 +09003831 * @throws IllegalArgumentException if {@code request} contains invalid network capabilities.
3832 * @throws SecurityException if missing the appropriate permissions.
Chalard Jean7f06b342020-05-20 16:11:50 +09003833 * @throws RuntimeException if the app already has too many callbacks registered.
Robert Greenwalt2bebbfb2014-03-26 16:47:06 -07003834 */
Chalard Jean42a9c292019-01-07 19:26:34 +09003835 public void requestNetwork(@NonNull NetworkRequest request,
3836 @NonNull NetworkCallback networkCallback) {
Hugo Benichicd5a0e02017-02-02 17:02:36 +09003837 requestNetwork(request, networkCallback, getDefaultHandler());
3838 }
3839
3840 /**
Roshan Pius7992afd2020-12-22 15:10:42 -08003841 * Request a network to satisfy a set of {@link NetworkCapabilities}.
Hugo Benichicd5a0e02017-02-02 17:02:36 +09003842 *
Chalard Jean7f3a8742019-08-30 16:27:28 +09003843 * This method behaves identically to {@link #requestNetwork(NetworkRequest, NetworkCallback)}
3844 * but runs all the callbacks on the passed Handler.
Hugo Benichicd5a0e02017-02-02 17:02:36 +09003845 *
Chalard Jean7f3a8742019-08-30 16:27:28 +09003846 * <p>This method has the same permission requirements as
Chalard Jean7f06b342020-05-20 16:11:50 +09003847 * {@link #requestNetwork(NetworkRequest, NetworkCallback)}, is subject to the same limitations,
3848 * and throws the same exceptions in the same conditions.
Hugo Benichicd5a0e02017-02-02 17:02:36 +09003849 *
3850 * @param request {@link NetworkRequest} describing this request.
3851 * @param networkCallback The {@link NetworkCallback} to be utilized for this request. Note
3852 * the callback must not be shared - it uniquely specifies this request.
3853 * @param handler {@link Handler} to specify the thread upon which the callback will be invoked.
Hugo Benichicd5a0e02017-02-02 17:02:36 +09003854 */
Chalard Jean42a9c292019-01-07 19:26:34 +09003855 public void requestNetwork(@NonNull NetworkRequest request,
3856 @NonNull NetworkCallback networkCallback, @NonNull Handler handler) {
Hugo Benichicd5a0e02017-02-02 17:02:36 +09003857 CallbackHandler cbHandler = new CallbackHandler(handler);
markchieneddbe2b2020-03-18 21:16:15 +08003858 NetworkCapabilities nc = request.networkCapabilities;
3859 sendRequestForNetwork(nc, networkCallback, 0, REQUEST, TYPE_NONE, cbHandler);
Robert Greenwalt2bebbfb2014-03-26 16:47:06 -07003860 }
3861
3862 /**
Roshan Pius7992afd2020-12-22 15:10:42 -08003863 * Request a network to satisfy a set of {@link NetworkCapabilities}, limited
Etan Cohen67e58a02017-03-01 12:47:28 -08003864 * by a timeout.
3865 *
3866 * This function behaves identically to the non-timed-out version
3867 * {@link #requestNetwork(NetworkRequest, NetworkCallback)}, but if a suitable network
3868 * is not found within the given time (in milliseconds) the
3869 * {@link NetworkCallback#onUnavailable()} callback is called. The request can still be
3870 * released normally by calling {@link #unregisterNetworkCallback(NetworkCallback)} but does
3871 * not have to be released if timed-out (it is automatically released). Unregistering a
3872 * request that timed out is not an error.
3873 *
3874 * <p>Do not use this method to poll for the existence of specific networks (e.g. with a small
3875 * timeout) - {@link #registerNetworkCallback(NetworkRequest, NetworkCallback)} is provided
3876 * for that purpose. Calling this method will attempt to bring up the requested network.
3877 *
Chalard Jean7f3a8742019-08-30 16:27:28 +09003878 * <p>This method has the same permission requirements as
Chalard Jean7f06b342020-05-20 16:11:50 +09003879 * {@link #requestNetwork(NetworkRequest, NetworkCallback)}, is subject to the same limitations,
3880 * and throws the same exceptions in the same conditions.
Etan Cohen67e58a02017-03-01 12:47:28 -08003881 *
3882 * @param request {@link NetworkRequest} describing this request.
Lorenzo Colitti38ea8102017-04-28 00:56:30 +09003883 * @param networkCallback The {@link NetworkCallback} to be utilized for this request. Note
3884 * the callback must not be shared - it uniquely specifies this request.
Etan Cohen67e58a02017-03-01 12:47:28 -08003885 * @param timeoutMs The time in milliseconds to attempt looking for a suitable network
3886 * before {@link NetworkCallback#onUnavailable()} is called. The timeout must
3887 * be a positive value (i.e. >0).
Etan Cohen67e58a02017-03-01 12:47:28 -08003888 */
Chalard Jean42a9c292019-01-07 19:26:34 +09003889 public void requestNetwork(@NonNull NetworkRequest request,
3890 @NonNull NetworkCallback networkCallback, int timeoutMs) {
Hugo Benichiff4bf602017-05-09 15:19:01 +09003891 checkTimeout(timeoutMs);
markchieneddbe2b2020-03-18 21:16:15 +08003892 NetworkCapabilities nc = request.networkCapabilities;
3893 sendRequestForNetwork(nc, networkCallback, timeoutMs, REQUEST, TYPE_NONE,
3894 getDefaultHandler());
Hugo Benichicd5a0e02017-02-02 17:02:36 +09003895 }
3896
Hugo Benichicd5a0e02017-02-02 17:02:36 +09003897 /**
Roshan Pius7992afd2020-12-22 15:10:42 -08003898 * Request a network to satisfy a set of {@link NetworkCapabilities}, limited
Hugo Benichicd5a0e02017-02-02 17:02:36 +09003899 * by a timeout.
3900 *
Chalard Jean7f3a8742019-08-30 16:27:28 +09003901 * This method behaves identically to
3902 * {@link #requestNetwork(NetworkRequest, NetworkCallback, int)} but runs all the callbacks
3903 * on the passed Handler.
Etan Cohen67e58a02017-03-01 12:47:28 -08003904 *
Chalard Jean7f3a8742019-08-30 16:27:28 +09003905 * <p>This method has the same permission requirements as
Chalard Jean7f06b342020-05-20 16:11:50 +09003906 * {@link #requestNetwork(NetworkRequest, NetworkCallback)}, is subject to the same limitations,
3907 * and throws the same exceptions in the same conditions.
Hugo Benichicd5a0e02017-02-02 17:02:36 +09003908 *
3909 * @param request {@link NetworkRequest} describing this request.
Etan Cohen67e58a02017-03-01 12:47:28 -08003910 * @param networkCallback The {@link NetworkCallback} to be utilized for this request. Note
3911 * the callback must not be shared - it uniquely specifies this request.
Hugo Benichicd5a0e02017-02-02 17:02:36 +09003912 * @param handler {@link Handler} to specify the thread upon which the callback will be invoked.
Lorenzo Colitti38ea8102017-04-28 00:56:30 +09003913 * @param timeoutMs The time in milliseconds to attempt looking for a suitable network
3914 * before {@link NetworkCallback#onUnavailable} is called.
Hugo Benichicd5a0e02017-02-02 17:02:36 +09003915 */
Chalard Jean42a9c292019-01-07 19:26:34 +09003916 public void requestNetwork(@NonNull NetworkRequest request,
3917 @NonNull NetworkCallback networkCallback, @NonNull Handler handler, int timeoutMs) {
Hugo Benichiff4bf602017-05-09 15:19:01 +09003918 checkTimeout(timeoutMs);
Hugo Benichicd5a0e02017-02-02 17:02:36 +09003919 CallbackHandler cbHandler = new CallbackHandler(handler);
markchieneddbe2b2020-03-18 21:16:15 +08003920 NetworkCapabilities nc = request.networkCapabilities;
3921 sendRequestForNetwork(nc, networkCallback, timeoutMs, REQUEST, TYPE_NONE, cbHandler);
Robert Greenwalt2bebbfb2014-03-26 16:47:06 -07003922 }
3923
3924 /**
Robert Greenwaltd0ebdbb2014-05-18 23:07:25 -07003925 * The lookup key for a {@link Network} object included with the intent after
Jeremy Joslin94c30992014-11-05 10:32:09 -08003926 * successfully finding a network for the applications request. Retrieve it with
Robert Greenwaltd0ebdbb2014-05-18 23:07:25 -07003927 * {@link android.content.Intent#getParcelableExtra(String)}.
Jeremy Joslinfe76e0e2015-02-11 16:51:13 -08003928 * <p>
Paul Jensen895c3942015-03-10 10:54:12 -04003929 * Note that if you intend to invoke {@link Network#openConnection(java.net.URL)}
3930 * then you must get a ConnectivityManager instance before doing so.
Robert Greenwaltd0ebdbb2014-05-18 23:07:25 -07003931 */
Erik Klinefb087f12014-11-19 12:12:24 +09003932 public static final String EXTRA_NETWORK = "android.net.extra.NETWORK";
Robert Greenwaltd0ebdbb2014-05-18 23:07:25 -07003933
3934 /**
Robert Greenwalt09e5cff2014-06-11 16:05:07 -07003935 * The lookup key for a {@link NetworkRequest} object included with the intent after
Jeremy Joslin94c30992014-11-05 10:32:09 -08003936 * successfully finding a network for the applications request. Retrieve it with
Robert Greenwaltd0ebdbb2014-05-18 23:07:25 -07003937 * {@link android.content.Intent#getParcelableExtra(String)}.
3938 */
Erik Klinefb087f12014-11-19 12:12:24 +09003939 public static final String EXTRA_NETWORK_REQUEST = "android.net.extra.NETWORK_REQUEST";
Robert Greenwaltd0ebdbb2014-05-18 23:07:25 -07003940
3941
3942 /**
Roshan Pius7992afd2020-12-22 15:10:42 -08003943 * Request a network to satisfy a set of {@link NetworkCapabilities}.
Robert Greenwalt2bebbfb2014-03-26 16:47:06 -07003944 *
Jeremy Joslin94c30992014-11-05 10:32:09 -08003945 * This function behaves identically to the version that takes a NetworkCallback, but instead
Robert Greenwalt09e5cff2014-06-11 16:05:07 -07003946 * of {@link NetworkCallback} a {@link PendingIntent} is used. This means
Robert Greenwaltd0ebdbb2014-05-18 23:07:25 -07003947 * the request may outlive the calling application and get called back when a suitable
3948 * network is found.
Robert Greenwalt2bebbfb2014-03-26 16:47:06 -07003949 * <p>
3950 * The operation is an Intent broadcast that goes to a broadcast receiver that
3951 * you registered with {@link Context#registerReceiver} or through the
3952 * &lt;receiver&gt; tag in an AndroidManifest.xml file
3953 * <p>
3954 * The operation Intent is delivered with two extras, a {@link Network} typed
Erik Klinefb087f12014-11-19 12:12:24 +09003955 * extra called {@link #EXTRA_NETWORK} and a {@link NetworkRequest}
3956 * typed extra called {@link #EXTRA_NETWORK_REQUEST} containing
Robert Greenwalt2bebbfb2014-03-26 16:47:06 -07003957 * the original requests parameters. It is important to create a new,
Robert Greenwalt09e5cff2014-06-11 16:05:07 -07003958 * {@link NetworkCallback} based request before completing the processing of the
Robert Greenwalt2bebbfb2014-03-26 16:47:06 -07003959 * Intent to reserve the network or it will be released shortly after the Intent
3960 * is processed.
3961 * <p>
Paul Jensen9ceed862015-06-17 14:15:39 -04003962 * If there is already a request for this Intent registered (with the equality of
Robert Greenwalt2bebbfb2014-03-26 16:47:06 -07003963 * two Intents defined by {@link Intent#filterEquals}), then it will be removed and
Robert Greenwaltd0ebdbb2014-05-18 23:07:25 -07003964 * replaced by this one, effectively releasing the previous {@link NetworkRequest}.
Robert Greenwalt2bebbfb2014-03-26 16:47:06 -07003965 * <p>
Jeremy Joslin94c30992014-11-05 10:32:09 -08003966 * The request may be released normally by calling
3967 * {@link #releaseNetworkRequest(android.app.PendingIntent)}.
Paul Jensen1fcbf9c2015-06-16 15:11:58 -04003968 * <p>It is presently unsupported to request a network with either
3969 * {@link NetworkCapabilities#NET_CAPABILITY_VALIDATED} or
3970 * {@link NetworkCapabilities#NET_CAPABILITY_CAPTIVE_PORTAL}
3971 * as these {@code NetworkCapabilities} represent states that a particular
3972 * network may never attain, and whether a network will attain these states
3973 * is unknown prior to bringing up the network so the framework does not
Chalard Jean73d9db72018-06-04 16:52:49 +09003974 * know how to go about satisfying a request with these capabilities.
Lorenzo Colitti91de63b2015-10-15 16:29:00 +09003975 *
Chalard Jean7f06b342020-05-20 16:11:50 +09003976 * <p>To avoid performance issues due to apps leaking callbacks, the system will limit the
3977 * number of outstanding requests to 100 per app (identified by their UID), shared with
3978 * all variants of this method, of {@link #registerNetworkCallback} as well as
3979 * {@link ConnectivityDiagnosticsManager#registerConnectivityDiagnosticsCallback}.
3980 * Requesting a network with this method will count toward this limit. If this limit is
3981 * exceeded, an exception will be thrown. To avoid hitting this issue and to conserve resources,
3982 * make sure to unregister the callbacks with {@link #unregisterNetworkCallback(PendingIntent)}
3983 * or {@link #releaseNetworkRequest(PendingIntent)}.
3984 *
Lorenzo Colitti91de63b2015-10-15 16:29:00 +09003985 * <p>This method requires the caller to hold either the
3986 * {@link android.Manifest.permission#CHANGE_NETWORK_STATE} permission
3987 * or the ability to modify system settings as determined by
3988 * {@link android.provider.Settings.System#canWrite}.</p>
3989 *
Robert Greenwalt09e5cff2014-06-11 16:05:07 -07003990 * @param request {@link NetworkRequest} describing this request.
Robert Greenwalt2bebbfb2014-03-26 16:47:06 -07003991 * @param operation Action to perform when the network is available (corresponds
Robert Greenwalt09e5cff2014-06-11 16:05:07 -07003992 * to the {@link NetworkCallback#onAvailable} call. Typically
Jeremy Joslin94c30992014-11-05 10:32:09 -08003993 * comes from {@link PendingIntent#getBroadcast}. Cannot be null.
Chalard Jean9f1a7632019-05-13 15:13:58 +09003994 * @throws IllegalArgumentException if {@code request} contains invalid network capabilities.
3995 * @throws SecurityException if missing the appropriate permissions.
Chalard Jean7f06b342020-05-20 16:11:50 +09003996 * @throws RuntimeException if the app already has too many callbacks registered.
Robert Greenwalt2bebbfb2014-03-26 16:47:06 -07003997 */
Chalard Jean42a9c292019-01-07 19:26:34 +09003998 public void requestNetwork(@NonNull NetworkRequest request,
3999 @NonNull PendingIntent operation) {
Soi, Yoshinari99efcbd2015-11-12 12:09:02 +09004000 printStackTrace();
Hugo Benichiff4bf602017-05-09 15:19:01 +09004001 checkPendingIntentNotNull(operation);
Robert Greenwalt2bebbfb2014-03-26 16:47:06 -07004002 try {
Roshan Pius34dc4be2020-01-16 12:17:17 -08004003 mService.pendingRequestForNetwork(
Philip P. Moltmannbbf050b2020-03-26 11:50:35 -07004004 request.networkCapabilities, operation, mContext.getOpPackageName(),
4005 getAttributionTag());
Jeff Sharkeya6066fd2016-03-01 19:27:23 -07004006 } catch (RemoteException e) {
4007 throw e.rethrowFromSystemServer();
Hugo Benichi145e3792017-05-11 13:16:17 +09004008 } catch (ServiceSpecificException e) {
4009 throw convertServiceException(e);
Jeff Sharkeya6066fd2016-03-01 19:27:23 -07004010 }
Robert Greenwalt2bebbfb2014-03-26 16:47:06 -07004011 }
4012
4013 /**
Jeremy Joslin94c30992014-11-05 10:32:09 -08004014 * Removes a request made via {@link #requestNetwork(NetworkRequest, android.app.PendingIntent)}
4015 * <p>
Lorenzo Colittiad458ad2016-04-13 22:00:02 +09004016 * This method has the same behavior as
4017 * {@link #unregisterNetworkCallback(android.app.PendingIntent)} with respect to
Jeremy Joslin94c30992014-11-05 10:32:09 -08004018 * releasing network resources and disconnecting.
4019 *
4020 * @param operation A PendingIntent equal (as defined by {@link Intent#filterEquals}) to the
4021 * PendingIntent passed to
4022 * {@link #requestNetwork(NetworkRequest, android.app.PendingIntent)} with the
4023 * corresponding NetworkRequest you'd like to remove. Cannot be null.
4024 */
Chalard Jean42a9c292019-01-07 19:26:34 +09004025 public void releaseNetworkRequest(@NonNull PendingIntent operation) {
Soi, Yoshinari99efcbd2015-11-12 12:09:02 +09004026 printStackTrace();
Hugo Benichiff4bf602017-05-09 15:19:01 +09004027 checkPendingIntentNotNull(operation);
Jeremy Joslin94c30992014-11-05 10:32:09 -08004028 try {
4029 mService.releasePendingNetworkRequest(operation);
Jeff Sharkeya6066fd2016-03-01 19:27:23 -07004030 } catch (RemoteException e) {
4031 throw e.rethrowFromSystemServer();
4032 }
Jeremy Joslin94c30992014-11-05 10:32:09 -08004033 }
4034
Hugo Benichiff4bf602017-05-09 15:19:01 +09004035 private static void checkPendingIntentNotNull(PendingIntent intent) {
Remi NGUYEN VAN1c17c882021-03-11 20:49:13 +09004036 Objects.requireNonNull(intent, "PendingIntent cannot be null.");
Hugo Benichiff4bf602017-05-09 15:19:01 +09004037 }
4038
4039 private static void checkCallbackNotNull(NetworkCallback callback) {
Remi NGUYEN VAN1c17c882021-03-11 20:49:13 +09004040 Objects.requireNonNull(callback, "null NetworkCallback");
Hugo Benichiff4bf602017-05-09 15:19:01 +09004041 }
4042
4043 private static void checkTimeout(int timeoutMs) {
Remi NGUYEN VAN1c17c882021-03-11 20:49:13 +09004044 if (timeoutMs <= 0) {
4045 throw new IllegalArgumentException("timeoutMs must be strictly positive.");
4046 }
Jeremy Joslin94c30992014-11-05 10:32:09 -08004047 }
4048
4049 /**
Robert Greenwalt2bebbfb2014-03-26 16:47:06 -07004050 * Registers to receive notifications about all networks which satisfy the given
Robert Greenwalt09e5cff2014-06-11 16:05:07 -07004051 * {@link NetworkRequest}. The callbacks will continue to be called until
Chiachang Wang0d92d1a2019-02-27 17:14:50 +08004052 * either the application exits or {@link #unregisterNetworkCallback(NetworkCallback)} is
4053 * called.
Robert Greenwalt2bebbfb2014-03-26 16:47:06 -07004054 *
Chalard Jean7f06b342020-05-20 16:11:50 +09004055 * <p>To avoid performance issues due to apps leaking callbacks, the system will limit the
4056 * number of outstanding requests to 100 per app (identified by their UID), shared with
4057 * all variants of this method, of {@link #requestNetwork} as well as
4058 * {@link ConnectivityDiagnosticsManager#registerConnectivityDiagnosticsCallback}.
4059 * Requesting a network with this method will count toward this limit. If this limit is
4060 * exceeded, an exception will be thrown. To avoid hitting this issue and to conserve resources,
4061 * make sure to unregister the callbacks with
4062 * {@link #unregisterNetworkCallback(NetworkCallback)}.
4063 *
Robert Greenwalt09e5cff2014-06-11 16:05:07 -07004064 * @param request {@link NetworkRequest} describing this request.
4065 * @param networkCallback The {@link NetworkCallback} that the system will call as suitable
4066 * networks change state.
Hugo Benichicd5a0e02017-02-02 17:02:36 +09004067 * The callback is invoked on the default internal Handler.
Chalard Jean7f06b342020-05-20 16:11:50 +09004068 * @throws RuntimeException if the app already has too many callbacks registered.
Robert Greenwalt2bebbfb2014-03-26 16:47:06 -07004069 */
Jeff Sharkey2ac62992017-04-24 11:18:03 -06004070 @RequiresPermission(android.Manifest.permission.ACCESS_NETWORK_STATE)
Chalard Jean42a9c292019-01-07 19:26:34 +09004071 public void registerNetworkCallback(@NonNull NetworkRequest request,
4072 @NonNull NetworkCallback networkCallback) {
Hugo Benichicd5a0e02017-02-02 17:02:36 +09004073 registerNetworkCallback(request, networkCallback, getDefaultHandler());
4074 }
4075
4076 /**
4077 * Registers to receive notifications about all networks which satisfy the given
4078 * {@link NetworkRequest}. The callbacks will continue to be called until
Chiachang Wang0d92d1a2019-02-27 17:14:50 +08004079 * either the application exits or {@link #unregisterNetworkCallback(NetworkCallback)} is
4080 * called.
Hugo Benichicd5a0e02017-02-02 17:02:36 +09004081 *
Chalard Jean7f06b342020-05-20 16:11:50 +09004082 * <p>To avoid performance issues due to apps leaking callbacks, the system will limit the
4083 * number of outstanding requests to 100 per app (identified by their UID), shared with
4084 * all variants of this method, of {@link #requestNetwork} as well as
4085 * {@link ConnectivityDiagnosticsManager#registerConnectivityDiagnosticsCallback}.
4086 * Requesting a network with this method will count toward this limit. If this limit is
4087 * exceeded, an exception will be thrown. To avoid hitting this issue and to conserve resources,
4088 * make sure to unregister the callbacks with
4089 * {@link #unregisterNetworkCallback(NetworkCallback)}.
4090 *
4091 *
Hugo Benichicd5a0e02017-02-02 17:02:36 +09004092 * @param request {@link NetworkRequest} describing this request.
4093 * @param networkCallback The {@link NetworkCallback} that the system will call as suitable
4094 * networks change state.
4095 * @param handler {@link Handler} to specify the thread upon which the callback will be invoked.
Chalard Jean7f06b342020-05-20 16:11:50 +09004096 * @throws RuntimeException if the app already has too many callbacks registered.
Hugo Benichicd5a0e02017-02-02 17:02:36 +09004097 */
Jeff Sharkey2ac62992017-04-24 11:18:03 -06004098 @RequiresPermission(android.Manifest.permission.ACCESS_NETWORK_STATE)
Chalard Jean42a9c292019-01-07 19:26:34 +09004099 public void registerNetworkCallback(@NonNull NetworkRequest request,
4100 @NonNull NetworkCallback networkCallback, @NonNull Handler handler) {
Hugo Benichicd5a0e02017-02-02 17:02:36 +09004101 CallbackHandler cbHandler = new CallbackHandler(handler);
4102 NetworkCapabilities nc = request.networkCapabilities;
4103 sendRequestForNetwork(nc, networkCallback, 0, LISTEN, TYPE_NONE, cbHandler);
Robert Greenwalt2bebbfb2014-03-26 16:47:06 -07004104 }
4105
4106 /**
Paul Jensen9ceed862015-06-17 14:15:39 -04004107 * Registers a PendingIntent to be sent when a network is available which satisfies the given
4108 * {@link NetworkRequest}.
4109 *
4110 * This function behaves identically to the version that takes a NetworkCallback, but instead
4111 * of {@link NetworkCallback} a {@link PendingIntent} is used. This means
4112 * the request may outlive the calling application and get called back when a suitable
4113 * network is found.
4114 * <p>
4115 * The operation is an Intent broadcast that goes to a broadcast receiver that
4116 * you registered with {@link Context#registerReceiver} or through the
4117 * &lt;receiver&gt; tag in an AndroidManifest.xml file
4118 * <p>
4119 * The operation Intent is delivered with two extras, a {@link Network} typed
4120 * extra called {@link #EXTRA_NETWORK} and a {@link NetworkRequest}
4121 * typed extra called {@link #EXTRA_NETWORK_REQUEST} containing
4122 * the original requests parameters.
4123 * <p>
4124 * If there is already a request for this Intent registered (with the equality of
4125 * two Intents defined by {@link Intent#filterEquals}), then it will be removed and
4126 * replaced by this one, effectively releasing the previous {@link NetworkRequest}.
4127 * <p>
4128 * The request may be released normally by calling
Paul Jensen78f6b802015-06-30 14:29:18 -04004129 * {@link #unregisterNetworkCallback(android.app.PendingIntent)}.
Chalard Jean7f06b342020-05-20 16:11:50 +09004130 *
4131 * <p>To avoid performance issues due to apps leaking callbacks, the system will limit the
4132 * number of outstanding requests to 100 per app (identified by their UID), shared with
4133 * all variants of this method, of {@link #requestNetwork} as well as
4134 * {@link ConnectivityDiagnosticsManager#registerConnectivityDiagnosticsCallback}.
4135 * Requesting a network with this method will count toward this limit. If this limit is
4136 * exceeded, an exception will be thrown. To avoid hitting this issue and to conserve resources,
4137 * make sure to unregister the callbacks with {@link #unregisterNetworkCallback(PendingIntent)}
4138 * or {@link #releaseNetworkRequest(PendingIntent)}.
4139 *
Paul Jensen9ceed862015-06-17 14:15:39 -04004140 * @param request {@link NetworkRequest} describing this request.
4141 * @param operation Action to perform when the network is available (corresponds
4142 * to the {@link NetworkCallback#onAvailable} call. Typically
4143 * comes from {@link PendingIntent#getBroadcast}. Cannot be null.
Chalard Jean7f06b342020-05-20 16:11:50 +09004144 * @throws RuntimeException if the app already has too many callbacks registered.
Paul Jensen9ceed862015-06-17 14:15:39 -04004145 */
Jeff Sharkey2ac62992017-04-24 11:18:03 -06004146 @RequiresPermission(android.Manifest.permission.ACCESS_NETWORK_STATE)
Chalard Jean42a9c292019-01-07 19:26:34 +09004147 public void registerNetworkCallback(@NonNull NetworkRequest request,
4148 @NonNull PendingIntent operation) {
Soi, Yoshinari99efcbd2015-11-12 12:09:02 +09004149 printStackTrace();
Hugo Benichiff4bf602017-05-09 15:19:01 +09004150 checkPendingIntentNotNull(operation);
Paul Jensen9ceed862015-06-17 14:15:39 -04004151 try {
Roshan Pius34dc4be2020-01-16 12:17:17 -08004152 mService.pendingListenForNetwork(
Roshan Piusc97d8062020-12-17 14:53:09 -08004153 request.networkCapabilities, operation, mContext.getOpPackageName(),
4154 getAttributionTag());
Jeff Sharkeya6066fd2016-03-01 19:27:23 -07004155 } catch (RemoteException e) {
4156 throw e.rethrowFromSystemServer();
Hugo Benichi145e3792017-05-11 13:16:17 +09004157 } catch (ServiceSpecificException e) {
4158 throw convertServiceException(e);
Jeff Sharkeya6066fd2016-03-01 19:27:23 -07004159 }
Paul Jensen9ceed862015-06-17 14:15:39 -04004160 }
4161
4162 /**
Lorenzo Colitti80b48bc2021-01-29 20:14:04 +09004163 * Registers to receive notifications about changes in the application's default network. This
4164 * may be a physical network or a virtual network, such as a VPN that applies to the
4165 * application. The callbacks will continue to be called until either the application exits or
Lorenzo Colittiad458ad2016-04-13 22:00:02 +09004166 * {@link #unregisterNetworkCallback(NetworkCallback)} is called.
Erik Klinee0aed632016-03-16 15:31:39 +09004167 *
Chalard Jean7f06b342020-05-20 16:11:50 +09004168 * <p>To avoid performance issues due to apps leaking callbacks, the system will limit the
4169 * number of outstanding requests to 100 per app (identified by their UID), shared with
4170 * all variants of this method, of {@link #requestNetwork} as well as
4171 * {@link ConnectivityDiagnosticsManager#registerConnectivityDiagnosticsCallback}.
4172 * Requesting a network with this method will count toward this limit. If this limit is
4173 * exceeded, an exception will be thrown. To avoid hitting this issue and to conserve resources,
4174 * make sure to unregister the callbacks with
4175 * {@link #unregisterNetworkCallback(NetworkCallback)}.
4176 *
Erik Klinee0aed632016-03-16 15:31:39 +09004177 * @param networkCallback The {@link NetworkCallback} that the system will call as the
Lorenzo Colitti80b48bc2021-01-29 20:14:04 +09004178 * application's default network changes.
Hugo Benichicd5a0e02017-02-02 17:02:36 +09004179 * The callback is invoked on the default internal Handler.
Chalard Jean7f06b342020-05-20 16:11:50 +09004180 * @throws RuntimeException if the app already has too many callbacks registered.
Erik Klinee0aed632016-03-16 15:31:39 +09004181 */
Jeff Sharkey2ac62992017-04-24 11:18:03 -06004182 @RequiresPermission(android.Manifest.permission.ACCESS_NETWORK_STATE)
Chalard Jean42a9c292019-01-07 19:26:34 +09004183 public void registerDefaultNetworkCallback(@NonNull NetworkCallback networkCallback) {
Hugo Benichicd5a0e02017-02-02 17:02:36 +09004184 registerDefaultNetworkCallback(networkCallback, getDefaultHandler());
4185 }
4186
4187 /**
Lorenzo Colitti80b48bc2021-01-29 20:14:04 +09004188 * Registers to receive notifications about changes in the application's default network. This
4189 * may be a physical network or a virtual network, such as a VPN that applies to the
4190 * application. The callbacks will continue to be called until either the application exits or
4191 * {@link #unregisterNetworkCallback(NetworkCallback)} is called.
4192 *
4193 * <p>To avoid performance issues due to apps leaking callbacks, the system will limit the
4194 * number of outstanding requests to 100 per app (identified by their UID), shared with
4195 * all variants of this method, of {@link #requestNetwork} as well as
4196 * {@link ConnectivityDiagnosticsManager#registerConnectivityDiagnosticsCallback}.
4197 * Requesting a network with this method will count toward this limit. If this limit is
4198 * exceeded, an exception will be thrown. To avoid hitting this issue and to conserve resources,
4199 * make sure to unregister the callbacks with
4200 * {@link #unregisterNetworkCallback(NetworkCallback)}.
4201 *
4202 * @param networkCallback The {@link NetworkCallback} that the system will call as the
4203 * application's default network changes.
4204 * @param handler {@link Handler} to specify the thread upon which the callback will be invoked.
4205 * @throws RuntimeException if the app already has too many callbacks registered.
4206 */
4207 @RequiresPermission(android.Manifest.permission.ACCESS_NETWORK_STATE)
4208 public void registerDefaultNetworkCallback(@NonNull NetworkCallback networkCallback,
4209 @NonNull Handler handler) {
4210 CallbackHandler cbHandler = new CallbackHandler(handler);
4211 sendRequestForNetwork(null /* NetworkCapabilities need */, networkCallback, 0,
4212 TRACK_DEFAULT, TYPE_NONE, cbHandler);
4213 }
4214
4215 /**
Hugo Benichicd5a0e02017-02-02 17:02:36 +09004216 * Registers to receive notifications about changes in the system default network. The callbacks
4217 * will continue to be called until either the application exits or
4218 * {@link #unregisterNetworkCallback(NetworkCallback)} is called.
Hugo Benichicd5a0e02017-02-02 17:02:36 +09004219 *
Lorenzo Colitti80b48bc2021-01-29 20:14:04 +09004220 * This method should not be used to determine networking state seen by applications, because in
4221 * many cases, most or even all application traffic may not use the default network directly,
4222 * and traffic from different applications may go on different networks by default. As an
4223 * example, if a VPN is connected, traffic from all applications might be sent through the VPN
4224 * and not onto the system default network. Applications or system components desiring to do
4225 * determine network state as seen by applications should use other methods such as
4226 * {@link #registerDefaultNetworkCallback(NetworkCallback, Handler)}.
4227 *
Chalard Jean7f06b342020-05-20 16:11:50 +09004228 * <p>To avoid performance issues due to apps leaking callbacks, the system will limit the
4229 * number of outstanding requests to 100 per app (identified by their UID), shared with
4230 * all variants of this method, of {@link #requestNetwork} as well as
4231 * {@link ConnectivityDiagnosticsManager#registerConnectivityDiagnosticsCallback}.
4232 * Requesting a network with this method will count toward this limit. If this limit is
4233 * exceeded, an exception will be thrown. To avoid hitting this issue and to conserve resources,
4234 * make sure to unregister the callbacks with
4235 * {@link #unregisterNetworkCallback(NetworkCallback)}.
4236 *
Hugo Benichicd5a0e02017-02-02 17:02:36 +09004237 * @param networkCallback The {@link NetworkCallback} that the system will call as the
4238 * system default network changes.
4239 * @param handler {@link Handler} to specify the thread upon which the callback will be invoked.
Chalard Jean7f06b342020-05-20 16:11:50 +09004240 * @throws RuntimeException if the app already has too many callbacks registered.
Lorenzo Colitti80b48bc2021-01-29 20:14:04 +09004241 *
4242 * @hide
Hugo Benichicd5a0e02017-02-02 17:02:36 +09004243 */
Lorenzo Colitti80b48bc2021-01-29 20:14:04 +09004244 @SystemApi(client = MODULE_LIBRARIES)
4245 @SuppressLint({"ExecutorRegistration", "PairedRegistration"})
4246 @RequiresPermission(anyOf = {
4247 NetworkStack.PERMISSION_MAINLINE_NETWORK_STACK,
4248 android.Manifest.permission.NETWORK_SETTINGS})
4249 public void registerSystemDefaultNetworkCallback(@NonNull NetworkCallback networkCallback,
Chalard Jean42a9c292019-01-07 19:26:34 +09004250 @NonNull Handler handler) {
Hugo Benichicd5a0e02017-02-02 17:02:36 +09004251 CallbackHandler cbHandler = new CallbackHandler(handler);
Chalard Jean73d9db72018-06-04 16:52:49 +09004252 sendRequestForNetwork(null /* NetworkCapabilities need */, networkCallback, 0,
Lorenzo Colitti80b48bc2021-01-29 20:14:04 +09004253 TRACK_SYSTEM_DEFAULT, TYPE_NONE, cbHandler);
Erik Klinee0aed632016-03-16 15:31:39 +09004254 }
4255
4256 /**
junyulai6b0a6a22021-03-05 15:51:17 +08004257 * @hide
4258 */
4259 // TODO: Make it public api.
4260 @SuppressLint("ExecutorRegistration")
4261 public void registerBestMatchingNetworkCallback(@NonNull NetworkRequest request,
4262 @NonNull NetworkCallback networkCallback, @NonNull Handler handler) {
4263 final NetworkCapabilities nc = request.networkCapabilities;
4264 final CallbackHandler cbHandler = new CallbackHandler(handler);
4265 sendRequestForNetwork(nc, networkCallback, 0, TRACK_BEST, TYPE_NONE, cbHandler);
4266 }
4267
4268 /**
fenglu73169332015-04-21 17:12:05 -07004269 * Requests bandwidth update for a given {@link Network} and returns whether the update request
4270 * is accepted by ConnectivityService. Once accepted, ConnectivityService will poll underlying
4271 * network connection for updated bandwidth information. The caller will be notified via
4272 * {@link ConnectivityManager.NetworkCallback} if there is an update. Notice that this
Lorenzo Colittiad458ad2016-04-13 22:00:02 +09004273 * method assumes that the caller has previously called
4274 * {@link #registerNetworkCallback(NetworkRequest, NetworkCallback)} to listen for network
4275 * changes.
fenglu76564332015-03-20 11:29:56 -07004276 *
fenglu41808e82015-04-27 14:28:04 -07004277 * @param network {@link Network} specifying which network you're interested.
fenglu73169332015-04-21 17:12:05 -07004278 * @return {@code true} on success, {@code false} if the {@link Network} is no longer valid.
fenglu76564332015-03-20 11:29:56 -07004279 */
Chalard Jean42a9c292019-01-07 19:26:34 +09004280 public boolean requestBandwidthUpdate(@NonNull Network network) {
fenglu76564332015-03-20 11:29:56 -07004281 try {
fenglu73169332015-04-21 17:12:05 -07004282 return mService.requestBandwidthUpdate(network);
fenglu76564332015-03-20 11:29:56 -07004283 } catch (RemoteException e) {
Jeff Sharkeya6066fd2016-03-01 19:27:23 -07004284 throw e.rethrowFromSystemServer();
fenglu76564332015-03-20 11:29:56 -07004285 }
4286 }
4287
4288 /**
Hugo Benichi45a49542017-03-06 09:17:06 +09004289 * Unregisters a {@code NetworkCallback} and possibly releases networks originating from
Lorenzo Colittiad458ad2016-04-13 22:00:02 +09004290 * {@link #requestNetwork(NetworkRequest, NetworkCallback)} and
4291 * {@link #registerNetworkCallback(NetworkRequest, NetworkCallback)} calls.
4292 * If the given {@code NetworkCallback} had previously been used with
Lorenzo Colittiefd396e2015-04-24 17:03:31 +09004293 * {@code #requestNetwork}, any networks that had been connected to only to satisfy that request
4294 * will be disconnected.
Robert Greenwalt2bebbfb2014-03-26 16:47:06 -07004295 *
Hugo Benichi45a49542017-03-06 09:17:06 +09004296 * Notifications that would have triggered that {@code NetworkCallback} will immediately stop
4297 * triggering it as soon as this call returns.
4298 *
Robert Greenwalt09e5cff2014-06-11 16:05:07 -07004299 * @param networkCallback The {@link NetworkCallback} used when making the request.
Robert Greenwalt2bebbfb2014-03-26 16:47:06 -07004300 */
Chalard Jean42a9c292019-01-07 19:26:34 +09004301 public void unregisterNetworkCallback(@NonNull NetworkCallback networkCallback) {
Soi, Yoshinari99efcbd2015-11-12 12:09:02 +09004302 printStackTrace();
Hugo Benichiff4bf602017-05-09 15:19:01 +09004303 checkCallbackNotNull(networkCallback);
Hugo Benichi45a49542017-03-06 09:17:06 +09004304 final List<NetworkRequest> reqs = new ArrayList<>();
4305 // Find all requests associated to this callback and stop callback triggers immediately.
4306 // Callback is reusable immediately. http://b/20701525, http://b/35921499.
4307 synchronized (sCallbacks) {
Remi NGUYEN VAN1c17c882021-03-11 20:49:13 +09004308 if (networkCallback.networkRequest == null) {
4309 throw new IllegalArgumentException("NetworkCallback was not registered");
4310 }
Etan Cohene0e2ca92019-04-16 15:07:55 -07004311 if (networkCallback.networkRequest == ALREADY_UNREGISTERED) {
4312 Log.d(TAG, "NetworkCallback was already unregistered");
4313 return;
4314 }
Hugo Benichi45a49542017-03-06 09:17:06 +09004315 for (Map.Entry<NetworkRequest, NetworkCallback> e : sCallbacks.entrySet()) {
4316 if (e.getValue() == networkCallback) {
4317 reqs.add(e.getKey());
4318 }
4319 }
4320 // TODO: throw exception if callback was registered more than once (http://b/20701525).
4321 for (NetworkRequest r : reqs) {
4322 try {
4323 mService.releaseNetworkRequest(r);
4324 } catch (RemoteException e) {
4325 throw e.rethrowFromSystemServer();
4326 }
4327 // Only remove mapping if rpc was successful.
4328 sCallbacks.remove(r);
4329 }
Hugo Benichi1c027fe2017-06-17 13:14:12 +09004330 networkCallback.networkRequest = ALREADY_UNREGISTERED;
Jeff Sharkeya6066fd2016-03-01 19:27:23 -07004331 }
Robert Greenwalt2bebbfb2014-03-26 16:47:06 -07004332 }
Paul Jensene98c6e02014-05-29 10:12:39 -04004333
4334 /**
Paul Jensen78f6b802015-06-30 14:29:18 -04004335 * Unregisters a callback previously registered via
4336 * {@link #registerNetworkCallback(NetworkRequest, android.app.PendingIntent)}.
4337 *
4338 * @param operation A PendingIntent equal (as defined by {@link Intent#filterEquals}) to the
4339 * PendingIntent passed to
4340 * {@link #registerNetworkCallback(NetworkRequest, android.app.PendingIntent)}.
4341 * Cannot be null.
4342 */
Chalard Jean42a9c292019-01-07 19:26:34 +09004343 public void unregisterNetworkCallback(@NonNull PendingIntent operation) {
Paul Jensen78f6b802015-06-30 14:29:18 -04004344 releaseNetworkRequest(operation);
4345 }
4346
4347 /**
Lorenzo Colitti36fbebe2015-04-03 16:38:52 +09004348 * Informs the system whether it should switch to {@code network} regardless of whether it is
4349 * validated or not. If {@code accept} is true, and the network was explicitly selected by the
4350 * user (e.g., by selecting a Wi-Fi network in the Settings app), then the network will become
4351 * the system default network regardless of any other network that's currently connected. If
4352 * {@code always} is true, then the choice is remembered, so that the next time the user
4353 * connects to this network, the system will switch to it.
4354 *
Lorenzo Colitti36fbebe2015-04-03 16:38:52 +09004355 * @param network The network to accept.
4356 * @param accept Whether to accept the network even if unvalidated.
4357 * @param always Whether to remember this choice in the future.
4358 *
4359 * @hide
4360 */
lucaslin25a50472019-03-12 13:08:03 +08004361 @RequiresPermission(android.Manifest.permission.NETWORK_SETTINGS)
Lorenzo Colitti36fbebe2015-04-03 16:38:52 +09004362 public void setAcceptUnvalidated(Network network, boolean accept, boolean always) {
4363 try {
4364 mService.setAcceptUnvalidated(network, accept, always);
Jeff Sharkeya6066fd2016-03-01 19:27:23 -07004365 } catch (RemoteException e) {
4366 throw e.rethrowFromSystemServer();
4367 }
Lorenzo Colitti36fbebe2015-04-03 16:38:52 +09004368 }
4369
4370 /**
lucaslin25a50472019-03-12 13:08:03 +08004371 * Informs the system whether it should consider the network as validated even if it only has
4372 * partial connectivity. If {@code accept} is true, then the network will be considered as
4373 * validated even if connectivity is only partial. If {@code always} is true, then the choice
4374 * is remembered, so that the next time the user connects to this network, the system will
4375 * switch to it.
4376 *
4377 * @param network The network to accept.
4378 * @param accept Whether to consider the network as validated even if it has partial
4379 * connectivity.
4380 * @param always Whether to remember this choice in the future.
4381 *
4382 * @hide
4383 */
4384 @RequiresPermission(android.Manifest.permission.NETWORK_STACK)
4385 public void setAcceptPartialConnectivity(Network network, boolean accept, boolean always) {
4386 try {
4387 mService.setAcceptPartialConnectivity(network, accept, always);
4388 } catch (RemoteException e) {
4389 throw e.rethrowFromSystemServer();
4390 }
4391 }
4392
4393 /**
Lorenzo Colittie98557f2016-09-19 01:00:19 +09004394 * Informs the system to penalize {@code network}'s score when it becomes unvalidated. This is
4395 * only meaningful if the system is configured not to penalize such networks, e.g., if the
4396 * {@code config_networkAvoidBadWifi} configuration variable is set to 0 and the {@code
4397 * NETWORK_AVOID_BAD_WIFI setting is unset}.
4398 *
Lorenzo Colittie98557f2016-09-19 01:00:19 +09004399 * @param network The network to accept.
4400 *
4401 * @hide
4402 */
lucaslin25a50472019-03-12 13:08:03 +08004403 @RequiresPermission(android.Manifest.permission.NETWORK_SETTINGS)
Lorenzo Colittie98557f2016-09-19 01:00:19 +09004404 public void setAvoidUnvalidated(Network network) {
4405 try {
4406 mService.setAvoidUnvalidated(network);
4407 } catch (RemoteException e) {
4408 throw e.rethrowFromSystemServer();
4409 }
4410 }
4411
4412 /**
Lorenzo Colitti755e7962017-04-27 14:30:21 +09004413 * Requests that the system open the captive portal app on the specified network.
4414 *
4415 * @param network The network to log into.
4416 *
4417 * @hide
4418 */
paulhud70b7dd2019-08-12 16:25:11 +08004419 @RequiresPermission(android.Manifest.permission.NETWORK_SETTINGS)
Lorenzo Colitti755e7962017-04-27 14:30:21 +09004420 public void startCaptivePortalApp(Network network) {
4421 try {
4422 mService.startCaptivePortalApp(network);
4423 } catch (RemoteException e) {
4424 throw e.rethrowFromSystemServer();
4425 }
4426 }
4427
4428 /**
Remi NGUYEN VANf363f5f2019-02-04 11:32:20 +09004429 * Requests that the system open the captive portal app with the specified extras.
4430 *
4431 * <p>This endpoint is exclusively for use by the NetworkStack and is protected by the
4432 * corresponding permission.
Remi NGUYEN VAN89fbdc32019-02-13 20:58:59 +09004433 * @param network Network on which the captive portal was detected.
Remi NGUYEN VANf363f5f2019-02-04 11:32:20 +09004434 * @param appExtras Extras to include in the app start intent.
4435 * @hide
4436 */
4437 @SystemApi
Remi NGUYEN VANf363f5f2019-02-04 11:32:20 +09004438 @RequiresPermission(NetworkStack.PERMISSION_MAINLINE_NETWORK_STACK)
paulhu9bb04802019-03-08 16:35:20 +08004439 public void startCaptivePortalApp(@NonNull Network network, @NonNull Bundle appExtras) {
Remi NGUYEN VANf363f5f2019-02-04 11:32:20 +09004440 try {
Remi NGUYEN VAN89fbdc32019-02-13 20:58:59 +09004441 mService.startCaptivePortalAppInternal(network, appExtras);
Remi NGUYEN VANf363f5f2019-02-04 11:32:20 +09004442 } catch (RemoteException e) {
4443 throw e.rethrowFromSystemServer();
4444 }
4445 }
4446
4447 /**
Remi NGUYEN VAN3436b6f2019-01-20 20:35:06 +09004448 * Determine whether the device is configured to avoid bad wifi.
4449 * @hide
4450 */
4451 @SystemApi
Remi NGUYEN VAN338eb2a2019-03-22 11:14:13 +09004452 @RequiresPermission(anyOf = {
4453 NetworkStack.PERMISSION_MAINLINE_NETWORK_STACK,
4454 android.Manifest.permission.NETWORK_STACK})
4455 public boolean shouldAvoidBadWifi() {
Remi NGUYEN VAN3436b6f2019-01-20 20:35:06 +09004456 try {
Remi NGUYEN VAN338eb2a2019-03-22 11:14:13 +09004457 return mService.shouldAvoidBadWifi();
Remi NGUYEN VAN3436b6f2019-01-20 20:35:06 +09004458 } catch (RemoteException e) {
4459 throw e.rethrowFromSystemServer();
4460 }
4461 }
4462
4463 /**
Lorenzo Colitti231efb02017-01-24 18:08:41 +09004464 * It is acceptable to briefly use multipath data to provide seamless connectivity for
4465 * time-sensitive user-facing operations when the system default network is temporarily
Lorenzo Colitti38ea8102017-04-28 00:56:30 +09004466 * unresponsive. The amount of data should be limited (less than one megabyte for every call to
4467 * this method), and the operation should be infrequent to ensure that data usage is limited.
Lorenzo Colitti231efb02017-01-24 18:08:41 +09004468 *
4469 * An example of such an operation might be a time-sensitive foreground activity, such as a
4470 * voice command, that the user is performing while walking out of range of a Wi-Fi network.
4471 */
4472 public static final int MULTIPATH_PREFERENCE_HANDOVER = 1 << 0;
4473
4474 /**
4475 * It is acceptable to use small amounts of multipath data on an ongoing basis to provide
4476 * a backup channel for traffic that is primarily going over another network.
4477 *
4478 * An example might be maintaining backup connections to peers or servers for the purpose of
4479 * fast fallback if the default network is temporarily unresponsive or disconnects. The traffic
4480 * on backup paths should be negligible compared to the traffic on the main path.
4481 */
4482 public static final int MULTIPATH_PREFERENCE_RELIABILITY = 1 << 1;
4483
4484 /**
4485 * It is acceptable to use metered data to improve network latency and performance.
4486 */
4487 public static final int MULTIPATH_PREFERENCE_PERFORMANCE = 1 << 2;
4488
4489 /**
4490 * Return value to use for unmetered networks. On such networks we currently set all the flags
4491 * to true.
4492 * @hide
4493 */
4494 public static final int MULTIPATH_PREFERENCE_UNMETERED =
4495 MULTIPATH_PREFERENCE_HANDOVER |
4496 MULTIPATH_PREFERENCE_RELIABILITY |
4497 MULTIPATH_PREFERENCE_PERFORMANCE;
4498
4499 /** @hide */
4500 @Retention(RetentionPolicy.SOURCE)
4501 @IntDef(flag = true, value = {
4502 MULTIPATH_PREFERENCE_HANDOVER,
4503 MULTIPATH_PREFERENCE_RELIABILITY,
4504 MULTIPATH_PREFERENCE_PERFORMANCE,
4505 })
4506 public @interface MultipathPreference {
4507 }
4508
4509 /**
4510 * Provides a hint to the calling application on whether it is desirable to use the
4511 * multinetwork APIs (e.g., {@link Network#openConnection}, {@link Network#bindSocket}, etc.)
4512 * for multipath data transfer on this network when it is not the system default network.
4513 * Applications desiring to use multipath network protocols should call this method before
4514 * each such operation.
Lorenzo Colitti231efb02017-01-24 18:08:41 +09004515 *
4516 * @param network The network on which the application desires to use multipath data.
4517 * If {@code null}, this method will return the a preference that will generally
4518 * apply to metered networks.
4519 * @return a bitwise OR of zero or more of the {@code MULTIPATH_PREFERENCE_*} constants.
4520 */
Jeff Sharkey2ac62992017-04-24 11:18:03 -06004521 @RequiresPermission(android.Manifest.permission.ACCESS_NETWORK_STATE)
Chalard Jean42a9c292019-01-07 19:26:34 +09004522 public @MultipathPreference int getMultipathPreference(@Nullable Network network) {
Lorenzo Colitti231efb02017-01-24 18:08:41 +09004523 try {
4524 return mService.getMultipathPreference(network);
4525 } catch (RemoteException e) {
4526 throw e.rethrowFromSystemServer();
4527 }
4528 }
4529
4530 /**
Stuart Scottd3bb5082015-03-30 13:17:11 -07004531 * Resets all connectivity manager settings back to factory defaults.
4532 * @hide
4533 */
paulhud70b7dd2019-08-12 16:25:11 +08004534 @RequiresPermission(android.Manifest.permission.NETWORK_SETTINGS)
Stuart Scottd3bb5082015-03-30 13:17:11 -07004535 public void factoryReset() {
Stuart Scottd3bb5082015-03-30 13:17:11 -07004536 try {
Stuart Scottca888bf2015-04-02 18:00:02 -07004537 mService.factoryReset();
Amos Bianchiddadca42020-03-04 11:07:38 -08004538 mTetheringManager.stopAllTethering();
Stuart Scottd3bb5082015-03-30 13:17:11 -07004539 } catch (RemoteException e) {
Jeff Sharkeya6066fd2016-03-01 19:27:23 -07004540 throw e.rethrowFromSystemServer();
Stuart Scottd3bb5082015-03-30 13:17:11 -07004541 }
4542 }
4543
4544 /**
Paul Jensene98c6e02014-05-29 10:12:39 -04004545 * Binds the current process to {@code network}. All Sockets created in the future
4546 * (and not explicitly bound via a bound SocketFactory from
4547 * {@link Network#getSocketFactory() Network.getSocketFactory()}) will be bound to
4548 * {@code network}. All host name resolutions will be limited to {@code network} as well.
4549 * Note that if {@code network} ever disconnects, all Sockets created in this way will cease to
4550 * work and all host name resolutions will fail. This is by design so an application doesn't
4551 * accidentally use Sockets it thinks are still bound to a particular {@link Network}.
4552 * To clear binding pass {@code null} for {@code network}. Using individually bound
4553 * Sockets created by Network.getSocketFactory().createSocket() and
4554 * performing network-specific host name resolutions via
4555 * {@link Network#getAllByName Network.getAllByName} is preferred to calling
Paul Jensen895c3942015-03-10 10:54:12 -04004556 * {@code bindProcessToNetwork}.
Paul Jensene98c6e02014-05-29 10:12:39 -04004557 *
4558 * @param network The {@link Network} to bind the current process to, or {@code null} to clear
4559 * the current binding.
4560 * @return {@code true} on success, {@code false} if the {@link Network} is no longer valid.
4561 */
Chalard Jean42a9c292019-01-07 19:26:34 +09004562 public boolean bindProcessToNetwork(@Nullable Network network) {
Chalard Jean73d9db72018-06-04 16:52:49 +09004563 // Forcing callers to call through non-static function ensures ConnectivityManager
Paul Jensen895c3942015-03-10 10:54:12 -04004564 // instantiated.
4565 return setProcessDefaultNetwork(network);
4566 }
4567
4568 /**
4569 * Binds the current process to {@code network}. All Sockets created in the future
4570 * (and not explicitly bound via a bound SocketFactory from
4571 * {@link Network#getSocketFactory() Network.getSocketFactory()}) will be bound to
4572 * {@code network}. All host name resolutions will be limited to {@code network} as well.
4573 * Note that if {@code network} ever disconnects, all Sockets created in this way will cease to
4574 * work and all host name resolutions will fail. This is by design so an application doesn't
4575 * accidentally use Sockets it thinks are still bound to a particular {@link Network}.
4576 * To clear binding pass {@code null} for {@code network}. Using individually bound
4577 * Sockets created by Network.getSocketFactory().createSocket() and
4578 * performing network-specific host name resolutions via
4579 * {@link Network#getAllByName Network.getAllByName} is preferred to calling
4580 * {@code setProcessDefaultNetwork}.
4581 *
4582 * @param network The {@link Network} to bind the current process to, or {@code null} to clear
4583 * the current binding.
4584 * @return {@code true} on success, {@code false} if the {@link Network} is no longer valid.
4585 * @deprecated This function can throw {@link IllegalStateException}. Use
4586 * {@link #bindProcessToNetwork} instead. {@code bindProcessToNetwork}
4587 * is a direct replacement.
4588 */
Aurimas Liutikasa6bb0232016-05-24 15:22:55 -07004589 @Deprecated
Chalard Jean42a9c292019-01-07 19:26:34 +09004590 public static boolean setProcessDefaultNetwork(@Nullable Network network) {
Paul Jensen99c36662014-08-27 12:38:45 -04004591 int netId = (network == null) ? NETID_UNSET : network.netId;
Lorenzo Colitti3fadff92019-01-31 13:08:24 +09004592 boolean isSameNetId = (netId == NetworkUtils.getBoundNetworkForProcess());
4593
Lorenzo Colitti580483e2019-01-30 23:04:54 +09004594 if (netId != NETID_UNSET) {
4595 netId = network.getNetIdForResolv();
Paul Jensen99c36662014-08-27 12:38:45 -04004596 }
Lorenzo Colitti3fadff92019-01-31 13:08:24 +09004597
4598 if (!NetworkUtils.bindProcessToNetwork(netId)) {
4599 return false;
4600 }
4601
4602 if (!isSameNetId) {
Paul Jensen12131352014-12-10 15:12:18 -05004603 // Set HTTP proxy system properties to match network.
4604 // TODO: Deprecate this static method and replace it with a non-static version.
Lorenzo Colitti40898252015-04-22 11:52:48 +09004605 try {
Remi NGUYEN VAN9ff113b2021-02-03 10:18:20 +09004606 Proxy.setHttpProxyConfiguration(getInstance().getDefaultProxy());
Lorenzo Colitti40898252015-04-22 11:52:48 +09004607 } catch (SecurityException e) {
4608 // The process doesn't have ACCESS_NETWORK_STATE, so we can't fetch the proxy.
4609 Log.e(TAG, "Can't set proxy properties", e);
4610 }
Paul Jensen99c36662014-08-27 12:38:45 -04004611 // Must flush DNS cache as new network may have different DNS resolutions.
4612 InetAddress.clearDnsCache();
4613 // Must flush socket pool as idle sockets will be bound to previous network and may
4614 // cause subsequent fetches to be performed on old network.
4615 NetworkEventDispatcher.getInstance().onNetworkConfigurationChanged();
Paul Jensen99c36662014-08-27 12:38:45 -04004616 }
Lorenzo Colitti3fadff92019-01-31 13:08:24 +09004617
4618 return true;
Paul Jensene98c6e02014-05-29 10:12:39 -04004619 }
4620
4621 /**
4622 * Returns the {@link Network} currently bound to this process via
Paul Jensen895c3942015-03-10 10:54:12 -04004623 * {@link #bindProcessToNetwork}, or {@code null} if no {@link Network} is explicitly bound.
Paul Jensene98c6e02014-05-29 10:12:39 -04004624 *
4625 * @return {@code Network} to which this process is bound, or {@code null}.
4626 */
Chalard Jean42a9c292019-01-07 19:26:34 +09004627 @Nullable
Paul Jensen895c3942015-03-10 10:54:12 -04004628 public Network getBoundNetworkForProcess() {
4629 // Forcing callers to call thru non-static function ensures ConnectivityManager
4630 // instantiated.
4631 return getProcessDefaultNetwork();
4632 }
4633
4634 /**
4635 * Returns the {@link Network} currently bound to this process via
4636 * {@link #bindProcessToNetwork}, or {@code null} if no {@link Network} is explicitly bound.
4637 *
4638 * @return {@code Network} to which this process is bound, or {@code null}.
4639 * @deprecated Using this function can lead to other functions throwing
4640 * {@link IllegalStateException}. Use {@link #getBoundNetworkForProcess} instead.
4641 * {@code getBoundNetworkForProcess} is a direct replacement.
4642 */
Aurimas Liutikasa6bb0232016-05-24 15:22:55 -07004643 @Deprecated
Chalard Jean42a9c292019-01-07 19:26:34 +09004644 @Nullable
Paul Jensene98c6e02014-05-29 10:12:39 -04004645 public static Network getProcessDefaultNetwork() {
Paul Jensen895c3942015-03-10 10:54:12 -04004646 int netId = NetworkUtils.getBoundNetworkForProcess();
Paul Jensen8b6260f2014-07-11 08:17:29 -04004647 if (netId == NETID_UNSET) return null;
Paul Jensene98c6e02014-05-29 10:12:39 -04004648 return new Network(netId);
4649 }
4650
Lorenzo Colitti9b026fa2015-07-29 11:41:21 +09004651 private void unsupportedStartingFrom(int version) {
4652 if (Process.myUid() == Process.SYSTEM_UID) {
Lorenzo Colitti62f752c2018-09-28 11:31:55 +09004653 // The getApplicationInfo() call we make below is not supported in system context. Let
4654 // the call through here, and rely on the fact that ConnectivityService will refuse to
4655 // allow the system to use these APIs anyway.
Lorenzo Colitti9b026fa2015-07-29 11:41:21 +09004656 return;
4657 }
4658
4659 if (mContext.getApplicationInfo().targetSdkVersion >= version) {
4660 throw new UnsupportedOperationException(
4661 "This method is not supported in target SDK version " + version + " and above");
4662 }
4663 }
4664
4665 // Checks whether the calling app can use the legacy routing API (startUsingNetworkFeature,
4666 // stopUsingNetworkFeature, requestRouteToHost), and if not throw UnsupportedOperationException.
Lifu Tangae4adaa2016-01-07 23:20:38 -08004667 // TODO: convert the existing system users (Tethering, GnssLocationProvider) to the new APIs and
Lorenzo Colitti9b026fa2015-07-29 11:41:21 +09004668 // remove these exemptions. Note that this check is not secure, and apps can still access these
4669 // functions by accessing ConnectivityService directly. However, it should be clear that doing
4670 // so is unsupported and may break in the future. http://b/22728205
4671 private void checkLegacyRoutingApiAccess() {
Dianne Hackbornc16abda2015-07-31 10:35:34 -07004672 unsupportedStartingFrom(VERSION_CODES.M);
Lorenzo Colitti9b026fa2015-07-29 11:41:21 +09004673 }
4674
Paul Jensene98c6e02014-05-29 10:12:39 -04004675 /**
4676 * Binds host resolutions performed by this process to {@code network}.
Paul Jensen895c3942015-03-10 10:54:12 -04004677 * {@link #bindProcessToNetwork} takes precedence over this setting.
Paul Jensene98c6e02014-05-29 10:12:39 -04004678 *
4679 * @param network The {@link Network} to bind host resolutions from the current process to, or
4680 * {@code null} to clear the current binding.
4681 * @return {@code true} on success, {@code false} if the {@link Network} is no longer valid.
4682 * @hide
4683 * @deprecated This is strictly for legacy usage to support {@link #startUsingNetworkFeature}.
4684 */
Aurimas Liutikasa6bb0232016-05-24 15:22:55 -07004685 @Deprecated
Mathew Inwood5a09a712020-11-04 09:29:36 +00004686 @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.R, trackingBug = 170729553)
Paul Jensene98c6e02014-05-29 10:12:39 -04004687 public static boolean setProcessDefaultNetworkForHostResolution(Network network) {
Paul Jensen8b6260f2014-07-11 08:17:29 -04004688 return NetworkUtils.bindProcessToNetworkForHostResolution(
Erik Klinef2420792018-04-27 22:48:33 +09004689 (network == null) ? NETID_UNSET : network.getNetIdForResolv());
Paul Jensene98c6e02014-05-29 10:12:39 -04004690 }
Felipe Lemed16384b2016-01-22 09:44:57 -08004691
4692 /**
4693 * Device is not restricting metered network activity while application is running on
4694 * background.
4695 */
4696 public static final int RESTRICT_BACKGROUND_STATUS_DISABLED = 1;
4697
4698 /**
4699 * Device is restricting metered network activity while application is running on background,
4700 * but application is allowed to bypass it.
4701 * <p>
4702 * In this state, application should take action to mitigate metered network access.
4703 * For example, a music streaming application should switch to a low-bandwidth bitrate.
4704 */
4705 public static final int RESTRICT_BACKGROUND_STATUS_WHITELISTED = 2;
4706
4707 /**
4708 * Device is restricting metered network activity while application is running on background.
Felipe Leme6a5b7692016-01-27 14:46:39 -08004709 * <p>
Felipe Lemed16384b2016-01-22 09:44:57 -08004710 * In this state, application should not try to use the network while running on background,
4711 * because it would be denied.
4712 */
4713 public static final int RESTRICT_BACKGROUND_STATUS_ENABLED = 3;
4714
Felipe Leme6a5b7692016-01-27 14:46:39 -08004715 /**
4716 * A change in the background metered network activity restriction has occurred.
4717 * <p>
4718 * Applications should call {@link #getRestrictBackgroundStatus()} to check if the restriction
4719 * applies to them.
4720 * <p>
4721 * This is only sent to registered receivers, not manifest receivers.
4722 */
4723 @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION)
4724 public static final String ACTION_RESTRICT_BACKGROUND_CHANGED =
4725 "android.net.conn.RESTRICT_BACKGROUND_CHANGED";
4726
Felipe Leme1b42ef92016-01-25 11:48:04 -08004727 /** @hide */
4728 @Retention(RetentionPolicy.SOURCE)
Felipe Lemed16384b2016-01-22 09:44:57 -08004729 @IntDef(flag = false, value = {
4730 RESTRICT_BACKGROUND_STATUS_DISABLED,
4731 RESTRICT_BACKGROUND_STATUS_WHITELISTED,
4732 RESTRICT_BACKGROUND_STATUS_ENABLED,
4733 })
Felipe Lemed16384b2016-01-22 09:44:57 -08004734 public @interface RestrictBackgroundStatus {
4735 }
4736
4737 private INetworkPolicyManager getNetworkPolicyManager() {
4738 synchronized (this) {
4739 if (mNPManager != null) {
4740 return mNPManager;
4741 }
4742 mNPManager = INetworkPolicyManager.Stub.asInterface(ServiceManager
4743 .getService(Context.NETWORK_POLICY_SERVICE));
4744 return mNPManager;
4745 }
4746 }
4747
4748 /**
4749 * Determines if the calling application is subject to metered network restrictions while
4750 * running on background.
Felipe Leme6bd46fc2016-05-16 13:57:19 -07004751 *
4752 * @return {@link #RESTRICT_BACKGROUND_STATUS_DISABLED},
4753 * {@link #RESTRICT_BACKGROUND_STATUS_ENABLED},
4754 * or {@link #RESTRICT_BACKGROUND_STATUS_WHITELISTED}
Felipe Lemed16384b2016-01-22 09:44:57 -08004755 */
4756 public @RestrictBackgroundStatus int getRestrictBackgroundStatus() {
4757 try {
4758 return getNetworkPolicyManager().getRestrictBackgroundByCaller();
4759 } catch (RemoteException e) {
Jeff Sharkeya6066fd2016-03-01 19:27:23 -07004760 throw e.rethrowFromSystemServer();
Felipe Lemed16384b2016-01-22 09:44:57 -08004761 }
4762 }
Ricky Waid53cf002018-01-23 04:09:45 +00004763
4764 /**
4765 * The network watchlist is a list of domains and IP addresses that are associated with
Ricky Wai573dcab2018-03-20 14:20:54 +00004766 * potentially harmful apps. This method returns the SHA-256 of the watchlist config file
4767 * currently used by the system for validation purposes.
Ricky Waid53cf002018-01-23 04:09:45 +00004768 *
4769 * @return Hash of network watchlist config file. Null if config does not exist.
4770 */
Chalard Jean42a9c292019-01-07 19:26:34 +09004771 @Nullable
Ricky Waid53cf002018-01-23 04:09:45 +00004772 public byte[] getNetworkWatchlistConfigHash() {
4773 try {
4774 return mService.getNetworkWatchlistConfigHash();
4775 } catch (RemoteException e) {
4776 Log.e(TAG, "Unable to get watchlist config hash");
4777 throw e.rethrowFromSystemServer();
4778 }
4779 }
Jeff Vander Stoepe7205d62018-07-23 10:57:53 -07004780
4781 /**
4782 * Returns the {@code uid} of the owner of a network connection.
4783 *
Benedict Wong8974a1c2020-01-20 22:14:59 -08004784 * @param protocol The protocol of the connection. Only {@code IPPROTO_TCP} and {@code
4785 * IPPROTO_UDP} currently supported.
Jeff Vander Stoepe7205d62018-07-23 10:57:53 -07004786 * @param local The local {@link InetSocketAddress} of a connection.
4787 * @param remote The remote {@link InetSocketAddress} of a connection.
Jeff Vander Stoepe7205d62018-07-23 10:57:53 -07004788 * @return {@code uid} if the connection is found and the app has permission to observe it
Benedict Wong8974a1c2020-01-20 22:14:59 -08004789 * (e.g., if it is associated with the calling VPN app's VpnService tunnel) or {@link
4790 * android.os.Process#INVALID_UID} if the connection is not found.
4791 * @throws {@link SecurityException} if the caller is not the active VpnService for the current
4792 * user.
4793 * @throws {@link IllegalArgumentException} if an unsupported protocol is requested.
Jeff Vander Stoepe7205d62018-07-23 10:57:53 -07004794 */
Benedict Wong8974a1c2020-01-20 22:14:59 -08004795 public int getConnectionOwnerUid(
4796 int protocol, @NonNull InetSocketAddress local, @NonNull InetSocketAddress remote) {
Jeff Vander Stoepe7205d62018-07-23 10:57:53 -07004797 ConnectionInfo connectionInfo = new ConnectionInfo(protocol, local, remote);
4798 try {
4799 return mService.getConnectionOwnerUid(connectionInfo);
4800 } catch (RemoteException e) {
4801 throw e.rethrowFromSystemServer();
4802 }
4803 }
Soi, Yoshinari99efcbd2015-11-12 12:09:02 +09004804
4805 private void printStackTrace() {
4806 if (DEBUG) {
4807 final StackTraceElement[] callStack = Thread.currentThread().getStackTrace();
4808 final StringBuffer sb = new StringBuffer();
4809 for (int i = 3; i < callStack.length; i++) {
4810 final String stackTrace = callStack[i].toString();
4811 if (stackTrace == null || stackTrace.contains("android.os")) {
4812 break;
4813 }
4814 sb.append(" [").append(stackTrace).append("]");
4815 }
4816 Log.d(TAG, "StackLog:" + sb.toString());
4817 }
4818 }
Cody Kestingb5c7abd2020-04-15 12:33:28 -07004819
Remi NGUYEN VANfe97b482021-01-15 23:02:47 +09004820 /** @hide */
4821 public TestNetworkManager startOrGetTestNetworkManager() {
4822 final IBinder tnBinder;
4823 try {
4824 tnBinder = mService.startOrGetTestNetworkService();
4825 } catch (RemoteException e) {
4826 throw e.rethrowFromSystemServer();
4827 }
4828
4829 return new TestNetworkManager(ITestNetworkManager.Stub.asInterface(tnBinder));
4830 }
4831
Remi NGUYEN VANfe97b482021-01-15 23:02:47 +09004832 /** @hide */
4833 public ConnectivityDiagnosticsManager createDiagnosticsManager() {
4834 return new ConnectivityDiagnosticsManager(mContext, mService);
4835 }
4836
Cody Kestingb5c7abd2020-04-15 12:33:28 -07004837 /**
4838 * Simulates a Data Stall for the specified Network.
4839 *
Remi NGUYEN VAN7d4530b2021-01-12 18:40:04 +09004840 * <p>This method should only be used for tests.
4841 *
Cody Kestingb5c7abd2020-04-15 12:33:28 -07004842 * <p>The caller must be the owner of the specified Network.
4843 *
4844 * @param detectionMethod The detection method used to identify the Data Stall.
4845 * @param timestampMillis The timestamp at which the stall 'occurred', in milliseconds.
4846 * @param network The Network for which a Data Stall is being simluated.
4847 * @param extras The PersistableBundle of extras included in the Data Stall notification.
4848 * @throws SecurityException if the caller is not the owner of the given network.
4849 * @hide
4850 */
Remi NGUYEN VAN7d4530b2021-01-12 18:40:04 +09004851 @SystemApi(client = SystemApi.Client.MODULE_LIBRARIES)
Cody Kestingb5c7abd2020-04-15 12:33:28 -07004852 @RequiresPermission(anyOf = {android.Manifest.permission.MANAGE_TEST_NETWORKS,
4853 android.Manifest.permission.NETWORK_STACK})
4854 public void simulateDataStall(int detectionMethod, long timestampMillis,
4855 @NonNull Network network, @NonNull PersistableBundle extras) {
4856 try {
4857 mService.simulateDataStall(detectionMethod, timestampMillis, network, extras);
4858 } catch (RemoteException e) {
4859 e.rethrowFromSystemServer();
4860 }
4861 }
James Mattisdcea9fb2020-10-28 21:48:54 -07004862
Daniel Bright60f02ed2020-06-15 16:10:01 -07004863 @NonNull
4864 private final List<QosCallbackConnection> mQosCallbackConnections = new ArrayList<>();
4865
4866 /**
4867 * Registers a {@link QosSocketInfo} with an associated {@link QosCallback}. The callback will
4868 * receive available QoS events related to the {@link Network} and local ip + port
4869 * specified within socketInfo.
4870 * <p/>
4871 * The same {@link QosCallback} must be unregistered before being registered a second time,
4872 * otherwise {@link QosCallbackRegistrationException} is thrown.
4873 * <p/>
4874 * This API does not, in itself, require any permission if called with a network that is not
4875 * restricted. However, the underlying implementation currently only supports the IMS network,
4876 * which is always restricted. That means non-preinstalled callers can't possibly find this API
4877 * useful, because they'd never be called back on networks that they would have access to.
4878 *
4879 * @throws SecurityException if {@link QosSocketInfo#getNetwork()} is restricted and the app is
4880 * missing CONNECTIVITY_USE_RESTRICTED_NETWORKS permission.
4881 * @throws QosCallback.QosCallbackRegistrationException if qosCallback is already registered.
4882 * @throws RuntimeException if the app already has too many callbacks registered.
4883 *
4884 * Exceptions after the time of registration is passed through
4885 * {@link QosCallback#onError(QosCallbackException)}. see: {@link QosCallbackException}.
4886 *
4887 * @param socketInfo the socket information used to match QoS events
4888 * @param callback receives qos events that satisfy socketInfo
4889 * @param executor The executor on which the callback will be invoked. The provided
4890 * {@link Executor} must run callback sequentially, otherwise the order of
4891 * callbacks cannot be guaranteed.
4892 *
4893 * @hide
4894 */
4895 @SystemApi
4896 public void registerQosCallback(@NonNull final QosSocketInfo socketInfo,
4897 @NonNull final QosCallback callback,
4898 @CallbackExecutor @NonNull final Executor executor) {
4899 Objects.requireNonNull(socketInfo, "socketInfo must be non-null");
4900 Objects.requireNonNull(callback, "callback must be non-null");
4901 Objects.requireNonNull(executor, "executor must be non-null");
4902
4903 try {
4904 synchronized (mQosCallbackConnections) {
4905 if (getQosCallbackConnection(callback) == null) {
4906 final QosCallbackConnection connection =
4907 new QosCallbackConnection(this, callback, executor);
4908 mQosCallbackConnections.add(connection);
4909 mService.registerQosSocketCallback(socketInfo, connection);
4910 } else {
4911 Log.e(TAG, "registerQosCallback: Callback already registered");
4912 throw new QosCallbackRegistrationException();
4913 }
4914 }
4915 } catch (final RemoteException e) {
4916 Log.e(TAG, "registerQosCallback: Error while registering ", e);
4917
4918 // The same unregister method method is called for consistency even though nothing
4919 // will be sent to the ConnectivityService since the callback was never successfully
4920 // registered.
4921 unregisterQosCallback(callback);
4922 e.rethrowFromSystemServer();
4923 } catch (final ServiceSpecificException e) {
4924 Log.e(TAG, "registerQosCallback: Error while registering ", e);
4925 unregisterQosCallback(callback);
4926 throw convertServiceException(e);
4927 }
4928 }
4929
4930 /**
4931 * Unregisters the given {@link QosCallback}. The {@link QosCallback} will no longer receive
4932 * events once unregistered and can be registered a second time.
4933 * <p/>
4934 * If the {@link QosCallback} does not have an active registration, it is a no-op.
4935 *
4936 * @param callback the callback being unregistered
4937 *
4938 * @hide
4939 */
4940 @SystemApi
4941 public void unregisterQosCallback(@NonNull final QosCallback callback) {
4942 Objects.requireNonNull(callback, "The callback must be non-null");
4943 try {
4944 synchronized (mQosCallbackConnections) {
4945 final QosCallbackConnection connection = getQosCallbackConnection(callback);
4946 if (connection != null) {
4947 connection.stopReceivingMessages();
4948 mService.unregisterQosCallback(connection);
4949 mQosCallbackConnections.remove(connection);
4950 } else {
4951 Log.d(TAG, "unregisterQosCallback: Callback not registered");
4952 }
4953 }
4954 } catch (final RemoteException e) {
4955 Log.e(TAG, "unregisterQosCallback: Error while unregistering ", e);
4956 e.rethrowFromSystemServer();
4957 }
4958 }
4959
4960 /**
4961 * Gets the connection related to the callback.
4962 *
4963 * @param callback the callback to look up
4964 * @return the related connection
4965 */
4966 @Nullable
4967 private QosCallbackConnection getQosCallbackConnection(final QosCallback callback) {
4968 for (final QosCallbackConnection connection : mQosCallbackConnections) {
4969 // Checking by reference here is intentional
4970 if (connection.getCallback() == callback) {
4971 return connection;
4972 }
4973 }
4974 return null;
4975 }
Junyu Lai23568a42021-01-19 11:10:56 +00004976
4977 /**
Roshan Pius7992afd2020-12-22 15:10:42 -08004978 * Request a network to satisfy a set of {@link NetworkCapabilities}, but
Junyu Lai23568a42021-01-19 11:10:56 +00004979 * does not cause any networks to retain the NET_CAPABILITY_FOREGROUND capability. This can
4980 * be used to request that the system provide a network without causing the network to be
4981 * in the foreground.
4982 *
4983 * <p>This method will attempt to find the best network that matches the passed
4984 * {@link NetworkRequest}, and to bring up one that does if none currently satisfies the
4985 * criteria. The platform will evaluate which network is the best at its own discretion.
4986 * Throughput, latency, cost per byte, policy, user preference and other considerations
4987 * may be factored in the decision of what is considered the best network.
4988 *
4989 * <p>As long as this request is outstanding, the platform will try to maintain the best network
4990 * matching this request, while always attempting to match the request to a better network if
4991 * possible. If a better match is found, the platform will switch this request to the now-best
4992 * network and inform the app of the newly best network by invoking
4993 * {@link NetworkCallback#onAvailable(Network)} on the provided callback. Note that the platform
4994 * will not try to maintain any other network than the best one currently matching the request:
4995 * a network not matching any network request may be disconnected at any time.
4996 *
4997 * <p>For example, an application could use this method to obtain a connected cellular network
4998 * even if the device currently has a data connection over Ethernet. This may cause the cellular
4999 * radio to consume additional power. Or, an application could inform the system that it wants
5000 * a network supporting sending MMSes and have the system let it know about the currently best
5001 * MMS-supporting network through the provided {@link NetworkCallback}.
5002 *
5003 * <p>The status of the request can be followed by listening to the various callbacks described
5004 * in {@link NetworkCallback}. The {@link Network} object passed to the callback methods can be
5005 * used to direct traffic to the network (although accessing some networks may be subject to
5006 * holding specific permissions). Callers will learn about the specific characteristics of the
5007 * network through
5008 * {@link NetworkCallback#onCapabilitiesChanged(Network, NetworkCapabilities)} and
5009 * {@link NetworkCallback#onLinkPropertiesChanged(Network, LinkProperties)}. The methods of the
5010 * provided {@link NetworkCallback} will only be invoked due to changes in the best network
5011 * matching the request at any given time; therefore when a better network matching the request
5012 * becomes available, the {@link NetworkCallback#onAvailable(Network)} method is called
5013 * with the new network after which no further updates are given about the previously-best
5014 * network, unless it becomes the best again at some later time. All callbacks are invoked
5015 * in order on the same thread, which by default is a thread created by the framework running
5016 * in the app.
5017 *
5018 * <p>This{@link NetworkRequest} will live until released via
5019 * {@link #unregisterNetworkCallback(NetworkCallback)} or the calling application exits, at
5020 * which point the system may let go of the network at any time.
5021 *
5022 * <p>It is presently unsupported to request a network with mutable
5023 * {@link NetworkCapabilities} such as
5024 * {@link NetworkCapabilities#NET_CAPABILITY_VALIDATED} or
5025 * {@link NetworkCapabilities#NET_CAPABILITY_CAPTIVE_PORTAL}
5026 * as these {@code NetworkCapabilities} represent states that a particular
5027 * network may never attain, and whether a network will attain these states
5028 * is unknown prior to bringing up the network so the framework does not
5029 * know how to go about satisfying a request with these capabilities.
5030 *
5031 * <p>To avoid performance issues due to apps leaking callbacks, the system will limit the
5032 * number of outstanding requests to 100 per app (identified by their UID), shared with
5033 * all variants of this method, of {@link #registerNetworkCallback} as well as
5034 * {@link ConnectivityDiagnosticsManager#registerConnectivityDiagnosticsCallback}.
5035 * Requesting a network with this method will count toward this limit. If this limit is
5036 * exceeded, an exception will be thrown. To avoid hitting this issue and to conserve resources,
5037 * make sure to unregister the callbacks with
5038 * {@link #unregisterNetworkCallback(NetworkCallback)}.
5039 *
5040 * @param request {@link NetworkRequest} describing this request.
5041 * @param handler {@link Handler} to specify the thread upon which the callback will be invoked.
5042 * If null, the callback is invoked on the default internal Handler.
5043 * @param networkCallback The {@link NetworkCallback} to be utilized for this request. Note
5044 * the callback must not be shared - it uniquely specifies this request.
5045 * @throws IllegalArgumentException if {@code request} contains invalid network capabilities.
5046 * @throws SecurityException if missing the appropriate permissions.
5047 * @throws RuntimeException if the app already has too many callbacks registered.
5048 *
5049 * @hide
5050 */
5051 @SystemApi(client = MODULE_LIBRARIES)
5052 @SuppressLint("ExecutorRegistration")
5053 @RequiresPermission(anyOf = {
5054 android.Manifest.permission.NETWORK_SETTINGS,
5055 android.Manifest.permission.NETWORK_STACK,
5056 NetworkStack.PERMISSION_MAINLINE_NETWORK_STACK
5057 })
5058 public void requestBackgroundNetwork(@NonNull NetworkRequest request,
junyulaif4bc12f2021-03-09 20:49:48 +08005059 @NonNull Handler handler, @NonNull NetworkCallback networkCallback) {
Junyu Lai23568a42021-01-19 11:10:56 +00005060 final NetworkCapabilities nc = request.networkCapabilities;
5061 sendRequestForNetwork(nc, networkCallback, 0, BACKGROUND_REQUEST,
junyulaif4bc12f2021-03-09 20:49:48 +08005062 TYPE_NONE, new CallbackHandler(handler));
Junyu Lai23568a42021-01-19 11:10:56 +00005063 }
James Mattis6e6fabf2021-01-10 14:24:24 -08005064
5065 /**
5066 * Listener for {@link #setOemNetworkPreference(OemNetworkPreferences, Executor,
5067 * OnSetOemNetworkPreferenceListener)}.
James Mattis7a253542021-01-26 16:23:52 -08005068 * @hide
James Mattis6e6fabf2021-01-10 14:24:24 -08005069 */
James Mattis7a253542021-01-26 16:23:52 -08005070 @SystemApi
5071 public interface OnSetOemNetworkPreferenceListener {
James Mattis6e6fabf2021-01-10 14:24:24 -08005072 /**
5073 * Called when setOemNetworkPreference() successfully completes.
5074 */
5075 void onComplete();
5076 }
5077
5078 /**
5079 * Used by automotive devices to set the network preferences used to direct traffic at an
5080 * application level as per the given OemNetworkPreferences. An example use-case would be an
5081 * automotive OEM wanting to provide connectivity for applications critical to the usage of a
5082 * vehicle via a particular network.
5083 *
5084 * Calling this will overwrite the existing preference.
5085 *
5086 * @param preference {@link OemNetworkPreferences} The application network preference to be set.
5087 * @param executor the executor on which listener will be invoked.
5088 * @param listener {@link OnSetOemNetworkPreferenceListener} optional listener used to
5089 * communicate completion of setOemNetworkPreference(). This will only be
5090 * called once upon successful completion of setOemNetworkPreference().
5091 * @throws IllegalArgumentException if {@code preference} contains invalid preference values.
5092 * @throws SecurityException if missing the appropriate permissions.
5093 * @throws UnsupportedOperationException if called on a non-automotive device.
James Mattis7a253542021-01-26 16:23:52 -08005094 * @hide
James Mattis6e6fabf2021-01-10 14:24:24 -08005095 */
James Mattis7a253542021-01-26 16:23:52 -08005096 @SystemApi
James Mattis981865c2021-01-26 14:05:36 -08005097 @RequiresPermission(android.Manifest.permission.CONTROL_OEM_PAID_NETWORK_PREFERENCE)
James Mattis7a253542021-01-26 16:23:52 -08005098 public void setOemNetworkPreference(@NonNull final OemNetworkPreferences preference,
James Mattis6e6fabf2021-01-10 14:24:24 -08005099 @Nullable @CallbackExecutor final Executor executor,
5100 @Nullable final OnSetOemNetworkPreferenceListener listener) {
5101 Objects.requireNonNull(preference, "OemNetworkPreferences must be non-null");
5102 if (null != listener) {
5103 Objects.requireNonNull(executor, "Executor must be non-null");
5104 }
5105 final IOnSetOemNetworkPreferenceListener listenerInternal = listener == null ? null :
5106 new IOnSetOemNetworkPreferenceListener.Stub() {
5107 @Override
5108 public void onComplete() {
5109 executor.execute(listener::onComplete);
5110 }
5111 };
5112
5113 try {
5114 mService.setOemNetworkPreference(preference, listenerInternal);
5115 } catch (RemoteException e) {
5116 Log.e(TAG, "setOemNetworkPreference() failed for preference: " + preference.toString());
5117 throw e.rethrowFromSystemServer();
5118 }
5119 }
lucaslin1a6095c2021-03-12 00:46:33 +08005120
5121 // The first network ID of IPSec tunnel interface.
5122 private static final int TUN_INTF_NETID_START = 0xFC00;
5123 // The network ID range of IPSec tunnel interface.
5124 private static final int TUN_INTF_NETID_RANGE = 0x0400;
5125
5126 /**
5127 * Get the network ID range reserved for IPSec tunnel interfaces.
5128 *
5129 * @return A Range which indicates the network ID range of IPSec tunnel interface.
5130 * @hide
5131 */
5132 @SystemApi(client = MODULE_LIBRARIES)
5133 @NonNull
5134 public static Range<Integer> getIpSecNetIdRange() {
5135 return new Range(TUN_INTF_NETID_START, TUN_INTF_NETID_START + TUN_INTF_NETID_RANGE - 1);
5136 }
The Android Open Source Project28527d22009-03-03 19:31:44 -08005137}